From 288207fcdf99f473dc03687ed617dff4f3e24895 Mon Sep 17 00:00:00 2001 From: Stuart Baker Date: Tue, 7 Jun 2022 20:04:26 -0500 Subject: [PATCH 1/4] Fix debug build. --- src/freertos_drivers/common/TCAN4550Can.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/freertos_drivers/common/TCAN4550Can.cxx b/src/freertos_drivers/common/TCAN4550Can.cxx index 5e00d62e6..4739ffe0d 100644 --- a/src/freertos_drivers/common/TCAN4550Can.cxx +++ b/src/freertos_drivers/common/TCAN4550Can.cxx @@ -650,7 +650,7 @@ void *TCAN4550Can::entry() spiStatus_ = register_read(STATUS); status_ = register_read(INTERRUPT_STATUS); - MRAMMessage msg; + MRAMSPIMessage msg; msg.cmd = READ; msg.addrH = 0x10; msg.addrL = 0x00; @@ -659,7 +659,7 @@ void *TCAN4550Can::entry() spi_ioc_transfer xfer[2]; xfer[0].tx_buf = (unsigned long)&msg; xfer[0].rx_buf = 0; - xfer[0].len = sizeof(MRAMMessage); + xfer[0].len = sizeof(MRAMSPIMessage); xfer[1].tx_buf = 0; xfer[1].rx_buf = (unsigned long)regs_; xfer[1].len = sizeof(regs_); From e1b8c0d8ed867ccfd604c62c570093ebbbab2e70 Mon Sep 17 00:00:00 2001 From: Stuart Baker Date: Tue, 7 Jun 2022 20:05:07 -0500 Subject: [PATCH 2/4] Move TX FIFO flush to the bus-off state. --- src/freertos_drivers/common/TCAN4550Can.cxx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/freertos_drivers/common/TCAN4550Can.cxx b/src/freertos_drivers/common/TCAN4550Can.cxx index 4739ffe0d..5aef7b9c4 100644 --- a/src/freertos_drivers/common/TCAN4550Can.cxx +++ b/src/freertos_drivers/common/TCAN4550Can.cxx @@ -712,11 +712,6 @@ void *TCAN4550Can::entry() // error passive state ++softErrorCount; state_ = CAN_STATE_BUS_PASSIVE; - - // cancel TX FIFO buffers - register_write(TXBCR, TX_FIFO_BUFFERS_MASK); - - txBuf->signal_condition(); } else { @@ -730,6 +725,12 @@ void *TCAN4550Can::entry() // bus off ++busOffCount; state_ = CAN_STATE_BUS_OFF; + + // cancel TX FIFO buffers + register_write(TXBCR, TX_FIFO_BUFFERS_MASK); + + txBuf->signal_condition(); + } } } } From 218073a2f1bc1033632cfc5956a186b0ce39da84 Mon Sep 17 00:00:00 2001 From: Stuart Baker Date: Tue, 7 Jun 2022 20:05:45 -0500 Subject: [PATCH 3/4] Handle bus-off recovery. --- src/freertos_drivers/common/TCAN4550Can.cxx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/freertos_drivers/common/TCAN4550Can.cxx b/src/freertos_drivers/common/TCAN4550Can.cxx index 5aef7b9c4..d0b4290dd 100644 --- a/src/freertos_drivers/common/TCAN4550Can.cxx +++ b/src/freertos_drivers/common/TCAN4550Can.cxx @@ -725,12 +725,23 @@ void *TCAN4550Can::entry() // bus off ++busOffCount; state_ = CAN_STATE_BUS_OFF; + // attempt recovery + Cccr cccr; + do + { + cccr.data = 0; + register_write(CCCR, cccr.data); + cccr.data = register_read(CCCR); + } while (cccr.init == 1); // cancel TX FIFO buffers register_write(TXBCR, TX_FIFO_BUFFERS_MASK); txBuf->signal_condition(); } + else + { + state_ = CAN_STATE_ACTIVE; } } } From 3fa5b5995045150004f2363d6fd1c91821450f38 Mon Sep 17 00:00:00 2001 From: Stuart Baker Date: Tue, 7 Jun 2022 20:06:44 -0500 Subject: [PATCH 4/4] Add commentary about the memory write contract. --- src/freertos_drivers/common/TCAN4550Can.hxx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/freertos_drivers/common/TCAN4550Can.hxx b/src/freertos_drivers/common/TCAN4550Can.hxx index 11a2497b3..65aea3a27 100644 --- a/src/freertos_drivers/common/TCAN4550Can.hxx +++ b/src/freertos_drivers/common/TCAN4550Can.hxx @@ -1124,6 +1124,10 @@ private: MCANInterrupt mcanInterruptEnable_; ///< shadow for the interrupt enable uint32_t txCompleteMask_; ///< shadow for the transmit complete buffer mask uint8_t state_; ///< present bus state + + // These bitmasks are protected from a read-modify-write race condition + // because they are only set from a thread context that holds the SPI bus + // mutex. uint8_t txPending_ : 1; ///< waiting on a TX active event uint8_t rxPending_ : 1; ///< waiting on a RX active event