AnsweredAssumed Answered

USART2 Init STM32F101

Question asked by maurosmartins on Feb 20, 2017
Latest reply on Feb 20, 2017 by Clive One

Hello all,

I'm trying to configure USART on a STM32F101RB micrcontroller with Atollic True Studio.

the uC clock is configured as external clock (8Mhz) divided by 2 and pll'ed by 9 to a total 36Mhz.

I'm using the HAL to initialize the microcontroller, but so far I hadn't had success. I know the hw is working ok because setting that port with mikroC... it works.

normal usart initialization

 

static void MX_USART2_UART_Init(void){

huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}

Clock for porta has already been enabled by calling:

__HAL_RCC_GPIOA_CLK_ENABLE();

and finally the Hw initialization is as follows:

 

 

void HAL_UART_MspInit(UART_HandleTypeDef* huart){

GPIO_InitTypeDef GPIO_InitStruct;
if(huart->Instance==USART2){
__HAL_RCC_AFIO_CLK_ENABLE();

AFIO->MAPR &= (~AFIO_MAPR_USART2_REMAP_Msk);

__HAL_RCC_USART2_CLK_ENABLE();

/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

}

with this I've tryed to send the eternal "Hello World" as follows:

while(1){

char msg[] = "Hello World!\n\r";
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 0xFFFF);

}

but no success, I've even checked if alternative pins were enabled, but, they were not.

 

any ideas of what can be wrong?

 

thanks in advanced,
Best regards Mauro.

Outcomes