Discussion:
[libopencm3-devel] lib/stm32/can.c problem in can_init() and CAN_BTR_BRP_MASK
Warren Gay
2017-05-06 16:25:36 UTC
Permalink
While debugging CAN for STMF103C8T6, I came across a small problem in
can_init() that needs correction:

lib/stm32/can.c around line 176:

/* Set bit timings. */
CAN_BTR(canport) |= sjw | ts2 | ts1 |
((brp - 1ul) & CAN_BTR_BRP_MASK);

The problem with this statement is the "|=" part. I discovered I was
getting a high bit on in the BRP field because of a pre-existing value. If
I supply a different value than the current value, it just gets OR-ed into
the register with the existing bits. Obviously unintended behaviour. The
same problem exists for the SWJ/TS2/TS1 bit fields.

There is one additional problem in the header file
include/libopencm3/stm32/can.h near line 483:

/* BRP[9:0]: Baud rate prescaler */
#define CAN_BTR_BRP_MASK (0x1FFUL << 0)

This ten bit field should have the mask value of 0x3FFUL, rather than 1FFUL.

Thanks, Warren

Loading...