cancel
Showing results for 
Search instead for 
Did you mean: 

High speed GPIO switching

KKIM.6
Senior II

Hi. Everyone.

I'm trying to control external MUX using a GPIO pin in the STM32WB55 microcontroller.

Because of a limited number of PWM pins, I'm trying to use a GPIO pin for continuous regular switching.

My question is that even though the company said a maximum of 45 MHz (or 9 ns) is possible for the GPIO pin my code does not work properly (mux doesn't switch well without a 1 ms delay).

The mux speed is a possible maximum of 100 MHz. So, I do not doubt it's performance but it may be due to the too fast swithing of GPIO pin.

I need at least 1 us (1 MHz) switching speed. 

So, I wonder how can I give 1 us delay to the GPIO pin properly using IDE code?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
AScha.3
Chief III

1. the real switching speed on I/O depends on: 

- core speed + bus speed (to gpios )

- how you access port (HAL - slow, about 1us ; direct register write - fast, about 16ns posible )

- c compiler-> optimizer setting (-O2 or -Ofast )

to test it, make a small loop and check pin with DSO. then you see, what you get with your settings.

 

  while (1)
  {
	  // HAL_GPIO_WritePin(GPIOG, GPIO_PIN_4, GPIO_PIN_RESET);
	  GPIOG->BSRR = GPIO_PIN_4<<16;
	 //  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_4, GPIO_PIN_SET);
	  GPIOG->BSRR = GPIO_PIN_4;

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

 

 

- try HAL and  direct access (just un-/comment )

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
STea
ST Employee

Hello @KKIM.6 ,

to insert a delay you need to use the systick to count down to your desired time this is done in HAL_Delay() function .

You can write your own delay function to wok in us as shown bellow :

void delay_us(uint32_t us)

{

uint32_t ticks = (HAL_RCC_GetHCLKFreq() / 1000000) * us; HAL_Delay(ticks);

}

void Systick_Config(void) {

/* Configure the SysTick timer to overflow every 1 us */

HAL_SYSTICK_Config(SystemCoreClock / 1000000);

/* Configure the SysTick timer clock source */

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

}

BR

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
AScha.3
Chief III

1. the real switching speed on I/O depends on: 

- core speed + bus speed (to gpios )

- how you access port (HAL - slow, about 1us ; direct register write - fast, about 16ns posible )

- c compiler-> optimizer setting (-O2 or -Ofast )

to test it, make a small loop and check pin with DSO. then you see, what you get with your settings.

 

  while (1)
  {
	  // HAL_GPIO_WritePin(GPIOG, GPIO_PIN_4, GPIO_PIN_RESET);
	  GPIOG->BSRR = GPIO_PIN_4<<16;
	 //  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_4, GPIO_PIN_SET);
	  GPIOG->BSRR = GPIO_PIN_4;

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

 

 

- try HAL and  direct access (just un-/comment )

If you feel a post has answered your question, please click "Accept as Solution".

How is that ever going to work??

The MCU is going to saturate completely before it gets anywhere near 1MHz interrupt rate.

Need to delta a free running counter in a spin loops.

For GPIO patterns pace GPIO+DMA with a TIM trigger at the desired pattern rate.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..