|
@@ -21,6 +21,8 @@
|
|
|
#include "buttons.h"
|
|
|
#include "INA3221.h"
|
|
|
|
|
|
+#define INA_SCAN_PERIOS_MS 100
|
|
|
+
|
|
|
/* Type definitions */
|
|
|
typedef enum chrgr_state {
|
|
|
Stop = 0,
|
|
@@ -73,11 +75,21 @@ static const charger_channel_t charger_Channels[INA3221_CH_NUM] = {
|
|
|
{20, 8190}, {50, 3276}, {10, 16380}
|
|
|
};
|
|
|
|
|
|
+/* Privae functions */
|
|
|
+static void prepare_Screen(void);
|
|
|
+static void ina_Process(virtual_timer_t *vtp, void *p);
|
|
|
+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);
|
|
|
+
|
|
|
/* Private variables */
|
|
|
+static virtual_timer_t ina_vt;
|
|
|
static charger_state_t charger_State;
|
|
|
static uint8_t current_Channel;
|
|
|
-static int32_t Current, sumCurrent=0;
|
|
|
-static uint32_t Voltage, sumVoltage=0;
|
|
|
+static int32_t Current, sumCurrent=0, secCurrent;
|
|
|
+static uint32_t Voltage, sumVoltage=0, secVoltage;
|
|
|
+static uint32_t secPower, Capacity_I=0, Capacity_P=0;
|
|
|
static int INA_Present = 0;
|
|
|
static BaseSequentialStream * chp = (BaseSequentialStream*) &SD1;
|
|
|
GHandle GW1; // The handle for our console
|
|
@@ -85,53 +97,7 @@ ina3221_t ina3221;
|
|
|
static gFont font1;
|
|
|
static gFont font2;
|
|
|
static time_cnt_t Timer = {0};
|
|
|
-
|
|
|
-/* Privae functions */
|
|
|
-static void prepareScreen(void);
|
|
|
-static void btn1_handler(void);
|
|
|
-static void btn2_handler(void);
|
|
|
-static void btn3_handler(void);
|
|
|
-static void btn4_handler(void);
|
|
|
-
|
|
|
-/*
|
|
|
- * Read Voltage, Current, update screen info.
|
|
|
- */
|
|
|
-static THD_WORKING_AREA(waInaThread, 128);
|
|
|
-static THD_FUNCTION(InaThread, arg) {
|
|
|
- (void)arg;
|
|
|
- systime_t time;
|
|
|
- int32_t tmp1, tmp2;
|
|
|
- char buf[12];
|
|
|
-
|
|
|
- while (true) {
|
|
|
- time = chVTGetSystemTimeX();
|
|
|
-
|
|
|
- if (INA_Present != 0) {
|
|
|
- Current = INA3221_getCurrent(&ina3221, (ina3221_ch_t)current_Channel);
|
|
|
- Voltage = INA3221_getVoltage(&ina3221, (ina3221_ch_t)current_Channel);
|
|
|
-
|
|
|
- tmp1 = Voltage / 1000;
|
|
|
- tmp2 = Voltage % 1000;
|
|
|
- chsnprintf(buf, 10, "V:%2d.%03u", tmp1, tmp2);
|
|
|
- gdispFillStringBox(1, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
|
|
|
-
|
|
|
- if (Current < 0) {
|
|
|
- Current = 1 - Current;
|
|
|
- }
|
|
|
- tmp1 = Current / 1000;
|
|
|
- tmp2 = Current % 1000;
|
|
|
- chsnprintf(buf, 10, "I:%2d.%03u", tmp1, tmp2);
|
|
|
- gdispFillStringBox(160, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
|
|
|
-
|
|
|
- if (charger_State != Stop) {
|
|
|
- sumCurrent += Current;
|
|
|
- sumVoltage += Voltage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- chThdSleepUntil(time += TIME_MS2I(100));
|
|
|
- }
|
|
|
-}
|
|
|
+static btn_hndlr bha[Button_Num] = {btn1_handler, btn2_handler, btn3_handler, btn4_handler};
|
|
|
|
|
|
/*
|
|
|
* Green LED blinker thread, times are in milliseconds.
|
|
@@ -148,41 +114,6 @@ static THD_FUNCTION(Thread1, arg) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
- * Pointer to the Buton handler thread.
|
|
|
- */
|
|
|
-thread_t * btn_thread;
|
|
|
-
|
|
|
-static THD_WORKING_AREA(waBTNThread, 128);
|
|
|
-static THD_FUNCTION(BTNThread, arg) {
|
|
|
- (void)arg;
|
|
|
-
|
|
|
- /* Thread activity.*/
|
|
|
- while (true) {
|
|
|
- /* Waiting for any event.*/
|
|
|
- eventmask_t evt = chEvtWaitAny(ALL_EVENTS);
|
|
|
- palToggleLine(LINE_LED2);
|
|
|
-
|
|
|
- /* Serving events.*/
|
|
|
- if (evt & EVT_BTN1_PRS) {
|
|
|
- /* BTN1 event.*/
|
|
|
- btn1_handler();
|
|
|
- }
|
|
|
- if (evt & EVT_BTN2_PRS) {
|
|
|
- /* BTN2 event.*/
|
|
|
- btn2_handler();
|
|
|
- }
|
|
|
- if (evt & EVT_BTN3_PRS) {
|
|
|
- /* BTN3 event.*/
|
|
|
- btn3_handler();
|
|
|
- }
|
|
|
- if (evt & EVT_BTN4_PRS) {
|
|
|
- /* BTN4 event.*/
|
|
|
- btn4_handler();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
/*
|
|
|
* Application entry point.
|
|
|
*/
|
|
@@ -204,7 +135,7 @@ int main(void) {
|
|
|
* Initialize uGFX and the underlying system
|
|
|
*/
|
|
|
gfxInit();
|
|
|
- prepareScreen();
|
|
|
+ prepare_Screen();
|
|
|
|
|
|
gptStart(&GPTD5, &gpt_cfg);
|
|
|
|
|
@@ -223,14 +154,13 @@ int main(void) {
|
|
|
/*
|
|
|
* Starting the button handle thread.
|
|
|
*/
|
|
|
- btn_thread = chThdCreateStatic(waBTNThread, sizeof(waBTNThread),
|
|
|
- NORMALPRIO+1, BTNThread, NULL);
|
|
|
- buttons_Init(btn_thread);
|
|
|
+ buttons_Init(bha);
|
|
|
|
|
|
/*
|
|
|
* Creates the INA thread.
|
|
|
*/
|
|
|
- chThdCreateStatic(waInaThread, sizeof(waInaThread), NORMALPRIO, InaThread, NULL);
|
|
|
+ //chThdCreateStatic(waInaThread, sizeof(waInaThread), NORMALPRIO, InaThread, NULL);
|
|
|
+ chVTSetContinuous(&ina_vt, TIME_MS2I(INA_SCAN_PERIOS_MS), ina_Process, NULL);
|
|
|
|
|
|
gwinPrintf(GW1, "Try to init INA3221....\n");
|
|
|
ina3221.i2c_addr = INA3221_ADDR40_GND;
|
|
@@ -278,7 +208,7 @@ int main(void) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static void prepareScreen(void) {
|
|
|
+static void prepare_Screen(void) {
|
|
|
/* Set some fonts */
|
|
|
font1 = gdispOpenFont("DejaVu Sans Book 12");
|
|
|
font2 = gdispOpenFont("DejaVu Sans Book 24");
|
|
@@ -328,51 +258,120 @@ static void prepareScreen(void) {
|
|
|
gwinPrintf(GW1, "\033bConsole\033B started sucessful.\n");
|
|
|
}
|
|
|
|
|
|
-static void btn1_handler(void) {
|
|
|
- gwinPrintf(GW1, "Button 1 pressed\n");
|
|
|
+static void btn1_handler(button_state_t state) {
|
|
|
+ if (state == BTN_st_Pressed) {
|
|
|
+ gwinPrintf(GW1, "Button 1 pressed\n");
|
|
|
|
|
|
- current_Channel ++;
|
|
|
- if (current_Channel >= INA3221_CH_NUM) {
|
|
|
- current_Channel = INA3221_CH1;
|
|
|
- }
|
|
|
- int tmp = current_Channel + 1;
|
|
|
- gwinPrintf(GW1, "Channel #%u selected.\n", tmp);
|
|
|
+ current_Channel ++;
|
|
|
+ if (current_Channel >= INA3221_CH_NUM) {
|
|
|
+ current_Channel = INA3221_CH1;
|
|
|
+ }
|
|
|
+ int tmp = current_Channel + 1;
|
|
|
+ gwinPrintf(GW1, "Channel #%u selected.\n", tmp);
|
|
|
|
|
|
- char buf[16];
|
|
|
- chsnprintf(buf, 10, "[1] ch.%u", tmp);
|
|
|
- gdispFillStringBox(1, 1, 78, 15, buf, font1, Yellow, Blue, gJustifyLeft);
|
|
|
+ char buf[16];
|
|
|
+ chsnprintf(buf, 10, "[1] ch.%u", tmp);
|
|
|
+ gdispFillStringBox(1, 1, 78, 15, buf, font1, Yellow, Blue, gJustifyLeft);
|
|
|
|
|
|
- chsnprintf(buf, 12, " - %2uA ", charger_Channels[current_Channel].max_current/1000);
|
|
|
- gdispFillStringBox(1, 16, 78, 15, buf, font1, Yellow, Blue, gJustifyLeft);
|
|
|
+ chsnprintf(buf, 12, " - %2uA ", charger_Channels[current_Channel].max_current/1000);
|
|
|
+ gdispFillStringBox(1, 16, 78, 15, buf, font1, Yellow, Blue, gJustifyLeft);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-static void btn2_handler(void) {
|
|
|
- gwinPrintf(GW1, "Button 2 pressed\n");
|
|
|
+static void btn2_handler(button_state_t state) {
|
|
|
+ if (state == BTN_st_Pressed) {
|
|
|
+ gwinPrintf(GW1, "Button 2 pressed\n");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-static void btn3_handler(void) {
|
|
|
- gwinPrintf(GW1, "Button 3 pressed\n");
|
|
|
+static void btn3_handler(button_state_t state) {
|
|
|
+ if (state == BTN_st_Pressed) {
|
|
|
+ gwinPrintf(GW1, "Button 3 pressed\n");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-static void btn4_handler(void) {
|
|
|
- gwinPrintf(GW1, "Button 4 pressed\n");
|
|
|
-
|
|
|
- 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);
|
|
|
- break;
|
|
|
- default:
|
|
|
- charger_State = Stop;
|
|
|
- gwinPrintf(GW1, "Change state to Stop\n");
|
|
|
- gdispFillStringBox(1, 91, 78, 15, "[4] Start", font1, Yellow, Blue, gJustifyLeft);
|
|
|
- break;
|
|
|
+static void btn4_handler(button_state_t state) {
|
|
|
+ if (state == BTN_st_Pressed) {
|
|
|
+ gwinPrintf(GW1, "Button 4 pressed\n");
|
|
|
+
|
|
|
+ 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);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ charger_State = Stop;
|
|
|
+ gwinPrintf(GW1, "Change state to Stop\n");
|
|
|
+ gdispFillStringBox(1, 91, 78, 15, "[4] Start", font1, Yellow, Blue, gJustifyLeft);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ * Read Voltage & Current, update screen info.
|
|
|
+ */
|
|
|
+static void ina_Process(virtual_timer_t *vtp, void *p) {
|
|
|
+ (void)vtp;
|
|
|
+ (void)p;
|
|
|
+
|
|
|
+ int32_t tmp1, tmp2;
|
|
|
+ char buf[12];
|
|
|
+ static int idx = 0;
|
|
|
+
|
|
|
+ palToggleLine(LINE_LED2);
|
|
|
+ if (INA_Present != 0) {
|
|
|
+ Current = INA3221_getCurrent(&ina3221, (ina3221_ch_t)current_Channel);
|
|
|
+ Voltage = INA3221_getVoltage(&ina3221, (ina3221_ch_t)current_Channel);
|
|
|
+
|
|
|
+ tmp1 = Voltage / 1000;
|
|
|
+ tmp2 = Voltage % 1000;
|
|
|
+ chsnprintf(buf, 10, "V:%2d.%03u", tmp1, tmp2);
|
|
|
+ gdispFillStringBox(1, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
|
|
|
+
|
|
|
+ if (Current < 0) {
|
|
|
+ Current = 1 - Current;
|
|
|
+ }
|
|
|
+ tmp1 = Current / 1000;
|
|
|
+ tmp2 = Current % 1000;
|
|
|
+ chsnprintf(buf, 10, "I:%2d.%03u", tmp1, tmp2);
|
|
|
+ gdispFillStringBox(160, 123, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
|
|
|
+
|
|
|
+ if (charger_State != Stop) {
|
|
|
+ sumCurrent += Current;
|
|
|
+ sumVoltage += Voltage;
|
|
|
+ if (idx < 9) {
|
|
|
+ idx ++;
|
|
|
+ } else {
|
|
|
+ secCurrent = (sumCurrent + 5) / 10;
|
|
|
+ sumCurrent = 0;
|
|
|
+ secVoltage = (sumVoltage + 5) / 10;
|
|
|
+ sumVoltage = 0;
|
|
|
+
|
|
|
+ secPower = ((secVoltage * secCurrent) + 500) / 1000;
|
|
|
+ Capacity_I += (secCurrent + 1800) / 3600;
|
|
|
+ Capacity_P += (secPower + 1800) / 3600;
|
|
|
+
|
|
|
+ tmp1 = secVoltage / 1000;
|
|
|
+ tmp2 = secVoltage % 1000;
|
|
|
+ chsnprintf(buf, 10, "V:%2d.%03u", tmp1, tmp2);
|
|
|
+ gdispFillStringBox(1, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
|
|
|
+
|
|
|
+ if (secCurrent < 0) {
|
|
|
+ secCurrent = 1 - secCurrent;
|
|
|
+ }
|
|
|
+ tmp1 = secCurrent / 1000;
|
|
|
+ tmp2 = secCurrent % 1000;
|
|
|
+ chsnprintf(buf, 10, "I:%2d.%03u", tmp1, tmp2);
|
|
|
+ gdispFillStringBox(160, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
- * GPT callback.
|
|
|
+ * GPT callback. One second pulse.
|
|
|
*/
|
|
|
static void gpt_cb(GPTDriver *gptp) {
|
|
|
(void)gptp;
|