diff --git a/radio/src/targets/st16/CMakeLists.txt b/radio/src/targets/st16/CMakeLists.txt index 2bb703028..5ff843960 100644 --- a/radio/src/targets/st16/CMakeLists.txt +++ b/radio/src/targets/st16/CMakeLists.txt @@ -146,6 +146,7 @@ add_library(board OBJECT EXCLUDE_FROM_ALL ${BOARD_COMMON_SRC} ${TARGET_SRC_DIR}/led_driver.cpp ${TARGET_SRC_DIR}/touch_driver.cpp + ${TARGET_SRC_DIR}/battery_driver.cpp targets/common/arm/stm32/heartbeat_driver.cpp targets/common/arm/stm32/lsm6ds_driver.cpp targets/common/arm/stm32/mixer_scheduler_driver.cpp diff --git a/radio/src/targets/st16/battery_driver.cpp b/radio/src/targets/st16/battery_driver.cpp new file mode 100644 index 000000000..415a14d4c --- /dev/null +++ b/radio/src/targets/st16/battery_driver.cpp @@ -0,0 +1,348 @@ +/* + * Copyright (C) EdgeTX + * + * Based on code named + * opentx - https://github.com/opentx/opentx + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "edgetx.h" +#include "battery_driver.h" +#include "boards/generic_stm32/rgb_leds.h" +#include "bsp_io.h" + +#define __BATTERY_DRIVER_C__ + +#define BATTERY_W 140 +#define BATTERY_H (LCD_H - 120) +#define BATTERY_TOP ((LCD_H - BATTERY_H)/2) +#define BATTERY_CONNECTOR_W 32 +#define BATTERY_CONNECTOR_H 10 +#define BATTERY_BORDER 4 +#define BATTERY_W_INNER (BATTERY_W - 2*BATTERY_BORDER) +#define BATTERY_H_INNER (BATTERY_H - 2*BATTERY_BORDER) +#define BATTERY_TOP_INNER (BATTERY_TOP + BATTERY_BORDER) + +#define UCHARGER_SAMPLING_CNT 10 +#define UCHARGER_CHARGING_SAMPLING_CNT 10 + +typedef struct +{ + bool hasCharger : 1; + bool isChargeEnd : 1; + bool isChargerDetectionReady : 1; + bool isChargingDetectionReady : 1; + bool isHighCurrent : 1; + uint8_t chargerSamplingCount; + uint8_t chargingSamplingCount; + uint8_t chargeEndSamplingCount; +} STRUCT_BATTERY_CHARGER; + +STRUCT_BATTERY_CHARGER uCharger; // USB charger + +void chargerDetection(STRUCT_BATTERY_CHARGER* charger, uint8_t chargerPinActive, uint8_t samplingCountThreshold) +{ + if ((charger->hasCharger && chargerPinActive) || (!charger->hasCharger && !chargerPinActive)) + { + charger->chargerSamplingCount = 0; + } + else + { + charger->chargerSamplingCount++; + if (charger->chargerSamplingCount >= samplingCountThreshold) + { + charger->chargerSamplingCount = 0; + charger->hasCharger = !charger->hasCharger; + charger->isChargerDetectionReady = true; + } + } +} + +void resetChargeEndDetection(STRUCT_BATTERY_CHARGER* charger) +{ + charger->isChargeEnd = false; + charger->isChargingDetectionReady = false; + charger->chargingSamplingCount = 0; + charger->isHighCurrent = false; +} + +void chargeEndDetection(STRUCT_BATTERY_CHARGER* charger, uint8_t chargeEndPinActive, uint8_t samplingCountThreshold) +{ + if (charger->isChargeEnd) + { + if (chargeEndPinActive) + { + charger->chargingSamplingCount = 0; + if (charger->isChargingDetectionReady) + { + charger->chargeEndSamplingCount = 0; + } + else + { + charger->chargeEndSamplingCount++; + if (charger->chargeEndSamplingCount >= samplingCountThreshold) + { + charger->chargeEndSamplingCount = 0; + charger->isChargingDetectionReady = true; + } + } + } + else + { + charger->chargeEndSamplingCount = 0; + charger->chargingSamplingCount++; + if (charger->chargingSamplingCount >= samplingCountThreshold) + { + charger->chargingSamplingCount = 0; + charger->isChargeEnd = false; + charger->isChargingDetectionReady = true; + } + } + } + else + { + if (!chargeEndPinActive) + { + charger->chargeEndSamplingCount = 0; + if (charger->isChargingDetectionReady) + { + charger->chargingSamplingCount = 0; + } + else + { + charger->chargingSamplingCount++; + if (charger->chargingSamplingCount >= samplingCountThreshold) + { + charger->chargingSamplingCount = 0; + charger->isChargingDetectionReady = true; + } + } + } + else + { + charger->chargingSamplingCount = 0; + charger->chargeEndSamplingCount++; + if (charger->chargeEndSamplingCount >= samplingCountThreshold) + { + charger->chargeEndSamplingCount = 0; + charger->isChargeEnd = true; + charger->isChargingDetectionReady = true; + } + } + } +} + +uint16_t get_battery_charge_state() +{ + uint16_t state = CHARGE_UNKNOWN; + + chargerDetection(&uCharger, IS_UCHARGER_ACTIVE(), UCHARGER_SAMPLING_CNT); + if (uCharger.isChargerDetectionReady) + { + if (uCharger.hasCharger) // USB charger can be detected properly no matter it is enabled or not + { + ENABLE_UCHARGER(); + chargeEndDetection(&uCharger, IS_UCHARGER_CHARGE_END_ACTIVE(), UCHARGER_CHARGING_SAMPLING_CNT); + if (uCharger.isChargingDetectionReady) + { + if (uCharger.isChargeEnd) + { + state = CHARGE_FINISHED; + } + else + { + state = CHARGE_STARTED; + } + } + } + else + { + resetChargeEndDetection(&uCharger); + + // Disable USB charger if it is not present, so that wireless charger can be detected properly + DISABLE_UCHARGER(); + } + } + + return state; +} + +bool isChargerActive() +{ + while (!uCharger.isChargerDetectionReady) + { + get_battery_charge_state(); + delay_ms(10); + } + return uCharger.hasCharger; +} + +void battery_charge_init() +{ + // USB charger status pins + gpio_init(UCHARGER_GPIO, GPIO_IN, GPIO_PIN_SPEED_LOW); + gpio_init(UCHARGER_CHARGE_END_GPIO, GPIO_IN_PD, GPIO_PIN_SPEED_LOW); + + // USB charger state init + ENABLE_UCHARGER(); + uCharger.hasCharger = !IS_UCHARGER_ACTIVE(); // Init for sampling count works + uCharger.isChargerDetectionReady = false; + resetChargeEndDetection(&uCharger); +} + +void ledChargingInfo(uint16_t chargeState) { +#if defined(LED_STRIP_GPIO) + static int ledIdx = LED_CHARGING_END; + if (ledIdx == LED_CHARGING_END) + ledIdx = LED_CHARGING_START; + else + ledIdx += LED_CHARGING_START > LED_CHARGING_END ? -1 : 1; + for (uint8_t i = 0; i < LED_STRIP_LENGTH; i++) { + uint8_t green = 0; + if (CHARGE_FINISHED == chargeState) { + if (LED_CHARGING_START > LED_CHARGING_END && i <= LED_CHARGING_START && i >= LED_CHARGING_END) + { + green = 20; + } + if (LED_CHARGING_START <= LED_CHARGING_END && i >= LED_CHARGING_START && i <= LED_CHARGING_END) + { + green = 20; + } + } else if (ledIdx == i) { + green = 20; + } + rgbSetLedColor(i, 0, green, 0); + } + rgbLedColorApply(); +#endif +} + +void drawChargingInfo(uint16_t chargeState) { + static int progress = 0; + const char* text = chargeState == CHARGE_STARTED ? STR_BATTERYCHARGING : STR_BATTERYFULL; + int h = 0; + LcdFlags color = 0; + if (CHARGE_STARTED == chargeState) + { + if (progress >= 100) + { + progress = 0; + } + else + { + progress += 25; + } + text = STR_BATTERYCHARGING; + h = ((BATTERY_H_INNER * progress) / 100); + color = COLOR_THEME_EDIT; + } + else if (CHARGE_FINISHED == chargeState) + { + text = STR_BATTERYFULL; + h = BATTERY_H_INNER; + color = COLOR_THEME_EDIT; + } + else + { + text = STR_BATTERYNONE; + h = BATTERY_H_INNER; + color = COLOR_THEME_PRIMARY1; + } + + BACKLIGHT_ENABLE(); + lcd->drawSizedText(LCD_W / 2, LCD_H - 50, text, strlen(text), CENTERED | COLOR_THEME_PRIMARY2); + + lcd->drawFilledRect((LCD_W - BATTERY_W) / 2, BATTERY_TOP, BATTERY_W, BATTERY_H, SOLID, COLOR_THEME_PRIMARY2); + lcd->drawFilledRect((LCD_W - BATTERY_W_INNER) / 2, BATTERY_TOP_INNER, BATTERY_W_INNER, BATTERY_H_INNER, SOLID, COLOR_THEME_PRIMARY1); + + lcd->drawFilledRect((LCD_W - BATTERY_W_INNER) / 2, BATTERY_TOP_INNER + BATTERY_H_INNER - h, BATTERY_W_INNER, h, SOLID, color); + lcd->drawFilledRect((LCD_W - BATTERY_CONNECTOR_W) / 2, BATTERY_TOP - BATTERY_CONNECTOR_H, BATTERY_CONNECTOR_W, BATTERY_CONNECTOR_H, SOLID, COLOR_THEME_PRIMARY2); +} +#define CHARGE_INFO_DURATION 5000 // ms + +//this method should be called by timer interrupt or by GPIO interrupt +void handle_battery_charge(uint32_t last_press_time) +{ +#if !defined(SIMU) + static uint32_t updateTime = 0; + static uint16_t lastState = CHARGE_UNKNOWN; + static uint32_t info_until = 0; + static bool lcdInited = false; + + uint32_t now = timersGetMsTick(); + uint16_t chargeState = get_battery_charge_state(); + if (chargeState != CHARGE_UNKNOWN) { + + if (lastState != chargeState) { + //avoid positive check when none and unknown + if (lastState + chargeState > 1) { + //charge state changed - last state known + info_until = now + (CHARGE_INFO_DURATION); + } + } + //power buttons pressed + else if (now - last_press_time < POWER_ON_DELAY) { + info_until = now + CHARGE_INFO_DURATION; + } + lastState = chargeState; + } + + if (updateTime == 0 || ((timersGetMsTick() - updateTime) >= 500)) + { + updateTime = timersGetMsTick(); + ledChargingInfo(chargeState); + + if(now > info_until) { + info_until = 0; + lcd->clear(); + BACKLIGHT_DISABLE(); + if(lcdInited) { + lcdOff(); + } + } else { + if (!lcdInited) { + backlightInit(); + lcdInit(); + lcdInitDisplayDriver(); + lcdInited = true; + } else { + lcdOn(); + } + lcdInitDirectDrawing(); + lcd->clear(); + drawChargingInfo(chargeState); + + // DEBUG INFO +#if 0 + char buffer[1024]; + + sprintf(buffer, "%d,%d,%d,%d", uCharger.isChargerDetectionReady, uCharger.hasCharger, IS_UCHARGER_ACTIVE(), uCharger.chargerSamplingCount); + lcd->drawSizedText(100, 10, buffer, strlen(buffer), CENTERED | COLOR_THEME_PRIMARY2); + + sprintf(buffer, "%d,%d,%d,%d,%d,", uCharger.isChargingDetectionReady, uCharger.isChargeEnd, IS_UCHARGER_CHARGE_END_ACTIVE(), uCharger.chargingSamplingCount, uCharger.chargeEndSamplingCount); + lcd->drawSizedText(100, 40, buffer, strlen(buffer), CENTERED | COLOR_THEME_PRIMARY2); + + sprintf(buffer, "%d", isChargerActive()); + lcd->drawSizedText(100, 130, buffer, strlen(buffer), CENTERED | COLOR_THEME_PRIMARY2); +#endif + + lcdRefresh(); + } + + } +#endif +} + diff --git a/radio/src/targets/st16/battery_driver.h b/radio/src/targets/st16/battery_driver.h index 2b905a3ee..eaf85365b 100644 --- a/radio/src/targets/st16/battery_driver.h +++ b/radio/src/targets/st16/battery_driver.h @@ -22,9 +22,30 @@ /*************************************************************************************************** ***************************************************************************************************/ -#pragma once +#ifndef __BATTERY_DRIVER_H__ + #define __BATTERY_DRIVER_H__ /*************************************************************************************************** ***************************************************************************************************/ -#define IS_UCHARGER_ACTIVE() gpio_read(UCHARGER_GPIO) +#include "board.h" +#include "hal.h" + +#include "hal/gpio.h" +#include "stm32_gpio.h" + +enum ChargeState +{ + CHARGE_UNKNOWN, + CHARGE_NONE, + CHARGE_STARTED, + CHARGE_FINISHED +}; + +extern void battery_charge_init(); +extern void handle_battery_charge(uint32_t last_press_time); +extern uint16_t get_battery_charge_state(); +extern uint16_t getBatteryVoltage(); // returns current battery voltage in 10mV steps +extern bool isChargerActive(); + +#endif diff --git a/radio/src/targets/st16/board.cpp b/radio/src/targets/st16/board.cpp index 2d19d315e..85f5fb841 100644 --- a/radio/src/targets/st16/board.cpp +++ b/radio/src/targets/st16/board.cpp @@ -75,25 +75,6 @@ extern "C" void initialise_monitor_handles(); extern "C" void flushFTL(); #endif -static void led_strip_charge_animation(uint8_t ledOn) -{ - for (uint8_t i = 0; i < LED_STRIP_LENGTH; i++) { - if (i == ledOn) - ws2812_set_color(i, 10, 10, 10); - else - ws2812_set_color(i, 0, 0, 0); - } - ws2812_update(&_led_timer); -} - -static void led_strip_off() -{ - for (uint8_t i = 0; i < LED_STRIP_LENGTH; i++) { - ws2812_set_color(i, 0, 0, 0); - } - ws2812_update(&_led_timer); -} - void INTERNAL_MODULE_ON() { bsp_output_clear(BSP_INT_PWR); @@ -213,6 +194,9 @@ void boardInit() flashRegisterDriver(FLASH_BANK1_BASE, BOOTLOADER_SIZE, &stm32_flash_driver); flashRegisterDriver(QSPI_BASE, QSPI_FLASH_SIZE, &extflash_driver); + board_trainer_init(); + battery_charge_init(); + // init_trainer(); flysky_gimbal_init(); @@ -222,61 +206,48 @@ void boardInit() rgbLedInit(); #endif -#if !defined(DEBUG_SEGGER_RTT) - // prime debounce state... - usbPlugged(); + rotaryEncoderInit(); - if (usbPlugged()) { - delaysInit(); - uint8_t ledOn = 0; - while (usbPlugged()) { - if(IS_UCHARGER_ACTIVE()) { - led_strip_charge_animation(ledOn++); - if (ledOn == LED_STRIP_LENGTH) - ledOn = 0; +#if !defined(DEBUG_SEGGER_RTT) + + uint32_t press_start = 0; + uint32_t press_end = 0; + rotenc_t lastEncoderValue = 0; + + if (UNEXPECTED_SHUTDOWN()) { + pwrOn(); + } else if (isChargerActive()) { + while (true) { + pwrOn(); + uint32_t now = timersGetMsTick(); + if (pwrPressed()) { + press_end = now; + if (press_start == 0) press_start = now; + if ((now - press_start) > POWER_ON_DELAY) { + break; + } + } else if (!isChargerActive()) { + boardOff(); + } else { + uint32_t press_end_touch = press_end; + rotenc_t value = rotaryEncoderGetValue(); + if (value != lastEncoderValue) { + lastEncoderValue = value; + press_end_touch = timersGetMsTick(); + } + press_start = 0; + handle_battery_charge(press_end_touch); + delay_ms(10); + press_end = 0; } - else { - led_strip_off(); - } - delay_ms(1000); } - while(1) // Wait power to drain - boardOff(); } + #endif - led_strip_off(); - - // uint32_t press_start = 0; - // uint32_t press_end = 0; - - // if (UNEXPECTED_SHUTDOWN()) { - pwrOn(); - // } else if (isChargerActive()) { - // while (true) { - // pwrOn(); - // uint32_t now = get_tmr10ms(); - // if (pwrPressed()) { - // press_end = now; - // if (press_start == 0) press_start = now; - // if ((now - press_start) > POWER_ON_DELAY) { - // break; - // } - // } else if (!isChargerActive()) { - // boardOff(); - // } else { - // uint32_t press_end_touch = press_end; - // press_start = 0; - // handle_battery_charge(press_end_touch); - // delay_ms(10); - // press_end = 0; - // } - // } - // } - + rgbLedClearAll(); keysInit(); switchInit(); - rotaryEncoderInit(); touchPanelInit(); audioInit(); adcInit(&_adc_driver); @@ -306,6 +277,18 @@ void boardOff() rtcDisableBackupReg(); +#if !defined(BOOT) +#if defined(LED_STRIP_GPIO) + rgbLedClearAll(); +#endif + if (isChargerActive()) + { +// RTC->BKP0R = SOFTRESET_REQUEST; + NVIC_SystemReset(); + } + else +#endif + // RTC->BKP0R = SHUTDOWN_REQUEST; pwrOff(); diff --git a/radio/src/targets/st16/board.h b/radio/src/targets/st16/board.h index 0fb93d33f..863ead25e 100644 --- a/radio/src/targets/st16/board.h +++ b/radio/src/targets/st16/board.h @@ -195,9 +195,10 @@ bool isBacklightEnabled(); : g_eeGeneral.blOffBright); \ } -#define USB_NAME "ST16" -#define USB_MANUFACTURER 'F', 'l', 'y', 'S', 'k', 'y', ' ', ' ' /* 8 bytes */ -#define USB_PRODUCT 'S', 'T', '1', '6', ' ', ' ', ' ', ' ' /* 8 Bytes */ +#define IS_UCHARGER_ACTIVE() gpio_read(UCHARGER_GPIO) ? 1 : 0 +#define IS_UCHARGER_CHARGE_END_ACTIVE() gpio_read(UCHARGER_CHARGE_END_GPIO) ? 0 : 1 +#define ENABLE_UCHARGER() bsp_output_set(BSP_CHARGE_EN); +#define DISABLE_UCHARGER() bsp_output_clear(BSP_CHARGE_EN) #if defined(__cplusplus) && !defined(SIMU) } diff --git a/radio/src/targets/st16/bsp_io.cpp b/radio/src/targets/st16/bsp_io.cpp index db2d565f1..0cebfde95 100644 --- a/radio/src/targets/st16/bsp_io.cpp +++ b/radio/src/targets/st16/bsp_io.cpp @@ -71,8 +71,7 @@ int bsp_io_init() { // init outputs BSP_CHECK(pca95xx_init(&output_exp, BSP_OUT_I2C_BUS, BSP_OUT_I2C_ADDR)); - BSP_CHECK(pca95xx_write(&output_exp, BSP_OUT_MASK, - BSP_CHARGE_EN | BSP_EXT_PWR | BSP_INT_PWR)); + BSP_CHECK(pca95xx_write(&output_exp, BSP_OUT_MASK, BSP_EXT_PWR | BSP_INT_PWR)); BSP_CHECK(pca95xx_set_direction(&output_exp, BSP_OUT_MASK, 0)); // init inputs diff --git a/radio/src/targets/st16/hal.h b/radio/src/targets/st16/hal.h index 39e4185a6..ae2b08eb0 100644 --- a/radio/src/targets/st16/hal.h +++ b/radio/src/targets/st16/hal.h @@ -211,7 +211,8 @@ #define PWR_ON_GPIO GPIO_PIN(GPIOI, 8) // PE.04 // Chargers (USB and wireless) -#define UCHARGER_GPIO GPIO_PIN(GPIOD, 4) // PD.02 input +#define UCHARGER_GPIO GPIO_PIN(GPIOC, 0) // PC.00 +#define UCHARGER_CHARGE_END_GPIO GPIO_PIN(GPIOD, 4) // PD.04 // TODO! Check IOLL1 to PI.01 connectivity! @@ -394,6 +395,8 @@ #define LED_STRIP_TIMER_DMA_IRQHandler DMA1_Stream0_IRQHandler #define LED_STRIP_REFRESH_PERIOD 50 //ms #define STATUS_LEDS +#define LED_CHARGING_START 12 +#define LED_CHARGING_END 23 // Internal Module