From bd3e71fc31eaf623f6e333ab84c6995bb3395a93 Mon Sep 17 00:00:00 2001 From: nrs1g15 <nrs1g15@soton.ac.uk> Date: Thu, 1 Aug 2019 22:38:26 +0100 Subject: [PATCH] Added arduino interrupt --- sufst-controller/can.cpp | 44 ++++++++-------------------------------- sufst-controller/can.h | 2 +- 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/sufst-controller/can.cpp b/sufst-controller/can.cpp index cf6a339..781ede0 100644 --- a/sufst-controller/can.cpp +++ b/sufst-controller/can.cpp @@ -45,6 +45,13 @@ extern void ecuCanMsg0x2008(CanMsgTemplate *canMsg); extern void ecuCanMsg0x2009(CanMsgTemplate *canMsg); +static void canISR(); + +static void canISR() +{ + canSaveMsg(); +} + // ECU can messages function pointers for calling the corresponding function when that msg is seen. // Used in function canParseRxMsg. void @@ -68,6 +75,8 @@ uint8_t canBegin() pinMode(CAN_INT_PIN, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), canISR, FALLING); + // Initialize the Arduino CAN class with slave select pin. can.init_CS(CAN_CS_PIN); @@ -145,41 +154,6 @@ void canAddTxRequest(CanMsgTemplate *canMsg) cirBufferWrite(&canTxCirBuffer, canMsg); } -// External interrupt currently only designed to work with atmega 2560 -#if !defined(__AVR_ATmega2560__) -#error "PLATFORM NOT SUPPORTED CURRENTLY" -#endif // !defined(atmega2560) - -#if CAN_INT_PIN != 2 -#error "CAN_INT_PIN CHANGED FROM 2" -#endif //CAN_INT_PIN != 2 - -void canBeginExtInterrupt() -{ - noInterrupts(); - - // On the mega 2560 pin 2 is external interrupt 4 - EIMSK &= ~(1 << INT4); - - EICRB &= ~(1 << ISC41); - //EICRB &= ~(1 << ISC40); - - EIMSK |= (1 << INT4); - - interrupts(); -} - -ISR(INT4_vect) { - - noInterrupts(); - - lastCanMsgRecTimeMs = millis(); - - canSaveMsg(); - - interrupts(); -} - void canParseRxMsg(CanMsgTemplate *canMsg) { if ((canMsg->id >= 0x2000) && (canMsg->id <= 0x200A)) { diff --git a/sufst-controller/can.h b/sufst-controller/can.h index f01e8d4..5725d55 100644 --- a/sufst-controller/can.h +++ b/sufst-controller/can.h @@ -9,7 +9,7 @@ // CAN_INT_PIN MUST BE 2 #define CAN_INT_PIN 2 -#define CAN_CS_PIN 9 +#define CAN_CS_PIN 10 #define CAN_RX_BUFFER_LEN 32 #define CAN_TX_BUFFER_LEN 32 -- GitLab