From 3034d8654b10b2f49367cc61ab9ee3e41ff66764 Mon Sep 17 00:00:00 2001 From: MKS-Sean <173664774@qq.com> Date: Fri, 11 Jun 2021 15:20:08 +0800 Subject: [PATCH 01/16] mks robin nano v3 add function of wifi --- Marlin/src/MarlinCore.cpp | 7 - Marlin/src/lcd/extui/mks_ui/draw_printing.cpp | 4 +- .../src/lcd/extui/mks_ui/draw_ready_print.cpp | 89 +-- Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp | 3 + .../lcd/extui/mks_ui/printer_operation.cpp | 4 +- .../extui/mks_ui/tft_lvgl_configuration.cpp | 26 +- Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp | 547 +++++++++++++++--- Marlin/src/lcd/extui/mks_ui/wifiSerial.h | 163 ++++-- Marlin/src/lcd/extui/mks_ui/wifi_module.cpp | 537 ++++++++++++++--- Marlin/src/lcd/extui/mks_ui/wifi_module.h | 4 + Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp | 3 +- .../src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h | 7 + 12 files changed, 1091 insertions(+), 303 deletions(-) diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp index 40e6d77c749d..b716f6c834a3 100644 --- a/Marlin/src/MarlinCore.cpp +++ b/Marlin/src/MarlinCore.cpp @@ -1145,13 +1145,6 @@ void setup() { #endif #endif - #if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE) - mks_esp_wifi_init(); - WIFISERIAL.begin(WIFI_BAUDRATE); - serial_connect_timeout = millis() + 1000UL; - while (/*!WIFISERIAL && */PENDING(millis(), serial_connect_timeout)) { /*nada*/ } - #endif - TERN_(DYNAMIC_VECTORTABLE, hook_cpu_exceptions()); // If supported, install Marlin exception handlers at runtime SETUP_RUN(HAL_init()); diff --git a/Marlin/src/lcd/extui/mks_ui/draw_printing.cpp b/Marlin/src/lcd/extui/mks_ui/draw_printing.cpp index c9172d5887dc..d00308a62cfa 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_printing.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_printing.cpp @@ -47,14 +47,16 @@ static lv_obj_t *scr; static lv_obj_t *labelExt1, *labelFan, *labelZpos, *labelTime; static lv_obj_t *labelPause, *labelStop, *labelOperat; static lv_obj_t *bar1, *bar1ValueText; -static lv_obj_t *buttonPause, *buttonOperat, *buttonStop, *buttonExt1, *buttonExt2, *buttonBedstate, *buttonFanstate, *buttonZpos; +static lv_obj_t *buttonPause, *buttonOperat, *buttonStop, *buttonExt1, *buttonFanstate, *buttonZpos; #if ENABLED(HAS_MULTI_EXTRUDER) static lv_obj_t *labelExt2; + static lv_obj_t *buttonExt2; #endif #if HAS_HEATED_BED static lv_obj_t* labelBed; + static lv_obj_t* buttonBedstate; #endif enum { diff --git a/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp b/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp index 18f125b57d3a..0ad74fd45f8f 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp @@ -48,14 +48,16 @@ extern lv_group_t* g; static lv_obj_t *scr; -static lv_obj_t *labelExt1, *labelExt1Target, *labelFan; +static lv_obj_t *buttonExt1, *labelExt1, *buttonFanstate, *labelFan; -#if HAS_MULTI_EXTRUDER - static lv_obj_t *labelExt2, *labelExt2Target; +#if ENABLED(HAS_MULTI_EXTRUDER) + static lv_obj_t *labelExt2; + static lv_obj_t *buttonExt2; #endif #if HAS_HEATED_BED - static lv_obj_t *labelBed, *labelBedTarget; + static lv_obj_t* labelBed; + static lv_obj_t* buttonBedstate; #endif #if ENABLED(MKS_TEST) @@ -178,72 +180,35 @@ void lv_draw_ready_print() { lv_label_set_text(det_info, " "); } else { - lv_big_button_create(scr, "F:/bmp_tool.bin", main_menu.tool, 20, 180, event_handler, ID_TOOL); - lv_big_button_create(scr, "F:/bmp_set.bin", main_menu.set, 180, 180, event_handler, ID_SET); - lv_big_button_create(scr, "F:/bmp_printing.bin", main_menu.print, 340, 180, event_handler, ID_PRINT); + lv_big_button_create(scr, "F:/bmp_tool.bin", main_menu.tool, 20, 150, event_handler, ID_TOOL); + lv_big_button_create(scr, "F:/bmp_set.bin", main_menu.set, 180, 150, event_handler, ID_SET); + lv_big_button_create(scr, "F:/bmp_printing.bin", main_menu.print, 340, 150, event_handler, ID_PRINT); // Monitoring - lv_obj_t *buttonExt1 = lv_big_button_create(scr, "F:/bmp_ext1_state.bin", " ", 55, ICON_POS_Y, event_handler, ID_INFO_EXT); + buttonExt1 = lv_big_button_create(scr, "F:/bmp_ext1_state.bin", " ", 55, ICON_POS_Y, event_handler, ID_INFO_EXT); #if HAS_MULTI_EXTRUDER - lv_obj_t *buttonExt2 = lv_big_button_create(scr, "F:/bmp_ext2_state.bin", " ", 163, ICON_POS_Y, event_handler, ID_INFO_EXT); + buttonExt2 = lv_big_button_create(scr, "F:/bmp_ext2_state.bin", " ", 163, ICON_POS_Y, event_handler, ID_INFO_EXT); #if HAS_HEATED_BED - lv_obj_t *buttonBedstate = lv_big_button_create(scr, "F:/bmp_bed_state.bin", " ", 271, ICON_POS_Y, event_handler, ID_INFO_BED); + buttonBedstate = lv_big_button_create(scr, "F:/bmp_bed_state.bin", " ", 271, ICON_POS_Y, event_handler, ID_INFO_BED); #endif #else #if HAS_HEATED_BED - lv_obj_t *buttonBedstate = lv_big_button_create(scr, "F:/bmp_bed_state.bin", " ", 210, ICON_POS_Y, event_handler, ID_INFO_BED); + buttonBedstate = lv_big_button_create(scr, "F:/bmp_bed_state.bin", " ", 210, ICON_POS_Y, event_handler, ID_INFO_BED); #endif #endif - lv_obj_t *buttonFanstate = lv_big_button_create(scr, "F:/bmp_fan_state.bin", " ", 380, ICON_POS_Y, event_handler, ID_INFO_FAN); - - labelExt1 = lv_label_create(scr, 55, LABEL_MOD_Y, nullptr); - labelExt1Target = lv_label_create(scr, 55, LABEL_MOD_Y, nullptr); - - #if HAS_MULTI_EXTRUDER - labelExt2 = lv_label_create(scr, 163, LABEL_MOD_Y, nullptr); - labelExt2Target = lv_label_create(scr, 163, LABEL_MOD_Y, nullptr); - #if HAS_HEATED_BED - labelBed = lv_label_create(scr, 271, LABEL_MOD_Y, nullptr); - labelBedTarget = lv_label_create(scr, 271, LABEL_MOD_Y, nullptr); - #endif - #else - #if HAS_HEATED_BED - labelBed = lv_label_create(scr, 210, LABEL_MOD_Y, nullptr); - labelBedTarget = lv_label_create(scr, 210, LABEL_MOD_Y, nullptr); - #endif - #endif - - labelFan = lv_label_create(scr, 380, 80, nullptr); - - itoa(thermalManager.degHotend(0), buf, 10); - lv_label_set_text(labelExt1, buf); - lv_obj_align(labelExt1, buttonExt1, LV_ALIGN_CENTER, 0, LABEL_MOD_Y); - sprintf_P(buf, PSTR("-> %d"), thermalManager.degTargetHotend(0)); - lv_label_set_text(labelExt1Target, buf); - lv_obj_align(labelExt1Target, buttonExt1, LV_ALIGN_CENTER, 0, TARGET_LABEL_MOD_Y); + buttonFanstate = lv_big_button_create(scr, "F:/bmp_fan_state.bin", " ", 380, ICON_POS_Y, event_handler, ID_INFO_FAN); + labelExt1 = lv_label_create_empty(scr); #if HAS_MULTI_EXTRUDER - itoa(thermalManager.degHotend(1), buf, 10); - lv_label_set_text(labelExt2, buf); - lv_obj_align(labelExt2, buttonExt2, LV_ALIGN_CENTER, 0, LABEL_MOD_Y); - sprintf_P(buf, PSTR("-> %d"), thermalManager.degTargetHotend(1)); - lv_label_set_text(labelExt2Target, buf); - lv_obj_align(labelExt2Target, buttonExt2, LV_ALIGN_CENTER, 0, TARGET_LABEL_MOD_Y); + labelExt2 = lv_label_create_empty(scr); #endif - #if HAS_HEATED_BED - itoa(thermalManager.degBed(), buf, 10); - lv_label_set_text(labelBed, buf); - lv_obj_align(labelBed, buttonBedstate, LV_ALIGN_CENTER, 0, LABEL_MOD_Y); - sprintf_P(buf, PSTR("-> %d"), thermalManager.degTargetBed()); - lv_label_set_text(labelBedTarget, buf); - lv_obj_align(labelBedTarget, buttonBedstate, LV_ALIGN_CENTER, 0, TARGET_LABEL_MOD_Y); + labelBed = lv_label_create_empty(scr); #endif + labelFan = lv_label_create_empty(scr); - sprintf_P(buf, PSTR("%d%%"), (int)thermalManager.fanSpeedPercent(0)); - lv_label_set_text(labelFan, buf); - lv_obj_align(labelFan, buttonFanstate, LV_ALIGN_CENTER, 0, LABEL_MOD_Y); + lv_temp_refr(); } #if ENABLED(TOUCH_SCREEN_CALIBRATION) @@ -256,18 +221,22 @@ void lv_draw_ready_print() { } void lv_temp_refr() { - #if HAS_HEATED_BED - sprintf(public_buf_l, printing_menu.bed_temp, thermalManager.wholeDegBed(), thermalManager.degTargetBed()); - lv_label_set_text(labelBed, public_buf_l); - #endif - sprintf(public_buf_l, printing_menu.temp1, thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0)); lv_label_set_text(labelExt1, public_buf_l); - + lv_obj_align(labelExt1, buttonExt1, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); #if HAS_MULTI_EXTRUDER sprintf(public_buf_l, printing_menu.temp1, thermalManager.wholeDegHotend(1), thermalManager.degTargetHotend(1)); lv_label_set_text(labelExt2, public_buf_l); + lv_obj_align(labelExt2, buttonExt2, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); + #endif + #if HAS_HEATED_BED + sprintf(public_buf_l, printing_menu.bed_temp, thermalManager.wholeDegBed(), thermalManager.degTargetBed()); + lv_label_set_text(labelBed, public_buf_l); + lv_obj_align(labelBed, buttonBedstate, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); #endif + sprintf_P(public_buf_l, PSTR("%d%%"), (int)thermalManager.fanSpeedPercent(0)); + lv_label_set_text(labelFan, public_buf_l); + lv_obj_align(labelFan, buttonFanstate, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); } void lv_clear_ready_print() { diff --git a/Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp b/Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp index df48cbec0ac8..6822e6823284 100644 --- a/Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp +++ b/Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp @@ -25,6 +25,8 @@ #include "tft_lvgl_configuration.h" +#ifdef __STM32F1__ + #if ENABLED(MKS_WIFI_MODULE) #include "draw_ui.h" @@ -57,4 +59,5 @@ void __irq_usart1() { #endif #endif // MKS_WIFI_MODULE +#endif // __STM32F1__ #endif // HAS_TFT_LVGL_UI diff --git a/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp b/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp index b618a01957e8..46994da3a6ce 100644 --- a/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp +++ b/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp @@ -158,7 +158,9 @@ void filament_pin_setup() { } void filament_check() { - const int FIL_DELAY = 20; + #if (PIN_EXISTS(MT_DET_1) || PIN_EXISTS(MT_DET_2) || PIN_EXISTS(MT_DET_3)) + const int FIL_DELAY = 20; + #endif #if PIN_EXISTS(MT_DET_1) static int fil_det_count_1 = 0; if (!READ(MT_DET_1_PIN) && !MT_DET_PIN_INVERTING) diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index 84e3040e84e7..4ddc45c42ebb 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -45,6 +45,14 @@ XPT2046 touch; #include "../../../feature/powerloss.h" #endif +#if HAS_SERVOS + #include "../../../../module/servo.h" +#endif + +#if EITHER(PROBE_TARE, HAS_Z_SERVO_PROBE) + #include "../../../../module/probe.h" +#endif + #if ENABLED(TOUCH_SCREEN_CALIBRATION) #include "../../tft_io/touch_calibration.h" #include "draw_touch_calibration.h" @@ -193,8 +201,12 @@ void tft_lvgl_init() { filament_pin_setup(); lv_encoder_pin_init(); - TERN_(MKS_WIFI_MODULE, mks_wifi_firmware_update()); - + #if ENABLED(MKS_WIFI_MODULE) + mks_esp_wifi_init(); + mks_wifi_firmware_update(); + #endif + TERN_(HAS_SERVOS, servo_init()); + TERN_(HAS_Z_SERVO_PROBE, probe.servo_probe_init()); bool ready = true; #if ENABLED(POWER_LOSS_RECOVERY) recovery.load(); @@ -207,9 +219,13 @@ void tft_lvgl_init() { uiCfg.print_state = REPRINTING; - strncpy(public_buf_m, recovery.info.sd_filename, sizeof(public_buf_m)); - card.printLongPath(public_buf_m); - strncpy(list_file.long_name[sel_id], card.longFilename, sizeof(list_file.long_name[0])); + #if ENABLED(LONG_FILENAME_HOST_SUPPORT) + strncpy(public_buf_m, recovery.info.sd_filename, sizeof(public_buf_m)); + card.printLongPath(public_buf_m); + strncpy(list_file.long_name[sel_id], card.longFilename, sizeof(list_file.long_name[0])); + #else + strncpy(list_file.long_name[sel_id], recovery.info.sd_filename, sizeof(list_file.long_name[0])); + #endif lv_draw_printing(); } #endif diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp b/Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp index d00fd269d828..0af9476d785e 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp @@ -30,112 +30,497 @@ #include "draw_ui.h" #include "wifiSerial.h" -#include -#include -#include -#include -#include +#ifdef __STM32F1__ -#include "../../../MarlinCore.h" + #include + #include + #include + #include + #include -DEFINE_WFSERIAL(WifiSerial1, 1); + #include "../../../MarlinCore.h" -WifiSerial::WifiSerial(usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin) { - this->usart_device = usart_device; - this->tx_pin = tx_pin; - this->rx_pin = rx_pin; -} + DEFINE_WFSERIAL(WifiSerial1, 1); -/** - * Set up / tear down - */ -#if STM32_MCU_SERIES == STM32_SERIES_F1 - /* F1 MCUs have no GPIO_AFR[HL], so turn off PWM if there's a conflict - * on this GPIO bit. */ - static void disable_timer_if_necessary(timer_dev *dev, uint8 ch) { - if (dev) timer_set_mode(dev, ch, TIMER_DISABLED); - } - static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) { - if (with_irq) usart_enable(usart_device); - else { - usart_reg_map *regs = usart_device->regs; - regs->CR1 |= (USART_CR1_TE | USART_CR1_RE); // don't change the word length etc, and 'or' in the pattern not overwrite |USART_CR1_M_8N1); - regs->CR1 |= USART_CR1_UE; - } + WifiSerial::WifiSerial(usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin) { + this->usart_device = usart_device; + this->tx_pin = tx_pin; + this->rx_pin = rx_pin; } -#elif STM32_MCU_SERIES == STM32_SERIES_F2 || STM32_MCU_SERIES == STM32_SERIES_F4 - #define disable_timer_if_necessary(dev, ch) ((void)0) + /** + * Set up / tear down + */ + #if STM32_MCU_SERIES == STM32_SERIES_F1 + /* F1 MCUs have no GPIO_AFR[HL], so turn off PWM if there's a conflict + * on this GPIO bit. */ + static void disable_timer_if_necessary(timer_dev *dev, uint8 ch) { + if (dev) timer_set_mode(dev, ch, TIMER_DISABLED); + } + static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) { + if (with_irq) usart_enable(usart_device); + else { + usart_reg_map *regs = usart_device->regs; + regs->CR1 |= (USART_CR1_TE | USART_CR1_RE);// don't change the word length etc, and 'or' in the patten not overwrite |USART_CR1_M_8N1); + regs->CR1 |= USART_CR1_UE; + } + } - static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) { - if (with_irq) usart_enable(usart_device); - else { - usart_reg_map *regs = usart_device->regs; - regs->CR1 |= (USART_CR1_TE | USART_CR1_RE); // don't change the word length etc, and 'or' in the pattern not overwrite |USART_CR1_M_8N1); - regs->CR1 |= USART_CR1_UE; + #elif STM32_MCU_SERIES == STM32_SERIES_F2 || STM32_MCU_SERIES == STM32_SERIES_F4 + #define disable_timer_if_necessary(dev, ch) ((void)0) + + static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) { + if (with_irq) usart_enable(usart_device); + else { + usart_reg_map *regs = usart_device->regs; + regs->CR1 |= (USART_CR1_TE | USART_CR1_RE);// don't change the word length etc, and 'or' in the patten not overwrite |USART_CR1_M_8N1); + regs->CR1 |= USART_CR1_UE; + } } + #else + #warning "Unsupported STM32 series; timer conflicts are possible" + #define usart_enable_no_irq(X, Y) usart_enable(X) + #endif + + void WifiSerial::begin(uint32 baud) { begin(baud, SERIAL_8N1); } + + /** + * Roger Clark. + * Note. The config parameter is not currently used. This is a work in progress. + * Code needs to be written to set the config of the hardware serial control register in question. + */ + + void WifiSerial::begin(uint32 baud, uint8_t config) { + //ASSERT(baud <= this->usart_device->max_baud); // Roger Clark. Assert doesn't do anything useful, we may as well save the space in flash and ram etc + + if (baud > this->usart_device->max_baud) return; + + const stm32_pin_info *txi = &PIN_MAP[this->tx_pin], + *rxi = &PIN_MAP[this->rx_pin]; + + disable_timer_if_necessary(txi->timer_device, txi->timer_channel); + + usart_init(this->usart_device); + + // Reinitialize the receive buffer, mks_esp8266 fixed data frame length is 1k bytes + rb_init(this->usart_device->rb, WIFI_RX_BUF_SIZE, wifiRxBuf); + + usart_config_gpios_async(this->usart_device, + rxi->gpio_device, rxi->gpio_bit, + txi->gpio_device, txi->gpio_bit, + config); + usart_set_baud_rate(this->usart_device, USART_USE_PCLK, baud); + usart_enable_no_irq(this->usart_device, baud == WIFI_BAUDRATE); + } + + void WifiSerial::end() { + usart_disable(this->usart_device); + } + + int WifiSerial::available() { + return usart_data_available(this->usart_device); + } + + // + // I/O + // + + int WifiSerial::read() { + if (usart_data_available(usart_device) <= 0) return -1; + return usart_getc(usart_device); + } + + int WifiSerial::write(unsigned char ch) { + usart_putc(this->usart_device, ch); + return 1; + } + + int WifiSerial::wifi_rb_is_full() { + return rb_is_full(this->usart_device->rb); } + #else - #warning "Unsupported STM32 series; timer conflicts are possible" - #define usart_enable_no_irq(X, Y) usart_enable(X) -#endif -void WifiSerial::begin(uint32 baud) { begin(baud, SERIAL_8N1); } + WifiSerial WifiSerial1(USART1); -/** - * Roger Clark. - * Note. The config parameter is not currently used. This is a work in progress. - * Code needs to be written to set the config of the hardware serial control register in question. - */ + void WifiSerial::setRx(uint32_t _rx) + { + _serial.pin_rx = digitalPinToPinName(_rx); + } -void WifiSerial::begin(uint32 baud, uint8_t config) { - //ASSERT(baud <= this->usart_device->max_baud); // Roger Clark. Assert doesn't do anything useful, we may as well save the space in flash and ram etc + void WifiSerial::setTx(uint32_t _tx) + { + _serial.pin_tx = digitalPinToPinName(_tx); + } - if (baud > this->usart_device->max_baud) return; + void WifiSerial::setRx(PinName _rx) + { + _serial.pin_rx = _rx; + } - const stm32_pin_info *txi = &PIN_MAP[this->tx_pin], - *rxi = &PIN_MAP[this->rx_pin]; + void WifiSerial::setTx(PinName _tx) + { + _serial.pin_tx = _tx; + } - disable_timer_if_necessary(txi->timer_device, txi->timer_channel); + void WifiSerial::init(PinName _rx, PinName _tx) + { + if (_rx == _tx) { + _serial.pin_rx = NC; + } else { + _serial.pin_rx = _rx; + } + _serial.pin_tx = _tx; + _serial.rx_buff = wifiRxBuf; + _serial.rx_head = 0; + _serial.rx_tail = 0; + _serial.tx_buff = wifiTxBuf; + _serial.tx_head = 0; + _serial.tx_tail = 0; + } - usart_init(this->usart_device); + WifiSerial::WifiSerial(void *peripheral) + { + // If PIN_SERIALy_RX is not defined assume half-duplex + _serial.pin_rx = NC; + // If Serial is defined in variant set + // the Rx/Tx pins for com port if defined + #if defined(Serial) && defined(PIN_SERIAL_TX) + if ((void *)this == (void *)&Serial) { + #if defined(PIN_SERIAL_RX) + setRx(PIN_SERIAL_RX); + #endif + setTx(PIN_SERIAL_TX); + } else + #endif + #if defined(PIN_SERIAL1_TX) && defined(USART1_BASE) + if (peripheral == USART1) { + #if defined(PIN_SERIAL1_RX) + setRx(PIN_SERIAL1_RX); + #endif + setTx(PIN_SERIAL1_TX); + } else + #endif + #if defined(PIN_SERIAL2_TX) && defined(USART2_BASE) + if (peripheral == USART2) { + #if defined(PIN_SERIAL2_RX) + setRx(PIN_SERIAL2_RX); + #endif + setTx(PIN_SERIAL2_TX); + } else + #endif + #if defined(PIN_SERIAL3_TX) && defined(USART3_BASE) + if (peripheral == USART3) { + #if defined(PIN_SERIAL3_RX) + setRx(PIN_SERIAL3_RX); + #endif + setTx(PIN_SERIAL3_TX); + } else + #endif + #if defined(PIN_SERIAL4_TX) &&\ + (defined(USART4_BASE) || defined(UART4_BASE)) + #if defined(USART4_BASE) + if (peripheral == USART4) + #elif defined(UART4_BASE) + if (peripheral == UART4) + #endif + { + #if defined(PIN_SERIAL4_RX) + setRx(PIN_SERIAL4_RX); + #endif + setTx(PIN_SERIAL4_TX); + } else + #endif + #if defined(PIN_SERIAL5_TX) &&\ + (defined(USART5_BASE) || defined(UART5_BASE)) + #if defined(USART5_BASE) + if (peripheral == USART5) + #elif defined(UART5_BASE) + if (peripheral == UART5) + #endif + { + #if defined(PIN_SERIAL5_RX) + setRx(PIN_SERIAL5_RX); + #endif + setTx(PIN_SERIAL5_TX); + } else + #endif + #if defined(PIN_SERIAL6_TX) && defined(USART6_BASE) + if (peripheral == USART6) { + #if defined(PIN_SERIAL6_RX) + setRx(PIN_SERIAL6_RX); + #endif + setTx(PIN_SERIAL6_TX); + } else + #endif + #if defined(PIN_SERIAL7_TX) &&\ + (defined(USART7_BASE) || defined(UART7_BASE)) + #if defined(USART7_BASE) + if (peripheral == USART7) + #elif defined(UART7_BASE) + if (peripheral == UART7) + #endif + { + #if defined(PIN_SERIAL7_RX) + setRx(PIN_SERIAL7_RX); + #endif + setTx(PIN_SERIAL7_TX); + } else + #endif + #if defined(PIN_SERIAL8_TX) &&\ + (defined(USART8_BASE) || defined(UART8_BASE)) + #if defined(USART8_BASE) + if (peripheral == USART8) + #elif defined(UART8_BASE) + if (peripheral == UART8) + #endif + { + #if defined(PIN_SERIAL8_RX) + setRx(PIN_SERIAL8_RX); + #endif + setTx(PIN_SERIAL8_TX); + } else + #endif + #if defined(PIN_SERIAL9_TX) && defined(UART9_BASE) + if (peripheral == UART9) { + #if defined(PIN_SERIAL9_RX) + setRx(PIN_SERIAL9_RX); + #endif + setTx(PIN_SERIAL9_TX); + } else + #endif + #if defined(PIN_SERIAL10_TX) &&\ + (defined(USART10_BASE) || defined(UART10_BASE)) + #if defined(USART10_BASE) + if (peripheral == USART10) + #elif defined(UART10_BASE) + if (peripheral == UART10) + #endif + { + #if defined(PIN_SERIAL10_RX) + setRx(PIN_SERIAL10_RX); + #endif + setTx(PIN_SERIAL10_TX); + } else + #endif + #if defined(PIN_SERIALLP1_TX) && defined(LPUART1_BASE) + if (peripheral == LPUART1) { + #if defined(PIN_SERIALLP1_RX) + setRx(PIN_SERIALLP1_RX); + #endif + setTx(PIN_SERIALLP1_TX); + } else + #endif + // else get the pins of the first peripheral occurence in PinMap + { + _serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX); + _serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX); + } + // if (halfDuplex == HALF_DUPLEX_ENABLED) { + // _serial.pin_rx = NC; + // } + init(_serial.pin_rx, _serial.pin_tx); + } - // Reinitialize the receive buffer, mks_esp8266 fixed data frame length is 1k bytes - rb_init(this->usart_device->rb, WIFI_RX_BUF_SIZE, wifiRxBuf); + void WifiSerial::flush() + { + // If we have never written a byte, no need to flush. This special + // case is needed since there is no way to force the TXC (transmit + // complete) bit to 1 during initialization + if (!_written) { + return; + } - usart_config_gpios_async(this->usart_device, - rxi->gpio_device, rxi->gpio_bit, - txi->gpio_device, txi->gpio_bit, - config); - usart_set_baud_rate(this->usart_device, USART_USE_PCLK, baud); - usart_enable_no_irq(this->usart_device, baud == WIFI_BAUDRATE); -} + while ((_serial.tx_head != _serial.tx_tail)) { + // nop, the interrupt handler will free up space for us + } + // If we get here, nothing is queued anymore (DRIE is disabled) and + // the hardware finished tranmission (TXC is set). + } -void WifiSerial::end() { - usart_disable(this->usart_device); -} + bool WifiSerial::isHalfDuplex(void) const + { + return _serial.pin_rx == NC; + } -int WifiSerial::available() { - return usart_data_available(this->usart_device); -} + void WifiSerial::enableHalfDuplexRx(void) + { + if (isHalfDuplex()) { + // In half-duplex mode we have to wait for all TX characters to + // be transmitted before we can receive data. + flush(); + if (!_rx_enabled) { + _rx_enabled = true; + uart_enable_rx(&_serial); + } + } + } -// -// I/O -// + // Actual interrupt handlers ////////////////////////////////////////////////////////////// -int WifiSerial::read() { - if (usart_data_available(usart_device) <= 0) return -1; - return usart_getc(usart_device); -} + void WifiSerial::_rx_complete_irq(serial_t *obj) + { + // No Parity error, read byte and store it in the buffer if there is room + unsigned char c; -int WifiSerial::write(unsigned char ch) { - usart_putc(this->usart_device, ch); - return 1; -} + if (uart_getc(obj, &c) == 0) { -int WifiSerial::wifi_rb_is_full() { - return rb_is_full(this->usart_device->rb); -} + WRITE(WIFI_IO1_PIN, HIGH); + rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % WIFI_RX_BUF_SIZE; + + // if we should be storing the received character into the location + // just before the tail (meaning that the head would advance to the + // current location of the tail), we're about to overflow the buffer + // and so we don't write the character or advance the head. + if (i != obj->rx_tail) { + obj->rx_buff[obj->rx_head] = c; + obj->rx_head = i; + } + } + } + + // Actual interrupt handlers ////////////////////////////////////////////////////////////// + + int WifiSerial::_tx_complete_irq(serial_t *obj) + { + // If interrupts are enabled, there must be more data in the output + // buffer. Send the next byte + obj->tx_tail = (obj->tx_tail + 1) % WIFI_TX_BUF_SIZE; + + if (obj->tx_head == obj->tx_tail) { + return -1; + } + + return 0; + } + + void WifiSerial::begin(unsigned long baud) { begin(baud, SERIAL_8N1); } + + void WifiSerial::begin(unsigned long baud, byte config) { + uint32_t databits = 0; + uint32_t stopbits = 0; + uint32_t parity = 0; + + _baud = baud; + _config = config; + + // Manage databits + switch (config & 0x07) { + case 0x02: + databits = 6; + break; + case 0x04: + databits = 7; + break; + case 0x06: + databits = 8; + break; + default: + databits = 0; + break; + } + + if ((config & 0x30) == 0x30) { + parity = UART_PARITY_ODD; + databits++; + } else if ((config & 0x20) == 0x20) { + parity = UART_PARITY_EVEN; + databits++; + } else { + parity = UART_PARITY_NONE; + } + + if ((config & 0x08) == 0x08) { + stopbits = UART_STOPBITS_2; + } else { + stopbits = UART_STOPBITS_1; + } + + switch (databits) { + #ifdef UART_WORDLENGTH_7B + case 7: + databits = UART_WORDLENGTH_7B; + break; + #endif + case 8: + databits = UART_WORDLENGTH_8B; + break; + case 9: + databits = UART_WORDLENGTH_9B; + break; + default: + case 0: + Error_Handler(); + break; + } + + uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits); + enableHalfDuplexRx(); + if (baud == WIFI_BAUDRATE) uart_attach_rx_callback(&_serial, _rx_complete_irq); + else { + USART1->CR1 |= (USART_CR1_RE);// don't change the word length etc, and 'or' in the patten not overwrite |USART_CR1_M_8N1); + } + } + + void WifiSerial::end(void) + { + // wait for transmission of outgoing data + flush(); + + uart_deinit(&_serial); + + // clear any received data + _serial.rx_head = _serial.rx_tail; + } + + int WifiSerial::available(void) { + return ((unsigned int)(WIFI_RX_BUF_SIZE + _serial.rx_head - _serial.rx_tail)) % WIFI_RX_BUF_SIZE; + } + + // + // I/O + // + int WifiSerial::read(void) + { + enableHalfDuplexRx(); + // if the head isn't ahead of the tail, we don't have any characters + if (_serial.rx_head == _serial.rx_tail) { + return -1; + } else { + unsigned char c = _serial.rx_buff[_serial.rx_tail]; + _serial.rx_tail = (rx_buffer_index_t)(_serial.rx_tail + 1) % WIFI_RX_BUF_SIZE; + return c; + } + } + + int WifiSerial::write(uint8_t c) + { + _written = true; + if (isHalfDuplex()) { + if (_rx_enabled) { + _rx_enabled = false; + uart_enable_tx(&_serial); + } + } + + tx_buffer_index_t i = (_serial.tx_head + 1) % WIFI_TX_BUF_SIZE; + + // If the output buffer is full, there's nothing for it other than to + // wait for the interrupt handler to empty it a bit + while (i == _serial.tx_tail) { + // nop, the interrupt handler will free up space for us + } + + _serial.tx_buff[_serial.tx_head] = c; + _serial.tx_head = i; + + if (!serial_tx_active(&_serial)) { + uart_attach_tx_callback(&_serial, _tx_complete_irq); + } + + return 1; + } +#endif // __STM32F1__ #endif // MKS_WIFI_MODULE #endif // HAS_TFT_LVGL_UI diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial.h b/Marlin/src/lcd/extui/mks_ui/wifiSerial.h index fcc35a6a657c..f6d41d45bdb5 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifiSerial.h +++ b/Marlin/src/lcd/extui/mks_ui/wifiSerial.h @@ -33,70 +33,113 @@ #define WIFI_UPLOAD_BAUDRATE 1958400 #define USART_SAFE_INSERT -#define WIFI_RX_BUF_SIZE (1024+1) - -#include -#include -#include -#include -#include -#include - -#define DEFINE_WFSERIAL(name, n)\ - WifiSerial name(USART##n, \ - BOARD_USART##n##_TX_PIN, \ - BOARD_USART##n##_RX_PIN) - -class WifiSerial { - public: - uint8 wifiRxBuf[WIFI_RX_BUF_SIZE]; - - public: - WifiSerial(struct usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin); - - /* Set up/tear down */ - void begin(uint32 baud); - void begin(uint32 baud,uint8_t config); - void end(); - int available(); - int read(); - int write(uint8_t); - inline void wifi_usart_irq(usart_reg_map *regs) { - /* Handling RXNEIE and TXEIE interrupts. - * RXNE signifies availability of a byte in DR. - * - * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15. - * We enable RXNEIE. - */ - if ((regs->CR1 & USART_CR1_RXNEIE) && (regs->SR & USART_SR_RXNE)) { - #ifdef USART_SAFE_INSERT - /* If the buffer is full and the user defines USART_SAFE_INSERT, - * ignore new bytes. */ - rb_safe_insert(this->usart_device->rb, (uint8)regs->DR); - #else - /* By default, push bytes around in the ring buffer. */ - rb_push_insert(this->usart_device->rb, (uint8)regs->DR); - #endif - } - /* TXE signifies readiness to send a byte to DR. */ - if ((regs->CR1 & USART_CR1_TXEIE) && (regs->SR & USART_SR_TXE)) { - if (!rb_is_empty(this->usart_device->wb)) - regs->DR=rb_remove(this->usart_device->wb); - else - regs->CR1 &= ~((uint32)USART_CR1_TXEIE); // disable TXEIE +#define WIFI_RX_BUF_SIZE (1024) +#define WIFI_TX_BUF_SIZE (64) + +#ifdef __STM32F1__ + + #include + #include + #include + #include + #include + #include + + #define DEFINE_WFSERIAL(name, n)\ + WifiSerial name(USART##n, \ + BOARD_USART##n##_TX_PIN, \ + BOARD_USART##n##_RX_PIN) + + class WifiSerial { + public: + uint8 wifiRxBuf[WIFI_RX_BUF_SIZE]; + + public: + WifiSerial(struct usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin); + + /* Set up/tear down */ + void begin(uint32 baud); + void begin(uint32 baud,uint8_t config); + void end(); + int available(); + int read(); + int write(uint8_t); + inline void wifi_usart_irq(usart_reg_map *regs) { + /* Handling RXNEIE and TXEIE interrupts. + * RXNE signifies availability of a byte in DR. + * + * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15. + * We enable RXNEIE. + */ + if ((regs->CR1 & USART_CR1_RXNEIE) && (regs->SR & USART_SR_RXNE)) { + #ifdef USART_SAFE_INSERT + /* If the buffer is full and the user defines USART_SAFE_INSERT, + * ignore new bytes. */ + rb_safe_insert(this->usart_device->rb, (uint8)regs->DR); + #else + /* By default, push bytes around in the ring buffer. */ + rb_push_insert(this->usart_device->rb, (uint8)regs->DR); + #endif + } + /* TXE signifies readiness to send a byte to DR. */ + if ((regs->CR1 & USART_CR1_TXEIE) && (regs->SR & USART_SR_TXE)) { + if (!rb_is_empty(this->usart_device->wb)) + regs->DR=rb_remove(this->usart_device->wb); + else + regs->CR1 &= ~((uint32)USART_CR1_TXEIE); // disable TXEIE + } } - } + int wifi_rb_is_full(); + struct usart_dev *usart_device; + private: + uint8 tx_pin; + uint8 rx_pin; + }; + extern WifiSerial WifiSerial1; + #define WIFISERIAL WifiSerial1 +#else - int wifi_rb_is_full(); + #include + #include "Stream.h" + #include "uart.h" - struct usart_dev *usart_device; - private: - uint8 tx_pin; - uint8 rx_pin; -}; + class WifiSerial { + protected: + // Has any byte been written to the UART since begin() + bool _written; + serial_t _serial; + public: + uint8_t wifiRxBuf[WIFI_RX_BUF_SIZE]; + uint8_t wifiTxBuf[WIFI_TX_BUF_SIZE]; + WifiSerial(void *peripheral); -extern WifiSerial WifiSerial1; + /* Set up/tear down */ + void begin(uint32_t baud); + void begin(uint32_t baud,uint8_t config); + void end(); + int available(void); + int read(void); + int write(uint8_t); + + // Interrupt handlers + static int _tx_complete_irq(serial_t *obj); + static void _rx_complete_irq(serial_t *obj); -#define WIFISERIAL WifiSerial1 + void flush(void); + bool isHalfDuplex(void) const; + void enableHalfDuplexRx(void); + private: + void setRx(uint32_t _rx); + void setTx(uint32_t _tx); + void setRx(PinName _rx); + void setTx(PinName _tx); + void init(PinName _rx, PinName _tx); + bool _rx_enabled; + uint8_t _config; + unsigned long _baud; + }; + extern WifiSerial WifiSerial1; + #define WIFISERIAL WifiSerial1 +#endif // __STM32F1__ #endif // MKS_WIFI_MODULE diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp index 52a877031f83..f790b7a389b8 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp @@ -58,10 +58,6 @@ extern uint8_t Explore_Disk (char *path , uint8_t recu_level); extern uint8_t commands_in_queue; extern uint8_t sel_id; - -int usartFifoAvailable(SZ_USART_FIFO *fifo); -int readUsartFifo(SZ_USART_FIFO *fifo, int8_t *buf, int32_t len); -int writeUsartFifo(SZ_USART_FIFO * fifo, int8_t * buf, int32_t len); extern unsigned int getTickDiff(unsigned int curTick, unsigned int lastTick); volatile SZ_USART_FIFO WifiRxFifo; @@ -154,9 +150,44 @@ void mount_file_sys(uint8_t disk_type) { } } -#include -#include -#include +static bool longName2DosName(const char *longName, char *dosName) { + uint8_t i = FILENAME_LENGTH; + while (i) + dosName[--i] = '\0'; + while (*longName) { + uint8_t c = *longName++; + if (c == '.') { // For a dot... + if (i == 0) { + return false; + } + else { + strcat(dosName, ".GCO"); + return dosName[0] != '\0'; + } + } + else { + // Fail for illegal characters + PGM_P p = PSTR("|<>^+=?/[];,*\"\\"); + while (uint8_t b = pgm_read_byte(p++)) + if (b == c) + return false; + if (c < 0x21 || c == 0x7F) + return false; // Check size, non-printable characters + dosName[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a')); // Uppercase required for 8.3 name + } + if (i >= 5) { + strcat(dosName, "~1.GCO"); + return dosName[0] != '\0'; + } + } + return dosName[0] != '\0'; // Return true if any name was set +} + +#ifdef __STM32F1__ + + #include + #include + #include #include #include @@ -178,45 +209,17 @@ void changeFlashMode(const bool dmaMode) { } } } - -static bool longName2DosName(const char *longName, char *dosName) { - uint8_t i; - for (i = FILENAME_LENGTH; i--;) dosName[i] = '\0'; - while (*longName) { - uint8_t c = *longName++; - if (c == '.') { // For a dot... - if (i == 0) return false; - strcat_P(dosName, PSTR(".GCO")); - break; - } - else { - if (c < 0x21 || c == 0x7F) return false; // Check size, non-printable characters - // Fail for illegal characters - PGM_P p = PSTR("|<>^+=?/[];,*\"\\"); - while (const uint8_t b = pgm_read_byte(p++)) if (b == c) return false; - dosName[i++] = c + (WITHIN(c, 'a', 'z') ? 'A' - 'a' : 0); // Uppercase required for 8.3 name + static int storeRcvData(volatile uint8_t *bufToCpy, int32_t len) { + unsigned char tmpW = wifiDmaRcvFifo.write_cur; + if (len > UDISKBUFLEN) return 0; + if (wifiDmaRcvFifo.state[tmpW] == udisk_buf_empty) { + memcpy((unsigned char *) wifiDmaRcvFifo.bufferAddr[tmpW], (uint8_t *)bufToCpy, len); + wifiDmaRcvFifo.state[tmpW] = udisk_buf_full; + wifiDmaRcvFifo.write_cur = (tmpW + 1) % TRANS_RCV_FIFO_BLOCK_NUM; + return 1; } - if (i >= 5) { - strcat_P(dosName, PSTR("~1.GCO")); - break; - } - } - return dosName[0] != '\0'; // Return true if any name was set -} - -static int storeRcvData(volatile uint8_t *bufToCpy, int32_t len) { - unsigned char tmpW = wifiDmaRcvFifo.write_cur; - - if (len > UDISKBUFLEN) return 0; - - if (wifiDmaRcvFifo.state[tmpW] == udisk_buf_empty) { - memcpy((unsigned char *) wifiDmaRcvFifo.bufferAddr[tmpW], (uint8_t *)bufToCpy, len); - wifiDmaRcvFifo.state[tmpW] = udisk_buf_full; - wifiDmaRcvFifo.write_cur = (tmpW + 1) % TRANS_RCV_FIFO_BLOCK_NUM; - return 1; + return 0; } - return 0; -} static void esp_dma_pre() { dma_channel_reg_map *channel_regs = dma_tube_regs(DMA1, DMA_CH5); @@ -281,40 +284,385 @@ static void wifi_usart_dma_init() { wifiDmaRcvFifo.write_cur = 0; } -void esp_port_begin(uint8_t interrupt) { - WifiRxFifo.uart_read_point = 0; - WifiRxFifo.uart_write_point = 0; - - #if 1 - - #if ENABLED(MKS_WIFI_MODULE) - WIFISERIAL.end(); - if (interrupt) { + void esp_port_begin(uint8_t interrupt) { + WifiRxFifo.uart_read_point = 0; + WifiRxFifo.uart_write_point = 0; + if (interrupt) { + #if ENABLED(MKS_WIFI_MODULE) + WIFISERIAL.end(); for (uint16_t i = 0; i < 65535; i++) { /*nada*/ } WIFISERIAL.begin(WIFI_BAUDRATE); millis_t serial_connect_timeout = millis() + 1000UL; while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ } - } - else { + #endif + } + else { + #if ENABLED(MKS_WIFI_MODULE) + WIFISERIAL.end(); WIFISERIAL.usart_device->regs->CR1 &= ~USART_CR1_RXNEIE; WIFISERIAL.begin(WIFI_UPLOAD_BAUDRATE); wifi_usart_dma_init(); + #endif + } + } +#else + + DMA_HandleTypeDef wifiUsartDMArx; + + void changeFlashMode(const bool dmaMode) { + if (flash_dma_mode != dmaMode) { + flash_dma_mode = dmaMode; + if (flash_dma_mode == 1) { } - #endif + else { + } + } + } + + #ifdef STM32F1xx + + HAL_StatusTypeDef HAL_DMA_PollForTransferCustomize(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_t Timeout) + { + uint32_t temp; + uint32_t tickstart = 0U; + + if(HAL_DMA_STATE_BUSY != hdma->State) + { + /* no transfer ongoing */ + hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER; + __HAL_UNLOCK(hdma); + return HAL_ERROR; + } + + /* Polling mode not supported in circular mode */ + if (RESET != (hdma->Instance->CCR & DMA_CCR_CIRC)) + { + hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED; + return HAL_ERROR; + } + + /* Get the level transfer complete flag */ + if(CompleteLevel == HAL_DMA_FULL_TRANSFER) + { + /* Transfer Complete flag */ + temp = __HAL_DMA_GET_TC_FLAG_INDEX(hdma); + } + else + { + /* Half Transfer Complete flag */ + temp = __HAL_DMA_GET_HT_FLAG_INDEX(hdma); + } + + /* Get tick */ + tickstart = HAL_GetTick(); + + while(__HAL_DMA_GET_FLAG(hdma, temp) == RESET) + { + if((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)) != RESET)) + { + /* Clear the half transfer complete flag */ + __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)); + WIFI_IO1_SET(); + } + + if((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET)) + { + /* When a DMA transfer error occurs */ + /* A hardware clear of its EN bits is performed */ + /* Clear all flags */ + hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << hdma->ChannelIndex); + + /* Update error code */ + SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TE); + + /* Change the DMA state */ + hdma->State= HAL_DMA_STATE_READY; + + /* Process Unlocked */ + __HAL_UNLOCK(hdma); + return HAL_ERROR; + } + /* Check for the Timeout */ + if(Timeout != HAL_MAX_DELAY) + { + if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout)) + { + /* Update error code */ + SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TIMEOUT); + + /* Change the DMA state */ + hdma->State = HAL_DMA_STATE_READY; + + /* Process Unlocked */ + __HAL_UNLOCK(hdma); + + return HAL_ERROR; + } + } + } + + if(CompleteLevel == HAL_DMA_FULL_TRANSFER) + { + /* Clear the transfer complete flag */ + __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma)); + + /* The selected Channelx EN bit is cleared (DMA is disabled and + all transfers are complete) */ + hdma->State = HAL_DMA_STATE_READY; + } + else + { + /* Clear the half transfer complete flag */ + __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)); + } + + /* Process unlocked */ + __HAL_UNLOCK(hdma); + + return HAL_OK; + } #else - #if MKS_WIFI_MODULE - WIFISERIAL.end(); - for (uint16_t i = 0; i < 65535; i++) { /*nada*/ } - WIFISERIAL.begin(interrupt ? WIFI_BAUDRATE : WIFI_UPLOAD_BAUDRATE); - millis_t serial_connect_timeout = millis() + 1000UL; - while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ } - #endif - if (!interrupt) wifi_usart_dma_init(); + typedef struct + { + __IO uint32_t ISR; /*!< DMA interrupt status register */ + __IO uint32_t Reserved0; + __IO uint32_t IFCR; /*!< DMA interrupt flag clear register */ + } MYDMA_Base_Registers; + + HAL_StatusTypeDef HAL_DMA_PollForTransferCustomize(DMA_HandleTypeDef *hdma, HAL_DMA_LevelCompleteTypeDef CompleteLevel, uint32_t Timeout) + { + HAL_StatusTypeDef status = HAL_OK; + uint32_t mask_cpltlevel; + uint32_t tickstart = HAL_GetTick(); + uint32_t tmpisr; + + /* calculate DMA base and stream number */ + MYDMA_Base_Registers *regs; + + if(HAL_DMA_STATE_BUSY != hdma->State) + { + /* No transfer ongoing */ + hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER; + __HAL_UNLOCK(hdma); + return HAL_ERROR; + } + + /* Polling mode not supported in circular mode and double buffering mode */ + if ((hdma->Instance->CR & DMA_SxCR_CIRC) != RESET) + { + hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED; + return HAL_ERROR; + } + + /* Get the level transfer complete flag */ + if(CompleteLevel == HAL_DMA_FULL_TRANSFER) + { + /* Transfer Complete flag */ + mask_cpltlevel = DMA_FLAG_TCIF0_4 << hdma->StreamIndex; + } + else + { + /* Half Transfer Complete flag */ + mask_cpltlevel = DMA_FLAG_HTIF0_4 << hdma->StreamIndex; + } + regs = (MYDMA_Base_Registers *)hdma->StreamBaseAddress; + tmpisr = regs->ISR; + + while(((tmpisr & mask_cpltlevel) == RESET) && ((hdma->ErrorCode & HAL_DMA_ERROR_TE) == RESET)) + { + /* Check for the Timeout (Not applicable in circular mode)*/ + if(Timeout != HAL_MAX_DELAY) + { + if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) + { + /* Update error code */ + hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT; + + /* Process Unlocked */ + __HAL_UNLOCK(hdma); + + /* Change the DMA state */ + hdma->State = HAL_DMA_STATE_READY; + + return HAL_TIMEOUT; + } + } + + /* Get the ISR register value */ + tmpisr = regs->ISR; + + if((tmpisr & (DMA_FLAG_HTIF0_4 << hdma->StreamIndex)) != RESET) + { + /* Clear the Direct Mode error flag */ + regs->IFCR = DMA_FLAG_HTIF0_4 << hdma->StreamIndex; + WIFI_IO1_SET(); + } + + if((tmpisr & (DMA_FLAG_TEIF0_4 << hdma->StreamIndex)) != RESET) + { + /* Update error code */ + hdma->ErrorCode |= HAL_DMA_ERROR_TE; + + /* Clear the transfer error flag */ + regs->IFCR = DMA_FLAG_TEIF0_4 << hdma->StreamIndex; + } + + if((tmpisr & (DMA_FLAG_FEIF0_4 << hdma->StreamIndex)) != RESET) + { + /* Update error code */ + hdma->ErrorCode |= HAL_DMA_ERROR_FE; + + /* Clear the FIFO error flag */ + regs->IFCR = DMA_FLAG_FEIF0_4 << hdma->StreamIndex; + } + + if((tmpisr & (DMA_FLAG_DMEIF0_4 << hdma->StreamIndex)) != RESET) + { + /* Update error code */ + hdma->ErrorCode |= HAL_DMA_ERROR_DME; + + /* Clear the Direct Mode error flag */ + regs->IFCR = DMA_FLAG_DMEIF0_4 << hdma->StreamIndex; + } + } + + if(hdma->ErrorCode != HAL_DMA_ERROR_NONE) + { + if((hdma->ErrorCode & HAL_DMA_ERROR_TE) != RESET) + { + HAL_DMA_Abort(hdma); + + /* Clear the half transfer and transfer complete flags */ + regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex; + + /* Process Unlocked */ + __HAL_UNLOCK(hdma); + + /* Change the DMA state */ + hdma->State= HAL_DMA_STATE_READY; + + return HAL_ERROR; + } + } + + /* Get the level transfer complete flag */ + if(CompleteLevel == HAL_DMA_FULL_TRANSFER) + { + /* Clear the half transfer and transfer complete flags */ + regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex; + + /* Process Unlocked */ + __HAL_UNLOCK(hdma); + + hdma->State = HAL_DMA_STATE_READY; + } + else + { + /* Clear the half transfer and transfer complete flags */ + regs->IFCR = (DMA_FLAG_HTIF0_4) << hdma->StreamIndex; + } + + return status; + } #endif -} + + static void dmaTransmitBegin() { + wifiUsartDMArx.Init.MemInc = DMA_MINC_ENABLE; + if (HAL_DMA_Init((DMA_HandleTypeDef *)&wifiUsartDMArx) != HAL_OK) { + Error_Handler(); + } + if (HAL_DMA_Start(&wifiUsartDMArx, (uint32_t)&(USART1->DR), (uint32_t)WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE)) { + Error_Handler(); + } + USART1->CR1 |= USART_CR1_UE; + + SET_BIT(USART1->CR3, USART_CR3_DMAR); + WIFI_IO1_RESET(); + } + + static int storeRcvData(volatile uint8_t *bufToCpy, int32_t len) { + unsigned char tmpW = wifiDmaRcvFifo.write_cur; + + if (len > UDISKBUFLEN) return 0; + + if (wifiDmaRcvFifo.state[tmpW] == udisk_buf_empty) { + const int timeOut = 2000; //millisecond + dmaTransmitBegin(); + if(HAL_DMA_PollForTransferCustomize(&wifiUsartDMArx, HAL_DMA_FULL_TRANSFER, timeOut) == HAL_OK) { + memcpy((unsigned char *) wifiDmaRcvFifo.bufferAddr[tmpW], (uint8_t *)bufToCpy, len); + wifiDmaRcvFifo.state[tmpW] = udisk_buf_full; + wifiDmaRcvFifo.write_cur = (tmpW + 1) % TRANS_RCV_FIFO_BLOCK_NUM; + return 1; + } + } + return 0; + } + + static void wifi_usart_dma_init() { + #ifdef STM32F1xx + __HAL_RCC_DMA1_CLK_ENABLE(); + wifiUsartDMArx.Instance = DMA1_Channel5; + #else + __HAL_RCC_DMA2_CLK_ENABLE(); + wifiUsartDMArx.Instance = DMA2_Stream2; + wifiUsartDMArx.Init.Channel = DMA_CHANNEL_4; + #endif + wifiUsartDMArx.Init.Direction = DMA_PERIPH_TO_MEMORY; + wifiUsartDMArx.Init.PeriphInc = DMA_PINC_DISABLE; + wifiUsartDMArx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + wifiUsartDMArx.Init.MemDataAlignment = DMA_PDATAALIGN_BYTE; + wifiUsartDMArx.Init.Mode = DMA_NORMAL; + #ifdef STM32F4xx + wifiUsartDMArx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; + #endif + wifiUsartDMArx.Init.MemInc = DMA_MINC_ENABLE; + if (HAL_DMA_Init((DMA_HandleTypeDef *)&wifiUsartDMArx) != HAL_OK) { + Error_Handler(); + } + if (HAL_DMA_Start(&wifiUsartDMArx, (uint32_t)&(USART1->DR), (uint32_t)WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE)) { + Error_Handler(); + } + USART1->CR1 |= USART_CR1_UE; + + SET_BIT(USART1->CR3, USART_CR3_DMAR); /* Enable Rx DMA Request */ + + for (uint8_t i = 0; i < TRANS_RCV_FIFO_BLOCK_NUM; i++) { + wifiDmaRcvFifo.bufferAddr[i] = &bmp_public_buf[1024 * i]; + wifiDmaRcvFifo.state[i] = udisk_buf_empty; + } + + memset(wifiDmaRcvFifo.bufferAddr[0], 0, 1024 * TRANS_RCV_FIFO_BLOCK_NUM); + wifiDmaRcvFifo.read_cur = 0; + wifiDmaRcvFifo.write_cur = 0; + } + + + void esp_port_begin(uint8_t interrupt) { + WifiRxFifo.uart_read_point = 0; + WifiRxFifo.uart_write_point = 0; + + if (interrupt) { + #if ENABLED(MKS_WIFI_MODULE) + WIFISERIAL.end(); + for (uint16_t i = 0; i < 65535; i++) { /*nada*/ } + WIFISERIAL.begin(WIFI_BAUDRATE); + uint32_t serial_connect_timeout = millis() + 1000UL; + while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ } + #endif + } + else { + #if ENABLED(MKS_WIFI_MODULE) + WIFISERIAL.end(); + USART1->CR1 &= ~USART_CR1_RXNEIE; + WIFISERIAL.begin(WIFI_UPLOAD_BAUDRATE); + wifi_usart_dma_init(); + #endif + } + } +#endif // #if ENABLED(MKS_WIFI_MODULE) @@ -458,6 +806,7 @@ int package_to_wifi(WIFI_RET_TYPE type, uint8_t *buf, int len) { return 1; } + #define SEND_OK_TO_WIFI send_to_wifi((uint8_t *)"ok\r\n", strlen("ok\r\n")) int send_to_wifi(uint8_t *buf, int len) { return package_to_wifi(WIFI_TRANS_INF, buf, len); } @@ -747,8 +1096,10 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { if (card.isFileOpen()) { //saved_feedrate_percentage = feedrate_percentage; feedrate_percentage = 100; - planner.flow_percentage[0] = 100; - planner.e_factor[0] = planner.flow_percentage[0] * 0.01f; + #if EXTRUDERS + planner.flow_percentage[0] = 100; + planner.e_factor[0] = planner.flow_percentage[0] * 0.01f; + #endif #if EXTRUDERS == 2 planner.flow_percentage[1] = 100; planner.e_factor[1] = planner.flow_percentage[1] * 0.01f; @@ -1366,6 +1717,7 @@ static void file_fragment_msg_handle(uint8_t * msg, uint16_t msgLen) { lastFragment = frag; if ((frag & (~FRAG_MASK)) != 0) { + wifiDmaRcvFifo.receiveEspData = false; int res = upload_file.write(public_buf, file_writer.write_index); if (res == -1) { upload_file.close(); @@ -1540,9 +1892,16 @@ void stopEspTransfer() { WIFI_IO1_SET(); // disable dma - dma_clear_isr_bits(DMA1, DMA_CH5); - bb_peri_set_bit(&USART1_BASE->CR3, USART_CR3_DMAR_BIT, 0); - dma_disable(DMA1, DMA_CH5); + #ifdef __STM32F1__ + dma_clear_isr_bits(DMA1, DMA_CH5); + bb_peri_set_bit(&USART1_BASE->CR3, USART_CR3_DMAR_BIT, 0); + dma_disable(DMA1, DMA_CH5); + #else + // First, abort any running dma + HAL_DMA_Abort(&wifiUsartDMArx); + // DeInit objects + HAL_DMA_DeInit(&wifiUsartDMArx); + #endif wifi_delay(200); changeFlashMode(true); // Set SPI flash to use DMA mode @@ -1571,6 +1930,9 @@ void wifi_rcv_handle() { } } #else + #ifndef __STM32F1__ + if (wifiDmaRcvFifo.receiveEspData) storeRcvData(WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE); + #endif len = readWifiFifo(ucStr, UART_RX_BUFFER_SIZE); #endif if (len > 0) { @@ -1582,15 +1944,16 @@ void wifi_rcv_handle() { } getDataF = 1; } - if (esp_state == TRANSFER_STORE) { - if (storeRcvData(WIFISERIAL.usart_device->rb->buf, UART_RX_BUFFER_SIZE)) { - esp_state = TRANSFERING; - esp_dma_pre(); - if (wifiTransError.flag != 0x1) WIFI_IO1_RESET(); + #ifdef __STM32F1__ + if (esp_state == TRANSFER_STORE) { + if (storeRcvData(WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE)) { + esp_state = TRANSFERING; + esp_dma_pre(); + if (wifiTransError.flag != 0x1) WIFI_IO1_RESET(); + } + else WIFI_IO1_SET(); } - else - WIFI_IO1_SET(); - } + #endif } else { len = readWifiBuf((int8_t *)ucStr, UART_RX_BUFFER_SIZE); @@ -1602,6 +1965,10 @@ void wifi_rcv_handle() { esp_port_begin(0); wifi_delay(10); tick_net_time1 = 0; + #ifndef __STM32F1__ + wifiDmaRcvFifo.receiveEspData = true; + return; + #endif } if (wifiTransError.flag != 0x1) WIFI_IO1_RESET(); getDataF = 1; @@ -1664,7 +2031,7 @@ void mks_esp_wifi_init() { esp_state = TRANSFER_IDLE; esp_port_begin(1); - + watchdog_refresh(); wifi_reset(); #if 0 @@ -1723,13 +2090,14 @@ void mks_esp_wifi_init() { } void mks_wifi_firmware_update() { + watchdog_refresh(); card.openFileRead((char *)ESP_FIRMWARE_FILE); if (card.isFileOpen()) { card.closefile(); wifi_delay(2000); - + watchdog_refresh(); if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) return; @@ -1753,12 +2121,6 @@ void mks_wifi_firmware_update() { } } -#define BUF_INC_POINTER(p) ((p + 1 == UART_FIFO_BUFFER_SIZE) ? 0:(p + 1)) - -int usartFifoAvailable(SZ_USART_FIFO *fifo) { - return WIFISERIAL.available(); -} - void get_wifi_commands() { static char wifi_line_buffer[MAX_CMD_SIZE]; static bool wifi_comment_mode = false; @@ -1836,4 +2198,7 @@ int readWifiBuf(int8_t *buf, int32_t len) { return i; } +int usartFifoAvailable(SZ_USART_FIFO *fifo) { + return WIFISERIAL.available(); +} #endif // HAS_TFT_LVGL_UI && MKS_WIFI_MODULE diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_module.h b/Marlin/src/lcd/extui/mks_ui/wifi_module.h index 8f6461341799..709d3d17190f 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_module.h +++ b/Marlin/src/lcd/extui/mks_ui/wifi_module.h @@ -64,6 +64,7 @@ typedef enum{ #define TRANS_RCV_FIFO_BLOCK_NUM 14 typedef struct { + bool receiveEspData; unsigned char *bufferAddr[TRANS_RCV_FIFO_BLOCK_NUM]; unsigned char *p; UDISK_DATA_BUFFER_STATE state[TRANS_RCV_FIFO_BLOCK_NUM]; @@ -191,6 +192,9 @@ void get_wifi_list_command_send(); void get_wifi_commands(); int readWifiBuf(int8_t *buf, int32_t len); void mks_wifi_firmware_update(); +int usartFifoAvailable(SZ_USART_FIFO *fifo); +int readUsartFifo(SZ_USART_FIFO *fifo, int8_t *buf, int32_t len); +void esp_port_begin(uint8_t interrupt); #ifdef __cplusplus } /* C-declarations for C++ */ diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp b/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp index 4a8e473a5cca..16f69edd78c1 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp @@ -40,7 +40,6 @@ extern SZ_USART_FIFO WifiRxFifo; extern int readUsartFifo(SZ_USART_FIFO *fifo, int8_t *buf, int32_t len); extern int writeUsartFifo(SZ_USART_FIFO * fifo, int8_t * buf, int32_t len); void esp_port_begin(uint8_t interrupt); -extern int usartFifoAvailable(SZ_USART_FIFO *fifo); void wifi_delay(int n); #define ARRAY_SIZE(a) sizeof(a) / sizeof((a)[0]) @@ -280,7 +279,7 @@ EspUploadResult readPacket(uint8_t op, uint32_t *valp, size_t *bodyLen, uint32_t switch (state) { case begin: // expecting frame start c = uploadPort_read(); - if (c == (uint8_t)0xC0) break; + if (c != (uint8_t)0xC0) break; state = header; needBytes = 2; break; diff --git a/Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h b/Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h index 1146582a5b06..4ef77cfe3172 100644 --- a/Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h +++ b/Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h @@ -212,6 +212,13 @@ #define SDCARD_CONNECTION ONBOARD #endif +// MKS WIFI MODULE +#if ENABLED(MKS_WIFI_MODULE) + #define WIFI_IO0_PIN PC13 // MKS ESP WIFI IO0 PIN + #define WIFI_IO1_PIN PC7 // MKS ESP WIFI IO1 PIN + #define WIFI_RESET_PIN PE9 // MKS ESP WIFI RESET PIN +#endif + // // Onboard SD card // From 8975478f718a6d3b7544d09d9e35567a71b68191 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 11 Jun 2021 17:17:51 -0500 Subject: [PATCH 02/16] Update irq_overrid.cpp --- Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp b/Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp index 6822e6823284..f3c87c03c4b5 100644 --- a/Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp +++ b/Marlin/src/lcd/extui/mks_ui/irq_overrid.cpp @@ -19,16 +19,14 @@ * along with this program. If not, see . * */ +#ifdef __STM32F1__ + #include "../../../inc/MarlinConfigPre.h" -#if HAS_TFT_LVGL_UI +#if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE) #include "tft_lvgl_configuration.h" -#ifdef __STM32F1__ - -#if ENABLED(MKS_WIFI_MODULE) - #include "draw_ui.h" #include "wifiSerial.h" @@ -48,16 +46,15 @@ #define WIFI_IO1_RESET() WRITE(WIFI_IO1_PIN, LOW); void __irq_usart1() { - if ((USART1_BASE->CR1 & USART_CR1_RXNEIE) && (USART1_BASE->SR & USART_SR_RXNE)) - WRITE(WIFI_IO1_PIN, HIGH); + if ((USART1_BASE->CR1 & USART_CR1_RXNEIE) && (USART1_BASE->SR & USART_SR_RXNE)) + WRITE(WIFI_IO1_PIN, HIGH); - WIFISERIAL.wifi_usart_irq(USART1_BASE); + WIFISERIAL.wifi_usart_irq(USART1_BASE); } #ifdef __cplusplus } /* C-declarations for C++ */ #endif -#endif // MKS_WIFI_MODULE +#endif // HAS_TFT_LVGL_UI && MKS_WIFI_MODULE #endif // __STM32F1__ -#endif // HAS_TFT_LVGL_UI From 5708db536eb9cc4c6268476a5b7284566520b16d Mon Sep 17 00:00:00 2001 From: MKS-Sean <173664774@qq.com> Date: Sat, 12 Jun 2021 14:32:47 +0800 Subject: [PATCH 03/16] Fix the header file path --- Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index 4ddc45c42ebb..25e6ec314627 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -46,11 +46,11 @@ XPT2046 touch; #endif #if HAS_SERVOS - #include "../../../../module/servo.h" + #include "../../../module/servo.h" #endif #if EITHER(PROBE_TARE, HAS_Z_SERVO_PROBE) - #include "../../../../module/probe.h" + #include "../../../module/probe.h" #endif #if ENABLED(TOUCH_SCREEN_CALIBRATION) From 321a2fa8b716e8c4f27c74b47cc0c4483db01a5c Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 13 Jun 2021 16:39:38 -0500 Subject: [PATCH 04/16] Update printer_operation.cpp --- Marlin/src/lcd/extui/mks_ui/printer_operation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp b/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp index 46994da3a6ce..87269207bd31 100644 --- a/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp +++ b/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp @@ -158,7 +158,7 @@ void filament_pin_setup() { } void filament_check() { - #if (PIN_EXISTS(MT_DET_1) || PIN_EXISTS(MT_DET_2) || PIN_EXISTS(MT_DET_3)) + #if ANY_PIN(MT_DET_1, MT_DET_2, MT_DET_3) const int FIL_DELAY = 20; #endif #if PIN_EXISTS(MT_DET_1) From 523f2db95f937b2226431bf4ae2a80582ad5c210 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 13 Jun 2021 17:30:38 -0500 Subject: [PATCH 05/16] split up wifiserial --- Marlin/src/core/macros.h | 2 +- Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp | 526 --------------- Marlin/src/lcd/extui/mks_ui/wifiSerial.h | 7 +- .../src/lcd/extui/mks_ui/wifiSerial_STM32.cpp | 352 ++++++++++ .../lcd/extui/mks_ui/wifiSerial_STM32F1.cpp | 141 ++++ Marlin/src/lcd/extui/mks_ui/wifi_module.cpp | 621 +++++++----------- 6 files changed, 742 insertions(+), 907 deletions(-) delete mode 100644 Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp create mode 100644 Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.cpp create mode 100644 Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32F1.cpp diff --git a/Marlin/src/core/macros.h b/Marlin/src/core/macros.h index 295eee9bcfa8..93364ff7201b 100644 --- a/Marlin/src/core/macros.h +++ b/Marlin/src/core/macros.h @@ -442,7 +442,7 @@ return contains(str, '/') ? findLastPos(findStringEnd(str), '/') : str; } - // Find the first occurence of a character in a string (or return the last position in the string) + // Find the first occurrence of a character in a string (or return the last position in the string) constexpr const char* findFirst(const char *str, const char ch) { return *str == ch || *str == 0 ? (str + 1) : findFirst(str + 1, ch); } diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp b/Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp deleted file mode 100644 index 0af9476d785e..000000000000 --- a/Marlin/src/lcd/extui/mks_ui/wifiSerial.cpp +++ /dev/null @@ -1,526 +0,0 @@ -/** - * Marlin 3D Printer Firmware - * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] - * - * Based on Sprinter and grbl. - * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm - * - * This program 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. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ -#include "../../../inc/MarlinConfigPre.h" - -#if HAS_TFT_LVGL_UI - -#include "tft_lvgl_configuration.h" - -#if ENABLED(MKS_WIFI_MODULE) - -#include "draw_ui.h" -#include "wifiSerial.h" - -#ifdef __STM32F1__ - - #include - #include - #include - #include - #include - - #include "../../../MarlinCore.h" - - DEFINE_WFSERIAL(WifiSerial1, 1); - - WifiSerial::WifiSerial(usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin) { - this->usart_device = usart_device; - this->tx_pin = tx_pin; - this->rx_pin = rx_pin; - } - - /** - * Set up / tear down - */ - #if STM32_MCU_SERIES == STM32_SERIES_F1 - /* F1 MCUs have no GPIO_AFR[HL], so turn off PWM if there's a conflict - * on this GPIO bit. */ - static void disable_timer_if_necessary(timer_dev *dev, uint8 ch) { - if (dev) timer_set_mode(dev, ch, TIMER_DISABLED); - } - static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) { - if (with_irq) usart_enable(usart_device); - else { - usart_reg_map *regs = usart_device->regs; - regs->CR1 |= (USART_CR1_TE | USART_CR1_RE);// don't change the word length etc, and 'or' in the patten not overwrite |USART_CR1_M_8N1); - regs->CR1 |= USART_CR1_UE; - } - } - - #elif STM32_MCU_SERIES == STM32_SERIES_F2 || STM32_MCU_SERIES == STM32_SERIES_F4 - #define disable_timer_if_necessary(dev, ch) ((void)0) - - static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) { - if (with_irq) usart_enable(usart_device); - else { - usart_reg_map *regs = usart_device->regs; - regs->CR1 |= (USART_CR1_TE | USART_CR1_RE);// don't change the word length etc, and 'or' in the patten not overwrite |USART_CR1_M_8N1); - regs->CR1 |= USART_CR1_UE; - } - } - #else - #warning "Unsupported STM32 series; timer conflicts are possible" - #define usart_enable_no_irq(X, Y) usart_enable(X) - #endif - - void WifiSerial::begin(uint32 baud) { begin(baud, SERIAL_8N1); } - - /** - * Roger Clark. - * Note. The config parameter is not currently used. This is a work in progress. - * Code needs to be written to set the config of the hardware serial control register in question. - */ - - void WifiSerial::begin(uint32 baud, uint8_t config) { - //ASSERT(baud <= this->usart_device->max_baud); // Roger Clark. Assert doesn't do anything useful, we may as well save the space in flash and ram etc - - if (baud > this->usart_device->max_baud) return; - - const stm32_pin_info *txi = &PIN_MAP[this->tx_pin], - *rxi = &PIN_MAP[this->rx_pin]; - - disable_timer_if_necessary(txi->timer_device, txi->timer_channel); - - usart_init(this->usart_device); - - // Reinitialize the receive buffer, mks_esp8266 fixed data frame length is 1k bytes - rb_init(this->usart_device->rb, WIFI_RX_BUF_SIZE, wifiRxBuf); - - usart_config_gpios_async(this->usart_device, - rxi->gpio_device, rxi->gpio_bit, - txi->gpio_device, txi->gpio_bit, - config); - usart_set_baud_rate(this->usart_device, USART_USE_PCLK, baud); - usart_enable_no_irq(this->usart_device, baud == WIFI_BAUDRATE); - } - - void WifiSerial::end() { - usart_disable(this->usart_device); - } - - int WifiSerial::available() { - return usart_data_available(this->usart_device); - } - - // - // I/O - // - - int WifiSerial::read() { - if (usart_data_available(usart_device) <= 0) return -1; - return usart_getc(usart_device); - } - - int WifiSerial::write(unsigned char ch) { - usart_putc(this->usart_device, ch); - return 1; - } - - int WifiSerial::wifi_rb_is_full() { - return rb_is_full(this->usart_device->rb); - } - -#else - - WifiSerial WifiSerial1(USART1); - - void WifiSerial::setRx(uint32_t _rx) - { - _serial.pin_rx = digitalPinToPinName(_rx); - } - - void WifiSerial::setTx(uint32_t _tx) - { - _serial.pin_tx = digitalPinToPinName(_tx); - } - - void WifiSerial::setRx(PinName _rx) - { - _serial.pin_rx = _rx; - } - - void WifiSerial::setTx(PinName _tx) - { - _serial.pin_tx = _tx; - } - - void WifiSerial::init(PinName _rx, PinName _tx) - { - if (_rx == _tx) { - _serial.pin_rx = NC; - } else { - _serial.pin_rx = _rx; - } - _serial.pin_tx = _tx; - _serial.rx_buff = wifiRxBuf; - _serial.rx_head = 0; - _serial.rx_tail = 0; - _serial.tx_buff = wifiTxBuf; - _serial.tx_head = 0; - _serial.tx_tail = 0; - } - - WifiSerial::WifiSerial(void *peripheral) - { - // If PIN_SERIALy_RX is not defined assume half-duplex - _serial.pin_rx = NC; - // If Serial is defined in variant set - // the Rx/Tx pins for com port if defined - #if defined(Serial) && defined(PIN_SERIAL_TX) - if ((void *)this == (void *)&Serial) { - #if defined(PIN_SERIAL_RX) - setRx(PIN_SERIAL_RX); - #endif - setTx(PIN_SERIAL_TX); - } else - #endif - #if defined(PIN_SERIAL1_TX) && defined(USART1_BASE) - if (peripheral == USART1) { - #if defined(PIN_SERIAL1_RX) - setRx(PIN_SERIAL1_RX); - #endif - setTx(PIN_SERIAL1_TX); - } else - #endif - #if defined(PIN_SERIAL2_TX) && defined(USART2_BASE) - if (peripheral == USART2) { - #if defined(PIN_SERIAL2_RX) - setRx(PIN_SERIAL2_RX); - #endif - setTx(PIN_SERIAL2_TX); - } else - #endif - #if defined(PIN_SERIAL3_TX) && defined(USART3_BASE) - if (peripheral == USART3) { - #if defined(PIN_SERIAL3_RX) - setRx(PIN_SERIAL3_RX); - #endif - setTx(PIN_SERIAL3_TX); - } else - #endif - #if defined(PIN_SERIAL4_TX) &&\ - (defined(USART4_BASE) || defined(UART4_BASE)) - #if defined(USART4_BASE) - if (peripheral == USART4) - #elif defined(UART4_BASE) - if (peripheral == UART4) - #endif - { - #if defined(PIN_SERIAL4_RX) - setRx(PIN_SERIAL4_RX); - #endif - setTx(PIN_SERIAL4_TX); - } else - #endif - #if defined(PIN_SERIAL5_TX) &&\ - (defined(USART5_BASE) || defined(UART5_BASE)) - #if defined(USART5_BASE) - if (peripheral == USART5) - #elif defined(UART5_BASE) - if (peripheral == UART5) - #endif - { - #if defined(PIN_SERIAL5_RX) - setRx(PIN_SERIAL5_RX); - #endif - setTx(PIN_SERIAL5_TX); - } else - #endif - #if defined(PIN_SERIAL6_TX) && defined(USART6_BASE) - if (peripheral == USART6) { - #if defined(PIN_SERIAL6_RX) - setRx(PIN_SERIAL6_RX); - #endif - setTx(PIN_SERIAL6_TX); - } else - #endif - #if defined(PIN_SERIAL7_TX) &&\ - (defined(USART7_BASE) || defined(UART7_BASE)) - #if defined(USART7_BASE) - if (peripheral == USART7) - #elif defined(UART7_BASE) - if (peripheral == UART7) - #endif - { - #if defined(PIN_SERIAL7_RX) - setRx(PIN_SERIAL7_RX); - #endif - setTx(PIN_SERIAL7_TX); - } else - #endif - #if defined(PIN_SERIAL8_TX) &&\ - (defined(USART8_BASE) || defined(UART8_BASE)) - #if defined(USART8_BASE) - if (peripheral == USART8) - #elif defined(UART8_BASE) - if (peripheral == UART8) - #endif - { - #if defined(PIN_SERIAL8_RX) - setRx(PIN_SERIAL8_RX); - #endif - setTx(PIN_SERIAL8_TX); - } else - #endif - #if defined(PIN_SERIAL9_TX) && defined(UART9_BASE) - if (peripheral == UART9) { - #if defined(PIN_SERIAL9_RX) - setRx(PIN_SERIAL9_RX); - #endif - setTx(PIN_SERIAL9_TX); - } else - #endif - #if defined(PIN_SERIAL10_TX) &&\ - (defined(USART10_BASE) || defined(UART10_BASE)) - #if defined(USART10_BASE) - if (peripheral == USART10) - #elif defined(UART10_BASE) - if (peripheral == UART10) - #endif - { - #if defined(PIN_SERIAL10_RX) - setRx(PIN_SERIAL10_RX); - #endif - setTx(PIN_SERIAL10_TX); - } else - #endif - #if defined(PIN_SERIALLP1_TX) && defined(LPUART1_BASE) - if (peripheral == LPUART1) { - #if defined(PIN_SERIALLP1_RX) - setRx(PIN_SERIALLP1_RX); - #endif - setTx(PIN_SERIALLP1_TX); - } else - #endif - // else get the pins of the first peripheral occurence in PinMap - { - _serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX); - _serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX); - } - // if (halfDuplex == HALF_DUPLEX_ENABLED) { - // _serial.pin_rx = NC; - // } - init(_serial.pin_rx, _serial.pin_tx); - } - - void WifiSerial::flush() - { - // If we have never written a byte, no need to flush. This special - // case is needed since there is no way to force the TXC (transmit - // complete) bit to 1 during initialization - if (!_written) { - return; - } - - while ((_serial.tx_head != _serial.tx_tail)) { - // nop, the interrupt handler will free up space for us - } - // If we get here, nothing is queued anymore (DRIE is disabled) and - // the hardware finished tranmission (TXC is set). - } - - bool WifiSerial::isHalfDuplex(void) const - { - return _serial.pin_rx == NC; - } - - void WifiSerial::enableHalfDuplexRx(void) - { - if (isHalfDuplex()) { - // In half-duplex mode we have to wait for all TX characters to - // be transmitted before we can receive data. - flush(); - if (!_rx_enabled) { - _rx_enabled = true; - uart_enable_rx(&_serial); - } - } - } - - // Actual interrupt handlers ////////////////////////////////////////////////////////////// - - void WifiSerial::_rx_complete_irq(serial_t *obj) - { - // No Parity error, read byte and store it in the buffer if there is room - unsigned char c; - - if (uart_getc(obj, &c) == 0) { - - WRITE(WIFI_IO1_PIN, HIGH); - - rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % WIFI_RX_BUF_SIZE; - - // if we should be storing the received character into the location - // just before the tail (meaning that the head would advance to the - // current location of the tail), we're about to overflow the buffer - // and so we don't write the character or advance the head. - if (i != obj->rx_tail) { - obj->rx_buff[obj->rx_head] = c; - obj->rx_head = i; - } - } - } - - // Actual interrupt handlers ////////////////////////////////////////////////////////////// - - int WifiSerial::_tx_complete_irq(serial_t *obj) - { - // If interrupts are enabled, there must be more data in the output - // buffer. Send the next byte - obj->tx_tail = (obj->tx_tail + 1) % WIFI_TX_BUF_SIZE; - - if (obj->tx_head == obj->tx_tail) { - return -1; - } - - return 0; - } - - void WifiSerial::begin(unsigned long baud) { begin(baud, SERIAL_8N1); } - - void WifiSerial::begin(unsigned long baud, byte config) { - uint32_t databits = 0; - uint32_t stopbits = 0; - uint32_t parity = 0; - - _baud = baud; - _config = config; - - // Manage databits - switch (config & 0x07) { - case 0x02: - databits = 6; - break; - case 0x04: - databits = 7; - break; - case 0x06: - databits = 8; - break; - default: - databits = 0; - break; - } - - if ((config & 0x30) == 0x30) { - parity = UART_PARITY_ODD; - databits++; - } else if ((config & 0x20) == 0x20) { - parity = UART_PARITY_EVEN; - databits++; - } else { - parity = UART_PARITY_NONE; - } - - if ((config & 0x08) == 0x08) { - stopbits = UART_STOPBITS_2; - } else { - stopbits = UART_STOPBITS_1; - } - - switch (databits) { - #ifdef UART_WORDLENGTH_7B - case 7: - databits = UART_WORDLENGTH_7B; - break; - #endif - case 8: - databits = UART_WORDLENGTH_8B; - break; - case 9: - databits = UART_WORDLENGTH_9B; - break; - default: - case 0: - Error_Handler(); - break; - } - - uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits); - enableHalfDuplexRx(); - if (baud == WIFI_BAUDRATE) uart_attach_rx_callback(&_serial, _rx_complete_irq); - else { - USART1->CR1 |= (USART_CR1_RE);// don't change the word length etc, and 'or' in the patten not overwrite |USART_CR1_M_8N1); - } - } - - void WifiSerial::end(void) - { - // wait for transmission of outgoing data - flush(); - - uart_deinit(&_serial); - - // clear any received data - _serial.rx_head = _serial.rx_tail; - } - - int WifiSerial::available(void) { - return ((unsigned int)(WIFI_RX_BUF_SIZE + _serial.rx_head - _serial.rx_tail)) % WIFI_RX_BUF_SIZE; - } - - // - // I/O - // - int WifiSerial::read(void) - { - enableHalfDuplexRx(); - // if the head isn't ahead of the tail, we don't have any characters - if (_serial.rx_head == _serial.rx_tail) { - return -1; - } else { - unsigned char c = _serial.rx_buff[_serial.rx_tail]; - _serial.rx_tail = (rx_buffer_index_t)(_serial.rx_tail + 1) % WIFI_RX_BUF_SIZE; - return c; - } - } - - int WifiSerial::write(uint8_t c) - { - _written = true; - if (isHalfDuplex()) { - if (_rx_enabled) { - _rx_enabled = false; - uart_enable_tx(&_serial); - } - } - - tx_buffer_index_t i = (_serial.tx_head + 1) % WIFI_TX_BUF_SIZE; - - // If the output buffer is full, there's nothing for it other than to - // wait for the interrupt handler to empty it a bit - while (i == _serial.tx_tail) { - // nop, the interrupt handler will free up space for us - } - - _serial.tx_buff[_serial.tx_head] = c; - _serial.tx_head = i; - - if (!serial_tx_active(&_serial)) { - uart_attach_tx_callback(&_serial, _tx_complete_irq); - } - - return 1; - } -#endif // __STM32F1__ -#endif // MKS_WIFI_MODULE -#endif // HAS_TFT_LVGL_UI diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial.h b/Marlin/src/lcd/extui/mks_ui/wifiSerial.h index f6d41d45bdb5..3fcc741b4af5 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifiSerial.h +++ b/Marlin/src/lcd/extui/mks_ui/wifiSerial.h @@ -97,7 +97,8 @@ }; extern WifiSerial WifiSerial1; #define WIFISERIAL WifiSerial1 -#else + +#else // !__STM32F1__ #include #include "Stream.h" @@ -120,7 +121,7 @@ int available(void); int read(void); int write(uint8_t); - + // Interrupt handlers static int _tx_complete_irq(serial_t *obj); static void _rx_complete_irq(serial_t *obj); @@ -141,5 +142,5 @@ }; extern WifiSerial WifiSerial1; #define WIFISERIAL WifiSerial1 -#endif // __STM32F1__ +#endif // !__STM32F1__ #endif // MKS_WIFI_MODULE diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.cpp b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.cpp new file mode 100644 index 000000000000..36e84b653bf1 --- /dev/null +++ b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.cpp @@ -0,0 +1,352 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) + +#include "../../../inc/MarlinConfigPre.h" + +#if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE) + +#include "tft_lvgl_configuration.h" + +#include "draw_ui.h" +#include "wifiSerial.h" + +WifiSerial WifiSerial1(USART1); + +void WifiSerial::setRx(uint32_t _rx) { _serial.pin_rx = digitalPinToPinName(_rx); } +void WifiSerial::setTx(uint32_t _tx) { _serial.pin_tx = digitalPinToPinName(_tx); } +void WifiSerial::setRx(PinName _rx) { _serial.pin_rx = _rx; } +void WifiSerial::setTx(PinName _tx) { _serial.pin_tx = _tx; } + +void WifiSerial::init(PinName _rx, PinName _tx) { + _serial.pin_rx = (_rx == _tx) ? NC : _rx; + _serial.pin_tx = _tx; + _serial.rx_buff = wifiRxBuf; + _serial.rx_head = 0; + _serial.rx_tail = 0; + _serial.tx_buff = wifiTxBuf; + _serial.tx_head = 0; + _serial.tx_tail = 0; +} + +WifiSerial::WifiSerial(void *peripheral) { + // If PIN_SERIALy_RX is not defined assume half-duplex + _serial.pin_rx = NC; + // If Serial is defined in variant set + // the Rx/Tx pins for com port if defined + #if defined(Serial) && defined(PIN_SERIAL_TX) + if ((void *)this == (void *)&Serial) { + #ifdef PIN_SERIAL_RX + setRx(PIN_SERIAL_RX); + #endif + setTx(PIN_SERIAL_TX); + } else + #endif + #if defined(PIN_SERIAL1_TX) && defined(USART1_BASE) + if (peripheral == USART1) { + #ifdef PIN_SERIAL1_RX + setRx(PIN_SERIAL1_RX); + #endif + setTx(PIN_SERIAL1_TX); + } else + #endif + #if defined(PIN_SERIAL2_TX) && defined(USART2_BASE) + if (peripheral == USART2) { + #ifdef PIN_SERIAL2_RX + setRx(PIN_SERIAL2_RX); + #endif + setTx(PIN_SERIAL2_TX); + } else + #endif + #if defined(PIN_SERIAL3_TX) && defined(USART3_BASE) + if (peripheral == USART3) { + #ifdef PIN_SERIAL3_RX + setRx(PIN_SERIAL3_RX); + #endif + setTx(PIN_SERIAL3_TX); + } else + #endif + #ifdef PIN_SERIAL4_TX + if (false + #ifdef USART4_BASE + || peripheral == USART4 + #elif defined(UART4_BASE) + || peripheral == UART4 + #endif + ) { + #ifdef PIN_SERIAL4_RX + setRx(PIN_SERIAL4_RX); + #endif + setTx(PIN_SERIAL4_TX); + } else + #endif + #ifdef PIN_SERIAL5_TX + if (false + #ifdef USART5_BASE + || peripheral == USART5 + #elif defined(UART5_BASE) + || peripheral == UART5 + #endif + ) { + #ifdef PIN_SERIAL5_RX + setRx(PIN_SERIAL5_RX); + #endif + setTx(PIN_SERIAL5_TX); + } else + #endif + #if defined(PIN_SERIAL6_TX) && defined(USART6_BASE) + if (peripheral == USART6) { + #ifdef PIN_SERIAL6_RX + setRx(PIN_SERIAL6_RX); + #endif + setTx(PIN_SERIAL6_TX); + } else + #endif + #ifdef PIN_SERIAL7_TX + if (false + #ifdef USART7_BASE + || peripheral == USART7 + #elif defined(UART7_BASE) + || peripheral == UART7 + #endif + ) { + #ifdef PIN_SERIAL7_RX + setRx(PIN_SERIAL7_RX); + #endif + setTx(PIN_SERIAL7_TX); + } else + #endif + #ifdef PIN_SERIAL8_TX + if (false + #ifdef USART8_BASE + || peripheral == USART8 + #elif defined(UART8_BASE) + || peripheral == UART8 + #endif + ) { + #ifdef PIN_SERIAL8_RX + setRx(PIN_SERIAL8_RX); + #endif + setTx(PIN_SERIAL8_TX); + } else + #endif + #if defined(PIN_SERIAL9_TX) && defined(UART9_BASE) + if (peripheral == UART9) { + #ifdef PIN_SERIAL9_RX + setRx(PIN_SERIAL9_RX); + #endif + setTx(PIN_SERIAL9_TX); + } else + #endif + #ifdef PIN_SERIAL10_TX + if (false + #ifdef USART10_BASE + || peripheral == USART10 + #elif defined(UART10_BASE) + || peripheral == UART10 + #endif + ) { + #ifdef PIN_SERIAL10_RX + setRx(PIN_SERIAL10_RX); + #endif + setTx(PIN_SERIAL10_TX); + } else + #endif + #if defined(PIN_SERIALLP1_TX) && defined(LPUART1_BASE) + if (peripheral == LPUART1) { + #ifdef PIN_SERIALLP1_RX + setRx(PIN_SERIALLP1_RX); + #endif + setTx(PIN_SERIALLP1_TX); + } else + #endif + // else get the pins of the first peripheral occurrence in PinMap + { + _serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX); + _serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX); + } + //if (halfDuplex == HALF_DUPLEX_ENABLED) _serial.pin_rx = NC; + init(_serial.pin_rx, _serial.pin_tx); +} + +void WifiSerial::flush() { + // If we have never written a byte, no need to flush. This special + // case is needed since there is no way to force the TXC (transmit + // complete) bit to 1 during initialization + if (!_written) return; + + while ((_serial.tx_head != _serial.tx_tail)) { + // nop, the interrupt handler will free up space for us + } + // If we get here, nothing is queued anymore (DRIE is disabled) and + // the hardware finished tranmission (TXC is set). +} + +bool WifiSerial::isHalfDuplex() const { return _serial.pin_rx == NC; } + +void WifiSerial::enableHalfDuplexRx() { + if (isHalfDuplex()) { + // In half-duplex mode we have to wait for all TX characters to + // be transmitted before we can receive data. + flush(); + if (!_rx_enabled) { + _rx_enabled = true; + uart_enable_rx(&_serial); + } + } +} + +// Actual interrupt handlers ////////////////////////////////////////////////////////////// + +void WifiSerial::_rx_complete_irq(serial_t *obj) { + // No Parity error, read byte and store it in the buffer if there is room + unsigned char c; + + if (uart_getc(obj, &c) == 0) { + + WRITE(WIFI_IO1_PIN, HIGH); + + rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % WIFI_RX_BUF_SIZE; + + // if we should be storing the received character into the location + // just before the tail (meaning that the head would advance to the + // current location of the tail), we're about to overflow the buffer + // and so we don't write the character or advance the head. + if (i != obj->rx_tail) { + obj->rx_buff[obj->rx_head] = c; + obj->rx_head = i; + } + } +} + +// Actual interrupt handlers ////////////////////////////////////////////////////////////// + +int WifiSerial::_tx_complete_irq(serial_t *obj) { + // If interrupts are enabled, there must be more data in the output + // buffer. Send the next byte + obj->tx_tail = (obj->tx_tail + 1) % WIFI_TX_BUF_SIZE; + + return (obj->tx_head == obj->tx_tail) ? -1 : 0; +} + +void WifiSerial::begin(unsigned long baud) { begin(baud, SERIAL_8N1); } + +void WifiSerial::begin(unsigned long baud, byte config) { + uint32_t databits = 0, stopbits = 0, parity = 0; + + _baud = baud; + _config = config; + + // Manage databits + switch (config & 0x07) { + case 0x02: databits = 6; break; + case 0x04: databits = 7; break; + case 0x06: databits = 8; break; + default: databits = 0; break; + } + + if ((config & 0x30) == 0x30) { + parity = UART_PARITY_ODD; + databits++; + } + else if ((config & 0x20) == 0x20) { + parity = UART_PARITY_EVEN; + databits++; + } + else + parity = UART_PARITY_NONE; + + stopbits = ((config & 0x08) == 0x08) ? UART_STOPBITS_2 : UART_STOPBITS_1; + + switch (databits) { + #ifdef UART_WORDLENGTH_7B + case 7: databits = UART_WORDLENGTH_7B; break; + #endif + case 8: databits = UART_WORDLENGTH_8B; break; + case 9: databits = UART_WORDLENGTH_9B; break; + default: + case 0: Error_Handler(); break; + } + + uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits); + enableHalfDuplexRx(); + if (baud == WIFI_BAUDRATE) + uart_attach_rx_callback(&_serial, _rx_complete_irq); + else + USART1->CR1 |= USART_CR1_RE; // Preserve word length, etc. Use 'or' to preserve USART_CR1_M_8N1 +} + +void WifiSerial::end() { + // wait for transmission of outgoing data + flush(); + + uart_deinit(&_serial); + + // clear any received data + _serial.rx_head = _serial.rx_tail; +} + +int WifiSerial::available() { + return ((unsigned int)(WIFI_RX_BUF_SIZE + _serial.rx_head - _serial.rx_tail)) % WIFI_RX_BUF_SIZE; +} + +// +// I/O +// +int WifiSerial::read() { + enableHalfDuplexRx(); + // if the head isn't ahead of the tail, we don't have any characters + if (_serial.rx_head == _serial.rx_tail) return -1; + + unsigned char c = _serial.rx_buff[_serial.rx_tail]; + _serial.rx_tail = (rx_buffer_index_t)(_serial.rx_tail + 1) % WIFI_RX_BUF_SIZE; + return c; +} + +int WifiSerial::write(uint8_t c) { + _written = true; + if (isHalfDuplex()) { + if (_rx_enabled) { + _rx_enabled = false; + uart_enable_tx(&_serial); + } + } + + tx_buffer_index_t i = (_serial.tx_head + 1) % WIFI_TX_BUF_SIZE; + + // If the output buffer is full, there's nothing for it other than to + // wait for the interrupt handler to empty it a bit + while (i == _serial.tx_tail) { + // nop, the interrupt handler will free up space for us + } + + _serial.tx_buff[_serial.tx_head] = c; + _serial.tx_head = i; + + if (!serial_tx_active(&_serial)) + uart_attach_tx_callback(&_serial, _tx_complete_irq); + + return 1; +} + +#endif // HAS_TFT_LVGL_UI && MKS_WIFI_MODULE +#endif // !__STM32F1__ diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32F1.cpp b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32F1.cpp new file mode 100644 index 000000000000..75830ce1bd4a --- /dev/null +++ b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32F1.cpp @@ -0,0 +1,141 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#ifdef __STM32F1__ + +#include "../../../inc/MarlinConfigPre.h" + +#if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE) + +#include "tft_lvgl_configuration.h" + +#include "draw_ui.h" +#include "wifiSerial.h" + +#include +#include +#include +#include +#include + +#include "../../../MarlinCore.h" + +DEFINE_WFSERIAL(WifiSerial1, 1); + +WifiSerial::WifiSerial(usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin) { + this->usart_device = usart_device; + this->tx_pin = tx_pin; + this->rx_pin = rx_pin; +} + +/** + * Set up / tear down + */ +#if STM32_MCU_SERIES == STM32_SERIES_F1 + /* F1 MCUs have no GPIO_AFR[HL], so turn off PWM if there's a conflict + * on this GPIO bit. */ + static void disable_timer_if_necessary(timer_dev *dev, uint8 ch) { + if (dev) timer_set_mode(dev, ch, TIMER_DISABLED); + } + static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) { + if (with_irq) usart_enable(usart_device); + else { + usart_reg_map *regs = usart_device->regs; + regs->CR1 |= (USART_CR1_TE | USART_CR1_RE); // Preserve word length, etc. Use 'or' to preserve USART_CR1_M_8N1 + regs->CR1 |= USART_CR1_UE; + } + } + +#elif STM32_MCU_SERIES == STM32_SERIES_F2 || STM32_MCU_SERIES == STM32_SERIES_F4 + #define disable_timer_if_necessary(dev, ch) NOOP + + static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) { + if (with_irq) usart_enable(usart_device); + else { + usart_reg_map *regs = usart_device->regs; + regs->CR1 |= (USART_CR1_TE | USART_CR1_RE); // Preserve word length, etc. Use 'or' to preserve USART_CR1_M_8N1 + regs->CR1 |= USART_CR1_UE; + } + } +#else + #warning "Unsupported STM32 series; timer conflicts are possible" + #define usart_enable_no_irq(X, Y) usart_enable(X) +#endif + +void WifiSerial::begin(uint32 baud) { begin(baud, SERIAL_8N1); } + +/** + * Roger Clark. + * Note. The config parameter is not currently used. This is a work in progress. + * Code needs to be written to set the config of the hardware serial control register in question. + */ + +void WifiSerial::begin(uint32 baud, uint8_t config) { + //ASSERT(baud <= this->usart_device->max_baud); // Roger Clark. Assert doesn't do anything useful, we may as well save the space in flash and ram etc + + if (baud > this->usart_device->max_baud) return; + + const stm32_pin_info *txi = &PIN_MAP[this->tx_pin], + *rxi = &PIN_MAP[this->rx_pin]; + + disable_timer_if_necessary(txi->timer_device, txi->timer_channel); + + usart_init(this->usart_device); + + // Reinitialize the receive buffer, mks_esp8266 fixed data frame length is 1k bytes + rb_init(this->usart_device->rb, WIFI_RX_BUF_SIZE, wifiRxBuf); + + usart_config_gpios_async(this->usart_device, + rxi->gpio_device, rxi->gpio_bit, + txi->gpio_device, txi->gpio_bit, + config); + usart_set_baud_rate(this->usart_device, USART_USE_PCLK, baud); + usart_enable_no_irq(this->usart_device, baud == WIFI_BAUDRATE); +} + +void WifiSerial::end() { + usart_disable(this->usart_device); +} + +int WifiSerial::available() { + return usart_data_available(this->usart_device); +} + +// +// I/O +// + +int WifiSerial::read() { + if (usart_data_available(usart_device) <= 0) return -1; + return usart_getc(usart_device); +} + +int WifiSerial::write(unsigned char ch) { + usart_putc(this->usart_device, ch); + return 1; +} + +int WifiSerial::wifi_rb_is_full() { + return rb_is_full(this->usart_device->rb); +} + +#endif // HAS_TFT_LVGL_UI && MKS_WIFI_MODULE +#endif // __STM32F1__ diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp index f790b7a389b8..9db9f25cf90f 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp @@ -116,15 +116,11 @@ extern bool flash_dma_mode; uint32_t getWifiTick() { return millis(); } uint32_t getWifiTickDiff(int32_t lastTick, int32_t curTick) { - if (lastTick <= curTick) - return (curTick - lastTick) * TICK_CYCLE; - else - return (0xFFFFFFFF - lastTick + curTick) * TICK_CYCLE; + return (lastTick <= curTick ? curTick - lastTick : 0xFFFFFFFF - lastTick + curTick) * TICK_CYCLE; } void wifi_delay(int n) { - uint32_t begin = getWifiTick(); - uint32_t end = begin; + uint32_t begin = getWifiTick(), end = begin; while (getWifiTickDiff(begin, end) < (uint32_t)n) { watchdog_refresh(); end = getWifiTick(); @@ -136,9 +132,7 @@ void wifi_reset() { start = getWifiTick(); now = start; WIFI_RESET(); - while (getWifiTickDiff(start, now) < 500) - now = getWifiTick(); - + while (getWifiTickDiff(start, now) < 500) now = getWifiTick(); WIFI_SET(); } @@ -152,28 +146,21 @@ void mount_file_sys(uint8_t disk_type) { static bool longName2DosName(const char *longName, char *dosName) { uint8_t i = FILENAME_LENGTH; - while (i) - dosName[--i] = '\0'; + while (i) dosName[--i] = '\0'; + while (*longName) { uint8_t c = *longName++; if (c == '.') { // For a dot... - if (i == 0) { - return false; - } - else { - strcat(dosName, ".GCO"); - return dosName[0] != '\0'; - } + if (i == 0) return false; + strcat(dosName, ".GCO"); + return dosName[0] != '\0'; } else { // Fail for illegal characters PGM_P p = PSTR("|<>^+=?/[];,*\"\\"); - while (uint8_t b = pgm_read_byte(p++)) - if (b == c) - return false; - if (c < 0x21 || c == 0x7F) - return false; // Check size, non-printable characters - dosName[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a')); // Uppercase required for 8.3 name + while (uint8_t b = pgm_read_byte(p++)) if (b == c) return false; + if (c < 0x21 || c == 0x7F) return false; // Check size, non-printable characters + dosName[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a')); // Uppercase required for 8.3 name } if (i >= 5) { strcat(dosName, "~1.GCO"); @@ -189,26 +176,27 @@ static bool longName2DosName(const char *longName, char *dosName) { #include #include -#include -#include + #include + #include -#include -#include + #include + #include -#include -#include -#include -#include + #include + #include + #include + #include -void changeFlashMode(const bool dmaMode) { - if (flash_dma_mode != dmaMode) { - flash_dma_mode = dmaMode; - if (!flash_dma_mode) { - dma_disable(DMA1, DMA_CH5); - dma_clear_isr_bits(DMA1, DMA_CH4); + void changeFlashMode(const bool dmaMode) { + if (flash_dma_mode != dmaMode) { + flash_dma_mode = dmaMode; + if (!flash_dma_mode) { + dma_disable(DMA1, DMA_CH5); + dma_clear_isr_bits(DMA1, DMA_CH4); + } } } -} + static int storeRcvData(volatile uint8_t *bufToCpy, int32_t len) { unsigned char tmpW = wifiDmaRcvFifo.write_cur; if (len > UDISKBUFLEN) return 0; @@ -221,18 +209,18 @@ void changeFlashMode(const bool dmaMode) { return 0; } -static void esp_dma_pre() { - dma_channel_reg_map *channel_regs = dma_tube_regs(DMA1, DMA_CH5); + static void esp_dma_pre() { + dma_channel_reg_map *channel_regs = dma_tube_regs(DMA1, DMA_CH5); - CBI32(channel_regs->CCR, 0); - channel_regs->CMAR = (uint32_t)WIFISERIAL.usart_device->rb->buf; - channel_regs->CNDTR = 0x0000; - channel_regs->CNDTR = UART_RX_BUFFER_SIZE; - DMA1->regs->IFCR = 0xF0000; - SBI32(channel_regs->CCR, 0); -} + CBI32(channel_regs->CCR, 0); + channel_regs->CMAR = (uint32_t)WIFISERIAL.usart_device->rb->buf; + channel_regs->CNDTR = 0x0000; + channel_regs->CNDTR = UART_RX_BUFFER_SIZE; + DMA1->regs->IFCR = 0xF0000; + SBI32(channel_regs->CCR, 0); + } -static void dma_ch5_irq_handle() { + static void dma_ch5_irq_handle() { uint8 status_bits = dma_get_isr_bits(DMA1, DMA_CH5); dma_clear_isr_bits(DMA1, DMA_CH5); if (status_bits & 0x8) { @@ -257,58 +245,57 @@ static void dma_ch5_irq_handle() { // DMA transmit half WIFI_IO1_SET(); } -} + } -static void wifi_usart_dma_init() { - dma_init(DMA1); - uint32_t flags = ( DMA_MINC_MODE | DMA_TRNS_CMPLT | DMA_HALF_TRNS | DMA_TRNS_ERR); - dma_xfer_size dma_bit_size = DMA_SIZE_8BITS; - dma_setup_transfer(DMA1, DMA_CH5, &USART1_BASE->DR, dma_bit_size, - (volatile void*)WIFISERIAL.usart_device->rb->buf, dma_bit_size, flags);// Transmit buffer DMA - dma_set_priority(DMA1, DMA_CH5, DMA_PRIORITY_LOW); - dma_attach_interrupt(DMA1, DMA_CH5, &dma_ch5_irq_handle); + static void wifi_usart_dma_init() { + dma_init(DMA1); + uint32_t flags = ( DMA_MINC_MODE | DMA_TRNS_CMPLT | DMA_HALF_TRNS | DMA_TRNS_ERR); + dma_xfer_size dma_bit_size = DMA_SIZE_8BITS; + dma_setup_transfer(DMA1, DMA_CH5, &USART1_BASE->DR, dma_bit_size, + (volatile void*)WIFISERIAL.usart_device->rb->buf, dma_bit_size, flags);// Transmit buffer DMA + dma_set_priority(DMA1, DMA_CH5, DMA_PRIORITY_LOW); + dma_attach_interrupt(DMA1, DMA_CH5, &dma_ch5_irq_handle); - dma_clear_isr_bits(DMA1, DMA_CH5); - dma_set_num_transfers(DMA1, DMA_CH5, UART_RX_BUFFER_SIZE); + dma_clear_isr_bits(DMA1, DMA_CH5); + dma_set_num_transfers(DMA1, DMA_CH5, UART_RX_BUFFER_SIZE); - bb_peri_set_bit(&USART1_BASE->CR3, USART_CR3_DMAR_BIT, 1); - dma_enable(DMA1, DMA_CH5); // enable transmit + bb_peri_set_bit(&USART1_BASE->CR3, USART_CR3_DMAR_BIT, 1); + dma_enable(DMA1, DMA_CH5); // enable transmit - for (uint8_t i = 0; i < TRANS_RCV_FIFO_BLOCK_NUM; i++) { - wifiDmaRcvFifo.bufferAddr[i] = &bmp_public_buf[1024 * i]; - wifiDmaRcvFifo.state[i] = udisk_buf_empty; - } + for (uint8_t i = 0; i < TRANS_RCV_FIFO_BLOCK_NUM; i++) { + wifiDmaRcvFifo.bufferAddr[i] = &bmp_public_buf[1024 * i]; + wifiDmaRcvFifo.state[i] = udisk_buf_empty; + } - memset(wifiDmaRcvFifo.bufferAddr[0], 0, 1024 * TRANS_RCV_FIFO_BLOCK_NUM); - wifiDmaRcvFifo.read_cur = 0; - wifiDmaRcvFifo.write_cur = 0; -} + memset(wifiDmaRcvFifo.bufferAddr[0], 0, 1024 * TRANS_RCV_FIFO_BLOCK_NUM); + wifiDmaRcvFifo.read_cur = 0; + wifiDmaRcvFifo.write_cur = 0; + } void esp_port_begin(uint8_t interrupt) { WifiRxFifo.uart_read_point = 0; WifiRxFifo.uart_write_point = 0; - if (interrupt) { - #if ENABLED(MKS_WIFI_MODULE) + #if ENABLED(MKS_WIFI_MODULE) + if (interrupt) { WIFISERIAL.end(); for (uint16_t i = 0; i < 65535; i++) { /*nada*/ } WIFISERIAL.begin(WIFI_BAUDRATE); millis_t serial_connect_timeout = millis() + 1000UL; while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ } - #endif - } - else { - #if ENABLED(MKS_WIFI_MODULE) + } + else { WIFISERIAL.end(); WIFISERIAL.usart_device->regs->CR1 &= ~USART_CR1_RXNEIE; WIFISERIAL.begin(WIFI_UPLOAD_BAUDRATE); wifi_usart_dma_init(); - #endif - } + } + #endif } -#else + +#else // !__STM32F1__ DMA_HandleTypeDef wifiUsartDMArx; - + void changeFlashMode(const bool dmaMode) { if (flash_dma_mode != dmaMode) { flash_dma_mode = dmaMode; @@ -321,249 +308,163 @@ static void wifi_usart_dma_init() { #ifdef STM32F1xx - HAL_StatusTypeDef HAL_DMA_PollForTransferCustomize(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_t Timeout) - { + HAL_StatusTypeDef HAL_DMA_PollForTransferCustomize(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_t Timeout) { uint32_t temp; uint32_t tickstart = 0U; - if(HAL_DMA_STATE_BUSY != hdma->State) - { - /* no transfer ongoing */ + if (HAL_DMA_STATE_BUSY != hdma->State) { // No transfer ongoing hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER; __HAL_UNLOCK(hdma); return HAL_ERROR; } - /* Polling mode not supported in circular mode */ - if (RESET != (hdma->Instance->CCR & DMA_CCR_CIRC)) - { + // Polling mode not supported in circular mode + if (RESET != (hdma->Instance->CCR & DMA_CCR_CIRC)) { hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED; return HAL_ERROR; } - /* Get the level transfer complete flag */ - if(CompleteLevel == HAL_DMA_FULL_TRANSFER) - { - /* Transfer Complete flag */ - temp = __HAL_DMA_GET_TC_FLAG_INDEX(hdma); - } - else - { - /* Half Transfer Complete flag */ - temp = __HAL_DMA_GET_HT_FLAG_INDEX(hdma); - } + // Get the level transfer complete flag + temp = (CompleteLevel == HAL_DMA_FULL_TRANSFER + ? __HAL_DMA_GET_TC_FLAG_INDEX(hdma) // Transfer Complete flag + : __HAL_DMA_GET_HT_FLAG_INDEX(hdma) // Half Transfer Complete flag + ); - /* Get tick */ + // Get tick tickstart = HAL_GetTick(); - while(__HAL_DMA_GET_FLAG(hdma, temp) == RESET) - { - if((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)) != RESET)) - { - /* Clear the half transfer complete flag */ - __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)); + while (__HAL_DMA_GET_FLAG(hdma, temp) == RESET) { + if ((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)) != RESET)) { + __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)); // Clear the half transfer complete flag WIFI_IO1_SET(); } - - if((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET)) - { - /* When a DMA transfer error occurs */ - /* A hardware clear of its EN bits is performed */ - /* Clear all flags */ - hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << hdma->ChannelIndex); - - /* Update error code */ - SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TE); - /* Change the DMA state */ - hdma->State= HAL_DMA_STATE_READY; - - /* Process Unlocked */ - __HAL_UNLOCK(hdma); + if ((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET)) { + /** + * When a DMA transfer error occurs + * A hardware clear of its EN bits is performed + * Clear all flags + */ + hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << hdma->ChannelIndex); + SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TE); // Update error code + hdma->State= HAL_DMA_STATE_READY; // Change the DMA state + __HAL_UNLOCK(hdma); // Process Unlocked return HAL_ERROR; } - /* Check for the Timeout */ - if(Timeout != HAL_MAX_DELAY) - { - if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout)) - { - /* Update error code */ - SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TIMEOUT); - /* Change the DMA state */ - hdma->State = HAL_DMA_STATE_READY; - - /* Process Unlocked */ - __HAL_UNLOCK(hdma); - - return HAL_ERROR; - } + // Check for the Timeout + if (Timeout != HAL_MAX_DELAY && (!Timeout || (HAL_GetTick() - tickstart) > Timeout)) { + SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TIMEOUT); // Update error code + hdma->State = HAL_DMA_STATE_READY; // Change the DMA state + __HAL_UNLOCK(hdma); // Process Unlocked + return HAL_ERROR; } } - if(CompleteLevel == HAL_DMA_FULL_TRANSFER) - { - /* Clear the transfer complete flag */ + if (CompleteLevel == HAL_DMA_FULL_TRANSFER) { + // Clear the transfer complete flag __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma)); /* The selected Channelx EN bit is cleared (DMA is disabled and - all transfers are complete) */ + all transfers are complete) */ hdma->State = HAL_DMA_STATE_READY; } else - { - /* Clear the half transfer complete flag */ - __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)); - } + __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)); // Clear the half transfer complete flag - /* Process unlocked */ - __HAL_UNLOCK(hdma); + __HAL_UNLOCK(hdma); // Process unlocked return HAL_OK; } - #else - typedef struct - { - __IO uint32_t ISR; /*!< DMA interrupt status register */ + #else // !STM32F1xx + + typedef struct { + __IO uint32_t ISR; //!< DMA interrupt status register __IO uint32_t Reserved0; - __IO uint32_t IFCR; /*!< DMA interrupt flag clear register */ + __IO uint32_t IFCR; //!< DMA interrupt flag clear register } MYDMA_Base_Registers; - HAL_StatusTypeDef HAL_DMA_PollForTransferCustomize(DMA_HandleTypeDef *hdma, HAL_DMA_LevelCompleteTypeDef CompleteLevel, uint32_t Timeout) - { + HAL_StatusTypeDef HAL_DMA_PollForTransferCustomize(DMA_HandleTypeDef *hdma, HAL_DMA_LevelCompleteTypeDef CompleteLevel, uint32_t Timeout) { HAL_StatusTypeDef status = HAL_OK; uint32_t mask_cpltlevel; uint32_t tickstart = HAL_GetTick(); uint32_t tmpisr; - /* calculate DMA base and stream number */ - MYDMA_Base_Registers *regs; + MYDMA_Base_Registers *regs; // Calculate DMA base and stream number - if(HAL_DMA_STATE_BUSY != hdma->State) - { - /* No transfer ongoing */ + if (HAL_DMA_STATE_BUSY != hdma->State) { // No transfer ongoing hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER; __HAL_UNLOCK(hdma); return HAL_ERROR; } - /* Polling mode not supported in circular mode and double buffering mode */ - if ((hdma->Instance->CR & DMA_SxCR_CIRC) != RESET) - { + // Polling mode not supported in circular mode and double buffering mode + if ((hdma->Instance->CR & DMA_SxCR_CIRC) != RESET) { hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED; return HAL_ERROR; } - /* Get the level transfer complete flag */ - if(CompleteLevel == HAL_DMA_FULL_TRANSFER) - { - /* Transfer Complete flag */ - mask_cpltlevel = DMA_FLAG_TCIF0_4 << hdma->StreamIndex; - } - else - { - /* Half Transfer Complete flag */ - mask_cpltlevel = DMA_FLAG_HTIF0_4 << hdma->StreamIndex; - } + // Get the level transfer complete flag + mask_cpltlevel = (CompleteLevel == HAL_DMA_FULL_TRANSFER + ? DMA_FLAG_TCIF0_4 << hdma->StreamIndex // Transfer Complete flag + : DMA_FLAG_HTIF0_4 << hdma->StreamIndex // Half Transfer Complete flag + ); regs = (MYDMA_Base_Registers *)hdma->StreamBaseAddress; tmpisr = regs->ISR; - while(((tmpisr & mask_cpltlevel) == RESET) && ((hdma->ErrorCode & HAL_DMA_ERROR_TE) == RESET)) - { - /* Check for the Timeout (Not applicable in circular mode)*/ - if(Timeout != HAL_MAX_DELAY) - { - if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) - { - /* Update error code */ - hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT; - - /* Process Unlocked */ - __HAL_UNLOCK(hdma); - - /* Change the DMA state */ - hdma->State = HAL_DMA_STATE_READY; - + while (((tmpisr & mask_cpltlevel) == RESET) && ((hdma->ErrorCode & HAL_DMA_ERROR_TE) == RESET)) { + // Check for the Timeout (Not applicable in circular mode) + if (Timeout != HAL_MAX_DELAY) { + if (!Timeout || (HAL_GetTick() - tickstart) > Timeout) { + hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT; // Update error code + __HAL_UNLOCK(hdma); // Process Unlocked + hdma->State = HAL_DMA_STATE_READY; // Change the DMA state return HAL_TIMEOUT; } } - /* Get the ISR register value */ - tmpisr = regs->ISR; + tmpisr = regs->ISR; // Get the ISR register value - if((tmpisr & (DMA_FLAG_HTIF0_4 << hdma->StreamIndex)) != RESET) - { - /* Clear the Direct Mode error flag */ - regs->IFCR = DMA_FLAG_HTIF0_4 << hdma->StreamIndex; + if ((tmpisr & (DMA_FLAG_HTIF0_4 << hdma->StreamIndex)) != RESET) { + regs->IFCR = DMA_FLAG_HTIF0_4 << hdma->StreamIndex; // Clear the Direct Mode error flag WIFI_IO1_SET(); } - if((tmpisr & (DMA_FLAG_TEIF0_4 << hdma->StreamIndex)) != RESET) - { - /* Update error code */ - hdma->ErrorCode |= HAL_DMA_ERROR_TE; - - /* Clear the transfer error flag */ - regs->IFCR = DMA_FLAG_TEIF0_4 << hdma->StreamIndex; + if ((tmpisr & (DMA_FLAG_TEIF0_4 << hdma->StreamIndex)) != RESET) { + hdma->ErrorCode |= HAL_DMA_ERROR_TE; // Update error code + regs->IFCR = DMA_FLAG_TEIF0_4 << hdma->StreamIndex; // Clear the transfer error flag } - if((tmpisr & (DMA_FLAG_FEIF0_4 << hdma->StreamIndex)) != RESET) - { - /* Update error code */ - hdma->ErrorCode |= HAL_DMA_ERROR_FE; - - /* Clear the FIFO error flag */ - regs->IFCR = DMA_FLAG_FEIF0_4 << hdma->StreamIndex; + if ((tmpisr & (DMA_FLAG_FEIF0_4 << hdma->StreamIndex)) != RESET) { + hdma->ErrorCode |= HAL_DMA_ERROR_FE; // Update error code + regs->IFCR = DMA_FLAG_FEIF0_4 << hdma->StreamIndex; // Clear the FIFO error flag } - if((tmpisr & (DMA_FLAG_DMEIF0_4 << hdma->StreamIndex)) != RESET) - { - /* Update error code */ - hdma->ErrorCode |= HAL_DMA_ERROR_DME; - - /* Clear the Direct Mode error flag */ - regs->IFCR = DMA_FLAG_DMEIF0_4 << hdma->StreamIndex; + if ((tmpisr & (DMA_FLAG_DMEIF0_4 << hdma->StreamIndex)) != RESET) { + hdma->ErrorCode |= HAL_DMA_ERROR_DME; // Update error code + regs->IFCR = DMA_FLAG_DMEIF0_4 << hdma->StreamIndex; // Clear the Direct Mode error flag } } - if(hdma->ErrorCode != HAL_DMA_ERROR_NONE) - { - if((hdma->ErrorCode & HAL_DMA_ERROR_TE) != RESET) - { - HAL_DMA_Abort(hdma); - - /* Clear the half transfer and transfer complete flags */ - regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex; - - /* Process Unlocked */ - __HAL_UNLOCK(hdma); - - /* Change the DMA state */ - hdma->State= HAL_DMA_STATE_READY; - - return HAL_ERROR; - } + if (hdma->ErrorCode != HAL_DMA_ERROR_NONE && (hdma->ErrorCode & HAL_DMA_ERROR_TE) != RESET) { + HAL_DMA_Abort(hdma); + regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex; // Clear the half transfer and transfer complete flags + __HAL_UNLOCK(hdma); // Process Unlocked + hdma->State= HAL_DMA_STATE_READY; // Change the DMA state + return HAL_ERROR; } - /* Get the level transfer complete flag */ - if(CompleteLevel == HAL_DMA_FULL_TRANSFER) - { - /* Clear the half transfer and transfer complete flags */ - regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex; - - /* Process Unlocked */ - __HAL_UNLOCK(hdma); - + // Get the level transfer complete flag + if (CompleteLevel == HAL_DMA_FULL_TRANSFER) { + regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex; // Clear the half transfer and transfer complete flags + __HAL_UNLOCK(hdma); // Process Unlocked hdma->State = HAL_DMA_STATE_READY; } else - { - /* Clear the half transfer and transfer complete flags */ - regs->IFCR = (DMA_FLAG_HTIF0_4) << hdma->StreamIndex; - } + regs->IFCR = (DMA_FLAG_HTIF0_4) << hdma->StreamIndex; // Clear the half transfer and transfer complete flags return status; } @@ -571,12 +472,12 @@ static void wifi_usart_dma_init() { static void dmaTransmitBegin() { wifiUsartDMArx.Init.MemInc = DMA_MINC_ENABLE; - if (HAL_DMA_Init((DMA_HandleTypeDef *)&wifiUsartDMArx) != HAL_OK) { + if (HAL_DMA_Init((DMA_HandleTypeDef *)&wifiUsartDMArx) != HAL_OK) Error_Handler(); - } - if (HAL_DMA_Start(&wifiUsartDMArx, (uint32_t)&(USART1->DR), (uint32_t)WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE)) { + + if (HAL_DMA_Start(&wifiUsartDMArx, (uint32_t)&(USART1->DR), (uint32_t)WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE)) Error_Handler(); - } + USART1->CR1 |= USART_CR1_UE; SET_BIT(USART1->CR3, USART_CR3_DMAR); @@ -591,7 +492,7 @@ static void wifi_usart_dma_init() { if (wifiDmaRcvFifo.state[tmpW] == udisk_buf_empty) { const int timeOut = 2000; //millisecond dmaTransmitBegin(); - if(HAL_DMA_PollForTransferCustomize(&wifiUsartDMArx, HAL_DMA_FULL_TRANSFER, timeOut) == HAL_OK) { + if (HAL_DMA_PollForTransferCustomize(&wifiUsartDMArx, HAL_DMA_FULL_TRANSFER, timeOut) == HAL_OK) { memcpy((unsigned char *) wifiDmaRcvFifo.bufferAddr[tmpW], (uint8_t *)bufToCpy, len); wifiDmaRcvFifo.state[tmpW] = udisk_buf_full; wifiDmaRcvFifo.write_cur = (tmpW + 1) % TRANS_RCV_FIFO_BLOCK_NUM; @@ -619,15 +520,15 @@ static void wifi_usart_dma_init() { wifiUsartDMArx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; #endif wifiUsartDMArx.Init.MemInc = DMA_MINC_ENABLE; - if (HAL_DMA_Init((DMA_HandleTypeDef *)&wifiUsartDMArx) != HAL_OK) { + if (HAL_DMA_Init((DMA_HandleTypeDef *)&wifiUsartDMArx) != HAL_OK) Error_Handler(); - } - if (HAL_DMA_Start(&wifiUsartDMArx, (uint32_t)&(USART1->DR), (uint32_t)WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE)) { + + if (HAL_DMA_Start(&wifiUsartDMArx, (uint32_t)&(USART1->DR), (uint32_t)WIFISERIAL.wifiRxBuf, UART_RX_BUFFER_SIZE)) Error_Handler(); - } + USART1->CR1 |= USART_CR1_UE; - SET_BIT(USART1->CR3, USART_CR3_DMAR); /* Enable Rx DMA Request */ + SET_BIT(USART1->CR3, USART_CR3_DMAR); // Enable Rx DMA Request for (uint8_t i = 0; i < TRANS_RCV_FIFO_BLOCK_NUM; i++) { wifiDmaRcvFifo.bufferAddr[i] = &bmp_public_buf[1024 * i]; @@ -639,37 +540,34 @@ static void wifi_usart_dma_init() { wifiDmaRcvFifo.write_cur = 0; } - void esp_port_begin(uint8_t interrupt) { - WifiRxFifo.uart_read_point = 0; - WifiRxFifo.uart_write_point = 0; + WifiRxFifo.uart_read_point = 0; + WifiRxFifo.uart_write_point = 0; + #if ENABLED(MKS_WIFI_MODULE) if (interrupt) { - #if ENABLED(MKS_WIFI_MODULE) - WIFISERIAL.end(); - for (uint16_t i = 0; i < 65535; i++) { /*nada*/ } - WIFISERIAL.begin(WIFI_BAUDRATE); - uint32_t serial_connect_timeout = millis() + 1000UL; - while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ } - #endif + WIFISERIAL.end(); + for (uint16_t i = 0; i < 65535; i++) { /*nada*/ } + WIFISERIAL.begin(WIFI_BAUDRATE); + uint32_t serial_connect_timeout = millis() + 1000UL; + while (PENDING(millis(), serial_connect_timeout)) { /*nada*/ } } else { - #if ENABLED(MKS_WIFI_MODULE) - WIFISERIAL.end(); - USART1->CR1 &= ~USART_CR1_RXNEIE; - WIFISERIAL.begin(WIFI_UPLOAD_BAUDRATE); - wifi_usart_dma_init(); - #endif + WIFISERIAL.end(); + USART1->CR1 &= ~USART_CR1_RXNEIE; + WIFISERIAL.begin(WIFI_UPLOAD_BAUDRATE); + wifi_usart_dma_init(); } + #endif } -#endif // + +#endif // !__STM32F1__ #if ENABLED(MKS_WIFI_MODULE) int raw_send_to_wifi(uint8_t *buf, int len) { if (buf == 0 || len <= 0) return 0; - for (int i = 0; i < len; i++) - WIFISERIAL.write(*(buf + i)); + for (int i = 0; i < len; i++) WIFISERIAL.write(*(buf + i)); return len; } @@ -684,9 +582,9 @@ int package_to_wifi(WIFI_RET_TYPE type, uint8_t *buf, int len) { uint8_t wifi_ret_tail = 0xFC; if (type == WIFI_PARA_SET) { - int data_offset = 4; - int apLen = strlen((const char *)uiCfg.wifi_name); - int keyLen = strlen((const char *)uiCfg.wifi_key); + int data_offset = 4, + apLen = strlen((const char *)uiCfg.wifi_name), + keyLen = strlen((const char *)uiCfg.wifi_key); ZERO(buf_to_wifi); index_to_wifi = 0; @@ -726,9 +624,9 @@ int package_to_wifi(WIFI_RET_TYPE type, uint8_t *buf, int len) { if (buf_to_wifi[index_to_wifi + 3] == '\n') { // mask "wait" "busy" "X:" - if (((buf_to_wifi[4] == 'w') && (buf_to_wifi[5] == 'a') && (buf_to_wifi[6] == 'i') && (buf_to_wifi[7] == 't') ) - || ((buf_to_wifi[4] == 'b') && (buf_to_wifi[5] == 'u') && (buf_to_wifi[6] == 's') && (buf_to_wifi[7] == 'y') ) - || ((buf_to_wifi[4] == 'X') && (buf_to_wifi[5] == ':') ) + if ( ((buf_to_wifi[4] == 'w') && (buf_to_wifi[5] == 'a') && (buf_to_wifi[6] == 'i') && (buf_to_wifi[7] == 't')) + || ((buf_to_wifi[4] == 'b') && (buf_to_wifi[5] == 'u') && (buf_to_wifi[6] == 's') && (buf_to_wifi[7] == 'y')) + || ((buf_to_wifi[4] == 'X') && (buf_to_wifi[5] == ':')) ) { ZERO(buf_to_wifi); index_to_wifi = 0; @@ -879,15 +777,15 @@ int write_to_file(char *buf, int len) { return 0; } -#define ESP_PROTOC_HEAD (uint8_t)0xA5 -#define ESP_PROTOC_TAIL (uint8_t)0xFC +#define ESP_PROTOC_HEAD (uint8_t)0xA5 +#define ESP_PROTOC_TAIL (uint8_t)0xFC -#define ESP_TYPE_NET (uint8_t)0x0 -#define ESP_TYPE_GCODE (uint8_t)0x1 +#define ESP_TYPE_NET (uint8_t)0x0 +#define ESP_TYPE_GCODE (uint8_t)0x1 #define ESP_TYPE_FILE_FIRST (uint8_t)0x2 -#define ESP_TYPE_FILE_FRAGMENT (uint8_t)0x3 +#define ESP_TYPE_FILE_FRAGMENT (uint8_t)0x3 -#define ESP_TYPE_WIFI_LIST (uint8_t)0x4 +#define ESP_TYPE_WIFI_LIST (uint8_t)0x4 uint8_t esp_msg_buf[UART_RX_BUFFER_SIZE] = { 0 }; uint16_t esp_msg_index = 0; @@ -1000,13 +898,10 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { } break; - case 21: - /*init sd card*/ - SEND_OK_TO_WIFI; - break; + case 21: SEND_OK_TO_WIFI; break; // Init SD card case 23: - /*select the file*/ + // Select the file if (uiCfg.print_state == IDLE) { int index = 0; while (tmpStr[index] == ' ') index++; @@ -1137,7 +1032,7 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { break; case 25: - /*pause print file*/ + // Pause print file if (uiCfg.print_state == WORKING) { stop_print_time(); @@ -1157,7 +1052,7 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { break; case 26: - /*stop print file*/ + // Stop print file if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED) || (uiCfg.print_state == REPRINTING)) { stop_print_time(); @@ -1174,7 +1069,7 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { break; case 27: - /*report print rate*/ + // Report print rate if ((uiCfg.print_state == WORKING) || (uiCfg.print_state == PAUSED)|| (uiCfg.print_state == REPRINTING)) { print_rate = uiCfg.totalSend; ZERO(tempBuf); @@ -1184,7 +1079,7 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { break; case 28: - /*begin to transfer file to filesys*/ + // Begin to transfer file to filesys if (uiCfg.print_state == IDLE) { int index = 0; @@ -1238,7 +1133,6 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { sprintf_P(tbuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0)); const int tlen = strlen(tbuf); - sprintf_P(outBuf, PSTR("T:%s"), tbuf); outBuf += 2 + tlen; @@ -1260,7 +1154,7 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { #if HAS_MULTI_HOTEND sprintf_P(outBuf, PSTR("%d /%d"), thermalManager.wholeDegHotend(1), thermalManager.degTargetHotend(1)); #else - strcat_P(outBuf, PSTR("0 /0")); + strcpy_P(outBuf, PSTR("0 /0")); #endif outBuf += 4; @@ -1269,17 +1163,11 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { else { sprintf_P((char *)tempBuf, PSTR("T:%d /%d B:%d /%d T0:%d /%d T1:%d /%d @:0 B@:0\r\n"), thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0), - #if HAS_HEATED_BED - thermalManager.wholeDegBed(), thermalManager.degTargetBed(), - #else - 0, 0, - #endif + TERN0(HAS_HEATED_BED, thermalManager.wholeDegBed()), + TERN0(HAS_HEATED_BED, thermalManager.degTargetBed()), thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0), - #if HAS_MULTI_HOTEND - thermalManager.wholeDegHotend(1), thermalManager.degTargetHotend(1) - #else - 0, 0 - #endif + TERN0(HAS_MULTI_HOTEND, thermalManager.wholeDegHotend(1)), + TERN0(HAS_MULTI_HOTEND, thermalManager.degTargetHotend(1)) ); } @@ -1328,11 +1216,8 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { case 998: if (uiCfg.print_state == IDLE) { - int v = atoi((char *)tmpStr); - if (v == 0) - set_cur_file_sys(0); - else if (v == 1) - set_cur_file_sys(1); + const int v = atoi((char *)tmpStr); + if (v == 0 || v == 1) set_cur_file_sys(v); wifi_ret_ack(); } break; @@ -1347,18 +1232,14 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { strcat_P((char *)cmd_line, PSTR("\n")); if (espGcodeFifo.wait_tick > 5) { - uint32_t left; - if (espGcodeFifo.r > espGcodeFifo.w) - left = espGcodeFifo.r - espGcodeFifo.w - 1; - else - left = WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1; + const uint32_t left = espGcodeFifo.r > espGcodeFifo.w + ? espGcodeFifo.r - espGcodeFifo.w - 1 + : WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1; if (left >= strlen((const char *)cmd_line)) { - uint32_t index = 0; - while (index < strlen((const char *)cmd_line)) { + while (uint32_t index = 0; index < strlen((const char *)cmd_line); index++) { espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ; espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE; - index++; } if (left - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line)) SEND_OK_TO_WIFI; @@ -1373,18 +1254,14 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { strcat_P((char *)cmd_line, PSTR("\n")); if (espGcodeFifo.wait_tick > 5) { - uint32_t left_g; - if (espGcodeFifo.r > espGcodeFifo.w) - left_g = espGcodeFifo.r - espGcodeFifo.w - 1; - else - left_g = WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1; + const uint32_t left_g = espGcodeFifo.r > espGcodeFifo.w + ? espGcodeFifo.r - espGcodeFifo.w - 1 + : WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1; if (left_g >= strlen((const char *)cmd_line)) { - uint32_t index = 0; - while (index < strlen((const char *)cmd_line)) { + for (uint32_t index = 0; index < strlen((const char *)cmd_line); index++) { espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ; espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE; - index++; } if (left_g - WIFI_GCODE_BUFFER_LEAST_SIZE >= strlen((const char *)cmd_line)) SEND_OK_TO_WIFI; @@ -1412,11 +1289,10 @@ static void net_msg_handle(uint8_t * msg, uint16_t msgLen) { if (msgLen <= 0) return; - // ip + // IP address sprintf_P(ipPara.ip_addr, PSTR("%d.%d.%d.%d"), msg[0], msg[1], msg[2], msg[3]); - // port - // connect state + // port connect state switch (msg[6]) { case 0x0A: wifi_link_state = WIFI_CONNECTED; break; case 0x0E: wifi_link_state = WIFI_EXCEPTION; break; @@ -1426,7 +1302,7 @@ static void net_msg_handle(uint8_t * msg, uint16_t msgLen) { // mode wifiPara.mode = msg[7]; - // wifi name + // WiFi name wifiNameLen = msg[8]; wifiKeyLen = msg[9 + wifiNameLen]; if (wifiNameLen < 32) { @@ -1436,7 +1312,7 @@ static void net_msg_handle(uint8_t * msg, uint16_t msgLen) { memset(&wifi_list.wifiConnectedName, 0, sizeof(wifi_list.wifiConnectedName)); memcpy(&wifi_list.wifiConnectedName, &msg[9], wifiNameLen); - // wifi key + // WiFi key if (wifiKeyLen < 64) { ZERO(wifiPara.keyCode); memcpy(wifiPara.keyCode, &msg[10 + wifiNameLen], wifiKeyLen); @@ -1453,7 +1329,7 @@ static void net_msg_handle(uint8_t * msg, uint16_t msgLen) { cloud_para.port = msg[12 + wifiNameLen + wifiKeyLen + hostLen] + (msg[13 + wifiNameLen + wifiKeyLen + hostLen] << 8); } - // id + // ID id_len = msg[14 + wifiNameLen + wifiKeyLen + hostLen]; if (id_len == 20) { ZERO(cloud_para.id); @@ -1476,10 +1352,10 @@ static void net_msg_handle(uint8_t * msg, uint16_t msgLen) { if (cfg_cloud_flag == 1) { if (((cloud_para.state >> 4) != (char)gCfgItems.cloud_enable) || (strncmp(cloud_para.hostUrl, (const char *)uiCfg.cloud_hostUrl, 96) != 0) - || (cloud_para.port != uiCfg.cloud_port)) { - package_to_wifi(WIFI_CLOUD_CFG, (uint8_t *)0, 0); - } - else cfg_cloud_flag = 0; + || (cloud_para.port != uiCfg.cloud_port) + ) package_to_wifi(WIFI_CLOUD_CFG, (uint8_t *)0, 0); + else + cfg_cloud_flag = 0; } } @@ -1979,9 +1855,8 @@ void wifi_rcv_handle() { } } - if (getDataF == 1) { + if (getDataF == 1) tick_net_time1 = getWifiTick(); - } else { tick_net_time2 = getWifiTick(); @@ -2037,53 +1912,47 @@ void mks_esp_wifi_init() { #if 0 if (update_flag == 0) { res = f_open(&esp_upload.uploadFile, ESP_WEB_FIRMWARE_FILE, FA_OPEN_EXISTING | FA_READ); + if (res == FR_OK) { + f_close(&esp_upload.uploadFile); - if (res == FR_OK) { - f_close(&esp_upload.uploadFile); - - wifi_delay(2000); - - if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) { - return; - } + wifi_delay(2000); - clear_cur_ui(); + if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) return; - draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMWARE); - if (wifi_upload(1) >= 0) { + clear_cur_ui(); - f_unlink("1:/MKS_WIFI_CUR"); - f_rename(ESP_WEB_FIRMWARE_FILE,"/MKS_WIFI_CUR"); - } - draw_return_ui(); - update_flag = 1; + draw_dialog(DIALOG_TYPE_UPDATE_ESP_FIRMWARE); + if (wifi_upload(1) >= 0) { + f_unlink("1:/MKS_WIFI_CUR"); + f_rename(ESP_WEB_FIRMWARE_FILE,"/MKS_WIFI_CUR"); } - + draw_return_ui(); + update_flag = 1; } - if (update_flag == 0) { - res = f_open(&esp_upload.uploadFile, ESP_WEB_FILE, FA_OPEN_EXISTING | FA_READ); - if (res == FR_OK) { - f_close(&esp_upload.uploadFile); + } - wifi_delay(2000); + if (update_flag == 0) { + res = f_open(&esp_upload.uploadFile, ESP_WEB_FILE, FA_OPEN_EXISTING | FA_READ); + if (res == FR_OK) { + f_close(&esp_upload.uploadFile); - if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) { - return; - } + wifi_delay(2000); - clear_cur_ui(); + if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) return; - draw_dialog(DIALOG_TYPE_UPDATE_ESP_DATA); + clear_cur_ui(); - if (wifi_upload(2) >= 0) { + draw_dialog(DIALOG_TYPE_UPDATE_ESP_DATA); - f_unlink("1:/MKS_WEB_CONTROL_CUR"); - f_rename(ESP_WEB_FILE,"/MKS_WEB_CONTROL_CUR"); - } - draw_return_ui(); + if (wifi_upload(2) >= 0) { + f_unlink("1:/MKS_WEB_CONTROL_CUR"); + f_rename(ESP_WEB_FILE,"/MKS_WEB_CONTROL_CUR"); } + draw_return_ui(); } + } #endif + wifiPara.decodeType = WIFI_DECODE_TYPE; wifiPara.baud = 115200; wifi_link_state = WIFI_NOT_CONFIG; @@ -2098,8 +1967,7 @@ void mks_wifi_firmware_update() { wifi_delay(2000); watchdog_refresh(); - if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) - return; + if (usartFifoAvailable((SZ_USART_FIFO *)&WifiRxFifo) < 20) return; clear_cur_ui(); @@ -2198,7 +2066,6 @@ int readWifiBuf(int8_t *buf, int32_t len) { return i; } -int usartFifoAvailable(SZ_USART_FIFO *fifo) { - return WIFISERIAL.available(); -} +int usartFifoAvailable(SZ_USART_FIFO *fifo) { return WIFISERIAL.available(); } + #endif // HAS_TFT_LVGL_UI && MKS_WIFI_MODULE From 25a582fba6f55c672d656bacc0e2cba93561b29d Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 14 Jun 2021 03:12:58 -0500 Subject: [PATCH 06/16] Add STM32 test --- buildroot/tests/mks_robin_nano35 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/buildroot/tests/mks_robin_nano35 b/buildroot/tests/mks_robin_nano35 index 08917446927e..99ff2623bb61 100755 --- a/buildroot/tests/mks_robin_nano35 +++ b/buildroot/tests/mks_robin_nano35 @@ -21,8 +21,8 @@ exec_test $1 $2 "MKS Robin nano v1.2 Emulated DOGM FSMC" "$3" use_example_configs Mks/Robin opt_set MOTHERBOARD BOARD_MKS_ROBIN_NANO_V2 opt_disable TFT_INTERFACE_FSMC -opt_enable TFT_INTERFACE_SPI -exec_test $1 $2 "MKS Robin v2 nano Emulated DOGM SPI" "$3" +opt_enable TFT_INTERFACE_SPI MKS_WIFI_MODULE +exec_test $1 $2 "MKS Robin v2 nano Emulated DOGM SPI, MKS_WIFI_MODULE" "$3" # # MKS Robin nano v1.2 LVGL FSMC From 7b91ec33858962b32024260266661294cb6848ca Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 14 Jun 2021 03:13:34 -0500 Subject: [PATCH 07/16] split up headers by platform --- Marlin/src/lcd/extui/mks_ui/wifiSerial.h | 118 ++---------------- .../src/lcd/extui/mks_ui/wifiSerial_STM32.h | 63 ++++++++++ .../src/lcd/extui/mks_ui/wifiSerial_STM32F1.h | 77 ++++++++++++ Marlin/src/lcd/extui/mks_ui/wifi_module.cpp | 2 +- ini/stm32f1-maple.ini | 4 +- 5 files changed, 151 insertions(+), 113 deletions(-) create mode 100644 Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.h create mode 100644 Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32F1.h diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial.h b/Marlin/src/lcd/extui/mks_ui/wifiSerial.h index 3fcc741b4af5..1a3e9aae9b2d 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifiSerial.h +++ b/Marlin/src/lcd/extui/mks_ui/wifiSerial.h @@ -21,10 +21,6 @@ */ #pragma once -#include "tft_lvgl_configuration.h" - -#if ENABLED(MKS_WIFI_MODULE) - #ifdef SERIAL_PORT_2 #error "SERIAL_PORT_2 must be disabled with TFT_LVGL_UI* and MKS_WIFI_MODULE." #endif @@ -36,111 +32,13 @@ #define WIFI_RX_BUF_SIZE (1024) #define WIFI_TX_BUF_SIZE (64) -#ifdef __STM32F1__ - - #include - #include - #include - #include - #include - #include - - #define DEFINE_WFSERIAL(name, n)\ - WifiSerial name(USART##n, \ - BOARD_USART##n##_TX_PIN, \ - BOARD_USART##n##_RX_PIN) - - class WifiSerial { - public: - uint8 wifiRxBuf[WIFI_RX_BUF_SIZE]; - - public: - WifiSerial(struct usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin); - - /* Set up/tear down */ - void begin(uint32 baud); - void begin(uint32 baud,uint8_t config); - void end(); - int available(); - int read(); - int write(uint8_t); - inline void wifi_usart_irq(usart_reg_map *regs) { - /* Handling RXNEIE and TXEIE interrupts. - * RXNE signifies availability of a byte in DR. - * - * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15. - * We enable RXNEIE. - */ - if ((regs->CR1 & USART_CR1_RXNEIE) && (regs->SR & USART_SR_RXNE)) { - #ifdef USART_SAFE_INSERT - /* If the buffer is full and the user defines USART_SAFE_INSERT, - * ignore new bytes. */ - rb_safe_insert(this->usart_device->rb, (uint8)regs->DR); - #else - /* By default, push bytes around in the ring buffer. */ - rb_push_insert(this->usart_device->rb, (uint8)regs->DR); - #endif - } - /* TXE signifies readiness to send a byte to DR. */ - if ((regs->CR1 & USART_CR1_TXEIE) && (regs->SR & USART_SR_TXE)) { - if (!rb_is_empty(this->usart_device->wb)) - regs->DR=rb_remove(this->usart_device->wb); - else - regs->CR1 &= ~((uint32)USART_CR1_TXEIE); // disable TXEIE - } - } - int wifi_rb_is_full(); - struct usart_dev *usart_device; - private: - uint8 tx_pin; - uint8 rx_pin; - }; - extern WifiSerial WifiSerial1; - #define WIFISERIAL WifiSerial1 - -#else // !__STM32F1__ - - #include - #include "Stream.h" - #include "uart.h" - - class WifiSerial { - protected: - // Has any byte been written to the UART since begin() - bool _written; - serial_t _serial; - public: - uint8_t wifiRxBuf[WIFI_RX_BUF_SIZE]; - uint8_t wifiTxBuf[WIFI_TX_BUF_SIZE]; - WifiSerial(void *peripheral); - - /* Set up/tear down */ - void begin(uint32_t baud); - void begin(uint32_t baud,uint8_t config); - void end(); - int available(void); - int read(void); - int write(uint8_t); - - // Interrupt handlers - static int _tx_complete_irq(serial_t *obj); - static void _rx_complete_irq(serial_t *obj); +#include "tft_lvgl_configuration.h" - void flush(void); - bool isHalfDuplex(void) const; - void enableHalfDuplexRx(void); +#ifdef __STM32F1__ + #include "wifiSerial_STM32F1.h" +#else + #include "wifiSerial_STM32.h" +#endif - private: - void setRx(uint32_t _rx); - void setTx(uint32_t _tx); - void setRx(PinName _rx); - void setTx(PinName _tx); - void init(PinName _rx, PinName _tx); - bool _rx_enabled; - uint8_t _config; - unsigned long _baud; - }; - extern WifiSerial WifiSerial1; - #define WIFISERIAL WifiSerial1 -#endif // !__STM32F1__ -#endif // MKS_WIFI_MODULE +extern WifiSerial WifiSerial1; +#define WIFISERIAL WifiSerial1 diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.h b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.h new file mode 100644 index 000000000000..87de27c04439 --- /dev/null +++ b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.h @@ -0,0 +1,63 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +#include +#include "Stream.h" +#include "uart.h" + +class WifiSerial { + protected: + // Has any byte been written to the UART since begin() + bool _written; + serial_t _serial; + public: + uint8_t wifiRxBuf[WIFI_RX_BUF_SIZE]; + uint8_t wifiTxBuf[WIFI_TX_BUF_SIZE]; + WifiSerial(void *peripheral); + + // Set up / tear down + void begin(uint32_t baud); + void begin(uint32_t baud,uint8_t config); + void end(); + int available(void); + int read(void); + int write(uint8_t); + + // Interrupt handlers + static int _tx_complete_irq(serial_t *obj); + static void _rx_complete_irq(serial_t *obj); + + void flush(void); + bool isHalfDuplex(void) const; + void enableHalfDuplexRx(void); + + private: + void setRx(uint32_t _rx); + void setTx(uint32_t _tx); + void setRx(PinName _rx); + void setTx(PinName _tx); + void init(PinName _rx, PinName _tx); + bool _rx_enabled; + uint8_t _config; + unsigned long _baud; +}; diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32F1.h b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32F1.h new file mode 100644 index 000000000000..6af2f9743ba7 --- /dev/null +++ b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32F1.h @@ -0,0 +1,77 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +#include +#include +#include +#include +#include +#include + +#define DEFINE_WFSERIAL(name, n) WifiSerial name(USART##n, BOARD_USART##n##_TX_PIN, BOARD_USART##n##_RX_PIN) + +class WifiSerial { + public: + uint8 wifiRxBuf[WIFI_RX_BUF_SIZE]; + + public: + WifiSerial(struct usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin); + + /* Set up/tear down */ + void begin(uint32 baud); + void begin(uint32 baud,uint8_t config); + void end(); + int available(); + int read(); + int write(uint8_t); + inline void wifi_usart_irq(usart_reg_map *regs) { + /* Handling RXNEIE and TXEIE interrupts. + * RXNE signifies availability of a byte in DR. + * + * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15. + * We enable RXNEIE. + */ + if ((regs->CR1 & USART_CR1_RXNEIE) && (regs->SR & USART_SR_RXNE)) { + #ifdef USART_SAFE_INSERT + /* If the buffer is full and the user defines USART_SAFE_INSERT, + * ignore new bytes. */ + rb_safe_insert(this->usart_device->rb, (uint8)regs->DR); + #else + /* By default, push bytes around in the ring buffer. */ + rb_push_insert(this->usart_device->rb, (uint8)regs->DR); + #endif + } + /* TXE signifies readiness to send a byte to DR. */ + if ((regs->CR1 & USART_CR1_TXEIE) && (regs->SR & USART_SR_TXE)) { + if (!rb_is_empty(this->usart_device->wb)) + regs->DR=rb_remove(this->usart_device->wb); + else + regs->CR1 &= ~((uint32)USART_CR1_TXEIE); // disable TXEIE + } + } + int wifi_rb_is_full(); + struct usart_dev *usart_device; + private: + uint8 tx_pin; + uint8 rx_pin; +}; diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp index 9db9f25cf90f..5b75221163ab 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp @@ -1237,7 +1237,7 @@ static void wifi_gcode_exec(uint8_t *cmd_line) { : WIFI_GCODE_BUFFER_SIZE + espGcodeFifo.r - espGcodeFifo.w - 1; if (left >= strlen((const char *)cmd_line)) { - while (uint32_t index = 0; index < strlen((const char *)cmd_line); index++) { + for (uint32_t index = 0; index < strlen((const char *)cmd_line); index++) { espGcodeFifo.Buffer[espGcodeFifo.w] = cmd_line[index] ; espGcodeFifo.w = (espGcodeFifo.w + 1) % WIFI_GCODE_BUFFER_SIZE; } diff --git a/ini/stm32f1-maple.ini b/ini/stm32f1-maple.ini index 25ebfb64b625..9bf5a47daa7a 100644 --- a/ini/stm32f1-maple.ini +++ b/ini/stm32f1-maple.ini @@ -27,7 +27,7 @@ platform = ststm32@~12.1 board_build.core = maple build_flags = !python Marlin/src/HAL/STM32F1/build_flags.py ${common.build_flags} - -DARDUINO_ARCH_STM32 + -DARDUINO_ARCH_STM32F1 build_unflags = -std=gnu11 -std=gnu++11 src_filter = ${common.default_src_filter} + lib_ignore = SPI, FreeRTOS701, FreeRTOS821 @@ -165,7 +165,7 @@ extends = common_stm32f1 board = genericSTM32F103VE build_flags = ${common_stm32f1.build_flags} -ffunction-sections -fdata-sections -nostdlib -MMD - -DMCU_STM32F103VE -DARDUINO_GENERIC_STM32F103V -DARDUINO_ARCH_STM32F1 -DBOARD_generic_stm32f103v + -DMCU_STM32F103VE -DARDUINO_GENERIC_STM32F103V -DBOARD_generic_stm32f103v -DDEBUG_LEVEL=DEBUG_NONE -DCONFIG_MAPLE_MINI_NO_DISABLE_DEBUG=1 -DVECT_TAB_ADDR=0x8000000 -DERROR_LED_PORT=GPIOE -DERROR_LED_PIN=6 upload_protocol = serial From ee3599d6324fc935340e93629e455f54532dc2b8 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 14 Jun 2021 03:40:15 -0500 Subject: [PATCH 08/16] Indicate Maple target --- Marlin/src/HAL/STM32/HAL.cpp | 4 ++-- Marlin/src/HAL/STM32/HAL_MinSerial.cpp | 2 +- Marlin/src/HAL/STM32/HAL_SPI.cpp | 4 ++-- Marlin/src/HAL/STM32/MarlinSPI.cpp | 4 ++-- Marlin/src/HAL/STM32/MarlinSerial.cpp | 4 ++-- Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp | 4 ++-- Marlin/src/HAL/STM32/Servo.cpp | 4 ++-- Marlin/src/HAL/STM32/eeprom_flash.cpp | 4 ++-- Marlin/src/HAL/STM32/eeprom_sdcard.cpp | 4 ++-- Marlin/src/HAL/STM32/eeprom_sram.cpp | 4 ++-- Marlin/src/HAL/STM32/eeprom_wired.cpp | 4 ++-- Marlin/src/HAL/STM32/fast_pwm.cpp | 4 ++-- Marlin/src/HAL/STM32/fastio.cpp | 2 +- Marlin/src/HAL/STM32/msc_sd.cpp | 14 +++++++------- Marlin/src/HAL/STM32/tft/gt911.cpp | 4 ++-- Marlin/src/HAL/STM32/tft/tft_fsmc.cpp | 4 ++-- Marlin/src/HAL/STM32/tft/tft_ltdc.cpp | 4 ++-- Marlin/src/HAL/STM32/tft/tft_spi.cpp | 4 ++-- Marlin/src/HAL/STM32/tft/xpt2046.cpp | 4 ++-- Marlin/src/HAL/STM32/timers.cpp | 4 ++-- Marlin/src/HAL/STM32/usb_host.cpp | 4 ++-- Marlin/src/HAL/STM32/usb_serial.cpp | 4 ++-- Marlin/src/HAL/STM32/watchdog.cpp | 4 ++-- .../extui/ftdi_eve_touch_ui/ftdi_eve_lib/compat.h | 2 +- Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.cpp | 2 +- ini/stm32f1-maple.ini | 5 ++--- 26 files changed, 53 insertions(+), 54 deletions(-) diff --git a/Marlin/src/HAL/STM32/HAL.cpp b/Marlin/src/HAL/STM32/HAL.cpp index e09b52f7db5a..febd396b5c36 100644 --- a/Marlin/src/HAL/STM32/HAL.cpp +++ b/Marlin/src/HAL/STM32/HAL.cpp @@ -20,7 +20,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "HAL.h" #include "usb_serial.h" @@ -167,4 +167,4 @@ void HAL_SYSTICK_Callback() { if (systick_user_callback) systick_user_callback(); } -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/HAL_MinSerial.cpp b/Marlin/src/HAL/STM32/HAL_MinSerial.cpp index 7268eed5919c..44fb93337d4a 100644 --- a/Marlin/src/HAL/STM32/HAL_MinSerial.cpp +++ b/Marlin/src/HAL/STM32/HAL_MinSerial.cpp @@ -20,7 +20,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfigPre.h" diff --git a/Marlin/src/HAL/STM32/HAL_SPI.cpp b/Marlin/src/HAL/STM32/HAL_SPI.cpp index bd36562de9ca..8a6d24f19436 100644 --- a/Marlin/src/HAL/STM32/HAL_SPI.cpp +++ b/Marlin/src/HAL/STM32/HAL_SPI.cpp @@ -20,7 +20,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" @@ -224,4 +224,4 @@ static SPISettings spiConfig; #endif // SOFTWARE_SPI -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/MarlinSPI.cpp b/Marlin/src/HAL/STM32/MarlinSPI.cpp index 896ec1433f40..26c0e831f5bd 100644 --- a/Marlin/src/HAL/STM32/MarlinSPI.cpp +++ b/Marlin/src/HAL/STM32/MarlinSPI.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(STM32H7xx) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) && !defined(STM32H7xx) #include "MarlinSPI.h" @@ -165,4 +165,4 @@ uint8_t MarlinSPI::dmaSend(const void * transmitBuf, uint16_t length, bool minc) return 1; } -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/MarlinSerial.cpp b/Marlin/src/HAL/STM32/MarlinSerial.cpp index 265e8b5ab61b..307868871672 100644 --- a/Marlin/src/HAL/STM32/MarlinSerial.cpp +++ b/Marlin/src/HAL/STM32/MarlinSerial.cpp @@ -16,7 +16,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" #include "MarlinSerial.h" @@ -101,4 +101,4 @@ void MarlinSerial::_rx_complete_irq(serial_t *obj) { } } -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp b/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp index 3353f8c36b8c..b85ef1ca0e5e 100644 --- a/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp +++ b/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" @@ -343,4 +343,4 @@ #endif // !USBD_USE_CDC_COMPOSITE #endif // SDIO_SUPPORT -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/Servo.cpp b/Marlin/src/HAL/STM32/Servo.cpp index 1cf117a05694..1e17c05f551c 100644 --- a/Marlin/src/HAL/STM32/Servo.cpp +++ b/Marlin/src/HAL/STM32/Servo.cpp @@ -20,7 +20,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" @@ -107,4 +107,4 @@ void libServo::setInterruptPriority(uint32_t preemptPriority, uint32_t subPriori } #endif // HAS_SERVOS -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/eeprom_flash.cpp b/Marlin/src/HAL/STM32/eeprom_flash.cpp index dfeae9e9e5e6..927f0fb2300f 100644 --- a/Marlin/src/HAL/STM32/eeprom_flash.cpp +++ b/Marlin/src/HAL/STM32/eeprom_flash.cpp @@ -20,7 +20,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" @@ -270,4 +270,4 @@ bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t } #endif // FLASH_EEPROM_EMULATION -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/eeprom_sdcard.cpp b/Marlin/src/HAL/STM32/eeprom_sdcard.cpp index f811468fb484..2f72457e4c34 100644 --- a/Marlin/src/HAL/STM32/eeprom_sdcard.cpp +++ b/Marlin/src/HAL/STM32/eeprom_sdcard.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) /** * Implementation of EEPROM settings in SD Card @@ -88,4 +88,4 @@ bool PersistentStore::read_data(int &pos, uint8_t *value, const size_t size, uin } #endif // SDCARD_EEPROM_EMULATION -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/eeprom_sram.cpp b/Marlin/src/HAL/STM32/eeprom_sram.cpp index 135bcabde9aa..76acc8881470 100644 --- a/Marlin/src/HAL/STM32/eeprom_sram.cpp +++ b/Marlin/src/HAL/STM32/eeprom_sram.cpp @@ -20,7 +20,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" @@ -65,4 +65,4 @@ bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t } #endif // SRAM_EEPROM_EMULATION -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/eeprom_wired.cpp b/Marlin/src/HAL/STM32/eeprom_wired.cpp index 6aa2f1d360f8..2e902c8b999b 100644 --- a/Marlin/src/HAL/STM32/eeprom_wired.cpp +++ b/Marlin/src/HAL/STM32/eeprom_wired.cpp @@ -20,7 +20,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" @@ -75,4 +75,4 @@ bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t } #endif // USE_WIRED_EEPROM -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/fast_pwm.cpp b/Marlin/src/HAL/STM32/fast_pwm.cpp index 42eecb5e1a96..19b16a6037fe 100644 --- a/Marlin/src/HAL/STM32/fast_pwm.cpp +++ b/Marlin/src/HAL/STM32/fast_pwm.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfigPre.h" @@ -56,4 +56,4 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255 } #endif // NEEDS_HARDWARE_PWM -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/fastio.cpp b/Marlin/src/HAL/STM32/fastio.cpp index 0d55579d8886..92550c9fa639 100644 --- a/Marlin/src/HAL/STM32/fastio.cpp +++ b/Marlin/src/HAL/STM32/fastio.cpp @@ -20,7 +20,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" diff --git a/Marlin/src/HAL/STM32/msc_sd.cpp b/Marlin/src/HAL/STM32/msc_sd.cpp index cbfb837875c8..6a04bf92e7fc 100644 --- a/Marlin/src/HAL/STM32/msc_sd.cpp +++ b/Marlin/src/HAL/STM32/msc_sd.cpp @@ -13,21 +13,21 @@ * along with this program. If not, see . * */ -#include "../../inc/MarlinConfigPre.h" - -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && HAS_SD_HOST_DRIVE +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) && HAS_SD_HOST_DRIVE #include "msc_sd.h" -#include "../shared/Marduino.h" #include "usbd_core.h" + +#include "../../inc/MarlinConfigPre.h" +#include "../shared/Marduino.h" +#include "../../sd/cardreader.h" + #include #include #define BLOCK_SIZE 512 #define PRODUCT_ID 0x29 -#include "../../sd/cardreader.h" - class Sd2CardUSBMscHandler : public USBMscHandler { public: DiskIODriver* diskIODriver() { @@ -121,4 +121,4 @@ void MSC_SD_init() { USBDevice.begin(); } -#endif // __STM32F1__ && HAS_SD_HOST_DRIVE +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 && HAS_SD_HOST_DRIVE diff --git a/Marlin/src/HAL/STM32/tft/gt911.cpp b/Marlin/src/HAL/STM32/tft/gt911.cpp index f99fa46e1946..e5de8da8fdb8 100644 --- a/Marlin/src/HAL/STM32/tft/gt911.cpp +++ b/Marlin/src/HAL/STM32/tft/gt911.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../../inc/MarlinConfig.h" @@ -199,4 +199,4 @@ bool GT911::getPoint(int16_t *x, int16_t *y) { } #endif // TFT_TOUCH_DEVICE_GT911 -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/tft/tft_fsmc.cpp b/Marlin/src/HAL/STM32/tft/tft_fsmc.cpp index 87ca2dbbe18a..6fefcfbdba21 100644 --- a/Marlin/src/HAL/STM32/tft/tft_fsmc.cpp +++ b/Marlin/src/HAL/STM32/tft/tft_fsmc.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../../inc/MarlinConfig.h" @@ -178,4 +178,4 @@ void TFT_FSMC::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Cou } #endif // HAS_FSMC_TFT -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/tft/tft_ltdc.cpp b/Marlin/src/HAL/STM32/tft/tft_ltdc.cpp index f2509ce5e4db..a1a7701671cb 100644 --- a/Marlin/src/HAL/STM32/tft/tft_ltdc.cpp +++ b/Marlin/src/HAL/STM32/tft/tft_ltdc.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../../inc/MarlinConfig.h" @@ -384,4 +384,4 @@ void TFT_LTDC::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Cou } #endif // HAS_LTDC_TFT -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/tft/tft_spi.cpp b/Marlin/src/HAL/STM32/tft/tft_spi.cpp index 6bfce81f1a53..5f41f8f0192f 100644 --- a/Marlin/src/HAL/STM32/tft/tft_spi.cpp +++ b/Marlin/src/HAL/STM32/tft/tft_spi.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../../inc/MarlinConfig.h" @@ -240,4 +240,4 @@ void TFT_SPI::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Coun } #endif // HAS_SPI_TFT -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/tft/xpt2046.cpp b/Marlin/src/HAL/STM32/tft/xpt2046.cpp index dffeb6aaf72f..498fc4932c38 100644 --- a/Marlin/src/HAL/STM32/tft/xpt2046.cpp +++ b/Marlin/src/HAL/STM32/tft/xpt2046.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../../inc/MarlinConfig.h" @@ -167,4 +167,4 @@ uint16_t XPT2046::SoftwareIO(uint16_t data) { } #endif // HAS_TFT_XPT2046 -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/timers.cpp b/Marlin/src/HAL/STM32/timers.cpp index 03353c2ca30e..5ec9cc3b3cdb 100644 --- a/Marlin/src/HAL/STM32/timers.cpp +++ b/Marlin/src/HAL/STM32/timers.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" @@ -319,4 +319,4 @@ static constexpr bool verify_no_timer_conflicts() { // when hovering over it, making it easy to identify the conflicting timers. static_assert(verify_no_timer_conflicts(), "One or more timer conflict detected. Examine \"timers_in_use\" to help identify conflict."); -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/usb_host.cpp b/Marlin/src/HAL/STM32/usb_host.cpp index 8fa49ccbcc16..20070877373e 100644 --- a/Marlin/src/HAL/STM32/usb_host.cpp +++ b/Marlin/src/HAL/STM32/usb_host.cpp @@ -20,7 +20,7 @@ * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfig.h" @@ -114,4 +114,4 @@ uint8_t BulkStorage::Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t b } #endif // USE_OTG_USB_HOST && USBHOST -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/usb_serial.cpp b/Marlin/src/HAL/STM32/usb_serial.cpp index 705d649ff58c..f79608c496c6 100644 --- a/Marlin/src/HAL/STM32/usb_serial.cpp +++ b/Marlin/src/HAL/STM32/usb_serial.cpp @@ -16,7 +16,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfigPre.h" @@ -51,4 +51,4 @@ void USB_Hook_init() { } #endif // EMERGENCY_PARSER && USBD_USE_CDC -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/HAL/STM32/watchdog.cpp b/Marlin/src/HAL/STM32/watchdog.cpp index aad0a79a0cb2..e29efd2576e9 100644 --- a/Marlin/src/HAL/STM32/watchdog.cpp +++ b/Marlin/src/HAL/STM32/watchdog.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../inc/MarlinConfigPre.h" @@ -46,4 +46,4 @@ void HAL_watchdog_refresh() { } #endif // USE_WATCHDOG -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 diff --git a/Marlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/compat.h b/Marlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/compat.h index 8ae7a150f876..339b337e5533 100644 --- a/Marlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/compat.h +++ b/Marlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/compat.h @@ -288,7 +288,7 @@ // Remove compiler warning on an unused variable #ifndef UNUSED - #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) + #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #define UNUSED(X) (void)X #else #define UNUSED(x) ((void)(x)) diff --git a/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.cpp b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.cpp index 36e84b653bf1..39d85626761f 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifiSerial_STM32.cpp @@ -19,7 +19,7 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) #include "../../../inc/MarlinConfigPre.h" diff --git a/ini/stm32f1-maple.ini b/ini/stm32f1-maple.ini index 9bf5a47daa7a..6e5e3f2ae3eb 100644 --- a/ini/stm32f1-maple.ini +++ b/ini/stm32f1-maple.ini @@ -26,8 +26,7 @@ platform = ststm32@~12.1 board_build.core = maple build_flags = !python Marlin/src/HAL/STM32F1/build_flags.py - ${common.build_flags} - -DARDUINO_ARCH_STM32F1 + ${common.build_flags} -DARDUINO_ARCH_STM32 -DMAPLE_STM32F1 build_unflags = -std=gnu11 -std=gnu++11 src_filter = ${common.default_src_filter} + lib_ignore = SPI, FreeRTOS701, FreeRTOS821 @@ -165,7 +164,7 @@ extends = common_stm32f1 board = genericSTM32F103VE build_flags = ${common_stm32f1.build_flags} -ffunction-sections -fdata-sections -nostdlib -MMD - -DMCU_STM32F103VE -DARDUINO_GENERIC_STM32F103V -DBOARD_generic_stm32f103v + -DMCU_STM32F103VE -DARDUINO_GENERIC_STM32F103V -DARDUINO_ARCH_STM32F1 -DBOARD_generic_stm32f103v -DDEBUG_LEVEL=DEBUG_NONE -DCONFIG_MAPLE_MINI_NO_DISABLE_DEBUG=1 -DVECT_TAB_ADDR=0x8000000 -DERROR_LED_PORT=GPIOE -DERROR_LED_PIN=6 upload_protocol = serial From f5f59cdee444c66db1d6c6fbf073572cb5129b75 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 24 Jun 2021 14:23:12 -0500 Subject: [PATCH 09/16] Fix #endif comments --- Marlin/src/HAL/STM32/HAL.cpp | 2 +- Marlin/src/HAL/STM32/HAL_SPI.cpp | 2 +- Marlin/src/HAL/STM32/MarlinSPI.cpp | 2 +- Marlin/src/HAL/STM32/MarlinSerial.cpp | 2 +- Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp | 2 +- Marlin/src/HAL/STM32/Servo.cpp | 2 +- Marlin/src/HAL/STM32/eeprom_flash.cpp | 2 +- Marlin/src/HAL/STM32/eeprom_sdcard.cpp | 2 +- Marlin/src/HAL/STM32/eeprom_sram.cpp | 2 +- Marlin/src/HAL/STM32/eeprom_wired.cpp | 2 +- Marlin/src/HAL/STM32/fast_pwm.cpp | 2 +- Marlin/src/HAL/STM32/fastio.cpp | 2 +- Marlin/src/HAL/STM32/tft/gt911.cpp | 2 +- Marlin/src/HAL/STM32/tft/tft_fsmc.cpp | 2 +- Marlin/src/HAL/STM32/tft/tft_ltdc.cpp | 2 +- Marlin/src/HAL/STM32/tft/tft_spi.cpp | 2 +- Marlin/src/HAL/STM32/tft/xpt2046.cpp | 2 +- Marlin/src/HAL/STM32/timers.cpp | 2 +- Marlin/src/HAL/STM32/usb_host.cpp | 2 +- Marlin/src/HAL/STM32/usb_serial.cpp | 2 +- Marlin/src/HAL/STM32/watchdog.cpp | 2 +- 21 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Marlin/src/HAL/STM32/HAL.cpp b/Marlin/src/HAL/STM32/HAL.cpp index febd396b5c36..26adf522fbf3 100644 --- a/Marlin/src/HAL/STM32/HAL.cpp +++ b/Marlin/src/HAL/STM32/HAL.cpp @@ -167,4 +167,4 @@ void HAL_SYSTICK_Callback() { if (systick_user_callback) systick_user_callback(); } -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/HAL_SPI.cpp b/Marlin/src/HAL/STM32/HAL_SPI.cpp index 8a6d24f19436..ba8e6bef197e 100644 --- a/Marlin/src/HAL/STM32/HAL_SPI.cpp +++ b/Marlin/src/HAL/STM32/HAL_SPI.cpp @@ -224,4 +224,4 @@ static SPISettings spiConfig; #endif // SOFTWARE_SPI -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/MarlinSPI.cpp b/Marlin/src/HAL/STM32/MarlinSPI.cpp index 26c0e831f5bd..41081dfb80ca 100644 --- a/Marlin/src/HAL/STM32/MarlinSPI.cpp +++ b/Marlin/src/HAL/STM32/MarlinSPI.cpp @@ -165,4 +165,4 @@ uint8_t MarlinSPI::dmaSend(const void * transmitBuf, uint16_t length, bool minc) return 1; } -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 && !STM32H7xx diff --git a/Marlin/src/HAL/STM32/MarlinSerial.cpp b/Marlin/src/HAL/STM32/MarlinSerial.cpp index 307868871672..d990d2f428a1 100644 --- a/Marlin/src/HAL/STM32/MarlinSerial.cpp +++ b/Marlin/src/HAL/STM32/MarlinSerial.cpp @@ -101,4 +101,4 @@ void MarlinSerial::_rx_complete_irq(serial_t *obj) { } } -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp b/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp index b85ef1ca0e5e..2ba0359cac69 100644 --- a/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp +++ b/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp @@ -343,4 +343,4 @@ #endif // !USBD_USE_CDC_COMPOSITE #endif // SDIO_SUPPORT -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/Servo.cpp b/Marlin/src/HAL/STM32/Servo.cpp index 1e17c05f551c..c0a64c5ea949 100644 --- a/Marlin/src/HAL/STM32/Servo.cpp +++ b/Marlin/src/HAL/STM32/Servo.cpp @@ -107,4 +107,4 @@ void libServo::setInterruptPriority(uint32_t preemptPriority, uint32_t subPriori } #endif // HAS_SERVOS -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/eeprom_flash.cpp b/Marlin/src/HAL/STM32/eeprom_flash.cpp index 927f0fb2300f..05e0d4c42015 100644 --- a/Marlin/src/HAL/STM32/eeprom_flash.cpp +++ b/Marlin/src/HAL/STM32/eeprom_flash.cpp @@ -270,4 +270,4 @@ bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t } #endif // FLASH_EEPROM_EMULATION -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/eeprom_sdcard.cpp b/Marlin/src/HAL/STM32/eeprom_sdcard.cpp index 2f72457e4c34..9cab90f10909 100644 --- a/Marlin/src/HAL/STM32/eeprom_sdcard.cpp +++ b/Marlin/src/HAL/STM32/eeprom_sdcard.cpp @@ -88,4 +88,4 @@ bool PersistentStore::read_data(int &pos, uint8_t *value, const size_t size, uin } #endif // SDCARD_EEPROM_EMULATION -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/eeprom_sram.cpp b/Marlin/src/HAL/STM32/eeprom_sram.cpp index 76acc8881470..c39178523472 100644 --- a/Marlin/src/HAL/STM32/eeprom_sram.cpp +++ b/Marlin/src/HAL/STM32/eeprom_sram.cpp @@ -65,4 +65,4 @@ bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t } #endif // SRAM_EEPROM_EMULATION -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/eeprom_wired.cpp b/Marlin/src/HAL/STM32/eeprom_wired.cpp index 2e902c8b999b..3346abbe4ac3 100644 --- a/Marlin/src/HAL/STM32/eeprom_wired.cpp +++ b/Marlin/src/HAL/STM32/eeprom_wired.cpp @@ -75,4 +75,4 @@ bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t } #endif // USE_WIRED_EEPROM -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/fast_pwm.cpp b/Marlin/src/HAL/STM32/fast_pwm.cpp index 19b16a6037fe..eaffb8cfa40f 100644 --- a/Marlin/src/HAL/STM32/fast_pwm.cpp +++ b/Marlin/src/HAL/STM32/fast_pwm.cpp @@ -56,4 +56,4 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255 } #endif // NEEDS_HARDWARE_PWM -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/fastio.cpp b/Marlin/src/HAL/STM32/fastio.cpp index 92550c9fa639..5056e99d35f0 100644 --- a/Marlin/src/HAL/STM32/fastio.cpp +++ b/Marlin/src/HAL/STM32/fastio.cpp @@ -31,4 +31,4 @@ void FastIO_init() { FastIOPortMap[STM_PORT(digitalPin[i])] = get_GPIO_Port(STM_PORT(digitalPin[i])); } -#endif +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/tft/gt911.cpp b/Marlin/src/HAL/STM32/tft/gt911.cpp index e5de8da8fdb8..8c59a60f92d5 100644 --- a/Marlin/src/HAL/STM32/tft/gt911.cpp +++ b/Marlin/src/HAL/STM32/tft/gt911.cpp @@ -199,4 +199,4 @@ bool GT911::getPoint(int16_t *x, int16_t *y) { } #endif // TFT_TOUCH_DEVICE_GT911 -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/tft/tft_fsmc.cpp b/Marlin/src/HAL/STM32/tft/tft_fsmc.cpp index 6fefcfbdba21..f349eacac31c 100644 --- a/Marlin/src/HAL/STM32/tft/tft_fsmc.cpp +++ b/Marlin/src/HAL/STM32/tft/tft_fsmc.cpp @@ -178,4 +178,4 @@ void TFT_FSMC::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Cou } #endif // HAS_FSMC_TFT -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/tft/tft_ltdc.cpp b/Marlin/src/HAL/STM32/tft/tft_ltdc.cpp index a1a7701671cb..53e5bd83e027 100644 --- a/Marlin/src/HAL/STM32/tft/tft_ltdc.cpp +++ b/Marlin/src/HAL/STM32/tft/tft_ltdc.cpp @@ -384,4 +384,4 @@ void TFT_LTDC::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Cou } #endif // HAS_LTDC_TFT -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/tft/tft_spi.cpp b/Marlin/src/HAL/STM32/tft/tft_spi.cpp index 5f41f8f0192f..4e3f894a52e0 100644 --- a/Marlin/src/HAL/STM32/tft/tft_spi.cpp +++ b/Marlin/src/HAL/STM32/tft/tft_spi.cpp @@ -240,4 +240,4 @@ void TFT_SPI::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Coun } #endif // HAS_SPI_TFT -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/tft/xpt2046.cpp b/Marlin/src/HAL/STM32/tft/xpt2046.cpp index 498fc4932c38..d50c24d1777d 100644 --- a/Marlin/src/HAL/STM32/tft/xpt2046.cpp +++ b/Marlin/src/HAL/STM32/tft/xpt2046.cpp @@ -167,4 +167,4 @@ uint16_t XPT2046::SoftwareIO(uint16_t data) { } #endif // HAS_TFT_XPT2046 -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/timers.cpp b/Marlin/src/HAL/STM32/timers.cpp index 5ec9cc3b3cdb..78061981803d 100644 --- a/Marlin/src/HAL/STM32/timers.cpp +++ b/Marlin/src/HAL/STM32/timers.cpp @@ -319,4 +319,4 @@ static constexpr bool verify_no_timer_conflicts() { // when hovering over it, making it easy to identify the conflicting timers. static_assert(verify_no_timer_conflicts(), "One or more timer conflict detected. Examine \"timers_in_use\" to help identify conflict."); -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/usb_host.cpp b/Marlin/src/HAL/STM32/usb_host.cpp index 20070877373e..e45ab560e662 100644 --- a/Marlin/src/HAL/STM32/usb_host.cpp +++ b/Marlin/src/HAL/STM32/usb_host.cpp @@ -114,4 +114,4 @@ uint8_t BulkStorage::Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t b } #endif // USE_OTG_USB_HOST && USBHOST -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/usb_serial.cpp b/Marlin/src/HAL/STM32/usb_serial.cpp index f79608c496c6..0e23175fc060 100644 --- a/Marlin/src/HAL/STM32/usb_serial.cpp +++ b/Marlin/src/HAL/STM32/usb_serial.cpp @@ -51,4 +51,4 @@ void USB_Hook_init() { } #endif // EMERGENCY_PARSER && USBD_USE_CDC -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32/watchdog.cpp b/Marlin/src/HAL/STM32/watchdog.cpp index e29efd2576e9..09b403e7f237 100644 --- a/Marlin/src/HAL/STM32/watchdog.cpp +++ b/Marlin/src/HAL/STM32/watchdog.cpp @@ -46,4 +46,4 @@ void HAL_watchdog_refresh() { } #endif // USE_WATCHDOG -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32 +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 From a338b70248e2d4bf9eec8fd5779c5d2b52034ef9 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 24 Jun 2021 14:29:51 -0500 Subject: [PATCH 10/16] Tweak some #ifdefs --- Marlin/src/HAL/STM32/HAL.cpp | 10 ++++------ Marlin/src/HAL/STM32/msc_sd.cpp | 10 +++++++--- Marlin/src/HAL/STM32F1/msc_sd.cpp | 7 +++++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Marlin/src/HAL/STM32/HAL.cpp b/Marlin/src/HAL/STM32/HAL.cpp index 26adf522fbf3..d8035a979ded 100644 --- a/Marlin/src/HAL/STM32/HAL.cpp +++ b/Marlin/src/HAL/STM32/HAL.cpp @@ -91,15 +91,13 @@ void HAL_init() { USB_Hook_init(); #endif - TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the min serial handler + TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the min serial handler - #if HAS_SD_HOST_DRIVE - MSC_SD_init(); // Enable USB SD card access - #endif + TERN_(HAS_SD_HOST_DRIVE, MSC_SD_init()); // Enable USB SD card access #if PIN_EXISTS(USB_CONNECT) - OUT_WRITE(USB_CONNECT_PIN, !USB_CONNECT_INVERTING); // USB clear connection - delay(1000); // Give OS time to notice + OUT_WRITE(USB_CONNECT_PIN, !USB_CONNECT_INVERTING); // USB clear connection + delay(1000); // Give OS time to notice WRITE(USB_CONNECT_PIN, USB_CONNECT_INVERTING); #endif } diff --git a/Marlin/src/HAL/STM32/msc_sd.cpp b/Marlin/src/HAL/STM32/msc_sd.cpp index 5be7754f4cec..98f75d89f041 100644 --- a/Marlin/src/HAL/STM32/msc_sd.cpp +++ b/Marlin/src/HAL/STM32/msc_sd.cpp @@ -13,12 +13,15 @@ * along with this program. If not, see . * */ -#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) && HAS_SD_HOST_DRIVE +#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC) && !defined(MAPLE_STM32F1) + +#include "../../inc/MarlinConfigPre.h" + +#if HAS_SD_HOST_DRIVE #include "msc_sd.h" #include "usbd_core.h" -#include "../../inc/MarlinConfigPre.h" #include "../shared/Marduino.h" #include "../../sd/cardreader.h" @@ -121,4 +124,5 @@ void MSC_SD_init() { USBDevice.begin(); } -#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 && HAS_SD_HOST_DRIVE +#endif // HAS_SD_HOST_DRIVE +#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC && !MAPLE_STM32F1 diff --git a/Marlin/src/HAL/STM32F1/msc_sd.cpp b/Marlin/src/HAL/STM32F1/msc_sd.cpp index 7725b2c32400..f490c83ed829 100644 --- a/Marlin/src/HAL/STM32F1/msc_sd.cpp +++ b/Marlin/src/HAL/STM32F1/msc_sd.cpp @@ -13,9 +13,11 @@ * along with this program. If not, see . * */ +#ifdef __STM32F1__ + #include "../../inc/MarlinConfigPre.h" -#if defined(__STM32F1__) && HAS_SD_HOST_DRIVE +#if HAS_SD_HOST_DRIVE #include "msc_sd.h" #include "SPI.h" @@ -92,4 +94,5 @@ void MSC_SD_init() { #endif } -#endif // __STM32F1__ && HAS_SD_HOST_DRIVE +#endif // HAS_SD_HOST_DRIVE +#endif // __STM32F1__ From 7491014e0be27645215894aff385c8a368c1bf0b Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 24 Jun 2021 14:45:42 -0500 Subject: [PATCH 11/16] apply some PSTR --- Marlin/src/lcd/extui/mks_ui/draw_dialog.cpp | 28 +++++++-------- Marlin/src/lcd/extui/mks_ui/draw_ui.cpp | 26 +++++++------- .../lcd/extui/mks_ui/tft_multi_language.cpp | 34 ++++++++++--------- Marlin/src/lcd/extui/mks_ui/wifi_module.cpp | 14 ++++---- 4 files changed, 50 insertions(+), 52 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/draw_dialog.cpp b/Marlin/src/lcd/extui/mks_ui/draw_dialog.cpp index 6d207b86a7c7..3f2cd32c7377 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_dialog.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_dialog.cpp @@ -61,7 +61,7 @@ static lv_obj_t *scr, *tempText1, *filament_bar; extern uint8_t sel_id; extern bool once_flag, gcode_preview_over; extern int upload_result; -extern uint32_t upload_time; +extern uint32_t upload_time_sec; extern uint32_t upload_size; extern bool temps_update_flag; @@ -406,28 +406,24 @@ void lv_draw_dialog(uint8_t type) { char buf[200]; int _index = 0; - strcpy(buf, DIALOG_UPLOAD_FINISH_EN); + strcpy_P(buf, PSTR(DIALOG_UPLOAD_FINISH_EN)); _index = strlen(buf); - buf[_index] = '\n'; - _index++; - strcat(buf, DIALOG_UPLOAD_SIZE_EN); + buf[_index++] = '\n'; + strcat_P(buf, PSTR(DIALOG_UPLOAD_SIZE_EN)); _index = strlen(buf); - buf[_index] = ':'; - _index++; - sprintf(&buf[_index], " %d KBytes\n", (int)(upload_size / 1024)); + buf[_index++] = ':'; + sprintf_P(&buf[_index], PSTR(" %d KBytes\n"), (int)(upload_size / 1024)); - strcat(buf, DIALOG_UPLOAD_TIME_EN); + strcat_P(buf, PSTR(DIALOG_UPLOAD_TIME_EN)); _index = strlen(buf); - buf[_index] = ':'; - _index++; - sprintf(&buf[_index], " %d s\n", (int)upload_time); + buf[_index++] = ':'; + sprintf_P(&buf[_index], PSTR(" %d s\n"), (int)upload_time_sec); - strcat(buf, DIALOG_UPLOAD_SPEED_EN); + strcat_P(buf, PSTR(DIALOG_UPLOAD_SPEED_EN)); _index = strlen(buf); - buf[_index] = ':'; - _index++; - sprintf(&buf[_index], " %d KBytes/s\n", (int)(upload_size / upload_time / 1024)); + buf[_index++] = ':'; + sprintf_P(&buf[_index], PSTR(" %d KBytes/s\n"), (int)(upload_size / upload_time_sec / 1024)); lv_label_set_text(labelDialog, buf); lv_obj_align(labelDialog, nullptr, LV_ALIGN_CENTER, 0, -20); diff --git a/Marlin/src/lcd/extui/mks_ui/draw_ui.cpp b/Marlin/src/lcd/extui/mks_ui/draw_ui.cpp index 727120c18356..146165363d0e 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_ui.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_ui.cpp @@ -202,27 +202,27 @@ void ui_cfg_init() { #if ENABLED(MKS_WIFI_MODULE) memset(&wifiPara, 0, sizeof(wifiPara)); memset(&ipPara, 0, sizeof(ipPara)); - strcpy(wifiPara.ap_name, WIFI_AP_NAME); - strcpy(wifiPara.keyCode, WIFI_KEY_CODE); + strcpy_P(wifiPara.ap_name, PSTR(WIFI_AP_NAME)); + strcpy_P(wifiPara.keyCode, PSTR(WIFI_KEY_CODE)); //client - strcpy(ipPara.ip_addr, IP_ADDR); - strcpy(ipPara.mask, IP_MASK); - strcpy(ipPara.gate, IP_GATE); - strcpy(ipPara.dns, IP_DNS); + strcpy_P(ipPara.ip_addr, PSTR(IP_ADDR)); + strcpy_P(ipPara.mask, PSTR(IP_MASK)); + strcpy_P(ipPara.gate, PSTR(IP_GATE)); + strcpy_P(ipPara.dns, PSTR(IP_DNS)); ipPara.dhcp_flag = IP_DHCP_FLAG; //AP - strcpy(ipPara.dhcpd_ip, AP_IP_ADDR); - strcpy(ipPara.dhcpd_mask, AP_IP_MASK); - strcpy(ipPara.dhcpd_gate, AP_IP_GATE); - strcpy(ipPara.dhcpd_dns, AP_IP_DNS); - strcpy(ipPara.start_ip_addr, IP_START_IP); - strcpy(ipPara.end_ip_addr, IP_END_IP); + strcpy_P(ipPara.dhcpd_ip, PSTR(AP_IP_ADDR)); + strcpy_P(ipPara.dhcpd_mask, PSTR(AP_IP_MASK)); + strcpy_P(ipPara.dhcpd_gate, PSTR(AP_IP_GATE)); + strcpy_P(ipPara.dhcpd_dns, PSTR(AP_IP_DNS)); + strcpy_P(ipPara.start_ip_addr, PSTR(IP_START_IP)); + strcpy_P(ipPara.end_ip_addr, PSTR(IP_END_IP)); ipPara.dhcpd_flag = AP_IP_DHCP_FLAG; - strcpy((char*)uiCfg.cloud_hostUrl, "baizhongyun.cn"); + strcpy_P((char*)uiCfg.cloud_hostUrl, PSTR("baizhongyun.cn")); uiCfg.cloud_port = 10086; #endif diff --git a/Marlin/src/lcd/extui/mks_ui/tft_multi_language.cpp b/Marlin/src/lcd/extui/mks_ui/tft_multi_language.cpp index b34942303a82..0771a31cb415 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_multi_language.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_multi_language.cpp @@ -58,6 +58,8 @@ pause_msg_def pause_msg_menu; eeprom_def eeprom_menu; media_select_menu_def media_select_menu; +// TODO: Make all strings PSTR and update accessors for the benefit of AVR + machine_common_def machine_menu; void machine_setting_disp() { if (gCfgItems.language == LANG_SIMPLE_CHINESE) { @@ -1093,8 +1095,8 @@ void disp_language_init() { eeprom_menu.read = EEPROM_SETTINGS_READ_CN; eeprom_menu.revert = EEPROM_SETTINGS_REVERT_CN; eeprom_menu.storeTips = EEPROM_STORE_TIPS_CN; - eeprom_menu.readTips = EEPROM_READ_TIPS_CN; - eeprom_menu.revertTips = EEPROM_REVERT_TIPS_CN; + eeprom_menu.readTips = EEPROM_READ_TIPS_CN; + eeprom_menu.revertTips = EEPROM_REVERT_TIPS_CN; break; #if 1 @@ -1337,8 +1339,8 @@ void disp_language_init() { eeprom_menu.read = EEPROM_SETTINGS_READ_T_CN; eeprom_menu.revert = EEPROM_SETTINGS_REVERT_T_CN; eeprom_menu.storeTips = EEPROM_STORE_TIPS_T_CN; - eeprom_menu.readTips = EEPROM_READ_TIPS_T_CN; - eeprom_menu.revertTips = EEPROM_REVERT_TIPS_T_CN; + eeprom_menu.readTips = EEPROM_READ_TIPS_T_CN; + eeprom_menu.revertTips = EEPROM_REVERT_TIPS_T_CN; break; case LANG_ENGLISH: common_menu.dialog_confirm_title = TITLE_DIALOG_CONFIRM_EN; @@ -1571,8 +1573,8 @@ void disp_language_init() { eeprom_menu.read = EEPROM_SETTINGS_READ_EN; eeprom_menu.revert = EEPROM_SETTINGS_REVERT_EN; eeprom_menu.storeTips = EEPROM_STORE_TIPS_EN; - eeprom_menu.readTips = EEPROM_READ_TIPS_EN; - eeprom_menu.revertTips = EEPROM_REVERT_TIPS_EN; + eeprom_menu.readTips = EEPROM_READ_TIPS_EN; + eeprom_menu.revertTips = EEPROM_REVERT_TIPS_EN; break; case LANG_RUSSIAN: common_menu.dialog_confirm_title = TITLE_DIALOG_CONFIRM_RU; @@ -1896,8 +1898,8 @@ void disp_language_init() { eeprom_menu.read = EEPROM_SETTINGS_READ_RU; eeprom_menu.revert = EEPROM_SETTINGS_REVERT_RU; eeprom_menu.storeTips = EEPROM_STORE_TIPS_RU; - eeprom_menu.readTips = EEPROM_READ_TIPS_RU; - eeprom_menu.revertTips = EEPROM_REVERT_TIPS_RU; + eeprom_menu.readTips = EEPROM_READ_TIPS_RU; + eeprom_menu.revertTips = EEPROM_REVERT_TIPS_RU; break; case LANG_SPANISH: common_menu.dialog_confirm_title = TITLE_DIALOG_CONFIRM_SP; @@ -2133,8 +2135,8 @@ void disp_language_init() { eeprom_menu.read = EEPROM_SETTINGS_READ_SP; eeprom_menu.revert = EEPROM_SETTINGS_REVERT_SP; eeprom_menu.storeTips = EEPROM_STORE_TIPS_SP; - eeprom_menu.readTips = EEPROM_READ_TIPS_SP; - eeprom_menu.revertTips = EEPROM_REVERT_TIPS_SP; + eeprom_menu.readTips = EEPROM_READ_TIPS_SP; + eeprom_menu.revertTips = EEPROM_REVERT_TIPS_SP; break; #endif // if 1 @@ -2367,8 +2369,8 @@ void disp_language_init() { eeprom_menu.read = EEPROM_SETTINGS_READ_FR; eeprom_menu.revert = EEPROM_SETTINGS_REVERT_FR; eeprom_menu.storeTips = EEPROM_STORE_TIPS_FR; - eeprom_menu.readTips = EEPROM_READ_TIPS_FR; - eeprom_menu.revertTips = EEPROM_REVERT_TIPS_FR; + eeprom_menu.readTips = EEPROM_READ_TIPS_FR; + eeprom_menu.revertTips = EEPROM_REVERT_TIPS_FR; break; case LANG_ITALY: @@ -2600,8 +2602,8 @@ void disp_language_init() { eeprom_menu.read = EEPROM_SETTINGS_READ_IT; eeprom_menu.revert = EEPROM_SETTINGS_REVERT_IT; eeprom_menu.storeTips = EEPROM_STORE_TIPS_IT; - eeprom_menu.readTips = EEPROM_READ_TIPS_IT; - eeprom_menu.revertTips = EEPROM_REVERT_TIPS_IT; + eeprom_menu.readTips = EEPROM_READ_TIPS_IT; + eeprom_menu.revertTips = EEPROM_REVERT_TIPS_IT; break; #endif // if 1 @@ -2836,8 +2838,8 @@ void disp_language_init() { eeprom_menu.read = EEPROM_SETTINGS_READ_EN; eeprom_menu.revert = EEPROM_SETTINGS_REVERT_EN; eeprom_menu.storeTips = EEPROM_STORE_TIPS_EN; - eeprom_menu.readTips = EEPROM_READ_TIPS_EN; - eeprom_menu.revertTips = EEPROM_REVERT_TIPS_EN; + eeprom_menu.readTips = EEPROM_READ_TIPS_EN; + eeprom_menu.revertTips = EEPROM_REVERT_TIPS_EN; break; } } diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp index 5b75221163ab..308af9de0394 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp @@ -51,8 +51,8 @@ #define WIFI_SET() WRITE(WIFI_RESET_PIN, HIGH); #define WIFI_RESET() WRITE(WIFI_RESET_PIN, LOW); -#define WIFI_IO1_SET() WRITE(WIFI_IO1_PIN, HIGH); -#define WIFI_IO1_RESET() WRITE(WIFI_IO1_PIN, LOW); +#define WIFI_IO1_SET() WRITE(WIFI_IO1_PIN, HIGH); +#define WIFI_IO1_RESET() WRITE(WIFI_IO1_PIN, LOW); extern uint8_t Explore_Disk (char *path , uint8_t recu_level); @@ -75,12 +75,12 @@ extern uint8_t pause_resum; uint8_t wifi_connect_flg = 0; extern volatile uint8_t get_temp_flag; -#define WIFI_MODE 2 +#define WIFI_MODE 2 #define WIFI_AP_MODE 3 int upload_result = 0; -uint32_t upload_time = 0; +uint32_t upload_time_sec = 0; uint32_t upload_size = 0; volatile WIFI_STATE wifi_link_state; @@ -152,7 +152,7 @@ static bool longName2DosName(const char *longName, char *dosName) { uint8_t c = *longName++; if (c == '.') { // For a dot... if (i == 0) return false; - strcat(dosName, ".GCO"); + strcat_P(dosName, PSTR(".GCO")); return dosName[0] != '\0'; } else { @@ -163,7 +163,7 @@ static bool longName2DosName(const char *longName, char *dosName) { dosName[i++] = (c < 'a' || c > 'z') ? (c) : (c + ('A' - 'a')); // Uppercase required for 8.3 name } if (i >= 5) { - strcat(dosName, "~1.GCO"); + strcat_P(dosName, PSTR("~1.GCO")); return dosName[0] != '\0'; } } @@ -1620,7 +1620,7 @@ static void file_fragment_msg_handle(uint8_t * msg, uint16_t msgLen) { ZERO(public_buf); file_writer.write_index = 0; file_writer.tick_end = getWifiTick(); - upload_time = getWifiTickDiff(file_writer.tick_begin, file_writer.tick_end) / 1000; + upload_time_sec = getWifiTickDiff(file_writer.tick_begin, file_writer.tick_end) / 1000; upload_size = gCfgItems.curFilesize; wifi_link_state = WIFI_CONNECTED; upload_result = 3; From e1a22863e729837397dbecede091a55b2bf52180 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 24 Jun 2021 14:54:57 -0500 Subject: [PATCH 12/16] tweak wifi ticks --- Marlin/src/lcd/extui/mks_ui/wifi_module.cpp | 12 ++++-------- Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp | 7 ++----- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp index 308af9de0394..1d136a407549 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifi_module.cpp @@ -120,19 +120,15 @@ uint32_t getWifiTickDiff(int32_t lastTick, int32_t curTick) { } void wifi_delay(int n) { - uint32_t begin = getWifiTick(), end = begin; - while (getWifiTickDiff(begin, end) < (uint32_t)n) { + const uint32_t start = getWifiTick(); + while (getWifiTickDiff(start, getWifiTick()) < (uint32_t)n) watchdog_refresh(); - end = getWifiTick(); - } } void wifi_reset() { - uint32_t start, now; - start = getWifiTick(); - now = start; + uint32_t start = getWifiTick(); WIFI_RESET(); - while (getWifiTickDiff(start, now) < 500) now = getWifiTick(); + while (getWifiTickDiff(start, getWifiTick()) < 500) { /* nada */ } WIFI_SET(); } diff --git a/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp b/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp index 16f69edd78c1..4a5f08edaaea 100644 --- a/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp +++ b/Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp @@ -643,10 +643,7 @@ static const uint32_t FirmwareAddress = 0x00000000, WebFilesAddress = 0x00100000 void ResetWiFiForUpload(int begin_or_end) { //#if 0 - uint32_t start, now; - - start = getWifiTick(); - now = start; + uint32_t start = getWifiTick(); if (begin_or_end == 0) { SET_OUTPUT(WIFI_IO0_PIN); @@ -656,7 +653,7 @@ void ResetWiFiForUpload(int begin_or_end) { SET_INPUT_PULLUP(WIFI_IO0_PIN); WIFI_RESET(); - while (getWifiTickDiff(start, now) < 500) now = getWifiTick(); + while (getWifiTickDiff(start, getWifiTick()) < 500) { /* nada */ } WIFI_SET(); //#endif } From c347ebd7382e29f6583922de5d2ea63efec68dee Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 24 Jun 2021 15:02:12 -0500 Subject: [PATCH 13/16] Simplify MKS custom filament check --- .../lcd/extui/mks_ui/printer_operation.cpp | 33 ++----------------- .../src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h | 8 ++--- 2 files changed, 7 insertions(+), 34 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp b/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp index 87269207bd31..5e359c409182 100644 --- a/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp +++ b/Marlin/src/lcd/extui/mks_ui/printer_operation.cpp @@ -163,16 +163,7 @@ void filament_check() { #endif #if PIN_EXISTS(MT_DET_1) static int fil_det_count_1 = 0; - if (!READ(MT_DET_1_PIN) && !MT_DET_PIN_INVERTING) - fil_det_count_1++; - else if (READ(MT_DET_1_PIN) && MT_DET_PIN_INVERTING) - fil_det_count_1++; - else if (fil_det_count_1 > 0) - fil_det_count_1--; - - if (!READ(MT_DET_1_PIN) && !MT_DET_PIN_INVERTING) - fil_det_count_1++; - else if (READ(MT_DET_1_PIN) && MT_DET_PIN_INVERTING) + if (READ(MT_DET_1_PIN) == MT_DET_PIN_INVERTING) fil_det_count_1++; else if (fil_det_count_1 > 0) fil_det_count_1--; @@ -180,16 +171,7 @@ void filament_check() { #if PIN_EXISTS(MT_DET_2) static int fil_det_count_2 = 0; - if (!READ(MT_DET_2_PIN) && !MT_DET_PIN_INVERTING) - fil_det_count_2++; - else if (READ(MT_DET_2_PIN) && MT_DET_PIN_INVERTING) - fil_det_count_2++; - else if (fil_det_count_2 > 0) - fil_det_count_2--; - - if (!READ(MT_DET_2_PIN) && !MT_DET_PIN_INVERTING) - fil_det_count_2++; - else if (READ(MT_DET_2_PIN) && MT_DET_PIN_INVERTING) + if (READ(MT_DET_2_PIN) == MT_DET_PIN_INVERTING) fil_det_count_2++; else if (fil_det_count_2 > 0) fil_det_count_2--; @@ -197,16 +179,7 @@ void filament_check() { #if PIN_EXISTS(MT_DET_3) static int fil_det_count_3 = 0; - if (!READ(MT_DET_3_PIN) && !MT_DET_PIN_INVERTING) - fil_det_count_3++; - else if (READ(MT_DET_3_PIN) && MT_DET_PIN_INVERTING) - fil_det_count_3++; - else if (fil_det_count_3 > 0) - fil_det_count_3--; - - if (!READ(MT_DET_3_PIN) && !MT_DET_PIN_INVERTING) - fil_det_count_3++; - else if (READ(MT_DET_3_PIN) && MT_DET_PIN_INVERTING) + if (READ(MT_DET_3_PIN) == MT_DET_PIN_INVERTING) fil_det_count_3++; else if (fil_det_count_3 > 0) fil_det_count_3--; diff --git a/Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h b/Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h index 68138687cdfe..70ae467652d5 100644 --- a/Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h +++ b/Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h @@ -178,16 +178,16 @@ // // Misc. Functions // -#define MT_DET_1 PA4 -#define MT_DET_2 PE6 +#define MT_DET_1_PIN PA4 +#define MT_DET_2_PIN PE6 #define PW_DET PA13 #define PW_OFF PB2 #ifndef FIL_RUNOUT_PIN - #define FIL_RUNOUT_PIN MT_DET_1 + #define FIL_RUNOUT_PIN MT_DET_1_PIN #endif #ifndef FIL_RUNOUT2_PIN - #define FIL_RUNOUT2_PIN MT_DET_2 + #define FIL_RUNOUT2_PIN MT_DET_2_PIN #endif #ifndef POWER_LOSS_PIN From c226226a41498e25642ef31d6843a0908d6b1d0e Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 24 Jun 2021 15:23:52 -0500 Subject: [PATCH 14/16] Tweak MKS hardware test --- .../lcd/extui/mks_ui/draw_error_message.cpp | 5 +- .../src/lcd/extui/mks_ui/draw_ready_print.cpp | 5 +- Marlin/src/lcd/extui/mks_ui/draw_ui.cpp | 10 +- .../lcd/extui/mks_ui/mks_hardware_test.cpp | 134 ++++++++---------- .../src/lcd/extui/mks_ui/mks_hardware_test.h | 6 +- Marlin/src/lcd/extui/mks_ui/pic_manager.cpp | 5 +- .../extui/mks_ui/tft_lvgl_configuration.cpp | 7 +- Marlin/src/pins/stm32f1/pins_MKS_ROBIN_E3P.h | 1 + 8 files changed, 88 insertions(+), 85 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/draw_error_message.cpp b/Marlin/src/lcd/extui/mks_ui/draw_error_message.cpp index 3297b9da2772..7ee2617326f0 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_error_message.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_error_message.cpp @@ -29,9 +29,12 @@ #include "tft_lvgl_configuration.h" #include "SPI_TFT.h" -#include "mks_hardware_test.h" #include "../../../inc/MarlinConfig.h" +#if ENABLED(MKS_TEST) + #include "mks_hardware_test.h" +#endif + static lv_obj_t *scr; void lv_draw_error_message(PGM_P const msg) { diff --git a/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp b/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp index 0ad74fd45f8f..5f9932a2b311 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp @@ -27,7 +27,6 @@ #include "draw_tool.h" #include #include "tft_lvgl_configuration.h" -#include "mks_hardware_test.h" #include "draw_ui.h" #include @@ -40,6 +39,10 @@ #include "draw_touch_calibration.h" #endif +#if ENABLED(MKS_TEST) + #include "mks_hardware_test.h" +#endif + #include #define ICON_POS_Y 38 diff --git a/Marlin/src/lcd/extui/mks_ui/draw_ui.cpp b/Marlin/src/lcd/extui/mks_ui/draw_ui.cpp index 146165363d0e..30e08e03ed45 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_ui.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_ui.cpp @@ -30,7 +30,6 @@ #include "pic_manager.h" #include "draw_ui.h" -#include "mks_hardware_test.h" #include @@ -52,6 +51,10 @@ #include "draw_touch_calibration.h" #endif +#if ENABLED(MKS_TEST) + #include "mks_hardware_test.h" +#endif + CFG_ITMES gCfgItems; UI_CFG uiCfg; DISP_STATE_STACK disp_state_stack; @@ -1362,7 +1365,10 @@ void print_time_count() { void LV_TASK_HANDLER() { lv_task_handler(); - if (mks_test_flag == 0x1E) mks_hardware_test(); + + #if ENABLED(MKS_TEST) + if (mks_test_flag == 0x1E) mks_hardware_test(); + #endif TERN_(HAS_GCODE_PREVIEW, disp_pre_gcode(2, 36)); diff --git a/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.cpp b/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.cpp index bcb3cdb6a27e..67f1eed10f9f 100644 --- a/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.cpp +++ b/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.cpp @@ -27,7 +27,6 @@ #include "tft_lvgl_configuration.h" #include "draw_ready_print.h" -#include "mks_hardware_test.h" #include "draw_ui.h" #include "pic_manager.h" #include @@ -36,51 +35,51 @@ #include "../../../module/temperature.h" #include "../../../sd/cardreader.h" -uint8_t pw_det_sta, pw_off_sta, mt_det_sta, mt_det3_sta; -#if PIN_EXISTS(MT_DET_2) - uint8_t mt_det2_sta; -#endif -uint8_t endstopx1_sta, endstopx2_sta, endstopy1_sta, endstopy2_sta, endstopz1_sta, endstopz2_sta; -void test_gpio_readlevel_L() { - #if ENABLED(MKS_TEST) +#if ENABLED(MKS_TEST) + + #include "mks_hardware_test.h" + + bool pw_det_sta, pw_off_sta, mt_det_sta; + #if PIN_EXISTS(MT_DET_2) + bool mt_det2_sta; + #endif + bool endstopx1_sta, endstopx2_sta, endstopy1_sta, endstopy2_sta, endstopz1_sta, endstopz2_sta; + + void test_gpio_readlevel_L() { volatile uint32_t itest; WRITE(WIFI_IO0_PIN, HIGH); itest = 10000; - while (itest--); - pw_det_sta = (READ(MKS_TEST_POWER_LOSS_PIN) == 0); - pw_off_sta = (READ(MKS_TEST_PS_ON_PIN) == 0); - mt_det_sta = (READ(MT_DET_1_PIN) == 0); + while (itest--); // Counting to 10000 takes some amount of time + pw_det_sta = (READ(MKS_TEST_POWER_LOSS_PIN) == LOW); + pw_off_sta = (READ(MKS_TEST_PS_ON_PIN) == LOW); + mt_det_sta = (READ(MT_DET_1_PIN) == LOW); #if PIN_EXISTS(MT_DET_2) - mt_det2_sta = (READ(MT_DET_2_PIN) == 0); + mt_det2_sta = (READ(MT_DET_2_PIN) == LOW); #endif - endstopx1_sta = (READ(X_MIN_PIN) == 0); - endstopy1_sta = (READ(Y_MIN_PIN) == 0); - endstopz1_sta = (READ(Z_MIN_PIN) == 0); - endstopz2_sta = (READ(Z_MAX_PIN) == 0); - #endif -} + endstopx1_sta = (READ(X_MIN_PIN) == LOW); + endstopy1_sta = (READ(Y_MIN_PIN) == LOW); + endstopz1_sta = (READ(Z_MIN_PIN) == LOW); + endstopz2_sta = (READ(Z_MAX_PIN) == LOW); + } -void test_gpio_readlevel_H() { - #if ENABLED(MKS_TEST) + void test_gpio_readlevel_H() { volatile uint32_t itest; WRITE(WIFI_IO0_PIN, LOW); itest = 10000; while (itest--); - pw_det_sta = (READ(MKS_TEST_POWER_LOSS_PIN) == 1); - pw_off_sta = (READ(MKS_TEST_PS_ON_PIN) == 1); - mt_det_sta = (READ(MT_DET_1_PIN) == 1); + pw_det_sta = (READ(MKS_TEST_POWER_LOSS_PIN) == HIGH); + pw_off_sta = (READ(MKS_TEST_PS_ON_PIN) == HIGH); + mt_det_sta = (READ(MT_DET_1_PIN) == HIGH); #if PIN_EXISTS(MT_DET_2) - mt_det2_sta = (READ(MT_DET_2_PIN) == 1); + mt_det2_sta = (READ(MT_DET_2_PIN) == HIGH); #endif - endstopx1_sta = (READ(X_MIN_PIN) == 1); - endstopy1_sta = (READ(Y_MIN_PIN) == 1); - endstopz1_sta = (READ(Z_MIN_PIN) == 1); - endstopz2_sta = (READ(Z_MAX_PIN) == 1); - #endif -} + endstopx1_sta = (READ(X_MIN_PIN) == HIGH); + endstopy1_sta = (READ(Y_MIN_PIN) == HIGH); + endstopz1_sta = (READ(Z_MIN_PIN) == HIGH); + endstopz2_sta = (READ(Z_MAX_PIN) == HIGH); + } -void init_test_gpio() { - #ifdef MKS_TEST + void init_test_gpio() { SET_INPUT_PULLUP(X_MIN_PIN); SET_INPUT_PULLUP(Y_MIN_PIN); SET_INPUT_PULLUP(Z_MIN_PIN); @@ -102,7 +101,7 @@ void init_test_gpio() { SET_OUTPUT(Y_ENABLE_PIN); SET_OUTPUT(Z_ENABLE_PIN); SET_OUTPUT(E0_ENABLE_PIN); - #if !MB(MKS_ROBIN_E3P) + #if DISABLED(MKS_HARDWARE_TEST_ONLY_E0) SET_OUTPUT(E1_ENABLE_PIN); #endif @@ -110,11 +109,11 @@ void init_test_gpio() { WRITE(Y_ENABLE_PIN, LOW); WRITE(Z_ENABLE_PIN, LOW); WRITE(E0_ENABLE_PIN, LOW); - #if !MB(MKS_ROBIN_E3P) + #if DISABLED(MKS_HARDWARE_TEST_ONLY_E0) WRITE(E1_ENABLE_PIN, LOW); #endif - #if MB(MKS_ROBIN_E3P) + #if ENABLED(MKS_HARDWARE_TEST_ONLY_E0) SET_INPUT_PULLUP(PA1); SET_INPUT_PULLUP(PA3); SET_INPUT_PULLUP(PC2); @@ -123,68 +122,56 @@ void init_test_gpio() { SET_INPUT_PULLUP(PE6); SET_INPUT_PULLUP(PE7); #endif - #endif -} + } -void mks_test_beeper() { - #ifdef MKS_TEST + void mks_test_beeper() { WRITE(BEEPER_PIN, HIGH); delay(100); WRITE(BEEPER_PIN, LOW); delay(100); - #endif -} + } -void mks_gpio_test() { - #if ENABLED(MKS_TEST) + void mks_gpio_test() { init_test_gpio(); test_gpio_readlevel_L(); test_gpio_readlevel_H(); test_gpio_readlevel_L(); - if ((pw_det_sta == 1) - && (pw_off_sta == 1) - && (mt_det_sta == 1) + if (pw_det_sta && pw_off_sta && mt_det_sta #if PIN_EXISTS(MT_DET_2) - && (mt_det2_sta == 1) + && mt_det2_sta #endif - #if MB(MKS_ROBIN_E3P) - && (READ(PA1) == 0) - && (READ(PA3) == 0) - && (READ(PC2) == 0) - && (READ(PD8) == 0) - && (READ(PE5) == 0) - && (READ(PE6) == 0) - && (READ(PE7) == 0) + #if ENABLED(MKS_HARDWARE_TEST_ONLY_E0) + && (READ(PA1) == LOW) + && (READ(PA3) == LOW) + && (READ(PC2) == LOW) + && (READ(PD8) == LOW) + && (READ(PE5) == LOW) + && (READ(PE6) == LOW) + && (READ(PE7) == LOW) #endif ) disp_det_ok(); else disp_det_error(); - if ( (endstopx1_sta == 1) - && (endstopy1_sta == 1) - && (endstopz1_sta == 1) - && (endstopz2_sta == 1) - ) + if (endstopx1_sta && endstopy1_sta && endstopz1_sta && endstopz2_sta) disp_Limit_ok(); else disp_Limit_error(); - #endif -} + } -void mks_hardware_test() { - #if ENABLED(MKS_TEST) + void mks_hardware_test() { if (millis() % 2000 < 1000) { WRITE(X_DIR_PIN, LOW); WRITE(Y_DIR_PIN, LOW); WRITE(Z_DIR_PIN, LOW); WRITE(E0_DIR_PIN, LOW); - #if !MB(MKS_ROBIN_E3P) + #if DISABLED(MKS_HARDWARE_TEST_ONLY_E0) WRITE(E1_DIR_PIN, LOW); #endif thermalManager.fan_speed[0] = 255; - #if !MB(MKS_ROBIN_E3P) + #if DISABLED(MKS_HARDWARE_TEST_ONLY_E0) WRITE(HEATER_1_PIN, HIGH); // HE1 #endif WRITE(HEATER_0_PIN, HIGH); // HE0 @@ -195,21 +182,18 @@ void mks_hardware_test() { WRITE(Y_DIR_PIN, HIGH); WRITE(Z_DIR_PIN, HIGH); WRITE(E0_DIR_PIN, HIGH); - #if !MB(MKS_ROBIN_E3P) + #if DISABLED(MKS_HARDWARE_TEST_ONLY_E0) WRITE(E1_DIR_PIN, HIGH); #endif thermalManager.fan_speed[0] = 0; - #if !MB(MKS_ROBIN_E3P) + #if DISABLED(MKS_HARDWARE_TEST_ONLY_E0) WRITE(HEATER_1_PIN, LOW); // HE1 #endif WRITE(HEATER_0_PIN, LOW); // HE0 WRITE(HEATER_BED_PIN, LOW); // HOT-BED } - if ( (endstopx1_sta == 1) && (endstopx2_sta == 1) - && (endstopy1_sta == 1) && (endstopy2_sta == 1) - && (endstopz1_sta == 1) && (endstopz2_sta == 1) - ) { + if (endstopx1_sta && endstopx2_sta && endstopy1_sta && endstopy2_sta && endstopz1_sta && endstopz2_sta) { // nothing here } else { @@ -217,9 +201,9 @@ void mks_hardware_test() { if (disp_state == PRINT_READY_UI) mks_disp_test(); + } - #endif -} +#endif // MKS_TEST static const uint16_t ASCII_Table_16x24[] PROGMEM = { // Space ' ' diff --git a/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.h b/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.h index 0e2d8096bac9..1b46d4b0e61e 100644 --- a/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.h +++ b/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.h @@ -24,10 +24,12 @@ #include void mks_gpio_test(); +void mks_hardware_test(); +void mks_test_get(); + void disp_char_1624(uint16_t x, uint16_t y, uint8_t c, uint16_t charColor, uint16_t bkColor); void disp_string(uint16_t x, uint16_t y, const char * string, uint16_t charColor, uint16_t bkColor); -void mks_hardware_test(); void disp_assets_update(); void disp_assets_update_progress(const char *msg); -void mks_test_get(); + extern uint8_t mks_test_flag; diff --git a/Marlin/src/lcd/extui/mks_ui/pic_manager.cpp b/Marlin/src/lcd/extui/mks_ui/pic_manager.cpp index da7b1929cec0..03e408e32a38 100644 --- a/Marlin/src/lcd/extui/mks_ui/pic_manager.cpp +++ b/Marlin/src/lcd/extui/mks_ui/pic_manager.cpp @@ -27,7 +27,10 @@ #include "draw_ui.h" #include "pic_manager.h" #include "draw_ready_print.h" -#include "mks_hardware_test.h" + +#if ENABLED(MKS_TEST) + #include "mks_hardware_test.h" +#endif #include "SPIFlashStorage.h" #include "../../../libs/W25Qxx.h" diff --git a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp index 25e6ec314627..7f84277ef07b 100644 --- a/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/mks_ui/tft_lvgl_configuration.cpp @@ -139,10 +139,9 @@ void tft_lvgl_init() { #if ENABLED(SDSUPPORT) UpdateAssets(); watchdog_refresh(); // LVGL init takes time + mks_test_get(); #endif - mks_test_get(); - touch.Init(); lv_init(); @@ -232,7 +231,9 @@ void tft_lvgl_init() { if (ready) lv_draw_ready_print(); - if (mks_test_flag == 0x1E) mks_gpio_test(); + #if ENABLED(MKS_TEST) + if (mks_test_flag == 0x1E) mks_gpio_test(); + #endif } void my_disp_flush(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * color_p) { diff --git a/Marlin/src/pins/stm32f1/pins_MKS_ROBIN_E3P.h b/Marlin/src/pins/stm32f1/pins_MKS_ROBIN_E3P.h index 14664bda3940..c7850903e7bf 100644 --- a/Marlin/src/pins/stm32f1/pins_MKS_ROBIN_E3P.h +++ b/Marlin/src/pins/stm32f1/pins_MKS_ROBIN_E3P.h @@ -36,6 +36,7 @@ #define BOARD_INFO_NAME "MKS Robin E3P" #define BOARD_NO_NATIVE_USB +#define MKS_HARDWARE_TEST_ONLY_E0 // // Release PB4 (Y_ENABLE_PIN) from JTAG NRST role From 22b5de0f1126249140186458f7a087eed60cf6d5 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 12 Jul 2021 19:03:45 -0500 Subject: [PATCH 15/16] Allow for no hotend / fan --- .../src/lcd/extui/mks_ui/draw_ready_print.cpp | 60 +++++++++---------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp b/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp index 5f9932a2b311..83aec7613e01 100644 --- a/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp +++ b/Marlin/src/lcd/extui/mks_ui/draw_ready_print.cpp @@ -53,7 +53,7 @@ extern lv_group_t* g; static lv_obj_t *scr; static lv_obj_t *buttonExt1, *labelExt1, *buttonFanstate, *labelFan; -#if ENABLED(HAS_MULTI_EXTRUDER) +#if HAS_MULTI_HOTEND static lv_obj_t *labelExt2; static lv_obj_t *buttonExt2; #endif @@ -141,18 +141,18 @@ void lv_draw_ready_print() { lv_obj_align(label_tool, buttonTool, LV_ALIGN_IN_BOTTOM_MID, 0, BUTTON_TEXT_Y_OFFSET); } - e1 = lv_label_create_empty(scr); - lv_obj_set_pos(e1, 20, 20); - sprintf_P(buf, PSTR("e1: %d"), thermalManager.wholeDegHotend(0)); - lv_label_set_text(e1, buf); - + #if HAS_HOTEND + e1 = lv_label_create_empty(scr); + lv_obj_set_pos(e1, 20, 20); + sprintf_P(buf, PSTR("e1: %d"), thermalManager.wholeDegHotend(0)); + lv_label_set_text(e1, buf); + #endif #if HAS_MULTI_HOTEND e2 = lv_label_create_empty(scr); lv_obj_set_pos(e2, 20, 45); sprintf_P(buf, PSTR("e1: %d"), thermalManager.wholeDegHotend(1)); lv_label_set_text(e2, buf); #endif - #if HAS_HEATED_BED bed = lv_label_create_empty(scr); lv_obj_set_pos(bed, 20, 95); @@ -188,28 +188,20 @@ void lv_draw_ready_print() { lv_big_button_create(scr, "F:/bmp_printing.bin", main_menu.print, 340, 150, event_handler, ID_PRINT); // Monitoring - buttonExt1 = lv_big_button_create(scr, "F:/bmp_ext1_state.bin", " ", 55, ICON_POS_Y, event_handler, ID_INFO_EXT); - #if HAS_MULTI_EXTRUDER - buttonExt2 = lv_big_button_create(scr, "F:/bmp_ext2_state.bin", " ", 163, ICON_POS_Y, event_handler, ID_INFO_EXT); - #if HAS_HEATED_BED - buttonBedstate = lv_big_button_create(scr, "F:/bmp_bed_state.bin", " ", 271, ICON_POS_Y, event_handler, ID_INFO_BED); - #endif - #else - #if HAS_HEATED_BED - buttonBedstate = lv_big_button_create(scr, "F:/bmp_bed_state.bin", " ", 210, ICON_POS_Y, event_handler, ID_INFO_BED); - #endif + #if HAS_HOTEND + buttonExt1 = lv_big_button_create(scr, "F:/bmp_ext1_state.bin", " ", 55, ICON_POS_Y, event_handler, ID_INFO_EXT); #endif - - buttonFanstate = lv_big_button_create(scr, "F:/bmp_fan_state.bin", " ", 380, ICON_POS_Y, event_handler, ID_INFO_FAN); - - labelExt1 = lv_label_create_empty(scr); - #if HAS_MULTI_EXTRUDER - labelExt2 = lv_label_create_empty(scr); + #if HAS_MULTI_HOTEND + buttonExt2 = lv_big_button_create(scr, "F:/bmp_ext2_state.bin", " ", 163, ICON_POS_Y, event_handler, ID_INFO_EXT); #endif #if HAS_HEATED_BED - labelBed = lv_label_create_empty(scr); + buttonBedstate = lv_big_button_create(scr, "F:/bmp_bed_state.bin", " ", TERN(HAS_MULTI_HOTEND, 271, 210), ICON_POS_Y, event_handler, ID_INFO_BED); #endif - labelFan = lv_label_create_empty(scr); + + TERN_(HAS_HOTEND, labelExt1 = lv_label_create_empty(scr)); + TERN_(HAS_MULTI_HOTEND, labelExt2 = lv_label_create_empty(scr)); + TERN_(HAS_HEATED_BED, labelBed = lv_label_create_empty(scr)); + TERN_(HAS_FAN, labelFan = lv_label_create_empty(scr)); lv_temp_refr(); } @@ -224,10 +216,12 @@ void lv_draw_ready_print() { } void lv_temp_refr() { - sprintf(public_buf_l, printing_menu.temp1, thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0)); - lv_label_set_text(labelExt1, public_buf_l); - lv_obj_align(labelExt1, buttonExt1, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); - #if HAS_MULTI_EXTRUDER + #if HAS_HOTEND + sprintf(public_buf_l, printing_menu.temp1, thermalManager.wholeDegHotend(0), thermalManager.degTargetHotend(0)); + lv_label_set_text(labelExt1, public_buf_l); + lv_obj_align(labelExt1, buttonExt1, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); + #endif + #if HAS_MULTI_HOTEND sprintf(public_buf_l, printing_menu.temp1, thermalManager.wholeDegHotend(1), thermalManager.degTargetHotend(1)); lv_label_set_text(labelExt2, public_buf_l); lv_obj_align(labelExt2, buttonExt2, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); @@ -237,9 +231,11 @@ void lv_temp_refr() { lv_label_set_text(labelBed, public_buf_l); lv_obj_align(labelBed, buttonBedstate, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); #endif - sprintf_P(public_buf_l, PSTR("%d%%"), (int)thermalManager.fanSpeedPercent(0)); - lv_label_set_text(labelFan, public_buf_l); - lv_obj_align(labelFan, buttonFanstate, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); + #if HAS_FAN + sprintf_P(public_buf_l, PSTR("%d%%"), (int)thermalManager.fanSpeedPercent(0)); + lv_label_set_text(labelFan, public_buf_l); + lv_obj_align(labelFan, buttonFanstate, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); + #endif } void lv_clear_ready_print() { From 5e38f79f5294fe757a06d81a29e45b4872e6a363 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 12 Jul 2021 19:11:51 -0500 Subject: [PATCH 16/16] Use delay() instead of loop --- Marlin/src/lcd/extui/mks_ui/mks_hardware_test.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.cpp b/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.cpp index 67f1eed10f9f..125f8be0f6c3 100644 --- a/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.cpp +++ b/Marlin/src/lcd/extui/mks_ui/mks_hardware_test.cpp @@ -46,10 +46,8 @@ bool endstopx1_sta, endstopx2_sta, endstopy1_sta, endstopy2_sta, endstopz1_sta, endstopz2_sta; void test_gpio_readlevel_L() { - volatile uint32_t itest; WRITE(WIFI_IO0_PIN, HIGH); - itest = 10000; - while (itest--); // Counting to 10000 takes some amount of time + delay(10); pw_det_sta = (READ(MKS_TEST_POWER_LOSS_PIN) == LOW); pw_off_sta = (READ(MKS_TEST_PS_ON_PIN) == LOW); mt_det_sta = (READ(MT_DET_1_PIN) == LOW); @@ -63,10 +61,8 @@ } void test_gpio_readlevel_H() { - volatile uint32_t itest; WRITE(WIFI_IO0_PIN, LOW); - itest = 10000; - while (itest--); + delay(10); pw_det_sta = (READ(MKS_TEST_POWER_LOSS_PIN) == HIGH); pw_off_sta = (READ(MKS_TEST_PS_ON_PIN) == HIGH); mt_det_sta = (READ(MT_DET_1_PIN) == HIGH);