2023-11-15 06:52 PM
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?
Solved! Go to Solution.
2023-11-20 01:35 AM - edited 2023-11-20 01:36 AM
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 )
2023-11-20 12:45 AM
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
2023-11-20 01:35 AM - edited 2023-11-20 01:36 AM
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 )
2023-11-20 03:35 AM
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.