From 540ab0c8ac5e008bf58c75447753a91650b9b60d Mon Sep 17 00:00:00 2001 From: Antonino Di Guardo <64427768+digant73@users.noreply.github.com> Date: Mon, 25 Mar 2024 03:14:19 +0100 Subject: [PATCH] code reduction & cleanup (#2920) --- TFT/src/User/API/AddonHardware.c | 2 +- TFT/src/User/API/BuzzerControl.h | 2 +- TFT/src/User/API/FanControl.c | 2 +- TFT/src/User/API/FlashStore.c | 13 +- TFT/src/User/API/Gcode/gcode.c | 78 +-- TFT/src/User/API/Gcode/gcode.h | 23 +- TFT/src/User/API/Gcode/mygcodefs.c | 2 +- TFT/src/User/API/HW_Init.c | 2 +- TFT/src/User/API/LCD_Colors.h | 4 +- TFT/src/User/API/LCD_Dimming.c | 4 +- TFT/src/User/API/LCD_Dimming.h | 2 +- TFT/src/User/API/LED_Colors.c | 1 + TFT/src/User/API/LED_Colors.h | 2 +- TFT/src/User/API/LED_Event.c | 4 +- TFT/src/User/API/Language/Language.c | 13 +- TFT/src/User/API/Language/utf8_decode.c | 64 +- TFT/src/User/API/LevelingControl.c | 10 +- TFT/src/User/API/MachineParameters.c | 33 +- TFT/src/User/API/MachineParameters.h | 20 +- TFT/src/User/API/Mainboard_AckHandler.c | 37 +- TFT/src/User/API/Mainboard_CmdControl.c | 4 +- TFT/src/User/API/Mainboard_CmdHandler.c | 39 +- TFT/src/User/API/Mainboard_FlowControl.c | 6 +- TFT/src/User/API/ModeSwitching.c | 3 +- TFT/src/User/API/Notification.c | 4 +- TFT/src/User/API/PowerFailed.c | 24 +- TFT/src/User/API/PowerFailed.h | 4 +- TFT/src/User/API/Printing.c | 36 +- TFT/src/User/API/Printing.h | 6 +- TFT/src/User/API/ProbeHeightControl.c | 4 +- TFT/src/User/API/ProbeHeightControl.h | 4 +- TFT/src/User/API/RRFAckHandler.cpp | 14 +- TFT/src/User/API/RRFAckHandler.hpp | 12 +- TFT/src/User/API/RRFM20Parser.cpp | 26 +- TFT/src/User/API/RRFM20Parser.hpp | 18 +- TFT/src/User/API/RRFStatusControl.c | 1 + TFT/src/User/API/ScreenShot.c | 19 +- TFT/src/User/API/ScreenShot.h | 2 +- TFT/src/User/API/SerialConnection.h | 9 +- TFT/src/User/API/Settings.c | 40 +- TFT/src/User/API/Settings.h | 10 - TFT/src/User/API/SpeedControl.c | 2 +- TFT/src/User/API/Temperature.c | 1 + TFT/src/User/API/Touch_Screen.c | 10 +- TFT/src/User/API/Touch_Screen.h | 8 +- TFT/src/User/API/UI/CharIcon.c | 5 +- TFT/src/User/API/UI/Char_Icon.inc | 12 +- TFT/src/User/API/UI/GUI.c | 598 ++++++++++-------- TFT/src/User/API/UI/GUI.h | 177 +++--- TFT/src/User/API/UI/HD44780_Emulator.c | 247 +++++++- TFT/src/User/API/UI/HD44780_Emulator.h | 200 ------ TFT/src/User/API/UI/ListItem.c | 11 +- TFT/src/User/API/UI/ListItem.h | 8 +- TFT/src/User/API/UI/ListManager.c | 41 +- TFT/src/User/API/UI/ListManager.h | 27 +- TFT/src/User/API/UI/Numpad.c | 46 +- TFT/src/User/API/UI/Numpad.h | 4 +- TFT/src/User/API/UI/ST7920_Emulator.c | 146 +++-- TFT/src/User/API/UI/ST7920_Emulator.h | 35 +- TFT/src/User/API/UI/ui_draw.c | 116 ++-- TFT/src/User/API/UI/ui_draw.h | 20 +- TFT/src/User/API/Vfs/vfs.c | 16 +- TFT/src/User/API/boot.c | 20 +- TFT/src/User/API/comment.c | 3 +- TFT/src/User/API/config.c | 204 ++++-- TFT/src/User/API/config.h | 6 +- TFT/src/User/API/coordinate.c | 14 +- TFT/src/User/API/coordinate.h | 4 +- TFT/src/User/API/debug.h | 2 +- TFT/src/User/API/menu.c | 233 +++---- TFT/src/User/API/menu.h | 23 +- TFT/src/User/API/printf/printf.c | 469 ++++++++++---- TFT/src/User/API/printf/printf.h | 2 +- TFT/src/User/Fatfs/diskio.c | 44 +- TFT/src/User/Fatfs/diskio.h | 6 +- TFT/src/User/Fatfs/myfatfs.c | 62 +- TFT/src/User/Fatfs/myfatfs.h | 6 +- TFT/src/User/Hal/HD44780.c | 20 +- TFT/src/User/Hal/HD44780.h | 8 +- TFT/src/User/Hal/Knob_LED.c | 4 +- TFT/src/User/Hal/Knob_LED.h | 4 +- TFT/src/User/Hal/LCD_Driver/HX8558.c | 7 +- TFT/src/User/Hal/LCD_Driver/HX8558.h | 5 +- TFT/src/User/Hal/LCD_Driver/ILI9325.c | 2 +- TFT/src/User/Hal/LCD_Driver/ILI9325.h | 2 + TFT/src/User/Hal/LCD_Driver/ILI9341.c | 7 +- TFT/src/User/Hal/LCD_Driver/ILI9341.h | 5 +- TFT/src/User/Hal/LCD_Driver/ILI9488.c | 14 +- TFT/src/User/Hal/LCD_Driver/ILI9488.h | 12 +- TFT/src/User/Hal/LCD_Driver/NT35310.c | 7 +- TFT/src/User/Hal/LCD_Driver/NT35310.h | 5 +- TFT/src/User/Hal/LCD_Driver/RM68042.c | 7 +- TFT/src/User/Hal/LCD_Driver/RM68042.h | 5 +- TFT/src/User/Hal/LCD_Driver/SSD1963.c | 14 + TFT/src/User/Hal/LCD_Driver/SSD1963.h | 13 +- TFT/src/User/Hal/LCD_Driver/ST7789.c | 7 +- TFT/src/User/Hal/LCD_Driver/ST7789.h | 5 +- TFT/src/User/Hal/LCD_Driver/ST7796S.c | 7 +- TFT/src/User/Hal/LCD_Driver/ST7796S.h | 5 +- TFT/src/User/Hal/LCD_Encoder.c | 9 +- TFT/src/User/Hal/LCD_Encoder.h | 4 +- TFT/src/User/Hal/LCD_Init.c | 16 +- TFT/src/User/Hal/LCD_Init.h | 3 +- TFT/src/User/Hal/STM32Fxx_Pins.h | 1 - TFT/src/User/Hal/buzzer.c | 8 +- TFT/src/User/Hal/buzzer.h | 2 +- TFT/src/User/Hal/gd32f20x/GPIO_Init.c | 152 ++++- TFT/src/User/Hal/gd32f20x/GPIO_Init.h | 154 +---- TFT/src/User/Hal/gd32f20x/HAL_Flash.c | 30 +- TFT/src/User/Hal/gd32f20x/HAL_Flash.h | 6 +- TFT/src/User/Hal/gd32f20x/Serial.c | 10 +- TFT/src/User/Hal/gd32f20x/Serial.h | 4 +- TFT/src/User/Hal/gd32f20x/lcd.c | 29 +- TFT/src/User/Hal/gd32f20x/lcd.h | 8 +- TFT/src/User/Hal/gd32f20x/lcd_dma.c | 69 +- TFT/src/User/Hal/gd32f20x/lcd_dma.h | 2 +- TFT/src/User/Hal/gd32f20x/spi.c | 18 +- TFT/src/User/Hal/gd32f20x/spi.h | 10 +- TFT/src/User/Hal/gd32f20x/spi_slave.c | 98 +-- TFT/src/User/Hal/gd32f20x/spi_slave.h | 4 +- TFT/src/User/Hal/gd32f20x/timer_pwm.c | 47 +- TFT/src/User/Hal/gd32f20x/timer_pwm.h | 2 +- TFT/src/User/Hal/gd32f20x/uart.c | 37 +- TFT/src/User/Hal/gd32f20x/uart.h | 16 +- TFT/src/User/Hal/gd32f30x/GPIO_Init.c | 152 ++++- TFT/src/User/Hal/gd32f30x/GPIO_Init.h | 154 +---- TFT/src/User/Hal/gd32f30x/HAL_Flash.c | 30 +- TFT/src/User/Hal/gd32f30x/HAL_Flash.h | 6 +- TFT/src/User/Hal/gd32f30x/Serial.c | 18 +- TFT/src/User/Hal/gd32f30x/Serial.h | 4 +- TFT/src/User/Hal/gd32f30x/lcd.c | 66 +- TFT/src/User/Hal/gd32f30x/lcd.h | 33 +- TFT/src/User/Hal/gd32f30x/lcd_dma.c | 69 +- TFT/src/User/Hal/gd32f30x/lcd_dma.h | 2 +- TFT/src/User/Hal/gd32f30x/spi.c | 18 +- TFT/src/User/Hal/gd32f30x/spi.h | 10 +- TFT/src/User/Hal/gd32f30x/spi_slave.c | 98 +-- TFT/src/User/Hal/gd32f30x/spi_slave.h | 4 +- TFT/src/User/Hal/gd32f30x/timer_pwm.c | 47 +- TFT/src/User/Hal/gd32f30x/timer_pwm.h | 2 +- TFT/src/User/Hal/gd32f30x/uart.c | 34 +- TFT/src/User/Hal/gd32f30x/uart.h | 15 +- TFT/src/User/Hal/sd.c | 456 +++++++------ TFT/src/User/Hal/sd.h | 74 +-- TFT/src/User/Hal/stm32f10x/GPIO_Init.c | 21 +- TFT/src/User/Hal/stm32f10x/GPIO_Init.h | 26 +- TFT/src/User/Hal/stm32f10x/HAL_Flash.c | 14 +- TFT/src/User/Hal/stm32f10x/HAL_Flash.h | 6 +- TFT/src/User/Hal/stm32f10x/Serial.c | 8 +- TFT/src/User/Hal/stm32f10x/Serial.h | 4 +- TFT/src/User/Hal/stm32f10x/lcd.c | 99 ++- TFT/src/User/Hal/stm32f10x/lcd.h | 47 +- TFT/src/User/Hal/stm32f10x/lcd_dma.c | 66 +- TFT/src/User/Hal/stm32f10x/lcd_dma.h | 2 +- TFT/src/User/Hal/stm32f10x/sdio_sdcard.c | 5 +- TFT/src/User/Hal/stm32f10x/sdio_sdcard.h | 15 +- TFT/src/User/Hal/stm32f10x/spi.c | 24 +- TFT/src/User/Hal/stm32f10x/spi.h | 10 +- TFT/src/User/Hal/stm32f10x/spi_slave.c | 130 ++-- TFT/src/User/Hal/stm32f10x/spi_slave.h | 4 +- TFT/src/User/Hal/stm32f10x/spi_slave_mks.c | 136 ++-- TFT/src/User/Hal/stm32f10x/timer_pwm.c | 29 +- TFT/src/User/Hal/stm32f10x/timer_pwm.h | 2 +- TFT/src/User/Hal/stm32f10x/uart.c | 33 +- TFT/src/User/Hal/stm32f10x/uart.h | 14 +- TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.c | 164 ++++- TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.h | 155 +---- TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.c | 64 +- TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.h | 7 +- TFT/src/User/Hal/stm32f2_f4xx/Serial.c | 10 +- TFT/src/User/Hal/stm32f2_f4xx/Serial.h | 4 +- TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.c | 206 +++--- TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.h | 32 +- TFT/src/User/Hal/stm32f2_f4xx/lcd.c | 65 +- TFT/src/User/Hal/stm32f2_f4xx/lcd.h | 10 +- TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.c | 50 +- TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.h | 2 +- TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.c | 12 +- TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.h | 8 + TFT/src/User/Hal/stm32f2_f4xx/spi.c | 24 +- TFT/src/User/Hal/stm32f2_f4xx/spi.h | 10 +- TFT/src/User/Hal/stm32f2_f4xx/spi_slave.c | 128 ++-- TFT/src/User/Hal/stm32f2_f4xx/spi_slave.h | 4 +- TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.c | 29 +- TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.h | 2 +- TFT/src/User/Hal/stm32f2_f4xx/uart.c | 43 +- TFT/src/User/Hal/stm32f2_f4xx/uart.h | 16 +- TFT/src/User/Hal/sw_spi.c | 18 +- TFT/src/User/Hal/sw_spi.h | 9 +- TFT/src/User/Hal/w25qxx.c | 116 ++-- TFT/src/User/Hal/w25qxx.h | 12 +- TFT/src/User/Hal/xpt2046.c | 69 +- TFT/src/User/Hal/xpt2046.h | 3 +- TFT/src/User/Menu/ABL.c | 4 +- TFT/src/User/Menu/BLTouch.c | 1 + TFT/src/User/Menu/Babystep.c | 11 +- TFT/src/User/Menu/BedLeveling.c | 7 +- TFT/src/User/Menu/BedLevelingLayer2.c | 1 + TFT/src/User/Menu/CaseLight.c | 2 +- TFT/src/User/Menu/ConnectionSettings.c | 11 +- TFT/src/User/Menu/FeatureSettings.c | 46 +- TFT/src/User/Menu/Home.c | 5 +- TFT/src/User/Menu/LEDColor.c | 38 +- TFT/src/User/Menu/LevelCorner.c | 15 +- TFT/src/User/Menu/Leveling.c | 1 + TFT/src/User/Menu/LoadUnload.c | 4 +- TFT/src/User/Menu/MBL.c | 8 +- TFT/src/User/Menu/MPC.c | 21 +- TFT/src/User/Menu/MachineSettings.c | 6 +- TFT/src/User/Menu/MachineSettings.h | 2 +- TFT/src/User/Menu/MainPage.c | 1 + TFT/src/User/Menu/MeshEditor.c | 40 +- TFT/src/User/Menu/MeshEditor.h | 2 +- TFT/src/User/Menu/MeshTuner.c | 2 +- TFT/src/User/Menu/MeshValid.c | 5 +- TFT/src/User/Menu/More.c | 7 +- TFT/src/User/Menu/Move.c | 9 +- TFT/src/User/Menu/NotificationMenu.c | 3 +- TFT/src/User/Menu/ParameterSettings.c | 18 +- TFT/src/User/Menu/PersistentInfo.c | 2 +- TFT/src/User/Menu/Pid.c | 8 +- TFT/src/User/Menu/Popup.c | 129 ++-- TFT/src/User/Menu/Popup.h | 30 +- TFT/src/User/Menu/PreheatMenu.c | 10 +- TFT/src/User/Menu/Print.c | 12 +- TFT/src/User/Menu/PrintRestore.c | 9 +- TFT/src/User/Menu/PrintingMenu.c | 63 +- TFT/src/User/Menu/RRFMacros.c | 8 +- TFT/src/User/Menu/ScreenSettings.c | 27 +- TFT/src/User/Menu/SelectMode.c | 10 +- TFT/src/User/Menu/SettingsMenu.c | 5 +- TFT/src/User/Menu/Speed.c | 4 +- TFT/src/User/Menu/StatusScreen.c | 64 +- TFT/src/User/Menu/StatusScreen.h | 2 +- TFT/src/User/Menu/Terminal.c | 38 +- TFT/src/User/Menu/Touchmi.c | 3 +- TFT/src/User/Menu/TuneExtruder.c | 8 +- TFT/src/User/Menu/Tuning.c | 1 + TFT/src/User/Menu/UnifiedHeat.c | 3 +- TFT/src/User/Menu/UnifiedMove.c | 3 +- TFT/src/User/Menu/ZOffset.c | 4 +- TFT/src/User/Menu/common.c | 4 +- TFT/src/User/Menu/common.h | 4 +- TFT/src/User/SanityCheck.h | 8 +- .../User/Variants/pin_MKS_GD_TFT28_V1_2_4.h | 4 +- TFT/src/User/Variants/pin_MKS_TFT32_V1_4.h | 4 +- TFT/src/User/Variants/pin_MKS_TFT35_V1_0.h | 4 +- TFT/src/User/Variants/pin_TFT24_V1_1.h | 4 +- TFT/src/User/Variants/pin_TFT35_V1_0.h | 4 +- TFT/src/User/Variants/pin_TFT35_V2_0.h | 4 +- TFT/src/User/Variants/pin_TFT35_V3_0.h | 4 +- TFT/src/User/Variants/pin_TFT70_V3_0.h | 4 +- TFT/src/User/Variants/pin_Template.h | 4 +- TFT/src/User/Variants/variants.h | 14 +- TFT/src/User/includes.h | 2 +- TFT/src/User/my_misc.c | 20 +- TFT/src/User/my_misc.h | 18 +- TFT/src/User/os_timer.c | 8 +- TFT/src/User/os_timer.h | 12 +- buildroot/boards/GD32F205VC_0x3000.json | 2 +- buildroot/boards/GD32F305VC_0x7000.json | 6 +- buildroot/boards/STM32F103VC_0x3000.json | 2 +- buildroot/boards/STM32F103VC_0x6000.json | 2 +- buildroot/boards/STM32F105RC_0x6000.json | 2 +- buildroot/boards/STM32F107VC_0x0000.json | 2 +- buildroot/boards/STM32F107VC_0x7000.json | 2 +- buildroot/boards/STM32F207VC_0x8000.json | 2 +- buildroot/boards/STM32F407VE_0xC000.json | 2 +- buildroot/boards/STM32F407VG_0x8000.json | 2 +- buildroot/ldscripts/gd32f20xxC_0x3000_iap.ld | 19 +- buildroot/ldscripts/gd32f30xxC_0x7000_iap.ld | 9 +- buildroot/ldscripts/stm32f103xC_0x3000_iap.ld | 21 +- buildroot/ldscripts/stm32f103xC_0x6000_iap.ld | 23 +- buildroot/ldscripts/stm32f105xC_0x6000_iap.ld | 20 +- buildroot/ldscripts/stm32f107xC_0x0000_iap.ld | 20 +- buildroot/ldscripts/stm32f107xC_0x7000_iap.ld | 20 +- buildroot/ldscripts/stm32f2xxxC_0x8000_iap.ld | 14 +- buildroot/ldscripts/stm32f4xxxE_0x0000_iap.ld | 24 +- buildroot/ldscripts/stm32f4xxxE_0xC000_iap.ld | 24 +- buildroot/ldscripts/stm32f4xxxG_0x8000_iap.ld | 24 +- coding_standard.md | 2 +- 281 files changed, 4652 insertions(+), 4025 deletions(-) diff --git a/TFT/src/User/API/AddonHardware.c b/TFT/src/User/API/AddonHardware.c index 027003550e..203f4eb5ed 100644 --- a/TFT/src/User/API/AddonHardware.c +++ b/TFT/src/User/API/AddonHardware.c @@ -83,7 +83,7 @@ void FIL_SFS_SetAlive(bool alive) sfs_alive = alive; } -bool FIL_NormalRunoutDetect(void) +static bool FIL_NormalRunoutDetect(void) { static bool runout = false; static int32_t trigBalance = 0; diff --git a/TFT/src/User/API/BuzzerControl.h b/TFT/src/User/API/BuzzerControl.h index dd0f4a4302..12c1cfb082 100644 --- a/TFT/src/User/API/BuzzerControl.h +++ b/TFT/src/User/API/BuzzerControl.h @@ -6,7 +6,7 @@ extern "C" { #endif #include -#include "variants.h" // for BUZZER_PIN etc... +#include "variants.h" // for BUZZER_PIN etc. typedef enum { diff --git a/TFT/src/User/API/FanControl.c b/TFT/src/User/API/FanControl.c index 107de2447e..6cb59b60ee 100644 --- a/TFT/src/User/API/FanControl.c +++ b/TFT/src/User/API/FanControl.c @@ -3,7 +3,7 @@ #define FAN_REFRESH_TIME 500 // 1 second is 1000 -const char * fanID[MAX_FAN_COUNT] = FAN_DISPLAY_ID; +const char * fanID[MAX_FAN_COUNT] = FAN_DISPLAY_ID; const char * fanCmd[MAX_FAN_COUNT] = FAN_CMD; static uint8_t setFanSpeed[MAX_FAN_COUNT] = {0}; diff --git a/TFT/src/User/API/FlashStore.c b/TFT/src/User/API/FlashStore.c index 59e222b84b..f8380a114c 100644 --- a/TFT/src/User/API/FlashStore.c +++ b/TFT/src/User/API/FlashStore.c @@ -17,12 +17,13 @@ enum PARA_NOT_STORED = (1 << 1), }; -uint8_t paraStatus = 0; +static uint8_t paraStatus = 0; -void wordToByte(uint32_t word, uint8_t *bytes) +static void wordToByte(uint32_t word, uint8_t * bytes) { uint8_t len = 4; uint8_t i = 0; + for (i = 0; i < len; i++) { bytes[i] = (word >> 24) & 0xFF; @@ -30,15 +31,17 @@ void wordToByte(uint32_t word, uint8_t *bytes) } } -uint32_t byteToWord(uint8_t *bytes, uint8_t len) +static uint32_t byteToWord(uint8_t * bytes, uint8_t len) { uint32_t word = 0; uint8_t i = 0; + for (i = 0; i < len; i++) { word <<= 8; word |= bytes[i]; } + return word; } @@ -55,9 +58,11 @@ void readStoredPara(void) #endif sign = byteToWord(data + (index += 4), 4); + if (sign == TSC_SIGN) { paraStatus |= PARA_TSC_EXIST; // if the touch screen calibration parameter already exists + for (int i = 0; i < sizeof(TS_CalPara) / sizeof(TS_CalPara[0]); i++) { TS_CalPara[i] = byteToWord(data + (index += 4), 4); @@ -65,6 +70,7 @@ void readStoredPara(void) } sign = byteToWord(data + (index += 4), 4); + if (sign != PARA_SIGN) // if the settings parameter is illegal, reset settings parameter { paraStatus |= PARA_NOT_STORED; @@ -83,6 +89,7 @@ void storePara(void) uint32_t index = 0; wordToByte(TSC_SIGN, data + (index += 4)); + for (int i = 0; i < sizeof(TS_CalPara) / sizeof(TS_CalPara[0]); i++) { wordToByte(TS_CalPara[i], data + (index += 4)); diff --git a/TFT/src/User/API/Gcode/gcode.c b/TFT/src/User/API/Gcode/gcode.c index 1b8b9cb062..f4d90d6609 100644 --- a/TFT/src/User/API/Gcode/gcode.c +++ b/TFT/src/User/API/Gcode/gcode.c @@ -32,11 +32,11 @@ void abortRequestCommandInfo(void) } static void resetRequestCommandInfo( - const char * string_start, // The magic to identify the start - const char * string_stop, // The magic to identify the stop - const char * string_error0, // The first magic to identify the error response - const char * string_error1, // The second error magic - const char * string_error2 // The third error magic + const char * string_start, // the magic to identify the start + const char * string_stop, // the magic to identify the stop + const char * string_error0, // the first magic to identify the error response + const char * string_error1, // the second error magic + const char * string_error2 // the third error magic ) { clearRequestCommandInfo(); // release requestCommandInfo.cmd_rev_buf before allocating a new one @@ -81,11 +81,11 @@ void detectAdvancedOk(void) TASK_LOOP_WHILE(isPendingCmd() && isNotEmptyCmdQueue()); // wait for the communication to be clean - resetRequestCommandInfo("ok", // The magic to identify the start - "ok", // The magic to identify the stop - NULL, // The first magic to identify the error response - NULL, // The second error magic - NULL); // The third error magic + resetRequestCommandInfo("ok", // the magic to identify the start + "ok", // the magic to identify the stop + NULL, // the first magic to identify the error response + NULL, // the second error magic + NULL); // the third error magic // send any gcode replied by the mainboard with a regular OK response ("ok\n") or an ADVANCED_OK response (e.g. "ok N10 P15 B3\n") mustStoreCmd("M220\n"); @@ -118,11 +118,11 @@ void detectAdvancedOk(void) */ bool request_M21(void) { - resetRequestCommandInfo("SD card ", // The magic to identify the start - "ok", // The magic to identify the stop - "No SD card", // The first magic to identify the error response - "SD init fail", // The second error magic - "volume.init failed"); // The third error magic + resetRequestCommandInfo("SD card ", // the magic to identify the start + "ok", // the magic to identify the stop + "No SD card", // the first magic to identify the error response + "SD init fail", // the second error magic + "volume.init failed"); // the third error magic mustStoreCmd((infoMachineSettings.multiVolume == ENABLED) ? ((infoFile.onboardSource == BOARD_SD) ? "M21 S\n" : "M21 U\n") : "M21\n"); @@ -130,17 +130,17 @@ bool request_M21(void) clearRequestCommandInfo(); - // Check reponse + // check reponse return !requestCommandInfo.inError; } char * request_M20(void) { - resetRequestCommandInfo("Begin file list", // The magic to identify the start - "End file list", // The magic to identify the stop - "Error", // The first magic to identify the error response - NULL, // The second error magic - NULL); // The third error magic + resetRequestCommandInfo("Begin file list", // the magic to identify the start + "End file list", // the magic to identify the stop + "Error", // the first magic to identify the error response + NULL, // the second error magic + NULL); // the third error magic if (infoMachineSettings.longFilename == ENABLED) // if long filename is supported mustStoreCmd("M20 L\n"); // L option is supported since Marlin 2.0.9 @@ -149,7 +149,7 @@ char * request_M20(void) waitForResponse(); // wait for response - //clearRequestCommandInfo(); // shall be call after copying the buffer ... + //clearRequestCommandInfo(); // shall be call after copying the buffer return requestCommandInfo.cmd_rev_buf; } @@ -161,11 +161,11 @@ char * request_M20(void) */ char * request_M33(const char * filename) { - resetRequestCommandInfo("/", // The magic to identify the start - "ok", // The magic to identify the stop - "Cannot open subdir", // The first magic to identify the error response - NULL, // The second error magic - NULL); // The third error magic + resetRequestCommandInfo("/", // the magic to identify the start + "ok", // the magic to identify the stop + "Cannot open subdir", // the first magic to identify the error response + NULL, // the second error magic + NULL); // the third error magic if (filename[0] != '/') mustStoreCmd("M33 /%s\n", filename); // append '/' to short file path @@ -199,11 +199,11 @@ long request_M23_M36(const char * filename) if (infoMachineSettings.firmwareType != FW_REPRAPFW) // all other firmwares except RepRap firmware { - resetRequestCommandInfo("File opened", // The magic to identify the start - "File selected", // The magic to identify the stop - "open failed", // The first magic to identify the error response - NULL, // The second error magic - NULL); // The third error magic + resetRequestCommandInfo("File opened", // the magic to identify the start + "File selected", // the magic to identify the stop + "open failed", // the first magic to identify the error response + NULL, // the second error magic + NULL); // the third error magic // skip source and first "/" character (e.g. "oMD:/sub_dir/cap2.gcode" -> "sub_dir/cap2.gcode") mustStoreCmd("M23 %s\n", filename + strlen(getFS()) + 1); @@ -212,11 +212,11 @@ long request_M23_M36(const char * filename) } else // RepRap firmware { - resetRequestCommandInfo("{\"err\"", // The magic to identify the start - "}", // The magic to identify the stop - "Error:", // The first magic to identify the error response - NULL, // The second error magic - NULL); // The third error magic + resetRequestCommandInfo("{\"err\"", // the magic to identify the start + "}", // the magic to identify the stop + "Error:", // the first magic to identify the error response + NULL, // the second error magic + NULL); // the third error magic mustStoreCmd("M36 /%s\n", filename); @@ -235,7 +235,7 @@ long request_M23_M36(const char * filename) if (infoMachineSettings.firmwareType == FW_REPRAPFW) mustStoreCmd("M23 /%s\n", filename); // send M23 for RepRap firmware - // Find file size and report it + // find file size and report it strPtr = strstr(requestCommandInfo.cmd_rev_buf, sizeTag); if (strPtr != NULL) @@ -309,7 +309,7 @@ void request_M98(const char * filename) rrfStatusQueryFast(); - // Wait for macro to complete + // wait for macro to complete TASK_LOOP_WHILE(rrfStatusIsBusy()); rrfStatusQueryNormal(); diff --git a/TFT/src/User/API/Gcode/gcode.h b/TFT/src/User/API/Gcode/gcode.h index 5c2c9186cb..01119d5776 100644 --- a/TFT/src/User/API/Gcode/gcode.h +++ b/TFT/src/User/API/Gcode/gcode.h @@ -15,18 +15,17 @@ typedef void (* FP_STREAM_HANDLER)(const char *); typedef struct { - char * cmd_rev_buf; // Buffer where store the command response - const char * startMagic; // The magic to identify the start - const char * stopMagic; // The magic to identify the stop - const char * errorMagic[MAX_ERROR_NUM]; - // The magic to identify the error response - // Some gcodes respond to multiple errors, such as M21 - "SD card failed", "No SD card", "volume.init failed" - uint8_t error_num; // Number of error magic corresponding to current gcode - bool inResponse; // true if between start and stop magic - bool inWaitResponse; // true if waiting for start magic - bool done; // true if command is executed and response is received - bool inError; // true if error response - bool inJson; // true if !inResponse and !inWaitResponse and '{' is found + char * cmd_rev_buf; // buffer where store the command response + const char * startMagic; // the magic to identify the start + const char * stopMagic; // the magic to identify the stop + const char * errorMagic[MAX_ERROR_NUM]; // the magic to identify the error response. Some gcodes respond to multiple errors, + // such as M21 - "SD card failed", "No SD card", "volume.init failed" + uint8_t error_num; // number of error magic corresponding to current gcode + bool inResponse; // "true" if between start and stop magic + bool inWaitResponse; // "true" if waiting for start magic + bool done; // "true" if command is executed and response is received + bool inError; // "true" if error response + bool inJson; // "true" if !inResponse and !inWaitResponse and '{' is found FP_STREAM_HANDLER stream_handler; } REQUEST_COMMAND_INFO; diff --git a/TFT/src/User/API/Gcode/mygcodefs.c b/TFT/src/User/API/Gcode/mygcodefs.c index b2d7ef57ab..b995a8a818 100644 --- a/TFT/src/User/API/Gcode/mygcodefs.c +++ b/TFT/src/User/API/Gcode/mygcodefs.c @@ -33,7 +33,7 @@ static inline void rrfScanPrintFilesGcodeFs(void) * * So the long name will be parsed "0.00 @:0 B@:0" instead of "1.gcode" if the truncated character is "\n" not string "\nok" */ -void addName(bool isFile, char * longPath, const char * shortPath, const char * relativePath) +static void addName(bool isFile, char * longPath, const char * shortPath, const char * relativePath) { char * longName = NULL; // initialize to NULL in case long filename is not supported or no long name exists diff --git a/TFT/src/User/API/HW_Init.c b/TFT/src/User/API/HW_Init.c index 45b4ceb953..ceefad1b0f 100644 --- a/TFT/src/User/API/HW_Init.c +++ b/TFT/src/User/API/HW_Init.c @@ -5,7 +5,7 @@ #include "i2c_eeprom.h" #endif -void HW_GetClocksFreq(CLOCKS *clk) +static inline void HW_GetClocksFreq(CLOCKS * clk) { #if defined(GD32F2XX) || defined(GD32F3XX) RCU_GetClocksFreq(&clk->rccClocks); diff --git a/TFT/src/User/API/LCD_Colors.h b/TFT/src/User/API/LCD_Colors.h index 9f916800b1..bab2c7620a 100644 --- a/TFT/src/User/API/LCD_Colors.h +++ b/TFT/src/User/API/LCD_Colors.h @@ -7,10 +7,10 @@ extern "C" { #endif #include -#include "Configuration.h" // for KEYBOARD_MATERIAL_THEME, LIVE_TEXT_COMMON_COLOR etc... +#include "Configuration.h" // for KEYBOARD_MATERIAL_THEME, LIVE_TEXT_COMMON_COLOR etc. #include "menu.h" -// Color Definition +// color definition #define WHITE 0xFFFF #define BLACK 0x0000 #define RED 0xF800 diff --git a/TFT/src/User/API/LCD_Dimming.c b/TFT/src/User/API/LCD_Dimming.c index 0f04511817..b3615a03fe 100644 --- a/TFT/src/User/API/LCD_Dimming.c +++ b/TFT/src/User/API/LCD_Dimming.c @@ -71,7 +71,7 @@ typedef struct bool locked; } LCD_AUTO_DIM; -LCD_AUTO_DIM lcd_dim = {0, false, false}; +static LCD_AUTO_DIM lcd_dim = {0, false, false}; bool LCD_IsBlocked(void) { @@ -160,7 +160,7 @@ void LCD_CheckDimming(void) #ifdef KNOB_LED_COLOR_PIN -bool knob_led_idle = false; +static bool knob_led_idle = false; void LCD_SetKnobLedIdle(bool enabled) { diff --git a/TFT/src/User/API/LCD_Dimming.h b/TFT/src/User/API/LCD_Dimming.h index d1c63a967c..2e2467a908 100644 --- a/TFT/src/User/API/LCD_Dimming.h +++ b/TFT/src/User/API/LCD_Dimming.h @@ -5,7 +5,7 @@ extern "C" { #endif -#include "variants.h" // for LCD_LED_PWM_CHANNEL, KNOB_LED_COLOR_PIN etc... +#include "variants.h" // for LCD_LED_PWM_CHANNEL, KNOB_LED_COLOR_PIN etc. #include "menu.h" #ifdef LCD_LED_PWM_CHANNEL diff --git a/TFT/src/User/API/LED_Colors.c b/TFT/src/User/API/LED_Colors.c index 7aea44767c..503acc5bdd 100644 --- a/TFT/src/User/API/LED_Colors.c +++ b/TFT/src/User/API/LED_Colors.c @@ -51,6 +51,7 @@ uint16_t LED_GetRGBColor(LED_COLOR * led) uint8_t r = (*led)[0] >> 3; uint8_t g = (*led)[1] >> 2; uint8_t b = (*led)[2] >> 3; + return ((r & 0x001F) << 11) | ((g & 0x003F) << 5) | ((b & 0x001F)); // RGB color in RGB 565 16 bit format } diff --git a/TFT/src/User/API/LED_Colors.h b/TFT/src/User/API/LED_Colors.h index f55d168ba1..a792afc8db 100644 --- a/TFT/src/User/API/LED_Colors.h +++ b/TFT/src/User/API/LED_Colors.h @@ -22,7 +22,7 @@ extern "C" { #define GRB_INDIGO 0x00004B82 #define GRB_VIOLET 0x0000FEFE - // Color macro // 颜色宏定 // R G B + // color macro // 颜色宏定 // R G B #define COLOR_LIGHTPINK 0xFFB6C1 // 浅粉 #define COLOR_PINK 0xFFC0CB // 粉红 #define COLOR_CRIMSON 0xDC143C // 猩红 diff --git a/TFT/src/User/API/LED_Event.c b/TFT/src/User/API/LED_Event.c index b98b78c0f4..dac1c222a8 100644 --- a/TFT/src/User/API/LED_Event.c +++ b/TFT/src/User/API/LED_Event.c @@ -15,7 +15,7 @@ #define COLD_TEMPERATURE 0 #endif -inline static bool nextUpdate(void) +static inline bool nextUpdate(void) { static uint32_t lastUpdateTime = 0; @@ -29,7 +29,7 @@ inline static bool nextUpdate(void) #ifdef ROOM_TEMPERATURE -void getColdTemperature(void) +static inline void getColdTemperature(void) { // let's estimate the room temperature if (coldTemperature == 0) { diff --git a/TFT/src/User/API/Language/Language.c b/TFT/src/User/API/Language/Language.c index 757d742136..4dfb2bb019 100644 --- a/TFT/src/User/API/Language/Language.c +++ b/TFT/src/User/API/Language/Language.c @@ -53,7 +53,7 @@ #endif // add new keywords in Language.inc file only -uint8_t tempLabelString[MAX_LANG_LABEL_LENGTH]; +static uint8_t tempLabelString[MAX_LANG_LABEL_LENGTH]; const char *const default_pack[LABEL_NUM] = { #define X_WORD(NAME) STRING_##NAME , @@ -71,7 +71,9 @@ const char *const lang_key_list[LABEL_NUM] = uint32_t getLabelFlashAddr(uint16_t index) { - if (index > LABEL_NULL) return LANGUAGE_ADDR; + if (index > LABEL_NULL) + return LANGUAGE_ADDR; + return (LANGUAGE_ADDR + (MAX_LANG_LABEL_LENGTH * index)); } @@ -81,9 +83,11 @@ uint8_t *textSelect(uint16_t index) { case LANG_DEFAULT: return (uint8_t *)default_pack[index]; + case LANG_FLASH: W25Qxx_ReadBuffer(tempLabelString, getLabelFlashAddr(index), MAX_LANG_LABEL_LENGTH); return tempLabelString; + default: return NULL; } @@ -91,7 +95,10 @@ uint8_t *textSelect(uint16_t index) bool loadLabelText(uint8_t * buf, uint16_t index) { - if (index >= LABEL_NUM) return false; + if (index >= LABEL_NUM) + return false; + memcpy(buf, textSelect(index), sizeof(tempLabelString)); + return true; } diff --git a/TFT/src/User/API/Language/utf8_decode.c b/TFT/src/User/API/Language/utf8_decode.c index 09fcb59f1b..788e86db29 100644 --- a/TFT/src/User/API/Language/utf8_decode.c +++ b/TFT/src/User/API/Language/utf8_decode.c @@ -1,12 +1,12 @@ #include "utf8_decode.h" #include "includes.h" -// High 8bits: font height -// Low 8bits: font width -uint16_t fontSize = FONT_SIZE_NORMAL; +// high 8bits: font height +// low 8bits: font width +static uint16_t fontSize = FONT_SIZE_NORMAL; static FONT_BITMAP font[] = { - { // Visible ASCII code, from ' ' to '~' + { // visible ASCII code, from ' ' to '~' // start unicode code point for language 0x20, // 0x20 means the first visible character ' ' // end unicode code point for language @@ -115,7 +115,7 @@ static void getUTF8EncodeInfo(const uint8_t *ch, CHAR_INFO *pInfo) pInfo->bytes = 4; pInfo->codePoint = (ch[0] & 0x07); } - else // Wrong char return '?' means unkown + else // wrong char return '?' means unkown { pInfo->bytes = 1; pInfo->codePoint = '?'; @@ -128,13 +128,14 @@ static void getUTF8EncodeInfo(const uint8_t *ch, CHAR_INFO *pInfo) } // get character font bitmap info -static void getBitMapFontInfo(CHAR_INFO *pInfo) +static inline void getBitMapFontInfo(CHAR_INFO *pInfo) { if (pInfo->codePoint < 9) { pInfo->pixelWidth = 0; pInfo->pixelHeight = 0; pInfo->bitMapAddr = 0; + return; } @@ -142,17 +143,16 @@ static void getBitMapFontInfo(CHAR_INFO *pInfo) { if (pInfo->codePoint >= font[i].startCodePoint && pInfo->codePoint <= font[i].endCodePoint) { - if (pInfo->codePoint >= 0x20 && pInfo->codePoint <= 0x7E) // The font size of ASCII is variable + if (pInfo->codePoint >= 0x20 && pInfo->codePoint <= 0x7E) // the font size of ASCII is variable { if (_FONT_H(fontSize) != font[i].pixelHeight || _FONT_W(fontSize) != font[i].pixelWidth) - { continue; - } } pInfo->pixelWidth = font[i].pixelWidth; pInfo->pixelHeight = font[i].pixelHeight; pInfo->bitMapAddr = font[i].bitMapStartAddr + (pInfo->codePoint - font[i].bitMapStartCodePoint) * (font[i].bitMapHeight * font[i].bitMapWidth / 8); + return; } } @@ -162,49 +162,59 @@ void getCharacterInfo(const uint8_t *ch, CHAR_INFO *pInfo) { pInfo->bytes = 0; - if (ch == NULL || *ch == 0) return; + if (ch == NULL || *ch == 0) + return; getUTF8EncodeInfo(ch, pInfo); getBitMapFontInfo(pInfo); } -// decode UTF-8 char display bit width -uint16_t GUI_StrPixelWidth_str(const uint8_t * str) +uint16_t getUTF8Length(const uint8_t *const str) { uint16_t i = 0, len = 0; CHAR_INFO info; - if (str == NULL) return 0; + if (str == NULL) + return 0; + while (str[i]) { - getCharacterInfo(str + i, &info); + info.bytes = 0; + getUTF8EncodeInfo(str + i, &info); i += info.bytes; - len += info.pixelWidth; + len++; } + return len; } // decode UTF-8 char display bit width -uint16_t GUI_StrPixelWidth_label(int16_t index) -{ - uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; - if (loadLabelText((uint8_t*)tempstr, index) == false) return 0; - return GUI_StrPixelWidth_str(tempstr); -} - -uint16_t getUTF8Length(const uint8_t *const str) +uint16_t GUI_StrPixelWidth_str(const uint8_t * str) { uint16_t i = 0, len = 0; CHAR_INFO info; - if (str == NULL) return 0; + if (str == NULL) + return 0; + while (str[i]) { - info.bytes = 0; - getUTF8EncodeInfo(str + i, &info); + getCharacterInfo(str + i, &info); i += info.bytes; - len++; + len += info.pixelWidth; } + return len; } + +// decode UTF-8 char display bit width +uint16_t GUI_StrPixelWidth_label(int16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText((uint8_t*)tempstr, index) == false) + return 0; + + return GUI_StrPixelWidth_str(tempstr); +} diff --git a/TFT/src/User/API/LevelingControl.c b/TFT/src/User/API/LevelingControl.c index b97f94bd07..fed99a853d 100644 --- a/TFT/src/User/API/LevelingControl.c +++ b/TFT/src/User/API/LevelingControl.c @@ -7,16 +7,16 @@ typedef struct XY_coord int16_t y_coord; } COORD, LEVELING_POINT_COORDS[LEVELING_POINT_COUNT]; -LEVELING_POINT probedPoint = LEVEL_NO_POINT; // last probed point or LEVEL_NO_POINT in case of no new updates -float probedZ = 0.0f; // last Z offset measured by probe +static LEVELING_POINT probedPoint = LEVEL_NO_POINT; // last probed point or LEVEL_NO_POINT in case of no new updates +static float probedZ = 0.0f; // last Z offset measured by probe -int16_t setCoordValue(AXIS axis, ALIGN_POSITION align) +static int16_t setCoordValue(AXIS axis, ALIGN_POSITION align) { return (align == LEFT || align == BOTTOM) ? infoSettings.machine_size_min[axis] + infoSettings.level_edge : infoSettings.machine_size_max[axis] - infoSettings.level_edge; } -void levelingGetPointCoords(LEVELING_POINT_COORDS coords) +static void levelingGetPointCoords(LEVELING_POINT_COORDS coords) { int16_t x_left = setCoordValue(X_AXIS, LEFT); int16_t x_right = setCoordValue(X_AXIS, RIGHT); @@ -45,7 +45,7 @@ void levelingGetPointCoords(LEVELING_POINT_COORDS coords) coords[LEVEL_CENTER] = (COORD){(x_left + x_right) / 2, (y_bottom + y_top) / 2}; } -LEVELING_POINT levelingGetPoint(int16_t x, int16_t y) +static LEVELING_POINT levelingGetPoint(int16_t x, int16_t y) { LEVELING_POINT_COORDS coords; uint8_t i; diff --git a/TFT/src/User/API/MachineParameters.c b/TFT/src/User/API/MachineParameters.c index 924b262257..0ca5970e06 100644 --- a/TFT/src/User/API/MachineParameters.c +++ b/TFT/src/User/API/MachineParameters.c @@ -3,7 +3,7 @@ #define MAX_ELEMENT_COUNT 10 -const uint8_t parameterElementCount[PARAMETERS_COUNT] = { +static const uint8_t parameterElementCount[PARAMETERS_COUNT] = { AXIS_INDEX_COUNT, // Steps/mm (X, Y, Z, E0, E1) 3, // Filament Diameter (Enable, E0, E1) AXIS_INDEX_COUNT, // MaxAcceleration (X, Y, Z, E0, E1) @@ -33,7 +33,7 @@ const uint8_t parameterElementCount[PARAMETERS_COUNT] = { 1 // MBL offset }; -const char * const parameterCode[PARAMETERS_COUNT] = { +static const char * const parameterCode[PARAMETERS_COUNT] = { "M92", // Steps/mm "M200", // Filament Diameter "M201", // MaxAcceleration @@ -63,7 +63,7 @@ const char * const parameterCode[PARAMETERS_COUNT] = { "G29", // MBL offset }; -const char * const parameterCmd[PARAMETERS_COUNT][MAX_ELEMENT_COUNT] = { +static const char * const parameterCmd[PARAMETERS_COUNT][MAX_ELEMENT_COUNT] = { {"X%.4f\n", "Y%.4f\n", "Z%.2f\n", "T0 E%.2f\n", "T1 E%.2f\n", NULL, NULL, NULL, NULL, NULL}, // Steps/mm (X, Y, Z, E0, E1) {"S%.0f\n", "S1 T0 D%.2f\n", "S1 T1 D%.2f\n", NULL, NULL, NULL, NULL, NULL, NULL, NULL}, // Filament Diameter (Enable, E0, E1) {"X%.0f\n", "Y%.0f\n", "Z%.0f\n", "T0 E%.0f\n", "T1 E%.0f\n", NULL, NULL, NULL, NULL, NULL}, // MaxAcceleration (X, Y, Z, E0, E1) @@ -93,7 +93,7 @@ const char * const parameterCmd[PARAMETERS_COUNT][MAX_ELEMENT_COUNT] = { {"S4 Z%.2f\nG29 S0\n", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, // MBL offset }; -const VAL_TYPE parameterValType[PARAMETERS_COUNT][MAX_ELEMENT_COUNT] = { +static const VAL_TYPE parameterValType[PARAMETERS_COUNT][MAX_ELEMENT_COUNT] = { {VAL_TYPE_FLOAT, VAL_TYPE_FLOAT, VAL_TYPE_FLOAT, VAL_TYPE_FLOAT, VAL_TYPE_FLOAT}, // Steps/mm (X, Y, Z, E0, E1) {VAL_TYPE_INT, VAL_TYPE_FLOAT, VAL_TYPE_FLOAT}, // Filament Diameter (Enable, E0, E1) {VAL_TYPE_INT, VAL_TYPE_INT, VAL_TYPE_INT, VAL_TYPE_INT, VAL_TYPE_INT}, // MaxAcceleration (X, Y, Z, E0, E1) @@ -127,16 +127,17 @@ const VAL_TYPE parameterValType[PARAMETERS_COUNT][MAX_ELEMENT_COUNT] = { {VAL_TYPE_NEG_FLOAT}, // MBL offset }; -PARAMETERS infoParameters; -uint32_t parametersEnabled = 0; -uint16_t elementEnabled[PARAMETERS_COUNT]; // parameterElementCount must be less than 16 +static uint32_t parametersEnabled = 0; +static uint16_t elementEnabled[PARAMETERS_COUNT]; // parameterElementCount must be less than 16 -#define ONOFF_DISPLAY_ID "1=ON 0=OFF" +PARAMETERS infoParameters; // param attributes multi purpose hard coded labels char * const axisDisplayID[AXIS_INDEX_COUNT] = AXIS_DISPLAY_ID; char * const stepperDisplayID[STEPPER_INDEX_COUNT] = STEPPER_DISPLAY_ID; +#define ONOFF_DISPLAY_ID "1=ON 0=OFF" + // param attributes hard coded labels char * const filamentDiaDisplayID[] = {"S " ONOFF_DISPLAY_ID, "T0 Ø Filament", "T1 Ø Filament"}; char * const autoRetractDisplayID[] = {"S " ONOFF_DISPLAY_ID}; @@ -172,7 +173,12 @@ static inline uint8_t getElementStatus(PARAMETER_NAME name, uint8_t element) uint8_t getEnabledElementCount(PARAMETER_NAME name) { uint8_t count = 0; - for (uint8_t i = 0; i < parameterElementCount[name]; i++) { count += GET_BIT(elementEnabled[name], i); } + + for (uint8_t i = 0; i < parameterElementCount[name]; i++) + { + count += GET_BIT(elementEnabled[name], i); + } + return count; } @@ -189,6 +195,7 @@ uint8_t getEnabledElement(PARAMETER_NAME name, uint8_t index) if (state && count == (index + 1)) return i; } + return parameterElementCount[name]; } @@ -205,7 +212,12 @@ static inline uint8_t getParameterStatus(PARAMETER_NAME name) uint8_t getEnabledParameterCount(void) { uint8_t count = 0; - for (uint8_t i = 0; i < PARAMETERS_COUNT; i++) { count += GET_BIT(parametersEnabled, i); } + + for (uint8_t i = 0; i < PARAMETERS_COUNT; i++) + { + count += GET_BIT(parametersEnabled, i); + } + return count; } @@ -222,6 +234,7 @@ PARAMETER_NAME getEnabledParameter(uint8_t index) if (state && count == (index + 1)) return i; } + return PARAMETERS_COUNT; } diff --git a/TFT/src/User/API/MachineParameters.h b/TFT/src/User/API/MachineParameters.h index 0c067b452a..d71a415b71 100644 --- a/TFT/src/User/API/MachineParameters.h +++ b/TFT/src/User/API/MachineParameters.h @@ -140,39 +140,39 @@ extern const LABEL junctionDeviationDisplayID[]; extern const LABEL retractDisplayID[]; extern const LABEL recoverDisplayID[]; -// Get enable element count for the parameter +// get enable element count for the parameter uint8_t getEnabledElementCount(PARAMETER_NAME name); -// Get element index out of total enabled enabled element +// get element index out of total enabled enabled element. // If no element is enabled, total element count is returned uint8_t getEnabledElement(PARAMETER_NAME name, uint8_t index); -// Get total enabled parameters +// get total enabled parameters uint8_t getEnabledParameterCount(void); -// Get PARAMETER_NAME of selected index out of total enabled parameters +// get PARAMETER_NAME of selected index out of total enabled parameters. // If no parameter is enabled, total parameter count is returned PARAMETER_NAME getEnabledParameter(uint8_t index); float getParameter(PARAMETER_NAME name, uint8_t index); void setParameter(PARAMETER_NAME name, uint8_t index, float val); -// Get total elements in a parameter +// get total elements in a parameter uint8_t getElementCount(PARAMETER_NAME para); -// Get type of value a parameter element holds +// get type of value a parameter element holds VAL_TYPE getParameterValType(PARAMETER_NAME para, uint8_t index); -// Send parameter cmd (Parameter value gets updated after the cmd passes through the cmd cache) +// send parameter cmd (parameter value gets updated after the cmd passes through the cmd cache) void sendParameterCmd(PARAMETER_NAME para_index, uint8_t stepper_index, float Value); -// Save parameter setting to eeprom +// save parameter setting to eeprom void saveEepromSettings(void); -// Restore settings from eeprom +// restore settings from eeprom void restoreEepromSettings(void); -// Reset settings and eeprom to default values +// reset settings and eeprom to default values void resetEepromSettings(void); #ifdef __cplusplus diff --git a/TFT/src/User/API/Mainboard_AckHandler.c b/TFT/src/User/API/Mainboard_AckHandler.c index fcbf5a01a6..fdf76da2cc 100644 --- a/TFT/src/User/API/Mainboard_AckHandler.c +++ b/TFT/src/User/API/Mainboard_AckHandler.c @@ -16,7 +16,7 @@ typedef struct } ECHO; // notify or ignore messages starting with following text -const ECHO knownEcho[] = { +static const ECHO knownEcho[] = { {ECHO_NOTIFY_NONE, "busy: paused for user"}, {ECHO_NOTIFY_NONE, "busy: processing"}, {ECHO_NOTIFY_NONE, "Now fresh file:"}, @@ -35,21 +35,21 @@ const ECHO knownEcho[] = { {ECHO_NOTIFY_NONE, "Unknown command: \"M150"}, // M150 }; -const char magic_error[] = "Error:"; -const char magic_echo[] = "echo:"; -const char magic_warning[] = "Warning:"; // RRF warning -const char magic_message[] = "message"; // RRF message in Json format +static const char magic_error[] = "Error:"; +static const char magic_echo[] = "echo:"; +static const char magic_warning[] = "Warning:"; // RRF warning +static const char magic_message[] = "message"; // RRF message in Json format // size of buffer where read ACK messages are stored (including terminating null character '\0') #define ACK_CACHE_SIZE 512 -char ack_cache[ACK_CACHE_SIZE]; // buffer where read ACK messages are stored -uint16_t ack_len; // length of data present in ack_cache without the terminating null character '\0' -uint16_t ack_index; -SERIAL_PORT_INDEX ack_port_index = PORT_1; // index of target serial port for the ACK message (related to originating gcode) -bool hostDialog = false; +static char ack_cache[ACK_CACHE_SIZE]; // buffer where read ACK messages are stored +static uint16_t ack_len; // length of data present in ack_cache without the terminating null character '\0' +static uint16_t ack_index; +static SERIAL_PORT_INDEX ack_port_index = PORT_1; // index of target serial port for the ACK message (related to originating gcode) +static bool hostDialog = false; -struct HOST_ACTION +static struct HOST_ACTION { char prompt_begin[30]; char prompt_button[2][20]; @@ -82,6 +82,7 @@ static bool ack_starts_with(const char * str) if (str[++i] == '\0') { ack_index = i; + return true; } } @@ -103,6 +104,7 @@ static bool ack_seen(const char * str) if (str[++i] == '\0') { ack_index += i; + return true; } } @@ -125,6 +127,7 @@ static bool ack_continue_seen(const char * str) if (str[++i] == '\0') { ack_index = tmp_index + i; + return true; } } @@ -133,13 +136,13 @@ static bool ack_continue_seen(const char * str) return false; } -static float ack_value() +static float ack_value(void) { return (strtod(&ack_cache[ack_index], NULL)); } // read the value after "/", if any -static float ack_second_value() +static float ack_second_value(void) { char * secondValue = strchr(&ack_cache[ack_index], '/'); @@ -149,7 +152,7 @@ static float ack_second_value() return -0.5; } -void ack_values_sum(float * data) +static void ack_values_sum(float * data) { while (((ack_cache[ack_index] < '0') || (ack_cache[ack_index] > '9')) && ack_cache[ack_index] != '\n') { @@ -168,7 +171,7 @@ void ack_values_sum(float * data) ack_values_sum(data); } -void ackPopupInfo(const char * info) +static void ackPopupInfo(const char * info) { bool show_dialog = true; @@ -272,11 +275,11 @@ static inline void hostActionCommands(void) } } } - else if (ack_seen(":print_start")) // print started from remote host (e.g. OctoPrint etc...) + else if (ack_seen(":print_start")) // print started from remote host (e.g. OctoPrint etc.) { startPrintingFromRemoteHost(NULL); // start print originated and hosted by remote host and open Printing menu } - else if (ack_seen(":print_end")) // print ended from remote host (e.g. OctoPrint etc...) + else if (ack_seen(":print_end")) // print ended from remote host (e.g. OctoPrint etc.) { endPrint(); } diff --git a/TFT/src/User/API/Mainboard_CmdControl.c b/TFT/src/User/API/Mainboard_CmdControl.c index b85b48eb57..7fd2a0598a 100644 --- a/TFT/src/User/API/Mainboard_CmdControl.c +++ b/TFT/src/User/API/Mainboard_CmdControl.c @@ -4,9 +4,9 @@ // line number of last command properly processed by mainboard, // base line number and line number of last command sent by TFT respectively. // Required COMMAND_CHECKSUM feature enabled in TFT -static uint32_t cmd_line_number_ok = NO_LINE_NUMBER; +static uint32_t cmd_line_number_ok = NO_LINE_NUMBER; static uint32_t cmd_line_number_base = 0; -static uint32_t cmd_line_number = 0; +static uint32_t cmd_line_number = 0; uint32_t getCmdLineNumberOk(void) { diff --git a/TFT/src/User/API/Mainboard_CmdHandler.c b/TFT/src/User/API/Mainboard_CmdHandler.c index b6e37040a2..888661da83 100644 --- a/TFT/src/User/API/Mainboard_CmdHandler.c +++ b/TFT/src/User/API/Mainboard_CmdHandler.c @@ -8,7 +8,7 @@ typedef struct { CMD gcode; - SERIAL_PORT_INDEX port_index; // 0: for SERIAL_PORT, 1: for SERIAL_PORT_2 etc... + SERIAL_PORT_INDEX port_index; // 0: for SERIAL_PORT, 1: for SERIAL_PORT_2 etc. } GCODE_INFO; typedef struct @@ -34,16 +34,16 @@ typedef enum ONBOARD_WRITING } WRITING_MODE; -GCODE_QUEUE cmdQueue; // command queue where commands to be sent are stored -GCODE_RETRY_INFO cmdRetryInfo = {0}; // command retry info. Required COMMAND_CHECKSUM feature enabled in TFT +static GCODE_QUEUE cmdQueue; // command queue where commands to be sent are stored +static GCODE_RETRY_INFO cmdRetryInfo = {0}; // command retry info. Required COMMAND_CHECKSUM feature enabled in TFT -char * cmd_ptr; -uint8_t cmd_len; -SERIAL_PORT_INDEX cmd_port_index; // index of serial port originating the gcode -uint8_t cmd_base_index; // base index in case the gcode has checksum ("Nxx " is present at the beginning of gcode) -uint8_t cmd_index; -WRITING_MODE writing_mode = NO_WRITING; // writing mode. Used by M28 and M29 -FIL file; +static char * cmd_ptr; +static uint8_t cmd_len; +static SERIAL_PORT_INDEX cmd_port_index; // index of serial port originating the gcode +static uint8_t cmd_base_index; // base index in case the gcode has checksum ("Nxx " is present at the beginning of gcode) +static uint8_t cmd_index; +static WRITING_MODE writing_mode = NO_WRITING; // writing mode. Used by M28 and M29 +static FIL file; uint8_t getQueueCount(void) { @@ -82,7 +82,7 @@ bool isWritingMode(void) } // Common store cmd. -void commonStoreCmd(GCODE_QUEUE * pQueue, const char * format, va_list va) +static void commonStoreCmd(GCODE_QUEUE * pQueue, const char * format, va_list va) { vsnprintf(pQueue->queue[pQueue->index_w].gcode, CMD_MAX_SIZE, format, va); @@ -163,7 +163,7 @@ void mustStoreScript(const char * format, ...) } } -// Store gcode cmd received from UART (e.g. ESP3D, OctoPrint, other TouchScreen etc...) to cmdQueue queue. +// Store gcode cmd received from UART (e.g. ESP3D, OctoPrint, other TouchScreen etc.) to cmdQueue queue. // This command will be sent to the printer by sendQueueCmd(). // If the cmdQueue queue is full, a reminder message is displayed and the command is discarded. bool storeCmdFromUART(const CMD cmd, const SERIAL_PORT_INDEX portIndex) @@ -193,7 +193,7 @@ void clearCmdQueue(void) // Strip out any leading space from the passed command. // Furthermore, skip any N[-0-9] (line number) and return a pointer to the beginning of the command. -char * stripCmd(char * cmdPtr) +static char * stripCmd(char * cmdPtr) { // skip leading spaces while (*cmdPtr == ' ') cmdPtr++; // e.g. " N1 G28*18\n" -> "N1 G28*18\n" @@ -249,7 +249,7 @@ static inline void setCmdRetryInfo(uint32_t lineNumber) } // Purge gcode cmd or send it to the printer and then remove it from cmdQueue queue. -bool sendCmd(bool purge, bool avoidTerminal) +static bool sendCmd(bool purge, bool avoidTerminal) { char * purgeStr = "[Purged] "; @@ -323,6 +323,7 @@ static bool cmd_seen_from(uint8_t index, const char * keyword) if (keyword[++i] == '\0') { cmd_index = index + i; + return true; } } @@ -368,7 +369,7 @@ static float cmd_float(void) return (strtod(&cmd_ptr[cmd_index], NULL)); } -bool initRemoteTFT() +static bool initRemoteTFT(void) { // examples: // @@ -401,7 +402,7 @@ bool initRemoteTFT() return true; } -bool openRemoteTFT(bool writingMode) +static bool openRemoteTFT(bool writingMode) { bool open = false; @@ -453,7 +454,7 @@ bool openRemoteTFT(bool writingMode) return open; } -static inline void writeRemoteTFT() +static inline void writeRemoteTFT(void) { // examples: // @@ -489,7 +490,7 @@ static inline void writeRemoteTFT() Serial_Forward(cmd_port_index, "ok\n"); } -void setWaitHeating(uint8_t index) +static void setWaitHeating(uint8_t index) { if (cmd_seen('R')) { @@ -502,7 +503,7 @@ void setWaitHeating(uint8_t index) } } -void syncTargetTemp(uint8_t index) +static void syncTargetTemp(uint8_t index) { uint16_t temp; diff --git a/TFT/src/User/API/Mainboard_FlowControl.c b/TFT/src/User/API/Mainboard_FlowControl.c index 2d51e6902a..5529310412 100644 --- a/TFT/src/User/API/Mainboard_FlowControl.c +++ b/TFT/src/User/API/Mainboard_FlowControl.c @@ -7,7 +7,7 @@ PRIORITY_COUNTER priorityCounter; HOST infoHost; MENU infoMenu; -void resetInfoQueries(void) +static inline void resetInfoQueries(void) { fanResetSpeed(); coordinateSetKnown(false); @@ -44,7 +44,7 @@ void loopBackEnd(void) // parse the received slave response information parseAck(); - // retrieve and store (in command queue) the gcodes received from other UART, such as ESP3D etc... + // retrieve and store (in command queue) the gcodes received from other UART, such as ESP3D etc. #ifdef SERIAL_PORT_2 Serial_GetFromUART(); #endif @@ -153,7 +153,7 @@ void loopProcess(void) loopFrontEnd(); } -void menuDummy(void) +static void menuDummy(void) { CLOSE_MENU(); } diff --git a/TFT/src/User/API/ModeSwitching.c b/TFT/src/User/API/ModeSwitching.c index 29269b95ff..9464ddf36e 100644 --- a/TFT/src/User/API/ModeSwitching.c +++ b/TFT/src/User/API/ModeSwitching.c @@ -1,7 +1,8 @@ #include "ModeSwitching.h" #include "includes.h" -bool modeFreshBoot = true; +static bool modeFreshBoot = true; + bool modeSwitching = false; // change UI mode diff --git a/TFT/src/User/API/Notification.c b/TFT/src/User/API/Notification.c index 8b5c09e19c..8721cecb46 100644 --- a/TFT/src/User/API/Notification.c +++ b/TFT/src/User/API/Notification.c @@ -3,8 +3,8 @@ #include "my_misc.h" // area for toast notification -const GUI_RECT toastRect = {START_X + TITLE_END_Y - (TOAST_Y_PAD * 2), TOAST_Y_PAD, LCD_WIDTH - START_X, TITLE_END_Y - TOAST_Y_PAD}; -const GUI_RECT toastIconRect = {START_X, TOAST_Y_PAD, START_X + TITLE_END_Y - (TOAST_Y_PAD * 2), TITLE_END_Y - TOAST_Y_PAD}; +static const GUI_RECT toastRect = {START_X + TITLE_END_Y - (TOAST_Y_PAD * 2), TOAST_Y_PAD, LCD_WIDTH - START_X, TITLE_END_Y - TOAST_Y_PAD}; +static const GUI_RECT toastIconRect = {START_X, TOAST_Y_PAD, START_X + TITLE_END_Y - (TOAST_Y_PAD * 2), TITLE_END_Y - TOAST_Y_PAD}; typedef struct { diff --git a/TFT/src/User/API/PowerFailed.c b/TFT/src/User/API/PowerFailed.c index f1ab46cb45..79945c4048 100644 --- a/TFT/src/User/API/PowerFailed.c +++ b/TFT/src/User/API/PowerFailed.c @@ -18,9 +18,9 @@ typedef struct bool pause; } BREAK_POINT; -BREAK_POINT infoBreakPoint; -char powerFailedFileName[sizeof(BREAK_POINT_FILE) + 20 + 1]; // extra 20 chars for concatenation with string returned by getFS() -FIL fpPowerFailed; +static BREAK_POINT infoBreakPoint; +static char powerFailedFileName[sizeof(BREAK_POINT_FILE) + 20 + 1]; // extra 20 chars for concatenation with string returned by getFS() +static FIL fpPowerFailed; static bool restore = false; // print restore flag disabled by default static bool restore_ok = false; // print restore initialization flag disabled by default @@ -37,12 +37,12 @@ bool powerFailedGetRestore(void) return restore; } -void powerFailedSetDriverSource(void) +static void powerFailedSetDriverSource(void) { sprintf(powerFailedFileName, "%s%s", getFS(), BREAK_POINT_FILE); } -bool powerFailedLoad(FIL *print_fp) +bool powerFailedLoad(FIL * print_fp) { // set status flag first load_ok = false; @@ -106,13 +106,13 @@ bool powerFailedInitRestore(void) mustStoreCmd("%s\n", toolChange[infoBreakPoint.tool]); - for (uint8_t i = MAX_HEATER_COUNT - 1; i >= MAX_HOTEND_COUNT; i--) // Bed & Chamber + for (uint8_t i = MAX_HEATER_COUNT - 1; i >= MAX_HOTEND_COUNT; i--) // bed & chamber { if (infoBreakPoint.target[i] != 0) mustStoreCmd("%s S%d\n", heatWaitCmd[i], infoBreakPoint.target[i]); } - for (int8_t i = infoSettings.hotend_count - 1; i >= 0; i--) // Tool nozzle + for (int8_t i = infoSettings.hotend_count - 1; i >= 0; i--) // tool nozzle { if (infoBreakPoint.target[i] != 0) mustStoreCmd("%s S%d\n", heatWaitCmd[i], infoBreakPoint.target[i]); @@ -175,7 +175,7 @@ bool powerFailedExist(void) return access_ok; } -void powerFailedCreate(char *path) +void powerFailedCreate(char * path) { powerFailedDelete(); // close and delete PLR file, if any, first @@ -231,15 +231,15 @@ void powerFailedCache(uint32_t offset) } infoBreakPoint.feedrate = coordinateGetFeedRate(); - infoBreakPoint.speed = speedGetCurPercent(0); // Move speed percent - infoBreakPoint.flow = speedGetCurPercent(1); // Flow percent + infoBreakPoint.speed = speedGetCurPercent(0); // speed percent + infoBreakPoint.flow = speedGetCurPercent(1); // flow percent - for (uint8_t i = 0; i < infoSettings.hotend_count; i++) // Tool nozzle + for (uint8_t i = 0; i < infoSettings.hotend_count; i++) // tool nozzle { infoBreakPoint.target[i] = heatGetTargetTemp(i); } - for (uint8_t i = MAX_HOTEND_COUNT; i < MAX_HEATER_COUNT; i++) // Bed & Chamber + for (uint8_t i = MAX_HOTEND_COUNT; i < MAX_HEATER_COUNT; i++) // bed & chamber { infoBreakPoint.target[i] = heatGetTargetTemp(i); } diff --git a/TFT/src/User/API/PowerFailed.h b/TFT/src/User/API/PowerFailed.h index 01a31d33f1..148cc89b2d 100644 --- a/TFT/src/User/API/PowerFailed.h +++ b/TFT/src/User/API/PowerFailed.h @@ -25,10 +25,10 @@ bool powerFailedInitRestore(void); // initialize print restore, if any, if not * It finally disables print restore flag (one shot flag) for the next print. * The flag must always be explicitly re-enabled (e.g by powerFailedSetRestore function) */ -bool powerFailedLoad(FIL *print_fp); +bool powerFailedLoad(FIL * print_fp); bool powerFailedExist(void); -void powerFailedCreate(char *path); +void powerFailedCreate(char * path); void powerFailedCache(uint32_t offset); void powerFailedDelete(void); diff --git a/TFT/src/User/API/Printing.c b/TFT/src/User/API/Printing.c index e3b2e72b1f..49b5601e53 100644 --- a/TFT/src/User/API/Printing.c +++ b/TFT/src/User/API/Printing.c @@ -21,9 +21,7 @@ typedef struct PAUSE_TYPE pauseType; // pause type trigged by different sources and gcodes like M0 & M600 } PRINTING; -PRINTING infoPrinting = {0}; -PRINT_SUMMARY infoPrintSummary = {.name[0] = '\0', 0, 0, 0, 0, false}; - +static PRINTING infoPrinting = {0}; static bool extrusionDuringPause = false; // flag for extrusion during Print -> Pause static bool filamentRunoutAlarm = false; static float lastEPos = 0; // used only to update stats in infoPrintSummary @@ -31,6 +29,8 @@ static float lastEPos = 0; // used only to update stats in infoP static uint32_t nextUpdateTime = 0; static bool sendingWaiting = false; +PRINT_SUMMARY infoPrintSummary = {.name[0] = '\0', 0, 0, 0, 0, false}; + void setExtrusionDuringPause(bool extruded) { extrusionDuringPause = extruded; @@ -51,7 +51,7 @@ bool getRunoutAlarm(void) return filamentRunoutAlarm; } -void clearQueueAndMore(void) +static void clearQueueAndMore(void) { clearCmdQueue(); resetPendingQueries(); @@ -76,7 +76,7 @@ void resumeAndContinue(void) sendEmergencyCmd("M876 S1\n"); } -void abortAndTerminate(void) +static void abortAndTerminate(void) { clearQueueAndMore(); @@ -93,7 +93,7 @@ void abortAndTerminate(void) } } -void waitForAbort(void) +static void waitForAbort(void) { // M108 is sent to Marlin because consecutive blocking operations such as heating bed/extruder may defer processing of other gcodes. // If there's any ongoing blocking command, "M108" will take that out from the closed loop and a response will be received @@ -164,7 +164,7 @@ void parsePrintRemainingTime(char * buffer) setPrintRemainingTime(((int32_t) (hour) * 3600) + ((int32_t) (min) * 60) + (int32_t) (sec)); } -uint32_t getPrintRemainingTime() +uint32_t getPrintRemainingTime(void) { return infoPrinting.remainingTime; } @@ -174,7 +174,7 @@ void setPrintLayerNumber(uint16_t layerNumber) infoPrinting.layerNumber = layerNumber; } -uint16_t getPrintLayerNumber() +uint16_t getPrintLayerNumber(void) { return infoPrinting.layerNumber; } @@ -184,7 +184,7 @@ void setPrintLayerCount(uint16_t layerCount) infoPrinting.layerCount = layerCount; } -uint16_t getPrintLayerCount() +uint16_t getPrintLayerCount(void) { return infoPrinting.layerCount; } @@ -260,9 +260,9 @@ bool getPrintRunout(void) return infoPrinting.runout; } -// Shut down menu, when the hotend temperature is higher than "AUTO_SHUT_DOWN_MAXTEMP" +// shut down menu, when the hotend temperature is higher than "AUTO_SHUT_DOWN_MAXTEMP" // wait for cool down, in the meantime, you can shut down by force -void shutdown(void) +static void shutdown(void) { for (uint8_t i = 0; i < infoSettings.fan_count; i++) { @@ -273,7 +273,7 @@ void shutdown(void) popupReminder(DIALOG_TYPE_INFO, LABEL_SHUT_DOWN, LABEL_SHUTTING_DOWN); } -void shutdownLoop(void) +static void shutdownLoop(void) { for (uint8_t i = NOZZLE0; i < infoSettings.hotend_count; i++) { @@ -284,7 +284,7 @@ void shutdownLoop(void) shutdown(); } -void shutdownStart(void) +static void shutdownStart(void) { char tempstr[75]; @@ -299,7 +299,7 @@ void shutdownStart(void) popupDialog(DIALOG_TYPE_INFO, LABEL_SHUT_DOWN, (uint8_t *)tempstr, LABEL_FORCE_SHUT_DOWN, LABEL_CANCEL, shutdown, NULL, shutdownLoop); } -void initPrintSummary(void) +static void initPrintSummary(void) { lastEPos = coordinateGetAxis(E_AXIS); infoPrintSummary = (PRINT_SUMMARY){.name[0] = '\0', 0, 0, 0, 0, false}; @@ -308,7 +308,7 @@ void initPrintSummary(void) strncpy_no_pad(infoPrintSummary.name, getPrintFilename(), SUMMARY_NAME_LEN); } -void preparePrintSummary(void) +static void preparePrintSummary(void) { infoPrintSummary.time = infoPrinting.elapsedTime; @@ -321,7 +321,7 @@ void preparePrintSummary(void) } // send print codes [0: start gcode, 1: end gcode 2: cancel gcode] -void sendPrintCodes(uint8_t index) +static void sendPrintCodes(uint8_t index) { PRINT_GCODES printcodes; @@ -362,7 +362,7 @@ void clearInfoPrint(void) memset(&infoPrinting, 0, sizeof(PRINTING)); } -void completePrint(void) +static void completePrint(void) { infoPrinting.cur = infoPrinting.size; // always update the print progress to 100% even if the print terminated infoPrinting.progress = 100; // set progress to 100% in case progress is controlled by slicer @@ -518,6 +518,7 @@ void endPrint(void) if (!infoPrinting.printing) { infoHost.status = HOST_STATUS_IDLE; + return; } @@ -754,6 +755,7 @@ void setPrintAbort(void) if (!infoPrinting.printing) { infoHost.status = HOST_STATUS_IDLE; + return; } diff --git a/TFT/src/User/API/Printing.h b/TFT/src/User/API/Printing.h index ba382b87b0..449014a431 100644 --- a/TFT/src/User/API/Printing.h +++ b/TFT/src/User/API/Printing.h @@ -72,13 +72,13 @@ uint32_t getPrintTime(void); void setPrintRemainingTime(int32_t remainingTime); // used for M73 Rxx and M117 Time Left xx void parsePrintRemainingTime(char * buffer); // used for M117 Time Left xx -uint32_t getPrintRemainingTime(); +uint32_t getPrintRemainingTime(void); void setPrintLayerNumber(uint16_t layerNumber); -uint16_t getPrintLayerNumber(); +uint16_t getPrintLayerNumber(void); void setPrintLayerCount(uint16_t layerCount); -uint16_t getPrintLayerCount(); +uint16_t getPrintLayerCount(void); void setPrintProgressSource(PROG_FROM progressSource); PROG_FROM getPrintProgressSource(void); diff --git a/TFT/src/User/API/ProbeHeightControl.c b/TFT/src/User/API/ProbeHeightControl.c index b7e58322ef..cd1f576524 100644 --- a/TFT/src/User/API/ProbeHeightControl.c +++ b/TFT/src/User/API/ProbeHeightControl.c @@ -10,7 +10,7 @@ static uint8_t origEndstopsState = DISABLED; static float origAblState = DISABLED; -// Enable probe height +// Enable probe height. // Temporary disable software endstops and save ABL state void probeHeightEnable(void) { @@ -26,7 +26,7 @@ void probeHeightEnable(void) } } -// Disable probe height +// Disable probe height. // Restore original software endstops state and ABL state void probeHeightDisable(void) { diff --git a/TFT/src/User/API/ProbeHeightControl.h b/TFT/src/User/API/ProbeHeightControl.h index da074706c3..6394470e66 100644 --- a/TFT/src/User/API/ProbeHeightControl.h +++ b/TFT/src/User/API/ProbeHeightControl.h @@ -8,11 +8,11 @@ extern "C" { #include #include -// Enable probe height +// Enable probe height. // Temporary disable software endstops and save ABL state void probeHeightEnable(void); -// Disable probe height +// Disable probe height. // Restore original software endstops state and ABL state void probeHeightDisable(void); diff --git a/TFT/src/User/API/RRFAckHandler.cpp b/TFT/src/User/API/RRFAckHandler.cpp index fa2e0ec177..4efd046c4c 100644 --- a/TFT/src/User/API/RRFAckHandler.cpp +++ b/TFT/src/User/API/RRFAckHandler.cpp @@ -74,7 +74,7 @@ static void m291_loop(void) } } -void ParseACKJsonParser::endDocument() +void ParseACKJsonParser::endDocument(void) { requestCommandInfo.inJson = false; @@ -112,11 +112,11 @@ void ParseACKJsonParser::endDocument() need_parser_reset = true; } -void ParseACKJsonParser::value(const char *value) +void ParseACKJsonParser::value(const char * value) { uint32_t seq; - char *string_end; - char *string_start; + char * string_end; + char * string_start; switch (state) { @@ -201,12 +201,12 @@ void ParseACKJsonParser::value(const char *value) break; case mbox_msg: - m291_msg = (char*)malloc(strlen(value) + 1); + m291_msg = (char *)malloc(strlen(value) + 1); strcpy(m291_msg, value); break; case mbox_title: - m291_title = (char*)malloc(strlen(value) + 1); + m291_title = (char *)malloc(strlen(value) + 1); strcpy(m291_title, value); break; @@ -272,7 +272,7 @@ void ParseACKJsonParser::value(const char *value) ++index; } -void rrfParseAck(const char *data) +void rrfParseAck(const char * data) { static ParseACKJsonParser handler; diff --git a/TFT/src/User/API/RRFAckHandler.hpp b/TFT/src/User/API/RRFAckHandler.hpp index 030e550904..1470e37213 100644 --- a/TFT/src/User/API/RRFAckHandler.hpp +++ b/TFT/src/User/API/RRFAckHandler.hpp @@ -5,7 +5,7 @@ extern "C" { #endif - void rrfParseAck(const char *data); + void rrfParseAck(const char * data); #ifdef __cplusplus } #endif @@ -68,8 +68,8 @@ class ParseACKJsonParser : public JsonListener uint16_t index = 0; - char *m291_msg = NULL; - char *m291_title = NULL; + char * m291_msg = NULL; + char * m291_title = NULL; uint32_t m291_timeo = 0; public: @@ -78,7 +78,7 @@ class ParseACKJsonParser : public JsonListener inline void startObject() {} inline void endObject() {} inline void whitespace(char c) {} - virtual void endDocument(); + virtual void endDocument(void); inline void startArray() { @@ -91,7 +91,7 @@ class ParseACKJsonParser : public JsonListener in_array = false; } - inline void key(const char *key) + inline void key(const char * key) { if (strcmp(STATUS, key) == 0) { @@ -167,7 +167,7 @@ class ParseACKJsonParser : public JsonListener } } - virtual void value(const char *value); + virtual void value(const char * value); }; #endif diff --git a/TFT/src/User/API/RRFM20Parser.cpp b/TFT/src/User/API/RRFM20Parser.cpp index 1b4f3c7cbb..973301a4fc 100644 --- a/TFT/src/User/API/RRFM20Parser.cpp +++ b/TFT/src/User/API/RRFM20Parser.cpp @@ -37,7 +37,7 @@ } */ -const TCHAR *skip_number(const TCHAR *value) +static const TCHAR * skip_number(const TCHAR * value) { if (isdigit(*value)) { @@ -52,7 +52,7 @@ const TCHAR *skip_number(const TCHAR *value) return value; } -int compare_items(void *arg, const void *a, const void *b) +static int compare_items(void * arg, const void * a, const void * b) { bool macro_sort = *(bool *)arg; @@ -78,12 +78,12 @@ int compare_items(void *arg, const void *a, const void *b) return strcmp(((M20_LIST_ITEM *)a)->file_name, ((M20_LIST_ITEM *)b)->file_name); } -void RRFM20Parser::startObject() +void RRFM20Parser::startObject(void) { in_object = in_files; } -void RRFM20Parser::endObject() +void RRFM20Parser::endObject(void) { in_object = false; @@ -91,7 +91,7 @@ void RRFM20Parser::endObject() ++fileCount; } -void RRFM20Parser::endDocument() +void RRFM20Parser::endDocument(void) { if (macro_sort) { @@ -142,7 +142,7 @@ void RRFM20Parser::endDocument() // TODO handle `next` parameter when there are too many files to list // above ~8k response payloads result in pagination that needs handling -void RRFM20Parser::key(const char *key) +void RRFM20Parser::key(const char * key) { state = none; @@ -160,7 +160,7 @@ void RRFM20Parser::key(const char *key) } } -void RRFM20Parser::value(const char *value) +void RRFM20Parser::value(const char * value) { if (!in_files) return; @@ -183,7 +183,7 @@ void RRFM20Parser::value(const char *value) if ((fileList[fileCount].file_name = (TCHAR *)malloc(len)) != NULL) strcpy(fileList[fileCount].file_name, value); - const char *skipped = macro_sort ? skip_number(value) : value; + const char * skipped = macro_sort ? skip_number(value) : value; len = strlen(skipped) + 1; if (macro_sort && value != skipped && (fileList[fileCount].display_name = (TCHAR *)malloc(len)) != NULL) @@ -196,7 +196,7 @@ void RRFM20Parser::value(const char *value) case date: { // convert to uint to enable sorting: 2021-08-11T07:06:28 - char *out; + char * out; uint8_t year = strtol(value, &out, 10) - 1970; // will allow up to year 2225 uint8_t mnth = strtol(out + 1, &out, 10); uint8_t date = strtol(out + 1, &out, 10); @@ -239,9 +239,9 @@ void RRFM20Parser::value(const char *value) } } -void parseM20Response(const char *data, bool macro_sorting) +static void parseM20Response(const char * data, bool macro_sorting) { - static RRFM20Parser *handler = NULL; + static RRFM20Parser * handler = NULL; if (handler == NULL) handler = new RRFM20Parser; @@ -262,12 +262,12 @@ void parseM20Response(const char *data, bool macro_sorting) } } -void parseMacroListResponse(const char *data) +void parseMacroListResponse(const char * data) { parseM20Response(data, true); } -void parseJobListResponse(const char *data) +void parseJobListResponse(const char * data) { parseM20Response(data, false); } diff --git a/TFT/src/User/API/RRFM20Parser.hpp b/TFT/src/User/API/RRFM20Parser.hpp index e0709f5754..e24907744a 100644 --- a/TFT/src/User/API/RRFM20Parser.hpp +++ b/TFT/src/User/API/RRFM20Parser.hpp @@ -14,13 +14,13 @@ extern "C" typedef struct { bool is_directory; - TCHAR *display_name; - TCHAR *file_name; + TCHAR * display_name; + TCHAR * file_name; uint32_t timestamp; } M20_LIST_ITEM; - void parseJobListResponse(const char *data); - void parseMacroListResponse(const char *data); + void parseJobListResponse(const char * data); + void parseMacroListResponse(const char * data); #ifdef __cplusplus } #endif @@ -53,12 +53,12 @@ class RRFM20Parser : public JsonListener virtual ~RRFM20Parser() {} inline void startDocument() {} - virtual void startObject(); - virtual void endObject(); + virtual void startObject(void); + virtual void endObject(void); inline void whitespace(char c) {} - virtual void endDocument(); - virtual void key(const char *key); - virtual void value(const char *value); + virtual void endDocument(void); + virtual void key(const char * key); + virtual void value(const char * value); inline void reset() { diff --git a/TFT/src/User/API/RRFStatusControl.c b/TFT/src/User/API/RRFStatusControl.c index 8313b2d3ac..91cc4195a7 100644 --- a/TFT/src/User/API/RRFStatusControl.c +++ b/TFT/src/User/API/RRFStatusControl.c @@ -12,6 +12,7 @@ static bool was_printing = false; static uint16_t rrf_query_interval = RRF_NORMAL_STATUS_QUERY_MS; static bool macro_busy = false; + bool starting_print = false; void rrfStatusSet(char status) diff --git a/TFT/src/User/API/ScreenShot.c b/TFT/src/User/API/ScreenShot.c index 0346176afc..33a49d43e0 100644 --- a/TFT/src/User/API/ScreenShot.c +++ b/TFT/src/User/API/ScreenShot.c @@ -31,11 +31,11 @@ typedef struct __attribute__((__packed__)) #define ALIGNMENT_4BYTE(n) (((n + 3) >> 2) << 2) -bool screenShotBMP(char *bmp) +bool screenShotBMP(char * bmp) { BMP_FILEHEADER bmp_file = { .bfType = 0x4D42, // "BM" - .bfSize = (ALIGNMENT_4BYTE(LCD_WIDTH) * LCD_HEIGHT) * 3 + sizeof(BMP_FILEHEADER) + sizeof(BMP_INFOHEADER), + .bfSize = (ALIGNMENT_4BYTE(LCD_WIDTH) * LCD_HEIGHT) * 3 + sizeof(BMP_FILEHEADER) + sizeof(BMP_INFOHEADER), .bfReserved1 = 0, .bfReserved2 = 0, .bfoffBits = sizeof(BMP_FILEHEADER) + sizeof(BMP_INFOHEADER), @@ -59,9 +59,7 @@ bool screenShotBMP(char *bmp) UINT mybw; if (f_open(&bmpFile, bmp, FA_CREATE_NEW | FA_WRITE) != FR_OK) - { - return false; // Create failed, filename maybe existed. - } + return false; // Create failed, filename maybe existed f_write(&bmpFile, &bmp_file, sizeof(BMP_FILEHEADER), &mybw); f_write(&bmpFile, &bmp_info, sizeof(BMP_INFOHEADER), &mybw); @@ -69,6 +67,7 @@ bool screenShotBMP(char *bmp) for (uint16_t y = 0; y < LCD_HEIGHT; y++) { f_lseek(&bmpFile, bmp_file.bfoffBits + (LCD_HEIGHT - y - 1) * (bmp_info.biWidth * bmp_info.biBitCount / 8)); + for (uint16_t x = 0; x < LCD_WIDTH; x++) { uint32_t c = LCD_ReadPixel_24Bit(x, y); @@ -77,6 +76,7 @@ bool screenShotBMP(char *bmp) } f_close(&bmpFile); + return true; } @@ -91,7 +91,7 @@ void loopScreenShot(void) * - A long press of 1.5 seconds on touch screen to trigger a screenshot */ #if LCD_ENCODER_SUPPORT - bool (*screenShotTriggered)(uint16_t ) = (MENU_IS(menuMarlinMode)) ? Touch_Enc_ReadPen : LCD_Enc_ReadBtn; + bool (* screenShotTriggered)(uint16_t ) = (MENU_IS(menuMarlinMode)) ? Touch_Enc_ReadPen : LCD_Enc_ReadBtn; #define SCREEN_SHOT_TRIGGERED() screenShotTriggered(LCD_ENC_BUTTON_INTERVAL) #else @@ -106,12 +106,12 @@ void loopScreenShot(void) char screenShotFileName[FF_LFN_BUF] = "ScreenShot"; if (!f_dir_exists(screenShotFileName)) - { f_mkdir(screenShotFileName); - } + strcat(screenShotFileName, "/"); uint16_t i = strlen(screenShotFileName); + switch (getMenuType()) { case MENU_TYPE_ICON: @@ -130,13 +130,12 @@ void loopScreenShot(void) for (uint16_t j = strlen(screenShotFileName); j >= i; j--) { if (screenShotFileName[j] == '/') - { screenShotFileName[j] = '_'; - } } uint8_t index = 0; char fileName[FF_LFN_BUF]; + do { sprintf(fileName, "%s_%d.bmp", screenShotFileName, index); index++; diff --git a/TFT/src/User/API/ScreenShot.h b/TFT/src/User/API/ScreenShot.h index 095a0fb4fe..eb81ce1fa9 100644 --- a/TFT/src/User/API/ScreenShot.h +++ b/TFT/src/User/API/ScreenShot.h @@ -9,7 +9,7 @@ extern "C" { #include "variants.h" // for SCREEN_SHOT_TO_SD #ifdef SCREEN_SHOT_TO_SD - bool screenShotBMP(char *bmp); + bool screenShotBMP(char * bmp); void loopScreenShot(void); #endif diff --git a/TFT/src/User/API/SerialConnection.h b/TFT/src/User/API/SerialConnection.h index 69104da579..5eec11997e 100644 --- a/TFT/src/User/API/SerialConnection.h +++ b/TFT/src/User/API/SerialConnection.h @@ -9,7 +9,6 @@ extern "C" { #include #include "variants.h" // for SERIAL_PORT_2 etc. #include "Serial.h" // for dmaL1DataTX etc. -#include "uart.h" // for _UART_CNT etc. #define BAUDRATE_COUNT 12 @@ -32,14 +31,14 @@ typedef enum typedef struct { - uint8_t port; // physical port (e.g. _USART1) related to serial port (e.g. 0 for SERIAL_PORT, 1 for SERIAL_PORT_2 etc...) + uint8_t port; // physical port (e.g. _USART1) related to serial port (e.g. 0 for SERIAL_PORT, 1 for SERIAL_PORT_2 etc.) uint16_t cacheSizeRX; // buffer size for receiving data from the serial port uint16_t cacheSizeTX; // buffer size for sending data to the serial port - const char * const id; // serial port ID (e.g. "" for SERIAL_PORT, "2" for SERIAL_PORT_2 etc...) - const char * const desc; // serial port description (e.g. "1 - Printer" for SERIAL_PORT, "2 - WIFI" for SERIAL_PORT_2 etc...) + const char * const id; // serial port ID (e.g. "" for SERIAL_PORT, "2" for SERIAL_PORT_2 etc.) + const char * const desc; // serial port description (e.g. "1 - Printer" for SERIAL_PORT, "2 - WIFI" for SERIAL_PORT_2 etc.) } SERIAL_PORT_INFO; // serial port info -extern const SERIAL_PORT_INFO serialPort[SERIAL_PORT_COUNT]; // serial port (index 0 for SERIAL_PORT, 1 for SERIAL_PORT_2 etc...) +extern const SERIAL_PORT_INFO serialPort[SERIAL_PORT_COUNT]; // serial port (index 0 for SERIAL_PORT, 1 for SERIAL_PORT_2 etc.) extern const uint32_t baudrateValues[BAUDRATE_COUNT]; // baudrate values extern const char * const baudrateNames[BAUDRATE_COUNT]; // baudrate names diff --git a/TFT/src/User/API/Settings.c b/TFT/src/User/API/Settings.c index ec42370c7f..3a5e788656 100644 --- a/TFT/src/User/API/Settings.c +++ b/TFT/src/User/API/Settings.c @@ -1,21 +1,21 @@ #include "Settings.h" #include "includes.h" +static const uint8_t default_serial_port[] = {SP_1, SP_2, SP_3, SP_4}; +static const uint16_t default_max_temp[] = MAX_TEMP; +static const uint16_t default_max_fan[] = FAN_MAX; +static const uint16_t default_size_min[] = {X_MIN_POS, Y_MIN_POS, Z_MIN_POS}; +static const uint16_t default_size_max[] = {X_MAX_POS, Y_MAX_POS, Z_MAX_POS}; +static const uint16_t default_xy_speed[] = {SPEED_XY_SLOW, SPEED_XY_NORMAL, SPEED_XY_FAST}; +static const uint16_t default_z_speed[] = {SPEED_Z_SLOW, SPEED_Z_NORMAL, SPEED_Z_FAST}; +static const uint16_t default_ext_speed[] = {EXTRUDE_SLOW_SPEED, EXTRUDE_NORMAL_SPEED, EXTRUDE_FAST_SPEED}; +static const uint16_t default_pause_speed[] = {NOZZLE_PAUSE_XY_FEEDRATE, NOZZLE_PAUSE_Z_FEEDRATE, NOZZLE_PAUSE_E_FEEDRATE}; +static const uint16_t default_level_speed[] = {LEVELING_XY_FEEDRATE, LEVELING_Z_FEEDRATE}; +static const uint8_t default_led_color[] = {LED_R, LED_G, LED_B, LED_W, LED_P, LED_I}; + SETTINGS infoSettings; MACHINE_SETTINGS infoMachineSettings; -const uint8_t default_serial_port[] = {SP_1, SP_2, SP_3, SP_4}; -const uint16_t default_max_temp[] = MAX_TEMP; -const uint16_t default_max_fan[] = FAN_MAX; -const uint16_t default_size_min[] = {X_MIN_POS, Y_MIN_POS, Z_MIN_POS}; -const uint16_t default_size_max[] = {X_MAX_POS, Y_MAX_POS, Z_MAX_POS}; -const uint16_t default_xy_speed[] = {SPEED_XY_SLOW, SPEED_XY_NORMAL, SPEED_XY_FAST}; -const uint16_t default_z_speed[] = {SPEED_Z_SLOW, SPEED_Z_NORMAL, SPEED_Z_FAST}; -const uint16_t default_ext_speed[] = {EXTRUDE_SLOW_SPEED, EXTRUDE_NORMAL_SPEED, EXTRUDE_FAST_SPEED}; -const uint16_t default_pause_speed[] = {NOZZLE_PAUSE_XY_FEEDRATE, NOZZLE_PAUSE_Z_FEEDRATE, NOZZLE_PAUSE_E_FEEDRATE}; -const uint16_t default_level_speed[] = {LEVELING_XY_FEEDRATE, LEVELING_Z_FEEDRATE}; -const uint8_t default_led_color[] = {LED_R, LED_G, LED_B, LED_W, LED_P, LED_I}; - // Init settings data with default values void initSettings(void) { @@ -183,17 +183,17 @@ void initSettings(void) resetConfig(); - // Calculate checksum excluding the CRC variable in infoSettings - infoSettings.CRC_checksum = calculateCRC16((uint8_t*)&infoSettings + sizeof(infoSettings.CRC_checksum), - sizeof(infoSettings) - sizeof(infoSettings.CRC_checksum)); + // calculate checksum excluding the CRC variable in infoSettings + infoSettings.CRC_checksum = calculateCRC16((uint8_t *)&infoSettings + sizeof(infoSettings.CRC_checksum), + sizeof(infoSettings) - sizeof(infoSettings.CRC_checksum)); } // Save settings to Flash only if CRC does not match void saveSettings(void) { - // Calculate checksum excluding the CRC variable in infoSettings - uint32_t curCRC = calculateCRC16((uint8_t*)&infoSettings + sizeof(infoSettings.CRC_checksum), - sizeof(infoSettings) - sizeof(infoSettings.CRC_checksum)); + // calculate checksum excluding the CRC variable in infoSettings + uint32_t curCRC = calculateCRC16((uint8_t *)&infoSettings + sizeof(infoSettings.CRC_checksum), + sizeof(infoSettings) - sizeof(infoSettings.CRC_checksum)); if (curCRC != infoSettings.CRC_checksum) // save to Flash only if CRC does not match { @@ -290,7 +290,7 @@ float flashUsedPercentage(void) return percent; } -// check font/icon/config signature in SPI flash for update +// Check font/icon/config signature in SPI flash for update void checkflashSign(void) { //cur_flash_sign[lang_sign] = flash_sign[lang_sign]; // ignore language signature not implemented yet @@ -347,7 +347,7 @@ bool getFlashSignStatus(int index) uint32_t addr = FLASH_SIGN_ADDR; uint32_t len = sizeof(flash_sign); - W25Qxx_ReadBuffer((uint8_t*)&cur_flash_sign, addr, len); + W25Qxx_ReadBuffer((uint8_t *)&cur_flash_sign, addr, len); return (flash_sign[index] == cur_flash_sign[index]); } diff --git a/TFT/src/User/API/Settings.h b/TFT/src/User/API/Settings.h index 37e1318cce..046a9cccdc 100644 --- a/TFT/src/User/API/Settings.h +++ b/TFT/src/User/API/Settings.h @@ -357,16 +357,6 @@ typedef struct extern SETTINGS infoSettings; extern MACHINE_SETTINGS infoMachineSettings; -extern const uint16_t default_max_temp[]; -extern const uint16_t default_max_fanPWM[]; -extern const uint16_t default_size_min[]; -extern const uint16_t default_size_max[]; -extern const uint16_t default_move_speed[]; -extern const uint16_t default_ext_speed[]; -extern const uint16_t default_level_speed[]; -extern const uint16_t default_pause_speed[]; -extern const uint8_t default_custom_enabled[]; - // Init settings data with default values void initSettings(void); diff --git a/TFT/src/User/API/SpeedControl.c b/TFT/src/User/API/SpeedControl.c index d5727c495c..86985a28d2 100644 --- a/TFT/src/User/API/SpeedControl.c +++ b/TFT/src/User/API/SpeedControl.c @@ -3,7 +3,7 @@ #define SPEED_REFRESH_TIME 500 // 1 second is 1000 -const char * const speedCmd[SPEED_NUM] = {"M220", "M221"}; +static const char * const speedCmd[SPEED_NUM] = {"M220", "M221"}; static uint16_t setPercent[SPEED_NUM] = {100, 100}; static uint16_t curPercent[SPEED_NUM] = {100, 100}; diff --git a/TFT/src/User/API/Temperature.c b/TFT/src/User/API/Temperature.c index 010a492904..915a358a25 100644 --- a/TFT/src/User/API/Temperature.c +++ b/TFT/src/User/API/Temperature.c @@ -157,6 +157,7 @@ bool heatSetTool(const uint8_t toolIndex) if (storeCmd("%s\n", toolChange[toolIndex])) { heater.toolIndex = toolIndex; + return true; } diff --git a/TFT/src/User/API/Touch_Screen.c b/TFT/src/User/API/Touch_Screen.c index 0e57df113a..3939e1725d 100644 --- a/TFT/src/User/API/Touch_Screen.c +++ b/TFT/src/User/API/Touch_Screen.c @@ -43,9 +43,9 @@ static volatile uint8_t touchCountdown = TS_DEBOUNCE_MS; int32_t TS_CalPara[7]; bool TS_Sound = true; -void (*TS_ReDrawIcon)(uint8_t position, uint8_t isPressed) = NULL; +void (* TS_ReDrawIcon)(uint8_t position, uint8_t isPressed) = NULL; -void TS_GetCoordinates(uint16_t *x, uint16_t *y) +void TS_GetCoordinates(uint16_t * x, uint16_t * y) { uint16_t tp_x = XPT2046_Repeated_Compare_AD(CMD_RDX); uint16_t tp_y = XPT2046_Repeated_Compare_AD(CMD_RDY); @@ -72,7 +72,7 @@ bool TS_IsPressed(void) return (touchCountdown == 0); } -uint16_t TS_KeyValue(uint8_t totalRect, const GUI_RECT *menuRect) +uint16_t TS_KeyValue(uint8_t totalRect, const GUI_RECT * menuRect) { uint8_t i = 0; uint16_t x, y; @@ -95,7 +95,7 @@ uint16_t TS_KeyValue(uint8_t totalRect, const GUI_RECT *menuRect) return IDLE_TOUCH; } -uint16_t KEY_GetValue(uint8_t totalRect, const GUI_RECT* menuRect) +uint16_t KEY_GetValue(uint8_t totalRect, const GUI_RECT * menuRect) { static uint16_t key_num = IDLE_TOUCH; static bool firstPress = true; @@ -136,7 +136,7 @@ uint16_t KEY_GetValue(uint8_t totalRect, const GUI_RECT* menuRect) return key_return; } -uint8_t TS_CalibrationEnsure(uint16_t x, uint16_t y) +static inline uint8_t TS_CalibrationEnsure(uint16_t x, uint16_t y) { uint32_t i; uint16_t tp_x, tp_y; diff --git a/TFT/src/User/API/Touch_Screen.h b/TFT/src/User/API/Touch_Screen.h index 0687fa8624..b34a513ca4 100644 --- a/TFT/src/User/API/Touch_Screen.h +++ b/TFT/src/User/API/Touch_Screen.h @@ -11,13 +11,13 @@ extern "C" { extern int32_t TS_CalPara[7]; // touch screen calibration parameters extern bool TS_Sound; -extern void (*TS_ReDrawIcon)(uint8_t position, uint8_t isPressed); +extern void (* TS_ReDrawIcon)(uint8_t position, uint8_t isPressed); -void TS_GetCoordinates(uint16_t *x, uint16_t *y); +void TS_GetCoordinates(uint16_t * x, uint16_t * y); void TS_CheckPress(void); // WARNING, TIMER INTERRUPT ROUTINE CALLED ONCE A MILLISECOND bool TS_IsPressed(void); -uint16_t TS_KeyValue(uint8_t totalRect, const GUI_RECT *menuRect); -uint16_t KEY_GetValue(uint8_t totalRect, const GUI_RECT *menuRect); +uint16_t TS_KeyValue(uint8_t totalRect, const GUI_RECT * menuRect); +uint16_t KEY_GetValue(uint8_t totalRect, const GUI_RECT * menuRect); void TS_Calibrate(void); diff --git a/TFT/src/User/API/UI/CharIcon.c b/TFT/src/User/API/UI/CharIcon.c index 752147aeb1..929feca98d 100644 --- a/TFT/src/User/API/UI/CharIcon.c +++ b/TFT/src/User/API/UI/CharIcon.c @@ -211,7 +211,7 @@ const uint16_t charIconColor[CHARICON_NUM] = #define CHAR_TOGGLE_ON "" // only for toggle state detection in list menu #define CHAR_TOGGLE_OFF "" // only for toggle state detection in list menu -const char *const charIcon[CHARICON_NUM]={ +static const char * const charIcon[CHARICON_NUM] = { #define X_CHAR(NAME) CHAR_##NAME , #include "CharIcon.inc" #undef X_CHAR @@ -227,12 +227,15 @@ uint8_t * IconCharSelect(uint8_t sel) void drawCharIcon(const GUI_RECT * rect, ALIGN_POSITION iconalign, uint16_t iconindex, bool drawBgColor, uint16_t btn_color) { GUI_POINT icon_p = getTextStartPoint(rect->x0, rect->y0, rect->x1, rect->y1, iconalign, (char *)IconCharSelect(iconindex)); + GUI_SetColor(charIconColor[iconindex]); + if (drawBgColor) { GUI_SetBkColor(btn_color); GUI_ClearPrect(rect); } + GUI_DispString(icon_p.x, icon_p.y, IconCharSelect(iconindex)); GUI_RestoreColorDefault(); } diff --git a/TFT/src/User/API/UI/Char_Icon.inc b/TFT/src/User/API/UI/Char_Icon.inc index 679ea1c9ef..2f7a77e344 100644 --- a/TFT/src/User/API/UI/Char_Icon.inc +++ b/TFT/src/User/API/UI/Char_Icon.inc @@ -1,10 +1,10 @@ /** -* Char Icons Kewords Generation -* Usage: X_CHAR(VALUE) -* 'VALUE' is the keyword name without the 'ICONCHAR_' -* -* Description: https://en.wikipedia.org/wiki/X_Macro -*/ + * Char Icons Kewords Generation + * Usage: X_CHAR(VALUE) + * 'VALUE' is the keyword name without the 'ICONCHAR_' + * + * Description: https://en.wikipedia.org/wiki/X_Macro + */ X_CHAR (BLANK) X_CHAR (NOZZLE) X_CHAR (BED) diff --git a/TFT/src/User/API/UI/GUI.c b/TFT/src/User/API/UI/GUI.c index 514d4ba0bc..65fa33be10 100644 --- a/TFT/src/User/API/UI/GUI.c +++ b/TFT/src/User/API/UI/GUI.c @@ -2,11 +2,14 @@ #include "includes.h" #include -uint16_t foreGroundColor = WHITE; -uint16_t backGroundColor = BLACK; -GUI_TEXT_MODE guiTextMode = GUI_TEXTMODE_NORMAL; -GUI_NUM_MODE guiNumMode = GUI_NUMMODE_SPACE; -uint16_t *iconBuffer; +static uint16_t foreGroundColor = WHITE; +static uint16_t backGroundColor = BLACK; +static GUI_TEXT_MODE guiTextMode = GUI_TEXTMODE_NORMAL; +static GUI_NUM_MODE guiNumMode = GUI_NUMMODE_SPACE; +static uint16_t * iconBuffer; +static uint8_t pixel_limit_flag = 0; +static GUI_RECT pixel_limit_rect; +static const uint32_t GUI_Pow10[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; void GUI_SetColor(uint16_t color) { @@ -48,19 +51,6 @@ GUI_NUM_MODE GUI_GetNumMode(void) return guiNumMode; } -void GUI_Clear(uint16_t color) -{ - uint32_t index=0; - LCD_SetWindow(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1); - for (index=0; indexx0, rect->y0, rect->x1, rect->y1); } @@ -128,21 +132,23 @@ void GUI_FillPrect(const GUI_RECT *rect) * @param y1 - y point of top left corner * @param x2 - x point of bottom right corner * @param y2 - y point of bottom right corner -*/ + */ void GUI_ClearRect(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey) { - uint16_t i=0, j=0; - LCD_SetWindow( sx, sy, ex-1, ey-1); - for (i=sx; ix0, rect->y0, rect->x1, rect->y1); } @@ -153,14 +159,16 @@ void GUI_ClearPrect(const GUI_RECT *rect) * @param x2 - x point of bottom right corner * @param y2 - y point of bottom right corner * @param color - color to be filled -*/ + */ void GUI_FillRectColor(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t color) { - uint16_t i=0, j=0; - LCD_SetWindow(sx, sy, ex-1, ey-1); - for (i=sx; i 0) + { incx = 1; + } else if (delta_x == 0) + { incx = 0; + } else - { incx = -1; delta_x = -delta_x;} + { + incx = -1; + delta_x = -delta_x; + } if (delta_y > 0) + { incy = 1; + } else if (delta_y == 0) + { incy = 0; + } else - { incy = -1; delta_y = -delta_y;} + { + incy = -1; + delta_y = -delta_y; + } if (delta_x > delta_y) distance = delta_x; else distance = delta_y; - for (t=0; t <= distance + 1; t++ ) // draw all point on line + for (t = 0; t <= distance + 1; t++ ) // draw all point on line { GUI_DrawPoint(uRow, uCol); // draw calculated point xerr += delta_x; yerr += delta_y; + if (xerr > distance) { xerr -= distance; uRow += incx; } + if (yerr > distance) { yerr -= distance; @@ -239,13 +267,14 @@ void GUI_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) /** @brief Draw a line at an angle * @param x - x point of line origin * @param y - y point of line origin - * @param r - radius/ length of line + * @param r - radius / length of line * @param angle - angle in degree -*/ + */ void GUI_DrawAngleLine(uint16_t x, uint16_t y, uint16_t r, int16_t angle) { uint16_t ex,ey; float a = angle * 3.1415926f / 180; + ex = x + cos(a) * r; ey = y - sin(a) * r; GUI_DrawLine(x, y, ex, ey); @@ -255,12 +284,14 @@ void GUI_DrawAngleLine(uint16_t x, uint16_t y, uint16_t r, int16_t angle) * @param x1 - x point of line start * @param y - y point of line start * @param x2 - x point of line end -*/ + */ void GUI_HLine(uint16_t x1, uint16_t y, uint16_t x2) { - uint16_t i=0; - LCD_SetWindow(x1, y, x2-1, y); - for (i=x1; ix0, rect->y0, rect->x1, rect->y1); } @@ -307,12 +340,13 @@ void GUI_DrawPrect(const GUI_RECT *rect) * @param x0 - x point of circle center * @param y0 - y point of circle center * @param r - radius of circle -*/ + */ void GUI_DrawCircle(uint16_t x0, uint16_t y0, uint16_t r) { int16_t a = 0, + b = r, - di = 3 - (r << 1); // Diameter + di = 3 - (r << 1); // diameter // draw points for each quadrant while (a <= b) @@ -327,11 +361,13 @@ void GUI_DrawCircle(uint16_t x0, uint16_t y0, uint16_t r) GUI_DrawPoint(x0 - b, y0 - a); // 7 a++; - if (di<0) - di += (a<<2) + 6; + if (di < 0) + { + di += (a <<2 ) + 6; + } else { - di += 10 + ((a-b)<<2); + di += 10 + ((a - b) << 2); b--; } } @@ -341,42 +377,41 @@ void GUI_DrawCircle(uint16_t x0, uint16_t y0, uint16_t r) * @param x0 - x point of circle center * @param y0 - y point of circle center * @param r - radius of circle -*/ -void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) + */ +void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) { - int16_t draw_x0, draw_y0; - int16_t draw_x1, draw_y1; - int16_t draw_x2, draw_y2; - int16_t draw_x3, draw_y3; - int16_t draw_x4, draw_y4; - int16_t draw_x5, draw_y5; - int16_t draw_x6, draw_y6; - int16_t draw_x7, draw_y7; - int16_t fill_x0, fill_y0; - int16_t fill_x1; - int16_t xx, yy; - int16_t di; + int16_t draw_x0, draw_y0; + int16_t draw_x1, draw_y1; + int16_t draw_x2, draw_y2; + int16_t draw_x3, draw_y3; + int16_t draw_x4, draw_y4; + int16_t draw_x5, draw_y5; + int16_t draw_x6, draw_y6; + int16_t draw_x7, draw_y7; + int16_t fill_x0, fill_y0; + int16_t fill_x1; + int16_t xx, yy; + int16_t di; // return if radius is 0 - if (0 == r) return; - + if (0 == r) + return; draw_x0 = draw_x1 = x0; draw_y0 = draw_y1 = y0 + r; + if (draw_y0 < LCD_HEIGHT) - { GUI_DrawPoint(draw_x0, draw_y0); // 90 degree - } draw_x2 = draw_x3 = x0; draw_y2 = draw_y3 = y0 - r; + if (draw_y2 >= 0) - { GUI_DrawPoint(draw_x2, draw_y2); // 270 degree - } draw_x4 = draw_x6 = x0 + r; draw_y4 = draw_y6 = y0; + if (draw_x4 < LCD_WIDTH) { GUI_DrawPoint(draw_x4, draw_y4); // 0 degree @@ -389,31 +424,34 @@ void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) fill_y0 = y0; fill_x0 = x0 - r; - if (fill_x0<0) - fill_x0 = 0; + + if (fill_x0 < 0) + fill_x0 = 0; GUI_HLine(fill_x0, fill_y0, fill_x1); draw_x5 = draw_x7 = x0 - r; draw_y5 = draw_y7 = y0; + if (draw_x5 >= 0) - { GUI_DrawPoint(draw_x5, draw_y5); // 180 degree - } - if (1==r) return; - di = 3 - 2*r; // Diameter + if (1 == r) + return; + + di = 3 - 2 * r; // diameter xx = 0; yy = r; + while (xx < yy) { - if (di<0) + if (di < 0) { - di += 4*xx + 6; + di += 4 * xx + 6; } else { - di += 4*(xx - yy) + 10; + di += 4 * (xx - yy) + 10; yy--; draw_y0--; @@ -437,13 +475,10 @@ void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) draw_y7--; if ((draw_x0 <= LCD_WIDTH) && (draw_y0 >= 0)) - { GUI_DrawPoint(draw_x0, draw_y0); - } + if ((draw_x1 >= 0) && (draw_y1 >= 0)) - { GUI_DrawPoint(draw_x1, draw_y1); - } if (draw_x1 >= 0) { @@ -454,6 +489,7 @@ void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) fill_y0 = NOBEYOND(0, fill_y0, LCD_HEIGHT); fill_x1 = x0 * 2 - draw_x1; + // fill upto max screen height if fill_x1 is beyond screen width if (fill_x1 > LCD_WIDTH) fill_x1 = LCD_WIDTH; @@ -462,14 +498,10 @@ void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) } if ((draw_x2 <= LCD_WIDTH) && (draw_y2 <= LCD_HEIGHT) ) - { GUI_DrawPoint(draw_x2, draw_y2); - } if ((draw_x3 >= 0) && (draw_y3 <= LCD_HEIGHT) ) - { GUI_DrawPoint(draw_x3, draw_y3); - } if (draw_x3 >= 0) { @@ -480,6 +512,7 @@ void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) fill_y0 = NOBEYOND(0, fill_y0, LCD_HEIGHT); fill_x1 = x0 * 2 - draw_x3; + // fill upto max screen height if fill_x1 is beyond screen width if (fill_x1 > LCD_WIDTH) fill_x1 = LCD_WIDTH; @@ -488,13 +521,10 @@ void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) } if ((draw_x4 <= LCD_WIDTH) && (draw_y4 >= 0)) - { GUI_DrawPoint(draw_x4, draw_y4); - } + if ((draw_x5 >= 0) && (draw_y5 >= 0)) - { GUI_DrawPoint(draw_x5, draw_y5); - } if (draw_x5 >= 0) { @@ -504,7 +534,8 @@ void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) // fill upto max screen limits fill_y0 = NOBEYOND(0, fill_y0, LCD_HEIGHT); - fill_x1 = x0*2 - draw_x5; + fill_x1 = x0 * 2 - draw_x5; + // fill upto max screen height if fill_x1 is beyond screen width if (fill_x1 > LCD_WIDTH) fill_x1 = LCD_WIDTH; @@ -513,14 +544,10 @@ void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) } if ((draw_x6 <= LCD_WIDTH) && (draw_y6 <= LCD_HEIGHT)) - { GUI_DrawPoint(draw_x6, draw_y6); - } if ((draw_x7 >= 0) && (draw_y7 <= LCD_HEIGHT)) - { GUI_DrawPoint(draw_x7, draw_y7); - } if (draw_x7 >= 0) { @@ -530,10 +557,12 @@ void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) // fill upto max screen limits fill_y0 = NOBEYOND(0, fill_y0, LCD_HEIGHT); - fill_x1 = x0*2 - draw_x7; + fill_x1 = x0 * 2 - draw_x7; + // fill upto max screen height if fill_x1 is beyond screen width if (fill_x1 > LCD_WIDTH) fill_x1 = LCD_WIDTH; + GUI_HLine(fill_x0, fill_y0, fill_x1); } } @@ -599,12 +628,14 @@ GUI_POINT getTextStartPoint(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey,A point_item.y = sy + 1; break; } + return point_item; } -void GUI_DispOne(int16_t sx, int16_t sy, const CHAR_INFO *pInfo) +void GUI_DispOne(int16_t sx, int16_t sy, const CHAR_INFO * pInfo) { - if (pInfo->bytes == 0) return; + if (pInfo->bytes == 0) + return; uint8_t w = pInfo->pixelWidth; uint8_t h = pInfo->pixelHeight; @@ -646,7 +677,7 @@ void GUI_DispOne(int16_t sx, int16_t sy, const CHAR_INFO *pInfo) else // if GUI_TEXTMODE_NORMAL or GUI_TEXTMODE_ON_ICON { uint16_t _buf[h * w]; - uint16_t *buf; + uint16_t * buf; GUI_RECT limit = {0}; if (guiTextMode == GUI_TEXTMODE_NORMAL) @@ -692,9 +723,10 @@ void GUI_DispOne(int16_t sx, int16_t sy, const CHAR_INFO *pInfo) } } -void _GUI_DispString(int16_t x, int16_t y, const uint8_t *p) +void _GUI_DispString(int16_t x, int16_t y, const uint8_t * p) { - if (p == NULL) return; + if (p == NULL) + return; CHAR_INFO info; @@ -707,18 +739,21 @@ void _GUI_DispString(int16_t x, int16_t y, const uint8_t *p) } } -const uint8_t* _GUI_DispLenString(int16_t x, int16_t y, const uint8_t *p, uint16_t pixelWidth, bool truncate) +const uint8_t * _GUI_DispLenString(int16_t x, int16_t y, const uint8_t * p, uint16_t pixelWidth, bool truncate) { - if (p == NULL) return NULL; + if (p == NULL) + return NULL; CHAR_INFO info; uint16_t curPixelWidth = 0; - if (truncate) pixelWidth -= BYTE_HEIGHT; + if (truncate) + pixelWidth -= BYTE_HEIGHT; while (curPixelWidth < pixelWidth && *p) { getCharacterInfo(p, &info); + if (curPixelWidth + info.pixelWidth > pixelWidth) { if (truncate && *(p + 1)) // check if there is at least 1 more character @@ -729,13 +764,16 @@ const uint8_t* _GUI_DispLenString(int16_t x, int16_t y, const uint8_t *p, uint16 GUI_DispOne(x, y, &info); } + return p; } + GUI_DispOne(x, y, &info); x += info.pixelWidth; curPixelWidth += info.pixelWidth; p += info.bytes; } + return p; } @@ -747,7 +785,7 @@ void _GUI_DispStringRight(int16_t x, int16_t y, const uint8_t *p) void _GUI_DispStringCenter(int16_t x, int16_t y, const uint8_t *p) { - x -= GUI_StrPixelWidth(p)/2; + x -= GUI_StrPixelWidth(p) / 2; _GUI_DispString(x, y, p); } @@ -765,22 +803,22 @@ void _GUI_DispStringInRect(int16_t sx, int16_t sy, int16_t ex, int16_t ey, const uint16_t y_offset = (nline * BYTE_HEIGHT) >= height ? 0 : ((height - (nline * BYTE_HEIGHT)) >> 1); uint16_t x = sx + x_offset, y = sy + y_offset; - uint8_t i = 0; - for (i = 0; i < nline; i++) + for (uint8_t i = 0; i < nline; i++) { p = GUI_DispLenString(x, y, p, width, false); y += BYTE_HEIGHT; } } -void _GUI_DispStringInPrect(const GUI_RECT *rect, const uint8_t *p) +void _GUI_DispStringInPrect(const GUI_RECT * rect, const uint8_t * p) { _GUI_DispStringInRect(rect->x0, rect->y0, rect->x1, rect->y1, p); } -static GUI_POINT GUI_DisplayWordInPrect(const GUI_RECT *rect, GUI_POINT cursor, uint8_t *str) +static GUI_POINT GUI_DisplayWordInPrect(const GUI_RECT * rect, GUI_POINT cursor, uint8_t * str) { CHAR_INFO info; + // return cursor to new line if line is full or on new line character if ((cursor.x + GUI_StrPixelWidth(str)) > rect->x1 || (*str == '\n')) { @@ -799,6 +837,7 @@ static GUI_POINT GUI_DisplayWordInPrect(const GUI_RECT *rect, GUI_POINT cursor, while (*str) { getCharacterInfo(str, &info); + // check line width after each character to clip long words to rect width if ((cursor.x + info.pixelWidth) > rect->x1) { @@ -817,9 +856,10 @@ static GUI_POINT GUI_DisplayWordInPrect(const GUI_RECT *rect, GUI_POINT cursor, return cursor; } -void _GUI_DispStringInRectEOL(int16_t sx, int16_t sy, int16_t ex, int16_t ey, const uint8_t *p) +void _GUI_DispStringInRectEOL(int16_t sx, int16_t sy, int16_t ex, int16_t ey, const uint8_t * p) { - if (p == NULL || *p == 0) return; + if (p == NULL || *p == 0) + return; GUI_RECT rect = (GUI_RECT){sx, sy, ex, ey}; GUI_POINT cursor = (GUI_POINT){sx, sy}; @@ -833,10 +873,11 @@ void _GUI_DispStringInRectEOL(int16_t sx, int16_t sy, int16_t ex, int16_t ey, co if (bufLength) { // draw word from buffer before reading next word - cursor = GUI_DisplayWordInPrect(&rect, cursor, (uint8_t*)buf); + cursor = GUI_DisplayWordInPrect(&rect, cursor, (uint8_t *)buf); bufLength = 0; buf[bufLength] = 0; } + // draw space or new line individually cursor = GUI_DisplayWordInPrect(&rect, cursor, (uint8_t *)((*p == ' ') ? " " : "\n")); } @@ -850,21 +891,22 @@ void _GUI_DispStringInRectEOL(int16_t sx, int16_t sy, int16_t ex, int16_t ey, co if (!*p && bufLength) // draw remaining text if reached end of string { - cursor = GUI_DisplayWordInPrect(&rect, cursor, (uint8_t*)buf); + cursor = GUI_DisplayWordInPrect(&rect, cursor, (uint8_t *)buf); bufLength = 0; buf[bufLength] = 0; } } } -void _GUI_DispStringInPrectEOL(const GUI_RECT *rect, const uint8_t *p) +void _GUI_DispStringInPrectEOL(const GUI_RECT * rect, const uint8_t * p) { _GUI_DispStringInRectEOL(rect->x0, rect->y0, rect->x1, rect->y1, p); } void _GUI_DispStringOnIcon(uint16_t iconIndex, GUI_POINT iconPoint, GUI_POINT textPos, const uint8_t *p) { - if (p == NULL) return; + if (p == NULL) + return; uint16_t _iconBuffer[LARGE_BYTE_WIDTH * LARGE_BYTE_HEIGHT]; BMP_INFO iconInfo = {.index = iconIndex, .address = 0}; @@ -893,7 +935,95 @@ void _GUI_DispStringOnIcon(uint16_t iconIndex, GUI_POINT iconPoint, GUI_POINT te GUI_SetTextMode(GUI_TEXTMODE_NORMAL); } -const uint32_t GUI_Pow10[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; +void _GUI_DispLabel(int16_t x, int16_t y, uint16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText(tempstr, index) == false) + return; + + _GUI_DispString(x, y, tempstr); +} + +const uint8_t * _GUI_DispLenLabel(int16_t x, int16_t y, uint16_t index, uint16_t pixelWidth, bool truncate) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText(tempstr, index) == false) + return NULL; + + return _GUI_DispLenString(x, y, tempstr, pixelWidth, truncate); +} + +void _GUI_DispLabelRight(int16_t x, int16_t y, uint16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText(tempstr, index) == false) + return; + + _GUI_DispStringRight(x, y, tempstr); +} + +void _GUI_DispLabelCenter(int16_t x, int16_t y, uint16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText(tempstr, index) == false) + return; + + _GUI_DispStringCenter(x, y, tempstr); +} + +void _GUI_DispLabelInRect(int16_t sx, int16_t sy, int16_t ex, int16_t ey, uint16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText(tempstr, index) == false) + return; + + _GUI_DispStringInRect(sx, sy, ex, ey, tempstr); +} + +void _GUI_DispLabelInPrect(const GUI_RECT * rect, uint16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText(tempstr, index) == false) + return; + + _GUI_DispStringInPrect(rect, tempstr); +} + +void _GUI_DispLabelInRectEOL(int16_t sx, int16_t sy, int16_t ex, int16_t ey, uint16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText(tempstr, index) == false) + return; + + _GUI_DispStringInRectEOL(sx, sy, ex, ey, tempstr); +} + +void _GUI_DispLabelInPrectEOL(const GUI_RECT * rect, uint16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText(tempstr, index) == false) + return; + + _GUI_DispStringInPrectEOL(rect, tempstr); +} + +void _GUI_DispLabelOnIcon(uint16_t iconIndex, GUI_POINT iconPoint, GUI_POINT textPos, uint16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH]; + + if (loadLabelText(tempstr, index) == false) + return; + + _GUI_DispStringOnIcon(iconIndex, iconPoint, textPos, tempstr); +} void GUI_DispDec(int16_t x, int16_t y, int32_t num, uint8_t len, uint8_t leftOrRight) { @@ -909,220 +1039,172 @@ void GUI_DispDec(int16_t x, int16_t y, int32_t num, uint8_t len, uint8_t leftOrR { num = -num; isNegative = 1; - len--; // Negative '-' takes up a display length + len--; // negative '-' takes up a display length } for (i = 0; i < len; i++) { bit_value = (num / GUI_Pow10[len - i - 1]) % 10; + if (notZero == 0) { if (bit_value == 0 && i < (len - 1)) { if (leftOrRight == RIGHT) - { decBuf[bufIndex++] = (guiNumMode == GUI_NUMMODE_SPACE) ? ' ' : '0'; - } else - { blank_bit_len++; - } + continue; } else { notZero = 1; + if (isNegative) - { decBuf[bufIndex++] = '-'; - } } } + decBuf[bufIndex++] = bit_value + '0'; } + for (; blank_bit_len > 0; blank_bit_len--) { decBuf[bufIndex++] = ' '; } + decBuf[bufIndex] = 0; _GUI_DispString(x, y, decBuf); } void GUI_DispFloat(int16_t x, int16_t y, float num, uint8_t llen, uint8_t rlen, uint8_t leftOrRight) { - uint8_t alen = 0; - uint8_t i = 0; - uint8_t notZero = 0; - char isNegative = 0; - uint8_t floatBuf[64]; - uint8_t bufIndex = 0; - - if (num<0) + uint8_t alen = 0; + uint8_t i = 0; + uint8_t notZero = 0; + char isNegative = 0; + uint8_t floatBuf[64]; + uint8_t bufIndex = 0; + + if (num < 0) { num = -num; isNegative = 1; - llen--; // Negative '-' takes up a display length + llen--; // negative '-' takes up a display length } num *= GUI_Pow10[(unsigned)rlen]; num += 0.5f; num = (float) floor(num); - for (i=0; inum;i++) + uint8_t i = 0; + + for (i = 0; i < radio->num; i++) { - if (i==radio->select) + if (i == radio->select) GUI_SetColor(RADIO_SELECTED_COLOR); else GUI_SetColor(RADIO_IDLE_COLOR); - GUI_FillCircle(radio->sx+BYTE_HEIGHT/2, i*radio->distance+radio->sy+BYTE_HEIGHT/2, BYTE_HEIGHT/8); - GUI_DrawCircle(radio->sx+BYTE_HEIGHT/2, i*radio->distance+radio->sy+BYTE_HEIGHT/2, BYTE_HEIGHT/4); - GUI_DispString(radio->sx+BYTE_HEIGHT, i*radio->distance+radio->sy, radio->context[i]); + + GUI_FillCircle(radio->sx + BYTE_HEIGHT / 2, i * radio->distance + radio->sy + BYTE_HEIGHT / 2, BYTE_HEIGHT / 8); + GUI_DrawCircle(radio->sx + BYTE_HEIGHT / 2, i * radio->distance + radio->sy + BYTE_HEIGHT / 2, BYTE_HEIGHT / 4); + GUI_DispString(radio->sx + BYTE_HEIGHT, i * radio->distance + radio->sy, radio->context[i]); } + GUI_SetColor(tmp); } -void RADIO_Select(RADIO *radio, uint8_t select) +void RADIO_Select(RADIO * radio, uint8_t select) { uint16_t tmp = GUI_GetColor(); - uint8_t i=0; - if (radio->select==select) - return; - for (i=0;i<2;i++) + uint8_t i = 0; + + if (radio->select == select) + return; + + for (i = 0; i < 2; i++) { - if (i==0) + if (i == 0) { GUI_SetColor(RADIO_IDLE_COLOR); } else { - radio->select=select; + radio->select = select; GUI_SetColor(RADIO_SELECTED_COLOR); } - GUI_FillCircle(radio->sx+BYTE_HEIGHT/2, radio->select*radio->distance+radio->sy+BYTE_HEIGHT/2, BYTE_HEIGHT/8); - GUI_DrawCircle(radio->sx+BYTE_HEIGHT/2, radio->select*radio->distance+radio->sy+BYTE_HEIGHT/2, BYTE_HEIGHT/4); - GUI_DispString(radio->sx+BYTE_HEIGHT, radio->select*radio->distance+radio->sy, radio->context[radio->select]); + + GUI_FillCircle(radio->sx + BYTE_HEIGHT / 2, radio->select * radio->distance + radio->sy + BYTE_HEIGHT / 2, BYTE_HEIGHT / 8); + GUI_DrawCircle(radio->sx + BYTE_HEIGHT / 2, radio->select * radio->distance + radio->sy + BYTE_HEIGHT / 2, BYTE_HEIGHT / 4); + GUI_DispString(radio->sx + BYTE_HEIGHT, radio->select * radio->distance + radio->sy, radio->context[radio->select]); } + GUI_SetColor(tmp); } -void Scroll_CreatePara(SCROLL * para, const uint8_t *pstr, const GUI_RECT *prect) +void Scroll_CreatePara(SCROLL * para, const uint8_t * pstr, const GUI_RECT * prect) { CHAR_INFO info; + getCharacterInfo(pstr, &info); memset(para,0,sizeof(SCROLL)); - para->text = (uint8_t *const)pstr; + para->text = (uint8_t * const)pstr; para->maxByte = strlen((char *)pstr); para->curPixelWidth = para->totalPixelWidth = GUI_StrPixelWidth(pstr); para->maxPixelWidth = prect->x1 - prect->x0; @@ -1134,20 +1216,24 @@ void Scroll_CreatePara(SCROLL * para, const uint8_t *pstr, const GUI_RECT *prect void Scroll_DispString(SCROLL * para, uint8_t align) { + if (para->text == NULL) + return; + uint16_t i = 0; CHAR_INFO info; - if (para->text == NULL) return; if (para->totalPixelWidth > para->maxPixelWidth) { if (OS_GetTimeMs() >= para->time) { para->time = OS_GetTimeMs() + 50; // 50ms GUI_SetRange(para->rect.x0, para->rect.y0, para->rect.x1, para->rect.y1); + if (para->curByte < para->maxByte) { getCharacterInfo(¶->text[para->curByte], &info); para->off_head++; + if (para->off_head == info.pixelWidth) { para->curByte += info.bytes; @@ -1157,27 +1243,30 @@ void Scroll_DispString(SCROLL * para, uint8_t align) GUI_DispLenString(para->rect.x0 - para->off_head, para->rect.y0, ¶->text[para->curByte], para->maxPixelWidth + info.pixelWidth, false); para->curPixelWidth--; + if (para->curPixelWidth < para->maxPixelWidth) { - for (i = para->rect.y0; irect.y1; i++) + for (i = para->rect.y0; i < para->rect.y1; i++) { GUI_DrawPixel(para->rect.x0 + para->curPixelWidth, i, backGroundColor); } } } - if (para->curPixelWidth + 2*BYTE_WIDTH < para->maxPixelWidth) + if (para->curPixelWidth + 2 * BYTE_WIDTH < para->maxPixelWidth) { para->off_tail++; - GUI_DispLenString(para->rect.x1-para->off_tail, para->rect.y0, para->text, para->off_tail, false); + GUI_DispLenString(para->rect.x1 - para->off_tail, para->rect.y0, para->text, para->off_tail, false); + if (para->off_tail + para->rect.x0 >= para->rect.x1) { - para->off_head=0; - para->off_tail=0; - para->curByte=0; + para->off_head = 0; + para->off_tail = 0; + para->curByte = 0; para->curPixelWidth = para->totalPixelWidth; } } + GUI_CancelRange(); } } @@ -1192,22 +1281,25 @@ void Scroll_DispString(SCROLL * para, uint8_t align) } case RIGHT: { - uint16_t x_offset=(para->rect.x1 - para->totalPixelWidth); + uint16_t x_offset = (para->rect.x1 - para->totalPixelWidth); + GUI_DispString(x_offset, para->rect.y0, para->text); break; } case CENTER: { - uint16_t x_offset=((para->rect.x1 - para->rect.x0 - para->totalPixelWidth) >> 1); - GUI_DispString(para->rect.x0+x_offset, para->rect.y0, para->text); + uint16_t x_offset = ((para->rect.x1 - para->rect.x0 - para->totalPixelWidth) >> 1); + + GUI_DispString(para->rect.x0 + x_offset, para->rect.y0, para->text); break; } } + para->has_disp = 1; } } -void GUI_DrawButton(const BUTTON *button, uint8_t pressed) +void GUI_DrawButton(const BUTTON * button, uint8_t pressed) { const uint16_t radius = button->radius; const uint16_t lineWidth = button->lineWidth; @@ -1224,8 +1316,8 @@ void GUI_DrawButton(const BUTTON *button, uint8_t pressed) const uint16_t fontColor = pressed ? button->pFontColor : button->fontColor; GUI_SetColor(lineColor); - GUI_FillCircle(sx + radius, sy + radius, radius); - GUI_FillCircle(ex - radius - 1, sy + radius, radius); + GUI_FillCircle(sx + radius, sy + radius, radius); + GUI_FillCircle(ex - radius - 1, sy + radius, radius); GUI_FillCircle(sx + radius, ey - radius - 1, radius); GUI_FillCircle(ex - radius - 1, ey - radius - 1, radius); @@ -1238,13 +1330,13 @@ void GUI_DrawButton(const BUTTON *button, uint8_t pressed) } GUI_SetColor(backColor); - GUI_FillCircle(sx + radius, sy + radius, radius - lineWidth); - GUI_FillCircle(ex - radius - 1, sy + radius, radius - lineWidth); + GUI_FillCircle(sx + radius, sy + radius, radius - lineWidth); + GUI_FillCircle(ex - radius - 1, sy + radius, radius - lineWidth); GUI_FillCircle(sx + radius, ey - radius - 1, radius - lineWidth); GUI_FillCircle(ex - radius - 1, ey - radius - 1, radius - lineWidth); - GUI_FillRect(sx + radius, sy + lineWidth, ex - radius, sy + lineWidth + radius); + GUI_FillRect(sx + radius, sy + lineWidth, ex - radius, sy + lineWidth + radius); GUI_FillRect(sx + lineWidth, sy + lineWidth + radius, ex - lineWidth, ey - lineWidth - radius); - GUI_FillRect(sx + radius, ey - lineWidth - radius, ex - radius, ey - lineWidth); + GUI_FillRect(sx + radius, ey - lineWidth - radius, ex - radius, ey - lineWidth); GUI_SetColor(fontColor); GUI_SetTextMode(GUI_TEXTMODE_TRANS); @@ -1255,7 +1347,7 @@ void GUI_DrawButton(const BUTTON *button, uint8_t pressed) GUI_SetTextMode(nowTextMode); } -void GUI_DrawWindow(const WINDOW *window, const uint8_t *title, const uint8_t *inf, bool actionBar) +void GUI_DrawWindow(const WINDOW * window, const uint8_t * title, const uint8_t * inf, bool actionBar) { uint16_t title_y0 = window->rect.y0 + (window->titleHeight - BYTE_HEIGHT) / 2; uint16_t title_y1 = window->rect.y0 + window->titleHeight; @@ -1315,7 +1407,7 @@ void GUI_DrawWindow(const WINDOW *window, const uint8_t *title, const uint8_t *i GUI_DrawRect(window->rect.x0, title_y1 - 1, window->rect.x1, title_y1 + 1); if (actionBar) - { // draw actionbar accent line + { // draw actionbar accent line GUI_SetColor(GRAY); GUI_DrawRect(window->rect.x0, info_y1 - 1, window->rect.x1, info_y1 + 1); } diff --git a/TFT/src/User/API/UI/GUI.h b/TFT/src/User/API/UI/GUI.h index b748e68be3..d56438545a 100644 --- a/TFT/src/User/API/UI/GUI.h +++ b/TFT/src/User/API/UI/GUI.h @@ -66,114 +66,117 @@ GUI_TEXT_MODE GUI_GetTextMode(void); void GUI_SetNumMode(GUI_NUM_MODE mode); GUI_NUM_MODE GUI_GetNumMode(void); +void GUI_SetRange(int16_t x0, int16_t y0, int16_t x1, int16_t y1); +void GUI_CancelRange(void); + void GUI_Clear(uint16_t color); +void GUI_DrawPixel(int16_t x, int16_t y, uint16_t color); void GUI_DrawPoint(uint16_t x, uint16_t y); void GUI_FillRect(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey); -void GUI_FillPrect(const GUI_RECT *rect); +void GUI_FillPrect(const GUI_RECT * rect); void GUI_ClearRect(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey); -void GUI_ClearPrect(const GUI_RECT *rect); +void GUI_ClearPrect(const GUI_RECT * rect); void GUI_FillRectColor(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t color); -void GUI_FillRectArry(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint8_t *arry); +void GUI_FillRectArry(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint8_t * arry); void GUI_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); +void GUI_DrawAngleLine(uint16_t x, uint16_t y, uint16_t r, int16_t angle); void GUI_HLine(uint16_t x1, uint16_t y, uint16_t x2); void GUI_VLine(uint16_t x1, uint16_t y1, uint16_t y2); void GUI_DrawRect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); -void GUI_DrawPrect(const GUI_RECT *rect); +void GUI_DrawPrect(const GUI_RECT * rect); void GUI_DrawCircle(uint16_t x0, uint16_t y0, uint16_t r); void GUI_FillCircle(uint16_t x0, uint16_t y0, uint16_t r); -void GUI_SetRange(int16_t x0, int16_t y0, int16_t x1, int16_t y1); -void GUI_CancelRange(void); -void GUI_DrawPixel(int16_t x, int16_t y, uint16_t color); GUI_POINT getTextStartPoint(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, ALIGN_POSITION pos, const char * textchar); -void GUI_DispOne(int16_t sx, int16_t sy, const CHAR_INFO *pInfo); +void GUI_DispOne(int16_t sx, int16_t sy, const CHAR_INFO * pInfo); // display string from char array pointers -void _GUI_DispString(int16_t x, int16_t y, const uint8_t *p); -const uint8_t* _GUI_DispLenString(int16_t x, int16_t y, const uint8_t *p, uint16_t pixelWidth, bool truncate); -void _GUI_DispStringRight(int16_t x, int16_t y, const uint8_t *p); -void _GUI_DispStringCenter(int16_t x, int16_t y, const uint8_t *p); -void _GUI_DispStringInRect(int16_t sx, int16_t sy, int16_t ex, int16_t ey, const uint8_t *p); -void _GUI_DispStringInPrect(const GUI_RECT *rect, const uint8_t *p); -void _GUI_DispStringInRectEOL(int16_t sx, int16_t sy, int16_t ex, int16_t ey, const uint8_t *p); -void _GUI_DispStringInPrectEOL(const GUI_RECT *rect, const uint8_t *p); -void _GUI_DispStringOnIcon(uint16_t iconIndex, GUI_POINT iconPoint, GUI_POINT textPos, const uint8_t *p); +void _GUI_DispString(int16_t x, int16_t y, const uint8_t * p); +const uint8_t * _GUI_DispLenString(int16_t x, int16_t y, const uint8_t * p, uint16_t pixelWidth, bool truncate); +void _GUI_DispStringRight(int16_t x, int16_t y, const uint8_t * p); +void _GUI_DispStringCenter(int16_t x, int16_t y, const uint8_t * p); +void _GUI_DispStringInRect(int16_t sx, int16_t sy, int16_t ex, int16_t ey, const uint8_t * p); +void _GUI_DispStringInPrect(const GUI_RECT * rect, const uint8_t * p); +void _GUI_DispStringInRectEOL(int16_t sx, int16_t sy, int16_t ex, int16_t ey, const uint8_t * p); +void _GUI_DispStringInPrectEOL(const GUI_RECT * rect, const uint8_t * p); +void _GUI_DispStringOnIcon(uint16_t iconIndex, GUI_POINT iconPoint, GUI_POINT textPos, const uint8_t * p); // display string from label index void _GUI_DispLabel(int16_t x, int16_t y, uint16_t index); -const uint8_t* _GUI_DispLenLabel(int16_t x, int16_t y, uint16_t index, uint16_t pixelWidth, bool truncate); +const uint8_t * _GUI_DispLenLabel(int16_t x, int16_t y, uint16_t index, uint16_t pixelWidth, bool truncate); void _GUI_DispLabelRight(int16_t x, int16_t y, uint16_t index); void _GUI_DispLabelCenter(int16_t x, int16_t y, uint16_t index); void _GUI_DispLabelInRect(int16_t sx, int16_t sy, int16_t ex, int16_t ey, uint16_t index); -void _GUI_DispLabelInPrect(const GUI_RECT *rect, uint16_t index); +void _GUI_DispLabelInPrect(const GUI_RECT * rect, uint16_t index); void _GUI_DispLabelInRectEOL(int16_t sx, int16_t sy, int16_t ex, int16_t ey, uint16_t index); -void _GUI_DispLabelInPrectEOL(const GUI_RECT *rect, uint16_t index); +void _GUI_DispLabelInPrectEOL(const GUI_RECT * rect, uint16_t index); void _GUI_DispLabelOnIcon(uint16_t iconIndex, GUI_POINT iconPoint, GUI_POINT textPos, uint16_t index); // macros for selecting right function based on variable type -#define GUI_DispString(x, y, c) _Generic(((c+0)), const uint8_t*: _GUI_DispString, \ - uint8_t*: _GUI_DispString, \ - default: _GUI_DispLabel)(x,y,c) -#define GUI_DispLenString(x, y, c, width, truncate) _Generic(((c+0)), const uint8_t*: _GUI_DispLenString, \ - uint8_t*: _GUI_DispLenString, \ - default: _GUI_DispLenLabel)(x,y,c,width,truncate) -#define GUI_DispStringRight(x, y, c) _Generic(((c+0)), const uint8_t*: _GUI_DispStringRight, \ - uint8_t*: _GUI_DispStringRight, \ - default: _GUI_DispLabelRight)(x,y,c) -#define GUI_DispStringCenter(x, y, c) _Generic(((c+0)), const uint8_t*: _GUI_DispStringCenter, \ - uint8_t*: _GUI_DispStringCenter, \ - default: _GUI_DispLabelCenter)(x,y,c) -#define GUI_DispStringInRect(sx, sy, ex, ey, c) _Generic(((c+0)), const uint8_t*: _GUI_DispStringInRect, \ - uint8_t*: _GUI_DispStringInRect, \ - default: _GUI_DispLabelInRect)(sx,sy,ex,ey,c) -#define GUI_DispStringInPrect(rect, c) _Generic(((c+0)), const uint8_t*: _GUI_DispStringInPrect, \ - uint8_t*: _GUI_DispStringInPrect, \ - default: _GUI_DispLabelInPrect)(rect,c) -#define GUI_DispStringInRectEOL(sx, sy, ex, ey, c) _Generic(((c+0)), const uint8_t*: _GUI_DispStringInRectEOL, \ - uint8_t*: _GUI_DispStringInRectEOL, \ - default: _GUI_DispLabelInRectEOL)(sx,sy,ex,ey,c) -#define GUI_DispStringInPrectEOL(rect, c) _Generic(((c+0)), const uint8_t*: _GUI_DispStringInPrectEOL, \ - uint8_t*: _GUI_DispStringInPrectEOL, \ - default: _GUI_DispLabelInPrectEOL)(rect,c) -#define GUI_DispStringOnIcon(icon, iconPt, txtPos, c) _Generic(((c+0)), const uint8_t*: _GUI_DispStringOnIcon, \ - uint8_t*: _GUI_DispStringOnIcon, \ - default: _GUI_DispLabelOnIcon)(icon,iconPt,txtPos,c) +#define GUI_DispString(x, y, c) _Generic(((c+0)), const uint8_t *: _GUI_DispString, \ + uint8_t *: _GUI_DispString, \ + default: _GUI_DispLabel)(x,y,c) +#define GUI_DispLenString(x, y, c, width, truncate) _Generic(((c+0)), const uint8_t *: _GUI_DispLenString, \ + uint8_t *: _GUI_DispLenString, \ + default: _GUI_DispLenLabel)(x,y,c,width,truncate) +#define GUI_DispStringRight(x, y, c) _Generic(((c+0)), const uint8_t *: _GUI_DispStringRight, \ + uint8_t *: _GUI_DispStringRight, \ + default: _GUI_DispLabelRight)(x,y,c) +#define GUI_DispStringCenter(x, y, c) _Generic(((c+0)), const uint8_t *: _GUI_DispStringCenter, \ + uint8_t *: _GUI_DispStringCenter, \ + default: _GUI_DispLabelCenter)(x,y,c) +#define GUI_DispStringInRect(sx, sy, ex, ey, c) _Generic(((c+0)), const uint8_t *: _GUI_DispStringInRect, \ + uint8_t *: _GUI_DispStringInRect, \ + default: _GUI_DispLabelInRect)(sx,sy,ex,ey,c) +#define GUI_DispStringInPrect(rect, c) _Generic(((c+0)), const uint8_t *: _GUI_DispStringInPrect, \ + uint8_t *: _GUI_DispStringInPrect, \ + default: _GUI_DispLabelInPrect)(rect,c) +#define GUI_DispStringInRectEOL(sx, sy, ex, ey, c) _Generic(((c+0)), const uint8_t *: _GUI_DispStringInRectEOL, \ + uint8_t *: _GUI_DispStringInRectEOL, \ + default: _GUI_DispLabelInRectEOL)(sx,sy,ex,ey,c) +#define GUI_DispStringInPrectEOL(rect, c) _Generic(((c+0)), const uint8_t *: _GUI_DispStringInPrectEOL, \ + uint8_t *: _GUI_DispStringInPrectEOL, \ + default: _GUI_DispLabelInPrectEOL)(rect,c) +#define GUI_DispStringOnIcon(icon, iconPt, txtPos, c) _Generic(((c+0)), const uint8_t *: _GUI_DispStringOnIcon, \ + uint8_t *: _GUI_DispStringOnIcon, \ + default: _GUI_DispLabelOnIcon)(icon,iconPt,txtPos,c) void GUI_DispDec(int16_t x, int16_t y,int32_t num, uint8_t len, uint8_t leftOrRight); void GUI_DispFloat(int16_t x, int16_t y, float num, uint8_t llen, uint8_t rlen, uint8_t leftOrRight); -/**************************************************** Widget *******************************************************************/ +/**************************** Widget ****************************/ #define RADIO_SIZE 5 + typedef struct { - uint8_t *context[RADIO_SIZE]; - uint16_t sx; - uint16_t sy; - uint8_t distance; - uint8_t num; - uint8_t select; + uint8_t * context[RADIO_SIZE]; + uint16_t sx; + uint16_t sy; + uint8_t distance; + uint8_t num; + uint8_t select; } RADIO; -void RADIO_Create(RADIO *radio); -void RADIO_Select(RADIO *radio, uint8_t select); +void RADIO_Create(RADIO * radio); +void RADIO_Select(RADIO * radio, uint8_t select); typedef struct { - GUI_RECT rect; - uint8_t *text; - uint32_t time; - int16_t off_head; - int16_t off_tail; - uint16_t maxByte; - uint16_t curByte; - uint16_t totalPixelWidth; - int16_t curPixelWidth; - uint16_t maxPixelWidth; - uint8_t has_disp; + GUI_RECT rect; + uint8_t * text; + uint32_t time; + int16_t off_head; + int16_t off_tail; + uint16_t maxByte; + uint16_t curByte; + uint16_t totalPixelWidth; + int16_t curPixelWidth; + uint16_t maxPixelWidth; + uint8_t has_disp; } SCROLL; -void Scroll_CreatePara(SCROLL * para,const uint8_t *pstr, const GUI_RECT *prect); +void Scroll_CreatePara(SCROLL * para,const uint8_t * pstr, const GUI_RECT * prect); void Scroll_DispString(SCROLL * para, uint8_t align); typedef enum @@ -183,23 +186,23 @@ typedef enum DIALOG_TYPE_QUESTION, DIALOG_TYPE_ERROR, DIALOG_TYPE_SUCCESS, -}DIALOG_TYPE; +} DIALOG_TYPE; typedef struct { - const GUI_RECT rect; - const uint8_t *context; - const uint16_t radius; - const uint16_t lineWidth; - const uint16_t lineColor; // normal button colors - const uint16_t fontColor; - const uint16_t backColor; - const uint16_t pLineColor; // pressed button colors - const uint16_t pFontColor; - const uint16_t pBackColor; + const GUI_RECT rect; + const uint8_t * context; + const uint16_t radius; + const uint16_t lineWidth; + const uint16_t lineColor; // normal button colors + const uint16_t fontColor; + const uint16_t backColor; + const uint16_t pLineColor; // pressed button colors + const uint16_t pFontColor; + const uint16_t pBackColor; } BUTTON; -void GUI_DrawButton(const BUTTON *button, uint8_t pressed); +void GUI_DrawButton(const BUTTON * button, uint8_t pressed); typedef struct { @@ -209,18 +212,18 @@ typedef struct typedef struct { - DIALOG_TYPE type; - const GUI_RECT rect; - const uint16_t titleHeight; - const uint16_t actionBarHeight; - const uint16_t lineWidth; - const uint16_t lineColor; + DIALOG_TYPE type; + const GUI_RECT rect; + const uint16_t titleHeight; + const uint16_t actionBarHeight; + const uint16_t lineWidth; + const uint16_t lineColor; const WINDOW_ITEM title; const WINDOW_ITEM info; const WINDOW_ITEM actionBar; } WINDOW; -void GUI_DrawWindow(const WINDOW *window, const uint8_t *title, const uint8_t *inf, bool actionBar); +void GUI_DrawWindow(const WINDOW * window, const uint8_t * title, const uint8_t * inf, bool actionBar); #ifdef __cplusplus } diff --git a/TFT/src/User/API/UI/HD44780_Emulator.c b/TFT/src/User/API/UI/HD44780_Emulator.c index 5d356e5c8d..70e9a57c26 100644 --- a/TFT/src/User/API/UI/HD44780_Emulator.c +++ b/TFT/src/User/API/UI/HD44780_Emulator.c @@ -1,23 +1,223 @@ #include "HD44780_Emulator.h" #include "includes.h" -#include "GUI.h" #include "../../Configuration.h" +#include "GUI.h" #include "HD44780.h" #ifdef LCD2004_EMULATOR -uint8_t HD44780_CGRAM[8][8]; // [64*2] = [4 * 16*2*8], means 4 * [16*16] bitmap font, + +#define XROWS 20 +#define YROWS 4 +#define FONT_PIXEL (MIN(LCD_WIDTH / XROWS / BYTE_WIDTH, LCD_HEIGHT / YROWS / BYTE_HEIGHT)) +#define BITMAP_PIXEL (MIN(LCD_WIDTH / XROWS / 6, LCD_HEIGHT / YROWS / 9)) +#define XSTART ((LCD_WIDTH - FONT_PIXEL * XROWS * BYTE_WIDTH) / 2) +#define YSTART ((LCD_HEIGHT - FONT_PIXEL * YROWS * BYTE_HEIGHT) / 2) +#define YOFFSET (BYTE_HEIGHT * FONT_PIXEL - 9 * BITMAP_PIXEL) + +typedef void (* HD44780_CMD)(uint8_t); + +typedef enum +{ + HD44780_DATA_DDRAM = 0, + HD44780_DATA_CGRAM, +} HD44780_DATA_TYPE; + +typedef struct +{ + int16_t x; // Current x pixel, range is 0 - 127 + int16_t y; // Current y pixel, range is 0 - 63 + // Extended Instruction, the first address set Y, second address set X + uint8_t address_is_y; // Record current address is Y or X +} HD44780_PIXEL; + +/*** Common Instruction ***/ + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t : 2, + f : 1, // Sets the character font + n : 1, // Sets the number of display lines + dl : 1; // 4/8-bit interface control bit (0-4bit, 1-8bit) + }; + }; +} HD44780_REG_FS; // Function Set + +/*** Basic Instruction ***/ + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t slr : 1, // Display Shift Control: (Shift Left/Right) + id : 1; // Address Counter Control: (Increase/Decrease) + }; + }; +} HD44780_BIREG_EMS; // Entry Mode Set + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t blink : 1, // Character Blink ON/OFF control bit + cursor : 1, // Cursor ON/OFF control bit + display : 1; // Display ON/OFF control bit + }; + }; +} HD44780_BIREG_DC; // Display Control + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t : 2, + rl : 1, // Cursor moves/shift left/right + sc : 1; // Display shift, cursor also follows to shift + }; + }; +} HD44780_BIREG_CDSC; // Cursor/Display Shift Control + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t ac : 6; // AC range is 00H…3FH + }; + }; +} HD44780_BIREG_CGRAMA; // Set CGRAM Address + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t ac : 7; // First line AC range is 80H … 93h + // Second line AC range is C0H … D3H + // Third line AC range is 94H … 2FH + // Fourth line AC range is D4H … E7H + }; + }; +} HD44780_BIREG_DDRAMA; // Set DDRAM Address + +/*** Extended Instruction ***/ + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t sb : 1; + }; + }; +} HD44780_EIREG_SB; // Standby + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t sr : 1; // "1", the Vertical Scroll mode is enabled + // "0", “Set CGRAM Address” instruction (basic instruction) is enabled + }; + }; +} HD44780_EIREG_VSRA; // Vertical Scroll or RAM Address Select + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t r0 : 1, // R1 R0 Description + r1 : 1; // 0 0 First line normal or reverse + // 0 1 Second line normal or reverse + // 1 0 Third line normal or reverse + // 1 1 Fourth line normal or reverse + }; + }; +} HD44780_EIREG_REV; // Reverse + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t ac : 6; // HD44780_EIREG_VSR.sr = 1: AC5~AC0 is vertical scroll displacement address + }; + }; +} HD44780_EIREG_SA; // Set Scroll Address + +typedef struct +{ + union + { + uint8_t reg; + struct + { + uint8_t ac : 6; + }; + }; +} HD44780_EIREG_GRAMA; // Set Graphic RAM Address + +typedef struct +{ + // 1 << 0 : Display Clear + // 1 << 1 : Return Home + HD44780_BIREG_EMS ems; // 1 << 2 : Entry Mode Set + HD44780_BIREG_DC dc; // 1 << 3 : Display Control + HD44780_BIREG_CDSC cdsc; // 1 << 4 : Cursor/Display Shift Control + // 1 << 5 : Function Set + HD44780_BIREG_CGRAMA cgrama; // 1 << 6 : Set CGRAM Address + HD44780_BIREG_DDRAMA ddrama; // 1 << 7 : Set DDRAM Address +} HD44780_BI; // Basic Instruction + +typedef struct +{ + HD44780_REG_FS fs; // Function Set + HD44780_BI bi; // Basic Instruction + uint8_t reverse; // Begin with normal and toggle to reverse + uint8_t cgram; + HD44780_DATA_TYPE data_type; +} HD44780_REG; // Extended Instruction + +uint8_t HD44780_CGRAM[8][8]; // [64 * 2] = [4 * 16 * 2 * 8], means 4 * [16 * 16] bitmap font HD44780_PIXEL HD44780 = { - .x = 0, // current x pixel, range is 0 - 127 - .y = 0, // current y pixel, range is 0 - 63 - .address_is_y = 1, // Extended Instruction, The first address set Y, second address set X + .x = 0, // Current x pixel, range is 0 - 127 + .y = 0, // Current y pixel, range is 0 - 63 + .address_is_y = 1, // Extended instruction, the first address set Y, second address set X }; HD44780_REG HD44780_reg = { - .fs = //0x00, // default basic instruction, 8-bit MPU interface, Graphic display OFF - 0x26, // default extended instruction, 4-bit MPU interface, Graphic display On + .fs = //0x00, // Default basic instruction, 8-bit MPU interface, graphic display OFF + 0x26, // Default extended instruction, 4-bit MPU interface, graphic display On .bi = { - .ems = 0x02, // Cursor move to right ,DDRAM address counter (AC) plus 1 + .ems = 0x02, // Cursor move to right, DDRAM address counter (AC) plus 1 .dc = 0x04, // Display, cursor and blink are ALL OFF .cdsc = 0x00, // No cursor or display shift operation }, @@ -26,7 +226,7 @@ HD44780_REG HD44780_reg = { .data_type = HD44780_DATA_DDRAM, }; -/*** Basic instruction ***/ +/*** Basic Instruction ***/ // cmd : 1 << 0 void HD44780_BI10_DisplayClear(uint8_t cmd) @@ -43,7 +243,7 @@ void HD44780_BI10_DisplayClear(uint8_t cmd) // cmd : 1 << 1 void HD44780_BI11_ReturnHome(uint8_t cmd) { - // Set address counter (AC) to "00H". + // Set address counter (AC) to "00H" HD44780.x = HD44780.y = 0; } @@ -75,7 +275,9 @@ void HD44780_CI15_FunctionSet(uint8_t cmd) void HD44780_BI16_SetCGRAMAddress(uint8_t cmd) { HD44780_reg.bi.cgrama.reg = cmd; + uint8_t address = HD44780_reg.bi.cgrama.ac; + // Set CGRAM address HD44780.y = (address >> 3) & 0x07; HD44780.x = 0; @@ -86,28 +288,34 @@ void HD44780_BI16_SetCGRAMAddress(uint8_t cmd) void HD44780_BI17_SetDDRAMAddress(uint8_t cmd) { HD44780_reg.bi.ddrama.reg = cmd; + // Set DDRAM address // x is 0-20 . y is 0-4 + if (0x80 <= cmd && cmd <= 0x93) { // First line AC range is 80H … 93h HD44780.y = 0; HD44780.x = cmd - 0x80; } + if (0xC0 <= cmd && cmd <= 0xD3) { // Second line AC range is C0H … D3H HD44780.y = 1; HD44780.x = cmd - 0xC0; } + if (0x94 <= cmd && cmd <= 0xA7) { // Third line AC range is 94H … A7H HD44780.y = 2; HD44780.x = cmd - 0x94; } + if (0xD4 <= cmd && cmd <= 0xE7) { // Fourth line AC range is D4H … E7H HD44780.y = 3; HD44780.x = cmd - 0xD4; } + HD44780_reg.data_type = HD44780_DATA_DDRAM; } @@ -137,13 +345,16 @@ void HD44780_DispDDRAM(uint8_t data) uint16_t i = 0, ex = 0, ey = 0; + if (data < 8) - { // 5*8 bitmap + { // 5 * 8 bitmap ex = HD44780.x * 6 + 6; ey = HD44780.y * 12 + 8; + for (uint16_t y = HD44780.y * 12; y < ey; y++) { uint8_t temp = HD44780_CGRAM[data][i++]; + for (uint16_t x = HD44780.x * 6; x < ex; x++) { HD44780_DrawPixel(x, y, temp & 0x10, 0); @@ -153,12 +364,16 @@ void HD44780_DispDDRAM(uint8_t data) } else { // font - if (data < ' ' || data > '~') return; + if (data < ' ' || data > '~') + return; + ex = HD44780.x * BYTE_WIDTH + BYTE_WIDTH - 1; ey = HD44780.y * BYTE_HEIGHT + BYTE_HEIGHT - 1; + CHAR_INFO info = {.bytes = 0}; getCharacterInfo(&data, &info); + uint16_t x = 0, y = 0, j = 0; @@ -183,12 +398,14 @@ void HD44780_DispDDRAM(uint8_t data) } } } + HD44780.x ++; } void HD44780_SetCGRAMData(uint8_t data) { HD44780_CGRAM[HD44780.y][HD44780.x++] = data; + if (HD44780.x > 7) { HD44780.x = 0; @@ -215,6 +432,7 @@ void HD44780_ParseWCmd(uint8_t cmd) if (cmd & (1 << i)) { (*hd44780CmdCallBack[i])(cmd); + break; } } @@ -240,16 +458,17 @@ void HD44780_ParseRecv(uint8_t val) static uint8_t rcvData = 0; static uint8_t rcvIndex = 0; static uint8_t curIsCmd = 0; + if (val & 0x80) - { curIsCmd = 1; - } val &= 0x0F; // Every 8 bits instruction/data will be separated into 2 groups, higher 4 bits always 0 in every groups + if (rcvIndex == 0) { rcvData = val << 4; // Higher 4 bits in first byte rcvIndex++; + return; } else diff --git a/TFT/src/User/API/UI/HD44780_Emulator.h b/TFT/src/User/API/UI/HD44780_Emulator.h index de66809cc2..a936e1b336 100644 --- a/TFT/src/User/API/UI/HD44780_Emulator.h +++ b/TFT/src/User/API/UI/HD44780_Emulator.h @@ -6,206 +6,6 @@ extern "C" { #endif #include -#include "../../Configuration.h" - -#define XROWS 20 -#define YROWS 4 -#define FONT_PIXEL (MIN(LCD_WIDTH / XROWS / BYTE_WIDTH, LCD_HEIGHT / YROWS / BYTE_HEIGHT)) -#define BITMAP_PIXEL (MIN(LCD_WIDTH / XROWS / 6, LCD_HEIGHT / YROWS / 9)) -#define XSTART ((LCD_WIDTH - FONT_PIXEL * XROWS * BYTE_WIDTH) / 2) -#define YSTART ((LCD_HEIGHT - FONT_PIXEL * YROWS * BYTE_HEIGHT) / 2) -#define YOFFSET (BYTE_HEIGHT * FONT_PIXEL - 9 * BITMAP_PIXEL) - -typedef void (*HD44780_CMD)(uint8_t); - -typedef enum -{ - HD44780_DATA_DDRAM = 0, - HD44780_DATA_CGRAM, -} HD44780_DATA_TYPE; - -typedef struct -{ - int16_t x; // current x pixel, range is 0 - 127 - int16_t y; // current y pixel, range is 0 - 63 - // Extended Instruction, The first address set Y, second address set X - uint8_t address_is_y; // record current address is Y or X -} HD44780_PIXEL; - -/*** Common Instruction ***/ - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t : 2, - f : 1, // Sets the character font. - n : 1, // Sets the number of display lines. - dl : 1; // 4/8-bit interface control bit (0-4bit, 1-8bit) - }; - }; -} HD44780_REG_FS; // Function Set - -/*** Basic Instruction ***/ - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t slr : 1, // Display Shift Control: (Shift Left/Right) - id : 1; // Address Counter Control: (Increase/Decrease) - }; - }; -} HD44780_BIREG_EMS; // Entry Mode Set - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t blink : 1, // Character Blink ON/OFF control bit - cursor : 1, // Cursor ON/OFF control bit - display : 1; // Display ON/OFF control bit - }; - }; -} HD44780_BIREG_DC; // Display Control - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t : 2, - rl : 1, // Cursor moves/shift left/right - sc : 1; // Display shift, cursor also follows to shift - }; - }; -} HD44780_BIREG_CDSC; // Cursor/Display Shift Control - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t ac : 6; // AC range is 00H…3FH - }; - }; -} HD44780_BIREG_CGRAMA; // Set CGRAM Address - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t ac : 7; // First line AC range is 80H … 93h - // Second line AC range is C0H … D3H - // Third line AC range is 94H … 2FH - // Fourth line AC range is D4H … E7H - }; - }; -} HD44780_BIREG_DDRAMA; // Set DDRAM Address - -/*** Extended Instruction ***/ - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t sb : 1; - }; - }; -} HD44780_EIREG_SB; // Standby - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t sr : 1; // "1", the Vertical Scroll mode is enabled. - // "0", “Set CGRAM Address” instruction (basic instruction) is enabled - }; - }; -} HD44780_EIREG_VSRA; // Vertical Scroll or RAM Address Select - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t r0 : 1, // R1 R0 Description - r1 : 1; // 0 0 First line normal or reverse - // 0 1 Second line normal or reverse - // 1 0 Third line normal or reverse - // 1 1 Fourth line normal or reverse - }; - }; -} HD44780_EIREG_REV; // Reverse - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t ac : 6; // HD44780_EIREG_VSR.sr = 1: AC5~AC0 is vertical scroll displacement address - }; - }; -} HD44780_EIREG_SA; // Set Scroll Address - -typedef struct -{ - union - { - uint8_t reg; - struct - { - uint8_t ac : 6; - }; - }; -} HD44780_EIREG_GRAMA; // Set Graphic RAM Address - -typedef struct -{ - // 1 << 0 : Display Clear - // 1 << 1 : Return Home - HD44780_BIREG_EMS ems; // 1 << 2 : Entry Mode Set - HD44780_BIREG_DC dc; // 1 << 3 : Display Control - HD44780_BIREG_CDSC cdsc; // 1 << 4 : Cursor/Display Shift Control - // 1 << 5 : Function Set - HD44780_BIREG_CGRAMA cgrama; // 1 << 6 : Set CGRAM Address - HD44780_BIREG_DDRAMA ddrama; // 1 << 7 : Set DDRAM Address -} HD44780_BI; // Basic Instruction - -typedef struct -{ - HD44780_REG_FS fs; // Function Set - HD44780_BI bi; // Basic Instruction - uint8_t reverse; // Begin with normal and toggle to reverse - uint8_t cgram; - HD44780_DATA_TYPE data_type; -} HD44780_REG; // Extended Instruction void HD44780_ParseRecv(uint8_t val); diff --git a/TFT/src/User/API/UI/ListItem.c b/TFT/src/User/API/UI/ListItem.c index a9f8fb78db..34f949efac 100644 --- a/TFT/src/User/API/UI/ListItem.c +++ b/TFT/src/User/API/UI/ListItem.c @@ -5,8 +5,8 @@ #define CUSTOM_VAL_LEN 7 -char * dynamic_label[LISTITEM_PER_PAGE]; -char dynamic_text_value[LISTITEM_PER_PAGE][10]; +static char * dynamic_label[LISTITEM_PER_PAGE]; +static char dynamic_text_value[LISTITEM_PER_PAGE][10]; void setDynamicLabel(uint8_t i, char * label) { @@ -59,7 +59,7 @@ static inline void DrawListItemPress(const GUI_RECT * rect, bool pressed) } // draw title text of list item -static inline void draw_itemtitle(GUI_POINT pos, LABEL label, uint8_t position, int textarea_width) +static void draw_itemtitle(GUI_POINT pos, LABEL label, uint8_t position, int textarea_width) { if (label.index != LABEL_NULL) { @@ -140,6 +140,7 @@ void ListItem_Display(const GUI_RECT * rect, uint8_t position, const LISTITEM * { GUI_ClearPrect(rect); } + GUI_RestoreColorDefault(); } // draw list items @@ -156,6 +157,7 @@ void ListItem_Display(const GUI_RECT * rect, uint8_t position, const LISTITEM * drawCharIcon(rect, LEFT, curitem->icon, true, infoSettings.bg_color); pos.x += (BYTE_HEIGHT + 1); } + textarea_width = LISTITEM_WIDTH - (pos.x + 1); // width after removing the width for icon draw_itemtitle(pos, curitem->titlelabel, position, textarea_width); break; @@ -181,6 +183,7 @@ void ListItem_Display(const GUI_RECT * rect, uint8_t position, const LISTITEM * drawCharIcon(rect, LEFT, curitem->icon, true, infoSettings.bg_color); pos.x += (BYTE_HEIGHT + 1); } + textarea_width = LISTITEM_WIDTH - (pos.x + BYTE_HEIGHT + 2); // width after removing the width for icon draw_itemtitle(pos, curitem->titlelabel, position, textarea_width); @@ -195,6 +198,7 @@ void ListItem_Display(const GUI_RECT * rect, uint8_t position, const LISTITEM * drawCharIcon(rect, LEFT, curitem->icon, true, infoSettings.bg_color); pos.x += (BYTE_HEIGHT + 3); } + GUI_ClearRect(pos.x, rect->y0, rect->x1 - BYTE_WIDTH * 8 - 1, rect->y1); // clear only text area textarea_width = LISTITEM_WIDTH - (pos.x + 1); // width after removing the width for icon draw_itemtitle(pos, curitem->titlelabel, position, textarea_width); @@ -211,5 +215,6 @@ void ListItem_Display(const GUI_RECT * rect, uint8_t position, const LISTITEM * { GUI_ClearPrect(rect); } + GUI_RestoreColorDefault(); } diff --git a/TFT/src/User/API/UI/ListItem.h b/TFT/src/User/API/UI/ListItem.h index c6ad33052b..30fe9bc2dd 100644 --- a/TFT/src/User/API/UI/ListItem.h +++ b/TFT/src/User/API/UI/ListItem.h @@ -9,10 +9,8 @@ extern "C" { #include #include -#include "GUI.h" -#include "menu.h" - -#define LISTBTN_BKCOLOR MAT_DARKSLATE +#include "GUI.h" // for GUI_RECT etc. +#include "menu.h" // for LISTITEM etc. void setDynamicLabel(uint8_t i, char * label); // set list item title to any text char * getDynamicLabel(uint8_t i); // get the custom text of the list item title @@ -22,8 +20,6 @@ char * getDynamicTextValue(uint8_t i); // get the custom text of the void setDynamicValue(uint8_t i, float value); // set list item value to any numeric value (upto 7 digits) void ListItem_Display(const GUI_RECT * rect, uint8_t position, const LISTITEM * curitem, bool pressed); -void ListMenuSetItem(const LISTITEM * menuItem, uint8_t position); -GUI_POINT getTextStartPoint(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey,ALIGN_POSITION pos, const char * textchar); #ifdef __cplusplus } diff --git a/TFT/src/User/API/UI/ListManager.c b/TFT/src/User/API/UI/ListManager.c index 216fbdcae0..2f6b63ea09 100644 --- a/TFT/src/User/API/UI/ListManager.c +++ b/TFT/src/User/API/UI/ListManager.c @@ -6,7 +6,7 @@ #define KEY_INDEX_BACK (KEY_INDEX_PAGEDOWN + 1) static LISTITEMS listItems; -static LISTITEM *totalItems; +static LISTITEM * totalItems; static uint16_t maxItemCount; static uint8_t maxPageCount; @@ -14,11 +14,11 @@ static uint16_t curPageIndex; static uint16_t * curPageIndexSource; static bool handleBack = true; -static void (*action_preparePage)(LISTITEMS * listItems, uint8_t index) = NULL; -static void (*action_prepareItem)(LISTITEM * item, uint16_t index, uint8_t itemPos) = NULL; +static void (* action_preparePage)(LISTITEMS * listItems, uint8_t index) = NULL; +static void (* action_prepareItem)(LISTITEM * item, uint16_t index, uint8_t itemPos) = NULL; // display page at selected index -void listViewSetCurPage(uint8_t curPage) +static void listViewSetCurPage(uint8_t curPage) { if (action_preparePage != NULL) { @@ -55,20 +55,9 @@ void listViewSetCurPage(uint8_t curPage) curPageIndex = curPage; } -/** - * @brief Set and innitialize list menu - * - * @param title Title of menu - * @param items Preset list of items. Set to NULL if not used. - * @param maxItems Maximum number of items possilbe in current list. - * @param curPage Display this page index. - * @param handleBackPress Set true to handle back button automatically. - * @param preparePage_action Pointer to function to execute for preparing page before display. Set to NULL if not used. - * @param prepareItem_action Pointer to function to execute for preparing item before display. Set to NULL if not used. - */ void listViewCreate(LABEL title, LISTITEM * items, uint16_t maxItems, uint16_t * curPage, bool handleBackPress, - void (*preparePage_action)(LISTITEMS * listItems, uint8_t pageIndex), - void (*prepareItem_action)(LISTITEM * item, uint16_t index, uint8_t itemPos)) + void (* preparePage_action)(LISTITEMS * listItems, uint8_t pageIndex), + void (* prepareItem_action)(LISTITEM * item, uint16_t index, uint8_t itemPos)) { listItems.title = title; totalItems = items; @@ -85,19 +74,12 @@ void listViewCreate(LABEL title, LISTITEM * items, uint16_t maxItems, uint16_t * menuDrawListPage(&listItems); } -// Set/Update List view title void listViewSetTitle(LABEL title) { listItems.title = title; menuSetTitle(&listItems.title); } -// Get current displayed pade index -uint8_t listViewGetCurPage(void) -{ - return curPageIndex; -} - // open next page static inline bool listViewNextPage(void) { @@ -132,25 +114,23 @@ static inline bool listViewPreviousPage(void) return true; } -// refresh list items void listViewRefreshPage(void) { menuRefreshListPage(); } -// refresh whole list menu void listViewRefreshMenu(void) { menuDrawListPage(&listItems); } -// refresh selected list item void listViewRefreshItem(uint16_t item) { uint8_t cur_i = item % LISTITEM_PER_PAGE; if (item > maxItemCount) return; // error index. + if (item < curPageIndex * LISTITEM_PER_PAGE || item >= (curPageIndex + 1) * LISTITEM_PER_PAGE) return; // not in cur page @@ -165,10 +145,15 @@ void listViewRefreshItem(uint16_t item) { listItems.items[cur_i].icon = CHARICON_NULL; } + menuDrawListItem(&listItems.items[cur_i], cur_i); } -// get index of selected item +uint8_t listViewGetCurPage(void) +{ + return curPageIndex; +} + uint16_t listViewGetSelectedIndex(void) { KEY_VALUES key_num = menuKeyGetValue(); diff --git a/TFT/src/User/API/UI/ListManager.h b/TFT/src/User/API/UI/ListManager.h index 64664ed98d..dc2581be0f 100644 --- a/TFT/src/User/API/UI/ListManager.h +++ b/TFT/src/User/API/UI/ListManager.h @@ -9,16 +9,27 @@ extern "C" { #include #include "menu.h" +/** + * @brief set and innitialize list view menu + * + * @param title Title of menu + * @param items Preset list of items. Set to NULL if not used. + * @param maxItems Maximum number of items possilbe in current list. + * @param curPage Display this page index. + * @param handleBackPress Set true to handle back button automatically. + * @param preparePage_action Pointer to function to execute for preparing page before display. Set to NULL if not used. + * @param prepareItem_action Pointer to function to execute for preparing item before display. Set to NULL if not used. + */ void listViewCreate(LABEL title, LISTITEM * items, uint16_t max_items, uint16_t * cur_page, bool handleBackPress, - void (*preparePage_action)(LISTITEMS * listItems, uint8_t pageIndex), - void (*prepareItem_action)(LISTITEM * item, uint16_t index, uint8_t itemPos)); + void (* preparePage_action)(LISTITEMS * listItems, uint8_t pageIndex), + void (* prepareItem_action)(LISTITEM * item, uint16_t index, uint8_t itemPos)); -void listViewSetTitle(LABEL title); -void listViewRefreshPage(void); -void listViewRefreshMenu(void); -void listViewRefreshItem(uint16_t item); -uint8_t listViewGetCurPage(void); -uint16_t listViewGetSelectedIndex(void); +void listViewSetTitle(LABEL title); // set / update list view title +void listViewRefreshPage(void); // refresh list items +void listViewRefreshMenu(void); // refresh whole list view menu +void listViewRefreshItem(uint16_t item); // refresh selected list item +uint8_t listViewGetCurPage(void); // get current displayed page index +uint16_t listViewGetSelectedIndex(void); // get index of selected item #ifdef __cplusplus } diff --git a/TFT/src/User/API/UI/Numpad.c b/TFT/src/User/API/UI/Numpad.c index 2f631e6b96..0759c34991 100644 --- a/TFT/src/User/API/UI/Numpad.c +++ b/TFT/src/User/API/UI/Numpad.c @@ -32,7 +32,7 @@ typedef enum NUM_KEY_IDLE = IDLE_TOUCH, } NUM_KEY_VALUES; -const GUI_RECT rect_of_numkey[KEY_COUNT] = { +static const GUI_RECT rect_of_numkey[KEY_COUNT] = { {0 * SKEYWIDTH, ICON_START_Y + 0 * SKEYHEIGHT, 1 * SKEYWIDTH, ICON_START_Y + 1 * SKEYHEIGHT}, // 1 {1 * SKEYWIDTH, ICON_START_Y + 0 * SKEYHEIGHT, 2 * SKEYWIDTH, ICON_START_Y + 1 * SKEYHEIGHT}, // 2 {2 * SKEYWIDTH, ICON_START_Y + 0 * SKEYHEIGHT, 3 * SKEYWIDTH, ICON_START_Y + 1 * SKEYHEIGHT}, // 3 @@ -54,22 +54,23 @@ const GUI_RECT rect_of_numkey[KEY_COUNT] = { {3 * SKEYWIDTH, ICON_START_Y + 3 * SKEYHEIGHT, 4 * SKEYWIDTH, ICON_START_Y + 4 * SKEYHEIGHT}, // Undo/Reset }; -const GUI_RECT oldParameterRect = {0, 0, LCD_WIDTH / 2 - BYTE_WIDTH, ICON_START_Y + 0 * SKEYHEIGHT}; -const GUI_RECT newParameterRect = {LCD_WIDTH / 2 + BYTE_WIDTH, 0, LCD_WIDTH, ICON_START_Y + 0 * SKEYHEIGHT}; -const GUI_RECT arrowRect = {LCD_WIDTH / 2 - BYTE_WIDTH, 0, LCD_WIDTH / 2 + BYTE_WIDTH, ICON_START_Y + 0 * SKEYHEIGHT}; +static const GUI_RECT oldParameterRect = {0, 0, LCD_WIDTH / 2 - BYTE_WIDTH, ICON_START_Y + 0 * SKEYHEIGHT}; +static const GUI_RECT newParameterRect = {LCD_WIDTH / 2 + BYTE_WIDTH, 0, LCD_WIDTH, ICON_START_Y + 0 * SKEYHEIGHT}; +static const GUI_RECT arrowRect = {LCD_WIDTH / 2 - BYTE_WIDTH, 0, LCD_WIDTH / 2 + BYTE_WIDTH, ICON_START_Y + 0 * SKEYHEIGHT}; -const char * const numPadKeyChar[KEY_COUNT] = { +static const char * const numPadKeyChar[KEY_COUNT] = { "1", "2", "3", "\u0894", "4", "5", "6", "\u0899", "7", "8", "9", "\u0895", ".", "0", "-", "\u08A5" }; -uint8_t numpadType = 0; // numpad type identifier +static uint8_t numpadType = 0; // numpad type identifier -void drawKeypadButton(uint8_t index, uint8_t isPressed) +static void drawKeypadButton(uint8_t index, uint8_t isPressed) { - if (index >= COUNT(rect_of_numkey)) return; + if (index >= COUNT(rect_of_numkey)) + return; #ifdef KEYBOARD_MATERIAL_THEME uint16_t fontcolor = CTRL_FONT_COLOR; @@ -112,18 +113,21 @@ void drawKeypadButton(uint8_t index, uint8_t isPressed) .rect = rectBtn}; setFontSize(FONT_SIZE_LARGE); + if (!(index == NUM_KEY_DEC && GET_BIT(numpadType, 0)) && !(index == NUM_KEY_MINUS && !GET_BIT(numpadType, 1))) GUI_DrawButton(&btn, isPressed); + setFontSize(FONT_SIZE_NORMAL); #else if (!isPressed) GUI_SetColor(infoSettings.bg_color); + GUI_DrawRect(rect_of_numkey[index].x0 + 2, rect_of_numkey[index].y0 + 2, rect_of_numkey[index].x1-2, rect_of_numkey[index].y1 - 2); GUI_SetColor(infoSettings.font_color); #endif // KEYBOARD_MATERIAL_THEME } -void Draw_keyboard(uint8_t * title, bool numberOnly, bool negative) +static void Draw_keyboard(uint8_t * title, bool numberOnly, bool negative) { numpadType = (negative << 1) | (numberOnly << 0); // numpad type identfier GUI_SetTextMode(GUI_TEXTMODE_TRANS); @@ -143,6 +147,7 @@ void Draw_keyboard(uint8_t * title, bool numberOnly, bool negative) { drawKeypadButton(i, false); } + GUI_SetColor(BAR_FONT_COLOR); #else GUI_ClearRect(0, 0, LCD_WIDTH, rect_of_numkey[0].y0); @@ -150,10 +155,11 @@ void Draw_keyboard(uint8_t * title, bool numberOnly, bool negative) // draw button borders GUI_SetColor(infoSettings.list_border_color); + for (int i = 0; i < 3; i++) { - GUI_VLine(rect_of_numkey[i].x1, rect_of_numkey[i].y0, rect_of_numkey[12+i].y1); - GUI_HLine(rect_of_numkey[i*4].x0, rect_of_numkey[i*4].y1, rect_of_numkey[3+i*4].x1); + GUI_VLine(rect_of_numkey[i].x1, rect_of_numkey[i].y0, rect_of_numkey[12 + i].y1); + GUI_HLine(rect_of_numkey[i * 4].x0, rect_of_numkey[i * 4].y1, rect_of_numkey[3 + i * 4].x1); } // draw value display border line @@ -170,6 +176,7 @@ void Draw_keyboard(uint8_t * title, bool numberOnly, bool negative) if (!numberOnly) GUI_DispStringInPrect(&rect_of_numkey[NUM_KEY_DEC],(uint8_t *)numPadKeyChar[NUM_KEY_DEC]); + if (negative) GUI_DispStringInPrect(&rect_of_numkey[NUM_KEY_MINUS],(uint8_t *)numPadKeyChar[NUM_KEY_MINUS]); @@ -182,8 +189,8 @@ void Draw_keyboard(uint8_t * title, bool numberOnly, bool negative) #endif // KEYBOARD_MATERIAL_THEME GUI_DispStringInPrect(&arrowRect,(uint8_t *)"\u089A"); - setFontSize(FONT_SIZE_LARGE); + if ((oldParameterRect.x1 - oldParameterRect.x0) <= GUI_StrPixelWidth_str(title)) setFontSize(FONT_SIZE_NORMAL); @@ -191,7 +198,7 @@ void Draw_keyboard(uint8_t * title, bool numberOnly, bool negative) setFontSize(FONT_SIZE_NORMAL); } -static inline void drawValue(char * str) +static void drawValue(char * str) { #ifdef KEYBOARD_MATERIAL_THEME GUI_SetBkColor(BAR_BG_COLOR); @@ -204,7 +211,6 @@ static inline void drawValue(char * str) setFontSize(FONT_SIZE_NORMAL); } -// Numpad for decimal numbers double numPadFloat(uint8_t * title, double param_val, double reset_val, bool negative) { NUM_KEY_VALUES key_num = NUM_KEY_IDLE; @@ -231,6 +237,7 @@ double numPadFloat(uint8_t * title, double param_val, double reset_val, bool neg while (TS_Sound == false) // TS_TouchSound gets true only when exit from numpad is requested { key_num = menuKeyGetValue(); + switch (key_num) { case NUM_KEY_EXIT: @@ -321,6 +328,7 @@ double numPadFloat(uint8_t * title, double param_val, double reset_val, bool neg { parameterBuf[bufIndex++] = '0'; // add zero between minus and decimal sign } + parameterBuf[bufIndex++] = '.'; parameterBuf[bufIndex] = '\0'; BUZZER_PLAY(SOUND_KEYPRESS); @@ -381,13 +389,13 @@ double numPadFloat(uint8_t * title, double param_val, double reset_val, bool neg loopBackEnd(); } - if (title == NULL) free(numTitle); + if (title == NULL) + free(numTitle); return param_val; } -// Numpad for integer numbers -int32_t numPadInt(uint8_t* title, int32_t param_val, int32_t reset_val, bool negative) +int32_t numPadInt(uint8_t * title, int32_t param_val, int32_t reset_val, bool negative) { NUM_KEY_VALUES key_num = NUM_KEY_IDLE; uint8_t * numTitle = title; @@ -414,6 +422,7 @@ int32_t numPadInt(uint8_t* title, int32_t param_val, int32_t reset_val, bool neg while (TS_Sound == false) // TS_TouchSound gets true only when exit from numpad is requested { key_num = menuKeyGetValue(); + switch (key_num) { case NUM_KEY_EXIT: @@ -496,7 +505,8 @@ int32_t numPadInt(uint8_t* title, int32_t param_val, int32_t reset_val, bool neg loopBackEnd(); } - if (title == NULL) free(numTitle); + if (title == NULL) + free(numTitle); return param_val; } diff --git a/TFT/src/User/API/UI/Numpad.h b/TFT/src/User/API/UI/Numpad.h index 982225c43f..e5c1593906 100644 --- a/TFT/src/User/API/UI/Numpad.h +++ b/TFT/src/User/API/UI/Numpad.h @@ -8,8 +8,8 @@ extern "C" { #include #include -double numPadFloat(uint8_t* title, double param_val, double reset_val, bool negative); -int32_t numPadInt(uint8_t* title, int32_t param_val, int32_t reset_val, bool negative); +double numPadFloat(uint8_t * title, double param_val, double reset_val, bool negative); // numpad for decimal numbers +int32_t numPadInt(uint8_t * title, int32_t param_val, int32_t reset_val, bool negative); // numpad for integer numbers #ifdef __cplusplus } diff --git a/TFT/src/User/API/UI/ST7920_Emulator.c b/TFT/src/User/API/UI/ST7920_Emulator.c index cf3a8f121b..93194d07a6 100644 --- a/TFT/src/User/API/UI/ST7920_Emulator.c +++ b/TFT/src/User/API/UI/ST7920_Emulator.c @@ -1,21 +1,29 @@ #include "ST7920_Emulator.h" #include "includes.h" #include "GUI.h" -#include "../../Configuration.h" #ifdef ST7920_EMULATOR -const ST7920_POSITION st7920_init_position = { - .xByte = 0, // current x byte, range is 0 ~ 15 byte = 0 ~ 127 pixel - .yPixel = 0, // current y pixel, range is 0 ~ 63 - .address_is_y = 1, // Extended Instruction, The first address set Y, second address set X +#define ST7920_GXROWS 128 +#define ST7920_GYROWS 64 +#define ST7920_GXDOT (MIN(LCD_WIDTH / ST7920_GXROWS, LCD_HEIGHT / ST7920_GYROWS)) +#define ST7920_GYDOT (ST7920_GXDOT) +#define ST7920_GXSTART ((LCD_WIDTH - ST7920_GXDOT * ST7920_GXROWS) / 2) +#define ST7920_GYSTART ((LCD_HEIGHT - ST7920_GYDOT * ST7920_GYROWS) / 2) + +typedef void (* FP_CMD)(uint8_t); + +static const ST7920_POSITION st7920_init_position = { + .xByte = 0, // Current x byte, range is 0 ~ 15 byte = 0 ~ 127 pixel + .yPixel = 0, // Current y pixel, range is 0 ~ 63 + .address_is_y = 1, // Extended instruction, the first address set Y, second address set X }; -const ST7920_REG st7920_inti_reg = { - .fs = //0x00, // default basic instruction, 8-bit MPU interface, Graphic display OFF - 0x26, // default extended instruction, 4-bit MPU interface, Graphic display On +static const ST7920_REG st7920_inti_reg = { + .fs = //0x00, // Default basic instruction, 8-bit MPU interface, graphic display OFF + 0x26, // Default extended instruction, 4-bit MPU interface, graphic display On .bi = { - .ems = 0x02, // Cursor move to right ,DDRAM address counter (AC) plus 1 + .ems = 0x02, // Cursor move to right, DDRAM address counter (AC) plus 1 .dc = 0x00, // Display, cursor and blink are ALL OFF .cdsc = 0x00, // No cursor or display shift operation }, @@ -28,18 +36,18 @@ const ST7920_REG st7920_inti_reg = { .ctrl_status = ST7920_IDLE, }; -ST7920 *pSt7920; +static const float st7920_gx_start_full = ST7920_GXSTART_FULLSCREEN; +static const float st7920_gy_start_full = ST7920_GYSTART_FULLSCREEN; +static const float st7920_gx_dot_full = ST7920_GXDOT_FULLSCREEN; +static const float st7920_gy_dot_full = ST7920_GYDOT_FULLSCREEN; +static const uint16_t st7920_gx_start = ST7920_GXSTART; +static const uint16_t st7920_gy_start = ST7920_GYSTART; +static const uint16_t st7920_gx_dot = ST7920_GXDOT; +static const uint16_t st7920_gy_dot = ST7920_GYDOT; -const float st7920_gx_start_full = ST7920_GXSTART_FULLSCREEN; -const float st7920_gy_start_full = ST7920_GYSTART_FULLSCREEN; -const float st7920_gx_dot_full = ST7920_GXDOT_FULLSCREEN; -const float st7920_gy_dot_full = ST7920_GYDOT_FULLSCREEN; -const uint16_t st7920_gx_start = ST7920_GXSTART; -const uint16_t st7920_gy_start = ST7920_GYSTART; -const uint16_t st7920_gx_dot = ST7920_GXDOT; -const uint16_t st7920_gy_dot = ST7920_GYDOT; +static ST7920 * pSt7920; -void ST7920_DrawPixel(int16_t x, int16_t y, bool isForeGround) +static inline void ST7920_DrawPixel(int16_t x, int16_t y, bool isForeGround) { if (infoSettings.marlin_fullscreen) { @@ -59,18 +67,18 @@ void ST7920_DrawPixel(int16_t x, int16_t y, bool isForeGround) } } -void ST7920_ClearRAM(void) +static void ST7920_ClearRAM(void) { // Clear CGRAM buffer memset(pSt7920->CGRAM, 0, sizeof(pSt7920->CGRAM)); - // Clear DDRAM buffer, Fill with "20H"(space code). + // Clear DDRAM buffer, fill with "20H"(space code) memset(pSt7920->DDRAM, ' ', sizeof(pSt7920->DDRAM)); // Clear GDRAM buffer memset(pSt7920->GDRAM, 0, sizeof(pSt7920->GDRAM)); } // Init St7920 struct and load 8x16 ASCII font from SPI flash -void ST7920_Init(ST7920 *pStruct) +void ST7920_Init(ST7920 * pStruct) { pSt7920 = pStruct; // Init pSt7920 @@ -82,31 +90,35 @@ void ST7920_Init(ST7920 *pStruct) } // When I/D = "1", cursor moves right, address counter (AC) is increased by 1 -void ST7920_CursorIncrease(void) +static void ST7920_CursorIncrease(void) { pSt7920->position.xByte++; + if (pSt7920->position.xByte >= 16) { pSt7920->position.xByte = 0; pSt7920->position.yPixel = (pSt7920->position.yPixel + 32); - if (pSt7920->position.yPixel == 64) pSt7920->position.yPixel = 16; - else if (pSt7920->position.yPixel == 80) pSt7920->position.yPixel = 0; + + if (pSt7920->position.yPixel == 64) + pSt7920->position.yPixel = 16; + else if (pSt7920->position.yPixel == 80) + pSt7920->position.yPixel = 0; } } -// Draw the byte at the specified position +// Draw the byte at the specified position. // The byte comes from the XOR of DDRAM(CGRAM or HCGROM) and GDRAM -void ST7920_DrawByte(uint8_t xByte, uint8_t yPixel) +static void ST7920_DrawByte(uint8_t xByte, uint8_t yPixel) { uint8_t yByte = yPixel / 16; uint8_t yOffset = yPixel & 0x0F; - uint8_t xStartByte = xByte & 0xFE; // CGRAM fonts can only be displayed in the start position of each address - + uint8_t xStartByte = xByte & 0xFE; // CGRAM fonts can only be displayed in the start position of each address uint8_t drawByte = 0; if (pSt7920->DDRAM[yByte][xStartByte] == 0) // 0x00 means this is a CGRAM data(0x0000, 0x0002, 0x0004, 0x0006) { uint8_t np = pSt7920->DDRAM[yByte][xStartByte + 1] & 0xF6; // bit0 and bit3 are don’t care + if (xByte == xStartByte) // CGRAM high 8bits { drawByte = pSt7920->CGRAM[np * 8 + yOffset][0]; @@ -120,10 +132,12 @@ void ST7920_DrawByte(uint8_t xByte, uint8_t yPixel) { // HCGROM fonts uint8_t p = pSt7920->DDRAM[yByte][xByte]; + drawByte = pSt7920->_8x16Font[p - ' '][yOffset]; } drawByte ^= pSt7920->GDRAM[yPixel][xByte]; // XOR GDRAM + for (uint8_t i = 0; i < 8; i++) { ST7920_DrawPixel(xByte * 8 + i, yPixel, drawByte & (1 << 7)); @@ -132,9 +146,10 @@ void ST7920_DrawByte(uint8_t xByte, uint8_t yPixel) } // Display graphic -void ST7920_DrawGDRAM(uint8_t data) +static inline void ST7920_DrawGDRAM(uint8_t data) { - if (pSt7920->reg.fs.g != 1) return; // Graphic display off + if (pSt7920->reg.fs.g != 1) // Graphic display off + return; pSt7920->GDRAM[pSt7920->position.yPixel][pSt7920->position.xByte] = data; @@ -144,7 +159,7 @@ void ST7920_DrawGDRAM(uint8_t data) } // Display DDRAM(CGRAM or HCGROM) -void ST7920_DrawDDRAM(uint8_t data) +static inline void ST7920_DrawDDRAM(uint8_t data) { uint8_t xStartByte = pSt7920->position.xByte & 0xFE; // CGRAM fonts can only be displayed in the start position of each address uint8_t yByte = pSt7920->position.yPixel / 16; @@ -158,7 +173,7 @@ void ST7920_DrawDDRAM(uint8_t data) { if (xStartByte != pSt7920->position.xByte) { - // Display CGRAM 16*16 bitmap fonts + // Display CGRAM 16 * 16 bitmap fonts for (uint8_t y = pSt7920->position.yPixel; y < ey; y++) { ST7920_DrawByte(xStartByte, y); @@ -168,7 +183,7 @@ void ST7920_DrawDDRAM(uint8_t data) } else { - // Display HCGROM fonts, 02H~7EH. + // Display HCGROM fonts, 02H~7EH if (data >= ' ' && data <= '~') { for (uint8_t y = pSt7920->position.yPixel; y < ey; y++) @@ -193,18 +208,18 @@ void ST7920_DrawDDRAM(uint8_t data) ST7920_CursorIncrease(); } -/*** Common instruction ***/ +/*** Common Instruction ***/ // cmd : 1 << 5 -void ST7920_CI15_FunctionSet(uint8_t cmd) +static void ST7920_CI15_FunctionSet(uint8_t cmd) { pSt7920->reg.fs.reg = cmd; } -/*** Basic instruction ***/ +/*** Basic Instruction ***/ // cmd : 1 << 0 -void ST7920_BI10_DisplayClear(uint8_t cmd) +static inline void ST7920_BI10_DisplayClear(uint8_t cmd) { // Set DDRAM address counter (AC) to"00H" pSt7920->position.xByte = pSt7920->position.yPixel = 0; @@ -215,46 +230,49 @@ void ST7920_BI10_DisplayClear(uint8_t cmd) } // cmd : 1 << 1 -void ST7920_BI11_ReturnHome(uint8_t cmd) +static inline void ST7920_BI11_ReturnHome(uint8_t cmd) { - // Set address counter (AC) to "00H". + // Set address counter (AC) to "00H" pSt7920->position.xByte = pSt7920->position.yPixel = 0; } // cmd : 1 << 2 -void ST7920_BI12_EntryModeSet(uint8_t cmd) +static inline void ST7920_BI12_EntryModeSet(uint8_t cmd) { pSt7920->reg.bi.ems.reg = cmd; } // cmd : 1 << 3 -void ST7920_BI13_DisplayControl(uint8_t cmd) +static inline void ST7920_BI13_DisplayControl(uint8_t cmd) { pSt7920->reg.bi.dc.reg = cmd; } // cmd : 1 << 4 -void ST7920_BI14_CursorDisplayControl(uint8_t cmd) +static inline void ST7920_BI14_CursorDisplayControl(uint8_t cmd) { pSt7920->reg.bi.cdsc.reg = cmd; } // cmd : 1 << 6 -void ST7920_BI16_SetCGRAMAddress(uint8_t cmd) +static inline void ST7920_BI16_SetCGRAMAddress(uint8_t cmd) { pSt7920->reg.bi.cgrama.reg = cmd; + uint8_t address = pSt7920->reg.bi.cgrama.ac; + // Set CGRAM address - // xByte is 0-1 = [2*8], yPixel is 0-64 = [4*16], means 4 * [16*16] bitmap font + // xByte is 0-1 = [2 * 8], yPixel is 0-64 = [4 * 16], means 4 * [16 * 16] bitmap font pSt7920->position.xByte = 0; pSt7920->position.yPixel = address; pSt7920->reg.data_type = ST7920_DATA_CGRAM; } -// the data follow cmd : 1 << 6 -void ST7920_BI_SetCGRAMData(uint8_t data) +// The data follow cmd : 1 << 6 +static inline void ST7920_BI_SetCGRAMData(uint8_t data) { pSt7920->CGRAM[pSt7920->position.yPixel][pSt7920->position.xByte++] = data; + if (pSt7920->position.xByte >= 2) { pSt7920->position.xByte = 0; @@ -263,10 +281,12 @@ void ST7920_BI_SetCGRAMData(uint8_t data) } // cmd : 1 << 7 -void ST7920_BI17_SetDDRAMAddress(uint8_t cmd) +static inline void ST7920_BI17_SetDDRAMAddress(uint8_t cmd) { pSt7920->reg.bi.ddrama.reg = cmd; + uint8_t address = pSt7920->reg.bi.ddrama.ac; + // Set DDRAM address // xByte is 0~15. y is 0~63 pSt7920->position.xByte = (address & 0x07) * 2; @@ -277,35 +297,37 @@ void ST7920_BI17_SetDDRAMAddress(uint8_t cmd) /*** Extended Instruction ***/ // cmd : 1 << 0 -void ST7920_EI10_StandBy(uint8_t cmd) +static inline void ST7920_EI10_StandBy(uint8_t cmd) { pSt7920->reg.ei.sb.reg = cmd; } // cmd : 1 << 1 -void ST7920_EI11_ScrollOrRAMAddress(uint8_t cmd) +static inline void ST7920_EI11_ScrollOrRAMAddress(uint8_t cmd) { pSt7920->reg.ei.vsra.reg = cmd; } // cmd : 1 << 2 -void ST7920_EI12_Reverse(uint8_t cmd) +static inline void ST7920_EI12_Reverse(uint8_t cmd) { pSt7920->reg.ei.rev.reg = cmd; pSt7920->reg.reverse = !pSt7920->reg.reverse; // Reverse the display by toggling this instruction } // cmd : 1 << 6 -void ST7920_EI16_SetScrollAddress(uint8_t cmd) +static inline void ST7920_EI16_SetScrollAddress(uint8_t cmd) { pSt7920->reg.ei.sa.reg = cmd; } // cmd : 1 << 7 -void ST7920_EI17_SetGDRAMAddress(uint8_t cmd) +static inline void ST7920_EI17_SetGDRAMAddress(uint8_t cmd) { pSt7920->reg.ei.gdrama.reg = cmd; + uint8_t address = pSt7920->reg.ei.gdrama.ac; + if (pSt7920->position.address_is_y) { pSt7920->position.yPixel = address; @@ -313,10 +335,13 @@ void ST7920_EI17_SetGDRAMAddress(uint8_t cmd) } else { - if (address & 0x08) pSt7920->position.yPixel += 32; + if (address & 0x08) + pSt7920->position.yPixel += 32; + pSt7920->position.xByte = (address & 0x07) * 2; pSt7920->position.address_is_y = 1; } + pSt7920->reg.data_type = ST7920_DATA_GDRAM; } @@ -332,7 +357,7 @@ static const FP_CMD st7920CmdCallBack[8][2] = { { ST7920_BI17_SetDDRAMAddress, ST7920_EI17_SetGDRAMAddress}, // cmd 1 << 7 }; -void ST7920_ST7920_ParseWCmd(uint8_t cmd) +static inline void ST7920_ST7920_ParseWCmd(uint8_t cmd) { for (int8_t i = 7; i >= 0; i--) { @@ -342,12 +367,13 @@ void ST7920_ST7920_ParseWCmd(uint8_t cmd) { (*st7920CmdCallBack[i][pSt7920->reg.fs.re])(cmd); } + break; } } } -void ST7920_ST7920_ParseWData(uint8_t data) +static inline void ST7920_ST7920_ParseWData(uint8_t data) { switch (pSt7920->reg.data_type) { @@ -365,7 +391,7 @@ void ST7920_ST7920_ParseWData(uint8_t data) } } -uint8_t ST7920_IsCtrlByte(uint8_t data) +static inline uint8_t ST7920_IsCtrlByte(uint8_t data) { if (data == ST7920_WCMD || data == ST7920_WDATA || data == ST7920_RCMD || data == ST7920_RDATA) return true; @@ -378,22 +404,24 @@ void ST7920_ParseRecv(uint8_t val) { static uint8_t rcvData = 0; static uint8_t rcvIndex = 0; + if (ST7920_IsCtrlByte(val)) { pSt7920->reg.ctrl_status = (ST7920_CTRL_STATUS)val; rcvIndex = 0; + if (pSt7920->reg.ctrl_status == ST7920_WDATA) - { pSt7920->position.address_is_y = 1; - } } else { val &= 0xF0; // Every 8 bits instruction/data will be separated into 2 groups, lower 4 bits always 0 in every groups + if (rcvIndex == 0) { rcvData = val; // Higher 4 bits in first byte rcvIndex++; + return; } else diff --git a/TFT/src/User/API/UI/ST7920_Emulator.h b/TFT/src/User/API/UI/ST7920_Emulator.h index 536cc964f6..ec9f6baff8 100644 --- a/TFT/src/User/API/UI/ST7920_Emulator.h +++ b/TFT/src/User/API/UI/ST7920_Emulator.h @@ -6,10 +6,6 @@ extern "C" { #endif #include -#include "../../Configuration.h" - -#define ST7920_XSTART (0x80) -#define ST7920_YSTART (0x80) // ST7920_FULLSCREEN #define ST7920_GXROWS_FULLSCREEN 128.0f @@ -19,15 +15,6 @@ extern "C" { #define ST7920_GXSTART_FULLSCREEN ((LCD_WIDTH - ST7920_GXDOT_FULLSCREEN * ST7920_GXROWS_FULLSCREEN) / 2) #define ST7920_GYSTART_FULLSCREEN ((LCD_HEIGHT - ST7920_GYDOT_FULLSCREEN * ST7920_GYROWS_FULLSCREEN) / 2) -#define ST7920_GXROWS 128 -#define ST7920_GYROWS 64 -#define ST7920_GXDOT (MIN(LCD_WIDTH / ST7920_GXROWS, LCD_HEIGHT / ST7920_GYROWS)) -#define ST7920_GYDOT (ST7920_GXDOT) -#define ST7920_GXSTART ((LCD_WIDTH - ST7920_GXDOT * ST7920_GXROWS) / 2) -#define ST7920_GYSTART ((LCD_HEIGHT - ST7920_GYDOT * ST7920_GYROWS) / 2) - -typedef void (*FP_CMD)(uint8_t); - typedef enum { ST7920_IDLE = 0, @@ -46,10 +33,10 @@ typedef enum typedef struct { - int16_t xByte; // current x byte, range is 0 ~ 15 byte = 0 ~ 127 pixel - int16_t yPixel; // current y pixel, range is 0 ~ 63 - // Extended Instruction, The first address set Y, second address set X - uint8_t address_is_y; // record current address is Y or X + int16_t xByte; // Current x byte, range is 0 ~ 15 byte = 0 ~ 127 pixel + int16_t yPixel; // Current y pixel, range is 0 ~ 63 + // Extended Instruction, the first address set Y, second address set X + uint8_t address_is_y; // Record current address is Y or X } ST7920_POSITION; /*** Common Instruction ***/ @@ -63,7 +50,7 @@ typedef struct { uint8_t : 1, g : 1, // Graphic display control bit - re : 1, // extended instruction set control bit + re : 1, // Extended instruction set control bit :1, dl : 1; // 4/8-bit interface control bit (0-4bit, 1-8bit) }; @@ -161,7 +148,7 @@ typedef struct uint8_t reg; struct { - uint8_t sr : 1; // "1", the Vertical Scroll mode is enabled. + uint8_t sr : 1; // "1", the Vertical Scroll mode is enabled // "0", “Set CGRAM Address” instruction (basic instruction) is enabled }; }; @@ -241,23 +228,23 @@ typedef struct ST7920_CTRL_STATUS ctrl_status; } ST7920_REG; // Extended Instruction -typedef struct +typedef struct { // Position info ST7920_POSITION position; // ST7920 register ST7920_REG reg; // Character Generation RAM - uint8_t CGRAM[64][2]; // [64*2] = [4 * 16*2*8], means 4 * [16*16] bitmap font + uint8_t CGRAM[64][2]; // [64 * 2] = [4 * 16 * 2 * 8], means 4 * [16 * 16] bitmap font // Display Data RAM - uint8_t DDRAM[4][16]; // 16 characters (8x16) by 4 lines + uint8_t DDRAM[4][16]; // 16 characters (8x16) by 4 lines // Graphic Display RAM - uint8_t GDRAM[64][16]; // [64*16] = [64 * 16*8], means 64 * 128 pixels + uint8_t GDRAM[64][16]; // [64 * 16] = [64 * 16 * 8], means 64 * 128 pixels // 8x16 ASCII font uint8_t _8x16Font[95][16]; // 0x20 ~ 0x7E = 95char, 8width * 16height / 8 = 16 bytes, scan dir: left to right & top to bottom } ST7920; -void ST7920_Init(ST7920 *pStruct); +void ST7920_Init(ST7920 * pStruct); void ST7920_ParseRecv(uint8_t val); #ifdef __cplusplus diff --git a/TFT/src/User/API/UI/ui_draw.c b/TFT/src/User/API/UI/ui_draw.c index a4d22b00dd..b4647f7b92 100644 --- a/TFT/src/User/API/UI/ui_draw.c +++ b/TFT/src/User/API/UI/ui_draw.c @@ -10,8 +10,11 @@ #include "base64.h" #endif +#define COLOR_BYTE_SIZE sizeof(uint16_t) // RGB565 color byte is equal to uint16_t + #ifdef STM32_HAS_FSMC +// defined and implemented on lcd_dma.h / .c void lcd_frame_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint32_t addr); #else @@ -44,7 +47,7 @@ void lcd_frame_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint32_ #endif -void lcd_buffer_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint16_t *buf, GUI_RECT *limit) +void lcd_buffer_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint16_t * buf, GUI_RECT * limit) { uint16_t wl = w - limit->x1; uint16_t hl = h - limit->y1; @@ -62,18 +65,18 @@ void lcd_buffer_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint16 } } -void getBMPsize(BMP_INFO *bmp) +void getBMPsize(BMP_INFO * bmp) { if (!bmp->address && bmp->index < ICON_NULL) bmp->address = ICON_ADDR(bmp->index); - W25Qxx_ReadBuffer((uint8_t*)&bmp->width, bmp->address, COLOR_BYTE_SIZE); + W25Qxx_ReadBuffer((uint8_t *)&bmp->width, bmp->address, COLOR_BYTE_SIZE); bmp->address += COLOR_BYTE_SIZE; - W25Qxx_ReadBuffer((uint8_t*)&bmp->height, bmp->address, COLOR_BYTE_SIZE); + W25Qxx_ReadBuffer((uint8_t *)&bmp->height, bmp->address, COLOR_BYTE_SIZE); bmp->address += COLOR_BYTE_SIZE; } -void bmpToBuffer(uint16_t *buf, GUI_POINT startPoint, GUI_POINT endPoint, BMP_INFO *iconInfo) +static inline void bmpToBuffer(uint16_t * buf, GUI_POINT startPoint, GUI_POINT endPoint, BMP_INFO * iconInfo) { uint16_t frameLines = (endPoint.y - startPoint.y); // total lines in frame uint16_t blockLines = (endPoint.y >= iconInfo->height) ? (iconInfo->height - startPoint.y) : frameLines; // total drawable lines @@ -141,7 +144,7 @@ void ICON_ReadDisplay(uint16_t sx, uint16_t sy, uint8_t icon) } // load the selected area of bmp icon from flash to buffer -void ICON_ReadBuffer(uint16_t *buf, uint16_t x, uint16_t y, int16_t w, int16_t h, uint16_t icon) +void ICON_ReadBuffer(uint16_t * buf, uint16_t x, uint16_t y, int16_t w, int16_t h, uint16_t icon) { BMP_INFO iconInfo = {.index = icon, .address = 0}; GUI_POINT startPoint = {.x = x, .y = y}; @@ -153,11 +156,10 @@ void ICON_ReadBuffer(uint16_t *buf, uint16_t x, uint16_t y, int16_t w, int16_t h uint16_t ICON_ReadPixel(uint32_t address, uint16_t w, uint16_t h, int16_t x, int16_t y) { - // Out of range calls + // out of range calls if (x > w || y > h) return infoSettings.bg_color; - uint16_t color; address += ((w * y) + x) * COLOR_BYTE_SIZE; W25Qxx_SPI_CS_Set(0); @@ -166,7 +168,9 @@ uint16_t ICON_ReadPixel(uint32_t address, uint16_t w, uint16_t h, int16_t x, int W25Qxx_SPI_Read_Write_Byte((address & 0xFF00) >> 8); W25Qxx_SPI_Read_Write_Byte(address & 0xFF); - color = (W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE) << 8); + uint16_t color; + + color = (W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE) << 8); color |= W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); W25Qxx_SPI_CS_Set(1); @@ -174,11 +178,11 @@ uint16_t ICON_ReadPixel(uint32_t address, uint16_t w, uint16_t h, int16_t x, int return color; } -uint16_t modelFileReadHalfword(FIL *fp) +static uint16_t modelFileReadHalfword(FIL * fp) { uint8_t buf[4]; uint8_t ascii[4]; - uint8_t *pd = ascii; + uint8_t * pd = ascii; UINT mybr; uint8_t rest = 4; @@ -189,8 +193,8 @@ uint16_t modelFileReadHalfword(FIL *fp) if (mybr != rest) return 0; - // Check buf for non-valid character i.e. not 0-9, a-f or A-F and skip - for (uint8_t *ps = buf; ps < buf + rest; ) + // check buf for non-valid character i.e. not 0-9, a-f or A-F and skip + for (uint8_t * ps = buf; ps < buf + rest; ) { char c = *ps++; @@ -206,15 +210,15 @@ uint16_t modelFileReadHalfword(FIL *fp) #if (THUMBNAIL_PARSER == PARSER_RGB565) || (THUMBNAIL_PARSER == PARSER_BASE64PNG) -// Define where to search for dedicated thumbnail comments at max (defaults to first 100kb) +// define where to search for dedicated thumbnail comments at max (defaults to first 100kb) #define BLOCKSIZE_THUMBNAIL_SEARCH (512) #define MAX_THUMBNAIL_SEARCH_BLOCKS (100 * 2) -// Search for the gcode thumbnail comment signature within the first BLOCKSIZE_THUMBNAIL_SEARCH * MAX_THUMBNAIL_SEARCH_BLOCKS bytes -bool modelFileFind(FIL *fp, char *find) +// search for the gcode thumbnail comment signature within the first BLOCKSIZE_THUMBNAIL_SEARCH * MAX_THUMBNAIL_SEARCH_BLOCKS bytes +static bool modelFileFind(FIL * fp, char * find) { char search_buf[BLOCKSIZE_THUMBNAIL_SEARCH]; - char *cFind = find; + char * cFind = find; dbg_printf("Find: '%s' starting from %d\n", find, f_tell(fp)); @@ -228,7 +232,7 @@ bool modelFileFind(FIL *fp, char *find) if (len == 0) return false; - for (char *cSearch = search_buf; cSearch < search_buf + len; cSearch++) + for (char * cSearch = search_buf; cSearch < search_buf + len; cSearch++) { if (*cSearch == *cFind) cFind++; @@ -252,8 +256,8 @@ bool modelFileFind(FIL *fp, char *find) #if (THUMBNAIL_PARSER == PARSER_BASE64PNG) -// Read an unsigned int value from a file -uint32_t modelFileReadValue(FIL *fp) +// read an unsigned int value from a file +static inline uint32_t modelFileReadValue(FIL * fp) { char current = 0; UINT br = 0; @@ -274,30 +278,30 @@ uint32_t modelFileReadValue(FIL *fp) return 0; } -uint32_t modelFileSeekToThumbnailBase64PNG(FIL *fp, uint16_t width, uint16_t height) +static inline uint32_t modelFileSeekToThumbnailBase64PNG(FIL * fp, uint16_t width, uint16_t height) { uint32_t len = 0; char buf[32]; - // Find thumbnail begin marker for the right thumbnail resolution and read the base64 length + // find thumbnail begin marker for the right thumbnail resolution and read the base64 length snprintf(buf, sizeof(buf), "; thumbnail begin %hux%hu ", width, height); dbg_print("Start search\n"); - // Seek to the beginning of the file as the file pointer was moved during the RGB565 thumbnail search + // seek to the beginning of the file as the file pointer was moved during the RGB565 thumbnail search f_lseek(fp, 0); if (!modelFileFind(fp, buf)) return 0; dbg_printf("Found signature '%s' at %ld\n", buf, f_tell(fp)); - // Get the base64 length of the encoded PNG thumbnail file + // get the base64 length of the encoded PNG thumbnail file len = modelFileReadValue(fp); dbg_printf("Base64 len=%d\n", len); if (len == 0) return 0; - // Seek to the start of the base64 block + // seek to the start of the base64 block if (!modelFileFind(fp, ";")) return 0; @@ -306,7 +310,7 @@ uint32_t modelFileSeekToThumbnailBase64PNG(FIL *fp, uint16_t width, uint16_t hei static b64_decoder_t gcode_thumb_b64; -uint16_t color_alpha_565(const uint8_t r0, const uint8_t g0, const uint8_t b0, const uint8_t r1, const uint8_t g1, const uint8_t b1, const uint8_t alpha) +static uint16_t color_alpha_565(const uint8_t r0, const uint8_t g0, const uint8_t b0, const uint8_t r1, const uint8_t g1, const uint8_t b1, const uint8_t alpha) { const uint8_t r = ((255 - alpha) * r0 + alpha * r1) / 255; const uint8_t g = ((255 - alpha) * g0 + alpha * g1) / 255; @@ -315,9 +319,9 @@ uint16_t color_alpha_565(const uint8_t r0, const uint8_t g0, const uint8_t b0, c return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3); } -void on_draw_png_pixel(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4]) +static inline void on_draw_png_pixel(pngle_t * pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4]) { - GUI_POINT *pos = (GUI_POINT *)pngle_get_user_data(pngle); + GUI_POINT * pos = (GUI_POINT *)pngle_get_user_data(pngle); LCD_SetWindow(pos->x + x, pos->y + y, pos->x + x, pos->y + y); LCD_WR_16BITS_DATA(color_alpha_565(0, 0, 0, rgba[0], rgba[1], rgba[2], rgba[3])); @@ -330,9 +334,9 @@ typedef struct uint8_t buf[256]; } W25Qxx_DECODE; -void on_decode_png_pixel(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4]) +static void on_decode_png_pixel(pngle_t * pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4]) { - W25Qxx_DECODE *session = (W25Qxx_DECODE *) pngle_get_user_data(pngle); + W25Qxx_DECODE * session = (W25Qxx_DECODE *) pngle_get_user_data(pngle); uint16_t color = color_alpha_565(0, 0, 0, rgba[0], rgba[1], rgba[2], rgba[3]); @@ -347,9 +351,9 @@ void on_decode_png_pixel(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uin } } -void on_decode_png_pixel_done(pngle_t *pngle) +static inline void on_decode_png_pixel_done(pngle_t * pngle) { - W25Qxx_DECODE *session = (W25Qxx_DECODE *) pngle_get_user_data(pngle); + W25Qxx_DECODE * session = (W25Qxx_DECODE *) pngle_get_user_data(pngle); W25Qxx_WritePage(session->buf, session->addr, session->bnum); } @@ -360,27 +364,28 @@ void on_decode_png_pixel_done(pngle_t *pngle) * * ; thumbnail end */ -bool model_Process_Base64PNG(FIL *gcodeFile, void *user_data, pngle_draw_callback_t draw_callback, - pngle_done_callback_t done_callback) +static bool model_Process_Base64PNG(FIL * gcodeFile, void * user_data, pngle_draw_callback_t draw_callback, + pngle_done_callback_t done_callback) { uint32_t base64_len; char buf[256]; dbg_printf("Finding BASE64PNG\n"); - // Find thumbnail block with correct picture size + // find thumbnail block with correct picture size base64_len = modelFileSeekToThumbnailBase64PNG(gcodeFile, ICON_WIDTH, ICON_HEIGHT); if (base64_len == 0) { dbg_printf("thumbnail for w=%d,h=%d not found.\n", ICON_WIDTH, ICON_HEIGHT); + return false; } // Base64 decode on the fly while reading the PNG b64_init(&gcode_thumb_b64, gcodeFile, base64_len); - pngle_t *pngle = pngle_new(); + pngle_t * pngle = pngle_new(); if (!pngle) goto pngle_new_failed; @@ -422,12 +427,12 @@ bool model_Process_Base64PNG(FIL *gcodeFile, void *user_data, pngle_draw_callbac return false; } -bool model_DirectDisplay_Base64PNG(GUI_POINT pos, FIL *gcodeFile) +static inline bool model_DirectDisplay_Base64PNG(GUI_POINT pos, FIL * gcodeFile) { return model_Process_Base64PNG(gcodeFile, &pos, on_draw_png_pixel, NULL); } -bool model_DecodeToFlash_Base64PNG(FIL *gcodeFile, uint32_t addr) +static inline bool model_DecodeToFlash_Base64PNG(FIL * gcodeFile, uint32_t addr) { uint16_t w = ICON_WIDTH; uint16_t h = ICON_HEIGHT; @@ -446,25 +451,26 @@ bool model_DecodeToFlash_Base64PNG(FIL *gcodeFile, uint32_t addr) #if (THUMBNAIL_PARSER == PARSER_BASE64PNG) || (THUMBNAIL_PARSER == PARSER_RGB565) -bool modelFileSeekToThumbnailRGB565(FIL *fp, uint16_t width, uint16_t height) +static inline bool modelFileSeekToThumbnailRGB565(FIL * fp, uint16_t width, uint16_t height) { char buf[39]; dbg_printf("Finding RGB565 by signature\n"); - // Find thumbnail begin marker for the right thumbnail resolution and read the base64 length + // find thumbnail begin marker for the right thumbnail resolution and read the base64 length snprintf(buf, sizeof(buf), "; bigtree thumbnail begin %hux%hu", width, height); if (modelFileFind(fp, buf)) { dbg_printf("Found signature '%s' at %ld\n", buf, f_tell(fp)); - // Seek to the start of the RGB565 block + // seek to the start of the RGB565 block if (modelFileFind(fp, ";")) return true; } dbg_printf("bigtree thumbnail for w=%d,h=%d not found.\n", ICON_WIDTH, ICON_HEIGHT); + return false; } @@ -477,23 +483,24 @@ bool modelFileSeekToThumbnailRGB565(FIL *fp, uint16_t width, uint16_t height) * * ; bigtreetech thumbnail end */ -bool model_DirectDisplay_Classic(GUI_POINT pos, FIL *gcodeFile) +static inline bool model_DirectDisplay_Classic(GUI_POINT pos, FIL * gcodeFile) { // try finding RGB565 thumbnail signature #if (THUMBNAIL_PARSER >= PARSER_RGB565) - // Move the file cursor to the signature location if found + // move the file cursor to the signature location if found if (!modelFileSeekToThumbnailRGB565(gcodeFile, ICON_WIDTH, ICON_HEIGHT)) #endif { dbg_printf("Finding RGB565 by predefined offset\n"); - // Move the file cursor to the predefined location + // move the file cursor to the predefined location f_lseek(gcodeFile, MODEL_PREVIEW_OFFSET); - // Check whether the icon size matches + // check whether the icon size matches if (modelFileReadHalfword(gcodeFile) != ICON_WIDTH || modelFileReadHalfword(gcodeFile) != ICON_HEIGHT) { dbg_printf("RGB565 not found\n"); + return false; } } @@ -504,10 +511,11 @@ bool model_DirectDisplay_Classic(GUI_POINT pos, FIL *gcodeFile) { LCD_WR_16BITS_DATA(modelFileReadHalfword(gcodeFile)); } + return true; } -bool model_DirectDisplay(GUI_POINT pos, char *gcode) +bool model_DirectDisplay(GUI_POINT pos, char * gcode) { FIL gcodeFile; @@ -516,7 +524,7 @@ bool model_DirectDisplay(GUI_POINT pos, char *gcode) if (f_open(&gcodeFile, gcode, FA_OPEN_EXISTING | FA_READ) != FR_OK) return false; - // Try all available options from fastest to slowest + // try all available options from fastest to slowest if (model_DirectDisplay_Classic(pos, &gcodeFile)) return true; @@ -528,22 +536,21 @@ bool model_DirectDisplay(GUI_POINT pos, char *gcode) return false; } -bool model_DecodeToFlash_Classic(FIL *gcodeFile, uint32_t addr) +static inline bool model_DecodeToFlash_Classic(FIL * gcodeFile, uint32_t addr) { uint16_t bnum = 0; uint16_t w = ICON_WIDTH; uint16_t h = ICON_HEIGHT; - uint8_t buf[256]; - // Move the file cursor to the corresponding resolution area + // move the file cursor to the corresponding resolution area f_lseek(gcodeFile, MODEL_PREVIEW_OFFSET); - // Check whether the icon size matches + // check whether the icon size matches if (modelFileReadHalfword(gcodeFile) != w || modelFileReadHalfword(gcodeFile) != h) return false; - // Move to next line + // move to next line f_lseek(gcodeFile, gcodeFile->fptr + 3); memcpy(buf, (uint8_t *) &w, sizeof(uint16_t)); @@ -568,7 +575,7 @@ bool model_DecodeToFlash_Classic(FIL *gcodeFile, uint32_t addr) } } - // Move to next line + // move to next line f_lseek(gcodeFile, gcodeFile->fptr + 3); } @@ -577,7 +584,7 @@ bool model_DecodeToFlash_Classic(FIL *gcodeFile, uint32_t addr) return true; } -bool model_DecodeToFlash(char *gcode) +bool model_DecodeToFlash(char * gcode) { FIL gcodeFile; @@ -614,6 +621,7 @@ void ICON_PressedDisplay(uint16_t sx, uint16_t sy, uint8_t icon) uint16_t x, y; uint16_t color = 0; BMP_INFO bmpInfo = {.index = icon, .address = 0}; + getBMPsize(&bmpInfo); LCD_SetWindow(sx, sy, sx + bmpInfo.width - 1, sy + bmpInfo.height - 1); diff --git a/TFT/src/User/API/UI/ui_draw.h b/TFT/src/User/API/UI/ui_draw.h index f4ace433c1..02fa75b0cb 100644 --- a/TFT/src/User/API/UI/ui_draw.h +++ b/TFT/src/User/API/UI/ui_draw.h @@ -7,8 +7,8 @@ extern "C" { #include #include -#include "variants.h" -#include "GUI.h" +#include "variants.h" // for PORTRAIT_MODE etc. +#include "GUI.h" // for GUI_POINT etc. #ifdef PORTRAIT_MODE #define SPACE_X ((LCD_WIDTH - ICON_WIDTH * 3) / 3) @@ -29,15 +29,13 @@ extern "C" { // select marlin or bigtree #define SPACE_SELEX ((LCD_WIDTH - ICON_WIDTH * 2) / 4) #define SPACE_SELEY ((LCD_HEIGHT - ICON_HEIGHT) / 2) -#define text_startx (LCD_WIDTH / 2) +#define TEXT_STARTX (LCD_WIDTH / 2) // thumbnail parser options #define PARSER_CLASSIC 0 #define PARSER_RGB565 1 #define PARSER_BASE64PNG 2 -#define COLOR_BYTE_SIZE sizeof(uint16_t) // RGB565 color byte is equal to uint16_t - typedef struct { uint16_t index; @@ -46,17 +44,17 @@ typedef struct uint16_t height; } BMP_INFO; -void lcd_buffer_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint16_t *buf, GUI_RECT *limit); +void lcd_buffer_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint16_t * buf, GUI_RECT * limit); -void getBMPsize(BMP_INFO *bmp); +void getBMPsize(BMP_INFO * bmp); +void IMAGE_ReadDisplay(uint16_t sx, uint16_t sy, uint32_t address); void LOGO_ReadDisplay(void); void ICON_ReadDisplay(uint16_t sx, uint16_t sy, uint8_t icon); -void ICON_ReadBuffer(uint16_t *buf, uint16_t x, uint16_t y, int16_t w, int16_t h, uint16_t icon); +void ICON_ReadBuffer(uint16_t * buf, uint16_t x, uint16_t y, int16_t w, int16_t h, uint16_t icon); uint16_t ICON_ReadPixel(uint32_t address, uint16_t w, uint16_t h, int16_t x, int16_t y); -bool model_DirectDisplay(GUI_POINT pos, char *gcode); -bool model_DecodeToFlash(char *gcode); -void IMAGE_ReadDisplay(uint16_t sx, uint16_t sy, uint32_t address); +bool model_DirectDisplay(GUI_POINT pos, char * gcode); +bool model_DecodeToFlash(char * gcode); void SMALLICON_ReadDisplay(uint16_t sx, uint16_t sy, uint8_t icon); void ICON_PressedDisplay(uint16_t sx, uint16_t sy, uint8_t icon); diff --git a/TFT/src/User/API/Vfs/vfs.c b/TFT/src/User/API/Vfs/vfs.c index 986f5cbd57..621e80d722 100644 --- a/TFT/src/User/API/Vfs/vfs.c +++ b/TFT/src/User/API/Vfs/vfs.c @@ -1,6 +1,9 @@ #include "vfs.h" #include "includes.h" +static bool volumeSrcStatus[FF_VOLUMES] = {false, false}; // volume exist detect +static uint8_t (* volumeInserted[FF_VOLUMES])(void) = {SD_CD_Inserted, USBH_USR_Inserted}; + MYFILE infoFile = {FS_TFT_SD, BOARD_SD, "?:", {0}, {0}, {0}, {0}, 0, 0, 0, 0, false}; void setPrintModelIcon(bool exist) @@ -123,7 +126,7 @@ void resetInfoFile(void) } // skip path information, if any -char * getPathTail(void) +static char * getPathTail(void) { // examples: // @@ -175,7 +178,7 @@ bool isRootFolder(void) } // check if filename provides a supported filename extension -char * isSupportedFile(const char * filename) +static char * isSupportedFile(const char * filename) { char * extPos = strrchr(filename, '.'); // check last "." in the name where extension is supposed to start @@ -275,7 +278,7 @@ const char * getFilename(uint8_t index) return infoFile.file[index]; } -const char * hideExtension(const char * filename) +static const char * hideExtension(const char * filename) { // if filename extension feature is disabled and extension is not already hidden if (infoSettings.filename_extension == 0 && strchr(filename, '\0')[1] == '\0') @@ -290,7 +293,7 @@ const char * hideExtension(const char * filename) return filename; } -const char * restoreExtension(const char * filename) +static const char * restoreExtension(const char * filename) { if (infoSettings.filename_extension == 0) // if filename extension feature is disabled { @@ -351,9 +354,6 @@ bool getPrintTitle(char * buf, uint8_t len) return true; } -// volume exist detect -static bool volumeSrcStatus[FF_VOLUMES] = {false, false}; - bool volumeExists(uint8_t src) { if (src >= FF_VOLUMES) @@ -362,8 +362,6 @@ bool volumeExists(uint8_t src) return volumeSrcStatus[src]; } -uint8_t (* volumeInserted[FF_VOLUMES])(void) = {SD_CD_Inserted, USBH_USR_Inserted}; - void loopVolumeSource(void) { for (uint8_t i = 0; i < FF_VOLUMES; i++) diff --git a/TFT/src/User/API/boot.c b/TFT/src/User/API/boot.c index b13654ab9c..cffb3e8fd6 100644 --- a/TFT/src/User/API/boot.c +++ b/TFT/src/User/API/boot.c @@ -13,19 +13,19 @@ typedef enum BMP_INVALIDFILE } BMPUPDATE_STAT; -const GUI_RECT labelUpdateRect = {0, BYTE_HEIGHT + PADDING, LCD_WIDTH, (BYTE_HEIGHT*2) + PADDING}; -const GUI_RECT iconUpdateRect = {0, (BYTE_HEIGHT*2) + PADDING*2, LCD_WIDTH, (BYTE_HEIGHT*2) + PADDING*3 + ICON_HEIGHT}; -const GUI_RECT statUpdateRect = {0, (BYTE_HEIGHT*3) + PADDING*3 + ICON_HEIGHT, LCD_WIDTH, (BYTE_HEIGHT*4) + PADDING*3 + ICON_HEIGHT}; -const GUI_RECT labelFailedRect = {0, (BYTE_HEIGHT*4) + PADDING*4 + ICON_HEIGHT, LCD_WIDTH, (BYTE_HEIGHT*5) + PADDING*4 + ICON_HEIGHT}; +static const GUI_RECT labelUpdateRect = {0, BYTE_HEIGHT + PADDING, LCD_WIDTH, (BYTE_HEIGHT*2) + PADDING}; +static const GUI_RECT iconUpdateRect = {0, (BYTE_HEIGHT*2) + PADDING*2, LCD_WIDTH, (BYTE_HEIGHT*2) + PADDING*3 + ICON_HEIGHT}; +static const GUI_RECT statUpdateRect = {0, (BYTE_HEIGHT*3) + PADDING*3 + ICON_HEIGHT, LCD_WIDTH, (BYTE_HEIGHT*4) + PADDING*3 + ICON_HEIGHT}; +static const GUI_RECT labelFailedRect = {0, (BYTE_HEIGHT*4) + PADDING*4 + ICON_HEIGHT, LCD_WIDTH, (BYTE_HEIGHT*5) + PADDING*4 + ICON_HEIGHT}; -const uint32_t fontAddrList[] = { +static const uint32_t fontAddrList[] = { BYTE_ASCII_ADDR, WORD_UNICODE_ADDR, LARGE_FONT_ADDR, _8X16_FONT_ADDR }; -const char * fontPathList[] = { +static const char * fontPathList[] = { FONT_UPDATE_DIR "/" FILE_ASCII_FONT, FONT_UPDATE_DIR "/" FILE_UNICODE_FONT, FONT_UPDATE_DIR "/" FILE_LARGE_ASCII_FONT, @@ -33,15 +33,15 @@ const char * fontPathList[] = { }; // this list is Auto-Generated. Please add new icons in icon_list.inc only -const char * const iconBmpName[] = { +static const char * const iconBmpName[] = { #define X_ICON(NAME) #NAME , #include "icon_list.inc" #undef X_ICON }; -GUI_POINT bmpSize; +static GUI_POINT bmpSize; -BMPUPDATE_STAT bmpDecode(char * bmp, uint32_t addr) +static BMPUPDATE_STAT bmpDecode(char * bmp, uint32_t addr) { FIL bmpFile; char magic[2]; @@ -135,7 +135,7 @@ BMPUPDATE_STAT bmpDecode(char * bmp, uint32_t addr) return BMP_SUCCESS; } -void dispIconFail(uint8_t * lbl, BMPUPDATE_STAT bmpState) +static void dispIconFail(uint8_t * lbl, BMPUPDATE_STAT bmpState) { char * statTxt; char errorTxt[30]; diff --git a/TFT/src/User/API/comment.c b/TFT/src/User/API/comment.c index cd5d34ad82..5aa84b2609 100644 --- a/TFT/src/User/API/comment.c +++ b/TFT/src/User/API/comment.c @@ -4,8 +4,9 @@ #define TOKEN_DELIMITERS " :=_" // possible delimiters found in the comments of the G-code file #define KEYWORD_INTIALS "lLrRtT" // the initials of the searched keywords, add new initials for new keywords +static bool slicerTimePresence = false; + char gCodeCommentLine[COMMENT_MAX_CHAR] = {0}; -bool slicerTimePresence = false; void setTimeFromSlicer(bool present) { diff --git a/TFT/src/User/API/config.c b/TFT/src/User/API/config.c index 7d5795f27b..7c7873494b 100644 --- a/TFT/src/User/API/config.c +++ b/TFT/src/User/API/config.c @@ -173,7 +173,7 @@ enum }; // this list is Auto-Generated. Please add new config in config.inc only -const char * const config_keywords[CONFIG_COUNT] = { +static const char * const config_keywords[CONFIG_COUNT] = { #define X_CONFIG(NAME) CONFIG_##NAME , #include "config.inc" #undef X_CONFIG @@ -206,34 +206,33 @@ typedef enum CSTAT_SPI_WRITE_FAIL, } CONFIG_STATS; -const GUI_POINT pointConfigTitle = {2, 2}; -const GUI_RECT rectTitleline = {0, BYTE_HEIGHT+4, LCD_WIDTH, BYTE_HEIGHT+6}; -const GUI_RECT recterror = {BYTE_WIDTH/2-2, BYTE_HEIGHT*2+2, LCD_WIDTH-BYTE_WIDTH/2+2, LCD_HEIGHT-(BYTE_HEIGHT*4)-4}; -const GUI_RECT recterrortxt = {BYTE_WIDTH/2, BYTE_HEIGHT*2+4, LCD_WIDTH-BYTE_WIDTH/2, LCD_HEIGHT-(BYTE_HEIGHT*4)-6}; -const GUI_RECT rectProgressframe = {BYTE_WIDTH/2-2, LCD_HEIGHT-(BYTE_HEIGHT*2+BYTE_HEIGHT/2), LCD_WIDTH-BYTE_WIDTH/2+2,LCD_HEIGHT-BYTE_HEIGHT/2}; -const GUI_POINT pointProgressText = {BYTE_WIDTH/2-2, LCD_HEIGHT-(BYTE_HEIGHT*4)}; - -const char * const preheatNames[] = PREHEAT_LABELS; -const uint16_t preheatHotend[] = PREHEAT_HOTEND; -const uint16_t preheatBed[] = PREHEAT_BED; -const uint8_t cgEnabled[] = CUSTOM_GCODE_ENABLED; -const char * const cgNames[] = CUSTOM_GCODE_LABELS; -const char * const cgList[] = CUSTOM_GCODE_LIST; - -CONFIGFILE* CurConfigFile; -STRINGS_STORE* configStringsStore = NULL; -PREHEAT_STORE* configPreheatStore = NULL; -PRINT_GCODES* configPrintGcodes = NULL; -CUSTOM_GCODES* configCustomGcodes = NULL; - -char * cur_line = NULL; -uint16_t c_index = 0; -uint16_t foundkeys = 0; -uint8_t customcode_index = 0; -uint8_t customcode_good[CUSTOM_GCODES_COUNT]; -bool scheduleRotate = false; - -void drawProgressPage(uint8_t * title) +static const GUI_RECT rectTitleline = {0, BYTE_HEIGHT+4, LCD_WIDTH, BYTE_HEIGHT+6}; +static const GUI_RECT recterror = {BYTE_WIDTH/2-2, BYTE_HEIGHT*2+2, LCD_WIDTH-BYTE_WIDTH/2+2, LCD_HEIGHT-(BYTE_HEIGHT*4)-4}; +static const GUI_RECT recterrortxt = {BYTE_WIDTH/2, BYTE_HEIGHT*2+4, LCD_WIDTH-BYTE_WIDTH/2, LCD_HEIGHT-(BYTE_HEIGHT*4)-6}; +static const GUI_RECT rectProgressframe = {BYTE_WIDTH/2-2, LCD_HEIGHT-(BYTE_HEIGHT*2+BYTE_HEIGHT/2), LCD_WIDTH-BYTE_WIDTH/2+2,LCD_HEIGHT-BYTE_HEIGHT/2}; +static const GUI_POINT pointProgressText = {BYTE_WIDTH/2-2, LCD_HEIGHT-(BYTE_HEIGHT*4)}; + +static const char * const preheatNames[] = PREHEAT_LABELS; +static const uint16_t preheatHotend[] = PREHEAT_HOTEND; +static const uint16_t preheatBed[] = PREHEAT_BED; +static const uint8_t cgEnabled[] = CUSTOM_GCODE_ENABLED; +static const char * const cgNames[] = CUSTOM_GCODE_LABELS; +static const char * const cgList[] = CUSTOM_GCODE_LIST; + +static CONFIGFILE * CurConfigFile; +static STRINGS_STORE * configStringsStore = NULL; +static PREHEAT_STORE * configPreheatStore = NULL; +static PRINT_GCODES * configPrintGcodes = NULL; +static CUSTOM_GCODES * configCustomGcodes = NULL; + +static char * cur_line = NULL; +static uint16_t c_index = 0; +static uint16_t foundkeys = 0; +static uint8_t customcode_index = 0; +static uint8_t customcode_good[CUSTOM_GCODES_COUNT]; +static bool scheduleRotate = false; + +static void drawProgressPage(uint8_t * title) { GUI_Clear(BLACK); GUI_DispString(2, 2, title); @@ -241,19 +240,22 @@ void drawProgressPage(uint8_t * title) GUI_DrawPrect(&rectProgressframe); } -void drawProgress(void) +static void drawProgress(void) { char tempstr[50]; + sprintf(tempstr, "Total keywords found: %d", foundkeys); GUI_DispString(pointProgressText.x, pointProgressText.y, (uint8_t *)tempstr); + uint16_t p = map(CurConfigFile->cur, 0, CurConfigFile->size, rectProgressframe.x0, rectProgressframe.x1); + GUI_FillRect(rectProgressframe.x0, rectProgressframe.y0, p, rectProgressframe.y1); } -void showError(CONFIG_STATS stat) +static void showError(CONFIG_STATS stat) { - char *ttl; - char *txt; + char * ttl; + char * txt; char tempstr[50]; GUI_SetBkColor(BLACK); @@ -291,16 +293,18 @@ void showError(CONFIG_STATS stat) txt = "Invalid config File"; break; } - GUI_DispString(recterrortxt.x0, recterrortxt.y0, (uint8_t*)ttl); - GUI_DispStringInRect(recterrortxt.x0, recterrortxt.y0 + (BYTE_HEIGHT * 2), recterrortxt.x1, recterrortxt.y1, (uint8_t*)txt); + + GUI_DispString(recterrortxt.x0, recterrortxt.y0, (uint8_t *)ttl); + GUI_DispStringInRect(recterrortxt.x0, recterrortxt.y0 + (BYTE_HEIGHT * 2), recterrortxt.x1, recterrortxt.y1, (uint8_t *)txt); GUI_SetColor(WHITE); Delay_ms(5000); } // check if config keyword exits in the buffer line -bool key_seen(const char * keyStr) +static bool key_seen(const char * keyStr) { uint16_t i; + for (c_index = 0, i = 0; c_index < LINE_MAX_CHAR && cur_line[c_index] != '\0'; c_index++, i = 0) { while (cur_line[c_index + i] == keyStr[i]) @@ -308,10 +312,12 @@ bool key_seen(const char * keyStr) if (keyStr[++i] == '\0') { c_index += i; + return true; } } } + return false; } @@ -319,24 +325,28 @@ bool key_seen(const char * keyStr) static inline bool param_seen(const char * keyStr) { bool found = key_seen(keyStr); + if (found) { size_t index = c_index - strlen(keyStr); + if (index > 0) { if (cur_line[index - 1] != ' ') // if keyStr is not fully matching (it is a substring of another key)) return false; } } + return found; } // check if the value is within min and max limits -bool inLimit(int val, int min, int max) +static bool inLimit(int val, int min, int max) { if (val < min || val > max) { showError(CSTAT_INVALID_VALUE); + return false; } else @@ -345,13 +355,13 @@ bool inLimit(int val, int min, int max) } } -// Get the int after config keyword. +// get the int after config keyword static inline int config_int(void) { return (strtol(&cur_line[c_index], NULL, 10)); } -// Get valid int value or old value +// get valid int value or old value static inline int valid_intValue(int min, int max, int defaultVal) { if (inLimit(config_int(), min, max)) @@ -360,8 +370,8 @@ static inline int valid_intValue(int min, int max, int defaultVal) return defaultVal; } -// Treat all values other than 0 as 1. -int8_t getOnOff(void) +// treat all values other than 0 as 1 +static int8_t getOnOff(void) { if (config_int() == 0) return 0; @@ -369,13 +379,13 @@ int8_t getOnOff(void) return 1; } -// Get the float after config keyword. +// get the float after config keyword static inline float config_float(void) { return (strtod(&cur_line[c_index], NULL)); } -// Get valid float value or old value +// get valid float value or old value static inline float valid_floatValue(float min, float max, float defaultVal) { if (inLimit(config_float(), min, max)) @@ -390,7 +400,7 @@ static inline bool config_is_hex(void) return (strstr(&cur_line[c_index], "0x") != NULL); } -// Get the hex after config keyword. +// get the hex after config keyword static inline uint32_t config_hex(void) { return (strtol(&cur_line[c_index], NULL, 16)); @@ -402,10 +412,11 @@ static inline uint16_t RGB888_to_RGB565(uint32_t rgb888) uint8_t r = ((rgb888 >> 16) & 0xFF) >> 3; // R5 uint8_t g = ((rgb888 >> 8) & 0xFF) >> 2; // G6 uint8_t b = (rgb888 & 0xFF) >> 3; // B5 + return ((r << 11) | (g << 5) | b); } -static inline void config_set_color(uint16_t *color_src) +static inline void config_set_color(uint16_t * color_src) { if (config_is_hex()) { @@ -418,7 +429,7 @@ static inline void config_set_color(uint16_t *color_src) } // parse the keyword values in the buffer -void parseConfigKey(uint16_t index) +static void parseConfigKey(uint16_t index) { switch (index) { @@ -581,8 +592,9 @@ void parseConfigKey(uint16_t index) case C_INDEX_MARLIN_TITLE: { char * pchr = strchr(cur_line, ':') + 1; - int utf8len = getUTF8Length((uint8_t*)pchr); + int utf8len = getUTF8Length((uint8_t *)pchr); int bytelen = strlen(pchr) + 1; + if (inLimit(utf8len, NAME_MIN_LENGTH, MAX_STRING_LENGTH) && inLimit(bytelen, NAME_MIN_LENGTH, MAX_GCODE_LENGTH)) strcpy(configStringsStore->marlin_title, pchr); break; @@ -766,9 +778,12 @@ void parseConfigKey(uint16_t index) case C_INDEX_PREHEAT_NAME_6: { char pchr[LINE_MAX_CHAR]; + strcpy(pchr, strchr(cur_line, ':') + 1); + int utf8len = getUTF8Length((uint8_t *)pchr); int bytelen = strlen(pchr) + 1; + if (inLimit(utf8len, NAME_MIN_LENGTH, MAX_STRING_LENGTH) && inLimit(bytelen, NAME_MIN_LENGTH, MAX_STRING_LENGTH)) strcpy(configPreheatStore->preheat_name[index - C_INDEX_PREHEAT_NAME_1], pchr); break; @@ -782,6 +797,7 @@ void parseConfigKey(uint16_t index) case C_INDEX_PREHEAT_TEMP_6: { int val_index = index - C_INDEX_PREHEAT_TEMP_1; + if (key_seen("T")) SET_VALID_INT_VALUE(configPreheatStore->preheat_hotend[val_index], MIN_TOOL_TEMP, MAX_TOOL_TEMP); if (key_seen("B")) SET_VALID_INT_VALUE(configPreheatStore->preheat_bed[val_index], MIN_BED_TEMP, MAX_BED_TEMP); break; @@ -862,8 +878,9 @@ void parseConfigKey(uint16_t index) #ifdef LCD_LED_PWM_CHANNEL case C_INDEX_LCD_BRIGHTNESS: SET_VALID_INT_VALUE(infoSettings.lcd_brightness, 0, LCD_BRIGHTNESS_COUNT - 1); - if (infoSettings.lcd_brightness == 0) - infoSettings.lcd_brightness = 1; // If someone set it to 0 set it to 1 + + if (infoSettings.lcd_brightness == 0) // if someone set it to 0 set it to 1 + infoSettings.lcd_brightness = 1; break; case C_INDEX_LCD_IDLE_BRIGHTNESS: @@ -928,9 +945,12 @@ void parseConfigKey(uint16_t index) case C_INDEX_CUSTOM_LABEL_15: { char pchr[LINE_MAX_CHAR]; + strcpy(pchr, strchr(cur_line, ':') + 1); - int utf8len = getUTF8Length((uint8_t*)pchr); + + int utf8len = getUTF8Length((uint8_t *)pchr); int bytelen = strlen(pchr) + 1; + if (inLimit(utf8len, NAME_MIN_LENGTH, MAX_GCODE_NAME_LENGTH) && inLimit(bytelen, NAME_MIN_LENGTH, MAX_GCODE_LENGTH)) { strcpy(configCustomGcodes->name[customcode_index++], pchr); @@ -960,8 +980,11 @@ void parseConfigKey(uint16_t index) { int lineIndex = index - C_INDEX_CUSTOM_GCODE_1; // actual gcode index in config file char pchr[LINE_MAX_CHAR]; + strcpy(pchr, strchr(cur_line, ':') + 1); + int len = strlen(pchr) + 1; + // check if gcode length is ok and the name was ok if (inLimit(len, GCODE_MIN_LENGTH, MAX_GCODE_LENGTH) && (customcode_good[lineIndex] == 1)) strcpy(configCustomGcodes->gcode[customcode_index - 1], pchr); @@ -982,6 +1005,7 @@ void parseConfigKey(uint16_t index) { char * pchr = strchr(cur_line, ':') + 1; int len = strlen(pchr); + if (inLimit(len, GCODE_MIN_LENGTH, MAX_GCODE_LENGTH)) { strcpy(configPrintGcodes->start_gcode, pchr); @@ -999,6 +1023,7 @@ void parseConfigKey(uint16_t index) { char * pchr = strchr(cur_line, ':') + 1; int len = strlen(pchr); + if (inLimit(len, GCODE_MIN_LENGTH, MAX_GCODE_LENGTH)) { strcpy(configPrintGcodes->end_gcode, pchr); @@ -1016,6 +1041,7 @@ void parseConfigKey(uint16_t index) { char * pchr = strchr(cur_line, ':') + 1; int len = strlen(pchr); + if (inLimit(len, GCODE_MIN_LENGTH, MAX_GCODE_LENGTH)) { strcpy(configPrintGcodes->cancel_gcode, pchr); @@ -1035,7 +1061,7 @@ void parseConfigKey(uint16_t index) } // check keywords in the config line in buffer -void parseConfigLine(void) +static void parseConfigLine(void) { for (uint16_t i = 0; i < CONFIG_COUNT; i++) { @@ -1043,16 +1069,19 @@ void parseConfigLine(void) { PRINTDEBUG("\n"); PRINTDEBUG((char *)config_keywords[i]); + parseConfigKey(i); foundkeys++; + return; } } + showError(CSTAT_UNKNOWN_KEYWORD); } // parse keywords from line read from language file -void parseLangLine(void) +static void parseLangLine(void) { for (int i = 0; i < LABEL_NUM; i++) { @@ -1060,15 +1089,18 @@ void parseLangLine(void) { PRINTDEBUG("\n"); PRINTDEBUG((char *)lang_key_list[i]); + uint32_t key_addr = LANGUAGE_ADDR + (MAX_LANG_LABEL_LENGTH * i); uint8_t * pchr = (uint8_t *)strchr(cur_line, ':') + 1; int bytelen = strlen((char *)pchr); if (inLimit(bytelen, 1, MAX_LANG_LABEL_LENGTH)) { - W25Qxx_WritePage(pchr, key_addr, MAX_LANG_LABEL_LENGTH); char check[MAX_LANG_LABEL_LENGTH]; + + W25Qxx_WritePage(pchr, key_addr, MAX_LANG_LABEL_LENGTH); W25Qxx_ReadBuffer((uint8_t *)&check, key_addr, MAX_LANG_LABEL_LENGTH); + if (strcmp(strchr(cur_line, ':') + 1, check) != 0) showError(CSTAT_SPI_WRITE_FAIL); } @@ -1076,14 +1108,17 @@ void parseLangLine(void) { showError(CSTAT_INVALID_VALUE); } + foundkeys++; + return; } } + showError(CSTAT_UNKNOWN_KEYWORD); } -bool readConfigFile(const char * path, void (* lineParser)(), uint16_t maxLineLen) +static bool readConfigFile(const char * path, void (* lineParser)(), uint16_t maxLineLen) { bool comment_mode = false; bool comment_space = true; @@ -1100,34 +1135,43 @@ bool readConfigFile(const char * path, void (* lineParser)(), uint16_t maxLineLe if (f_open(&configFile.file, path, FA_OPEN_EXISTING | FA_READ) != FR_OK) { PRINTDEBUG("parse error\n"); + showError(CSTAT_FILE_NOTOPEN); + return false; } else { configFile.size = f_size(&configFile.file); + if (configFile.size == 0) { showError(CSTAT_FILE_INVALID); f_close(&configFile.file); + return false; } configFile.cur = 0; + for (; configFile.cur < configFile.size;) { if (f_read(&configFile.file, &cur_char, 1, &br) != FR_OK) { PRINTDEBUG("read error\n"); + return false; } + configFile.cur++; + //PRINTDEBUG("Line ++\n"); - if (cur_char == '\n') // start parsing line after new line. + if (cur_char == '\n') // start parsing line after new line { comment_mode = false; // for new command comment_space = true; + if (count != 0) { cur_line[count] = '\0'; // terminate string @@ -1136,17 +1180,22 @@ bool readConfigFile(const char * path, void (* lineParser)(), uint16_t maxLineLe PRINTDEBUG("\n"); PRINTDEBUG(cur_line); + count = 0; // clear buffer } } else if (count < maxLineLen - 2) { if (cur_char == '#') + { comment_mode = true; + } else { if (comment_space && cur_char != ' ') // ignore ' ' space bytes + { comment_space = false; + } if (!comment_mode && !comment_space && cur_char != '\r') // normal code { if (cur_char == 'n' && last_char == '\\') // replace "\n" with new line char('\n') @@ -1154,36 +1203,44 @@ bool readConfigFile(const char * path, void (* lineParser)(), uint16_t maxLineLe cur_char = '\n'; count--; } + cur_line[count++] = cur_char; last_char = cur_char; if (configFile.cur == configFile.size) { cur_line[count] = '\0'; // terminate string + PRINTDEBUG("line read\n"); - lineParser(); // start parsing at the end of the file. + + lineParser(); // start parsing at the end of the file drawProgress(); } } } } } + f_close(&configFile.file); configFile.cur = 0; configFile.size = 0; + return true; } } -void writeConfig(uint8_t * dataBytes, uint16_t numBytes, uint32_t addr, uint32_t maxSize) +static void writeConfig(uint8_t * dataBytes, uint16_t numBytes, uint32_t addr, uint32_t maxSize) { - // do not proceed if data size is larger than reserved max size. + // do not proceed if data size is larger than reserved max size if (numBytes > maxSize) { PRINTDEBUG("\nwrite error\n"); + showError(CSTAT_STORAGE_LOW); + return; } + int sectorCount = maxSize / W25QXX_SECTOR_SIZE; // erase part of flash to be rewritten @@ -1191,12 +1248,13 @@ void writeConfig(uint8_t * dataBytes, uint16_t numBytes, uint32_t addr, uint32_t { W25Qxx_EraseSector(addr + (i * W25QXX_SECTOR_SIZE)); } - Delay_ms(100); // give time for spi flash to settle + + Delay_ms(100); // give time for spi flash to settle W25Qxx_WriteBuffer(dataBytes, addr, numBytes); // write data to spi flash - Delay_ms(100); // give time for spi flash to settle + Delay_ms(100); // give time for spi flash to settle } -void saveConfig(void) +static void saveConfig(void) { writeConfig((uint8_t *)configStringsStore, sizeof(STRINGS_STORE), STRINGS_STORE_ADDR, STRINGS_STORE_MAX_SIZE); writeConfig((uint8_t *)configPreheatStore, sizeof(PREHEAT_STORE), PREHEAT_STORE_ADDR, PREHEAT_STORE_MAX_SIZE); @@ -1208,13 +1266,14 @@ void saveConfig(void) uint8_t * data_r = (uint8_t *)&tempgcode; W25Qxx_ReadBuffer(data_r, CUSTOM_GCODE_ADDR, sizeof(CUSTOM_GCODES)); + PRINTDEBUG("\nread done"); PRINTDEBUG("\nread from flash:"); PRINTDEBUG(tempgcode.gcode[1]); #endif } -// Reset & store config settings +// reset & store config settings void resetConfig(void) { STRINGS_STORE tempST; @@ -1247,6 +1306,7 @@ void resetConfig(void) { strcpy(tempCG.name[n], cgNames[i]); strcpy(tempCG.gcode[n], cgList[i]); + n++; } } @@ -1265,6 +1325,7 @@ bool getConfigFromFile(char * configPath) if (f_file_exists(configPath) == false) { PRINTDEBUG("configFile not found\n"); + return false; } @@ -1278,10 +1339,10 @@ bool getConfigFromFile(char * configPath) PRINT_GCODES tempPrintCodes; CUSTOM_GCODES tempCustomGcodes; - W25Qxx_ReadBuffer((uint8_t*)&tempStringStore, STRINGS_STORE_ADDR, sizeof(STRINGS_STORE)); - W25Qxx_ReadBuffer((uint8_t*)&tempPreheatStore, PREHEAT_STORE_ADDR, sizeof(PREHEAT_STORE)); - W25Qxx_ReadBuffer((uint8_t*)&tempPrintCodes, PRINT_GCODES_ADDR, sizeof(PRINT_GCODES)); - W25Qxx_ReadBuffer((uint8_t*)&tempCustomGcodes, CUSTOM_GCODE_ADDR, sizeof(CUSTOM_GCODES)); + W25Qxx_ReadBuffer((uint8_t *)&tempStringStore, STRINGS_STORE_ADDR, sizeof(STRINGS_STORE)); + W25Qxx_ReadBuffer((uint8_t *)&tempPreheatStore, PREHEAT_STORE_ADDR, sizeof(PREHEAT_STORE)); + W25Qxx_ReadBuffer((uint8_t *)&tempPrintCodes, PRINT_GCODES_ADDR, sizeof(PRINT_GCODES)); + W25Qxx_ReadBuffer((uint8_t *)&tempCustomGcodes, CUSTOM_GCODE_ADDR, sizeof(CUSTOM_GCODES)); configStringsStore = &tempStringStore; configPreheatStore = &tempPreheatStore; @@ -1291,9 +1352,10 @@ bool getConfigFromFile(char * configPath) foundkeys = 0; char cur_line_buffer[LINE_MAX_CHAR]; + cur_line = cur_line_buffer; - drawProgressPage((uint8_t*)"Updating Configuration..."); + drawProgressPage((uint8_t *)"Updating Configuration..."); if (readConfigFile(configPath, parseConfigLine, LINE_MAX_CHAR)) { @@ -1313,11 +1375,13 @@ bool getConfigFromFile(char * configPath) saveConfig(); PRINTDEBUG("config saved\n"); + return true; } else { PRINTDEBUG("configFile save failed\n"); + return false; } } @@ -1336,15 +1400,17 @@ bool getLangFromFile(char * rootDir) return false; char langpath[256]; + sprintf(langpath, "%s/%s", rootDir, f.fname); if (!f_file_exists(langpath)) return false; char cur_line_buffer[MAX_LANG_LABEL_LENGTH + 100]; + cur_line = cur_line_buffer; - drawProgressPage((uint8_t*)f.fname); + drawProgressPage((uint8_t *)f.fname); // erase part of flash to be rewritten for (int i = 0; i < (LANGUAGE_SIZE / W25QXX_SECTOR_SIZE); i++) @@ -1357,6 +1423,7 @@ bool getLangFromFile(char * rootDir) if (foundkeys != LABEL_NUM) { showError(CSTAT_FILE_INVALID); + success = false; } else // rename file if update was successful @@ -1364,6 +1431,7 @@ bool getLangFromFile(char * rootDir) if (!f_file_exists(FILE_ADMIN_MODE) && f_file_exists(langpath)) // language exists { char newlangpath[256]; + sprintf(newlangpath, "%s/%s.CUR", rootDir, f.fname); if (f_file_exists(newlangpath)) // old language also exists diff --git a/TFT/src/User/API/config.h b/TFT/src/User/API/config.h index 88fa2d72af..62fa0f225b 100644 --- a/TFT/src/User/API/config.h +++ b/TFT/src/User/API/config.h @@ -13,7 +13,7 @@ extern "C" { //-----------------------------Limits #define MAX_TX_SLOTS 16 // tx slots over this will not be parsed #define MAX_SIZE_LIMIT 2000 // machine size over this will not be parsed -#define MAX_EXT_SPEED_LIMIT 5000 // Extruder speed over this will not be parsed +#define MAX_EXT_SPEED_LIMIT 5000 // extruder speed over this will not be parsed #define MAX_TOOL_TEMP 1000 // extruder temp over this will not be parsed #define MAX_BED_TEMP 400 // bed temp over this will not be parsed #define MAX_CHAMBER_TEMP 200 // bed temp over this will not be parsed @@ -30,9 +30,9 @@ extern "C" { #define MIN_SIZE_LIMIT -2000 // machine size less than this will not be parsed #define NAME_MIN_LENGTH 3 // minimum name length #define GCODE_MIN_LENGTH 3 // gcode length less than this will not be parsed -#define MIN_XY_POS_LIMIT -2000 // Set a negative minimum position for Delta printers +#define MIN_XY_POS_LIMIT -2000 // set a negative minimum position for Delta printers #define MIN_Z_POS_LIMIT 0 -#define MIN_PROBING_Z_RAISE -2000 // Set a negative minimum position for Delta printers +#define MIN_PROBING_Z_RAISE -2000 // set a negative minimum position for Delta printers #define MIN_TOOL_TEMP 20 // extruder temp less than this will not be parsed #define MIN_BED_TEMP 20 // bed temp less than this will not be parsed #define MIN_CHAMBER_TEMP 20 // chamber temp less than this will not be parsed diff --git a/TFT/src/User/API/coordinate.c b/TFT/src/User/API/coordinate.c index 1ad3f37d0a..8b71fb25e6 100644 --- a/TFT/src/User/API/coordinate.c +++ b/TFT/src/User/API/coordinate.c @@ -1,15 +1,15 @@ #include "coordinate.h" #include "includes.h" -const char axis_id[TOTAL_AXIS] = {'X', 'Y', 'Z', 'E'}; - static COORDINATE targetPosition = {{0.0f, 0.0f, 0.0f, 0.0f}, 3000}; -static COORDINATE curPosition = {{0.0f, 0.0f, 0.0f, 0.0f}, 3000}; -E_AXIS_BACKUP eAxisBackup = {0, 0, false, false}; +static COORDINATE curPosition = {{0.0f, 0.0f, 0.0f, 0.0f}, 3000}; + +const char axis_id[TOTAL_AXIS] = {'X', 'Y', 'Z', 'E'}; +E_AXIS_BACKUP eAxisBackup = {0, 0, false, false}; /** * Obtained from "M114 E" instead of "M114", Because the coordinates of "M114" are not real-time coordinates. - * It may be replaced by "M114 R". + * It may be replaced by "M114 R" */ static float extruderPostion = 0.0f; @@ -79,7 +79,7 @@ void coordinateSetFeedRate(uint32_t feedrate) targetPosition.feedrate = feedrate; } -void coordinateGetAll(COORDINATE *tmp) +void coordinateGetAll(COORDINATE * tmp) { memcpy(tmp, &targetPosition, sizeof(targetPosition)); } @@ -104,7 +104,7 @@ void coordinateSetAxisActual(AXIS axis, float position) curPosition.axis[axis] = position; } -void coordinateGetAllActual(COORDINATE *tmp) +void coordinateGetAllActual(COORDINATE * tmp) { memcpy(tmp, &curPosition, sizeof(curPosition)); } diff --git a/TFT/src/User/API/coordinate.h b/TFT/src/User/API/coordinate.h index 0e9c5d01ec..83bef84942 100644 --- a/TFT/src/User/API/coordinate.h +++ b/TFT/src/User/API/coordinate.h @@ -44,12 +44,12 @@ float coordinateGetAxisTarget(AXIS axis); void coordinateSetAxisTarget(AXIS axis, float position); uint32_t coordinateGetFeedRate(void); void coordinateSetFeedRate(uint32_t feedrate); -void coordinateGetAll(COORDINATE *tmp); +void coordinateGetAll(COORDINATE * tmp); float coordinateGetExtruderActual(void); void coordinateSetExtruderActualSteps(float steps); float coordinateGetAxisActual(AXIS axis); void coordinateSetAxisActual(AXIS axis, float position); -void coordinateGetAllActual(COORDINATE *tmp); +void coordinateGetAllActual(COORDINATE * tmp); float coordinateGetAxis(AXIS axis); void coordinateQueryClearSendingWaiting(void); // called in sendQueueCmd(). Clear sending waiting for coordinate query diff --git a/TFT/src/User/API/debug.h b/TFT/src/User/API/debug.h index 99ba5ff358..5fca3e511b 100644 --- a/TFT/src/User/API/debug.h +++ b/TFT/src/User/API/debug.h @@ -2,7 +2,7 @@ #define _DEBUG_H_ #if defined(SERIAL_DEBUG_ENABLED) && defined(SERIAL_DEBUG_PORT) - // Redirect dbg_printf to TinyPrintf (printf_) which itself write to Serial_PutChar(SERIAL_DEBUG_PORT, ...) + // redirect dbg_printf to TinyPrintf (printf_) which itself write to Serial_PutChar(SERIAL_DEBUG_PORT, ...) #define dbg_printf(...) printf_(__VA_ARGS__) #define dbg_print(str) Serial_Put(SERIAL_DEBUG_PORT, str) #else diff --git a/TFT/src/User/API/menu.c b/TFT/src/User/API/menu.c index 892cbc1251..608084a161 100644 --- a/TFT/src/User/API/menu.c +++ b/TFT/src/User/API/menu.c @@ -15,8 +15,8 @@ const GUI_RECT exhibitRect = { #endif }; -// MENU FOR A TEMPERATURE DISPLAY SCREEN (TOOL TEMP, PID, ETC...) -const GUI_RECT rect_of_keysIN[TM_RECT_COUNT] = { +// MENU FOR A TEMPERATURE DISPLAY SCREEN (TOOL TEMP, PID, ETC.) +static const GUI_RECT rect_of_keysIN[TM_RECT_COUNT] = { #ifdef PORTRAIT_MODE // 8 icons area VERTICAL GUI {0*ICON_WIDTH+0*SPACE_X+START_X, 1*ICON_HEIGHT+1*SPACE_Y+ICON_START_Y, 1*ICON_WIDTH+0*SPACE_X+START_X, 2*ICON_HEIGHT+1*SPACE_Y+ICON_START_Y}, // decrease @@ -268,7 +268,7 @@ const GUI_RECT rect_of_keyPS[] = { // PS = PRINT SCREEN, This template is used t }; #ifdef PORTRAIT_MODE -const GUI_RECT rect_of_keyPS_draw[] = { // VERTICAL GUI Printing menu for drawing UI +static const GUI_RECT rect_of_keyPS_draw[] = { // VERTICAL GUI Printing menu for drawing UI {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, @@ -367,7 +367,7 @@ const GUI_RECT rect_of_keyPS_end[] = { #endif }; -const GUI_RECT rect_of_keyListView[ITEM_PER_PAGE + 1] = { +static const GUI_RECT rect_of_keyListView[ITEM_PER_PAGE + 1] = { #ifdef PORTRAIT_MODE // list area VERTICAL GUI {START_X, 0*LISTITEM_HEIGHT+ICON_START_Y, LISTITEM_WIDTH+START_X, 1*LISTITEM_HEIGHT+ICON_START_Y}, @@ -402,8 +402,8 @@ const GUI_RECT rect_of_titleBar[] = { {0, 0, LCD_WIDTH, ICON_START_Y} }; -// Clean up the gaps outside icons -void menuClearGaps(void) +// clean up the gaps outside icons +static void menuClearGaps(void) { #ifdef TFT70_V3_0 const GUI_RECT gapsMenu[] = { @@ -507,6 +507,7 @@ void menuClearGaps(void) { GUI_ClearPrect(gapsSS); GUI_SetBkColor(infoSettings.bg_color); + for (uint8_t i = 1; i < COUNT(gapsSS); i++) GUI_ClearPrect(gapsSS + i); } @@ -514,6 +515,7 @@ void menuClearGaps(void) { GUI_ClearPrect(gaps); GUI_SetBkColor(infoSettings.bg_color); + for (uint8_t i = 1; i < COUNT(gaps); i++) GUI_ClearPrect(gaps + i); } @@ -531,40 +533,45 @@ void menuClearGaps(void) GUI_SetBkColor(infoSettings.title_bg_color); GUI_ClearPrect(gaps); GUI_SetBkColor(infoSettings.bg_color); + for (uint8_t i = 1; i < COUNT(gaps); i++) GUI_ClearPrect(gaps + i); #endif } -static const MENUITEMS *curMenuItems = NULL; // current menu -static const LISTITEMS *curListItems = NULL; // current listmenu +static const MENUITEMS * curMenuItems = NULL; // current menu +static const LISTITEMS * curListItems = NULL; // current listmenu static const void (* curMenuRedrawHandle)(void) = NULL; // current custom menu static MENU_TYPE menuType = MENU_TYPE_ICON; -static const LABEL *curTitle = NULL; -static const GUI_RECT *curRect = NULL; // current menu layout grid -static uint16_t curRectCount = 0; // current menu layout rect count - -static struct { GUI_RECT rect; - SYS_STATUS status; - uint16_t inf; - uint32_t time; - } reminder = {{0, 0, LCD_WIDTH, TITLE_END_Y}, SYS_STATUS_DISCONNECTED, LABEL_UNCONNECTED, 0}; - -static struct { uint16_t x; - uint16_t y; - uint8_t r; - SYS_STATUS status; - uint32_t time; - } busySign = {LCD_WIDTH - 3, 3, 3, SYS_STATUS_BUSY, 0}; +static const LABEL * curTitle = NULL; +static const GUI_RECT * curRect = NULL; // current menu layout grid +static uint16_t curRectCount = 0; // current menu layout rect count + +static struct +{ + GUI_RECT rect; + SYS_STATUS status; + uint16_t inf; + uint32_t time; +} reminder = {{0, 0, LCD_WIDTH, TITLE_END_Y}, SYS_STATUS_DISCONNECTED, LABEL_UNCONNECTED, 0}; + +static struct +{ + uint16_t x; + uint16_t y; + uint8_t r; + SYS_STATUS status; + uint32_t time; +} busySign = {LCD_WIDTH - 3, 3, 3, SYS_STATUS_BUSY, 0}; SYS_STATUS getReminderStatus(void) { return reminder.status; } -void drawReminderMsg(void) +static void drawReminderMsg(void) { uint16_t msgRectOffset = (LCD_WIDTH - GUI_StrPixelWidth(reminder.inf)) / 2 - BYTE_WIDTH; @@ -640,6 +647,7 @@ void drawBusySign(void) GUI_SetColor(infoSettings.font_color); busySign.status = SYS_STATUS_BUSY; } + busySign.time = OS_GetTimeMs() + STATUS_BAR_REFRESH_TIME; } @@ -651,7 +659,9 @@ void loopBusySignClear(void) busySign.status = SYS_STATUS_IDLE; // clear busy signal status if (menuType == MENU_TYPE_FULLSCREEN) + { curMenuRedrawHandle(); + } else { GUI_SetColor(infoSettings.title_bg_color); @@ -676,68 +686,69 @@ void GUI_RestoreColorDefault(void) setFontSize(FONT_SIZE_NORMAL); } -MENUITEMS *getCurMenuItems(void) +MENUITEMS * getCurMenuItems(void) { return (MENUITEMS *)curMenuItems; } -LISTITEMS *getCurListItems(void) +LISTITEMS * getCurListItems(void) { return (LISTITEMS *)curListItems; } -// Get the top left point of the corresponding icon position) +// get the top left point of the corresponding icon position) GUI_POINT getIconStartPoint(int index) { GUI_POINT p = {curRect[index].x0, curRect[index].y0}; + return p; } -uint8_t *labelGetAddress(const LABEL *label) +uint8_t * labelGetAddress(const LABEL * label) { - if (label == NULL || label->index == LABEL_NULL) // No content in label + if (label == NULL || label->index == LABEL_NULL) // no content in label return NULL; - if (label->index < LABEL_NUM) // Index of language + if (label->index < LABEL_NUM) // index of language return textSelect(label->index); - else // Address of string + else // address of string return label->address; } -void menuDrawItem(const ITEM *item, uint8_t position) +void menuDrawItem(const ITEM * item, uint8_t position) { menuDrawIconOnly(item, position); menuDrawIconText(item, position); } -void menuDrawIconOnly(const ITEM *item, uint8_t position) +void menuDrawIconOnly(const ITEM * item, uint8_t position) { - const GUI_RECT *rect = curRect + position; + const GUI_RECT * rect = curRect + position; + if (item->icon != ICON_NULL) ICON_ReadDisplay(rect->x0, rect->y0, item->icon); else GUI_ClearPrect(rect); } -void menuDrawIconText(const ITEM *item, uint8_t position) +void menuDrawIconText(const ITEM * item, uint8_t position) { - uint8_t *content = labelGetAddress(&item->label); - const GUI_RECT *rect = curRect + ITEM_PER_PAGE + position; + uint8_t * content = labelGetAddress(&item->label); + const GUI_RECT * rect = curRect + ITEM_PER_PAGE + position; + GUI_ClearPrect(rect); + if (content) GUI_DispStringInPrect(rect, content); } -void menuDrawListItem(const LISTITEM *item, uint8_t position) +void menuDrawListItem(const LISTITEM * item, uint8_t position) { - const GUI_RECT *rect = rect_of_keyListView + position; + const GUI_RECT * rect = rect_of_keyListView + position; + if (item->icon == CHARICON_NULL) - { GUI_ClearPrect(rect); - } else - { ListItem_Display(rect, position, item, false); - } } void menuRefreshListPage(void) @@ -760,8 +771,8 @@ MENU_TYPE getMenuType(void) } void setMenu(MENU_TYPE menu_type, LABEL * title, uint16_t rectCount, const GUI_RECT * menuRect, - void (*action_redraw)(uint8_t position, uint8_t is_press), - void (*menu_redraw)(void)) + void (* action_redraw)(uint8_t position, uint8_t is_press), + void (* menu_redraw)(void)) { menuType = menu_type; curRect = menuRect; @@ -775,7 +786,7 @@ void setMenu(MENU_TYPE menu_type, LABEL * title, uint16_t rectCount, const GUI_R #endif } -void menuSetTitle(const LABEL *title) +void menuSetTitle(const LABEL * title) { curTitle = title; menuDrawTitle(); @@ -791,12 +802,14 @@ void menuDrawTitle(void) { if (curMenuRedrawHandle != NULL) curMenuRedrawHandle(); + return; } if (toastRunning()) { drawToast(true); + return; } @@ -808,7 +821,7 @@ void menuDrawTitle(void) // NOTE: load the label just before displaying it. This is needed only in case a secondary language pack (.ini file) is used // by the TFT (secondary language shares a common buffer where all labels are loaded from flash memory) just to avoid the // possibility to display a wrong label - uint8_t *titleString = labelGetAddress(curTitle); + uint8_t * titleString = labelGetAddress(curTitle); GUI_SetBkColor(infoSettings.title_bg_color); @@ -831,8 +844,8 @@ void menuDrawTitle(void) if (reminder.status != SYS_STATUS_IDLE) drawReminderMsg(); } -// When there is a button value, the icon changes color and redraws -void itemDrawIconPress(uint8_t position, uint8_t is_press) +// when there is a button value, the icon changes color and redraws +static void itemDrawIconPress(uint8_t position, uint8_t is_press) { if (position > KEY_ICON_7) return; @@ -841,25 +854,26 @@ void itemDrawIconPress(uint8_t position, uint8_t is_press) if (curMenuItems == NULL) return; if (curMenuItems->items[position].icon == ICON_NULL) return; - const GUI_RECT *rect = curRect + position; + const GUI_RECT * rect = curRect + position; - if (is_press) // Turn green when pressed + if (is_press) // turn green when pressed ICON_PressedDisplay(rect->x0, rect->y0, curMenuItems->items[position].icon); - else // Redraw normal icon when released + else // redraw normal icon when released ICON_ReadDisplay(rect->x0, rect->y0,curMenuItems->items[position].icon); } else if (menuType == MENU_TYPE_LISTVIEW) { // draw rec over list item if pressed - if (curListItems == NULL) - return; + if (curListItems == NULL) return; - const GUI_RECT *rect = rect_of_keyListView + position; + const GUI_RECT * rect = rect_of_keyListView + position; if (curListItems->items[position].icon == CHARICON_NULL) { GUI_ClearPrect(rect); + return; } + if (is_press) ListItem_Display(rect,position,&curListItems->items[position], true); else @@ -867,24 +881,26 @@ void itemDrawIconPress(uint8_t position, uint8_t is_press) } } -// When there is a button value, the icon changes color and redraws -void itemDrawIconPress_PS(uint8_t position, uint8_t is_press) +// when there is a button value, the icon changes color and redraws +static void itemDrawIconPress_PS(uint8_t position, uint8_t is_press) { if (position < PS_KEY_6 || position > PS_KEY_9) return; + position -= PS_TOUCH_OFFSET; - const GUI_RECT *rect = curRect + position; + const GUI_RECT * rect = curRect + position; - if (is_press) // Turn green when pressed + if (is_press) // turn green when pressed ICON_PressedDisplay(rect->x0, rect->y0, curMenuItems->items[position].icon); - else // Redraw normal icon when released + else // redraw normal icon when released ICON_ReadDisplay(rect->x0, rect->y0,curMenuItems->items[position].icon); } -// Draw the entire interface -void menuDrawPage(const MENUITEMS *menuItems) +// draw the entire interface +void menuDrawPage(const MENUITEMS * menuItems) { uint8_t i = 0; + menuType = MENU_TYPE_ICON; curMenuItems = menuItems; curMenuRedrawHandle = NULL; @@ -914,7 +930,7 @@ void menuDrawPage(const MENUITEMS *menuItems) curRect = (MENU_IS(menuStatus)) ? rect_of_keySS : rect_of_key; #endif - menuClearGaps(); // Use this function instead of GUI_Clear to eliminate the splash screen when clearing the screen. + menuClearGaps(); // use this function instead of GUI_Clear to eliminate the splash screen when clearing the screen. menuSetTitle(&curMenuItems->title); for (i = 0; i < ITEM_PER_PAGE; i++) @@ -928,10 +944,11 @@ void menuDrawPage(const MENUITEMS *menuItems) #endif } -// Draw the entire interface -void menuDrawListPage(const LISTITEMS *listItems) +// draw the entire interface +void menuDrawListPage(const LISTITEMS * listItems) { uint8_t i = 0; + menuType = MENU_TYPE_LISTVIEW; curListItems = listItems; curMenuRedrawHandle = NULL; @@ -942,14 +959,15 @@ void menuDrawListPage(const LISTITEMS *listItems) GUI_SetBkColor(infoSettings.bg_color); GUI_ClearRect(0, TITLE_END_Y, LCD_WIDTH, LCD_HEIGHT); - //menuClearGaps(); // Use this function instead of GUI_Clear to eliminate the splash screen when clearing the screen. + //menuClearGaps(); // use this function instead of GUI_Clear to eliminate the splash screen when clearing the screen menuSetTitle(&listItems->title); for (i = 0; i < ITEM_PER_PAGE; i++) { - //const GUI_RECT *rect = rect_of_keyListView + i; + //const GUI_RECT * rect = rect_of_keyListView + i; if (curListItems->items[i].icon != CHARICON_NULL) menuDrawListItem(&curListItems->items[i], i); + RAPID_PRINTING_COMM() // perform backend printing loop between drawing icons to avoid printer idling } @@ -958,10 +976,10 @@ void menuDrawListPage(const LISTITEMS *listItems) #endif } -// Show live info text on icons +// show live info text on icons void showLiveInfo(uint8_t index, const LIVE_INFO * liveicon, bool redrawIcon) { - const GUI_RECT *iconRect = MENU_IS(menuPrinting) ? rect_of_keyPS : curRect; + const GUI_RECT * iconRect = MENU_IS(menuPrinting) ? rect_of_keyPS : curRect; const GUI_POINT iconPt = {iconRect[index].x0, iconRect[index].y0}; if (redrawIcon) @@ -1031,6 +1049,7 @@ void displayExhibitHeader(const char * titleStr, const char * unitStr) if (titleStr != NULL) { char tempstr[20]; + sprintf(tempstr, "%-8s", titleStr); GUI_DispString(exhibitRect.x0, exhibitRect.y0, (uint8_t *)tempstr); } @@ -1052,7 +1071,7 @@ void displayExhibitValue(const char * valueStr) setFontSize(FONT_SIZE_NORMAL); } -// Get button value +// get button value KEY_VALUES menuKeyGetValue(void) { KEY_VALUES tempkey = KEY_IDLE; @@ -1062,42 +1081,42 @@ KEY_VALUES menuKeyGetValue(void) switch (menuType) { case MENU_TYPE_ICON: + { + if (MENU_IS(menuStatus)) + { + tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_keySS), rect_of_keySS); + } + else if (MENU_IS(menuPrinting)) { - if (MENU_IS(menuStatus)) - { - tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_keySS), rect_of_keySS); - } - else if (MENU_IS(menuPrinting)) - { - if (isPrinting() || isPrintingFromOnboard()) - tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_keyPS), rect_of_keyPS); - else - tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_keyPS_end), rect_of_keyPS_end); - - if (tempkey == (KEY_VALUES)PS_KEY_TITLEBAR) - tempkey = KEY_TITLEBAR; - } - else if ((MENU_IS(menuHeat)) || - (MENU_IS(menuLoadUnload)) || - (MENU_IS(menuMPC)) || - (MENU_IS(menuPid)) || - (MENU_IS(menuTuneExtruder)) || - (MENU_IS(menuFan)) || - (MENU_IS(menuExtrude)) || - (MENU_IS(menuSpeed)) || - (MENU_IS(menuZOffset)) || - (MENU_IS(menuMBL)) || - (MENU_IS(menuBabystep)) || - (MENU_IS(menuMeshEditor))) - { - tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_keysIN), rect_of_keysIN); - } + if (isPrinting() || isPrintingFromOnboard()) + tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_keyPS), rect_of_keyPS); else - { - tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_key), rect_of_key); - } + tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_keyPS_end), rect_of_keyPS_end); + + if (tempkey == (KEY_VALUES)PS_KEY_TITLEBAR) + tempkey = KEY_TITLEBAR; + } + else if ((MENU_IS(menuHeat)) || + (MENU_IS(menuLoadUnload)) || + (MENU_IS(menuMPC)) || + (MENU_IS(menuPid)) || + (MENU_IS(menuTuneExtruder)) || + (MENU_IS(menuFan)) || + (MENU_IS(menuExtrude)) || + (MENU_IS(menuSpeed)) || + (MENU_IS(menuZOffset)) || + (MENU_IS(menuMBL)) || + (MENU_IS(menuBabystep)) || + (MENU_IS(menuMeshEditor))) + { + tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_keysIN), rect_of_keysIN); + } + else + { + tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_key), rect_of_key); } break; + } case MENU_TYPE_LISTVIEW: tempkey = (KEY_VALUES)KEY_GetValue(COUNT(rect_of_keyListView), rect_of_keyListView); @@ -1134,7 +1153,7 @@ KEY_VALUES menuKeyGetValue(void) return tempkey; } -// Smart home (long press on back button to go to status screen) +// smart home (long press on back button to go to status screen) #ifdef SMART_HOME void loopCheckBackPress(void) @@ -1171,6 +1190,7 @@ void loopCheckBackPress(void) if (backHeld == true) // prevent mode selection or screenshot if Back button is held { backHeld = Touch_Enc_ReadPen(0); + return; } #endif @@ -1178,17 +1198,14 @@ void loopCheckBackPress(void) if (longPress == false && Touch_Enc_ReadPen(LONG_TOUCH)) // check if longpress already handled and check if TSC is pressed and held { KEY_VALUES tempKey = KEY_IDLE; + longPress = true; TS_Sound = false; if (MENU_IS(menuPrinting)) - { tempKey = TS_KeyValue(COUNT(rect_of_keySS), rect_of_keySS); - } else - { tempKey = TS_KeyValue(COUNT(rect_of_key), rect_of_key); - } TS_Sound = true; diff --git a/TFT/src/User/API/menu.h b/TFT/src/User/API/menu.h index 0d78934cd9..04555bfbfe 100644 --- a/TFT/src/User/API/menu.h +++ b/TFT/src/User/API/menu.h @@ -88,7 +88,7 @@ typedef enum typedef union { int32_t index; // language index, address = textSelect(index); - void *address; + void * address; } LABEL; // always initialize label to default values @@ -163,7 +163,6 @@ extern const GUI_RECT rect_of_key[MENU_RECT_COUNT]; extern const GUI_RECT rect_of_keySS[SS_RECT_COUNT]; extern const GUI_RECT rect_of_keyPS[]; extern const GUI_RECT rect_of_keyPS_end[]; -extern const GUI_RECT rect_of_keyPS_draw[]; // used to draw VERTICAL GUI Printing menu extern const GUI_RECT rect_of_titleBar[1]; SYS_STATUS getReminderStatus(void); @@ -175,25 +174,25 @@ void loopBusySignClear(void); void notificationDot(void); void GUI_RestoreColorDefault(void); -MENUITEMS *getCurMenuItems(void); -LISTITEMS *getCurListItems(void); +MENUITEMS * getCurMenuItems(void); +LISTITEMS * getCurListItems(void); GUI_POINT getIconStartPoint(int index); -uint8_t *labelGetAddress(const LABEL * label); +uint8_t * labelGetAddress(const LABEL * label); void menuDrawItem (const ITEM * menuItem, uint8_t position); -void menuDrawIconOnly(const ITEM *item, uint8_t position); -void menuDrawIconText(const ITEM *item, uint8_t position); -void menuDrawListItem(const LISTITEM *item, uint8_t position); +void menuDrawIconOnly(const ITEM * item, uint8_t position); +void menuDrawIconText(const ITEM * item, uint8_t position); +void menuDrawListItem(const LISTITEM * item, uint8_t position); void menuRefreshListPage(void); void setMenuType(MENU_TYPE type); MENU_TYPE getMenuType(void); void setMenu(MENU_TYPE menu_type, LABEL * title, uint16_t rectCount, const GUI_RECT * menuRect, - void (*action_redraw)(uint8_t position, uint8_t is_press), - void (*menu_redraw)(void)); -void menuSetTitle(const LABEL *title); + void (* action_redraw)(uint8_t position, uint8_t is_press), + void (* menu_redraw)(void)); +void menuSetTitle(const LABEL * title); void menuDrawTitle(void); void menuDrawPage(const MENUITEMS * menuItems); -void menuDrawListPage(const LISTITEMS *listItems); +void menuDrawListPage(const LISTITEMS * listItems); void showLiveInfo(uint8_t index, const LIVE_INFO * liveicon, bool redrawIcon); void displayExhibitHeader(const char * titleStr, const char * unitStr); diff --git a/TFT/src/User/API/printf/printf.c b/TFT/src/User/API/printf/printf.c index cc3d65d6fc..6bd0d12bcd 100644 --- a/TFT/src/User/API/printf/printf.c +++ b/TFT/src/User/API/printf/printf.c @@ -47,14 +47,14 @@ // numeric number including padded zeros (dynamically created on stack) // default: 32 byte #ifndef PRINTF_NTOA_BUFFER_SIZE -#define PRINTF_NTOA_BUFFER_SIZE 32U +#define PRINTF_NTOA_BUFFER_SIZE 32U #endif // 'ftoa' conversion buffer size, this must be big enough to hold one converted // float number including padded zeros (dynamically created on stack) // default: 32 byte #ifndef PRINTF_FTOA_BUFFER_SIZE -#define PRINTF_FTOA_BUFFER_SIZE 32U +#define PRINTF_FTOA_BUFFER_SIZE 32U #endif // support for the floating point type (%f) @@ -94,10 +94,11 @@ //#define PRINTF_SUPPORT_PTRDIFF_T #endif -//The maximum number of characters to store in the buffer, including a terminating null character +// the maximum number of characters to store in the buffer, including a terminating null character #ifndef MAX_BUFFER_LENGTH #define MAX_BUFFER_LENGTH (size_t)-1 // default: (size_t)-1 #endif + /////////////////////////////////////////////////////////////////////////////// // internal flag definitions @@ -126,8 +127,9 @@ typedef void (*out_fct_type)(char character, void* buffer, size_t idx, size_t ma // wrapper (used as buffer) for output function type -typedef struct { - void (*fct)(char character, void* arg); +typedef struct +{ + void (*fct)(char character, void* arg); void* arg; } out_fct_wrap_type; @@ -135,7 +137,8 @@ typedef struct { // internal buffer output static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen) { - if (idx < maxlen) { + if (idx < maxlen) + { ((char*)buffer)[idx] = character; } } @@ -152,7 +155,9 @@ static inline void _out_null(char character, void* buffer, size_t idx, size_t ma static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen) { (void)buffer; (void)idx; (void)maxlen; - if (character) { + + if (character) + { _putchar(character); } } @@ -162,8 +167,9 @@ static inline void _out_char(char character, void* buffer, size_t idx, size_t ma static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen) { (void)idx; (void)maxlen; - if (character) { - // buffer is the output fct pointer + + if (character) + { // buffer is the output fct pointer ((out_fct_wrap_type*)buffer)->fct(character, ((out_fct_wrap_type*)buffer)->arg); } } @@ -174,7 +180,9 @@ static inline void _out_fct(char character, void* buffer, size_t idx, size_t max static inline unsigned int _strnlen_s(const char* str, size_t maxsize) { const char* s; + for (s = str; *s && maxsize--; ++s); + return (unsigned int)(s - str); } @@ -191,9 +199,12 @@ static inline bool _is_digit(char ch) static unsigned int _atoi(const char** str) { unsigned int i = 0U; - while (_is_digit(**str)) { + + while (_is_digit(**str)) + { i = i * 10U + (unsigned int)(*((*str)++) - '0'); } + return i; } @@ -204,20 +215,25 @@ static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen const size_t start_idx = idx; // pad spaces up to given width - if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { - for (size_t i = len; i < width; i++) { + if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) + { + for (size_t i = len; i < width; i++) + { out(' ', buffer, idx++, maxlen); } } // reverse string - while (len) { + while (len) + { out(buf[--len], buffer, idx++, maxlen); } // append pad spaces up to given width - if (flags & FLAGS_LEFT) { - while (idx - start_idx < width) { + if (flags & FLAGS_LEFT) + { + while (idx - start_idx < width) + { out(' ', buffer, idx++, maxlen); } } @@ -230,48 +246,68 @@ static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags) { // pad leading zeros - if (!(flags & FLAGS_LEFT)) { - if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { + if (!(flags & FLAGS_LEFT)) + { + if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) + { width--; } - while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + + while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) + { buf[len++] = '0'; } - while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + + while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) + { buf[len++] = '0'; } } // handle hash - if (flags & FLAGS_HASH) { - if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) { + if (flags & FLAGS_HASH) + { + if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) + { len--; - if (len && (base == 16U)) { + + if (len && (base == 16U)) + { len--; } } - if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + + if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) + { buf[len++] = 'x'; } - else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) + { buf[len++] = 'X'; } - else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) + { buf[len++] = 'b'; } - if (len < PRINTF_NTOA_BUFFER_SIZE) { + + if (len < PRINTF_NTOA_BUFFER_SIZE) + { buf[len++] = '0'; } } - if (len < PRINTF_NTOA_BUFFER_SIZE) { - if (negative) { + if (len < PRINTF_NTOA_BUFFER_SIZE) + { + if (negative) + { buf[len++] = '-'; } - else if (flags & FLAGS_PLUS) { + else if (flags & FLAGS_PLUS) + { buf[len++] = '+'; // ignore the space if the '+' exists } - else if (flags & FLAGS_SPACE) { + else if (flags & FLAGS_SPACE) + { buf[len++] = ' '; } } @@ -287,14 +323,17 @@ static size_t _ntoa_long(out_fct_type out, char* buffer, size_t idx, size_t maxl size_t len = 0U; // no hash for 0 values - if (!value) { + if (!value) + { flags &= ~FLAGS_HASH; } // write if precision != 0 and value is != 0 - if (!(flags & FLAGS_PRECISION) || value) { + if (!(flags & FLAGS_PRECISION) || value) + { do { const char digit = (char)(value % base); + buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; value /= base; } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); @@ -312,14 +351,17 @@ static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t size_t len = 0U; // no hash for 0 values - if (!value) { + if (!value) + { flags &= ~FLAGS_HASH; } // write if precision != 0 and value is != 0 - if (!(flags & FLAGS_PRECISION) || value) { + if (!(flags & FLAGS_PRECISION) || value) + { do { const char digit = (char)(value % base); + buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10; value /= base; } while (value && (len < PRINTF_NTOA_BUFFER_SIZE)); @@ -357,7 +399,8 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d // test for very large values // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad - if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) { + if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) + { #if defined(PRINTF_SUPPORT_EXPONENTIAL) return _etoa(out, buffer, idx, maxlen, value, prec, width, flags); #else @@ -367,17 +410,22 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d // test for negative bool negative = false; - if (value < 0) { + + if (value < 0) + { negative = true; value = 0 - value; } // set default precision, if not set explicitly - if (!(flags & FLAGS_PRECISION)) { + if (!(flags & FLAGS_PRECISION)) + { prec = PRINTF_DEFAULT_FLOAT_PRECISION; } + // limit precision to 9, cause a prec >= 10 can lead to overflow errors - while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) { + while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) + { buf[len++] = '0'; prec--; } @@ -385,77 +433,106 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d int whole = (int)value; double tmp = (value - whole) * pow10[prec]; unsigned long frac = (unsigned long)tmp; + diff = tmp - frac; - if (diff > 0.5) { + if (diff > 0.5) + { ++frac; + // handle rollover, e.g. case 0.99 with prec 1 is 1.0 - if (frac >= pow10[prec]) { + if (frac >= pow10[prec]) + { frac = 0; ++whole; } } - else if (diff < 0.5) { + else if (diff < 0.5) + { } - else if ((frac == 0U) || (frac & 1U)) { + else if ((frac == 0U) || (frac & 1U)) + { // if halfway, round up if odd OR if last digit is 0 ++frac; } - if (prec == 0U) { + if (prec == 0U) + { diff = value - (double)whole; - if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) { + + if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) + { // exactly 0.5 and ODD, then round up // 1.5 -> 2, but 2.5 -> 2 ++whole; } } - else { + else + { unsigned int count = prec; + // now do fractional part, as an unsigned number - while (len < PRINTF_FTOA_BUFFER_SIZE) { + while (len < PRINTF_FTOA_BUFFER_SIZE) + { --count; buf[len++] = (char)(48U + (frac % 10U)); - if (!(frac /= 10U)) { + + if (!(frac /= 10U)) + { break; } } + // add extra 0s - while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) { + while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) + { buf[len++] = '0'; } - if (len < PRINTF_FTOA_BUFFER_SIZE) { + + if (len < PRINTF_FTOA_BUFFER_SIZE) + { // add decimal buf[len++] = '.'; } } // do whole part, number is reversed - while (len < PRINTF_FTOA_BUFFER_SIZE) { + while (len < PRINTF_FTOA_BUFFER_SIZE) + { buf[len++] = (char)(48 + (whole % 10)); - if (!(whole /= 10)) { + + if (!(whole /= 10)) + { break; } } // pad leading zeros - if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) { - if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { + if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) + { + if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) + { width--; } - while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) { + + while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) + { buf[len++] = '0'; } } - if (len < PRINTF_FTOA_BUFFER_SIZE) { - if (negative) { + if (len < PRINTF_FTOA_BUFFER_SIZE) + { + if (negative) + { buf[len++] = '-'; } - else if (flags & FLAGS_PLUS) { + else if (flags & FLAGS_PLUS) + { buf[len++] = '+'; // ignore the space if the '+' exists } - else if (flags & FLAGS_SPACE) { + else if (flags & FLAGS_SPACE) + { buf[len++] = ' '; } } @@ -465,46 +542,61 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d #if defined(PRINTF_SUPPORT_EXPONENTIAL) + // internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) { // check for NaN and special values - if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) { + if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) + { return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags); } // determine the sign const bool negative = value < 0; - if (negative) { + + if (negative) + { value = -value; } // default precision - if (!(flags & FLAGS_PRECISION)) { + if (!(flags & FLAGS_PRECISION)) + { prec = PRINTF_DEFAULT_FLOAT_PRECISION; } // determine the decimal exponent // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c) - union { + union + { uint64_t U; double F; } conv; conv.F = value; + int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023; // effectively log2 + conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U); // drop the exponent so conv.F is now in [1,2) + // now approximate log10 from the log2 integer part and an expansion of ln around 1.5 int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168); + // now we want to compute 10^expval but we want to be sure it won't overflow exp2 = (int)(expval * 3.321928094887362 + 0.5); + const double z = expval * 2.302585092994046 - exp2 * 0.6931471805599453; const double z2 = z * z; + conv.U = (uint64_t)(exp2 + 1023) << 52U; + // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14))))); + // correct for rounding errors - if (value < conv.F) { + if (value < conv.F) + { expval--; conv.F /= 10; } @@ -513,23 +605,30 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U; // in "%g" mode, "prec" is the number of *significant figures* not decimals - if (flags & FLAGS_ADAPT_EXP) { + if (flags & FLAGS_ADAPT_EXP) + { // do we want to fall-back to "%f" mode? - if ((value >= 1e-4) && (value < 1e6)) { - if ((int)prec > expval) { + if ((value >= 1e-4) && (value < 1e6)) + { + if ((int)prec > expval) + { prec = (unsigned)((int)prec - expval - 1); } - else { + else + { prec = 0; } + flags |= FLAGS_PRECISION; // make sure _ftoa respects precision // no characters in exponent minwidth = 0U; expval = 0; } - else { + else + { // we use one sigfig for the whole part - if ((prec > 0) && (flags & FLAGS_PRECISION)) { + if ((prec > 0) && (flags & FLAGS_PRECISION)) + { --prec; } } @@ -537,41 +636,57 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d // will everything fit? unsigned int fwidth = width; - if (width > minwidth) { + + if (width > minwidth) + { // we didn't fall-back so subtract the characters required for the exponent fwidth -= minwidth; - } else { + } + else + { // not enough characters, so go back to default sizing fwidth = 0U; } - if ((flags & FLAGS_LEFT) && minwidth) { + + if ((flags & FLAGS_LEFT) && minwidth) + { // if we're padding on the right, DON'T pad the floating part fwidth = 0U; } // rescale the float value - if (expval) { + if (expval) + { value /= conv.F; } // output the floating part const size_t start_idx = idx; + idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP); // output the exponent part - if (minwidth) { + if (minwidth) + { // output the exponential symbol out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen); + // output the exponent value idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS); + // might need to right-pad spaces - if (flags & FLAGS_LEFT) { - while (idx - start_idx < width) out(' ', buffer, idx++, maxlen); + if (flags & FLAGS_LEFT) + { + while (idx - start_idx < width) + out(' ', buffer, idx++, maxlen); } } + return idx; } + #endif // PRINTF_SUPPORT_EXPONENTIAL + #endif // PRINTF_SUPPORT_FLOAT @@ -581,7 +696,8 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const unsigned int flags, width, precision, n; size_t idx = 0U; - if (!buffer) { + if (!buffer) + { // use null output function out = _out_null; } @@ -589,21 +705,26 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const while (*format) { // format specifier? %[flags][width][.precision][length] - if (*format != '%') { + if (*format != '%') + { // no out(*format, buffer, idx++, maxlen); format++; + continue; } - else { + else + { // yes, evaluate it format++; } // evaluate flags flags = 0U; + do { - switch (*format) { + switch (*format) + { case '0': flags |= FLAGS_ZEROPAD; format++; n = 1U; break; case '-': flags |= FLAGS_LEFT; format++; n = 1U; break; case '+': flags |= FLAGS_PLUS; format++; n = 1U; break; @@ -615,151 +736,200 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const // evaluate width field width = 0U; - if (_is_digit(*format)) { + + if (_is_digit(*format)) + { width = _atoi(&format); } - else if (*format == '*') { + else if (*format == '*') + { const int w = va_arg(va, int); - if (w < 0) { - flags |= FLAGS_LEFT; // reverse padding + + if (w < 0) + { + flags |= FLAGS_LEFT; // reverse padding width = (unsigned int)-w; } - else { + else + { width = (unsigned int)w; } + format++; } // evaluate precision field precision = 0U; - if (*format == '.') { + + if (*format == '.') + { flags |= FLAGS_PRECISION; format++; - if (_is_digit(*format)) { + + if (_is_digit(*format)) + { precision = _atoi(&format); } - else if (*format == '*') { + else if (*format == '*') + { const int prec = (int)va_arg(va, int); + precision = prec > 0 ? (unsigned int)prec : 0U; format++; } } // evaluate length field - switch (*format) { + switch (*format) + { case 'l' : flags |= FLAGS_LONG; format++; - if (*format == 'l') { + + if (*format == 'l') + { flags |= FLAGS_LONG_LONG; format++; } break; + case 'h' : flags |= FLAGS_SHORT; format++; - if (*format == 'h') { + + if (*format == 'h') + { flags |= FLAGS_CHAR; format++; } break; + #if defined(PRINTF_SUPPORT_PTRDIFF_T) case 't' : flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); format++; break; #endif + case 'j' : flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); format++; break; + case 'z' : flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); format++; break; + default : break; } // evaluate specifier - switch (*format) { + switch (*format) + { case 'd' : case 'i' : case 'u' : case 'x' : case 'X' : case 'o' : - case 'b' : { + case 'b' : + { // set the base unsigned int base; - if (*format == 'x' || *format == 'X') { + + if (*format == 'x' || *format == 'X') + { base = 16U; } - else if (*format == 'o') { + else if (*format == 'o') + { base = 8U; } - else if (*format == 'b') { + else if (*format == 'b') + { base = 2U; } - else { + else + { base = 10U; - flags &= ~FLAGS_HASH; // no hash for dec format + flags &= ~FLAGS_HASH; // no hash for dec format } + // uppercase - if (*format == 'X') { + if (*format == 'X') + { flags |= FLAGS_UPPERCASE; } // no plus or space flag for u, x, X, o, b - if ((*format != 'i') && (*format != 'd')) { + if ((*format != 'i') && (*format != 'd')) + { flags &= ~(FLAGS_PLUS | FLAGS_SPACE); } // ignore '0' flag when precision is given - if (flags & FLAGS_PRECISION) { + if (flags & FLAGS_PRECISION) + { flags &= ~FLAGS_ZEROPAD; } // convert the integer - if ((*format == 'i') || (*format == 'd')) { + if ((*format == 'i') || (*format == 'd')) + { // signed - if (flags & FLAGS_LONG_LONG) { + if (flags & FLAGS_LONG_LONG) + { #if defined(PRINTF_SUPPORT_LONG_LONG) const long long value = va_arg(va, long long); + idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); #endif } - else if (flags & FLAGS_LONG) { + else if (flags & FLAGS_LONG) + { const long value = va_arg(va, long); + idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); } else { const int value = (flags & FLAGS_CHAR) ? (signed char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va, int) : va_arg(va, int); + idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision, width, flags); } } - else { + else + { // unsigned - if (flags & FLAGS_LONG_LONG) { + if (flags & FLAGS_LONG_LONG) + { #if defined(PRINTF_SUPPORT_LONG_LONG) idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags); #endif } - else if (flags & FLAGS_LONG) { + else if (flags & FLAGS_LONG) + { idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags); } - else { + else + { const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va, unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int); + idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags); } } + format++; break; } + #if defined(PRINTF_SUPPORT_FLOAT) case 'f' : case 'F' : - if (*format == 'F') flags |= FLAGS_UPPERCASE; + if (*format == 'F') + flags |= FLAGS_UPPERCASE; + idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); format++; break; @@ -768,73 +938,104 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const case 'E': case 'g': case 'G': - if ((*format == 'g')||(*format == 'G')) flags |= FLAGS_ADAPT_EXP; - if ((*format == 'E')||(*format == 'G')) flags |= FLAGS_UPPERCASE; + if ((*format == 'g') || (*format == 'G')) + flags |= FLAGS_ADAPT_EXP; + + if ((*format == 'E') || (*format == 'G')) + flags |= FLAGS_UPPERCASE; + idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags); format++; break; #endif // PRINTF_SUPPORT_EXPONENTIAL #endif // PRINTF_SUPPORT_FLOAT + case 'c' : { unsigned int l = 1U; + // pre padding - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { + if (!(flags & FLAGS_LEFT)) + { + while (l++ < width) + { out(' ', buffer, idx++, maxlen); } } + // char output out((char)va_arg(va, int), buffer, idx++, maxlen); + // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { + if (flags & FLAGS_LEFT) + { + while (l++ < width) + { out(' ', buffer, idx++, maxlen); } } + format++; break; } - case 's' : { + case 's' : + { const char* p = va_arg(va, char*); unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1); + // pre padding - if (flags & FLAGS_PRECISION) { + if (flags & FLAGS_PRECISION) + { l = (l < precision ? l : precision); } - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { + + if (!(flags & FLAGS_LEFT)) + { + while (l++ < width) + { out(' ', buffer, idx++, maxlen); } } + // string output - while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) { + while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) + { out(*(p++), buffer, idx++, maxlen); } + // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { + if (flags & FLAGS_LEFT) + { + while (l++ < width) + { out(' ', buffer, idx++, maxlen); } } + format++; break; } - case 'p' : { + case 'p' : + { width = sizeof(void*) * 2U; flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE; + #if defined(PRINTF_SUPPORT_LONG_LONG) const bool is_ll = sizeof(uintptr_t) == sizeof(long long); - if (is_ll) { + + if (is_ll) + { idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void*), false, 16U, precision, width, flags); } - else { + else + { #endif idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void*)), false, 16U, precision, width, flags); #if defined(PRINTF_SUPPORT_LONG_LONG) } #endif + format++; break; } @@ -864,10 +1065,14 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const int printf_(const char* format, ...) { va_list va; + va_start(va, format); + char buffer[1]; const int ret = _vsnprintf(_out_char, buffer, MAX_BUFFER_LENGTH, format, va); + va_end(va); + return ret; } @@ -875,19 +1080,26 @@ int printf_(const char* format, ...) int sprintf_(char* buffer, const char* format, ...) { va_list va; + va_start(va, format); + const int ret = _vsnprintf(_out_buffer, buffer, MAX_BUFFER_LENGTH, format, va); + va_end(va); + return ret; } - int snprintf_(char* buffer, size_t count, const char* format, ...) { va_list va; + va_start(va, format); + const int ret = _vsnprintf(_out_buffer, buffer, count, format, va); + va_end(va); + return ret; } @@ -895,6 +1107,7 @@ int snprintf_(char* buffer, size_t count, const char* format, ...) int vprintf_(const char* format, va_list va) { char buffer[1]; + return _vsnprintf(_out_char, buffer, MAX_BUFFER_LENGTH, format, va); } @@ -908,9 +1121,13 @@ int vsnprintf_(char* buffer, size_t count, const char* format, va_list va) int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...) { va_list va; + va_start(va, format); + const out_fct_wrap_type out_fct_wrap = { out, arg }; const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, MAX_BUFFER_LENGTH, format, va); + va_end(va); + return ret; } diff --git a/TFT/src/User/API/printf/printf.h b/TFT/src/User/API/printf/printf.h index ddce7a2dab..29894aad9b 100644 --- a/TFT/src/User/API/printf/printf.h +++ b/TFT/src/User/API/printf/printf.h @@ -41,7 +41,7 @@ extern "C" { #include "variants.h" -// largest possilbe Float number +// Largest possilbe Float number #define PRINTF_MAX_FLOAT 1e8 // Disable support for long long diff --git a/TFT/src/User/Fatfs/diskio.c b/TFT/src/User/Fatfs/diskio.c index a12f772d53..2a4000932f 100644 --- a/TFT/src/User/Fatfs/diskio.c +++ b/TFT/src/User/Fatfs/diskio.c @@ -8,7 +8,7 @@ /*-----------------------------------------------------------------------*/ #include "ff.h" // Obtains integer types -#include "variants.h" // for SD_SPI_SUPPORT etc... +#include "variants.h" // for SD_SPI_SUPPORT etc. #include "diskio.h" // Declarations of disk functions #include "usb_conf.h" #include "usbh_msc_core.h" @@ -22,7 +22,7 @@ #include -#define MAX_RETRY_ATTEMPTS_COUNT 100 // used by handleDiskError function +#define MAX_RETRY_ATTEMPTS_COUNT 100 // Used by handleDiskError function // Definitions of physical drive number for each drive #define DEV_MMC 0 // MMC/SD card to physical drive 0 @@ -84,15 +84,15 @@ bool handleDiskError(BYTE pdrv, uint8_t * errorNum) { // PUT HERE ANY SPECIFIC ERROR HANDLING PROCEDURE // - // current error handling procedure based on device re-initialization + // Current error handling procedure based on device re-initialization // for a maximum number of retry attempts - // return "false" to force an abort or "true" to force a retry + // Return "false" to force an abort or "true" to force a retry if (++(*errorNum) <= MAX_RETRY_ATTEMPTS_COUNT) { if (pdrv == DEV_MMC) SD_Init(); -// else // if DEV_USB, the following code is already invoked by USBH_UDISK_Read / _Write so you can leave it commented out +// else // If DEV_USB, the following code is already invoked by USBH_UDISK_Read / _Write so you can leave it commented out // HCD_IsDeviceConnected(&USB_OTG_Core); return true; @@ -109,7 +109,7 @@ bool handleDiskError(BYTE pdrv, uint8_t * errorNum) DRESULT disk_read( BYTE pdrv, // Physical drive nmuber to identify the drive - BYTE *buff, // Data buffer to store read data + BYTE * buff, // Data buffer to store read data LBA_t sector, // Start sector in LBA UINT count // Number of sectors to read ) @@ -124,19 +124,19 @@ DRESULT disk_read( switch (pdrv) { case DEV_MMC: - if (SD_ReadDisk(buff, sector, count) == 0) // no read error + if (SD_ReadDisk(buff, sector, count) == 0) // No read error return RES_OK; break; case DEV_USB: - if (USBH_UDISK_Read(buff, sector, count) == 0) // no read error + if (USBH_UDISK_Read(buff, sector, count) == 0) // No read error return RES_OK; break; default: return RES_PARERR; } - } while (handleDiskError(pdrv, &errorNum)); // read error, retry or exit with error + } while (handleDiskError(pdrv, &errorNum)); // Read error, retry or exit with error return RES_ERROR; } @@ -148,10 +148,10 @@ DRESULT disk_read( #if FF_FS_READONLY == 0 DRESULT disk_write( - BYTE pdrv, // Physical drive nmuber to identify the drive - const BYTE *buff, // Data to be written - LBA_t sector, // Start sector in LBA - UINT count // Number of sectors to write + BYTE pdrv, // Physical drive nmuber to identify the drive + const BYTE * buff, // Data to be written + LBA_t sector, // Start sector in LBA + UINT count // Number of sectors to write ) { if (!count) return RES_PARERR; @@ -165,19 +165,19 @@ DRESULT disk_write( switch (pdrv) { case DEV_MMC: - if (SD_WriteDisk((uint8_t*)buff, sector, count) == 0) // no write error + if (SD_WriteDisk((uint8_t *)buff, sector, count) == 0) // No write error return RES_OK; break; case DEV_USB: - if (USBH_UDISK_Write((uint8_t*)buff, sector, count) == 0) // no write error + if (USBH_UDISK_Write((uint8_t *)buff, sector, count) == 0) // No write error return RES_OK; break; default: return RES_PARERR; } - } while (handleDiskError(pdrv, &errorNum)); // write error, retry or exit with error + } while (handleDiskError(pdrv, &errorNum)); // Write error, retry or exit with error return RES_ERROR; } @@ -189,9 +189,9 @@ DRESULT disk_write( /*-----------------------------------------------------------------------*/ DRESULT disk_ioctl( - BYTE pdrv, // Physical drive nmuber (0..) - BYTE cmd, // Control code - void *buff // Buffer to send/receive control data + BYTE pdrv, // Physical drive nmuber (0..) + BYTE cmd, // Control code + void * buff // Buffer to send/receive control data ) { switch (pdrv) @@ -210,17 +210,17 @@ DRESULT disk_ioctl( // break; // case GET_SECTOR_COUNT: // Get number of sectors on the disk (DWORD) - // *(DWORD*)buff = (DWORD) USBH_MSC_Param.MSCapacity; + // *(DWORD *)buff = (DWORD) USBH_MSC_Param.MSCapacity; // res = RES_OK; // break; // case GET_SECTOR_SIZE: // Get R/W sector size (WORD) - // *(WORD*)buff = 512; + // *(WORD *)buff = 512; // res = RES_OK; // break; // case GET_BLOCK_SIZE: // Get erase block size in unit of sector (DWORD) - // *(DWORD*)buff = 512; + // *(DWORD *)buff = 512; // break; // default: diff --git a/TFT/src/User/Fatfs/diskio.h b/TFT/src/User/Fatfs/diskio.h index 4fa32c027f..2031160579 100644 --- a/TFT/src/User/Fatfs/diskio.h +++ b/TFT/src/User/Fatfs/diskio.h @@ -32,9 +32,9 @@ typedef enum { // Prototypes for disk control functions DSTATUS disk_initialize (BYTE pdrv); DSTATUS disk_status (BYTE pdrv); -DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count); -DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count); -DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); +DRESULT disk_read (BYTE pdrv, BYTE * buff, LBA_t sector, UINT count); +DRESULT disk_write (BYTE pdrv, const BYTE * buff, LBA_t sector, UINT count); +DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void * buff); // Disk Status Bits (DSTATUS) #define STA_NOINIT 0x01 // Drive not initialized diff --git a/TFT/src/User/Fatfs/myfatfs.c b/TFT/src/User/Fatfs/myfatfs.c index e402d66771..371eb42b1b 100644 --- a/TFT/src/User/Fatfs/myfatfs.c +++ b/TFT/src/User/Fatfs/myfatfs.c @@ -1,5 +1,5 @@ #include "myfatfs.h" -#include "includes.h" // for infoSettings etc... +#include "includes.h" // for infoSettings etc. #include "diskio.h" FATFS fatfs[FF_VOLUMES]; // FATFS work area @@ -129,82 +129,94 @@ bool scanPrintFilesFatFs(void) return true; } -bool f_file_exists(const TCHAR* path) +bool f_file_exists(const TCHAR * path) { FIL tmp; if (f_open(&tmp, path, FA_OPEN_EXISTING) == FR_OK) { f_close(&tmp); + return true; } + return false; } -bool f_dir_exists(const TCHAR* path) +bool f_dir_exists(const TCHAR * path) { DIR tmp; if (f_opendir(&tmp, path) == FR_OK) { f_closedir(&tmp); + return true; } + return false; } static FRESULT f_remove_node( - TCHAR* path, // Path name buffer with the sub-directory to delete - UINT sz_buff, // Size of path name buffer (items) - FILINFO* fno // Name read buffer + TCHAR * path, // path name buffer with the sub-directory to delete + UINT sz_buff, // size of path name buffer (items) + FILINFO * fno // name read buffer ) { UINT i, j; FRESULT fr; DIR dir; - fr = f_opendir(&dir, path); // Open the directory - if (fr != FR_OK) return fr; + fr = f_opendir(&dir, path); // open the directory + + if (fr != FR_OK) + return fr; + + for (i = 0; path[i]; i++); // get current path length - for (i = 0; path[i]; i++); // Get current path length path[i++] = '/'; for (;;) { - fr = f_readdir(&dir, fno); // Get a directory item - if (fr != FR_OK || !fno->fname[0]) // End of directory? + fr = f_readdir(&dir, fno); // get a directory item + + if (fr != FR_OK || !fno->fname[0]) // end of directory? break; + j = 0; + do - { // Make a path name + { // make a path name if (i + j >= sz_buff) - { // Buffer over flow? + { // buffer over flow? fr = FR_DENIED; - goto end_delete; // Fails with 100 when buffer overflow + + goto end_delete; // fails with 100 when buffer overflow } + path[i + j] = fno->fname[j]; } while (fno->fname[j++]); - if (fno->fattrib & AM_DIR) - { // Item is a directory + + if (fno->fattrib & AM_DIR) // item is a directory fr = f_remove_node(path, sz_buff, fno); - } - else - { // Item is a file + else // item is a file fr = f_unlink(path); - } + if (fr != FR_OK) break; } end_delete: - path[--i] = 0; // Restore the path name + path[--i] = 0; // restore the path name f_closedir(&dir); - if (fr == FR_OK) fr = f_unlink(path); // Delete the empty directory + if (fr == FR_OK) + fr = f_unlink(path); // delete the empty directory + return fr; } -bool f_remove_full_dir(const TCHAR* path) +bool f_remove_full_dir(const TCHAR * path) { #define BUFFER_SIZE 256 @@ -212,9 +224,9 @@ bool f_remove_full_dir(const TCHAR* path) FILINFO tmpInfo; strncpy_no_pad(dirBuffer, path, BUFFER_SIZE); + if (f_remove_node(dirBuffer, BUFFER_SIZE, &tmpInfo) == FR_OK) - { return true; - } + return false; } diff --git a/TFT/src/User/Fatfs/myfatfs.h b/TFT/src/User/Fatfs/myfatfs.h index c3b1eb409f..9924e02907 100644 --- a/TFT/src/User/Fatfs/myfatfs.h +++ b/TFT/src/User/Fatfs/myfatfs.h @@ -11,9 +11,9 @@ bool mountSDCard(void); bool mountUSBDisk(void); bool scanPrintFilesFatFs(void); -bool f_file_exists(const char* path); -bool f_dir_exists(const char* path); -bool f_remove_full_dir(const char* path); +bool f_file_exists(const char * path); +bool f_dir_exists(const char * path); +bool f_remove_full_dir(const char * path); #ifdef __cplusplus } diff --git a/TFT/src/User/Hal/HD44780.c b/TFT/src/User/Hal/HD44780.c index f0708bed38..c39fc13c8a 100644 --- a/TFT/src/User/Hal/HD44780.c +++ b/TFT/src/User/Hal/HD44780.c @@ -4,7 +4,7 @@ #ifdef LCD2004_EMULATOR -CIRCULAR_QUEUE *HD44780_queue = NULL; +static CIRCULAR_QUEUE * HD44780_queue = NULL; void HD44780_DeConfig(void) { @@ -28,14 +28,14 @@ void HD44780_DeConfig(void) } /** -* LCD_EN PB15 4 line mode -* LCD_RS PB12 -* LCD_D4 PB13 -* LCD_D5 PB14 -* LCD_D6 PC7 -* LCD_D7 PC6 -*/ -void HD44780_Config(CIRCULAR_QUEUE *queue) + * LCD_EN PB15 4 line mode + * LCD_RS PB12 + * LCD_D4 PB13 + * LCD_D5 PB14 + * LCD_D6 PC7 + * LCD_D7 PC6 + */ +void HD44780_Config(CIRCULAR_QUEUE * queue) { HD44780_queue = queue; @@ -130,7 +130,7 @@ bool HD44780_writeData(void) return dataWritten; } -bool HD44780_getData(uint8_t *data) +bool HD44780_getData(uint8_t * data) { bool dataRead = false; diff --git a/TFT/src/User/Hal/HD44780.h b/TFT/src/User/Hal/HD44780.h index 27697bbe9f..2397cac9af 100644 --- a/TFT/src/User/Hal/HD44780.h +++ b/TFT/src/User/Hal/HD44780.h @@ -6,14 +6,14 @@ extern "C" { #endif #include -#include "variants.h" // for LCD2004_EMULATOR etc... -#include "CircularQueue.h" +#include "variants.h" // for LCD2004_EMULATOR etc. +#include "CircularQueue.h" // for CIRCULAR_QUEUE etc. #ifdef LCD2004_EMULATOR void HD44780_DeConfig(void); - void HD44780_Config(CIRCULAR_QUEUE *queue); + void HD44780_Config(CIRCULAR_QUEUE * queue); bool HD44780_writeData(void); - bool HD44780_getData(uint8_t *data); + bool HD44780_getData(uint8_t * data); #endif #ifdef __cplusplus diff --git a/TFT/src/User/Hal/Knob_LED.c b/TFT/src/User/Hal/Knob_LED.c index bf3c37d921..7c497d297d 100644 --- a/TFT/src/User/Hal/Knob_LED.c +++ b/TFT/src/User/Hal/Knob_LED.c @@ -1,10 +1,10 @@ #include "Knob_LED.h" -#include "includes.h" // for mcuClocks, KNOB_LED_COLOR_PIN etc... +#include "includes.h" // for mcuClocks, KNOB_LED_COLOR_PIN etc. #include "GPIO_Init.h" #ifdef KNOB_LED_COLOR_PIN -uint32_t frameTimeStamp = 0; // Frame unit need > 280us for WS2812 +static uint32_t frameTimeStamp = 0; // Frame unit need > 280us for WS2812 // total 2.5us, run in 400Khz #define NEOPIXEL_T0H_US 0.35 // Neopixel code 0 high level hold time in us diff --git a/TFT/src/User/Hal/Knob_LED.h b/TFT/src/User/Hal/Knob_LED.h index 47d939d397..6dcece408e 100644 --- a/TFT/src/User/Hal/Knob_LED.h +++ b/TFT/src/User/Hal/Knob_LED.h @@ -5,8 +5,8 @@ extern "C" { #endif -#include -#include "variants.h" // for KNOB_LED_COLOR_PIN etc... +#include // for uint32_t etc. +#include "variants.h" // for KNOB_LED_COLOR_PIN etc. #ifdef KNOB_LED_COLOR_PIN void knob_LED_Init(void); diff --git a/TFT/src/User/Hal/LCD_Driver/HX8558.c b/TFT/src/User/Hal/LCD_Driver/HX8558.c index bc6e535b9d..95ced67c6e 100644 --- a/TFT/src/User/Hal/LCD_Driver/HX8558.c +++ b/TFT/src/User/Hal/LCD_Driver/HX8558.c @@ -3,7 +3,12 @@ #if LCD_DRIVER_HAS(HX8558) #include "HX8558.h" - // HX8558 + + #define HX8558_0_DEGREE_REG_VALUE 0xA4 + #define HX8558_180_DEGREE_REG_VALUE 0X64 + #define HX8558_90_DEGREE_REG_VALUE 0x04 + #define HX8558_270_DEGREE_REG_VALUE 0xC4 + void HX8558_Init_Sequential(void) { Delay_ms(50); // delay 50 ms diff --git a/TFT/src/User/Hal/LCD_Driver/HX8558.h b/TFT/src/User/Hal/LCD_Driver/HX8558.h index 403322e603..cc43c37f98 100644 --- a/TFT/src/User/Hal/LCD_Driver/HX8558.h +++ b/TFT/src/User/Hal/LCD_Driver/HX8558.h @@ -5,10 +5,7 @@ extern "C" { #endif -#define HX8558_0_DEGREE_REG_VALUE 0xA4 -#define HX8558_180_DEGREE_REG_VALUE 0X64 -#define HX8558_90_DEGREE_REG_VALUE 0x04 -#define HX8558_270_DEGREE_REG_VALUE 0xC4 +#include uint8_t LCD_DriveIsHX8558(void); void HX8558_Init_Sequential(void); diff --git a/TFT/src/User/Hal/LCD_Driver/ILI9325.c b/TFT/src/User/Hal/LCD_Driver/ILI9325.c index ccc6ed4b4c..fd07af80dd 100644 --- a/TFT/src/User/Hal/LCD_Driver/ILI9325.c +++ b/TFT/src/User/Hal/LCD_Driver/ILI9325.c @@ -3,7 +3,7 @@ #if LCD_DRIVER_HAS(ILI9325) #include "ILI9325.h" - // ILI9325 + void ILI9325_Init_Sequential(void) { #if defined(MKS_TFT32L_V3_0) // For this, SS=0 and GS=0 for default screen rotation=0 diff --git a/TFT/src/User/Hal/LCD_Driver/ILI9325.h b/TFT/src/User/Hal/LCD_Driver/ILI9325.h index ed0e8da0ba..3617b1450a 100644 --- a/TFT/src/User/Hal/LCD_Driver/ILI9325.h +++ b/TFT/src/User/Hal/LCD_Driver/ILI9325.h @@ -5,6 +5,8 @@ extern "C" { #endif +#include + uint8_t LCD_DriveIsILI9325(void); void ILI9325_Init_Sequential(void); void ILI9325_SetDirection(uint8_t rotate); diff --git a/TFT/src/User/Hal/LCD_Driver/ILI9341.c b/TFT/src/User/Hal/LCD_Driver/ILI9341.c index 1b7cc0fe8e..73f60dd114 100644 --- a/TFT/src/User/Hal/LCD_Driver/ILI9341.c +++ b/TFT/src/User/Hal/LCD_Driver/ILI9341.c @@ -3,7 +3,12 @@ #if LCD_DRIVER_HAS(ILI9341) #include "ILI9341.h" - // ILI9341 + + #define ILI9341_0_DEGREE_REG_VALUE 0X68 + #define ILI9341_180_DEGREE_REG_VALUE 0XA8 + #define ILI9341_90_DEGREE_REG_VALUE 0XC8 + #define ILI9341_270_DEGREE_REG_VALUE 0X08 + void ILI9341_Init_Sequential(void) { Delay_ms(50); // delay 50 ms diff --git a/TFT/src/User/Hal/LCD_Driver/ILI9341.h b/TFT/src/User/Hal/LCD_Driver/ILI9341.h index 33d4d5fae0..45cb15b6ac 100644 --- a/TFT/src/User/Hal/LCD_Driver/ILI9341.h +++ b/TFT/src/User/Hal/LCD_Driver/ILI9341.h @@ -5,10 +5,7 @@ extern "C" { #endif -#define ILI9341_0_DEGREE_REG_VALUE 0X68 -#define ILI9341_180_DEGREE_REG_VALUE 0XA8 -#define ILI9341_90_DEGREE_REG_VALUE 0XC8 -#define ILI9341_270_DEGREE_REG_VALUE 0X08 +#include uint8_t LCD_DriveIsILI9341(void); void ILI9341_Init_Sequential(void); diff --git a/TFT/src/User/Hal/LCD_Driver/ILI9488.c b/TFT/src/User/Hal/LCD_Driver/ILI9488.c index 1cfc5d6bb5..6203f76b29 100644 --- a/TFT/src/User/Hal/LCD_Driver/ILI9488.c +++ b/TFT/src/User/Hal/LCD_Driver/ILI9488.c @@ -3,7 +3,19 @@ #if LCD_DRIVER_HAS(ILI9488) #include "ILI9488.h" - // ILI9488 + + #ifdef MKS_TFT35_V1_0 // rotate display 180 degrees by default for MKS_TFT35_V1_0 + #define ILI9488_180_DEGREE_REG_VALUE 0X28 + #define ILI9488_0_DEGREE_REG_VALUE 0XE8 + #define ILI9488_90_DEGREE_REG_VALUE 0X88 + #define ILI9488_270_DEGREE_REG_VALUE 0X48 + #else + #define ILI9488_0_DEGREE_REG_VALUE 0X28 + #define ILI9488_180_DEGREE_REG_VALUE 0XE8 + #define ILI9488_90_DEGREE_REG_VALUE 0X88 + #define ILI9488_270_DEGREE_REG_VALUE 0X48 + #endif + uint8_t LCD_DriveIsILI9488(void) { uint16_t id = 0; diff --git a/TFT/src/User/Hal/LCD_Driver/ILI9488.h b/TFT/src/User/Hal/LCD_Driver/ILI9488.h index cdc090377d..c6a53065d6 100644 --- a/TFT/src/User/Hal/LCD_Driver/ILI9488.h +++ b/TFT/src/User/Hal/LCD_Driver/ILI9488.h @@ -5,17 +5,7 @@ extern "C" { #endif -#ifdef MKS_TFT35_V1_0 // rotate display 180 degrees by default for MKS_TFT35_V1_0 - #define ILI9488_180_DEGREE_REG_VALUE 0X28 - #define ILI9488_0_DEGREE_REG_VALUE 0XE8 - #define ILI9488_90_DEGREE_REG_VALUE 0X88 - #define ILI9488_270_DEGREE_REG_VALUE 0X48 -#else - #define ILI9488_0_DEGREE_REG_VALUE 0X28 - #define ILI9488_180_DEGREE_REG_VALUE 0XE8 - #define ILI9488_90_DEGREE_REG_VALUE 0X88 - #define ILI9488_270_DEGREE_REG_VALUE 0X48 -#endif +#include uint8_t LCD_DriveIsILI9488(void); void ILI9488_Init_Sequential(void); diff --git a/TFT/src/User/Hal/LCD_Driver/NT35310.c b/TFT/src/User/Hal/LCD_Driver/NT35310.c index ce5e35c1c4..422876f2bd 100644 --- a/TFT/src/User/Hal/LCD_Driver/NT35310.c +++ b/TFT/src/User/Hal/LCD_Driver/NT35310.c @@ -3,7 +3,12 @@ #if LCD_DRIVER_HAS(NT35310) #include "NT35310.h" - // NT35310 + + #define NT35310_0_DEGREE_REG_VALUE 0X60 + #define NT35310_180_DEGREE_REG_VALUE 0XA0 + #define NT35310_90_DEGREE_REG_VALUE 0XC0 + #define NT35310_270_DEGREE_REG_VALUE 0X00 + uint8_t LCD_DriveIsNT35310(void) { uint16_t id = 0; diff --git a/TFT/src/User/Hal/LCD_Driver/NT35310.h b/TFT/src/User/Hal/LCD_Driver/NT35310.h index 8fe3138ad8..4bd405d8b0 100644 --- a/TFT/src/User/Hal/LCD_Driver/NT35310.h +++ b/TFT/src/User/Hal/LCD_Driver/NT35310.h @@ -5,10 +5,7 @@ extern "C" { #endif -#define NT35310_0_DEGREE_REG_VALUE 0X60 -#define NT35310_180_DEGREE_REG_VALUE 0XA0 -#define NT35310_90_DEGREE_REG_VALUE 0XC0 -#define NT35310_270_DEGREE_REG_VALUE 0X00 +#include uint8_t LCD_DriveIsNT35310(void); void NT35310_Init_Sequential(void); diff --git a/TFT/src/User/Hal/LCD_Driver/RM68042.c b/TFT/src/User/Hal/LCD_Driver/RM68042.c index c2688409ed..b9d28563c9 100644 --- a/TFT/src/User/Hal/LCD_Driver/RM68042.c +++ b/TFT/src/User/Hal/LCD_Driver/RM68042.c @@ -3,7 +3,12 @@ #if LCD_DRIVER_HAS(RM68042) #include "RM68042.h" - // RM68042 + + #define RM68042_0_DEGREE_REG_VALUE 0X28 + #define RM68042_180_DEGREE_REG_VALUE 0X2B + #define RM68042_90_DEGREE_REG_VALUE 0x09 + #define RM68042_270_DEGREE_REG_VALUE 0x0A + void RM68042_Init_Sequential(void) { LCD_WR_REG(0X11); diff --git a/TFT/src/User/Hal/LCD_Driver/RM68042.h b/TFT/src/User/Hal/LCD_Driver/RM68042.h index 9b5f787962..4bb1bbb37f 100644 --- a/TFT/src/User/Hal/LCD_Driver/RM68042.h +++ b/TFT/src/User/Hal/LCD_Driver/RM68042.h @@ -5,10 +5,7 @@ extern "C" { #endif -#define RM68042_0_DEGREE_REG_VALUE 0X28 -#define RM68042_180_DEGREE_REG_VALUE 0X2B -#define RM68042_90_DEGREE_REG_VALUE 0x09 -#define RM68042_270_DEGREE_REG_VALUE 0x0A +#include uint8_t LCD_DriveIsRM68042(void); void RM68042_Init_Sequential(void); diff --git a/TFT/src/User/Hal/LCD_Driver/SSD1963.c b/TFT/src/User/Hal/LCD_Driver/SSD1963.c index d0bb3ee2c6..eb763fcf50 100644 --- a/TFT/src/User/Hal/LCD_Driver/SSD1963.c +++ b/TFT/src/User/Hal/LCD_Driver/SSD1963.c @@ -3,6 +3,20 @@ #if LCD_DRIVER_HAS(SSD1963) #include "SSD1963.h" + + #define SSD1963_0_DEGREE_REG_VALUE 0x00 + #define SSD1963_180_DEGREE_REG_VALUE 0x03 + #define SSD1963_90_DEGREE_REG_VALUE 0x21 + #define SSD1963_270_DEGREE_REG_VALUE 0x22 + + #ifdef PORTRAIT_MODE + #define SSD1963_CMD_SET_X 0x2B + #define SSD1963_CMD_SET_Y 0x2A + #else + #define SSD1963_CMD_SET_X 0x2A + #define SSD1963_CMD_SET_Y 0x2B + #endif + // SSD1963 resolution max:864*480 #define SSD_HOR_RESOLUTION LCD_HARDWARE_WIDTH // LCD width pixel #define SSD_VER_RESOLUTION LCD_HARDWARE_HEIGHT // LCD height pixel diff --git a/TFT/src/User/Hal/LCD_Driver/SSD1963.h b/TFT/src/User/Hal/LCD_Driver/SSD1963.h index 7e772277fa..318013df8a 100644 --- a/TFT/src/User/Hal/LCD_Driver/SSD1963.h +++ b/TFT/src/User/Hal/LCD_Driver/SSD1963.h @@ -5,18 +5,7 @@ extern "C" { #endif -#define SSD1963_0_DEGREE_REG_VALUE 0x00 -#define SSD1963_180_DEGREE_REG_VALUE 0x03 -#define SSD1963_90_DEGREE_REG_VALUE 0x21 -#define SSD1963_270_DEGREE_REG_VALUE 0x22 - -#ifdef PORTRAIT_MODE - #define SSD1963_CMD_SET_X 0x2B - #define SSD1963_CMD_SET_Y 0x2A -#else - #define SSD1963_CMD_SET_X 0x2A - #define SSD1963_CMD_SET_Y 0x2B -#endif +#include uint8_t LCD_DriveIsSSD1963(void); void SSD1963_Init_Sequential(void); diff --git a/TFT/src/User/Hal/LCD_Driver/ST7789.c b/TFT/src/User/Hal/LCD_Driver/ST7789.c index abd4069a13..40431c0df9 100644 --- a/TFT/src/User/Hal/LCD_Driver/ST7789.c +++ b/TFT/src/User/Hal/LCD_Driver/ST7789.c @@ -3,7 +3,12 @@ #if LCD_DRIVER_HAS(ST7789) #include "ST7789.h" - // ST7789 + + #define ST7789_0_DEGREE_REG_VALUE 0X60 + #define ST7789_180_DEGREE_REG_VALUE 0XA0 + #define ST7789_90_DEGREE_REG_VALUE 0XC0 + #define ST7789_270_DEGREE_REG_VALUE 0X00 + void ST7789_Init_Sequential(void) { LCD_WR_REG(0x11); diff --git a/TFT/src/User/Hal/LCD_Driver/ST7789.h b/TFT/src/User/Hal/LCD_Driver/ST7789.h index 029315b2ab..fc087c74fa 100644 --- a/TFT/src/User/Hal/LCD_Driver/ST7789.h +++ b/TFT/src/User/Hal/LCD_Driver/ST7789.h @@ -5,10 +5,7 @@ extern "C" { #endif -#define ST7789_0_DEGREE_REG_VALUE 0X60 -#define ST7789_180_DEGREE_REG_VALUE 0XA0 -#define ST7789_90_DEGREE_REG_VALUE 0XC0 -#define ST7789_270_DEGREE_REG_VALUE 0X00 +#include uint8_t LCD_DriveIsST7789(void); void ST7789_Init_Sequential(void); diff --git a/TFT/src/User/Hal/LCD_Driver/ST7796S.c b/TFT/src/User/Hal/LCD_Driver/ST7796S.c index e0c438d4b6..eeddc85fa9 100644 --- a/TFT/src/User/Hal/LCD_Driver/ST7796S.c +++ b/TFT/src/User/Hal/LCD_Driver/ST7796S.c @@ -3,7 +3,12 @@ #if LCD_DRIVER_HAS(ST7796S) #include "ST7796S.h" - // ST7796S + + #define ST7796S_0_DEGREE_REG_VALUE 0X28 + #define ST7796S_180_DEGREE_REG_VALUE 0XE8 + #define ST7796S_90_DEGREE_REG_VALUE 0X88 + #define ST7796S_270_DEGREE_REG_VALUE 0X48 + uint8_t LCD_DriveIsST7796S(void) { uint16_t id = 0; diff --git a/TFT/src/User/Hal/LCD_Driver/ST7796S.h b/TFT/src/User/Hal/LCD_Driver/ST7796S.h index 4d15c41b38..32c40d85ba 100644 --- a/TFT/src/User/Hal/LCD_Driver/ST7796S.h +++ b/TFT/src/User/Hal/LCD_Driver/ST7796S.h @@ -5,10 +5,7 @@ extern "C" { #endif -#define ST7796S_0_DEGREE_REG_VALUE 0X28 -#define ST7796S_180_DEGREE_REG_VALUE 0XE8 -#define ST7796S_90_DEGREE_REG_VALUE 0X88 -#define ST7796S_270_DEGREE_REG_VALUE 0X48 +#include uint8_t LCD_DriveIsST7796S(void); void ST7796S_Init_Sequential(void); diff --git a/TFT/src/User/Hal/LCD_Encoder.c b/TFT/src/User/Hal/LCD_Encoder.c index 09cef9aea8..d198e62a1b 100644 --- a/TFT/src/User/Hal/LCD_Encoder.c +++ b/TFT/src/User/Hal/LCD_Encoder.c @@ -25,13 +25,14 @@ void LCD_Enc_SetActiveSignal(uint8_t status) #define B01 1 #define B10 2 +static volatile int8_t encoderDiff; // updated in LCD_Enc_CheckSteps(), added to encoderPosition at every LCD update static uint8_t encoderLastState = 0; static uint8_t encoderLastSteps = 0; -volatile int8_t encoderDiff; // updated in LCD_Enc_CheckSteps(), added to encoderPosition at every LCD update -int8_t encoderDirection = 1; +static int8_t encoderDirection = 1; + int16_t encoderPosition = 0; -bool LCD_Enc_ReadStep(uint16_t io_pin) +static bool LCD_Enc_ReadStep(uint16_t io_pin) { return !GPIO_GetLevel(io_pin); } @@ -138,7 +139,7 @@ void LCD_Enc_SendPulse(uint8_t num) LCD_Enc_Init(); } -bool LCD_Enc_CheckState() +bool LCD_Enc_CheckState(void) { if (LCD_Enc_ReadBtn(LCD_ENC_BUTTON_INTERVAL) || LCD_Enc_ReadPos() != encoderLastState) { diff --git a/TFT/src/User/Hal/LCD_Encoder.h b/TFT/src/User/Hal/LCD_Encoder.h index 3415f94c34..6b509cb963 100644 --- a/TFT/src/User/Hal/LCD_Encoder.h +++ b/TFT/src/User/Hal/LCD_Encoder.h @@ -6,8 +6,8 @@ extern "C" { #endif #include -#include -#include "variants.h" // for ENC_ACTIVE_SIGNAL, LCD_ENCODER_SUPPORT etc... +#include // for uint8_t etc. +#include "variants.h" // for ENC_ACTIVE_SIGNAL, LCD_ENCODER_SUPPORT etc. #include "menu.h" #if ENC_ACTIVE_SIGNAL diff --git a/TFT/src/User/Hal/LCD_Init.c b/TFT/src/User/Hal/LCD_Init.c index 60376785d9..aca540f566 100644 --- a/TFT/src/User/Hal/LCD_Init.c +++ b/TFT/src/User/Hal/LCD_Init.c @@ -1,5 +1,5 @@ #include "LCD_Init.h" -#include "includes.h" // for RM68042..., lcd.h, lcd_dma.h, timer_pwm.h etc... +#include "includes.h" // for RM68042..., lcd.h, lcd_dma.h, timer_pwm.h etc. #include "GPIO_Init.h" // LCD driver sequential @@ -31,12 +31,12 @@ #include "LCD_Driver/ST7796S.h" #endif -void (*pLCD_SetDirection)(uint8_t rotate); -void (*pLCD_SetWindow)(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey); +static void (* pLCD_SetDirection)(uint8_t rotate); +static void (* pLCD_SetWindow)(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey); #ifdef LCD_LED_PIN -void LCD_LED_On() +static inline void LCD_LED_On(void) { #ifdef LCD_LED_PWM_CHANNEL LCD_SET_BRIGHTNESS(100); @@ -45,7 +45,7 @@ void LCD_LED_On() #endif } -void LCD_LED_Off() +static inline void LCD_LED_Off(void) { #ifdef LCD_LED_PWM_CHANNEL LCD_SET_BRIGHTNESS(0); @@ -54,7 +54,7 @@ void LCD_LED_Off() #endif } -void LCD_LED_Init(void) +static inline void LCD_LED_Init(void) { #ifdef LCD_LED_PWM_CHANNEL GPIO_InitSet(LCD_LED_PIN, MGPIO_MODE_AF_PP, LCD_LED_PIN_ALTERNATE); @@ -69,7 +69,7 @@ void LCD_LED_Init(void) #ifdef SCREEN_SHOT_TO_SD -uint32_t (*pLCD_ReadPixel_24Bit)(int16_t x, int16_t y); +static uint32_t (* pLCD_ReadPixel_24Bit)(int16_t x, int16_t y); uint32_t LCD_ReadPixel_24Bit(int16_t x, int16_t y) { @@ -78,7 +78,7 @@ uint32_t LCD_ReadPixel_24Bit(int16_t x, int16_t y) #endif // SCREEN_SHOT_TO_SD -void LCD_Init_Sequential(void) +static inline void LCD_Init_Sequential(void) { #if LCD_DRIVER_IS(RM68042) RM68042_Init_Sequential(); diff --git a/TFT/src/User/Hal/LCD_Init.h b/TFT/src/User/Hal/LCD_Init.h index a7d59720ba..212861144f 100644 --- a/TFT/src/User/Hal/LCD_Init.h +++ b/TFT/src/User/Hal/LCD_Init.h @@ -5,7 +5,8 @@ extern "C" { #endif -#include "variants.h" // for LCD_DATA_16BIT, LCD_LED_PWM_CHANNEL etc... +#include // for uint8_t etc. +#include "variants.h" // for LCD_DATA_16BIT, LCD_LED_PWM_CHANNEL etc. #if LCD_DATA_16BIT == 1 #define LCD_WR_16BITS_DATA(c) do{ LCD_WR_DATA(c); }while(0) diff --git a/TFT/src/User/Hal/STM32Fxx_Pins.h b/TFT/src/User/Hal/STM32Fxx_Pins.h index 363c5dddfd..997b9904e0 100644 --- a/TFT/src/User/Hal/STM32Fxx_Pins.h +++ b/TFT/src/User/Hal/STM32Fxx_Pins.h @@ -204,7 +204,6 @@ extern "C" { #define PK14 ((_GPIOK_MAP<<8) | 14) #define PK15 ((_GPIOK_MAP<<8) | 15) - #define GPIO_GET_PORT(n) (((n)>>8) & 0xFF) #define GPIO_GET_PIN(n) ((n) & 0xFF) diff --git a/TFT/src/User/Hal/buzzer.c b/TFT/src/User/Hal/buzzer.c index 1f0bfc6bd7..4fa79e7cba 100644 --- a/TFT/src/User/Hal/buzzer.c +++ b/TFT/src/User/Hal/buzzer.c @@ -1,5 +1,5 @@ #include "buzzer.h" -#include "includes.h" // for mcuClocks etc... +#include "includes.h" // for mcuClocks etc. #include "GPIO_Init.h" #ifdef BUZZER_PIN @@ -18,7 +18,7 @@ typedef volatile struct static BUZZER buzzer; -void Buzzer_ConfigTimer(void) +static inline void Buzzer_ConfigTimer(void) { #if defined(GD32F2XX) || defined(GD32F3XX) rcu_periph_clock_enable(RCU_TIMER2); // enable timer clock @@ -65,7 +65,7 @@ void Buzzer_DeConfig(void) } // play a tone with the help of interrupts -void Buzzer_PlaySound(uint16_t frequency, const uint16_t duration) +static inline void Buzzer_PlaySound(uint16_t frequency, const uint16_t duration) { uint32_t silence = (frequency == SILENCE_FREQ); // frequency == 0 indicates silence/pause in the sound @@ -95,7 +95,7 @@ void Buzzer_PlaySound(uint16_t frequency, const uint16_t duration) } // play a sound from sound queue. Called by timer interrupt and Buzzer_AddSound() function -void Buzzer_GetSound(void) +static void Buzzer_GetSound(void) { // stop timer #if defined(GD32F2XX) || defined(GD32F3XX) diff --git a/TFT/src/User/Hal/buzzer.h b/TFT/src/User/Hal/buzzer.h index b9164df5bc..b2b8e3dbbd 100644 --- a/TFT/src/User/Hal/buzzer.h +++ b/TFT/src/User/Hal/buzzer.h @@ -6,7 +6,7 @@ extern "C" { #endif #include -#include "variants.h" // for BUZZER_PIN etc... +#include "variants.h" // for BUZZER_PIN etc. #ifdef BUZZER_PIN void Buzzer_Config(void); diff --git a/TFT/src/User/Hal/gd32f20x/GPIO_Init.c b/TFT/src/User/Hal/gd32f20x/GPIO_Init.c index 49ffe37030..2e85091848 100644 --- a/TFT/src/User/Hal/gd32f20x/GPIO_Init.c +++ b/TFT/src/User/Hal/gd32f20x/GPIO_Init.c @@ -1,21 +1,139 @@ #include "GPIO_Init.h" -/* -* bit 0:1 gpio mode 00: Input (reset state) 01: General purpose output mode 10: Alternate function mode 11: Analog mode -* bit 2 gpio output type 0: Output push-pull (reset state) 1: Output open-drain -* bit 3:4 gpio output speed 00: Low speed 01: Medium speed 10: High speed 11: Very high speed -* bit 5:6 gpio pull-up/pull-down 00: No pull-up, pull-down 01: Pull-up 10: Pull-down 11: Reserved -* bit 7 reserved -*/ -const uint32_t GPIO_Port[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG}; -const rcu_periph_enum RCU_Port[] = {RCU_GPIOA, RCU_GPIOB, RCU_GPIOC, RCU_GPIOD, RCU_GPIOE, RCU_GPIOF, RCU_GPIOG}; +#define GPIO_MODE_GET_MODE(n) ((n) & 0x3) +#define GPIO_MODE_GET_OTYPE(n) (((n)>>2) & 0x1) +#define GPIO_MODE_GET_OSPEED(n) (((n)>>3) & 0x3) +#define GPIO_MODE_GET_PULL(n) (((n)>>5) & 0x3) + +#define GPIO_MODE_AF 2 +#define GPIO_AF0 0 +#define GPIO_AF1 1 +#define GPIO_AF2 2 +#define GPIO_AF3 3 +#define GPIO_AF4 4 +#define GPIO_AF5 5 +#define GPIO_AF6 6 +#define GPIO_AF7 7 +#define GPIO_AF8 8 +#define GPIO_AF9 9 +#define GPIO_AF10 10 +#define GPIO_AF11 11 +#define GPIO_AF12 12 +#define GPIO_AF13 13 +#define GPIO_AF14 14 +#define GPIO_AF15 15 + +#ifndef GPIO_AF_RTC_50Hz + /** + * @brief AF 0 selection + */ + #define GPIO_AF_RTC_50Hz ((uint8_t)0x00) /* RTC_50Hz Alternate Function mapping */ + #define GPIO_AF_MCO ((uint8_t)0x00) /* MCO (MCO1 and MCO2) Alternate Function mapping */ + #define GPIO_AF_TAMPER ((uint8_t)0x00) /* TAMPER (TAMPER_1 and TAMPER_2) Alternate Function mapping */ + #define GPIO_AF_SWJ ((uint8_t)0x00) /* SWJ (SWD and JTAG) Alternate Function mapping */ + #define GPIO_AF_TRACE ((uint8_t)0x00) /* TRACE Alternate Function mapping */ + + /** + * @brief AF 1 selection + */ + #define GPIO_AF_TIM1 ((uint8_t)0x01) /* TIM1 Alternate Function mapping */ + #define GPIO_AF_TIM2 ((uint8_t)0x01) /* TIM2 Alternate Function mapping */ + + /** + * @brief AF 2 selection + */ + #define GPIO_AF_TIM3 ((uint8_t)0x02) /* TIM3 Alternate Function mapping */ + #define GPIO_AF_TIM4 ((uint8_t)0x02) /* TIM4 Alternate Function mapping */ + #define GPIO_AF_TIM5 ((uint8_t)0x02) /* TIM5 Alternate Function mapping */ + + /** + * @brief AF 3 selection + */ + #define GPIO_AF_TIM8 ((uint8_t)0x03) /* TIM8 Alternate Function mapping */ + #define GPIO_AF_TIM9 ((uint8_t)0x03) /* TIM9 Alternate Function mapping */ + #define GPIO_AF_TIM10 ((uint8_t)0x03) /* TIM10 Alternate Function mapping */ + #define GPIO_AF_TIM11 ((uint8_t)0x03) /* TIM11 Alternate Function mapping */ + + /** + * @brief AF 4 selection + */ + #define GPIO_AF_I2C1 ((uint8_t)0x04) /* I2C1 Alternate Function mapping */ + #define GPIO_AF_I2C2 ((uint8_t)0x04) /* I2C2 Alternate Function mapping */ + #define GPIO_AF_I2C3 ((uint8_t)0x04) /* I2C3 Alternate Function mapping */ + + /** + * @brief AF 5 selection + */ + #define GPIO_AF_SPI1 ((uint8_t)0x05) /* SPI1 Alternate Function mapping */ + #define GPIO_AF_SPI2 ((uint8_t)0x05) /* SPI2/I2S2 Alternate Function mapping */ + + /** + * @brief AF 6 selection + */ + #define GPIO_AF_SPI3 ((uint8_t)0x06) /* SPI3/I2S3 Alternate Function mapping */ + + /** + * @brief AF 7 selection + */ + #define GPIO_AF_USART1 ((uint8_t)0x07) /* USART1 Alternate Function mapping */ + #define GPIO_AF_USART2 ((uint8_t)0x07) /* USART2 Alternate Function mapping */ + #define GPIO_AF_USART3 ((uint8_t)0x07) /* USART3 Alternate Function mapping */ + + /** + * @brief AF 8 selection + */ + #define GPIO_AF_UART4 ((uint8_t)0x08) /* UART4 Alternate Function mapping */ + #define GPIO_AF_UART5 ((uint8_t)0x08) /* UART5 Alternate Function mapping */ + #define GPIO_AF_USART6 ((uint8_t)0x08) /* USART6 Alternate Function mapping */ + + /** + * @brief AF 9 selection + */ + #define GPIO_AF_CAN1 ((uint8_t)0x09) /* CAN1 Alternate Function mapping */ + #define GPIO_AF_CAN2 ((uint8_t)0x09) /* CAN2 Alternate Function mapping */ + #define GPIO_AF_TIM12 ((uint8_t)0x09) /* TIM12 Alternate Function mapping */ + #define GPIO_AF_TIM13 ((uint8_t)0x09) /* TIM13 Alternate Function mapping */ + #define GPIO_AF_TIM14 ((uint8_t)0x09) /* TIM14 Alternate Function mapping */ + + /** + * @brief AF 10 selection + */ + #define GPIO_AF_OTG_FS ((uint8_t)0xA) /* OTG_FS Alternate Function mapping */ + #define GPIO_AF_OTG_HS ((uint8_t)0xA) /* OTG_HS Alternate Function mapping */ + + /** + * @brief AF 11 selection + */ + #define GPIO_AF_ETH ((uint8_t)0x0B) /* ETHERNET Alternate Function mapping */ + + /** + * @brief AF 12 selection + */ + #define GPIO_AF_FSMC ((uint8_t)0xC) /* FSMC Alternate Function mapping */ + #define GPIO_AF_OTG_HS_FS ((uint8_t)0xC) /* OTG HS configured in FS, Alternate Function mapping */ + #define GPIO_AF_SDIO ((uint8_t)0xC) /* SDIO Alternate Function mapping */ + + /** + * @brief AF 13 selection + */ + #define GPIO_AF_DCMI ((uint8_t)0x0D) /* DCMI Alternate Function mapping */ + + /** + * @brief AF 15 selection + */ + #define GPIO_AF_EVENTOUT ((uint8_t)0x0F) /* EVENTOUT Alternate Function mapping */ + +#endif + +static const uint32_t GPIO_Port[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG}; +static const rcu_periph_enum RCU_Port[] = {RCU_GPIOA, RCU_GPIOB, RCU_GPIOC, RCU_GPIOD, RCU_GPIOE, RCU_GPIOF, RCU_GPIOG}; void GPIO_InitSet(uint16_t io, GPIO_MODE mode, uint32_t remap) { uint16_t port = GPIO_GET_PORT(io); uint16_t pin = GPIO_GET_PIN(io); - // RCU_REG_VAL(RCU_Port[port]) |= BIT(RCU_BIT_POS(RCU_Port[port])); // Enable GPIO Clock + // RCU_REG_VAL(RCU_Port[port]) |= BIT(RCU_BIT_POS(RCU_Port[port])); // enable GPIO Clock rcu_periph_clock_enable(RCU_Port[port]); rcu_periph_clock_enable(RCU_AF); @@ -41,16 +159,10 @@ void GPIO_ToggleLevel(uint16_t io) uint16_t port = GPIO_GET_PORT(io); uint16_t pin = GPIO_GET_PIN(io); - - if((uint32_t)RESET !=(GPIO_OCTL(GPIO_Port[port])&(1< +#include "variants.h" // for GPIO_MODE_IN_FLOATING etc. #include "STM32Fxx_Pins.h" /* -* bit 0:1 gpio mode 00: Input (reset state) 01: General purpose output mode 10: Alternate function mode 11: Analog mode -* bit 2 gpio output type 0: Output push-pull (reset state) 1: Output open-drain -* bit 3:4 gpio output speed 00: Low speed 01: Medium speed 10: High speed 11: Very high speed -* bit 5:6 gpio pull-up/pull-down 00: No pull-up, pull-down 01: Pull-up 10: Pull-down 11: Reserved -* bit 7 reserved -*/ - + * bit 0:1 gpio mode 00: Input (reset state) 01: General purpose output mode 10: Alternate function mode 11: Analog mode + * bit 2 gpio output type 0: Output push-pull (reset state) 1: Output open-drain + * bit 3:4 gpio output speed 00: Low speed 01: Medium speed 10: High speed 11: Very high speed + * bit 5:6 gpio pull-up/pull-down 00: No pull-up, pull-down 01: Pull-up 10: Pull-down 11: Reserved + * bit 7 reserved + */ typedef enum { - MGPIO_MODE_IPN = GPIO_MODE_IN_FLOATING, - MGPIO_MODE_IPU = GPIO_MODE_IPU, - MGPIO_MODE_IPD = GPIO_MODE_IPD, + MGPIO_MODE_IPN = GPIO_MODE_IN_FLOATING, + MGPIO_MODE_IPU = GPIO_MODE_IPU, + MGPIO_MODE_IPD = GPIO_MODE_IPD, MGPIO_MODE_OUT_PP = GPIO_MODE_OUT_PP, MGPIO_MODE_OUT_OD = GPIO_MODE_OUT_OD, - MGPIO_MODE_AF_PP = GPIO_MODE_AF_PP, - MGPIO_MODE_AF_OD = GPIO_MODE_AF_OD, - MGPIO_MODE_AIN = GPIO_MODE_AIN, + MGPIO_MODE_AF_PP = GPIO_MODE_AF_PP, + MGPIO_MODE_AF_OD = GPIO_MODE_AF_OD, + MGPIO_MODE_AIN = GPIO_MODE_AIN, } GPIO_MODE; - -#define GPIO_MODE_GET_MODE(n) ((n) & 0x3) -#define GPIO_MODE_GET_OTYPE(n) (((n)>>2) & 0x1) -#define GPIO_MODE_GET_OSPEED(n) (((n)>>3) & 0x3) -#define GPIO_MODE_GET_PULL(n) (((n)>>5) & 0x3) - -#define GPIO_MODE_AF 2 -#define GPIO_AF0 0 -#define GPIO_AF1 1 -#define GPIO_AF2 2 -#define GPIO_AF3 3 -#define GPIO_AF4 4 -#define GPIO_AF5 5 -#define GPIO_AF6 6 -#define GPIO_AF7 7 -#define GPIO_AF8 8 -#define GPIO_AF9 9 -#define GPIO_AF10 10 -#define GPIO_AF11 11 -#define GPIO_AF12 12 -#define GPIO_AF13 13 -#define GPIO_AF14 14 -#define GPIO_AF15 15 - -#ifndef GPIO_AF_RTC_50Hz - /** - * @brief AF 0 selection - */ - #define GPIO_AF_RTC_50Hz ((uint8_t)0x00) /* RTC_50Hz Alternate Function mapping */ - #define GPIO_AF_MCO ((uint8_t)0x00) /* MCO (MCO1 and MCO2) Alternate Function mapping */ - #define GPIO_AF_TAMPER ((uint8_t)0x00) /* TAMPER (TAMPER_1 and TAMPER_2) Alternate Function mapping */ - #define GPIO_AF_SWJ ((uint8_t)0x00) /* SWJ (SWD and JTAG) Alternate Function mapping */ - #define GPIO_AF_TRACE ((uint8_t)0x00) /* TRACE Alternate Function mapping */ - - /** - * @brief AF 1 selection - */ - #define GPIO_AF_TIM1 ((uint8_t)0x01) /* TIM1 Alternate Function mapping */ - #define GPIO_AF_TIM2 ((uint8_t)0x01) /* TIM2 Alternate Function mapping */ - - /** - * @brief AF 2 selection - */ - #define GPIO_AF_TIM3 ((uint8_t)0x02) /* TIM3 Alternate Function mapping */ - #define GPIO_AF_TIM4 ((uint8_t)0x02) /* TIM4 Alternate Function mapping */ - #define GPIO_AF_TIM5 ((uint8_t)0x02) /* TIM5 Alternate Function mapping */ - - /** - * @brief AF 3 selection - */ - #define GPIO_AF_TIM8 ((uint8_t)0x03) /* TIM8 Alternate Function mapping */ - #define GPIO_AF_TIM9 ((uint8_t)0x03) /* TIM9 Alternate Function mapping */ - #define GPIO_AF_TIM10 ((uint8_t)0x03) /* TIM10 Alternate Function mapping */ - #define GPIO_AF_TIM11 ((uint8_t)0x03) /* TIM11 Alternate Function mapping */ - - /** - * @brief AF 4 selection - */ - #define GPIO_AF_I2C1 ((uint8_t)0x04) /* I2C1 Alternate Function mapping */ - #define GPIO_AF_I2C2 ((uint8_t)0x04) /* I2C2 Alternate Function mapping */ - #define GPIO_AF_I2C3 ((uint8_t)0x04) /* I2C3 Alternate Function mapping */ - - /** - * @brief AF 5 selection - */ - #define GPIO_AF_SPI1 ((uint8_t)0x05) /* SPI1 Alternate Function mapping */ - #define GPIO_AF_SPI2 ((uint8_t)0x05) /* SPI2/I2S2 Alternate Function mapping */ - - /** - * @brief AF 6 selection - */ - #define GPIO_AF_SPI3 ((uint8_t)0x06) /* SPI3/I2S3 Alternate Function mapping */ - - /** - * @brief AF 7 selection - */ - #define GPIO_AF_USART1 ((uint8_t)0x07) /* USART1 Alternate Function mapping */ - #define GPIO_AF_USART2 ((uint8_t)0x07) /* USART2 Alternate Function mapping */ - #define GPIO_AF_USART3 ((uint8_t)0x07) /* USART3 Alternate Function mapping */ - - /** - * @brief AF 8 selection - */ - #define GPIO_AF_UART4 ((uint8_t)0x08) /* UART4 Alternate Function mapping */ - #define GPIO_AF_UART5 ((uint8_t)0x08) /* UART5 Alternate Function mapping */ - #define GPIO_AF_USART6 ((uint8_t)0x08) /* USART6 Alternate Function mapping */ - - /** - * @brief AF 9 selection - */ - #define GPIO_AF_CAN1 ((uint8_t)0x09) /* CAN1 Alternate Function mapping */ - #define GPIO_AF_CAN2 ((uint8_t)0x09) /* CAN2 Alternate Function mapping */ - #define GPIO_AF_TIM12 ((uint8_t)0x09) /* TIM12 Alternate Function mapping */ - #define GPIO_AF_TIM13 ((uint8_t)0x09) /* TIM13 Alternate Function mapping */ - #define GPIO_AF_TIM14 ((uint8_t)0x09) /* TIM14 Alternate Function mapping */ - - /** - * @brief AF 10 selection - */ - #define GPIO_AF_OTG_FS ((uint8_t)0xA) /* OTG_FS Alternate Function mapping */ - #define GPIO_AF_OTG_HS ((uint8_t)0xA) /* OTG_HS Alternate Function mapping */ - - /** - * @brief AF 11 selection - */ - #define GPIO_AF_ETH ((uint8_t)0x0B) /* ETHERNET Alternate Function mapping */ - - /** - * @brief AF 12 selection - */ - #define GPIO_AF_FSMC ((uint8_t)0xC) /* FSMC Alternate Function mapping */ - #define GPIO_AF_OTG_HS_FS ((uint8_t)0xC) /* OTG HS configured in FS, Alternate Function mapping */ - #define GPIO_AF_SDIO ((uint8_t)0xC) /* SDIO Alternate Function mapping */ - - /** - * @brief AF 13 selection - */ - #define GPIO_AF_DCMI ((uint8_t)0x0D) /* DCMI Alternate Function mapping */ - - /** - * @brief AF 15 selection - */ - #define GPIO_AF_EVENTOUT ((uint8_t)0x0F) /* EVENTOUT Alternate Function mapping */ - -#endif - void GPIO_InitSet(uint16_t io, GPIO_MODE mode, uint32_t remap); void GPIO_SetLevel(uint16_t io, uint8_t level); void GPIO_ToggleLevel(uint16_t io); diff --git a/TFT/src/User/Hal/gd32f20x/HAL_Flash.c b/TFT/src/User/Hal/gd32f20x/HAL_Flash.c index ac757a3559..68dd8588a6 100644 --- a/TFT/src/User/Hal/gd32f20x/HAL_Flash.c +++ b/TFT/src/User/Hal/gd32f20x/HAL_Flash.c @@ -1,42 +1,48 @@ #include "HAL_Flash.h" +#include "variants.h" // for fmc_unlock etc. #include "my_misc.h" /* -Page 0 0x0800 0000 - 0x0800 07FF 2 Kbyte -Page 1 0x0800 0800 - 0x0800 0FFF 2 Kbyte -Page 2 0x0800 1000 - 0x0800 17FF 2 Kbyte -... -... -... -Page 255 0x0807 F800 - 0x0807 FFFF 2 Kbyte // 512KByte -*/ + * Page 0 0x0800 0000 - 0x0800 07FF 2 Kbyte + * Page 1 0x0800 0800 - 0x0800 0FFF 2 Kbyte + * Page 2 0x0800 1000 - 0x0800 17FF 2 Kbyte + * ... + * ... + * ... + * Page 255 0x0807 F800 - 0x0807 FFFF 2 Kbyte // 512KByte + */ #define SIGN_ADDRESS (0x08040000 - 0x800) // reserve the last page (2KB) to save user parameters -void HAL_FlashRead(uint8_t *data, uint32_t len) +void HAL_FlashRead(uint8_t * data, uint32_t len) { uint32_t i = 0; + for (i = 0; i < len; i++) { - data[i] = *(volatile uint8_t*)(SIGN_ADDRESS + i); + data[i] = *(volatile uint8_t *)(SIGN_ADDRESS + i); } } -void HAL_FlashWrite(uint8_t *data, uint32_t len) +void HAL_FlashWrite(uint8_t * data, uint32_t len) { uint32_t i = 0; + fmc_unlock(); fmc_page_erase(SIGN_ADDRESS); fmc_flag_clear(FMC_FLAG_BANK0_END); fmc_flag_clear(FMC_FLAG_BANK0_WPERR); fmc_flag_clear(FMC_FLAG_BANK0_PGERR); + for (i = 0; i < len; i += 2) { - uint16_t data16 = data[i] | (data[MIN(i+1, len-1)] << 8); // gd32f20x needs to write at least 16 bits at a time + uint16_t data16 = data[i] | (data[MIN(i + 1, len - 1)] << 8); // gd32f20x needs to write at least 16 bits at a time + fmc_halfword_program(SIGN_ADDRESS + i, data16); fmc_flag_clear(FMC_FLAG_BANK0_END); fmc_flag_clear(FMC_FLAG_BANK0_WPERR); fmc_flag_clear(FMC_FLAG_BANK0_PGERR); } + fmc_lock(); } diff --git a/TFT/src/User/Hal/gd32f20x/HAL_Flash.h b/TFT/src/User/Hal/gd32f20x/HAL_Flash.h index d37103fc08..88792034a2 100644 --- a/TFT/src/User/Hal/gd32f20x/HAL_Flash.h +++ b/TFT/src/User/Hal/gd32f20x/HAL_Flash.h @@ -1,9 +1,9 @@ #ifndef _HAL_FLASH_H_ #define _HAL_FLASH_H_ -#include "variants.h" // for uint8_t etc... +#include -void HAL_FlashRead(uint8_t *data, uint32_t len); -void HAL_FlashWrite(uint8_t *data, uint32_t len); +void HAL_FlashRead(uint8_t * data, uint32_t len); +void HAL_FlashWrite(uint8_t * data, uint32_t len); #endif diff --git a/TFT/src/User/Hal/gd32f20x/Serial.c b/TFT/src/User/Hal/gd32f20x/Serial.c index 0a67aebc01..e97f7ee053 100644 --- a/TFT/src/User/Hal/gd32f20x/Serial.c +++ b/TFT/src/User/Hal/gd32f20x/Serial.c @@ -29,7 +29,7 @@ const SERIAL_CFG Serial[_UART_CNT] = { }; // disable RX DMA and clear all interrupt flags for a serial port -void Serial_DMA_DisableAndClearFlagsRX(uint8_t port) +static void Serial_DMA_DisableAndClearFlagsRX(uint8_t port) { DMA_CHCTL(Serial[port].dma_stream, Serial[port].dma_channelRX) &= ~(1<<0); // disable RX DMA @@ -66,7 +66,7 @@ void Serial_DMA_DisableAndClearFlagsRX(uint8_t port) #ifdef TX_DMA_WRITE // disable TX DMA and clear all interrupt flags for a serial port -void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) +static void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) { DMA_CHCTL(Serial[port].dma_stream, Serial[port].dma_channelTX) &= ~(1<<0); // disable TX DMA @@ -102,7 +102,7 @@ void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) #endif -void Serial_DMA_Config(uint8_t port) +static inline void Serial_DMA_Config(uint8_t port) { const SERIAL_CFG * cfg = &Serial[port]; @@ -157,7 +157,7 @@ void Serial_DMA_Config(uint8_t port) DMA_CHCTL(cfg->dma_stream, cfg->dma_channelRX) |= (1<<0); // RX enable DMA } -void Serial_ClearData(uint8_t port) +static void Serial_ClearData(uint8_t port) { dmaL1DataRX[port].wIndex = dmaL1DataRX[port].rIndex = dmaL1DataRX[port].flag = dmaL1DataRX[port].cacheSize = 0; @@ -209,7 +209,7 @@ void Serial_DeConfig(uint8_t port) #ifdef TX_DMA_WRITE // TX DMA based serial writing // DMA serial write support function -void Serial_Send_TX(uint8_t port) +static void Serial_Send_TX(uint8_t port) { // setup DMA transfer, and wait for serial Transfer Complete (TX) interrupt in ISR if (dmaL1DataTX[port].wIndex >= dmaL1DataTX[port].rIndex) diff --git a/TFT/src/User/Hal/gd32f20x/Serial.h b/TFT/src/User/Hal/gd32f20x/Serial.h index 43041be815..171d838d9a 100644 --- a/TFT/src/User/Hal/gd32f20x/Serial.h +++ b/TFT/src/User/Hal/gd32f20x/Serial.h @@ -2,8 +2,8 @@ #define _SERIAL_H_ #include -#include "variants.h" // for uint32_t etc... -#include "uart.h" +#include "variants.h" // for rcu_periph_enum etc. +#include "uart.h" // for _UART_CNT etc. // comment out this line to use TX interrupt based serial writing instead of TX DMA based serial writing #define TX_DMA_WRITE diff --git a/TFT/src/User/Hal/gd32f20x/lcd.c b/TFT/src/User/Hal/gd32f20x/lcd.c index 4091bdf230..d2164b4373 100644 --- a/TFT/src/User/Hal/gd32f20x/lcd.c +++ b/TFT/src/User/Hal/gd32f20x/lcd.c @@ -7,26 +7,30 @@ uint16_t LCD_RD_DATA(void) { volatile uint16_t ram; + ram = LCD->LCD_RAM; + return ram; } -void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) +static void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) { LCD->LCD_REG = LCD_Reg; LCD->LCD_RAM = LCD_RegValue; } -uint16_t LCD_ReadReg(uint8_t LCD_Reg) +static uint16_t LCD_ReadReg(uint8_t LCD_Reg) { LCD_WR_REG(LCD_Reg); Delay_us(5); + return LCD_RD_DATA(); } -void LCD_GPIO_Config(void) +static inline void LCD_GPIO_Config(void) { rcu_periph_clock_enable(RCU_EXMC); + // fsmc 16bit data pins GPIO_InitSet(PD0, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PD1, MGPIO_MODE_AF_PP, 0); @@ -45,24 +49,25 @@ void LCD_GPIO_Config(void) GPIO_InitSet(PE14, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PE15, MGPIO_MODE_AF_PP, 0); - /*Configure the control line corresponding to FSMC - * PD4-FSMC_NOE :LCD-RD - * PD5-FSMC_NWE :LCD-WR - * PD7-FSMC_NE1 :LCD-CS - * PE2-FSMC_A23 :LCD-RS LCD-RS data or cmd - */ + /* + * configure the control line corresponding to FSMC + * PD4-FSMC_NOE :LCD-RD + * PD5-FSMC_NWE :LCD-WR + * PD7-FSMC_NE1 :LCD-CS + * PE2-FSMC_A23 :LCD-RS LCD-RS data or cmd + */ GPIO_InitSet(PD4, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PD5, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PE2, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PD7, MGPIO_MODE_AF_PP, 0); } -void LCD_EXMC_Config(void) +static inline void LCD_EXMC_Config(void) { exmc_norsram_parameter_struct EXMC_NORSRAMInitStructure; exmc_norsram_timing_parameter_struct readWriteTiming,writeTiming; - /* EXMC configuration */ + // EXMC configuration readWriteTiming.asyn_address_setuptime = 1U; readWriteTiming.asyn_address_holdtime = 0U; readWriteTiming.asyn_data_setuptime = 15U; @@ -96,7 +101,7 @@ void LCD_EXMC_Config(void) EXMC_NORSRAMInitStructure.write_timing = &writeTiming; exmc_norsram_init(&EXMC_NORSRAMInitStructure); - /* enable EXMC SARM bank0 */ + // enable EXMC SRAM bank0 exmc_norsram_enable(EXMC_BANK0_NORSRAM_REGION0); } diff --git a/TFT/src/User/Hal/gd32f20x/lcd.h b/TFT/src/User/Hal/gd32f20x/lcd.h index 9d9150cb53..d8afc693cb 100644 --- a/TFT/src/User/Hal/gd32f20x/lcd.h +++ b/TFT/src/User/Hal/gd32f20x/lcd.h @@ -2,7 +2,7 @@ #define _LCD_H_ #include -#include "variants.h" // for STM32_HAS_FSMC etc... +#include "variants.h" // for STM32_HAS_FSMC etc. #ifdef STM32_HAS_FSMC @@ -12,8 +12,8 @@ volatile uint16_t LCD_RAM; } LCD_TypeDef; - #define LCD_BASE ((uint32_t)(0x60000000 | 0x00FFFFFE)) // 1111 1111 1111 1111 1111 1110 - #define LCD ((LCD_TypeDef *) LCD_BASE) + #define LCD_BASE ((uint32_t)(0x60000000 | 0x00FFFFFE)) // 1111 1111 1111 1111 1111 1110 + #define LCD ((LCD_TypeDef *) LCD_BASE) #define LCD_WR_REG(regval) do{ LCD->LCD_REG = regval; }while(0) #define LCD_WR_DATA(data) do{ LCD->LCD_RAM = data; }while(0) @@ -22,7 +22,7 @@ #error "don't support LCD-GPIO yet" #endif -void LCD_HardwareConfig(void); uint16_t LCD_RD_DATA(void); +void LCD_HardwareConfig(void); #endif diff --git a/TFT/src/User/Hal/gd32f20x/lcd_dma.c b/TFT/src/User/Hal/gd32f20x/lcd_dma.c index 0a1af8584e..96ca17ea62 100644 --- a/TFT/src/User/Hal/gd32f20x/lcd_dma.c +++ b/TFT/src/User/Hal/gd32f20x/lcd_dma.c @@ -1,5 +1,5 @@ #include "lcd_dma.h" -#include "variants.h" // for STM32_HAS_FSMC etc... +#include "variants.h" // for STM32_HAS_FSMC etc. #include "lcd.h" #include "spi.h" #include "LCD_Init.h" @@ -8,6 +8,7 @@ #include "debug.h" #ifdef STM32_HAS_FSMC + #if W25Qxx_SPI == _SPI1 #define W25QXX_SPI_NUM SPI0 #define W25QXX_SPI_DMA DMA0 @@ -29,30 +30,30 @@ #endif // SPI --> FSMC DMA (LCD_RAM) -// 16bits, SPI_RX to LCD_RAM. +// 16bits, SPI_RX to LCD_RAM void LCD_DMA_Config(void) { - rcu_periph_clock_enable(W25QXX_SPI_DMA_RCU); // Turn on the DMA clock - Delay_ms(5); // Wait for the DMA clock to stabilize - DMA_CHPADDR(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = (uint32_t)&SPI_DATA(W25QXX_SPI_NUM); // The peripheral address is: SPI-> DR - DMA_CHMADDR(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = (uint32_t)&LCD->LCD_RAM; // The target address is LCD_RAM - DMA_CHCNT(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = 0; // DMA1, the amount of data transferred, temporarily set to 0 - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = 0x00000000; // Reset - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<4; // Read from peripheral - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<5; // Normal mode - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<6; // Peripheral address non-incremental mode - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<7; // Memory non-incremental mode - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= LCD_DATA_16BIT<<8; // Peripheral data width is 16 bits - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= LCD_DATA_16BIT<<10; // Memory data width 16 bits - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 1<<12; // Medium priority - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<14; // Non-memory to memory mode + rcu_periph_clock_enable(W25QXX_SPI_DMA_RCU); // turn on the DMA clock + Delay_ms(5); // wait for the DMA clock to stabilize + DMA_CHPADDR(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = (uint32_t)&SPI_DATA(W25QXX_SPI_NUM); // the peripheral address is: SPI-> DR + DMA_CHMADDR(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = (uint32_t)&LCD->LCD_RAM; // the target address is LCD_RAM + DMA_CHCNT(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = 0; // DMA1, the amount of data transferred, temporarily set to 0 + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = 0x00000000; // reset + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<4; // read from peripheral + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<5; // normal mode + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<6; // peripheral address non-incremental mode + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<7; // memory non-incremental mode + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= LCD_DATA_16BIT<<8; // peripheral data width is 16 bits + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= LCD_DATA_16BIT<<10; // memory data width 16 bits + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 1<<12; // medium priority + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<14; // non-memory to memory mode } #define LCD_DMA_MAX_TRANS 65535 // DMA 65535 bytes one frame // start DMA transfer from SPI->DR to FSMC // the max bytes of one frame is LCD_DMA_MAX_TRANS 65535 -void lcd_frame_segment_display(uint16_t size, uint32_t addr) +static inline void lcd_frame_segment_display(uint16_t size, uint32_t addr) { DMA_CHCNT(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = size; @@ -63,36 +64,36 @@ void lcd_frame_segment_display(uint16_t size, uint32_t addr) W25Qxx_SPI_Read_Write_Byte((uint8_t)addr); W25Qxx_SPI_Read_Write_Byte(0XFF); // 8 dummy clock - //set SPI to 16bit DMA rx only mode - SPI_CTL0(W25QXX_SPI_NUM) &= ~(1<<6); - SPI_CTL1(W25QXX_SPI_NUM) |= 1<<0; // enable SPI rx DMA - SPI_CTL0(W25QXX_SPI_NUM) |= LCD_DATA_16BIT<<11; // 16bit data frame - SPI_CTL0(W25QXX_SPI_NUM) |= 1<<10; // rx only + // set SPI to 16bit DMA rx only mode + SPI_CTL0(W25QXX_SPI_NUM) &= ~(1<<6); // disable SPI + SPI_CTL1(W25QXX_SPI_NUM) |= 1<<0; // enable SPI rx DMA + SPI_CTL0(W25QXX_SPI_NUM) |= LCD_DATA_16BIT<<11; // 16bit data frame + SPI_CTL0(W25QXX_SPI_NUM) |= 1<<10; // rx only - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 1<<0; // enable dma channel - SPI_CTL0(W25QXX_SPI_NUM) |= 1<<6; // enable SPI + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 1<<0; // enable dma channel + SPI_CTL0(W25QXX_SPI_NUM) |= 1<<6; // enable SPI - while (((DMA_INTF(W25QXX_SPI_DMA) & (1< void LCD_DMA_Config(void); void lcd_frame_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint32_t addr); diff --git a/TFT/src/User/Hal/gd32f20x/spi.c b/TFT/src/User/Hal/gd32f20x/spi.c index 5eb6a18cb1..fc3b9fce08 100644 --- a/TFT/src/User/Hal/gd32f20x/spi.c +++ b/TFT/src/User/Hal/gd32f20x/spi.c @@ -1,5 +1,5 @@ #include "spi.h" -#include "variants.h" // for SPI1_SCK_PIN etc... +#include "variants.h" // for SPI1_SCK_PIN etc. #include "GPIO_Init.h" // SPI1 default pins config @@ -69,9 +69,9 @@ void SPI_Slave_GPIO_Init(uint8_t port) GPIO_InitSet(spi_mosi[port], MGPIO_MODE_IPN, 0); // MOSI } -void SPI_GPIO_DeInit(uint8_t port) +static inline void SPI_GPIO_DeInit(uint8_t port) { - // Set all of spi pins to input + // set all of spi pins to input GPIO_InitSet(spi_sck[port], MGPIO_MODE_IPN, 0); // SCK GPIO_InitSet(spi_miso[port], MGPIO_MODE_IPN, 0); // MISO GPIO_InitSet(spi_mosi[port], MGPIO_MODE_IPN, 0); // MOSI @@ -82,9 +82,9 @@ void SPI_GPIO_DeInit(uint8_t port) void SPI_Protocol_Init(uint8_t port, uint8_t baudrate) { rcu_periph_reset_enable(rcu_spi_rst[port]); - rcu_periph_reset_disable(rcu_spi_rst[port]); + rcu_periph_reset_disable(rcu_spi_rst[port]); // reset SPI clock - rcu_periph_clock_enable(rcu_spi_en[port]); + rcu_periph_clock_enable(rcu_spi_en[port]); // enable SPI clock SPI_CTL0(spi[port]) = (0<<15) // 0:2-line 1: 1-line | (0<<14) // in bidirectional mode 0:read only 1: read/write @@ -93,9 +93,9 @@ void SPI_Protocol_Init(uint8_t port, uint8_t baudrate) | (0<<11) // 0:8-bit data frame 1:16-bit data frame | (0<<10) // 0:Full duplex 1:Receive-only | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) - | (1<<8) // This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored + | (1<<8) // this bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored | (0<<7) // 0:MSB 1:LSB - | (1<<6) // Enable SPI + | (1<<6) // enable SPI | (baudrate<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 | (1<<2) // 0:Slave 1:Master @@ -112,8 +112,9 @@ void SPI_Config(uint8_t port) void SPI_DeConfig(uint8_t port) { SPI_GPIO_DeInit(port); + rcu_periph_reset_enable(rcu_spi_rst[port]); - rcu_periph_reset_disable(rcu_spi_rst[port]); + rcu_periph_reset_disable(rcu_spi_rst[port]); // reset SPI clock } uint16_t SPI_Read_Write(uint8_t port, uint16_t d) @@ -121,5 +122,6 @@ uint16_t SPI_Read_Write(uint8_t port, uint16_t d) while ((SPI_STAT(spi[port]) & (1 << 1)) == RESET); // wait for tx empty SPI_DATA(spi[port]) = d; while ((SPI_STAT(spi[port]) & (1 << 0)) == RESET); // wait for rx no empty + return SPI_DATA(spi[port]); } diff --git a/TFT/src/User/Hal/gd32f20x/spi.h b/TFT/src/User/Hal/gd32f20x/spi.h index 28d8fa267c..3b14f32c11 100644 --- a/TFT/src/User/Hal/gd32f20x/spi.h +++ b/TFT/src/User/Hal/gd32f20x/spi.h @@ -3,16 +3,16 @@ #include -#define _SPI1 0 -#define _SPI2 1 -#define _SPI3 2 -#define _SPI_CNT 3 +#define _SPI1 0 +#define _SPI2 1 +#define _SPI3 2 +#define _SPI_CNT 3 void SPI_GPIO_Init(uint8_t port); void SPI_Slave_GPIO_Init(uint8_t port); +void SPI_Protocol_Init(uint8_t port, uint8_t baudrate); void SPI_Config(uint8_t port); void SPI_DeConfig(uint8_t port); -void SPI_Protocol_Init(uint8_t port, uint8_t baudrate); uint16_t SPI_Read_Write(uint8_t port, uint16_t d); #endif diff --git a/TFT/src/User/Hal/gd32f20x/spi_slave.c b/TFT/src/User/Hal/gd32f20x/spi_slave.c index 4f0aeec476..62f9ecdf7e 100644 --- a/TFT/src/User/Hal/gd32f20x/spi_slave.c +++ b/TFT/src/User/Hal/gd32f20x/spi_slave.c @@ -1,47 +1,56 @@ #include "spi_slave.h" -#include "includes.h" // for infoSettings, ST7920_EMULATOR etc... +#include "includes.h" // for infoSettings, ST7920_EMULATOR etc. #include "spi.h" #include "GPIO_Init.h" #include "HD44780.h" #if defined(ST7920_EMULATOR) + // TODO: // now support SPI2 and PB12 CS only // more compatibility changes are needed -// Config for SPI Channel +// config for SPI Channel #if ST7920_SPI == _SPI1 - #define ST7920_SPI_NUM SPI0 + #define ST7920_SPI_NUM SPI0 #elif ST7920_SPI == _SPI2 - #define ST7920_SPI_NUM SPI1 + #define ST7920_SPI_NUM SPI1 #elif ST7920_SPI == _SPI3 - #define ST7920_SPI_NUM SPI2 + #define ST7920_SPI_NUM SPI2 #endif -CIRCULAR_QUEUE *spi_queue = NULL; +static CIRCULAR_QUEUE * spi_queue = NULL; + +// external interruption arrangement +static inline void SPI_Slave_CS_Config(void) +{ + gpio_exti_source_select(GPIO_EVENT_PORT_GPIOB, GPIO_EVENT_PIN_12); -void SPI_Slave_CS_Config(void); // forward declaration + exti_init(EXTI_12, EXTI_INTERRUPT, EXTI_TRIG_BOTH); + exti_interrupt_flag_clear(EXTI_12); + nvic_irq_enable(EXTI10_15_IRQn, 0U, 1U); +} -void SPI_ReEnable(uint8_t mode) +static void SPI_ReEnable(uint8_t mode) { - SPI_CTL0(ST7920_SPI_NUM) = (0<<15) // 0:2-line 1: 1-line - | (0<<14) // in bidirectional mode 0:read only 1: read/write - | (0<<13) // 0:disable CRC 1:enable CRC - | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) - | (0<<11) // 0:8-bit data frame 1:16-bit data frame - | (1<<10) // 0:Full duplex 1:Receive-only - | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) - | (0<<8) // This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored - | (0<<7) // 0:MSB 1:LSB - | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 - // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 - | (0<<2) // 0:Slave 1:Master - | (mode<<1) // CPOL - | (mode<<0); // CPHA - - SPI_CTL1(ST7920_SPI_NUM) |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE + SPI_CTL0(ST7920_SPI_NUM) = (0<<15) // 0:2-line 1: 1-line + | (0<<14) // in bidirectional mode 0:read only 1: read/write + | (0<<13) // 0:disable CRC 1:enable CRC + | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) + | (0<<11) // 0:8-bit data frame 1:16-bit data frame + | (1<<10) // 0:Full duplex 1:Receive-only + | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) + | (0<<8) // this bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored + | (0<<7) // 0:MSB 1:LSB + | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 + // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 + | (0<<2) // 0:Slave 1:Master + | (mode<<1) // CPOL + | (mode<<0); // CPHA + + SPI_CTL1(ST7920_SPI_NUM) |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE } -void SPI_Slave(CIRCULAR_QUEUE *queue) +void SPI_Slave(CIRCULAR_QUEUE * queue) { spi_queue = queue; @@ -50,19 +59,17 @@ void SPI_Slave(CIRCULAR_QUEUE *queue) spi_queue->index_r = spi_queue->index_w = spi_queue->count = 0; SPI_Slave_GPIO_Init(ST7920_SPI); - GPIO_InitSet(PB12, MGPIO_MODE_IPU, 0); // CS + GPIO_InitSet(PB12, MGPIO_MODE_IPU, 0); // CS nvic_irq_enable(SPI1_IRQn, 1U, 0U); rcu_periph_clock_enable(RCU_SPI1); SPI_Slave_CS_Config(); - SPI_ReEnable(0); // spi mode0 + SPI_ReEnable(0); // spi mode0 if ((GPIO_ISTAT(GPIOB) & (1<<12)) != 0) - { SPI_CTL0(ST7920_SPI_NUM) |= (1<<6); - } } void SPI_SlaveDeInit(void) @@ -75,13 +82,7 @@ void SPI_SlaveDeInit(void) spi_queue = NULL; } -void SPI1_IRQHandler(void) -{ - spi_queue->data[spi_queue->index_w] = SPI_DATA(ST7920_SPI_NUM); - spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; -} - -bool SPI_SlaveGetData(uint8_t *data) +bool SPI_SlaveGetData(uint8_t * data) { bool dataRead = false; @@ -96,19 +97,17 @@ bool SPI_SlaveGetData(uint8_t *data) return dataRead; } -// External interruption arrangement -void SPI_Slave_CS_Config(void) +void SPI1_IRQHandler(void) { - gpio_exti_source_select(GPIO_EVENT_PORT_GPIOB, GPIO_EVENT_PIN_12); - - exti_init(EXTI_12, EXTI_INTERRUPT, EXTI_TRIG_BOTH); - exti_interrupt_flag_clear(EXTI_12); - nvic_irq_enable(EXTI10_15_IRQn, 0U, 1U); + spi_queue->data[spi_queue->index_w] = SPI_DATA(ST7920_SPI_NUM); + spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; } -#endif + +#endif // ST7920_EMULATOR #ifdef HAS_EMULATOR -// External interruption + +// external interruption void EXTI10_15_IRQHandler(void) { switch (infoSettings.marlin_type) @@ -123,18 +122,19 @@ void EXTI10_15_IRQHandler(void) case LCD12864: if ((GPIO_ISTAT(GPIOB) & (1<<12)) != 0) { - SPI_ReEnable(!!(GPIO_ISTAT(GPIOB)& (1<<13))); // Adaptive spi mode0 / mode3 + SPI_ReEnable(!!(GPIO_ISTAT(GPIOB) & (1<<13))); // adaptive spi mode0 / mode3 SPI_CTL0(ST7920_SPI_NUM) |= (1<<6); } else { rcu_periph_reset_enable(RCU_SPI1RST); - rcu_periph_reset_disable(RCU_SPI1RST); // Reset SPI1 + rcu_periph_reset_disable(RCU_SPI1RST); // reset SPI1 } - EXTI_PD = 1<<12; // Clear interrupt status register + EXTI_PD = 1<<12; // clear interrupt status register break; #endif } } -#endif + +#endif // HAS_EMULATOR diff --git a/TFT/src/User/Hal/gd32f20x/spi_slave.h b/TFT/src/User/Hal/gd32f20x/spi_slave.h index 58ce091326..c6be7b5586 100644 --- a/TFT/src/User/Hal/gd32f20x/spi_slave.h +++ b/TFT/src/User/Hal/gd32f20x/spi_slave.h @@ -5,8 +5,8 @@ #include #include "CircularQueue.h" -void SPI_Slave(CIRCULAR_QUEUE *queue); +void SPI_Slave(CIRCULAR_QUEUE * queue); void SPI_SlaveDeInit(void); -bool SPI_SlaveGetData(uint8_t *data); +bool SPI_SlaveGetData(uint8_t * data); #endif diff --git a/TFT/src/User/Hal/gd32f20x/timer_pwm.c b/TFT/src/User/Hal/gd32f20x/timer_pwm.c index 89923f7315..ea2bfe2ecd 100644 --- a/TFT/src/User/Hal/gd32f20x/timer_pwm.c +++ b/TFT/src/User/Hal/gd32f20x/timer_pwm.c @@ -8,27 +8,28 @@ typedef struct { } TIMER; static TIMER pwmTimer[_TIM_CNT] = { - {TIMER0, RCU_TIMER0, 2}, // Timer1 APB2 bit0 - {TIMER1, RCU_TIMER1, 1}, // Timer2 APB1 bit0 - {TIMER2, RCU_TIMER2, 1}, // Timer3 APB1 bit1 - {TIMER3, RCU_TIMER3, 1}, // Timer4 APB1 bit2 - {TIMER4, RCU_TIMER4, 1}, // Timer5 APB1 bit3 - {TIMER5, RCU_TIMER5, 1}, // Timer6 APB1 bit4 - {TIMER6, RCU_TIMER6, 1}, // Timer7 APB1 bit5 - {TIMER7, RCU_TIMER7, 2}, // Timer8 APB2 bit1 + {TIMER0, RCU_TIMER0, 2}, // Timer1 APB2 bit0 + {TIMER1, RCU_TIMER1, 1}, // Timer2 APB1 bit0 + {TIMER2, RCU_TIMER2, 1}, // Timer3 APB1 bit1 + {TIMER3, RCU_TIMER3, 1}, // Timer4 APB1 bit2 + {TIMER4, RCU_TIMER4, 1}, // Timer5 APB1 bit3 + {TIMER5, RCU_TIMER5, 1}, // Timer6 APB1 bit4 + {TIMER6, RCU_TIMER6, 1}, // Timer7 APB1 bit5 + {TIMER7, RCU_TIMER7, 2}, // Timer8 APB2 bit1 {TIMER8, RCU_TIMER8, 2}, // Timer9 APB2 bit16 {TIMER9, RCU_TIMER9, 2}, // Timer10 APB2 bit17 {TIMER10, RCU_TIMER10, 2}, // Timer11 APB2 bit18 - {TIMER11, RCU_TIMER11, 1}, // Timer12 APB1 bit6 - {TIMER12, RCU_TIMER12, 1}, // Timer13 APB1 bit7 - {TIMER13, RCU_TIMER13, 1}, // Timer14 APB1 bit8 + {TIMER11, RCU_TIMER11, 1}, // Timer12 APB1 bit6 + {TIMER12, RCU_TIMER12, 1}, // Timer13 APB1 bit7 + {TIMER13, RCU_TIMER13, 1}, // Timer14 APB1 bit8 }; void TIM_PWM_SetDutyCycle(uint16_t tim_ch, uint8_t duty) { uint16_t timerIndex = TIMER_GET_TIM(tim_ch); uint16_t channel = TIMER_GET_CH(tim_ch); - const TIMER* timer = &pwmTimer[timerIndex]; + const TIMER * timer = &pwmTimer[timerIndex]; + switch (channel) { case 0: TIMER_CH0CV(timer->tim) = duty; break; @@ -42,29 +43,29 @@ void TIM_PWM_Init(uint16_t tim_ch) { uint16_t timerIndex = TIMER_GET_TIM(tim_ch); uint16_t channel = TIMER_GET_CH(tim_ch); - const TIMER* timer = &pwmTimer[timerIndex]; + const TIMER * timer = &pwmTimer[timerIndex]; uint32_t timerTmpClk = (timer->apb == 1) ? mcuClocks.PCLK1_Timer_Frequency : mcuClocks.PCLK2_Timer_Frequency; - rcu_periph_clock_enable(timer->rcc_src); + rcu_periph_clock_enable(timer->rcc_src); // enable timer clock - // Set PWM frequency to 500Hz + // set PWM frequency to 500Hz TIMER_CAR(timer->tim) = 100 - 1; TIMER_PSC(timer->tim) = timerTmpClk / (500 * 100) - 1; switch (channel) { - case 0: TIMER_CHCTL0(timer->tim) |= (6<<4) | (1<<3); break; // CH1 PWM0 mode - case 1: TIMER_CHCTL0(timer->tim) |= (6<<12) | (1<<11); break; // CH2 PWM0 mode - case 2: TIMER_CHCTL1(timer->tim) |= (6<<4) | (1<<3); break; // CH3 PWM0 mode - case 3: TIMER_CHCTL1(timer->tim) |= (6<<12) | (1<<11); break; // CH4 PWM0 mode + case 0: TIMER_CHCTL0(timer->tim) |= (6<<4) | (1<<3); break; // CH1 PWM0 mode + case 1: TIMER_CHCTL0(timer->tim) |= (6<<12) | (1<<11); break; // CH2 PWM0 mode + case 2: TIMER_CHCTL1(timer->tim) |= (6<<4) | (1<<3); break; // CH3 PWM0 mode + case 3: TIMER_CHCTL1(timer->tim) |= (6<<12) | (1<<11); break; // CH4 PWM0 mode } - TIMER_CHCTL2(timer->tim) |= 1 << (2 * channel); //enable channle - TIMER_CTL0(timer->tim) = (1 << 7) // Auto-reload preload enable - | (1 << 0); // Enbale timer + TIMER_CHCTL2(timer->tim) |= 1 << (2 * channel); // enable channle + TIMER_CTL0(timer->tim) = (1 << 7) // auto-reload preload enable + | (1 << 0); // enbale timer if (timer->tim == TIMER0 || timer->tim == TIMER7) { // TIM1 & TIM8 advanced timer need config BDTR register for PWM - TIMER_CCHP(timer->tim) |= 1 << 15; // Main output enable + TIMER_CCHP(timer->tim) |= 1 << 15; // main output enable } } diff --git a/TFT/src/User/Hal/gd32f20x/timer_pwm.h b/TFT/src/User/Hal/gd32f20x/timer_pwm.h index 8fa08d5535..efb48c2be6 100644 --- a/TFT/src/User/Hal/gd32f20x/timer_pwm.h +++ b/TFT/src/User/Hal/gd32f20x/timer_pwm.h @@ -96,7 +96,7 @@ #define _TIM15_CH4 (((_TIM15)<<8) + 3) #define TIMER_GET_TIM(n) ((n>>8) & 0xFF) -#define TIMER_GET_CH(n) (n & 0xFF) +#define TIMER_GET_CH(n) (n & 0xFF) void TIM_PWM_SetDutyCycle(uint16_t tim_ch, uint8_t duty); void TIM_PWM_Init(uint16_t tim_ch); diff --git a/TFT/src/User/Hal/gd32f20x/uart.c b/TFT/src/User/Hal/gd32f20x/uart.c index 92b95a67eb..fee6eec207 100644 --- a/TFT/src/User/Hal/gd32f20x/uart.c +++ b/TFT/src/User/Hal/gd32f20x/uart.c @@ -1,5 +1,5 @@ #include "uart.h" -#include "variants.h" // for USART1_TX_PIN etc... +#include "variants.h" // for USART1_TX_PIN etc. #include "GPIO_Init.h" // USART1 default pins config @@ -64,32 +64,33 @@ static rcu_periph_enum rcu_uart_en[_UART_CNT] = { }; static uint32_t const uart[_UART_CNT] = { - USART0, // TX--PA9 RX--PA10 - USART1, // TX--PA2 RX--PA3 - USART2, // TX--PB10 RX--PB11 - UART3, // TX--PC10 RX--PC11 - UART4, // TX--PC12 RX--PD2 - USART5}; // TX--PG14 RX--PG9 //error + USART0, // TX--PA9 RX--PA10 + USART1, // TX--PA2 RX--PA3 + USART2, // TX--PB10 RX--PB11 + UART3, // TX--PC10 RX--PC11 + UART4, // TX--PC12 RX--PD2 + USART5, // TX--PG14 RX--PG9 // error +}; static const uint16_t uart_tx[_UART_CNT] = {USART1_TX_PIN, USART2_TX_PIN, USART3_TX_PIN, UART4_TX_PIN, UART5_TX_PIN, USART6_TX_PIN}; // TX static const uint16_t uart_rx[_UART_CNT] = {USART1_RX_PIN, USART2_RX_PIN, USART3_RX_PIN, UART4_RX_PIN, UART5_RX_PIN, USART6_RX_PIN}; // RX -void UART_GPIO_Init(uint8_t port) +static inline void UART_GPIO_Init(uint8_t port) { GPIO_InitSet(uart_tx[port], MGPIO_MODE_AF_PP, 0); GPIO_InitSet(uart_rx[port], MGPIO_MODE_IPU, 0); } -void UART_GPIO_DeInit(uint8_t port) +static inline void UART_GPIO_DeInit(uint8_t port) { // set tx/rx to input GPIO_InitSet(uart_tx[port], MGPIO_MODE_IPN, 0); GPIO_InitSet(uart_rx[port], MGPIO_MODE_IPN, 0); } -void UART_Protocol_Init(uint8_t port, uint32_t baud) +static inline void UART_Protocol_Init(uint8_t port, uint32_t baud) { - rcu_periph_clock_enable(rcu_uart_en[port]); // Enable clock + rcu_periph_clock_enable(rcu_uart_en[port]); // enable clock usart_deinit(uart[port]); usart_baudrate_set(uart[port], baud); @@ -103,7 +104,7 @@ void UART_Protocol_Init(uint8_t port, uint32_t baud) usart_enable(uart[port]); } -void UART_IRQ_Init(uint8_t port, uint16_t usart_it, bool idle_interrupt) +static inline void UART_IRQ_Init(uint8_t port, uint16_t usart_it, bool idle_interrupt) { uint32_t IRQ_Channel[_UART_CNT] = {USART0_IRQn, USART1_IRQn, USART2_IRQn, UART3_IRQn, UART4_IRQn, USART5_IRQn}; nvic_irq_enable(IRQ_Channel[port], 0U, 0U); @@ -119,28 +120,28 @@ void UART_Config(uint8_t port, uint32_t baud, uint16_t usart_it, bool idle_inter { UART_Protocol_Init(port, baud); UART_IRQ_Init(port, usart_it, idle_interrupt); - UART_GPIO_Init(port); // After all initialization is completed, enable IO, otherwise a 0xFF will be sent automatically after power-on + UART_GPIO_Init(port); // after all initialization is completed, enable IO, otherwise a 0xFF will be sent automatically after power-on } void UART_DeConfig(uint8_t port) { UART_GPIO_DeInit(port); - rcu_periph_reset_enable(rcu_uart_rst[port]); // Reset clock - rcu_periph_reset_disable(rcu_uart_rst[port]); + rcu_periph_reset_enable(rcu_uart_rst[port]); + rcu_periph_reset_disable(rcu_uart_rst[port]); // reset clock } void UART_Write(uint8_t port, uint8_t d) { - while ((USART_STAT0(uart[port]) & (1 << USART_FLAG_TC)) == (uint16_t)RESET); + while ((USART_STAT0(uart[port]) & (1 << USART_FLAG_TC)) == (uint16_t)RESET); // wait USART_DATA(uart[port]) = ((uint16_t)d & (uint16_t)0x01FF); } -void UART_Puts(uint8_t port, uint8_t *str) +void UART_Puts(uint8_t port, uint8_t * str) { while (*str) { - while ((USART_STAT0(uart[port]) & (1 << USART_FLAG_TC)) == (uint16_t)RESET); + while ((USART_STAT0(uart[port]) & (1 << USART_FLAG_TC)) == (uint16_t)RESET); // wait USART_DATA(uart[port]) = ((uint16_t)*str++ & (uint16_t)0x01FF); } } diff --git a/TFT/src/User/Hal/gd32f20x/uart.h b/TFT/src/User/Hal/gd32f20x/uart.h index 4230750ff6..0d4dc0589e 100644 --- a/TFT/src/User/Hal/gd32f20x/uart.h +++ b/TFT/src/User/Hal/gd32f20x/uart.h @@ -4,17 +4,17 @@ #include #include -#define _USART1 0 -#define _USART2 1 -#define _USART3 2 -#define _UART4 3 -#define _UART5 4 -#define _USART6 5 -#define _UART_CNT 6 +#define _USART1 0 // USART0 +#define _USART2 1 // USART1 +#define _USART3 2 // USART2 +#define _UART4 3 // UART3 +#define _UART5 4 // UART4 +#define _USART6 5 // USART5 +#define _UART_CNT 6 void UART_Config(uint8_t port, uint32_t baud, uint16_t usart_it, bool idle_interrupt); void UART_DeConfig(uint8_t port); -void UART_Puts(uint8_t port, uint8_t *str); void UART_Write(uint8_t port, uint8_t d); +void UART_Puts(uint8_t port, uint8_t * str); #endif diff --git a/TFT/src/User/Hal/gd32f30x/GPIO_Init.c b/TFT/src/User/Hal/gd32f30x/GPIO_Init.c index 49ffe37030..2e85091848 100644 --- a/TFT/src/User/Hal/gd32f30x/GPIO_Init.c +++ b/TFT/src/User/Hal/gd32f30x/GPIO_Init.c @@ -1,21 +1,139 @@ #include "GPIO_Init.h" -/* -* bit 0:1 gpio mode 00: Input (reset state) 01: General purpose output mode 10: Alternate function mode 11: Analog mode -* bit 2 gpio output type 0: Output push-pull (reset state) 1: Output open-drain -* bit 3:4 gpio output speed 00: Low speed 01: Medium speed 10: High speed 11: Very high speed -* bit 5:6 gpio pull-up/pull-down 00: No pull-up, pull-down 01: Pull-up 10: Pull-down 11: Reserved -* bit 7 reserved -*/ -const uint32_t GPIO_Port[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG}; -const rcu_periph_enum RCU_Port[] = {RCU_GPIOA, RCU_GPIOB, RCU_GPIOC, RCU_GPIOD, RCU_GPIOE, RCU_GPIOF, RCU_GPIOG}; +#define GPIO_MODE_GET_MODE(n) ((n) & 0x3) +#define GPIO_MODE_GET_OTYPE(n) (((n)>>2) & 0x1) +#define GPIO_MODE_GET_OSPEED(n) (((n)>>3) & 0x3) +#define GPIO_MODE_GET_PULL(n) (((n)>>5) & 0x3) + +#define GPIO_MODE_AF 2 +#define GPIO_AF0 0 +#define GPIO_AF1 1 +#define GPIO_AF2 2 +#define GPIO_AF3 3 +#define GPIO_AF4 4 +#define GPIO_AF5 5 +#define GPIO_AF6 6 +#define GPIO_AF7 7 +#define GPIO_AF8 8 +#define GPIO_AF9 9 +#define GPIO_AF10 10 +#define GPIO_AF11 11 +#define GPIO_AF12 12 +#define GPIO_AF13 13 +#define GPIO_AF14 14 +#define GPIO_AF15 15 + +#ifndef GPIO_AF_RTC_50Hz + /** + * @brief AF 0 selection + */ + #define GPIO_AF_RTC_50Hz ((uint8_t)0x00) /* RTC_50Hz Alternate Function mapping */ + #define GPIO_AF_MCO ((uint8_t)0x00) /* MCO (MCO1 and MCO2) Alternate Function mapping */ + #define GPIO_AF_TAMPER ((uint8_t)0x00) /* TAMPER (TAMPER_1 and TAMPER_2) Alternate Function mapping */ + #define GPIO_AF_SWJ ((uint8_t)0x00) /* SWJ (SWD and JTAG) Alternate Function mapping */ + #define GPIO_AF_TRACE ((uint8_t)0x00) /* TRACE Alternate Function mapping */ + + /** + * @brief AF 1 selection + */ + #define GPIO_AF_TIM1 ((uint8_t)0x01) /* TIM1 Alternate Function mapping */ + #define GPIO_AF_TIM2 ((uint8_t)0x01) /* TIM2 Alternate Function mapping */ + + /** + * @brief AF 2 selection + */ + #define GPIO_AF_TIM3 ((uint8_t)0x02) /* TIM3 Alternate Function mapping */ + #define GPIO_AF_TIM4 ((uint8_t)0x02) /* TIM4 Alternate Function mapping */ + #define GPIO_AF_TIM5 ((uint8_t)0x02) /* TIM5 Alternate Function mapping */ + + /** + * @brief AF 3 selection + */ + #define GPIO_AF_TIM8 ((uint8_t)0x03) /* TIM8 Alternate Function mapping */ + #define GPIO_AF_TIM9 ((uint8_t)0x03) /* TIM9 Alternate Function mapping */ + #define GPIO_AF_TIM10 ((uint8_t)0x03) /* TIM10 Alternate Function mapping */ + #define GPIO_AF_TIM11 ((uint8_t)0x03) /* TIM11 Alternate Function mapping */ + + /** + * @brief AF 4 selection + */ + #define GPIO_AF_I2C1 ((uint8_t)0x04) /* I2C1 Alternate Function mapping */ + #define GPIO_AF_I2C2 ((uint8_t)0x04) /* I2C2 Alternate Function mapping */ + #define GPIO_AF_I2C3 ((uint8_t)0x04) /* I2C3 Alternate Function mapping */ + + /** + * @brief AF 5 selection + */ + #define GPIO_AF_SPI1 ((uint8_t)0x05) /* SPI1 Alternate Function mapping */ + #define GPIO_AF_SPI2 ((uint8_t)0x05) /* SPI2/I2S2 Alternate Function mapping */ + + /** + * @brief AF 6 selection + */ + #define GPIO_AF_SPI3 ((uint8_t)0x06) /* SPI3/I2S3 Alternate Function mapping */ + + /** + * @brief AF 7 selection + */ + #define GPIO_AF_USART1 ((uint8_t)0x07) /* USART1 Alternate Function mapping */ + #define GPIO_AF_USART2 ((uint8_t)0x07) /* USART2 Alternate Function mapping */ + #define GPIO_AF_USART3 ((uint8_t)0x07) /* USART3 Alternate Function mapping */ + + /** + * @brief AF 8 selection + */ + #define GPIO_AF_UART4 ((uint8_t)0x08) /* UART4 Alternate Function mapping */ + #define GPIO_AF_UART5 ((uint8_t)0x08) /* UART5 Alternate Function mapping */ + #define GPIO_AF_USART6 ((uint8_t)0x08) /* USART6 Alternate Function mapping */ + + /** + * @brief AF 9 selection + */ + #define GPIO_AF_CAN1 ((uint8_t)0x09) /* CAN1 Alternate Function mapping */ + #define GPIO_AF_CAN2 ((uint8_t)0x09) /* CAN2 Alternate Function mapping */ + #define GPIO_AF_TIM12 ((uint8_t)0x09) /* TIM12 Alternate Function mapping */ + #define GPIO_AF_TIM13 ((uint8_t)0x09) /* TIM13 Alternate Function mapping */ + #define GPIO_AF_TIM14 ((uint8_t)0x09) /* TIM14 Alternate Function mapping */ + + /** + * @brief AF 10 selection + */ + #define GPIO_AF_OTG_FS ((uint8_t)0xA) /* OTG_FS Alternate Function mapping */ + #define GPIO_AF_OTG_HS ((uint8_t)0xA) /* OTG_HS Alternate Function mapping */ + + /** + * @brief AF 11 selection + */ + #define GPIO_AF_ETH ((uint8_t)0x0B) /* ETHERNET Alternate Function mapping */ + + /** + * @brief AF 12 selection + */ + #define GPIO_AF_FSMC ((uint8_t)0xC) /* FSMC Alternate Function mapping */ + #define GPIO_AF_OTG_HS_FS ((uint8_t)0xC) /* OTG HS configured in FS, Alternate Function mapping */ + #define GPIO_AF_SDIO ((uint8_t)0xC) /* SDIO Alternate Function mapping */ + + /** + * @brief AF 13 selection + */ + #define GPIO_AF_DCMI ((uint8_t)0x0D) /* DCMI Alternate Function mapping */ + + /** + * @brief AF 15 selection + */ + #define GPIO_AF_EVENTOUT ((uint8_t)0x0F) /* EVENTOUT Alternate Function mapping */ + +#endif + +static const uint32_t GPIO_Port[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG}; +static const rcu_periph_enum RCU_Port[] = {RCU_GPIOA, RCU_GPIOB, RCU_GPIOC, RCU_GPIOD, RCU_GPIOE, RCU_GPIOF, RCU_GPIOG}; void GPIO_InitSet(uint16_t io, GPIO_MODE mode, uint32_t remap) { uint16_t port = GPIO_GET_PORT(io); uint16_t pin = GPIO_GET_PIN(io); - // RCU_REG_VAL(RCU_Port[port]) |= BIT(RCU_BIT_POS(RCU_Port[port])); // Enable GPIO Clock + // RCU_REG_VAL(RCU_Port[port]) |= BIT(RCU_BIT_POS(RCU_Port[port])); // enable GPIO Clock rcu_periph_clock_enable(RCU_Port[port]); rcu_periph_clock_enable(RCU_AF); @@ -41,16 +159,10 @@ void GPIO_ToggleLevel(uint16_t io) uint16_t port = GPIO_GET_PORT(io); uint16_t pin = GPIO_GET_PIN(io); - - if((uint32_t)RESET !=(GPIO_OCTL(GPIO_Port[port])&(1< +#include "variants.h" // for GPIO_MODE_IN_FLOATING etc. #include "STM32Fxx_Pins.h" /* -* bit 0:1 gpio mode 00: Input (reset state) 01: General purpose output mode 10: Alternate function mode 11: Analog mode -* bit 2 gpio output type 0: Output push-pull (reset state) 1: Output open-drain -* bit 3:4 gpio output speed 00: Low speed 01: Medium speed 10: High speed 11: Very high speed -* bit 5:6 gpio pull-up/pull-down 00: No pull-up, pull-down 01: Pull-up 10: Pull-down 11: Reserved -* bit 7 reserved -*/ - + * bit 0:1 gpio mode 00: Input (reset state) 01: General purpose output mode 10: Alternate function mode 11: Analog mode + * bit 2 gpio output type 0: Output push-pull (reset state) 1: Output open-drain + * bit 3:4 gpio output speed 00: Low speed 01: Medium speed 10: High speed 11: Very high speed + * bit 5:6 gpio pull-up/pull-down 00: No pull-up, pull-down 01: Pull-up 10: Pull-down 11: Reserved + * bit 7 reserved + */ typedef enum { - MGPIO_MODE_IPN = GPIO_MODE_IN_FLOATING, - MGPIO_MODE_IPU = GPIO_MODE_IPU, - MGPIO_MODE_IPD = GPIO_MODE_IPD, + MGPIO_MODE_IPN = GPIO_MODE_IN_FLOATING, + MGPIO_MODE_IPU = GPIO_MODE_IPU, + MGPIO_MODE_IPD = GPIO_MODE_IPD, MGPIO_MODE_OUT_PP = GPIO_MODE_OUT_PP, MGPIO_MODE_OUT_OD = GPIO_MODE_OUT_OD, - MGPIO_MODE_AF_PP = GPIO_MODE_AF_PP, - MGPIO_MODE_AF_OD = GPIO_MODE_AF_OD, - MGPIO_MODE_AIN = GPIO_MODE_AIN, + MGPIO_MODE_AF_PP = GPIO_MODE_AF_PP, + MGPIO_MODE_AF_OD = GPIO_MODE_AF_OD, + MGPIO_MODE_AIN = GPIO_MODE_AIN, } GPIO_MODE; - -#define GPIO_MODE_GET_MODE(n) ((n) & 0x3) -#define GPIO_MODE_GET_OTYPE(n) (((n)>>2) & 0x1) -#define GPIO_MODE_GET_OSPEED(n) (((n)>>3) & 0x3) -#define GPIO_MODE_GET_PULL(n) (((n)>>5) & 0x3) - -#define GPIO_MODE_AF 2 -#define GPIO_AF0 0 -#define GPIO_AF1 1 -#define GPIO_AF2 2 -#define GPIO_AF3 3 -#define GPIO_AF4 4 -#define GPIO_AF5 5 -#define GPIO_AF6 6 -#define GPIO_AF7 7 -#define GPIO_AF8 8 -#define GPIO_AF9 9 -#define GPIO_AF10 10 -#define GPIO_AF11 11 -#define GPIO_AF12 12 -#define GPIO_AF13 13 -#define GPIO_AF14 14 -#define GPIO_AF15 15 - -#ifndef GPIO_AF_RTC_50Hz - /** - * @brief AF 0 selection - */ - #define GPIO_AF_RTC_50Hz ((uint8_t)0x00) /* RTC_50Hz Alternate Function mapping */ - #define GPIO_AF_MCO ((uint8_t)0x00) /* MCO (MCO1 and MCO2) Alternate Function mapping */ - #define GPIO_AF_TAMPER ((uint8_t)0x00) /* TAMPER (TAMPER_1 and TAMPER_2) Alternate Function mapping */ - #define GPIO_AF_SWJ ((uint8_t)0x00) /* SWJ (SWD and JTAG) Alternate Function mapping */ - #define GPIO_AF_TRACE ((uint8_t)0x00) /* TRACE Alternate Function mapping */ - - /** - * @brief AF 1 selection - */ - #define GPIO_AF_TIM1 ((uint8_t)0x01) /* TIM1 Alternate Function mapping */ - #define GPIO_AF_TIM2 ((uint8_t)0x01) /* TIM2 Alternate Function mapping */ - - /** - * @brief AF 2 selection - */ - #define GPIO_AF_TIM3 ((uint8_t)0x02) /* TIM3 Alternate Function mapping */ - #define GPIO_AF_TIM4 ((uint8_t)0x02) /* TIM4 Alternate Function mapping */ - #define GPIO_AF_TIM5 ((uint8_t)0x02) /* TIM5 Alternate Function mapping */ - - /** - * @brief AF 3 selection - */ - #define GPIO_AF_TIM8 ((uint8_t)0x03) /* TIM8 Alternate Function mapping */ - #define GPIO_AF_TIM9 ((uint8_t)0x03) /* TIM9 Alternate Function mapping */ - #define GPIO_AF_TIM10 ((uint8_t)0x03) /* TIM10 Alternate Function mapping */ - #define GPIO_AF_TIM11 ((uint8_t)0x03) /* TIM11 Alternate Function mapping */ - - /** - * @brief AF 4 selection - */ - #define GPIO_AF_I2C1 ((uint8_t)0x04) /* I2C1 Alternate Function mapping */ - #define GPIO_AF_I2C2 ((uint8_t)0x04) /* I2C2 Alternate Function mapping */ - #define GPIO_AF_I2C3 ((uint8_t)0x04) /* I2C3 Alternate Function mapping */ - - /** - * @brief AF 5 selection - */ - #define GPIO_AF_SPI1 ((uint8_t)0x05) /* SPI1 Alternate Function mapping */ - #define GPIO_AF_SPI2 ((uint8_t)0x05) /* SPI2/I2S2 Alternate Function mapping */ - - /** - * @brief AF 6 selection - */ - #define GPIO_AF_SPI3 ((uint8_t)0x06) /* SPI3/I2S3 Alternate Function mapping */ - - /** - * @brief AF 7 selection - */ - #define GPIO_AF_USART1 ((uint8_t)0x07) /* USART1 Alternate Function mapping */ - #define GPIO_AF_USART2 ((uint8_t)0x07) /* USART2 Alternate Function mapping */ - #define GPIO_AF_USART3 ((uint8_t)0x07) /* USART3 Alternate Function mapping */ - - /** - * @brief AF 8 selection - */ - #define GPIO_AF_UART4 ((uint8_t)0x08) /* UART4 Alternate Function mapping */ - #define GPIO_AF_UART5 ((uint8_t)0x08) /* UART5 Alternate Function mapping */ - #define GPIO_AF_USART6 ((uint8_t)0x08) /* USART6 Alternate Function mapping */ - - /** - * @brief AF 9 selection - */ - #define GPIO_AF_CAN1 ((uint8_t)0x09) /* CAN1 Alternate Function mapping */ - #define GPIO_AF_CAN2 ((uint8_t)0x09) /* CAN2 Alternate Function mapping */ - #define GPIO_AF_TIM12 ((uint8_t)0x09) /* TIM12 Alternate Function mapping */ - #define GPIO_AF_TIM13 ((uint8_t)0x09) /* TIM13 Alternate Function mapping */ - #define GPIO_AF_TIM14 ((uint8_t)0x09) /* TIM14 Alternate Function mapping */ - - /** - * @brief AF 10 selection - */ - #define GPIO_AF_OTG_FS ((uint8_t)0xA) /* OTG_FS Alternate Function mapping */ - #define GPIO_AF_OTG_HS ((uint8_t)0xA) /* OTG_HS Alternate Function mapping */ - - /** - * @brief AF 11 selection - */ - #define GPIO_AF_ETH ((uint8_t)0x0B) /* ETHERNET Alternate Function mapping */ - - /** - * @brief AF 12 selection - */ - #define GPIO_AF_FSMC ((uint8_t)0xC) /* FSMC Alternate Function mapping */ - #define GPIO_AF_OTG_HS_FS ((uint8_t)0xC) /* OTG HS configured in FS, Alternate Function mapping */ - #define GPIO_AF_SDIO ((uint8_t)0xC) /* SDIO Alternate Function mapping */ - - /** - * @brief AF 13 selection - */ - #define GPIO_AF_DCMI ((uint8_t)0x0D) /* DCMI Alternate Function mapping */ - - /** - * @brief AF 15 selection - */ - #define GPIO_AF_EVENTOUT ((uint8_t)0x0F) /* EVENTOUT Alternate Function mapping */ - -#endif - void GPIO_InitSet(uint16_t io, GPIO_MODE mode, uint32_t remap); void GPIO_SetLevel(uint16_t io, uint8_t level); void GPIO_ToggleLevel(uint16_t io); diff --git a/TFT/src/User/Hal/gd32f30x/HAL_Flash.c b/TFT/src/User/Hal/gd32f30x/HAL_Flash.c index ac757a3559..68dd8588a6 100644 --- a/TFT/src/User/Hal/gd32f30x/HAL_Flash.c +++ b/TFT/src/User/Hal/gd32f30x/HAL_Flash.c @@ -1,42 +1,48 @@ #include "HAL_Flash.h" +#include "variants.h" // for fmc_unlock etc. #include "my_misc.h" /* -Page 0 0x0800 0000 - 0x0800 07FF 2 Kbyte -Page 1 0x0800 0800 - 0x0800 0FFF 2 Kbyte -Page 2 0x0800 1000 - 0x0800 17FF 2 Kbyte -... -... -... -Page 255 0x0807 F800 - 0x0807 FFFF 2 Kbyte // 512KByte -*/ + * Page 0 0x0800 0000 - 0x0800 07FF 2 Kbyte + * Page 1 0x0800 0800 - 0x0800 0FFF 2 Kbyte + * Page 2 0x0800 1000 - 0x0800 17FF 2 Kbyte + * ... + * ... + * ... + * Page 255 0x0807 F800 - 0x0807 FFFF 2 Kbyte // 512KByte + */ #define SIGN_ADDRESS (0x08040000 - 0x800) // reserve the last page (2KB) to save user parameters -void HAL_FlashRead(uint8_t *data, uint32_t len) +void HAL_FlashRead(uint8_t * data, uint32_t len) { uint32_t i = 0; + for (i = 0; i < len; i++) { - data[i] = *(volatile uint8_t*)(SIGN_ADDRESS + i); + data[i] = *(volatile uint8_t *)(SIGN_ADDRESS + i); } } -void HAL_FlashWrite(uint8_t *data, uint32_t len) +void HAL_FlashWrite(uint8_t * data, uint32_t len) { uint32_t i = 0; + fmc_unlock(); fmc_page_erase(SIGN_ADDRESS); fmc_flag_clear(FMC_FLAG_BANK0_END); fmc_flag_clear(FMC_FLAG_BANK0_WPERR); fmc_flag_clear(FMC_FLAG_BANK0_PGERR); + for (i = 0; i < len; i += 2) { - uint16_t data16 = data[i] | (data[MIN(i+1, len-1)] << 8); // gd32f20x needs to write at least 16 bits at a time + uint16_t data16 = data[i] | (data[MIN(i + 1, len - 1)] << 8); // gd32f20x needs to write at least 16 bits at a time + fmc_halfword_program(SIGN_ADDRESS + i, data16); fmc_flag_clear(FMC_FLAG_BANK0_END); fmc_flag_clear(FMC_FLAG_BANK0_WPERR); fmc_flag_clear(FMC_FLAG_BANK0_PGERR); } + fmc_lock(); } diff --git a/TFT/src/User/Hal/gd32f30x/HAL_Flash.h b/TFT/src/User/Hal/gd32f30x/HAL_Flash.h index d37103fc08..88792034a2 100644 --- a/TFT/src/User/Hal/gd32f30x/HAL_Flash.h +++ b/TFT/src/User/Hal/gd32f30x/HAL_Flash.h @@ -1,9 +1,9 @@ #ifndef _HAL_FLASH_H_ #define _HAL_FLASH_H_ -#include "variants.h" // for uint8_t etc... +#include -void HAL_FlashRead(uint8_t *data, uint32_t len); -void HAL_FlashWrite(uint8_t *data, uint32_t len); +void HAL_FlashRead(uint8_t * data, uint32_t len); +void HAL_FlashWrite(uint8_t * data, uint32_t len); #endif diff --git a/TFT/src/User/Hal/gd32f30x/Serial.c b/TFT/src/User/Hal/gd32f30x/Serial.c index daad4cdea9..3be1a46ccc 100644 --- a/TFT/src/User/Hal/gd32f30x/Serial.c +++ b/TFT/src/User/Hal/gd32f30x/Serial.c @@ -27,7 +27,7 @@ const SERIAL_CFG Serial[_UART_CNT] = { }; // disable RX DMA and clear all interrupt flags for a serial port -void Serial_DMA_DisableAndClearFlagsRX(uint8_t port) +static void Serial_DMA_DisableAndClearFlagsRX(uint8_t port) { DMA_CHCTL(Serial[port].dma_stream, Serial[port].dma_channelRX) &= ~(1<<0); // disable RX DMA @@ -54,17 +54,13 @@ void Serial_DMA_DisableAndClearFlagsRX(uint8_t port) case _UART5: DMA_INTC(DMA1) = (0x0F << 4); // DMA1 channel 1 break; - - case _USART6: - DMA_INTC(DMA1) = (0x0F << 20); // DMA1 channel 5 - break; } } #ifdef TX_DMA_WRITE // disable TX DMA and clear all interrupt flags for a serial port -void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) +static void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) { DMA_CHCTL(Serial[port].dma_stream, Serial[port].dma_channelTX) &= ~(1<<0); // disable TX DMA @@ -91,16 +87,12 @@ void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) case _UART5: DMA_INTC(DMA1) = (0x0F << 12); // DMA1 channel 3 break; - - case _USART6: - DMA_INTC(DMA1) = (0x0F << 24); // DMA1 channel 6 - break; } } #endif -void Serial_DMA_Config(uint8_t port) +static inline void Serial_DMA_Config(uint8_t port) { const SERIAL_CFG * cfg = &Serial[port]; @@ -155,7 +147,7 @@ void Serial_DMA_Config(uint8_t port) DMA_CHCTL(cfg->dma_stream, cfg->dma_channelRX) |= (1<<0); // RX enable DMA } -void Serial_ClearData(uint8_t port) +static void Serial_ClearData(uint8_t port) { dmaL1DataRX[port].wIndex = dmaL1DataRX[port].rIndex = dmaL1DataRX[port].flag = dmaL1DataRX[port].cacheSize = 0; @@ -207,7 +199,7 @@ void Serial_DeConfig(uint8_t port) #ifdef TX_DMA_WRITE // TX DMA based serial writing // DMA serial write support function -void Serial_Send_TX(uint8_t port) +static void Serial_Send_TX(uint8_t port) { // setup DMA transfer, and wait for serial Transfer Complete (TX) interrupt in ISR if (dmaL1DataTX[port].wIndex >= dmaL1DataTX[port].rIndex) diff --git a/TFT/src/User/Hal/gd32f30x/Serial.h b/TFT/src/User/Hal/gd32f30x/Serial.h index 43041be815..171d838d9a 100644 --- a/TFT/src/User/Hal/gd32f30x/Serial.h +++ b/TFT/src/User/Hal/gd32f30x/Serial.h @@ -2,8 +2,8 @@ #define _SERIAL_H_ #include -#include "variants.h" // for uint32_t etc... -#include "uart.h" +#include "variants.h" // for rcu_periph_enum etc. +#include "uart.h" // for _UART_CNT etc. // comment out this line to use TX interrupt based serial writing instead of TX DMA based serial writing #define TX_DMA_WRITE diff --git a/TFT/src/User/Hal/gd32f30x/lcd.c b/TFT/src/User/Hal/gd32f30x/lcd.c index ee5649c7c2..c99ee57146 100644 --- a/TFT/src/User/Hal/gd32f30x/lcd.c +++ b/TFT/src/User/Hal/gd32f30x/lcd.c @@ -7,26 +7,30 @@ uint16_t LCD_RD_DATA(void) { volatile uint16_t ram; + ram = LCD->LCD_RAM; + return ram; } -void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) +static void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) { LCD->LCD_REG = LCD_Reg; LCD->LCD_RAM = LCD_RegValue; } -uint16_t LCD_ReadReg(uint8_t LCD_Reg) +static uint16_t LCD_ReadReg(uint8_t LCD_Reg) { LCD_WR_REG(LCD_Reg); Delay_us(5); + return LCD_RD_DATA(); } -void LCD_GPIO_Config(void) +static inline void LCD_GPIO_Config(void) { rcu_periph_clock_enable(RCU_EXMC); + // fsmc 16bit data pins GPIO_InitSet(PD0, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PD1, MGPIO_MODE_AF_PP, 0); @@ -45,24 +49,25 @@ void LCD_GPIO_Config(void) GPIO_InitSet(PE14, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PE15, MGPIO_MODE_AF_PP, 0); - /*Configure the control line corresponding to FSMC - * PD4-FSMC_NOE :LCD-RD - * PD5-FSMC_NWE :LCD-WR - * PD7-FSMC_NE1 :LCD-CS - * PE2-FSMC_A23 :LCD-RS LCD-RS data or cmd - */ + /* + * configure the control line corresponding to FSMC + * PD4-FSMC_NOE :LCD-RD + * PD5-FSMC_NWE :LCD-WR + * PD7-FSMC_NE1 :LCD-CS + * PE2-FSMC_A23 :LCD-RS LCD-RS data or cmd + */ GPIO_InitSet(PD4, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PD5, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PE2, MGPIO_MODE_AF_PP, 0); GPIO_InitSet(PD7, MGPIO_MODE_AF_PP, 0); } -void LCD_EXMC_Config(void) +static inline void LCD_EXMC_Config(void) { exmc_norsram_parameter_struct EXMC_NORSRAMInitStructure; exmc_norsram_timing_parameter_struct readWriteTiming,writeTiming; - /* EXMC configuration */ + // EXMC configuration readWriteTiming.asyn_address_setuptime = 1U; readWriteTiming.asyn_address_holdtime = 0U; readWriteTiming.asyn_data_setuptime = 15U; @@ -96,7 +101,7 @@ void LCD_EXMC_Config(void) EXMC_NORSRAMInitStructure.write_timing = &writeTiming; exmc_norsram_init(&EXMC_NORSRAMInitStructure); - /* enable EXMC SARM bank0 */ + // enable EXMC SRAM bank0 exmc_norsram_enable(EXMC_BANK0_NORSRAM_REGION0); } @@ -108,6 +113,30 @@ void LCD_HardwareConfig(void) #else +#if defined(MKS_TFT) + /* + #define LCD_WR PB14 + #define LCD_RS PD13 + #define LCD_CS PC8 + #define LCD_RD PD15 + */ + // GPIO_SetLevel + #define LCD_CS_SET gpio_bit_set(GPIOC, GPIO_PIN_8 ) // Chip Select Port PC8 //片选端口 + #define LCD_RS_SET gpio_bit_set(GPIOD, GPIO_PIN_13) // Data / command PD13 //数据/命令 + #define LCD_WR_SET gpio_bit_set(GPIOB, GPIO_PIN_14) // Write data PB14 //写数据 + #define LCD_RD_SET gpio_bit_set(GPIOD, GPIO_PIN_15) // Read data PD15 //读数据 + + #define LCD_CS_CLR gpio_bit_reset(GPIOC, GPIO_PIN_8) // Chip select port PC8 //片选端口 + #define LCD_RS_CLR gpio_bit_reset(GPIOD, GPIO_PIN_13) // Data / command PD13 //数据/命令 + #define LCD_WR_CLR gpio_bit_reset(GPIOB, GPIO_PIN_14) // Write data PB14 //写数据 + #define LCD_RD_CLR gpio_bit_reset(GPIOD, GPIO_PIN_15) // Read data PD15 //读数据 + + #define DATAOUT(x) do{ gpio_port_write(GPIOE, x); }while(0) // Data output //数据输出 + #define DATAIN() gpio_input_port_get(GPIOE) // Data input //数据输入 +#else + #error "don't support LCD-GPIO yet" +#endif + void LCD_WR_REG(uint16_t data) { LCD_RS_CLR; @@ -145,7 +174,7 @@ uint16_t LCD_RD_DATA(void) LCD_RS_SET; LCD_CS_CLR; - LCD_RD_CLR; // double for delay. + LCD_RD_CLR; // double for delay LCD_RD_CLR; ram = DATAIN(); LCD_RD_SET; @@ -156,19 +185,20 @@ uint16_t LCD_RD_DATA(void) gpio_init(LCD_DATA_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_ALL); // LCD_DATA_PORT->ODR = 0XFFFF; gpio_port_write(LCD_DATA_PORT, 0XFFFF); + return ram; } -void LCD_GPIO_Config(void) +static inline void LCD_GPIO_Config(void) { #if defined(MKS_TFT) // GPIO_InitTypeDef GPIO_InitStructure; - /* GPIO Ports Clock Enable */ + // GPIO Ports Clock Enable // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB, ENABLE); - /* Configure GPIO pin Output Level */ + // configure GPIO pin Output Level - /* Configure GPIO pins : Pin1_Pin Pin2_Pin */ + // configure GPIO pins : Pin1_Pin Pin2_Pin // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_9; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; @@ -211,4 +241,4 @@ void LCD_HardwareConfig(void) LCD_GPIO_Config(); } -#endif +#endif // STM32_HAS_FSMC diff --git a/TFT/src/User/Hal/gd32f30x/lcd.h b/TFT/src/User/Hal/gd32f30x/lcd.h index c64acd856c..5139a7e7ff 100644 --- a/TFT/src/User/Hal/gd32f30x/lcd.h +++ b/TFT/src/User/Hal/gd32f30x/lcd.h @@ -2,7 +2,7 @@ #define _LCD_H_ #include -#include "variants.h" // for STM32_HAS_FSMC etc... +#include "variants.h" // for STM32_HAS_FSMC etc. #ifdef STM32_HAS_FSMC @@ -12,41 +12,20 @@ volatile uint16_t LCD_RAM; } LCD_TypeDef; - #define LCD_BASE ((uint32_t)(0x60000000 | 0x00FFFFFE)) // 1111 1111 1111 1111 1111 1110 - #define LCD ((LCD_TypeDef *) LCD_BASE) + #define LCD_BASE ((uint32_t)(0x60000000 | 0x00FFFFFE)) // 1111 1111 1111 1111 1111 1110 + #define LCD ((LCD_TypeDef *) LCD_BASE) #define LCD_WR_REG(regval) do{ LCD->LCD_REG = regval; }while(0) #define LCD_WR_DATA(data) do{ LCD->LCD_RAM = data; }while(0) #else - #if defined(MKS_TFT) - /* - #define LCD_WR PB14 - #define LCD_RS PD13 - #define LCD_CS PC8 - #define LCD_RD PD15 - */ - // GPIO_SetLevel - #define LCD_CS_SET gpio_bit_set(GPIOC, GPIO_PIN_8 ) // Chip Select Port PC8 //片选端口 - #define LCD_RS_SET gpio_bit_set(GPIOD, GPIO_PIN_13) // Data / command PD13 //数据/命令 - #define LCD_WR_SET gpio_bit_set(GPIOB, GPIO_PIN_14) // Write data PB14 //写数据 - #define LCD_RD_SET gpio_bit_set(GPIOD, GPIO_PIN_15) // Read data PD15 //读数据 - - #define LCD_CS_CLR gpio_bit_reset(GPIOC, GPIO_PIN_8) // Chip select port PC8 //片选端口 - #define LCD_RS_CLR gpio_bit_reset(GPIOD, GPIO_PIN_13) // Data / command PD13 //数据/命令 - #define LCD_WR_CLR gpio_bit_reset(GPIOB, GPIO_PIN_14) // Write data PB14 //写数据 - #define LCD_RD_CLR gpio_bit_reset(GPIOD, GPIO_PIN_15) // Read data PD15 //读数据 - - #define DATAOUT(x) do{gpio_port_write(GPIOE, x);}while(0) // Data output //数据输出 - #define DATAIN() gpio_input_port_get(GPIOE) // Data input //数据输入 - #else - #error "don't support LCD-GPIO yet" - #endif + void LCD_WR_REG(uint16_t data); void LCD_WR_DATA(uint16_t data); + #endif -void LCD_HardwareConfig(void); uint16_t LCD_RD_DATA(void); +void LCD_HardwareConfig(void); #endif diff --git a/TFT/src/User/Hal/gd32f30x/lcd_dma.c b/TFT/src/User/Hal/gd32f30x/lcd_dma.c index 0a1af8584e..96ca17ea62 100644 --- a/TFT/src/User/Hal/gd32f30x/lcd_dma.c +++ b/TFT/src/User/Hal/gd32f30x/lcd_dma.c @@ -1,5 +1,5 @@ #include "lcd_dma.h" -#include "variants.h" // for STM32_HAS_FSMC etc... +#include "variants.h" // for STM32_HAS_FSMC etc. #include "lcd.h" #include "spi.h" #include "LCD_Init.h" @@ -8,6 +8,7 @@ #include "debug.h" #ifdef STM32_HAS_FSMC + #if W25Qxx_SPI == _SPI1 #define W25QXX_SPI_NUM SPI0 #define W25QXX_SPI_DMA DMA0 @@ -29,30 +30,30 @@ #endif // SPI --> FSMC DMA (LCD_RAM) -// 16bits, SPI_RX to LCD_RAM. +// 16bits, SPI_RX to LCD_RAM void LCD_DMA_Config(void) { - rcu_periph_clock_enable(W25QXX_SPI_DMA_RCU); // Turn on the DMA clock - Delay_ms(5); // Wait for the DMA clock to stabilize - DMA_CHPADDR(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = (uint32_t)&SPI_DATA(W25QXX_SPI_NUM); // The peripheral address is: SPI-> DR - DMA_CHMADDR(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = (uint32_t)&LCD->LCD_RAM; // The target address is LCD_RAM - DMA_CHCNT(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = 0; // DMA1, the amount of data transferred, temporarily set to 0 - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = 0x00000000; // Reset - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<4; // Read from peripheral - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<5; // Normal mode - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<6; // Peripheral address non-incremental mode - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<7; // Memory non-incremental mode - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= LCD_DATA_16BIT<<8; // Peripheral data width is 16 bits - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= LCD_DATA_16BIT<<10; // Memory data width 16 bits - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 1<<12; // Medium priority - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<14; // Non-memory to memory mode + rcu_periph_clock_enable(W25QXX_SPI_DMA_RCU); // turn on the DMA clock + Delay_ms(5); // wait for the DMA clock to stabilize + DMA_CHPADDR(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = (uint32_t)&SPI_DATA(W25QXX_SPI_NUM); // the peripheral address is: SPI-> DR + DMA_CHMADDR(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = (uint32_t)&LCD->LCD_RAM; // the target address is LCD_RAM + DMA_CHCNT(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = 0; // DMA1, the amount of data transferred, temporarily set to 0 + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = 0x00000000; // reset + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<4; // read from peripheral + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<5; // normal mode + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<6; // peripheral address non-incremental mode + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<7; // memory non-incremental mode + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= LCD_DATA_16BIT<<8; // peripheral data width is 16 bits + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= LCD_DATA_16BIT<<10; // memory data width 16 bits + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 1<<12; // medium priority + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 0<<14; // non-memory to memory mode } #define LCD_DMA_MAX_TRANS 65535 // DMA 65535 bytes one frame // start DMA transfer from SPI->DR to FSMC // the max bytes of one frame is LCD_DMA_MAX_TRANS 65535 -void lcd_frame_segment_display(uint16_t size, uint32_t addr) +static inline void lcd_frame_segment_display(uint16_t size, uint32_t addr) { DMA_CHCNT(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) = size; @@ -63,36 +64,36 @@ void lcd_frame_segment_display(uint16_t size, uint32_t addr) W25Qxx_SPI_Read_Write_Byte((uint8_t)addr); W25Qxx_SPI_Read_Write_Byte(0XFF); // 8 dummy clock - //set SPI to 16bit DMA rx only mode - SPI_CTL0(W25QXX_SPI_NUM) &= ~(1<<6); - SPI_CTL1(W25QXX_SPI_NUM) |= 1<<0; // enable SPI rx DMA - SPI_CTL0(W25QXX_SPI_NUM) |= LCD_DATA_16BIT<<11; // 16bit data frame - SPI_CTL0(W25QXX_SPI_NUM) |= 1<<10; // rx only + // set SPI to 16bit DMA rx only mode + SPI_CTL0(W25QXX_SPI_NUM) &= ~(1<<6); // disable SPI + SPI_CTL1(W25QXX_SPI_NUM) |= 1<<0; // enable SPI rx DMA + SPI_CTL0(W25QXX_SPI_NUM) |= LCD_DATA_16BIT<<11; // 16bit data frame + SPI_CTL0(W25QXX_SPI_NUM) |= 1<<10; // rx only - DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 1<<0; // enable dma channel - SPI_CTL0(W25QXX_SPI_NUM) |= 1<<6; // enable SPI + DMA_CHCTL(W25QXX_SPI_DMA, W25QXX_SPI_DMA_CHANNEL) |= 1<<0; // enable dma channel + SPI_CTL0(W25QXX_SPI_NUM) |= 1<<6; // enable SPI - while (((DMA_INTF(W25QXX_SPI_DMA) & (1< void LCD_DMA_Config(void); void lcd_frame_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint32_t addr); diff --git a/TFT/src/User/Hal/gd32f30x/spi.c b/TFT/src/User/Hal/gd32f30x/spi.c index 5eb6a18cb1..fc3b9fce08 100644 --- a/TFT/src/User/Hal/gd32f30x/spi.c +++ b/TFT/src/User/Hal/gd32f30x/spi.c @@ -1,5 +1,5 @@ #include "spi.h" -#include "variants.h" // for SPI1_SCK_PIN etc... +#include "variants.h" // for SPI1_SCK_PIN etc. #include "GPIO_Init.h" // SPI1 default pins config @@ -69,9 +69,9 @@ void SPI_Slave_GPIO_Init(uint8_t port) GPIO_InitSet(spi_mosi[port], MGPIO_MODE_IPN, 0); // MOSI } -void SPI_GPIO_DeInit(uint8_t port) +static inline void SPI_GPIO_DeInit(uint8_t port) { - // Set all of spi pins to input + // set all of spi pins to input GPIO_InitSet(spi_sck[port], MGPIO_MODE_IPN, 0); // SCK GPIO_InitSet(spi_miso[port], MGPIO_MODE_IPN, 0); // MISO GPIO_InitSet(spi_mosi[port], MGPIO_MODE_IPN, 0); // MOSI @@ -82,9 +82,9 @@ void SPI_GPIO_DeInit(uint8_t port) void SPI_Protocol_Init(uint8_t port, uint8_t baudrate) { rcu_periph_reset_enable(rcu_spi_rst[port]); - rcu_periph_reset_disable(rcu_spi_rst[port]); + rcu_periph_reset_disable(rcu_spi_rst[port]); // reset SPI clock - rcu_periph_clock_enable(rcu_spi_en[port]); + rcu_periph_clock_enable(rcu_spi_en[port]); // enable SPI clock SPI_CTL0(spi[port]) = (0<<15) // 0:2-line 1: 1-line | (0<<14) // in bidirectional mode 0:read only 1: read/write @@ -93,9 +93,9 @@ void SPI_Protocol_Init(uint8_t port, uint8_t baudrate) | (0<<11) // 0:8-bit data frame 1:16-bit data frame | (0<<10) // 0:Full duplex 1:Receive-only | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) - | (1<<8) // This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored + | (1<<8) // this bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored | (0<<7) // 0:MSB 1:LSB - | (1<<6) // Enable SPI + | (1<<6) // enable SPI | (baudrate<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 | (1<<2) // 0:Slave 1:Master @@ -112,8 +112,9 @@ void SPI_Config(uint8_t port) void SPI_DeConfig(uint8_t port) { SPI_GPIO_DeInit(port); + rcu_periph_reset_enable(rcu_spi_rst[port]); - rcu_periph_reset_disable(rcu_spi_rst[port]); + rcu_periph_reset_disable(rcu_spi_rst[port]); // reset SPI clock } uint16_t SPI_Read_Write(uint8_t port, uint16_t d) @@ -121,5 +122,6 @@ uint16_t SPI_Read_Write(uint8_t port, uint16_t d) while ((SPI_STAT(spi[port]) & (1 << 1)) == RESET); // wait for tx empty SPI_DATA(spi[port]) = d; while ((SPI_STAT(spi[port]) & (1 << 0)) == RESET); // wait for rx no empty + return SPI_DATA(spi[port]); } diff --git a/TFT/src/User/Hal/gd32f30x/spi.h b/TFT/src/User/Hal/gd32f30x/spi.h index 28d8fa267c..3b14f32c11 100644 --- a/TFT/src/User/Hal/gd32f30x/spi.h +++ b/TFT/src/User/Hal/gd32f30x/spi.h @@ -3,16 +3,16 @@ #include -#define _SPI1 0 -#define _SPI2 1 -#define _SPI3 2 -#define _SPI_CNT 3 +#define _SPI1 0 +#define _SPI2 1 +#define _SPI3 2 +#define _SPI_CNT 3 void SPI_GPIO_Init(uint8_t port); void SPI_Slave_GPIO_Init(uint8_t port); +void SPI_Protocol_Init(uint8_t port, uint8_t baudrate); void SPI_Config(uint8_t port); void SPI_DeConfig(uint8_t port); -void SPI_Protocol_Init(uint8_t port, uint8_t baudrate); uint16_t SPI_Read_Write(uint8_t port, uint16_t d); #endif diff --git a/TFT/src/User/Hal/gd32f30x/spi_slave.c b/TFT/src/User/Hal/gd32f30x/spi_slave.c index 4f0aeec476..62f9ecdf7e 100644 --- a/TFT/src/User/Hal/gd32f30x/spi_slave.c +++ b/TFT/src/User/Hal/gd32f30x/spi_slave.c @@ -1,47 +1,56 @@ #include "spi_slave.h" -#include "includes.h" // for infoSettings, ST7920_EMULATOR etc... +#include "includes.h" // for infoSettings, ST7920_EMULATOR etc. #include "spi.h" #include "GPIO_Init.h" #include "HD44780.h" #if defined(ST7920_EMULATOR) + // TODO: // now support SPI2 and PB12 CS only // more compatibility changes are needed -// Config for SPI Channel +// config for SPI Channel #if ST7920_SPI == _SPI1 - #define ST7920_SPI_NUM SPI0 + #define ST7920_SPI_NUM SPI0 #elif ST7920_SPI == _SPI2 - #define ST7920_SPI_NUM SPI1 + #define ST7920_SPI_NUM SPI1 #elif ST7920_SPI == _SPI3 - #define ST7920_SPI_NUM SPI2 + #define ST7920_SPI_NUM SPI2 #endif -CIRCULAR_QUEUE *spi_queue = NULL; +static CIRCULAR_QUEUE * spi_queue = NULL; + +// external interruption arrangement +static inline void SPI_Slave_CS_Config(void) +{ + gpio_exti_source_select(GPIO_EVENT_PORT_GPIOB, GPIO_EVENT_PIN_12); -void SPI_Slave_CS_Config(void); // forward declaration + exti_init(EXTI_12, EXTI_INTERRUPT, EXTI_TRIG_BOTH); + exti_interrupt_flag_clear(EXTI_12); + nvic_irq_enable(EXTI10_15_IRQn, 0U, 1U); +} -void SPI_ReEnable(uint8_t mode) +static void SPI_ReEnable(uint8_t mode) { - SPI_CTL0(ST7920_SPI_NUM) = (0<<15) // 0:2-line 1: 1-line - | (0<<14) // in bidirectional mode 0:read only 1: read/write - | (0<<13) // 0:disable CRC 1:enable CRC - | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) - | (0<<11) // 0:8-bit data frame 1:16-bit data frame - | (1<<10) // 0:Full duplex 1:Receive-only - | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) - | (0<<8) // This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored - | (0<<7) // 0:MSB 1:LSB - | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 - // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 - | (0<<2) // 0:Slave 1:Master - | (mode<<1) // CPOL - | (mode<<0); // CPHA - - SPI_CTL1(ST7920_SPI_NUM) |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE + SPI_CTL0(ST7920_SPI_NUM) = (0<<15) // 0:2-line 1: 1-line + | (0<<14) // in bidirectional mode 0:read only 1: read/write + | (0<<13) // 0:disable CRC 1:enable CRC + | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) + | (0<<11) // 0:8-bit data frame 1:16-bit data frame + | (1<<10) // 0:Full duplex 1:Receive-only + | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) + | (0<<8) // this bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored + | (0<<7) // 0:MSB 1:LSB + | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 + // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 + | (0<<2) // 0:Slave 1:Master + | (mode<<1) // CPOL + | (mode<<0); // CPHA + + SPI_CTL1(ST7920_SPI_NUM) |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE } -void SPI_Slave(CIRCULAR_QUEUE *queue) +void SPI_Slave(CIRCULAR_QUEUE * queue) { spi_queue = queue; @@ -50,19 +59,17 @@ void SPI_Slave(CIRCULAR_QUEUE *queue) spi_queue->index_r = spi_queue->index_w = spi_queue->count = 0; SPI_Slave_GPIO_Init(ST7920_SPI); - GPIO_InitSet(PB12, MGPIO_MODE_IPU, 0); // CS + GPIO_InitSet(PB12, MGPIO_MODE_IPU, 0); // CS nvic_irq_enable(SPI1_IRQn, 1U, 0U); rcu_periph_clock_enable(RCU_SPI1); SPI_Slave_CS_Config(); - SPI_ReEnable(0); // spi mode0 + SPI_ReEnable(0); // spi mode0 if ((GPIO_ISTAT(GPIOB) & (1<<12)) != 0) - { SPI_CTL0(ST7920_SPI_NUM) |= (1<<6); - } } void SPI_SlaveDeInit(void) @@ -75,13 +82,7 @@ void SPI_SlaveDeInit(void) spi_queue = NULL; } -void SPI1_IRQHandler(void) -{ - spi_queue->data[spi_queue->index_w] = SPI_DATA(ST7920_SPI_NUM); - spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; -} - -bool SPI_SlaveGetData(uint8_t *data) +bool SPI_SlaveGetData(uint8_t * data) { bool dataRead = false; @@ -96,19 +97,17 @@ bool SPI_SlaveGetData(uint8_t *data) return dataRead; } -// External interruption arrangement -void SPI_Slave_CS_Config(void) +void SPI1_IRQHandler(void) { - gpio_exti_source_select(GPIO_EVENT_PORT_GPIOB, GPIO_EVENT_PIN_12); - - exti_init(EXTI_12, EXTI_INTERRUPT, EXTI_TRIG_BOTH); - exti_interrupt_flag_clear(EXTI_12); - nvic_irq_enable(EXTI10_15_IRQn, 0U, 1U); + spi_queue->data[spi_queue->index_w] = SPI_DATA(ST7920_SPI_NUM); + spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; } -#endif + +#endif // ST7920_EMULATOR #ifdef HAS_EMULATOR -// External interruption + +// external interruption void EXTI10_15_IRQHandler(void) { switch (infoSettings.marlin_type) @@ -123,18 +122,19 @@ void EXTI10_15_IRQHandler(void) case LCD12864: if ((GPIO_ISTAT(GPIOB) & (1<<12)) != 0) { - SPI_ReEnable(!!(GPIO_ISTAT(GPIOB)& (1<<13))); // Adaptive spi mode0 / mode3 + SPI_ReEnable(!!(GPIO_ISTAT(GPIOB) & (1<<13))); // adaptive spi mode0 / mode3 SPI_CTL0(ST7920_SPI_NUM) |= (1<<6); } else { rcu_periph_reset_enable(RCU_SPI1RST); - rcu_periph_reset_disable(RCU_SPI1RST); // Reset SPI1 + rcu_periph_reset_disable(RCU_SPI1RST); // reset SPI1 } - EXTI_PD = 1<<12; // Clear interrupt status register + EXTI_PD = 1<<12; // clear interrupt status register break; #endif } } -#endif + +#endif // HAS_EMULATOR diff --git a/TFT/src/User/Hal/gd32f30x/spi_slave.h b/TFT/src/User/Hal/gd32f30x/spi_slave.h index 58ce091326..c6be7b5586 100644 --- a/TFT/src/User/Hal/gd32f30x/spi_slave.h +++ b/TFT/src/User/Hal/gd32f30x/spi_slave.h @@ -5,8 +5,8 @@ #include #include "CircularQueue.h" -void SPI_Slave(CIRCULAR_QUEUE *queue); +void SPI_Slave(CIRCULAR_QUEUE * queue); void SPI_SlaveDeInit(void); -bool SPI_SlaveGetData(uint8_t *data); +bool SPI_SlaveGetData(uint8_t * data); #endif diff --git a/TFT/src/User/Hal/gd32f30x/timer_pwm.c b/TFT/src/User/Hal/gd32f30x/timer_pwm.c index 89923f7315..ea2bfe2ecd 100644 --- a/TFT/src/User/Hal/gd32f30x/timer_pwm.c +++ b/TFT/src/User/Hal/gd32f30x/timer_pwm.c @@ -8,27 +8,28 @@ typedef struct { } TIMER; static TIMER pwmTimer[_TIM_CNT] = { - {TIMER0, RCU_TIMER0, 2}, // Timer1 APB2 bit0 - {TIMER1, RCU_TIMER1, 1}, // Timer2 APB1 bit0 - {TIMER2, RCU_TIMER2, 1}, // Timer3 APB1 bit1 - {TIMER3, RCU_TIMER3, 1}, // Timer4 APB1 bit2 - {TIMER4, RCU_TIMER4, 1}, // Timer5 APB1 bit3 - {TIMER5, RCU_TIMER5, 1}, // Timer6 APB1 bit4 - {TIMER6, RCU_TIMER6, 1}, // Timer7 APB1 bit5 - {TIMER7, RCU_TIMER7, 2}, // Timer8 APB2 bit1 + {TIMER0, RCU_TIMER0, 2}, // Timer1 APB2 bit0 + {TIMER1, RCU_TIMER1, 1}, // Timer2 APB1 bit0 + {TIMER2, RCU_TIMER2, 1}, // Timer3 APB1 bit1 + {TIMER3, RCU_TIMER3, 1}, // Timer4 APB1 bit2 + {TIMER4, RCU_TIMER4, 1}, // Timer5 APB1 bit3 + {TIMER5, RCU_TIMER5, 1}, // Timer6 APB1 bit4 + {TIMER6, RCU_TIMER6, 1}, // Timer7 APB1 bit5 + {TIMER7, RCU_TIMER7, 2}, // Timer8 APB2 bit1 {TIMER8, RCU_TIMER8, 2}, // Timer9 APB2 bit16 {TIMER9, RCU_TIMER9, 2}, // Timer10 APB2 bit17 {TIMER10, RCU_TIMER10, 2}, // Timer11 APB2 bit18 - {TIMER11, RCU_TIMER11, 1}, // Timer12 APB1 bit6 - {TIMER12, RCU_TIMER12, 1}, // Timer13 APB1 bit7 - {TIMER13, RCU_TIMER13, 1}, // Timer14 APB1 bit8 + {TIMER11, RCU_TIMER11, 1}, // Timer12 APB1 bit6 + {TIMER12, RCU_TIMER12, 1}, // Timer13 APB1 bit7 + {TIMER13, RCU_TIMER13, 1}, // Timer14 APB1 bit8 }; void TIM_PWM_SetDutyCycle(uint16_t tim_ch, uint8_t duty) { uint16_t timerIndex = TIMER_GET_TIM(tim_ch); uint16_t channel = TIMER_GET_CH(tim_ch); - const TIMER* timer = &pwmTimer[timerIndex]; + const TIMER * timer = &pwmTimer[timerIndex]; + switch (channel) { case 0: TIMER_CH0CV(timer->tim) = duty; break; @@ -42,29 +43,29 @@ void TIM_PWM_Init(uint16_t tim_ch) { uint16_t timerIndex = TIMER_GET_TIM(tim_ch); uint16_t channel = TIMER_GET_CH(tim_ch); - const TIMER* timer = &pwmTimer[timerIndex]; + const TIMER * timer = &pwmTimer[timerIndex]; uint32_t timerTmpClk = (timer->apb == 1) ? mcuClocks.PCLK1_Timer_Frequency : mcuClocks.PCLK2_Timer_Frequency; - rcu_periph_clock_enable(timer->rcc_src); + rcu_periph_clock_enable(timer->rcc_src); // enable timer clock - // Set PWM frequency to 500Hz + // set PWM frequency to 500Hz TIMER_CAR(timer->tim) = 100 - 1; TIMER_PSC(timer->tim) = timerTmpClk / (500 * 100) - 1; switch (channel) { - case 0: TIMER_CHCTL0(timer->tim) |= (6<<4) | (1<<3); break; // CH1 PWM0 mode - case 1: TIMER_CHCTL0(timer->tim) |= (6<<12) | (1<<11); break; // CH2 PWM0 mode - case 2: TIMER_CHCTL1(timer->tim) |= (6<<4) | (1<<3); break; // CH3 PWM0 mode - case 3: TIMER_CHCTL1(timer->tim) |= (6<<12) | (1<<11); break; // CH4 PWM0 mode + case 0: TIMER_CHCTL0(timer->tim) |= (6<<4) | (1<<3); break; // CH1 PWM0 mode + case 1: TIMER_CHCTL0(timer->tim) |= (6<<12) | (1<<11); break; // CH2 PWM0 mode + case 2: TIMER_CHCTL1(timer->tim) |= (6<<4) | (1<<3); break; // CH3 PWM0 mode + case 3: TIMER_CHCTL1(timer->tim) |= (6<<12) | (1<<11); break; // CH4 PWM0 mode } - TIMER_CHCTL2(timer->tim) |= 1 << (2 * channel); //enable channle - TIMER_CTL0(timer->tim) = (1 << 7) // Auto-reload preload enable - | (1 << 0); // Enbale timer + TIMER_CHCTL2(timer->tim) |= 1 << (2 * channel); // enable channle + TIMER_CTL0(timer->tim) = (1 << 7) // auto-reload preload enable + | (1 << 0); // enbale timer if (timer->tim == TIMER0 || timer->tim == TIMER7) { // TIM1 & TIM8 advanced timer need config BDTR register for PWM - TIMER_CCHP(timer->tim) |= 1 << 15; // Main output enable + TIMER_CCHP(timer->tim) |= 1 << 15; // main output enable } } diff --git a/TFT/src/User/Hal/gd32f30x/timer_pwm.h b/TFT/src/User/Hal/gd32f30x/timer_pwm.h index 8fa08d5535..efb48c2be6 100644 --- a/TFT/src/User/Hal/gd32f30x/timer_pwm.h +++ b/TFT/src/User/Hal/gd32f30x/timer_pwm.h @@ -96,7 +96,7 @@ #define _TIM15_CH4 (((_TIM15)<<8) + 3) #define TIMER_GET_TIM(n) ((n>>8) & 0xFF) -#define TIMER_GET_CH(n) (n & 0xFF) +#define TIMER_GET_CH(n) (n & 0xFF) void TIM_PWM_SetDutyCycle(uint16_t tim_ch, uint8_t duty); void TIM_PWM_Init(uint16_t tim_ch); diff --git a/TFT/src/User/Hal/gd32f30x/uart.c b/TFT/src/User/Hal/gd32f30x/uart.c index a52feac4fe..cafdcd1979 100644 --- a/TFT/src/User/Hal/gd32f30x/uart.c +++ b/TFT/src/User/Hal/gd32f30x/uart.c @@ -1,5 +1,5 @@ #include "uart.h" -#include "variants.h" // for USART1_TX_PIN etc... +#include "variants.h" // for USART1_TX_PIN etc. #include "GPIO_Init.h" // USART1 (GD USART0) default pins config @@ -55,32 +55,32 @@ static rcu_periph_enum rcu_uart_en[_UART_CNT] = { }; static uint32_t const uart[_UART_CNT] = { - USART0, // TX--PA9 RX--PA10 - USART1, // TX--PA2 RX--PA3 - USART2, // TX--PB10 RX--PB11 - UART3, // TX--PC10 RX--PC11 - UART4, // TX--PC12 RX--PD2 + USART0, // TX--PA9 RX--PA10 + USART1, // TX--PA2 RX--PA3 + USART2, // TX--PB10 RX--PB11 + UART3, // TX--PC10 RX--PC11 + UART4, // TX--PC12 RX--PD2 }; static const uint16_t uart_tx[_UART_CNT] = {USART1_TX_PIN, USART2_TX_PIN, USART3_TX_PIN, UART4_TX_PIN, UART5_TX_PIN}; // TX static const uint16_t uart_rx[_UART_CNT] = {USART1_RX_PIN, USART2_RX_PIN, USART3_RX_PIN, UART4_RX_PIN, UART5_RX_PIN}; // RX -void UART_GPIO_Init(uint8_t port) +static inline void UART_GPIO_Init(uint8_t port) { GPIO_InitSet(uart_tx[port], MGPIO_MODE_AF_PP, 0); GPIO_InitSet(uart_rx[port], MGPIO_MODE_IPU, 0); } -void UART_GPIO_DeInit(uint8_t port) +static inline void UART_GPIO_DeInit(uint8_t port) { // set tx/rx to input GPIO_InitSet(uart_tx[port], MGPIO_MODE_IPN, 0); GPIO_InitSet(uart_rx[port], MGPIO_MODE_IPN, 0); } -void UART_Protocol_Init(uint8_t port, uint32_t baud) +static inline void UART_Protocol_Init(uint8_t port, uint32_t baud) { - rcu_periph_clock_enable(rcu_uart_en[port]); // Enable clock + rcu_periph_clock_enable(rcu_uart_en[port]); // enable clock usart_deinit(uart[port]); usart_baudrate_set(uart[port], baud); @@ -94,7 +94,7 @@ void UART_Protocol_Init(uint8_t port, uint32_t baud) usart_enable(uart[port]); } -void UART_IRQ_Init(uint8_t port, uint16_t usart_it, bool idle_interrupt) +static inline void UART_IRQ_Init(uint8_t port, uint16_t usart_it, bool idle_interrupt) { uint32_t IRQ_Channel[_UART_CNT] = {USART0_IRQn, USART1_IRQn, USART2_IRQn, UART3_IRQn, UART4_IRQn}; nvic_irq_enable(IRQ_Channel[port], 0U, 0U); @@ -110,28 +110,28 @@ void UART_Config(uint8_t port, uint32_t baud, uint16_t usart_it, bool idle_inter { UART_Protocol_Init(port, baud); UART_IRQ_Init(port, usart_it, idle_interrupt); - UART_GPIO_Init(port); // After all initialization is completed, enable IO, otherwise a 0xFF will be sent automatically after power-on + UART_GPIO_Init(port); // after all initialization is completed, enable IO, otherwise a 0xFF will be sent automatically after power-on } void UART_DeConfig(uint8_t port) { UART_GPIO_DeInit(port); - rcu_periph_reset_enable(rcu_uart_rst[port]); // Reset clock - rcu_periph_reset_disable(rcu_uart_rst[port]); + rcu_periph_reset_enable(rcu_uart_rst[port]); + rcu_periph_reset_disable(rcu_uart_rst[port]); // reset clock } void UART_Write(uint8_t port, uint8_t d) { - while ((USART_STAT0(uart[port]) & (1 << USART_FLAG_TC)) == (uint16_t)RESET); + while ((USART_STAT0(uart[port]) & (1 << USART_FLAG_TC)) == (uint16_t)RESET); // wait USART_DATA(uart[port]) = ((uint16_t)d & (uint16_t)0x01FF); } -void UART_Puts(uint8_t port, uint8_t *str) +void UART_Puts(uint8_t port, uint8_t * str) { while (*str) { - while ((USART_STAT0(uart[port]) & (1 << USART_FLAG_TC)) == (uint16_t)RESET); + while ((USART_STAT0(uart[port]) & (1 << USART_FLAG_TC)) == (uint16_t)RESET); // wait USART_DATA(uart[port]) = ((uint16_t)*str++ & (uint16_t)0x01FF); } } diff --git a/TFT/src/User/Hal/gd32f30x/uart.h b/TFT/src/User/Hal/gd32f30x/uart.h index 8617f44811..a48ea44a80 100644 --- a/TFT/src/User/Hal/gd32f30x/uart.h +++ b/TFT/src/User/Hal/gd32f30x/uart.h @@ -4,17 +4,16 @@ #include #include -#define _USART1 0 // USART0 -#define _USART2 1 // USART1 -#define _USART3 2 // USART2 -#define _UART4 3 // UART3 -#define _UART5 4 // UART4 -#define _USART6 5 -#define _UART_CNT 6 +#define _USART1 0 // USART0 +#define _USART2 1 // USART1 +#define _USART3 2 // USART2 +#define _UART4 3 // UART3 +#define _UART5 4 // UART4 +#define _UART_CNT 5 void UART_Config(uint8_t port, uint32_t baud, uint16_t usart_it, bool idle_interrupt); void UART_DeConfig(uint8_t port); -void UART_Puts(uint8_t port, uint8_t *str); void UART_Write(uint8_t port, uint8_t d); +void UART_Puts(uint8_t port, uint8_t * str); #endif diff --git a/TFT/src/User/Hal/sd.c b/TFT/src/User/Hal/sd.c index 41b4bde27e..943a4dd569 100644 --- a/TFT/src/User/Hal/sd.c +++ b/TFT/src/User/Hal/sd.c @@ -1,35 +1,77 @@ -#include "variants.h" // for SD_SPI_SUPPORT etc... +#include "sd.h" #ifdef SD_SPI_SUPPORT -#include "sd.h" + #include "GPIO_Init.h" #include "spi.h" -uint8_t SD_Type = 0; //SDCard type - -/***************************************** SD SPI Interface ported by the underlying pattern***********************************************/ +// SDCard type definition +#define SD_TYPE_ERR 0X00 +#define SD_TYPE_MMC 0X01 +#define SD_TYPE_V1 0X02 +#define SD_TYPE_V2 0X04 +#define SD_TYPE_V2HC 0X06 + +// SDCard instruction table +#define CMD0 0 // card reset +#define CMD1 1 +#define CMD8 8 // command 8, SEND_IF_COND +#define CMD9 9 // command 9 to read CSD data +#define CMD10 10 // command 10, read CID data +#define CMD12 12 // command 12, stop data transmission +#define CMD16 16 // command 16, set sector size should return 0x00 +#define CMD17 17 // command 17, read sector +#define CMD18 18 // command 18, read Multi sector +#define CMD23 23 // command 23, set N blocks to be erased before multi-sector write +#define CMD24 24 // command 24, write sector +#define CMD25 25 // command 25, write multiple sectors +#define CMD41 41 // command 41, should return 0x00 +#define CMD55 55 // command 55, should return 0x01 +#define CMD58 58 // command 58, read OCR information +#define CMD59 59 // command 59, enable / disable CRC, should return 0x00 + +// data write response word meaning +#define SD_DATA_OK 0x05 +#define SD_DATA_CRC_ERROR 0x0B +#define SD_DATA_WRITE_ERROR 0x0D +#define SD_DATA_OTHER_ERROR 0xFF + +// SD Card Response Marker +#define SD_RESPONSE_NO_ERROR 0x00 // response error +#define SD_IN_IDLE_STATE 0x01 // idle state +#define SD_ERASE_RESET 0x02 // erase reset +#define SD_ILLEGAL_COMMAND 0x04 +#define SD_COM_CRC_ERROR 0x08 +#define SD_ERASE_SEQUENCE_ERROR 0x10 +#define SD_ADDRESS_ERROR 0x20 +#define SD_PARAMETER_ERROR 0x40 +#define SD_RESPONSE_FAILURE 0xFF + +static uint8_t SD_Type = 0; // SDCard type + +/**************************** SD SPI Interface ported by the underlying pattern ****************************/ //#define SD_SPI _SPI1 //#define SD_LOW_SPEED 7 //#define SD_HIGH_SPEED 1 -//Read and write functions -uint8_t SD_SPI_Read_Write_Byte(uint8_t data) +// read and write functions +static uint8_t SD_SPI_Read_Write_Byte(uint8_t data) { return SPI_Read_Write(SD_SPI, data); } -//Chip Select -void SD_SPI_CS_Set(uint8_t level) +// Chip Select +static void SD_SPI_CS_Set(uint8_t level) { GPIO_SetLevel(SD_CS_PIN, level); } #ifdef SD_CD_PIN -//SD_CD insert detect -void SD_CD_WP_Init(void) -{ - GPIO_InitSet(SD_CD_PIN, MGPIO_MODE_IPU, 0); -} + // SD_CD insert detect + static inline void SD_CD_WP_Init(void) + { + GPIO_InitSet(SD_CD_PIN, MGPIO_MODE_IPU, 0); + } #endif uint8_t SD_CD_Inserted(void) @@ -41,8 +83,8 @@ uint8_t SD_CD_Inserted(void) #endif } -//initialization -void SD_SPI_Init(void) +// initialization +static inline void SD_SPI_Init(void) { #ifdef SD_CD_PIN SD_CD_WP_Init(); @@ -58,201 +100,229 @@ void SD_DeInit(void) SPI_DeConfig(SD_SPI); } -//Low speed required during initialization -void SD_SetLowSpeed(void) +// low speed required during initialization +static inline void SD_SetLowSpeed(void) { SPI_Protocol_Init(SD_SPI, SD_LOW_SPEED); } -//Use high speed when transferring data -void SD_SetHighSpeed(void) +// use high speed when transferring data +static inline void SD_SetHighSpeed(void) { SPI_Protocol_Init(SD_SPI, SD_HIGH_SPEED); } -/******************************************************************************************************************/ + +/****************************************************************************************************************/ /************************************************************************************ -**Deselect, release SPI bus +* deselect, release SPI bus *************************************************************************************/ -void SD_Cancel_CS(void) +static void SD_Cancel_CS(void) { SD_SPI_CS_Set(1); - SD_SPI_Read_Write_Byte(0xff); //Provides additional 8 clocks + SD_SPI_Read_Write_Byte(0xff); // provides additional 8 clocks } /************************************************************************************ -**Select the SD card and wait for the card to be ready -**Return value: 0, success; 1, failure; +* select the SD card and wait for the card to be ready +* return value: 0, success; 1, failure; *************************************************************************************/ -uint8_t SD_Select(void) +static inline uint8_t SD_Select(void) { SD_SPI_CS_Set(0); - if (SD_Wait_Ready() == 0) return 0; //Waiting for success + + if (SD_Wait_Ready() == 0) + return 0; // waiting for success + SD_Cancel_CS(); - return 1; //Wait for failure + + return 1; // wait for failure } /************************************************************************************ -**Waiting for the card to be ready -** Return value: 0, ready; other, error code -**************************************************************************************/ +* waiting for the card to be ready +* return value: 0, ready; other, error code +*************************************************************************************/ uint8_t SD_Wait_Ready(void) { uint32_t t = 0; do { - if (SD_SPI_Read_Write_Byte(0XFF) == 0XFF) return 0; //OK + if (SD_SPI_Read_Write_Byte(0XFF) == 0XFF) + return 0; // OK + t++; - } while (t<0XFFFFFF); //wait + } while (t<0XFFFFFF); // wait + return 1; } /************************************************************************************ -** Waiting for SD card response -** Response: the response value to get -** Return value: 0, the response value was successfully obtained -** Other, failed to get response value +* waiting for SD card response +* response: the response value to get +* return value: 0, the response value was successfully obtained +* other, failed to get response value *************************************************************************************/ uint8_t SD_Get_Ack(uint8_t Response) { - uint16_t Count = 0xFFFF; //Wait times + uint16_t Count = 0xFFFF; // wait times - while ((SD_SPI_Read_Write_Byte(0XFF) != Response) && Count) Count--; //Waiting for an accurate response + while ((SD_SPI_Read_Write_Byte(0XFF) != Response) && Count) Count--; // waiting for an accurate response if (Count == 0) - return SD_RESPONSE_FAILURE; //Get response failed + return SD_RESPONSE_FAILURE; // get response failed else - return SD_RESPONSE_NO_ERROR; //Respond correctly + return SD_RESPONSE_NO_ERROR; // respond correctly } -/************************************************************************************* -** Read the contents of a packet from the SD card -** buf: data buffer area -** len: Length of data to be read. -** Return value: 0, success; others, failure; -****************************************************************************************/ -uint8_t SD_RecvData(uint8_t * buf, uint16_t len) +/************************************************************************************ +* read the contents of a packet from the SD card +* buf: data buffer area +* len: Length of data to be read. +* return value: 0, success; others, failure; +*************************************************************************************/ +static uint8_t SD_RecvData(uint8_t * buf, uint16_t len) { - if (SD_Get_Ack(0xFE)) return 1; //Wait for SD card to send back data start token 0xFE - while (len--) //Start receiving data + if (SD_Get_Ack(0xFE)) + return 1; // wait for SD card to send back data start token 0xFE + + while (len--) // start receiving data { *buf = SD_SPI_Read_Write_Byte(0xFF); buf++; } - //Here are 2 pseudo CRCs��dummy CRC�� + + // here are 2 pseudo CRCs dummy CRC SD_SPI_Read_Write_Byte(0xFF); SD_SPI_Read_Write_Byte(0xFF); - return 0; //Read succeeded + + return 0; // read succeeded } /************************************************************************************ -** Write the contents of a data packet to the SD card 512 bytes -** buf: data buffer area -** cmd: instruction -** Return value: 0, success; others, failure; +* write the contents of a data packet to the SD card 512 bytes +* buf: data buffer area +* cmd: instruction +* return value: 0, success; others, failure; *************************************************************************************/ -uint8_t SD_Send_Data(uint8_t * buf, uint8_t cmd) +static uint8_t SD_Send_Data(uint8_t * buf, uint8_t cmd) { uint16_t t; - if (SD_Wait_Ready()) return 1; //Waiting for preparation to fail + if (SD_Wait_Ready()) + return 1; // waiting for preparation to fail + SD_SPI_Read_Write_Byte(cmd); - if (cmd != 0XFD) //Not an end instruction + + if (cmd != 0XFD) // not an end instruction { for (t = 0; t < 512; t++) SD_SPI_Read_Write_Byte(buf[t]); - SD_SPI_Read_Write_Byte(0xFF); //Ignore crc + SD_SPI_Read_Write_Byte(0xFF); // ignore crc SD_SPI_Read_Write_Byte(0xFF); - t = SD_SPI_Read_Write_Byte(0xFF); //Receive response - if ((t & 0x1F) != 0x05) return 2; //Response error + t = SD_SPI_Read_Write_Byte(0xFF); // receive response + + if ((t & 0x1F) != 0x05) + return 2; // response error } - return 0; //Write success + + return 0; // write success } -/************************************************************************************* -**Send a command to the SD card -** Enter: uint8_t cmd command -** uint32_t arg command parameters -** uint8_t crc crc check value -** Return value: Response returned by SD card -***************************************************************************************/ -uint8_t SD_SendCmd(uint8_t cmd, uint32_t arg, uint8_t crc) +/************************************************************************************ +* send a command to the SD card +* enter: uint8_t cmd command +* uint32_t arg command parameters +* uint8_t crc crc check value +* return value: Response returned by SD card +*************************************************************************************/ +static uint8_t SD_SendCmd(uint8_t cmd, uint32_t arg, uint8_t crc) { uint8_t r1; uint8_t Retry = 0; - SD_Cancel_CS(); //Cancel last selection - if (SD_Select()) return 0XFF; //Chip Select Failure - //send - SD_SPI_Read_Write_Byte(cmd | 0x40); //Write commands separately + SD_Cancel_CS(); // cancel last selection + + if (SD_Select()) + return 0XFF; // Chip Select Failure + + // send + SD_SPI_Read_Write_Byte(cmd | 0x40); // write commands separately SD_SPI_Read_Write_Byte(arg >> 24); SD_SPI_Read_Write_Byte(arg >> 16); SD_SPI_Read_Write_Byte(arg >> 8); SD_SPI_Read_Write_Byte(arg); SD_SPI_Read_Write_Byte(crc); - if (cmd == CMD12) SD_SPI_Read_Write_Byte(0xff); //Skip a stuff byte when stop reading - //Waiting for a response, or exiting after a timeout + + if (cmd == CMD12) + SD_SPI_Read_Write_Byte(0xff); // skip a stuff byte when stop reading + + // waiting for a response, or exiting after a timeout Retry = 0X1F; + do { r1 = SD_SPI_Read_Write_Byte(0xFF); } while ((r1 & 0X80) && Retry--); - //Return status value + + // return status value return r1; } -/************************************************************************************* -**Get CID information of SD card, including manufacturer information -**Input: uint8_t * cid_data (memory for CID, at least 16Byte) -**Return value: 0: NO_ERR -** 1��error +/************************************************************************************ +* get CID information of SD card, including manufacturer information +* input: uint8_t * cid_data (memory for CID, at least 16Byte) +* return value: 0: NO_ERR +* 1: error *************************************************************************************/ uint8_t SD_GetCID(uint8_t * cid_data) { uint8_t r1; - //Send CMD10 command, read CID + // send CMD10 command, read CID r1 = SD_SendCmd(CMD10, 0, 0x01); + if (r1 == 0x00) - { - r1 = SD_RecvData(cid_data, 16); //Receive 16 bytes of data - } - SD_Cancel_CS(); //Cancel film selection + r1 = SD_RecvData(cid_data, 16); // receive 16 bytes of data + + SD_Cancel_CS(); // cancel film selection + if (r1) return 1; else return 0; } -/************************************************************************************* -Get CSD information of SD card, including capacity and speed information -Input: uint8_t * cid_data (memory for CID, at least 16Byte) -Return value: 0: NO_ERR -1: error +/************************************************************************************ +* get CSD information of SD card, including capacity and speed information +* input: uint8_t * cid_data (memory for CID, at least 16Byte) +* return value: 0: NO_ERR +* 1: error *************************************************************************************/ uint8_t SD_GetCSD(uint8_t * csd_data) { uint8_t r1; - r1 = SD_SendCmd(CMD9, 0, 0x01); //Send CMD9 command, read CSD + r1 = SD_SendCmd(CMD9, 0, 0x01); // send CMD9 command, read CSD + if (r1 == 0) - { - r1 = SD_RecvData(csd_data, 16); //Receive 16 bytes of data - } - SD_Cancel_CS(); //Cancel film selection + r1 = SD_RecvData(csd_data, 16); // receive 16 bytes of data + + SD_Cancel_CS(); // cancel film selection + if (r1) return 1; else return 0; } -/************************************************************ -**Get the total number of sectors of the SD card (the number of sectors) -**Return value: 0: Error in taking capacity -**Other: SD card capacity (number of sectors / 512 bytes) -**The number of bytes per sector must be 512 bytes, because if it is not 512 bytes, the initialization cannot pass. -*************************************************************/ +/************************************************************************************ +* get the total number of sectors of the SD card (the number of sectors) +* return value: 0: Error in taking capacity +* other: SD card capacity (number of sectors / 512 bytes) +* the number of bytes per sector must be 512 bytes, because if it is not 512 bytes, the initialization cannot pass +*************************************************************************************/ uint32_t SD_Get_Sector_Count(void) { uint8_t csd[16]; @@ -260,171 +330,203 @@ uint32_t SD_Get_Sector_Count(void) uint8_t n; uint16_t csize; - if (SD_GetCSD(csd) != 0) return 0; //Get CSD information, if an error occurs during the period, return 0 - if ((csd[0] & 0xC0) == 0x40) //V2.00 card, if it is SDHC card, calculate it as follows + if (SD_GetCSD(csd) != 0) + return 0; // get CSD information, if an error occurs during the period, return 0 + + if ((csd[0] & 0xC0) == 0x40) // V2.00 card, if it is SDHC card, calculate it as follows { csize = csd[9] + ((uint16_t)csd[8] << 8) + 1; - Capacity = (uint32_t)csize << 10; //Get the number of sectors + Capacity = (uint32_t)csize << 10; // get the number of sectors } - else //V1.XX card + else // V1.XX card { n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2; csize = (csd[8] >> 6) + ((uint16_t)csd[7] << 2) + ((uint16_t)(csd[6] & 3) << 10) + 1; - Capacity= (uint32_t)csize << (n - 9); //Get the number of sectors + Capacity= (uint32_t)csize << (n - 9); // get the number of sectors } + return Capacity; } -/********************************** -Initialize SD card -***********************************/ +/************************************************************************************ +* initialize SD card +*************************************************************************************/ uint8_t SD_Init(void) { - uint8_t r1; // Store the return value of the SD card - uint16_t retry; // Used for timeout counting + uint8_t r1; // store the return value of the SD card + uint16_t retry; // used for timeout counting uint8_t buf[4]; uint16_t i; - SD_SPI_Init(); //Initialize IO + SD_SPI_Init(); // initialize IO SD_SetLowSpeed(); - for (i = 0; i < 10; i++) SD_SPI_Read_Write_Byte(0XFF); //Send a minimum of 74 pulses + + for (i = 0; i < 10; i++) SD_SPI_Read_Write_Byte(0XFF); // send a minimum of 74 pulses + retry = 20; + do { - r1 = SD_SendCmd(CMD0, 0, 0x95); //Enter IDLE state + r1 = SD_SendCmd(CMD0, 0, 0x95); // enter IDLE state } while ((r1 != 0X01) && retry--); - SD_Type = 0; // No card by default + + SD_Type = 0; // no card by default + if (r1 == 0X01) { - if (SD_SendCmd(CMD8, 0x1AA, 0x87) == 1) //SD V2.0 + if (SD_SendCmd(CMD8, 0x1AA, 0x87) == 1) // SD V2.0 { - for (i = 0; i < 4; i++) buf[i] = SD_SPI_Read_Write_Byte(0XFF); //Get trailing return value of R7 resp - if (buf[2] == 0X01 && buf[3] == 0XAA) //Does the card support 2.7 ~ 3.6V + for (i = 0; i < 4; i++) buf[i] = SD_SPI_Read_Write_Byte(0XFF); // get trailing return value of R7 resp + + if (buf[2] == 0X01 && buf[3] == 0XAA) // does the card support 2.7 ~ 3.6V { retry = 0XFFFE; + do { - SD_SendCmd(CMD55, 0, 0X01); //Send CMD55 - r1 = SD_SendCmd(CMD41, 0x40000000, 0X01); //Send CMD41 + SD_SendCmd(CMD55, 0, 0X01); // send CMD55 + r1 = SD_SendCmd(CMD41, 0x40000000, 0X01); // send CMD41 } while (r1 && retry--); - if (retry && SD_SendCmd(CMD58, 0, 0X01) == 0) //Identification of SD2.0 card version started + + if (retry && SD_SendCmd(CMD58, 0, 0X01) == 0) // identification of SD2.0 card version started { - for (i = 0; i < 4; i++) buf[i] = SD_SPI_Read_Write_Byte(0XFF); //Get OCR value - if (buf[0] & 0x40) SD_Type=SD_TYPE_V2HC; //Check CCS - else SD_Type = SD_TYPE_V2; + for (i = 0; i < 4; i++) buf[i] = SD_SPI_Read_Write_Byte(0XFF); // get OCR value + + if (buf[0] & 0x40) // check CCS + SD_Type=SD_TYPE_V2HC; + else + SD_Type = SD_TYPE_V2; } } } - else //SD V1.x/ MMC V3 + else // SD V1.x/ MMC V3 { - SD_SendCmd(CMD55, 0, 0X01); //Send CMD55 - r1 = SD_SendCmd(CMD41, 0, 0X01); //Send CMD41 + SD_SendCmd(CMD55, 0, 0X01); // send CMD55 + r1 = SD_SendCmd(CMD41, 0, 0X01); // send CMD41 + if (r1 <= 1) { SD_Type = SD_TYPE_V1; retry = 0XFFFE; - do //Waiting to exit IDLE mode + + do // waiting to exit IDLE mode { - SD_SendCmd(CMD55, 0, 0X01); //Send CMD55 - r1 = SD_SendCmd(CMD41, 0, 0X01); //Send CMD41 + SD_SendCmd(CMD55, 0, 0X01); // send CMD55 + r1 = SD_SendCmd(CMD41, 0, 0X01); // send CMD41 } while (r1 && retry--); } - else //MMCCard does not support CMD55 + CMD41 recognition + else // MMCCard does not support CMD55 + CMD41 recognition { - SD_Type = SD_TYPE_MMC; //MMC V3 + SD_Type = SD_TYPE_MMC; // MMC V3 retry = 0XFFFE; - do //Waiting to exit IDLE mode + + do // waiting to exit IDLE mode { - r1 = SD_SendCmd(CMD1, 0, 0X01); //Send CMD1 + r1 = SD_SendCmd(CMD1, 0, 0X01); // send CMD1 } while (r1 && retry--); } + if (retry == 0 || SD_SendCmd(CMD16, 512, 0X01) != 0) - SD_Type = SD_TYPE_ERR; //Wrong card + SD_Type = SD_TYPE_ERR; // wrong card } } - SD_Cancel_CS(); //Cancel film selection + + SD_Cancel_CS(); // cancel film selection SD_SetHighSpeed(); + if (SD_Type) return 0; else if (r1) return r1; - return 0xaa;//�������� + + return 0xaa; } -/************************************************************************************* -**Read SD card -** buf: data buffer area -** sector: sector -** cnt: Number of sectors -** Return value: 0, ok; other, failed. +/************************************************************************************ +* read SD card +* buf: data buffer area +* sector: sector +* cnt: Number of sectors +* return value: 0, ok; other, failed. *************************************************************************************/ -uint8_t SD_ReadDisk(uint8_t * buf, uint32_t sector, uint8_t cnt) //Read SD card, fatfs / usb calls x +uint8_t SD_ReadDisk(uint8_t * buf, uint32_t sector, uint8_t cnt) // read SD card, fatfs / usb calls x { uint8_t r1; - if (SD_Type != SD_TYPE_V2HC) sector <<= 9; //Translate to byte address + if (SD_Type != SD_TYPE_V2HC) + sector <<= 9; // translate to byte address + if (cnt == 1) { - r1 = SD_SendCmd(CMD17, sector, 0X01); //Read command - if (r1 == 0) //Instruction sent successfully - { - r1 = SD_RecvData(buf, 512); //Receive 512 bytes - } + r1 = SD_SendCmd(CMD17, sector, 0X01); // read command + + if (r1 == 0) // instruction sent successfully + r1 = SD_RecvData(buf, 512); // receive 512 bytes } else { - r1 = SD_SendCmd(CMD18, sector, 0X01); //Continuous read command + r1 = SD_SendCmd(CMD18, sector, 0X01); // continuous read command + do { - r1 = SD_RecvData(buf, 512); //Receive 512 bytes + r1 = SD_RecvData(buf, 512); // receive 512 bytes buf += 512; } while (--cnt && r1 == 0); - SD_SendCmd(CMD12, 0, 0X01); //Send stop command + + SD_SendCmd(CMD12, 0, 0X01); // send stop command } - SD_Cancel_CS(); //Cancel film selection + + SD_Cancel_CS(); // cancel film selection + return r1; } -/************************************************************************************* -**Write SD card -**buf:Data buffer -**sector:Starting sector -**cnt:Number of sectors -**Return value: 0, ok; other, failed. +/************************************************************************************ +* write SD card +* buf:Data buffer +* sector:Starting sector +* cnt:Number of sectors +* return value: 0, ok; other, failed. *************************************************************************************/ -uint8_t SD_WriteDisk(uint8_t * buf, uint32_t sector, uint8_t cnt) //Write SD card, fatfs / usb call +uint8_t SD_WriteDisk(uint8_t * buf, uint32_t sector, uint8_t cnt) // write SD card, fatfs / usb call { uint8_t r1; - if (SD_Type != SD_TYPE_V2HC) sector *= 512; //Translate to byte address + if (SD_Type != SD_TYPE_V2HC) + sector *= 512; // translate to byte address + if (cnt == 1) { - r1 = SD_SendCmd(CMD24, sector, 0X01); //Read command - if (r1 == 0) //Instruction sent successfully - { - r1 = SD_Send_Data(buf, 0xFE); //Write 512 bytes - } + r1 = SD_SendCmd(CMD24, sector, 0X01); // read command + + if (r1 == 0) // instruction sent successfully + r1 = SD_Send_Data(buf, 0xFE); // write 512 bytes } else { if (SD_Type != SD_TYPE_MMC) { SD_SendCmd(CMD55, 0, 0X01); - SD_SendCmd(CMD23, cnt, 0X01); //Send instruction + SD_SendCmd(CMD23, cnt, 0X01); // send instruction } - r1 = SD_SendCmd(CMD25, sector, 0X01); //Continuous read command + + r1 = SD_SendCmd(CMD25, sector, 0X01); // continuous read command + if (r1 == 0) { do { - r1 = SD_Send_Data(buf, 0xFC); //Receive 512 bytes + r1 = SD_Send_Data(buf, 0xFC); // receive 512 bytes buf += 512; } while (--cnt && r1 == 0); - r1 = SD_Send_Data(0, 0xFD); // Receive 512 bytes + + r1 = SD_Send_Data(0, 0xFD); // receive 512 bytes } } - SD_Cancel_CS(); //Cancel film selection + + SD_Cancel_CS(); // cancel film selection + return r1; } -#endif +#endif // SD_SPI_SUPPORT diff --git a/TFT/src/User/Hal/sd.h b/TFT/src/User/Hal/sd.h index 1600585956..1d4719508d 100644 --- a/TFT/src/User/Hal/sd.h +++ b/TFT/src/User/Hal/sd.h @@ -5,61 +5,25 @@ extern "C" { #endif -#include "variants.h" // for uint8_t etc... - -// SDCard type definition -#define SD_TYPE_ERR 0X00 -#define SD_TYPE_MMC 0X01 -#define SD_TYPE_V1 0X02 -#define SD_TYPE_V2 0X04 -#define SD_TYPE_V2HC 0X06 - -// SDCard instruction table -#define CMD0 0 //Card reset -#define CMD1 1 -#define CMD8 8 //Command 8, SEND_IF_COND -#define CMD9 9 //Command 9 to read CSD data -#define CMD10 10 //Command 10, read CID data -#define CMD12 12 //Command 12, stop data transmission -#define CMD16 16 //Command 16, set sector size should return 0x00 -#define CMD17 17 //Command 17, read sector -#define CMD18 18 //Command 18, read Multi sector -#define CMD23 23 //Command 23, set N blocks to be erased before multi-sector write -#define CMD24 24 //Command 24, write sector -#define CMD25 25 //Command 25, write multiple sectors -#define CMD41 41 //Command 41, should return 0x00 -#define CMD55 55 //Command 55, should return 0x01 -#define CMD58 58 //Command 58, read OCR information -#define CMD59 59 //Command 59, enable / disable CRC, should return 0x00�� - -// Data write response word meaning -#define SD_DATA_OK 0x05 -#define SD_DATA_CRC_ERROR 0x0B -#define SD_DATA_WRITE_ERROR 0x0D -#define SD_DATA_OTHER_ERROR 0xFF - -// SD Card Response Marker -#define SD_RESPONSE_NO_ERROR 0x00 // response error -#define SD_IN_IDLE_STATE 0x01 // Idle state -#define SD_ERASE_RESET 0x02 // Erase reset -#define SD_ILLEGAL_COMMAND 0x04 -#define SD_COM_CRC_ERROR 0x08 -#define SD_ERASE_SEQUENCE_ERROR 0x10 -#define SD_ADDRESS_ERROR 0x20 -#define SD_PARAMETER_ERROR 0x40 -#define SD_RESPONSE_FAILURE 0xFF - -uint8_t SD_CD_Inserted(void); -uint8_t SD_Init(void); //initialization -void SD_DeInit(void); -uint8_t SD_ReadDisk(uint8_t * buf, uint32_t sector, uint8_t cnt); //Read SD card, fatfs / usb call -uint8_t SD_WriteDisk(uint8_t * buf, uint32_t sector, uint8_t cnt); //Write SD card, fatfs / usb call - -uint8_t SD_Wait_Ready(void); //Waiting for SD card preparation -uint8_t SD_Get_Ack(uint8_t Response); //Get the answer -uint32_t SD_Get_Sector_Count(void); //Number of sectors read -uint8_t SD_GetCID(uint8_t * cid_data); //Read SD card CID -uint8_t SD_GetCSD(uint8_t * csd_data); //Read SD card CSD +#include // for uint8_t etc. +#include "variants.h" // for SD_SPI_SUPPORT, SD_SDIO_SUPPORT etc. + +#ifdef SD_SPI_SUPPORT + uint8_t SD_CD_Inserted(void); + + uint8_t SD_Wait_Ready(void); // Waiting for SD card preparation + uint8_t SD_Get_Ack(uint8_t Response); // Get the answer + uint8_t SD_GetCID(uint8_t * cid_data); // Read SD card CID + uint8_t SD_GetCSD(uint8_t * csd_data); // Read SD card CSD + uint32_t SD_Get_Sector_Count(void); // Number of sectors read + + uint8_t SD_Init(void); // initialization + void SD_DeInit(void); + uint8_t SD_ReadDisk(uint8_t * buf, uint32_t sector, uint8_t cnt); // Read SD card, fatfs / usb call + uint8_t SD_WriteDisk(uint8_t * buf, uint32_t sector, uint8_t cnt); // Write SD card, fatfs / usb call +#else // e.g. SD_SDIO_SUPPORT + #include "sdio_sdcard.h" +#endif #ifdef __cplusplus } diff --git a/TFT/src/User/Hal/stm32f10x/GPIO_Init.c b/TFT/src/User/Hal/stm32f10x/GPIO_Init.c index 613683ecee..23db8e8177 100644 --- a/TFT/src/User/Hal/stm32f10x/GPIO_Init.c +++ b/TFT/src/User/Hal/stm32f10x/GPIO_Init.c @@ -1,33 +1,34 @@ #include "GPIO_Init.h" +#include "variants.h" // for GPIO_TypeDef etc. -GPIO_TypeDef* const GPIO_Port[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG}; +static GPIO_TypeDef * const GPIO_Port[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG}; void GPIO_InitSet(uint16_t io, GPIO_MODE mode, uint8_t AF) { uint16_t port = GPIO_GET_PORT(io); uint16_t pin = GPIO_GET_PIN(io); - RCC->APB2ENR |= 1 << (port+2); // Enable GPIO Clock, GPIOA start at APB2 bit2 + RCC->APB2ENR |= 1 << (port+2); // enable GPIO Clock, GPIOA start at APB2 bit2 if (mode == MGPIO_MODE_IPD) { - GPIO_Port[port]->BSRR = 1 << (pin+16); // set low + GPIO_Port[port]->BSRR = 1 << (pin+16); // set low mode = MGPIO_MODE_IPU; } else if (mode == MGPIO_MODE_IPU) { - GPIO_Port[port]->BSRR = 1 << pin; // set high + GPIO_Port[port]->BSRR = 1 << pin; // set high } if (pin <= 7) { - GPIO_Port[port]->CRL &= ~(0xF << 4*(pin & 0x7)); // clear control reg bits - GPIO_Port[port]->CRL |= mode << 4*(pin & 0x7); // clear control reg bits + GPIO_Port[port]->CRL &= ~(0xF << 4 * (pin & 0x7)); // clear control reg bits + GPIO_Port[port]->CRL |= mode << 4 * (pin & 0x7); // clear control reg bits } else { - GPIO_Port[port]->CRH &= ~(0xF << 4*(pin & 0x7)); // clear control reg bits - GPIO_Port[port]->CRH |= mode << 4*(pin & 0x7); // clear control reg bits + GPIO_Port[port]->CRH &= ~(0xF << 4 * (pin & 0x7)); // clear control reg bits + GPIO_Port[port]->CRH |= mode << 4 * (pin & 0x7); // clear control reg bits } } @@ -53,11 +54,7 @@ uint8_t GPIO_GetLevel(uint16_t io) uint16_t pin = GPIO_GET_PIN(io); if ((GPIO_Port[port]->IDR & (1 << pin)) != 0) - { return 1; - } else - { return 0; - } } diff --git a/TFT/src/User/Hal/stm32f10x/GPIO_Init.h b/TFT/src/User/Hal/stm32f10x/GPIO_Init.h index e7a484020e..131a3c1a68 100644 --- a/TFT/src/User/Hal/stm32f10x/GPIO_Init.h +++ b/TFT/src/User/Hal/stm32f10x/GPIO_Init.h @@ -1,24 +1,24 @@ #ifndef _GPIO_INIT_H_ #define _GPIO_INIT_H_ -#include "variants.h" +#include #include "STM32Fxx_Pins.h" /* -bit 0:1 - 00:input(reset vale) 01:output 10MHz 10:output 2Mhz 11:output 50MHz -bit 2:3 - Input 00:Analog input 01:input floating(reset value) 10:pull up/down 11:Reserved - - Output 00:Output push-pull 01:Output open-drain 10:AF push-pull 11:AF open-drain -*/ + * bit 0:1 - 00:input(reset vale) 01:output 10MHz 10:output 2Mhz 11:output 50MHz + * bit 2:3 - Input 00:Analog input 01:input floating(reset value) 10:pull up/down 11:Reserved + * - Output 00:Output push-pull 01:Output open-drain 10:AF push-pull 11:AF open-drain + */ typedef enum { - MGPIO_MODE_AIN = (0<<2)|(0), - MGPIO_MODE_IPN = (1<<2)|(0), - MGPIO_MODE_IPU = (2<<2)|(0), - MGPIO_MODE_IPD = (3<<2)|(0), - MGPIO_MODE_OUT_PP = (0<<2)|(3), - MGPIO_MODE_OUT_OD = (1<<2)|(3), - MGPIO_MODE_AF_PP = (2<<2)|(3), - MGPIO_MODE_AF_OD = (3<<2)|(3), + MGPIO_MODE_AIN = (0<<2) | (0), + MGPIO_MODE_IPN = (1<<2) | (0), + MGPIO_MODE_IPU = (2<<2) | (0), + MGPIO_MODE_IPD = (3<<2) | (0), + MGPIO_MODE_OUT_PP = (0<<2) | (3), + MGPIO_MODE_OUT_OD = (1<<2) | (3), + MGPIO_MODE_AF_PP = (2<<2) | (3), + MGPIO_MODE_AF_OD = (3<<2) | (3), } GPIO_MODE; void GPIO_InitSet(uint16_t io, GPIO_MODE mode, uint8_t AF); diff --git a/TFT/src/User/Hal/stm32f10x/HAL_Flash.c b/TFT/src/User/Hal/stm32f10x/HAL_Flash.c index afaefd5932..51408314cc 100644 --- a/TFT/src/User/Hal/stm32f10x/HAL_Flash.c +++ b/TFT/src/User/Hal/stm32f10x/HAL_Flash.c @@ -1,26 +1,32 @@ #include "HAL_Flash.h" +#include "variants.h" // for FLASH_Unlock etc. #include "my_misc.h" #define SIGN_ADDRESS (0x08040000 - 0x800) // reserve the last page (2KB) to save user parameters -void HAL_FlashRead(uint8_t *data, uint32_t len) +void HAL_FlashRead(uint8_t * data, uint32_t len) { uint32_t i = 0; + for (i = 0; i < len; i++) { - data[i] = *(volatile uint8_t*)(SIGN_ADDRESS + i); + data[i] = *(volatile uint8_t *)(SIGN_ADDRESS + i); } } -void HAL_FlashWrite(uint8_t *data, uint32_t len) +void HAL_FlashWrite(uint8_t * data, uint32_t len) { uint32_t i = 0; + FLASH_Unlock(); FLASH_ErasePage(SIGN_ADDRESS); + for (i = 0; i < len; i += 2) { - uint16_t data16 = data[i] | (data[MIN(i+1, len-1)] << 8); // stm32f10x needs to write at least 16 bits at a time + uint16_t data16 = data[i] | (data[MIN(i + 1, len - 1)] << 8); // stm32f10x needs to write at least 16 bits at a time + FLASH_ProgramHalfWord(SIGN_ADDRESS + i, data16); } + FLASH_Lock(); } diff --git a/TFT/src/User/Hal/stm32f10x/HAL_Flash.h b/TFT/src/User/Hal/stm32f10x/HAL_Flash.h index d37103fc08..88792034a2 100644 --- a/TFT/src/User/Hal/stm32f10x/HAL_Flash.h +++ b/TFT/src/User/Hal/stm32f10x/HAL_Flash.h @@ -1,9 +1,9 @@ #ifndef _HAL_FLASH_H_ #define _HAL_FLASH_H_ -#include "variants.h" // for uint8_t etc... +#include -void HAL_FlashRead(uint8_t *data, uint32_t len); -void HAL_FlashWrite(uint8_t *data, uint32_t len); +void HAL_FlashRead(uint8_t * data, uint32_t len); +void HAL_FlashWrite(uint8_t * data, uint32_t len); #endif diff --git a/TFT/src/User/Hal/stm32f10x/Serial.c b/TFT/src/User/Hal/stm32f10x/Serial.c index 34cc882f8d..c0e45001e6 100644 --- a/TFT/src/User/Hal/stm32f10x/Serial.c +++ b/TFT/src/User/Hal/stm32f10x/Serial.c @@ -41,7 +41,7 @@ const SERIAL_CFG Serial[_UART_CNT] = { // RM0008 Table 78-79 #ifdef TX_DMA_WRITE // disable TX DMA and clear all interrupt flags for a serial port -void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) +static void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) { Serial[port].dma_channelTX->CCR &= ~(1<<0); // disable TX DMA @@ -69,7 +69,7 @@ void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) #endif -void Serial_DMA_Config(uint8_t port) +static inline void Serial_DMA_Config(uint8_t port) { const SERIAL_CFG * cfg = &Serial[port]; @@ -124,7 +124,7 @@ void Serial_DMA_Config(uint8_t port) cfg->dma_channelRX->CCR |= (1<<0); // RX enable DMA } -void Serial_ClearData(uint8_t port) +static void Serial_ClearData(uint8_t port) { dmaL1DataRX[port].wIndex = dmaL1DataRX[port].rIndex = dmaL1DataRX[port].flag = dmaL1DataRX[port].cacheSize = 0; @@ -176,7 +176,7 @@ void Serial_DeConfig(uint8_t port) #ifdef TX_DMA_WRITE // TX DMA based serial writing // DMA serial write support function -void Serial_Send_TX(uint8_t port) +static void Serial_Send_TX(uint8_t port) { // setup DMA transfer, and wait for serial Transfer Complete (TX) interrupt in ISR if (dmaL1DataTX[port].wIndex >= dmaL1DataTX[port].rIndex) diff --git a/TFT/src/User/Hal/stm32f10x/Serial.h b/TFT/src/User/Hal/stm32f10x/Serial.h index 7d5fd491a3..5069f48590 100644 --- a/TFT/src/User/Hal/stm32f10x/Serial.h +++ b/TFT/src/User/Hal/stm32f10x/Serial.h @@ -2,8 +2,8 @@ #define _SERIAL_H_ #include -#include "variants.h" // for uint32_t etc... -#include "uart.h" +#include "variants.h" // for USART_TypeDef etc. +#include "uart.h" // for _UART_CNT etc. // comment out this line to use TX interrupt based serial writing instead of TX DMA based serial writing #define TX_DMA_WRITE diff --git a/TFT/src/User/Hal/stm32f10x/lcd.c b/TFT/src/User/Hal/stm32f10x/lcd.c index 50efb4c806..e9612e54a0 100644 --- a/TFT/src/User/Hal/stm32f10x/lcd.c +++ b/TFT/src/User/Hal/stm32f10x/lcd.c @@ -1,4 +1,5 @@ #include "lcd.h" +#include "GPIO_Init.h" #include "delay.h" #ifdef STM32_HAS_FSMC @@ -6,33 +7,37 @@ uint16_t LCD_RD_DATA(void) { volatile uint16_t ram; + ram = LCD->LCD_RAM; + return ram; } -void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) +static void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) { LCD->LCD_REG = LCD_Reg; LCD->LCD_RAM = LCD_RegValue; } -uint16_t LCD_ReadReg(uint8_t LCD_Reg) +static uint16_t LCD_ReadReg(uint8_t LCD_Reg) { LCD_WR_REG(LCD_Reg); Delay_us(5); + return LCD_RD_DATA(); } -void LCD_GPIO_Config(void) +static inline void LCD_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; + // fsmc 16bit data pins RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - /*FSMC-D0~D15: PD 14 15 0 1 8 9 10,PE 7 8 9 10 11 12 13 14 15*/ + // FSMC-D0~D15: PD 14 15 0 1 8 9 10,PE 7 8 9 10 11 12 13 14 15 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15 | GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; GPIO_Init(GPIOD, &GPIO_InitStructure); @@ -40,21 +45,24 @@ void LCD_GPIO_Config(void) | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 | GPIO_Pin_2; GPIO_Init(GPIOE, &GPIO_InitStructure); + /* - * PD4-FSMC_NOE :LCD-RD - * PD5-FSMC_NWE :LCD-WR - * PD7-FSMC_NE1 :LCD-CS - * PE2-FSMC_A23 :LCD-RS - */ + * configure the control line corresponding to FSMC + * PD4-FSMC_NOE :LCD-RD + * PD5-FSMC_NWE :LCD-WR + * PD7-FSMC_NE1 :LCD-CS + * PE2-FSMC_A23 :LCD-RS + */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_11; GPIO_Init(GPIOD, &GPIO_InitStructure); } -void LCD_FSMC_Config(void) +static inline void LCD_FSMC_Config(void) { FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; FSMC_NORSRAMTimingInitTypeDef readWriteTiming,writeTiming; + // FSMC configuration RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); @@ -95,7 +103,7 @@ void LCD_FSMC_Config(void) FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &writeTiming; FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); - // Enable FSMC Bank1_SRAM Bank + // enable FSMC Bank1_SRAM bank FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); } @@ -107,6 +115,47 @@ void LCD_HardwareConfig(void) #else +#if defined(MKS_TFT) + /* + #define LCD_WR PB14 + #define LCD_RS PD13 + #define LCD_CS PC8 + #define LCD_RD PD15 + */ + #define LCD_CS_SET GPIOC->BSRR=1<<8 // Chip Select Port PC8 //片选端口 + #define LCD_RS_SET GPIOD->BSRR=1<<13 // Data / command PD13 //数据/命令 + #define LCD_WR_SET GPIOB->BSRR=1<<14 // Write data PB14 //写数据 + #define LCD_RD_SET GPIOD->BSRR=1<<15 // Read data PD15 //读数据 + + #define LCD_CS_CLR GPIOC->BRR=1<<8 // Chip select port PC8 //片选端口 + #define LCD_RS_CLR GPIOD->BRR=1<<13 // Data / command PD13 //数据/命令 + #define LCD_WR_CLR GPIOB->BRR=1<<14 // Write data PB14 //写数据 + #define LCD_RD_CLR GPIOD->BRR=1<<15 // Read data PD15 //读数据 + + #define DATAOUT(x) do{ GPIOE->ODR = x; }while(0) // Data output //数据输出 + #define DATAIN() GPIOE->IDR // Data input //数据输入 +#else + /* TFT24-V1.1 + * PB6 :LCD-RD + * PB7 :LCD-WR + * PB9 :LCD-CS + * PB8 :LCD-RS + */ + #define LCD_CS_SET GPIOB->BSRR=1<<9 // Chip Select Port PB9 //片选端口 + #define LCD_RS_SET GPIOB->BSRR=1<<8 // Data / command PB8 //数据/命令 + #define LCD_WR_SET GPIOB->BSRR=1<<7 // Write data PB7 //写数据 + #define LCD_RD_SET GPIOB->BSRR=1<<6 // Read data PB6 //读数据 + + #define LCD_CS_CLR GPIOB->BRR=1<<9 // Chip Select Port PB9 //片选端口 + #define LCD_RS_CLR GPIOB->BRR=1<<8 // Data / command PB8 //数据/命令 + #define LCD_WR_CLR GPIOB->BRR=1<<7 // Write data PB7 //写数据 + #define LCD_RD_CLR GPIOB->BRR=1<<6 // Read data PB6 //读数据 + + // PB0~15, as a data line // PB0~15, 作为数据线 + #define DATAOUT(x) do{ GPIOC->ODR = x; }while(0) // Data output //数据输出 + #define DATAIN() GPIOC->IDR // Data input //数据输入 +#endif + void LCD_WR_REG(uint16_t data) { LCD_RS_CLR; @@ -136,13 +185,14 @@ uint16_t LCD_RD_DATA(void) #endif volatile uint16_t ram; + LCD_DATA_PORT->CRL = 0X88888888; LCD_DATA_PORT->CRH = 0X88888888; LCD_DATA_PORT->ODR = 0X0000; LCD_RS_SET; LCD_CS_CLR; - LCD_RD_CLR; // double for delay. + LCD_RD_CLR; // double for delay LCD_RD_CLR; ram = DATAIN(); LCD_RD_SET; @@ -151,25 +201,27 @@ uint16_t LCD_RD_DATA(void) LCD_DATA_PORT->CRL = 0X33333333; LCD_DATA_PORT->CRH = 0X33333333; LCD_DATA_PORT->ODR = 0XFFFF; + return ram; } -void LCD_GPIO_Config(void) +static inline void LCD_GPIO_Config(void) { #if defined(MKS_TFT) GPIO_InitTypeDef GPIO_InitStructure; - /* GPIO Ports Clock Enable */ + // GPIO Ports Clock Enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB, ENABLE); - /* Configure GPIO pin Output Level */ + // configure GPIO pin Output Level - /* Configure GPIO pins : Pin1_Pin Pin2_Pin */ + // configure GPIO pins : Pin1_Pin Pin2_Pin // GPIO_InitStructure.GPIO_Pin = LCD_nWR_Pin | FLASH_nCS_Pin | FILAMENT_DI_Pin | POWER_DI_Pin; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); + // GPIO_InitStructure.GPIO_Pin = SDCARD_nCS_Pin | LCD_RS_Pin | LCD_BACKLIGHT_Pin | LCD_nRD_Pin; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; @@ -190,6 +242,7 @@ void LCD_GPIO_Config(void) GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOE, &GPIO_InitStructure); + LCD_RD_SET; // set this as we only change it when reading #else GPIO_InitTypeDef GPIO_InitStructure; @@ -198,17 +251,17 @@ void LCD_GPIO_Config(void) GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; - /* D0 - D15: PC0 - PC15 */ + // D0 - D15: PC0 - PC15 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_SetBits(GPIOC, GPIO_Pin_All); /* - * PB6 :LCD-RD - * PB7 :LCD-WR - * PB9 :LCD-CS - * PB8 :LCD-RS - */ + * PB6 :LCD-RD + * PB7 :LCD-WR + * PB9 :LCD-CS + * PB8 :LCD-RS + */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9); @@ -220,4 +273,4 @@ void LCD_HardwareConfig(void) LCD_GPIO_Config(); } -#endif +#endif // STM32_HAS_FSMC diff --git a/TFT/src/User/Hal/stm32f10x/lcd.h b/TFT/src/User/Hal/stm32f10x/lcd.h index 6763c12da4..5139a7e7ff 100644 --- a/TFT/src/User/Hal/stm32f10x/lcd.h +++ b/TFT/src/User/Hal/stm32f10x/lcd.h @@ -2,7 +2,7 @@ #define _LCD_H_ #include -#include "variants.h" // for STM32_HAS_FSMC etc... +#include "variants.h" // for STM32_HAS_FSMC etc. #ifdef STM32_HAS_FSMC @@ -12,59 +12,20 @@ volatile uint16_t LCD_RAM; } LCD_TypeDef; - #define LCD_BASE ((uint32_t)(0x60000000 | 0x00FFFFFE)) // 1111 1111 1111 1111 1111 1110 - #define LCD ((LCD_TypeDef *) LCD_BASE) + #define LCD_BASE ((uint32_t)(0x60000000 | 0x00FFFFFE)) // 1111 1111 1111 1111 1111 1110 + #define LCD ((LCD_TypeDef *) LCD_BASE) #define LCD_WR_REG(regval) do{ LCD->LCD_REG = regval; }while(0) #define LCD_WR_DATA(data) do{ LCD->LCD_RAM = data; }while(0) #else - #if defined(MKS_TFT) - /* - #define LCD_WR PB14 - #define LCD_RS PD13 - #define LCD_CS PC8 - #define LCD_RD PD15 - */ - #define LCD_CS_SET GPIOC->BSRR=1<<8 // Chip Select Port PC8 //片选端口 - #define LCD_RS_SET GPIOD->BSRR=1<<13 // Data / command PD13 //数据/命令 - #define LCD_WR_SET GPIOB->BSRR=1<<14 // Write data PB14 //写数据 - #define LCD_RD_SET GPIOD->BSRR=1<<15 // Read data PD15 //读数据 - #define LCD_CS_CLR GPIOC->BRR=1<<8 // Chip select port PC8 //片选端口 - #define LCD_RS_CLR GPIOD->BRR=1<<13 // Data / command PD13 //数据/命令 - #define LCD_WR_CLR GPIOB->BRR=1<<14 // Write data PB14 //写数据 - #define LCD_RD_CLR GPIOD->BRR=1<<15 // Read data PD15 //读数据 - - #define DATAOUT(x) do{GPIOE->ODR = x;}while(0) // Data output //数据输出 - #define DATAIN() GPIOE->IDR // Data input //数据输入 - #else - /* TFT24-V1.1 - * PB6 :LCD-RD - * PB7 :LCD-WR - * PB9 :LCD-CS - * PB8 :LCD-RS - */ - #define LCD_CS_SET GPIOB->BSRR=1<<9 // Chip Select Port PB9 //片选端口 - #define LCD_RS_SET GPIOB->BSRR=1<<8 // Data / command PB8 //数据/命令 - #define LCD_WR_SET GPIOB->BSRR=1<<7 // Write data PB7 //写数据 - #define LCD_RD_SET GPIOB->BSRR=1<<6 // Read data PB6 //读数据 - - #define LCD_CS_CLR GPIOB->BRR=1<<9 // Chip Select Port PB9 //片选端口 - #define LCD_RS_CLR GPIOB->BRR=1<<8 // Data / command PB8 //数据/命令 - #define LCD_WR_CLR GPIOB->BRR=1<<7 // Write data PB7 //写数据 - #define LCD_RD_CLR GPIOB->BRR=1<<6 // Read data PB6 //读数据 - - // PB0~15, As a data line //PB0~15,作为数据线 - #define DATAOUT(x) do{GPIOC->ODR = x;}while(0) // Data output //数据输出 - #define DATAIN() GPIOC->IDR // Data input //数据输入 - #endif void LCD_WR_REG(uint16_t data); void LCD_WR_DATA(uint16_t data); #endif -void LCD_HardwareConfig(void); uint16_t LCD_RD_DATA(void); +void LCD_HardwareConfig(void); #endif diff --git a/TFT/src/User/Hal/stm32f10x/lcd_dma.c b/TFT/src/User/Hal/stm32f10x/lcd_dma.c index 4eb91cfd38..ceb7f23fa3 100644 --- a/TFT/src/User/Hal/stm32f10x/lcd_dma.c +++ b/TFT/src/User/Hal/stm32f10x/lcd_dma.c @@ -1,5 +1,5 @@ #include "lcd_dma.h" -#include "variants.h" // for STM32_HAS_FSMC etc... +#include "variants.h" // for STM32_HAS_FSMC etc. #include "lcd.h" #include "spi.h" #include "LCD_Init.h" @@ -7,7 +7,8 @@ #include "delay.h" #ifdef STM32_HAS_FSMC -// Config for SPI Channel + +// config for SPI Channel #if W25Qxx_SPI == _SPI1 #define W25QXX_SPI_NUM SPI1 #define W25QXX_SPI_DMA_RCC_AHB RCC_AHBPeriph_DMA1 @@ -29,30 +30,30 @@ #endif // SPI --> FSMC DMA (LCD_RAM) -// 16bits, SPI_RX to LCD_RAM. +// 16bits, SPI_RX to LCD_RAM void LCD_DMA_Config(void) { - RCC->AHBENR |= W25QXX_SPI_DMA_RCC_AHB; // Turn on the DMA clock - Delay_ms(5); // Wait for the DMA clock to stabilize - W25QXX_SPI_DMA_CHANNEL->CPAR = (uint32_t)&W25QXX_SPI_NUM->DR; // The peripheral address is: SPI-> DR - W25QXX_SPI_DMA_CHANNEL->CMAR = (uint32_t)&LCD->LCD_RAM; // The target address is LCD_RAM + RCC->AHBENR |= W25QXX_SPI_DMA_RCC_AHB; // turn on the DMA clock + Delay_ms(5); // wait for the DMA clock to stabilize + W25QXX_SPI_DMA_CHANNEL->CPAR = (uint32_t)&W25QXX_SPI_NUM->DR; // the peripheral address is: SPI-> DR + W25QXX_SPI_DMA_CHANNEL->CMAR = (uint32_t)&LCD->LCD_RAM; // the target address is LCD_RAM W25QXX_SPI_DMA_CHANNEL->CNDTR = 0; // DMA1, the amount of data transferred, temporarily set to 0 - W25QXX_SPI_DMA_CHANNEL->CCR = 0X00000000; // Reset - W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<4; // Read from peripheral - W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<5; // Normal mode - W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<6; // Peripheral address non-incremental mode - W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<7; // Memory non-incremental mode - W25QXX_SPI_DMA_CHANNEL->CCR |= LCD_DATA_16BIT<<8; // Peripheral data width is 16 bits - W25QXX_SPI_DMA_CHANNEL->CCR |= LCD_DATA_16BIT<<10; // Memory data width 16 bits - W25QXX_SPI_DMA_CHANNEL->CCR |= 1<<12; // Medium priority - W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<14; // Non-memory to memory mode + W25QXX_SPI_DMA_CHANNEL->CCR = 0X00000000; // reset + W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<4; // read from peripheral + W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<5; // normal mode + W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<6; // peripheral address non-incremental mode + W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<7; // memory non-incremental mode + W25QXX_SPI_DMA_CHANNEL->CCR |= LCD_DATA_16BIT<<8; // peripheral data width is 16 bits + W25QXX_SPI_DMA_CHANNEL->CCR |= LCD_DATA_16BIT<<10; // memory data width 16 bits + W25QXX_SPI_DMA_CHANNEL->CCR |= 1<<12; // medium priority + W25QXX_SPI_DMA_CHANNEL->CCR |= 0<<14; // non-memory to memory mode } #define LCD_DMA_MAX_TRANS 65535 // DMA 65535 bytes one frame // start DMA transfer from SPI->DR to FSMC // the max bytes of one frame is LCD_DMA_MAX_TRANS 65535 -void lcd_frame_segment_display(uint16_t size, uint32_t addr) +static inline void lcd_frame_segment_display(uint16_t size, uint32_t addr) { W25QXX_SPI_DMA_CHANNEL->CNDTR = size; @@ -63,35 +64,36 @@ void lcd_frame_segment_display(uint16_t size, uint32_t addr) W25Qxx_SPI_Read_Write_Byte((uint8_t)addr); W25Qxx_SPI_Read_Write_Byte(0XFF); // 8 dummy clock - //set SPI to 16bit DMA rx only mode - W25QXX_SPI_NUM->CR1 &= ~(1<<6); - W25QXX_SPI_NUM->CR2 |= 1<<0; // enable SPI rx DMA - W25QXX_SPI_NUM->CR1 |= LCD_DATA_16BIT<<11; // 16bit data frame - W25QXX_SPI_NUM->CR1 |= 1<<10; // rx only + // set SPI to 16bit DMA rx only mode + W25QXX_SPI_NUM->CR1 &= ~(1<<6); // disable SPI + W25QXX_SPI_NUM->CR2 |= 1<<0; // enable SPI rx DMA + W25QXX_SPI_NUM->CR1 |= LCD_DATA_16BIT<<11; // 16bit data frame + W25QXX_SPI_NUM->CR1 |= 1<<10; // rx only - W25QXX_SPI_DMA_CHANNEL->CCR |= 1<<0; // enable dma channel - W25QXX_SPI_NUM->CR1 |= 1<<6; // enable SPI + W25QXX_SPI_DMA_CHANNEL->CCR |= 1<<0; // enable dma channel + W25QXX_SPI_NUM->CR1 |= 1<<6; // enable SPI while ((W25QXX_SPI_DMA->ISR & (1<CCR &= (uint32_t)(~(1<<0)); W25QXX_SPI_DMA->IFCR |= (uint32_t)(1< void LCD_DMA_Config(void); void lcd_frame_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint32_t addr); diff --git a/TFT/src/User/Hal/stm32f10x/sdio_sdcard.c b/TFT/src/User/Hal/stm32f10x/sdio_sdcard.c index f52ab0992f..9982b098ff 100644 --- a/TFT/src/User/Hal/stm32f10x/sdio_sdcard.c +++ b/TFT/src/User/Hal/stm32f10x/sdio_sdcard.c @@ -1,8 +1,7 @@ -#include "variants.h" // for SD_SDIO_SUPPORT etc... +#include "sdio_sdcard.h" #ifdef SD_SDIO_SUPPORT -#include "sdio_sdcard.h" #include "GPIO_Init.h" #include @@ -1304,4 +1303,4 @@ uint8_t SD_WriteDisk(uint8_t*buf,uint32_t sector,uint8_t cnt) return sta; } -#endif +#endif // SD_SDIO_SUPPORT diff --git a/TFT/src/User/Hal/stm32f10x/sdio_sdcard.h b/TFT/src/User/Hal/stm32f10x/sdio_sdcard.h index eab4b17e38..d4959e73ce 100644 --- a/TFT/src/User/Hal/stm32f10x/sdio_sdcard.h +++ b/TFT/src/User/Hal/stm32f10x/sdio_sdcard.h @@ -1,7 +1,13 @@ #ifndef __SDIO_SDCARD_H #define __SDIO_SDCARD_H -#include "variants.h" // for uint16_t etc... +#ifdef __cplusplus + extern "C" { +#endif + +#include "variants.h" // for SD_SDIO_SUPPORT etc. + +#ifdef SD_SDIO_SUPPORT //SDIO��ر�־λ #define SDIO_FLAG_CCRCFAIL ((uint32_t)0x00000001) @@ -338,6 +344,7 @@ extern SD_CardInfo SDCardInfo;//SD card information //Related function definitions uint8_t SD_CD_Inserted(void); + SD_Error SD_Init(void); void SD_DeInit(void); void SDIO_Clock_Set(uint8_t clkdiv); @@ -372,4 +379,10 @@ void SD_DMA_Config(uint32_t*mbuf,uint32_t bufsize,uint8_t dir); uint8_t SD_ReadDisk(uint8_t*buf,uint32_t sector,uint8_t cnt); //Read SD card, fatfs / usb call uint8_t SD_WriteDisk(uint8_t*buf,uint32_t sector,uint8_t cnt); //Write SD card, fatfs / usb call +#endif // SD_SDIO_SUPPORT + +#ifdef __cplusplus +} +#endif + #endif diff --git a/TFT/src/User/Hal/stm32f10x/spi.c b/TFT/src/User/Hal/stm32f10x/spi.c index 70372b9cb3..f99e34216f 100644 --- a/TFT/src/User/Hal/stm32f10x/spi.c +++ b/TFT/src/User/Hal/stm32f10x/spi.c @@ -1,5 +1,5 @@ #include "spi.h" -#include "variants.h" // for SPI1_SCK_PIN etc... +#include "variants.h" // for SPI1_SCK_PIN etc. #include "GPIO_Init.h" // SPI1 default pins config @@ -33,13 +33,13 @@ #define SPI3_MOSI_PIN PB5 #endif -static volatile uint32_t* const rcc_spi_rst[_SPI_CNT] = { +static volatile uint32_t * const rcc_spi_rst[_SPI_CNT] = { &RCC->APB2RSTR, &RCC->APB1RSTR, &RCC->APB1RSTR, }; -static volatile uint32_t* const rcc_spi_en[_SPI_CNT] = { +static volatile uint32_t * const rcc_spi_en[_SPI_CNT] = { &RCC->APB2ENR, &RCC->APB1ENR, &RCC->APB1ENR, @@ -51,7 +51,7 @@ static const uint32_t rcc_spi_bit[_SPI_CNT] = { 0x00008000, // RCC_APB1 bit 15 }; -static SPI_TypeDef* const spi[_SPI_CNT] = { +static SPI_TypeDef * const spi[_SPI_CNT] = { SPI1, // SCK--PA5 MISO--PA6 MOSI--PA7 SPI2, // SCK--PB13 MISO--PB14 MOSI--PB15 SPI3, // SCK--PB3 MISO--PB4 MOSI--PB5 @@ -68,9 +68,9 @@ void SPI_GPIO_Init(uint8_t port) GPIO_InitSet(spi_mosi[port], MGPIO_MODE_AF_PP, 0); // MOSI } -void SPI_GPIO_DeInit(uint8_t port) +static inline void SPI_GPIO_DeInit(uint8_t port) { - // Set all of spi pins to input + // set all of spi pins to input GPIO_InitSet(spi_sck[port], MGPIO_MODE_IPN, 0); // SCK GPIO_InitSet(spi_miso[port], MGPIO_MODE_IPN, 0); // MISO GPIO_InitSet(spi_mosi[port], MGPIO_MODE_IPN, 0); // MOSI @@ -81,9 +81,9 @@ void SPI_GPIO_DeInit(uint8_t port) void SPI_Protocol_Init(uint8_t port, uint8_t baudrate) { *rcc_spi_rst[port] |= rcc_spi_bit[port]; - *rcc_spi_rst[port] &= ~rcc_spi_bit[port]; // Reset SPI clock + *rcc_spi_rst[port] &= ~rcc_spi_bit[port]; // reset SPI clock - *rcc_spi_en[port] |= rcc_spi_bit[port]; // Enable SPI clock + *rcc_spi_en[port] |= rcc_spi_bit[port]; // enable SPI clock spi[port]->CR1 = (0<<15) // 0:2-line 1: 1-line | (0<<14) // in bidirectional mode 0:read only 1: read/write @@ -92,9 +92,9 @@ void SPI_Protocol_Init(uint8_t port, uint8_t baudrate) | (0<<11) // 0:8-bit data frame 1:16-bit data frame | (0<<10) // 0:Full duplex 1:Receive-only | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) - | (1<<8) // This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored + | (1<<8) // this bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored | (0<<7) // 0:MSB 1:LSB - | (1<<6) // Enable SPI + | (1<<6) // enable SPI | (baudrate<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 | (1<<2) // 0:Slave 1:Master @@ -111,8 +111,9 @@ void SPI_Config(uint8_t port) void SPI_DeConfig(uint8_t port) { SPI_GPIO_DeInit(port); + *rcc_spi_rst[port] |= rcc_spi_bit[port]; - *rcc_spi_rst[port] &= ~rcc_spi_bit[port]; // Reset SPI clock + *rcc_spi_rst[port] &= ~rcc_spi_bit[port]; // reset SPI clock } uint16_t SPI_Read_Write(uint8_t port, uint16_t d) @@ -120,5 +121,6 @@ uint16_t SPI_Read_Write(uint8_t port, uint16_t d) while ((spi[port]->SR & (1 << 1)) == RESET); // wait for tx empty spi[port]->DR = d; while ((spi[port]->SR & (1 << 0)) == RESET); // wait for rx no empty + return spi[port]->DR; } diff --git a/TFT/src/User/Hal/stm32f10x/spi.h b/TFT/src/User/Hal/stm32f10x/spi.h index e6d05cd472..865ee40236 100644 --- a/TFT/src/User/Hal/stm32f10x/spi.h +++ b/TFT/src/User/Hal/stm32f10x/spi.h @@ -3,15 +3,15 @@ #include -#define _SPI1 0 -#define _SPI2 1 -#define _SPI3 2 -#define _SPI_CNT 3 +#define _SPI1 0 +#define _SPI2 1 +#define _SPI3 2 +#define _SPI_CNT 3 void SPI_GPIO_Init(uint8_t port); +void SPI_Protocol_Init(uint8_t port, uint8_t baudrate); void SPI_Config(uint8_t port); void SPI_DeConfig(uint8_t port); -void SPI_Protocol_Init(uint8_t port, uint8_t baudrate); uint16_t SPI_Read_Write(uint8_t port, uint16_t d); #endif diff --git a/TFT/src/User/Hal/stm32f10x/spi_slave.c b/TFT/src/User/Hal/stm32f10x/spi_slave.c index cae32f0b01..153ee2340b 100644 --- a/TFT/src/User/Hal/stm32f10x/spi_slave.c +++ b/TFT/src/User/Hal/stm32f10x/spi_slave.c @@ -1,5 +1,5 @@ #include "spi_slave.h" -#include "includes.h" // for infoSettings, ST7920_EMULATOR etc... +#include "includes.h" // for infoSettings, ST7920_EMULATOR etc. #include "spi.h" #include "GPIO_Init.h" #include "HD44780.h" @@ -7,43 +7,66 @@ #if !defined(MKS_TFT) #if defined(ST7920_EMULATOR) + // TODO: // now support SPI2 and PB12 CS only // more compatibility changes are needed -// Config for SPI Channel +// config for SPI Channel #if ST7920_SPI == _SPI1 - #define ST7920_SPI_NUM SPI1 + #define ST7920_SPI_NUM SPI1 #elif ST7920_SPI == _SPI2 - #define ST7920_SPI_NUM SPI2 + #define ST7920_SPI_NUM SPI2 #elif ST7920_SPI == _SPI3 - #define ST7920_SPI_NUM SPI3 + #define ST7920_SPI_NUM SPI3 #endif -CIRCULAR_QUEUE *spi_queue = NULL; +static CIRCULAR_QUEUE * spi_queue = NULL; + +// external interruption arrangement +static inline void SPI_Slave_CS_Config(void) +{ + EXTI_InitTypeDef EXTI_InitStructure; + NVIC_InitTypeDef NVIC_InitStructure; + + // connect GPIOB12 to the interrupt line + RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); // enable AFIO clock + GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12); // PB12 is connected to interrupt line 12 + + // set interrupt line 12 bit external falling edge interrupt + EXTI_InitStructure.EXTI_Line = EXTI_Line12; + EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; + EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; + EXTI_InitStructure.EXTI_LineCmd = ENABLE; + EXTI_Init(&EXTI_InitStructure); -void SPI_Slave_CS_Config(void); // forward declaration + NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; // external interrupt channel where the key is enabled + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; // preemption priority 2? + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; // sub-priority 1 + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // enable external interrupt channel + NVIC_Init(&NVIC_InitStructure); +} -void SPI_ReEnable(uint8_t mode) +static void SPI_ReEnable(uint8_t mode) { - ST7920_SPI_NUM->CR1 = (0<<15) // 0:2-line 1: 1-line - | (0<<14) // in bidirectional mode 0:read only 1: read/write - | (0<<13) // 0:disable CRC 1:enable CRC - | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) - | (0<<11) // 0:8-bit data frame 1:16-bit data frame - | (1<<10) // 0:Full duplex 1:Receive-only - | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) - | (0<<8) // This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored - | (0<<7) // 0:MSB 1:LSB - | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 - // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 - | (0<<2) // 0:Slave 1:Master - | (mode<<1) // CPOL - | (mode<<0); // CPHA - - ST7920_SPI_NUM->CR2 |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE + ST7920_SPI_NUM->CR1 = (0<<15) // 0:2-line 1: 1-line + | (0<<14) // in bidirectional mode 0:read only 1: read/write + | (0<<13) // 0:disable CRC 1:enable CRC + | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) + | (0<<11) // 0:8-bit data frame 1:16-bit data frame + | (1<<10) // 0:Full duplex 1:Receive-only + | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) + | (0<<8) // this bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored + | (0<<7) // 0:MSB 1:LSB + | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 + // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 + | (0<<2) // 0:Slave 1:Master + | (mode<<1) // CPOL + | (mode<<0); // CPHA + + ST7920_SPI_NUM->CR2 |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE } -void SPI_Slave(CIRCULAR_QUEUE *queue) +void SPI_Slave(CIRCULAR_QUEUE * queue) { spi_queue = queue; @@ -54,7 +77,7 @@ void SPI_Slave(CIRCULAR_QUEUE *queue) NVIC_InitTypeDef NVIC_InitStructure; SPI_GPIO_Init(ST7920_SPI); - GPIO_InitSet(PB12, MGPIO_MODE_IPU, 0); // CS + GPIO_InitSet(PB12, MGPIO_MODE_IPU, 0); // CS NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; @@ -64,12 +87,10 @@ void SPI_Slave(CIRCULAR_QUEUE *queue) RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE); SPI_Slave_CS_Config(); - SPI_ReEnable(0); // spi mode0 + SPI_ReEnable(0); // spi mode0 if ((GPIOB->IDR & (1<<12)) != 0) - { ST7920_SPI_NUM->CR1 |= (1<<6); - } } void SPI_SlaveDeInit(void) @@ -89,13 +110,7 @@ void SPI_SlaveDeInit(void) spi_queue = NULL; } -void SPI2_IRQHandler(void) -{ - spi_queue->data[spi_queue->index_w] = ST7920_SPI_NUM->DR; - spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; -} - -bool SPI_SlaveGetData(uint8_t *data) +bool SPI_SlaveGetData(uint8_t * data) { bool dataRead = false; @@ -110,33 +125,17 @@ bool SPI_SlaveGetData(uint8_t *data) return dataRead; } -// External interruption arrangement -void SPI_Slave_CS_Config(void) +void SPI2_IRQHandler(void) { - EXTI_InitTypeDef EXTI_InitStructure; - NVIC_InitTypeDef NVIC_InitStructure; - - // Connect GPIOB12 to the interrupt line - RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); - GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12); // PB12 is connected to interrupt line 12 - - // Set interrupt line 12 bit external falling edge interrupt - EXTI_InitStructure.EXTI_Line = EXTI_Line12; - EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; - EXTI_InitStructure.EXTI_LineCmd = ENABLE; - EXTI_Init(&EXTI_InitStructure); - - NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; // External interrupt channel where the key is enabled - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; // Preemption priority 2? - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; // Sub-priority 1 - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable external interrupt channel - NVIC_Init(&NVIC_InitStructure); + spi_queue->data[spi_queue->index_w] = ST7920_SPI_NUM->DR; + spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; } -#endif // endif for #if defined(ST7920_EMULATOR) + +#endif // ST7920_EMULATOR #ifdef HAS_EMULATOR -// External interruption + +// external interruption void EXTI15_10_IRQHandler(void) { switch (infoSettings.marlin_type) @@ -151,20 +150,21 @@ void EXTI15_10_IRQHandler(void) case LCD12864: if ((GPIOB->IDR & (1<<12)) != 0) { - SPI_ReEnable(!!(GPIOB->IDR & (1<<13))); // Adaptive spi mode0 / mode3 + SPI_ReEnable(!!(GPIOB->IDR & (1<<13))); // adaptive spi mode0 / mode3 ST7920_SPI_NUM->CR1 |= (1<<6); } else { - RCC->APB1RSTR |= 1<<14; // Reset SPI2 - RCC->APB1RSTR &= ~(1<<14); + RCC->APB1RSTR |= 1<<14; + RCC->APB1RSTR &= ~(1<<14); // reset SPI2 } - EXTI->PR = 1<<12; // Clear interrupt status register + EXTI->PR = 1<<12; // clear interrupt status register break; #endif } } -#endif // endif for #ifdef HAS_EMULATOR -#endif // endif for #if !defined(MKS_TFT) +#endif // HAS_EMULATOR + +#endif // MKS_TFT diff --git a/TFT/src/User/Hal/stm32f10x/spi_slave.h b/TFT/src/User/Hal/stm32f10x/spi_slave.h index 58ce091326..c6be7b5586 100644 --- a/TFT/src/User/Hal/stm32f10x/spi_slave.h +++ b/TFT/src/User/Hal/stm32f10x/spi_slave.h @@ -5,8 +5,8 @@ #include #include "CircularQueue.h" -void SPI_Slave(CIRCULAR_QUEUE *queue); +void SPI_Slave(CIRCULAR_QUEUE * queue); void SPI_SlaveDeInit(void); -bool SPI_SlaveGetData(uint8_t *data); +bool SPI_SlaveGetData(uint8_t * data); #endif diff --git a/TFT/src/User/Hal/stm32f10x/spi_slave_mks.c b/TFT/src/User/Hal/stm32f10x/spi_slave_mks.c index 9990136db0..7cfa2d4836 100644 --- a/TFT/src/User/Hal/stm32f10x/spi_slave_mks.c +++ b/TFT/src/User/Hal/stm32f10x/spi_slave_mks.c @@ -1,5 +1,5 @@ #include "spi_slave.h" -#include "variants.h" // for ST7920_EMULATOR etc... +#include "variants.h" // for ST7920_EMULATOR etc. #include "spi.h" #include "GPIO_Init.h" #include @@ -7,44 +7,67 @@ #if defined(MKS_TFT) #if defined(ST7920_EMULATOR) + // TODO: // now support SPI3 and PB1 CS only // more compatibility changes are needed -// Config for SPI Channel +// config for SPI Channel #if ST7920_SPI == _SPI1 - #define ST7920_SPI_NUM SPI1 + #define ST7920_SPI_NUM SPI1 #elif ST7920_SPI == _SPI2 - #define ST7920_SPI_NUM SPI2 + #define ST7920_SPI_NUM SPI2 #elif ST7920_SPI == _SPI3 - #define ST7920_SPI_NUM SPI3 + #define ST7920_SPI_NUM SPI3 #endif -CIRCULAR_QUEUE *spi_queue = NULL; +static CIRCULAR_QUEUE * spi_queue = NULL; + +// external interruption arrangement +static inline void SPI_Slave_CS_Config(void) +{ + EXTI_InitTypeDef EXTI_InitStructure; + NVIC_InitTypeDef NVIC_InitStructure; -void SPI_Slave_CS_Config(void); // forward declaration + // connect GPIOB1 to the interrupt line + RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); // enable AFIO clock + GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1); // PB1 is connected to interrupt line 1 -void SPI_ReEnable(uint8_t mode) + // set interrupt line 1 bit external falling edge interrupt + EXTI_InitStructure.EXTI_Line = EXTI_Line1; + EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; + EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; + EXTI_InitStructure.EXTI_LineCmd = ENABLE; + EXTI_Init(&EXTI_InitStructure); + + NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; // external interrupt channel where the key is enabled + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; // preemption priority 2? + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; // sub-priority 1 + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // enable external interrupt channel + NVIC_Init(&NVIC_InitStructure); +} + +static void SPI_ReEnable(uint8_t mode) { - ST7920_SPI_NUM->CR1 = (0<<15) // 0:2-line 1: 1-line - | (0<<14) // in bidirectional mode 0:read only 1: read/write - | (0<<13) // 0:disable CRC 1:enable CRC - | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) - | (0<<11) // 0:8-bit data frame 1:16-bit data frame - | (1<<10) // 0:Full duplex 1:Receive-only - | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) - | (0<<8) // This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored - | (0<<7) // 0:MSB 1:LSB - | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 - // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 - | (0<<2) // 0:Slave 1:Master - | (mode<<1) // CPOL -// | (mode<<0); // CPHA - | (1<<0); // always use 1 instead of "mode"! - - ST7920_SPI_NUM->CR2 |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE + ST7920_SPI_NUM->CR1 = (0<<15) // 0:2-line 1: 1-line + | (0<<14) // in bidirectional mode 0:read only 1: read/write + | (0<<13) // 0:disable CRC 1:enable CRC + | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) + | (0<<11) // 0:8-bit data frame 1:16-bit data frame + | (1<<10) // 0:Full duplex 1:Receive-only + | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) + | (0<<8) // this bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored + | (0<<7) // 0:MSB 1:LSB + | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 + // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 + | (0<<2) // 0:Slave 1:Master + | (mode<<1) // CPOL +// | (mode<<0); // CPHA + | (1<<0); // always use 1 instead of "mode"! + + ST7920_SPI_NUM->CR2 |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE } -void SPI_Slave(CIRCULAR_QUEUE *queue) +void SPI_Slave(CIRCULAR_QUEUE * queue) { spi_queue = queue; @@ -54,9 +77,9 @@ void SPI_Slave(CIRCULAR_QUEUE *queue) NVIC_InitTypeDef NVIC_InitStructure; - #ifndef SPI3_PIN_SMART_USAGE // if enabled, it avoids any SPI3 CS pin usage and free the MISO (PB4 pin) for encoder pins + #ifndef SPI3_PIN_SMART_USAGE // if enabled, it avoids any SPI3 CS pin usage and free the MISO (PB4 pin) for encoder pins SPI_GPIO_Init(ST7920_SPI); - GPIO_InitSet(SPI3_CS_PIN, MGPIO_MODE_IPN, 0); // MKS TFTs already have an external pull-up resistor on PB0 and PB1 pins + GPIO_InitSet(SPI3_CS_PIN, MGPIO_MODE_IPN, 0); // MKS TFTs already have an external pull-up resistor on PB0 and PB1 pins #endif NVIC_InitStructure.NVIC_IRQChannel = SPI3_IRQn; @@ -67,16 +90,14 @@ void SPI_Slave(CIRCULAR_QUEUE *queue) RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); - #ifndef SPI3_PIN_SMART_USAGE // if enabled, it avoids any SPI3 CS pin usage and free the MISO (PB4 pin) for encoder pins - SPI_Slave_CS_Config(); // not needed, you can also comment out this line! + #ifndef SPI3_PIN_SMART_USAGE // if enabled, it avoids any SPI3 CS pin usage and free the MISO (PB4 pin) for encoder pins + SPI_Slave_CS_Config(); // not needed, you can also comment out this line! #endif - SPI_ReEnable(0); // spi mode0 + SPI_ReEnable(0); // spi mode0 -// if ((GPIOB->IDR & (1<<12)) != 0) // always leave this line commented out! - { +//if ((GPIOB->IDR & (1<<12)) != 0) // always leave this line commented out! ST7920_SPI_NUM->CR1 |= (1<<6); - } } void SPI_SlaveDeInit(void) @@ -96,13 +117,7 @@ void SPI_SlaveDeInit(void) spi_queue = NULL; } -void SPI3_IRQHandler(void) -{ - spi_queue->data[spi_queue->index_w] = ST7920_SPI_NUM->DR; - spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; -} - -bool SPI_SlaveGetData(uint8_t *data) +bool SPI_SlaveGetData(uint8_t * data) { bool dataRead = false; @@ -117,46 +132,29 @@ bool SPI_SlaveGetData(uint8_t *data) return dataRead; } -// External interruption arrangement -void SPI_Slave_CS_Config(void) +void SPI3_IRQHandler(void) { - EXTI_InitTypeDef EXTI_InitStructure; - NVIC_InitTypeDef NVIC_InitStructure; - - // Connect GPIOB1 to the interrupt line - RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); - GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1); // PB1 is connected to interrupt line 1 - - // Set interrupt line 1 bit external falling edge interrupt - EXTI_InitStructure.EXTI_Line = EXTI_Line1; - EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; - EXTI_InitStructure.EXTI_LineCmd = ENABLE; - EXTI_Init(&EXTI_InitStructure); - - NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; // External interrupt channel where the key is enabled - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; // Preemption priority 2? - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; // Sub-priority 1 - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable external interrupt channel - NVIC_Init(&NVIC_InitStructure); + spi_queue->data[spi_queue->index_w] = ST7920_SPI_NUM->DR; + spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; } -// External interruption +// external interruption void EXTI1_IRQHandler(void) { if ((GPIOB->IDR & (1<<1)) != 0) { - SPI_ReEnable(!!(GPIOB->IDR & (1<<13))); // Adaptive spi mode0 / mode3 + SPI_ReEnable(!!(GPIOB->IDR & (1<<13))); // adaptive spi mode0 / mode3 ST7920_SPI_NUM->CR1 |= (1<<6); } else { - RCC->APB1RSTR |= 1<<14; // Reset SPI3 - RCC->APB1RSTR &= ~(1<<14); + RCC->APB1RSTR |= 1<<14; + RCC->APB1RSTR &= ~(1<<14); // reset SPI3 } - EXTI->PR = 1<<1; // Clear interrupt status register + EXTI->PR = 1<<1; // clear interrupt status register } -#endif // endif for #if defined(ST7920_EMULATOR) -#endif // endif for #if defined(MKS_TFT) +#endif // ST7920_EMULATOR + +#endif // MKS_TFT diff --git a/TFT/src/User/Hal/stm32f10x/timer_pwm.c b/TFT/src/User/Hal/stm32f10x/timer_pwm.c index 7857274fc4..197bcbf1f2 100644 --- a/TFT/src/User/Hal/stm32f10x/timer_pwm.c +++ b/TFT/src/User/Hal/stm32f10x/timer_pwm.c @@ -2,8 +2,8 @@ #include "includes.h" // for mcuClocks typedef struct { - TIM_TypeDef* tim; - volatile uint32_t* rcc_src; + TIM_TypeDef * tim; + volatile uint32_t * rcc_src; uint8_t rcc_bit; } TIMER; @@ -22,7 +22,8 @@ void TIM_PWM_SetDutyCycle(uint16_t tim_ch, uint8_t duty) { uint16_t timerIndex = TIMER_GET_TIM(tim_ch); uint16_t channel = TIMER_GET_CH(tim_ch); - const TIMER* timer = &pwmTimer[timerIndex]; + const TIMER * timer = &pwmTimer[timerIndex]; + switch (channel) { case 0: timer->tim->CCR1 = duty; break; @@ -36,29 +37,29 @@ void TIM_PWM_Init(uint16_t tim_ch) { uint16_t timerIndex = TIMER_GET_TIM(tim_ch); uint16_t channel = TIMER_GET_CH(tim_ch); - const TIMER* timer = &pwmTimer[timerIndex]; + const TIMER * timer = &pwmTimer[timerIndex]; uint32_t timerTmpClk = (timer->rcc_src == &RCC->APB1ENR) ? mcuClocks.PCLK1_Timer_Frequency : mcuClocks.PCLK2_Timer_Frequency; - *timer->rcc_src |= (1 << timer->rcc_bit); // Enable timer clock + *timer->rcc_src |= (1 << timer->rcc_bit); // enable timer clock - // Set PWM frequency to 500Hz + // set PWM frequency to 500Hz timer->tim->ARR = 100 - 1; timer->tim->PSC = timerTmpClk / (500 * 100) - 1; switch (channel) { - case 0: timer->tim->CCMR1 |= (6<<4) | (1<<3); break; // CH1 PWM1 mode - case 1: timer->tim->CCMR1 |= (6<<12) | (1<<11); break; // CH2 PWM1 mode - case 2: timer->tim->CCMR2 |= (6<<4) | (1<<3); break; // CH3 PWM1 mode - case 3: timer->tim->CCMR2 |= (6<<12) | (1<<11); break; // CH4 PWM1 mode + case 0: timer->tim->CCMR1 |= (6<<4) | (1<<3); break; // CH1 PWM1 mode + case 1: timer->tim->CCMR1 |= (6<<12) | (1<<11); break; // CH2 PWM1 mode + case 2: timer->tim->CCMR2 |= (6<<4) | (1<<3); break; // CH3 PWM1 mode + case 3: timer->tim->CCMR2 |= (6<<12) | (1<<11); break; // CH4 PWM1 mode } - timer->tim->CCER |= 1 << (4 * channel); - timer->tim->CR1 = (1 << 7) // Auto-reload preload enable - | (1 << 0); // Enbale timer + timer->tim->CCER |= 1 << (4 * channel); // enable channle + timer->tim->CR1 = (1 << 7) // auto-reload preload enable + | (1 << 0); // enbale timer if (timer->tim == TIM1 || timer->tim == TIM8) { // TIM1 & TIM8 advanced timer need config BDTR register for PWM - timer->tim->BDTR |= 1 << 15; // Main output enable + timer->tim->BDTR |= 1 << 15; // main output enable } } diff --git a/TFT/src/User/Hal/stm32f10x/timer_pwm.h b/TFT/src/User/Hal/stm32f10x/timer_pwm.h index ec906ffe0d..2677376be0 100644 --- a/TFT/src/User/Hal/stm32f10x/timer_pwm.h +++ b/TFT/src/User/Hal/stm32f10x/timer_pwm.h @@ -54,7 +54,7 @@ #define _TIM8_CH4 (((_TIM8)<<8) + 3) #define TIMER_GET_TIM(n) ((n>>8) & 0xFF) -#define TIMER_GET_CH(n) (n & 0xFF) +#define TIMER_GET_CH(n) (n & 0xFF) void TIM_PWM_SetDutyCycle(uint16_t tim_ch, uint8_t duty); void TIM_PWM_Init(uint16_t tim_ch); diff --git a/TFT/src/User/Hal/stm32f10x/uart.c b/TFT/src/User/Hal/stm32f10x/uart.c index b17881d198..fc55fe5791 100644 --- a/TFT/src/User/Hal/stm32f10x/uart.c +++ b/TFT/src/User/Hal/stm32f10x/uart.c @@ -1,5 +1,5 @@ #include "uart.h" -#include "variants.h" // for USART1_TX_PIN etc... +#include "variants.h" // for USART1_TX_PIN etc. #include "GPIO_Init.h" // USART1 default pins config @@ -38,7 +38,7 @@ #define UART5_RX_PIN PD2 #endif -static volatile uint32_t* const rcc_uart_rst[_UART_CNT] = { +static volatile uint32_t * const rcc_uart_rst[_UART_CNT] = { &RCC->APB2RSTR, &RCC->APB1RSTR, &RCC->APB1RSTR, @@ -46,7 +46,7 @@ static volatile uint32_t* const rcc_uart_rst[_UART_CNT] = { &RCC->APB1RSTR, }; -static volatile uint32_t* const rcc_uart_en[_UART_CNT] = { +static volatile uint32_t * const rcc_uart_en[_UART_CNT] = { &RCC->APB2ENR, &RCC->APB1ENR, &RCC->APB1ENR, @@ -62,34 +62,35 @@ static const uint32_t rcc_uart_bit[_UART_CNT] = { 0x00100000, // RCC_APB1 bit 20 }; -static USART_TypeDef* const uart[_UART_CNT] = { +static USART_TypeDef * const uart[_UART_CNT] = { USART1, // TX--PA9 RX--PA10 USART2, // TX--PA2 RX--PA3 USART3, // TX--PB10 RX--PB11 UART4, // TX--PC10 RX--PC11 - UART5}; // TX--PC12 RX--PD2 + UART5, // TX--PC12 RX--PD2 +}; static const uint16_t uart_tx[_UART_CNT] = {USART1_TX_PIN, USART2_TX_PIN, USART3_TX_PIN, UART4_TX_PIN, UART5_TX_PIN}; // TX static const uint16_t uart_rx[_UART_CNT] = {USART1_RX_PIN, USART2_RX_PIN, USART3_RX_PIN, UART4_RX_PIN, UART5_RX_PIN}; // RX -void UART_GPIO_Init(uint8_t port) +static inline void UART_GPIO_Init(uint8_t port) { GPIO_InitSet(uart_tx[port], MGPIO_MODE_AF_PP, 0); GPIO_InitSet(uart_rx[port], MGPIO_MODE_IPU, 0); } -void UART_GPIO_DeInit(uint8_t port) +static inline void UART_GPIO_DeInit(uint8_t port) { // set tx/rx to input GPIO_InitSet(uart_tx[port], MGPIO_MODE_IPN, 0); GPIO_InitSet(uart_rx[port], MGPIO_MODE_IPN, 0); } -void UART_Protocol_Init(uint8_t port, uint32_t baud) +static inline void UART_Protocol_Init(uint8_t port, uint32_t baud) { USART_InitTypeDef USART_InitStructure; - *rcc_uart_en[port] |= rcc_uart_bit[port]; // Enable clock + *rcc_uart_en[port] |= rcc_uart_bit[port]; // enable clock USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; @@ -97,12 +98,12 @@ void UART_Protocol_Init(uint8_t port, uint32_t baud) USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; - USART_Init(uart[port],&USART_InitStructure); + USART_Init(uart[port], &USART_InitStructure); USART_Cmd(uart[port],ENABLE); } -void UART_IRQ_Init(uint8_t port, uint16_t usart_it, FunctionalState idle_interrupt) +static inline void UART_IRQ_Init(uint8_t port, uint16_t usart_it, FunctionalState idle_interrupt) { uint32_t IRQ_Channel[_UART_CNT] = {USART1_IRQn, USART2_IRQn, USART3_IRQn, UART4_IRQn, UART5_IRQn}; @@ -121,7 +122,7 @@ void UART_Config(uint8_t port, uint32_t baud, uint16_t usart_it, bool idle_inter { UART_Protocol_Init(port, baud); UART_IRQ_Init(port, usart_it, idle_interrupt ? ENABLE : DISABLE); - UART_GPIO_Init(port); // After all initialization is completed, enable IO, otherwise a 0xFF will be sent automatically after power-on + UART_GPIO_Init(port); // after all initialization is completed, enable IO, otherwise a 0xFF will be sent automatically after power-on } void UART_DeConfig(uint8_t port) @@ -129,20 +130,20 @@ void UART_DeConfig(uint8_t port) UART_GPIO_DeInit(port); *rcc_uart_rst[port] |= rcc_uart_bit[port]; - *rcc_uart_rst[port] &= ~rcc_uart_bit[port]; // Reset clock + *rcc_uart_rst[port] &= ~rcc_uart_bit[port]; // reset clock } void UART_Write(uint8_t port, uint8_t d) { - while ((uart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET); + while ((uart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET); // wait uart[port]->DR = ((uint16_t)d & (uint16_t)0x01FF); } -void UART_Puts(uint8_t port, uint8_t *str) +void UART_Puts(uint8_t port, uint8_t * str) { while (*str) { - while ((uart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET); + while ((uart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET); // wait uart[port]->DR = ((uint16_t)*str++ & (uint16_t)0x01FF); } } diff --git a/TFT/src/User/Hal/stm32f10x/uart.h b/TFT/src/User/Hal/stm32f10x/uart.h index 3e481bdeb4..94036277c0 100644 --- a/TFT/src/User/Hal/stm32f10x/uart.h +++ b/TFT/src/User/Hal/stm32f10x/uart.h @@ -4,16 +4,16 @@ #include #include -#define _USART1 0 -#define _USART2 1 -#define _USART3 2 -#define _UART4 3 -#define _UART5 4 // UART5 don't support DMA -#define _UART_CNT 5 +#define _USART1 0 // USART0 +#define _USART2 1 // USART1 +#define _USART3 2 // USART2 +#define _UART4 3 // UART3 +#define _UART5 4 // UART4 don't support DMA +#define _UART_CNT 5 void UART_Config(uint8_t port, uint32_t baud, uint16_t usart_it, bool idle_interrupt); void UART_DeConfig(uint8_t port); -void UART_Puts(uint8_t port, uint8_t *str); void UART_Write(uint8_t port, uint8_t d); +void UART_Puts(uint8_t port, uint8_t * str); #endif diff --git a/TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.c b/TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.c index 186baeb750..a15f32f073 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.c @@ -1,37 +1,157 @@ #include "GPIO_Init.h" -/* -* bit 0:1 gpio mode 00: Input (reset state) 01: General purpose output mode 10: Alternate function mode 11: Analog mode -* bit 2 gpio output type 0: Output push-pull (reset state) 1: Output open-drain -* bit 3:4 gpio output speed 00: Low speed 01: Medium speed 10: High speed 11: Very high speed -* bit 5:6 gpio pull-up/pull-down 00: No pull-up, pull-down 01: Pull-up 10: Pull-down 11: Reserved -* bit 7 reserved -*/ -GPIO_TypeDef* const GPIO_Port[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH, GPIOI}; +#include "variants.h" // for GPIO_TypeDef etc. + +#define GPIO_MODE_GET_MODE(n) ((n) & 0x3) +#define GPIO_MODE_GET_OTYPE(n) (((n)>>2) & 0x1) +#define GPIO_MODE_GET_OSPEED(n) (((n)>>3) & 0x3) +#define GPIO_MODE_GET_PULL(n) (((n)>>5) & 0x3) + +#define GPIO_MODE_AF 2 +#define GPIO_AF0 0 +#define GPIO_AF1 1 +#define GPIO_AF2 2 +#define GPIO_AF3 3 +#define GPIO_AF4 4 +#define GPIO_AF5 5 +#define GPIO_AF6 6 +#define GPIO_AF7 7 +#define GPIO_AF8 8 +#define GPIO_AF9 9 +#define GPIO_AF10 10 +#define GPIO_AF11 11 +#define GPIO_AF12 12 +#define GPIO_AF13 13 +#define GPIO_AF14 14 +#define GPIO_AF15 15 + +#ifndef GPIO_AF_RTC_50Hz + /** + * @brief AF 0 selection + */ + #define GPIO_AF_RTC_50Hz ((uint8_t)0x00) /* RTC_50Hz Alternate Function mapping */ + #define GPIO_AF_MCO ((uint8_t)0x00) /* MCO (MCO1 and MCO2) Alternate Function mapping */ + #define GPIO_AF_TAMPER ((uint8_t)0x00) /* TAMPER (TAMPER_1 and TAMPER_2) Alternate Function mapping */ + #define GPIO_AF_SWJ ((uint8_t)0x00) /* SWJ (SWD and JTAG) Alternate Function mapping */ + #define GPIO_AF_TRACE ((uint8_t)0x00) /* TRACE Alternate Function mapping */ + + /** + * @brief AF 1 selection + */ + #define GPIO_AF_TIM1 ((uint8_t)0x01) /* TIM1 Alternate Function mapping */ + #define GPIO_AF_TIM2 ((uint8_t)0x01) /* TIM2 Alternate Function mapping */ + + /** + * @brief AF 2 selection + */ + #define GPIO_AF_TIM3 ((uint8_t)0x02) /* TIM3 Alternate Function mapping */ + #define GPIO_AF_TIM4 ((uint8_t)0x02) /* TIM4 Alternate Function mapping */ + #define GPIO_AF_TIM5 ((uint8_t)0x02) /* TIM5 Alternate Function mapping */ + + /** + * @brief AF 3 selection + */ + #define GPIO_AF_TIM8 ((uint8_t)0x03) /* TIM8 Alternate Function mapping */ + #define GPIO_AF_TIM9 ((uint8_t)0x03) /* TIM9 Alternate Function mapping */ + #define GPIO_AF_TIM10 ((uint8_t)0x03) /* TIM10 Alternate Function mapping */ + #define GPIO_AF_TIM11 ((uint8_t)0x03) /* TIM11 Alternate Function mapping */ + + /** + * @brief AF 4 selection + */ + #define GPIO_AF_I2C1 ((uint8_t)0x04) /* I2C1 Alternate Function mapping */ + #define GPIO_AF_I2C2 ((uint8_t)0x04) /* I2C2 Alternate Function mapping */ + #define GPIO_AF_I2C3 ((uint8_t)0x04) /* I2C3 Alternate Function mapping */ + + /** + * @brief AF 5 selection + */ + #define GPIO_AF_SPI1 ((uint8_t)0x05) /* SPI1 Alternate Function mapping */ + #define GPIO_AF_SPI2 ((uint8_t)0x05) /* SPI2/I2S2 Alternate Function mapping */ + + /** + * @brief AF 6 selection + */ + #define GPIO_AF_SPI3 ((uint8_t)0x06) /* SPI3/I2S3 Alternate Function mapping */ + + /** + * @brief AF 7 selection + */ + #define GPIO_AF_USART1 ((uint8_t)0x07) /* USART1 Alternate Function mapping */ + #define GPIO_AF_USART2 ((uint8_t)0x07) /* USART2 Alternate Function mapping */ + #define GPIO_AF_USART3 ((uint8_t)0x07) /* USART3 Alternate Function mapping */ + + /** + * @brief AF 8 selection + */ + #define GPIO_AF_UART4 ((uint8_t)0x08) /* UART4 Alternate Function mapping */ + #define GPIO_AF_UART5 ((uint8_t)0x08) /* UART5 Alternate Function mapping */ + #define GPIO_AF_USART6 ((uint8_t)0x08) /* USART6 Alternate Function mapping */ + + /** + * @brief AF 9 selection + */ + #define GPIO_AF_CAN1 ((uint8_t)0x09) /* CAN1 Alternate Function mapping */ + #define GPIO_AF_CAN2 ((uint8_t)0x09) /* CAN2 Alternate Function mapping */ + #define GPIO_AF_TIM12 ((uint8_t)0x09) /* TIM12 Alternate Function mapping */ + #define GPIO_AF_TIM13 ((uint8_t)0x09) /* TIM13 Alternate Function mapping */ + #define GPIO_AF_TIM14 ((uint8_t)0x09) /* TIM14 Alternate Function mapping */ + + /** + * @brief AF 10 selection + */ + #define GPIO_AF_OTG_FS ((uint8_t)0xA) /* OTG_FS Alternate Function mapping */ + #define GPIO_AF_OTG_HS ((uint8_t)0xA) /* OTG_HS Alternate Function mapping */ + + /** + * @brief AF 11 selection + */ + #define GPIO_AF_ETH ((uint8_t)0x0B) /* ETHERNET Alternate Function mapping */ + + /** + * @brief AF 12 selection + */ + #define GPIO_AF_FSMC ((uint8_t)0xC) /* FSMC Alternate Function mapping */ + #define GPIO_AF_OTG_HS_FS ((uint8_t)0xC) /* OTG HS configured in FS, Alternate Function mapping */ + #define GPIO_AF_SDIO ((uint8_t)0xC) /* SDIO Alternate Function mapping */ + + /** + * @brief AF 13 selection + */ + #define GPIO_AF_DCMI ((uint8_t)0x0D) /* DCMI Alternate Function mapping */ + + /** + * @brief AF 15 selection + */ + #define GPIO_AF_EVENTOUT ((uint8_t)0x0F) /* EVENTOUT Alternate Function mapping */ + +#endif + +static GPIO_TypeDef * const GPIO_Port[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH, GPIOI}; void GPIO_InitSet(uint16_t io, GPIO_MODE mode, uint8_t AF) { uint16_t port = GPIO_GET_PORT(io); uint16_t pin = GPIO_GET_PIN(io); - RCC->AHB1ENR |= 1 << port; // Enable GPIO Clock + RCC->AHB1ENR |= 1 << port; // enable GPIO Clock - GPIO_Port[port]->MODER &= ~(3 << (pin*2)); // clear mode bits - GPIO_Port[port]->MODER |= (GPIO_MODE_GET_MODE(mode)) << (pin*2); // set mode bits - GPIO_Port[port]->PUPDR &= ~(3 << (pin*2)); // clear pull bits - GPIO_Port[port]->PUPDR |= (GPIO_MODE_GET_PULL(mode)) << (pin*2); // set pull bits + GPIO_Port[port]->MODER &= ~(3 << (pin * 2)); // clear mode bits + GPIO_Port[port]->MODER |= (GPIO_MODE_GET_MODE(mode)) << (pin * 2); // set mode bits + GPIO_Port[port]->PUPDR &= ~(3 << (pin * 2)); // clear pull bits + GPIO_Port[port]->PUPDR |= (GPIO_MODE_GET_PULL(mode)) << (pin * 2); // set pull bits - if ((GPIO_MODE_GET_MODE(mode) == 1) || (GPIO_MODE_GET_MODE(mode) == 2)) // output + if ((GPIO_MODE_GET_MODE(mode) == 1) || (GPIO_MODE_GET_MODE(mode) == 2)) // output { - GPIO_Port[port]->OTYPER &= ~(1 << pin); // clear output type bit - GPIO_Port[port]->OTYPER |= GPIO_MODE_GET_OTYPE(mode) << pin; // set output type bit - GPIO_Port[port]->OSPEEDR &= ~(3 << (pin*2)); // clear speed bits - GPIO_Port[port]->OSPEEDR |= (GPIO_MODE_GET_OSPEED(mode)) << (pin*2); // set speed bits + GPIO_Port[port]->OTYPER &= ~(1 << pin); // clear output type bit + GPIO_Port[port]->OTYPER |= GPIO_MODE_GET_OTYPE(mode) << pin; // set output type bit + GPIO_Port[port]->OSPEEDR &= ~(3 << (pin * 2)); // clear speed bits + GPIO_Port[port]->OSPEEDR |= (GPIO_MODE_GET_OSPEED(mode)) << (pin * 2); // set speed bits } if (GPIO_MODE_GET_MODE(mode) == GPIO_MODE_AF) { - GPIO_Port[port]->AFR[pin >> 0x03] &= ~(0xF << ((pin & 0x07) * 4)); // clear alternate function bits - GPIO_Port[port]->AFR[pin >> 0x03] |= AF<< ((pin & 0x07) * 4); // set alternate function bits + GPIO_Port[port]->AFR[pin >> 0x03] &= ~(0xF << ((pin & 0x07) * 4)); // clear alternate function bits + GPIO_Port[port]->AFR[pin >> 0x03] |= AF<< ((pin & 0x07) * 4); // set alternate function bits } } @@ -60,11 +180,7 @@ uint8_t GPIO_GetLevel(uint16_t io) uint16_t pin = GPIO_GET_PIN(io); if ((GPIO_Port[port]->IDR & (1 << pin)) != 0) - { return 1; - } else - { return 0; - } } diff --git a/TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.h b/TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.h index 1394b91865..1b2ed327b4 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/GPIO_Init.h @@ -1,153 +1,28 @@ #ifndef _GPIO_INIT_H_ #define _GPIO_INIT_H_ -#include "variants.h" +#include #include "STM32Fxx_Pins.h" /* -* bit 0:1 gpio mode 00: Input (reset state) 01: General purpose output mode 10: Alternate function mode 11: Analog mode -* bit 2 gpio output type 0: Output push-pull (reset state) 1: Output open-drain -* bit 3:4 gpio output speed 00: Low speed 01: Medium speed 10: High speed 11: Very high speed -* bit 5:6 gpio pull-up/pull-down 00: No pull-up, pull-down 01: Pull-up 10: Pull-down 11: Reserved -* bit 7 reserved -*/ + * bit 0:1 gpio mode 00: Input (reset state) 01: General purpose output mode 10: Alternate function mode 11: Analog mode + * bit 2 gpio output type 0: Output push-pull (reset state) 1: Output open-drain + * bit 3:4 gpio output speed 00: Low speed 01: Medium speed 10: High speed 11: Very high speed + * bit 5:6 gpio pull-up/pull-down 00: No pull-up, pull-down 01: Pull-up 10: Pull-down 11: Reserved + * bit 7 reserved + */ typedef enum { - MGPIO_MODE_IPN = (0<<5)|(3<<3)|(0<<2)|(0), - MGPIO_MODE_IPU = (1<<5)|(3<<3)|(0<<2)|(0), - MGPIO_MODE_IPD = (2<<5)|(3<<3)|(0<<2)|(0), - MGPIO_MODE_OUT_PP = (0<<5)|(3<<3)|(0<<2)|(1), - MGPIO_MODE_OUT_OD = (0<<5)|(3<<3)|(1<<2)|(1), - MGPIO_MODE_AF_PP = (0<<5)|(3<<3)|(0<<2)|(2), - MGPIO_MODE_AF_OD = (0<<5)|(3<<3)|(1<<2)|(2), - MGPIO_MODE_AIN = (0<<5)|(3<<3)|(0<<2)|(3), + MGPIO_MODE_IPN = (0<<5) | (3<<3) | (0<<2) | (0), + MGPIO_MODE_IPU = (1<<5) | (3<<3) | (0<<2) | (0), + MGPIO_MODE_IPD = (2<<5) | (3<<3) | (0<<2) | (0), + MGPIO_MODE_OUT_PP = (0<<5) | (3<<3) | (0<<2) | (1), + MGPIO_MODE_OUT_OD = (0<<5) | (3<<3) | (1<<2) | (1), + MGPIO_MODE_AF_PP = (0<<5) | (3<<3) | (0<<2) | (2), + MGPIO_MODE_AF_OD = (0<<5) | (3<<3) | (1<<2) | (2), + MGPIO_MODE_AIN = (0<<5) | (3<<3) | (0<<2) | (3), } GPIO_MODE; -#define GPIO_MODE_GET_MODE(n) ((n) & 0x3) -#define GPIO_MODE_GET_OTYPE(n) (((n)>>2) & 0x1) -#define GPIO_MODE_GET_OSPEED(n) (((n)>>3) & 0x3) -#define GPIO_MODE_GET_PULL(n) (((n)>>5) & 0x3) - -#define GPIO_MODE_AF 2 -#define GPIO_AF0 0 -#define GPIO_AF1 1 -#define GPIO_AF2 2 -#define GPIO_AF3 3 -#define GPIO_AF4 4 -#define GPIO_AF5 5 -#define GPIO_AF6 6 -#define GPIO_AF7 7 -#define GPIO_AF8 8 -#define GPIO_AF9 9 -#define GPIO_AF10 10 -#define GPIO_AF11 11 -#define GPIO_AF12 12 -#define GPIO_AF13 13 -#define GPIO_AF14 14 -#define GPIO_AF15 15 - -#ifndef GPIO_AF_RTC_50Hz - /** - * @brief AF 0 selection - */ - #define GPIO_AF_RTC_50Hz ((uint8_t)0x00) /* RTC_50Hz Alternate Function mapping */ - #define GPIO_AF_MCO ((uint8_t)0x00) /* MCO (MCO1 and MCO2) Alternate Function mapping */ - #define GPIO_AF_TAMPER ((uint8_t)0x00) /* TAMPER (TAMPER_1 and TAMPER_2) Alternate Function mapping */ - #define GPIO_AF_SWJ ((uint8_t)0x00) /* SWJ (SWD and JTAG) Alternate Function mapping */ - #define GPIO_AF_TRACE ((uint8_t)0x00) /* TRACE Alternate Function mapping */ - - /** - * @brief AF 1 selection - */ - #define GPIO_AF_TIM1 ((uint8_t)0x01) /* TIM1 Alternate Function mapping */ - #define GPIO_AF_TIM2 ((uint8_t)0x01) /* TIM2 Alternate Function mapping */ - - /** - * @brief AF 2 selection - */ - #define GPIO_AF_TIM3 ((uint8_t)0x02) /* TIM3 Alternate Function mapping */ - #define GPIO_AF_TIM4 ((uint8_t)0x02) /* TIM4 Alternate Function mapping */ - #define GPIO_AF_TIM5 ((uint8_t)0x02) /* TIM5 Alternate Function mapping */ - - /** - * @brief AF 3 selection - */ - #define GPIO_AF_TIM8 ((uint8_t)0x03) /* TIM8 Alternate Function mapping */ - #define GPIO_AF_TIM9 ((uint8_t)0x03) /* TIM9 Alternate Function mapping */ - #define GPIO_AF_TIM10 ((uint8_t)0x03) /* TIM10 Alternate Function mapping */ - #define GPIO_AF_TIM11 ((uint8_t)0x03) /* TIM11 Alternate Function mapping */ - - /** - * @brief AF 4 selection - */ - #define GPIO_AF_I2C1 ((uint8_t)0x04) /* I2C1 Alternate Function mapping */ - #define GPIO_AF_I2C2 ((uint8_t)0x04) /* I2C2 Alternate Function mapping */ - #define GPIO_AF_I2C3 ((uint8_t)0x04) /* I2C3 Alternate Function mapping */ - - /** - * @brief AF 5 selection - */ - #define GPIO_AF_SPI1 ((uint8_t)0x05) /* SPI1 Alternate Function mapping */ - #define GPIO_AF_SPI2 ((uint8_t)0x05) /* SPI2/I2S2 Alternate Function mapping */ - - /** - * @brief AF 6 selection - */ - #define GPIO_AF_SPI3 ((uint8_t)0x06) /* SPI3/I2S3 Alternate Function mapping */ - - /** - * @brief AF 7 selection - */ - #define GPIO_AF_USART1 ((uint8_t)0x07) /* USART1 Alternate Function mapping */ - #define GPIO_AF_USART2 ((uint8_t)0x07) /* USART2 Alternate Function mapping */ - #define GPIO_AF_USART3 ((uint8_t)0x07) /* USART3 Alternate Function mapping */ - - /** - * @brief AF 8 selection - */ - #define GPIO_AF_UART4 ((uint8_t)0x08) /* UART4 Alternate Function mapping */ - #define GPIO_AF_UART5 ((uint8_t)0x08) /* UART5 Alternate Function mapping */ - #define GPIO_AF_USART6 ((uint8_t)0x08) /* USART6 Alternate Function mapping */ - - /** - * @brief AF 9 selection - */ - #define GPIO_AF_CAN1 ((uint8_t)0x09) /* CAN1 Alternate Function mapping */ - #define GPIO_AF_CAN2 ((uint8_t)0x09) /* CAN2 Alternate Function mapping */ - #define GPIO_AF_TIM12 ((uint8_t)0x09) /* TIM12 Alternate Function mapping */ - #define GPIO_AF_TIM13 ((uint8_t)0x09) /* TIM13 Alternate Function mapping */ - #define GPIO_AF_TIM14 ((uint8_t)0x09) /* TIM14 Alternate Function mapping */ - - /** - * @brief AF 10 selection - */ - #define GPIO_AF_OTG_FS ((uint8_t)0xA) /* OTG_FS Alternate Function mapping */ - #define GPIO_AF_OTG_HS ((uint8_t)0xA) /* OTG_HS Alternate Function mapping */ - - /** - * @brief AF 11 selection - */ - #define GPIO_AF_ETH ((uint8_t)0x0B) /* ETHERNET Alternate Function mapping */ - - /** - * @brief AF 12 selection - */ - #define GPIO_AF_FSMC ((uint8_t)0xC) /* FSMC Alternate Function mapping */ - #define GPIO_AF_OTG_HS_FS ((uint8_t)0xC) /* OTG HS configured in FS, Alternate Function mapping */ - #define GPIO_AF_SDIO ((uint8_t)0xC) /* SDIO Alternate Function mapping */ - - /** - * @brief AF 13 selection - */ - #define GPIO_AF_DCMI ((uint8_t)0x0D) /* DCMI Alternate Function mapping */ - - /** - * @brief AF 15 selection - */ - #define GPIO_AF_EVENTOUT ((uint8_t)0x0F) /* EVENTOUT Alternate Function mapping */ - -#endif - void GPIO_InitSet(uint16_t io, GPIO_MODE mode, uint8_t AF); void GPIO_SetLevel(uint16_t io, uint8_t level); void GPIO_ToggleLevel(uint16_t io); diff --git a/TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.c b/TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.c index 97c2233e50..76039c2760 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.c @@ -1,102 +1,81 @@ #include "HAL_Flash.h" +#include "variants.h" // for FLASH_Unlock etc. #include "my_misc.h" /* -Sector 0 0x0800 0000 - 0x0800 3FFF 16 Kbyte -Sector 1 0x0800 4000 - 0x0800 7FFF 16 Kbyte -Sector 2 0x0800 8000 - 0x0800 BFFF 16 Kbyte -Sector 3 0x0800 C000 - 0x0800 FFFF 16 Kbyte -Sector 4 0x0801 0000 - 0x0801 FFFF 64 Kbyte -Sector 5 0x0802 0000 - 0x0803 FFFF 128 Kbyte // 256KByte -Sector 6 0x0804 0000 - 0x0805 FFFF 128 Kbyte -... -... -... -Sector 11 0x080E 0000 - 0x080F FFFF 128 Kbyte -*/ + * Sector 0 0x0800 0000 - 0x0800 3FFF 16 Kbyte + * Sector 1 0x0800 4000 - 0x0800 7FFF 16 Kbyte + * Sector 2 0x0800 8000 - 0x0800 BFFF 16 Kbyte + * Sector 3 0x0800 C000 - 0x0800 FFFF 16 Kbyte + * Sector 4 0x0801 0000 - 0x0801 FFFF 64 Kbyte + * Sector 5 0x0802 0000 - 0x0803 FFFF 128 Kbyte // 256KByte + * Sector 6 0x0804 0000 - 0x0805 FFFF 128 Kbyte + * ... + * ... + * ... + * Sector 11 0x080E 0000 - 0x080F FFFF 128 Kbyte + */ #ifdef MKS_TFT35_V1_0 // MKS_TFT35_V1_0 bootloader is 48KB, adjust flash mapping // use darkspr1te alternative small loader to fix this issue and return to using 16kb lower flash for param storage or switch to I2C // if we use getsector routine it returns this value anyway, so FLASH_SECTOR is not used #define SIGN_ADDRESS (0x08040000) // reserve the last 128k for user params or we damage the boot loader on this board - #define FLASH_SECTOR FLASH_Sector_6 // Points to an available sector (0x08040000 - 0x0805FFFF) + #define FLASH_SECTOR FLASH_Sector_6 // points to an available sector (0x08040000 - 0x0805FFFF) #else - #define SIGN_ADDRESS (0x08004000) // reserve the second sector (16KB) to save user parameters + #define SIGN_ADDRESS (0x08004000) // reserve the second sector (16KB) to save user parameters #define FLASH_SECTOR FLASH_Sector_1 #endif #ifdef MKS_TFT35_V1_0 -static uint32_t GetSector(uint32_t Address) +static inline uint32_t GetSector(uint32_t Address) { uint32_t sector = 0; if ((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0)) - { sector = FLASH_Sector_0; - } else if ((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1)) - { sector = FLASH_Sector_1; - } else if ((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2)) - { sector = FLASH_Sector_2; - } else if ((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3)) - { sector = FLASH_Sector_3; - } else if ((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4)) - { sector = FLASH_Sector_4; - } else if ((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5)) - { sector = FLASH_Sector_5; - } else if ((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6)) - { sector = FLASH_Sector_6; - } else if ((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7)) - { sector = FLASH_Sector_7; - } else if ((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8)) - { sector = FLASH_Sector_8; - } else if ((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9)) - { sector = FLASH_Sector_9; - } else if ((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10)) - { sector = FLASH_Sector_10; - } else if ((Address < ADDR_FLASH_SECTOR_12) && (Address >= ADDR_FLASH_SECTOR_11)) - { sector = FLASH_Sector_11; - } return sector; } #endif // MKS_TFT35_V1_0 -void HAL_FlashRead(uint8_t *data, uint32_t len) +void HAL_FlashRead(uint8_t * data, uint32_t len) { uint32_t i = 0; + for (i = 0; i < len; i++) { - data[i] = *(volatile uint8_t*)(SIGN_ADDRESS + i); + data[i] = *(volatile uint8_t *)(SIGN_ADDRESS + i); } } -void HAL_FlashWrite(uint8_t *data, uint32_t len) +void HAL_FlashWrite(uint8_t * data, uint32_t len) { uint32_t i = 0; + FLASH_Unlock(); #ifdef MKS_TFT35_V1_0 // added for MKS_TFT35_V1_0 support @@ -109,5 +88,6 @@ void HAL_FlashWrite(uint8_t *data, uint32_t len) { FLASH_ProgramByte(SIGN_ADDRESS + i, data[i]); } + FLASH_Lock(); } diff --git a/TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.h b/TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.h index ecc0d76cec..52d9aed525 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/HAL_Flash.h @@ -1,7 +1,8 @@ #ifndef _HAL_FLASH_H_ #define _HAL_FLASH_H_ -#include "variants.h" // for uint8_t etc... +#include +#include "variants.h" // for MKS_TFT35_V1_0 etc. #ifdef MKS_TFT35_V1_0 // added for MKS TFT 35 V1.0 support #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) // Base @ of Sector 0, 16 Kbytes @@ -30,7 +31,7 @@ #define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) // Base @ of Sector 11, 128 Kbytes #endif -void HAL_FlashRead(uint8_t *data, uint32_t len); -void HAL_FlashWrite(uint8_t *data, uint32_t len); +void HAL_FlashRead(uint8_t * data, uint32_t len); +void HAL_FlashWrite(uint8_t * data, uint32_t len); #endif diff --git a/TFT/src/User/Hal/stm32f2_f4xx/Serial.c b/TFT/src/User/Hal/stm32f2_f4xx/Serial.c index 88baa21a07..fbf0cc1599 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/Serial.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/Serial.c @@ -41,7 +41,7 @@ const SERIAL_CFG Serial[_UART_CNT] = { }; // disable RX DMA and clear all interrupt flags for a serial port -void Serial_DMA_DisableAndClearFlagsRX(uint8_t port) +static void Serial_DMA_DisableAndClearFlagsRX(uint8_t port) { Serial[port].dma_streamRX->CR &= ~(1<<0); // disable RX DMA @@ -81,7 +81,7 @@ void Serial_DMA_DisableAndClearFlagsRX(uint8_t port) #ifdef TX_DMA_WRITE // disable TX DMA and clear all interrupt flags for a serial port -void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) +static void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) { Serial[port].dma_streamTX->CR &= ~(1<<0); // disable TX DMA @@ -120,7 +120,7 @@ void Serial_DMA_DisableAndClearFlagsTX(uint8_t port) #endif -void Serial_DMA_Config(uint8_t port) +static inline void Serial_DMA_Config(uint8_t port) { const SERIAL_CFG * cfg = &Serial[port]; @@ -175,7 +175,7 @@ void Serial_DMA_Config(uint8_t port) cfg->dma_streamRX->CR |= (1<<0); // RX enable DMA } -void Serial_ClearData(uint8_t port) +static void Serial_ClearData(uint8_t port) { dmaL1DataRX[port].wIndex = dmaL1DataRX[port].rIndex = dmaL1DataRX[port].flag = dmaL1DataRX[port].cacheSize = 0; @@ -227,7 +227,7 @@ void Serial_DeConfig(uint8_t port) #ifdef TX_DMA_WRITE // TX DMA based serial writing // DMA serial write support function -void Serial_Send_TX(uint8_t port) +static void Serial_Send_TX(uint8_t port) { // setup DMA transfer, and wait for serial Transfer Complete (TX) interrupt in ISR if (dmaL1DataTX[port].wIndex >= dmaL1DataTX[port].rIndex) diff --git a/TFT/src/User/Hal/stm32f2_f4xx/Serial.h b/TFT/src/User/Hal/stm32f2_f4xx/Serial.h index e7b9a2fd33..5487a0cd88 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/Serial.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/Serial.h @@ -2,8 +2,8 @@ #define _SERIAL_H_ #include -#include "variants.h" // for uint32_t etc... -#include "uart.h" +#include "variants.h" // for USART_TypeDef etc. +#include "uart.h" // for _UART_CNT etc. // comment out this line to use TX interrupt based serial writing instead of TX DMA based serial writing #define TX_DMA_WRITE diff --git a/TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.c b/TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.c index 06317d308a..1105e0321e 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.c @@ -12,30 +12,53 @@ #include "GPIO_Init.h" #include "variants.h" -uint8_t buffing[AT24CXX_PAGE_SIZE + 2] = {0}; - -void I2C1_init(uint32_t I2C_baudrate) +#define EEPROM_OK 0 +#define EEPROM_FAIL 1 +#define EEPROM_TIMEOUT 3 + +#define AT24C01 127 // PAGE_SIZE 8 bytes +#define AT24C02 255 // PAGE_SIZE 8 bytes +#define AT24C04 511 // PAGE_SIZE 16 bytes +#define AT24C08 1023 // PAGE_SIZE 16 bytes +#define AT24C16 2047 // PAGE_SIZE 16 bytes +#define AT24C32 4095 +#define AT24C64 8191 +#define AT24C128 16383 +#define AT24C256 32767 + +//#define AT24CXX_TYPE EEPROM_TYPE +#define AT24CXX_ADDRESS EEPROM_ADDRESS +#define AT24CXX_PAGE_SIZE 16 // 8 bytes + +// timeout for write based on max read/write which is +// AT24CXX_PAGE_SIZE bytes: 8 bits * AT24CXX_PAGE_SIZE / I2C_SPEED +//#define EEPROM_READ_TIMEOUT ((uint32_t)(1000)) +//#define EEPROM_WRITE_TIMEOUT ((uint32_t)(1000)) + +static inline void I2C1_init(uint32_t I2C_baudrate) { I2C_InitTypeDef I2C_InitStructure; - I2C_DeInit(I2C1); // Deinit I2C1 + + I2C_DeInit(I2C1); // deinit I2C1 // configure I2C1 I2C_InitStructure.I2C_ClockSpeed = I2C_baudrate; // 400kHz I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; // I2C mode I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard I2C_InitStructure.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode - I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; // Enable acknowledge when reading (can be changed later on) + I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; // enable acknowledge when reading (can be changed later on) I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses I2C_Init(I2C1, &I2C_InitStructure); // init I2Cx - I2C_Cmd(I2C1, ENABLE); // Enable I2C1 + I2C_Cmd(I2C1, ENABLE); // enable I2C1 I2C_StretchClockCmd(I2C1, ENABLE); } -void I2C_GPIO_Config() +static inline void I2C_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // Enable GPIO clock - RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // Enable I2C clock + + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // enable GPIO clock + RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // enable I2C clock GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); // SCL, Pin B6 GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); // SDA, Pin B7 @@ -46,103 +69,101 @@ void I2C_GPIO_Config() GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; // set output to open drain, only pulled low, don't drive high GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // enable pull up resistors GPIO_Init(GPIOB, &GPIO_InitStructure); - // Connect I2Cx pins to Alternative Function I2C + // connect I2Cx pins to Alternative Function I2C } /*===============================================================================================================*/ /* I2C_Write_Byte */ /*===============================================================================================================*/ -void I2C_Write_Byte(I2C_TypeDef* I2Cx, uint8_t reg_addr, uint8_t reg_data, uint8_t dev_Addr) +static void I2C_Write_Byte(I2C_TypeDef * I2Cx, uint8_t reg_addr, uint8_t reg_data, uint8_t dev_Addr) { - while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)); /* While the bus is busy */ + while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)); // while the bus is busy - I2C_GenerateSTART(I2Cx, ENABLE); /* Send I2C1 START condition */ + I2C_GenerateSTART(I2Cx, ENABLE); // send I2C1 START condition while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)); - GPIO_SetLevel(PB4, 0); // Pin 6 of XS13 Auto Power Off for MKS_TFT35_V1_0 + GPIO_SetLevel(PB4, 0); // pin 6 of XS13 Auto Power Off for MKS_TFT35_V1_0 - I2C_Send7bitAddress(I2Cx, dev_Addr, I2C_Direction_Transmitter); /* Send Device slave Address for write */ + I2C_Send7bitAddress(I2Cx, dev_Addr, I2C_Direction_Transmitter); // send Device slave Address for write while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); - GPIO_SetLevel(PA8, 0); // WIFI_RST for MKS_TFT35_V1_0 + GPIO_SetLevel(PA8, 0); // WIFI_RST for MKS_TFT35_V1_0 - I2C_SendData(I2Cx, reg_addr); /* Send the Device internal register address */ + I2C_SendData(I2Cx, reg_addr); // send the Device internal register address while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); - I2C_SendData(I2Cx, reg_data); /* Send I2C1 EEPROM data */ + I2C_SendData(I2Cx, reg_data); // send I2C1 EEPROM data while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); - I2C_AcknowledgeConfig(I2Cx, DISABLE); /* Disable Acknowledgement */ - Delay_us(1); /* Generate 12.285uS delay as required*/ + I2C_AcknowledgeConfig(I2Cx, DISABLE); // disable Acknowledgement + Delay_us(1); // generate 12.285uS delay as required - I2C_GenerateSTOP(I2Cx, ENABLE); /* Send I2C1 STOP Condition */ - Delay_us(1); /* Generate 12.285uS delay as required*/ + I2C_GenerateSTOP(I2Cx, ENABLE); // send I2C1 STOP Condition + Delay_us(1); // generate 12.285uS delay as required - I2C_AcknowledgeConfig(I2Cx, ENABLE); /* Enable Acknowledgement to be ready for another reception */ + I2C_AcknowledgeConfig(I2Cx, ENABLE); // enable Acknowledgement to be ready for another reception while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); - GPIO_SetLevel(PA8, 1); // WIFI_RST for MKS_TFT35_V1_0 - GPIO_SetLevel(PB4, 1); // Pin 6 of XS13 Auto Power Off for MKS_TFT35_V1_0 + GPIO_SetLevel(PA8, 1); // WIFI_RST for MKS_TFT35_V1_0 + GPIO_SetLevel(PB4, 1); // pin 6 of XS13 Auto Power Off for MKS_TFT35_V1_0 } /*=============================================================================================*/ /* I2C_Read_Byte */ /*=============================================================================================*/ -uint8_t I2C_Read_Byte(I2C_TypeDef* I2Cx, uint8_t reg_addr, uint8_t dev_Addr) +static uint8_t I2C_Read_Byte(I2C_TypeDef * I2Cx, uint8_t reg_addr, uint8_t dev_Addr) { char Red_reg; - /*------------------------------ Start Dummy write ----------------------------------------*/ - while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)); /* While the bus is busy */ + /*------------------------------ start dummy write ----------------------------------------*/ + while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)); // while the bus is busy - I2C_GenerateSTART(I2Cx, ENABLE); /* Send I2C1 START condition */ + I2C_GenerateSTART(I2Cx, ENABLE); // send I2C1 START condition while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)); - I2C_Send7bitAddress(I2Cx, dev_Addr, I2C_Direction_Transmitter); /* Send Device slave Address for write */ + I2C_Send7bitAddress(I2Cx, dev_Addr, I2C_Direction_Transmitter); // send Device slave Address for write while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); - I2C_SendData(I2Cx, reg_addr); /* Send the Device internal register address */ + I2C_SendData(I2Cx, reg_addr); // send the Device internal register address while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); - /*------------------------------- Dummy write End -----------------------------------------*/ + /*------------------------------- dummy write end -----------------------------------------*/ - /*------------------------------ Random Read Start ----------------------------------------*/ + /*------------------------------ start random read ----------------------------------------*/ - I2C_GenerateSTART(I2Cx, ENABLE); /* Send STRAT condition a second time called repeated start*/ + I2C_GenerateSTART(I2Cx, ENABLE); // send START condition a second time called repeated start while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)); - I2C_Send7bitAddress(I2Cx, (dev_Addr | 0x01), I2C_Direction_Receiver); /* Send Device slave Address for read */ + I2C_Send7bitAddress(I2Cx, (dev_Addr | 0x01), I2C_Direction_Receiver); // send Device slave Address for read while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); - /* Test on I2C1 EV7 and clear it */ /* Check for BUSY, MSL and RXNE flags = (EV7) */ + // test on I2C1 EV7 and clear it. Check for BUSY, MSL and RXNE flags = (EV7) while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)); Red_reg = I2C_ReceiveData(I2Cx); - I2C_AcknowledgeConfig(I2Cx, DISABLE); /* Disable Acknowledgement */ - Delay_us(1); /* Generate 12.285uS delay as required */ - I2C_GenerateSTOP(I2Cx, ENABLE); /* Send I2C1 STOP Condition */ - Delay_us(1); /* Generate 12.285uS delay as required */ - // I2C_AcknowledgeConfig(I2C1, ENABLE); /* Enable Acknowledgement to be ready for another reception */ + I2C_AcknowledgeConfig(I2Cx, DISABLE); // disable Acknowledgement + Delay_us(1); // generate 12.285uS delay as required + I2C_GenerateSTOP(I2Cx, ENABLE); // send I2C1 STOP Condition + Delay_us(1); // generate 12.285uS delay as required + //I2C_AcknowledgeConfig(I2C1, ENABLE); // enable Acknowledgement to be ready for another reception - return Red_reg; /* Return data which is to be read from device */ + return Red_reg; // return data which is to be read from device } -u8 I2C_Write(I2C_TypeDef* I2Cx, const uint8_t* buf, uint8_t reg, uint32_t nbyte, uint8_t SlaveAddress) +static inline uint8_t I2C_Write(I2C_TypeDef * I2Cx, const uint8_t * buf, uint8_t reg, uint32_t nbyte, uint8_t SlaveAddress) { -/* Enable Error IT (used in all modes: DMA, Polling and Interrupts */ -// I2Cx->CR2 |= I2C_IT_ERR; + // enable Error IT (used in all modes: DMA, Polling and Interrupts + //I2Cx->CR2 |= I2C_IT_ERR; if (nbyte) { while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)); - // Intiate Start Sequence - + // intiate Start Sequence I2C_GenerateSTART(I2Cx, ENABLE); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)); - // Send Address EV5 - + // send Address EV5 I2C_Send7bitAddress(I2Cx, SlaveAddress, I2C_Direction_Transmitter); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); @@ -150,16 +171,17 @@ u8 I2C_Write(I2C_TypeDef* I2Cx, const uint8_t* buf, uint8_t reg, uint32_t nbyte, I2C_SendData(I2Cx, reg); while (!I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF)); - // Write first byte EV8_1 + // write first byte EV8_1 if (nbyte > 16) GUI_DispString(200, 0, (uint8_t *)"large byte write"); I2C_SendData(I2Cx, *buf++); - while (--nbyte) { // wait on BTF + while (--nbyte) + { // wait on BTF while (!I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF)); - I2C_SendData(I2Cx, *buf++); + I2C_SendData(I2Cx, *buf++); } while (!I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF)); @@ -167,12 +189,13 @@ u8 I2C_Write(I2C_TypeDef* I2Cx, const uint8_t* buf, uint8_t reg, uint32_t nbyte, I2C_GenerateSTOP(I2Cx, ENABLE); while (I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF)); } + return 0; } -void EEPROM_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead) +static inline void EEPROM_ReadBuffer(uint8_t * pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead) { - // uint8_t I2C_Read_Byte(I2C_TypeDef* I2Cx, uint8_t reg_addr, uint8_t dev_Addr) + //uint8_t I2C_Read_Byte(I2C_TypeDef * I2Cx, uint8_t reg_addr, uint8_t dev_Addr) *pBuffer = I2C_Read_Byte(I2C1, ReadAddr, AT24CXX_ADDRESS); for (uint16_t i = 0; i < NumByteToRead; i++) @@ -183,74 +206,63 @@ void EEPROM_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRe } } -void EEPROM_FlashRead(u8 *data, u16 len) -{ - EEPROM_ReadBuffer(data, 0, (u16)len); -} - -void EEPROM_FlashWrite(u8 *data, u16 len) +static inline uint8_t I2Cx_WriteMultiple(uint8_t Addr, uint16_t Reg, uint8_t * Buffer, uint16_t Length) { - EEPROM_WriteData(0x0, data, (u16)len); -} - -void i2C_Init_EEPROM() -{ - I2C_GPIO_Config(); - I2C1_init(I2C_SPEED); -} - -u8 I2Cx_WriteMultiple(uint8_t Addr, uint16_t Reg, uint8_t *Buffer, uint16_t Length) -{ - u8 status = EEPROM_OK; + uint8_t status = EEPROM_OK; I2C_Write(I2C1, Buffer, Reg, Length, Addr); - /* Check the communication status */ + + // check the communication status if (status != EEPROM_OK) { - /* Re-Initiaize the I2C Bus */ - // I2Cx_Error(Addr); + // re-Initiaize the I2C Bus + //I2Cx_Error(Addr); } + return status; } -u8 EEPROM_PageWrite(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize) +static uint8_t EEPROM_PageWrite(uint16_t MemAddress, uint8_t * pBuffer, uint32_t BufferSize) { - u8 status = EEPROM_OK; + uint8_t status = EEPROM_OK; status = I2Cx_WriteMultiple((AT24CXX_ADDRESS + ((MemAddress / 256) << 1)), MemAddress, pBuffer, BufferSize); Delay_us(3000); + return status; } -u8 EEPROM_WriteData(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize) +static inline uint8_t EEPROM_WriteData(uint16_t MemAddress, uint8_t * pBuffer, uint32_t BufferSize) { uint8_t NumOfPage = 0; uint8_t NumOfSingle = 0; uint8_t Addr = 0; uint8_t count = 0; - u8 err = EEPROM_OK; + uint8_t err = EEPROM_OK; Addr = MemAddress % AT24CXX_PAGE_SIZE; count = AT24CXX_PAGE_SIZE - Addr; NumOfPage = BufferSize / AT24CXX_PAGE_SIZE; NumOfSingle = BufferSize % AT24CXX_PAGE_SIZE; - /* If WriteAddr is I2C_PageSize aligned */ + // if WriteAddr is I2C_PageSize aligned if (Addr == 0) { - /* If NumByteToWrite < I2C_PageSize */ + // if NumByteToWrite < I2C_PageSize if (NumOfPage == 0) { return (EEPROM_PageWrite(MemAddress, pBuffer, BufferSize)); } - /* If NumByteToWrite > I2C_PageSize */ + // if NumByteToWrite > I2C_PageSiz else { while (NumOfPage--) { err = EEPROM_PageWrite(MemAddress, pBuffer, AT24CXX_PAGE_SIZE); + if (err != EEPROM_OK) return err; + MemAddress += AT24CXX_PAGE_SIZE; pBuffer += AT24CXX_PAGE_SIZE; } @@ -259,31 +271,33 @@ u8 EEPROM_WriteData(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize) return (EEPROM_PageWrite(MemAddress, pBuffer, NumOfSingle)); } } - /* If WriteAddr is not I2C_PageSize aligned */ + // if WriteAddr is not I2C_PageSize aligned else - { /* If NumByteToWrite < I2C_PageSize */ + { // if NumByteToWrite < I2C_PageSize if (BufferSize <= count) - { return (EEPROM_PageWrite(MemAddress, pBuffer, NumOfSingle)); - } else - { /* If NumByteToWrite > I2C_PageSize */ + { // if NumByteToWrite > I2C_PageSize BufferSize -= count; NumOfPage = BufferSize / AT24CXX_PAGE_SIZE; NumOfSingle = BufferSize % AT24CXX_PAGE_SIZE; err = EEPROM_PageWrite(MemAddress, pBuffer, count); + if (err != EEPROM_OK) return err; + MemAddress += count; pBuffer += count; while (NumOfPage--) { err = EEPROM_PageWrite(MemAddress, pBuffer, AT24CXX_PAGE_SIZE); + if (err != EEPROM_OK) return err; + MemAddress += AT24CXX_PAGE_SIZE; pBuffer += AT24CXX_PAGE_SIZE; } @@ -296,4 +310,20 @@ u8 EEPROM_WriteData(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize) return err; } -#endif // defined(I2C_EEPROM) +void i2C_Init_EEPROM(void) +{ + I2C_GPIO_Config(); + I2C1_init(I2C_SPEED); +} + +void EEPROM_FlashRead(uint8_t * data, uint16_t len) +{ + EEPROM_ReadBuffer(data, 0, (uint16_t)len); +} + +void EEPROM_FlashWrite(uint8_t * data, uint16_t len) +{ + EEPROM_WriteData(0x0, data, (uint16_t)len); +} + +#endif // I2C_EEPROM diff --git a/TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.h b/TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.h index 2cba0fe3e1..1311514c68 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/i2c_eeprom.h @@ -1,34 +1,10 @@ #ifndef _I2C_EEPROM__H_ #define _I2C_EEPROM__H_ -#include "includes.h" +#include -#define EEPROM_OK 0 -#define EEPROM_FAIL 1 -#define EEPROM_TIMEOUT 3 - -#define AT24C01 127 // PAGE_SIZE 8 bytes -#define AT24C02 255 // PAGE_SIZE 8 bytes -#define AT24C04 511 // PAGE_SIZE 16 bytes -#define AT24C08 1023 // PAGE_SIZE 16 bytes -#define AT24C16 2047 // PAGE_SIZE 16 bytes -#define AT24C32 4095 -#define AT24C64 8191 -#define AT24C128 16383 -#define AT24C256 32767 - -//#define AT24CXX_TYPE EEPROM_TYPE -#define AT24CXX_ADDRESS EEPROM_ADDRESS -#define AT24CXX_PAGE_SIZE 16 // 8 bytes - -// Timeout for write based on max read/write which is -// AT24CXX_PAGE_SIZE bytes: 8 bits * AT24CXX_PAGE_SIZE / I2C_SPEED -//#define EEPROM_READ_TIMEOUT ((uint32_t)(1000)) -//#define EEPROM_WRITE_TIMEOUT ((uint32_t)(1000)) - -u8 EEPROM_WriteData(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize); -void EEPROM_FlashRead(u8 *data, u16 len); -void EEPROM_FlashWrite(u8 *data, u16 len); -void i2C_Init_EEPROM(); +void i2C_Init_EEPROM(void); +void EEPROM_FlashRead(uint8_t * data, uint16_t len); +void EEPROM_FlashWrite(uint8_t * data, uint16_t len); #endif diff --git a/TFT/src/User/Hal/stm32f2_f4xx/lcd.c b/TFT/src/User/Hal/stm32f2_f4xx/lcd.c index 67b43e344c..e7d1d70128 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/lcd.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/lcd.c @@ -7,24 +7,27 @@ uint16_t LCD_RD_DATA(void) { volatile uint16_t ram; + ram = LCD->LCD_RAM; + return ram; } -void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) +static void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) { LCD->LCD_REG = LCD_Reg; LCD->LCD_RAM = LCD_RegValue; } -uint16_t LCD_ReadReg(uint8_t LCD_Reg) +static uint16_t LCD_ReadReg(uint8_t LCD_Reg) { LCD_WR_REG(LCD_Reg); Delay_us(5); + return LCD_RD_DATA(); } -void LCD_GPIO_Config(void) +static inline void LCD_GPIO_Config(void) { // fsmc 16bit data pins GPIO_InitSet(PD0, MGPIO_MODE_AF_PP, GPIO_AF_FSMC); @@ -44,12 +47,13 @@ void LCD_GPIO_Config(void) GPIO_InitSet(PE14, MGPIO_MODE_AF_PP, GPIO_AF_FSMC); GPIO_InitSet(PE15, MGPIO_MODE_AF_PP, GPIO_AF_FSMC); - /*Configure the control line corresponding to FSMC - * PD4-FSMC_NOE :LCD-RD - * PD5-FSMC_NWE :LCD-WR - * PD7-FSMC_NE1 :LCD-CS - * PE2-FSMC_A23 :LCD-RS LCD-RS data or cmd - */ + /* + * configure the control line corresponding to FSMC + * PD4-FSMC_NOE :LCD-RD + * PD5-FSMC_NWE :LCD-WR + * PD7-FSMC_NE1 :LCD-CS + * PE2-FSMC_A23 :LCD-RS LCD-RS data or cmd + */ GPIO_InitSet(PD4, MGPIO_MODE_AF_PP, GPIO_AF_FSMC); GPIO_InitSet(PD5, MGPIO_MODE_AF_PP, GPIO_AF_FSMC); @@ -61,51 +65,52 @@ void LCD_GPIO_Config(void) GPIO_InitSet(PD7, MGPIO_MODE_AF_PP, GPIO_AF_FSMC); } -void LCD_FSMC_Config(void) +static inline void LCD_FSMC_Config(void) { FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; FSMC_NORSRAMTimingInitTypeDef readWriteTiming,writeTiming; + // FSMC configuration RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); - readWriteTiming.FSMC_AddressSetupTime = 0x01; // Address setup time (ADDSET) is 2 HCLK 1 / 36M = 27ns + readWriteTiming.FSMC_AddressSetupTime = 0x01; // address setup time (ADDSET) is 2 HCLK 1 / 36M = 27ns readWriteTiming.FSMC_AddressHoldTime = 0x00; readWriteTiming.FSMC_DataSetupTime = 0x0f; readWriteTiming.FSMC_BusTurnAroundDuration = 0x00; readWriteTiming.FSMC_CLKDivision = 0x00; readWriteTiming.FSMC_DataLatency = 0x00; - readWriteTiming.FSMC_AccessMode = FSMC_AccessMode_A; // Mode A + readWriteTiming.FSMC_AccessMode = FSMC_AccessMode_A; // mode A - writeTiming.FSMC_AddressSetupTime = 0x00; // Address setup time (ADDSET) is 1 HCLK + writeTiming.FSMC_AddressSetupTime = 0x00; // address setup time (ADDSET) is 1 HCLK writeTiming.FSMC_AddressHoldTime = 0x00; - writeTiming.FSMC_DataSetupTime = TFTLCD_DRIVER_SPEED; // Data save time + writeTiming.FSMC_DataSetupTime = TFTLCD_DRIVER_SPEED; // data save time writeTiming.FSMC_BusTurnAroundDuration = 0x00; writeTiming.FSMC_CLKDivision = 0x00; writeTiming.FSMC_DataLatency = 0x00; - writeTiming.FSMC_AccessMode = FSMC_AccessMode_A; // Mode A + writeTiming.FSMC_AccessMode = FSMC_AccessMode_A; // mode A - FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; // Select the address of the external storage area - FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; // Configure whether the data and address lines are multiplexed + FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; // select the address of the external storage area + FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; // configure whether the data and address lines are multiplexed FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM; - FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR; // Configure the type of external storage - FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; // Set the data width of the FSMC interface + FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR; // configure the type of external storage + FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; // set the data width of the FSMC interface - FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; // Configure access mode - FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; // Configure the polarity of the wait signal - FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; // Configure whether to use non-alignment + FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; // configure access mode + FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; // configure the polarity of the wait signal + FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; // configure whether to use non-alignment FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; - FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; // Configure when to wait for signals - FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; // Configure whether to use wait signals - FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; // Configure whether to allow burst writes + FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; // configure when to wait for signals + FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; // configure whether to use wait signals + FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; // configure whether to allow burst writes - FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; // Configuration write operation enabled - FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; // Configure whether to use extended mode + FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; // configuration write operation enabled + FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable; // configure whether to use extended mode - FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readWriteTiming; // Read timing - FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &writeTiming; // Write timing + FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &readWriteTiming; // read timing + FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &writeTiming; // write timing FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); - // Enable FSMC Bank1_SRAM Bank + // enable FSMC Bank1_SRAM bank FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); } diff --git a/TFT/src/User/Hal/stm32f2_f4xx/lcd.h b/TFT/src/User/Hal/stm32f2_f4xx/lcd.h index e97c5d0040..5daeaf5739 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/lcd.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/lcd.h @@ -2,7 +2,7 @@ #define _LCD_H_ #include -#include "variants.h" // for STM32_HAS_FSMC etc... +#include "variants.h" // for STM32_HAS_FSMC etc. #ifdef STM32_HAS_FSMC @@ -13,12 +13,12 @@ } LCD_TypeDef; #ifdef MKS_TFT35_V1_0 // different LCD base address for MKS_TFT35_V1_0 - #define LCD_BASE ((uint32_t)(0x60000000 | 0x003fffe)) // 1111 1111 1111 1111 1111 1110 + #define LCD_BASE ((uint32_t)(0x60000000 | 0x003fffe)) // 1111 1111 1111 1111 1111 1110 #else - #define LCD_BASE ((uint32_t)(0x60000000 | 0x00FFFFFE)) // 1111 1111 1111 1111 1111 1110 + #define LCD_BASE ((uint32_t)(0x60000000 | 0x00FFFFFE)) // 1111 1111 1111 1111 1111 1110 #endif - #define LCD ((LCD_TypeDef *) LCD_BASE) + #define LCD ((LCD_TypeDef *) LCD_BASE) #define LCD_WR_REG(regval) do{ LCD->LCD_REG = regval; }while(0) #define LCD_WR_DATA(data) do{ LCD->LCD_RAM = data; }while(0) @@ -27,7 +27,7 @@ #error "don't support LCD-GPIO yet" #endif -void LCD_HardwareConfig(void); uint16_t LCD_RD_DATA(void); +void LCD_HardwareConfig(void); #endif diff --git a/TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.c b/TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.c index 6598b1fd7e..9786b48b58 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.c @@ -1,5 +1,5 @@ #include "lcd_dma.h" -#include "variants.h" // for STM32_HAS_FSMC etc... +#include "variants.h" // for STM32_HAS_FSMC etc. #include "lcd.h" #include "spi.h" #include "LCD_Init.h" @@ -7,7 +7,8 @@ #include "delay.h" #ifdef STM32_HAS_FSMC -// Config for SPI Channel + +// config for SPI Channel // SPI1 RX DMA2 Channel3 Steam0/2 // SPI2 RX DMA1 Channel0 Steam3 // SPI3 RX DMA1 Channel0 Steam0/2 @@ -30,36 +31,36 @@ #define W25QXX_SPI_DMA_RCC_AHB RCC_AHB1Periph_DMA1 #define W25QXX_SPI_DMA_STREAM DMA1_Stream0 #define W25QXX_SPI_DMA_CHANNEL 0 - #define W25QXX_SPI_DMA_READING() (DMA1->LISR & (1<<5)) == 0 + #define W25QXX_SPI_DMA_READING() (DMA1->LISR & (1<<5)) == 0 #define W25QXX_SPI_DMA_CLEAR_FLAG() DMA1->LIFCR = 0x3F // bit:0-5 #endif // SPI --> FSMC DMA (LCD_RAM) -// 16bits, SPI_RX to LCD_RAM. +// 16bits, SPI_RX to LCD_RAM void LCD_DMA_Config(void) { - RCC->AHB1ENR |= W25QXX_SPI_DMA_RCC_AHB; // Turn on the DMA clock - Delay_ms(5); // Wait for the DMA clock to stabilize - W25QXX_SPI_DMA_STREAM->PAR = (uint32_t)&W25QXX_SPI_NUM->DR; // The peripheral address is: SPI-> DR - W25QXX_SPI_DMA_STREAM->M0AR = (uint32_t)&LCD->LCD_RAM; // The target address is LCD_RAM + RCC->AHB1ENR |= W25QXX_SPI_DMA_RCC_AHB; // turn on the DMA clock + Delay_ms(5); // wait for the DMA clock to stabilize + W25QXX_SPI_DMA_STREAM->PAR = (uint32_t)&W25QXX_SPI_NUM->DR; // the peripheral address is: SPI-> DR + W25QXX_SPI_DMA_STREAM->M0AR = (uint32_t)&LCD->LCD_RAM; // the target address is LCD_RAM //W25QXX_SPI_DMA_STREAM->M1AR = 0; // (used in case of Double buffer mode) //W25QXX_SPI_DMA_CHANNEL->CMAR = W25QXX_SPI_DMA_STREAM->NDTR = 0; // DMA, the amount of data transferred, temporarily set to 0 W25QXX_SPI_DMA_STREAM->CR = W25QXX_SPI_DMA_CHANNEL << 25; - W25QXX_SPI_DMA_STREAM->CR |= 1<<16; // Priority level: Medium - W25QXX_SPI_DMA_STREAM->CR |= LCD_DATA_16BIT<<13; // Memory data width 16 bits - W25QXX_SPI_DMA_STREAM->CR |= LCD_DATA_16BIT<<11; // Peripheral data width is 16 bits - W25QXX_SPI_DMA_STREAM->CR |= 0<<10; // Memory non-incremental mode - W25QXX_SPI_DMA_STREAM->CR |= 0<<9; // Peripheral address non-incremental mode - W25QXX_SPI_DMA_STREAM->CR |= 0<<6; // Non-memory to memory mode + W25QXX_SPI_DMA_STREAM->CR |= 1<<16; // priority level: Medium + W25QXX_SPI_DMA_STREAM->CR |= LCD_DATA_16BIT<<13; // memory data width 16 bits + W25QXX_SPI_DMA_STREAM->CR |= LCD_DATA_16BIT<<11; // peripheral data width is 16 bits + W25QXX_SPI_DMA_STREAM->CR |= 0<<10; // memory non-incremental mode + W25QXX_SPI_DMA_STREAM->CR |= 0<<9; // peripheral address non-incremental mode + W25QXX_SPI_DMA_STREAM->CR |= 0<<6; // non-memory to memory mode } #define LCD_DMA_MAX_TRANS 65535 // DMA 65535 bytes one frame // start DMA transfer from SPI->DR to FSMC // the max bytes of one frame is LCD_DMA_MAX_TRANS 65535 -void lcd_frame_segment_display(uint16_t size, uint32_t addr) +static inline void lcd_frame_segment_display(uint16_t size, uint32_t addr) { W25QXX_SPI_DMA_STREAM->NDTR = size; @@ -70,7 +71,7 @@ void lcd_frame_segment_display(uint16_t size, uint32_t addr) W25Qxx_SPI_Read_Write_Byte((uint8_t)addr); W25Qxx_SPI_Read_Write_Byte(0XFF); // 8 dummy clock - //set SPI to 16bit DMA rx only mode + // set SPI to 16bit DMA rx only mode W25QXX_SPI_NUM->CR1 &= ~(1<<6); // disable SPI W25QXX_SPI_NUM->CR2 |= 1<<0; // enable SPI rx DMA W25QXX_SPI_NUM->CR1 |= LCD_DATA_16BIT<<11; // 16bit data frame @@ -84,21 +85,22 @@ void lcd_frame_segment_display(uint16_t size, uint32_t addr) W25QXX_SPI_DMA_CLEAR_FLAG(); // clear ISR for rx complete W25Qxx_SPI_CS_Set(1); - SPI_Protocol_Init(W25Qxx_SPI, W25Qxx_SPEED); // Reset SPI clock and config again + SPI_Protocol_Init(W25Qxx_SPI, W25Qxx_SPEED); // reset SPI clock and config again } -void lcd_frame_display(uint16_t sx,uint16_t sy,uint16_t w,uint16_t h, uint32_t addr) +void lcd_frame_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint32_t addr) { - uint32_t cur=0; + uint32_t cur = 0; uint32_t segmentSize; - uint32_t totalSize = w*h*(2-LCD_DATA_16BIT); + uint32_t totalSize = w * h * (2 - LCD_DATA_16BIT); - LCD_SetWindow(sx,sy,sx+w-1,sy+h-1); + LCD_SetWindow(sx, sy, sx + w - 1, sy + h - 1); for (cur = 0; cur < totalSize; cur += LCD_DMA_MAX_TRANS) { - segmentSize = (cur+LCD_DMA_MAX_TRANS)<=totalSize ? LCD_DMA_MAX_TRANS : totalSize-cur; - lcd_frame_segment_display(segmentSize, addr+cur*(LCD_DATA_16BIT + 1)); + segmentSize = (cur + LCD_DMA_MAX_TRANS) <= totalSize ? LCD_DMA_MAX_TRANS : totalSize - cur; + lcd_frame_segment_display(segmentSize, addr + cur * (LCD_DATA_16BIT + 1)); } } -#endif + +#endif // STM32_HAS_FSMC diff --git a/TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.h b/TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.h index 8903255c22..1fbaa9b375 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/lcd_dma.h @@ -1,7 +1,7 @@ #ifndef _LCD_DMA_H_ #define _LCD_DMA_H_ -#include "variants.h" // for uint16_t etc... +#include void LCD_DMA_Config(void); void lcd_frame_display(uint16_t sx, uint16_t sy, uint16_t w, uint16_t h, uint32_t addr); diff --git a/TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.c b/TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.c index 294a51c1ed..eed5b34279 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.c @@ -223,11 +223,13 @@ */ /* Includes ------------------------------------------------------------------*/ -#include "variants.h" -#ifdef SD_SDIO_SUPPORT #include "sdio_sdcard.h" -#include "string.h" + +#ifdef SD_SDIO_SUPPORT + #include "GPIO_Init.h" +#include "string.h" + /** @addtogroup Utilities * @{ */ @@ -3203,7 +3205,9 @@ void SD_SDIO_DMA_IRQHANDLER(void) { SD_ProcessDMAIRQ(); } -#endif + +#endif // SD_SDIO_SUPPORT + /** * @} */ diff --git a/TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.h b/TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.h index bfd17b1f9b..0cf38064a0 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/sdio_sdcard.h @@ -34,6 +34,10 @@ extern "C" { #endif +#include "variants.h" // for SD_SDIO_SUPPORT etc. + +#ifdef SD_SDIO_SUPPORT + /* Includes ------------------------------------------------------------------*/ //#include "stm324xg_eval.h" #include "stm32f4xx.h" @@ -354,6 +358,8 @@ typedef struct /** @defgroup STM324xG_EVAL_SDIO_SD_Exported_Functions * @{ */ +uint8_t SD_CD_Inserted(void); + void SD_DeInit(void); SD_Error SD_Init(void); SDTransferState SD_GetStatus(void); @@ -382,6 +388,8 @@ SD_Error SD_WaitWriteOperation(void); u8 SD_ReadDisk(u8*buf,u32 sector,u8 cnt); //Read SD card, fatfs / usb call u8 SD_WriteDisk(u8*buf,u32 sector,u8 cnt); //Write SD card, fatfs / usb call +#endif // SD_SDIO_SUPPORT + #ifdef __cplusplus } #endif diff --git a/TFT/src/User/Hal/stm32f2_f4xx/spi.c b/TFT/src/User/Hal/stm32f2_f4xx/spi.c index 1f97e1a7cf..b71d2792fc 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/spi.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/spi.c @@ -1,5 +1,5 @@ #include "spi.h" -#include "variants.h" // for SPI1_SCK_PIN etc... +#include "variants.h" // for SPI1_SCK_PIN etc. #include "GPIO_Init.h" // SPI1 default pins config @@ -33,13 +33,13 @@ #define SPI3_MOSI_PIN PB5 #endif -static volatile uint32_t* const rcc_spi_rst[_SPI_CNT] = { +static volatile uint32_t * const rcc_spi_rst[_SPI_CNT] = { &RCC->APB2RSTR, &RCC->APB1RSTR, &RCC->APB1RSTR, }; -static volatile uint32_t* const rcc_spi_en[_SPI_CNT] = { +static volatile uint32_t * const rcc_spi_en[_SPI_CNT] = { &RCC->APB2ENR, &RCC->APB1ENR, &RCC->APB1ENR, @@ -51,7 +51,7 @@ static const uint32_t rcc_spi_bit[_SPI_CNT] = { 0x00008000, // RCC_APB1 bit 15 }; -static SPI_TypeDef* const spi[_SPI_CNT] = { +static SPI_TypeDef * const spi[_SPI_CNT] = { SPI1, // SCK--PA5 MISO--PA6 MOSI--PA7 SPI2, // SCK--PB13 MISO--PB14 MOSI--PB15 SPI3, // SCK--PB3 MISO--PB4 MOSI--PB5 @@ -70,9 +70,9 @@ void SPI_GPIO_Init(uint8_t port) GPIO_InitSet(spi_mosi[port], MGPIO_MODE_AF_PP, SPI_AF_NUM[port]); // MOSI } -void SPI_GPIO_DeInit(uint8_t port) +static inline void SPI_GPIO_DeInit(uint8_t port) { - // Set all of spi pins to input + // set all of spi pins to input GPIO_InitSet(spi_sck[port], MGPIO_MODE_IPN, 0); // SCK GPIO_InitSet(spi_miso[port], MGPIO_MODE_IPN, 0); // MISO GPIO_InitSet(spi_mosi[port], MGPIO_MODE_IPN, 0); // MOSI @@ -83,9 +83,9 @@ void SPI_GPIO_DeInit(uint8_t port) void SPI_Protocol_Init(uint8_t port, uint8_t baudrate) { *rcc_spi_rst[port] |= rcc_spi_bit[port]; - *rcc_spi_rst[port] &= ~rcc_spi_bit[port]; // Reset SPI clock + *rcc_spi_rst[port] &= ~rcc_spi_bit[port]; // reset SPI clock - *rcc_spi_en[port] |= rcc_spi_bit[port]; // Enable SPI clock + *rcc_spi_en[port] |= rcc_spi_bit[port]; // enable SPI clock spi[port]->CR1 = (0<<15) // 0:2-line 1: 1-line | (0<<14) // in bidirectional mode 0:read only 1: read/write @@ -94,9 +94,9 @@ void SPI_Protocol_Init(uint8_t port, uint8_t baudrate) | (0<<11) // 0:8-bit data frame 1:16-bit data frame | (0<<10) // 0:Full duplex 1:Receive-only | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) - | (1<<8) // This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored + | (1<<8) // this bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored | (0<<7) // 0:MSB 1:LSB - | (1<<6) // Enable SPI + | (1<<6) // enable SPI | (baudrate<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 | (1<<2) // 0:Slave 1:Master @@ -113,8 +113,9 @@ void SPI_Config(uint8_t port) void SPI_DeConfig(uint8_t port) { SPI_GPIO_DeInit(port); + *rcc_spi_rst[port] |= rcc_spi_bit[port]; - *rcc_spi_rst[port] &= ~rcc_spi_bit[port]; // Reset SPI clock + *rcc_spi_rst[port] &= ~rcc_spi_bit[port]; // reset SPI clock } uint16_t SPI_Read_Write(uint8_t port, uint16_t d) @@ -122,5 +123,6 @@ uint16_t SPI_Read_Write(uint8_t port, uint16_t d) while ((spi[port]->SR & (1 << 1)) == RESET); // wait for tx empty spi[port]->DR = d; while ((spi[port]->SR & (1 << 0)) == RESET); // wait for rx no empty + return spi[port]->DR; } diff --git a/TFT/src/User/Hal/stm32f2_f4xx/spi.h b/TFT/src/User/Hal/stm32f2_f4xx/spi.h index e6d05cd472..865ee40236 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/spi.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/spi.h @@ -3,15 +3,15 @@ #include -#define _SPI1 0 -#define _SPI2 1 -#define _SPI3 2 -#define _SPI_CNT 3 +#define _SPI1 0 +#define _SPI2 1 +#define _SPI3 2 +#define _SPI_CNT 3 void SPI_GPIO_Init(uint8_t port); +void SPI_Protocol_Init(uint8_t port, uint8_t baudrate); void SPI_Config(uint8_t port); void SPI_DeConfig(uint8_t port); -void SPI_Protocol_Init(uint8_t port, uint8_t baudrate); uint16_t SPI_Read_Write(uint8_t port, uint16_t d); #endif diff --git a/TFT/src/User/Hal/stm32f2_f4xx/spi_slave.c b/TFT/src/User/Hal/stm32f2_f4xx/spi_slave.c index e66ed8cbfc..7b18087f27 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/spi_slave.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/spi_slave.c @@ -1,47 +1,70 @@ #include "spi_slave.h" -#include "includes.h" // for infoSettings, ST7920_EMULATOR etc... +#include "includes.h" // for infoSettings, ST7920_EMULATOR etc. #include "spi.h" #include "GPIO_Init.h" #include "HD44780.h" #if defined(ST7920_EMULATOR) + // TODO: // now support SPI2 and PB12 CS only // more compatibility changes are needed -// Config for SPI Channel +// config for SPI Channel #if ST7920_SPI == _SPI1 - #define ST7920_SPI_NUM SPI1 + #define ST7920_SPI_NUM SPI1 #elif ST7920_SPI == _SPI2 - #define ST7920_SPI_NUM SPI2 + #define ST7920_SPI_NUM SPI2 #elif ST7920_SPI == _SPI3 - #define ST7920_SPI_NUM SPI3 + #define ST7920_SPI_NUM SPI3 #endif -CIRCULAR_QUEUE *spi_queue = NULL; +static CIRCULAR_QUEUE * spi_queue = NULL; + +// external interruption arrangement +static inline void SPI_Slave_CS_Config(void) +{ + EXTI_InitTypeDef EXTI_InitStructure; + NVIC_InitTypeDef NVIC_InitStructure; + + // connect GPIOB12 to the interrupt line + RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // enable SYSCFG clock + SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource12); // PB12 is connected to interrupt line 12 -void SPI_Slave_CS_Config(void); // forward declaration + // set interrupt line 12 bit external falling edge interrupt + EXTI_InitStructure.EXTI_Line = EXTI_Line12; + EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; + EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; + EXTI_InitStructure.EXTI_LineCmd = ENABLE; + EXTI_Init(&EXTI_InitStructure); -void SPI_ReEnable(uint8_t mode) + NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; // external interrupt channel where the key is enabled + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; // preemption priority 2? + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; // sub-priority 1 + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // enable external interrupt channel + NVIC_Init(&NVIC_InitStructure); +} + +static void SPI_ReEnable(uint8_t mode) { - ST7920_SPI_NUM->CR1 = (0<<15) // 0:2-line 1: 1-line - | (0<<14) // in bidirectional mode 0:read only 1: read/write - | (0<<13) // 0:disable CRC 1:enable CRC - | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) - | (0<<11) // 0:8-bit data frame 1:16-bit data frame - | (1<<10) // 0:Full duplex 1:Receive-only - | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) - | (0<<8) // This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored - | (0<<7) // 0:MSB 1:LSB - | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 - // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 - | (0<<2) // 0:Slave 1:Master - | (mode<<1) // CPOL - | (mode<<0); // CPHA - - ST7920_SPI_NUM->CR2 |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE + ST7920_SPI_NUM->CR1 = (0<<15) // 0:2-line 1: 1-line + | (0<<14) // in bidirectional mode 0:read only 1: read/write + | (0<<13) // 0:disable CRC 1:enable CRC + | (0<<12) // 0:Data phase (no CRC phase) 1:Next transfer is CRC (CRC phase) + | (0<<11) // 0:8-bit data frame 1:16-bit data frame + | (1<<10) // 0:Full duplex 1:Receive-only + | (1<<9) // 0:enable NSS 1:disable NSS (Software slave management) + | (0<<8) // this bit has an effect only when the SSM bit is set. The value of this bit is forced onto the NSS pin and the IO value of the NSS pin is ignored + | (0<<7) // 0:MSB 1:LSB + | (7<<3) // bit3-5 000:fPCLK/2 001:fPCLK/4 010:fPCLK/8 011:fPCLK/16 + // 100:fPCLK/32 101:fPCLK/64 110:fPCLK/128 111:fPCLK/256 + | (0<<2) // 0:Slave 1:Master + | (mode<<1) // CPOL + | (mode<<0); // CPHA + + ST7920_SPI_NUM->CR2 |= 1<<6; // RX buffer not empty interrupt enable SPI_I2S_IT_RXNE } -void SPI_Slave(CIRCULAR_QUEUE *queue) +void SPI_Slave(CIRCULAR_QUEUE * queue) { spi_queue = queue; @@ -52,7 +75,7 @@ void SPI_Slave(CIRCULAR_QUEUE *queue) NVIC_InitTypeDef NVIC_InitStructure; SPI_GPIO_Init(ST7920_SPI); - GPIO_InitSet(PB12, MGPIO_MODE_IPU, 0); // CS + GPIO_InitSet(PB12, MGPIO_MODE_IPU, 0); // CS NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; @@ -62,12 +85,10 @@ void SPI_Slave(CIRCULAR_QUEUE *queue) RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE); SPI_Slave_CS_Config(); - SPI_ReEnable(0); // spi mode0 + SPI_ReEnable(0); // spi mode0 if ((GPIOB->IDR & (1<<12)) != 0) - { ST7920_SPI_NUM->CR1 |= (1<<6); - } } void SPI_SlaveDeInit(void) @@ -86,13 +107,7 @@ void SPI_SlaveDeInit(void) spi_queue = NULL; } -void SPI2_IRQHandler(void) -{ - spi_queue->data[spi_queue->index_w] = ST7920_SPI_NUM->DR; - spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; -} - -bool SPI_SlaveGetData(uint8_t *data) +bool SPI_SlaveGetData(uint8_t * data) { bool dataRead = false; @@ -107,33 +122,17 @@ bool SPI_SlaveGetData(uint8_t *data) return dataRead; } -// External interruption arrangement -void SPI_Slave_CS_Config(void) +void SPI2_IRQHandler(void) { - EXTI_InitTypeDef EXTI_InitStructure; - NVIC_InitTypeDef NVIC_InitStructure; - - // Connect GPIOB12 to the interrupt line - RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Enable SYSCFG clock - SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource12); // PB12 is connected to interrupt line 12 - - // Set interrupt line 12 bit external falling edge interrupt - EXTI_InitStructure.EXTI_Line = EXTI_Line12; - EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; - EXTI_InitStructure.EXTI_LineCmd = ENABLE; - EXTI_Init(&EXTI_InitStructure); - - NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; // External interrupt channel where the key is enabled - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; // Preemption priority 2? - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; // Sub-priority 1 - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable external interrupt channel - NVIC_Init(&NVIC_InitStructure); + spi_queue->data[spi_queue->index_w] = ST7920_SPI_NUM->DR; + spi_queue->index_w = (spi_queue->index_w + 1) % CIRCULAR_QUEUE_SIZE; } -#endif + +#endif // ST7920_EMULATOR #ifdef HAS_EMULATOR -// External interruption + +// external interruption void EXTI15_10_IRQHandler(void) { switch (infoSettings.marlin_type) @@ -148,18 +147,19 @@ void EXTI15_10_IRQHandler(void) case LCD12864: if ((GPIOB->IDR & (1<<12)) != 0) { - SPI_ReEnable(!!(GPIOB->IDR & (1<<13))); // Adaptive spi mode0 / mode3 + SPI_ReEnable(!!(GPIOB->IDR & (1<<13))); // adaptive spi mode0 / mode3 ST7920_SPI_NUM->CR1 |= (1<<6); } else { - RCC->APB1RSTR |= 1<<14; // Reset SPI2 - RCC->APB1RSTR &= ~(1<<14); + RCC->APB1RSTR |= 1<<14; + RCC->APB1RSTR &= ~(1<<14); // reset SPI2 } - EXTI->PR = 1<<12; // Clear interrupt status register + EXTI->PR = 1<<12; // clear interrupt status register break; #endif } } -#endif + +#endif // HAS_EMULATOR diff --git a/TFT/src/User/Hal/stm32f2_f4xx/spi_slave.h b/TFT/src/User/Hal/stm32f2_f4xx/spi_slave.h index 58ce091326..c6be7b5586 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/spi_slave.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/spi_slave.h @@ -5,8 +5,8 @@ #include #include "CircularQueue.h" -void SPI_Slave(CIRCULAR_QUEUE *queue); +void SPI_Slave(CIRCULAR_QUEUE * queue); void SPI_SlaveDeInit(void); -bool SPI_SlaveGetData(uint8_t *data); +bool SPI_SlaveGetData(uint8_t * data); #endif diff --git a/TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.c b/TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.c index 2bded9cbd9..35595820ec 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.c @@ -2,8 +2,8 @@ #include "includes.h" // for mcuClocks typedef struct { - TIM_TypeDef* tim; - volatile uint32_t* rcc_src; + TIM_TypeDef * tim; + volatile uint32_t * rcc_src; uint8_t rcc_bit; } TIMER; @@ -28,7 +28,8 @@ void TIM_PWM_SetDutyCycle(uint16_t tim_ch, uint8_t duty) { uint16_t timerIndex = TIMER_GET_TIM(tim_ch); uint16_t channel = TIMER_GET_CH(tim_ch); - const TIMER* timer = &pwmTimer[timerIndex]; + const TIMER * timer = &pwmTimer[timerIndex]; + switch (channel) { case 0: timer->tim->CCR1 = duty; break; @@ -42,29 +43,29 @@ void TIM_PWM_Init(uint16_t tim_ch) { uint16_t timerIndex = TIMER_GET_TIM(tim_ch); uint16_t channel = TIMER_GET_CH(tim_ch); - const TIMER* timer = &pwmTimer[timerIndex]; + const TIMER * timer = &pwmTimer[timerIndex]; uint32_t timerTmpClk = (timer->rcc_src == &RCC->APB1ENR) ? mcuClocks.PCLK1_Timer_Frequency : mcuClocks.PCLK2_Timer_Frequency; - *timer->rcc_src |= (1 << timer->rcc_bit); // Enable timer clock + *timer->rcc_src |= (1 << timer->rcc_bit); // enable timer clock - // Set PWM frequency to 500Hz + // set PWM frequency to 500Hz timer->tim->ARR = 100 - 1; timer->tim->PSC = timerTmpClk / (500 * 100) - 1; switch (channel) { - case 0: timer->tim->CCMR1 |= (6<<4) | (1<<3); break; // CH1 PWM1 mode - case 1: timer->tim->CCMR1 |= (6<<12) | (1<<11); break; // CH2 PWM1 mode - case 2: timer->tim->CCMR2 |= (6<<4) | (1<<3); break; // CH3 PWM1 mode - case 3: timer->tim->CCMR2 |= (6<<12) | (1<<11); break; // CH4 PWM1 mode + case 0: timer->tim->CCMR1 |= (6<<4) | (1<<3); break; // CH1 PWM1 mode + case 1: timer->tim->CCMR1 |= (6<<12) | (1<<11); break; // CH2 PWM1 mode + case 2: timer->tim->CCMR2 |= (6<<4) | (1<<3); break; // CH3 PWM1 mode + case 3: timer->tim->CCMR2 |= (6<<12) | (1<<11); break; // CH4 PWM1 mode } - timer->tim->CCER |= 1 << (4 * channel); - timer->tim->CR1 = (1 << 7) // Auto-reload preload enable - | (1 << 0); // Enbale timer + timer->tim->CCER |= 1 << (4 * channel); // enable channle + timer->tim->CR1 = (1 << 7) // auto-reload preload enable + | (1 << 0); // enbale timer if (timer->tim == TIM1 || timer->tim == TIM8) { // TIM1 & TIM8 advanced timer need config BDTR register for PWM - timer->tim->BDTR |= 1 << 15; // Main output enable + timer->tim->BDTR |= 1 << 15; // main output enable } } diff --git a/TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.h b/TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.h index 8fa08d5535..efb48c2be6 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/timer_pwm.h @@ -96,7 +96,7 @@ #define _TIM15_CH4 (((_TIM15)<<8) + 3) #define TIMER_GET_TIM(n) ((n>>8) & 0xFF) -#define TIMER_GET_CH(n) (n & 0xFF) +#define TIMER_GET_CH(n) (n & 0xFF) void TIM_PWM_SetDutyCycle(uint16_t tim_ch, uint8_t duty); void TIM_PWM_Init(uint16_t tim_ch); diff --git a/TFT/src/User/Hal/stm32f2_f4xx/uart.c b/TFT/src/User/Hal/stm32f2_f4xx/uart.c index b2878583ea..05f7e3afc8 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/uart.c +++ b/TFT/src/User/Hal/stm32f2_f4xx/uart.c @@ -1,5 +1,5 @@ #include "uart.h" -#include "variants.h" // for USART1_TX_PIN etc... +#include "variants.h" // for USART1_TX_PIN etc. #include "GPIO_Init.h" // USART1 default pins config @@ -45,7 +45,7 @@ #define USART6_RX_PIN PG9 #endif -static volatile uint32_t* const rcc_uart_rst[_UART_CNT] = { +static volatile uint32_t * const rcc_uart_rst[_UART_CNT] = { &RCC->APB2RSTR, &RCC->APB1RSTR, &RCC->APB1RSTR, @@ -54,7 +54,7 @@ static volatile uint32_t* const rcc_uart_rst[_UART_CNT] = { &RCC->APB2RSTR, }; -static volatile uint32_t* const rcc_uart_en[_UART_CNT] = { +static volatile uint32_t * const rcc_uart_en[_UART_CNT] = { &RCC->APB2ENR, &RCC->APB1ENR, &RCC->APB1ENR, @@ -72,18 +72,19 @@ static const uint32_t rcc_uart_bit[_UART_CNT] = { 0x00000020, // RCC_APB2 bit 5 }; -static USART_TypeDef* const uart[_UART_CNT] = { - USART1, // TX--PA9 RX--PA10 - USART2, // TX--PA2 RX--PA3 - USART3, // TX--PB10 RX--PB11 - UART4, // TX--PC10 RX--PC11 - UART5, // TX--PC12 RX--PD2 - USART6}; // TX--PG14 RX--PG9 +static USART_TypeDef * const uart[_UART_CNT] = { + USART1, // TX--PA9 RX--PA10 + USART2, // TX--PA2 RX--PA3 + USART3, // TX--PB10 RX--PB11 + UART4, // TX--PC10 RX--PC11 + UART5, // TX--PC12 RX--PD2 + USART6, // TX--PG14 RX--PG9 +}; static const uint16_t uart_tx[_UART_CNT] = {USART1_TX_PIN, USART2_TX_PIN, USART3_TX_PIN, UART4_TX_PIN, UART5_TX_PIN, USART6_TX_PIN}; // TX static const uint16_t uart_rx[_UART_CNT] = {USART1_RX_PIN, USART2_RX_PIN, USART3_RX_PIN, UART4_RX_PIN, UART5_RX_PIN, USART6_RX_PIN}; // RX -void UART_GPIO_Init(uint8_t port) +static inline void UART_GPIO_Init(uint8_t port) { uint8_t UART_AF_NUM[_UART_CNT] = {GPIO_AF_USART1, GPIO_AF_USART2, GPIO_AF_USART3, GPIO_AF_UART4, GPIO_AF_UART5, GPIO_AF_USART6}; @@ -91,18 +92,18 @@ void UART_GPIO_Init(uint8_t port) GPIO_InitSet(uart_rx[port], MGPIO_MODE_AF_PP, UART_AF_NUM[port]); } -void UART_GPIO_DeInit(uint8_t port) +static inline void UART_GPIO_DeInit(uint8_t port) { // set tx/rx to input GPIO_InitSet(uart_tx[port], MGPIO_MODE_IPN, 0); GPIO_InitSet(uart_rx[port], MGPIO_MODE_IPN, 0); } -void UART_Protocol_Init(uint8_t port, uint32_t baud) +static inline void UART_Protocol_Init(uint8_t port, uint32_t baud) { USART_InitTypeDef USART_InitStructure; - *rcc_uart_en[port] |= rcc_uart_bit[port]; // Enable clock + *rcc_uart_en[port] |= rcc_uart_bit[port]; // enable clock USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; @@ -110,12 +111,12 @@ void UART_Protocol_Init(uint8_t port, uint32_t baud) USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; - USART_Init(uart[port],&USART_InitStructure); + USART_Init(uart[port], &USART_InitStructure); USART_Cmd(uart[port],ENABLE); } -void UART_IRQ_Init(uint8_t port, uint16_t usart_it, FunctionalState idle_interrupt) +static inline void UART_IRQ_Init(uint8_t port, uint16_t usart_it, FunctionalState idle_interrupt) { uint32_t IRQ_Channel[_UART_CNT] = {USART1_IRQn, USART2_IRQn, USART3_IRQn, UART4_IRQn, UART5_IRQn, USART6_IRQn}; @@ -134,7 +135,7 @@ void UART_Config(uint8_t port, uint32_t baud, uint16_t usart_it, bool idle_inter { UART_Protocol_Init(port, baud); UART_IRQ_Init(port, usart_it, idle_interrupt ? ENABLE : DISABLE); - UART_GPIO_Init(port); // After all initialization is completed, enable IO, otherwise a 0xFF will be sent automatically after power-on + UART_GPIO_Init(port); // after all initialization is completed, enable IO, otherwise a 0xFF will be sent automatically after power-on } void UART_DeConfig(uint8_t port) @@ -142,20 +143,20 @@ void UART_DeConfig(uint8_t port) UART_GPIO_DeInit(port); *rcc_uart_rst[port] |= rcc_uart_bit[port]; - *rcc_uart_rst[port] &= ~rcc_uart_bit[port]; // Reset clock + *rcc_uart_rst[port] &= ~rcc_uart_bit[port]; // reset clock } void UART_Write(uint8_t port, uint8_t d) { - while ((uart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET); + while ((uart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET); // wait uart[port]->DR = ((uint16_t)d & (uint16_t)0x01FF); } -void UART_Puts(uint8_t port, uint8_t *str) +void UART_Puts(uint8_t port, uint8_t * str) { while (*str) { - while ((uart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET); + while ((uart[port]->SR & USART_FLAG_TC) == (uint16_t)RESET); // wait uart[port]->DR = ((uint16_t)*str++ & (uint16_t)0x01FF); } } diff --git a/TFT/src/User/Hal/stm32f2_f4xx/uart.h b/TFT/src/User/Hal/stm32f2_f4xx/uart.h index 4230750ff6..0d4dc0589e 100644 --- a/TFT/src/User/Hal/stm32f2_f4xx/uart.h +++ b/TFT/src/User/Hal/stm32f2_f4xx/uart.h @@ -4,17 +4,17 @@ #include #include -#define _USART1 0 -#define _USART2 1 -#define _USART3 2 -#define _UART4 3 -#define _UART5 4 -#define _USART6 5 -#define _UART_CNT 6 +#define _USART1 0 // USART0 +#define _USART2 1 // USART1 +#define _USART3 2 // USART2 +#define _UART4 3 // UART3 +#define _UART5 4 // UART4 +#define _USART6 5 // USART5 +#define _UART_CNT 6 void UART_Config(uint8_t port, uint32_t baud, uint16_t usart_it, bool idle_interrupt); void UART_DeConfig(uint8_t port); -void UART_Puts(uint8_t port, uint8_t *str); void UART_Write(uint8_t port, uint8_t d); +void UART_Puts(uint8_t port, uint8_t * str); #endif diff --git a/TFT/src/User/Hal/sw_spi.c b/TFT/src/User/Hal/sw_spi.c index b69b35fa73..d7e9064a01 100644 --- a/TFT/src/User/Hal/sw_spi.c +++ b/TFT/src/User/Hal/sw_spi.c @@ -2,7 +2,7 @@ #include "GPIO_Init.h" #include "delay.h" -void SW_SPI_Config(_SW_SPI *sw_spi, _SPI_MODE mode, uint8_t dataSize, +void SW_SPI_Config(_SW_SPI * sw_spi, _SPI_MODE mode, uint8_t dataSize, uint16_t cs, uint16_t sck, uint16_t miso, @@ -15,18 +15,18 @@ void SW_SPI_Config(_SW_SPI *sw_spi, _SPI_MODE mode, uint8_t dataSize, sw_spi->mode = mode; sw_spi->dataSize = dataSize; - GPIO_InitSet(sw_spi->cs, MGPIO_MODE_OUT_PP, 0); //CS - GPIO_InitSet(sw_spi->sck, MGPIO_MODE_OUT_PP, 0); //SCK - GPIO_InitSet(sw_spi->miso, MGPIO_MODE_IPN, 0); //MISO - GPIO_InitSet(sw_spi->mosi, MGPIO_MODE_OUT_PP, 0); //MOSI + GPIO_InitSet(sw_spi->cs, MGPIO_MODE_OUT_PP, 0); // CS + GPIO_InitSet(sw_spi->sck, MGPIO_MODE_OUT_PP, 0); // SCK + GPIO_InitSet(sw_spi->miso, MGPIO_MODE_IPN, 0); // MISO + GPIO_InitSet(sw_spi->mosi, MGPIO_MODE_OUT_PP, 0); // MOSI } -#define SCK_HIGH() GPIO_SetLevel(sw_spi->sck, 1); Delay_us(2) // The speed of xpt2046 should not be too fast -#define SCK_LOW() GPIO_SetLevel(sw_spi->sck, 0); Delay_us(2) // It is better to be below 200KHz. +#define SCK_HIGH() GPIO_SetLevel(sw_spi->sck, 1); Delay_us(2) // the speed of xpt2046 should not be too fast +#define SCK_LOW() GPIO_SetLevel(sw_spi->sck, 0); Delay_us(2) // it is better to be below 200KHz #define MOSI_WRITE(n) GPIO_SetLevel(sw_spi->mosi, n) #define MISO_READ() GPIO_GetLevel(sw_spi->miso) -uint16_t SW_SPI_Read_Write(_SW_SPI *sw_spi, uint16_t d) +uint16_t SW_SPI_Read_Write(_SW_SPI * sw_spi, uint16_t d) { uint8_t data_bits = sw_spi->dataSize; uint8_t i = 0; @@ -86,7 +86,7 @@ uint16_t SW_SPI_Read_Write(_SW_SPI *sw_spi, uint16_t d) return rcv; } -void SW_SPI_CS_Set(_SW_SPI *sw_spi, uint8_t level) +void SW_SPI_CS_Set(_SW_SPI * sw_spi, uint8_t level) { GPIO_SetLevel(sw_spi->cs, level); } diff --git a/TFT/src/User/Hal/sw_spi.h b/TFT/src/User/Hal/sw_spi.h index 8b55056cdb..6ac4cad3a8 100644 --- a/TFT/src/User/Hal/sw_spi.h +++ b/TFT/src/User/Hal/sw_spi.h @@ -5,8 +5,7 @@ extern "C" { #endif -#include -#include "variants.h" // for uint8_t etc... +#include // for uint8_t etc. typedef enum { @@ -26,14 +25,14 @@ typedef struct uint8_t dataSize; } _SW_SPI; -void SW_SPI_Config(_SW_SPI *sw_spi, _SPI_MODE mode, uint8_t dataSize, +void SW_SPI_Config(_SW_SPI * sw_spi, _SPI_MODE mode, uint8_t dataSize, uint16_t cs, uint16_t sck, uint16_t miso, uint16_t mosi); -uint16_t SW_SPI_Read_Write(_SW_SPI *sw_spi, uint16_t d); -void SW_SPI_CS_Set(_SW_SPI *sw_spi, uint8_t level); +uint16_t SW_SPI_Read_Write(_SW_SPI * sw_spi, uint16_t d); +void SW_SPI_CS_Set(_SW_SPI * sw_spi, uint8_t level); #ifdef __cplusplus } diff --git a/TFT/src/User/Hal/w25qxx.c b/TFT/src/User/Hal/w25qxx.c index 0d10c500eb..d55e64d08a 100644 --- a/TFT/src/User/Hal/w25qxx.c +++ b/TFT/src/User/Hal/w25qxx.c @@ -1,28 +1,32 @@ #include "w25qxx.h" +#include "variants.h" // for W25Qxx_SPI etc. #include "GPIO_Init.h" #include "spi.h" -/*************************** W25Qxx SPI Interface ported by the underlying pattern ***************************/ +#define KB(x) (x * 1024l) +#define MB(x) (x * 1024l * 1024l) + +/**************************** W25Qxx SPI Interface ported by the underlying pattern ****************************/ //#define W25Qxx_SPI _SPI3 //#define W25Qxx_SPEED 0 //#define W25Qxx_CS_PIN PA4 -const uint8_t cap_ID[14] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x43, 0x4B, 0x00, 0x01}; -const uint32_t flash_size[14] = {KB(64), KB(128), KB(256), KB(512), MB(1), MB(2), MB(4), MB(8), MB(16), MB(32), MB(8), MB(8), KB(256), KB(512)}; +static const uint8_t cap_ID[14] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x43, 0x4B, 0x00, 0x01}; +static const uint32_t flash_size[14] = {KB(64), KB(128), KB(256), KB(512), MB(1), MB(2), MB(4), MB(8), MB(16), MB(32), MB(8), MB(8), KB(256), KB(512)}; -//Chip Select -void W25Qxx_SPI_CS_Set(uint8_t level) +// read and write functions +uint8_t W25Qxx_SPI_Read_Write_Byte(uint8_t data) { - GPIO_SetLevel(W25Qxx_CS_PIN, level); + return SPI_Read_Write(W25Qxx_SPI, data); } -//Read and write functions -uint8_t W25Qxx_SPI_Read_Write_Byte(uint8_t data) +// Chip Select +void W25Qxx_SPI_CS_Set(uint8_t level) { - return SPI_Read_Write(W25Qxx_SPI,data); + GPIO_SetLevel(W25Qxx_CS_PIN, level); } -//initialization +// initialization void W25Qxx_Init(void) { GPIO_InitSet(W25Qxx_CS_PIN, MGPIO_MODE_OUT_PP, 0); @@ -30,9 +34,10 @@ void W25Qxx_Init(void) SPI_Protocol_Init(W25Qxx_SPI, W25Qxx_SPEED); W25Qxx_SPI_CS_Set(1); } -/*************************************************************************************/ -// Write enable +/****************************************************************************************************************/ + +// write enable void W25Qxx_WriteEnable(void) { W25Qxx_SPI_CS_Set(0); @@ -40,23 +45,25 @@ void W25Qxx_WriteEnable(void) W25Qxx_SPI_CS_Set(1); } -//Waiting for W25Qxx to be idle +// waiting for W25Qxx to be idle void W25Qxx_WaitForWriteEnd(void) { uint8_t flashstatus = 0; W25Qxx_SPI_CS_Set(0); W25Qxx_SPI_Read_Write_Byte(CMD_READ_REGISTER1); + do { flashstatus = W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); } while ((flashstatus & 0x01) == SET); + W25Qxx_SPI_CS_Set(1); } -//Write by page -void W25Qxx_WritePage(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) +// write by page +void W25Qxx_WritePage(uint8_t * pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) { W25Qxx_WriteEnable(); W25Qxx_SPI_CS_Set(0); @@ -76,17 +83,17 @@ void W25Qxx_WritePage(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWr W25Qxx_WaitForWriteEnd(); } -//Send buffer data to FLASH -void W25Qxx_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) +// send buffer data to FLASH +void W25Qxx_WriteBuffer(uint8_t * pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) { uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0; - Addr = WriteAddr % W25QXX_SPI_PAGESIZE; //Represents which address in a page starts to write data - count = W25QXX_SPI_PAGESIZE - Addr; //How much data can be written on this page - NumOfPage = NumByteToWrite / W25QXX_SPI_PAGESIZE; //Represents how many pages can be written to the length of the data to be stored - NumOfSingle = NumByteToWrite % W25QXX_SPI_PAGESIZE; //Represents the length of data that can be written in addition to the entire page of data + Addr = WriteAddr % W25QXX_SPI_PAGESIZE; // represents which address in a page starts to write data + count = W25QXX_SPI_PAGESIZE - Addr; // how much data can be written on this page + NumOfPage = NumByteToWrite / W25QXX_SPI_PAGESIZE; // represents how many pages can be written to the length of the data to be stored + NumOfSingle = NumByteToWrite % W25QXX_SPI_PAGESIZE; // represents the length of data that can be written in addition to the entire page of data - if (Addr == 0) /*!< WriteAddr is sFLASH_PAGESIZE aligned */ + if (Addr == 0) /*!< WriteAddr is sFLASH_PAGESIZE aligned */ { if (NumOfPage == 0) /*!< NumByteToWrite < sFLASH_PAGESIZE */ { @@ -97,13 +104,14 @@ void W25Qxx_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteTo while (NumOfPage--) { W25Qxx_WritePage(pBuffer, WriteAddr, W25QXX_SPI_PAGESIZE); - WriteAddr += W25QXX_SPI_PAGESIZE; + WriteAddr += W25QXX_SPI_PAGESIZE; pBuffer += W25QXX_SPI_PAGESIZE; } + W25Qxx_WritePage(pBuffer, WriteAddr, NumOfSingle); } } - else /*!< WriteAddr is not sFLASH_PAGESIZE aligned */ + else /*!< WriteAddr is not sFLASH_PAGESIZE aligned */ { if (NumOfPage == 0) /*!< NumByteToWrite < sFLASH_PAGESIZE */ { @@ -112,7 +120,7 @@ void W25Qxx_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteTo temp = NumOfSingle - count; W25Qxx_WritePage(pBuffer, WriteAddr, count); - WriteAddr += count; + WriteAddr += count; pBuffer += count; W25Qxx_WritePage(pBuffer, WriteAddr, temp); @@ -125,30 +133,28 @@ void W25Qxx_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteTo else /*!< NumByteToWrite > sFLASH_PAGESIZE */ { NumByteToWrite -= count; - NumOfPage = NumByteToWrite / W25QXX_SPI_PAGESIZE; + NumOfPage = NumByteToWrite / W25QXX_SPI_PAGESIZE; NumOfSingle = NumByteToWrite % W25QXX_SPI_PAGESIZE; W25Qxx_WritePage(pBuffer, WriteAddr, count); - WriteAddr += count; + WriteAddr += count; pBuffer += count; while (NumOfPage--) { W25Qxx_WritePage(pBuffer, WriteAddr, W25QXX_SPI_PAGESIZE); - WriteAddr += W25QXX_SPI_PAGESIZE; + WriteAddr += W25QXX_SPI_PAGESIZE; pBuffer += W25QXX_SPI_PAGESIZE; } if (NumOfSingle != 0) - { W25Qxx_WritePage(pBuffer, WriteAddr, NumOfSingle); - } } } } -//Reading data from flash -void W25Qxx_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead) +// reading data from flash +void W25Qxx_ReadBuffer(uint8_t * pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead) { W25Qxx_SPI_CS_Set(0); @@ -167,23 +173,7 @@ void W25Qxx_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRe W25Qxx_SPI_CS_Set(1); } -//Read ID -uint32_t W25Qxx_ReadID(void) -{ - uint32_t Temp = 0; - - W25Qxx_SPI_CS_Set(0); - W25Qxx_SPI_Read_Write_Byte(CMD_READ_ID); - - Temp = W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); - Temp = (Temp<<8) | W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); - Temp = (Temp<<8) | W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); - - W25Qxx_SPI_CS_Set(1); - return Temp; -} - -//Sector erase +// sector erase void W25Qxx_EraseSector(uint32_t SectorAddr) { W25Qxx_WriteEnable(); @@ -198,7 +188,7 @@ void W25Qxx_EraseSector(uint32_t SectorAddr) W25Qxx_WaitForWriteEnd(); } -//Block erase +// block erase void W25Qxx_EraseBlock(uint32_t BlockAddr) { W25Qxx_WriteEnable(); @@ -213,7 +203,7 @@ void W25Qxx_EraseBlock(uint32_t BlockAddr) W25Qxx_WaitForWriteEnd(); } -//Full-chip erase +// full-chip erase void W25Qxx_EraseBulk(void) { W25Qxx_WriteEnable(); @@ -225,24 +215,42 @@ void W25Qxx_EraseBulk(void) W25Qxx_WaitForWriteEnd(); } -//Read flash Size/Capacity +// read ID +uint32_t W25Qxx_ReadID(void) +{ + uint32_t Temp = 0; + + W25Qxx_SPI_CS_Set(0); + W25Qxx_SPI_Read_Write_Byte(CMD_READ_ID); + + Temp = W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); + Temp = (Temp<<8) | W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); + Temp = (Temp<<8) | W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); + + W25Qxx_SPI_CS_Set(1); + + return Temp; +} + +// read flash Size / Capacity uint32_t W25Qxx_ReadCapacity(void) { W25Qxx_SPI_CS_Set(0); W25Qxx_SPI_Read_Write_Byte(CMD_READ_ID); uint8_t cap; + W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); cap = W25Qxx_SPI_Read_Write_Byte(W25QXX_DUMMY_BYTE); W25Qxx_SPI_CS_Set(1); + for (uint8_t i = 0; i < sizeof(cap_ID); i++) { if (cap == cap_ID[i]) - { return flash_size[i]; - } } + return 0; } diff --git a/TFT/src/User/Hal/w25qxx.h b/TFT/src/User/Hal/w25qxx.h index 7bd0023b4e..b5c51d4012 100644 --- a/TFT/src/User/Hal/w25qxx.h +++ b/TFT/src/User/Hal/w25qxx.h @@ -5,8 +5,7 @@ extern "C" { #endif -#include -#include "variants.h" // for uint8_t etc... +#include // for uint8_t etc. #define CMD_WRITE_ENABLE 0x06 #define CMD_WRITE_DISABLE 0x04 @@ -23,18 +22,15 @@ extern "C" { #define W25QXX_DUMMY_BYTE 0xFF #define W25QXX_SPI_PAGESIZE 0x100 -#define KB(x) (x * 1024l) -#define MB(x) (x * 1024l * 1024l) - uint8_t W25Qxx_SPI_Read_Write_Byte(uint8_t data); void W25Qxx_SPI_CS_Set(uint8_t level); void W25Qxx_Init(void); void W25Qxx_WriteEnable(void); void W25Qxx_WaitForWriteEnd(void); -void W25Qxx_WritePage(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); -void W25Qxx_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); -void W25Qxx_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead); +void W25Qxx_WritePage(uint8_t * pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); +void W25Qxx_WriteBuffer(uint8_t * pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); +void W25Qxx_ReadBuffer(uint8_t * pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead); void W25Qxx_EraseSector(uint32_t SectorAddr); void W25Qxx_EraseBlock(uint32_t BlockAddr); void W25Qxx_EraseBulk(void); diff --git a/TFT/src/User/Hal/xpt2046.c b/TFT/src/User/Hal/xpt2046.c index 58239d90f1..9d32904ef3 100644 --- a/TFT/src/User/Hal/xpt2046.c +++ b/TFT/src/User/Hal/xpt2046.c @@ -1,15 +1,17 @@ #include "xpt2046.h" +#include "includes.h" // for HW_SPI_TOUCH, XPT2046_SPI (added for MKS_TFT35_V1_0 support) #include "GPIO_Init.h" #include "sw_spi.h" -#include "includes.h" // for HW_SPI_TOUCH, XPT2046_SPI (added for MKS_TFT35_V1_0 support) -/***************************************** XPT2046 SPI mode low-level porting interface********************************************/ +/**************************** XPT2046 SPI mode low-level porting interface ****************************/ -//XPT2046 SPI related-using analog SPI //XPT2046 SPIRelated-Using Analog SPI -_SW_SPI xpt2046; +// XPT2046 SPI related-using analog SPI +#ifndef HW_SPI_TOUCH + static _SW_SPI xpt2046; +#endif -//Chip Select //Chip Select -void XPT2046_CS_Set(uint8_t level) +// Chip Select +static void XPT2046_CS_Set(uint8_t level) { #ifdef HW_SPI_TOUCH // added for MKS_TFT35_V1_0 support GPIO_SetLevel(XPT2046_CS, level); @@ -19,8 +21,8 @@ void XPT2046_CS_Set(uint8_t level) #endif } -//Read and write functions //Read and write functions -uint8_t XPT2046_ReadWriteByte(uint8_t TxData) +// read and write functions +static uint8_t XPT2046_ReadWriteByte(uint8_t TxData) { #ifdef HW_SPI_TOUCH // added for MKS_TFT35_V1_0 support return SPI_Read_Write(XPT2046_SPI, TxData); @@ -29,37 +31,38 @@ uint8_t XPT2046_ReadWriteByte(uint8_t TxData) #endif } -//XPT2046 SPI and pen interrupt initialization //XPT2046 SPI and pen interrupt initialization +// XPT2046 SPI and pen interrupt initialization void XPT2046_Init(void) { - //PA15-TPEN - GPIO_InitSet(XPT2046_TPEN, MGPIO_MODE_IPN, 0); + GPIO_InitSet(XPT2046_TPEN, MGPIO_MODE_IPN, 0); // PA15-TPEN #ifdef HW_SPI_TOUCH // added for MKS_TFT35_V1_0 support GPIO_InitSet(XPT2046_CS, MGPIO_MODE_OUT_PP, 0); #else SW_SPI_Config(&xpt2046, _SPI_MODE3, 8, // 8bit - XPT2046_CS, //CS - XPT2046_SCK, //SCK - XPT2046_MISO, //MISO - XPT2046_MOSI //MOSI + XPT2046_CS, // CS + XPT2046_SCK, // SCK + XPT2046_MISO, // MISO + XPT2046_MOSI // MOSI ); #endif XPT2046_CS_Set(1); } -//Reading pen interrupt //Reading pen interrupt +// reading pen interrupt uint8_t XPT2046_Read_Pen(void) { return GPIO_GetLevel(XPT2046_TPEN); } -/******************************************************************************************************************/ -//Read the converted AD value of XPT2046 //Read the converted AD value of XPT2046 -uint16_t XPT2046_Read_AD(uint8_t CMD) +/****************************************************************************************************************/ + +// read the converted AD value of XPT2046 +static uint16_t XPT2046_Read_AD(uint8_t CMD) { uint16_t ADNum; + XPT2046_CS_Set(0); #ifdef MKS_TFT35_V1_0 // added for MKS_TFT35_V1_0 support @@ -77,27 +80,30 @@ uint16_t XPT2046_Read_AD(uint8_t CMD) SPI_Protocol_Init(W25Qxx_SPI, W25Qxx_SPEED); // switch back to high speed SPI #endif - ADNum >>= 4; //XPT2046 data is only 12 bits, discarding the lower four bits + ADNum >>= 4; // XPT2046 data is only 12 bits, discarding the lower four bits XPT2046_CS_Set(1); + return ADNum; } -#define READ_TIMES 5 //Read times //Read times -#define LOST_VAL 1 //Drop value //Drop value +#define READ_TIMES 5 // read times +#define LOST_VAL 1 // drop value -uint16_t XPT2046_Average_AD(uint8_t CMD) +static uint16_t XPT2046_Average_AD(uint8_t CMD) { uint16_t i, j; uint16_t buf[READ_TIMES]; uint16_t sum = 0; uint16_t temp; + for (i = 0; i < READ_TIMES; i++) buf[i] = XPT2046_Read_AD(CMD); - for (i = 0; i < READ_TIMES - 1; i++) //Sort // Sort + + for (i = 0; i < READ_TIMES - 1; i++) // sort { for (j = i + 1; j < READ_TIMES; j++) { - if (buf[i] > buf[j]) //Ascending + if (buf[i] > buf[j]) // ascending { temp = buf[i]; buf[i] = buf[j]; @@ -107,22 +113,23 @@ uint16_t XPT2046_Average_AD(uint8_t CMD) } for (i = LOST_VAL; i < READ_TIMES - LOST_VAL; i++) sum += buf[i]; + temp = sum / (READ_TIMES - 2 * LOST_VAL); + return temp; } -#define ERR_RANGE 50 //Tolerance scope +#define ERR_RANGE 50 // tolerance scope uint16_t XPT2046_Repeated_Compare_AD(uint8_t CMD) { uint16_t ad1, ad2; + ad1 = XPT2046_Average_AD(CMD); ad2 = XPT2046_Average_AD(CMD); - if ((ad2 <= ad1 && ad1 < ad2 + ERR_RANGE) - || (ad1 <= ad2 && ad2 < ad1 + ERR_RANGE)) //The error before and after is less than ERR_RANGE //Two errors before and after ERR_RANGE - { + if ((ad2 <= ad1 && ad1 < ad2 + ERR_RANGE) || (ad1 <= ad2 && ad2 < ad1 + ERR_RANGE)) // the error before and after is less than ERR_RANGE return (ad1 + ad2) / 2; - } - else return 0; + else + return 0; } diff --git a/TFT/src/User/Hal/xpt2046.h b/TFT/src/User/Hal/xpt2046.h index 69f518a921..8fb541d3c3 100644 --- a/TFT/src/User/Hal/xpt2046.h +++ b/TFT/src/User/Hal/xpt2046.h @@ -5,8 +5,7 @@ extern "C" { #endif -#include -#include "variants.h" // for uint8_t etc... +#include // for uint8_t etc. void XPT2046_Init(void); uint8_t XPT2046_Read_Pen(void); diff --git a/TFT/src/User/Menu/ABL.c b/TFT/src/User/Menu/ABL.c index cb7faa9482..fcfd3c078e 100644 --- a/TFT/src/User/Menu/ABL.c +++ b/TFT/src/User/Menu/ABL.c @@ -84,7 +84,7 @@ void ablStart(void) storeCmd("M118 P0 ABL Completed\n"); } -void ublSaveloadConfirm(void) +static void ublSaveloadConfirm(void) { if (!ublIsSaving) storeCmd("G29 L%d\n", ublSlot); @@ -92,7 +92,7 @@ void ublSaveloadConfirm(void) ublSlotSaved = storeCmd("G29 S%d\n", ublSlot); } -void menuUBLSaveLoad(void) +static void menuUBLSaveLoad(void) { MENUITEMS UBLSaveLoadItems = { // title diff --git a/TFT/src/User/Menu/BLTouch.c b/TFT/src/User/Menu/BLTouch.c index 12f5f2df44..08a70d44b7 100644 --- a/TFT/src/User/Menu/BLTouch.c +++ b/TFT/src/User/Menu/BLTouch.c @@ -42,6 +42,7 @@ void menuBLTouch(void) while (MENU_IS(menuBLTouch)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/Babystep.c b/TFT/src/User/Menu/Babystep.c index 383aba9a30..1750a0786b 100644 --- a/TFT/src/User/Menu/Babystep.c +++ b/TFT/src/User/Menu/Babystep.c @@ -3,7 +3,7 @@ static uint8_t moveLenSteps_index = 0; -void babyReDraw(float babystep, float z_offset, bool force_z_offset, bool drawHeader) +static void babyReDraw(float babystep, float z_offset, bool force_z_offset, bool drawHeader) { if (drawHeader) { @@ -27,7 +27,7 @@ void babyReDraw(float babystep, float z_offset, bool force_z_offset, bool drawHe setFontSize(FONT_SIZE_LARGE); sprintf(tempstr, "% 6.2f", babystep); - GUI_DispStringRight(point_bs.x, point_bs.y, (uint8_t*) tempstr); + GUI_DispStringRight(point_bs.x, point_bs.y, (uint8_t *) tempstr); if (infoMachineSettings.firmwareType != FW_REPRAPFW) { @@ -38,7 +38,7 @@ void babyReDraw(float babystep, float z_offset, bool force_z_offset, bool drawHe else GUI_SetColor(infoSettings.font_color); - GUI_DispStringRight(point_of.x, point_of.y, (uint8_t*) tempstr); + GUI_DispStringRight(point_of.x, point_of.y, (uint8_t *) tempstr); } GUI_SetColor(infoSettings.font_color); // restore default font color @@ -46,15 +46,16 @@ void babyReDraw(float babystep, float z_offset, bool force_z_offset, bool drawHe } // Set Z offset value for MBL bl type -float babyMblOffsetSetValue(float value) +static float babyMblOffsetSetValue(float value) { mustStoreCmd("G29 S4 Z%.2f\n", value); mustStoreCmd("G29 S0\n"); // needed by babyMblOffsetGetValue() to retrieve the new value + return value; } // Get current Z offset value for MBL bl type -float babyMblOffsetGetValue(void) +static float babyMblOffsetGetValue(void) { return getParameter(P_MBL_OFFSET, 0); } diff --git a/TFT/src/User/Menu/BedLeveling.c b/TFT/src/User/Menu/BedLeveling.c index e6b830c7fb..738460703f 100644 --- a/TFT/src/User/Menu/BedLeveling.c +++ b/TFT/src/User/Menu/BedLeveling.c @@ -1,7 +1,7 @@ #include "BedLeveling.h" #include "includes.h" -void blUpdateState(MENUITEMS * menu, const uint8_t bedLevelState) +static void blUpdateState(MENUITEMS * menu, const uint8_t bedLevelState) { if (bedLevelState == ENABLED) { @@ -16,12 +16,12 @@ void blUpdateState(MENUITEMS * menu, const uint8_t bedLevelState) } #if DELTA_PROBE_TYPE == 2 // if Delta printer with removable probe - void deltaMeshEditor(void) + static void deltaMeshEditor(void) { OPEN_MENU(menuMeshEditor); } - void deltaZOffset(void) + static void deltaZOffset(void) { storeCmd("M851\n"); zOffsetSetMenu(true); // use Probe Offset menu @@ -92,6 +92,7 @@ void menuBedLeveling(void) while (MENU_IS(menuBedLeveling)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/BedLevelingLayer2.c b/TFT/src/User/Menu/BedLevelingLayer2.c index f2fd6c99a9..f47e446f85 100644 --- a/TFT/src/User/Menu/BedLevelingLayer2.c +++ b/TFT/src/User/Menu/BedLevelingLayer2.c @@ -73,6 +73,7 @@ void menuBedLevelingLayer2(void) while (MENU_IS(menuBedLevelingLayer2)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/CaseLight.c b/TFT/src/User/Menu/CaseLight.c index 587c9006b3..54eb3de666 100644 --- a/TFT/src/User/Menu/CaseLight.c +++ b/TFT/src/User/Menu/CaseLight.c @@ -10,7 +10,7 @@ static void updateCaseLightIcon(MENUITEMS * curmenu, const bool state) curmenu->items[KEY_ICON_5].label.index = state ? LABEL_ON : LABEL_OFF; } -void caseLightPercentReDraw(void) +static void caseLightPercentReDraw(void) { char tempstr[20]; diff --git a/TFT/src/User/Menu/ConnectionSettings.c b/TFT/src/User/Menu/ConnectionSettings.c index bdce62d07e..c1071f5613 100644 --- a/TFT/src/User/Menu/ConnectionSettings.c +++ b/TFT/src/User/Menu/ConnectionSettings.c @@ -1,9 +1,9 @@ #include "ConnectionSettings.h" #include "includes.h" -SERIAL_PORT_INDEX portIndex = 0; // index on serialPort array +static SERIAL_PORT_INDEX portIndex = 0; // index on serialPort array -void updateListeningMode(MENUITEMS * menu) +static void updateListeningMode(MENUITEMS * menu) { menu->items[4].label.index = (GET_BIT(infoSettings.general_settings, INDEX_LISTENING_MODE) == 1) ? LABEL_OFF : LABEL_ON; @@ -11,7 +11,7 @@ void updateListeningMode(MENUITEMS * menu) } // disconnect (free uart), wait for a key press and finally connect again (set uart pins to input) -void refreshConnection(void) +static void refreshConnection(void) { GUI_Clear(infoSettings.bg_color); GUI_DispStringInRect(20, 0, LCD_WIDTH - 20, LCD_HEIGHT, textSelect(LABEL_DISCONNECT_INFO)); @@ -38,7 +38,7 @@ void refreshConnection(void) Serial_Init(ALL_PORTS); } -void menuBaudrate(void) +static void menuBaudrate(void) { LABEL title = {LABEL_BAUDRATE}; uint8_t minIndex = portIndex == PORT_1 ? 1 : 0; // if primary serial port, set minIndex to 1 (value OFF is skipped) @@ -92,7 +92,7 @@ void menuBaudrate(void) saveSettings(); // save settings } -void menuSerialPorts(void) +static void menuSerialPorts(void) { LABEL title = {LABEL_SERIAL_PORTS}; LISTITEM totalItems[SERIAL_PORT_COUNT]; @@ -149,6 +149,7 @@ void menuConnectionSettings(void) while (MENU_IS(menuConnectionSettings)) { curIndex = menuKeyGetValue(); + switch (curIndex) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/FeatureSettings.c b/TFT/src/User/Menu/FeatureSettings.c index 95f0d18882..f01ec08681 100644 --- a/TFT/src/User/Menu/FeatureSettings.c +++ b/TFT/src/User/Menu/FeatureSettings.c @@ -1,25 +1,6 @@ #include "FeatureSettings.h" #include "includes.h" -static uint16_t fe_cur_page = 0; - -// parameter values - -#define ITEM_TOGGLE_AUTO_NUM 3 -const LABEL itemToggleAuto[ITEM_TOGGLE_AUTO_NUM] = -{ - LABEL_OFF, - LABEL_ON, - LABEL_AUTO -}; - -#define ITEM_TOGGLE_SMART_NUM 2 -const LABEL itemToggleSmart[ITEM_TOGGLE_SMART_NUM] = -{ - LABEL_ON, - LABEL_SMART -}; - // add key number index of the items typedef enum { @@ -53,7 +34,26 @@ typedef enum SKEY_COUNT // keep this always at the end } SKEY_LIST; -void resetSettings(void) +// parameter values + +#define ITEM_TOGGLE_AUTO_NUM 3 +static const LABEL itemToggleAuto[ITEM_TOGGLE_AUTO_NUM] = +{ + LABEL_OFF, + LABEL_ON, + LABEL_AUTO +}; + +#define ITEM_TOGGLE_SMART_NUM 2 +static const LABEL itemToggleSmart[ITEM_TOGGLE_SMART_NUM] = +{ + LABEL_ON, + LABEL_SMART +}; + +static uint16_t fe_cur_page = 0; + +static void resetSettings(void) { initSettings(); storePara(); @@ -131,10 +131,10 @@ static inline void updateFeatureSettings(uint8_t item_index) default: return; } -} // updateFeatureSettings +} // updateFeatureSettings // load values on page change and reload -void loadFeatureSettings(LISTITEM * item, uint16_t item_index, uint8_t itemPos) +static void loadFeatureSettings(LISTITEM * item, uint16_t item_index, uint8_t itemPos) { if (item_index < SKEY_COUNT) { @@ -209,7 +209,7 @@ void loadFeatureSettings(LISTITEM * item, uint16_t item_index, uint8_t itemPos) break; } } -} // loadFeatureSettings +} // loadFeatureSettings void menuFeatureSettings(void) { diff --git a/TFT/src/User/Menu/Home.c b/TFT/src/User/Menu/Home.c index 01ebff5807..e0e79e0627 100644 --- a/TFT/src/User/Menu/Home.c +++ b/TFT/src/User/Menu/Home.c @@ -1,7 +1,7 @@ #include "Home.h" #include "includes.h" -const MENUITEMS homeItems = { +static const MENUITEMS homeItems = { // title LABEL_HOME, // icon label @@ -26,13 +26,14 @@ void menuHome(void) while (MENU_IS(menuHome)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: storeCmd("G28\n"); break; case KEY_ICON_1: storeCmd("G28 X\n"); break; case KEY_ICON_2: storeCmd("G28 Y\n"); break; case KEY_ICON_3: storeCmd("G28 Z\n"); break; - case KEY_ICON_7: CLOSE_MENU(); break; + case KEY_ICON_7: CLOSE_MENU(); break; default: break; } diff --git a/TFT/src/User/Menu/LEDColor.c b/TFT/src/User/Menu/LEDColor.c index 55bea976f9..e7989ceecf 100644 --- a/TFT/src/User/Menu/LEDColor.c +++ b/TFT/src/User/Menu/LEDColor.c @@ -40,7 +40,7 @@ typedef enum #define CTRL_HEIGHT ROW_HEIGHT // key button rectangles -const GUI_RECT ledKeyRect[KEY_NUM] = { +static const GUI_RECT ledKeyRect[KEY_NUM] = { #ifdef KEYBOARD_ON_LEFT // control bar {0 * CTRL_WIDTH, 4 * CTRL_HEIGHT, 1 * CTRL_WIDTH, 5 * CTRL_HEIGHT}, // PREV @@ -98,7 +98,7 @@ const GUI_RECT ledKeyRect[KEY_NUM] = { #endif }; -const GUI_RECT ledColorRect = { +static const GUI_RECT ledColorRect = { #ifdef KEYBOARD_ON_LEFT 3 * KB_WIDTH, 0 * KB_HEIGHT, 4 * KB_WIDTH, 1 * KB_HEIGHT #else @@ -106,7 +106,7 @@ const GUI_RECT ledColorRect = { #endif }; -const GUI_RECT ledPageRect = { +static const GUI_RECT ledPageRect = { #ifdef KEYBOARD_ON_LEFT 2 * CTRL_WIDTH, 4 * CTRL_HEIGHT, 3 * CTRL_WIDTH, 5 * KB_HEIGHT #else @@ -115,22 +115,22 @@ const GUI_RECT ledPageRect = { }; // area rectangles -const GUI_RECT ledAreaRect[2] = { +static const GUI_RECT ledAreaRect[2] = { {0, 0, LCD_WIDTH, LCD_HEIGHT - CTRL_HEIGHT}, // keyboard area {0, 4 * CTRL_HEIGHT, LCD_WIDTH, LCD_HEIGHT} // control bar area }; -const char *const ledKeyString[2] = { +static const char * const ledKeyString[2] = { "<", // PREV ">", // NEXT }; -const char * const ledString[LED_COLOR_COMPONENT_COUNT] = {"R", "G", "B", "W", "P", "I"}; +static const char * const ledString[LED_COLOR_COMPONENT_COUNT] = {"R", "G", "B", "W", "P", "I"}; -uint8_t ledPage = 0; -uint8_t ledIndex = 0; +static uint8_t ledPage = 0; +static uint8_t ledIndex = 0; -uint8_t ledGetComponentIndex(uint8_t index) +static uint8_t ledGetComponentIndex(uint8_t index) { return ledPage * PAGE_ITEMS + index; } @@ -147,24 +147,24 @@ static inline uint8_t ledEditComponentValue(uint8_t index) return ledColor[realIndex] = editIntValue(LED_MIN_VALUE, LED_MAX_VALUE, ledColor[realIndex], ledColor[realIndex]); } -uint8_t ledUpdateComponentValue(uint8_t index, int8_t unit) +static uint8_t ledUpdateComponentValue(uint8_t index, int8_t unit) { uint8_t realIndex = ledGetComponentIndex(index); return ledColor[realIndex] = NOBEYOND(LED_MIN_VALUE, ledColor[realIndex] + unit, LED_MAX_VALUE); } -uint8_t ledGetControlIndex(uint8_t keyNum) +static uint8_t ledGetControlIndex(uint8_t keyNum) { return (keyNum - (LED_KEY_OK + 1)) / 4; } -uint8_t ledGetControlSubIndex(uint8_t keyNum) +static uint8_t ledGetControlSubIndex(uint8_t keyNum) { return (keyNum - (LED_KEY_OK + 1)) % 4; } -uint16_t ledGetComponentRGBColor(uint8_t component, uint8_t index) +static uint16_t ledGetComponentRGBColor(uint8_t component, uint8_t index) { LED_COLOR led = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // component RGB color in RGB 565 16 bit format @@ -182,7 +182,7 @@ static inline void ledDrawPageNumber(void) drawStandardValue(&ledPageRect, VALUE_STRING, &tempstr, FONT_SIZE_LARGE, LC_CTRL_FONT_COLOR, LC_CTRL_BG_COLOR, 1, true); } -void ledDrawControl(uint8_t index, bool isFocused, bool drawFocus, bool drawAll) +static void ledDrawControl(uint8_t index, bool isFocused, bool drawFocus, bool drawAll) { GUI_RECT rect, rect2; uint16_t bgColorFocus; @@ -227,7 +227,7 @@ void ledDrawControl(uint8_t index, bool isFocused, bool drawFocus, bool drawAll) } } -void ledDrawButton(uint8_t index, uint8_t isPressed) +static void ledDrawButton(uint8_t index, uint8_t isPressed) { if (index >= KEY_NUM) return; @@ -248,7 +248,7 @@ void ledDrawButton(uint8_t index, uint8_t isPressed) } } -void ledDrawKeyboard(void) +static void ledDrawKeyboard(void) { // draw horizontal button borders GUI_SetColor(LC_KB_BORDER_COLOR); @@ -292,7 +292,7 @@ void ledDrawKeyboard(void) GUI_RestoreColorDefault(); } -void ledDrawMenu(void) +static void ledDrawMenu(void) { setMenu(MENU_TYPE_FULLSCREEN, NULL, COUNT(ledKeyRect), ledKeyRect, ledDrawButton, &ledDrawMenu); @@ -315,7 +315,7 @@ void ledDrawMenu(void) ledDrawKeyboard(); } -void menuLEDColorCustom(void) +static void menuLEDColorCustom(void) { LED_KEY_VALUES key_num = LED_KEY_IDLE; LED_COLOR origLedColor; @@ -333,6 +333,7 @@ void menuLEDColorCustom(void) while (MENU_IS(menuLEDColorCustom)) { key_num = menuKeyGetValue(); + switch (key_num) { // previous page @@ -489,6 +490,7 @@ void menuLEDColor(void) while (MENU_IS(menuLEDColor)) { key_num = menuKeyGetValue(); + switch (key_num) { // red diff --git a/TFT/src/User/Menu/LevelCorner.c b/TFT/src/User/Menu/LevelCorner.c index f0f4071d66..e5a4516fa8 100644 --- a/TFT/src/User/Menu/LevelCorner.c +++ b/TFT/src/User/Menu/LevelCorner.c @@ -2,14 +2,14 @@ #include "includes.h" // correlation between KEY position and measure point index -const uint8_t valIconIndex[LEVELING_POINT_COUNT - 1] = {KEY_ICON_4, KEY_ICON_5, KEY_ICON_1, KEY_ICON_0}; +static const uint8_t valIconIndex[LEVELING_POINT_COUNT - 1] = {KEY_ICON_4, KEY_ICON_5, KEY_ICON_1, KEY_ICON_0}; // buffer current Z value measured in Level Corner = {position 1, position 2, position 3, position 4} -float levelCornerPosition[LEVELING_POINT_COUNT - 1] = {0}; +static float levelCornerPosition[LEVELING_POINT_COUNT - 1] = {0}; -int16_t origLevelEdge = -1; +static int16_t origLevelEdge = -1; -uint8_t getLevelEdgeMin(void) +static uint8_t getLevelEdgeMin(void) { // min edge limit for the probe with probe offset set in Mainboard_AckHandler.c int16_t maxXedge = getParameter(P_PROBE_OFFSET, AXIS_INDEX_X) + getParameter(P_HOME_OFFSET, AXIS_INDEX_X); @@ -21,18 +21,18 @@ uint8_t getLevelEdgeMin(void) return MAX(maxXedge, maxYedge) + 1; } -uint8_t getLevelEdgeDefault(void) +static uint8_t getLevelEdgeDefault(void) { return MAX(origLevelEdge, getLevelEdgeMin()); } -void setLevelEdgeMin(void) +static void setLevelEdgeMin(void) { infoSettings.level_edge = getLevelEdgeMin(); } // draw values under icons -void refreshValue(MENUITEMS * levelItems, uint8_t index) +static void refreshValue(MENUITEMS * levelItems, uint8_t index) { sprintf((char *)levelItems->items[valIconIndex[index]].label.address, "%.4f", levelCornerPosition[index]); menuDrawIconText(&levelItems->items[valIconIndex[index]], valIconIndex[index]); @@ -75,6 +75,7 @@ void menuLevelCorner(void) while (MENU_IS(menuLevelCorner)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/Leveling.c b/TFT/src/User/Menu/Leveling.c index b949d4279a..9dbae83aae 100644 --- a/TFT/src/User/Menu/Leveling.c +++ b/TFT/src/User/Menu/Leveling.c @@ -37,6 +37,7 @@ void menuManualLeveling(void) while (MENU_IS(menuManualLeveling)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/LoadUnload.c b/TFT/src/User/Menu/LoadUnload.c index d184422a49..745bfef53d 100644 --- a/TFT/src/User/Menu/LoadUnload.c +++ b/TFT/src/User/Menu/LoadUnload.c @@ -10,7 +10,7 @@ typedef enum UNLOAD_STARTED, } CMD_TYPE; -const MENUITEMS loadUnloadItems = { +static const MENUITEMS loadUnloadItems = { // title LABEL_LOAD_UNLOAD, // icon label @@ -27,7 +27,7 @@ const MENUITEMS loadUnloadItems = { }; static uint8_t tool_index = NOZZLE0; -CMD_TYPE lastCmd = NONE; +static CMD_TYPE lastCmd = NONE; void menuLoadUnload(void) { diff --git a/TFT/src/User/Menu/MBL.c b/TFT/src/User/Menu/MBL.c index 47a518e948..5a100d734c 100644 --- a/TFT/src/User/Menu/MBL.c +++ b/TFT/src/User/Menu/MBL.c @@ -2,8 +2,8 @@ #include "includes.h" static uint8_t curUnit_index = 0; -uint8_t mblPoint = 0; -bool mblRunning = false; +static uint8_t mblPoint = 0; +static bool mblRunning = false; // Start MBL static inline void mblStart(void) @@ -81,7 +81,7 @@ void mblUpdateStatus(bool succeeded) } // Show an error notification -void mblNotifyError(bool isStarted) +static void mblNotifyError(bool isStarted) { LABELCHAR(tempMsg, LABEL_MBL); @@ -90,7 +90,7 @@ void mblNotifyError(bool isStarted) addToast(DIALOG_TYPE_ERROR, tempMsg); } -void mblDraw(COORDINATE *val) +static void mblDraw(COORDINATE * val) { char tempstr[24], tempstr2[24], tempstr3[24]; diff --git a/TFT/src/User/Menu/MPC.c b/TFT/src/User/Menu/MPC.c index 39f74154d1..9cb9cd2d22 100644 --- a/TFT/src/User/Menu/MPC.c +++ b/TFT/src/User/Menu/MPC.c @@ -4,7 +4,7 @@ #define DEF_HEATER_POWER 40 #define DEF_FIL_HEAT_CAPACITY 0.0046f #define MPC_PARAM_ITEMS_COUNT 3 -#define MPC_METHOD_COUNT 3 +#define MPC_METHOD_COUNT 3 typedef struct { @@ -13,19 +13,19 @@ typedef struct enum {AUTOMATIC = 0, DIFFERENTIAL, ASYMPTOMATIC} method; } MPC_Parameter; -static MPC_Parameter mpcParameter[MAX_HOTEND_COUNT] = {0}; - -static struct +typedef struct { MPC_STATUS status; MPC_RESULT result; -} mpcTuning = {DONE, NO_RESULT}; +} MPC_Tuning; +static MPC_Parameter mpcParameter[MAX_HOTEND_COUNT] = {0}; +static MPC_Tuning mpcTuning = {DONE, NO_RESULT}; static uint8_t curTool_index = NOZZLE0; -char * mpcMethod_label[] = {"Auto", "Diff.", "Asym."}; +static char * mpcMethod_label[] = {"Auto", "Diff.", "Asym."}; // 1 title, ITEM_PER_PAGE items (icon + label) -const MENUITEMS mpcItems = { +static const MENUITEMS mpcItems = { // title LABEL_MPC_TITLE, // icon label @@ -41,7 +41,7 @@ const MENUITEMS mpcItems = { } }; -void mpcDisplayValues(void) +static void mpcDisplayValues(void) { char tmpStr[15]; MPC_Parameter * parameter = &mpcParameter[curTool_index]; @@ -60,7 +60,7 @@ void mpcDisplayValues(void) GUI_DispString(exhibitRect.x0, exhibitRect.y0 + 4 * BYTE_HEIGHT, (uint8_t *)tmpStr); } -void menuSetMpcParam(void) +static void menuSetMpcParam(void) { LABEL title = {LABEL_PARAMETER_SETTINGS}; uint16_t curIndex = KEY_IDLE; @@ -140,7 +140,7 @@ void menuSetMpcParam(void) } } -void mpcStart(void) +static void mpcStart(void) { if (heatSetTool(curTool_index)) { @@ -194,6 +194,7 @@ void menuMPC(void) if (mpcTuning.status == DONE) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_4: diff --git a/TFT/src/User/Menu/MachineSettings.c b/TFT/src/User/Menu/MachineSettings.c index c2fbc04d4d..b4c14c1a02 100644 --- a/TFT/src/User/Menu/MachineSettings.c +++ b/TFT/src/User/Menu/MachineSettings.c @@ -17,7 +17,7 @@ void menuCustom(void) { customItems[i].icon = CHARICON_CODE; customItems[i].itemType = LIST_LABEL; - customItems[i].titlelabel.address = (uint8_t*)customcodes.name[i]; + customItems[i].titlelabel.address = (uint8_t *)customcodes.name[i]; } listViewCreate(title, customItems, customcodes.count, NULL, true, NULL, NULL); @@ -28,7 +28,7 @@ void menuCustom(void) #ifdef QUICK_EEPROM_BUTTON -void menuEepromSettings(void) +static void menuEepromSettings(void) { MENUITEMS eepromSettingsItems = { // title @@ -53,6 +53,7 @@ void menuEepromSettings(void) while (MENU_IS(menuEepromSettings)) { curIndex = menuKeyGetValue(); + switch (curIndex) { case KEY_ICON_0: @@ -132,6 +133,7 @@ void menuMachineSettings(void) while (MENU_IS(menuMachineSettings)) { curIndex = menuKeyGetValue(); + switch (curIndex) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/MachineSettings.h b/TFT/src/User/Menu/MachineSettings.h index 7bab8c3ef2..28f0b7360f 100644 --- a/TFT/src/User/Menu/MachineSettings.h +++ b/TFT/src/User/Menu/MachineSettings.h @@ -5,8 +5,8 @@ extern "C" { #endif -void menuMachineSettings(void); void menuCustom(void); +void menuMachineSettings(void); #ifdef __cplusplus } diff --git a/TFT/src/User/Menu/MainPage.c b/TFT/src/User/Menu/MainPage.c index 189d2074eb..e7a07c3e0c 100644 --- a/TFT/src/User/Menu/MainPage.c +++ b/TFT/src/User/Menu/MainPage.c @@ -40,6 +40,7 @@ void menuMain(void) while (MENU_IS(menuMain)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/MeshEditor.c b/TFT/src/User/Menu/MeshEditor.c index af0b973ace..3f17bbf3e0 100644 --- a/TFT/src/User/Menu/MeshEditor.c +++ b/TFT/src/User/Menu/MeshEditor.c @@ -159,9 +159,9 @@ enum #endif #endif -const GUI_RECT meshGridRect = {MESH_GRID_X0, MESH_GRID_Y0, MESH_GRID_X0 + MESH_GRID_WIDTH, MESH_GRID_Y0 + MESH_GRID_HEIGHT}; +static const GUI_RECT meshGridRect = {MESH_GRID_X0, MESH_GRID_Y0, MESH_GRID_X0 + MESH_GRID_WIDTH, MESH_GRID_Y0 + MESH_GRID_HEIGHT}; -const GUI_RECT meshInfoRect[ME_INFO_NUM] = { +static const GUI_RECT meshInfoRect[ME_INFO_NUM] = { #ifdef KEYBOARD_ON_LEFT {MESH_INFO_X0 + 1 * MESH_INFO_WIDTH, MESH_INFO_Y0 + 0 * MESH_INFO_HEIGHT, MESH_INFO_X0 + 2 * MESH_INFO_WIDTH, MESH_INFO_Y0 + 1 * MESH_INFO_HEIGHT}, // min value {MESH_INFO_X0 + 2 * MESH_INFO_WIDTH, MESH_INFO_Y0 + 0 * MESH_INFO_HEIGHT, MESH_INFO_X0 + 3 * MESH_INFO_WIDTH, MESH_INFO_Y0 + 1 * MESH_INFO_HEIGHT}, // max value @@ -180,7 +180,7 @@ const GUI_RECT meshInfoRect[ME_INFO_NUM] = { #endif }; -const GUI_RECT meshKeyRect[ME_KEY_NUM] = { +static const GUI_RECT meshKeyRect[ME_KEY_NUM] = { #ifdef PORTRAIT_MODE {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // SAVE {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // OK @@ -219,7 +219,7 @@ const GUI_RECT meshKeyRect[ME_KEY_NUM] = { #endif }; -const GUI_RECT meshAreaRect[ME_AREA_NUM] = { +static const GUI_RECT meshAreaRect[ME_AREA_NUM] = { {MESH_GRID_X0, MESH_GRID_Y0, MESH_GRID_X0 + MESH_GRID_WIDTH, MESH_GRID_Y0 + MESH_GRID_HEIGHT}, // grid area {MESH_INFO_X0, MESH_INFO_Y0, MESH_INFO_X0 + MESH_GRID_WIDTH, MESH_GRID_Y0}, // info area {MESH_KEY_X0, MESH_KEY_Y0, MESH_KEY_X0 + (LCD_WIDTH - MESH_GRID_WIDTH), LCD_HEIGHT}, // keyboard area @@ -228,7 +228,7 @@ const GUI_RECT meshAreaRect[ME_AREA_NUM] = { #endif }; -const char * const meshKeyString[ME_KEY_NUM] = { +static const char * const meshKeyString[ME_KEY_NUM] = { "\u08A7", // SAVE "\u0894", // OK "\u08A5", // RESET @@ -240,7 +240,7 @@ const char * const meshKeyString[ME_KEY_NUM] = { "\u02C5", // DOWN }; -const MESH_DATA_FORMAT meshDataFormat[] = { +static const MESH_DATA_FORMAT meshDataFormat[] = { // columns to skip, rows to skip, rows inverted, bed leveling data type { 1, 4, true, "Mesh Bed Level data:"}, // MBL { 0, 2, false, "Bed Topography Report for CSV:"}, // UBL @@ -248,7 +248,7 @@ const MESH_DATA_FORMAT meshDataFormat[] = { { 0, 1, true, "Bed Level Correction Matrix:"}, // ABL Linear or 3-Point }; -const char * meshErrorMsg[] = {"Invalid mesh"}; // list of possible error responses to "M420 V1 T1" command +static const char * meshErrorMsg[] = {"Invalid mesh"}; // list of possible error responses to "M420 V1 T1" command static MESH_DATA * meshData = NULL; @@ -288,7 +288,7 @@ static inline void meshAllocData(void) } } -void meshDeallocData(void) +static void meshDeallocData(void) { if (meshData != NULL) { @@ -346,6 +346,7 @@ static inline bool processKnownDataFormat(const char * dataRow) } strcpy(meshData->saveTitle, title); + return true; } } @@ -353,7 +354,7 @@ static inline bool processKnownDataFormat(const char * dataRow) return false; } -void meshSaveCallback(void) +static void meshSaveCallback(void) { if (infoMachineSettings.leveling == BL_UBL) menuUBLSave(); @@ -400,7 +401,7 @@ static inline void meshUpdateIndex(const MESH_KEY_VALUES key_num) meshData->index = meshData->row * meshData->colsNum + meshData->col; } -uint16_t meshGetJ(void) +static uint16_t meshGetJ(void) { // J index (independent by data format) to be used by G42 (mesh tuner menu) and M421 (meshSetValue() function). // Bed's top left point -> J = max row index @@ -408,7 +409,7 @@ uint16_t meshGetJ(void) return (meshData->rowsNum - 1) - meshData->row; } -bool meshSetValue(const float value) +static bool meshSetValue(const float value) { if (meshData->curData[meshData->index] != value) // if mesh value is changed { @@ -441,12 +442,12 @@ static inline void meshUpdateValueMinMax(void) meshData->valueDelta = meshData->valueMax - meshData->valueMin; } -uint16_t mapRGBdata(const uint16_t * rgbStart, const int16_t * rgbDelta, const float * valueDiff, const float * valueDelta) +static uint16_t mapRGBdata(const uint16_t * rgbStart, const int16_t * rgbDelta, const float * valueDiff, const float * valueDelta) { return *rgbStart + (*valueDiff * *rgbDelta) / *valueDelta; } -uint16_t meshGetRGBColor(const float value) +static uint16_t meshGetRGBColor(const float value) { if (meshData->valueDelta == 0) return (meshData->rStart << 11) | (meshData->gStart << 5) | (meshData->bStart); @@ -459,7 +460,7 @@ uint16_t meshGetRGBColor(const float value) return (r << 11) | (g << 5) | (b); } -void meshDrawGridCell(const uint16_t index, const bool clearBgColor) +static void meshDrawGridCell(const uint16_t index, const bool clearBgColor) { float value = meshData->curData[index]; uint16_t col = index % meshData->colsNum; @@ -496,7 +497,7 @@ void meshDrawGridCell(const uint16_t index, const bool clearBgColor) GUI_SetColor(MESH_FONT_COLOR); } -void meshDrawGrid(void) +static void meshDrawGrid(void) { meshUpdateValueMinMax(); @@ -508,7 +509,7 @@ void meshDrawGrid(void) } } -void meshDrawInfo(const bool drawMinMax) +static void meshDrawInfo(const bool drawMinMax) { if (drawMinMax) { @@ -571,7 +572,7 @@ static inline void meshDrawKeyboard(void) GUI_RestoreColorDefault(); } -void meshDrawMenu(void) +static void meshDrawMenu(void) { setMenu(MENU_TYPE_FULLSCREEN, NULL, COUNT(meshKeyRect), meshKeyRect, meshDrawButton, &meshDrawMenu); @@ -605,7 +606,7 @@ void meshDrawMenu(void) meshDrawInfo(true); } -void meshSave(void) +static void meshSave(void) { if (infoMachineSettings.EEPROM == 1) popupDialog(DIALOG_TYPE_QUESTION, (uint8_t *) meshData->saveTitle, LABEL_EEPROM_SAVE_INFO, LABEL_CONFIRM, LABEL_CANCEL, meshSaveCallback, NULL, NULL); @@ -637,7 +638,7 @@ bool meshIsWaitingData(void) return true; } -uint16_t meshParseDataRow(char * dataRow, float * dataGrid, const uint16_t maxCount) +static uint16_t meshParseDataRow(char * dataRow, float * dataGrid, const uint16_t maxCount) { if (meshData->parsedRows < meshData->rowsToSkip) return 0; @@ -796,6 +797,7 @@ void menuMeshEditor(void) if (meshData->status == ME_DATA_FAILED) { meshDeallocData(); + return; } diff --git a/TFT/src/User/Menu/MeshEditor.h b/TFT/src/User/Menu/MeshEditor.h index 886bc5110f..daf2cfe24e 100644 --- a/TFT/src/User/Menu/MeshEditor.h +++ b/TFT/src/User/Menu/MeshEditor.h @@ -9,7 +9,7 @@ extern "C" { // called by parseAck() bool meshIsWaitingData(void); -void meshUpdateData(char *dataRow); +void meshUpdateData(char * dataRow); void menuMeshEditor(void); diff --git a/TFT/src/User/Menu/MeshTuner.c b/TFT/src/User/Menu/MeshTuner.c index 8b194e91ea..edcf6bf89c 100644 --- a/TFT/src/User/Menu/MeshTuner.c +++ b/TFT/src/User/Menu/MeshTuner.c @@ -26,7 +26,7 @@ static inline void meshResetPoint(void) // probeHeightDisable(); // restore original software endstops state and ABL state } -void meshDraw(uint16_t col, uint16_t row, COORDINATE *val) +static void meshDraw(uint16_t col, uint16_t row, COORDINATE * val) { char tempstr[24], tempstr2[24], tempstr3[24]; diff --git a/TFT/src/User/Menu/MeshValid.c b/TFT/src/User/Menu/MeshValid.c index 937840c3c2..a7b264b7f8 100644 --- a/TFT/src/User/Menu/MeshValid.c +++ b/TFT/src/User/Menu/MeshValid.c @@ -1,7 +1,7 @@ #include "MeshValid.h" #include "includes.h" -const MENUITEMS meshValidItems = { +static const MENUITEMS meshValidItems = { // title LABEL_MESH_VALID, // icon label @@ -22,7 +22,7 @@ void menuMeshValid(void) KEY_VALUES key_num; PREHEAT_STORE preheatStore; - W25Qxx_ReadBuffer((uint8_t*)&preheatStore, PREHEAT_STORE_ADDR, sizeof(PREHEAT_STORE)); + W25Qxx_ReadBuffer((uint8_t *)&preheatStore, PREHEAT_STORE_ADDR, sizeof(PREHEAT_STORE)); menuDrawPage(&meshValidItems); for (int i = 0; i < PREHEAT_COUNT; i++) @@ -33,6 +33,7 @@ void menuMeshValid(void) while (MENU_IS(menuMeshValid)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: // MESHVALID PLA diff --git a/TFT/src/User/Menu/More.c b/TFT/src/User/Menu/More.c index a62363f917..6705ce6c14 100644 --- a/TFT/src/User/Menu/More.c +++ b/TFT/src/User/Menu/More.c @@ -1,7 +1,7 @@ #include "More.h" #include "includes.h" -const MENUITEMS moreItems = { +static const MENUITEMS moreItems = { // title LABEL_MORE, // icon label @@ -21,13 +21,13 @@ const MENUITEMS moreItems = { } }; -void isPauseExtrude(void) +static void isPauseExtrude(void) { if (pausePrint(true, PAUSE_NORMAL)) REPLACE_MENU(menuExtrude); } -void isPauseLoadUnload(void) +static void isPauseLoadUnload(void) { if (pausePrint(true, PAUSE_NORMAL)) REPLACE_MENU(menuLoadUnload); @@ -42,6 +42,7 @@ void menuMore(void) while (MENU_IS(menuMore)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/Move.c b/TFT/src/User/Menu/Move.c index 61d00a143e..9b2328faf2 100644 --- a/TFT/src/User/Menu/Move.c +++ b/TFT/src/User/Menu/Move.c @@ -18,11 +18,12 @@ #define OFFSET 1 #endif -const char *const xyzMoveCmd[] = {X_MOVE_GCODE, Y_MOVE_GCODE, Z_MOVE_GCODE}; +static const char * const xyzMoveCmd[] = {X_MOVE_GCODE, Y_MOVE_GCODE, Z_MOVE_GCODE}; + static uint8_t item_moveLen_index = 1; -AXIS nowAxis = X_AXIS; +static AXIS nowAxis = X_AXIS; -void storeMoveCmd(const AXIS xyz, const float amount) +static void storeMoveCmd(const AXIS xyz, const float amount) { // if invert is true, use 'amount' multiplied by -1 storeCmd(xyzMoveCmd[xyz], GET_BIT(infoSettings.inverted_axis, xyz) ? -amount : amount, @@ -31,7 +32,7 @@ void storeMoveCmd(const AXIS xyz, const float amount) nowAxis = xyz; // update now axis } -void drawXYZ(void) +static void drawXYZ(void) { if (getReminderStatus() != SYS_STATUS_IDLE || toastRunning()) return; diff --git a/TFT/src/User/Menu/NotificationMenu.c b/TFT/src/User/Menu/NotificationMenu.c index d1c9549ec6..63bcf2bd54 100644 --- a/TFT/src/User/Menu/NotificationMenu.c +++ b/TFT/src/User/Menu/NotificationMenu.c @@ -1,7 +1,7 @@ #include "NotificationMenu.h" #include "includes.h" -void loadNotificationItems(void) +static void loadNotificationItems(void) { LISTITEMS * itemlist = getCurListItems(); @@ -67,6 +67,7 @@ void menuNotification(void) while (MENU_IS(menuNotification)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/ParameterSettings.c b/TFT/src/User/Menu/ParameterSettings.c index dacb23e4da..0e2abd581b 100644 --- a/TFT/src/User/Menu/ParameterSettings.c +++ b/TFT/src/User/Menu/ParameterSettings.c @@ -1,11 +1,7 @@ #include "ParameterSettings.h" #include "includes.h" -static uint16_t psCurPage = 0; -static uint8_t curParameter = 0; -bool parametersChanged = false; - -const LABEL parameterTypes[PARAMETERS_COUNT] = { +static const LABEL parameterTypes[PARAMETERS_COUNT] = { LABEL_STEPS_SETTING, LABEL_FILAMENT_SETTING, LABEL_MAXACCELERATION, @@ -35,15 +31,19 @@ const LABEL parameterTypes[PARAMETERS_COUNT] = { LABEL_MBL_OFFSET, }; -const LISTITEM eepromItems[P_SETTINGS_COUNT] = { +static const LISTITEM eepromItems[P_SETTINGS_COUNT] = { // icon item type item title item value text(only for custom value) {CHARICON_SAVE, LIST_LABEL, LABEL_SETTINGS_SAVE, LABEL_NULL}, {CHARICON_UNDO, LIST_LABEL, LABEL_SETTINGS_RESTORE, LABEL_NULL}, {CHARICON_RESET, LIST_LABEL, LABEL_SETTINGS_RESET, LABEL_NULL}, }; +static uint16_t psCurPage = 0; +static uint8_t curParameter = 0; +static bool parametersChanged = false; + // Load elements for selected parameter -void loadElements(LISTITEM * parameterMainItem, uint16_t index, uint8_t itemPos) +static void loadElements(LISTITEM * parameterMainItem, uint16_t index, uint8_t itemPos) { uint8_t enabledElementCount = getEnabledElementCount(curParameter); @@ -155,7 +155,7 @@ void loadElements(LISTITEM * parameterMainItem, uint16_t index, uint8_t itemPos) } // Show menu for selected parameter type -void menuShowParameter(void) +static void menuShowParameter(void) { uint8_t enabledElementCount = getEnabledElementCount(curParameter); float oldval[enabledElementCount]; @@ -223,7 +223,7 @@ void menuShowParameter(void) } // Load main parameter list page -void loadParameters(LISTITEM * parameterMainItem, uint16_t index, uint8_t itemPos) +static void loadParameters(LISTITEM * parameterMainItem, uint16_t index, uint8_t itemPos) { uint8_t enabledParameterCount = getEnabledParameterCount(); uint8_t totalItems = (infoMachineSettings.EEPROM == 1) ? (enabledParameterCount + P_SETTINGS_COUNT) : enabledParameterCount; diff --git a/TFT/src/User/Menu/PersistentInfo.c b/TFT/src/User/Menu/PersistentInfo.c index d9cb0907c3..0e0262c8fb 100644 --- a/TFT/src/User/Menu/PersistentInfo.c +++ b/TFT/src/User/Menu/PersistentInfo.c @@ -7,7 +7,7 @@ #define GLOBALICON_INTERVAL 2 // check current menu to avoid display info -bool temperatureStatusValid(void) +static bool temperatureStatusValid(void) { if (infoSettings.persistent_info != 1) return false; if (infoHost.connected == false) return false; diff --git a/TFT/src/User/Menu/Pid.c b/TFT/src/User/Menu/Pid.c index 902196c096..270bf62524 100644 --- a/TFT/src/User/Menu/Pid.c +++ b/TFT/src/User/Menu/Pid.c @@ -3,8 +3,8 @@ //#define ENABLE_PID_STATUS_UPDATE_NOTIFICATION -const char *const pidCmdMarlin[] = PID_CMD_MARLIN; -const char *const pidCmdRRF[] = PID_CMD_RRF; +static const char * const pidCmdMarlin[] = PID_CMD_MARLIN; +static const char * const pidCmdRRF[] = PID_CMD_RRF; static int16_t pidHeaterTarget[MAX_HEATER_PID_COUNT] = {0}; static uint8_t curTool_index = NOZZLE0; @@ -21,7 +21,7 @@ void pidUpdateStatus(PID_STATUS status) pidStatus = status; } -uint8_t checkFirstValidPID(void) +static uint8_t checkFirstValidPID(void) { uint8_t tool = 0; @@ -36,7 +36,7 @@ uint8_t checkFirstValidPID(void) return tool; } -void pidRun(void) +static void pidRun(void) { uint8_t tool = checkFirstValidPID(); diff --git a/TFT/src/User/Menu/Popup.c b/TFT/src/User/Menu/Popup.c index d0d6c6067b..3ecc0849fc 100644 --- a/TFT/src/User/Menu/Popup.c +++ b/TFT/src/User/Menu/Popup.c @@ -5,6 +5,9 @@ #define MAX_MSG_LINES 4 #define POPUP_MAX_CHAR (X_MAX_CHAR * MAX_MSG_LINES) +static const GUI_RECT singleBtnRect = POPUP_RECT_SINGLE_CONFIRM; +const GUI_RECT doubleBtnRect[] = {POPUP_RECT_DOUBLE_CONFIRM, POPUP_RECT_DOUBLE_CANCEL}; + static BUTTON bottomSingleBtn = { // button location color before pressed color after pressed POPUP_RECT_SINGLE_CONFIRM, NULL, 5, 1, DARKGREEN, DARKGREEN, MAT_LOWWHITE, DARKGREEN, WHITE, DARKGREEN @@ -15,9 +18,6 @@ BUTTON bottomDoubleBtn[] = { {POPUP_RECT_DOUBLE_CANCEL, NULL, 5, 1, MAT_RED, MAT_RED, MAT_LOWWHITE, MAT_RED, WHITE, MAT_RED}, }; -const GUI_RECT doubleBtnRect[] = {POPUP_RECT_DOUBLE_CONFIRM, POPUP_RECT_DOUBLE_CANCEL}; -static const GUI_RECT singleBtnRect = POPUP_RECT_SINGLE_CONFIRM; - static WINDOW window = { DIALOG_TYPE_INFO, // default window type POPUP_RECT_WINDOW, // rectangle position and size of popup window @@ -30,13 +30,13 @@ static WINDOW window = { {POPUP_ACTION_FONT_COLOR, POPUP_ACTION_BG_COLOR}, // action bar font color / background color }; -static BUTTON *windowButton = NULL; +static BUTTON * windowButton = NULL; static uint16_t buttonNum = 0; static const GUI_RECT * cur_btn_rect = NULL; -static void (*action_ok)() = NULL; -static void (*action_cancel)() = NULL; -static void (*action_loop)() = NULL; +static void (* action_ok)() = NULL; +static void (* action_cancel)() = NULL; +static void (* action_loop)() = NULL; static bool popup_redraw = false; static uint8_t popup_title[X_MAX_CHAR]; @@ -45,7 +45,60 @@ static uint8_t popup_ok[24]; static uint8_t popup_cancel[24]; static DIALOG_TYPE popup_type; -void windowReDrawButton(uint8_t position, uint8_t pressed) +void _setDialogTitleStr(uint8_t * str) +{ + strncpy_no_pad((char *)popup_title, (char *)str, sizeof(popup_title)); +} + +void _setDialogMsgStr(uint8_t * str) +{ + strncpy_no_pad((char *)popup_msg, (char *)str, sizeof(popup_msg)); +} + +uint8_t * getDialogMsgStr(void) +{ + return (uint8_t *)popup_msg; +} + +void _setDialogOkTextStr(uint8_t * str) +{ + strncpy_no_pad((char *)popup_ok, (char *)str, sizeof(popup_ok)); +} + +void _setDialogCancelTextStr(uint8_t * str) +{ + strncpy_no_pad((char *)popup_cancel, (char *)str, sizeof(popup_cancel)); +} + +void _setDialogTitleLabel(int16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; + loadLabelText(tempstr, index); + strncpy_no_pad((char *)popup_title, (char *)tempstr, sizeof(popup_title)); +} + +void _setDialogMsgLabel(int16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; + loadLabelText(tempstr, index); + strncpy_no_pad((char *)popup_msg, (char *)tempstr, sizeof(popup_msg)); +} + +void _setDialogOkTextLabel(int16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; + loadLabelText(tempstr, index); + strncpy_no_pad((char *)popup_ok, (char *)tempstr, sizeof(popup_ok)); +} + +void _setDialogCancelTextLabel(int16_t index) +{ + uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; + loadLabelText(tempstr, index); + strncpy_no_pad((char *)popup_cancel, (char *)tempstr, sizeof(popup_cancel)); +} + +static void windowReDrawButton(uint8_t position, uint8_t pressed) { if (position >= buttonNum) return; @@ -96,9 +149,11 @@ void popupDrawPage(DIALOG_TYPE type, BUTTON * btn, const uint8_t * title, const void menuDialog(void) { menuSetTitle(NULL); + while (MENU_IS(menuDialog)) { uint16_t key_num = KEY_GetValue(buttonNum, cur_btn_rect); + switch (key_num) { case KEY_POPUP_CONFIRM: @@ -124,59 +179,6 @@ void menuDialog(void) } } -void _setDialogTitleStr(uint8_t * str) -{ - strncpy_no_pad((char *)popup_title, (char *)str, sizeof(popup_title)); -} - -void _setDialogMsgStr(uint8_t * str) -{ - strncpy_no_pad((char *)popup_msg, (char *)str, sizeof(popup_msg)); -} - -uint8_t *getDialogMsgStr() -{ - return (uint8_t *)popup_msg; -} - -void _setDialogOkTextStr(uint8_t * str) -{ - strncpy_no_pad((char *)popup_ok, (char *)str, sizeof(popup_ok)); -} - -void _setDialogCancelTextStr(uint8_t * str) -{ - strncpy_no_pad((char *)popup_cancel, (char *)str, sizeof(popup_cancel)); -} - -void _setDialogTitleLabel(int16_t index) -{ - uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; - loadLabelText(tempstr, index); - strncpy_no_pad((char *)popup_title, (char *)tempstr, sizeof(popup_title)); -} - -void _setDialogMsgLabel(int16_t index) -{ - uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; - loadLabelText(tempstr, index); - strncpy_no_pad((char *)popup_msg, (char *)tempstr, sizeof(popup_msg)); -} - -void _setDialogOkTextLabel(int16_t index) -{ - uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; - loadLabelText(tempstr, index); - strncpy_no_pad((char *)popup_ok, (char *)tempstr, sizeof(popup_ok)); -} - -void _setDialogCancelTextLabel(int16_t index) -{ - uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; - loadLabelText(tempstr, index); - strncpy_no_pad((char *)popup_cancel, (char *)tempstr, sizeof(popup_cancel)); -} - /** * @brief Show a popup with a message. Set dialog text before calling showDialog * @@ -184,8 +186,8 @@ void _setDialogCancelTextLabel(int16_t index) * @param ok_action pointer to a function to perform if ok is pressed. (pass NULL if no action need to be performed) * @param cancel_action pointer to a function to perform if Cancel is pressed.(pass NULL if no action need to be performed) * @param loop_action pointer to a function to perform whilst the dialog is active (visible/not answered) -*/ -void showDialog(DIALOG_TYPE type, void (*ok_action)(), void (*cancel_action)(), void (*loop_action)()) + */ +void showDialog(DIALOG_TYPE type, void (* ok_action)(), void (* cancel_action)(), void (* loop_action)()) { if ((infoSettings.mode == MODE_MARLIN) || (infoSettings.mode == MODE_BLOCKED_MARLIN)) // if standard/blocked Marlin mode, then exit return; @@ -223,6 +225,7 @@ void loopPopup(void) // display only a splash screen, avoiding to register the menuDialog handler // (the handler needs at least one button to allow to close the dialog box) popupDrawPage(popup_type, NULL, popup_title, popup_msg, NULL, NULL); + return; } diff --git a/TFT/src/User/Menu/Popup.h b/TFT/src/User/Menu/Popup.h index 072baa7dfa..6aecff12af 100644 --- a/TFT/src/User/Menu/Popup.h +++ b/TFT/src/User/Menu/Popup.h @@ -19,7 +19,7 @@ extern BUTTON bottomDoubleBtn[]; void _setDialogTitleStr(uint8_t * str); void _setDialogMsgStr(uint8_t * str); -uint8_t *getDialogMsgStr(void); +uint8_t * getDialogMsgStr(void); void _setDialogOkTextStr(uint8_t * str); void _setDialogCancelTextStr(uint8_t * str); @@ -28,20 +28,20 @@ void _setDialogMsgLabel(int16_t index); void _setDialogOkTextLabel(int16_t index); void _setDialogCancelTextLabel(int16_t index); -#define setDialogTitle(x) _Generic(((x+0)), const uint8_t*: _setDialogTitleStr, \ - uint8_t*: _setDialogTitleStr, \ - default: _setDialogTitleLabel)(x) -#define setDialogMsg(x) _Generic(((x+0)), const uint8_t*: _setDialogMsgStr, \ - uint8_t*: _setDialogMsgStr, \ - default: _setDialogMsgLabel)(x) -#define setDialogOkText(x) _Generic(((x+0)), const uint8_t*: _setDialogOkTextStr, \ - uint8_t*: _setDialogOkTextStr, \ - default: _setDialogOkTextLabel)(x) -#define setDialogCancelText(x) _Generic(((x+0)), const uint8_t*: _setDialogCancelTextStr, \ - uint8_t*: _setDialogCancelTextStr, \ - default: _setDialogCancelTextLabel)(x) +#define setDialogTitle(x) _Generic(((x+0)), const uint8_t *: _setDialogTitleStr, \ + uint8_t *: _setDialogTitleStr, \ + default: _setDialogTitleLabel)(x) +#define setDialogMsg(x) _Generic(((x+0)), const uint8_t *: _setDialogMsgStr, \ + uint8_t *: _setDialogMsgStr, \ + default: _setDialogMsgLabel)(x) +#define setDialogOkText(x) _Generic(((x+0)), const uint8_t *: _setDialogOkTextStr, \ + uint8_t *: _setDialogOkTextStr, \ + default: _setDialogOkTextLabel)(x) +#define setDialogCancelText(x) _Generic(((x+0)), const uint8_t *: _setDialogCancelTextStr, \ + uint8_t *: _setDialogCancelTextStr, \ + default: _setDialogCancelTextLabel)(x) -// set text from LABEL index or pointer (uint8_t*) +// set text from LABEL index or pointer (uint8_t *) #define setDialogText(title, msg, oktext, canceltext) \ { \ setDialogTitle(title); \ @@ -53,7 +53,7 @@ void _setDialogCancelTextLabel(int16_t index); void popupDrawPage(DIALOG_TYPE type, BUTTON * btn, const uint8_t * title, const uint8_t * context, const uint8_t * yes, const uint8_t * no); void menuDialog(void); -void showDialog(DIALOG_TYPE type, void (*ok_action)(), void (*cancel_action)(), void (*loop_action)()); +void showDialog(DIALOG_TYPE type, void (* ok_action)(), void (* cancel_action)(), void (* loop_action)()); void loopPopup(void); /** diff --git a/TFT/src/User/Menu/PreheatMenu.c b/TFT/src/User/Menu/PreheatMenu.c index 154f5a5e15..ce502dc702 100755 --- a/TFT/src/User/Menu/PreheatMenu.c +++ b/TFT/src/User/Menu/PreheatMenu.c @@ -1,9 +1,9 @@ #include "PreheatMenu.h" #include "includes.h" -const GUI_POINT preheat_title = {ICON_WIDTH / 2, PREHEAT_TITLE_Y }; -const GUI_POINT preheat_val_tool = {ICON_WIDTH - BYTE_WIDTH / 2, PREHEAT_TOOL_Y}; -const GUI_POINT preheat_val_bed = {ICON_WIDTH - BYTE_WIDTH / 2, PREHEAT_BED_Y}; +static const GUI_POINT preheat_title = {ICON_WIDTH / 2, PREHEAT_TITLE_Y }; +static const GUI_POINT preheat_val_tool = {ICON_WIDTH - BYTE_WIDTH / 2, PREHEAT_TOOL_Y}; +static const GUI_POINT preheat_val_bed = {ICON_WIDTH - BYTE_WIDTH / 2, PREHEAT_BED_Y}; typedef enum { @@ -13,7 +13,7 @@ typedef enum PREHEAT_TOOL_COUNT = 3, } TOOLPREHEAT; -void setPreheatIcon(ITEM * item, TOOLPREHEAT nowHeater) +static void setPreheatIcon(ITEM * item, TOOLPREHEAT nowHeater) { switch (nowHeater) { @@ -107,7 +107,7 @@ void menuPreheat(void) setPreheatIcon(&preheatItems.items[KEY_ICON_6], nowHeater); menuDrawPage(&preheatItems); - W25Qxx_ReadBuffer((uint8_t*)&preheatStore, PREHEAT_STORE_ADDR, sizeof(PREHEAT_STORE)); + W25Qxx_ReadBuffer((uint8_t *)&preheatStore, PREHEAT_STORE_ADDR, sizeof(PREHEAT_STORE)); for (int i = 0; i < PREHEAT_COUNT; i++) { diff --git a/TFT/src/User/Menu/Print.c b/TFT/src/User/Menu/Print.c index 9b9936ce15..14ad5875ab 100644 --- a/TFT/src/User/Menu/Print.c +++ b/TFT/src/User/Menu/Print.c @@ -15,9 +15,9 @@ enum PRINT_KEY_ONBOARD_USB }; -const GUI_RECT titleRect = {10, (TITLE_END_Y - BYTE_HEIGHT) / 2, LCD_WIDTH - 10, (TITLE_END_Y - BYTE_HEIGHT) / 2 + BYTE_HEIGHT}; +static const GUI_RECT titleRect = {10, (TITLE_END_Y - BYTE_HEIGHT) / 2, LCD_WIDTH - 10, (TITLE_END_Y - BYTE_HEIGHT) / 2 + BYTE_HEIGHT}; -const GUI_RECT gcodeRect[NUM_PER_PAGE] = { +static const GUI_RECT gcodeRect[NUM_PER_PAGE] = { #ifdef PORTRAIT_MODE {BYTE_WIDTH/2+0*SPACE_X_PER_ICON, 1*ICON_HEIGHT+0*SPACE_Y+ICON_START_Y+(SPACE_Y-BYTE_HEIGHT)/2, 1*SPACE_X_PER_ICON-BYTE_WIDTH/2, 1*ICON_HEIGHT+0*SPACE_Y+ICON_START_Y+(SPACE_Y-BYTE_HEIGHT)/2+BYTE_HEIGHT}, @@ -52,11 +52,11 @@ const GUI_RECT gcodeRect[NUM_PER_PAGE] = { }; // error labels for files/volume errors -const int16_t labelVolumeError[3] = {LABEL_TFT_SD_READ_ERROR, LABEL_TFT_USB_READ_ERROR, LABEL_ONBOARD_SD_READ_ERROR}; +static const int16_t labelVolumeError[3] = {LABEL_TFT_SD_READ_ERROR, LABEL_TFT_USB_READ_ERROR, LABEL_ONBOARD_SD_READ_ERROR}; static bool list_mode = true; -void normalNameDisp(const GUI_RECT * rect, uint8_t * name) +static void normalNameDisp(const GUI_RECT * rect, uint8_t * name) { if (name == NULL) return; @@ -118,7 +118,7 @@ static inline void gcodeIconDraw(void) } // update items in list mode -void gcodeListDraw(LISTITEM * item, uint16_t index, uint8_t itemPos) +static void gcodeListDraw(LISTITEM * item, uint16_t index, uint8_t itemPos) { if (index < infoFile.folderCount) // folder { @@ -139,7 +139,7 @@ void gcodeListDraw(LISTITEM * item, uint16_t index, uint8_t itemPos) } // open selected file/folder -bool printPageItemSelected(uint16_t index) +static bool printPageItemSelected(uint16_t index) { bool hasUpdate = true; diff --git a/TFT/src/User/Menu/PrintRestore.c b/TFT/src/User/Menu/PrintRestore.c index ef0d29da90..b0f5406748 100644 --- a/TFT/src/User/Menu/PrintRestore.c +++ b/TFT/src/User/Menu/PrintRestore.c @@ -14,15 +14,16 @@ void menuPrintRestore(void) { char okTxt[MAX_LANG_LABEL_LENGTH]; char cancelTxt[MAX_LANG_LABEL_LENGTH]; - loadLabelText((uint8_t*)okTxt, LABEL_CONFIRM); - loadLabelText((uint8_t*)cancelTxt, LABEL_CANCEL); + loadLabelText((uint8_t *)okTxt, LABEL_CONFIRM); + loadLabelText((uint8_t *)cancelTxt, LABEL_CANCEL); - popupDrawPage(DIALOG_TYPE_QUESTION, bottomDoubleBtn, textSelect(LABEL_POWER_FAILED), (uint8_t*)infoFile.path, - (uint8_t*)okTxt, (uint8_t*)cancelTxt); + popupDrawPage(DIALOG_TYPE_QUESTION, bottomDoubleBtn, textSelect(LABEL_POWER_FAILED), (uint8_t *)infoFile.path, + (uint8_t *)okTxt, (uint8_t *)cancelTxt); while (MENU_IS(menuPrintRestore)) { key_num = KEY_GetValue(2, doubleBtnRect); + switch (key_num) { case KEY_POPUP_CONFIRM: diff --git a/TFT/src/User/Menu/PrintingMenu.c b/TFT/src/User/Menu/PrintingMenu.c index ad8338d2dd..beb2ec711e 100644 --- a/TFT/src/User/Menu/PrintingMenu.c +++ b/TFT/src/User/Menu/PrintingMenu.c @@ -1,15 +1,6 @@ #include "Printing.h" #include "includes.h" -const GUI_RECT printinfo_val_rect[6] = { - {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_LG_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, - {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_LG_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, - {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_SM_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, - {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_LG_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, - {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_LG_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, - {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_SM_EX, PS_ICON_VAL_Y + BYTE_HEIGHT} -}; - #define PROGRESS_BAR_RAW_X0 (START_X) // X0 aligned to first icon #ifdef PORTRAIT_MODE #define PROGRESS_BAR_RAW_X1 (START_X + 3 * ICON_WIDTH + 2 * SPACE_X) // X1 aligned to last icon @@ -31,36 +22,25 @@ const GUI_RECT printinfo_val_rect[6] = { #define PROGRESS_BAR_SLICE_WIDTH (PROGRESS_BAR_FULL_WIDTH / 10) // 10% progress bar width #ifdef PORTRAIT_MODE - const GUI_RECT progressBar = {PROGRESS_BAR_X0, TITLE_END_Y + 1, - PROGRESS_BAR_X1, PS_ICON_START_Y - PS_ICON_SPACE_Y - 1}; + static const GUI_RECT progressBar = {PROGRESS_BAR_X0, TITLE_END_Y + 1, + PROGRESS_BAR_X1, PS_ICON_START_Y - PS_ICON_SPACE_Y - 1}; #else - const GUI_RECT progressBar = {PROGRESS_BAR_X0, PS_ICON_START_Y + PS_ICON_HEIGHT * 2 + PS_ICON_SPACE_Y * 2 + 1, - PROGRESS_BAR_X1, ICON_START_Y + ICON_HEIGHT + SPACE_Y - PS_ICON_SPACE_Y - 1}; + static const GUI_RECT progressBar = {PROGRESS_BAR_X0, PS_ICON_START_Y + PS_ICON_HEIGHT * 2 + PS_ICON_SPACE_Y * 2 + 1, + PROGRESS_BAR_X1, ICON_START_Y + ICON_HEIGHT + SPACE_Y - PS_ICON_SPACE_Y - 1}; #endif -enum -{ - LIVE_INFO_ICON = (1 << 0), - LIVE_INFO_TOP_ROW = (1 << 1), - LIVE_INFO_BOTTOM_ROW = (1 << 2), -}; - -const uint8_t printingIcon[] = {ICON_PRINTING_NOZZLE, ICON_PRINTING_BED, ICON_PRINTING_FAN, - ICON_PRINTING_TIMER, ICON_PRINTING_ZLAYER, ICON_PRINTING_SPEED}; - -const uint8_t printingIcon2nd[] = {ICON_PRINTING_CHAMBER, ICON_PRINTING_FLOW}; - -const char * const speedId[2] = {"Speed", "Flow "}; - #define TOGGLE_TIME 2000 // 1 seconds is 1000 #define LAYER_DELTA 0.1 // minimal layer height change to update the layer display (avoid congestion in vase mode) #define LAYER_TITLE "Layer" #define MAX_TITLE_LEN 70 #define TIME_FORMAT_STR "%02u:%02u:%02u" -PROGRESS_DISPLAY progDisplayType; -LAYER_TYPE layerDisplayType; -char title[MAX_TITLE_LEN]; +enum +{ + LIVE_INFO_ICON = (1 << 0), + LIVE_INFO_TOP_ROW = (1 << 1), + LIVE_INFO_BOTTOM_ROW = (1 << 2), +}; enum { @@ -79,6 +59,26 @@ enum ICON_POS_SPD, }; +static const GUI_RECT printingInfo_rect[6] = { + {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_LG_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, + {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_LG_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, + {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_SM_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, + {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_LG_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, + {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_LG_EX, PS_ICON_VAL_Y + BYTE_HEIGHT}, + {PS_ICON_VAL_X, PS_ICON_VAL_Y, PS_ICON_VAL_SM_EX, PS_ICON_VAL_Y + BYTE_HEIGHT} +}; + +static const uint8_t printingIcon[] = {ICON_PRINTING_NOZZLE, ICON_PRINTING_BED, ICON_PRINTING_FAN, + ICON_PRINTING_TIMER, ICON_PRINTING_ZLAYER, ICON_PRINTING_SPEED}; + +static const uint8_t printingIcon2nd[] = {ICON_PRINTING_CHAMBER, ICON_PRINTING_FLOW}; + +static const char * const speedId[2] = {"Speed", "Flow "}; + +static PROGRESS_DISPLAY progDisplayType; +static LAYER_TYPE layerDisplayType; +static char title[MAX_TITLE_LEN]; + static inline void setPauseResumeIcon(MENUITEMS * curmenu, bool paused) { curmenu->items[KEY_ICON_4].icon = paused ? ICON_RESUME : ICON_PAUSE; @@ -188,7 +188,7 @@ void startPrinting(void) static void reDrawPrintingValue(uint8_t icon_pos, uint8_t draw_type) { LIVE_INFO lvIcon; - GUI_RECT const * curRect = &printinfo_val_rect[icon_pos]; + GUI_RECT const * curRect = &printingInfo_rect[icon_pos]; char tempstrTop[14]; char tempstrBottom[14]; @@ -650,6 +650,7 @@ void menuPrinting(void) toggleInfo(); KEY_VALUES key_num = menuKeyGetValue(); + switch (key_num) { case PS_KEY_0: diff --git a/TFT/src/User/Menu/RRFMacros.c b/TFT/src/User/Menu/RRFMacros.c index 3e41d03011..f0de80e1d6 100644 --- a/TFT/src/User/Menu/RRFMacros.c +++ b/TFT/src/User/Menu/RRFMacros.c @@ -2,11 +2,11 @@ #include "includes.h" #include "RRFM20Parser.hpp" -static const char *running_macro_name; -extern const GUI_RECT titleRect; +static const char * running_macro_name; +static const GUI_RECT titleRect; // Scan files in RRF -void scanInfoFilesFs(void) +static void scanInfoFilesFs(void) { clearInfoFile(); request_M20_rrf(infoFile.path, false, parseMacroListResponse); @@ -33,7 +33,7 @@ static inline void runMacro(const char *display_name) // Draw Macro file list // update items in list mode -void macroListDraw(LISTITEM * item, uint16_t index, uint8_t itemPos) +static void macroListDraw(LISTITEM * item, uint16_t index, uint8_t itemPos) { if (index < infoFile.folderCount) { diff --git a/TFT/src/User/Menu/ScreenSettings.c b/TFT/src/User/Menu/ScreenSettings.c index 2604504a32..7badd9a09a 100644 --- a/TFT/src/User/Menu/ScreenSettings.c +++ b/TFT/src/User/Menu/ScreenSettings.c @@ -16,7 +16,7 @@ enum }; #define ITEM_MARLIN_TYPE_NUM 2 -const char *const labelMarlinType[ITEM_MARLIN_TYPE_NUM] = +static const char * const labelMarlinType[ITEM_MARLIN_TYPE_NUM] = { // item value text(only for custom value) "128x64", @@ -24,7 +24,7 @@ const char *const labelMarlinType[ITEM_MARLIN_TYPE_NUM] = }; #define ITEM_NOTIFICATION_TYPE_NUM 3 -const char *const itemNotificationType[ITEM_NOTIFICATION_TYPE_NUM] = +static const char * const itemNotificationType[ITEM_NOTIFICATION_TYPE_NUM] = { // item value text(only for custom value) "OFF", @@ -32,7 +32,7 @@ const char *const itemNotificationType[ITEM_NOTIFICATION_TYPE_NUM] = "TOAST" }; -const char *const itemSortBy[SORT_BY_COUNT] = +static const char * const itemSortBy[SORT_BY_COUNT] = { // item value text(only for custom value) "Date ▼", @@ -43,7 +43,7 @@ const char *const itemSortBy[SORT_BY_COUNT] = #ifdef HAS_EMULATOR -void menuEmulatorFontColor(void) +static void menuEmulatorFontColor(void) { LABEL title = {LABEL_FONT_COLOR}; LISTITEM totalItems[LCD_COLOR_COUNT]; @@ -93,7 +93,7 @@ void menuEmulatorFontColor(void) saveSettings(); // save settings } -void menuEmulatorBGColor(void) +static void menuEmulatorBGColor(void) { LABEL title = {LABEL_BG_COLOR}; LISTITEM totalItems[LCD_COLOR_COUNT]; @@ -143,7 +143,7 @@ void menuEmulatorBGColor(void) saveSettings(); // save settings } -void menuMarlinModeSettings(void) +static void menuMarlinModeSettings(void) { LABEL title = {LABEL_MARLIN_MODE_SETTINGS}; LISTITEM marlinModeitems[] = { @@ -217,7 +217,7 @@ void menuMarlinModeSettings(void) #endif // ST7920_EMULATOR -void menuLanguage(void) +static void menuLanguage(void) { LABEL title = {LABEL_LANGUAGE}; LISTITEM totalItems[LANGUAGE_NUM]; @@ -225,7 +225,7 @@ void menuLanguage(void) uint16_t curItem = infoSettings.language; SETTINGS now = infoSettings; - char *firstLanguage = (char *)default_pack[LABEL_LANGUAGE]; // get first language name directly from memory + char * firstLanguage = (char *)default_pack[LABEL_LANGUAGE]; // get first language name directly from memory char secondLanguage[MAX_LANG_LABEL_LENGTH]; W25Qxx_ReadBuffer((uint8_t *)&secondLanguage, getLabelFlashAddr(LABEL_LANGUAGE), MAX_LANG_LABEL_LENGTH); // read second language name from SPI flash @@ -273,7 +273,7 @@ void menuLanguage(void) } } -void menuUISettings(void) +static void menuUISettings(void) { LABEL title = {LABEL_UI_SETTINGS}; LISTITEM uiItems[] = { @@ -320,6 +320,7 @@ void menuUISettings(void) while (MENU_IS(menuUISettings)) { curIndex = listViewGetSelectedIndex(); + switch (curIndex) { case 0: @@ -392,7 +393,7 @@ void menuUISettings(void) #ifdef BUZZER_PIN -void menuSoundSettings(void) +static void menuSoundSettings(void) { LABEL title = {LABEL_SOUND}; LISTITEM sounditems[] = { @@ -427,13 +428,13 @@ void menuSoundSettings(void) } saveSettings(); // save settings -} // menuSoundSettings +} // menuSoundSettings #endif // BUZZER_PIN #ifdef LCD_LED_PWM_CHANNEL -void menuBrightnessSettings(void) +static void menuBrightnessSettings(void) { LABEL title = {LABEL_LCD_BRIGHTNESS}; LISTITEM brightnessitems[] = { @@ -461,6 +462,7 @@ void menuBrightnessSettings(void) while (MENU_IS(menuBrightnessSettings)) { curIndex = listViewGetSelectedIndex(); + switch (curIndex) { case 0: @@ -547,6 +549,7 @@ void menuScreenSettings(void) while (MENU_IS(menuScreenSettings)) { curIndex = menuKeyGetValue(); + switch (curIndex) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/SelectMode.c b/TFT/src/User/Menu/SelectMode.c index 32a9d5571a..2e544c1e1e 100644 --- a/TFT/src/User/Menu/SelectMode.c +++ b/TFT/src/User/Menu/SelectMode.c @@ -8,13 +8,13 @@ typedef enum MKEY_IDLE = IDLE_TOUCH, } MKEY_VALUES; -const GUI_RECT rect_of_mode[MODE_COUNT] = { +static const GUI_RECT rect_of_mode[MODE_COUNT] = { // 2 select icon {1 * SPACE_SELEX + 0 * ICON_WIDTH, SPACE_SELEY, 1 * SPACE_SELEX + 1 * ICON_WIDTH, SPACE_SELEY + ICON_HEIGHT}, {3 * SPACE_SELEX + 1 * ICON_WIDTH, SPACE_SELEY, 3 * SPACE_SELEX + 2 * ICON_WIDTH, SPACE_SELEY + ICON_HEIGHT}, }; -const uint8_t icon_mode [MODE_COUNT] = { +static const uint8_t icon_mode [MODE_COUNT] = { ICON_MARLIN_MODE, ICON_TOUCH_MODE, }; @@ -27,8 +27,8 @@ static inline void drawModeIcon(void) } const GUI_RECT mode_title_rect[MODE_COUNT] = { - {0, rect_of_mode[0].y1 + BYTE_HEIGHT / 2, text_startx, rect_of_mode[0].y1 + BYTE_HEIGHT / 2 + BYTE_HEIGHT}, - {text_startx, rect_of_mode[0].y1 + BYTE_HEIGHT / 2, LCD_WIDTH, rect_of_mode[0].y1 + BYTE_HEIGHT / 2 + BYTE_HEIGHT}, + {0, rect_of_mode[0].y1 + BYTE_HEIGHT / 2, TEXT_STARTX, rect_of_mode[0].y1 + BYTE_HEIGHT / 2 + BYTE_HEIGHT}, + {TEXT_STARTX, rect_of_mode[0].y1 + BYTE_HEIGHT / 2, LCD_WIDTH, rect_of_mode[0].y1 + BYTE_HEIGHT / 2 + BYTE_HEIGHT}, }; GUI_RestoreColorDefault(); @@ -41,7 +41,7 @@ static inline MKEY_VALUES MKeyGetValue(void) return (MKEY_VALUES)KEY_GetValue(COUNT(rect_of_mode), rect_of_mode); } -void drawSelectedMode(int8_t nowMode) +static void drawSelectedMode(int8_t nowMode) { const uint8_t border_off = 4; diff --git a/TFT/src/User/Menu/SettingsMenu.c b/TFT/src/User/Menu/SettingsMenu.c index e9bf98117b..564cd73293 100644 --- a/TFT/src/User/Menu/SettingsMenu.c +++ b/TFT/src/User/Menu/SettingsMenu.c @@ -1,7 +1,7 @@ #include "SettingsMenu.h" #include "includes.h" -const MENUITEMS settingsItems = { +static const MENUITEMS settingsItems = { // title LABEL_SETTINGS, // icon label @@ -61,7 +61,7 @@ void menuInfo(void) { char buf[128]; - const char *const hardware = HARDWARE_MANUFACTURER HARDWARE_VERSION; + const char * const hardware = HARDWARE_MANUFACTURER HARDWARE_VERSION; GUI_Clear(infoSettings.bg_color); GUI_SetColor(GRAY); @@ -150,6 +150,7 @@ void menuSettings(void) while (MENU_IS(menuSettings)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/Speed.c b/TFT/src/User/Menu/Speed.c index 7f8eb82c7a..d6880c4b49 100644 --- a/TFT/src/User/Menu/Speed.c +++ b/TFT/src/User/Menu/Speed.c @@ -7,13 +7,13 @@ typedef struct uint8_t set; } LASTSPEED; -const ITEM itemPercentType[SPEED_NUM] = { +static const ITEM itemPercentType[SPEED_NUM] = { // icon label {ICON_MOVE, LABEL_PERCENTAGE_SPEED}, {ICON_EXTRUDE, LABEL_PERCENTAGE_FLOW}, }; -const int16_t itemPercentTypeTitle[SPEED_NUM] = { +static const int16_t itemPercentTypeTitle[SPEED_NUM] = { LABEL_PERCENTAGE_SPEED, LABEL_PERCENTAGE_FLOW }; diff --git a/TFT/src/User/Menu/StatusScreen.c b/TFT/src/User/Menu/StatusScreen.c index c96e7f0280..bea5bca1d0 100644 --- a/TFT/src/User/Menu/StatusScreen.c +++ b/TFT/src/User/Menu/StatusScreen.c @@ -12,15 +12,37 @@ #define SET_SPEEDMENUINDEX(x) #endif -#define TOOL_TOGGLE_TIME 2000 // 1 seconds is 1000 - #ifdef PORTRAIT_MODE #define XYZ_STATUS "X:%.2f Y:%.2f Z:%.2f" #else #define XYZ_STATUS " X: %.2f Y: %.2f Z: %.2f " #endif -const MENUITEMS statusItems = { +#define TOOL_TOGGLE_TIME 2000 // 1 seconds is 1000 + +// text position rectangles for Live icons +static const GUI_POINT ss_title_point = {SS_ICON_WIDTH - BYTE_WIDTH / 2, SS_ICON_NAME_Y0}; +static const GUI_POINT ss_val_point = {SS_ICON_WIDTH / 2, SS_ICON_VAL_Y0}; +#ifdef TFT70_V3_0 + static const GUI_POINT ss_val_point_2 = {SS_ICON_WIDTH / 2, SS_ICON_VAL_Y0_2}; +#endif + +// info box msg area +#ifdef PORTRAIT_MODE + const GUI_RECT msgRect = {START_X + 0.5 * ICON_WIDTH + 0 * SPACE_X + 2, ICON_START_Y + 0 * ICON_HEIGHT + 0 * SPACE_Y + STATUS_MSG_BODY_YOFFSET, + START_X + 2.5 * ICON_WIDTH + 1 * SPACE_X - 2, ICON_START_Y + 1 * ICON_HEIGHT + 0 * SPACE_Y - STATUS_MSG_BODY_BOTTOM}; + + const GUI_RECT recGantry = {START_X - 3, SS_ICON_HEIGHT + ICON_START_Y + STATUS_GANTRY_YOFFSET, + START_X + 3 + 3 * ICON_WIDTH + 2 * SPACE_X, ICON_HEIGHT + SPACE_Y + ICON_START_Y - STATUS_GANTRY_YOFFSET}; +#else + const GUI_RECT msgRect = {START_X + 1 * ICON_WIDTH + 1 * SPACE_X + 2, ICON_START_Y + 1 * ICON_HEIGHT + 1 * SPACE_Y + STATUS_MSG_BODY_YOFFSET, + START_X + 3 * ICON_WIDTH + 2 * SPACE_X - 2, ICON_START_Y + 2 * ICON_HEIGHT + 1 * SPACE_Y - STATUS_MSG_BODY_BOTTOM}; + + const GUI_RECT recGantry = {START_X, SS_ICON_HEIGHT + ICON_START_Y + STATUS_GANTRY_YOFFSET, + START_X + 4 * ICON_WIDTH + 3 * SPACE_X, ICON_HEIGHT + SPACE_Y + ICON_START_Y - STATUS_GANTRY_YOFFSET}; +#endif + +static const MENUITEMS statusItems = { // title LABEL_READY, // icon label @@ -41,9 +63,9 @@ const MENUITEMS statusItems = { } }; -const uint8_t bedIcons[2] = {ICON_STATUS_BED, ICON_STATUS_CHAMBER}; - -const uint8_t speedIcons[2] = {ICON_STATUS_SPEED, ICON_STATUS_FLOW}; +static const uint8_t bedIcons[2] = {ICON_STATUS_BED, ICON_STATUS_CHAMBER}; +static const uint8_t speedIcons[2] = {ICON_STATUS_SPEED, ICON_STATUS_FLOW}; +static const char * const speedID[2] = SPEED_ID; static int8_t lastConnectionStatus = -1; static bool msgNeedRefresh = false; @@ -51,31 +73,7 @@ static bool msgNeedRefresh = false; static char msgTitle[20]; static char msgBody[MAX_MSG_LENGTH]; -const char *const speedID[2] = SPEED_ID; - -// text position rectangles for Live icons -const GUI_POINT ss_title_point = {SS_ICON_WIDTH - BYTE_WIDTH / 2, SS_ICON_NAME_Y0}; -const GUI_POINT ss_val_point = {SS_ICON_WIDTH / 2, SS_ICON_VAL_Y0}; -#ifdef TFT70_V3_0 - const GUI_POINT ss_val_point_2 = {SS_ICON_WIDTH / 2, SS_ICON_VAL_Y0_2}; -#endif - -// info box msg area -#ifdef PORTRAIT_MODE - const GUI_RECT msgRect = {START_X + 0.5 * ICON_WIDTH + 0 * SPACE_X + 2, ICON_START_Y + 0 * ICON_HEIGHT + 0 * SPACE_Y + STATUS_MSG_BODY_YOFFSET, - START_X + 2.5 * ICON_WIDTH + 1 * SPACE_X - 2, ICON_START_Y + 1 * ICON_HEIGHT + 0 * SPACE_Y - STATUS_MSG_BODY_BOTTOM}; - - const GUI_RECT recGantry = {START_X - 3, SS_ICON_HEIGHT + ICON_START_Y + STATUS_GANTRY_YOFFSET, - START_X + 3 + 3 * ICON_WIDTH + 2 * SPACE_X, ICON_HEIGHT + SPACE_Y + ICON_START_Y - STATUS_GANTRY_YOFFSET}; -#else - const GUI_RECT msgRect = {START_X + 1 * ICON_WIDTH + 1 * SPACE_X + 2, ICON_START_Y + 1 * ICON_HEIGHT + 1 * SPACE_Y + STATUS_MSG_BODY_YOFFSET, - START_X + 3 * ICON_WIDTH + 2 * SPACE_X - 2, ICON_START_Y + 2 * ICON_HEIGHT + 1 * SPACE_Y - STATUS_MSG_BODY_BOTTOM}; - - const GUI_RECT recGantry = {START_X, SS_ICON_HEIGHT + ICON_START_Y + STATUS_GANTRY_YOFFSET, - START_X + 4 * ICON_WIDTH + 3 * SPACE_X, ICON_HEIGHT + SPACE_Y + ICON_START_Y - STATUS_GANTRY_YOFFSET}; -#endif - -void statusSetMsg(const uint8_t *title, const uint8_t *msg) +void statusSetMsg(const uint8_t * title, const uint8_t * msg) { strncpy_no_pad(msgTitle, (char *)title, sizeof(msgTitle)); strncpy_no_pad(msgBody, (char *)msg, sizeof(msgBody)); @@ -94,7 +92,7 @@ void statusSetReady(void) msgNeedRefresh = true; } -void statusDraw(void) +static void statusDraw(void) { // icons and their values are updated one by one to reduce flicker/clipping char tempstr[45]; @@ -219,7 +217,7 @@ void statusDraw(void) GUI_RestoreColorDefault(); } -void statusDrawMsg(void) +static void statusDrawMsg(void) { GUI_SetTextMode(GUI_TEXTMODE_TRANS); diff --git a/TFT/src/User/Menu/StatusScreen.h b/TFT/src/User/Menu/StatusScreen.h index f5b08be180..2cfb681d91 100644 --- a/TFT/src/User/Menu/StatusScreen.h +++ b/TFT/src/User/Menu/StatusScreen.h @@ -10,7 +10,7 @@ extern "C" { extern const GUI_RECT msgRect; -void statusSetMsg(const uint8_t *title,const uint8_t *msg); +void statusSetMsg(const uint8_t * title,const uint8_t * msg); void statusSetReady(void); void menuStatus(void); diff --git a/TFT/src/User/Menu/Terminal.c b/TFT/src/User/Menu/Terminal.c index 688cb2f991..907c73c917 100644 --- a/TFT/src/User/Menu/Terminal.c +++ b/TFT/src/User/Menu/Terminal.c @@ -111,11 +111,11 @@ typedef enum #define CURSOR_END_Y ((KB_START_ROW + KB_ROW_COUNT) * KEY_HEIGHT) // gcode command draw area inside text box -const GUI_RECT textBoxRect = { 0 + TEXTBOX_INSET, (COMMAND_START_ROW + 0) * CTRL_HEIGHT + TEXTBOX_INSET, - 3 * CTRL_WIDTH - TEXTBOX_INSET, (COMMAND_START_ROW + 1) * CTRL_HEIGHT - TEXTBOX_INSET}; +static const GUI_RECT textBoxRect = { 0 + TEXTBOX_INSET, (COMMAND_START_ROW + 0) * CTRL_HEIGHT + TEXTBOX_INSET, + 3 * CTRL_WIDTH - TEXTBOX_INSET, (COMMAND_START_ROW + 1) * CTRL_HEIGHT - TEXTBOX_INSET}; // keyboard rectangles -const GUI_RECT editorKeyRect[KEY_COUNT] = { +static const GUI_RECT editorKeyRect[KEY_COUNT] = { // row text box + send button {0 * CTRL_WIDTH + TEXTBOX_INSET + TEXTBOX_BUTTON_INSET, (COMMAND_START_ROW + 0) * CTRL_HEIGHT + TEXTBOX_INSET + TEXTBOX_BUTTON_INSET, 1 * CTRL_WIDTH + TEXTBOX_BUTTON_INSET, (COMMAND_START_ROW + 1) * CTRL_HEIGHT - TEXTBOX_INSET - TEXTBOX_BUTTON_INSET}, // Prev gcode (top row) @@ -223,29 +223,29 @@ const GUI_RECT editorKeyRect[KEY_COUNT] = { }; // area rectangles -const GUI_RECT editorAreaRect[3] = { +static const GUI_RECT editorAreaRect[3] = { {0, COMMAND_START_ROW * CTRL_HEIGHT, LCD_WIDTH, ROW_HEIGHT}, // text box + send area {0, ROW_HEIGHT, LCD_WIDTH, LCD_HEIGHT - CTRL_HEIGHT}, // keyboard area {0, CTRL_START_ROW * CTRL_HEIGHT, LCD_WIDTH, LCD_HEIGHT} // control bar area }; -const GUI_RECT terminalKeyRect[TERM_KEY_COUNT] = { +static const GUI_RECT terminalKeyRect[TERM_KEY_COUNT] = { {1 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT - CTRL_HEIGHT, 2 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT}, // page down {2 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT - CTRL_HEIGHT, 3 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT}, // page up {3 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT - CTRL_HEIGHT, 4 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT}, // ACK {4 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT - CTRL_HEIGHT, 5 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT}, // Back }; -const GUI_RECT terminalPageRect = { +static const GUI_RECT terminalPageRect = { 0 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT - CTRL_HEIGHT, 1 * TERMINAL_CTRL_WIDTH, LCD_HEIGHT}; // page number -const GUI_RECT terminalAreaRect[2] = { +static const GUI_RECT terminalAreaRect[2] = { {0, 0, LCD_WIDTH, CURSOR_END_Y}, // terminal area {0, CURSOR_END_Y, LCD_WIDTH, LCD_HEIGHT}, // control area }; // keyboard keys for first layout -const char * const gcodeKey123[KEY_COUNT] = { +static const char * const gcodeKey123[KEY_COUNT] = { "Prev", "Next", "Clear", "Send", "ABC", "Space", "Del", "Back", #if KB_COL_COUNT == LAYOUT_1_COL_COUNT "1", "2", "3", "M", "G", "T", @@ -268,7 +268,7 @@ const char * const gcodeKey123[KEY_COUNT] = { }; // keyboard keys for second layout -const char * const gcodeKeyABC[KEY_COUNT] = { +static const char * const gcodeKeyABC[KEY_COUNT] = { "Prev", "Next", "Clear", "Send", "123", "Space", "Del", "Back", #if KB_COL_COUNT == LAYOUT_1_COL_COUNT "A", "B", "C", "D", "H", "I", @@ -306,19 +306,19 @@ const char * const gcodeKeyABC[KEY_COUNT] = { #endif }; -const uint16_t fontSrcColor[3][3] = { +static const uint16_t fontSrcColor[3][3] = { // Gcode ACK Background {COLORSCHEME1_TERM_GCODE, COLORSCHEME1_TERM_ACK, COLORSCHEME1_TERM_BACK}, // Material Dark {COLORSCHEME2_TERM_GCODE, COLORSCHEME2_TERM_ACK, COLORSCHEME2_TERM_BACK}, // Material Light {COLORSCHEME3_TERM_GCODE, COLORSCHEME3_TERM_ACK, COLORSCHEME3_TERM_BACK}, // High Contrast }; -KEYBOARD_DATA * keyboardData; -TERMINAL_DATA * terminalData; -char * terminalBuf; -TERMINAL_WINDOW curView = KEYBOARD_VIEW; +static KEYBOARD_DATA * keyboardData; +static TERMINAL_DATA * terminalData; +static char * terminalBuf; +static TERMINAL_WINDOW curView = KEYBOARD_VIEW; -bool numpad = +static bool numpad = #if defined(KB_TYPE_QWERTY) false; // show qwerty as default for larger #else @@ -563,6 +563,7 @@ static inline void menuKeyboardView(void) break; key_num = menuKeyGetValue(); + switch (key_num) { case GKEY_IDLE: @@ -694,7 +695,7 @@ void terminalCache(const char * stream, uint16_t streamLen, SERIAL_PORT_INDEX po } // reverse lookup for source identifier -TERMINAL_SRC getLastSrc(char * ptr) +static TERMINAL_SRC getLastSrc(char * ptr) { TERMINAL_SRC lastSrc = SRC_TERMINAL_COUNT; char * endPtr = (ptr + 1); @@ -784,7 +785,7 @@ static inline void terminalDrawButton(uint8_t index, uint8_t isPressed) #endif // KEYBOARD_MATERIAL_THEME } -uint8_t terminalUpdatePageCount(void) +static uint8_t terminalUpdatePageCount(void) { if (terminalData->pageTail >= terminalData->pageHead) terminalData->pageCount = abs(terminalData->pageTail - terminalData->pageHead); @@ -828,7 +829,7 @@ static inline void terminalDrawMenu(void) terminalDrawPageNumber(); } -void menuTerminalView(void) +static void menuTerminalView(void) { #define CURSOR_START_X (terminalAreaRect[0].x0 + CURSOR_H_OFFSET) @@ -850,6 +851,7 @@ void menuTerminalView(void) break; key_num = menuKeyGetValue(); + switch (key_num) { case TERM_PAGE_UP: // page up diff --git a/TFT/src/User/Menu/Touchmi.c b/TFT/src/User/Menu/Touchmi.c index 9c1c333766..0b64d2fa40 100644 --- a/TFT/src/User/Menu/Touchmi.c +++ b/TFT/src/User/Menu/Touchmi.c @@ -1,7 +1,7 @@ #include "Touchmi.h" #include "includes.h" -const MENUITEMS touchmiItems = { +static const MENUITEMS touchmiItems = { // title LABEL_TOUCHMI, // icon label @@ -26,6 +26,7 @@ void menuTouchMi(void) while (MENU_IS(menuTouchMi)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/TuneExtruder.c b/TFT/src/User/Menu/TuneExtruder.c index e843a943a6..6e5b392870 100644 --- a/TFT/src/User/Menu/TuneExtruder.c +++ b/TFT/src/User/Menu/TuneExtruder.c @@ -11,7 +11,7 @@ static uint8_t degreeSteps_index = 1; static uint8_t extStep_index = 0; static bool loadRequested = false; -void showNewESteps(const float measured_length, const float old_esteps, float * new_esteps) +static void showNewESteps(const float measured_length, const float old_esteps, float * new_esteps) { char tempstr[20]; @@ -23,14 +23,14 @@ void showNewESteps(const float measured_length, const float old_esteps, float * sprintf(tempstr, " %0.2fmm ", measured_length); GUI_DispStringInPrect(&exhibitRect, (uint8_t *)tempstr); - sprintf(tempstr, (char*)textSelect(LABEL_TUNE_EXT_OLD_ESTEP), old_esteps); + sprintf(tempstr, (char *)textSelect(LABEL_TUNE_EXT_OLD_ESTEP), old_esteps); GUI_DispString(exhibitRect.x0, exhibitRect.y1 - BYTE_HEIGHT, (uint8_t *)tempstr); - sprintf(tempstr, (char*)textSelect(LABEL_TUNE_EXT_NEW_ESTEP), *new_esteps); + sprintf(tempstr, (char *)textSelect(LABEL_TUNE_EXT_NEW_ESTEP), *new_esteps); GUI_DispString(exhibitRect.x0, exhibitRect.y1, (uint8_t *)tempstr); } -void menuNewExtruderESteps(void) +static void menuNewExtruderESteps(void) { // extruder steps are not correct. Ask user for the amount that's extruded. // Automaticaly calculate new steps/mm when changing the measured distance. diff --git a/TFT/src/User/Menu/Tuning.c b/TFT/src/User/Menu/Tuning.c index d34bc1e94c..de8a03d26f 100644 --- a/TFT/src/User/Menu/Tuning.c +++ b/TFT/src/User/Menu/Tuning.c @@ -45,6 +45,7 @@ void menuTuning(void) while (MENU_IS(menuTuning)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/UnifiedHeat.c b/TFT/src/User/Menu/UnifiedHeat.c index f71a259ef3..02323b1eff 100644 --- a/TFT/src/User/Menu/UnifiedHeat.c +++ b/TFT/src/User/Menu/UnifiedHeat.c @@ -1,7 +1,7 @@ #include "UnifiedHeat.h" #include "includes.h" -const MENUITEMS UnifiedHeatItems = { +static const MENUITEMS UnifiedHeatItems = { // title LABEL_UNIFIEDHEAT, // icon label @@ -26,6 +26,7 @@ void menuUnifiedHeat(void) while (MENU_IS(menuUnifiedHeat)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/UnifiedMove.c b/TFT/src/User/Menu/UnifiedMove.c index f33068f1b2..ba902cacdf 100644 --- a/TFT/src/User/Menu/UnifiedMove.c +++ b/TFT/src/User/Menu/UnifiedMove.c @@ -2,7 +2,7 @@ #include "includes.h" #if DELTA_PROBE_TYPE != 0 // if Delta printer - void deltaCalibration(void) + static void deltaCalibration(void) { mustStoreCmd("G33\n"); } @@ -43,6 +43,7 @@ void menuUnifiedMove(void) while (MENU_IS(menuUnifiedMove)) { key_num = menuKeyGetValue(); + switch (key_num) { case KEY_ICON_0: diff --git a/TFT/src/User/Menu/ZOffset.c b/TFT/src/User/Menu/ZOffset.c index 2e1c3530f1..5253e29408 100644 --- a/TFT/src/User/Menu/ZOffset.c +++ b/TFT/src/User/Menu/ZOffset.c @@ -6,7 +6,7 @@ static uint8_t curUnit_index = 0; static uint8_t curSubmenu_index = 0; // Show an error notification -void zOffsetNotifyError(bool isStarted) +static void zOffsetNotifyError(bool isStarted) { LABELCHAR(tempMsg, LABEL_PROBE_OFFSET) @@ -21,7 +21,7 @@ void zOffsetNotifyError(bool isStarted) addToast(DIALOG_TYPE_ERROR, tempMsg); } -void zOffsetDraw(bool status, float val) +static void zOffsetDraw(bool status, float val) { char tempstr[20], tempstr2[20], tempstr3[30]; diff --git a/TFT/src/User/Menu/common.c b/TFT/src/User/Menu/common.c index 66365665e3..78bd2d5b58 100644 --- a/TFT/src/User/Menu/common.c +++ b/TFT/src/User/Menu/common.c @@ -302,7 +302,7 @@ float editFloatValue(float minValue, float maxValue, float resetValue, float val } // set the hotend to the minimum extrusion temperature if user selected "OK" -void heatToMinTemp(void) +static void heatToMinTemp(void) { heatSetTargetTemp(heatGetToolIndex(), infoSettings.min_ext_temp, FROM_GUI); } @@ -330,6 +330,7 @@ NOZZLE_STATUS warmupNozzle(void) else { // contiunue with current temp but no lower than the minimum extruder temperature heatSetTargetTemp(toolIndex, MAX(infoSettings.min_ext_temp, heatGetCurrentTemp(toolIndex)), FROM_GUI); + return SETTLING; } } @@ -346,6 +347,7 @@ NOZZLE_STATUS warmupNozzle(void) strcat(tempMsg, tempStr); popupReminder(DIALOG_TYPE_ERROR, LABEL_WARNING, (uint8_t *)tempMsg); + return COLD; } } diff --git a/TFT/src/User/Menu/common.h b/TFT/src/User/Menu/common.h index 09bdfb5308..9da3480358 100644 --- a/TFT/src/User/Menu/common.h +++ b/TFT/src/User/Menu/common.h @@ -63,9 +63,9 @@ bool nextScreenUpdate(uint32_t refreshTime); #ifdef FRIENDLY_Z_OFFSET_LANGUAGE void invertZAxisIcons(MENUITEMS * menuItems); - #define INVERT_Z_AXIS_ICONS(menuItemsPtr) invertZAxisIcons(menuItemsPtr) + #define INVERT_Z_AXIS_ICONS(menuItemsPtr) invertZAxisIcons(menuItemsPtr) #else - #define INVERT_Z_AXIS_ICONS(menuItemsPtr) + #define INVERT_Z_AXIS_ICONS(menuItemsPtr) #endif void drawBorder(const GUI_RECT * rect, uint16_t color, uint16_t edgeDistance); diff --git a/TFT/src/User/SanityCheck.h b/TFT/src/User/SanityCheck.h index c99517f672..9a228a29f0 100644 --- a/TFT/src/User/SanityCheck.h +++ b/TFT/src/User/SanityCheck.h @@ -13,7 +13,7 @@ extern "C" { #include "ui_draw.h" // check size of settings against max allocated size at compile time -#define SIZE_CHECK(object) ((void)sizeof(char[1 - 2*!!(object)])) +#define SIZE_CHECK(object) ((void)sizeof(char[1 - 2 * !!(object)])) #if CONFIG_VERSION != CONFIG_SUPPPORT #error "the Configuration.h is old. please use the latest Configuration.h file" @@ -56,8 +56,8 @@ extern "C" { #ifdef DEFAULT_MODE #undef DEFAULT_MODE #endif - #define DEFAULT_MODE 1 // Just set hardcoded here. - //#warning "DEFAULT_MODE supports only Touch Mode. Please update/check your configuration." + #define DEFAULT_MODE 1 // just set hardcoded here + //#warning "DEFAULT_MODE supports only Touch Mode. Please update/check your configuration" #endif #endif @@ -335,7 +335,7 @@ extern "C" { #if THUMBNAIL_PARSER == PARSER_BASE64PNG #if RAM_SIZE < 96 - // Decoding Base64-encoded PNGs is not possible due to memory requirements. Downgrading to the "RGB565 bitmap" option. + // decoding Base64-encoded PNGs is not possible due to memory requirements. Downgrading to the "RGB565 bitmap" option #undef THUMBNAIL_PARSER #define THUMBNAIL_PARSER PARSER_RGB565 #endif diff --git a/TFT/src/User/Variants/pin_MKS_GD_TFT28_V1_2_4.h b/TFT/src/User/Variants/pin_MKS_GD_TFT28_V1_2_4.h index d1d176723b..164800a4ed 100644 --- a/TFT/src/User/Variants/pin_MKS_GD_TFT28_V1_2_4.h +++ b/TFT/src/User/Variants/pin_MKS_GD_TFT28_V1_2_4.h @@ -17,8 +17,8 @@ #define HARDWARE_VERSION "GD_TFT28_V1.2-4" #endif -// SERIAL_PORT: communicating with host (Marlin, RRF etc...) -// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc...) +// SERIAL_PORT: communicating with host (Marlin, RRF etc.) +// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc.) #ifndef SERIAL_PORT #define SERIAL_PORT _USART2 // default USART port #define SERIAL_PORT_2 _USART3 diff --git a/TFT/src/User/Variants/pin_MKS_TFT32_V1_4.h b/TFT/src/User/Variants/pin_MKS_TFT32_V1_4.h index c3a75d0338..38ea126474 100644 --- a/TFT/src/User/Variants/pin_MKS_TFT32_V1_4.h +++ b/TFT/src/User/Variants/pin_MKS_TFT32_V1_4.h @@ -70,8 +70,8 @@ #define LCD_DATA_16BIT 1 #endif -// SERIAL_PORT: communicating with host (Marlin, RRF etc...) -// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc...) +// SERIAL_PORT: communicating with host (Marlin, RRF etc.) +// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc.) #ifndef SERIAL_PORT #define SERIAL_PORT _USART2 // default USART port #define SERIAL_PORT_2 _USART1 diff --git a/TFT/src/User/Variants/pin_MKS_TFT35_V1_0.h b/TFT/src/User/Variants/pin_MKS_TFT35_V1_0.h index fc3174ae79..a6825b9d77 100644 --- a/TFT/src/User/Variants/pin_MKS_TFT35_V1_0.h +++ b/TFT/src/User/Variants/pin_MKS_TFT35_V1_0.h @@ -75,8 +75,8 @@ #define LCD_DATA_16BIT 1 #endif -// SERIAL_PORT: communicating with host (Marlin, RRF etc...) -// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc...) +// SERIAL_PORT: communicating with host (Marlin, RRF etc.) +// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc.) #ifndef SERIAL_PORT #define SERIAL_PORT _USART3 // default USART port for host communication PB10 PB11 #define SERIAL_PORT_2 _USART1 // ESP3D port PA9 PA10 diff --git a/TFT/src/User/Variants/pin_TFT24_V1_1.h b/TFT/src/User/Variants/pin_TFT24_V1_1.h index 28aefe5711..25c713d129 100644 --- a/TFT/src/User/Variants/pin_TFT24_V1_1.h +++ b/TFT/src/User/Variants/pin_TFT24_V1_1.h @@ -76,8 +76,8 @@ #define LCD_DATA_16BIT 1 #endif -// SERIAL_PORT: communicating with host (Marlin, RRF etc...) -// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc...) +// SERIAL_PORT: communicating with host (Marlin, RRF etc.) +// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc.) #ifndef SERIAL_PORT #define SERIAL_PORT _USART1 // default USART port //#define SERIAL_PORT_2 _USART2 diff --git a/TFT/src/User/Variants/pin_TFT35_V1_0.h b/TFT/src/User/Variants/pin_TFT35_V1_0.h index 2385563e9e..23c5c3b4b3 100644 --- a/TFT/src/User/Variants/pin_TFT35_V1_0.h +++ b/TFT/src/User/Variants/pin_TFT35_V1_0.h @@ -71,8 +71,8 @@ #define LCD_DATA_16BIT 0 #endif -// SERIAL_PORT: communicating with host (Marlin, RRF etc...) -// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc...) +// SERIAL_PORT: communicating with host (Marlin, RRF etc.) +// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc.) #ifndef SERIAL_PORT #define SERIAL_PORT _USART1 // default USART port //#define SERIAL_PORT_2 _USART2 diff --git a/TFT/src/User/Variants/pin_TFT35_V2_0.h b/TFT/src/User/Variants/pin_TFT35_V2_0.h index b58fcc1421..dc02975edb 100644 --- a/TFT/src/User/Variants/pin_TFT35_V2_0.h +++ b/TFT/src/User/Variants/pin_TFT35_V2_0.h @@ -76,8 +76,8 @@ #define LCD_DATA_16BIT 1 #endif -// SERIAL_PORT: communicating with host (Marlin, RRF etc...) -// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc...) +// SERIAL_PORT: communicating with host (Marlin, RRF etc.) +// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc.) #ifndef SERIAL_PORT #define SERIAL_PORT _USART1 // default USART port #define SERIAL_PORT_2 _USART2 diff --git a/TFT/src/User/Variants/pin_TFT35_V3_0.h b/TFT/src/User/Variants/pin_TFT35_V3_0.h index 34d38310c9..c6b68d9ce9 100644 --- a/TFT/src/User/Variants/pin_TFT35_V3_0.h +++ b/TFT/src/User/Variants/pin_TFT35_V3_0.h @@ -71,8 +71,8 @@ #define LCD_DATA_16BIT 1 #endif -// SERIAL_PORT: communicating with host (Marlin, RRF etc...) -// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc...) +// SERIAL_PORT: communicating with host (Marlin, RRF etc.) +// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc.) #ifndef SERIAL_PORT #define SERIAL_PORT _USART2 // default USART port #define SERIAL_PORT_2 _USART1 diff --git a/TFT/src/User/Variants/pin_TFT70_V3_0.h b/TFT/src/User/Variants/pin_TFT70_V3_0.h index ed50be0cb6..7f0028481d 100644 --- a/TFT/src/User/Variants/pin_TFT70_V3_0.h +++ b/TFT/src/User/Variants/pin_TFT70_V3_0.h @@ -87,8 +87,8 @@ #define LCD_DATA_16BIT 1 #endif -// SERIAL_PORT: communicating with host (Marlin, RRF etc...) -// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc...) +// SERIAL_PORT: communicating with host (Marlin, RRF etc.) +// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc.) #ifndef SERIAL_PORT #define SERIAL_PORT _USART2 // default USART port #define SERIAL_PORT_2 _USART1 diff --git a/TFT/src/User/Variants/pin_Template.h b/TFT/src/User/Variants/pin_Template.h index 31909464d4..41aa4cae7e 100644 --- a/TFT/src/User/Variants/pin_Template.h +++ b/TFT/src/User/Variants/pin_Template.h @@ -93,8 +93,8 @@ #define LCD_DATA_16BIT 1 #endif -// SERIAL_PORT: communicating with host (Marlin, RRF etc...) -// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc...) +// SERIAL_PORT: communicating with host (Marlin, RRF etc.) +// SERIAL_PORT_X: communicating with other controllers (OctoPrint, ESP3D, other UART Touch Screen etc.) #ifndef SERIAL_PORT #define SERIAL_PORT _USART1 // default USART port //#define SERIAL_PORT_2 _USART2 diff --git a/TFT/src/User/Variants/variants.h b/TFT/src/User/Variants/variants.h index 03c0fd1e11..fc6b7f594d 100644 --- a/TFT/src/User/Variants/variants.h +++ b/TFT/src/User/Variants/variants.h @@ -4,11 +4,11 @@ #include "../Configuration.h" /* -* hardware source -* TIM3 for Buzzer timer -* TIM6 for Neopixel RGB -* TIM7 for OS Timer -*/ + * hardware source + * TIM3 for Buzzer timer + * TIM6 for Neopixel RGB + * TIM7 for OS Timer + */ // Type of LCD driver, now support[RM68042, ILI9488, ILI9341, ILI9325, ST7789, HX8558, SSD1963]. #define RM68042 (1 << 0) @@ -89,7 +89,7 @@ #define _FONT_H(size) (size >> 8) #define _FONT_W(size) (size & 0xFF) -#define FONT_SIZE_NORMAL _FONT_SIZE(BYTE_HEIGHT, BYTE_WIDTH) // Normal font for common menu etc... -#define FONT_SIZE_LARGE _FONT_SIZE(LARGE_BYTE_HEIGHT, LARGE_BYTE_WIDTH) // Large font for numpad menu etc... +#define FONT_SIZE_NORMAL _FONT_SIZE(BYTE_HEIGHT, BYTE_WIDTH) // Normal font for common menu etc. +#define FONT_SIZE_LARGE _FONT_SIZE(LARGE_BYTE_HEIGHT, LARGE_BYTE_WIDTH) // Large font for numpad menu etc. #endif diff --git a/TFT/src/User/includes.h b/TFT/src/User/includes.h index 36f33b407e..d0517304c9 100644 --- a/TFT/src/User/includes.h +++ b/TFT/src/User/includes.h @@ -18,7 +18,7 @@ #include "os_timer.h" #include "SanityCheck.h" -// User/HAL/stm32f10x or // HAL/stm32f2_f4xx +// User/HAL/stm32f10x, HAL/stm32f2_f4xx, HAL/gd32f20x or HAL/gd32f30x #include "lcd_dma.h" #include "lcd.h" #include "Serial.h" diff --git a/TFT/src/User/my_misc.c b/TFT/src/User/my_misc.c index 0aa7d580e1..9c4575f34a 100644 --- a/TFT/src/User/my_misc.c +++ b/TFT/src/User/my_misc.c @@ -20,7 +20,7 @@ long map(long x, long in_min, long in_max, long out_min, long out_max) } // calculate CRC16 checksum -uint32_t calculateCRC16(const uint8_t *data, uint32_t length) +uint32_t calculateCRC16(const uint8_t * data, uint32_t length) { uint16_t crc = 0xFFFF; uint32_t i; @@ -42,7 +42,7 @@ uint32_t calculateCRC16(const uint8_t *data, uint32_t length) } // string convert to uint8, MSB ("2C" to 0x2C) -uint8_t string_2_uint8(const uint8_t *str) +uint8_t string_2_uint8(const uint8_t * str) { uint8_t rtv = 0; @@ -64,7 +64,7 @@ uint8_t string_2_uint8(const uint8_t *str) } // uint8 convert to string, MSB (0x2C to "2C") -uint8_t *uint8_2_string(uint8_t num, uint8_t *str) +uint8_t * uint8_2_string(uint8_t num, uint8_t * str) { for (unsigned char i = 0; i < 2; i++) { @@ -84,7 +84,7 @@ uint8_t *uint8_2_string(uint8_t num, uint8_t *str) } // string convert to uint32, MSB -uint32_t string_2_uint32(const uint8_t *str, const uint8_t bytes_num) +uint32_t string_2_uint32(const uint8_t * str, const uint8_t bytes_num) { uint32_t rtv = 0; @@ -98,7 +98,7 @@ uint32_t string_2_uint32(const uint8_t *str, const uint8_t bytes_num) } // uint32 convert to string, MSB -uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *str) +uint8_t * uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t * str) { for (uint8_t i = 0; i < bytes_num; i++) { @@ -112,7 +112,7 @@ uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *str) } // convert time to string with given formatting -void time_2_string(char *buf, char *str_format, uint32_t time) +void time_2_string(char * buf, char * str_format, uint32_t time) { uint8_t hour = HOURS(time); uint8_t min = MINUTES(time); @@ -123,9 +123,9 @@ void time_2_string(char *buf, char *str_format, uint32_t time) // light weight strtod() function without exponential support. // Convert string to double (without exponential support) -double strtod_ligth(char *str, char **endptr) +double strtod_ligth(char * str, char ** endptr) { - char *p = str; + char * p = str; double val = 0.0; int8_t sign = 1; uint32_t prec = 0; @@ -177,7 +177,7 @@ double strtod_ligth(char *str, char **endptr) // - copy "src" to "dest" for a maximum of "n-1" characters // - if null terminating character is found in "src" the rest in "dest" is padded with '\0' // - "dest" always ends with '\0' -void strncpy_pad(char *dest, const char *src, size_t n) +void strncpy_pad(char * dest, const char * src, size_t n) { // if "src" is not NULL, proceed first with the copy. // Otherwise, proceed only padding "dest" with '\0' @@ -197,7 +197,7 @@ void strncpy_pad(char *dest, const char *src, size_t n) // - copy "src" to "dest" for a maximum of "n-1" characters // - if null terminating character is found in "src" the copy stops there // - "dest" always ends with '\0' -void strncpy_no_pad(char *dest, const char *src, size_t n) +void strncpy_no_pad(char * dest, const char * src, size_t n) { // if "src" is not NULL, proceed with the copy if (src != NULL) diff --git a/TFT/src/User/my_misc.h b/TFT/src/User/my_misc.h index f60d5cdfff..6a5baf1a30 100644 --- a/TFT/src/User/my_misc.h +++ b/TFT/src/User/my_misc.h @@ -72,17 +72,17 @@ extern "C" { uint8_t inRange(int cur, int tag , int range); long map(long x, long in_min, long in_max, long out_min, long out_max); -uint32_t calculateCRC16(const uint8_t *data, uint32_t length); // calculate CRC16 checksum +uint32_t calculateCRC16(const uint8_t * data, uint32_t length); // calculate CRC16 checksum -uint8_t string_2_uint8(const uint8_t *str); // string convert to uint8, MSB ("2C" to 0x2C) -uint8_t *uint8_2_string(uint8_t num, uint8_t *str); // uint8 convert to string, MSB (0x2C to "2C") -uint32_t string_2_uint32(const uint8_t *str, const uint8_t bytes_num); // string convert to uint32, MSB -uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *str); // uint32 convert to string, MSB -void time_2_string(char *buf, char *str_format, uint32_t time); // convert time to string with given formatting +uint8_t string_2_uint8(const uint8_t * str); // string convert to uint8, MSB ("2C" to 0x2C) +uint8_t * uint8_2_string(uint8_t num, uint8_t * str); // uint8 convert to string, MSB (0x2C to "2C") +uint32_t string_2_uint32(const uint8_t * str, const uint8_t bytes_num); // string convert to uint32, MSB +uint8_t * uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t * str); // uint32 convert to string, MSB +void time_2_string(char * buf, char * str_format, uint32_t time); // convert time to string with given formatting -double strtod_ligth(char *str, char **endptr); // light weight strtod() function without exponential support -void strncpy_pad(char *dest, const char *src, size_t n); // light weight and safe strncpy() function with padding -void strncpy_no_pad(char *dest, const char *src, size_t n); // light weight and safe strncpy() function without padding +double strtod_ligth(char * str, char ** endptr); // light weight strtod() function without exponential support +void strncpy_pad(char * dest, const char * src, size_t n); // light weight and safe strncpy() function with padding +void strncpy_no_pad(char * dest, const char * src, size_t n); // light weight and safe strncpy() function without padding #ifdef __cplusplus } diff --git a/TFT/src/User/os_timer.c b/TFT/src/User/os_timer.c index 408e7aaf14..0ec3cce35e 100644 --- a/TFT/src/User/os_timer.c +++ b/TFT/src/User/os_timer.c @@ -81,14 +81,14 @@ void TIM7_IRQHandler(void) // task: task structure to be filled // time_ms: // -void OS_TaskInit(OS_TASK *task_t, uint32_t time_ms, FP_TASK function, void *para) +void OS_TaskInit(OS_TASK * task_t, uint32_t time_ms, FP_TASK function, void * para) { task_t->time_ms = time_ms; task_t->task = function; task_t->para = para; } -void OS_TaskLoop(OS_TASK *task_t) +void OS_TaskLoop(OS_TASK * task_t) { if (task_t->is_exist == 0) return; @@ -108,7 +108,7 @@ void OS_TaskLoop(OS_TASK *task_t) (*task_t->task)(task_t->para); } -void OS_TaskEnable(OS_TASK *task_t, uint8_t is_exec, uint8_t is_repeat) +void OS_TaskEnable(OS_TASK * task_t, uint8_t is_exec, uint8_t is_repeat) { task_t->is_exist =1; task_t->is_repeat = is_repeat; @@ -118,7 +118,7 @@ void OS_TaskEnable(OS_TASK *task_t, uint8_t is_exec, uint8_t is_repeat) (*task_t->task)(task_t->para); } -void OS_TaskDisable(OS_TASK *task_t) +void OS_TaskDisable(OS_TASK * task_t) { task_t->is_exist = 0; } diff --git a/TFT/src/User/os_timer.h b/TFT/src/User/os_timer.h index cf01698724..0a6ee60a3c 100644 --- a/TFT/src/User/os_timer.h +++ b/TFT/src/User/os_timer.h @@ -17,14 +17,14 @@ typedef struct uint16_t sec; // seconds } OS_COUNTER; -typedef void (*FP_TASK)(void *); +typedef void (* FP_TASK)(void *); typedef struct { uint32_t time_ms; uint32_t next_time; FP_TASK task; - void *para; + void * para; uint8_t is_exist; uint8_t is_repeat; } OS_TASK; @@ -38,10 +38,10 @@ static inline uint32_t OS_GetTimeMs(void) return os_counter.ms; } -void OS_TaskInit(OS_TASK *task, uint32_t time_ms, FP_TASK function, void *para); -void OS_TaskLoop(OS_TASK *task); -void OS_TaskEnable(OS_TASK *task, uint8_t is_exec, uint8_t is_repeat); -void OS_TaskDisable(OS_TASK *task); +void OS_TaskInit(OS_TASK * task, uint32_t time_ms, FP_TASK function, void * para); +void OS_TaskLoop(OS_TASK * task); +void OS_TaskEnable(OS_TASK * task, uint8_t is_exec, uint8_t is_repeat); +void OS_TaskDisable(OS_TASK * task); #ifdef __cplusplus } diff --git a/buildroot/boards/GD32F205VC_0x3000.json b/buildroot/boards/GD32F205VC_0x3000.json index bb27004e9e..6e930b9109 100644 --- a/buildroot/boards/GD32F205VC_0x3000.json +++ b/buildroot/boards/GD32F205VC_0x3000.json @@ -25,7 +25,7 @@ "arduino", "spl" ], - "name": "GD32F205VC (128k RAM, 256k - 12K Flash)", + "name": "GD32F205VC (128K RAM, 256K - 12K Flash)", "upload": { "disable_flushing": false, "maximum_ram_size": 131072, diff --git a/buildroot/boards/GD32F305VC_0x7000.json b/buildroot/boards/GD32F305VC_0x7000.json index cd21878ea7..fc98f18431 100644 --- a/buildroot/boards/GD32F305VC_0x7000.json +++ b/buildroot/boards/GD32F305VC_0x7000.json @@ -34,12 +34,12 @@ "frameworks": [ "spl" ], - "name": "GD32F305VC (64k RAM, 256k Flash)", + "name": "GD32F305VC (96K RAM, 256K - 28K Flash)", "upload": { "disable_flushing": false, "offset_address": "0x8007000", - "maximum_ram_size": 65536, - "maximum_size": 262144, + "maximum_ram_size": 98304, + "maximum_size": 233472, "protocol": "stlink", "protocols": [ "jlink", diff --git a/buildroot/boards/STM32F103VC_0x3000.json b/buildroot/boards/STM32F103VC_0x3000.json index e7dc35f085..d9a1aa5fd1 100644 --- a/buildroot/boards/STM32F103VC_0x3000.json +++ b/buildroot/boards/STM32F103VC_0x3000.json @@ -28,7 +28,7 @@ "cmsis", "stm32cube" ], - "name": "STM32F103VC (48k RAM. 256k - 12K Flash)", + "name": "STM32F103VC (48K RAM, 256K - 12K Flash)", "upload": { "disable_flushing": false, "offset_address": "0x8003000", diff --git a/buildroot/boards/STM32F103VC_0x6000.json b/buildroot/boards/STM32F103VC_0x6000.json index 0dfbf33967..9721dce127 100644 --- a/buildroot/boards/STM32F103VC_0x6000.json +++ b/buildroot/boards/STM32F103VC_0x6000.json @@ -28,7 +28,7 @@ "cmsis", "stm32cube" ], - "name": "STM32F103VC (48k RAM. 256k - 24K Flash)", + "name": "STM32F103VC (48K RAM, 256K - 24K Flash)", "upload": { "disable_flushing": false, "offset_address": "0x8006000", diff --git a/buildroot/boards/STM32F105RC_0x6000.json b/buildroot/boards/STM32F105RC_0x6000.json index 46cf13144b..bdfb87981a 100644 --- a/buildroot/boards/STM32F105RC_0x6000.json +++ b/buildroot/boards/STM32F105RC_0x6000.json @@ -28,7 +28,7 @@ "cmsis", "stm32cube" ], - "name": "STM32F105RC (64k RAM. 256k - 24K Flash)", + "name": "STM32F105RC (64K RAM, 256K - 24K Flash)", "upload": { "disable_flushing": false, "maximum_ram_size": 65536, diff --git a/buildroot/boards/STM32F107VC_0x0000.json b/buildroot/boards/STM32F107VC_0x0000.json index 5c8d5fc864..42cfd1b8e6 100644 --- a/buildroot/boards/STM32F107VC_0x0000.json +++ b/buildroot/boards/STM32F107VC_0x0000.json @@ -28,7 +28,7 @@ "cmsis", "stm32cube" ], - "name": "STM32F107VC (64k RAM. 256k Flash)", + "name": "STM32F107VC (64K RAM, 256K Flash)", "upload": { "disable_flushing": false, "maximum_ram_size": 65536, diff --git a/buildroot/boards/STM32F107VC_0x7000.json b/buildroot/boards/STM32F107VC_0x7000.json index 6b3bb5e148..a2a8682e20 100644 --- a/buildroot/boards/STM32F107VC_0x7000.json +++ b/buildroot/boards/STM32F107VC_0x7000.json @@ -28,7 +28,7 @@ "cmsis", "stm32cube" ], - "name": "STM32F107VC (64k RAM. 256k - 28K Flash)", + "name": "STM32F107VC (64K RAM, 256K - 28K Flash)", "upload": { "disable_flushing": false, "offset_address": "0x8007000", diff --git a/buildroot/boards/STM32F207VC_0x8000.json b/buildroot/boards/STM32F207VC_0x8000.json index 8235d80b21..1f96153d0e 100644 --- a/buildroot/boards/STM32F207VC_0x8000.json +++ b/buildroot/boards/STM32F207VC_0x8000.json @@ -28,7 +28,7 @@ "mbed", "stm32cube" ], - "name": "STM32F207VC (96k RAM. 256k - 32K Flash)", + "name": "STM32F207VC (96K RAM, 256K - 32K Flash)", "upload": { "offset_address": "0x8008000", "maximum_ram_size": 98304, diff --git a/buildroot/boards/STM32F407VE_0xC000.json b/buildroot/boards/STM32F407VE_0xC000.json index a52aef753f..6090a1b2d3 100644 --- a/buildroot/boards/STM32F407VE_0xC000.json +++ b/buildroot/boards/STM32F407VE_0xC000.json @@ -30,7 +30,7 @@ "stm32cube", "zephyr" ], - "name": "STM32F407VG (192k RAM. 512k - 48K Flash)", + "name": "STM32F407VG (192K RAM, 512K - 48K Flash)", "upload": { "offset_address": "0x800C000", "maximum_ram_size": 196608, diff --git a/buildroot/boards/STM32F407VG_0x8000.json b/buildroot/boards/STM32F407VG_0x8000.json index e20f7e0601..663d86fb5e 100644 --- a/buildroot/boards/STM32F407VG_0x8000.json +++ b/buildroot/boards/STM32F407VG_0x8000.json @@ -30,7 +30,7 @@ "stm32cube", "zephyr" ], - "name": "STM32F407VG (192k RAM. 1024k - 32K Flash)", + "name": "STM32F407VG (192K RAM, 1024K - 32K Flash)", "upload": { "offset_address": "0x8008000", "maximum_ram_size": 196608, diff --git a/buildroot/ldscripts/gd32f20xxC_0x3000_iap.ld b/buildroot/ldscripts/gd32f20xxC_0x3000_iap.ld index c58154867e..36c0bf1b8c 100644 --- a/buildroot/ldscripts/gd32f20xxC_0x3000_iap.ld +++ b/buildroot/ldscripts/gd32f20xxC_0x3000_iap.ld @@ -8,20 +8,22 @@ /* Entry Point */ ENTRY(Reset_Handler) -/* Highest address of the user mode stack */ -_estack = 0x20020000; /* end of RAM */ /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x200; /* required amount of heap */ -_Min_Stack_Size = 0x400; /* required amount of stack */ +_Min_Heap_Size = 0x200; /* required amount of heap */ +_Min_Stack_Size = 0x400; /* required amount of stack */ /* Specify the memory areas */ MEMORY { - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K - CCRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 0K - FLASH (rx) : ORIGIN = 0x8003000, LENGTH = 256K - 12K + FLASH (rx) : ORIGIN = 0x8003000, LENGTH = 256K - 12K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K + CCRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 0K } +/* Highest address of the user mode stack */ +/* _estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ +_estack = 0x20020000; /* end of RAM */ + /* Define output sections */ SECTIONS { @@ -152,7 +154,6 @@ SECTIONS _ccram_end_data = .; /* define a global symbol at data end */ } >CCRAM AT> FLASH - /* Remove information from the standard libraries */ /DISCARD/ : { @@ -163,5 +164,3 @@ SECTIONS .ARM.attributes 0 : { *(.ARM.attributes) } } - - diff --git a/buildroot/ldscripts/gd32f30xxC_0x7000_iap.ld b/buildroot/ldscripts/gd32f30xxC_0x7000_iap.ld index 5df00e5b2a..602716c5b2 100644 --- a/buildroot/ldscripts/gd32f30xxC_0x7000_iap.ld +++ b/buildroot/ldscripts/gd32f30xxC_0x7000_iap.ld @@ -16,13 +16,13 @@ _Min_Stack_Size = 0x2000; /* required amount of stack */ MEMORY { FLASH (rx) : ORIGIN = 0x08007000, LENGTH = 256K - 28K - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K } /* Highest address of the user mode stack */ -/* _estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16 ,8); /* end of RAM */ -_estack = 0x20010000; /* end of RAM */ +/* _estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ +_estack = 0x20018000; /* end of RAM */ /* Define output sections */ SECTIONS @@ -53,7 +53,6 @@ SECTIONS _etext = .; /* define a global symbols at end of code */ } >FLASH - .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH .ARM : { __exidx_start = .; @@ -145,5 +144,3 @@ SECTIONS libgcc.a ( * ) } } - - diff --git a/buildroot/ldscripts/stm32f103xC_0x3000_iap.ld b/buildroot/ldscripts/stm32f103xC_0x3000_iap.ld index 51cb1c05b0..8b5ea30060 100644 --- a/buildroot/ldscripts/stm32f103xC_0x3000_iap.ld +++ b/buildroot/ldscripts/stm32f103xC_0x3000_iap.ld @@ -32,12 +32,9 @@ /* Entry Point */ ENTRY(Reset_Handler) -/* Highest address of the user mode stack */ -_estack = 0x2000C000; /* end of 48K RAM */ - /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x5000; /* required amount of heap */ -_Min_Stack_Size = 0x1000; /* required amount of stack */ +_Min_Heap_Size = 0x5000; /* required amount of heap */ +_Min_Stack_Size = 0x1000; /* required amount of stack */ /* Specify the memory areas */ MEMORY @@ -47,6 +44,10 @@ MEMORY MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K } +/* Highest address of the user mode stack */ +/* _estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ +_estack = 0x2000C000; /* end of 48K RAM */ + /* Define output sections */ SECTIONS { @@ -77,12 +78,12 @@ SECTIONS } >FLASH - .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH - .ARM : { + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH + .ARM : { __exidx_start = .; - *(.ARM.exidx*) - __exidx_end = .; - } >FLASH + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH .ARM.attributes : { *(.ARM.attributes) } > FLASH diff --git a/buildroot/ldscripts/stm32f103xC_0x6000_iap.ld b/buildroot/ldscripts/stm32f103xC_0x6000_iap.ld index 0497823bb5..26f1127654 100644 --- a/buildroot/ldscripts/stm32f103xC_0x6000_iap.ld +++ b/buildroot/ldscripts/stm32f103xC_0x6000_iap.ld @@ -15,7 +15,7 @@ ** ** Environment : Atollic TrueSTUDIO(R) ** -** Distribution: The file is distributed �as is,� without any warranty +** Distribution: The file is distributed “as is,” without any warranty ** of any kind. ** ** (c)Copyright Atollic AB. @@ -32,12 +32,9 @@ /* Entry Point */ ENTRY(Reset_Handler) -/* Highest address of the user mode stack */ -_estack = 0x2000C000; /* end of 48K RAM */ - /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x5000; /* required amount of heap */ -_Min_Stack_Size = 0x1000; /* required amount of stack */ +_Min_Heap_Size = 0x5000; /* required amount of heap */ +_Min_Stack_Size = 0x1000; /* required amount of stack */ /* Specify the memory areas */ MEMORY @@ -47,6 +44,10 @@ MEMORY MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K } +/* Highest address of the user mode stack */ +/* _estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ +_estack = 0x2000C000; /* end of 48K RAM */ + /* Define output sections */ SECTIONS { @@ -77,12 +78,12 @@ SECTIONS } >FLASH - .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH - .ARM : { + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH + .ARM : { __exidx_start = .; - *(.ARM.exidx*) - __exidx_end = .; - } >FLASH + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH .ARM.attributes : { *(.ARM.attributes) } > FLASH diff --git a/buildroot/ldscripts/stm32f105xC_0x6000_iap.ld b/buildroot/ldscripts/stm32f105xC_0x6000_iap.ld index f39c12ee81..e6787bf6bb 100644 --- a/buildroot/ldscripts/stm32f105xC_0x6000_iap.ld +++ b/buildroot/ldscripts/stm32f105xC_0x6000_iap.ld @@ -15,7 +15,7 @@ ** ** Environment : Atollic TrueSTUDIO(R) ** -** Distribution: The file is distributed �as is,� without any warranty +** Distribution: The file is distributed “as is,” without any warranty ** of any kind. ** ** (c)Copyright Atollic AB. @@ -33,8 +33,8 @@ ENTRY(Reset_Handler) /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x5000; /* required amount of heap */ -_Min_Stack_Size = 0x2000; /* required amount of stack */ +_Min_Heap_Size = 0x5000; /* required amount of heap */ +_Min_Stack_Size = 0x2000; /* required amount of stack */ /* Specify the memory areas */ MEMORY @@ -45,7 +45,7 @@ MEMORY } /* Highest address of the user mode stack */ -_estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16 ,8); /* end of RAM */ +_estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ /* Define output sections */ SECTIONS @@ -77,12 +77,12 @@ SECTIONS } >FLASH - .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH - .ARM : { + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH + .ARM : { __exidx_start = .; - *(.ARM.exidx*) - __exidx_end = .; - } >FLASH + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH .ARM.attributes : { *(.ARM.attributes) } > FLASH @@ -167,4 +167,4 @@ SECTIONS libm.a ( * ) libgcc.a ( * ) } -} \ No newline at end of file +} diff --git a/buildroot/ldscripts/stm32f107xC_0x0000_iap.ld b/buildroot/ldscripts/stm32f107xC_0x0000_iap.ld index 40768975c3..fd337c0f2e 100644 --- a/buildroot/ldscripts/stm32f107xC_0x0000_iap.ld +++ b/buildroot/ldscripts/stm32f107xC_0x0000_iap.ld @@ -15,7 +15,7 @@ ** ** Environment : Atollic TrueSTUDIO(R) ** -** Distribution: The file is distributed �as is,� without any warranty +** Distribution: The file is distributed “as is,” without any warranty ** of any kind. ** ** (c)Copyright Atollic AB. @@ -33,8 +33,8 @@ ENTRY(Reset_Handler) /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x5000; /* required amount of heap */ -_Min_Stack_Size = 0x2000; /* required amount of stack */ +_Min_Heap_Size = 0x5000; /* required amount of heap */ +_Min_Stack_Size = 0x2000; /* required amount of stack */ /* Specify the memory areas */ MEMORY @@ -45,7 +45,7 @@ MEMORY } /* Highest address of the user mode stack */ -_estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16 ,8); /* end of RAM */ +_estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ /* Define output sections */ SECTIONS @@ -77,12 +77,12 @@ SECTIONS } >FLASH - .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH - .ARM : { + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH + .ARM : { __exidx_start = .; - *(.ARM.exidx*) - __exidx_end = .; - } >FLASH + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH .ARM.attributes : { *(.ARM.attributes) } > FLASH @@ -167,4 +167,4 @@ SECTIONS libm.a ( * ) libgcc.a ( * ) } -} \ No newline at end of file +} diff --git a/buildroot/ldscripts/stm32f107xC_0x7000_iap.ld b/buildroot/ldscripts/stm32f107xC_0x7000_iap.ld index 8f71e51d42..8c85d18424 100644 --- a/buildroot/ldscripts/stm32f107xC_0x7000_iap.ld +++ b/buildroot/ldscripts/stm32f107xC_0x7000_iap.ld @@ -15,7 +15,7 @@ ** ** Environment : Atollic TrueSTUDIO(R) ** -** Distribution: The file is distributed �as is,� without any warranty +** Distribution: The file is distributed “as is,” without any warranty ** of any kind. ** ** (c)Copyright Atollic AB. @@ -33,8 +33,8 @@ ENTRY(Reset_Handler) /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x5000; /* required amount of heap */ -_Min_Stack_Size = 0x2000; /* required amount of stack */ +_Min_Heap_Size = 0x5000; /* required amount of heap */ +_Min_Stack_Size = 0x2000; /* required amount of stack */ /* Specify the memory areas */ MEMORY @@ -45,7 +45,7 @@ MEMORY } /* Highest address of the user mode stack */ -_estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16 ,8); /* end of RAM */ +_estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ /* Define output sections */ SECTIONS @@ -77,12 +77,12 @@ SECTIONS } >FLASH - .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH - .ARM : { + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH + .ARM : { __exidx_start = .; - *(.ARM.exidx*) - __exidx_end = .; - } >FLASH + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH .ARM.attributes : { *(.ARM.attributes) } > FLASH @@ -167,4 +167,4 @@ SECTIONS libm.a ( * ) libgcc.a ( * ) } -} \ No newline at end of file +} diff --git a/buildroot/ldscripts/stm32f2xxxC_0x8000_iap.ld b/buildroot/ldscripts/stm32f2xxxC_0x8000_iap.ld index ec5d3231af..15e0d56cb0 100644 --- a/buildroot/ldscripts/stm32f2xxxC_0x8000_iap.ld +++ b/buildroot/ldscripts/stm32f2xxxC_0x8000_iap.ld @@ -9,18 +9,18 @@ ENTRY(Reset_Handler) /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x5000; /* required amount of heap */ -_Min_Stack_Size = 0x1000; /* required amount of stack */ +_Min_Heap_Size = 0x5000; /* required amount of heap */ +_Min_Stack_Size = 0x1000; /* required amount of stack */ /* Specify the memory areas */ MEMORY { - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K - FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 256K - 32K + FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 256K - 32K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K } /* Highest address of the user mode stack */ -_estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16 ,8); /* end of RAM */ +_estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ /* Define output sections */ SECTIONS @@ -129,8 +129,6 @@ SECTIONS . = ALIGN(4); } >RAM - - /* Remove information from the standard libraries */ /DISCARD/ : { @@ -141,5 +139,3 @@ SECTIONS .ARM.attributes 0 : { *(.ARM.attributes) } } - - diff --git a/buildroot/ldscripts/stm32f4xxxE_0x0000_iap.ld b/buildroot/ldscripts/stm32f4xxxE_0x0000_iap.ld index 3a777b90a2..c016c36ff9 100644 --- a/buildroot/ldscripts/stm32f4xxxE_0x0000_iap.ld +++ b/buildroot/ldscripts/stm32f4xxxE_0x0000_iap.ld @@ -1,7 +1,6 @@ /* ***************************************************************************** ** - ** File : stm32_flash.ld ** ** Abstract : Linker script for STM32F407VE Device with @@ -16,7 +15,7 @@ ** ** Environment : Atollic TrueSTUDIO(R) ** -** Distribution: The file is distributed as is, without any warranty +** Distribution: The file is distributed �as is,� without any warranty ** of any kind. ** ** (c)Copyright Atollic AB. @@ -31,20 +30,22 @@ /* Entry Point */ ENTRY(Reset_Handler) -/* Highest address of the user mode stack */ -_estack = 0x20020000; /* end of RAM */ /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x5000; /* required amount of heap */ -_Min_Stack_Size = 0x1000; /* required amount of stack */ +_Min_Heap_Size = 0x5000; /* required amount of heap */ +_Min_Stack_Size = 0x1000; /* required amount of stack */ /* Specify the memory areas */ MEMORY { -RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K -CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K -FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 512K + FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 512K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K + CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K } +/* Highest address of the user mode stack */ +/* _estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ +_estack = 0x20020000; /* end of RAM */ + /* Define output sections */ SECTIONS { @@ -144,7 +145,6 @@ SECTIONS _eccmram = .; /* create a global symbol at ccmram end */ } >CCMRAM AT> FLASH - /* Uninitialized data section */ . = ALIGN(4); .bss : @@ -172,8 +172,6 @@ SECTIONS . = ALIGN(4); } >RAM - - /* Remove information from the standard libraries */ /DISCARD/ : { @@ -184,5 +182,3 @@ SECTIONS .ARM.attributes 0 : { *(.ARM.attributes) } } - - diff --git a/buildroot/ldscripts/stm32f4xxxE_0xC000_iap.ld b/buildroot/ldscripts/stm32f4xxxE_0xC000_iap.ld index 8f16fba9c4..4cc4b53b73 100644 --- a/buildroot/ldscripts/stm32f4xxxE_0xC000_iap.ld +++ b/buildroot/ldscripts/stm32f4xxxE_0xC000_iap.ld @@ -1,7 +1,6 @@ /* ***************************************************************************** ** - ** File : stm32_flash.ld ** ** Abstract : Linker script for STM32F407VE Device with @@ -16,7 +15,7 @@ ** ** Environment : Atollic TrueSTUDIO(R) ** -** Distribution: The file is distributed as is, without any warranty +** Distribution: The file is distributed �as is,� without any warranty ** of any kind. ** ** (c)Copyright Atollic AB. @@ -31,20 +30,22 @@ /* Entry Point */ ENTRY(Reset_Handler) -/* Highest address of the user mode stack */ -_estack = 0x20020000; /* end of RAM */ /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x5000; /* required amount of heap */ -_Min_Stack_Size = 0x1000; /* required amount of stack */ +_Min_Heap_Size = 0x5000; /* required amount of heap */ +_Min_Stack_Size = 0x1000; /* required amount of stack */ /* Specify the memory areas */ MEMORY { -RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K -CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K -FLASH (rx) : ORIGIN = 0x800C000, LENGTH = 512K - 48K + FLASH (rx) : ORIGIN = 0x800C000, LENGTH = 512K - 48K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K + CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K } +/* Highest address of the user mode stack */ +/* _estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ +_estack = 0x20020000; /* end of RAM */ + /* Define output sections */ SECTIONS { @@ -144,7 +145,6 @@ SECTIONS _eccmram = .; /* create a global symbol at ccmram end */ } >CCMRAM AT> FLASH - /* Uninitialized data section */ . = ALIGN(4); .bss : @@ -172,8 +172,6 @@ SECTIONS . = ALIGN(4); } >RAM - - /* Remove information from the standard libraries */ /DISCARD/ : { @@ -184,5 +182,3 @@ SECTIONS .ARM.attributes 0 : { *(.ARM.attributes) } } - - diff --git a/buildroot/ldscripts/stm32f4xxxG_0x8000_iap.ld b/buildroot/ldscripts/stm32f4xxxG_0x8000_iap.ld index 9760d49d18..9d493e50eb 100644 --- a/buildroot/ldscripts/stm32f4xxxG_0x8000_iap.ld +++ b/buildroot/ldscripts/stm32f4xxxG_0x8000_iap.ld @@ -1,7 +1,6 @@ /* ***************************************************************************** ** - ** File : stm32_flash.ld ** ** Abstract : Linker script for STM32F407VG Device with @@ -16,7 +15,7 @@ ** ** Environment : Atollic TrueSTUDIO(R) ** -** Distribution: The file is distributed as is, without any warranty +** Distribution: The file is distributed �as is,� without any warranty ** of any kind. ** ** (c)Copyright Atollic AB. @@ -31,20 +30,22 @@ /* Entry Point */ ENTRY(Reset_Handler) -/* Highest address of the user mode stack */ -_estack = 0x20020000; /* end of RAM */ /* Generate a link error if heap and stack don't fit into RAM */ -_Min_Heap_Size = 0x5000; /* required amount of heap */ -_Min_Stack_Size = 0x1000; /* required amount of stack */ +_Min_Heap_Size = 0x5000; /* required amount of heap */ +_Min_Stack_Size = 0x1000; /* required amount of stack */ /* Specify the memory areas */ MEMORY { -RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K -CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K -FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 1024K - 32K + FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 1024K - 32K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K + CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K } +/* Highest address of the user mode stack */ +/* _estack = ALIGN(ORIGIN(RAM) + LENGTH(RAM) - 16, 8); /* end of RAM */ +_estack = 0x20020000; /* end of RAM */ + /* Define output sections */ SECTIONS { @@ -144,7 +145,6 @@ SECTIONS _eccmram = .; /* create a global symbol at ccmram end */ } >CCMRAM AT> FLASH - /* Uninitialized data section */ . = ALIGN(4); .bss : @@ -172,8 +172,6 @@ SECTIONS . = ALIGN(4); } >RAM - - /* Remove information from the standard libraries */ /DISCARD/ : { @@ -184,5 +182,3 @@ SECTIONS .ARM.attributes 0 : { *(.ARM.attributes) } } - - diff --git a/coding_standard.md b/coding_standard.md index 9549316a5c..f4aae44f5a 100644 --- a/coding_standard.md +++ b/coding_standard.md @@ -228,7 +228,7 @@ void parseData(uint8_t * data) - Use 2 spaces and don’t use tabs. - All block elements should increase the indentation level, including #if blocks and other non-brace compiler blocks: ```C -void testFunction() +void testFunction(void) { if (variableA == 0) {