Эх сурвалжийг харах

Switch to 'Black STM32F407ZG Pro Development Board'.

Vladimir N. Shilov 2 жил өмнө
parent
commit
24cce01d1e

+ 1 - 1
.vscode/c_cpp_properties.json

@@ -4,7 +4,7 @@
         {
             "name": "Accum-Tester",
             "defines": [
-                "STM32F103xx="
+                "STM32F407xx="
             ],
             "includePath": [
                 "${workspaceRoot}/inc",

+ 14 - 16
Makefile

@@ -5,7 +5,7 @@
 
 # Compiler options here.
 ifeq ($(USE_OPT),)
-  USE_OPT = -O3 -ggdb -fomit-frame-pointer -falign-functions=16
+  USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16
 endif
 
 # C specific options here (added to USE_OPT).
@@ -25,7 +25,7 @@ endif
 
 # Linker extra options here.
 ifeq ($(USE_LDOPT),)
-  USE_LDOPT = -lm
+  USE_LDOPT = 
 endif
 
 # Enable this if you want link time optimizations (LTO).
@@ -86,23 +86,22 @@ endif
 PROJECT = acc-tst
 
 # Target settings.
-MCU  = cortex-m3
+MCU  = cortex-m4
 
 # Imported source files and paths.
 CHIBIOS  := C:/MCU/ChibiOS_21.11.2
 CONFDIR  := ./cfg
 BUILDDIR := ./build
-DEPDIR   := ./.dep
+DEPDIR   := $(BUILDDIR)/.dep
 
 # Licensing files.
 include $(CHIBIOS)/os/license/license.mk
 # Startup files.
-include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32f1xx.mk
+include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32f4xx.mk
 # HAL-OSAL files (optional).
 include $(CHIBIOS)/os/hal/hal.mk
-include $(CHIBIOS)/os/hal/ports/STM32/STM32F1xx/platform.mk
-#include $(CHIBIOS)/os/hal/boards/ST_NUCLEO64_F103RB/board.mk
-include board/board.mk
+include $(CHIBIOS)/os/hal/ports/STM32/STM32F4xx/platform.mk
+include ./board/board.mk
 include $(CHIBIOS)/os/hal/osal/rt-nil/osal.mk
 # RTOS files (optional).
 include $(CHIBIOS)/os/rt/rt.mk
@@ -116,24 +115,24 @@ include $(CHIBIOS)/os/hal/lib/streams/streams.mk
 # µGFX
 GFXLIB = C:/MCU/uGFX
 include $(GFXLIB)/gfx.mk
-include $(GFXLIB)/drivers/gdisp/LGDP4532/driver.mk       # your board
-include $(GFXLIB)/drivers/ginput/toggle/Pal/driver.mk    # your button keys
-include $(GFXLIB)/drivers/ginput/touch/ADS7843/driver.mk # your touch panel
+include $(GFXLIB)/drivers/gdisp/ILI9341/driver.mk		# your board
+include $(GFXLIB)/drivers/ginput/toggle/Pal/driver.mk		# your button keys
+include $(GFXLIB)/drivers/ginput/touch/ADS7843/driver.mk	# your touch panel
 
 # Define linker script file here
-LDSCRIPT= $(STARTUPLD)/STM32F103xE.ld
+LDSCRIPT= $(STARTUPLD)/STM32F407xG.ld
 
 # C sources that can be compiled in ARM or THUMB mode depending on the global
 # setting.
 CSRC =	$(ALLCSRC) \
 	$(GFXSRC) \
-	src/INA3221.c \
 	src/buttons.c \
+	src/INA3221.c \
 	src/main.c
 
 # C++ sources that can be compiled in ARM or THUMB mode depending on the global
 # setting.
-CPPSRC = $(ALLCPPSRC) \
+CPPSRC = $(ALLCPPSRC)
 
 # List ASM source files here.
 ASMSRC = $(ALLASMSRC)
@@ -159,7 +158,7 @@ CPPWARN = -Wall -Wextra -Wundef
 #
 
 # List all user C define here, like -D_DEBUG=1
-UDEFS = 
+UDEFS =
 
 # Define ASM defines here
 UADEFS =
@@ -194,7 +193,6 @@ include $(RULESPATH)/rules.mk
 #
 flash: all
 	@flash.cmd
-
 #
 # Custom rules
 ##############################################################################

+ 196 - 29
board/board.c

@@ -14,56 +14,223 @@
     limitations under the License.
 */
 
+/*
+ * This file has been automatically generated using ChibiStudio board
+ * generator plugin. Do not edit manually.
+ */
+
 #include "hal.h"
+#include "stm32_gpio.h"
+
+/*===========================================================================*/
+/* Driver local definitions.                                                 */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported variables.                                                */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local variables and types.                                         */
+/*===========================================================================*/
+
+/**
+ * @brief   Type of STM32 GPIO port setup.
+ */
+typedef struct {
+  uint32_t              moder;
+  uint32_t              otyper;
+  uint32_t              ospeedr;
+  uint32_t              pupdr;
+  uint32_t              odr;
+  uint32_t              afrl;
+  uint32_t              afrh;
+} gpio_setup_t;
+
+/**
+ * @brief   Type of STM32 GPIO initialization data.
+ */
+typedef struct {
+#if STM32_HAS_GPIOA || defined(__DOXYGEN__)
+  gpio_setup_t          PAData;
+#endif
+#if STM32_HAS_GPIOB || defined(__DOXYGEN__)
+  gpio_setup_t          PBData;
+#endif
+#if STM32_HAS_GPIOC || defined(__DOXYGEN__)
+  gpio_setup_t          PCData;
+#endif
+#if STM32_HAS_GPIOD || defined(__DOXYGEN__)
+  gpio_setup_t          PDData;
+#endif
+#if STM32_HAS_GPIOE || defined(__DOXYGEN__)
+  gpio_setup_t          PEData;
+#endif
+#if STM32_HAS_GPIOF || defined(__DOXYGEN__)
+  gpio_setup_t          PFData;
+#endif
+#if STM32_HAS_GPIOG || defined(__DOXYGEN__)
+  gpio_setup_t          PGData;
+#endif
+#if STM32_HAS_GPIOH || defined(__DOXYGEN__)
+  gpio_setup_t          PHData;
+#endif
+} gpio_config_t;
 
 /**
- * @brief   PAL setup.
- * @details Digital I/O ports static configuration as defined in @p board.h.
- *          This variable is used by the HAL when initializing the PAL driver.
+ * @brief   STM32 GPIO static initialization data.
  */
-#if HAL_USE_PAL || defined(__DOXYGEN__)
-const PALConfig pal_default_config =
-{
-  {VAL_GPIOAODR, VAL_GPIOACRL, VAL_GPIOACRH},
-  {VAL_GPIOBODR, VAL_GPIOBCRL, VAL_GPIOBCRH},
-  {VAL_GPIOCODR, VAL_GPIOCCRL, VAL_GPIOCCRH},
-  {VAL_GPIODODR, VAL_GPIODCRL, VAL_GPIODCRH},
-  {VAL_GPIOEODR, VAL_GPIOECRL, VAL_GPIOECRH},
-  {VAL_GPIOFODR, VAL_GPIOFCRL, VAL_GPIOFCRH},
-  {VAL_GPIOGODR, VAL_GPIOGCRL, VAL_GPIOGCRH},
+static const gpio_config_t gpio_default_config = {
+#if STM32_HAS_GPIOA
+  {VAL_GPIOA_MODER, VAL_GPIOA_OTYPER, VAL_GPIOA_OSPEEDR, VAL_GPIOA_PUPDR,
+   VAL_GPIOA_ODR,   VAL_GPIOA_AFRL,   VAL_GPIOA_AFRH},
+#endif
+#if STM32_HAS_GPIOB
+  {VAL_GPIOB_MODER, VAL_GPIOB_OTYPER, VAL_GPIOB_OSPEEDR, VAL_GPIOB_PUPDR,
+   VAL_GPIOB_ODR,   VAL_GPIOB_AFRL,   VAL_GPIOB_AFRH},
+#endif
+#if STM32_HAS_GPIOC
+  {VAL_GPIOC_MODER, VAL_GPIOC_OTYPER, VAL_GPIOC_OSPEEDR, VAL_GPIOC_PUPDR,
+   VAL_GPIOC_ODR,   VAL_GPIOC_AFRL,   VAL_GPIOC_AFRH},
+#endif
+#if STM32_HAS_GPIOD
+  {VAL_GPIOD_MODER, VAL_GPIOD_OTYPER, VAL_GPIOD_OSPEEDR, VAL_GPIOD_PUPDR,
+   VAL_GPIOD_ODR,   VAL_GPIOD_AFRL,   VAL_GPIOD_AFRH},
+#endif
+#if STM32_HAS_GPIOE
+  {VAL_GPIOE_MODER, VAL_GPIOE_OTYPER, VAL_GPIOE_OSPEEDR, VAL_GPIOE_PUPDR,
+   VAL_GPIOE_ODR,   VAL_GPIOE_AFRL,   VAL_GPIOE_AFRH},
+#endif
+#if STM32_HAS_GPIOF
+  {VAL_GPIOF_MODER, VAL_GPIOF_OTYPER, VAL_GPIOF_OSPEEDR, VAL_GPIOF_PUPDR,
+   VAL_GPIOF_ODR,   VAL_GPIOF_AFRL,   VAL_GPIOF_AFRH},
+#endif
+#if STM32_HAS_GPIOG
+  {VAL_GPIOG_MODER, VAL_GPIOG_OTYPER, VAL_GPIOG_OSPEEDR, VAL_GPIOG_PUPDR,
+   VAL_GPIOG_ODR,   VAL_GPIOG_AFRL,   VAL_GPIOG_AFRH},
+#endif
+#if STM32_HAS_GPIOH
+  {VAL_GPIOH_MODER, VAL_GPIOH_OTYPER, VAL_GPIOH_OSPEEDR, VAL_GPIOH_PUPDR,
+   VAL_GPIOH_ODR,   VAL_GPIOH_AFRL,   VAL_GPIOH_AFRH},
+#endif
 };
+
+/*===========================================================================*/
+/* Driver local functions.                                                   */
+/*===========================================================================*/
+
+static void gpio_init(stm32_gpio_t *gpiop, const gpio_setup_t *config) {
+
+  gpiop->OTYPER  = config->otyper;
+  gpiop->OSPEEDR = config->ospeedr;
+  gpiop->PUPDR   = config->pupdr;
+  gpiop->ODR     = config->odr;
+  gpiop->AFRL    = config->afrl;
+  gpiop->AFRH    = config->afrh;
+  gpiop->MODER   = config->moder;
+}
+
+static void stm32_gpio_init(void) {
+
+  /* Enabling GPIO-related clocks, the mask comes from the
+     registry header file.*/
+  rccResetAHB1(STM32_GPIO_EN_MASK);
+  rccEnableAHB1(STM32_GPIO_EN_MASK, true);
+
+  /* Initializing all the defined GPIO ports.*/
+#if STM32_HAS_GPIOA
+  gpio_init(GPIOA, &gpio_default_config.PAData);
+#endif
+#if STM32_HAS_GPIOB
+  gpio_init(GPIOB, &gpio_default_config.PBData);
+#endif
+#if STM32_HAS_GPIOC
+  gpio_init(GPIOC, &gpio_default_config.PCData);
+#endif
+#if STM32_HAS_GPIOD
+  gpio_init(GPIOD, &gpio_default_config.PDData);
+#endif
+#if STM32_HAS_GPIOE
+  gpio_init(GPIOE, &gpio_default_config.PEData);
 #endif
+#if STM32_HAS_GPIOF
+  gpio_init(GPIOF, &gpio_default_config.PFData);
+#endif
+#if STM32_HAS_GPIOG
+  gpio_init(GPIOG, &gpio_default_config.PGData);
+#endif
+#if STM32_HAS_GPIOH
+  gpio_init(GPIOH, &gpio_default_config.PHData);
+#endif
+}
 
-/*
- * Early initialization code.
- * This initialization must be performed just after stack setup and before
- * any other initialization.
+/*===========================================================================*/
+/* Driver interrupt handlers.                                                */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported functions.                                                */
+/*===========================================================================*/
+
+/**
+ * @brief   Early initialization code.
+ * @details GPIO ports and system clocks are initialized before everything
+ *          else.
  */
 void __early_init(void) {
+
+  stm32_gpio_init();
   stm32_clock_init();
 }
 
-#if HAL_USE_MMC_SPI
-/* Board-related functions related to the MMC_SPI driver.*/
+#if HAL_USE_SDC || defined(__DOXYGEN__)
+/**
+ * @brief   SDC card detection.
+ */
+bool sdc_lld_is_card_inserted(SDCDriver *sdcp) {
+
+  (void)sdcp;
+  /* TODO: Fill the implementation.*/
+  return true;
+}
+
+/**
+ * @brief   SDC card write protection detection.
+ */
+bool sdc_lld_is_write_protected(SDCDriver *sdcp) {
+
+  (void)sdcp;
+  /* TODO: Fill the implementation.*/
+  return false;
+}
+#endif /* HAL_USE_SDC */
+
+#if HAL_USE_MMC_SPI || defined(__DOXYGEN__)
+/**
+ * @brief   MMC_SPI card detection.
+ */
 bool mmc_lld_is_card_inserted(MMCDriver *mmcp) {
+
   (void)mmcp;
-  return palReadPad(GPIOC, GPIOC_MMCCP);
+  /* TODO: Fill the implementation.*/
+  return true;
 }
 
+/**
+ * @brief   MMC_SPI card write protection detection.
+ */
 bool mmc_lld_is_write_protected(MMCDriver *mmcp) {
+
   (void)mmcp;
-  return !palReadPad(GPIOC, GPIOC_MMCWP);
+  /* TODO: Fill the implementation.*/
+  return false;
 }
 #endif
 
-/*
- * Board-specific initialization code.
+/**
+ * @brief   Board-specific initialization code.
+ * @todo    Add your board-specific code, if any.
  */
 void boardInit(void) {
-  /*
-   * Pin remap, RM0008, 9.3, page 175.
-   * Îòêëþ÷èëè JTAG, SWD âêëþ÷åí (9.3.5/176)
-   * I2C to PB8/PB9 (9.3.9/181)
-   */
-  AFIO->MAPR |= (AFIO_MAPR_SWJ_CFG_JTAGDISABLE | AFIO_MAPR_I2C1_REMAP);
+
 }

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 1311 - 182
board/board.h


+ 2 - 2
board/board.mk

@@ -1,8 +1,8 @@
 # List of all the board related files.
-BOARDSRC = board/board.c
+BOARDSRC = ./board/board.c
 
 # Required include directories
-BOARDINC = board
+BOARDINC = ./board
 
 # Shared variables
 ALLCSRC += $(BOARDSRC)

+ 136 - 0
board/board_ILI9341.h

@@ -0,0 +1,136 @@
+/*
+ * This file is subject to the terms of the GFX License. If a copy of
+ * the license was not distributed with this file, you can obtain one at:
+ *
+ *              http://ugfx.io/license.html
+ */
+
+/**
+ * @file    boards/addons/gdisp/board_ILI9341_stm32f4xx.h
+ * @brief   GDISP Graphic Driver subsystem board SPI interface for the ILI9341 display.
+ *
+ * @note  This file contains a mix of hardware specific and operating system specific
+ *      code. You will need to change it for your CPU and/or operating system.
+ */
+
+#ifndef _GDISP_LLD_BOARD_H
+#define _GDISP_LLD_BOARD_H
+
+// For a multiple display configuration we would put all this in a structure and then
+//  set g->board to that structure.
+
+// Use Bank1.sector4 of NOR/SRAM, address bit HADDR[27,26]=11
+// A6 as data command distinguishing line
+// Note that STM32 will be shifted to the right by one bit during setting! 111 1110=0x7E
+#define GDISP_REG              (*((volatile uint16_t *) 0x6C000000)) /* RS = 0 */
+#define GDISP_RAM              (*((volatile uint16_t *) 0x6C000080)) /* RS = 1 */
+
+#ifdef USE_BL_PWM
+/* PWM configuration structure. We use timer 12 channel 1 */
+static const PWMConfig pwmcfg = {
+  100000,       /* 100 kHz PWM clock frequency. */
+  100,          /* PWM period is 100 cycles. */
+  0,
+  {
+    {PWM_OUTPUT_ACTIVE_LOW, 0},
+    {PWM_OUTPUT_DISABLED, 0},
+    {PWM_OUTPUT_DISABLED, 0},
+    {PWM_OUTPUT_DISABLED, 0}
+  },
+  0,
+  0
+};
+#endif // USE_BL_PWM
+
+static GFXINLINE void init_board(GDisplay *g) {
+  (void) g;
+  // As we are not using multiple displays we set g->board to NULL as we don't use it.
+
+  /* GPIO setup done by board.h */
+
+  /* FSMC setup for F4 */
+  rccEnableAHB3(RCC_AHB3ENR_FSMCEN, 0);
+/*** ADD DMA support!!!! */
+  const unsigned char FSMC_Bank = 0x06; // FSMC_NE4
+
+  /** From china driver */
+  /* Bank1 NOR/SRAM control register configuration */
+  // FSMC_MemoryDataWidth_16b | FSMC_WriteOperation_Enable | FSMC_ExtendedMode_Enable
+  FSMC_Bank1->BTCR[FSMC_Bank] = (uint32_t)(0x10 | 0x1000 | 0x4000);
+
+  /* Bank1 NOR/SRAM timing register configuration */
+  // Address setup time (ADDSET) is 16 HCLK 1/168M=6ns*16=96ns
+  // Data save time is 25 HCLK = 6*25 = 150ns
+  FSMC_Bank1->BTCR[FSMC_Bank+1] = (uint32_t)(0x0F | (0x18 << 8));
+
+  /* Bank1 NOR/SRAM timing register for write configuration, if extended mode is used */
+  // Address setup time (ADDSET) is 8 HCLK = 48ns
+  // Data save time is 6ns*9 HCLK = 54ns
+  FSMC_Bank1E->BWTR[FSMC_Bank] = (uint32_t)(0x08 | (0x08 << 8));
+
+  /* Enable the selected NOR/SRAM Bank by setting the PBKEN bit in the BCRx register */
+  FSMC_Bank1->BTCR[FSMC_Bank] |= 0x01;
+
+#ifdef USE_BL_PWM
+  /* Display backlight control */
+  /* TIM12 is an alternate function 9 (AF9) */
+  pwmStart(&PWMD12, &pwmcfg);
+  palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(9));
+  pwmEnableChannel(&PWMD12, 0, 100);
+#endif // USE_BL_PWM
+}
+
+static GFXINLINE void post_init_board(GDisplay *g) {
+  (void) g;
+}
+
+static GFXINLINE void setpin_reset(GDisplay *g, gBool state) {
+  (void) g;
+  (void) state;
+}
+
+static GFXINLINE void set_backlight(GDisplay *g, gU8 percent) {
+  (void) g;
+#ifdef USE_BL_PWM
+  pwmEnableChannel(&PWMD12, 0, percent);
+#else
+  if (percent) {
+    palClearLine(LINE_LCD_BL); // PNP transistor
+  } else {
+    palSetLine(LINE_LCD_BL);
+  }
+#endif // USE_BL_PWM
+}
+
+static GFXINLINE void acquire_bus(GDisplay *g) {
+  (void) g;
+}
+
+static GFXINLINE void release_bus(GDisplay *g) {
+  (void) g;
+}
+
+static GFXINLINE void write_index(GDisplay *g, gU16 index) {
+  (void) g;
+  GDISP_REG = index;
+}
+
+static GFXINLINE void write_data(GDisplay *g, gU16 data) {
+  (void) g;
+  GDISP_RAM = (data | (data<<8));
+}
+
+static GFXINLINE void setreadmode(GDisplay *g) {
+  (void) g;
+}
+
+static GFXINLINE void setwritemode(GDisplay *g) {
+  (void) g;
+}
+
+static GFXINLINE gU16 read_data(GDisplay *g) {
+  (void) g;
+  return GDISP_RAM;
+}
+
+#endif /* _GDISP_LLD_BOARD_H */

+ 0 - 127
board/board_LGDP4532.h

@@ -1,127 +0,0 @@
-/*
- * This file is subject to the terms of the GFX License. If a copy of
- * the license was not distributed with this file, you can obtain one at:
- *
- *              http://ugfx.org/license.html
- */
-
-/*
-	driver quickly hacked together from a chinese sourcecode that came
-	with the board and existing ili9320 code by Chris van Dongen (sjaak)
-	(sjaak2002 at msn.com)
-	
-	Also added rotation for 180 and 270 degrees and minor tweaks to
-	setcursor
-	
-	Added code comes without warranty and free bugs. Feel free to use 
-	or misuse the added code :D
-*/
-
-
-/**
- * @file    boards/addons/gdisp/board_ILI9325_hy_stm32_100p.h
- * @brief   GDISP Graphic Driver subsystem board SPI interface for the ILI9325 display.
- *
- * @note	This file contains a mix of hardware specific and operating system specific
- *			code. You will need to change it for your CPU and/or operating system.
- */
-
-#ifndef GDISP_LLD_BOARD_H
-#define GDISP_LLD_BOARD_H
-
-// For a multiple display configuration we would put all this in a structure and then
-//	set g->board to that structure.
-#define GDISP_REG              (*((volatile uint16_t *) 0x60000000)) /* RS = 0 */
-#define GDISP_RAM              (*((volatile uint16_t *) 0x60020000)) /* RS = 1 */
-
-static inline void init_board(GDisplay *g) {
-
-	(void) g;
-	// As we are not using multiple displays we set g->board to NULL as we don't use it.
-
-	/* FSMC setup for F1 */
-	rccEnableAHB(RCC_AHBENR_FSMCEN, 0);
-
-	/* set pin modes. by default sets in board files */
-/*	IOBus busD = {GPIOD, PAL_WHOLE_PORT, 0};
-	IOBus busE = {GPIOE, PAL_WHOLE_PORT, 0};
-	palSetBusMode(&busD, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
-	palSetBusMode(&busE, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
-	palSetPadMode(GPIOE, GPIOE_TFT_RST, PAL_MODE_OUTPUT_PUSHPULL);
-	palSetPadMode(GPIOD, GPIOD_TFT_LIGHT, PAL_MODE_OUTPUT_PUSHPULL);
-*/
-
-  const uint8_t FSMC_Bank = 0x0;
-
-  /* Bank1 NOR/SRAM control register configuration */ 
-  // FSMC_MemoryType_NOR | FSMC_MemoryDataWidth_16b | FSMC_WriteOperation_Enable
-  FSMC_Bank1->BTCR[FSMC_Bank] = (uint32_t)(0x8 | 0x10 | 0x1000);
-
-  // if FSMC_MemoryType_NOR
-  FSMC_Bank1->BTCR[FSMC_Bank] |= ((uint32_t)0x00000040);
-  
-  /* Bank1 NOR/SRAM timing register configuration */
-  // AddressSetupTime | DataSetupTime | FSMC_AccessMode_B
-  FSMC_Bank1->BTCR[FSMC_Bank+1] = (uint32_t)(0x2 | (0x5 << 8) | 0x10000000);
-
-  /* Bank1 NOR/SRAM timing register for write configuration, if extended mode is used */
-  FSMC_Bank1E->BWTR[FSMC_Bank] = 0x0FFFFFFF;
-
-  /* Enable FSMC Bank1_SRAM Bank */
-  FSMC_Bank1->BTCR[FSMC_Bank] |= 0x1;
-}
-
-static inline void post_init_board(GDisplay *g) {
-	(void) g;
-}
-
-static inline void setpin_reset(GDisplay *g, bool_t state) {
-	(void) g;
-	if(state)
-		palClearPad(GPIOE, GPIOE_TFT_RST);
-	else
-		palSetPad(GPIOE, GPIOE_TFT_RST);
-}
-
-static inline void set_backlight(GDisplay *g, uint8_t percent) {
-	(void) g;
-	(void)percent;
-}
-
-static inline void gdisp_lld_backlight(GDisplay *g, uint8_t percent) {
-	(void) g;
-	(void)percent;
-}
-
-static inline void acquire_bus(GDisplay *g) {
-	(void) g;
-}
-
-static inline void release_bus(GDisplay *g) {
-	(void) g;
-}
-
-static inline void write_index(GDisplay *g, uint16_t index) {
-	(void) g;
-	GDISP_REG = index;
-}
-
-static inline void write_data(GDisplay *g, uint16_t data) {
-	(void) g;
-	GDISP_RAM = data;
-}
-
-static inline void setreadmode(GDisplay *g) {
-	(void) g;
-}
-
-static inline void setwritemode(GDisplay *g) {
-	(void) g;
-}
-
-static inline uint16_t read_data(GDisplay *g) {
-	(void) g;
-	return GDISP_RAM;
-}
-
-#endif /* GDISP_LLD_BOARD_H */

+ 10 - 7
board/ginput_lld_toggle_board.h

@@ -8,19 +8,22 @@
 #ifndef _GDISP_LLD_TOGGLE_BOARD_H
 #define _GDISP_LLD_TOGGLE_BOARD_H
 
-#define GINPUT_TOGGLE_NUM_PORTS       4  // The total number of toggle inputs
-#define GINPUT_TOGGLE_CONFIG_ENTRIES  1  // The total number of GToggleConfig entries
+#define GINPUT_TOGGLE_NUM_PORTS       3  // The total number of toggle inputs
+#define GINPUT_TOGGLE_CONFIG_ENTRIES  2  // The total number of GToggleConfig entries
 
 #define GINPUT_TOGGLE_SW1      0  // Switch 1
 #define GINPUT_TOGGLE_SW2      1  // Switch 2
-#define GINPUT_TOGGLE_SW3      2  // Switch 3
-#define GINPUT_TOGGLE_SW4      3  // Switch 4
+#define GINPUT_TOGGLE_WK_UP    2  // Wake UP
 
 #define GINPUT_TOGGLE_DECLARE_STRUCTURE() \
   const GToggleConfig GInputToggleConfigTable[GINPUT_TOGGLE_CONFIG_ENTRIES] = { \
-    {GPIOE, /* Switch 1 - Switch 4 */ \
-      (1<<GPIOE_BUTTON1) | (1<<GPIOE_BUTTON2) | (1<<GPIOE_BUTTON3) | (1<<GPIOE_BUTTON4), \
-      (1<<GPIOE_BUTTON1) | (1<<GPIOE_BUTTON2) | (1<<GPIOE_BUTTON3) | (1<<GPIOE_BUTTON4), \
+    {KEY0_PORT, /* Switch 1 - Switch 2 */ \
+      (1<<KEY0) | (1<<KEY1), \
+      (1<<KEY0) | (1<<KEY1), \
+      PAL_MODE_INPUT}, \
+    {WAKE_UP_PORT, /* Switch 3 */ \
+      (1<<WAKE_UP), \
+      (1<<WAKE_UP), \
       PAL_MODE_INPUT}, \
   }
 

+ 128 - 62
board/gmouse_lld_ADS7843_board.h

@@ -9,79 +9,145 @@
 #define _GINPUT_LLD_MOUSE_BOARD_H
 
 // Resolution and Accuracy Settings
-#define GMOUSE_ADS7843_PEN_CALIBRATE_ERROR		2
-#define GMOUSE_ADS7843_PEN_CLICK_ERROR			2
-#define GMOUSE_ADS7843_PEN_MOVE_ERROR			2
-#define GMOUSE_ADS7843_FINGER_CALIBRATE_ERROR	20
-#define GMOUSE_ADS7843_FINGER_CLICK_ERROR		4
-#define GMOUSE_ADS7843_FINGER_MOVE_ERROR		4
+#define GMOUSE_ADS7843_PEN_CALIBRATE_ERROR      8
+#define GMOUSE_ADS7843_PEN_CLICK_ERROR          6
+#define GMOUSE_ADS7843_PEN_MOVE_ERROR           4
+#define GMOUSE_ADS7843_FINGER_CALIBRATE_ERROR  14
+#define GMOUSE_ADS7843_FINGER_CLICK_ERROR      18
+#define GMOUSE_ADS7843_FINGER_MOVE_ERROR       14
 
 // How much extra data to allocate at the end of the GMouse structure for the board's use
-#define GMOUSE_ADS7843_BOARD_DATA_SIZE			0
-
-static const SPIConfig spicfg = { 
-    0,
-	GPIOB, 
-    7, // CS
-    /* SPI_CR1_BR_2 |*/ SPI_CR1_BR_1 | SPI_CR1_BR_0,
-};
-
-static gBool init_board(GMouse* m, unsigned driverinstance)
-{
-	(void)m;
-
-	// Only one touch interface on this board
-	if (driverinstance)
-		return gFalse;
-	
-	// Set the GPIO modes
-	palSetPadMode(GPIOB, 6, PAL_MODE_INPUT_PULLUP); // IRQ
-	palSetPadMode(GPIOC, 6, PAL_MODE_INPUT); // BUSY
-
-	// Start the SPI peripheral
-	spiStart(&SPID1, &spicfg);
-
-	return gTrue;
+#define GMOUSE_ADS7843_BOARD_DATA_SIZE          0
+
+#define T_CS_HIGH    palSetPad(GPIOC, 13)
+#define T_CS_LOW     palClearPad(GPIOC, 13)
+#define T_SCK_HIGH   palSetPad(GPIOB, 0)
+#define T_SCK_LOW    palClearPad(GPIOB, 0)
+#define T_MOSI_HIGH  palSetPad(GPIOF, 11)
+#define T_MOSI_LOW   palClearPad(GPIOF, 11)
+#define T_MISO_LVL   palReadPad(GPIOB, 1)
+
+/**
+ * @brief   Initialise the board for the touch.
+ *
+ * @notapi
+ *
+ * ADS7843 (clone chip) is connected to this pins:
+ * T_CS    GPIOC_PIN13
+ * T_SCK   GPIOB_PIN0
+ * T_MISO  GPIOB_PIN2
+ * T_MOSI  GPIOF_PIN11
+ * T_PEN   GPIOB_PIN1
+ */
+static gBool init_board(GMouse* m, unsigned driverinstance) {
+  (void)m;
+  (void)driverinstance;
+/*
+  palSetPadMode(GPIOC, 13, PAL_MODE_OUTPUT_PUSHPULL);
+  palSetPadMode(GPIOB,  0, PAL_MODE_OUTPUT_PUSHPULL);
+  palSetPadMode(GPIOB,  2, PAL_MODE_INPUT_PULLUP);
+  palSetPadMode(GPIOF, 11, PAL_MODE_OUTPUT_PUSHPULL);
+  palSetPadMode(GPIOB,  1, PAL_MODE_INPUT);
+  * GPIO config must be done by board.h 
+*/
+  T_CS_HIGH;
+  T_SCK_LOW;
+  return true;
 }
 
-static GFXINLINE gBool getpin_pressed(GMouse* m)
-{
-	(void)	m;
-
-	return (!palReadPad(GPIOB, 6));
+/**
+ * @brief   Check whether the surface is currently touched
+ * @return	TRUE if the surface is currently touched
+ *
+ * @notapi
+ * PB1 is connected to TP_IRQ (low active).
+*/
+static GFXINLINE gBool getpin_pressed(GMouse* m) {
+  (void)m;
+  return (!palReadPad(GPIOB, T_PEN));
 }
 
-static GFXINLINE void aquire_bus(GMouse* m)
-{
-	(void)	m;
-
-	spiAcquireBus(&SPID1);
-    palClearPad(GPIOB, 7);
+/**
+ * @brief   Aquire the bus ready for readings
+ *
+ * @notapi
+ * PC13 is connected to TP_CS (low active):
+ */
+static GFXINLINE void aquire_bus(GMouse* m) {
+  (void)m;
+  T_CS_LOW;
+  T_SCK_LOW;
 }
 
-static GFXINLINE void release_bus(GMouse* m)
-{
-	(void)	m;
-
-	palSetPad(GPIOB, 7);
-	spiReleaseBus(&SPID1);
+/**
+ * @brief   Release the bus after readings
+ *
+ * @notapi
+ */
+static GFXINLINE void release_bus(GMouse* m) {
+  (void)m;
+  T_CS_HIGH;
 }
 
-static GFXINLINE gU16 read_value(GMouse* m, gU16 port)
-{
-    static gU8 txbuf[3] = {0};
-    static gU8 rxbuf[3] = {0};
-    gU16 ret;
-	(void)	m;
-
-    txbuf[0] = port;
-
-    spiExchange(&SPID1, 3, txbuf, rxbuf);
+/**
+ * @brief   Write a value to touch controller
+ * @return	none
+ *
+ * params[in] value	The value to be write.
+ *
+ * @notapi
+ */
+static GFXINLINE void write_value(GMouse* m, gU16 value) {
+  (void)m;
+  int i;
+
+  for (i=8; i>0; i--) {
+    T_SCK_LOW;
+    if (value & 0x0080) {
+      T_MOSI_HIGH;
+    } else {
+      T_MOSI_LOW;
+    }
+    asm("nop");
+    value <<= 1;
+    T_SCK_HIGH;
+  }
+}
 
-    ret = (rxbuf[1] << 5) | (rxbuf[2] >> 3); 
-    
-	return ret;
+/**
+ * @brief   Read a value from touch controller
+ * @return	The value read from the controller
+ *
+ * params[in] port	The controller port to read.
+ *
+ * @notapi
+ */
+static GFXINLINE gU16 read_value(GMouse* m, gU16 port) {
+  (void)m;
+  int i;
+  gU16 data = 0;
+
+  write_value(m, port);
+  gfxSleepMicroseconds(6); //The conversion time of ADS7846 is up to 6us
+
+  T_SCK_LOW;
+  asm("nop"); // or gfxSleepMicroseconds(1) ?
+  T_SCK_HIGH; //Give 1 clock, clear BUSY
+  asm("nop");
+
+  //Read 16-bit data, only the upper 12 bits are valid
+  for (i=16; i>0; i--) {
+    T_SCK_LOW;
+    data <<= 1;
+    asm("nop");
+    T_SCK_HIGH;
+    if (T_MISO_LVL) {
+      data ++;
+    }
+  }
+
+  data >>= 4;
+  return data;
 }
 
 #endif /* _GINPUT_LLD_MOUSE_BOARD_H */
-

+ 4 - 30
cfg/chconf.h

@@ -63,7 +63,7 @@
  * @note    Allowed values are 16, 32 or 64 bits.
  */
 #if !defined(CH_CFG_ST_RESOLUTION)
-#define CH_CFG_ST_RESOLUTION                16
+#define CH_CFG_ST_RESOLUTION                32
 #endif
 
 /**
@@ -72,7 +72,7 @@
  *          setting also defines the system tick time unit.
  */
 #if !defined(CH_CFG_ST_FREQUENCY)
-#define CH_CFG_ST_FREQUENCY                 2000
+#define CH_CFG_ST_FREQUENCY                 10000
 #endif
 
 /**
@@ -139,19 +139,6 @@
 #define CH_CFG_NO_IDLE_THREAD               FALSE
 #endif
 
-/**
- * @brief   Kernel hardening level.
- * @details This option is the level of functional-safety checks enabled
- *          in the kerkel. The meaning is:
- *          - 0: No checks, maximum performance.
- *          - 1: Reasonable checks.
- *          - 2: All checks.
- *          .
- */
-#if !defined(CH_CFG_HARDENING_LEVEL)
-#define CH_CFG_HARDENING_LEVEL              0
-#endif
-
 /** @} */
 
 /*===========================================================================*/
@@ -373,16 +360,6 @@
 #define CH_CFG_USE_MAILBOXES                TRUE
 #endif
 
-/**
- * @brief   Memory checks APIs.
- * @details If enabled then the memory checks APIs are included in the kernel.
- *
- * @note    The default is @p TRUE.
- */
-#if !defined(CH_CFG_USE_MEMCHECKS)
-#define CH_CFG_USE_MEMCHECKS                TRUE
-#endif
-
 /**
  * @brief   Core Memory Manager APIs.
  * @details If enabled then the core memory manager APIs are included
@@ -586,7 +563,7 @@
  * @note    The default is @p FALSE.
  */
 #if !defined(CH_DBG_SYSTEM_STATE_CHECK)
-#define CH_DBG_SYSTEM_STATE_CHECK           TRUE
+#define CH_DBG_SYSTEM_STATE_CHECK           FALSE
 #endif
 
 /**
@@ -597,7 +574,7 @@
  * @note    The default is @p FALSE.
  */
 #if !defined(CH_DBG_ENABLE_CHECKS)
-#define CH_DBG_ENABLE_CHECKS                TRUE
+#define CH_DBG_ENABLE_CHECKS                FALSE
 #endif
 
 /**
@@ -810,9 +787,6 @@
  */
 #define CH_CFG_SYSTEM_HALT_HOOK(reason) {                                   \
   /* System halt code here.*/                                               \
-  while (*reason) { \
-    while ((USART1->SR & USART_SR_TXE) == 0) { __NOP(); } \
-    USART1->DR = *reason; reason ++; } \
 }
 
 /**

+ 25 - 25
cfg/gfxconf.h

@@ -31,46 +31,46 @@
 #define _GFXCONF_H
 
 /* The operating system to use. One of these must be defined - preferably in your Makefile */
-#define GFX_USE_OS_CHIBIOS		GFXON
-    #define GFX_MEM_LT64K		GFXOFF
-#define GOS_NEED_X_HEAP 		GFXOFF
-#define GOS_NEED_X_THREADS		GFXOFF
+#define GFX_USE_OS_CHIBIOS		        GFXON
+    #define GFX_MEM_LT64K		        GFXOFF
+#define GOS_NEED_X_HEAP 		        GFXOFF
+#define GOS_NEED_X_THREADS		        GFXOFF
 
 /* GFX sub-systems to turn on */
-#define GFX_USE_GDISP			GFXON
-#define GFX_USE_GWIN			GFXON
-#define GFX_USE_GMISC			GFXON
-#define GFX_USE_GTIMER			GFXON
-#define GFX_USE_GQUEUE			GFXON
-#define GDISP_NEED_CONTROL		GFXON
+#define GFX_USE_GDISP			        GFXON
+#define GFX_USE_GWIN			        GFXON
+#define GFX_USE_GMISC			        GFXON
+#define GFX_USE_GTIMER			        GFXON
+#define GFX_USE_GQUEUE			        GFXON
+#define GDISP_NEED_CONTROL		        GFXON
 #define GDISP_DEFAULT_ORIENTATION       gOrientationLandscape
 
 /* Features for the GDISP sub-system. */
-#define GDISP_NEED_VALIDATION	    GFXON
-#define GDISP_NEED_CLIP			    GFXON
-#define GDISP_NEED_MULTITHREAD	    GFXON
-#define GDISP_NEED_TEXT			    GFXON
+#define GDISP_NEED_VALIDATION	        GFXON
+#define GDISP_NEED_CLIP			        GFXON
+#define GDISP_NEED_MULTITHREAD	        GFXON
+#define GDISP_NEED_TEXT			        GFXON
 
 /* GDISP fonts to include */
 #define GDISP_INCLUDE_FONT_DEJAVUSANS12 GFXON
-#define GDISP_INCLUDE_FONT_DEJAVUSANS24 GFXON
+#define GDISP_INCLUDE_FONT_DEJAVUSANS32 GFXON
 
 /* Features for the GWIN subsystem. */
-#define GWIN_NEED_WINDOWMANAGER	    GFXON
-#define GWIN_NEED_CONSOLE		    GFXON
-#define GWIN_CONSOLE_ESCSEQ		    GFXON
-#define GWIN_CONSOLE_USE_HISTORY	GFXOFF
+#define GWIN_NEED_WINDOWMANAGER	        GFXON
+#define GWIN_NEED_CONSOLE		        GFXON
+#define GWIN_CONSOLE_ESCSEQ		        GFXON
+#define GWIN_CONSOLE_USE_HISTORY	    GFXOFF
 //#define GWIN_CONSOLE_HISTORY_AVERAGING	GFXON
-//#define GWIN_CONSOLE_HISTORY_ATCREATE	GFXON
-#define GWIN_NEED_GRAPH			    GFXON
+//#define GWIN_CONSOLE_HISTORY_ATCREATE	    GFXON
+#define GWIN_NEED_GRAPH			        GFXON
 
 /* Features for the GQUEUE subsystem. */
-#define GQUEUE_NEED_ASYNC           GFXON
+#define GQUEUE_NEED_ASYNC               GFXON
 
 /* Features for the GMISC subsystem. */
-#define GMISC_NEED_FASTTRIG		    GFXON
-#define GMISC_NEED_MATRIXFLOAT2D    GFXOFF
-#define GMISC_NEED_MATRIXFIXED2D    GFXOFF
+#define GMISC_NEED_FASTTRIG		        GFXON
+#define GMISC_NEED_MATRIXFLOAT2D        GFXOFF
+#define GMISC_NEED_MATRIXFIXED2D        GFXOFF
 
 #endif /* _GFXCONF_H */
 

+ 1 - 1
cfg/halconf.h

@@ -401,7 +401,7 @@
  *          default configuration.
  */
 #if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__)
-#define SERIAL_DEFAULT_BITRATE              115200
+#define SERIAL_DEFAULT_BITRATE              38400
 #endif
 
 /**

+ 181 - 50
cfg/mcuconf.h

@@ -1,5 +1,5 @@
 /*
-    ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
+    ChibiOS - Copyright (C) 2006..2020 Giovanni Di Sirio
 
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -17,10 +17,8 @@
 #ifndef MCUCONF_H
 #define MCUCONF_H
 
-#define STM32F103_MCUCONF
-
 /*
- * STM32F103 drivers configuration.
+ * STM32F4xx drivers configuration.
  * The following settings override the default settings present in
  * the various device driver implementation headers.
  * Note that the settings for each driver only have effect if the whole
@@ -33,28 +31,42 @@
  * 0...3        Lowest...Highest.
  */
 
+#define STM32F4xx_MCUCONF
+#define STM32F405_MCUCONF
+#define STM32F415_MCUCONF
+#define STM32F407_MCUCONF
+#define STM32F417_MCUCONF
+
 /*
  * HAL driver system settings.
  */
 #define STM32_NO_INIT                       FALSE
+#define STM32_PVD_ENABLE                    FALSE
+#define STM32_PLS                           STM32_PLS_LEV0
+#define STM32_BKPRAM_ENABLE                 FALSE
 #define STM32_HSI_ENABLED                   TRUE
 #define STM32_LSI_ENABLED                   FALSE
 #define STM32_HSE_ENABLED                   TRUE
-#define STM32_LSE_ENABLED                   FALSE
+#define STM32_LSE_ENABLED                   TRUE
+#define STM32_CLOCK48_REQUIRED              TRUE
 #define STM32_SW                            STM32_SW_PLL
 #define STM32_PLLSRC                        STM32_PLLSRC_HSE
-#define STM32_PLLXTPRE                      STM32_PLLXTPRE_DIV1
-#define STM32_PLLMUL_VALUE                  9
+#define STM32_PLLM_VALUE                    8
+#define STM32_PLLN_VALUE                    336
+#define STM32_PLLP_VALUE                    2
+#define STM32_PLLQ_VALUE                    7
 #define STM32_HPRE                          STM32_HPRE_DIV1
-#define STM32_PPRE1                         STM32_PPRE1_DIV2
-#define STM32_PPRE2                         STM32_PPRE2_DIV1
-#define STM32_ADCPRE                        STM32_ADCPRE_DIV6
-#define STM32_USB_CLOCK_REQUIRED            TRUE
-#define STM32_USBPRE                        STM32_USBPRE_DIV1P5
-#define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK
-#define STM32_RTCSEL                        STM32_RTCSEL_NOCLOCK
-#define STM32_PVD_ENABLE                    FALSE
-#define STM32_PLS                           STM32_PLS_LEV0
+#define STM32_PPRE1                         STM32_PPRE1_DIV4
+#define STM32_PPRE2                         STM32_PPRE2_DIV2
+#define STM32_RTCSEL                        STM32_RTCSEL_LSE
+#define STM32_RTCPRE_VALUE                  8
+#define STM32_MCO1SEL                       STM32_MCO1SEL_HSI
+#define STM32_MCO1PRE                       STM32_MCO1PRE_DIV1
+#define STM32_MCO2SEL                       STM32_MCO2SEL_SYSCLK
+#define STM32_MCO2PRE                       STM32_MCO2PRE_DIV5
+#define STM32_I2SSRC                        STM32_I2SSRC_CKIN
+#define STM32_PLLI2SN_VALUE                 192
+#define STM32_PLLI2SR_VALUE                 5
 
 /*
  * IRQ system settings.
@@ -67,22 +79,73 @@
 #define STM32_IRQ_EXTI5_9_PRIORITY          6
 #define STM32_IRQ_EXTI10_15_PRIORITY        6
 #define STM32_IRQ_EXTI16_PRIORITY           6
-#define STM32_IRQ_EXTI17_PRIORITY           6
+#define STM32_IRQ_EXTI17_PRIORITY           15
 #define STM32_IRQ_EXTI18_PRIORITY           6
 #define STM32_IRQ_EXTI19_PRIORITY           6
+#define STM32_IRQ_EXTI20_PRIORITY           6
+#define STM32_IRQ_EXTI21_PRIORITY           15
+#define STM32_IRQ_EXTI22_PRIORITY           15
+
+#define STM32_IRQ_TIM1_BRK_TIM9_PRIORITY    7
+#define STM32_IRQ_TIM1_UP_TIM10_PRIORITY    7
+#define STM32_IRQ_TIM1_TRGCO_TIM11_PRIORITY 7
+#define STM32_IRQ_TIM1_CC_PRIORITY          7
+#define STM32_IRQ_TIM2_PRIORITY             7
+#define STM32_IRQ_TIM3_PRIORITY             7
+#define STM32_IRQ_TIM4_PRIORITY             7
+#define STM32_IRQ_TIM5_PRIORITY             7
+#define STM32_IRQ_TIM6_PRIORITY             7
+#define STM32_IRQ_TIM7_PRIORITY             7
+#define STM32_IRQ_TIM8_BRK_TIM12_PRIORITY   7
+#define STM32_IRQ_TIM8_UP_TIM13_PRIORITY    7
+#define STM32_IRQ_TIM8_TRGCO_TIM14_PRIORITY 7
+#define STM32_IRQ_TIM8_CC_PRIORITY          7
+
+#define STM32_IRQ_USART1_PRIORITY           12
+#define STM32_IRQ_USART2_PRIORITY           12
+#define STM32_IRQ_USART3_PRIORITY           12
+#define STM32_IRQ_UART4_PRIORITY            12
+#define STM32_IRQ_UART5_PRIORITY            12
+#define STM32_IRQ_USART6_PRIORITY           12
 
 /*
  * ADC driver system settings.
  */
+#define STM32_ADC_ADCPRE                    ADC_CCR_ADCPRE_DIV4
 #define STM32_ADC_USE_ADC1                  FALSE
+#define STM32_ADC_USE_ADC2                  FALSE
+#define STM32_ADC_USE_ADC3                  FALSE
+#define STM32_ADC_ADC1_DMA_STREAM           STM32_DMA_STREAM_ID(2, 4)
+#define STM32_ADC_ADC2_DMA_STREAM           STM32_DMA_STREAM_ID(2, 2)
+#define STM32_ADC_ADC3_DMA_STREAM           STM32_DMA_STREAM_ID(2, 1)
 #define STM32_ADC_ADC1_DMA_PRIORITY         2
-#define STM32_ADC_ADC1_IRQ_PRIORITY         6
+#define STM32_ADC_ADC2_DMA_PRIORITY         2
+#define STM32_ADC_ADC3_DMA_PRIORITY         2
+#define STM32_ADC_IRQ_PRIORITY              6
+#define STM32_ADC_ADC1_DMA_IRQ_PRIORITY     6
+#define STM32_ADC_ADC2_DMA_IRQ_PRIORITY     6
+#define STM32_ADC_ADC3_DMA_IRQ_PRIORITY     6
 
 /*
  * CAN driver system settings.
  */
 #define STM32_CAN_USE_CAN1                  FALSE
+#define STM32_CAN_USE_CAN2                  FALSE
 #define STM32_CAN_CAN1_IRQ_PRIORITY         11
+#define STM32_CAN_CAN2_IRQ_PRIORITY         11
+
+/*
+ * DAC driver system settings.
+ */
+#define STM32_DAC_DUAL_MODE                 FALSE
+#define STM32_DAC_USE_DAC1_CH1              FALSE
+#define STM32_DAC_USE_DAC1_CH2              FALSE
+#define STM32_DAC_DAC1_CH1_IRQ_PRIORITY     10
+#define STM32_DAC_DAC1_CH2_IRQ_PRIORITY     10
+#define STM32_DAC_DAC1_CH1_DMA_PRIORITY     2
+#define STM32_DAC_DAC1_CH2_DMA_PRIORITY     2
+#define STM32_DAC_DAC1_CH1_DMA_STREAM       STM32_DMA_STREAM_ID(1, 5)
+#define STM32_DAC_DAC1_CH2_DMA_STREAM       STM32_DMA_STREAM_ID(1, 6)
 
 /*
  * GPT driver system settings.
@@ -95,25 +158,49 @@
 #define STM32_GPT_USE_TIM6                  FALSE
 #define STM32_GPT_USE_TIM7                  TRUE
 #define STM32_GPT_USE_TIM8                  FALSE
-#define STM32_GPT_TIM1_IRQ_PRIORITY         7
-#define STM32_GPT_TIM2_IRQ_PRIORITY         7
-#define STM32_GPT_TIM3_IRQ_PRIORITY         7
-#define STM32_GPT_TIM4_IRQ_PRIORITY         7
-#define STM32_GPT_TIM5_IRQ_PRIORITY         7
-#define STM32_GPT_TIM8_IRQ_PRIORITY         7
+#define STM32_GPT_USE_TIM9                  FALSE
+#define STM32_GPT_USE_TIM10                 FALSE
+#define STM32_GPT_USE_TIM11                 FALSE
+#define STM32_GPT_USE_TIM12                 FALSE
+#define STM32_GPT_USE_TIM13                 FALSE
+#define STM32_GPT_USE_TIM14                 FALSE
 
 /*
  * I2C driver system settings.
  */
 #define STM32_I2C_USE_I2C1                  TRUE
 #define STM32_I2C_USE_I2C2                  FALSE
+#define STM32_I2C_USE_I2C3                  FALSE
 #define STM32_I2C_BUSY_TIMEOUT              50
+#define STM32_I2C_I2C1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 0)
+#define STM32_I2C_I2C1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 6)
+#define STM32_I2C_I2C2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 2)
+#define STM32_I2C_I2C2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 7)
+#define STM32_I2C_I2C3_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 2)
+#define STM32_I2C_I2C3_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
 #define STM32_I2C_I2C1_IRQ_PRIORITY         5
 #define STM32_I2C_I2C2_IRQ_PRIORITY         5
+#define STM32_I2C_I2C3_IRQ_PRIORITY         5
 #define STM32_I2C_I2C1_DMA_PRIORITY         3
 #define STM32_I2C_I2C2_DMA_PRIORITY         3
+#define STM32_I2C_I2C3_DMA_PRIORITY         3
 #define STM32_I2C_DMA_ERROR_HOOK(i2cp)      osalSysHalt("DMA failure")
 
+/*
+ * I2S driver system settings.
+ */
+#define STM32_I2S_USE_SPI2                  FALSE
+#define STM32_I2S_USE_SPI3                  FALSE
+#define STM32_I2S_SPI2_IRQ_PRIORITY         10
+#define STM32_I2S_SPI3_IRQ_PRIORITY         10
+#define STM32_I2S_SPI2_DMA_PRIORITY         1
+#define STM32_I2S_SPI3_DMA_PRIORITY         1
+#define STM32_I2S_SPI2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 3)
+#define STM32_I2S_SPI2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
+#define STM32_I2S_SPI3_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 0)
+#define STM32_I2S_SPI3_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 7)
+#define STM32_I2S_DMA_ERROR_HOOK(i2sp)      osalSysHalt("DMA failure")
+
 /*
  * ICU driver system settings.
  */
@@ -123,34 +210,58 @@
 #define STM32_ICU_USE_TIM4                  FALSE
 #define STM32_ICU_USE_TIM5                  FALSE
 #define STM32_ICU_USE_TIM8                  FALSE
-#define STM32_ICU_TIM1_IRQ_PRIORITY         7
-#define STM32_ICU_TIM2_IRQ_PRIORITY         7
-#define STM32_ICU_TIM3_IRQ_PRIORITY         7
-#define STM32_ICU_TIM4_IRQ_PRIORITY         7
-#define STM32_ICU_TIM5_IRQ_PRIORITY         7
-#define STM32_ICU_TIM8_IRQ_PRIORITY         7
+#define STM32_ICU_USE_TIM9                  FALSE
+#define STM32_ICU_USE_TIM10                 FALSE
+#define STM32_ICU_USE_TIM11                 FALSE
+#define STM32_ICU_USE_TIM12                 FALSE
+#define STM32_ICU_USE_TIM13                 FALSE
+#define STM32_ICU_USE_TIM14                 FALSE
+
+/*
+ * MAC driver system settings.
+ */
+#define STM32_MAC_TRANSMIT_BUFFERS          2
+#define STM32_MAC_RECEIVE_BUFFERS           4
+#define STM32_MAC_BUFFERS_SIZE              1522
+#define STM32_MAC_PHY_TIMEOUT               100
+#define STM32_MAC_ETH1_CHANGE_PHY_STATE     TRUE
+#define STM32_MAC_ETH1_IRQ_PRIORITY         13
+#define STM32_MAC_IP_CHECKSUM_OFFLOAD       0
 
 /*
  * PWM driver system settings.
  */
-#define STM32_PWM_USE_ADVANCED              FALSE
 #define STM32_PWM_USE_TIM1                  FALSE
 #define STM32_PWM_USE_TIM2                  FALSE
 #define STM32_PWM_USE_TIM3                  FALSE
 #define STM32_PWM_USE_TIM4                  FALSE
 #define STM32_PWM_USE_TIM5                  FALSE
 #define STM32_PWM_USE_TIM8                  FALSE
-#define STM32_PWM_TIM1_IRQ_PRIORITY         7
-#define STM32_PWM_TIM2_IRQ_PRIORITY         7
-#define STM32_PWM_TIM3_IRQ_PRIORITY         7
-#define STM32_PWM_TIM4_IRQ_PRIORITY         7
-#define STM32_PWM_TIM5_IRQ_PRIORITY         7
-#define STM32_PWM_TIM8_IRQ_PRIORITY         7
+#define STM32_PWM_USE_TIM9                  FALSE
+#define STM32_PWM_USE_TIM10                 FALSE
+#define STM32_PWM_USE_TIM11                 FALSE
+#define STM32_PWM_USE_TIM12                 FALSE
+#define STM32_PWM_USE_TIM13                 FALSE
+#define STM32_PWM_USE_TIM14                 FALSE
 
 /*
  * RTC driver system settings.
  */
-#define STM32_RTC_IRQ_PRIORITY              15
+#define STM32_RTC_PRESA_VALUE               32
+#define STM32_RTC_PRESS_VALUE               1024
+#define STM32_RTC_CR_INIT                   0
+#define STM32_RTC_TAMPCR_INIT               0
+
+/*
+ * SDC driver system settings.
+ */
+#define STM32_SDC_SDIO_DMA_PRIORITY         3
+#define STM32_SDC_SDIO_IRQ_PRIORITY         9
+#define STM32_SDC_WRITE_TIMEOUT_MS          1000
+#define STM32_SDC_READ_TIMEOUT_MS           1000
+#define STM32_SDC_CLOCK_ACTIVATION_DELAY    10
+#define STM32_SDC_SDIO_UNALIGNED_SUPPORT    TRUE
+#define STM32_SDC_SDIO_DMA_STREAM           STM32_DMA_STREAM_ID(2, 3)
 
 /*
  * SERIAL driver system settings.
@@ -160,11 +271,7 @@
 #define STM32_SERIAL_USE_USART3             FALSE
 #define STM32_SERIAL_USE_UART4              FALSE
 #define STM32_SERIAL_USE_UART5              FALSE
-#define STM32_SERIAL_USART1_PRIORITY        12
-#define STM32_SERIAL_USART2_PRIORITY        12
-#define STM32_SERIAL_USART3_PRIORITY        12
-#define STM32_SERIAL_UART4_PRIORITY         12
-#define STM32_SERIAL_UART5_PRIORITY         12
+#define STM32_SERIAL_USE_USART6             FALSE
 
 /*
  * SPI driver system settings.
@@ -172,6 +279,12 @@
 #define STM32_SPI_USE_SPI1                  FALSE
 #define STM32_SPI_USE_SPI2                  FALSE
 #define STM32_SPI_USE_SPI3                  FALSE
+#define STM32_SPI_SPI1_RX_DMA_STREAM        STM32_DMA_STREAM_ID(2, 0)
+#define STM32_SPI_SPI1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(2, 3)
+#define STM32_SPI_SPI2_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 3)
+#define STM32_SPI_SPI2_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 4)
+#define STM32_SPI_SPI3_RX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 0)
+#define STM32_SPI_SPI3_TX_DMA_STREAM        STM32_DMA_STREAM_ID(1, 7)
 #define STM32_SPI_SPI1_DMA_PRIORITY         1
 #define STM32_SPI_SPI2_DMA_PRIORITY         1
 #define STM32_SPI_SPI3_DMA_PRIORITY         1
@@ -192,21 +305,39 @@
 #define STM32_UART_USE_USART1               FALSE
 #define STM32_UART_USE_USART2               FALSE
 #define STM32_UART_USE_USART3               FALSE
-#define STM32_UART_USART1_IRQ_PRIORITY      12
-#define STM32_UART_USART2_IRQ_PRIORITY      12
-#define STM32_UART_USART3_IRQ_PRIORITY      12
+#define STM32_UART_USE_UART4                FALSE
+#define STM32_UART_USE_UART5                FALSE
+#define STM32_UART_USE_USART6               FALSE
+#define STM32_UART_USART1_RX_DMA_STREAM     STM32_DMA_STREAM_ID(2, 5)
+#define STM32_UART_USART1_TX_DMA_STREAM     STM32_DMA_STREAM_ID(2, 7)
+#define STM32_UART_USART2_RX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 5)
+#define STM32_UART_USART2_TX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 6)
+#define STM32_UART_USART3_RX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 1)
+#define STM32_UART_USART3_TX_DMA_STREAM     STM32_DMA_STREAM_ID(1, 3)
+#define STM32_UART_UART4_RX_DMA_STREAM      STM32_DMA_STREAM_ID(1, 2)
+#define STM32_UART_UART4_TX_DMA_STREAM      STM32_DMA_STREAM_ID(1, 4)
+#define STM32_UART_UART5_RX_DMA_STREAM      STM32_DMA_STREAM_ID(1, 0)
+#define STM32_UART_UART5_TX_DMA_STREAM      STM32_DMA_STREAM_ID(1, 7)
+#define STM32_UART_USART6_RX_DMA_STREAM     STM32_DMA_STREAM_ID(2, 2)
+#define STM32_UART_USART6_TX_DMA_STREAM     STM32_DMA_STREAM_ID(2, 7)
 #define STM32_UART_USART1_DMA_PRIORITY      0
 #define STM32_UART_USART2_DMA_PRIORITY      0
 #define STM32_UART_USART3_DMA_PRIORITY      0
+#define STM32_UART_UART4_DMA_PRIORITY       0
+#define STM32_UART_UART5_DMA_PRIORITY       0
+#define STM32_UART_USART6_DMA_PRIORITY      0
 #define STM32_UART_DMA_ERROR_HOOK(uartp)    osalSysHalt("DMA failure")
 
 /*
  * USB driver system settings.
  */
-#define STM32_USB_USE_USB1                  FALSE
-#define STM32_USB_LOW_POWER_ON_SUSPEND      FALSE
-#define STM32_USB_USB1_HP_IRQ_PRIORITY      13
-#define STM32_USB_USB1_LP_IRQ_PRIORITY      14
+#define STM32_USB_USE_OTG1                  FALSE
+#define STM32_USB_USE_OTG2                  FALSE
+#define STM32_USB_OTG1_IRQ_PRIORITY         14
+#define STM32_USB_OTG2_IRQ_PRIORITY         14
+#define STM32_USB_OTG1_RX_FIFO_SIZE         512
+#define STM32_USB_OTG2_RX_FIFO_SIZE         1024
+#define STM32_USB_HOST_WAKEUP_DURATION      2
 
 /*
  * WDG driver system settings.

+ 4 - 3
flash.jlink

@@ -1,6 +1,7 @@
-if swd
-speed 4000
-device stm32f103ve
+if jtag
+JTAGConf -1 -1
+speed 20000
+device stm32f407zg
 loadfile build\acc-tst.hex
 r
 g

+ 2 - 3
inc/buttons.h

@@ -8,14 +8,13 @@
 #define BTN_TIME_PRESSED    (30/BTN_SCAN_PERIOD)
 #define BTN_TIME_HOLDED     (500/BTN_SCAN_PERIOD)
 #define BTN_TIME_REPEATED   (50/BTN_SCAN_PERIOD)
+#define BUTTON_PRESSED      PAL_LOW
 
 /* type defs */
 typedef enum btn_num {
   Button1 = 0,
   Button2 = 1,
-  Button3 = 2,
-  Button4 = 3,
-  Button_Num = 4
+  Button_Num = 2
 } button_num_t;
 
 typedef enum btn_state {

+ 7 - 0
readme.txt

@@ -48,3 +48,10 @@ Tasks:
 оконочания процессов заряда/розряда.
 
 Потом вывод в ком-порт, в файл на микросд-карту.
+---
+03.01.2023
+
+Спалил HYSTM-32, переходим на STM32F4xx.
+
+Так как тут всего 2 кнопки, будем одной перебирать пункты меню, а второй - 
+активировать.

+ 1 - 3
src/buttons.c

@@ -6,7 +6,7 @@ static btn_hndlr btnh[4];
 
 /* Constants */
 static const ioline_t button_Lines[Button_Num] = {
-  LINE_BUTTON1, LINE_BUTTON2, LINE_BUTTON3, LINE_BUTTON4
+  LINE_BUTTON1, LINE_BUTTON2
 };
 
 /* Variables */
@@ -75,8 +75,6 @@ static THD_FUNCTION(BTNThread, arg) {
 void buttons_Init(btn_hndlr ha[]) {
   btnh[Button1] = ha[Button1];
   btnh[Button2] = ha[Button2];
-  btnh[Button3] = ha[Button3];
-  btnh[Button4] = ha[Button4];
 
   /* Initializing the Button semaphore in the "taken" state. */
   chBSemObjectInit(&btn_bsem, true);

+ 216 - 103
src/main.c

@@ -14,10 +14,10 @@
     limitations under the License.
 */
 
-#include <ch.h>
-#include <hal.h>
-#include <gfx.h>
-#include <chprintf.h>
+#include "ch.h"
+#include "hal.h"
+#include "chprintf.h"
+#include "gfx.h"
 #include "buttons.h"
 #include "INA3221.h"
 
@@ -68,6 +68,21 @@ accum_profile_t Profile[ACCUM_PRIFILE_NUM] = {
   {"LiIon", "1S2", 2600, 4200, 2750, 1300, 130, {3,0,0}, "ICR18650-26F"}
 };
 
+enum menu_items {
+  MenuItem_1 = 0,
+  MenuItem_2 = 1,
+  MenuItem_3 = 2,
+  MenuItem_4 = 3,
+  MenuItems = 4
+};
+
+typedef struct {
+  color_t activeFG;
+  color_t activeBG;
+  color_t inactiveFG;
+  color_t inactiveBG;
+} menu_colors_t;
+
 // types and description of accums - SLA, LiIon
 
 // variants and description of voltaged accroding to cemistry type - 6/12V, 1-4S
@@ -109,8 +124,6 @@ static void ina_Process(void);
 static void mode_vt_cb(virtual_timer_t *vtp, void *st);
 static void btn1_handler(const button_state_t);
 static void btn2_handler(const button_state_t);
-static void btn3_handler(const button_state_t);
-static void btn4_handler(const button_state_t);
 static void show_MenuItem(const int item);
 
 /* Private variables */
@@ -132,7 +145,9 @@ ina3221_t ina3221;
 static gFont font1;
 static gFont font2;
 static time_cnt_t Timer = {0}, dechTimer, chTimer;
-static btn_hndlr bha[Button_Num] = {btn1_handler, btn2_handler, btn3_handler, btn4_handler};
+static btn_hndlr bha[Button_Num] = {btn1_handler, btn2_handler};
+static int menu_Active = MenuItem_1;
+static menu_colors_t menu_Colors = {Blue, Yellow, Yellow, Blue};
 
 /*
  * INA process thread.
@@ -203,10 +218,35 @@ static THD_FUNCTION(Thread1, arg) {
   (void)arg;
   chRegSetThreadName("blinker");
   while (true) {
-    palSetLine(LINE_LED4);
-    chThdSleepMilliseconds(500);
-    palClearLine(LINE_LED4);
-    chThdSleepMilliseconds(500);
+    switch (charger_State) {
+    case Stop:
+      palClearLine(LINE_LED1);
+      chThdSleepMilliseconds(500);
+      palSetLine(LINE_LED1);
+      chThdSleepMilliseconds(500);
+      break;
+    
+    case Error:
+      palClearLine(LINE_LED1);
+      chThdSleepMilliseconds(200);
+      palSetLine(LINE_LED1);
+      chThdSleepMilliseconds(200);
+      break;
+
+    case Decharge:
+      palClearLine(LINE_LED1);
+      chThdSleepMilliseconds(700);
+      palSetLine(LINE_LED1);
+      chThdSleepMilliseconds(300);
+      break;
+
+    default:
+      palClearLine(LINE_LED1);
+      chThdSleepMilliseconds(300);
+      palSetLine(LINE_LED1);
+      chThdSleepMilliseconds(700);
+      break;
+    }
   }
 }
 
@@ -519,6 +559,9 @@ int main(void) {
   }
 }
 
+/**
+ * @brief Prepare screen for inforation presentation.
+ */
 static void prepare_Screen(void) {
   /* Set some fonts */
   font1 = gdispOpenFont("DejaVu Sans Book 12");
@@ -535,12 +578,10 @@ static void prepare_Screen(void) {
   gdispFillArea(1, 1, 78, 121, Blue); // text area
 
   /* print text to top-left area */
-  show_MenuItem(1);
-  show_MenuItem(2);
-  gdispDrawStringBox(1, 61, 78, 15, "[3] show ch", font1, Yellow, gJustifyLeft);
-  gdispDrawStringBox(1, 76, 78, 15, "dech results", font1, Yellow, gJustifyLeft);
-  gdispDrawStringBox(1, 91, 78, 15, "[4] start", font1, Yellow, gJustifyLeft);
-//  gdispDrawStringBox(1, 106, 78, 15, "        ", font1, Yellow, gJustifyLeft);
+  show_MenuItem(MenuItem_1);
+  show_MenuItem(MenuItem_2);
+  show_MenuItem(MenuItem_3);
+  show_MenuItem(MenuItem_4);
 
   /* clear bootom area */
   gdispFillStringBox(1, 123, 318, 115, " ", font2, Red, Gray, gJustifyLeft);
@@ -567,142 +608,214 @@ static void prepare_Screen(void) {
   gwinPrintf(GW1, "\033bConsole\033B started sucessful.\n");
 }
 
+/**
+ * @brief Select next menu item
+ */
 static void btn1_handler(button_state_t state) {
   if (state == BTN_st_Pressed) {
     gwinPrintf(GW1, "Button 1 pressed\n");
-
-    if (charger_State == Stop) {
-      switch (charger_Channel)
-      {
-      case INA3221_CH1:
-        charger_Channel = INA3221_CH2;
-        break;
-      case INA3221_CH2:
-        charger_Channel = INA3221_CH3;
-        break;
-      case INA3221_CH3:
-        charger_Channel = INA3221_CH1;
-        break;
-      
-      default:
-        charger_Channel = INA3221_CH1;
-        break;
-      }
-      show_MenuItem(1);
+    switch (menu_Active) {
+    case MenuItem_1:
+      menu_Active = MenuItem_2;
+      show_MenuItem(MenuItem_1);
+      show_MenuItem(MenuItem_2);
+      break;
+    
+    case MenuItem_2:
+      menu_Active = MenuItem_3;
+      show_MenuItem(MenuItem_2);
+      show_MenuItem(MenuItem_3);
+      break;
+    
+    case MenuItem_3:
+      menu_Active = MenuItem_4;
+      show_MenuItem(MenuItem_3);
+      show_MenuItem(MenuItem_4);
+      break;
+    
+    case MenuItem_4:
+      menu_Active = MenuItem_1;
+      show_MenuItem(MenuItem_4);
+      show_MenuItem(MenuItem_1);
+      break;
+    
+    default:
+      menu_Active = MenuItem_1;
+      break;
     }
   }
 }
 
+/**
+ * @brief Process selected menu item
+ */
 static void btn2_handler(button_state_t state) {
+  static int s3 = 0; // menu_item3 state
+
   if (state == BTN_st_Pressed) {
-    charger_Profile ++;
-    if (charger_Profile >= ACCUM_PRIFILE_NUM) {
-      charger_Profile = 0;
-    }
-    show_MenuItem(2);
-  }
-}
 
-static void btn3_handler(button_state_t state) {
-  static int s = 0;
+    switch (menu_Active) {
+    case MenuItem_1:
+      if (charger_State == Stop) {
+        switch (charger_Channel) {
+        case INA3221_CH1:
+          charger_Channel = INA3221_CH2;
+          break;
+        case INA3221_CH2:
+          charger_Channel = INA3221_CH3;
+          break;
+        case INA3221_CH3:
+          charger_Channel = INA3221_CH1;
+          break;
+        
+        default:
+          charger_Channel = INA3221_CH1;
+          break;
+        }
+        show_MenuItem(MenuItem_1);
+      }
+      break;
+    
+    case MenuItem_2:
+      charger_Profile ++;
+      if (charger_Profile >= ACCUM_PRIFILE_NUM) {
+        charger_Profile = 0;
+      }
+      show_MenuItem(MenuItem_2);
+      break;
 
-  if (state == BTN_st_Pressed) {
-    if (s == 0) {
-      s ++;
-      gwinPrintf(GW1, "Show Charge results\n");
-      gwinPrintf(GW1, "Button 2 pressed\n");
-      Capacity_I = ch_Capacity_I;
-      Capacity_P = ch_Capacity_P;
-      Timer = chTimer;
-    } else {
-      s = 0;
-      gwinPrintf(GW1, "Show Decharge results\n");
-      gwinPrintf(GW1, "Button 3 pressed\n");
-      Capacity_I = dech_Capacity_I;
-      Capacity_P = dech_Capacity_P;
-      Timer = dechTimer;
-    }
+    case MenuItem_3:
+      if (s3 == 0) {
+        s3 ++;
+        gwinPrintf(GW1, "Show Charge results\n");
+        Capacity_I = ch_Capacity_I;
+        Capacity_P = ch_Capacity_P;
+        Timer = chTimer;
+      } else {
+        s3 = 0;
+        gwinPrintf(GW1, "Show Decharge results\n");
+        Capacity_I = dech_Capacity_I;
+        Capacity_P = dech_Capacity_P;
+        Timer = dechTimer;
+      }
 
-    chEvtBroadcast(&ina_all_event);
-    chEvtBroadcast(&time_event);
-  }
-}
+      chEvtBroadcast(&ina_all_event);
+      chEvtBroadcast(&time_event);
+      break;
 
-static void btn4_handler(button_state_t state) {
-  if (state == BTN_st_Pressed) {
-    gwinPrintf(GW1, "Button 4 pressed\n");
+    case MenuItem_4:
+      switch(charger_State) {
+      case Stop:
+        charger_State = Charge1;
+        show_MenuItem(MenuItem_4);
+        break;
 
-    switch(charger_State) {
-    case Stop:
-      charger_State = Charge1;
-      //gwinPrintf(GW1, "Change state to Charge_1\n");
-      gdispFillStringBox(1, 91, 78, 15, "[4] Stop", font1, Yellow, Blue, gJustifyLeft);
+      default:
+        charger_State = Stop;
+        Current = 0;
+        Voltage = 0;
+        Power = 0;
+        show_MenuItem(MenuItem_4);
+        break;
+      }
+
+      chEvtBroadcast(&chrgr_st_event);
       break;
 
     default:
-      charger_State = Stop;
-      Current = 0;
-      Voltage = 0;
-      Power = 0;
-      //gwinPrintf(GW1, "Change state to Stop\n");
-      gdispFillStringBox(1, 91, 78, 15, "[4] Start", font1, Yellow, Blue, gJustifyLeft);
       break;
-    }
+    } /* switch menu_Active */
 
-    chEvtBroadcast(&chrgr_st_event);
-  }
-}
+  } /* BTN pressed */ 
+} /* end of btn2_handler() */
 
+/**
+ * @brief Virtual timer callback. Change charger state.
+ */
 static void mode_vt_cb(virtual_timer_t *vtp, void * st) {
   (void)vtp;
 
-  charger_State = (charger_state_t)st;
+  charger_State = (charger_state_t)st; // new charger state
 
   chSysLockFromISR();
   chEvtBroadcastI(&chrgr_st_event);
   chSysUnlockFromISR();
 }
 
+/** 
+ * @brief Show/update menu items.
+ */
 static void show_MenuItem(const int item) {
-  char buf[16];
+  char buf1[16] = {0};
+  char buf2[16] = {0};
   int tmp;
+  coord_t y1=0, y2=0;
+  static const coord_t x=1, cx=78, cy=15;
+  static const justify_t j1 = gJustifyLeft;
+  justify_t j2 = gJustifyLeft;
+  color_t fgc, bgc;
+
+  if (item == menu_Active) {
+    fgc = menu_Colors.activeFG;
+    bgc = menu_Colors.activeBG;
+  } else {
+    fgc = menu_Colors.inactiveFG;
+    bgc = menu_Colors.inactiveBG;
+  }
 
   switch (item) {
-  case 1:
+  case MenuItem_1:
     tmp = (int)charger_Channel + 1;
     gwinPrintf(GW1, "Channel #%u selected.\n", tmp);
 
-    chsnprintf(buf, 10, "[1] ch.%u", tmp);
-    gdispFillStringBox(1, 1, 78, 15,  buf, font1, Yellow, Blue, gJustifyLeft);
+    chsnprintf(buf1, 10, "[1] ch.%u", tmp);
+    chsnprintf(buf2, 12, "%2uA", charger_Channels[charger_Channel].max_current/1000);
 
-    chsnprintf(buf, 12, "%2uA", charger_Channels[charger_Channel].max_current/1000);
-    gdispFillStringBox(1, 16, 78, 15, buf, font1, Yellow, Blue, gJustifyCenter);
+    y1 = 1;
+    y2 = 16;
+    j2 = gJustifyCenter;
     break;
   
-  case 2:
+  case MenuItem_2:
     gwinPrintf(GW1, "Select profile for %s\n", Profile[charger_Profile].Description);
-    chsnprintf(buf, 10, "[2] %s", Profile[charger_Profile].Chemistry);
-    gdispFillStringBox(1, 31, 78, 15, buf, font1, Yellow, Blue, gJustifyLeft);
+
+    chsnprintf(buf1, 10, "[2] %s", Profile[charger_Profile].Chemistry);
     tmp = Profile[charger_Profile].Capacity_mAh / 1000;
-    chsnprintf(buf, 10, "%s/%uAh", Profile[charger_Profile].VoltageNominal, tmp);
-    gdispFillStringBox(1, 46, 78, 15, buf, font1, Yellow, Blue, gJustifyCenter);
+    chsnprintf(buf2, 12, "%s/%uAh", Profile[charger_Profile].VoltageNominal, tmp);
+
+    y1 = 31;
+    y2 = 46;
+    j2 = gJustifyCenter;
     break;
 
-  case 3:
-    /* code */
+  case MenuItem_3:
+    chsnprintf(buf1, 12, "[3] show ch");
+    chsnprintf(buf2, 12, "dech results");
+    y1 = 61;
+    y2 = 76;
     break;
 
-  case 4:
-    /* code */
+  case MenuItem_4:
+    if (charger_State == Stop) {
+      chsnprintf(buf1, 12, "[4] Start");
+    } else {
+      chsnprintf(buf1, 12, "[4] Stop");
+    }
+    //chsnprintf(buf2, 12, "          ");
+    y1 = 91;
+    y2 = 106;
     break;
 
   default:
     break;
   }
+
+  gdispFillStringBox(x, y1, cx, cy, buf1, font1, fgc, bgc, j1);
+  gdispFillStringBox(x, y2, cx, cy, buf2, font1, fgc, bgc, j2);
 }
 
-/*
- * Read Voltage & Current, update screen info.
+/**
+ * @brief Read Voltage & Current, update screen info.
  */
 static void ina_Process(void) {
   int32_t current;
@@ -749,8 +862,8 @@ static void ina_Process(void) {
   }
 }
 
-/*
- * GPT callback. One second pulse.
+/**
+ * @brief GPT callback. One 16 pulse per second.
  */
 static void gpt_cb(GPTDriver *gptp) {
   (void)gptp;

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно