Skip to main content
Explorer
July 9, 2024
Solved

Unable to receive messages from USB CAN Analyzer to STM32F407 Discovery board.

  • July 9, 2024
  • 7 replies
  • 3845 views

Hello,

I am trying to send the message from my USB-CAN Analyzer to my board STM32F407 discovery board, but I am unable to do that. 

 

HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rx_header, rx_data);



// Process received message

handleReceivedMessage(&rx_header, rx_data);

 

This is my function to read message.

 

 

void handleReceivedMessage(CAN_RxHeaderTypeDef *rx_header, uint8_t *rx_data)

{

// Your custom handling of received message

// Example: LED blinking based on received data

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_12); // Toggle LED

}

 

This is my handlereceivedMessage function.

 

Below is my message which I am sending from USB-CAN Analyzer.

 

Best Regards

Rohit Kumar

    This topic has been closed for replies.
    Best answer by mƎALLEm

    In that case I suspect the following issues:

    1- A HW issue: check your wiring, the power supply of the transceiver , your transceiver connections, termination resistors. In that case you need to provide schematics of your board.

    2- Bit timing issue: are you sure you have the same bitrate config as for your CAN analyzer?

    3- The clock source: are you sing HSE and a crystal? if not you can face issue with CAN communication in normal mode.

    You need also to share your project as requested previously.

     

    7 replies

    Technical Moderator
    July 9, 2024

    Hello,

    In this previous thread I recommended you to review on how to post a thread on this community: https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

    Could you please review it again and edit your post accordingly?

    Thank you for your understanding

    Explorer
    July 10, 2024

    Hello,

    Hello,

    I am trying to send the message from my USB-CAN Analyzer to my board STM32F407 discovery board, but I am unable to do that.

     

    while (1)
    	{
    		
    		CAN_RxHeaderTypeDef rxHeader;
    		uint8_t rx_data[8];
    
    		HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rxHeader, rx_data);
    		{
    			// Process received message
    			handleReceivedMessage(&rxHeader, rx_data);
    		}
    	}

     

    This is my function to read message.

     

    void handleReceivedMessage(CAN_RxHeaderTypeDef *rx_header, uint8_t *rx_data)
    {
    	// Your custom handling of received message
    	// Example: LED blinking based on received data
    	HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_12); // Toggle LED
    }

     

    This is my handlereceivedMessage function.

    I have configured the interrupt too.

     

    /*Enable CAN RX FIFO Interrupt in NVIC*/
    	HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0); // Set priority
    	HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); // Enable interrupt

     

    Below is my Can bit timing and filter configuration.

     

    static void MX_CAN1_Init(void)
    {
    
    	/* USER CODE END CAN1_Init 1 */
    	hcan1.Instance = CAN1;
    	hcan1.Init.Prescaler = 4;
    	hcan1.Init.Mode = CAN_MODE_NORMAL;
    	hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
    	hcan1.Init.TimeSeg1 = CAN_BS1_12TQ;
    	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 */
    	CAN_FilterTypeDef can_filter;
    	can_filter.FilterIdHigh = 0x0000;
    	can_filter.FilterIdLow = 0x0000;
    	can_filter.FilterMaskIdHigh = 0x0000;
    	can_filter.FilterMaskIdLow = 0x0000;
    	can_filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
    	can_filter.FilterBank = 1;
    	can_filter.FilterMode = CAN_FILTERMODE_IDMASK;
    	can_filter.FilterScale = CAN_FILTERSCALE_32BIT;
    	can_filter.FilterActivation = CAN_FILTER_ENABLE;
    	can_filter.SlaveStartFilterBank = 2;
    	if (HAL_CAN_ConfigFilter(&hcan1, &can_filter) != HAL_OK)
    	{
    		Error_Handler();
    	}
    
    }

     

    I review it again and edit accordingly. The problem in the above query I am facing is the form error. I have my bit timing set to the 75% point. SJW=1, BS1=14, BS2= 5, 500,000baud. Could you please check and recommend me some accurate values for bit timings so that i can overcome this form error.

    Technical Moderator
    July 10, 2024

    Hello,

    You activated the CAN interrupt :

    	HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0); // Set priority
    	HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); // Enable interrupt

    but you didn't use it and in the while loop you didn't check if a message has been received or not. This is not the correct implementation.

    I suggest you to review the example provided in STM32CubeF4 in this path under https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_Networking.

    Also could you please attach your project (including the ioc file if there is one)?

    Explorer
    July 10, 2024

    Hello,

    I am getting form error in CAN_ESR. According to my above configurations correct my bit timing configurations.

     

    Best regards 

    Rohit Kumar

    Technical Moderator
    July 10, 2024

    I think you accidently marked my answer as solution. I unmarked it then.

    Need to check you bitrate config + the clock source config: need to have the same CAN bitrate as your CAN analyzer + If you are using HSI you need to change it to HSE with an external crystal.

    But as a starting point use Loopback mode to at least validate your send/receive operations. If in Loopback mode doesn't work, it doesn't work also for Normal mode.

    Explorer
    July 10, 2024

    Hello,

    My configuration works for the loopback mode, but it doesn't work for the normal mode.

     

    B.R

    Rohit Kumar

    mƎALLEmAnswer
    Technical Moderator
    July 10, 2024

    In that case I suspect the following issues:

    1- A HW issue: check your wiring, the power supply of the transceiver , your transceiver connections, termination resistors. In that case you need to provide schematics of your board.

    2- Bit timing issue: are you sure you have the same bitrate config as for your CAN analyzer?

    3- The clock source: are you sing HSE and a crystal? if not you can face issue with CAN communication in normal mode.

    You need also to share your project as requested previously.

     

    Explorer
    July 11, 2024

    Hello,

    Can you suggest me how to implement HAL_CAN_GetRxFifoFillLevel for the reception of messages.

    Explain it with example for just only reception.

    B.R

    Rohit Kumar

    Technical Moderator
    July 11, 2024
    Explorer
    July 11, 2024

    Hello,

    I have tried this method but it didn't work on my STM32F407 discovery board. In my STM32F407 discovery board  I want to configure the reception using these. How can I do with these ?

    HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0);

     

    HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rx_header, rx_data);

     

    // Handle the received message

    handleReceivedMessage(&rx_header, rx_data);

     

    What is the return value of fifolevel in this ?

    B.R

    Rohit Kumar

    Explorer
    July 12, 2024

    Hello,

    I want to receive messages in stm32F407 board, from the USB CAN Analyzer in interrupt based. My FMPIE1 bit got updated and In LEC it shows stuff error, but RFOR bit still not get updated. I have attached my source code below . I have tried the github example code also but unable to do that.

     

     

    #include "main.h"
    
    #include "stm32f4xx_hal_can.h"
    /* USER CODE BEGIN PM */
    
    
    /* Private variables ---------------------------------------------------------*/
    CAN_HandleTypeDef hcan1;
    
    /* USER CODE BEGIN PV */
    
    static CAN_RxHeaderTypeDef rx_header;
    static uint8_t rx_data[8];
    
    int datacheck = 0;
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_CAN1_Init(void);
    
    
    
    void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan1)
    {
    	HAL_CAN_GetRxMessage(hcan1, CAN_RX_FIFO1, &rx_header, rx_data);
    
    }
    int main(void)
    {
    
    	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    	HAL_Init();
    
    	/* Configure the system clock */
    	SystemClock_Config();
    
    
    	// Enable CAN RX FIFO Interrupt in NVIC
    	HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0); // Set priority
    	HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); // Enable interrupt
    	/* USER CODE END 2 */
    
    	/* Initialize all configured peripherals */
    	MX_GPIO_Init();
    	MX_CAN1_Init();
    
    
    
    
    	while (1)
    	{
    
    
    	}
    }
    
    
    
    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_SCALE1);
    
    	/** 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.PLL.PLLState = RCC_PLL_ON;
    	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    	RCC_OscInitStruct.PLL.PLLM = 25;
    	RCC_OscInitStruct.PLL.PLLN = 288;
    	RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
    	RCC_OscInitStruct.PLL.PLLQ = 4;
    	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_5) != HAL_OK)
    	{
    		Error_Handler();
    	}
    }
    
    
    static void MX_CAN1_Init(void)
    {
    
    	/* USER CODE END CAN1_Init 1 */
    	hcan1.Instance = CAN1;
    	hcan1.Init.Prescaler = 3 ;
    	hcan1.Init.Mode = CAN_MODE_NORMAL;
    	hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
    	hcan1.Init.TimeSeg1 = CAN_BS1_16TQ;
    	hcan1.Init.TimeSeg2 = CAN_BS2_7TQ;
    	hcan1.Init.TimeTriggeredMode = DISABLE;
    	hcan1.Init.AutoBusOff = DISABLE;
    	hcan1.Init.AutoWakeUp = DISABLE;
    	hcan1.Init.AutoRetransmission = ENABLE;
    	hcan1.Init.ReceiveFifoLocked = DISABLE;
    	hcan1.Init.TransmitFifoPriority = DISABLE;
    	if (HAL_CAN_Init(&hcan1) != HAL_OK)
    	{
    		Error_Handler();
    	}
    	/* USER CODE BEGIN CAN1_Init 2 */
    	CAN_FilterTypeDef can_filter;
    	can_filter.FilterIdHigh = 0x0000;
    	can_filter.FilterIdLow = 0x0000;
    	can_filter.FilterMaskIdHigh = 0x0000;
    	can_filter.FilterMaskIdLow = 0x0000;
    	can_filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
    	can_filter.FilterBank = 0;
    	can_filter.FilterMode = CAN_FILTERMODE_IDMASK;
    	can_filter.FilterScale = CAN_FILTERSCALE_32BIT;
    	can_filter.FilterActivation = CAN_FILTER_ENABLE;
    	can_filter.SlaveStartFilterBank = 2;
    	if (HAL_CAN_ConfigFilter(&hcan1, &can_filter) != HAL_OK)
    	{
    		Error_Handler();
    	}
    	HAL_CAN_Start(&hcan1);
    
    	// Activate the notification
    	HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING);
    }
    
    
    static void MX_GPIO_Init(void)
    {
    	GPIO_InitTypeDef GPIO_InitStruct = {0};
    
    	/* GPIO Ports Clock Enable */
    	//__HAL_RCC_GPIOA_CLK_ENABLE();
    	__HAL_RCC_GPIOD_CLK_ENABLE();
    
    	/*Configure GPIO pin Output Level */
    	HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin, GPIO_PIN_RESET);
    
    	/*Configure GPIO pin : B1_Pin */
    	GPIO_InitStruct.Pin = B1_Pin;
    	GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
    	GPIO_InitStruct.Pull = GPIO_NOPULL;
    	HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
    
    	/*Configure GPIO pins : LD4_Pin LD3_Pin LD5_Pin LD6_Pin */
    	GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin;
    	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    	GPIO_InitStruct.Pull = GPIO_NOPULL;
    	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    	HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
    
    }
    
    
    
    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 */
    }
    

     

    Technical Moderator
    July 12, 2024

    Hello,

    Tips in CAN communication:

    When you face an issue in normal mode start by using Loopback mode at least to validate your Software and filters config. Because it could be something related to your HW. You can refer to this example: https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_LoopBack/

    Meanwhile, you still using a bad CAN config. You assigned FIFO0 to the filter but you activated FIFO1 interrupt.

    can_filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
    	// Activate the notification
    	HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING)
    void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan1)
    {
    	HAL_CAN_GetRxMessage(hcan1, CAN_RX_FIFO1, &rx_header, rx_data);
    
    }

    PS: regarding your question about the usage of HAL_CAN_GetRxMessage(); refer also to the example I pointed out above (loopback mode) where it has used as follows:

     /*##-5- Start the Reception process ########################################*/
     if(HAL_CAN_GetRxFifoFillLevel(&CanHandle, CAN_RX_FIFO0) != 1)
     {
     /* Reception Missing */
     Error_Handler();
     }
    
     if(HAL_CAN_GetRxMessage(&CanHandle, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
     {
     /* Reception Error */
     Error_Handler();
     }

    In any case you need to use the examples from STM32CubeF4 package.

    The two examples of the two modes are there:

    https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_LoopBack

    https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_Networking

    They are working well.