cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 UART interrupt not working

Smdeint
Associate III

Hello, I'm building a project where I will use CAN, UART and a TIMER. All of these work with an interrupt. How it works: I get data via CAN and use a TIMER to send a Request on the CANbus. With the UART I need to receive some commands that will trigger some functions. I have provided my code. In this code everything is working except for the UART interrupt. I had a different project with UART where everything was in the main, so I copied that to see if that is working, but it doesn't work. The UART interrupt is not getting triggered, but in the separate project it is working where I only use UART. What is wrong? 

 

/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2023 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include <engine_control.h> /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ // Uncomment the next line to enable debug prints //#define DEBUGG #ifdef DEBUGG #define DebugPrint(fmt, ...) printf(fmt, ##__VA_ARGS__) #else #define DebugPrint(fmt, ...) #endif /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ CAN_HandleTypeDef hcan; TIM_HandleTypeDef htim14; UART_HandleTypeDef huart1; UART_HandleTypeDef huart2; /* USER CODE BEGIN PV */ CAN_RxHeaderTypeDef can_rxheader; //CAN Bus Receive Header CAN_TxHeaderTypeDef can_txheader; //CAN Bus Transmit Header uint8_t can_rx[8]; //CAN Bus Receive Buffer //char received_char; // RS232 received /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_CAN_Init(void); static void MX_USART1_UART_Init(void); static void MX_USART2_UART_Init(void); static void MX_TIM14_Init(void); /* USER CODE BEGIN PFP */ // Enable Serial print via USART1 #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif PUTCHAR_PROTOTYPE { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ //+++++++++++++++++++++++ #define BUFFER_SIZE 50 #define XYZ_BUFFER_SIZE 5 // Global variables char received_char; char message_buffer[BUFFER_SIZE]; uint8_t message_index = 0; uint8_t message_type = 0; // 0: No message, 1: X, 2: Y, 3: Z, 4: Array // Ring buffer for X, Y, Z messages char xyz_ring_buffer[XYZ_BUFFER_SIZE]; volatile uint8_t xyz_ring_head = 0; volatile uint8_t xyz_ring_tail = 0; uint8_t xyz_buffer_count = 0; // Ring buffer for character array message char array_ring_buffer[BUFFER_SIZE]; volatile uint8_t array_ring_head = 0; volatile uint8_t array_ring_tail = 0; uint8_t array_buffer_count = 0; //uint8_t tx_battery_data[] = {100, 49, 50, 44, 50, 103}; //uint8_t tx_fuel_data[] = {101, 53, 65, 44, 49, 142}; //uint8_t tx_hour_data[] = {102, 51, 49, 50, 57, 44, 50, 53, 60}; //uint8_t tx_rpm_data[] = {103, 49, 53, 51, 56, 44, 54, 51}; uint8_t tx_data[] = { 100, 49, 50, 46, 50, 101, 53, 56, 46, 52, 102, 51, 49, 50, 57, 46, 50, 53, 103, 49, 53, 51, 56, 46, 54, 195 }; uint8_t tx_status[2] = { 3, 9 }; uint8_t tx_alarm[2] = { 37, 251 }; double voltage = 12.2; double fuel = 87.19; double hour = 51427.75; double rpm = 1409.792; double test[4]; // Reset ring buffer void UART_reset_xyz() { xyz_ring_head = 0; xyz_ring_tail = 0; } // Function to handle UART error void UART_Error_Handler(void) { // Toggle led1 when UART error occurred HAL_GPIO_TogglePin(GPIOF, LED1_Pin); } // UART callback function void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART2) { // Check if there's an overrun error if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE)) { __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_ORE); // Clear the overrun error flag UART_Error_Handler(); } // Check if there's a framing error if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE)) { __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_FE); // Clear the framing error flag UART_Error_Handler(); } // Check if there's a noise error if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE)) { __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_NE); // Clear the noise error flag UART_Error_Handler(); } // Check if there's a parity error if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE)) { __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_PE); // Clear the parity error flag UART_Error_Handler(); } } } // Calculate the CRC-8 for checksum uint8_t calculate_CRC8(const uint8_t *data, size_t length) { uint8_t crc = 0; for (size_t i = 0; i < length; i++) { crc ^= data[i]; for (int j = 0; j < 8; j++) { if (crc & 0x80) { crc = (crc << 1) ^ 0x07; // Polynomial for CRC-8 (0x07) } else { crc = crc << 1; } } } return crc; } // Function to convert double to ASCII characters void double_to_ASCII(double value, char *buffer, uint8_t *index) { uint8_t len = snprintf(NULL, 0, "%.2f", value); // Determine the length of the ASCII representation snprintf(&buffer[*index], len + 1, "%.2f", value); // Convert double to ASCII and store in the buffer *index += len; // Update the index to point to the next position in the buffer } // Function to send data over UART void send_double_array_UART(double *values, uint8_t num_values) { char buffer[100]; // Adjust buffer size based on the maximum possible size needed uint8_t index = 0; // Initialize the index for buffer position uint8_t identifier = 100; // Initialize the identifier starting from 100 for (uint8_t i = 0; i < num_values; ++i) { buffer[index++] = identifier++; // Add the identifier before each double double_to_ASCII(values[i], buffer, &index); // if (i != num_values - 1) { // buffer[index++] = ','; // Add a delimiter between values (comma in this case) // } } // Calculate CRC8 for the entire buffer (excluding the last character, which will be the CRC8) uint8_t crc = calculate_CRC8((uint8_t *)buffer, index); buffer[index++] = crc; // Convert CRC8 value to ASCII and add to buffer // buffer[index] = '\0'; // Add a null terminator to mark the end of the string // Send the string over UART HAL_UART_Transmit(&huart2, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY); } // Send message UART void send_msg(uint8_t *data, uint16_t size, const char *data_name) { while (HAL_UART_GetState(&huart2) == HAL_UART_STATE_READY) ; if (HAL_UART_Transmit_IT(&huart2, data, size) == HAL_OK) { printf("Send %s data\r\n", data_name); } else { HAL_UART_ErrorCallback(&huart2); DebugPrint("Failed to send %s data\r\n", data_name); } } // Send data over UART void UART_send_data(void) { send_msg(tx_data, sizeof(tx_data), "Data"); } // Send status over UART void UART_send_status(void) { send_msg(tx_status, sizeof(tx_status), "Status"); } // Send alarm over UART void UART_send_alarm(void) { send_msg(tx_alarm, sizeof(tx_alarm), "Alarm"); } void UART_process_xyz() { // Data available in the buffer if (xyz_ring_tail != xyz_ring_head) { DebugPrint("Received: %c\r\n", xyz_ring_buffer[xyz_ring_tail]); switch (xyz_ring_buffer[xyz_ring_tail]) { case 'X': // UART_send_data(); send_double_array_UART(test, sizeof(test) / sizeof(test[0])); DebugPrint("Data request \r\n"); break; case 'Y': UART_send_alarm(); DebugPrint("Alarm request \r\n"); break; case 'Z': UART_send_status(); DebugPrint("Status request \r\n"); break; default: printf("Incorrect UART message \r\n"); } xyz_ring_tail = (xyz_ring_tail + 1) % XYZ_BUFFER_SIZE; xyz_buffer_count--; } } //void UART_process_array() { // //// char data[]; // // if (array_ring_tail != array_ring_head) { // // char array_message = array_ring_buffer[array_ring_tail]; // // Process arrayMessage as needed // array_ring_tail = (array_ring_tail + 1) % BUFFER_SIZE; // array_buffer_count--; // } // //} // UART interrupt void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { printf("RS232 Interrupt \r\n"); // Check if correct UART is triggered if (huart->Instance == USART2) { // Check if UART is received correctly if (HAL_UART_Receive_IT(&huart2, (uint8_t*)&received_char, 1) == HAL_OK) { // Check for the type of message switch (message_type) { // No message received yet case 0: // Decide the message type if (received_char == 'X') { message_type = 1; } else if (received_char == 'Y') { message_type = 2; } else if (received_char == 'Z') { message_type = 3; } else { // Data message received message_type = 4; message_index = 0; message_buffer[message_index++] = received_char; } break; case 1: // X message case 2: // Y message case 3: // Z message // Store X, Y, Z messages in the ring buffer if (xyz_buffer_count < XYZ_BUFFER_SIZE) { xyz_ring_buffer[xyz_ring_head] = received_char; xyz_ring_head = (xyz_ring_head + 1) % XYZ_BUFFER_SIZE; xyz_buffer_count++; } else { // UART_reset_xyz(); // Overwrite the last received character in the xyzeRingBuffer xyz_ring_buffer[xyz_ring_tail] = received_char; xyz_ring_tail = (xyz_ring_tail + 1) % XYZ_BUFFER_SIZE; } break; // Array message case 4: // Store array message in the ring buffer if (array_buffer_count < BUFFER_SIZE) { array_ring_buffer[array_ring_head] = received_char; array_ring_head = (array_ring_head + 1) % BUFFER_SIZE; array_buffer_count++; } else { array_ring_tail = 0; array_ring_head = 0; array_buffer_count = 0; } if (message_index < BUFFER_SIZE - 1) { message_buffer[message_index++] = received_char; } else { // Process complete array message (message_buffer contains the data) message_type = 0; // Reset message type } break; } } else { // Error handler HAL_UART_ErrorCallback(&huart2); DebugPrint("UART no Data \r\n"); } // Restart reception HAL_UART_Receive_IT(&huart2, (uint8_t*)&received_char, 1); } } //+++++++++++++++++++++++ //uint8_t rx_buff[2]; //----------------------------------- // UART interrupt //void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { // // printf("UART interrupt"); // // HAL_UART_Receive_IT(&huart2, rx_buff, 2); // // // Check if correct UART is triggered // if (huart->Instance == USART2) { // // printf("RS232 MSG \r\n"); // // // Check if UART is received correctly // if (HAL_UART_Receive_IT(&huart2, (uint8_t*)&received_char, 1) == HAL_OK) { // // Store received characters in the ring buffer // queue_request(&received_char); // // // Restart reception // HAL_UART_Receive_IT(&huart2, (uint8_t*)&received_char, 1); // } // // // Restart reception // HAL_UART_Receive_IT(&huart2, (uint8_t*)&received_char, 1); // } //} //----------------------------------- volatile uint8_t request_timer_flag = 0; // Callback timer interrupt for sending request on CANbus void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim == &htim14 ) { HAL_GPIO_TogglePin(GPIOF, LED1_Pin); request_timer_flag = 1; } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_CAN_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init(); MX_TIM14_Init(); /* USER CODE BEGIN 2 */ // UART interrupt HAL_UART_Receive_IT(&huart2, (uint8_t*)&received_char, 1); // Start timer HAL_TIM_Base_Start_IT(&htim14); // CAN filters CAN_filter(0, FILTER_ID1, FILTER_MASK1, CAN_RX_FIFO0, &hcan); CAN_filter(1, FILTER_ID2, FILTER_MASK2, CAN_RX_FIFO0, &hcan); CAN_filter(2, FILTER_ID3, FILTER_MASK3, CAN_RX_FIFO0, &hcan); CAN_filter(3, FILTER_ID4, FILTER_MASK4, CAN_RX_FIFO1, &hcan); CAN_filter(4, FILTER_ID5, FILTER_MASK5, CAN_RX_FIFO1, &hcan); CAN_filter(5, FILTER_ID6, FILTER_MASK6, CAN_RX_FIFO1, &hcan); CAN_filter(6, FILTER_ID7, FILTER_MASK7, CAN_RX_FIFO1, &hcan); printf("test \r\n"); // Enable CAN if (HAL_CAN_Start(&hcan) != HAL_OK) { CAN_restart(&hcan); } // Send out name of this node if (CAN_name() == NAME_ERROR) { CAN_restart(&hcan); } // CAN interrupts HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING); HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO1_MSG_PENDING); HAL_CAN_ActivateNotification(&hcan, CAN_IT_ERROR); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { if (request_timer_flag) { request_PGN(TOTAL_ENGINE_HOUR); request_timer_flag = 0; // Reset flag } // Process incoming CAN messages CAN_process_msg(); // TEST CODE test[0] = voltage; test[1] = fuel; test[2] = hour; test[3] = rpm; UART_process_xyz(); // ++++++ /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /** * @brief CAN Initialization Function * @PAram None * @retval None */ static void MX_CAN_Init(void) { /* USER CODE BEGIN CAN_Init 0 */ /* USER CODE END CAN_Init 0 */ /* USER CODE BEGIN CAN_Init 1 */ /* USER CODE END CAN_Init 1 */ hcan.Instance = CAN1; hcan.Init.Prescaler = 8; hcan.Init.Mode = CAN_MODE_NORMAL; hcan.Init.SyncJumpWidth = CAN_SJW_1TQ; hcan.Init.TimeSeg1 = CAN_BS1_15TQ; hcan.Init.TimeSeg2 = CAN_BS2_2TQ; hcan.Init.TimeTriggeredMode = DISABLE; hcan.Init.AutoBusOff = DISABLE; hcan.Init.AutoWakeUp = DISABLE; hcan.Init.AutoRetransmission = DISABLE; hcan.Init.ReceiveFifoLocked = DISABLE; hcan.Init.TransmitFifoPriority = DISABLE; if (HAL_CAN_Init(&hcan) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN CAN_Init 2 */ /* USER CODE END CAN_Init 2 */ } /** * @brief TIM14 Initialization Function * @PAram None * @retval None */ static void MX_TIM14_Init(void) { /* USER CODE BEGIN TIM14_Init 0 */ /* USER CODE END TIM14_Init 0 */ /* USER CODE BEGIN TIM14_Init 1 */ /* USER CODE END TIM14_Init 1 */ htim14.Instance = TIM14; htim14.Init.Prescaler = 8000; htim14.Init.CounterMode = TIM_COUNTERMODE_UP; htim14.Init.Period = 9999; htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim14) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM14_Init 2 */ /* USER CODE END TIM14_Init 2 */ } /** * @brief USART1 Initialization Function * @PAram None * @retval None */ static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_Init 0 */ /* USER CODE BEGIN USART1_Init 1 */ /* USER CODE END USART1_Init 1 */ 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(); } /* USER CODE BEGIN USART1_Init 2 */ /* USER CODE END USART1_Init 2 */ } /** * @brief USART2 Initialization Function * @PAram None * @retval None */ static void MX_USART2_UART_Init(void) { /* USER CODE BEGIN USART2_Init 0 */ /* USER CODE END USART2_Init 0 */ /* USER CODE BEGIN USART2_Init 1 */ /* USER CODE END USART2_Init 1 */ 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(); } /* USER CODE BEGIN USART2_Init 2 */ /* USER CODE END USART2_Init 2 */ }
View more

 

  

1 ACCEPTED SOLUTION

Accepted Solutions

I got it working. I didn't look at the PIN settings. The receive pin (PA3) of USART2 was set to GPIO mode, but it should be input mode. afbeelding 2024-01-23 at 13.18.26 (2).png

 

View solution in original post

6 REPLIES 6
mÆŽALLEm
ST Employee

Hello, 

Did you activate the interrupt for the UART? (check if the NVIC interrupt line for your UART is configured).

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Yes, I activated the interrupt for UART, CAN, TIMER.

Try to isolate UART functions and built a simple application with only UART and see if the issue still happen.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

So I followed this link https://wiki.st.com/stm32mcu/wiki/Getting_started_with_UART#UART_with_Interrupt to test if it was working, but this also doesn't work. The interrupt won't trigger. Below is the code.

/* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include <engine_control.h> /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ // Uncomment the next line to enable debug prints //#define DEBUGG #ifdef DEBUGG #define DebugPrint(fmt, ...) printf(fmt, ##__VA_ARGS__) #else #define DebugPrint(fmt, ...) #endif /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ CAN_HandleTypeDef hcan; TIM_HandleTypeDef htim14; UART_HandleTypeDef huart1; UART_HandleTypeDef huart2; /* USER CODE BEGIN PV */ CAN_RxHeaderTypeDef can_rxheader; //CAN Bus Receive Header CAN_TxHeaderTypeDef can_txheader; //CAN Bus Transmit Header uint8_t can_rx[8]; //CAN Bus Receive Buffer //char received_char; // RS232 received /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_CAN_Init(void); static void MX_USART1_UART_Init(void); static void MX_USART2_UART_Init(void); static void MX_TIM14_Init(void); /* USER CODE BEGIN PFP */ // Enable Serial print via USART1 #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif PUTCHAR_PROTOTYPE { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ uint8_t rx_buff[2]; //----------------------------------- // UART interrupt void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { HAL_GPIO_TogglePin(GPIOF, LED2_Pin); printf("UART interrupt"); // Check if correct UART is triggered if (huart->Instance == USART2) { printf("RS232 MSG \r\n"); } HAL_UART_Receive_IT(&huart2, rx_buff, 2); } //----------------------------------- volatile uint8_t request_timer_flag = 0; // Callback timer interrupt for sending request on CANbus void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim == &htim14 ) { HAL_GPIO_TogglePin(GPIOF, LED1_Pin); request_timer_flag = 1; } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_CAN_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init(); MX_TIM14_Init(); /* USER CODE BEGIN 2 */ // UART interrupt HAL_UART_Receive_IT(&huart2, rx_buff, 2); // Start timer HAL_TIM_Base_Start_IT(&htim14); // CAN filters CAN_filter(0, FILTER_ID1, FILTER_MASK1, CAN_RX_FIFO0, &hcan); CAN_filter(1, FILTER_ID2, FILTER_MASK2, CAN_RX_FIFO0, &hcan); CAN_filter(2, FILTER_ID3, FILTER_MASK3, CAN_RX_FIFO0, &hcan); CAN_filter(3, FILTER_ID4, FILTER_MASK4, CAN_RX_FIFO1, &hcan); CAN_filter(4, FILTER_ID5, FILTER_MASK5, CAN_RX_FIFO1, &hcan); CAN_filter(5, FILTER_ID6, FILTER_MASK6, CAN_RX_FIFO1, &hcan); CAN_filter(6, FILTER_ID7, FILTER_MASK7, CAN_RX_FIFO1, &hcan); printf("test \r\n"); // Enable CAN if (HAL_CAN_Start(&hcan) != HAL_OK) { CAN_restart(&hcan); } // Send out name of this node if (CAN_name() == NAME_ERROR) { CAN_restart(&hcan); } // CAN interrupts HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING); HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO1_MSG_PENDING); HAL_CAN_ActivateNotification(&hcan, CAN_IT_ERROR); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { if (request_timer_flag) { request_PGN(TOTAL_ENGINE_HOUR); request_timer_flag = 0; // Reset flag } // Process incoming CAN messages CAN_process_msg(); // Process RS232 communication // RS232_process_request(); // printf("STATE MAIN: %d \r\n", engine_info.engine_state); // printf("AMBER: %d \r\n", active_lamp.amber); // printf("MIL: %d \r\n", active_lamp.mil); // printf("PROTECT: %d \r\n", active_lamp.protect); // printf("RED: %d \r\n", active_lamp.red); // printf("\r\n"); // printf("ALARMEN: %d \r\n", engine_info.alarm); // printf("\r\n"); // printf("STATE: %d \r\n", engine_data.engine_state); // printf("\r\n"); // printf("++++++BATT+++++++: %0.2f \r\n", engine_data.battery_voltage); // printf("++++++FUEL+++++++: %0.2f \r\n", engine_data.fuel_level); // printf("++++++HOUR+++++++: %0.2f \r\n", engine_data.engine_hour); // printf("++++++SPEED++++++: %0.2f \r\n", engine_data.engine_speed); // printf("++++++OIL++++++++: %0.2f \r\n", engine_data.oil_pressure); // printf("++++++TEMP+++++++: %0.2f \r\n", engine_data.engine_temperature); // printf("++++++ECO++++++++: %0.2f \r\n", engine_data.fuel_economy); // printf("++++++STATE++++++: %d \r\n", engine_data.engine_state); // printf("\r\n"); // printf("\r\n"); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /** * @brief CAN Initialization Function * @PAram None * @retval None */ static void MX_CAN_Init(void) { /* USER CODE BEGIN CAN_Init 0 */ /* USER CODE END CAN_Init 0 */ /* USER CODE BEGIN CAN_Init 1 */ /* USER CODE END CAN_Init 1 */ hcan.Instance = CAN1; hcan.Init.Prescaler = 8; hcan.Init.Mode = CAN_MODE_NORMAL; hcan.Init.SyncJumpWidth = CAN_SJW_1TQ; hcan.Init.TimeSeg1 = CAN_BS1_15TQ; hcan.Init.TimeSeg2 = CAN_BS2_2TQ; hcan.Init.TimeTriggeredMode = DISABLE; hcan.Init.AutoBusOff = DISABLE; hcan.Init.AutoWakeUp = DISABLE; hcan.Init.AutoRetransmission = DISABLE; hcan.Init.ReceiveFifoLocked = DISABLE; hcan.Init.TransmitFifoPriority = DISABLE; if (HAL_CAN_Init(&hcan) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN CAN_Init 2 */ /* USER CODE END CAN_Init 2 */ } /** * @brief TIM14 Initialization Function * @PAram None * @retval None */ static void MX_TIM14_Init(void) { /* USER CODE BEGIN TIM14_Init 0 */ /* USER CODE END TIM14_Init 0 */ /* USER CODE BEGIN TIM14_Init 1 */ /* USER CODE END TIM14_Init 1 */ htim14.Instance = TIM14; htim14.Init.Prescaler = 8000; htim14.Init.CounterMode = TIM_COUNTERMODE_UP; htim14.Init.Period = 9999; htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim14) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM14_Init 2 */ /* USER CODE END TIM14_Init 2 */ } /** * @brief USART1 Initialization Function * @PAram None * @retval None */ static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_Init 0 */ /* USER CODE BEGIN USART1_Init 1 */ /* USER CODE END USART1_Init 1 */ 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(); } /* USER CODE BEGIN USART1_Init 2 */ /* USER CODE END USART1_Init 2 */ } /** * @brief USART2 Initialization Function * @PAram None * @retval None */ static void MX_USART2_UART_Init(void) { /* USER CODE BEGIN USART2_Init 0 */ /* USER CODE END USART2_Init 0 */ /* USER CODE BEGIN USART2_Init 1 */ /* USER CODE END USART2_Init 1 */ 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(); } /* USER CODE BEGIN USART2_Init 2 */ /* USER CODE END USART2_Init 2 */ }
View more

  

I got it working. I didn't look at the PIN settings. The receive pin (PA3) of USART2 was set to GPIO mode, but it should be input mode. afbeelding 2024-01-23 at 13.18.26 (2).png

 

Well, that's why you need to isolate the function having the issue to surround it quickly ;).

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.