GPIO Setting for Different Speed

Preface

We were operating the SPI, but strange phenomena occurred when the SPI speed exceeded 10MHz and the CPOL/CPHA became mismatched. After researching various methods, we found the trick was in the GPIO settings. Here, we provide detailed instructions for setting the GPIO SPEED SETTING to avoid such issues, which is applicable not only to SPI but also to all communication protocols.

 STM32 GPIO Ports 

On STM32, the GPIO structure is shown in the following diagram:

  1. GPIO_Mode_AIN: Analog input mode
  2. GPIO_Mode_IN_FLOATING: Input floating mode
  3. GPIO_Mode_IPD: Input pull-down mode
  4. GPIO_Mode_IPU: Input pull-up mode
  5. GPIO_Mode_Out_OD: General-purpose output open-drain mode
  6. GPIO_Mode_Out_PP: General-purpose output push-pull mode
  7. GPIO_Mode_AF_OD: Alternate function output open-drain mode
  8. GPIO_Mode_AF_PP: Alternate function output push-pull mode

 STM32 GPIO Speed

STM32CubeMX configures GPIO output pins with the GPIO_InitStruct.Speed option.

  GPIO_InitStruct.Pin = GPIO_PIN_5;	
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;	
  GPIO_InitStruct.Pull = GPIO_NOPULL;	
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;	
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

Different MCU models may have different speed options, with some having 3 and others having 4. The speed options are typically defined in the xxx_gpio.h file.

#define GPIO_Speed_2MHz  GPIO_Speed_Level_1   /*!< I/O output speed: Low 2 MHz  */	
#define GPIO_Speed_10MHz GPIO_Speed_Level_2   /*!< I/O output speed: Medium 10 MHz */	
#define GPIO_Speed_50MHz GPIO_Speed_Level_3   /*!< I/O output speed: High 50 MHz */
#define  GPIO_SPEED_FREQ_LOW        (0x00000000u)  /*!< Low speed       */	
#define  GPIO_SPEED_FREQ_MEDIUM     (0x00000001u)  /*!< Medium speed    */	
#define  GPIO_SPEED_FREQ_HIGH       (0x00000002u)  /*!< High speed      */	
#define  GPIO_SPEED_FREQ_VERY_HIGH  (0x00000003u)  /*!< Very high speed */

When configuring with STM32CubeMX, the default is set to the lowest speed. Therefore, if a higher transfer speed is required, the settings must be adjusted accordingly.

PS:If the output speed and configuration speed do not match, the waveform will appear abnormal. The difference between high and low speeds is shown below.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart