cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 using CAN-BUS.

MJørg.2
Associate II

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.

7 REPLIES 7
Javier1
Principal

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
 
}

we dont need to firmware by ourselves, lets talk

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

Can you explain the math?

@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

we dont need to firmware by ourselves, lets talk

We did that as well in our code.

  1. We used the logic from the website you reffered to //https://schulz-m.github.io/2017/03/23/stm32-can-id-filter/

Here we used the logic as shown on the picture where the mask is 0xFFFF and 0xFFFF. This will by this logic make every bit of the ID be compared with the filter. We have used the same ID so it should work no matter what the mask is. This is not the case. It only works when the mask is 0x00000003 and below.

0693W000008yxM3QAI.png

@Javier Muñoz​ Can you explain why there is a need for bit-shifting and what the bit shifting is resulting in?

no idea, that is an old code i copied from the link i show you.

It worked for me. (yeeey)

If its not working for you still, did you checked if you are initialising the interrupt (in case you use interrupt)

HAL_CAN_ActivateNotification(_hcan, CAN_IT_RX_FIFO0_MSG_PENDING); //activate interrupt for received canbus

or maybe youre not starting the canbus?

we dont need to firmware by ourselves, lets talk