cancel
Showing results for 
Search instead for 
Did you mean: 

USART doesn't work Please help me

yaghiyah
Associate II
Posted on August 31, 2014 at 23:29

Hi Everyone im new around here.

I own a Discovery kit for STM32F407/417and I have a problem getting the usart to work, ive used STM32CubeMX to genarate all the initialization files along with FreeRTOS (which works well) I'm trying to send USART txdata in blocking mode however its not working, scope shows no data packets, computer terminal shows nothing. vie stepped carefully through the program and no error occurs, I don't get any HAL errors during low level initialization (hal_okay) and when I send data using see blew, I still get no errors, so what do I do now ? I have attached the full IAR project, its a simple skeleton project with freertos, any help will be greatly appreciated as I just started out.

HAL_UART_Transmit( &huart2, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000 ) != HAL_OK

demo application project files. www.soundsreal.co.za/uploads/app.rar #stm32f4 #stm32cubemx
9 REPLIES 9
yaghiyah
Associate II
Posted on September 01, 2014 at 12:35

Can somebody please help me with my issue, would really appreciate it.

yaghiyah
Associate II
Posted on September 02, 2014 at 00:19

bump.

Posted on September 02, 2014 at 02:19

Is it really necessary to bump a thread that's already on the front page of the forum? It's not as if a magic army of elves are here to address everyone's problems.

I'll support Standard Library implementations, for HAL/Cube you'll have to rely on those here using it.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
malcolm2
Associate II
Posted on September 02, 2014 at 11:57

Hi

Quick look through your setup code and I cannot see where you have turned on the clock for uarts.  Also uarts do not appear to be configured as alternate function

Add the following block in your MX_GPIO_INIT file to activate USART2.

 

     __GPIOD_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

Do the same for USART3.

This is an example of the dangers involved in using tools like STM32CubeMX without fully understanding what it is generating.

The standard files generated all have a stupid call to an error handler if a Hal call fails but this handler is blank.  Much better, in my opinion, to handle these in the same code block when they occur.

malcolm2
Associate II
Posted on September 02, 2014 at 12:00

Opps sorry mistake clock enable command is

    __USART2_CLK_ENABLE();

yaghiyah
Associate II
Posted on September 02, 2014 at 12:17

You not really adding any value by giving such answers, anyway I get your point just so you know.

yaghiyah
Associate II
Posted on September 02, 2014 at 12:26

Thanks rook for looking into this appreciate it.

From what I discovered if you look at line 250 file ''stm32f4xx_hal_usart.c'' there is a call to the setup of the low level resources function ''

HAL_UART_MspInit''

, all the low level UARTresources is taken care of see file ''stm32f4xx_hal_msp.c''

if(huart->State == HAL_UART_STATE_RESET)

{

/* Init the low level hardware */

HAL_UART_MspInit(huart);

}

void HAL_UART_MspInit(UART_HandleTypeDef* huart)

{

GPIO_InitTypeDef GPIO_InitStruct;

if(huart->Instance==USART2)

{

/* Peripheral clock enable */

__USART2_CLK_ENABLE();

/**USART2 GPIO Configuration

PA2 ------> USART2_TX

PA3 ------> USART2_RX

*/

GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

else if(huart->Instance==USART3)

{

/* Peripheral clock enable */

__USART3_CLK_ENABLE();

/**USART3 GPIO Configuration

PB11 ------> USART3_RX

PD8 ------> USART3_TX

*/

GPIO_InitStruct.Pin = GPIO_PIN_11;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_8;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

}

}

void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)

{

if(huart->Instance==USART2)

{

/* Peripheral clock disable */

__USART2_CLK_DISABLE();

/**USART2 GPIO Configuration

PA2 ------> USART2_TX

PA3 ------> USART2_RX

*/

HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

}

else if(huart->Instance==USART3)

{

/* Peripheral clock disable */

__USART3_CLK_DISABLE();

/**USART3 GPIO Configuration

PB11 ------> USART3_RX

PD8 ------> USART3_TX

*/

HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11);

HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8);

}

}

As you can see the clock is enabled.

__USART2_CLK_ENABLE();

________________

Attachments :

uart_hal_setup.PNG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzOd&d=%2Fa%2F0X0000000bLB%2FZNXqQQylP9VA8MGQZc7WIjFOufiWkcO8ahjHRL51iCw&asPdf=false
Osto
Senior
Posted on September 07, 2014 at 15:31

Hi,

You can very fast verify if the U(S)ART system is configured correctly or not.

In Cube, config all serial parameters like baud, data, parity,....

Then compile and run with your debugger until end of the initialisation process in main.c.

Then write with the debugger tools a value into the DR register of the U(S)ART. If you get characters out of it, then you can go on with your code. If you dont get any charachters out of your cpu with debugger then you will get also nothing with your code.

Take care that this HAL implentation is very new and has still some bugs and design problems.

Posted on September 09, 2014 at 10:20

Hi,

Please note that the GPIO pull up should be enabled on UART I/O: a high state is required on USART GPIO pins to be able to generate/detect the start bit correctly. You've just to update manually theHAL_UART_MspInit() within the stm32f4xx_hal_msp.c file as following:

/**USART2 GPIO Configuration 
PA2 ------> USART2_TX
PA3 ------> USART2_RX 
*/
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP; // GPIO_NOPULL
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

Or re-generate your code using STM32CubeMX, after changing the USART2 GPIO Settings (GPIO Pull-up/Pull-down) under the Configuration - Connectivity tab. Regards.