From c2d5e55a40d5467db9572857d38785e24a62dae9 Mon Sep 17 00:00:00 2001 From: Pontus Oldberg Date: Mon, 1 Jul 2024 10:26:54 +0200 Subject: [PATCH 1/4] Updated support for RP2040. --- DCCTimerRP2040.cpp | 151 +++++++++++++++++++++++++++++++++++++++++++++ MotorDriver.cpp | 2 + MotorDriver.h | 2 +- defines.h | 4 -- 4 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 DCCTimerRP2040.cpp diff --git a/DCCTimerRP2040.cpp b/DCCTimerRP2040.cpp new file mode 100644 index 0000000..5245962 --- /dev/null +++ b/DCCTimerRP2040.cpp @@ -0,0 +1,151 @@ +/* + * © 2020-2022 Harald Barth + * + * This file is part of CommandStation-EX + * + * 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 . + */ + +//////////////////////////////////////////////////////////////////////// +#ifdef ARDUINO_ARCH_RP2040 + +#include "pico/stdlib.h" + +#include +#include +#include +#include +#include +#include "hardware/timer.h" +#include "hardware/irq.h" + +#if defined(ADC_INPUT_MAX_VALUE) +#undef ADC_INPUT_MAX_VALUE +#endif +#define ADC_INPUT_MAX_VALUE 4095 // 12 bit ADC +#define NUM_ADC_INPUTS 4 + +#include "DCCTimer.h" +INTERRUPT_CALLBACK interruptHandler=0; + +#define ALARM_NUM 0 +#define ALARM_IRQ TIMER_IRQ_0 + +void DCCTimer::begin(INTERRUPT_CALLBACK callback) { + // Enable the interrupt for our alarm (the timer outputs 4 alarm irqs) + hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM); + // Set irq handler for alarm irq + irq_set_exclusive_handler(ALARM_IRQ, callback); + // Enable the alarm irq + irq_set_enabled(ALARM_IRQ, true); + // Enable interrupt in block and at processor + + // Alarm is only 32 bits so if trying to delay more + // than that need to be careful and keep track of the upper + // bits + uint64_t target = timer_hw->timerawl + DCC_SIGNAL_TIME; + + // Write the lower 32 bits of the target time to the alarm which + // will arm it + timer_hw->alarm[ALARM_NUM] = (uint32_t) target; +} + +// All pins are PWM capable +bool DCCTimer::isPWMPin(byte pin) { + return false; +} + +void DCCTimer::setPWM(byte pin, bool high) { + (void) pin; + (void) high; +} + +void DCCTimer::clearPWM() { +} + +// Fake this as it should not be used +void DCCTimer::getSimulatedMacAddress(byte mac[6]) { + mac[0] = 0xFE; + mac[1] = 0xBE; + mac[2] = 0xEF; + mac[3] = 0xC0; + mac[4] = 0xFF; + mac[5] = 0xEE; +} + +volatile int DCCTimer::minimum_free_memory=__INT_MAX__; + +// Return low memory value... +int DCCTimer::getMinimumFreeMemory() { + noInterrupts(); // Disable interrupts to get volatile value + int retval = minimum_free_memory; + interrupts(); + return retval; +} + +int DCCTimer::freeMemory() { + return rp2040.getFreeHeap(); +} + +void DCCTimer::reset() { + rp2040.reboot(); +} + +void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t frequency) { + (void) pin; /* Can't set different frequencies on different pins */ + analogWriteFreq(frequency); +} + +void DCCTimer::DCCEXanalogWrite(uint8_t pin, int value) { + analogWrite(pin, value); +} + +int ADCee::init(uint8_t pin) { + uint8_t id = pin - A0; + int value = 0; + + if (id > NUM_ADC_INPUTS) + return -1023; + + adc_gpio_init(pin); + adc_select_input(pin); + + return(adc_read()); +} + +int16_t ADCee::ADCmax() { + return ADC_INPUT_MAX_VALUE; +} + +/* + * Read function ADCee::read(pin) to get value instead of analogRead(pin) + */ +int ADCee::read(uint8_t pin, bool fromISR) { + adc_select_input(pin); + + return(adc_read()); +} + +/* + * Scan function that is called from interrupt + */ +void ADCee::scan() { +} + +void ADCee::begin() { + adc_init(); +} + +#endif // RP2040 + diff --git a/MotorDriver.cpp b/MotorDriver.cpp index d5dca13..a27b58c 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -446,6 +446,8 @@ void MotorDriver::getFastPin(const FSH* type,int pin, bool input, FASTPIN & res PortGroup *port = digitalPinToPort(pin); #elif defined(ARDUINO_ARCH_STM32) GPIO_TypeDef *port = digitalPinToPort(pin); +#elif defined(ARDUINO_ARCH_RP2040) + volatile uint32_t port = 0; #else uint8_t port = digitalPinToPort(pin); #endif diff --git a/MotorDriver.h b/MotorDriver.h index 21bceb6..a4884c4 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -92,7 +92,7 @@ public: byte invpin = UNUSED_PIN; }; -#if defined(__IMXRT1062__) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) +#if defined(__IMXRT1062__) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_RP2040) typedef uint32_t portreg_t; #else typedef uint8_t portreg_t; diff --git a/defines.h b/defines.h index f3822ca..469c63f 100644 --- a/defines.h +++ b/defines.h @@ -147,12 +147,8 @@ #ifndef I2C_USE_WIRE #define I2C_USE_WIRE #endif - -/* TODO when ready #elif defined(ARDUINO_ARCH_RP2040) #define ARDUINO_TYPE "RP2040" -*/ - #else #define CPU_TYPE_ERROR #endif From 09c2c032cc157b008f8b311e1f6240945ee25b21 Mon Sep 17 00:00:00 2001 From: Pontus Oldberg Date: Tue, 2 Jul 2024 11:16:41 +0200 Subject: [PATCH 2/4] RP2040 now works, but is unoptimized. --- .gitignore | 1 + CommandStation-EX.ino | 5 +++++ DCCTimerRP2040.cpp | 20 +++++++++++++++++--- DCCWaveform.cpp | 5 ++++- MotorDriver.cpp | 4 +--- MotorDriver.h | 4 ++++ MotorDrivers.h | 16 +++++++++++++++- 7 files changed, 47 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 6237359..9f44e98 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ myFilter.cpp my*.h !my*.example.h compile_commands.json +*.yaml diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino index 77e8f40..2e749a7 100644 --- a/CommandStation-EX.ino +++ b/CommandStation-EX.ino @@ -69,6 +69,11 @@ void setup() { // The main sketch has responsibilities during setup() + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, LOW); + + while(!Serial) + delay(100); // Responsibility 1: Start the usb connection for diagnostics // This is normally Serial but uses SerialUSB on a SAMD processor diff --git a/DCCTimerRP2040.cpp b/DCCTimerRP2040.cpp index 5245962..ae03836 100644 --- a/DCCTimerRP2040.cpp +++ b/DCCTimerRP2040.cpp @@ -42,11 +42,26 @@ INTERRUPT_CALLBACK interruptHandler=0; #define ALARM_NUM 0 #define ALARM_IRQ TIMER_IRQ_0 +static uint32_t dcc_signal_time = 0; + +static void alarm_irq(void) { + // Clear the alarm irq + hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM); + // Reload timer + uint64_t target = timer_hw->timerawl + dcc_signal_time; + timer_hw->alarm[ALARM_NUM] = (uint32_t) target; + + if (interruptHandler) + interruptHandler(); +} + void DCCTimer::begin(INTERRUPT_CALLBACK callback) { + interruptHandler = callback; + dcc_signal_time = DCC_SIGNAL_TIME; // Enable the interrupt for our alarm (the timer outputs 4 alarm irqs) hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM); // Set irq handler for alarm irq - irq_set_exclusive_handler(ALARM_IRQ, callback); + irq_set_exclusive_handler(ALARM_IRQ, alarm_irq); // Enable the alarm irq irq_set_enabled(ALARM_IRQ, true); // Enable interrupt in block and at processor @@ -54,7 +69,7 @@ void DCCTimer::begin(INTERRUPT_CALLBACK callback) { // Alarm is only 32 bits so if trying to delay more // than that need to be careful and keep track of the upper // bits - uint64_t target = timer_hw->timerawl + DCC_SIGNAL_TIME; + uint64_t target = timer_hw->timerawl + dcc_signal_time; // Write the lower 32 bits of the target time to the alarm which // will arm it @@ -148,4 +163,3 @@ void ADCee::begin() { } #endif // RP2040 - diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 4a99997..3479921 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -62,7 +62,7 @@ const bool signalTransform[]={ /* WAVE_PENDING (should not happen) -> */ LOW}; void DCCWaveform::begin() { - DCCTimer::begin(DCCWaveform::interruptHandler); + DCCTimer::begin(DCCWaveform::interruptHandler); } void DCCWaveform::loop() { @@ -71,7 +71,10 @@ void DCCWaveform::loop() { #pragma GCC push_options #pragma GCC optimize ("-O3") +static uint32_t blinker = 0; void DCCWaveform::interruptHandler() { + if (!(blinker++ % 25000)) + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // call the timer edge sensitive actions for progtrack and maintrack // member functions would be cleaner but have more overhead byte sigMain=signalTransform[mainTrack.state]; diff --git a/MotorDriver.cpp b/MotorDriver.cpp index a27b58c..0ff088f 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -91,7 +91,7 @@ MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, i } } else dualSignal=false; - + if (brake_pin!=UNUSED_PIN){ invertBrake=brake_pin < 0; if (invertBrake) @@ -446,8 +446,6 @@ void MotorDriver::getFastPin(const FSH* type,int pin, bool input, FASTPIN & res PortGroup *port = digitalPinToPort(pin); #elif defined(ARDUINO_ARCH_STM32) GPIO_TypeDef *port = digitalPinToPort(pin); -#elif defined(ARDUINO_ARCH_RP2040) - volatile uint32_t port = 0; #else uint8_t port = digitalPinToPort(pin); #endif diff --git a/MotorDriver.h b/MotorDriver.h index a4884c4..84d2e03 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -61,6 +61,10 @@ enum TRACK_MODE : byte {TRACK_MODE_NONE = 1, TRACK_MODE_MAIN = 2, TRACK_MODE_PRO #define PORTC GPIOC->ODR #define HAVE_PORTC(X) X #endif +#if defined(ARDUINO_ARCH_RP2040) +#define HAVE_PORTA(X) X +#define PORTA (sio_hw->gpio_out) +#endif // if macros not defined as pass-through we define // them here as someting that is valid as a diff --git a/MotorDrivers.h b/MotorDrivers.h index 907c11b..2282238 100644 --- a/MotorDrivers.h +++ b/MotorDrivers.h @@ -59,7 +59,21 @@ // Arduino STANDARD Motor Shield, used on different architectures: -#if defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) +#if defined(ARDUINO_ARCH_RP2040) +// Standard Motor Shield definition for RP2040 +// This setup is for the iLabs Challenger NB RP2040 WiFi board. Other boards likely need +// a separate setup. +#define STANDARD_MOTOR_SHIELD "STANDARD_MOTOR_SHIELD", \ + new MotorDriver(D17, D16, UNUSED_PIN, D15, A0, 0.488, 1500, UNUSED_PIN), \ + new MotorDriver(D14, D13, UNUSED_PIN, D12, A1, 0.488, 1500, UNUSED_PIN) +#define RP2040_STANDARD_MOTOR_SHIELD STANDARD_MOTOR_SHIELD + +// EX 8874 based shield connected to a 3V3 system with 12-bit (4096) ADC +#define EX8874_SHIELD "EX8874", \ + new MotorDriver(D17, D16, UNUSED_PIN, D15, A0, 1.27, 5000, A4), \ + new MotorDriver(D14, D13, UNUSED_PIN, D12, A1, 1.27, 5000, A5) + +#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) // Standard Motor Shield definition for 3v3 processors (other than the ESP32) // Setup for SAMD21 Sparkfun DEV board MUST use Arduino Motor Shield R3 (MUST be R3 // for 3v3 compatibility!!) senseFactor for 3.3v systems is 1.95 as calculated when using From c0886ebea33520afafda400e0532c09162737b12 Mon Sep 17 00:00:00 2001 From: Pontus Oldberg Date: Wed, 10 Jul 2024 14:50:33 +0200 Subject: [PATCH 3/4] Added a reset pin option for the OLED driver and cleaned up a bit. --- CommandStation-EX.ino | 3 --- SSD1306Ascii.cpp | 9 ++++++++- WifiInterface.cpp | 5 +++++ config.example.h | 3 +++ defines.h | 1 + 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino index 2e749a7..6b0ea71 100644 --- a/CommandStation-EX.ino +++ b/CommandStation-EX.ino @@ -72,9 +72,6 @@ void setup() pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); - while(!Serial) - delay(100); - // Responsibility 1: Start the usb connection for diagnostics // This is normally Serial but uses SerialUSB on a SAMD processor SerialManager::init(); diff --git a/SSD1306Ascii.cpp b/SSD1306Ascii.cpp index ea0c76b..3ada0c3 100644 --- a/SSD1306Ascii.cpp +++ b/SSD1306Ascii.cpp @@ -159,7 +159,14 @@ SSD1306AsciiWire::SSD1306AsciiWire(I2CAddress address, int width, int height) { bool SSD1306AsciiWire::begin() { I2CManager.begin(); - I2CManager.setClock(400000L); // Set max supported I2C speede + I2CManager.setClock(400000L); // Set max supported I2C speed + +#if defined(OLED_HAS_RESET_PIN) + pinMode(OLED_HAS_RESET_PIN, OUTPUT); + digitalWrite(OLED_HAS_RESET_PIN, LOW); + delay(1); + digitalWrite(OLED_HAS_RESET_PIN, HIGH); +#endif if (m_i2cAddr == 0) { // Probe for I2C device on 0x3c and 0x3d. diff --git a/WifiInterface.cpp b/WifiInterface.cpp index 8b2251a..cf95cd8 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -76,6 +76,11 @@ Stream * WifiInterface::wifiStream; #endif #endif +#if defined(ARDUINO_ARCH_RP2040) +#define NUM_SERIAL 2 +#define SERIAL1 Serial2 +#endif + #ifndef NUM_SERIAL #define NUM_SERIAL 1 #define SERIAL1 Serial1 diff --git a/config.example.h b/config.example.h index 0f136f9..11d7fe3 100644 --- a/config.example.h +++ b/config.example.h @@ -160,6 +160,9 @@ The configuration file for DCC-EX Command Station // 128x32 or 128x64 I2C SSD1306-based devices are supported. // Use 132,64 for a SH1106-based I2C device with a 128x64 display. // #define OLED_DRIVER 0x3c,128,32 +// Some OLED displays have an external reset pin that needs to be managed. Set the following +// macro to point to that pin. +// #define OLED_HAS_RESET_PIN 5 // Define scroll mode as 0, 1 or 2 // * #define SCROLLMODE 0 is scroll continuous (fill screen if poss), diff --git a/defines.h b/defines.h index 469c63f..7a33bbb 100644 --- a/defines.h +++ b/defines.h @@ -208,6 +208,7 @@ // Currently only devices which can communicate at 115200 are supported. // #define WIFI_SERIAL_LINK_SPEED 115200 +#define WIFI_SERIAL_PORT Serial2 #if __has_include ( "myAutomation.h") #if defined(HAS_ENOUGH_MEMORY) || defined(DISABLE_EEPROM) || defined(DISABLE_PROG) From 2cbfde0484f7f616f22b49f328dce6b7f835dcfa Mon Sep 17 00:00:00 2001 From: Pontus Oldberg Date: Wed, 10 Jul 2024 16:10:35 +0200 Subject: [PATCH 4/4] Removed irq debug code. --- CommandStation-EX.ino | 2 -- DCCWaveform.cpp | 3 --- 2 files changed, 5 deletions(-) diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino index 6b0ea71..77e8f40 100644 --- a/CommandStation-EX.ino +++ b/CommandStation-EX.ino @@ -69,8 +69,6 @@ void setup() { // The main sketch has responsibilities during setup() - pinMode(LED_BUILTIN, OUTPUT); - digitalWrite(LED_BUILTIN, LOW); // Responsibility 1: Start the usb connection for diagnostics // This is normally Serial but uses SerialUSB on a SAMD processor diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 3479921..53cab96 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -71,10 +71,7 @@ void DCCWaveform::loop() { #pragma GCC push_options #pragma GCC optimize ("-O3") -static uint32_t blinker = 0; void DCCWaveform::interruptHandler() { - if (!(blinker++ % 25000)) - digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // call the timer edge sensitive actions for progtrack and maintrack // member functions would be cleaner but have more overhead byte sigMain=signalTransform[mainTrack.state];