ESP32-Arduino-CAN
ESP32-Arduino-CAN copied to clipboard
Baud rate prescaler only 6 bits
I am trying to implement 33.33kbps CAN mode for single-wire GMLAN low speed communication. To do this, I need to use a baud rate prescaler value of 74. According to the expressif docs:
https://docs.espressif.com/projects/esp-idf/en/release-v3.3/api-reference/peripherals/can.html
"The brp can be any even number from 2 to 128. If the ESP32 is a revision 2 or later chip, the brp will also support any multiple of 4 from 132 to 256"
However, setting any value higher than 64 results in clipping, which makes sense since can_regdef.h in this library has the following:
union {
uint32_t U; /**< \brief Unsigned access */
struct {
unsigned int BRP : 6; /**< \brief BTR0[5:0] Baud Rate Prescaler */
unsigned int SJW : 2; /**< \brief BTR0[7:6] Synchronization Jump Width*/
unsigned int reserved_24 : 24; /**< \brief \internal Reserved */
} B;
} BTR0;
This appears to limit the BRP value to 6 bits. Does anyone know how to use BRP values greater than 64?
After looking into this a bit more, I think this is more a problem with the expressif docs than the library. The SJA1000 datasheet seems to show that only 6 bits are allocated for the BRP, so this appears to be a hardware limitation.
For anyone trying to get 33.33kbps mode working, I found some alternate settings that worked with a BRP <64.
80MHz APB clock Tseg1 16 Tseg2 3 BRP 60 TQ 1.5 Bit time 30uS Register values are 1 lower than above values
I added the following to CAN.c to allow selection of 33.3kbps mode:
case CAN_SPEED_33KBPS:
MODULE_CAN->BTR1.B.TSEG1 = 0xf;
MODULE_CAN->BTR1.B.TSEG2 = 0x2;
__tq = 1.5;
break;