// macro to format the timing register value from the timing parameters #define CAN_TIMING(tseg1, tseg2, sjw, brp) (((tseg2-1) & 0x07) << 12) | (((tseg1-1) & 0x0F) << 8) | (((sjw-1) & 0x03) << 6) | ((brp-1) & 0x3F)
/******************************************************************************* * Function Name : CAN_SetTiming * Description : Setup the CAN timing with specific parameters * Input 1 : Time Segment before the sample point position, from 2 to 16 * Input 2 : Time Segment after the sample point position, from 1 to 8 * Input 3 : Synchronisation Jump Width, from 1 to 4 * Input 4 : Baud Rate Prescaler, from 1 to 1024 * Output : None * Return : None * Note : CAN must be in initialization mode *******************************************************************************/ void CAN_SetTiming(u32 tseg1, u32 tseg2, u32 sjw, u32 brp) { CAN->BTR = CAN_TIMING(tseg1, tseg2, sjw, brp); CAN->BRPR = ((brp-1) >> 6) & 0x0F; }
//------------------------------------------------------------------------------ // write the predefined timing value //------------------------------------------------------------------------------ void ECU_CAN_SetupBaudRate(u32 dwBaudRate) { u32 brp; if (dwBaudRate <= 500000) { brp = (24000000 / 16) / dwBaudRate; CAN_SetTiming(11, 4, 4, brp); } else if (dwBaudRate <= 1000000) { brp = (24000000 / 8 ) / dwBaudRate; CAN_SetTiming( 4, 3, 3, brp); } CAN->BRPR = 0; }
|