cancel
Showing results for 
Search instead for 
Did you mean: 

Why doesn't receiver-interrupt work on UART4 in this example when it works for USART1 and USART3?

RLind.2
Associate II

#define STARTUP_MSG "Test Rx-interrupt\n"
 
#include <stdio.h>
#include "stm32f4xx_hal.h"
 
#define get_RTC HAL_GetTick
#define LED_inv HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5)
#define LED_on HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET)
#define LED_off HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET)
 
void waitXms( uint32_t );
 
void USART1_IRQHandler( void );
void USART3_IRQHandler( void );
void UART4_IRQHandler( void );
 
void uart1_init( void );
void uart2_init( void );
void uart3_init( void );
void uart4_init( void );
 
UART_HandleTypeDef hU1, hU2, hU3, hU4;
/*---------------------------------------------------------------------------*/
/* UART1 Interrupt Service Routine */
void USART1_IRQHandler( void ) {
   uint8_t uc;
 
   uc = (&hU1)->Instance->DR;  // Read data and clear flag
   putchar( uc ); // Echo to debug-uart
   LED_inv; // Toggle LED to check if program comes here
}
 
void uart1_init( void ) {
   GPIO_InitTypeDef GPIO_InitStruct;
 
   __GPIOA_CLK_ENABLE();
 
   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_LOW;
   GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // PA9 = TxD1, PA10 = RxD1 Alternate Function 8
 
   __USART1_CLK_ENABLE();
 
   hU1.Instance = USART1;
   hU1.Init.BaudRate = 38400;
   hU1.Init.WordLength = UART_WORDLENGTH_8B;
   hU1.Init.StopBits = UART_STOPBITS_1;
   hU1.Init.Parity = UART_PARITY_NONE;
   hU1.Init.Mode = UART_MODE_TX_RX;
   hU1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
   hU1.Init.OverSampling = UART_OVERSAMPLING_16;
 
   HAL_UART_Init(&hU1);
 
   /* Peripheral interrupt init*/
   HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
   HAL_NVIC_EnableIRQ(USART1_IRQn);
   __HAL_USART_ENABLE_IT(&hU1, UART_IT_RXNE);
}
 
/*---------------------------------------------------------------------------*/
void uart2_init( void ) {
   GPIO_InitTypeDef GPIO_InitStruct;
 
   __GPIOA_CLK_ENABLE();
 
   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_LOW;
   GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);// PA2 = TxD2 Alternate Function 8
                                          // PA3 = RxD2 Alternate Function 8
   __USART2_CLK_ENABLE();
 
   hU2.Instance = USART2;
   hU2.Init.BaudRate = 38400;
   hU2.Init.WordLength = UART_WORDLENGTH_8B;
   hU2.Init.StopBits = UART_STOPBITS_1;
   hU2.Init.Parity = UART_PARITY_NONE;
   hU2.Init.Mode = UART_MODE_TX_RX;
   hU2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
   hU2.Init.OverSampling = UART_OVERSAMPLING_16;
   HAL_UART_Init(&hU2);
 
   // No interrupt for debug-USART
}
 
/*---------------------------------------------------------------------------*/
/* UART3 Interrupt Service Routine */
void USART3_IRQHandler( void ) {
   uint8_t uc;
 
   uc = (&hU3)->Instance->DR;  // Read data and clear flag
   putchar( uc ); // Echo to debug-uart
   LED_inv; // Toggle LED to check if program comes here
}
 
void uart3_init( void ) {
   GPIO_InitTypeDef GPIO_InitStruct;
 
   __GPIOC_CLK_ENABLE();
 
   GPIO_InitStruct.Pin = GPIO_PIN_5;
   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
   GPIO_InitStruct.Pull = GPIO_PULLUP;
   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
   GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // PC5 = RxD3 Alternate Function 7
 
   __USART3_CLK_ENABLE();
 
   hU3.Instance = USART3;
   hU3.Init.BaudRate = 38400;
   hU3.Init.WordLength = UART_WORDLENGTH_8B;
   hU3.Init.StopBits = UART_STOPBITS_1;
   hU3.Init.Parity = UART_PARITY_NONE;
   hU3.Init.Mode = UART_MODE_RX; // Only receiver
   hU3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
   hU3.Init.OverSampling = UART_OVERSAMPLING_16;
 
   HAL_UART_Init(&hU3);
 
   /* Peripheral interrupt init*/
   HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
   HAL_NVIC_EnableIRQ(USART3_IRQn);
   __HAL_USART_ENABLE_IT(&hU3, UART_IT_RXNE);
}
/*---------------------------------------------------------------------------*/
/* UART4 Interrupt Service Routine */
void UART4_IRQHandler( void ) {
   uint8_t uc;
 
   uc = (&hU4)->Instance->DR;  // Read data and clear flag
   putchar( uc ); // Echo to debug-uart
   LED_inv; // Toggle LED to check if program comes here
}
 
void uart4_init( void ) {
   GPIO_InitTypeDef GPIO_InitStruct;
 
   __GPIOA_CLK_ENABLE();
 
   GPIO_InitStruct.Pin = GPIO_PIN_0;
   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
   GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // PA0 = TxD4 Alternate Function 8
 
   GPIO_InitStruct.Pin = GPIO_PIN_1;
   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
   GPIO_InitStruct.Pull = GPIO_PULLUP;
   GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
   GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // PA1 = RxD4 Alternate Function 8
 
   __UART4_CLK_ENABLE();
 
   hU4.Instance = UART4;
   hU4.Init.BaudRate = 38400;
   hU4.Init.WordLength = UART_WORDLENGTH_8B;
   hU4.Init.StopBits = UART_STOPBITS_1;
   hU4.Init.Parity = UART_PARITY_NONE;
   hU4.Init.Mode = UART_MODE_TX_RX;
   hU4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
   hU4.Init.OverSampling = UART_OVERSAMPLING_16;
 
   HAL_UART_Init(&hU4);
 
   /* Peripheral interrupt init*/
   HAL_NVIC_SetPriority(UART4_IRQn, 0, 0);
   HAL_NVIC_EnableIRQ(UART4_IRQn);
   __HAL_USART_ENABLE_IT(&hU4, UART_IT_RXNE);
}
/*---------------------------------------------------------------------------*/
int main( void ) {
   GPIO_InitTypeDef GPIO_InitStruct;
 
   // Reset of all peripherals, Initializes the Flash interface and the Systick.
   HAL_Init();
 
   __HAL_RCC_GPIOA_CLK_ENABLE();
   GPIO_InitStruct.Pin = GPIO_PIN_5;
   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);// PA5 == LED
 
   uart2_init(); // Debug-UART (putchar)
   printf( STARTUP_MSG );
 
   //uart1_init(); // OK
   //uart3_init(); // OK
   uart4_init(); // NOT WORKING
 
   while(1) {
      //LED_inv;
      waitXms( 500 );
   }
}
 
/* ------------------------------------------------------------------------- */
// Debug goes to Nucleo-standard UART2/TxD2/PA2
int putchar( int val ) {
 
   if( 10 == val ) putchar( 13 );
 
   while( __HAL_UART_GET_FLAG( &hU2, UART_FLAG_TC) != SET); // Wait for empty register
   (&hU2)->Instance->DR = val; // Direct write to register
 
   return val;
}
 
/* ------------------------------------------------------------------------ */
 
void waitXms( uint32_t loop ) {
    uint32_t ul;
 
    while( loop ) {
        for( ul=0; ul < 10000U; ul++ ) asm(" nop");
        loop--;
    }
}
/*----------------------------------------------------------------------------*/

1 ACCEPTED SOLUTION

Accepted Solutions

F446RE not an F1 series

GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // USE GPIO_MODE_AF_PP

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

3 REPLIES 3
TDK
Guru

Include your chip number.

What does "not working" mean in particular? How do you know it's not working?

If you feel a post has answered your question, please click "Accept as Solution".

F446RE not an F1 series

GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // USE GPIO_MODE_AF_PP

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

Thanks clive1,

now we got it to work! 🙂