cancel
Showing results for 
Search instead for 
Did you mean: 

change pin status UART to GPIO

ismail fatih iltar
Associate II
Posted on July 05, 2018 at 08:10

Hi everyone, in my system (freertos based) i need some low pulse before sending uart datas. So i must change tx pin to gpio and send low pulse along appx. 100uS and after that i must rechange it to uart fastly. How can i do this dynamically and time efficently. I need an advice for it. Thanks.

#freertos #uart #gpio
5 REPLIES 5
Tilen MAJERLE
ST Employee
Posted on July 05, 2018 at 09:07

Hello

ILTAR.Ismail_Fatih

,

take a look in reference manual for GPIO_MODER register. You may change between output and alternate function mode and configure pin state (when in output mode) using GPIO_ODR register.

Posted on July 05, 2018 at 09:57

I just tried something like that but uart tx pin always stays at reset state. ıt doesnt send any datas.

#define tim2_wait_usec(X) \
 TIM2->PSC = 71; \
 TIM2->ARR = X; \
 TIM2->CNT = 0; \
 TIM2->CR1 |= TIM_CR1_CEN; \
 while (TIM2->CNT != TIM2->ARR) { } \
 TIM2->CR1 &= ~TIM_CR1_CEN
void MX_GPIO_uart1tx_Init(void)
{
 GPIO_InitTypeDef GPIO_InitStruct;
 GPIO_InitStruct.Pin = GPIO_PIN_9;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_PULLUP;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void MX_GPIO_uart1tx_deInit(void)
{
 GPIO_InitTypeDef GPIO_InitStruct;
 GPIO_InitStruct.Pin = GPIO_PIN_9;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_PULLUP;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void send(uint8_t* buffer, uint16_t size)
{
 MX_GPIO_uart1tx_Init();
 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
 tim2_wait_usec(100);
 MX_GPIO_uart1tx_deInit();
 MX_USART1_UART_Init();
 HAL_UART_Transmit_IT(&huart1, buffer,size+1);
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Posted on July 05, 2018 at 10:52

You have to fill out all fields of the

GPIO_InitTypeDef 

struct.

JW

Posted on July 05, 2018 at 11:05

i simplified codes and filled out all fields (just speed neccessary). and it still hangs at low states

void MX_GPIO_uart1tx_Init(void)
{
 GPIO_InitTypeDef GPIO_InitStruct;
 GPIO_InitStruct.Pin = GPIO_PIN_9;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_PULLDOWN;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF7_USART1; //not used
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void send(..)
{
 MX_GPIO_uart1tx_Init();
 osDelay(1);
 MX_USART1_UART_Init(); //cubemx generated default uart init
 HAL_UART_Transmit_IT(&huart1, buffer,size+1);
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Posted on July 05, 2018 at 15:36

i have done with below code. ı had to add USART deinit before gpio init but i dont know the reason exactly. And this method is not fast and efficent in my opinion. Still need a best way

void send(..)
{
 HAL_UART_DeInit(&huart1);
 MX_GPIO_uart1tx_Init();
 tim2_wait_usec(100);
 MX_USART1_UART_Init();
 HAL_UART_Transmit_IT(&huart1, buffer,size+1);
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?