cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Started with CAN Communication on Nucleo F446RE

uti_prakash
Associate

uti_prakash_0-1747146978295.pnguti_prakash_1-1747147073014.png

Hello STM32 Community,

I'm trying to implement CAN communication on my Nucleo F446RE board and I'm facing some challenges getting it to work properly. I've attached screenshots of my .ioc configuration and the auto-generated code from STM32CubeIDE for reference.

Current Setup

  • Hardware: STM32 Nucleo F446RE

  • Transceiver: SN65HVD230

  • Baudrate: 500kbps (Prescaler=5, BS1=6TQ, BS2=3TQ)

  • IDE: STM32CubeIDE

Issues I'm Experiencing

When trying to transmit CAN messages, I'm getting "TX Error" and "CAN State: RESET" messages through UART. The free TX mailboxes show as 0, which seems unusual for initialisation.

What I've Tried

  1. Configured CAN1 in normal mode

  2. Set up the pins PA11 (RX) and PA12 (TX) for CAN

  3. Implemented basic transmission code using HAL_CAN_AddTxMessage()

  4. Added proper termination resistors (120Ω)

Questions

  1. Is my CAN configuration correct in the .ioc file?

  2. What could be causing the CAN peripheral to remain in RESET state?

  3. Are there any specific initialization steps I'm missing for the SN65HVD230 transceiver?

  4. How can I properly verify if my hardware connections are correct?

I'm planning to communicate with another STM32 board (L432KC) using CAN, but I need to get the basic transmission working first.

Any help, examples, or guidance would be greatly appreciated!

Thank you in advance,

/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2025 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 ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ CAN_HandleTypeDef hcan1; UART_HandleTypeDef huart2; /* USER CODE BEGIN PV */ CAN_TxHeaderTypeDef TxHeader; uint8_t TxData[8]; uint32_t TxMailbox; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART2_UART_Init(void); static void MX_CAN1_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void HAL_GPIO_EXITI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == GPIO_PIN_13){ TxData[0] = 100; // ms Delay TxData[1] = 10; // loop rep HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox); } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ char msg[50]; // Buffer for debug messages /* 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_USART2_UART_Init(); MX_CAN1_Init(); /* USER CODE BEGIN 2 */ // Send an initial debug message to confirm USART2 is working sprintf(msg, "USART2 working!\r\n"); HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 1000); HAL_CAN_Start(&hcan1); //Activate the notification HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING); TxHeader.DLC = 2; // data length TxHeader.IDE = CAN_ID_STD; TxHeader.RTR = CAN_RTR_DATA; TxHeader.StdId = 0x446; // ID TxData[0] = 50; TxData[1] = 0xAA; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { // Prepare data static uint8_t counter = 0; TxData[0] = 'A' + (counter++ % 26); // Cycle through A-Z // Send CAN message if (HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox) != HAL_OK) { char err_msg[] = "TX Error\r\n"; HAL_UART_Transmit(&huart2, (uint8_t*)err_msg, strlen(err_msg), 100); } else { char debug_msg[50]; sprintf(debug_msg, "Sent: %c (ID: 0x%03lX)\r\n", TxData[0], TxHeader.StdId); HAL_UART_Transmit(&huart2, (uint8_t*)debug_msg, strlen(debug_msg), 100); } CheckCANStatus(); HAL_Delay(2000); // 1Hz transmission /* 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}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLM = 16; RCC_OscInitStruct.PLL.PLLN = 200; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 2; RCC_OscInitStruct.PLL.PLLR = 2; 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_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) { Error_Handler(); } } /** * @brief CAN1 Initialization Function * None * @retval None */ static void MX_CAN1_Init(void) { /* USER CODE BEGIN CAN1_Init 0 */ /* USER CODE END CAN1_Init 0 */ /* USER CODE BEGIN CAN1_Init 1 */ /* USER CODE END CAN1_Init 1 */ hcan1.Instance = CAN1; hcan1.Init.Prescaler = 5; hcan1.Init.Mode = CAN_MODE_NORMAL; hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ; hcan1.Init.TimeSeg1 = CAN_BS1_6TQ; hcan1.Init.TimeSeg2 = CAN_BS2_3TQ; hcan1.Init.TimeTriggeredMode = DISABLE; hcan1.Init.AutoBusOff = DISABLE; hcan1.Init.AutoWakeUp = DISABLE; hcan1.Init.AutoRetransmission = DISABLE; hcan1.Init.ReceiveFifoLocked = DISABLE; hcan1.Init.TransmitFifoPriority = DISABLE; if (HAL_CAN_Init(&hcan1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN CAN1_Init 2 */ /* USER CODE END CAN1_Init 2 */ } /** * @brief USART2 Initialization Function * 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 */ } /** * @brief GPIO Initialization Function * None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* USER CODE BEGIN MX_GPIO_Init_1 */ /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : B1_Pin */ GPIO_InitStruct.Pin = B1_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pin : LD2_Pin */ GPIO_InitStruct.Pin = LD2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct); /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ void CheckCANStatus(CAN_HandleTypeDef *hcan) { // Get current state HAL_CAN_StateTypeDef state = HAL_CAN_GetState(hcan); uint32_t errorCode = HAL_CAN_GetError(hcan); char statusMsg[100]; // Report state switch(state) { case HAL_CAN_STATE_RESET: sprintf(statusMsg, "CAN State: RESET\r\n"); break; case HAL_CAN_STATE_READY: sprintf(statusMsg, "CAN State: READY\r\n"); break; case HAL_CAN_STATE_LISTENING: sprintf(statusMsg, "CAN State: LISTENING\r\n"); break; case HAL_CAN_STATE_SLEEP_PENDING: sprintf(statusMsg, "CAN State: SLEEP PENDING\r\n"); break; case HAL_CAN_STATE_SLEEP_ACTIVE: sprintf(statusMsg, "CAN State: SLEEP ACTIVE\r\n"); break; case HAL_CAN_STATE_ERROR: sprintf(statusMsg, "CAN State: ERROR\r\n"); break; default: sprintf(statusMsg, "CAN State: UNKNOWN\r\n"); } HAL_UART_Transmit(&huart2, (uint8_t*)statusMsg, strlen(statusMsg), 100); // Report any errors if (errorCode != HAL_CAN_ERROR_NONE) { if (errorCode & HAL_CAN_ERROR_EWG) HAL_UART_Transmit(&huart2, (uint8_t*)"Error: Warning\r\n", 16, 100); if (errorCode & HAL_CAN_ERROR_EPV) HAL_UART_Transmit(&huart2, (uint8_t*)"Error: Passive\r\n", 16, 100); if (errorCode & HAL_CAN_ERROR_BOF) HAL_UART_Transmit(&huart2, (uint8_t*)"Error: Bus-off\r\n", 16, 100); if (errorCode & HAL_CAN_ERROR_ACK) HAL_UART_Transmit(&huart2, (uint8_t*)"Error: No ACK\r\n", 15, 100); // Reset errors after reporting HAL_CAN_ResetError(hcan); } // Report TX status uint32_t txFree = HAL_CAN_GetTxMailboxesFreeLevel(hcan); sprintf(statusMsg, "Free TX mailboxes: %lu\r\n", txFree); HAL_UART_Transmit(&huart2, (uint8_t*)statusMsg, strlen(statusMsg), 100); } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * file: pointer to the source file name * line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */
View more

 

3 REPLIES 3
mÆŽALLEm
ST Employee

Hello @uti_prakash and welcome to the community,

First, please use </> button to paste your code. Please refer to this post: How to insert source code.I''m editing your post.

Second, as you are using Normal mode,

hcan1.Init.Mode = CAN_MODE_NORMAL;

are you establishing a complete CAN bus as the following i.e. connecting a second node on the bus?

CAN.png

As per your comment, I suspect it's not the case:


@uti_prakash wrote:

I'm planning to communicate with another STM32 board (L432KC) using CAN, but I need to get the basic transmission working first.


 If not, that's not the correct way to use CAN in normal mode.

So either use a second node with the minimal hardware as shown above or use Loopback mode where you can use only one device. CAN is not SPI or UART.

If you are establishing a complete CAN bus please provide more details on the hardware used especially how you did establish a CAN bus.

For Loopback mode please read this article: Guide to CAN (bxCAN/CAN2.0) configuration in Loop back mode on STM32 MCUs

PS: better to attach your ioc file instead of sharing screenshots.

 

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.

uti_prakash_0-1747508889390.pnguti_prakash_1-1747508922019.png

Hello mÆŽALLEm. Thanks for clarifying the difference between CAN and other communication protocols. I want to let you know that I have a custom dev-board consisting of Teensy 4.0 as the other node, which I plan to use as a recipient (instead of L432KC) for simplicity, of whatever I want to send through the F446RE nucleo board. I have shared the .sch screenshots of the dev board for context. I am using SN65HVD250 as CAN transceivers. I request that you guide me in setting up my nucleo in the NORMAL mode of operation. I have tried to look up online for some tutorials, but everything seems confusing to me. I have attached the .ioc file as per your suggestion. I am a novice in CAN communication, so if you could provide some material related to CAN theory, it would mean a lot.

What's that "Teensy 4.0" dev board? does it contain a CAN transceiver?

Please post a sketch of your CAN bus with the two nodes: Nucleo F446RE + the transceiver +  Teensy 4.0 + its transceiver + the two 120 ohm terminating resistors as indicated in my previous post.

I need a clear view of your environment to help you efficiently.

+ It's not recommended to use HSI for the CAN communication in Normal mode. You need an accurate clock source such as a crystal.

mALLEm_0-1747647688573.png

What I suggest is to use the STLINK MCO output and configure the HSE in Bypass mode. I've attached the ioc file where I updated the RCC.

 

 

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.