cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 how to change pins from USART to GPIO and back to USART again

MBoth
Associate II
// Init the GPIO
GPIO_InitTypeDef GPIO_InitStruct = {0};
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
// Set the pin low and wait
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2 , GPIO_PIN_RESET);
DWT_Delay_us (100); 
 
// set the pin high and wait	
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2 , GPIO_PIN_SET);
DWT_Delay_us (12);    														
 
// turn off GPIO - should go back to USART
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);
 
// Send DMX Data
RS485_TX(9);									// Send the start plus 8 data bytes

Hello. I am trying to setup a USART transmitter which has some specific timing requirements for the signal lines before transmitting the data (it's for a DMX lighting controller). I need to have the TX pin idle high, then go low for 100uS, then high again for 12uS, and then send the serial data after that.

I have no trouble either sending the serial data, or doing the high/low timing beforehand. But I can't seem to find a way to do both. The USART is initially setup (Using STMCube32 and Freertos), and works fine, but not if I init the pins as GPIO do the high.low timing then deinit the pins again. I read that I shouldn't need to deinit / reinit the USART itself - which I have tried too.

Any ideas? Using STM32F401VCT6. 

Cheers - Matt

5 REPLIES 5

Would probably just switch state in GPIOA->MODER, check RM0090

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

Thanks Clive. I've had a quick look but I don't have the hardware with me right now to check. Will try a bit later today. Cheers.

Hi Clive. Didn't work at first, but more reading and more suck it and see trying and have it all working now. Thanks for the guidance with the MODER command (never seen that register before - always been handled by STMCube32 setup). Cheers again - Matt

msilveus
Associate

Hi Matt,

I'm looking to do the same thing. Can you provide any guidance about how you accomplished the solution.

Thanks,

Mike

Don't hijack others threads. Start your own, stating the STM32 and hardware you are using, and the problem you came across. You can link to this thread if you think it's relevant.

JW