cancel
Showing results for 
Search instead for 
Did you mean: 

Struggling with CAN in latest HAL.

xrstokes
Associate III

I know that there a lot of care examples on the Internet, but I haven’t been able to get one to work with latest version of the HAL drivers. I’m attempting to use the loopback interface just to test on a single device at the moment. This is as far as I’ve got. But it crashes after the second time I press the button. The first time I do receive something. Do I have to reset the RX IT after each receive?

Thanks in advance.

	pTxHeader.DLC=1; //give message size of 1 byte
	pTxHeader.IDE=CAN_ID_STD; //set identifier to standard
	pTxHeader.RTR=CAN_RTR_DATA; //set data type to remote transmission request?
	pTxHeader.StdId=0x244; //define a standard identifier, used for message identification by filters (switch this for the other microcontroller)
 
	//filter one (stack light blink)
	sFilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0; //set fifo assignment
//	sFilterConfig.FilterIdHigh=0x245<<5; //the ID that the filter looks for (switch this for the other microcontroller)
	sFilterConfig.FilterIdHigh=0;
	sFilterConfig.FilterIdLow=0;
	sFilterConfig.FilterMaskIdHigh=0;
	sFilterConfig.FilterMaskIdLow=0;
	sFilterConfig.FilterScale=CAN_FILTERSCALE_32BIT; //set filter scale
	sFilterConfig.FilterActivation=ENABLE;
 
	HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig); //configure CAN filter
 
	HAL_CAN_Start(&hcan1); //start CAN
	HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING); //enable interrupts
 
	uint8_t myLEDVar = 1;
 
	while(1)
	{
		if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2))
		{
			HAL_CAN_AddTxMessage(&hcan1, &pTxHeader, myLEDVar, (uint32_t *)CAN_TX_MAILBOX0);
			myLEDVar++;
		}
 
		vTaskDelay(100);
	}
void CAN1_RX0_IRQHandler(void)
{
  /* USER CODE BEGIN CAN1_RX0_IRQn 0 */
	uint8_t result[1];
  /* USER CODE END CAN1_RX0_IRQn 0 */
  HAL_CAN_IRQHandler(&hcan1);
  /* USER CODE BEGIN CAN1_RX0_IRQn 1 */
  HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &pRxHeader, &result);
 
  Debug_Print_int(result);
 
  /* USER CODE END CAN1_RX0_IRQn 1 */
}

1 REPLY 1
Nizar Tenzekhti
Associate II

Hi xrstokes,

The best example to start with is to use ST firmwares examples in the Projects directory,

So you can use the example provided for the MCU STM32L4:

How to configure the CAN peripheral to send and receive CAN frames in normal mode. The sent frames are used to control LEDs by pressing Wkup/Tamper push-button.

https://github.com/STMicroelectronics/STM32CubeL4/tree/master/Projects/STM32L476G-EVAL/Examples/CAN/CAN_Networking

Or you can use the example provided for the MCU STM32L5 with the FDCAN IP:

How to configure the FDCAN peripheral to send and receive Classic CAN frames. The sent frames are used to control LEDs by pressing WKUP push-button.

https://github.com/STMicroelectronics/STM32CubeL5/tree/master/Projects/STM32L552E-EV/Examples/FDCAN/FDCAN_Classic_Frame_Networking

Nizar