From f02d75bfbe795fee3c4957437c53fdbdebe104fe Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Sat, 13 Mar 2021 08:43:51 +1100 Subject: [PATCH] Some initial work for same51 board support --- platformio.ini | 13 ++++- .../{src/boards/samd21.h => board_same51.h} | 57 +++++++++++++++---- speeduino/board_same51.ino | 41 +++++++++++++ speeduino/globals.h | 20 ++++++- speeduino/globals.ino | 9 ++- speeduino/speeduino.h | 2 + speeduino/src/FastCRC/FastCRC_tables.h | 2 +- speeduino/src/FlashStorage/FlashAsEEPROM.cpp | 2 +- speeduino/src/FlashStorage/FlashAsEEPROM.h | 25 ++++++++ speeduino/src/FlashStorage/FlashStorage.cpp | 2 +- speeduino/src/SPIAsEEPROM/SPIFlash.h | 2 +- 11 files changed, 154 insertions(+), 21 deletions(-) rename speeduino/{src/boards/samd21.h => board_same51.h} (73%) create mode 100644 speeduino/board_same51.ino diff --git a/platformio.ini b/platformio.ini index 3a22451e..0edaa5f2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -108,7 +108,18 @@ build_flags = -fpermissive -std=gnu++11 -Os -DCORE_STM32_OFFICIAL -UBOARD_MAX_IO platform = atmelsam framework = arduino board = zeroUSB -;lib_deps = EEPROM +;lib_deps = cmaglie/FlashStorage @ ^1.0.0 +;build_flags = -fpermissive -std=gnu++11 -DUSE_SPI_EEPROM +build_flags = -fpermissive -std=gnu++11 +upload_protocol = sam-ba + +;SAME51 +[env:same51] +platform = atmelsam +framework = arduino +board = adafruit_feather_m4_can +;lib_deps = cmaglie/FlashStorage @ ^1.0.0 +;build_flags = -fpermissive -std=gnu++11 -DUSE_SPI_EEPROM build_flags = -fpermissive -std=gnu++11 upload_protocol = sam-ba diff --git a/speeduino/src/boards/samd21.h b/speeduino/board_same51.h similarity index 73% rename from speeduino/src/boards/samd21.h rename to speeduino/board_same51.h index 713ebb81..415a2634 100644 --- a/speeduino/src/boards/samd21.h +++ b/speeduino/board_same51.h @@ -1,6 +1,6 @@ -#ifndef SAMD21_H -#define SAMD21_H -#if defined(CORE_SAMD21) +#ifndef SAME51_H +#define SAME51_H +#if defined(CORE_SAME51) #include "sam.h" @@ -12,6 +12,39 @@ #define BOARD_MAX_DIGITAL_PINS 54 //digital pins +1 #define BOARD_MAX_IO_PINS 58 //digital pins + analog channels + 1 + //#define PORT_TYPE uint8_t //Size of the port variables (Eg inj1_pin_port). + #define PINMASK_TYPE uint8_t + #define COMPARE_TYPE uint16_t + #define COUNTER_TYPE uint16_t + #ifdef USE_SPI_EEPROM + #define EEPROM_LIB_H "src/SPIAsEEPROM/SPIAsEEPROM.h" + #include EEPROM_LIB_H + //SPIClass SPI_for_flash(1, 2, 3); //SPI1_MOSI, SPI1_MISO, SPI1_SCK + SPIClass SPI_for_flash = SPI; //SPI1_MOSI, SPI1_MISO, SPI1_SCK + + //windbond W25Q16 SPI flash EEPROM emulation + EEPROM_Emulation_Config EmulatedEEPROMMconfig{255UL, 4096UL, 31, 0x00100000UL}; + //Flash_SPI_Config SPIconfig{USE_SPI_EEPROM, SPI_for_flash}; + SPI_EEPROM_Class EEPROM(EmulatedEEPROMMconfig, SPIconfig); + #else + //#define EEPROM_LIB_H + #define EEPROM_LIB_H "src/FlashStorage/FlashAsEEPROM.h" + #endif + #define RTC_LIB_H "TimeLib.h" + void initBoard(); + uint16_t freeRam(); + void doSystemReset(); + void jumpToBootloader(); + + #if defined(TIMER5_MICROS) + /*#define micros() (((timer5_overflow_count << 16) + TCNT5) * 4) */ //Fast version of micros() that uses the 4uS tick of timer5. See timers.ino for the overflow ISR of timer5 + #define millis() (ms_counter) //Replaces the standard millis() function with this macro. It is both faster and more accurate. See timers.ino for its counter increment. + static inline unsigned long micros_safe(); //A version of micros() that is interrupt safe + #else + #define micros_safe() micros() //If the timer5 method is not used, the micros_safe() macro is simply an alias for the normal micros() + #endif + #define pinIsReserved(pin) ( ((pin) == 0) ) //Forbiden pins like USB on other boards + //Additional analog pins (These won't work without other changes) #define PIN_A6 (8ul) #define PIN_A7 (9ul) @@ -21,7 +54,6 @@ #define PIN_A14 (9ul) #define PIN_A15 (9ul) - static const uint8_t A6 = PIN_A6; static const uint8_t A7 = PIN_A7; static const uint8_t A8 = PIN_A8; static const uint8_t A9 = PIN_A9; @@ -34,15 +66,16 @@ * Schedules */ //See : https://electronics.stackexchange.com/questions/325159/the-value-of-the-tcc-counter-on-an-atsam-controller-always-reads-as-zero + // SAME512 Timer channel list: https://user-images.githubusercontent.com/11770912/62131781-2e150b80-b31f-11e9-9970-9a6c2356a17c.png #define FUEL1_COUNTER TCC0->COUNT.reg #define FUEL2_COUNTER TCC0->COUNT.reg #define FUEL3_COUNTER TCC0->COUNT.reg #define FUEL4_COUNTER TCC0->COUNT.reg //The below are NOT YET RIGHT! - #define FUEL5_COUNTER TCC0->COUNT.reg - #define FUEL6_COUNTER TCC0->COUNT.reg - #define FUEL7_COUNTER TCC0->COUNT.reg - #define FUEL8_COUNTER TCC0->COUNT.reg + #define FUEL5_COUNTER TCC1->COUNT.reg + #define FUEL6_COUNTER TCC1->COUNT.reg + #define FUEL7_COUNTER TCC1->COUNT.reg + #define FUEL8_COUNTER TCC1->COUNT.reg #define IGN1_COUNTER TCC1->COUNT.reg #define IGN2_COUNTER TCC1->COUNT.reg @@ -59,10 +92,10 @@ #define FUEL3_COMPARE TCC0->CC[2].bit.CC #define FUEL4_COMPARE TCC0->CC[3].bit.CC //The below are NOT YET RIGHT! - #define FUEL5_COMPARE TCC0->CC0 - #define FUEL6_COMPARE TCC0->CC1 - #define FUEL7_COMPARE TCC0->CC2 - #define FUEL8_COMPARE TCC0->CC3 + #define FUEL5_COMPARE TCC1->CC[0].bit.CC + #define FUEL6_COMPARE TCC1->CC[1].bit.CC + #define FUEL7_COMPARE TCC1->CC[2].bit.CC + #define FUEL8_COMPARE TCC1->CC[3].bit.CC #define IGN1_COMPARE TCC1->CC[0].bit.CC #define IGN2_COMPARE TCC1->CC[1].bit.CC diff --git a/speeduino/board_same51.ino b/speeduino/board_same51.ino new file mode 100644 index 00000000..47462f6a --- /dev/null +++ b/speeduino/board_same51.ino @@ -0,0 +1,41 @@ +#if defined(CORE_SAME51) +#include "globals.h" +#include "auxiliaries.h" + +void initBoard() +{ + /* + *********************************************************************************************************** + * General + */ + + /* + *********************************************************************************************************** + * Timers + */ + + /* + *********************************************************************************************************** + * Auxilliaries + */ + + /* + *********************************************************************************************************** + * Idle + */ + + /* + *********************************************************************************************************** + * Schedules + */ +} + +uint16_t freeRam() +{ + return 0; +} + +void doSystemReset() { return; } +void jumpToBootloader() { return; } + +#endif \ No newline at end of file diff --git a/speeduino/globals.h b/speeduino/globals.h index d53ea4f2..03a956b9 100644 --- a/speeduino/globals.h +++ b/speeduino/globals.h @@ -101,9 +101,19 @@ #elif defined(__SAMD21G18A__) #define BOARD_H "board_samd21.h" #define CORE_SAMD21 + #define CORE_SAM + #define INJ_CHANNELS 4 + #define IGN_CHANNELS 4 #elif defined(__SAMC21J18A__) #define BOARD_H "board_samc21.h" #define CORE_SAMC21 + #define CORE_SAM +#elif defined(__SAME51J19A__) + #define BOARD_H "board_same51.h" + #define CORE_SAME51 + #define CORE_SAM + #define INJ_CHANNELS 8 + #define IGN_CHANNELS 8 #else #error Incorrect board selected. Please select the correct board (Usually Mega 2560) and upload again #endif @@ -507,8 +517,14 @@ extern volatile byte toothHistorySerialIndex; extern unsigned long currentLoopTime; /**< The time (in uS) that the current mainloop started */ extern unsigned long previousLoopTime; /**< The time (in uS) that the previous mainloop started */ extern volatile uint16_t ignitionCount; /**< The count of ignition events that have taken place since the engine started */ -extern byte primaryTriggerEdge; -extern byte secondaryTriggerEdge; +//The below shouldn't be needed and probably should be cleaned up, but the Atmel SAM boards use a specific type for the trigger edge values rather than a simple byte/int +#if defined(CORE_SAMD21) + extern PinStatus primaryTriggerEdge; + extern PinStatus secondaryTriggerEdge; +#else + extern byte primaryTriggerEdge; + extern byte secondaryTriggerEdge; +#endif extern int CRANK_ANGLE_MAX; extern int CRANK_ANGLE_MAX_IGN; extern int CRANK_ANGLE_MAX_INJ; //The number of crank degrees that the system track over. 360 for wasted / timed batch and 720 for sequential diff --git a/speeduino/globals.ino b/speeduino/globals.ino index ec9f3c31..8f458477 100644 --- a/speeduino/globals.ino +++ b/speeduino/globals.ino @@ -136,8 +136,13 @@ volatile byte toothHistorySerialIndex = 0; unsigned long currentLoopTime; /**< The time (in uS) that the current mainloop started */ unsigned long previousLoopTime; /**< The time (in uS) that the previous mainloop started */ volatile uint16_t ignitionCount; /**< The count of ignition events that have taken place since the engine started */ -byte primaryTriggerEdge; -byte secondaryTriggerEdge; +#if defined(CORE_SAMD21) + PinStatus primaryTriggerEdge; + PinStatus secondaryTriggerEdge; +#else + byte primaryTriggerEdge; + byte secondaryTriggerEdge; +#endif int CRANK_ANGLE_MAX = 720; int CRANK_ANGLE_MAX_IGN = 360; int CRANK_ANGLE_MAX_INJ = 360; //The number of crank degrees that the system track over. 360 for wasted / timed batch and 720 for sequential diff --git a/speeduino/speeduino.h b/speeduino/speeduino.h index 3eb82f42..80dc6868 100644 --- a/speeduino/speeduino.h +++ b/speeduino/speeduino.h @@ -13,6 +13,8 @@ #define SPEEDUINO_H //#include "globals.h" +void setup(); +void loop(); uint16_t PW(int REQ_FUEL, byte VE, long MAP, uint16_t corrections, int injOpen); byte getVE1(); byte getAdvance1(); diff --git a/speeduino/src/FastCRC/FastCRC_tables.h b/speeduino/src/FastCRC/FastCRC_tables.h index e20ddd7e..b3a36614 100755 --- a/speeduino/src/FastCRC/FastCRC_tables.h +++ b/speeduino/src/FastCRC/FastCRC_tables.h @@ -31,7 +31,7 @@ #define FastCRC_tables #include "inttypes.h" -#if defined(__AVR__) || defined(STM32_MCU_SERIES) || defined(ARDUINO_ARCH_STM32) || defined(_VARIANT_ARDUINO_STM32_) || defined(__SAMD21G18A__) || defined(__IMXRT1062__) +#if defined(__AVR__) || defined(STM32_MCU_SERIES) || defined(ARDUINO_ARCH_STM32) || defined(_VARIANT_ARDUINO_STM32_) || defined(__SAMD21G18A__) || defined(__IMXRT1062__) || defined(__SAME51J19A__) #include #else #include diff --git a/speeduino/src/FlashStorage/FlashAsEEPROM.cpp b/speeduino/src/FlashStorage/FlashAsEEPROM.cpp index 2950abe4..735258e2 100755 --- a/speeduino/src/FlashStorage/FlashAsEEPROM.cpp +++ b/speeduino/src/FlashStorage/FlashAsEEPROM.cpp @@ -18,7 +18,7 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#if defined(__SAMD21G18A__) +#if defined(CORE_SAM) && !defined(USE_SPI_EEPROM) #include "FlashAsEEPROM.h" FlashStorage(eeprom_storage, EEPROM_EMULATION); diff --git a/speeduino/src/FlashStorage/FlashAsEEPROM.h b/speeduino/src/FlashStorage/FlashAsEEPROM.h index 8ea3a913..e538e304 100755 --- a/speeduino/src/FlashStorage/FlashAsEEPROM.h +++ b/speeduino/src/FlashStorage/FlashAsEEPROM.h @@ -60,6 +60,31 @@ class EEPROMClass { */ void update(int, uint8_t); + /** + * Read AnyTypeOfData from eeprom + * @param address + * @return AnyTypeOfData + */ + template< typename T > T &get( int idx, T &t ){ + uint16_t e = idx; + uint8_t *ptr = (uint8_t*) &t; + for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = read(e); + return t; + } + + /** + * Write AnyTypeOfData to eeprom + * @param address + * @param AnyTypeOfData + * @return number of bytes written to flash + */ + template< typename T > const T &put( int idx, const T &t ){ + const uint8_t *ptr = (const uint8_t*) &t; + uint16_t e = idx; + for( int count = sizeof(T) ; count ; --count, ++e ) update(e, *ptr++); + return t; + } + /** * Check whether the eeprom data is valid * @return true, if eeprom data is valid (has been written at least once), false if not diff --git a/speeduino/src/FlashStorage/FlashStorage.cpp b/speeduino/src/FlashStorage/FlashStorage.cpp index dd09615c..6e28376d 100755 --- a/speeduino/src/FlashStorage/FlashStorage.cpp +++ b/speeduino/src/FlashStorage/FlashStorage.cpp @@ -16,7 +16,7 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#if defined(__SAMD21G18A__) +#if defined(CORE_SAM) && !defined(USE_SPI_EEPROM) #include "FlashStorage.h" diff --git a/speeduino/src/SPIAsEEPROM/SPIFlash.h b/speeduino/src/SPIAsEEPROM/SPIFlash.h index 7caa87ed..2732f662 100755 --- a/speeduino/src/SPIAsEEPROM/SPIFlash.h +++ b/speeduino/src/SPIAsEEPROM/SPIFlash.h @@ -34,7 +34,7 @@ class SPIFlash { public: //------------------------------------ Constructor ------------------------------------// //New Constructor to Accept the PinNames as a Chip select Parameter - @boseji 02.03.17 - #if defined (ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) + #if defined (ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) || defined (ARDUINO_ARCH_SAME) SPIFlash(uint8_t cs = CS, SPIClass *spiinterface=&SPI); #elif defined (BOARD_RTL8195A) SPIFlash(PinName cs = CS);