Skip to content

Commit a961822

Browse files
committed
ports: stm32: h7: CAN recovery
1 parent b1453cb commit a961822

7 files changed

Lines changed: 44 additions & 2 deletions

File tree

firmware/controllers/can/can.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
#include "periodic_thread_controller.h"
2121

22-
#define CAN_TIMEOUT MS2NT(100)
22+
// Try to recover CAN after following timeout
23+
#define CAN_RX_TIMEOUT TIME_MS2I(100)
2324

2425
//can tx periodic task cycle time in frequency, 200hz -> 5ms period
2526
#define CAN_CYCLE_FREQ (200.0f)

firmware/hw_layer/drivers/can/can_hw.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ class CanRead final : protected ThreadController<UTILITY_THREAD_STACK_SIZE> {
5959
void ThreadTask() override {
6060
while (true) {
6161
// Block until we get a message
62-
msg_t result = canReceiveTimeout(m_device, CAN_ANY_MAILBOX, &m_buffer, TIME_INFINITE);
62+
msg_t result = canReceiveTimeout(m_device, CAN_ANY_MAILBOX, &m_buffer, CAN_RX_TIMEOUT);
6363

6464
if (result != MSG_OK) {
65+
canHwRecover(m_index, m_device);
6566
continue;
6667
}
6768

firmware/hw_layer/ports/at32/at32_can.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,9 @@ void canHwInfo(CANDriver* cand)
132132
(void)cand;
133133
}
134134

135+
void canHwRecover(const size_t, CANDriver *)
136+
{
137+
/* TODO: */
138+
}
139+
135140
#endif /* EFI_CAN_SUPPORT */

firmware/hw_layer/ports/cypress/mpu_util.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ void canHwInfo(CANDriver* cand)
228228
(void)cand;
229229
}
230230

231+
void canHwRecover(const size_t, CANDriver *)
232+
{
233+
/* TODO: */
234+
}
235+
231236
#endif /* EFI_CAN_SUPPORT */
232237

233238
bool mcuCanFlashWhileRunning() {

firmware/hw_layer/ports/kinetis/mpu_util.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ void canHwInfo(CANDriver* cand)
221221
(void)cand;
222222
}
223223

224+
void canHwRecover(const size_t, CANDriver *)
225+
{
226+
/* TODO: */
227+
}
228+
224229
#endif /* EFI_CAN_SUPPORT */
225230

226231
bool mcuCanFlashWhileRunning() {

firmware/hw_layer/ports/mpu_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ bool isValidCanTxPin(brain_pin_e pin);
5050
bool isValidCanRxPin(brain_pin_e pin);
5151
CANDriver* detectCanDevice(brain_pin_e pinRx, brain_pin_e pinTx);
5252
void canHwInfo(CANDriver* cand);
53+
void canHwRecover(const size_t busIndex, CANDriver *cand);
5354
#endif // HAL_USE_CAN
5455

5556
// Serial

firmware/hw_layer/ports/stm32/stm32_can.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,28 @@ void canHwInfo(CANDriver* cand) {
417417
#endif
418418
}
419419

420+
void canHwRecover(const size_t busIndex, CANDriver *cand)
421+
{
422+
#if STM32_CAN_USE_FDCAN1 || STM32_CAN_USE_FDCAN2 || STM32_CAN_USE_FDCAN3
423+
// equal to TIMEOUT_INIT_MS
424+
#define TIMEOUT_RECOVER_MS 250U
425+
426+
// mostly copy-paste of static bool fdcan_active_mode(CANDriver *canp) from ChibiOS hal_can_lld.c
427+
if (cand->fdcan->CCCR & FDCAN_CCCR_INIT) {
428+
systime_t start, end;
429+
430+
/* Going in active mode then waiting for it to happen.*/
431+
cand->fdcan->CCCR &= ~FDCAN_CCCR_INIT;
432+
start = osalOsGetSystemTimeX();
433+
end = osalTimeAddX(start, TIME_MS2I(TIMEOUT_RECOVER_MS));
434+
while ((cand->fdcan->CCCR & FDCAN_CCCR_INIT) != 0U) {
435+
if (!osalTimeIsInRangeX(osalOsGetSystemTimeX(), start, end)) {
436+
return;
437+
}
438+
osalThreadSleep(1);
439+
}
440+
}
441+
#endif
442+
}
443+
420444
#endif /* EFI_CAN_SUPPORT */

0 commit comments

Comments
 (0)