Skip to main content
Arnas Matiukas
Associate II
February 11, 2018
Solved

USART1 doesn't work

  • February 11, 2018
  • 10 replies
  • 2757 views
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__);

   }

}

    This topic has been closed for replies.
    Best answer by Tesla DeLorean
    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

     

    10 replies

    Tesla DeLorean
    Guru
    February 11, 2018
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Arnas Matiukas
    Associate II
    February 11, 2018
    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),

     ,

     , , ,}

    }

    Tesla DeLorean
    Guru
    February 11, 2018
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..