diff --git a/CHANGELOG.md b/CHANGELOG.md index a23c3dd27..ce7a889fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,18 @@ # Changelog +## [1.5.b] - Unreleased + +### Added + +- added core support for ESP8266 with limitied functionalities (#222) + +### Changed + +- configurable RX serial buffer size (#222) + +### Fixed + ## [1.4.7] - 2022-07-29 ### Added @@ -926,6 +938,7 @@ Version 1.1.0 comes with many added features and improvements over the previous ### Initial release +[1.5.b]: https://github.com/Paciente8159/uCNC/releases/tag/v1.5.b [1.4.7]: https://github.com/Paciente8159/uCNC/releases/tag/v1.4.7 [1.4.6]: https://github.com/Paciente8159/uCNC/releases/tag/v1.4.6 [1.4.5]: https://github.com/Paciente8159/uCNC/releases/tag/v1.4.5 diff --git a/platformio.ini b/platformio.ini index 3f42234a4..90546e361 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,6 +16,7 @@ extra_configs = uCNC/src/hal/boards/avr/avr.ini uCNC/src/hal/boards/samd21/samd21.ini uCNC/src/hal/boards/stm32/stm32.ini + uCNC/src/hal/boards/esp8266/esp8266.ini ; uCNC/src/hal/mcus/virtual/virtual.ini [common] diff --git a/uCNC/cnc_config.h b/uCNC/cnc_config.h index dd849fe2f..9b2b1d2c7 100644 --- a/uCNC/cnc_config.h +++ b/uCNC/cnc_config.h @@ -25,7 +25,7 @@ extern "C" { #endif -#define INTERFACE_USART 0 +#define INTERFACE_UART 0 #define INTERFACE_USB 1 /** @@ -39,7 +39,11 @@ extern "C" #endif #ifndef INTERFACE -#define INTERFACE INTERFACE_USART +#define INTERFACE INTERFACE_UART +#endif + +#ifndef ENABLE_WIFI +// #define ENABLE_WIFI #endif /** @@ -94,7 +98,7 @@ extern "C" * Uncomment to enable G92 storring on non volatile memory * If disabled G92 will be stored in RAM only. Soft-reset will not erase stored value. * */ -// #define G92_STORE_NONVOLATILE + // #define G92_STORE_NONVOLATILE /** * Number of segments of an arc computed with aprox. of sin/cos math @@ -417,6 +421,27 @@ extern "C" // #define ENABLE_FAST_MATH +/** + * + * HAL offsets + * + * */ +#ifndef PWM_PINS_OFFSET +#define PWM_PINS_OFFSET 24 +#endif +#ifndef SERVO_PINS_OFFSET +#define SERVO_PINS_OFFSET 40 +#endif +#ifndef DOUT_PINS_OFFSET +#define DOUT_PINS_OFFSET 46 +#endif +#ifndef ANALOG_PINS_OFFSET +#define ANALOG_PINS_OFFSET 114 +#endif +#ifndef DIN_PINS_OFFSET +#define DIN_PINS_OFFSET 130 +#endif + #ifdef __cplusplus } #endif diff --git a/uCNC/src/cnc.c b/uCNC/src/cnc.c index be22d42db..aab344e26 100644 --- a/uCNC/src/cnc.c +++ b/uCNC/src/cnc.c @@ -206,7 +206,7 @@ bool cnc_dotasks(void) } // this function is executed every millisecond -void mcu_rtc_cb(uint32_t millis) +MCU_CALLBACK void mcu_rtc_cb(uint32_t millis) { static bool running = false; diff --git a/uCNC/src/cnc_build.h b/uCNC/src/cnc_build.h index 08a1833cc..918c705ec 100644 --- a/uCNC/src/cnc_build.h +++ b/uCNC/src/cnc_build.h @@ -24,8 +24,8 @@ extern "C" { #endif -#define CNC_MAJOR_MINOR_VERSION "1.4" -#define CNC_PATCH_VERSION ".7" +#define CNC_MAJOR_MINOR_VERSION "1.5" +#define CNC_PATCH_VERSION ".beta" #define CNC_VERSION CNC_MAJOR_MINOR_VERSION CNC_PATCH_VERSION diff --git a/uCNC/src/core/interpolator.c b/uCNC/src/core/interpolator.c index c08670f7e..1ab0a9e60 100644 --- a/uCNC/src/core/interpolator.c +++ b/uCNC/src/core/interpolator.c @@ -1080,13 +1080,13 @@ uint32_t itp_get_rt_line_number(void) #endif // always fires after pulse -void mcu_step_reset_cb(void) +MCU_CALLBACK void mcu_step_reset_cb(void) { // always resets all stepper pins io_set_steps(g_settings.step_invert_mask); } -void mcu_step_cb(void) +MCU_CALLBACK void mcu_step_cb(void) { static uint8_t stepbits = 0; static bool itp_busy = false; diff --git a/uCNC/src/core/io_control.c b/uCNC/src/core/io_control.c index 09ef45a03..4ef99a6a7 100644 --- a/uCNC/src/core/io_control.c +++ b/uCNC/src/core/io_control.c @@ -27,7 +27,7 @@ static volatile uint8_t io_spindle_speed; static uint8_t io_lock_limits_mask; static uint8_t io_invert_limits_mask; -void mcu_limits_changed_cb(void) +MCU_IO_CALLBACK void mcu_limits_changed_cb(void) { #ifndef DISABLE_ALL_LIMITS @@ -112,7 +112,7 @@ void mcu_limits_changed_cb(void) #endif } -void mcu_controls_changed_cb(void) +MCU_IO_CALLBACK void mcu_controls_changed_cb(void) { #ifdef DISABLE_ALL_CONTROLS return; @@ -157,7 +157,7 @@ void mcu_controls_changed_cb(void) #endif } -void mcu_probe_changed_cb(void) +MCU_IO_CALLBACK void mcu_probe_changed_cb(void) { #ifdef DISABLE_PROBE return; @@ -183,7 +183,7 @@ void mcu_probe_changed_cb(void) // overridable // for now if encoders are enabled this will be override by the encoder call -void __attribute__((weak)) mcu_inputs_changed_cb(void) +MCU_IO_CALLBACK void __attribute__((weak)) mcu_inputs_changed_cb(void) { #ifdef ENABLE_IO_MODULES mod_input_change_hook(); diff --git a/uCNC/src/hal/boards/boarddefs.h b/uCNC/src/hal/boards/boarddefs.h index dfe5aa203..decac884b 100644 --- a/uCNC/src/hal/boards/boarddefs.h +++ b/uCNC/src/hal/boards/boarddefs.h @@ -93,6 +93,16 @@ extern "C" #include "samd21/boardmap_zero.h" #endif +#if (BOARD == BOARD_RE_ARM) +#define MCU MCU_LPC176X +#include "lpc176x/boardmap_re_arm.h" +#endif + +#if (BOARD == BOARD_WEMOS_D1) +#define MCU MCU_ESP8266 +#include "esp8266/boardmap_wemos_d1.h" +#endif + #if (BOARD == BOARD_VIRTUAL) #ifndef __linux__ #define MCU MCU_VIRTUAL_WIN diff --git a/uCNC/src/hal/boards/boards.h b/uCNC/src/hal/boards/boards.h index 4c340c684..72cd051b1 100644 --- a/uCNC/src/hal/boards/boards.h +++ b/uCNC/src/hal/boards/boards.h @@ -34,6 +34,8 @@ extern "C" #define BOARD_BLACKPILL 11 #define BOARD_MZERO 20 #define BOARD_ZERO 21 +#define BOARD_RE_ARM 30 +#define BOARD_WEMOS_D1 40 #define BOARD_VIRTUAL 99 // special purpose board diff --git a/uCNC/src/hal/boards/esp8266/boardmap_wemos_d1.h b/uCNC/src/hal/boards/esp8266/boardmap_wemos_d1.h new file mode 100644 index 000000000..7fa91566a --- /dev/null +++ b/uCNC/src/hal/boards/esp8266/boardmap_wemos_d1.h @@ -0,0 +1,80 @@ +/* + Name: boardmap_wemos_d1.h + Description: Contains all MCU and PIN definitions for Arduino WeMos D1 to run µCNC. + + Copyright: Copyright (c) João Martins + Author: João Martins + Date: 17/06/2022 + + µCNC 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. Please see + + µCNC is distributed WITHOUT ANY WARRANTY; + Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. +*/ + +#ifndef BOARDMAP_WEMOS_D1_H +#define BOARDMAP_WEMOS_D1_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#ifndef BOARD_NAME +#define BOARD_NAME "WEMOS D1" +#endif + +// SAME AS GRBL for test purposes +// Setup step pins +#define STEP2_BIT 4 // assigns STEP2 pin +#define STEP2_PORT D // assigns STEP2 port +#define STEP1_BIT 5 // assigns STEP1 pin +#define STEP1_PORT D // assigns STEP1 port +#define STEP0_BIT 16 // assigns STEP0 pin +#define STEP0_PORT D // assigns STEP0 port + +// Setup dir pins +#define DIR2_BIT 13 // assigns DIR2 pin +#define DIR2_PORT D // assigns DIR2 port +#define DIR1_BIT 12 // assigns DIR1 pin +#define DIR1_PORT D // assigns DIR1 port +#define DIR0_BIT 14 // assigns DIR0 pin +#define DIR0_PORT D // assigns DIR0 port + +// Setup control input pins +// #define ESTOP_BIT 0 +// #define ESTOP_PORT A +//#define ESTOP_ISR + +// Setup com pins +#if (INTERFACE == INTERFACE_UART) +#define RX_BIT 3 +#define TX_BIT 1 +#define RX_PORT D +#define TX_PORT D +// only uncomment this if other port other then 0 is used +// #define COM_PORT 0 +#endif + + // Setup PWM +#define PWM0_BIT 2 // assigns PWM0 pin +#define PWM0_PORT D // assigns PWM0 pin + +// Setup generic IO Pins +// spindle dir +#define DOUT0_BIT 15 +#define DOUT0_PORT D + +// Stepper enable pin. For Grbl on Uno board a single pin is used +#define STEP0_EN_BIT 0 +#define STEP0_EN_PORT D + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/uCNC/src/hal/boards/esp8266/esp8266.ini b/uCNC/src/hal/boards/esp8266/esp8266.ini new file mode 100644 index 000000000..f35790575 --- /dev/null +++ b/uCNC/src/hal/boards/esp8266/esp8266.ini @@ -0,0 +1,14 @@ +################ +# STM32 Boards # +################ + +[env:d1] +platform = espressif8266 +framework = arduino +board = d1 +build_src_filter = +<*>- +lib_deps = + https://github.com/tzapu/WiFiManager/archive/refs/heads/master.zip +build_flags = -DBOARD=BOARD_WEMOS_D1 -DINTERFACE=0 -DENABLE_WIFI +upload_speed = 256000 +board_build.f_cpu = 160000000L diff --git a/uCNC/src/hal/mcus/esp8266/esp8266_eeprom.cpp b/uCNC/src/hal/mcus/esp8266/esp8266_eeprom.cpp new file mode 100644 index 000000000..6a48d040c --- /dev/null +++ b/uCNC/src/hal/mcus/esp8266/esp8266_eeprom.cpp @@ -0,0 +1,34 @@ +#include "../../../../cnc_config.h" +#ifdef ESP8266 +#ifndef RAM_ONLY_SETTINGS +#include +#include +#include +extern "C" +{ + void esp8266_eeprom_init(int size) + { + EEPROM.begin(size); + } + + uint8_t esp8266_eeprom_read(uint16_t address) + { + return EEPROM.read(address); + } + + void esp8266_eeprom_write(uint16_t address, uint8_t value) + { + EEPROM.write(address, value); + } + + void esp8266_eeprom_flush(void) + { + if (!EEPROM.commit()) + { + Serial.println("[MSG: EEPROM write error]"); + } + } +} + +#endif +#endif \ No newline at end of file diff --git a/uCNC/src/hal/mcus/esp8266/esp8266_uart.cpp b/uCNC/src/hal/mcus/esp8266/esp8266_uart.cpp new file mode 100644 index 000000000..e590bfd79 --- /dev/null +++ b/uCNC/src/hal/mcus/esp8266/esp8266_uart.cpp @@ -0,0 +1,195 @@ +/* + Name: esp8266_uart.cpp + Description: Contains all Arduino ESP8266 C++ to C functions used by UART in µCNC. + + Copyright: Copyright (c) João Martins + Author: João Martins + Date: 27-07-2022 + + µCNC 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. Please see + + µCNC is distributed WITHOUT ANY WARRANTY; + Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. +*/ + +#include "../../../../cnc_config.h" +#include "../mcu.h" +#ifdef ESP8266 +#include +#include +#include + +#ifdef ENABLE_WIFI +#include +#include +#include +#include +#include +#include + +#ifndef WIFI_PORT +#define WIFI_PORT 23 +#endif + +#ifndef WIFI_USER +#define WIFI_USER "admin" +#endif + +#ifndef WIFI_PASS +#define WIFI_PASS "pass" +#endif + +ESP8266WebServer httpServer(80); +ESP8266HTTPUpdateServer httpUpdater; +const char *update_path = "/firmware"; +const char *update_username = WIFI_USER; +const char *update_password = WIFI_PASS; +#define MAX_SRV_CLIENTS 1 +WiFiServer server(WIFI_PORT); +WiFiClient serverClient; +WiFiManager wifiManager; +#endif + +#ifndef ESP8266_BUFFER_SIZE +#define ESP8266_BUFFER_SIZE 255 +#endif + +static char esp8266_tx_buffer[ESP8266_BUFFER_SIZE]; +static uint8_t esp8266_tx_buffer_counter; + +extern "C" +{ + bool esp8266_wifi_clientok(void) + { + if (server.hasClient()) + { + if (serverClient) + { + if (serverClient.connected()) + { + serverClient.stop(); + } + } + serverClient = server.available(); + serverClient.println("[MSG: New client connected]\r\n"); + return false; + } + else if (serverClient) + { + if (serverClient.connected()) + { + return true; + } + } + + return false; + } + + void esp8266_uart_init(int baud) + { + Serial.begin(baud); +#ifdef WIFI_DEBUG + wifiManager.setDebugOutput(true); +#else + wifiManager.setDebugOutput(false); +#endif + wifiManager.setConfigPortalBlocking(false); + wifiManager.setConfigPortalTimeout(30); + if (!wifiManager.autoConnect("ESP8266")) + { + Serial.println("[MSG: WiFi manager up]"); + Serial.println("[MSG: Setup page @ 192.168.4.1]"); + } + + server.begin(); + server.setNoDelay(true); + httpUpdater.setup(&httpServer, update_path, update_username, update_password); + httpServer.begin(); + WiFi.setSleepMode(WIFI_NONE_SLEEP); + + esp8266_tx_buffer_counter = 0; + } + + void esp8266_uart_flush(void) + { + Serial.println(esp8266_tx_buffer); + Serial.flush(); + if (esp8266_wifi_clientok()) + { + serverClient.println(esp8266_tx_buffer); + serverClient.flush(); + } + esp8266_tx_buffer_counter = 0; + } + + unsigned char esp8266_uart_read(void) + { + return (unsigned char)Serial.read(); + } + + void esp8266_uart_write(char c) + { + switch (c) + { + case '\n': + case '\r': + if (esp8266_tx_buffer_counter) + { + esp8266_tx_buffer[esp8266_tx_buffer_counter] = 0; + esp8266_uart_flush(); + } + break; + default: + if (esp8266_tx_buffer_counter >= (ESP8266_BUFFER_SIZE - 1)) + { + esp8266_tx_buffer[esp8266_tx_buffer_counter] = 0; + esp8266_uart_flush(); + } + + esp8266_tx_buffer[esp8266_tx_buffer_counter++] = c; + break; + } + } + + bool esp8266_uart_rx_ready(void) + { + bool wifiready = false; + if (esp8266_wifi_clientok()) + { + wifiready = (serverClient.available() > 0); + } + + return ((Serial.available() > 0) || wifiready); + } + + bool esp8266_uart_tx_ready(void) + { + return (esp8266_tx_buffer_counter != ESP8266_BUFFER_SIZE); + } + + void esp8266_uart_process(void) + { + while (Serial.available() > 0) + { + system_soft_wdt_feed(); + mcu_com_rx_cb((unsigned char)Serial.read()); + } + + wifiManager.process(); + httpServer.handleClient(); + if (esp8266_wifi_clientok()) + { + while (serverClient.available() > 0) + { + system_soft_wdt_feed(); + mcu_com_rx_cb((unsigned char)serverClient.read()); + } + } + } +} + +#endif diff --git a/uCNC/src/hal/mcus/esp8266/mcu_esp8266.c b/uCNC/src/hal/mcus/esp8266/mcu_esp8266.c new file mode 100644 index 000000000..38586d9ad --- /dev/null +++ b/uCNC/src/hal/mcus/esp8266/mcu_esp8266.c @@ -0,0 +1,1317 @@ +/* + Name: mcu_esp8266.c + Description: Implements the µCNC HAL for ESP8266. + + Copyright: Copyright (c) João Martins + Author: João Martins + Date: 05-02-2022 + + µCNC 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. Please see + + µCNC is distributed WITHOUT ANY WARRANTY; + Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. +*/ + +#include "../../../cnc.h" + +#if (MCU == MCU_ESP8266) + +#include +#include +#include +#include +#include "ets_sys.h" +#include "os_type.h" +#include "gpio.h" +#include "eagle_soc.h" +#include "uart_register.h" +#include "osapi.h" + +#define UART_PARITY_EN (BIT(1)) +#define UART_PARITY_EN_M 0x00000001 +#define UART_PARITY_EN_S 1 +#define UART_PARITY (BIT(0)) +#define UART_PARITY_M 0x00000001 +#define UART_PARITY_S 0 + +static volatile bool esp8266_global_isr_enabled; +static volatile uint32_t mcu_runtime_ms; + +void esp8266_uart_init(int baud); +char esp8266_uart_read(void); +void esp8266_uart_write(char c); +bool esp8266_uart_rx_ready(void); +bool esp8266_uart_tx_ready(void); +void esp8266_uart_flush(void); +void esp8266_uart_process(void); + +#ifndef RAM_ONLY_SETTINGS +void esp8266_eeprom_init(int size); +uint8_t esp8266_eeprom_read(uint16_t address); +void esp8266_eeprom_write(uint16_t address, uint8_t value); +void esp8266_eeprom_flush(void); +#endif + +ETSTimer esp8266_rtc_timer; + +uint8_t esp8266_pwm[16]; +static IRAM_ATTR void mcu_gen_pwm(void) +{ + static uint8_t pwm_counter = 0; + // software PWM + if (++pwm_counter < 127) + { +#if !(PWM0 < 0) + if (pwm_counter > esp8266_pwm[0]) + { + mcu_clear_output(PWM0); + } +#endif +#if !(PWM1 < 0) + if (pwm_counter > esp8266_pwm[1]) + { + mcu_clear_output(PWM1); + } +#endif +#if !(PWM2 < 0) + if (pwm_counter > esp8266_pwm[2]) + { + mcu_clear_output(PWM2); + } +#endif +#if !(PWM3 < 0) + if (pwm_counter > esp8266_pwm[3]) + { + mcu_clear_output(PWM3); + } +#endif +#if !(PWM4 < 0) + if (pwm_counter > esp8266_pwm[4]) + { + mcu_clear_output(PWM4); + } +#endif +#if !(PWM5 < 0) + if (pwm_counter > esp8266_pwm[5]) + { + mcu_clear_output(PWM5); + } +#endif +#if !(PWM6 < 0) + if (pwm_counter > esp8266_pwm[6]) + { + mcu_clear_output(PWM6); + } +#endif +#if !(PWM7 < 0) + if (pwm_counter > esp8266_pwm[7]) + { + mcu_clear_output(PWM7); + } +#endif +#if !(PWM8 < 0) + if (pwm_counter > esp8266_pwm[8]) + { + mcu_clear_output(PWM8); + } +#endif +#if !(PWM9 < 0) + if (pwm_counter > esp8266_pwm[9]) + { + mcu_clear_output(PWM9); + } +#endif +#if !(PWM10 < 0) + if (pwm_counter > esp8266_pwm[10]) + { + mcu_clear_output(PWM10); + } +#endif +#if !(PWM11 < 0) + if (pwm_counter > esp8266_pwm[11]) + { + mcu_clear_output(PWM11); + } +#endif +#if !(PWM12 < 0) + if (pwm_counter > esp8266_pwm[12]) + { + mcu_clear_output(PWM12); + } +#endif +#if !(PWM13 < 0) + if (pwm_counter > esp8266_pwm[13]) + { + mcu_clear_output(PWM13); + } +#endif +#if !(PWM14 < 0) + if (pwm_counter > esp8266_pwm[14]) + { + mcu_clear_output(PWM14); + } +#endif +#if !(PWM15 < 0) + if (pwm_counter > esp8266_pwm[15]) + { + mcu_clear_output(PWM15); + } +#endif + } + else + { + pwm_counter = 0; +#if !(PWM0 < 0) + if (esp8266_pwm[0]) + { + mcu_set_output(PWM0); + } +#endif +#if !(PWM1 < 0) + if (esp8266_pwm[1]) + { + mcu_set_output(PWM1); + } +#endif +#if !(PWM2 < 0) + if (esp8266_pwm[2]) + { + mcu_set_output(PWM2); + } +#endif +#if !(PWM3 < 0) + if (esp8266_pwm[3]) + { + mcu_set_output(PWM3); + } +#endif +#if !(PWM4 < 0) + if (esp8266_pwm[4]) + { + mcu_set_output(PWM4); + } +#endif +#if !(PWM5 < 0) + if (esp8266_pwm[5]) + { + mcu_set_output(PWM5); + } +#endif +#if !(PWM6 < 0) + if (esp8266_pwm[6]) + { + mcu_set_output(PWM6); + } +#endif +#if !(PWM7 < 0) + if (esp8266_pwm[7]) + { + mcu_set_output(PWM7); + } +#endif +#if !(PWM8 < 0) + if (esp8266_pwm[8]) + { + mcu_set_output(PWM8); + } +#endif +#if !(PWM9 < 0) + if (esp8266_pwm[9]) + { + mcu_set_output(PWM9); + } +#endif +#if !(PWM10 < 0) + if (esp8266_pwm[10]) + { + mcu_set_output(PWM10); + } +#endif +#if !(PWM11 < 0) + if (esp8266_pwm[11]) + { + mcu_set_output(PWM11); + } +#endif +#if !(PWM12 < 0) + if (esp8266_pwm[12]) + { + mcu_set_output(PWM12); + } +#endif +#if !(PWM13 < 0) + if (esp8266_pwm[13]) + { + mcu_set_output(PWM13); + } +#endif +#if !(PWM14 < 0) + if (esp8266_pwm[14]) + { + mcu_set_output(PWM14); + } +#endif +#if !(PWM15 < 0) + if (esp8266_pwm[15]) + { + mcu_set_output(PWM15); + } +#endif + } +} + +static uint32_t mcu_step_counter; +static uint32_t mcu_step_reload; +static IRAM_ATTR void mcu_gen_step(void) +{ + if (mcu_step_reload) + { + if (!--mcu_step_counter) + { + static bool resetstep = false; + if (!resetstep) + mcu_step_cb(); + else + mcu_step_reset_cb(); + resetstep = !resetstep; + mcu_step_counter = mcu_step_reload; + } + } +} + +IRAM_ATTR void mcu_din_isr(void) +{ + io_inputs_isr(); +} + +IRAM_ATTR void mcu_probe_isr(void) +{ + io_probe_isr(); +} + +IRAM_ATTR void mcu_limits_isr(void) +{ + io_limits_isr(); +} + +IRAM_ATTR void mcu_controls_isr(void) +{ + io_controls_isr(); +} + +IRAM_ATTR void mcu_rtc_isr(void *arg) +{ + mcu_runtime_ms++; + mcu_rtc_cb(mcu_runtime_ms); +} + +IRAM_ATTR void mcu_itp_isr(void) +{ + // mcu_disable_global_isr(); + // static bool resetstep = false; + // if (!resetstep) + // mcu_step_cb(); + // else + // mcu_step_reset_cb(); + // resetstep = !resetstep; + // mcu_enable_global_isr(); + mcu_gen_step(); + mcu_gen_pwm(); +} + +// static void mcu_uart_isr(void *arg) +// { +// /*ATTENTION:*/ +// /*IN NON-OS VERSION SDK, DO NOT USE "ICACHE_FLASH_ATTR" FUNCTIONS IN THE WHOLE HANDLER PROCESS*/ +// /*ALL THE FUNCTIONS CALLED IN INTERRUPT HANDLER MUST BE DECLARED IN RAM */ +// /*IF NOT , POST AN EVENT AND PROCESS IN SYSTEM TASK */ +// if ((READ_PERI_REG(UART_INT_ST(0)) & UART_FRM_ERR_INT_ST)) +// { +// WRITE_PERI_REG(UART_INT_CLR(0), UART_FRM_ERR_INT_CLR); +// } +// else if ((READ_PERI_REG(UART_INT_ST(0)) & (UART_RXFIFO_FULL_INT_ST | UART_RXFIFO_TOUT_INT_ST))) +// { +// // disable ISR +// CLEAR_PERI_REG_MASK(UART_INT_ENA(0), UART_RXFIFO_FULL_INT_ENA | UART_RXFIFO_TOUT_INT_ENA); +// WRITE_PERI_REG(UART_INT_CLR(0), (READ_PERI_REG(UART_INT_ST(0)) & (UART_RXFIFO_FULL_INT_ST | UART_RXFIFO_TOUT_INT_ST))); +// uint8_t fifo_len = (READ_PERI_REG(UART_STATUS(0)) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT; +// unsigned char c = 0; + +// for (uint8_t i = 0; i < fifo_len; i++) +// { +// c = READ_PERI_REG(UART_FIFO(0)) & 0xFF; +// mcu_com_rx_cb(c); +// } + +// WRITE_PERI_REG(UART_INT_CLR(0), UART_RXFIFO_FULL_INT_CLR | UART_RXFIFO_TOUT_INT_CLR); +// // reenable ISR +// SET_PERI_REG_MASK(UART_INT_ENA(0), UART_RXFIFO_FULL_INT_ENA | UART_RXFIFO_TOUT_INT_ENA); +// } +// else if (UART_TXFIFO_EMPTY_INT_ST == (READ_PERI_REG(UART_INT_ST(0)) & UART_TXFIFO_EMPTY_INT_ST)) +// { +// /* to output uart data from uart buffer directly in empty interrupt handler*/ +// /*instead of processing in system event, in order not to wait for current task/function to quit */ +// /*ATTENTION:*/ +// /*IN NON-OS VERSION SDK, DO NOT USE "ICACHE_FLASH_ATTR" FUNCTIONS IN THE WHOLE HANDLER PROCESS*/ +// /*ALL THE FUNCTIONS CALLED IN INTERRUPT HANDLER MUST BE DECLARED IN RAM */ +// CLEAR_PERI_REG_MASK(UART_INT_ENA(0), UART_TXFIFO_EMPTY_INT_ENA); +// mcu_com_tx_cb(); +// // system_os_post(uart_recvTaskPrio, 1, 0); +// WRITE_PERI_REG(UART_INT_CLR(0), UART_TXFIFO_EMPTY_INT_CLR); +// } +// else if (UART_RXFIFO_OVF_INT_ST == (READ_PERI_REG(UART_INT_ST(0)) & UART_RXFIFO_OVF_INT_ST)) +// { +// WRITE_PERI_REG(UART_INT_CLR(0), UART_RXFIFO_OVF_INT_CLR); +// } +// } + +static void mcu_usart_init(void) +{ + esp8266_uart_init(BAUDRATE); +} +/** + * initializes the mcu + * this function needs to: + * - configure all IO pins (digital IO, PWM, Analog, etc...) + * - configure all interrupts + * - configure uart or usb + * - start the internal RTC + * */ +void mcu_init(void) +{ +#if STEP0 >= 0 + mcu_config_output(STEP0); +#endif +#if STEP1 >= 0 + mcu_config_output(STEP1); +#endif +#if STEP2 >= 0 + mcu_config_output(STEP2); +#endif +#if STEP3 >= 0 + mcu_config_output(STEP3); +#endif +#if STEP4 >= 0 + mcu_config_output(STEP4); +#endif +#if STEP5 >= 0 + mcu_config_output(STEP5); +#endif +#if STEP6 >= 0 + mcu_config_output(STEP6); +#endif +#if STEP7 >= 0 + mcu_config_output(STEP7); +#endif +#if DIR0 >= 0 + mcu_config_output(DIR0); +#endif +#if DIR1 >= 0 + mcu_config_output(DIR1); +#endif +#if DIR2 >= 0 + mcu_config_output(DIR2); +#endif +#if DIR3 >= 0 + mcu_config_output(DIR3); +#endif +#if DIR4 >= 0 + mcu_config_output(DIR4); +#endif +#if DIR5 >= 0 + mcu_config_output(DIR5); +#endif +#if DIR6 >= 0 + mcu_config_output(DIR6); +#endif +#if DIR7 >= 0 + mcu_config_output(DIR7); +#endif +#if STEP0_EN >= 0 + mcu_config_output(STEP0_EN); +#endif +#if STEP1_EN >= 0 + mcu_config_output(STEP1_EN); +#endif +#if STEP2_EN >= 0 + mcu_config_output(STEP2_EN); +#endif +#if STEP3_EN >= 0 + mcu_config_output(STEP3_EN); +#endif +#if STEP4_EN >= 0 + mcu_config_output(STEP4_EN); +#endif +#if STEP5_EN >= 0 + mcu_config_output(STEP5_EN); +#endif +#if STEP6_EN >= 0 + mcu_config_output(STEP6_EN); +#endif +#if STEP7_EN >= 0 + mcu_config_output(STEP7_EN); +#endif +#if PWM0 >= 0 + mcu_config_pwm(PWM0); +#endif +#if PWM1 >= 0 + mcu_config_pwm(PWM1); +#endif +#if PWM2 >= 0 + mcu_config_pwm(PWM2); +#endif +#if PWM3 >= 0 + mcu_config_pwm(PWM3); +#endif +#if PWM4 >= 0 + mcu_config_pwm(PWM4); +#endif +#if PWM5 >= 0 + mcu_config_pwm(PWM5); +#endif +#if PWM6 >= 0 + mcu_config_pwm(PWM6); +#endif +#if PWM7 >= 0 + mcu_config_pwm(PWM7); +#endif +#if PWM8 >= 0 + mcu_config_pwm(PWM8); +#endif +#if PWM9 >= 0 + mcu_config_pwm(PWM9); +#endif +#if PWM10 >= 0 + mcu_config_pwm(PWM10); +#endif +#if PWM11 >= 0 + mcu_config_pwm(PWM11); +#endif +#if PWM12 >= 0 + mcu_config_pwm(PWM12); +#endif +#if PWM13 >= 0 + mcu_config_pwm(PWM13); +#endif +#if PWM14 >= 0 + mcu_config_pwm(PWM14); +#endif +#if PWM15 >= 0 + mcu_config_pwm(PWM15); +#endif +#if SERVO0 >= 0 + mcu_config_output(SERVO0); +#endif +#if SERVO1 >= 0 + mcu_config_output(SERVO1); +#endif +#if SERVO2 >= 0 + mcu_config_output(SERVO2); +#endif +#if SERVO3 >= 0 + mcu_config_output(SERVO3); +#endif +#if SERVO4 >= 0 + mcu_config_output(SERVO4); +#endif +#if SERVO5 >= 0 + mcu_config_output(SERVO5); +#endif +#if DOUT0 >= 0 + mcu_config_output(DOUT0); +#endif +#if DOUT1 >= 0 + mcu_config_output(DOUT1); +#endif +#if DOUT2 >= 0 + mcu_config_output(DOUT2); +#endif +#if DOUT3 >= 0 + mcu_config_output(DOUT3); +#endif +#if DOUT4 >= 0 + mcu_config_output(DOUT4); +#endif +#if DOUT5 >= 0 + mcu_config_output(DOUT5); +#endif +#if DOUT6 >= 0 + mcu_config_output(DOUT6); +#endif +#if DOUT7 >= 0 + mcu_config_output(DOUT7); +#endif +#if DOUT8 >= 0 + mcu_config_output(DOUT8); +#endif +#if DOUT9 >= 0 + mcu_config_output(DOUT9); +#endif +#if DOUT10 >= 0 + mcu_config_output(DOUT10); +#endif +#if DOUT11 >= 0 + mcu_config_output(DOUT11); +#endif +#if DOUT12 >= 0 + mcu_config_output(DOUT12); +#endif +#if DOUT13 >= 0 + mcu_config_output(DOUT13); +#endif +#if DOUT14 >= 0 + mcu_config_output(DOUT14); +#endif +#if DOUT15 >= 0 + mcu_config_output(DOUT15); +#endif +#if DOUT16 >= 0 + mcu_config_output(DOUT16); +#endif +#if DOUT17 >= 0 + mcu_config_output(DOUT17); +#endif +#if DOUT18 >= 0 + mcu_config_output(DOUT18); +#endif +#if DOUT19 >= 0 + mcu_config_output(DOUT19); +#endif +#if DOUT20 >= 0 + mcu_config_output(DOUT20); +#endif +#if DOUT21 >= 0 + mcu_config_output(DOUT21); +#endif +#if DOUT22 >= 0 + mcu_config_output(DOUT22); +#endif +#if DOUT23 >= 0 + mcu_config_output(DOUT23); +#endif +#if DOUT24 >= 0 + mcu_config_output(DOUT24); +#endif +#if DOUT25 >= 0 + mcu_config_output(DOUT25); +#endif +#if DOUT26 >= 0 + mcu_config_output(DOUT26); +#endif +#if DOUT27 >= 0 + mcu_config_output(DOUT27); +#endif +#if DOUT28 >= 0 + mcu_config_output(DOUT28); +#endif +#if DOUT29 >= 0 + mcu_config_output(DOUT29); +#endif +#if DOUT30 >= 0 + mcu_config_output(DOUT30); +#endif +#if DOUT31 >= 0 + mcu_config_output(DOUT31); +#endif +#if LIMIT_X >= 0 + mcu_config_input(LIMIT_X); +#ifdef LIMIT_X_PULLUP + mcu_config_pullup(LIMIT_X); +#endif +#ifdef LIMIT_X_ISR + mcu_config_input_isr(LIMIT_X); +#endif +#endif +#if LIMIT_Y >= 0 + mcu_config_input(LIMIT_Y); +#ifdef LIMIT_Y_PULLUP + mcu_config_pullup(LIMIT_Y); +#endif +#ifdef LIMIT_Y_ISR + mcu_config_input_isr(LIMIT_Y); +#endif +#endif +#if LIMIT_Z >= 0 + mcu_config_input(LIMIT_Z); +#ifdef LIMIT_Z_PULLUP + mcu_config_pullup(LIMIT_Z); +#endif +#ifdef LIMIT_Z_ISR + mcu_config_input_isr(LIMIT_Z); +#endif +#endif +#if LIMIT_X2 >= 0 + mcu_config_input(LIMIT_X2); +#ifdef LIMIT_X2_PULLUP + mcu_config_pullup(LIMIT_X2); +#endif +#ifdef LIMIT_X2_ISR + mcu_config_input_isr(LIMIT_X2); +#endif +#endif +#if LIMIT_Y2 >= 0 + mcu_config_input(LIMIT_Y2); +#ifdef LIMIT_Y2_PULLUP + mcu_config_pullup(LIMIT_Y2); +#endif +#ifdef LIMIT_Y2_ISR + mcu_config_input_isr(LIMIT_Y2); +#endif +#endif +#if LIMIT_Z2 >= 0 + mcu_config_input(LIMIT_Z2); +#ifdef LIMIT_Z2_PULLUP + mcu_config_pullup(LIMIT_Z2); +#endif +#ifdef LIMIT_Z2_ISR + mcu_config_input_isr(LIMIT_Z2); +#endif +#endif +#if LIMIT_A >= 0 + mcu_config_input(LIMIT_A); +#ifdef LIMIT_A_PULLUP + mcu_config_pullup(LIMIT_A); +#endif +#ifdef LIMIT_A_ISR + mcu_config_input_isr(LIMIT_A); +#endif +#endif +#if LIMIT_B >= 0 + mcu_config_input(LIMIT_B); +#ifdef LIMIT_B_PULLUP + mcu_config_pullup(LIMIT_B); +#endif +#ifdef LIMIT_B_ISR + mcu_config_input_isr(LIMIT_B); +#endif +#endif +#if LIMIT_C >= 0 + mcu_config_input(LIMIT_C); +#ifdef LIMIT_C_PULLUP + mcu_config_pullup(LIMIT_C); +#endif +#ifdef LIMIT_C_ISR + mcu_config_input_isr(LIMIT_C); +#endif +#endif +#if PROBE >= 0 + mcu_config_input(PROBE); +#ifdef PROBE_PULLUP + mcu_config_pullup(PROBE); +#endif +#ifdef PROBE_ISR + mcu_config_input_isr(PROBE); +#endif +#endif +#if ESTOP >= 0 + mcu_config_input(ESTOP); +#ifdef ESTOP_PULLUP + mcu_config_pullup(ESTOP); +#endif +#ifdef ESTOP_ISR + mcu_config_input_isr(ESTOP); +#endif +#endif +#if SAFETY_DOOR >= 0 + mcu_config_input(SAFETY_DOOR); +#ifdef SAFETY_DOOR_PULLUP + mcu_config_pullup(SAFETY_DOOR); +#endif +#ifdef SAFETY_DOOR_ISR + mcu_config_input_isr(SAFETY_DOOR); +#endif +#endif +#if FHOLD >= 0 + mcu_config_input(FHOLD); +#ifdef FHOLD_PULLUP + mcu_config_pullup(FHOLD); +#endif +#ifdef FHOLD_ISR + mcu_config_input_isr(FHOLD); +#endif +#endif +#if CS_RES >= 0 + mcu_config_input(CS_RES); +#ifdef CS_RES_PULLUP + mcu_config_pullup(CS_RES); +#endif +#ifdef CS_RES_ISR + mcu_config_input_isr(CS_RES); +#endif +#endif +#if ANALOG0 >= 0 + mcu_config_input(ANALOG0); +#endif +#if ANALOG1 >= 0 + mcu_config_input(ANALOG1); +#endif +#if ANALOG2 >= 0 + mcu_config_input(ANALOG2); +#endif +#if ANALOG3 >= 0 + mcu_config_input(ANALOG3); +#endif +#if ANALOG4 >= 0 + mcu_config_input(ANALOG4); +#endif +#if ANALOG5 >= 0 + mcu_config_input(ANALOG5); +#endif +#if ANALOG6 >= 0 + mcu_config_input(ANALOG6); +#endif +#if ANALOG7 >= 0 + mcu_config_input(ANALOG7); +#endif +#if ANALOG8 >= 0 + mcu_config_input(ANALOG8); +#endif +#if ANALOG9 >= 0 + mcu_config_input(ANALOG9); +#endif +#if ANALOG10 >= 0 + mcu_config_input(ANALOG10); +#endif +#if ANALOG11 >= 0 + mcu_config_input(ANALOG11); +#endif +#if ANALOG12 >= 0 + mcu_config_input(ANALOG12); +#endif +#if ANALOG13 >= 0 + mcu_config_input(ANALOG13); +#endif +#if ANALOG14 >= 0 + mcu_config_input(ANALOG14); +#endif +#if ANALOG15 >= 0 + mcu_config_input(ANALOG15); +#endif +#if DIN0 >= 0 + mcu_config_input(DIN0); +#ifdef DIN0_PULLUP + mcu_config_pullup(DIN0); +#endif +#ifdef DIN0_ISR + mcu_config_input_isr(DIN0); +#endif +#endif +#if DIN1 >= 0 + mcu_config_input(DIN1); +#ifdef DIN1_PULLUP + mcu_config_pullup(DIN1); +#endif +#ifdef DIN1_ISR + mcu_config_input_isr(DIN1); +#endif +#endif +#if DIN2 >= 0 + mcu_config_input(DIN2); +#ifdef DIN2_PULLUP + mcu_config_pullup(DIN2); +#endif +#ifdef DIN2_ISR + mcu_config_input_isr(DIN2); +#endif +#endif +#if DIN3 >= 0 + mcu_config_input(DIN3); +#ifdef DIN3_PULLUP + mcu_config_pullup(DIN3); +#endif +#ifdef DIN3_ISR + mcu_config_input_isr(DIN3); +#endif +#endif +#if DIN4 >= 0 + mcu_config_input(DIN4); +#ifdef DIN4_PULLUP + mcu_config_pullup(DIN4); +#endif +#ifdef DIN4_ISR + mcu_config_input_isr(DIN4); +#endif +#endif +#if DIN5 >= 0 + mcu_config_input(DIN5); +#ifdef DIN5_PULLUP + mcu_config_pullup(DIN5); +#endif +#ifdef DIN5_ISR + mcu_config_input_isr(DIN5); +#endif +#endif +#if DIN6 >= 0 + mcu_config_input(DIN6); +#ifdef DIN6_PULLUP + mcu_config_pullup(DIN6); +#endif +#ifdef DIN6_ISR + mcu_config_input_isr(DIN6); +#endif +#endif +#if DIN7 >= 0 + mcu_config_input(DIN7); +#ifdef DIN7_PULLUP + mcu_config_pullup(DIN7); +#endif +#ifdef DIN7_ISR + mcu_config_input_isr(DIN7); +#endif +#endif +#if DIN8 >= 0 + mcu_config_input(DIN8); +#ifdef DIN8_PULLUP + mcu_config_pullup(DIN8); +#endif +#endif +#if DIN9 >= 0 + mcu_config_input(DIN9); +#ifdef DIN9_PULLUP + mcu_config_pullup(DIN9); +#endif +#endif +#if DIN10 >= 0 + mcu_config_input(DIN10); +#ifdef DIN10_PULLUP + mcu_config_pullup(DIN10); +#endif +#endif +#if DIN11 >= 0 + mcu_config_input(DIN11); +#ifdef DIN11_PULLUP + mcu_config_pullup(DIN11); +#endif +#endif +#if DIN12 >= 0 + mcu_config_input(DIN12); +#ifdef DIN12_PULLUP + mcu_config_pullup(DIN12); +#endif +#endif +#if DIN13 >= 0 + mcu_config_input(DIN13); +#ifdef DIN13_PULLUP + mcu_config_pullup(DIN13); +#endif +#endif +#if DIN14 >= 0 + mcu_config_input(DIN14); +#ifdef DIN14_PULLUP + mcu_config_pullup(DIN14); +#endif +#endif +#if DIN15 >= 0 + mcu_config_input(DIN15); +#ifdef DIN15_PULLUP + mcu_config_pullup(DIN15); +#endif +#endif +#if DIN16 >= 0 + mcu_config_input(DIN16); +#ifdef DIN16_PULLUP + mcu_config_pullup(DIN16); +#endif +#endif +#if DIN17 >= 0 + mcu_config_input(DIN17); +#ifdef DIN17_PULLUP + mcu_config_pullup(DIN17); +#endif +#endif +#if DIN18 >= 0 + mcu_config_input(DIN18); +#ifdef DIN18_PULLUP + mcu_config_pullup(DIN18); +#endif +#endif +#if DIN19 >= 0 + mcu_config_input(DIN19); +#ifdef DIN19_PULLUP + mcu_config_pullup(DIN19); +#endif +#endif +#if DIN20 >= 0 + mcu_config_input(DIN20); +#ifdef DIN20_PULLUP + mcu_config_pullup(DIN20); +#endif +#endif +#if DIN21 >= 0 + mcu_config_input(DIN21); +#ifdef DIN21_PULLUP + mcu_config_pullup(DIN21); +#endif +#endif +#if DIN22 >= 0 + mcu_config_input(DIN22); +#ifdef DIN22_PULLUP + mcu_config_pullup(DIN22); +#endif +#endif +#if DIN23 >= 0 + mcu_config_input(DIN23); +#ifdef DIN23_PULLUP + mcu_config_pullup(DIN23); +#endif +#endif +#if DIN24 >= 0 + mcu_config_input(DIN24); +#ifdef DIN24_PULLUP + mcu_config_pullup(DIN24); +#endif +#endif +#if DIN25 >= 0 + mcu_config_input(DIN25); +#ifdef DIN25_PULLUP + mcu_config_pullup(DIN25); +#endif +#endif +#if DIN26 >= 0 + mcu_config_input(DIN26); +#ifdef DIN26_PULLUP + mcu_config_pullup(DIN26); +#endif +#endif +#if DIN27 >= 0 + mcu_config_input(DIN27); +#ifdef DIN27_PULLUP + mcu_config_pullup(DIN27); +#endif +#endif +#if DIN28 >= 0 + mcu_config_input(DIN28); +#ifdef DIN28_PULLUP + mcu_config_pullup(DIN28); +#endif +#endif +#if DIN29 >= 0 + mcu_config_input(DIN29); +#ifdef DIN29_PULLUP + mcu_config_pullup(DIN29); +#endif +#endif +#if DIN30 >= 0 + mcu_config_input(DIN30); +#ifdef DIN30_PULLUP + mcu_config_pullup(DIN30); +#endif +#endif +#if DIN31 >= 0 + mcu_config_input(DIN31); +#ifdef DIN31_PULLUP + mcu_config_pullup(DIN31); +#endif +#endif +#if TX >= 0 + mcu_config_output(TX); +#endif +#if RX >= 0 + mcu_config_input(RX); +#ifdef RX_PULLUP + mcu_config_pullup(RX); +#endif +#endif +#if USB_DM >= 0 + mcu_config_input(USB_DM); +#ifdef USB_DM_PULLUP + mcu_config_pullup(USB_DM); +#endif +#endif +#if USB_DP >= 0 + mcu_config_input(USB_DP); +#ifdef USB_DP_PULLUP + mcu_config_pullup(USB_DP); +#endif +#endif +#if SPI_CLK >= 0 + mcu_config_output(SPI_CLK); +#endif +#if SPI_SDI >= 0 + mcu_config_input(SPI_SDI); +#ifdef SPI_SDI_PULLUP + mcu_config_pullup(SPI_SDI); +#endif +#endif +#if SPI_SDO >= 0 + mcu_config_output(SPI_SDO); +#endif + + mcu_usart_init(); + + // init rtc + os_timer_setfn(&esp8266_rtc_timer, (os_timer_func_t *)&mcu_rtc_isr, NULL); + os_timer_arm(&esp8266_rtc_timer, 1, true); + + // init timer1 + timer1_isr_init(); + timer1_attachInterrupt(mcu_itp_isr); + timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP); + timer1_write(625); + +#ifndef RAM_ONLY_SETTINGS + esp8266_eeprom_init(1024); // 1K Emulated EEPROM +#endif +} + +/** + * enables the pin probe mcu isr on change + * can be defined either as a function or a macro call + * */ +#ifndef mcu_enable_probe_isr +void mcu_enable_probe_isr(void) +{ +} +#endif + +/** + * disables the pin probe mcu isr on change + * can be defined either as a function or a macro call + * */ +#ifndef mcu_disable_probe_isr +void mcu_disable_probe_isr(void) +{ +} +#endif + +/** + * gets the voltage value of a built-in ADC pin + * can be defined either as a function or a macro call + * */ +#ifndef mcu_get_analog +uint8_t mcu_get_analog(uint8_t channel) +{ + return 0; +} +#endif + +/** + * sets the pwm value of a built-in pwm pin + * can be defined either as a function or a macro call + * */ +#ifndef mcu_set_pwm +void mcu_set_pwm(uint8_t pwm, uint8_t value) +{ +} +#endif + +/** + * gets the configured pwm value of a built-in pwm pin + * can be defined either as a function or a macro call + * */ +#ifndef mcu_get_pwm +uint8_t mcu_get_pwm(uint8_t pwm) +{ + return 0; +} +#endif + +/** + * checks if the serial hardware of the MCU is ready do send the next char + * */ +#ifndef mcu_tx_ready +bool mcu_tx_ready(void) +{ + return esp8266_uart_tx_ready(); +} +#endif + +/** + * checks if the serial hardware of the MCU has a new char ready to be read + * */ +#ifndef mcu_rx_ready +bool mcu_rx_ready(void) +{ + return esp8266_uart_rx_ready(); +} +#endif + +/** + * sends a char either via uart (hardware, software or USB virtual COM port) + * can be defined either as a function or a macro call + * */ +#ifndef mcu_putc + +void mcu_putc(char c) +{ +#if !(LED < 0) + mcu_toggle_output(LED); +#endif +#ifdef ENABLE_SYNC_TX + while (!mcu_tx_ready()) + ; +#endif + + esp8266_uart_write(c); +} +#endif + +/** + * gets a char either via uart (hardware, software or USB virtual COM port) + * can be defined either as a function or a macro call + * */ +#ifndef mcu_getc +char mcu_getc(void) +{ +#if !(LED < 0) + mcu_toggle_output(LED); +#endif +#ifdef ENABLE_SYNC_RX + while (!mcu_rx_ready()) + ; +#endif + + return esp8266_uart_read(); +} +#endif + +// ISR +/** + * enables global interrupts on the MCU + * can be defined either as a function or a macro call + * */ +#ifndef mcu_enable_global_isr +void mcu_enable_global_isr(void) +{ + // ets_intr_unlock(); + esp8266_global_isr_enabled = true; +} +#endif + +/** + * disables global interrupts on the MCU + * can be defined either as a function or a macro call + * */ +#ifndef mcu_disable_global_isr +void mcu_disable_global_isr(void) +{ + esp8266_global_isr_enabled = false; + // ets_intr_lock(); +} +#endif + +/** + * gets global interrupts state on the MCU + * can be defined either as a function or a macro call + * */ +#ifndef mcu_get_global_isr +bool mcu_get_global_isr(void) +{ + return esp8266_global_isr_enabled; +} +#endif + +// Step interpolator +/** + * convert step rate to clock cycles + * */ +void mcu_freq_to_clocks(float frequency, uint16_t *ticks, uint16_t *prescaller) +{ + // up and down counter (generates half the step rate at each event) + uint32_t totalticks = (uint32_t)((float)(128000UL >> 1) / frequency); + *prescaller = 0; + while (totalticks > 0xFFFF) + { + (*prescaller) += 1; + totalticks >>= 1; + } + + *ticks = (uint16_t)totalticks; +} + +/** + * starts the timer interrupt that generates the step pulses for the interpolator + * */ +void mcu_start_itp_isr(uint16_t ticks, uint16_t prescaller) +{ + mcu_step_reload = (((uint32_t)ticks) << prescaller); + mcu_step_counter = mcu_step_reload; +} + +/** + * changes the step rate of the timer interrupt that generates the step pulses for the interpolator + * */ +void mcu_change_itp_isr(uint16_t ticks, uint16_t prescaller) +{ + mcu_step_reload = (((uint32_t)ticks) << prescaller); + mcu_step_counter = mcu_step_reload; +} + +/** + * stops the timer interrupt that generates the step pulses for the interpolator + * */ +void mcu_stop_itp_isr(void) +{ + mcu_step_reload = 0; +} + +/** + * gets the MCU running time in milliseconds. + * the time counting is controled by the internal RTC + * */ +uint32_t mcu_millis() +{ + return mcu_runtime_ms; +} + +#ifndef mcu_delay_us +void mcu_delay_us(uint8_t delay) +{ + uint32_t time = system_get_time() + delay; + while (time > system_get_time()) + ; +} +#endif + +/** + * runs all internal tasks of the MCU. + * for the moment these are: + * - if USB is enabled and MCU uses tinyUSB framework run tinyUSB tud_task + * - if ENABLE_SYNC_RX is enabled check if there are any chars in the rx transmitter (or the tinyUSB buffer) and read them to the serial_rx_isr + * - if ENABLE_SYNC_TX is enabled check if serial_tx_empty is false and run serial_tx_isr + * */ +void mcu_dotasks(void) +{ + // reset WDT + system_soft_wdt_feed(); + esp8266_uart_process(); +} + +// Non volatile memory +/** + * gets a byte at the given EEPROM (or other non volatile memory) address of the MCU. + * */ +uint8_t mcu_eeprom_getc(uint16_t address) +{ +#ifndef RAM_ONLY_SETTINGS + return esp8266_eeprom_read(address); +#else + return 0; +#endif +} + +/** + * sets a byte at the given EEPROM (or other non volatile memory) address of the MCU. + * */ +void mcu_eeprom_putc(uint16_t address, uint8_t value) +{ +#ifndef RAM_ONLY_SETTINGS + esp8266_eeprom_write(address, value); +#endif +} + +/** + * flushes all recorded registers into the eeprom. + * */ +void mcu_eeprom_flush(void) +{ +#ifndef RAM_ONLY_SETTINGS + esp8266_eeprom_flush(); +#endif +} + +#endif diff --git a/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h b/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h new file mode 100644 index 000000000..ee7d4685a --- /dev/null +++ b/uCNC/src/hal/mcus/esp8266/mcumap_esp8266.h @@ -0,0 +1,1139 @@ +/* + Name: mcumap_esp8266.h + Description: Contains all MCU and PIN definitions for Arduino ESP8266 to run µCNC. + + Copyright: Copyright (c) João Martins + Author: João Martins + Date: 05-02-2022 + + µCNC 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. Please see + + µCNC is distributed WITHOUT ANY WARRANTY; + Also without the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. +*/ + +#ifndef MCUMAP_ESP8266_H +#define MCUMAP_ESP8266_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include + +/* + Generates all the interface definitions. + This creates a middle HAL layer between the board IO pins and the AVR funtionalities +*/ +/* + MCU specific definitions and replacements +*/ + +/* + ESP8266 Defaults +*/ +// defines the frequency of the mcu +#ifndef F_CPU +#define F_CPU 80000000L +#endif +// defines the maximum and minimum step rates +#ifndef F_STEP_MAX +#define F_STEP_MAX 64000 +#endif +#ifndef F_STEP_MIN +#define F_STEP_MIN 1 +#endif + +// defines special mcu to access flash strings and arrays +#define __rom__ +#define __romstr__ +#define rom_strptr * +#define rom_strcpy strcpy +#define rom_strncpy strncpy +#define rom_memcpy memcpy +#define rom_read_byte * + +#ifndef MCU_CALLBACK +#define MCU_CALLBACK IRAM_ATTR +#endif + +#ifdef ENABLE_RX_SYNC +#define MCU_RX_CALLBACK ICACHE_FLASH_ATTR +#endif + +#ifdef ENABLE_TX_SYNC +#define MCU_TX_CALLBACK ICACHE_FLASH_ATTR +#endif + +#define MCU_IO_CALLBACK ICACHE_FLASH_ATTR + +#ifdef RX_BUFFER_CAPACITY +#define RX_BUFFER_CAPACITY 255 +#endif + + +#define __SIZEOF_FLOAT__ 4 + +// used by the parser +// this method is faster then normal multiplication (for 32 bit for 16 and 8 bits is slightly lower) +// overrides utils.h definition to implement this method with or without fast math option enabled +#define fast_int_mul10(x) ((((x) << 2) + (x)) << 1) + +// PINNAMES for ESP8266 +#define PERIPHS_IO_MUX_GPIO0 (PERIPHS_IO_MUX + 0x34) +#define PERIPHS_IO_MUX_GPIO1 (PERIPHS_IO_MUX + 0x18) +#define PERIPHS_IO_MUX_GPIO2 (PERIPHS_IO_MUX + 0x38) +#define PERIPHS_IO_MUX_GPIO3 (PERIPHS_IO_MUX + 0x14) +#define PERIPHS_IO_MUX_GPIO4 (PERIPHS_IO_MUX + 0x3C) +#define PERIPHS_IO_MUX_GPIO5 (PERIPHS_IO_MUX + 0x40) +#define PERIPHS_IO_MUX_GPIO6 (PERIPHS_IO_MUX + 0x1c) +#define PERIPHS_IO_MUX_GPIO7 (PERIPHS_IO_MUX + 0x20) +#define PERIPHS_IO_MUX_GPIO8 (PERIPHS_IO_MUX + 0x24) +#define PERIPHS_IO_MUX_GPIO9 (PERIPHS_IO_MUX + 0x28) +#define PERIPHS_IO_MUX_GPIO10 (PERIPHS_IO_MUX + 0x2c) +#define PERIPHS_IO_MUX_GPIO11 (PERIPHS_IO_MUX + 0x30) +#define PERIPHS_IO_MUX_GPIO12 (PERIPHS_IO_MUX + 0x04) +#define PERIPHS_IO_MUX_GPIO13 (PERIPHS_IO_MUX + 0x08) +#define PERIPHS_IO_MUX_GPIO14 (PERIPHS_IO_MUX + 0x0C) +#define PERIPHS_IO_MUX_GPIO15 (PERIPHS_IO_MUX + 0x10) + +#define __gpioreg__(X) (__helper__(PERIPHS_IO_MUX_GPIO, X, )) + +// IO pins +#if (defined(STEP0_PORT) && defined(STEP0_BIT)) +#define STEP0 0 +#define DIO0 0 +#define DIO0_PORT STEP0_PORT +#define DIO0_BIT STEP0_BIT +#endif +#if (defined(STEP1_PORT) && defined(STEP1_BIT)) +#define STEP1 1 +#define DIO1 1 +#define DIO1_PORT STEP1_PORT +#define DIO1_BIT STEP1_BIT +#endif +#if (defined(STEP2_PORT) && defined(STEP2_BIT)) +#define STEP2 2 +#define DIO2 2 +#define DIO2_PORT STEP2_PORT +#define DIO2_BIT STEP2_BIT +#endif +#if (defined(STEP3_PORT) && defined(STEP3_BIT)) +#define STEP3 3 +#define DIO3 3 +#define DIO3_PORT STEP3_PORT +#define DIO3_BIT STEP3_BIT +#endif +#if (defined(STEP4_PORT) && defined(STEP4_BIT)) +#define STEP4 4 +#define DIO4 4 +#define DIO4_PORT STEP4_PORT +#define DIO4_BIT STEP4_BIT +#endif +#if (defined(STEP5_PORT) && defined(STEP5_BIT)) +#define STEP5 5 +#define DIO5 5 +#define DIO5_PORT STEP5_PORT +#define DIO5_BIT STEP5_BIT +#endif +#if (defined(STEP6_PORT) && defined(STEP6_BIT)) +#define STEP6 6 +#define DIO6 6 +#define DIO6_PORT STEP6_PORT +#define DIO6_BIT STEP6_BIT +#endif +#if (defined(STEP7_PORT) && defined(STEP7_BIT)) +#define STEP7 7 +#define DIO7 7 +#define DIO7_PORT STEP7_PORT +#define DIO7_BIT STEP7_BIT +#endif +#if (defined(DIR0_PORT) && defined(DIR0_BIT)) +#define DIR0 8 +#define DIO8 8 +#define DIO8_PORT DIR0_PORT +#define DIO8_BIT DIR0_BIT +#endif +#if (defined(DIR1_PORT) && defined(DIR1_BIT)) +#define DIR1 9 +#define DIO9 9 +#define DIO9_PORT DIR1_PORT +#define DIO9_BIT DIR1_BIT +#endif +#if (defined(DIR2_PORT) && defined(DIR2_BIT)) +#define DIR2 10 +#define DIO10 10 +#define DIO10_PORT DIR2_PORT +#define DIO10_BIT DIR2_BIT +#endif +#if (defined(DIR3_PORT) && defined(DIR3_BIT)) +#define DIR3 11 +#define DIO11 11 +#define DIO11_PORT DIR3_PORT +#define DIO11_BIT DIR3_BIT +#endif +#if (defined(DIR4_PORT) && defined(DIR4_BIT)) +#define DIR4 12 +#define DIO12 12 +#define DIO12_PORT DIR4_PORT +#define DIO12_BIT DIR4_BIT +#endif +#if (defined(DIR5_PORT) && defined(DIR5_BIT)) +#define DIR5 13 +#define DIO13 13 +#define DIO13_PORT DIR5_PORT +#define DIO13_BIT DIR5_BIT +#endif +#if (defined(DIR6_PORT) && defined(DIR6_BIT)) +#define DIR6 14 +#define DIO14 14 +#define DIO14_PORT DIR6_PORT +#define DIO14_BIT DIR6_BIT +#endif +#if (defined(DIR7_PORT) && defined(DIR7_BIT)) +#define DIR7 15 +#define DIO15 15 +#define DIO15_PORT DIR7_PORT +#define DIO15_BIT DIR7_BIT +#endif +#if (defined(STEP0_EN_PORT) && defined(STEP0_EN_BIT)) +#define STEP0_EN 16 +#define DIO16 16 +#define DIO16_PORT STEP0_EN_PORT +#define DIO16_BIT STEP0_EN_BIT +#endif +#if (defined(STEP1_EN_PORT) && defined(STEP1_EN_BIT)) +#define STEP1_EN 17 +#define DIO17 17 +#define DIO17_PORT STEP1_EN_PORT +#define DIO17_BIT STEP1_EN_BIT +#endif +#if (defined(STEP2_EN_PORT) && defined(STEP2_EN_BIT)) +#define STEP2_EN 18 +#define DIO18 18 +#define DIO18_PORT STEP2_EN_PORT +#define DIO18_BIT STEP2_EN_BIT +#endif +#if (defined(STEP3_EN_PORT) && defined(STEP3_EN_BIT)) +#define STEP3_EN 19 +#define DIO19 19 +#define DIO19_PORT STEP3_EN_PORT +#define DIO19_BIT STEP3_EN_BIT +#endif +#if (defined(STEP4_EN_PORT) && defined(STEP4_EN_BIT)) +#define STEP4_EN 20 +#define DIO20 20 +#define DIO20_PORT STEP4_EN_PORT +#define DIO20_BIT STEP4_EN_BIT +#endif +#if (defined(STEP5_EN_PORT) && defined(STEP5_EN_BIT)) +#define STEP5_EN 21 +#define DIO21 21 +#define DIO21_PORT STEP5_EN_PORT +#define DIO21_BIT STEP5_EN_BIT +#endif +#if (defined(STEP6_EN_PORT) && defined(STEP6_EN_BIT)) +#define STEP6_EN 22 +#define DIO22 22 +#define DIO22_PORT STEP6_EN_PORT +#define DIO22_BIT STEP6_EN_BIT +#endif +#if (defined(STEP7_EN_PORT) && defined(STEP7_EN_BIT)) +#define STEP7_EN 23 +#define DIO23 23 +#define DIO23_PORT STEP7_EN_PORT +#define DIO23_BIT STEP7_EN_BIT +#endif +#if (defined(PWM0_PORT) && defined(PWM0_BIT)) +#define PWM0 24 +#define DIO24 24 +#define DIO24_PORT PWM0_PORT +#define DIO24_BIT PWM0_BIT +#endif +#if (defined(PWM1_PORT) && defined(PWM1_BIT)) +#define PWM1 25 +#define DIO25 25 +#define DIO25_PORT PWM1_PORT +#define DIO25_BIT PWM1_BIT +#endif +#if (defined(PWM2_PORT) && defined(PWM2_BIT)) +#define PWM2 26 +#define DIO26 26 +#define DIO26_PORT PWM2_PORT +#define DIO26_BIT PWM2_BIT +#endif +#if (defined(PWM3_PORT) && defined(PWM3_BIT)) +#define PWM3 27 +#define DIO27 27 +#define DIO27_PORT PWM3_PORT +#define DIO27_BIT PWM3_BIT +#endif +#if (defined(PWM4_PORT) && defined(PWM4_BIT)) +#define PWM4 28 +#define DIO28 28 +#define DIO28_PORT PWM4_PORT +#define DIO28_BIT PWM4_BIT +#endif +#if (defined(PWM5_PORT) && defined(PWM5_BIT)) +#define PWM5 29 +#define DIO29 29 +#define DIO29_PORT PWM5_PORT +#define DIO29_BIT PWM5_BIT +#endif +#if (defined(PWM6_PORT) && defined(PWM6_BIT)) +#define PWM6 30 +#define DIO30 30 +#define DIO30_PORT PWM6_PORT +#define DIO30_BIT PWM6_BIT +#endif +#if (defined(PWM7_PORT) && defined(PWM7_BIT)) +#define PWM7 31 +#define DIO31 31 +#define DIO31_PORT PWM7_PORT +#define DIO31_BIT PWM7_BIT +#endif +#if (defined(PWM8_PORT) && defined(PWM8_BIT)) +#define PWM8 32 +#define DIO32 32 +#define DIO32_PORT PWM8_PORT +#define DIO32_BIT PWM8_BIT +#endif +#if (defined(PWM9_PORT) && defined(PWM9_BIT)) +#define PWM9 33 +#define DIO33 33 +#define DIO33_PORT PWM9_PORT +#define DIO33_BIT PWM9_BIT +#endif +#if (defined(PWM10_PORT) && defined(PWM10_BIT)) +#define PWM10 34 +#define DIO34 34 +#define DIO34_PORT PWM10_PORT +#define DIO34_BIT PWM10_BIT +#endif +#if (defined(PWM11_PORT) && defined(PWM11_BIT)) +#define PWM11 35 +#define DIO35 35 +#define DIO35_PORT PWM11_PORT +#define DIO35_BIT PWM11_BIT +#endif +#if (defined(PWM12_PORT) && defined(PWM12_BIT)) +#define PWM12 36 +#define DIO36 36 +#define DIO36_PORT PWM12_PORT +#define DIO36_BIT PWM12_BIT +#endif +#if (defined(PWM13_PORT) && defined(PWM13_BIT)) +#define PWM13 37 +#define DIO37 37 +#define DIO37_PORT PWM13_PORT +#define DIO37_BIT PWM13_BIT +#endif +#if (defined(PWM14_PORT) && defined(PWM14_BIT)) +#define PWM14 38 +#define DIO38 38 +#define DIO38_PORT PWM14_PORT +#define DIO38_BIT PWM14_BIT +#endif +#if (defined(PWM15_PORT) && defined(PWM15_BIT)) +#define PWM15 39 +#define DIO39 39 +#define DIO39_PORT PWM15_PORT +#define DIO39_BIT PWM15_BIT +#endif +#if (defined(SERVO0_PORT) && defined(SERVO0_BIT)) +#define SERVO0 40 +#define DIO40 40 +#define DIO40_PORT SERVO0_PORT +#define DIO40_BIT SERVO0_BIT +#endif +#if (defined(SERVO1_PORT) && defined(SERVO1_BIT)) +#define SERVO1 41 +#define DIO41 41 +#define DIO41_PORT SERVO1_PORT +#define DIO41_BIT SERVO1_BIT +#endif +#if (defined(SERVO2_PORT) && defined(SERVO2_BIT)) +#define SERVO2 42 +#define DIO42 42 +#define DIO42_PORT SERVO2_PORT +#define DIO42_BIT SERVO2_BIT +#endif +#if (defined(SERVO3_PORT) && defined(SERVO3_BIT)) +#define SERVO3 43 +#define DIO43 43 +#define DIO43_PORT SERVO3_PORT +#define DIO43_BIT SERVO3_BIT +#endif +#if (defined(SERVO4_PORT) && defined(SERVO4_BIT)) +#define SERVO4 44 +#define DIO44 44 +#define DIO44_PORT SERVO4_PORT +#define DIO44_BIT SERVO4_BIT +#endif +#if (defined(SERVO5_PORT) && defined(SERVO5_BIT)) +#define SERVO5 45 +#define DIO45 45 +#define DIO45_PORT SERVO5_PORT +#define DIO45_BIT SERVO5_BIT +#endif +#if (defined(DOUT0_PORT) && defined(DOUT0_BIT)) +#define DOUT0 46 +#define DIO46 46 +#define DIO46_PORT DOUT0_PORT +#define DIO46_BIT DOUT0_BIT +#endif +#if (defined(DOUT1_PORT) && defined(DOUT1_BIT)) +#define DOUT1 47 +#define DIO47 47 +#define DIO47_PORT DOUT1_PORT +#define DIO47_BIT DOUT1_BIT +#endif +#if (defined(DOUT2_PORT) && defined(DOUT2_BIT)) +#define DOUT2 48 +#define DIO48 48 +#define DIO48_PORT DOUT2_PORT +#define DIO48_BIT DOUT2_BIT +#endif +#if (defined(DOUT3_PORT) && defined(DOUT3_BIT)) +#define DOUT3 49 +#define DIO49 49 +#define DIO49_PORT DOUT3_PORT +#define DIO49_BIT DOUT3_BIT +#endif +#if (defined(DOUT4_PORT) && defined(DOUT4_BIT)) +#define DOUT4 50 +#define DIO50 50 +#define DIO50_PORT DOUT4_PORT +#define DIO50_BIT DOUT4_BIT +#endif +#if (defined(DOUT5_PORT) && defined(DOUT5_BIT)) +#define DOUT5 51 +#define DIO51 51 +#define DIO51_PORT DOUT5_PORT +#define DIO51_BIT DOUT5_BIT +#endif +#if (defined(DOUT6_PORT) && defined(DOUT6_BIT)) +#define DOUT6 52 +#define DIO52 52 +#define DIO52_PORT DOUT6_PORT +#define DIO52_BIT DOUT6_BIT +#endif +#if (defined(DOUT7_PORT) && defined(DOUT7_BIT)) +#define DOUT7 53 +#define DIO53 53 +#define DIO53_PORT DOUT7_PORT +#define DIO53_BIT DOUT7_BIT +#endif +#if (defined(DOUT8_PORT) && defined(DOUT8_BIT)) +#define DOUT8 54 +#define DIO54 54 +#define DIO54_PORT DOUT8_PORT +#define DIO54_BIT DOUT8_BIT +#endif +#if (defined(DOUT9_PORT) && defined(DOUT9_BIT)) +#define DOUT9 55 +#define DIO55 55 +#define DIO55_PORT DOUT9_PORT +#define DIO55_BIT DOUT9_BIT +#endif +#if (defined(DOUT10_PORT) && defined(DOUT10_BIT)) +#define DOUT10 56 +#define DIO56 56 +#define DIO56_PORT DOUT10_PORT +#define DIO56_BIT DOUT10_BIT +#endif +#if (defined(DOUT11_PORT) && defined(DOUT11_BIT)) +#define DOUT11 57 +#define DIO57 57 +#define DIO57_PORT DOUT11_PORT +#define DIO57_BIT DOUT11_BIT +#endif +#if (defined(DOUT12_PORT) && defined(DOUT12_BIT)) +#define DOUT12 58 +#define DIO58 58 +#define DIO58_PORT DOUT12_PORT +#define DIO58_BIT DOUT12_BIT +#endif +#if (defined(DOUT13_PORT) && defined(DOUT13_BIT)) +#define DOUT13 59 +#define DIO59 59 +#define DIO59_PORT DOUT13_PORT +#define DIO59_BIT DOUT13_BIT +#endif +#if (defined(DOUT14_PORT) && defined(DOUT14_BIT)) +#define DOUT14 60 +#define DIO60 60 +#define DIO60_PORT DOUT14_PORT +#define DIO60_BIT DOUT14_BIT +#endif +#if (defined(DOUT15_PORT) && defined(DOUT15_BIT)) +#define DOUT15 61 +#define DIO61 61 +#define DIO61_PORT DOUT15_PORT +#define DIO61_BIT DOUT15_BIT +#endif +#if (defined(DOUT16_PORT) && defined(DOUT16_BIT)) +#define DOUT16 62 +#define DIO62 62 +#define DIO62_PORT DOUT16_PORT +#define DIO62_BIT DOUT16_BIT +#endif +#if (defined(DOUT17_PORT) && defined(DOUT17_BIT)) +#define DOUT17 63 +#define DIO63 63 +#define DIO63_PORT DOUT17_PORT +#define DIO63_BIT DOUT17_BIT +#endif +#if (defined(DOUT18_PORT) && defined(DOUT18_BIT)) +#define DOUT18 64 +#define DIO64 64 +#define DIO64_PORT DOUT18_PORT +#define DIO64_BIT DOUT18_BIT +#endif +#if (defined(DOUT19_PORT) && defined(DOUT19_BIT)) +#define DOUT19 65 +#define DIO65 65 +#define DIO65_PORT DOUT19_PORT +#define DIO65_BIT DOUT19_BIT +#endif +#if (defined(DOUT20_PORT) && defined(DOUT20_BIT)) +#define DOUT20 66 +#define DIO66 66 +#define DIO66_PORT DOUT20_PORT +#define DIO66_BIT DOUT20_BIT +#endif +#if (defined(DOUT21_PORT) && defined(DOUT21_BIT)) +#define DOUT21 67 +#define DIO67 67 +#define DIO67_PORT DOUT21_PORT +#define DIO67_BIT DOUT21_BIT +#endif +#if (defined(DOUT22_PORT) && defined(DOUT22_BIT)) +#define DOUT22 68 +#define DIO68 68 +#define DIO68_PORT DOUT22_PORT +#define DIO68_BIT DOUT22_BIT +#endif +#if (defined(DOUT23_PORT) && defined(DOUT23_BIT)) +#define DOUT23 69 +#define DIO69 69 +#define DIO69_PORT DOUT23_PORT +#define DIO69_BIT DOUT23_BIT +#endif +#if (defined(DOUT24_PORT) && defined(DOUT24_BIT)) +#define DOUT24 70 +#define DIO70 70 +#define DIO70_PORT DOUT24_PORT +#define DIO70_BIT DOUT24_BIT +#endif +#if (defined(DOUT25_PORT) && defined(DOUT25_BIT)) +#define DOUT25 71 +#define DIO71 71 +#define DIO71_PORT DOUT25_PORT +#define DIO71_BIT DOUT25_BIT +#endif +#if (defined(DOUT26_PORT) && defined(DOUT26_BIT)) +#define DOUT26 72 +#define DIO72 72 +#define DIO72_PORT DOUT26_PORT +#define DIO72_BIT DOUT26_BIT +#endif +#if (defined(DOUT27_PORT) && defined(DOUT27_BIT)) +#define DOUT27 73 +#define DIO73 73 +#define DIO73_PORT DOUT27_PORT +#define DIO73_BIT DOUT27_BIT +#endif +#if (defined(DOUT28_PORT) && defined(DOUT28_BIT)) +#define DOUT28 74 +#define DIO74 74 +#define DIO74_PORT DOUT28_PORT +#define DIO74_BIT DOUT28_BIT +#endif +#if (defined(DOUT29_PORT) && defined(DOUT29_BIT)) +#define DOUT29 75 +#define DIO75 75 +#define DIO75_PORT DOUT29_PORT +#define DIO75_BIT DOUT29_BIT +#endif +#if (defined(DOUT30_PORT) && defined(DOUT30_BIT)) +#define DOUT30 76 +#define DIO76 76 +#define DIO76_PORT DOUT30_PORT +#define DIO76_BIT DOUT30_BIT +#endif +#if (defined(DOUT31_PORT) && defined(DOUT31_BIT)) +#define DOUT31 77 +#define DIO77 77 +#define DIO77_PORT DOUT31_PORT +#define DIO77_BIT DOUT31_BIT +#endif +#if (defined(LIMIT_X_PORT) && defined(LIMIT_X_BIT)) +#define LIMIT_X 100 +#define DIO100 100 +#define DIO100_PORT LIMIT_X_PORT +#define DIO100_BIT LIMIT_X_BIT +#endif +#if (defined(LIMIT_Y_PORT) && defined(LIMIT_Y_BIT)) +#define LIMIT_Y 101 +#define DIO101 101 +#define DIO101_PORT LIMIT_Y_PORT +#define DIO101_BIT LIMIT_Y_BIT +#endif +#if (defined(LIMIT_Z_PORT) && defined(LIMIT_Z_BIT)) +#define LIMIT_Z 102 +#define DIO102 102 +#define DIO102_PORT LIMIT_Z_PORT +#define DIO102_BIT LIMIT_Z_BIT +#endif +#if (defined(LIMIT_X2_PORT) && defined(LIMIT_X2_BIT)) +#define LIMIT_X2 103 +#define DIO103 103 +#define DIO103_PORT LIMIT_X2_PORT +#define DIO103_BIT LIMIT_X2_BIT +#endif +#if (defined(LIMIT_Y2_PORT) && defined(LIMIT_Y2_BIT)) +#define LIMIT_Y2 104 +#define DIO104 104 +#define DIO104_PORT LIMIT_Y2_PORT +#define DIO104_BIT LIMIT_Y2_BIT +#endif +#if (defined(LIMIT_Z2_PORT) && defined(LIMIT_Z2_BIT)) +#define LIMIT_Z2 105 +#define DIO105 105 +#define DIO105_PORT LIMIT_Z2_PORT +#define DIO105_BIT LIMIT_Z2_BIT +#endif +#if (defined(LIMIT_A_PORT) && defined(LIMIT_A_BIT)) +#define LIMIT_A 106 +#define DIO106 106 +#define DIO106_PORT LIMIT_A_PORT +#define DIO106_BIT LIMIT_A_BIT +#endif +#if (defined(LIMIT_B_PORT) && defined(LIMIT_B_BIT)) +#define LIMIT_B 107 +#define DIO107 107 +#define DIO107_PORT LIMIT_B_PORT +#define DIO107_BIT LIMIT_B_BIT +#endif +#if (defined(LIMIT_C_PORT) && defined(LIMIT_C_BIT)) +#define LIMIT_C 108 +#define DIO108 108 +#define DIO108_PORT LIMIT_C_PORT +#define DIO108_BIT LIMIT_C_BIT +#endif +#if (defined(PROBE_PORT) && defined(PROBE_BIT)) +#define PROBE 109 +#define DIO109 109 +#define DIO109_PORT PROBE_PORT +#define DIO109_BIT PROBE_BIT +#endif +#if (defined(ESTOP_PORT) && defined(ESTOP_BIT)) +#define ESTOP 110 +#define DIO110 110 +#define DIO110_PORT ESTOP_PORT +#define DIO110_BIT ESTOP_BIT +#endif +#if (defined(SAFETY_DOOR_PORT) && defined(SAFETY_DOOR_BIT)) +#define SAFETY_DOOR 111 +#define DIO111 111 +#define DIO111_PORT SAFETY_DOOR_PORT +#define DIO111_BIT SAFETY_DOOR_BIT +#endif +#if (defined(FHOLD_PORT) && defined(FHOLD_BIT)) +#define FHOLD 112 +#define DIO112 112 +#define DIO112_PORT FHOLD_PORT +#define DIO112_BIT FHOLD_BIT +#endif +#if (defined(CS_RES_PORT) && defined(CS_RES_BIT)) +#define CS_RES 113 +#define DIO113 113 +#define DIO113_PORT CS_RES_PORT +#define DIO113_BIT CS_RES_BIT +#endif +#if (defined(ANALOG0_PORT) && defined(ANALOG0_BIT)) +#define ANALOG0 114 +#define DIO114 114 +#define DIO114_PORT ANALOG0_PORT +#define DIO114_BIT ANALOG0_BIT +#endif +#if (defined(ANALOG1_PORT) && defined(ANALOG1_BIT)) +#define ANALOG1 115 +#define DIO115 115 +#define DIO115_PORT ANALOG1_PORT +#define DIO115_BIT ANALOG1_BIT +#endif +#if (defined(ANALOG2_PORT) && defined(ANALOG2_BIT)) +#define ANALOG2 116 +#define DIO116 116 +#define DIO116_PORT ANALOG2_PORT +#define DIO116_BIT ANALOG2_BIT +#endif +#if (defined(ANALOG3_PORT) && defined(ANALOG3_BIT)) +#define ANALOG3 117 +#define DIO117 117 +#define DIO117_PORT ANALOG3_PORT +#define DIO117_BIT ANALOG3_BIT +#endif +#if (defined(ANALOG4_PORT) && defined(ANALOG4_BIT)) +#define ANALOG4 118 +#define DIO118 118 +#define DIO118_PORT ANALOG4_PORT +#define DIO118_BIT ANALOG4_BIT +#endif +#if (defined(ANALOG5_PORT) && defined(ANALOG5_BIT)) +#define ANALOG5 119 +#define DIO119 119 +#define DIO119_PORT ANALOG5_PORT +#define DIO119_BIT ANALOG5_BIT +#endif +#if (defined(ANALOG6_PORT) && defined(ANALOG6_BIT)) +#define ANALOG6 120 +#define DIO120 120 +#define DIO120_PORT ANALOG6_PORT +#define DIO120_BIT ANALOG6_BIT +#endif +#if (defined(ANALOG7_PORT) && defined(ANALOG7_BIT)) +#define ANALOG7 121 +#define DIO121 121 +#define DIO121_PORT ANALOG7_PORT +#define DIO121_BIT ANALOG7_BIT +#endif +#if (defined(ANALOG8_PORT) && defined(ANALOG8_BIT)) +#define ANALOG8 122 +#define DIO122 122 +#define DIO122_PORT ANALOG8_PORT +#define DIO122_BIT ANALOG8_BIT +#endif +#if (defined(ANALOG9_PORT) && defined(ANALOG9_BIT)) +#define ANALOG9 123 +#define DIO123 123 +#define DIO123_PORT ANALOG9_PORT +#define DIO123_BIT ANALOG9_BIT +#endif +#if (defined(ANALOG10_PORT) && defined(ANALOG10_BIT)) +#define ANALOG10 124 +#define DIO124 124 +#define DIO124_PORT ANALOG10_PORT +#define DIO124_BIT ANALOG10_BIT +#endif +#if (defined(ANALOG11_PORT) && defined(ANALOG11_BIT)) +#define ANALOG11 125 +#define DIO125 125 +#define DIO125_PORT ANALOG11_PORT +#define DIO125_BIT ANALOG11_BIT +#endif +#if (defined(ANALOG12_PORT) && defined(ANALOG12_BIT)) +#define ANALOG12 126 +#define DIO126 126 +#define DIO126_PORT ANALOG12_PORT +#define DIO126_BIT ANALOG12_BIT +#endif +#if (defined(ANALOG13_PORT) && defined(ANALOG13_BIT)) +#define ANALOG13 127 +#define DIO127 127 +#define DIO127_PORT ANALOG13_PORT +#define DIO127_BIT ANALOG13_BIT +#endif +#if (defined(ANALOG14_PORT) && defined(ANALOG14_BIT)) +#define ANALOG14 128 +#define DIO128 128 +#define DIO128_PORT ANALOG14_PORT +#define DIO128_BIT ANALOG14_BIT +#endif +#if (defined(ANALOG15_PORT) && defined(ANALOG15_BIT)) +#define ANALOG15 129 +#define DIO129 129 +#define DIO129_PORT ANALOG15_PORT +#define DIO129_BIT ANALOG15_BIT +#endif +#if (defined(DIN0_PORT) && defined(DIN0_BIT)) +#define DIN0 130 +#define DIO130 130 +#define DIO130_PORT DIN0_PORT +#define DIO130_BIT DIN0_BIT +#endif +#if (defined(DIN1_PORT) && defined(DIN1_BIT)) +#define DIN1 131 +#define DIO131 131 +#define DIO131_PORT DIN1_PORT +#define DIO131_BIT DIN1_BIT +#endif +#if (defined(DIN2_PORT) && defined(DIN2_BIT)) +#define DIN2 132 +#define DIO132 132 +#define DIO132_PORT DIN2_PORT +#define DIO132_BIT DIN2_BIT +#endif +#if (defined(DIN3_PORT) && defined(DIN3_BIT)) +#define DIN3 133 +#define DIO133 133 +#define DIO133_PORT DIN3_PORT +#define DIO133_BIT DIN3_BIT +#endif +#if (defined(DIN4_PORT) && defined(DIN4_BIT)) +#define DIN4 134 +#define DIO134 134 +#define DIO134_PORT DIN4_PORT +#define DIO134_BIT DIN4_BIT +#endif +#if (defined(DIN5_PORT) && defined(DIN5_BIT)) +#define DIN5 135 +#define DIO135 135 +#define DIO135_PORT DIN5_PORT +#define DIO135_BIT DIN5_BIT +#endif +#if (defined(DIN6_PORT) && defined(DIN6_BIT)) +#define DIN6 136 +#define DIO136 136 +#define DIO136_PORT DIN6_PORT +#define DIO136_BIT DIN6_BIT +#endif +#if (defined(DIN7_PORT) && defined(DIN7_BIT)) +#define DIN7 137 +#define DIO137 137 +#define DIO137_PORT DIN7_PORT +#define DIO137_BIT DIN7_BIT +#endif +#if (defined(DIN8_PORT) && defined(DIN8_BIT)) +#define DIN8 138 +#define DIO138 138 +#define DIO138_PORT DIN8_PORT +#define DIO138_BIT DIN8_BIT +#endif +#if (defined(DIN9_PORT) && defined(DIN9_BIT)) +#define DIN9 139 +#define DIO139 139 +#define DIO139_PORT DIN9_PORT +#define DIO139_BIT DIN9_BIT +#endif +#if (defined(DIN10_PORT) && defined(DIN10_BIT)) +#define DIN10 140 +#define DIO140 140 +#define DIO140_PORT DIN10_PORT +#define DIO140_BIT DIN10_BIT +#endif +#if (defined(DIN11_PORT) && defined(DIN11_BIT)) +#define DIN11 141 +#define DIO141 141 +#define DIO141_PORT DIN11_PORT +#define DIO141_BIT DIN11_BIT +#endif +#if (defined(DIN12_PORT) && defined(DIN12_BIT)) +#define DIN12 142 +#define DIO142 142 +#define DIO142_PORT DIN12_PORT +#define DIO142_BIT DIN12_BIT +#endif +#if (defined(DIN13_PORT) && defined(DIN13_BIT)) +#define DIN13 143 +#define DIO143 143 +#define DIO143_PORT DIN13_PORT +#define DIO143_BIT DIN13_BIT +#endif +#if (defined(DIN14_PORT) && defined(DIN14_BIT)) +#define DIN14 144 +#define DIO144 144 +#define DIO144_PORT DIN14_PORT +#define DIO144_BIT DIN14_BIT +#endif +#if (defined(DIN15_PORT) && defined(DIN15_BIT)) +#define DIN15 145 +#define DIO145 145 +#define DIO145_PORT DIN15_PORT +#define DIO145_BIT DIN15_BIT +#endif +#if (defined(DIN16_PORT) && defined(DIN16_BIT)) +#define DIN16 146 +#define DIO146 146 +#define DIO146_PORT DIN16_PORT +#define DIO146_BIT DIN16_BIT +#endif +#if (defined(DIN17_PORT) && defined(DIN17_BIT)) +#define DIN17 147 +#define DIO147 147 +#define DIO147_PORT DIN17_PORT +#define DIO147_BIT DIN17_BIT +#endif +#if (defined(DIN18_PORT) && defined(DIN18_BIT)) +#define DIN18 148 +#define DIO148 148 +#define DIO148_PORT DIN18_PORT +#define DIO148_BIT DIN18_BIT +#endif +#if (defined(DIN19_PORT) && defined(DIN19_BIT)) +#define DIN19 149 +#define DIO149 149 +#define DIO149_PORT DIN19_PORT +#define DIO149_BIT DIN19_BIT +#endif +#if (defined(DIN20_PORT) && defined(DIN20_BIT)) +#define DIN20 150 +#define DIO150 150 +#define DIO150_PORT DIN20_PORT +#define DIO150_BIT DIN20_BIT +#endif +#if (defined(DIN21_PORT) && defined(DIN21_BIT)) +#define DIN21 151 +#define DIO151 151 +#define DIO151_PORT DIN21_PORT +#define DIO151_BIT DIN21_BIT +#endif +#if (defined(DIN22_PORT) && defined(DIN22_BIT)) +#define DIN22 152 +#define DIO152 152 +#define DIO152_PORT DIN22_PORT +#define DIO152_BIT DIN22_BIT +#endif +#if (defined(DIN23_PORT) && defined(DIN23_BIT)) +#define DIN23 153 +#define DIO153 153 +#define DIO153_PORT DIN23_PORT +#define DIO153_BIT DIN23_BIT +#endif +#if (defined(DIN24_PORT) && defined(DIN24_BIT)) +#define DIN24 154 +#define DIO154 154 +#define DIO154_PORT DIN24_PORT +#define DIO154_BIT DIN24_BIT +#endif +#if (defined(DIN25_PORT) && defined(DIN25_BIT)) +#define DIN25 155 +#define DIO155 155 +#define DIO155_PORT DIN25_PORT +#define DIO155_BIT DIN25_BIT +#endif +#if (defined(DIN26_PORT) && defined(DIN26_BIT)) +#define DIN26 156 +#define DIO156 156 +#define DIO156_PORT DIN26_PORT +#define DIO156_BIT DIN26_BIT +#endif +#if (defined(DIN27_PORT) && defined(DIN27_BIT)) +#define DIN27 157 +#define DIO157 157 +#define DIO157_PORT DIN27_PORT +#define DIO157_BIT DIN27_BIT +#endif +#if (defined(DIN28_PORT) && defined(DIN28_BIT)) +#define DIN28 158 +#define DIO158 158 +#define DIO158_PORT DIN28_PORT +#define DIO158_BIT DIN28_BIT +#endif +#if (defined(DIN29_PORT) && defined(DIN29_BIT)) +#define DIN29 159 +#define DIO159 159 +#define DIO159_PORT DIN29_PORT +#define DIO159_BIT DIN29_BIT +#endif +#if (defined(DIN30_PORT) && defined(DIN30_BIT)) +#define DIN30 160 +#define DIO160 160 +#define DIO160_PORT DIN30_PORT +#define DIO160_BIT DIN30_BIT +#endif +#if (defined(DIN31_PORT) && defined(DIN31_BIT)) +#define DIN31 161 +#define DIO161 161 +#define DIO161_PORT DIN31_PORT +#define DIO161_BIT DIN31_BIT +#endif +#if (defined(TX_PORT) && defined(TX_BIT)) +#define TX 200 +#define DIO200 200 +#define DIO200_PORT TX_PORT +#define DIO200_BIT TX_BIT +#endif +#if (defined(RX_PORT) && defined(RX_BIT)) +#define RX 201 +#define DIO201 201 +#define DIO201_PORT RX_PORT +#define DIO201_BIT RX_BIT +#endif +#if (defined(USB_DM_PORT) && defined(USB_DM_BIT)) +#define USB_DM 202 +#define DIO202 202 +#define DIO202_PORT USB_DM_PORT +#define DIO202_BIT USB_DM_BIT +#endif +#if (defined(USB_DP_PORT) && defined(USB_DP_BIT)) +#define USB_DP 203 +#define DIO203 203 +#define DIO203_PORT USB_DP_PORT +#define DIO203_BIT USB_DP_BIT +#endif +#if (defined(SPI_CLK_PORT) && defined(SPI_CLK_BIT)) +#define SPI_CLK 204 +#define DIO204 204 +#define DIO204_PORT SPI_CLK_PORT +#define DIO204_BIT SPI_CLK_BIT +#endif +#if (defined(SPI_SDI_PORT) && defined(SPI_SDI_BIT)) +#define SPI_SDI 205 +#define DIO205 205 +#define DIO205_PORT SPI_SDI_PORT +#define DIO205_BIT SPI_SDI_BIT +#endif +#if (defined(SPI_SDO_PORT) && defined(SPI_SDO_BIT)) +#define SPI_SDO 206 +#define DIO206 206 +#define DIO206_PORT SPI_SDO_PORT +#define DIO206_BIT SPI_SDO_BIT +#endif + +// ISR on change inputs +#if (defined(LIMIT_X_ISR) && defined(LIMIT_X)) +#define DIO52_ISR (LIMIT_X_ISR) +#define LIMIT_X_ISRCALLBACK mcu_limit_isr +#define DIO52_ISRCALLBACK mcu_limit_isr +#endif +#if (defined(LIMIT_Y_ISR) && defined(LIMIT_Y)) +#define DIO53_ISR (LIMIT_Y_ISR) +#define LIMIT_Y_ISRCALLBACK mcu_limit_isr +#define DIO53_ISRCALLBACK mcu_limit_isr +#endif +#if (defined(LIMIT_Z_ISR) && defined(LIMIT_Z)) +#define DIO54_ISR (LIMIT_Z_ISR) +#define LIMIT_Z_ISRCALLBACK mcu_limit_isr +#define DIO54_ISRCALLBACK mcu_limit_isr +#endif +#if (defined(LIMIT_X2_ISR) && defined(LIMIT_X2)) +#define DIO55_ISR (LIMIT_X2_ISR) +#define LIMIT_X2_ISRCALLBACK mcu_limit_isr +#define DIO55_ISRCALLBACK mcu_limit_isr +#endif +#if (defined(LIMIT_Y2_ISR) && defined(LIMIT_Y2)) +#define DIO56_ISR (LIMIT_Y2_ISR) +#define LIMIT_Y2_ISRCALLBACK mcu_limit_isr +#define DIO56_ISRCALLBACK mcu_limit_isr +#endif +#if (defined(LIMIT_Z2_ISR) && defined(LIMIT_Z2)) +#define DIO57_ISR (LIMIT_Z2_ISR) +#define LIMIT_Z2_ISRCALLBACK mcu_limit_isr +#define DIO57_ISRCALLBACK mcu_limit_isr +#endif +#if (defined(LIMIT_A_ISR) && defined(LIMIT_A)) +#define DIO58_ISR (LIMIT_A_ISR) +#define LIMIT_A_ISRCALLBACK mcu_limit_isr +#define DIO58_ISRCALLBACK mcu_limit_isr +#endif +#if (defined(LIMIT_B_ISR) && defined(LIMIT_B)) +#define DIO59_ISR (LIMIT_B_ISR) +#define LIMIT_B_ISRCALLBACK mcu_limit_isr +#define DIO59_ISRCALLBACK mcu_limit_isr +#endif +#if (defined(LIMIT_C_ISR) && defined(LIMIT_C)) +#define DIO60_ISR (LIMIT_C_ISR) +#define LIMIT_C_ISRCALLBACK mcu_limit_isr +#define DIO60_ISRCALLBACK mcu_limit_isr +#endif +#if (defined(PROBE_ISR) && defined(PROBE)) +#define DIO61_ISR (PROBE_ISR) +#define PROBE_ISRCALLBACK mcu_probe_isr +#define DIO61_ISRCALLBACK mcu_probe_isr +#endif +#if (defined(ESTOP_ISR) && defined(ESTOP)) +#define DIO62_ISR (ESTOP_ISR) +#define ESTOP_ISRCALLBACK mcu_control_isr +#define DIO62_ISRCALLBACK mcu_control_isr +#endif +#if (defined(SAFETY_DOOR_ISR) && defined(SAFETY_DOOR)) +#define DIO63_ISR (SAFETY_DOOR_ISR) +#define SAFETY_DOOR_ISRCALLBACK mcu_control_isr +#define DIO63_ISRCALLBACK mcu_control_isr +#endif +#if (defined(FHOLD_ISR) && defined(FHOLD)) +#define DIO64_ISR (FHOLD_ISR) +#define FHOLD_ISRCALLBACK mcu_control_isr +#define DIO64_ISRCALLBACK mcu_control_isr +#endif +#if (defined(CS_RES_ISR) && defined(CS_RES)) +#define DIO65_ISR (CS_RES_ISR) +#define CS_RES_ISRCALLBACK mcu_control_isr +#define DIO65_ISRCALLBACK mcu_control_isr +#endif +#if (defined(DIN0_ISR) && defined(DIN0)) +#define DIO82_ISR (DIN0_ISR) +#define DIN0_ISRCALLBACK mcu_din_isr +#define DIO82_ISRCALLBACK mcu_din_isr +#endif +#if (defined(DIN1_ISR) && defined(DIN1)) +#define DIO83_ISR (DIN1_ISR) +#define DIN1_ISRCALLBACK mcu_din_isr +#define DIO83_ISRCALLBACK mcu_din_isr +#endif +#if (defined(DIN2_ISR) && defined(DIN2)) +#define DIO84_ISR (DIN2_ISR) +#define DIN2_ISRCALLBACK mcu_din_isr +#define DIO84_ISRCALLBACK mcu_din_isr +#endif +#if (defined(DIN3_ISR) && defined(DIN3)) +#define DIO85_ISR (DIN3_ISR) +#define DIN3_ISRCALLBACK mcu_din_isr +#define DIO85_ISRCALLBACK mcu_din_isr +#endif +#if (defined(DIN4_ISR) && defined(DIN4)) +#define DIO86_ISR (DIN4_ISR) +#define DIN4_ISRCALLBACK mcu_din_isr +#define DIO86_ISRCALLBACK mcu_din_isr +#endif +#if (defined(DIN5_ISR) && defined(DIN5)) +#define DIO87_ISR (DIN5_ISR) +#define DIN5_ISRCALLBACK mcu_din_isr +#define DIO87_ISRCALLBACK mcu_din_isr +#endif +#if (defined(DIN6_ISR) && defined(DIN6)) +#define DIO88_ISR (DIN6_ISR) +#define DIN6_ISRCALLBACK mcu_din_isr +#define DIO88_ISRCALLBACK mcu_din_isr +#endif +#if (defined(DIN7_ISR) && defined(DIN7)) +#define DIO89_ISR (DIN7_ISR) +#define DIN7_ISRCALLBACK mcu_din_isr +#define DIO89_ISRCALLBACK __indirect__(X, ISRCALLBACK) +#endif + +#if (INTERFACE == INTERFACE_UART) +#ifndef COM_PORT +#define COM_PORT 0 +#endif +#endif + +#define ENABLE_SYNC_RX +#define ENABLE_SYNC_TX + +// Helper macros +#define __helper_ex__(left, mid, right) (left##mid##right) +#define __helper__(left, mid, right) (__helper_ex__(left, mid, right)) +#define __indirect__ex__(X, Y) DIO##X##_##Y +#define __indirect__(X, Y) __indirect__ex__(X, Y) + +#define mcu_config_output(X) pinMode(__indirect__(X, BIT), OUTPUT) +#define mcu_config_pwm(X) pinMode(__indirect__(X, BIT), OUTPUT) +#define mcu_config_input(X) pinMode(__indirect__(X, BIT), INPUT) +#define mcu_config_pullup(X) pinMode(__indirect__(X, BIT), INPUT_PULLUP) +#define mcu_config_input_isr(X) attachInterrupt(digitalPinToInterrupt(__indirect__(X, BIT)), __indirect__(X, ISRCALLBACK), CHANGE) + +#define mcu_get_input(X) digitalRead(__indirect__(X, BIT)) +#define mcu_get_output(X) digitalRead(__indirect__(X, BIT)) +#define mcu_set_output(X) digitalWrite(__indirect__(X, BIT), 1) +#define mcu_clear_output(X) digitalWrite(__indirect__(X, BIT), 0) +#define mcu_toggle_output(X) digitalWrite(__indirect__(X, BIT), !digitalRead(__indirect__(X, BIT))) + + extern uint8_t esp8266_pwm[16]; +#define mcu_set_pwm(X, Y) (esp8266_pwm[X - PWM_PINS_OFFSET] = (0x7F & (Y >> 1))) +#define mcu_get_pwm(X) (esp8266_pwm[X - PWM_PINS_OFFSET] << 1) +#define mcu_get_analog(X) (analogRead(__indirect__(X, BIT)) >> 2) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/uCNC/src/hal/mcus/mcu.h b/uCNC/src/hal/mcus/mcu.h index 3f8ac3b1a..679500082 100644 --- a/uCNC/src/hal/mcus/mcu.h +++ b/uCNC/src/hal/mcus/mcu.h @@ -29,7 +29,21 @@ extern "C" #include #include -#define MCU_CALLBACK extern +#ifndef MCU_CALLBACK +#define MCU_CALLBACK +#endif + +#ifndef MCU_TX_CALLBACK +#define MCU_TX_CALLBACK MCU_CALLBACK +#endif + +#ifndef MCU_RX_CALLBACK +#define MCU_RX_CALLBACK MCU_CALLBACK +#endif + +#ifndef MCU_IO_CALLBACK +#define MCU_IO_CALLBACK MCU_CALLBACK +#endif // the extern is not necessary // this explicit declaration just serves to reeinforce the idea that these callbacks are implemented on other µCNC core code translation units @@ -37,13 +51,13 @@ extern "C" MCU_CALLBACK void mcu_step_cb(void); MCU_CALLBACK void mcu_step_reset_cb(void); - MCU_CALLBACK void mcu_com_rx_cb(unsigned char c); - MCU_CALLBACK void mcu_com_tx_cb(); + MCU_RX_CALLBACK void mcu_com_rx_cb(unsigned char c); + MCU_TX_CALLBACK void mcu_com_tx_cb(); MCU_CALLBACK void mcu_rtc_cb(uint32_t millis); - MCU_CALLBACK void mcu_controls_changed_cb(void); - MCU_CALLBACK void mcu_limits_changed_cb(void); - MCU_CALLBACK void mcu_probe_changed_cb(void); - MCU_CALLBACK void mcu_inputs_changed_cb(void); + MCU_IO_CALLBACK void mcu_controls_changed_cb(void); + MCU_IO_CALLBACK void mcu_limits_changed_cb(void); + MCU_IO_CALLBACK void mcu_probe_changed_cb(void); + MCU_IO_CALLBACK void mcu_inputs_changed_cb(void); /*IO functions*/ diff --git a/uCNC/src/hal/mcus/mcudefs.h b/uCNC/src/hal/mcus/mcudefs.h index 77f38bce5..35985e002 100644 --- a/uCNC/src/hal/mcus/mcudefs.h +++ b/uCNC/src/hal/mcus/mcudefs.h @@ -57,6 +57,20 @@ extern "C" #endif #endif +#if (MCU == MCU_LPC176X) +#include "lpc176x/mcumap_lpc176x.h" +#ifndef CFG_TUSB_MCU +#define CFG_TUSB_MCU OPT_MCU_LPC175X_6X +#endif +#endif + +#if (MCU == MCU_ESP8266) +#include "esp8266/mcumap_esp8266.h" +#ifndef CFG_TUSB_MCU +#define CFG_TUSB_MCU OPT_MCU_NONE +#endif +#endif + #if (MCU == MCU_VIRTUAL_WIN) #include "virtual/mcumap_virtual.h" #endif diff --git a/uCNC/src/hal/mcus/mcus.h b/uCNC/src/hal/mcus/mcus.h index f140a6dd8..c09b68fa8 100644 --- a/uCNC/src/hal/mcus/mcus.h +++ b/uCNC/src/hal/mcus/mcus.h @@ -29,6 +29,8 @@ extern "C" #define MCU_STM32F1X 10 #define MCU_STM32F4X 11 #define MCU_SAMD21 20 +#define MCU_LPC176X 30 +#define MCU_ESP8266 40 #define MCU_VIRTUAL_WIN 99 #ifdef __cplusplus diff --git a/uCNC/src/hal/mcus/samd21/mcu_samd21.c b/uCNC/src/hal/mcus/samd21/mcu_samd21.c index 154482d49..9c9944f1b 100644 --- a/uCNC/src/hal/mcus/samd21/mcu_samd21.c +++ b/uCNC/src/hal/mcus/samd21/mcu_samd21.c @@ -207,7 +207,7 @@ void MCU_ITP_ISR(void) mcu_enable_global_isr(); } -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) void mcu_com_isr() { mcu_disable_global_isr(); @@ -232,7 +232,7 @@ void mcu_com_isr() void mcu_usart_init(void) { -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) PM->APBCMASK.reg |= PM_APBCMASK_COM; /* Setup GCLK SERCOMx to use GENCLK0 */ @@ -1322,7 +1322,7 @@ void mcu_putc(char c) tud_cdc_write_flush(); } #else -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #ifdef ENABLE_SYNC_TX while (!mcu_tx_ready()) ; @@ -1354,7 +1354,7 @@ char mcu_getc(void) return (unsigned char)tud_cdc_read_char(); #else -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #ifdef ENABLE_SYNC_RX while (!mcu_rx_ready()) ; diff --git a/uCNC/src/hal/mcus/samd21/mcumap_samd21.h b/uCNC/src/hal/mcus/samd21/mcumap_samd21.h index a56a548b5..79528f3d2 100644 --- a/uCNC/src/hal/mcus/samd21/mcumap_samd21.h +++ b/uCNC/src/hal/mcus/samd21/mcumap_samd21.h @@ -2914,7 +2914,7 @@ extern "C" } #define mcu_get_global_isr() samd21_global_isr_enabled -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #define mcu_rx_ready() (COM->USART.INTFLAG.bit.RXC) #define mcu_tx_ready() (COM->USART.INTFLAG.bit.DRE) #elif (INTERFACE == INTERFACE_USB) diff --git a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c index 6822dfd9d..ff497d5ef 100644 --- a/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c +++ b/uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c @@ -136,7 +136,7 @@ volatile bool stm32_global_isr_enabled; * The isr functions * The respective IRQHandler will execute these functions **/ -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) void MCU_SERIAL_ISR(void) { mcu_disable_global_isr(); @@ -416,7 +416,7 @@ void osSystickHandler(void) static void mcu_rtc_init(void); static void mcu_usart_init(void); -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #define APB2_PRESCALER RCC_CFGR_PPRE2_DIV2 #else #define APB2_PRESCALER RCC_CFGR_PPRE2_DIV1 @@ -465,7 +465,7 @@ void mcu_clocks_init() void mcu_usart_init(void) { -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) /*enables RCC clocks and GPIO*/ mcu_config_output_af(TX, GPIO_OUTALT_OD_50MHZ); mcu_config_input_af(RX); @@ -529,7 +529,7 @@ void mcu_putc(char c) #if !(LED < 0) mcu_toggle_output(LED); #endif -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #ifdef ENABLE_SYNC_TX while (!(COM_USART->SR & USART_SR_TC)) ; @@ -1274,7 +1274,7 @@ char mcu_getc(void) #if !(LED < 0) mcu_toggle_output(LED); #endif -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #ifdef ENABLE_SYNC_RX while (!(COM_USART->SR & USART_SR_RXNE)) ; diff --git a/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h b/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h index 2ab081176..0ee7423d1 100644 --- a/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h +++ b/uCNC/src/hal/mcus/stm32f1x/mcumap_stm32f1x.h @@ -4106,7 +4106,7 @@ extern "C" #endif // COM registers -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) // this MCU does not work well with both TX and RX interrupt // this forces the sync TX method to fix communication // #define ENABLE_SYNC_TX @@ -4294,7 +4294,7 @@ extern "C" // #define mcu_enable_tx_isr() // #define mcu_disable_tx_isr() // #endif -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #define mcu_rx_ready() (COM_USART->SR & USART_SR_RXNE) #define mcu_tx_ready() (COM_USART->SR & USART_SR_TXE) #elif (INTERFACE == INTERFACE_USB) diff --git a/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c b/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c index e6e2ce0c9..05cb88b2a 100644 --- a/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c +++ b/uCNC/src/hal/mcus/stm32f4x/mcu_stm32f4x.c @@ -135,7 +135,7 @@ volatile bool stm32_global_isr_enabled; * The isr functions * The respective IRQHandler will execute these functions **/ -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) void MCU_SERIAL_ISR(void) { mcu_disable_global_isr(); @@ -402,7 +402,7 @@ void osSystickHandler(void) static void mcu_rtc_init(void); static void mcu_usart_init(void); -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #define APB2_PRESCALER RCC_CFGR_PPRE2_DIV2 #else #define APB2_PRESCALER RCC_CFGR_PPRE2_DIV1 @@ -473,7 +473,7 @@ void mcu_clocks_init() void mcu_usart_init(void) { -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) /*enables RCC clocks and GPIO*/ RCC->COM_APB |= (COM_APBEN); mcu_config_af(TX, GPIO_AF_USART); @@ -550,7 +550,7 @@ void mcu_putc(char c) #if !(LED < 0) mcu_toggle_output(LED); #endif -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #ifdef ENABLE_SYNC_TX while (!(COM_USART->SR & USART_SR_TC)) ; @@ -1339,7 +1339,7 @@ char mcu_getc(void) #if !(LED < 0) mcu_toggle_output(LED); #endif -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #ifdef ENABLE_SYNC_RX while (!(COM_USART->SR & USART_SR_RXNE)) ; diff --git a/uCNC/src/hal/mcus/stm32f4x/mcumap_stm32f4x.h b/uCNC/src/hal/mcus/stm32f4x/mcumap_stm32f4x.h index 53f1a99d0..3d8c51763 100644 --- a/uCNC/src/hal/mcus/stm32f4x/mcumap_stm32f4x.h +++ b/uCNC/src/hal/mcus/stm32f4x/mcumap_stm32f4x.h @@ -2922,7 +2922,7 @@ extern "C" #endif // COM registers -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) // this MCU does not work well with both TX and RX interrupt // this forces the sync TX method to fix communication // #define ENABLE_SYNC_TX @@ -3102,7 +3102,7 @@ extern "C" // #define mcu_enable_tx_isr() // #define mcu_disable_tx_isr() // #endif -#if (INTERFACE == INTERFACE_USART) +#if (INTERFACE == INTERFACE_UART) #define mcu_rx_ready() (COM_USART->SR & USART_SR_RXNE) #define mcu_tx_ready() (COM_USART->SR & USART_SR_TXE) #elif (INTERFACE == INTERFACE_USB) diff --git a/uCNC/src/hal/mcus/virtual/mcu_virtual.c b/uCNC/src/hal/mcus/virtual/mcu_virtual.c index 7a25c4c7a..bef4083e0 100644 --- a/uCNC/src/hal/mcus/virtual/mcu_virtual.c +++ b/uCNC/src/hal/mcus/virtual/mcu_virtual.c @@ -84,7 +84,7 @@ #endif extern void mod_input_change_hook(void); -void mcu_inputs_changed_cb(void) +MCU_IO_CALLBACK void mcu_inputs_changed_cb(void) { #ifdef ENABLE_IO_MODULES mod_input_change_hook(); diff --git a/uCNC/src/interface/serial.c b/uCNC/src/interface/serial.c index da53d524d..2ee6d35af 100644 --- a/uCNC/src/interface/serial.c +++ b/uCNC/src/interface/serial.c @@ -348,7 +348,7 @@ void serial_flush(void) // ISR // New char handle strategy // All ascii will be sent to buffer and processed later (including comments) -void mcu_com_rx_cb(unsigned char c) +MCU_RX_CALLBACK void mcu_com_rx_cb(unsigned char c) { uint8_t write; if (c < ((unsigned char)'~')) // ascii (except CMD_CODE_CYCLE_START and DEL) @@ -389,7 +389,7 @@ void mcu_com_rx_cb(unsigned char c) } } -void mcu_com_tx_cb(void) +MCU_TX_CALLBACK void mcu_com_tx_cb(void) { #ifndef ENABLE_SYNC_TX uint8_t read = serial_tx_read; diff --git a/uCNC/src/interface/serial.h b/uCNC/src/interface/serial.h index e5fb59a7f..022406860 100644 --- a/uCNC/src/interface/serial.h +++ b/uCNC/src/interface/serial.h @@ -31,7 +31,9 @@ extern "C" #define EOL 0x00 // end of line char #define OVF 0x2A // overflow char #define SAFEMARGIN 2 +#ifndef RX_BUFFER_CAPACITY #define RX_BUFFER_CAPACITY 128 +#endif #define RX_BUFFER_SIZE (RX_BUFFER_CAPACITY + SAFEMARGIN) // buffer sizes #ifndef ECHO_CMD #define TX_BUFFER_SIZE (112 + SAFEMARGIN) // buffer sizes diff --git a/uCNC/uCNC.ino b/uCNC/uCNC.ino index 97f63da50..0d32aaa80 100644 --- a/uCNC/uCNC.ino +++ b/uCNC/uCNC.ino @@ -1,12 +1,13 @@ #include "src/cnc.h" -int main(void) +void setup() { - // initializes all systems - cnc_init(); + // put your setup code here, to run once: + cnc_init(); +} - for (;;) - { - cnc_run(); - } +void loop() +{ + // put your main code here, to run repeatedly: + cnc_run(); }