Skip to content

Commit 6e18433

Browse files
committed
clean up implementation
1 parent c5cb58c commit 6e18433

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/machine/can.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (can *CAN) TxFIFOLevel() (level int, maxlevel int) {
4444

4545
// Tx puts a CAN frame in TxFIFO for transmission. Returns error if TxFIFO is full.
4646
func (can *CAN) Tx(id canID, flags canFlags, data []byte) error {
47-
return can.tx(id, extendedID, data)
47+
return can.tx(id, flags, data)
4848
}
4949

5050
// RxFIFOLevel returns amount of CAN frames received and stored and total Rx fifo length.
@@ -53,8 +53,10 @@ func (can *CAN) RxFIFOLevel() (level int, maxlevel int) {
5353
return can.rxFIFOLevel()
5454
}
5555

56+
type canRxCallback = func(data []byte, id canID, timestamp uint32, flags canFlags)
57+
5658
// SetRxCallback sets the receive callback. See [canFlags] for information on how bits are layed out.
57-
func (can *CAN) SetRxCallback(cb func(data []byte, id canID, timestamp uint32, flags canFlags)) {
59+
func (can *CAN) SetRxCallback(cb canRxCallback) {
5860
can.setRxCallback(cb)
5961
}
6062

src/machine/machine_stm32g0_can.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ const (
111111

112112
// CANConfig holds FDCAN configuration parameters
113113
type CANConfig struct {
114-
TransferRate CANTransferRate // Nominal bit rate (arbitration phase)
115-
TransferRateFD CANTransferRate // Data bit rate (data phase), must be >= TransferRate
116-
Mode CANMode
117-
Tx Pin
118-
Rx Pin
114+
TransferRate CANTransferRate // Nominal bit rate (arbitration phase)
115+
TransferRateFD CANTransferRate // Data bit rate (data phase), must be >= TransferRate
116+
Mode CANMode
117+
Tx Pin
118+
Rx Pin
119119
Standby Pin // Optional standby pin for CAN transceiver (set to NoPin if not used)
120120
AlwaysFD bool // Always transmit as FD frames, even when data fits in classic CAN
121121
EnableRxInterrupt bool // Enable interrupt-driven receive (messages delivered via SetRxCallback)
@@ -145,7 +145,7 @@ func enableFDCANClock() {
145145
}
146146

147147
// flags implemented as described in [CAN.SetRxCallback]
148-
var canRxCB [2]func(data []byte, id uint32, extendedID bool, timestamp uint32, flags uint32)
148+
var canRxCB [2]canRxCallback
149149

150150
// canInstances tracks CAN peripherals with interrupt-driven RX enabled.
151151
// A non-nil entry means setRxCallback was called with a non-nil callback.
@@ -289,7 +289,7 @@ func (can *CAN) txFIFOLevel() (int, int) {
289289
}
290290

291291
// tx implements [CAN.Tx].
292-
func (can *CAN) tx(id uint32, extendedID bool, data []byte) error {
292+
func (can *CAN) tx(id canID, flags canFlags, data []byte) error {
293293
if can.Bus.TXFQS.Get()&0x00200000 != 0 { // TFQF bit
294294
return errCANTxFifoFull
295295
}
@@ -300,14 +300,14 @@ func (can *CAN) tx(id uint32, extendedID bool, data []byte) error {
300300
}
301301

302302
// Use FD framing if configured to always use FD, or if data exceeds classic CAN max.
303-
isFD := can.alwaysFD || length > 8
303+
isFD := flags&canFlagFDF != 0 || length > 8
304304

305305
putIndex := (can.Bus.TXFQS.Get() >> 16) & 0x03 // TFQPI[1:0]
306306
txAddr := can.sramBase() + sramcanTFQSA + uintptr(putIndex)*sramcanTFQSize
307307

308308
// Header word 1: identifier and flags.
309309
var w1 uint32
310-
if extendedID {
310+
if flags&canFlagESI != 0 {
311311
w1 = (id & 0x1FFFFFFF) | fdcanElementMaskXTD
312312
} else {
313313
w1 = (id & 0x7FF) << 18
@@ -350,7 +350,7 @@ func (can *CAN) rxFIFOLevel() (int, int) {
350350
// setRxCallback implements [CAN.SetRxCallback].
351351
// When cb is non-nil, interrupt-driven receive is enabled on RX FIFO 0.
352352
// The CAN.Interrupt field must be initialized with interrupt.New in the board file.
353-
func (can *CAN) setRxCallback(cb func(data []byte, id uint32, extendedID bool, timestamp uint32, flags uint32)) {
353+
func (can *CAN) setRxCallback(cb canRxCallback) {
354354
canRxCB[can.instance] = cb
355355
if cb != nil {
356356
canInstances[can.instance] = can
@@ -381,7 +381,7 @@ func (can *CAN) rxPoll() error {
381381

382382
// processRxFIFO0 drains RX FIFO 0 and delivers each message to cb.
383383
// Used by both rxPoll (poll mode) and canHandleInterrupt (interrupt mode).
384-
func processRxFIFO0(can *CAN, cb func(data []byte, id uint32, extendedID bool, timestamp uint32, flags uint32)) {
384+
func processRxFIFO0(can *CAN, cb canRxCallback) {
385385
for can.Bus.RXF0S.Get()&0x0F != 0 {
386386
getIndex := (can.Bus.RXF0S.Get() >> 8) & 0x03 // F0GI[1:0]
387387
rxAddr := can.sramBase() + sramcanRF0SA + uintptr(getIndex)*sramcanRF0Size
@@ -391,7 +391,9 @@ func processRxFIFO0(can *CAN, cb func(data []byte, id uint32, extendedID bool, t
391391

392392
extendedID := w1&fdcanElementMaskXTD != 0
393393
var id uint32
394+
var flags uint32
394395
if extendedID {
396+
flags |= canFlagIDE
395397
id = w1 & fdcanElementMaskEXTID
396398
} else {
397399
id = (w1 & fdcanElementMaskSTDID) >> 18
@@ -401,18 +403,17 @@ func processRxFIFO0(can *CAN, cb func(data []byte, id uint32, extendedID bool, t
401403
dlc := byte((w2 & fdcanElementMaskDLC) >> 16)
402404
isFD := w2&fdcanElementMaskFDF != 0
403405

404-
var flags uint32
405406
if isFD {
406-
flags |= 1 // bit 0: FD frame
407+
flags |= canFlagFDF
407408
}
408409
if w1&fdcanElementMaskRTR != 0 {
409-
flags |= 2 // bit 1: RTR
410+
flags |= canFlagRTR
410411
}
411412
if w2&fdcanElementMaskBRS != 0 {
412-
flags |= 4 // bit 2: BRS
413+
flags |= canFlagBRS
413414
}
414415
if w1&fdcanElementMaskESI != 0 {
415-
flags |= 8 // bit 3: ESI
416+
flags |= canFlagESI
416417
}
417418

418419
dataLen := dlcToLength(dlc)
@@ -430,7 +431,7 @@ func processRxFIFO0(can *CAN, cb func(data []byte, id uint32, extendedID bool, t
430431

431432
// Acknowledge before callback so the FIFO slot is freed.
432433
can.Bus.RXF0A.Set(uint32(getIndex))
433-
cb(buf[:dataLen], id, extendedID, timestamp, flags)
434+
cb(buf[:dataLen], id, timestamp, flags)
434435
}
435436
}
436437

0 commit comments

Comments
 (0)