cancel
Showing results for 
Search instead for 
Did you mean: 

usart doesn't work on stm32 Nucleo F401

lamard
Associate II
Posted on September 28, 2014 at 16:12

Hi friends,

I have nio clue why I can't send anu data to usart. I use a stm32 Nucleo F401 on the usart 2 and absolutly no signal on GPIOA Pin 2 and 3.

If you could help me.

Thanks a lot

My source code is very simple :

UART_HandleTypeDef UartHandle;

void HAL_UART_MspInit(UART_HandleTypeDef *huart)

{

    GPIO_InitTypeDef GPIO_InitStruct;

    __GPIOA_CLK_ENABLE();

    __USART2_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_2;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_3;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

void init_usart(void)

{

    UartHandle.Instance = USART2;

    UartHandle.Init.BaudRate = 9600;

    UartHandle.Init.WordLength = UART_WORDLENGTH_8B;

    UartHandle.Init.StopBits = UART_STOPBITS_1;

    UartHandle.Init.Parity = UART_PARITY_NONE;

    UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;

    UartHandle.Init.Mode = UART_MODE_TX_RX;

    HAL_UART_Init(&UartHandle);

    __HAL_UART_ENABLE(&UartHandle);

}

int main(int argc, char* argv[])

{

    char str[5] = { ''tests'' };

    init_usart();

    HAL_UART_Transmit(&UartHandle, (uint8_t*) str, 1, 0XFFFF);

    HAL_UART_Transmit(&UartHandle, (uint8_t*) str, 1, 0XFFFF);

    while (1)

    {

        //HAL_UART_Transmit(&UartHandle, &ch, 1, 0XFFFF);

    }

}

#nucleo-f401
6 REPLIES 6
Posted on September 28, 2014 at 17:25

Can't help you will HAL, this is how I did it with the Standard Peripheral Library.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/USART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=353]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FUSART%20example%20code%20for%20Nucleo%20F401RE&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=353
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lamard
Associate II
Posted on September 28, 2014 at 17:52

Thanks a lot for your help. It is quite the same in using HAL. Could you tell me what ''USART_Cmd'' does. I problably forget something to start USART.

Once again thanks

Posted on September 28, 2014 at 18:03

Equivalent to  __HAL_UART_ENABLE

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 29, 2014 at 12:01

Hi denis.lamard,

After analysing your code I've the following suggestions :

  • Try to change the GPIO_InitStruct.Pull  = GPIO_NOPULL to GPIO_PULLUP
  • Remove the __HAL_UART_ENABLE(&UartHandle); macro, because it's already called within the HAL_UART_Init() API.

PS: To have more insight into USART transmission please check the following example: \Projects\STM32F401RE-Nucleo\Examples\UART within the

http://www.st.com/web/en/catalog/tools/PF259243#

package.

Regards,

Heisenberg.

lamard
Associate II
Posted on September 29, 2014 at 15:43

Thanks for those tips. I'll try as soon as possible. I'm beginning to understand how ADC works. I need to use it to capture data from a ultrasonic sensor.

Best regards

Posted on September 29, 2014 at 16:03

It seems also that you've missed the call of the following functions under your main() function:

  • HAL_Init(); to initialize the HAL library
  • SystemClock_Config(); to configure the system clock 
OK, let's try and keep us informed about your finding.

Regards,

Heisenberg.