diff --git a/src/main.c b/src/main.c index 7d86220..eaf2a50 100644 --- a/src/main.c +++ b/src/main.c @@ -47,6 +47,8 @@ enum MODES { #define SCAN_BOOTLD_BTN (1 << 3) #define BLE_NEXT_STEP (1 << 4) +#define CHARGE_STT_PIN GPIO_Pin_0 // PA0 + static tmosTaskID common_taskid = INVALID_TASK_ID ; volatile uint16_t fb[LED_COLS] = {0}; @@ -104,6 +106,8 @@ void poweroff() // Configure wake-up GPIOA_ModeCfg(KEY1_PIN, GPIO_ModeIN_PD); GPIOA_ITModeCfg(KEY1_PIN, GPIO_ITMode_RiseEdge); + GPIOA_ModeCfg(CHARGE_STT_PIN, GPIO_ModeIN_PU); + GPIOA_ITModeCfg(CHARGE_STT_PIN, GPIO_ITMode_FallEdge); PFIC_EnableIRQ(GPIO_A_IRQn); PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay); @@ -310,6 +314,93 @@ static void debug_init() UART1_BaudRateCfg(921600); } +uint16_t adcBuff[40]; +static int read_batt_raw() +{ + int ret = 0; + /* adc 1 - pa5 */ + PRINT("\n2.Single channel sampling...\n"); + GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeIN_Floating); + ADC_ExtSingleChSampInit(SampleFreq_3_2, ADC_PGA_0); + + int16_t RoughCalib_Value = ADC_DataCalib_Rough(); + PRINT("RoughCalib_Value =%d \n", RoughCalib_Value); + + ADC_ChannelCfg(1); + + for(int i = 0; i < 20; i++) { + adcBuff[i] = ADC_ExcutSingleConver() + RoughCalib_Value; + ret += adcBuff[i]; + } + for(int i = 0; i < 20; i++) { + PRINT("%d \n", adcBuff[i]); + } + + return ret / 20; +} + +static int is_charging() +{ + GPIOA_ModeCfg(CHARGE_STT_PIN, GPIO_ModeIN_PU); + return GPIOA_ReadPortPin(CHARGE_STT_PIN) == 0; +} + +static void disp_bat_stt(int bat_percent, int col, int row) +{ + if (bat_percent < 0) { + xbm2fb(&batwarn_xbm, fb, col, row); + return; + } + + xbm2fb(&bat_xbm, fb, col, row); + bat_percent /= 10; + for (int i=1; i <= bat_percent; i++) { + fb[col + i] = fb[col]; + } +} + +#define ZERO_PERCENT_THRES (3.3) +#define _100_PERCENT_THRES (4.2) +#define ADC_MAX_VAL (4096.0) // 12 bit +#define ADC_MAX_VOLT (2.1) // Volt +#define R1 (182.0) // kOhm +#define R2 (100.0) // kOhm +#define PERCENT_RANGE (_100_PERCENT_THRES - ZERO_PERCENT_THRES) +#define VOLT_DIV(v) ((v) / (R1 + R2) * R2) // Voltage divider +#define VOLT_DIV_INV(v) ((v) / R2 * (R1 + R2)) // .. Inverse +#define ADC2VOLT(raw) ((raw) / ADC_MAX_VAL * ADC_MAX_VOLT) +#define VOLT2ADC(volt) ((volt) / ADC_MAX_VOLT * ADC_MAX_VAL) + +static int bat_raw2percent(int r) +{ + float vadc = ADC2VOLT(r); + float vbat = VOLT_DIV_INV(vadc); + float strip = vbat - ZERO_PERCENT_THRES; + if (strip < PERCENT_RANGE) { + // Negative values meaning the battery is not connected or died + return (int)(strip / PERCENT_RANGE * 100.0); + } + return 100; +} + +static void disp_charging() +{ + int blink = 0; + while (1) { + int percent = bat_raw2percent(read_batt_raw()); + + if (is_charging()) { + disp_bat_stt(blink ? percent : 0, 7, 2); + blink = !blink; + DelayMs(300); + } else { + disp_bat_stt(percent, 7, 2); + DelayMs(500); + return; + } + } +} + int main() { SetSysClock(CLK_SOURCE_PLL_60MHz); @@ -327,6 +418,8 @@ int main() PFIC_EnableIRQ(TMR0_IRQn); bmlist_init(LED_COLS * 4); + + disp_charging(); play_splash(&splash, 0, 0); diff --git a/src/res/bat-icon.xbm b/src/res/bat-icon.xbm new file mode 100644 index 0000000..c62e206 --- /dev/null +++ b/src/res/bat-icon.xbm @@ -0,0 +1,5 @@ +#define bat_icon_width 13 +#define bat_icon_height 7 +static unsigned char bat_icon_bits[] = { + 0xff, 0x0f, 0x01, 0x08, 0x01, 0x18, 0x01, 0x18, 0x01, 0x18, 0x01, 0x08, + 0xff, 0x0f }; diff --git a/src/res/bat-warn-icon.xbm b/src/res/bat-warn-icon.xbm new file mode 100644 index 0000000..6c0276b --- /dev/null +++ b/src/res/bat-warn-icon.xbm @@ -0,0 +1,5 @@ +#define bat_warn_icon_width 13 +#define bat_warn_icon_height 7 +static unsigned char bat_warn_icon_bits[] = { + 0xff, 0x0f, 0x01, 0x08, 0x01, 0x18, 0xf5, 0x1b, 0x01, 0x18, 0x01, 0x08, + 0xff, 0x0f }; diff --git a/src/resource.c b/src/resource.c index ed4766b..a8f8d14 100644 --- a/src/resource.c +++ b/src/resource.c @@ -2,6 +2,8 @@ #include "res/bluetooth.xbm" #include "res/foss-asia-2.xbm" +#include "res/bat-icon.xbm" +#include "res/bat-warn-icon.xbm" xbm_t bluetooth = { .bits = bluetooth_bits, @@ -15,4 +17,18 @@ xbm_t splash = { .w = foss_asia_2_width, .h = foss_asia_2_height, .fh = 11 +}; + +xbm_t bat_xbm = { + .bits = bat_icon_bits, + .w = bat_icon_width, + .h = bat_icon_height, + .fh = 11 +}; + +xbm_t batwarn_xbm = { + .bits = bat_warn_icon_bits, + .w = bat_warn_icon_width, + .h = bat_warn_icon_height, + .fh = 11 }; \ No newline at end of file diff --git a/src/resource.h b/src/resource.h index df91e37..968d725 100644 --- a/src/resource.h +++ b/src/resource.h @@ -6,5 +6,7 @@ extern xbm_t bluetooth; extern xbm_t splash; +extern xbm_t bat_xbm; +extern xbm_t batwarn_xbm; #endif /* __RES_H__ */