Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 4 months ago.
Is the CAN bus speed correct?
Hi Paul,
You have done an excellent job! I'm considering to revise my library according to your approach and avoid all those messy pre-compiler #if defined checks as well as to prevent loosing some messages silently.
However, I have a question regarding the CAN bus speed. Did you try to connect to your CAN bus also a node made of LPC1768 set to the same CAN speed as the NUCLEO-91RC CAN nodes? I'm asking this because I think the APB1 frequency on NUCLEO-91RC is 48MHz. If it is so, then the can_frequency() function for the NUCLEO-91RC might look like this:
int can_frequency(can_t* obj, int hz) {
HAL_NVIC_DisableIRQ(CEC_CAN_IRQn);
// APB1 peripheral clock = 48000000Hz
switch(hz) {
case 1000000:
// 1000kbps bit rate
obj->hcan->Init.Prescaler = 4; // number of time quanta = 48000000/4/1000000 = 12
obj->hcan->Init.SJW = CAN_SJW_1TQ;
obj->hcan->Init.BS1 = CAN_BS1_8TQ; // sample point at: (1 + 8) / 12 * 100 = 75%
obj->hcan->Init.BS2 = CAN_BS2_3TQ;
break;
case 500000:
// 500kbps bit rate
obj->hcan->Init.Prescaler = 8; // number of time quanta = 48000000/8/500000 = 12
obj->hcan->Init.SJW = CAN_SJW_1TQ;
obj->hcan->Init.BS1 = CAN_BS1_8TQ; // sample point at: (1 + 8) / 12 * 100 = 75%
obj->hcan->Init.BS2 = CAN_BS2_3TQ;
break;
case 250000:
// 250kbps
obj->hcan->Init.Prescaler = 12; // number of time quanta = 48000000/12/250000 = 16
obj->hcan->Init.SJW = CAN_SJW_1TQ;
obj->hcan->Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
obj->hcan->Init.BS2 = CAN_BS2_4TQ;
break;
case 125000:
// 125kbps
obj->hcan->Init.Prescaler = 24; // number of time quanta = 48000000/24/125000 = 16
obj->hcan->Init.SJW = CAN_SJW_1TQ;
obj->hcan->Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
obj->hcan->Init.BS2 = CAN_BS2_4TQ;
break;
default:
// 125kbps (default)
obj->hcan->Init.Prescaler = 24; // number of time quanta = 48000000/24/125000 = 16
obj->hcan->Init.SJW = CAN_SJW_1TQ;
obj->hcan->Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
obj->hcan->Init.BS2 = CAN_BS2_4TQ;
}
HAL_CAN_Init(obj->hcan);
/* HAL_CAN_INIT will call HAL_CAN_MspInit, which will init the interupts */
return 1;
}
The can_init() function might be revised accordingly.
Wish you Marry Christmas!
Zoltan