Skip to content

Commit

Permalink
Fix: Extract SPI.beginTransaction from select/deselect callbacks as i…
Browse files Browse the repository at this point in the history
…t breaks when called in ISR context from mbed-based boards. (#82)
  • Loading branch information
aentinger authored Apr 3, 2023
1 parent ab1141c commit b6b3061
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 30 deletions.
13 changes: 3 additions & 10 deletions examples/MCP2515-CAN-Sniffer/MCP2515-CAN-Sniffer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,8 @@ void onReceiveBufferFull(uint32_t const, uint32_t const, uint8_t const *, uint8_
* GLOBAL VARIABLES
**************************************************************************************/

ArduinoMCP2515 mcp2515([]()
{
SPI.beginTransaction(MCP2515x_SPI_SETTING);
digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW);
},
[]()
{
digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);
SPI.endTransaction();
},
ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
[]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); },
[](uint8_t const d) { return SPI.transfer(d); },
micros,
onReceiveBufferFull,
Expand All @@ -59,6 +51,7 @@ void setup()

/* Setup SPI access */
SPI.begin();
SPI.beginTransaction(MCP2515x_SPI_SETTING);
pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT);
digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);

Expand Down
13 changes: 3 additions & 10 deletions examples/MCP2515-Filter/MCP2515-Filter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,8 @@ void onReceiveBufferFull(uint32_t const, uint32_t const, uint8_t const *, uint8_
* GLOBAL VARIABLES
**************************************************************************************/

ArduinoMCP2515 mcp2515([]()
{
SPI.beginTransaction(MCP2515x_SPI_SETTING);
digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW);
},
[]()
{
digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);
SPI.endTransaction();
},
ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
[]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); },
[](uint8_t const d) { return SPI.transfer(d); },
micros,
onReceiveBufferFull,
Expand All @@ -59,6 +51,7 @@ void setup()

/* Setup SPI access */
SPI.begin();
SPI.beginTransaction(MCP2515x_SPI_SETTING);
pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT);
digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);

Expand Down
13 changes: 3 additions & 10 deletions examples/MCP2515-Loopback/MCP2515-Loopback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,8 @@ static std::array<sCanTestFrame, 7> const CAN_TEST_FRAME_ARRAY =
* GLOBAL VARIABLES
**************************************************************************************/

ArduinoMCP2515 mcp2515([]()
{
SPI.beginTransaction(MCP2515x_SPI_SETTING);
digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW);
},
[]()
{
digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);
SPI.endTransaction();
},
ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
[]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); },
[](uint8_t const d) { return SPI.transfer(d); },
micros,
onReceiveBufferFull,
Expand All @@ -93,6 +85,7 @@ void setup()

/* Setup SPI access */
SPI.begin();
SPI.beginTransaction(MCP2515x_SPI_SETTING);
pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT);
digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);

Expand Down

0 comments on commit b6b3061

Please sign in to comment.