cancel
Showing results for 
Search instead for 
Did you mean: 

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

rohitkumarkv07
Associate III

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

7 REPLIES 7
SofLit
ST Employee

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

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.

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.

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)?

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.
rohitkumarkv07
Associate III

Hello,

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

 

Best regards 

Rohit Kumar

SofLit
ST Employee

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.

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.
rohitkumarkv07
Associate III

Hello,

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

 

B.R

Rohit Kumar

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.

 

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.