Skip to main content
MJørg.2
Associate II
April 7, 2021
Question

STM32 using CAN-BUS.

  • April 7, 2021
  • 1 reply
  • 4787 views

We are not completly sure how it works. We want to make a receiving module reject some ID's and only approve some. The code we used just approve every ID. Can someone explain how the ID works within our code? (If we for example would like to approve the first 32 adresses). See the pictures for the code we have used.

This topic has been closed for replies.

1 reply

Javier1
Principal
April 7, 2021

aaah yess the well documented stm32 canbus filters :D

this helped me:

//usage of can filters
 
//https://schulz-m.github.io/2017/03/23/stm32-can-id-filter/
 
//https://www.diller-technologies.de/stm32.html <-----this guy knows better

this is how i setup with no filter

void CanSETUPnoFilter(CAN_HandleTypeDef *hcan_object_reference,){
 
	canfilter.FilterActivation = CAN_FILTER_ENABLE; //CAN_FILTER_ENABLE;//CAN_FILTER_DISABLE
	canfilter.FilterBank = 0;
	canfilter.FilterFIFOAssignment = CAN_FILTER_FIFO0; //CAN_FILTER_FIFO1
	canfilter.FilterIdHigh = 0;
	canfilter.FilterIdLow = 0;
	canfilter.FilterMaskIdHigh = 0;
	canfilter.FilterMaskIdLow = 0;
	canfilter.FilterMode = CAN_FILTERMODE_IDMASK; //CAN_FILTERMODE_IDMASK;//CAN_FILTERMODE_IDLIST
	canfilter.FilterScale = CAN_FILTERSCALE_32BIT; //CAN_FILTERSCALE_32BIT
	canfilter.SlaveStartFilterBank = 0;
 
	HAL_CAN_ConfigFilter(_hcan, &canfilter);
	HAL_CAN_ActivateNotification(_hcan, CAN_IT_RX_FIFO0_MSG_PENDING); //activate interrupt for received canbus
	//enable pins of the can transceivers
	HAL_GPIO_WritePin(CAN_INH_GPIO_Port, CAN_INH_Pin, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(CAN_INH_2_GPIO_Port, CAN_INH_2_Pin, GPIO_PIN_RESET);
 
	HAL_CAN_Start(_hcan); //CRANK UP THE MOTORS!! BRRREM BRRREM starts can
 
}

 this is how i setup a mask filter

void CanSETUPmaskFilter(CAN_HandleTypeDef *hcan_object_reference) {
 
	canfilter.FilterActivation = CAN_FILTER_ENABLE; //CAN_FILTER_ENABLE;//CAN_FILTER_DISABLE
	canfilter.FilterBank = 0;
	canfilter.FilterFIFOAssignment = CAN_FILTER_FIFO0; //CAN_FILTER_FIFO1
	canfilter.FilterIdHigh = ( 0x100 << 3 ) >> 16 ;
	canfilter.FilterIdLow = 0x100 << 3 | 4 ;
	canfilter.FilterMaskIdHigh = 0xFE0 << 5 ;
	canfilter.FilterMaskIdLow = 0xFFF << 5| 0x10;
	canfilter.FilterMode = CAN_FILTERMODE_IDMASK; //CAN_FILTERMODE_IDMASK;//CAN_FILTERMODE_IDLIST
	canfilter.FilterScale = CAN_FILTERSCALE_16BIT; //CAN_FILTERSCALE_32BIT
	canfilter.SlaveStartFilterBank = 0;
 
	HAL_CAN_ConfigFilter(_hcan, &canfilter);
	HAL_CAN_ActivateNotification(_hcan, CAN_IT_RX_FIFO0_MSG_PENDING); //activate interrupt for received canbus
	HAL_CAN_Start(_hcan); //CRANK UP THE MOTORS!! BRRREM BRRREM starts can
 
}

hit me up in https://www.linkedin.com/in/javiermuñoz/
MJørg.2
MJørg.2Author
Associate II
April 7, 2021

Thanks Javier for the quick reply. Line 6 for example if we do this the result will be 0.

Can you explain the math?

Javier1
Principal
April 7, 2021

@Michael Jørgensen​  what do you mean with line 6?, i copied this guy logic

//https://www.diller-technologies.de/stm32.html <-----this guy knows better

hit me up in https://www.linkedin.com/in/javiermuñoz/