Skip to content

2515 reset watchdog #26

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions src/mcp2515.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

SPISettings mcpSPISettings(8000000, MSBFIRST, SPI_MODE0);

static TaskHandle_t intDelegateTask = NULL;

QueueHandle_t callbackQueueM15;
DRAM_ATTR TaskHandle_t intDelegateTask = NULL;
QueueHandle_t callbackQueueM15;
TaskHandle_t taskHandleReset = NULL;

void MCP_INTHandler() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
Expand Down Expand Up @@ -81,6 +81,20 @@ void task_MCPInt15( void *pvParameters )
}
}

//checks every 2 seconds to see if the chip is in error state, and reset it if so.
void task_ResetWatcher(void *pvParameters)
{
MCP2515* mcpCan = (MCP2515*)pvParameters;
const TickType_t xDelay = 2000 / portTICK_PERIOD_MS;

while (1) {
vTaskDelay(xDelay);
if(mcpCan->isInErrorState()) {
mcpCan->resetErrors();
}
}
}

void MCP2515::sendCallback(CAN_FRAME *frame)
{
//frame buffer
Expand Down Expand Up @@ -131,8 +145,9 @@ void MCP2515::initializeResources()
callbackQueueM15 = xQueueCreate(16, sizeof(CAN_FRAME));

//func desc stack, params, priority, handle to task, core to pin to
xTaskCreatePinnedToCore(&task_MCP15, "CAN_RX_M15", 4096, this, 3, NULL, 0);
xTaskCreatePinnedToCore(&task_MCPInt15, "CAN_INT_M15", 4096, this, 10, &intDelegateTask, 0);
xTaskCreatePinnedToCore(&task_MCP15, "CAN_RX_M15", 4096, this, 8, NULL, 0);
xTaskCreatePinnedToCore(&task_MCPInt15, "CAN_INT_M15", 4096, this, 19, &intDelegateTask, 0);
xTaskCreatePinnedToCore(&task_ResetWatcher, "CAN_RSTWATCH_M15", 1536, this, 7, &taskHandleReset, 0);

initializedResources = true;
}
Expand Down Expand Up @@ -1000,3 +1015,19 @@ void MCP2515::handleFrameDispatch(CAN_FRAME *frame, int filterHit)
//if none of the callback types caught this frame then queue it in the buffer
xQueueSendFromISR(rxQueue, frame, NULL);
}

bool MCP2515::isInErrorState() {
byte errorRegisterContents = Read(EFLG);
if (errorRegisterContents & EFLG_ERRORMASK) {
// Serial.print("MCP2515 is in error state. Register EFLG contains ");
// Serial.println(errorRegisterContents);
return true;
} else {
return false;
}
}

void MCP2515::resetErrors() {
BitModify(CANINTF, 0xFF, 0); // acknowledge any interrupts
BitModify(EFLG, RX1OVR | RX0OVR, 0); // clear the error flags
}
2 changes: 2 additions & 0 deletions src/mcp2515.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class MCP2515 : public CAN_COMMON
void GetRXMask(uint8_t mask, uint32_t &filterVal);
void sendCallback(CAN_FRAME *frame);
void setBuffer0RolloverBUKT(bool enable);
bool isInErrorState();
void resetErrors();

void InitFilters(bool permissive);
void intHandler();
Expand Down
10 changes: 10 additions & 0 deletions src/mcp2515_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@
#define ERRIF 0x20
#define WAKIF 0x40
#define MERRF 0x80
// EFLG
#define EFLG_ERRORMASK 0xF8 // the first 5 flags (below) are error, the last 3 are warn
#define RX1OVR 0x80
#define RX0OVR 0x40
#define TXBO 0x20
#define TXEP 0x10
#define RXEP 0x08
#define TXWAR 0x04
#define RXWAR 0x02
#define EWARN 0x01

// Configuration Registers
#define CANSTAT 0x0E
Expand Down