From 169818e31e0aebed9d808dcbbfaa293dcb13af40 Mon Sep 17 00:00:00 2001 From: Stefan de Bruijn Date: Tue, 3 Aug 2021 10:21:40 +0200 Subject: [PATCH] Fixed some includes. Fixed name conflict of LEDCPin. Added native stub implementations for x86 for the rest. --- Grbl_Esp32/src/Logging.cpp | 7 +- Grbl_Esp32/src/Machine/LimitPin.cpp | 2 + Grbl_Esp32/src/Machine/LimitPin.h | 1 + Grbl_Esp32/src/Motors/RcServo.cpp | 2 +- Grbl_Esp32/src/Motors/StandardStepper.cpp | 2 + Grbl_Esp32/src/NutsBolts.h | 2 + Grbl_Esp32/src/Pins/{Ledc.cpp => LedcPin.cpp} | 2 +- Grbl_Esp32/src/Pins/{Ledc.h => LedcPin.h} | 0 Grbl_Esp32/src/Spindles/10vSpindle.cpp | 2 +- Grbl_Esp32/src/Spindles/BESCSpindle.cpp | 2 +- Grbl_Esp32/src/Spindles/PWMSpindle.cpp | 2 +- Grbl_Esp32/src/System.cpp | 1 + Grbl_Esp32/src/Uart.cpp | 1 + Grbl_Esp32/test/Pins/ErrorPinTest.cpp | 1 + Grbl_Esp32/test/Pins/Undefined.cpp | 1 + UnitTests.vcxproj | 25 ++- UnitTests.vcxproj.filters | 91 +++++++++-- X86TestSupport/Arduino.cpp | 5 + X86TestSupport/Arduino.h | 145 +----------------- X86TestSupport/Capture.h | 2 +- X86TestSupport/Esp.h | 36 +++++ X86TestSupport/I2SO.cpp | 35 +++++ X86TestSupport/SDFS.cpp | 19 +++ X86TestSupport/SPI.cpp | 3 + X86TestSupport/SPIFFS.cpp | 19 +++ X86TestSupport/SPIFFS.h | 5 +- X86TestSupport/SoftwareGPIO.h | 1 + X86TestSupport/Stream.cpp | 21 ++- X86TestSupport/Stream.h | 2 + X86TestSupport/TMCStepper.h | 1 - X86TestSupport/driver/ledc.h | 7 + X86TestSupport/driver/rmt.cpp | 16 ++ X86TestSupport/driver/rmt.h | 5 + X86TestSupport/esp32-hal-cpu.h | 3 + X86TestSupport/esp32-hal-dac.h | 3 + X86TestSupport/esp32-hal-gpio.h | 82 ++++++++++ X86TestSupport/esp32-hal-ledc.h | 3 + X86TestSupport/esp32-hal-matrix.h | 3 + X86TestSupport/esp32-hal-timer.cpp | 31 ++++ X86TestSupport/esp32-hal-timer.h | 33 ++++ X86TestSupport/esp32-hal.h | 3 + X86TestSupport/esp_attr.h | 3 + X86TestSupport/freertos/FreeRTOS.h | 24 ++- X86TestSupport/freertos/FreeRTOSTypes.h | 2 - X86TestSupport/soc/ledc_struct.cpp | 3 + X86TestSupport/soc/ledc_struct.h | 2 + X86TestSupport/xtensa/core-macros.h | 7 + 47 files changed, 482 insertions(+), 186 deletions(-) rename Grbl_Esp32/src/Pins/{Ledc.cpp => LedcPin.cpp} (99%) rename Grbl_Esp32/src/Pins/{Ledc.h => LedcPin.h} (100%) create mode 100644 X86TestSupport/Esp.h create mode 100644 X86TestSupport/I2SO.cpp create mode 100644 X86TestSupport/SDFS.cpp create mode 100644 X86TestSupport/SPI.cpp create mode 100644 X86TestSupport/SPIFFS.cpp delete mode 100644 X86TestSupport/TMCStepper.h create mode 100644 X86TestSupport/driver/rmt.cpp create mode 100644 X86TestSupport/esp32-hal-cpu.h create mode 100644 X86TestSupport/esp32-hal-dac.h create mode 100644 X86TestSupport/esp32-hal-gpio.h create mode 100644 X86TestSupport/esp32-hal-ledc.h create mode 100644 X86TestSupport/esp32-hal-matrix.h create mode 100644 X86TestSupport/esp32-hal-timer.cpp create mode 100644 X86TestSupport/esp32-hal-timer.h create mode 100644 X86TestSupport/esp32-hal.h create mode 100644 X86TestSupport/esp_attr.h create mode 100644 X86TestSupport/soc/ledc_struct.cpp create mode 100644 X86TestSupport/xtensa/core-macros.h diff --git a/Grbl_Esp32/src/Logging.cpp b/Grbl_Esp32/src/Logging.cpp index 94619a2e..4b5ac3d6 100644 --- a/Grbl_Esp32/src/Logging.cpp +++ b/Grbl_Esp32/src/Logging.cpp @@ -17,11 +17,16 @@ */ #include "Logging.h" +#include "SettingsDefinitions.h" #ifndef ESP32 # include +bool atMsgLevel(MsgLevel level) { + return message_level == nullptr || message_level->get() >= level; +} + DebugStream::DebugStream(const char* name) { std::cout << "[MSG:" << name << ": "; } @@ -35,8 +40,6 @@ DebugStream::~DebugStream() { #else -# include "SettingsDefinitions.h" - bool atMsgLevel(MsgLevel level) { return message_level == nullptr || message_level->get() >= level; } diff --git a/Grbl_Esp32/src/Machine/LimitPin.cpp b/Grbl_Esp32/src/Machine/LimitPin.cpp index ee924869..9deb9cee 100644 --- a/Grbl_Esp32/src/Machine/LimitPin.cpp +++ b/Grbl_Esp32/src/Machine/LimitPin.cpp @@ -7,6 +7,8 @@ #include "../Limits.h" #include "../System.h" // sys_rt_exec_alarm +#include // CHANGE + namespace Machine { LimitPin::LimitPin(Pin& pin, int axis, int gang, int direction, bool& pHardLimits) : _axis(axis), _gang(gang), _value(false), _pHardLimits(pHardLimits), _pin(pin) { diff --git a/Grbl_Esp32/src/Machine/LimitPin.h b/Grbl_Esp32/src/Machine/LimitPin.h index dd53b760..89e665ce 100644 --- a/Grbl_Esp32/src/Machine/LimitPin.h +++ b/Grbl_Esp32/src/Machine/LimitPin.h @@ -1,6 +1,7 @@ #pragma once #include "../Pin.h" + #include // IRAM_ATTR namespace Machine { diff --git a/Grbl_Esp32/src/Motors/RcServo.cpp b/Grbl_Esp32/src/Motors/RcServo.cpp index 80b2c601..bcc3dd32 100644 --- a/Grbl_Esp32/src/Motors/RcServo.cpp +++ b/Grbl_Esp32/src/Motors/RcServo.cpp @@ -31,7 +31,7 @@ #include "RcServo.h" #include "../Machine/MachineConfig.h" -#include "../Pins/Ledc.h" +#include "../Pins/LedcPin.h" #include "../Pin.h" #include "../Limits.h" // limitsMaxPosition #include "RcServoSettings.h" diff --git a/Grbl_Esp32/src/Motors/StandardStepper.cpp b/Grbl_Esp32/src/Motors/StandardStepper.cpp index 8c94eef4..61a3984a 100644 --- a/Grbl_Esp32/src/Motors/StandardStepper.cpp +++ b/Grbl_Esp32/src/Motors/StandardStepper.cpp @@ -27,6 +27,8 @@ #include "../Stepper.h" // ST_I2S_* #include "../Stepping.h" // config->_stepping->_engine +#include // gpio + using namespace Machine; namespace Motors { diff --git a/Grbl_Esp32/src/NutsBolts.h b/Grbl_Esp32/src/NutsBolts.h index dd275488..84e13d67 100644 --- a/Grbl_Esp32/src/NutsBolts.h +++ b/Grbl_Esp32/src/NutsBolts.h @@ -146,7 +146,9 @@ inline int32_t IRAM_ATTR usToEndTicks(int32_t us) { inline void IRAM_ATTR spinUntil(int32_t endTicks) { while ((XTHAL_GET_CCOUNT() - endTicks) < 0) { +#ifdef ESP32 asm volatile("nop"); +#endif } } diff --git a/Grbl_Esp32/src/Pins/Ledc.cpp b/Grbl_Esp32/src/Pins/LedcPin.cpp similarity index 99% rename from Grbl_Esp32/src/Pins/Ledc.cpp rename to Grbl_Esp32/src/Pins/LedcPin.cpp index 0dc286c6..e9477708 100644 --- a/Grbl_Esp32/src/Pins/Ledc.cpp +++ b/Grbl_Esp32/src/Pins/LedcPin.cpp @@ -1,4 +1,4 @@ -#include "Ledc.h" +#include "LedcPin.h" /* Ledc.cpp diff --git a/Grbl_Esp32/src/Pins/Ledc.h b/Grbl_Esp32/src/Pins/LedcPin.h similarity index 100% rename from Grbl_Esp32/src/Pins/Ledc.h rename to Grbl_Esp32/src/Pins/LedcPin.h diff --git a/Grbl_Esp32/src/Spindles/10vSpindle.cpp b/Grbl_Esp32/src/Spindles/10vSpindle.cpp index 4d590e69..39203a58 100644 --- a/Grbl_Esp32/src/Spindles/10vSpindle.cpp +++ b/Grbl_Esp32/src/Spindles/10vSpindle.cpp @@ -26,7 +26,7 @@ */ #include "10vSpindle.h" -#include "../Pins/Ledc.h" +#include "../Pins/LedcPin.h" #include "../System.h" // sys.spindle_speed #include "../GCode.h" // gc_state.modal #include // ledcDetachPin diff --git a/Grbl_Esp32/src/Spindles/BESCSpindle.cpp b/Grbl_Esp32/src/Spindles/BESCSpindle.cpp index 131bd829..dc29c5f2 100644 --- a/Grbl_Esp32/src/Spindles/BESCSpindle.cpp +++ b/Grbl_Esp32/src/Spindles/BESCSpindle.cpp @@ -32,7 +32,7 @@ */ #include "BESCSpindle.h" -#include "../Pins/Ledc.h" +#include "../Pins/LedcPin.h" #include diff --git a/Grbl_Esp32/src/Spindles/PWMSpindle.cpp b/Grbl_Esp32/src/Spindles/PWMSpindle.cpp index 3813eb9a..1d4429ab 100644 --- a/Grbl_Esp32/src/Spindles/PWMSpindle.cpp +++ b/Grbl_Esp32/src/Spindles/PWMSpindle.cpp @@ -24,7 +24,7 @@ #include "../System.h" // sys.report_ovr_counter #include "../GCode.h" // gc_state.modal #include "../Logging.h" -#include "../Pins/Ledc.h" +#include "../Pins/LedcPin.h" #include // ledcDetachPin // ======================= PWM ============================== diff --git a/Grbl_Esp32/src/System.cpp b/Grbl_Esp32/src/System.cpp index 95b7284d..3fcdafff 100644 --- a/Grbl_Esp32/src/System.cpp +++ b/Grbl_Esp32/src/System.cpp @@ -31,6 +31,7 @@ #include // memset #include #include +#include // LOW // Declare system global variable structure system_t sys; diff --git a/Grbl_Esp32/src/Uart.cpp b/Grbl_Esp32/src/Uart.cpp index 78f77886..7ae05a71 100644 --- a/Grbl_Esp32/src/Uart.cpp +++ b/Grbl_Esp32/src/Uart.cpp @@ -29,6 +29,7 @@ #include #include #include +#include // GPIO_NUM_1 etc Uart::Uart() : _pushback(-1) { static int currentNumber = 1; diff --git a/Grbl_Esp32/test/Pins/ErrorPinTest.cpp b/Grbl_Esp32/test/Pins/ErrorPinTest.cpp index 5db871a3..ebef7f05 100644 --- a/Grbl_Esp32/test/Pins/ErrorPinTest.cpp +++ b/Grbl_Esp32/test/Pins/ErrorPinTest.cpp @@ -1,6 +1,7 @@ #include "../TestFramework.h" #include +#include // CHANGE namespace Pins { Test(Error, Pins) { diff --git a/Grbl_Esp32/test/Pins/Undefined.cpp b/Grbl_Esp32/test/Pins/Undefined.cpp index 01d6a940..388c4640 100644 --- a/Grbl_Esp32/test/Pins/Undefined.cpp +++ b/Grbl_Esp32/test/Pins/Undefined.cpp @@ -1,6 +1,7 @@ #include "../TestFramework.h" #include +#include // CHANGE namespace Pins { Test(Undefined, Pins) { diff --git a/UnitTests.vcxproj b/UnitTests.vcxproj index 8c9e5ce1..9420c109 100644 --- a/UnitTests.vcxproj +++ b/UnitTests.vcxproj @@ -83,6 +83,7 @@ + @@ -108,7 +109,7 @@ - + @@ -172,6 +173,15 @@ + + + + + + + + + @@ -195,10 +205,10 @@ - + @@ -224,7 +234,9 @@ + + @@ -244,7 +256,7 @@ - + @@ -308,13 +320,20 @@ + + + + + + + diff --git a/UnitTests.vcxproj.filters b/UnitTests.vcxproj.filters index 176ca0e4..e2eda9cf 100644 --- a/UnitTests.vcxproj.filters +++ b/UnitTests.vcxproj.filters @@ -46,6 +46,12 @@ {2fe0b2ec-2f5f-44c9-8e5d-33a53d96d80c} + + {65268649-7855-443e-bf8e-984388ea6c17} + + + {52fb1ffd-68a9-4edc-aae1-ca2cdf3e7da9} + @@ -270,9 +276,6 @@ src - - X86TestSupport - src\Spindles @@ -300,12 +303,6 @@ src\Machine - - X86TestSupport - - - X86TestSupport - src @@ -360,9 +357,6 @@ src - - src\Pins - src\Spindles @@ -510,6 +504,48 @@ src + + X86TestSupport + + + X86TestSupport\xtensa + + + X86TestSupport\TMC + + + X86TestSupport\TMC + + + X86TestSupport + + + X86TestSupport + + + X86TestSupport + + + X86TestSupport + + + X86TestSupport + + + X86TestSupport + + + X86TestSupport + + + src\Machine + + + X86TestSupport + + + src\Pins + @@ -638,9 +674,6 @@ src\WebUI - - src\Pins - src @@ -860,6 +893,34 @@ src + + src\Machine + + + src\Machine + + + src\Pins + + + X86TestSupport + + + X86TestSupport + + + X86TestSupport + + + X86TestSupport + + + X86TestSupport\soc + + + + X86TestSupport + diff --git a/X86TestSupport/Arduino.cpp b/X86TestSupport/Arduino.cpp index 150bf3a0..7316b7fe 100644 --- a/X86TestSupport/Arduino.cpp +++ b/X86TestSupport/Arduino.cpp @@ -1,10 +1,15 @@ #include "Arduino.h" #include "SoftwareGPIO.h" +#include "Capture.h" #include #include +int64_t esp_timer_get_time() { + return Capture::instance().current(); +} + void attachInterrupt(uint8_t pin, void (*callback)(void), int mode) { attachInterruptArg( pin, diff --git a/X86TestSupport/Arduino.h b/X86TestSupport/Arduino.h index 4f2ce0d0..6e6ef02a 100644 --- a/X86TestSupport/Arduino.h +++ b/X86TestSupport/Arduino.h @@ -10,38 +10,6 @@ class SystemRestartException {}; // From Arduino.h: -// Interrupt Modes -#define RISING 0x01 -#define FALLING 0x02 -#define CHANGE 0x03 -// #define ONLOW 0x04 --> Weird. Same as falling / rising? -// #define ONHIGH 0x05 -// #define ONLOW_WE 0x0C --> not sure about these -// #define ONHIGH_WE 0x0D - -// From esp32-hal-gpio.h: - -#define LOW 0x0 -#define HIGH 0x1 - -// GPIO FUNCTIONS -#define INPUT 0x01 -#define OUTPUT 0x02 -#define PULLUP 0x04 -#define INPUT_PULLUP 0x05 -#define PULLDOWN 0x08 -#define INPUT_PULLDOWN 0x09 -// #define OPEN_DRAIN 0x10 -// #define OUTPUT_OPEN_DRAIN 0x12 - -void attachInterrupt(uint8_t pin, void (*)(void), int mode); -void attachInterruptArg(uint8_t pin, void (*)(void*), void* arg, int mode); -void detachInterrupt(uint8_t pin); - -extern "C" int __digitalRead(uint8_t pin); -extern "C" void __pinMode(uint8_t pin, uint8_t mode); -extern "C" void __digitalWrite(uint8_t pin, uint8_t val); - void delay(int ms); // Get time in microseconds since boot. @@ -52,119 +20,8 @@ int64_t esp_timer_get_time(); do { \ } while (0); -typedef enum { - GPIO_NUM_0 = 0, /*!< GPIO0, input and output */ - GPIO_NUM_1 = 1, /*!< GPIO1, input and output */ - GPIO_NUM_2 = 2, /*!< GPIO2, input and output - @note There are more enumerations like that - up to GPIO39, excluding GPIO20, GPIO24 and GPIO28..31. - They are not shown here to reduce redundant information. - @note GPIO34..39 are input mode only. */ - /** @cond */ - GPIO_NUM_3 = 3, /*!< GPIO3, input and output */ - GPIO_NUM_4 = 4, /*!< GPIO4, input and output */ - GPIO_NUM_5 = 5, /*!< GPIO5, input and output */ - GPIO_NUM_6 = 6, /*!< GPIO6, input and output */ - GPIO_NUM_7 = 7, /*!< GPIO7, input and output */ - GPIO_NUM_8 = 8, /*!< GPIO8, input and output */ - GPIO_NUM_9 = 9, /*!< GPIO9, input and output */ - GPIO_NUM_10 = 10, /*!< GPIO10, input and output */ - GPIO_NUM_11 = 11, /*!< GPIO11, input and output */ - GPIO_NUM_12 = 12, /*!< GPIO12, input and output */ - GPIO_NUM_13 = 13, /*!< GPIO13, input and output */ - GPIO_NUM_14 = 14, /*!< GPIO14, input and output */ - GPIO_NUM_15 = 15, /*!< GPIO15, input and output */ - GPIO_NUM_16 = 16, /*!< GPIO16, input and output */ - GPIO_NUM_17 = 17, /*!< GPIO17, input and output */ - GPIO_NUM_18 = 18, /*!< GPIO18, input and output */ - GPIO_NUM_19 = 19, /*!< GPIO19, input and output */ - - GPIO_NUM_21 = 21, /*!< GPIO21, input and output */ - GPIO_NUM_22 = 22, /*!< GPIO22, input and output */ - GPIO_NUM_23 = 23, /*!< GPIO23, input and output */ - - GPIO_NUM_25 = 25, /*!< GPIO25, input and output */ - GPIO_NUM_26 = 26, /*!< GPIO26, input and output */ - GPIO_NUM_27 = 27, /*!< GPIO27, input and output */ - - GPIO_NUM_32 = 32, /*!< GPIO32, input and output */ - GPIO_NUM_33 = 33, /*!< GPIO33, input and output */ - GPIO_NUM_34 = 34, /*!< GPIO34, input mode only */ - GPIO_NUM_35 = 35, /*!< GPIO35, input mode only */ - GPIO_NUM_36 = 36, /*!< GPIO36, input mode only */ - GPIO_NUM_37 = 37, /*!< GPIO37, input mode only */ - GPIO_NUM_38 = 38, /*!< GPIO38, input mode only */ - GPIO_NUM_39 = 39, /*!< GPIO39, input mode only */ - GPIO_NUM_MAX = 40, - /** @endcond */ -} gpio_num_t; - #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) -// PWM: -uint32_t getApbFrequency(); // In Hz -double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits); -void ledcWrite(uint8_t channel, uint32_t duty); -void ledcAttachPin(uint8_t pin, uint8_t channel); -void ledcDetachPin(uint8_t pin); - -// Timer: - -struct hw_timer_s; -typedef struct hw_timer_s hw_timer_t; - -void timerAlarmEnable(hw_timer_t* timer); -void timerAlarmDisable(hw_timer_t* timer); -void timerWrite(hw_timer_t* timer, uint64_t val); -void timerAlarmWrite(hw_timer_t* timer, uint64_t interruptAt, bool autoreload); - -hw_timer_t* timerBegin(uint8_t timer, uint16_t divider, bool countUp); -void timerEnd(hw_timer_t* timer); - -void timerAttachInterrupt(hw_timer_t* timer, void (*fn)(void), bool edge); -void timerDetachInterrupt(hw_timer_t* timer); - -// Figure this out: -extern "C" { -esp_err_t esp_task_wdt_reset(void); -} - -unsigned long micros(); -unsigned long millis(); -void delay(uint32_t); -void delayMicroseconds(uint32_t us); - // ESP... -typedef enum { - ESP_RST_UNKNOWN, //!< Reset reason can not be determined - ESP_RST_POWERON, //!< Reset due to power-on event - ESP_RST_EXT, //!< Reset by external pin (not applicable for ESP32) - ESP_RST_SW, //!< Software reset via esp_restart - ESP_RST_PANIC, //!< Software reset due to exception/panic - ESP_RST_INT_WDT, //!< Reset (software or hardware) due to interrupt watchdog - ESP_RST_TASK_WDT, //!< Reset due to task watchdog - ESP_RST_WDT, //!< Reset due to other watchdogs - ESP_RST_DEEPSLEEP, //!< Reset after exiting deep sleep mode - ESP_RST_BROWNOUT, //!< Brownout reset (software or hardware) - ESP_RST_SDIO, //!< Reset over SDIO -} esp_reset_reason_t; - -esp_reset_reason_t esp_reset_reason(void); - -struct EspClass { - uint64_t getEfuseMac(); - uint32_t getCpuFreqMHz(); - const char* getSdkVersion(); - uint32_t getFreeHeap(); - uint32_t getFlashChipSize(); - - void restart(); -}; -extern EspClass ESP; - -inline long map(long x, long in_min, long in_max, long out_min, long out_max) { - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; -} - -int temperatureRead(void); +#include "Esp.h" diff --git a/X86TestSupport/Capture.h b/X86TestSupport/Capture.h index 25088c3f..f8d3be1d 100644 --- a/X86TestSupport/Capture.h +++ b/X86TestSupport/Capture.h @@ -97,7 +97,7 @@ public: } void set(const std::string& id, const std::vector& value) { - auto it = data_.find(id); + auto it = data_.find(id); if (it == data_.end()) { data_.insert(std::make_pair(id, value)); } else { diff --git a/X86TestSupport/Esp.h b/X86TestSupport/Esp.h new file mode 100644 index 00000000..903e81ad --- /dev/null +++ b/X86TestSupport/Esp.h @@ -0,0 +1,36 @@ +#pragma once + +#include + +typedef enum { + ESP_RST_UNKNOWN, //!< Reset reason can not be determined + ESP_RST_POWERON, //!< Reset due to power-on event + ESP_RST_EXT, //!< Reset by external pin (not applicable for ESP32) + ESP_RST_SW, //!< Software reset via esp_restart + ESP_RST_PANIC, //!< Software reset due to exception/panic + ESP_RST_INT_WDT, //!< Reset (software or hardware) due to interrupt watchdog + ESP_RST_TASK_WDT, //!< Reset due to task watchdog + ESP_RST_WDT, //!< Reset due to other watchdogs + ESP_RST_DEEPSLEEP, //!< Reset after exiting deep sleep mode + ESP_RST_BROWNOUT, //!< Brownout reset (software or hardware) + ESP_RST_SDIO, //!< Reset over SDIO +} esp_reset_reason_t; + +esp_reset_reason_t esp_reset_reason(void); + +struct EspClass { + uint64_t getEfuseMac(); + uint32_t getCpuFreqMHz(); + const char* getSdkVersion(); + uint32_t getFreeHeap(); + uint32_t getFlashChipSize(); + + void restart(); +}; +extern EspClass ESP; + +inline long map(long x, long in_min, long in_max, long out_min, long out_max) { + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + +int temperatureRead(void); diff --git a/X86TestSupport/I2SO.cpp b/X86TestSupport/I2SO.cpp new file mode 100644 index 00000000..7f8fc55a --- /dev/null +++ b/X86TestSupport/I2SO.cpp @@ -0,0 +1,35 @@ +#include + +// STUB implementation. Won't do you any good. + +int i2s_out_init() { + return 0; +} + +void i2s_out_push() {} + +unsigned int i2s_out_push_sample(unsigned int x) { + return x; +} + +int i2s_out_set_passthrough() { + return 0; +} + +int i2s_out_set_stepping() { + return 0; +} + +void i2s_out_delay() {} + +int i2s_out_set_pulse_period(unsigned int x) { + return 0; +} + +int i2s_out_reset() { + return 0; +} + +i2s_out_pulser_status_t i2s_out_get_pulser_status() { + return i2s_out_pulser_status_t::PASSTHROUGH; +} diff --git a/X86TestSupport/SDFS.cpp b/X86TestSupport/SDFS.cpp new file mode 100644 index 00000000..fb40858d --- /dev/null +++ b/X86TestSupport/SDFS.cpp @@ -0,0 +1,19 @@ +#include "SD.h" + +namespace fs { + + SDFS::SDFS(FSImplPtr impl) : fs::FS(impl) {} + bool SDFS::begin(uint8_t ssPin, SPIClass& spi, uint32_t frequency, const char* mountpoint, uint8_t max_files, bool format_if_empty) { + return true; + } + void SDFS::end() {} + uint64_t SDFS::cardSize() { return 0; } + uint64_t SDFS::totalBytes() { return 0; } + uint64_t SDFS::usedBytes() { return 0; } + bool SDFS::readRAW(uint8_t* buffer, uint32_t sector) { return false; } + bool SDFS::writeRAW(uint8_t* buffer, uint32_t sector) { return false; } + + FSImplPtr inst; +} + +fs::SDFS SD(fs::inst); diff --git a/X86TestSupport/SPI.cpp b/X86TestSupport/SPI.cpp new file mode 100644 index 00000000..f207c900 --- /dev/null +++ b/X86TestSupport/SPI.cpp @@ -0,0 +1,3 @@ +#include "SPI.h" + +SPIClass SPI; diff --git a/X86TestSupport/SPIFFS.cpp b/X86TestSupport/SPIFFS.cpp new file mode 100644 index 00000000..398b2e89 --- /dev/null +++ b/X86TestSupport/SPIFFS.cpp @@ -0,0 +1,19 @@ +#include "SPIFFS.h" + +namespace fs { + + SPIFFSFS::SPIFFSFS() : fs::FS(nullptr), partitionLabel_("spiffs") {} + SPIFFSFS::~SPIFFSFS() {} + bool SPIFFSFS::begin(bool formatOnFail /* = false*/, + const char* basePath /*= "/spiffs"*/, + uint8_t maxOpenFiles /*= 10*/, + const char* partitionLabel /*= NULL*/) { + return true; + } + bool SPIFFSFS::format() { return true; } + size_t SPIFFSFS::totalBytes() { return 1024 * 1024; /* 1 MB */ } + size_t SPIFFSFS::usedBytes() { return 0; } + void SPIFFSFS::end() {} +} + +fs::SPIFFSFS SPIFFS; diff --git a/X86TestSupport/SPIFFS.h b/X86TestSupport/SPIFFS.h index 853b2e0c..a6691b2a 100644 --- a/X86TestSupport/SPIFFS.h +++ b/X86TestSupport/SPIFFS.h @@ -12,8 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef _SPIFFS_H_ -#define _SPIFFS_H_ +#pragma once #include "FS.h" @@ -36,5 +35,3 @@ namespace fs { } extern fs::SPIFFSFS SPIFFS; - -#endif diff --git a/X86TestSupport/SoftwareGPIO.h b/X86TestSupport/SoftwareGPIO.h index 741ee9e6..edb07ca7 100644 --- a/X86TestSupport/SoftwareGPIO.h +++ b/X86TestSupport/SoftwareGPIO.h @@ -3,6 +3,7 @@ #include "Arduino.h" #include #include +#include "esp32-hal-gpio.h" struct SoftwarePin { SoftwarePin() : callback(), argument(nullptr), mode(0), driverValue(false), padValue(false), pinMode(0) {} diff --git a/X86TestSupport/Stream.cpp b/X86TestSupport/Stream.cpp index cf64aff3..2ff9cef7 100644 --- a/X86TestSupport/Stream.cpp +++ b/X86TestSupport/Stream.cpp @@ -22,11 +22,11 @@ #include "Arduino.h" #include "Stream.h" +#include "esp32-hal.h" #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field -/* // private method to read stream with timeout int Stream::timedRead() { int c; @@ -52,7 +52,6 @@ int Stream::timedPeek() { } while ((millis() - _startMillis) < _timeout); return -1; // -1 indicates timeout } -*/ // returns peek of the next digit in the stream or -1 if timeout // discards non-numeric characters @@ -86,20 +85,30 @@ unsigned long Stream::getTimeout(void) { // find returns true if the target string is found bool Stream::find(const char* target) { - return findUntil(target, strlen(target), NULL, 0); + return find(target, strlen(target)); } // reads data from the stream until the target string of given length is found // returns true if target string is found, false if timed out -bool Stream::find(const char* target, size_t length) { - return findUntil(target, length, NULL, 0); +bool Stream::find(const char* target, size_t targetLen) { + int targetIdx = 0; + while (targetIdx < targetLen) { + auto current = read(); + if (current == target[targetIdx]) { + ++targetIdx; + } else { + targetIdx = 0; + } + } + return targetIdx == targetLen; } +/* // as find but search ends if the terminator string is found bool Stream::findUntil(const char* target, const char* terminator) { return findUntil(target, strlen(target), terminator, strlen(terminator)); } -/* + // reads data from the stream until the target string of the given length is found // search terminated if the terminator string is found // returns true if target string is found, false if terminated or timed out diff --git a/X86TestSupport/Stream.h b/X86TestSupport/Stream.h index 948869cb..5e883a15 100644 --- a/X86TestSupport/Stream.h +++ b/X86TestSupport/Stream.h @@ -66,6 +66,7 @@ public: bool find(char target) { return find(&target, 1); } + /* bool findUntil(const char* target, const char* terminator); // as find but search ends if the terminator string is found bool findUntil(const uint8_t* target, const char* terminator) { return findUntil((char*)target, terminator); } @@ -76,6 +77,7 @@ public: bool findUntil(const uint8_t* target, size_t targetLen, const char* terminate, size_t termLen) { return findUntil((char*)target, targetLen, terminate, termLen); } + */ long parseInt(); // returns the first valid (long) integer value from the current position. // initial characters that are not digits (or the minus sign) are skipped diff --git a/X86TestSupport/TMCStepper.h b/X86TestSupport/TMCStepper.h deleted file mode 100644 index 6f70f09b..00000000 --- a/X86TestSupport/TMCStepper.h +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/X86TestSupport/driver/ledc.h b/X86TestSupport/driver/ledc.h index 75c1ef15..8f592404 100644 --- a/X86TestSupport/driver/ledc.h +++ b/X86TestSupport/driver/ledc.h @@ -2,5 +2,12 @@ #include +// PWM: +uint32_t getApbFrequency(); // In Hz +double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits); +void ledcWrite(uint8_t channel, uint32_t duty); +void ledcAttachPin(uint8_t pin, uint8_t channel); +void ledcDetachPin(uint8_t pin); + void pinMatrixOutAttach(uint8_t pin, uint8_t function, bool invertOut, bool invertEnable); void pinMatrixOutDetach(uint8_t pin, bool invertOut, bool invertEnable); diff --git a/X86TestSupport/driver/rmt.cpp b/X86TestSupport/driver/rmt.cpp new file mode 100644 index 00000000..de088084 --- /dev/null +++ b/X86TestSupport/driver/rmt.cpp @@ -0,0 +1,16 @@ +#include "rmt.h" + +rmt_dev_t RMT; + +esp_err_t rmt_set_source_clk(rmt_channel_t channel, rmt_source_clk_t base_clk) { + return ESP_OK; +} + +esp_err_t rmt_config(const rmt_config_t* rmt_param) { + // TODO: figure out the stuff that matter, and push them. + return ESP_OK; +} +esp_err_t rmt_fill_tx_items(rmt_channel_t channel, const rmt_item32_t* item, uint16_t item_num, uint16_t mem_offset) { + // TODO: figure out the stuff that matter, and push them. + return ESP_OK; +} diff --git a/X86TestSupport/driver/rmt.h b/X86TestSupport/driver/rmt.h index 6c010628..04359e9e 100644 --- a/X86TestSupport/driver/rmt.h +++ b/X86TestSupport/driver/rmt.h @@ -14,6 +14,10 @@ #pragma once +#include + +#include "../esp_err.h" + typedef struct rmt_item32_s { union { struct { @@ -350,4 +354,5 @@ typedef volatile struct rmt_dev_s { uint32_t reserved_f8; uint32_t date; /*This is the version register.*/ } rmt_dev_t; + extern rmt_dev_t RMT; diff --git a/X86TestSupport/esp32-hal-cpu.h b/X86TestSupport/esp32-hal-cpu.h new file mode 100644 index 00000000..1f01934b --- /dev/null +++ b/X86TestSupport/esp32-hal-cpu.h @@ -0,0 +1,3 @@ +#pragma once + +#include "driver/ledc.h" diff --git a/X86TestSupport/esp32-hal-dac.h b/X86TestSupport/esp32-hal-dac.h new file mode 100644 index 00000000..ce7d4dca --- /dev/null +++ b/X86TestSupport/esp32-hal-dac.h @@ -0,0 +1,3 @@ +#pragma once + +#include "driver/dac.h" diff --git a/X86TestSupport/esp32-hal-gpio.h b/X86TestSupport/esp32-hal-gpio.h new file mode 100644 index 00000000..c6b2f823 --- /dev/null +++ b/X86TestSupport/esp32-hal-gpio.h @@ -0,0 +1,82 @@ +#pragma once + +#include + +// Interrupt Modes +#define RISING 0x01 +#define FALLING 0x02 +#define CHANGE 0x03 +// #define ONLOW 0x04 --> Weird. Same as falling / rising? +// #define ONHIGH 0x05 +// #define ONLOW_WE 0x0C --> not sure about these +// #define ONHIGH_WE 0x0D + +typedef enum { + GPIO_NUM_0 = 0, /*!< GPIO0, input and output */ + GPIO_NUM_1 = 1, /*!< GPIO1, input and output */ + GPIO_NUM_2 = 2, /*!< GPIO2, input and output + @note There are more enumerations like that + up to GPIO39, excluding GPIO20, GPIO24 and GPIO28..31. + They are not shown here to reduce redundant information. + @note GPIO34..39 are input mode only. */ + /** @cond */ + GPIO_NUM_3 = 3, /*!< GPIO3, input and output */ + GPIO_NUM_4 = 4, /*!< GPIO4, input and output */ + GPIO_NUM_5 = 5, /*!< GPIO5, input and output */ + GPIO_NUM_6 = 6, /*!< GPIO6, input and output */ + GPIO_NUM_7 = 7, /*!< GPIO7, input and output */ + GPIO_NUM_8 = 8, /*!< GPIO8, input and output */ + GPIO_NUM_9 = 9, /*!< GPIO9, input and output */ + GPIO_NUM_10 = 10, /*!< GPIO10, input and output */ + GPIO_NUM_11 = 11, /*!< GPIO11, input and output */ + GPIO_NUM_12 = 12, /*!< GPIO12, input and output */ + GPIO_NUM_13 = 13, /*!< GPIO13, input and output */ + GPIO_NUM_14 = 14, /*!< GPIO14, input and output */ + GPIO_NUM_15 = 15, /*!< GPIO15, input and output */ + GPIO_NUM_16 = 16, /*!< GPIO16, input and output */ + GPIO_NUM_17 = 17, /*!< GPIO17, input and output */ + GPIO_NUM_18 = 18, /*!< GPIO18, input and output */ + GPIO_NUM_19 = 19, /*!< GPIO19, input and output */ + + GPIO_NUM_21 = 21, /*!< GPIO21, input and output */ + GPIO_NUM_22 = 22, /*!< GPIO22, input and output */ + GPIO_NUM_23 = 23, /*!< GPIO23, input and output */ + + GPIO_NUM_25 = 25, /*!< GPIO25, input and output */ + GPIO_NUM_26 = 26, /*!< GPIO26, input and output */ + GPIO_NUM_27 = 27, /*!< GPIO27, input and output */ + + GPIO_NUM_32 = 32, /*!< GPIO32, input and output */ + GPIO_NUM_33 = 33, /*!< GPIO33, input and output */ + GPIO_NUM_34 = 34, /*!< GPIO34, input mode only */ + GPIO_NUM_35 = 35, /*!< GPIO35, input mode only */ + GPIO_NUM_36 = 36, /*!< GPIO36, input mode only */ + GPIO_NUM_37 = 37, /*!< GPIO37, input mode only */ + GPIO_NUM_38 = 38, /*!< GPIO38, input mode only */ + GPIO_NUM_39 = 39, /*!< GPIO39, input mode only */ + GPIO_NUM_MAX = 40, + /** @endcond */ +} gpio_num_t; + +// From esp32-hal-gpio.h: + +#define LOW 0x0 +#define HIGH 0x1 + +// GPIO FUNCTIONS +#define INPUT 0x01 +#define OUTPUT 0x02 +#define PULLUP 0x04 +#define INPUT_PULLUP 0x05 +#define PULLDOWN 0x08 +#define INPUT_PULLDOWN 0x09 +// #define OPEN_DRAIN 0x10 +// #define OUTPUT_OPEN_DRAIN 0x12 + +void attachInterrupt(uint8_t pin, void (*)(void), int mode); +void attachInterruptArg(uint8_t pin, void (*)(void*), void* arg, int mode); +void detachInterrupt(uint8_t pin); + +extern "C" int __digitalRead(uint8_t pin); +extern "C" void __pinMode(uint8_t pin, uint8_t mode); +extern "C" void __digitalWrite(uint8_t pin, uint8_t val); diff --git a/X86TestSupport/esp32-hal-ledc.h b/X86TestSupport/esp32-hal-ledc.h new file mode 100644 index 00000000..1f01934b --- /dev/null +++ b/X86TestSupport/esp32-hal-ledc.h @@ -0,0 +1,3 @@ +#pragma once + +#include "driver/ledc.h" diff --git a/X86TestSupport/esp32-hal-matrix.h b/X86TestSupport/esp32-hal-matrix.h new file mode 100644 index 00000000..1f01934b --- /dev/null +++ b/X86TestSupport/esp32-hal-matrix.h @@ -0,0 +1,3 @@ +#pragma once + +#include "driver/ledc.h" diff --git a/X86TestSupport/esp32-hal-timer.cpp b/X86TestSupport/esp32-hal-timer.cpp new file mode 100644 index 00000000..e5f491fa --- /dev/null +++ b/X86TestSupport/esp32-hal-timer.cpp @@ -0,0 +1,31 @@ +#include "esp32-hal-timer.h" + +uint32_t g_ticks_per_us_pro = 240 * 1000 * 1000; // For CPU 0 - typically 240 MHz +uint32_t g_ticks_per_us_app = 240 * 1000 * 1000; // For CPU 1 - typically 240 MHz + +struct hw_timer_s {}; + +// TODO: These are just stubs. + +void timerAlarmEnable(hw_timer_t* timer) {} +void timerAlarmDisable(hw_timer_t* timer) {} +void timerWrite(hw_timer_t* timer, uint64_t val) {} +void timerAlarmWrite(hw_timer_t* timer, uint64_t interruptAt, bool autoreload) {} + +hw_timer_t* timerBegin(uint8_t timer, uint16_t divider, bool countUp) { + return new hw_timer_t(); +} + +void timerEnd(hw_timer_t* timer) { + delete timer; +} + +void timerAttachInterrupt(hw_timer_t* timer, void (*fn)(void), bool edge) {} +void timerDetachInterrupt(hw_timer_t* timer) {} + +// Figure this out: +extern "C" { +esp_err_t esp_task_wdt_reset(void) { + return ESP_OK; +} +} diff --git a/X86TestSupport/esp32-hal-timer.h b/X86TestSupport/esp32-hal-timer.h new file mode 100644 index 00000000..52ea5867 --- /dev/null +++ b/X86TestSupport/esp32-hal-timer.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include "esp_err.h" + +extern uint32_t g_ticks_per_us_pro; // For CPU 0 - typically 240 MHz +extern uint32_t g_ticks_per_us_app; // For CPU 1 - typically 240 MHz + +// Timer: + +struct hw_timer_s; +typedef struct hw_timer_s hw_timer_t; + +void timerAlarmEnable(hw_timer_t* timer); +void timerAlarmDisable(hw_timer_t* timer); +void timerWrite(hw_timer_t* timer, uint64_t val); +void timerAlarmWrite(hw_timer_t* timer, uint64_t interruptAt, bool autoreload); + +hw_timer_t* timerBegin(uint8_t timer, uint16_t divider, bool countUp); +void timerEnd(hw_timer_t* timer); + +void timerAttachInterrupt(hw_timer_t* timer, void (*fn)(void), bool edge); +void timerDetachInterrupt(hw_timer_t* timer); + +// Figure this out: +extern "C" { +esp_err_t esp_task_wdt_reset(void); +} + +unsigned long micros(); +unsigned long millis(); +void delay(uint32_t); +void delayMicroseconds(uint32_t us); diff --git a/X86TestSupport/esp32-hal.h b/X86TestSupport/esp32-hal.h new file mode 100644 index 00000000..797eb922 --- /dev/null +++ b/X86TestSupport/esp32-hal.h @@ -0,0 +1,3 @@ +#pragma once + +#include "esp32-hal-timer.h" diff --git a/X86TestSupport/esp_attr.h b/X86TestSupport/esp_attr.h new file mode 100644 index 00000000..3b6ef24a --- /dev/null +++ b/X86TestSupport/esp_attr.h @@ -0,0 +1,3 @@ +#pragma once + +#include "Arduino.h" diff --git a/X86TestSupport/freertos/FreeRTOS.h b/X86TestSupport/freertos/FreeRTOS.h index 6cace0c5..6b20e161 100644 --- a/X86TestSupport/freertos/FreeRTOS.h +++ b/X86TestSupport/freertos/FreeRTOS.h @@ -4,10 +4,28 @@ #include "Queue.h" #include "FreeRTOSTypes.h" #include +#include /* "mux" data structure (spinlock) */ -void vTaskExitCritical(portMUX_TYPE* mux); -void vTaskEnterCritical(portMUX_TYPE* mux); +struct portMUX_TYPE { + std::atomic lock_ = { false }; -int32_t xPortGetFreeHeapSize(); + void lock() { + while (lock_.exchange(true, std::memory_order_acquire)) + ; + } + + void unlock() { lock_.store(false, std::memory_order_release); } +}; + +inline void vTaskExitCritical(portMUX_TYPE* mux) { + mux->lock(); +} +inline void vTaskEnterCritical(portMUX_TYPE* mux) { + mux->unlock(); +} + +inline int32_t xPortGetFreeHeapSize() { + return 1024 * 1024 * 4; +} diff --git a/X86TestSupport/freertos/FreeRTOSTypes.h b/X86TestSupport/freertos/FreeRTOSTypes.h index d08c806a..1b8351d6 100644 --- a/X86TestSupport/freertos/FreeRTOSTypes.h +++ b/X86TestSupport/freertos/FreeRTOSTypes.h @@ -2,8 +2,6 @@ #include -using portMUX_TYPE = std::mutex; - #define portMAX_DELAY (TickType_t)0xffffffffUL using portBASE_TYPE = int; diff --git a/X86TestSupport/soc/ledc_struct.cpp b/X86TestSupport/soc/ledc_struct.cpp new file mode 100644 index 00000000..ee77a10b --- /dev/null +++ b/X86TestSupport/soc/ledc_struct.cpp @@ -0,0 +1,3 @@ +#include "ledc_struct.h" + +ledc_dev_t LEDC; diff --git a/X86TestSupport/soc/ledc_struct.h b/X86TestSupport/soc/ledc_struct.h index 1b36807c..86e7a1a1 100644 --- a/X86TestSupport/soc/ledc_struct.h +++ b/X86TestSupport/soc/ledc_struct.h @@ -1,5 +1,7 @@ #pragma once +#include + // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/X86TestSupport/xtensa/core-macros.h b/X86TestSupport/xtensa/core-macros.h new file mode 100644 index 00000000..e6575dc3 --- /dev/null +++ b/X86TestSupport/xtensa/core-macros.h @@ -0,0 +1,7 @@ +#pragma once + +#include "../esp32-hal-timer.h" + +inline int32_t IRAM_ATTR XTHAL_GET_CCOUNT() { + return int32_t(millis()); +}