cancel
Showing results for 
Search instead for 
Did you mean: 

USART1 doesn't work

Arnas Matiukas
Associate II
Posted on February 11, 2018 at 15:00

Hello everyone. I have encountered problem with USART1 on STM32F446RE. It seems it doesn't work for me. I have used multiple times USART2 and it works as it should, but USART1 with same configurations just refuse to work. What could be the problem?

I use STM32CubeMX v4.24.0.

UART_HandleTypeDef huart1;

UART_HandleTypeDef huart2;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART1_UART_Init(void);

static void MX_USART2_UART_Init(void);

int main(void)

{

   HAL_Init();

   SystemClock_Config();

   MX_GPIO_Init();

   MX_USART1_UART_Init();

   MX_USART2_UART_Init();

   while (1)

   {

      HAL_UART_Transmit_IT(&huart1, (uint8_t *)'Hello with USART1\r\n', strlen('Hello with USART1\r\n'));

      HAL_Delay(1000);

      HAL_UART_Transmit_IT(&huart2, (uint8_t *)'Hello with USART2\r\n', strlen('Hello with USART2\r\n'));

   }

}

static void MX_USART1_UART_Init(void)

{

   huart1.Instance = USART1;

   huart1.Init.BaudRate = 115200;

   huart1.Init.WordLength = UART_WORDLENGTH_8B;

   huart1.Init.StopBits = UART_STOPBITS_1;

   huart1.Init.Parity = UART_PARITY_NONE;

   huart1.Init.Mode = UART_MODE_TX_RX;

   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

   huart1.Init.OverSampling = UART_OVERSAMPLING_16;

   if (HAL_UART_Init(&huart1) != HAL_OK)

   {

      _Error_Handler(__FILE__, __LINE__);

   }

}

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(__FILE__, __LINE__);

   }

}

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on February 11, 2018 at 17:19

Not sure I understand the question.

The ST-LINK VCP is connected to PA2/PA3 (USART2), you can't create imaginary connectivity. Review schematic.

http://www.st.com/content/ccc/resource/technical/document/user_manual/98/2e/fa/4b/e0/82/43/b7/DM00105823.pdf/files/DM00105823.pdf/jcr:content/translations/en.DM00105823.pdf

 
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

10 REPLIES 10
Posted on February 11, 2018 at 15:04

You should be looking at the MSP code that enables the clocks and pins.

stm32f4xx_hal_msp.c ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 11, 2018 at 15:41

 ,

 ,

This is the code of ,

stm32f4xx_hal_msp.c , . Maybe you have some insights what could be wrong?

♯ include 'stm32f4xx_hal.h'

extern void _Error_Handler(char *, int),

void HAL_MspInit(void)

 ,

{

 , , ,HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4),

 ,

 , , ,HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0),

 ,

 , , ,HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0),

 ,

 , , ,HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0),

 ,

 , , ,HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0),

 ,

 , , ,HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0),

 ,

 , , ,HAL_NVIC_SetPriority(PendSV_IRQn, 0, 0),

 ,

 , , ,HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0),

 ,

}

void HAL_UART_MspInit(UART_HandleTypeDef* huart)

 ,

{

 , , ,GPIO_InitTypeDef GPIO_InitStruct,

 ,

 , , ,if(huart->,Instance==USART1)

 ,

 , , ,{

 ,

 , , , , , ,__HAL_RCC_USART1_CLK_ENABLE(),

 ,

 ,

 , , , , , ,/**USART1 GPIO Configuration

 ,

 , , , , , ,PA9 ------>, USART1_TX

 ,

 , , , , , ,PA10 ------>, USART1_RX

 ,

 , , , , , ,*/

 ,

 , , , , , ,GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10,

 ,

 , , , , , ,GPIO_InitStruct.Mode = GPIO_MODE_AF_PP,

 ,

 , , , , , ,GPIO_InitStruct.Pull = GPIO_PULLUP,

 ,

 , , , , , ,GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH,

 ,

 , , , , , ,GPIO_InitStruct.Alternate = GPIO_AF7_USART1,

 ,

 , , , , , ,HAL_GPIO_Init(GPIOA, &,GPIO_InitStruct),

 ,

 , , , , , ,HAL_NVIC_SetPriority(USART1_IRQn, 5, 0),

 ,

 , , , , , ,HAL_NVIC_EnableIRQ(USART1_IRQn),

 ,

 , , ,}

 ,

 , , ,else if(huart->,Instance==USART2)

 ,

 , , ,{

 ,

 , , , , , ,__HAL_RCC_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_PULLUP,

 ,

 , , , , , ,GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH,

 ,

 , , , , , ,GPIO_InitStruct.Alternate = GPIO_AF7_USART2,

 ,

 , , , , , ,HAL_GPIO_Init(GPIOA, &,GPIO_InitStruct),

 ,

 , , , , , ,HAL_NVIC_SetPriority(USART2_IRQn, 0, 0),

 ,

 , , , , , ,HAL_NVIC_EnableIRQ(USART2_IRQn),

 ,

 , , ,}

}

void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)

 ,

{

 , , ,if(huart->,Instance==USART1)

 ,

 , , ,{

 ,

 , , , , , ,__HAL_RCC_USART1_CLK_DISABLE(),

 ,

 ,

 , , , , , ,/**USART1 GPIO Configuration

 ,

 , , , , , ,PA9 ------>, USART1_TX

 ,

 , , , , , ,PA10 ------>, USART1_RX

 ,

 , , , , , ,*/

 ,

 , , , , , ,HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10),

 , , , , , ,HAL_NVIC_DisableIRQ(USART1_IRQn),

 ,

 , , ,}

 ,

 , , ,else if(huart->,Instance==USART2)

 ,

 , , ,{

 ,

 , , , , , ,__HAL_RCC_USART2_CLK_DISABLE(),

 ,

 ,

 , , , , , ,/**USART2 GPIO Configuration

 ,

 , , , , , ,PA2 ------>, USART2_TX

 ,

 , , , , , ,PA3 ------>, USART2_RX

 ,

 , , , , , ,*/

 ,

 , , , , , ,HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3),

 ,

 , , , , , ,HAL_NVIC_DisableIRQ(USART2_IRQn),

 ,

 , , ,}

}

Posted on February 11, 2018 at 15:53

Would probably use following for a local/auto variable to guarantee clean initialization

GPIO_InitTypeDef GPIO_InitStruct = {0};

And add

__GPIOA_CLK_ENABLE();

Is this a custom board, or a NUCLEO/DISCO ?

How are things wired up?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 11, 2018 at 16:17

This is STM32F446RE Nucelo board, i don't have anything wired since i use only USART to output information through serial.

I have added the lines you but it doesn't seem to change anything. Maybe it can only communicate through GPIO, not serial.

void HAL_UART_MspInit(UART_HandleTypeDef* huart)

{

   GPIO_InitTypeDef GPIO_InitStruct = {0};

   if(huart->Instance==USART1)

      {

      /* USER CODE BEGIN USART1_MspInit 0 */

      __GPIOA_CLK_ENABLE();

      /* USER CODE END USART1_MspInit 0 */

      /* Peripheral clock enable */

      __HAL_RCC_USART1_CLK_ENABLE();

      .......

Posted on February 11, 2018 at 16:42

PA9/PA10 should get to the Arduino header

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 11, 2018 at 16:51

So this means, i can only transfer through serial USART2 and USART1 only through GPIO pins?

Posted on February 11, 2018 at 17:18

By default, on Nuc 64 boards, the USART2 is connected to STLINK.

Posted on February 11, 2018 at 17:19

Not sure I understand the question.

The ST-LINK VCP is connected to PA2/PA3 (USART2), you can't create imaginary connectivity. Review schematic.

http://www.st.com/content/ccc/resource/technical/document/user_manual/98/2e/fa/4b/e0/82/43/b7/DM00105823.pdf/files/DM00105823.pdf/jcr:content/translations/en.DM00105823.pdf

 
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 11, 2018 at 17:20

see page 27 of UM1724

0690X00000609UzQAI.png