-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Workaround for stm32can stuck in error passive state #726
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -219,6 +219,19 @@ void Stm32Can::disable() | |
*/ | ||
void Stm32Can::tx_msg() | ||
{ | ||
// If we are error passive, the last transmission ended with an error, and | ||
// there are no free TX mailboxes, then we flush the input queue. This is a | ||
// workaround because the STM32 CAN controller can get stuck in this state | ||
// and never get to bus off if the TX attempts end up with no-ack (meaning | ||
// the controller is alone on the bus). | ||
if ((CAN->ESR & CAN_ESR_EPVF) && ((CAN->ESR & CAN_ESR_LEC_Msk) != 0) && | ||
((CAN->TSR & (CAN_TSR_TME0 | CAN_TSR_TME1 | CAN_TSR_TME2)) == 0)) | ||
{ | ||
txBuf->flush(); | ||
txBuf->signal_condition(); | ||
return; | ||
} | ||
|
||
/* see if we can send anything out */ | ||
struct can_frame *can_frame; | ||
|
||
|
@@ -435,31 +448,35 @@ void Stm32Can::sce_interrupt_handler() | |
/* error interrupt has occured */ | ||
CAN->MSR |= CAN_MSR_ERRI; // clear flag | ||
|
||
bool cancel_queue = false; | ||
|
||
if (CAN->ESR & CAN_ESR_EWGF) | ||
{ | ||
/* error warning condition */ | ||
state_ = CAN_STATE_BUS_WARNING; | ||
CAN->ESR &= ~CAN_ESR_EWGF; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure we want to remove this flag clearing, and the two below? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add this from the git commit to the PR description:
|
||
} | ||
if (CAN->ESR & CAN_ESR_EPVF) | ||
{ | ||
/* error passive condition */ | ||
++softErrorCount; | ||
state_ = CAN_STATE_BUS_PASSIVE; | ||
CAN->ESR &= ~CAN_ESR_EPVF; | ||
cancel_queue = true; | ||
} | ||
if (CAN->ESR & CAN_ESR_BOFF) | ||
{ | ||
/* bus off error condition */ | ||
++busOffCount; | ||
state_ = CAN_STATE_BUS_OFF; | ||
cancel_queue = true; | ||
} | ||
if (cancel_queue) | ||
{ | ||
CAN->TSR |= CAN_TSR_ABRQ2; | ||
CAN->TSR |= CAN_TSR_ABRQ1; | ||
CAN->TSR |= CAN_TSR_ABRQ0; | ||
CAN->IER &= ~CAN_IER_TMEIE; | ||
txBuf->flush(); | ||
txBuf->signal_condition_from_isr(); | ||
++busOffCount; | ||
state_ = CAN_STATE_BUS_OFF; | ||
CAN->ESR &= ~CAN_ESR_BOFF; | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, should we actually return, or should we allow the data to be queued up in the space created. I think it probably doesn't matter, and the return is okay. The data is likely to get stale anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two invariants that we care about and should hold:
Once we are in here, there is no free tx mailbox, so return makes sense.
Another CAN driver (https://github.com/bakerstu/openmrn/blob/master/src/freertos_drivers/ti/TivaCan.cxx#L184) does the same, after flushing the queue they do nothing.
I guess we could rotate in the newer frames into some TX mailbox, but this would be pretty complex logic, and the frames in the TX mailbox would still be outdated when they do get transmitted, just somewhat less outdated.