From b6b306103c207df61cd752d825d54c03ef89ead8 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 3 Apr 2023 12:30:55 +0200 Subject: [PATCH] Fix: Extract SPI.beginTransaction from select/deselect callbacks as it breaks when called in ISR context from mbed-based boards. (#82) --- .../MCP2515-CAN-Sniffer/MCP2515-CAN-Sniffer.ino | 13 +++---------- examples/MCP2515-Filter/MCP2515-Filter.ino | 13 +++---------- examples/MCP2515-Loopback/MCP2515-Loopback.ino | 13 +++---------- 3 files changed, 9 insertions(+), 30 deletions(-) diff --git a/examples/MCP2515-CAN-Sniffer/MCP2515-CAN-Sniffer.ino b/examples/MCP2515-CAN-Sniffer/MCP2515-CAN-Sniffer.ino index a89b0b8..f735a2d 100644 --- a/examples/MCP2515-CAN-Sniffer/MCP2515-CAN-Sniffer.ino +++ b/examples/MCP2515-CAN-Sniffer/MCP2515-CAN-Sniffer.ino @@ -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, @@ -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); diff --git a/examples/MCP2515-Filter/MCP2515-Filter.ino b/examples/MCP2515-Filter/MCP2515-Filter.ino index f591cc4..23daab8 100644 --- a/examples/MCP2515-Filter/MCP2515-Filter.ino +++ b/examples/MCP2515-Filter/MCP2515-Filter.ino @@ -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, @@ -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); diff --git a/examples/MCP2515-Loopback/MCP2515-Loopback.ino b/examples/MCP2515-Loopback/MCP2515-Loopback.ino index 3f8bc03..cf4c22f 100644 --- a/examples/MCP2515-Loopback/MCP2515-Loopback.ino +++ b/examples/MCP2515-Loopback/MCP2515-Loopback.ino @@ -67,16 +67,8 @@ static std::array 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, @@ -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);