|

|  How to configure GPIOs for low-power modes in embedded C on STM32?

How to configure GPIOs for low-power modes in embedded C on STM32?

October 14, 2024

Optimize STM32 GPIOs for low power in embedded C. Tailored guidance for firmware developers, with step-by-step instructions to enhance energy efficiency.

How to configure GPIOs for low-power modes in embedded C on STM32?

 

Introduction to GPIO Configuration for Low-Power Modes

 

Configuring GPIOs (General Purpose Input/Output) on STM32 microcontrollers for low-power modes is crucial to optimize power consumption, especially in battery-powered applications. The STM32 microcontrollers offer various low-power modes such as Sleep, Stop, and Standby, which affect how GPIOs should behave. In these modes, proper configuration of GPIO pins can significantly reduce leakage currents, enabling longer battery life.

 

Identify Required GPIO Behavior

 

Before configuring, identify the required behavior of each GPIO in low-power mode. Typical configurations include:

  • Input (Analog or Digital)
  • Output (Push-Pull or Open-Drain)
  • Pull-Up, Pull-Down, or No Pull
  • Analog mode for lowest power consumption

 

Configure GPIOs for Low-Power Modes

 

To configure GPIOs in STM32, follow these steps:

  • Enable Clocks for GPIO Ports:
    Ensure the peripheral clock for the GPIO is enabled. This is crucial during initialization, but generally should be switched off in low-power modes if the GPIO is not in use to save power.

    ```c
    // Enable the GPIOA clock
    __HAL_RCC_GPIOA_CLK_ENABLE();
    ```

  • Set GPIO Mode:
    The mode must be set correct for application needs. Generally, you should set unused pins to Analog mode to save power:

    ```c
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // Configure unused pins as Analog to reduce power consumption
    GPIO_InitStruct.Pin = GPIO_PIN_All;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    ```

  • Configure Output Type and Speed:

If a GPIO is set as output, configure it to push-pull mode with low speed for power savings.

```c
// Example for setting a pin as low-speed push-pull output
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
```

  • Set Pull Configuration:
    Use internal pull-up or pull-down resistors to define stable logic levels for GPIOs acting as inputs.

    ```c
    // Example setting a pin with pull-up
    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    ```

 

Handling GPIOs in Different Low-Power Modes

 

STM32 microcontrollers have different low-power modes, and GPIO configuration should be adapted accordingly.

  • Sleep Mode:
    Sleep mode is a moderate power-saving mode where the CPU is stopped, but peripherals can still be active if their clocks are enabled. Consider setting the GPIO state before entering sleep mode.

  • Stop Mode:
    In Stop mode, all clocks in the 1.2V domain are stopped, reducing power considerably. Ensure GPIOs that need to retain state are appropriately configured.

  • Standby Mode:

This is the lowest power mode with the most restrictions. It's essential to configure GPIOs precisely to reduce power consumption to the minimum by putting unused pins in an analog state.

 

Practical Example of Low-Power Configuration

 

Here's a practical example demonstrating the configuration of GPIOs for low-power modes using HAL in STM32:

void ConfigureGPIOForLowPower(void) {
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // Enable clocks for GPIO ports A, B, C etc.
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOC_CLK_ENABLE();
    
    // Configure all pins on port A as Analog with no pull-up/pull-down
    GPIO_InitStruct.Pin = GPIO_PIN_All;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // Apply similar configuration to other ports as needed
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    // Disable GPIO clocks to save power
    __HAL_RCC_GPIOA_CLK_DISABLE();
    __HAL_RCC_GPIOB_CLK_DISABLE();
    __HAL_RCC_GPIOC_CLK_DISABLE();
}

 

Clearly configuring GPIOs for low power operation in STM32 devices is an intricate but rewarding task that extends standby time and optimizes energy efficiency in embedded systems. Strategic planning and configuration adjustments are necessary to achieve the desired balance between functionality and power consumption.

Pre-order Friend AI Necklace

Limited Beta: Claim Your Dev Kit and Start Building Today

Instant transcription

Access hundreds of community apps

Sync seamlessly on iOS & Android

Order Now

Turn Ideas Into Apps & Earn Big

Build apps for the AI wearable revolution, tap into a $100K+ bounty pool, and get noticed by top companies. Whether for fun or productivity, create unique use cases, integrate with real-time transcription, and join a thriving dev community.

Get Developer Kit Now

OMI AI PLATFORM
Remember Every Moment,
Talk to AI and Get Feedback

Omi Necklace

The #1 Open Source AI necklace: Experiment with how you capture and manage conversations.

Build and test with your own Omi Dev Kit 2.

Omi App

Fully Open-Source AI wearable app: build and use reminders, meeting summaries, task suggestions and more. All in one simple app.

Github →

Join the #1 open-source AI wearable community

Build faster and better with 3900+ community members on Omi Discord

Participate in hackathons to expand the Omi platform and win prizes

Participate in hackathons to expand the Omi platform and win prizes

Get cash bounties, free Omi devices and priority access by taking part in community activities

Join our Discord → 

OMI NECKLACE + OMI APP
First & only open-source AI wearable platform

a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded

OMI NECKLACE: DEV KIT
Order your Omi Dev Kit 2 now and create your use cases

Omi 開発キット 2

無限のカスタマイズ

OMI 開発キット 2

$69.99

Omi AIネックレスで会話を音声化、文字起こし、要約。アクションリストやパーソナライズされたフィードバックを提供し、あなたの第二の脳となって考えや感情を語り合います。iOSとAndroidでご利用いただけます。

  • リアルタイムの会話の書き起こしと処理。
  • 行動項目、要約、思い出
  • Omi ペルソナと会話を活用できる何千ものコミュニティ アプリ

もっと詳しく知る

Omi Dev Kit 2: 新しいレベルのビルド

主な仕様

OMI 開発キット

OMI 開発キット 2

マイクロフォン

はい

はい

バッテリー

4日間(250mAH)

2日間(250mAH)

オンボードメモリ(携帯電話なしで動作)

いいえ

はい

スピーカー

いいえ

はい

プログラム可能なボタン

いいえ

はい

配送予定日

-

1週間

人々が言うこと

「記憶を助ける、

コミュニケーション

ビジネス/人生のパートナーと、

アイデアを捉え、解決する

聴覚チャレンジ」

ネイサン・サッズ

「このデバイスがあればいいのに

去年の夏

記録する

「会話」

クリスY.

「ADHDを治して

私を助けてくれた

整頓された。"

デビッド・ナイ

OMIネックレス:開発キット
脳を次のレベルへ

最新ニュース
フォローして最新情報をいち早く入手しましょう

最新ニュース
フォローして最新情報をいち早く入手しましょう

thought to action.

Based Hardware Inc.
81 Lafayette St, San Francisco, CA 94103
team@basedhardware.com / help@omi.me

Company

Careers

Invest

Privacy

Events

Manifesto

Compliance

Products

Omi

Wrist Band

Omi Apps

omi Dev Kit

omiGPT

Personas

Omi Glass

Resources

Apps

Bounties

Affiliate

Docs

GitHub

Help Center

Feedback

Enterprise

Ambassadors

Resellers

© 2025 Based Hardware. All rights reserved.