From 6135272c329100248135d96d9d96a11936a1756f Mon Sep 17 00:00:00 2001 From: pmantoine Date: Tue, 17 May 2022 18:06:08 +0800 Subject: [PATCH] SAMD Support Initial Patches --- DCC.cpp | 4 ++ DCCEXParser.cpp | 3 + DCCTimer.h | 4 ++ DCCTimerSAMD.cpp | 170 ++++++++++++++++++++++++++++++++++++++++++++++ EEStore.h | 2 +- MotorDriver.cpp | 7 +- MotorDriver.h | 3 +- MotorDrivers.h | 8 +++ SerialManager.cpp | 6 ++ defines.h | 3 +- platformio.ini | 15 +++- 11 files changed, 220 insertions(+), 5 deletions(-) create mode 100644 DCCTimerSAMD.cpp diff --git a/DCC.cpp b/DCC.cpp index 8af4ccd..b3b1595 100644 --- a/DCC.cpp +++ b/DCC.cpp @@ -62,7 +62,11 @@ byte DCC::globalSpeedsteps=128; void DCC::begin(const FSH * motorShieldName) { shieldName=(FSH *)motorShieldName; +#if defined(ARDUINO_ARCH_SAMD) + StringFormatter::send(SerialUSB,F("\n"), F(VERSION), F(ARDUINO_TYPE), shieldName, F(GITHUB_SHA)); +#else StringFormatter::send(Serial,F("\n"), F(VERSION), F(ARDUINO_TYPE), shieldName, F(GITHUB_SHA)); +#endif #ifndef DISABLE_EEPROM // Load stuff from EEprom diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index b434428..92511aa 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -1,4 +1,5 @@ /* + * © 2022 Paul M Antoine * © 2021 Neil McKechnie * © 2021 Mike S * © 2021 Herb Morton @@ -881,7 +882,9 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[]) wdt_enable( WDTO_15MS); // set Arduino watchdog timer for 15ms delay(50); // wait for the prescaller time to expire #else +#if defined(ARDUINO_ARCH_ESP) ESP.restart(); +#endif #endif break; // and if we didnt restart } diff --git a/DCCTimer.h b/DCCTimer.h index 4ad507c..913762d 100644 --- a/DCCTimer.h +++ b/DCCTimer.h @@ -1,4 +1,5 @@ /* + * © 2022 Paul M Antoine * © 2021 Mike S * © 2021 Harald Barth * © 2021 Fred Decker @@ -121,6 +122,9 @@ private: #elif defined(ARDUINO_ARCH_ESP32) #define ARDUINO_TYPE "ESP32" #undef HAS_AVR_WDT +#elif defined(ARDUINO_ARCH_SAMD) +#define ARDUINO_TYPE "SAMD21" +#undef HAS_AVR_WDT #else #error CANNOT COMPILE - DCC++ EX ONLY WORKS WITH THE ARCHITECTURES LISTED IN DCCTimer.h #endif diff --git a/DCCTimerSAMD.cpp b/DCCTimerSAMD.cpp new file mode 100644 index 0000000..b03962b --- /dev/null +++ b/DCCTimerSAMD.cpp @@ -0,0 +1,170 @@ +/* + * © 2022 Paul M Antoine + * © 2021 Mike S + * © 2021 Harald Barth + * © 2021 Fred Decker + * © 2021 Chris Harlow + * © 2021 David Cutting + * All rights reserved. + * + * This file is part of Asbelos DCC API + * + * This is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * It is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CommandStation. If not, see . + */ + +// ATTENTION: this file only compiles on a SAMD21 based board +// Please refer to DCCTimer.h for general comments about how this class works +// This is to avoid repetition and duplication. +#ifdef ARDUINO_ARCH_SAMD + +#include "FSH.h" //PMA temp debug +#include "DIAG.h" //PMA temp debug +#include "DCCTimer.h" + +INTERRUPT_CALLBACK interruptHandler=0; + +void DCCTimer::begin(INTERRUPT_CALLBACK callback) { + interruptHandler=callback; + noInterrupts(); + + // Set up ADC to do faster reads... default for Arduino Zero platform configs is 436uS, + // and we need sub-100uS. This code sets it to a read speed of around 21uS, and for now + // enables 10-bit mode, although 12-bit is possible + ADC->CTRLA.bit.ENABLE = 0; // disable ADC + while( ADC->STATUS.bit.SYNCBUSY == 1 ); // wait for synchronization + + ADC->CTRLB.reg &= 0b1111100011111111; // mask PRESCALER bits + ADC->CTRLB.reg |= ADC_CTRLB_PRESCALER_DIV64 | // divide Clock by 64 + ADC_CTRLB_RESSEL_10BIT; // Result on 10 bits default, 12 bits possible + + ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_1 | // take 1 sample at a time + ADC_AVGCTRL_ADJRES(0x00ul); // adjusting result by 0 + ADC->SAMPCTRL.reg = 0x00; // sampling Time Length = 0 + + ADC->CTRLA.bit.ENABLE = 1; // enable ADC + while(ADC->STATUS.bit.SYNCBUSY == 1); // wait for synchronization + + // Timer setup - setup clock sources first + REG_GCLK_GENDIV = GCLK_GENDIV_DIV(1) | // Divide 48MHz by 1 + GCLK_GENDIV_ID(4); // Apply to GCLK4 + while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization + + REG_GCLK_GENCTRL = GCLK_GENCTRL_GENEN | // Enable GCLK + GCLK_GENCTRL_SRC_DFLL48M | // Set the 48MHz clock source + GCLK_GENCTRL_ID(4); // Select GCLK4 + while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization + + REG_GCLK_CLKCTRL = GCLK_CLKCTRL_CLKEN | // Enable generic clock + 4 << GCLK_CLKCTRL_GEN_Pos | // Apply to GCLK4 + GCLK_CLKCTRL_ID_TCC0_TCC1; // Feed GCLK to TCC0/1 + while (GCLK->STATUS.bit.SYNCBUSY); + + // Assume we're using TCC0... as we're bit-bashing the DCC waveform output pins anyway + // for "normal accuracy" DCC waveform generation. For high accuracy we're going to need + // to a good deal more. The TCC waveform output pins are mux'd on the SAMD, and output + // pins for each TCC are only available on certain pins + TCC0->WAVE.reg = TCC_WAVE_WAVEGEN_NPWM; // Select NPWM as waveform + while (TCC0->SYNCBUSY.bit.WAVE); // Wait for sync + + // Set the frequency + TCC0->CTRLA.reg |= TCC_CTRLA_PRESCALER(TCC_CTRLA_PRESCALER_DIV1_Val); + TCC0->PER.reg = CLOCK_CYCLES * 2; + while (TCC0->SYNCBUSY.bit.PER); + + // Start the timer + TCC0->CTRLA.bit.ENABLE = 1; + while (TCC0->SYNCBUSY.bit.ENABLE); + + // Set the interrupt condition, priority and enable it in the NVIC + TCC0->INTENSET.reg = TCC_INTENSET_OVF; // Only interrupt on overflow + NVIC_SetPriority((IRQn_Type)TCC0_IRQn, 0); // Make this highest priority + NVIC_EnableIRQ((IRQn_Type)TCC0_IRQn); // Enable the interrupt + interrupts(); +} + +// Timer IRQ handlers replace the dummy handlers (in cortex_handlers) +// copied from rf24 branch +void TCC0_Handler() { + if(TCC0->INTFLAG.bit.OVF) { + TCC0->INTFLAG.bit.OVF = 1; // writing a 1 clears the flag + interruptHandler(); + } +} + +void TCC1_Handler() { + if(TCC1->INTFLAG.bit.OVF) { + TCC1->INTFLAG.bit.OVF = 1; // writing a 1 clears the flag + interruptHandler(); + } +} + +void TCC2_Handler() { + if(TCC2->INTFLAG.bit.OVF) { + TCC2->INTFLAG.bit.OVF = 1; // writing a 1 clears the flag + interruptHandler(); + } +} + + +bool DCCTimer::isPWMPin(byte pin) { + //TODO: SAMD whilst this call to digitalPinHasPWM will reveal which pins can do PWM, + // there's no support yet for High Accuracy, so for now return false + // return digitalPinHasPWM(pin); + return false; +} + +void DCCTimer::setPWM(byte pin, bool high) { + // TODO: High Accuracy mode is not supported as yet, and may never need to be + (void) pin; + (void) high; +} + +void DCCTimer::clearPWM() { + return; +} + +void DCCTimer::getSimulatedMacAddress(byte mac[6]) { + volatile uint32_t *serno1 = (volatile uint32_t *)0x0080A00C; + volatile uint32_t *serno2 = (volatile uint32_t *)0x0080A040; +// volatile uint32_t *serno3 = (volatile uint32_t *)0x0080A044; +// volatile uint32_t *serno4 = (volatile uint32_t *)0x0080A048; + + volatile uint32_t m1 = *serno1; + volatile uint32_t m2 = *serno2; + mac[0] = m1 >> 8; + mac[1] = m1 >> 0; + mac[2] = m2 >> 24; + mac[3] = m2 >> 16; + mac[4] = m2 >> 8; + mac[5] = m2 >> 0; +} + +volatile int DCCTimer::minimum_free_memory=__INT_MAX__; + +// Return low memory value... +int DCCTimer::getMinimumFreeMemory() { + noInterrupts(); // Disable interrupts to get volatile value + int retval = freeMemory(); + interrupts(); + return retval; +} + +extern "C" char* sbrk(int incr); + +int DCCTimer::freeMemory() { + char top; + return (int)(&top - reinterpret_cast(sbrk(0))); +} + +#endif \ No newline at end of file diff --git a/EEStore.h b/EEStore.h index f0d9022..360cc9c 100644 --- a/EEStore.h +++ b/EEStore.h @@ -26,7 +26,7 @@ #include -#if defined(ARDUINO_ARCH_SAMD) +#if defined(ARDUINO_ARCH_SAMC) #include extern ExternalEEPROM EEPROM; #else diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 92d8c88..b013253 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -1,4 +1,5 @@ /* + * © 2022 Paul M Antoine * © 2021 Mike S * © 2021 Fred Decker * © 2020-2022 Harald Barth @@ -172,8 +173,12 @@ int MotorDriver::mA2raw( unsigned int mA) { void MotorDriver::getFastPin(const FSH* type,int pin, bool input, FASTPIN & result) { // DIAG(F("MotorDriver %S Pin=%d,"),type,pin); - (void) type; // avoid compiler warning if diag not used above. + (void) type; // avoid compiler warning if diag not used above. +#if defined(ARDUINO_ARCH_SAMD) + PortGroup *port = digitalPinToPort(pin); +#else uint8_t port = digitalPinToPort(pin); +#endif if (input) result.inout = portInputRegister(port); else diff --git a/MotorDriver.h b/MotorDriver.h index 283039c..086cdc6 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -1,4 +1,5 @@ /* + * © 2022 Paul M Antoine * © 2021 Mike S * © 2021 Fred Decker * © 2020 Chris Harlow @@ -36,7 +37,7 @@ #define UNUSED_PIN 127 // inside int8_t #endif -#if defined(__IMXRT1062__) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#if defined(__IMXRT1062__) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) struct FASTPIN { volatile uint32_t *inout; uint32_t maskHIGH; diff --git a/MotorDrivers.h b/MotorDrivers.h index b2b798d..4877af5 100644 --- a/MotorDrivers.h +++ b/MotorDrivers.h @@ -1,4 +1,5 @@ /* + * © 2022 Paul M Antoine * © 2021 Fred Decker * © 2020-2022 Harald Barth * (c) 2020 Chris Harlow. All rights reserved. @@ -50,6 +51,13 @@ new MotorDriver(3, 12, UNUSED_PIN, 9, A0, 2.99, 2000, UNUSED_PIN), \ new MotorDriver(11, 13, UNUSED_PIN, 8, A1, 2.99, 2000, UNUSED_PIN) +// Setup for SAMD21 Sparkfun DEV board using Arduino standard Motor Shield R3 (MUST be R3 for 3v3 compatibility!!) +// senseFactor for 3.3v systems is 1.95 as calculated when using 10-bit A/D samples, +// and for 12-bit samples it's more like 0.488, but we probably need to tweak both these +#define SAMD_STANDARD_MOTOR_SHIELD F("STANDARD_MOTOR_SHIELD"), \ + new MotorDriver(3, 12, UNUSED_PIN, 9, A0, 1.95, 2000, UNUSED_PIN), \ + new MotorDriver(11, 13, UNUSED_PIN, 8, A1, 1.95, 2000, UNUSED_PIN) + // Pololu Motor Shield #define POLOLU_MOTOR_SHIELD F("POLOLU_MOTOR_SHIELD"), \ new MotorDriver( 9, 7, UNUSED_PIN, -4, A0, 18, 3000, 12), \ diff --git a/SerialManager.cpp b/SerialManager.cpp index 8717227..0d1c3c3 100644 --- a/SerialManager.cpp +++ b/SerialManager.cpp @@ -1,4 +1,5 @@ /* + * © 2022 Paul M Antoine * © 2021 Chris Harlow * © 2022 Harald Barth * All rights reserved. @@ -33,8 +34,13 @@ SerialManager::SerialManager(Stream * myserial) { void SerialManager::init() { while (!Serial && millis() < 5000); // wait max 5s for Serial to start +#if defined(ARDUINO_ARCH_SAMD) + SerialUSB.begin(115200); + new SerialManager(&SerialUSB); +#else Serial.begin(115200); new SerialManager(&Serial); +#endif #ifdef SERIAL3_COMMANDS Serial3.begin(115200); new SerialManager(&Serial3); diff --git a/defines.h b/defines.h index 9ce154d..fe291e8 100644 --- a/defines.h +++ b/defines.h @@ -1,4 +1,5 @@ /* + * © 2022 Paul M Antoine * © 2021 Neil McKechnie * © 2021 Mike S * © 2021 Fred Decker @@ -40,7 +41,7 @@ // WIFI_ON: All prereqs for running with WIFI are met // Note: WIFI_CHANNEL may not exist in early config.h files so is added here if needed. -#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO) || defined(TEENSYDUINO) || defined(ARDUINO_AVR_NANO_EVERY) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO) || defined(TEENSYDUINO) || defined(ARDUINO_AVR_NANO_EVERY) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) #define BIG_RAM #endif #if ENABLE_WIFI diff --git a/platformio.ini b/platformio.ini index 8bba297..a8b1e8b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,22 +15,35 @@ default_envs = mega328 unowifiR2 nano + samd21-dev-usb src_dir = . include_dir = . [env] build_flags = -Wall -Wextra -[env:samd21] +[env:samd21-dev-usb] platform = atmelsam board = sparkfun_samd21_dev_usb framework = arduino +upload_protocol = sam-ba +lib_deps = + ${env.lib_deps} +monitor_speed = 115200 +monitor_flags = --echo +build_flags = -std=c++17 -DI2C_USE_WIRE -DDISABLE_EEPROM + +[env:samc21-firebox] +platform = atmelsam +board = firebox +framework = arduino upload_protocol = atmel-ice lib_deps = ${env.lib_deps} SparkFun External EEPROM Arduino Library monitor_speed = 115200 monitor_flags = --echo +build_flags = -std=c++17 -DI2C_USE_WIRE -DDISABLE_EEPROM [env:mega2560-debug] platform = atmelavr