cancel
Showing results for 
Search instead for 
Did you mean: 

USART2 Init STM32F101

maurosmartins
Associate II
Posted on February 20, 2017 at 22:39

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.

#stm32f101
2 REPLIES 2
Posted on February 20, 2017 at 23:15

Have you confirmed the USART2 registers in a debugger? Is the clock enabled?

Do you have a schematic for this hardware? Can you show the working Mikroe C code?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
maurosmartins
Associate II
Posted on February 20, 2017 at 23:22

Hello Clive One, 

thank you for your reply, I haven't check the registers with the debugger, I assumed they were filled correctly after my initialization.

The clock is enabled for PORTA, AFIO, USART2 must I enable the clock for something else?

the Mikroe code is simply the following (only the M letter being sent):

void main() {

    while(1){

        UART2_Init(115200);

       

UART2_Write('M');

       

Delay_ms(1000); /* One second pause */

   

}

}

Best regards, Mauro.