Explorar o código

Reworked INA and Charger processes.

Vladimir N. Shilov %!s(int64=2) %!d(string=hai) anos
pai
achega
6e269b1e35
Modificáronse 3 ficheiros con 107 adicións e 47 borrados
  1. 2 2
      cfg/mcuconf.h
  2. 14 0
      readme.txt
  3. 91 45
      src/main.c

+ 2 - 2
cfg/mcuconf.h

@@ -91,9 +91,9 @@
 #define STM32_GPT_USE_TIM2                  FALSE
 #define STM32_GPT_USE_TIM3                  FALSE
 #define STM32_GPT_USE_TIM4                  FALSE
-#define STM32_GPT_USE_TIM5                  TRUE
+#define STM32_GPT_USE_TIM5                  FALSE
 #define STM32_GPT_USE_TIM6                  FALSE
-#define STM32_GPT_USE_TIM7                  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

+ 14 - 0
readme.txt

@@ -27,3 +27,17 @@ Tasks:
  - сохранение лога в файл на MicroSD
  - вывод лога в COM-порт (возможно в формате для LogView/iMax B6)
  - отображение на экран текущего режима, текущих данных, графика...
+---
+28.12.2022
+
+Сделал кнопки на событиях - всё работало, а потом вдруг перестало...
+Сделал обратно на потоке, который пинает виртуальный таймер.
+
+Что-то не сложилось с RTC...
+В колбеках (и VT) можно только что-то короткое, функции только I-соместимые.
+
+Переделал назад на потоки, всех пинает GPT у которого что-то с частотой.
+Не уверен, что всё будет работать, потому как непонятно, почему сломалось?
+
+По идее, из потока INA нужно убрать весь вывод, оставить только подсчёт.
+Вся логика тестера должна быть в потоке Chrgr.

+ 91 - 45
src/main.c

@@ -21,7 +21,8 @@
 #include "buttons.h"
 #include "INA3221.h"
 
-#define INA_SCAN_PERIOS_MS    100
+#define INA_SCAN_PERIOS_US    62500
+#define INA_AVG_FACTOR        16
 
 /* Type definitions */
 typedef enum chrgr_state {
@@ -65,8 +66,8 @@ static const I2CConfig i2cfg1 = {
  */
 static void gpt_cb(GPTDriver *gptp);
 static GPTConfig gpt_cfg = {
-  1000,   /* Timer clock.    */
-  gpt_cb, /* Timer callback. */
+  1000000,  /* Timer clock 1 MHz. */
+  gpt_cb,   /* Timer callback. */
   0, 0
 };
 
@@ -77,14 +78,15 @@ static const charger_channel_t charger_Channels[INA3221_CH_NUM] = {
 
 /* Privae functions */
 static void prepare_Screen(void);
-static void ina_Process(virtual_timer_t *vtp, void *p);
+static void ina_Process(void); //(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 virtual_timer_t ina_vt;
+static binary_semaphore_t ina_bsem, charger_bsem;
 static charger_state_t charger_State;
 static uint8_t current_Channel;
 static int32_t Current, sumCurrent=0, secCurrent;
@@ -99,6 +101,41 @@ static gFont font2;
 static time_cnt_t Timer = {0};
 static btn_hndlr bha[Button_Num] = {btn1_handler, btn2_handler, btn3_handler, btn4_handler};
 
+/*
+ * INA process thread.
+ */
+static THD_WORKING_AREA(waInaThread, 128);
+static THD_FUNCTION(InaThread, arg) {
+  (void)arg;
+  chRegSetThreadName("INA");
+
+  while (true) {
+    chBSemWait(&ina_bsem);
+
+    ina_Process();
+  }
+}
+
+/*
+ * Charger process thread. Once per second.
+ */
+static THD_WORKING_AREA(waChrgThread, 128);
+static THD_FUNCTION(ChrgThread, arg) {
+  (void)arg;
+  char buf[16];
+
+  while (true) {
+    chBSemWait(&charger_bsem);
+
+    if (charger_State != Stop) {
+      chsnprintf(buf, 12, "T:%02u:%02u:%02u", Timer.hh, Timer.mm, Timer.ss);
+    } else {
+      chsnprintf(buf, 12, "T:00:00:00");
+    }
+    gdispFillStringBox(1, 153, 158, 29, buf, font2, Red, Gray, gJustifyCenter);
+  }
+}
+
 /*
  * Green LED blinker thread, times are in milliseconds.
  */
@@ -137,8 +174,6 @@ int main(void) {
   gfxInit();
   prepare_Screen();
 
-  gptStart(&GPTD5, &gpt_cfg);
-
   /*
    * Activates the I2C driver 1.
    */
@@ -156,11 +191,21 @@ int main(void) {
    */
   buttons_Init(bha);
 
+  /*
+   * Creates the blinker thread.
+   */
+  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO-1, Thread1, NULL);
+
+  /*
+   * Creates the charger thread.
+   */
+  chThdCreateStatic(waChrgThread, sizeof(waChrgThread), NORMALPRIO, ChrgThread, NULL);
+
   /*
    * Creates the INA thread.
    */
-  //chThdCreateStatic(waInaThread, sizeof(waInaThread), NORMALPRIO, InaThread, NULL);
-  chVTSetContinuous(&ina_vt, TIME_MS2I(INA_SCAN_PERIOS_MS), ina_Process, 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;
@@ -181,16 +226,14 @@ int main(void) {
     gwinPrintf(GW1, "Sensor INA3221 not give proper IDs.\n", Voltage);
   }
 
-  /*
-   * Creates the blinker thread.
-   */
-  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO-1, Thread1, NULL);
+  /* Starting GPT for ina scan and seconds counter */
+  gptStart(&GPTD7, &gpt_cfg);
+  gptStartContinuous(&GPTD7, INA_SCAN_PERIOS_US);
 
   /* Init charger module */
   charger_State = Stop;
   current_Channel = INA3221_CH1;
-  gptStartContinuous(&GPTD5, 10000);
-  char buf[16];
+
   /*
    * Normal main() thread activity, in this demo it does nothing except
    * sleeping in a loop and check the button state.
@@ -198,13 +241,6 @@ int main(void) {
   while (true) {
     chThdSleepMilliseconds(500);
 
-    if (charger_State != Stop) {
-      chsnprintf(buf, 12, "T:%02u:%02u:%02u", Timer.hh, Timer.mm, Timer.ss);
-    } else {
-      chsnprintf(buf, 12, "T:00:00:00");
-    }
-    gdispFillStringBox(1, 153, 158, 29, buf, font2, Red, Gray, gJustifyCenter);
-
   }
 }
 
@@ -233,9 +269,8 @@ static void prepare_Screen(void) {
   gdispDrawStringBox(1, 91, 78, 15, "[4] start", font1, Yellow, gJustifyLeft);
 //  gdispDrawStringBox(1, 106, 78, 15, "        ", font1, Yellow, gJustifyLeft);
 
-  /* print text to bootom area */
-  gdispFillStringBox(1, 123, 158, 115, " ", font2, Red, Gray, gJustifyCenter);
-  gdispFillStringBox(159, 123, 158, 115, " ", font2, Red, Silver, gJustifyCenter);
+  /* clear bootom area */
+  gdispFillStringBox(1, 123, 318, 115, " ", font2, Red, Gray, gJustifyCenter);
 
   /* create the console window */
   {
@@ -312,10 +347,11 @@ static void btn4_handler(button_state_t state) {
 /*
  * Read Voltage & Current, update screen info.
  */
-static void ina_Process(virtual_timer_t *vtp, void *p) {
-  (void)vtp;
-  (void)p;
+//static void ina_Process(virtual_timer_t *vtp, void *p) {
+//  (void)vtp;
+//  (void)p;
 
+static void ina_Process(void) {
   int32_t tmp1, tmp2;
   char buf[12];
   static int idx = 0;
@@ -341,13 +377,13 @@ static void ina_Process(virtual_timer_t *vtp, void *p) {
     if (charger_State != Stop) {
       sumCurrent += Current;
       sumVoltage += Voltage;
-      if (idx < 9) {
-        idx ++;
-      } else {
+      
+      idx ++;
+      if (idx >= INA_AVG_FACTOR) {
         idx = 0;
-        secCurrent = (sumCurrent + 5) / 10;
+        secCurrent = (sumCurrent + (INA_AVG_FACTOR/2)) / INA_AVG_FACTOR;
         sumCurrent = 0;
-        secVoltage = (sumVoltage + 5) / 10;
+        secVoltage = (sumVoltage + (INA_AVG_FACTOR/2)) / INA_AVG_FACTOR;
         sumVoltage = 0;
 
         secPower = ((secVoltage * secCurrent) + 500) / 1000;
@@ -356,7 +392,7 @@ static void ina_Process(virtual_timer_t *vtp, void *p) {
 
         tmp1 = secVoltage / 1000;
         tmp2 = secVoltage % 1000;
-        chsnprintf(buf, 10, "V:%2d.%03u", tmp1, tmp2);
+        chsnprintf(buf, 10, "Vs:%2d.%03u", tmp1, tmp2);
         gdispFillStringBox(1, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
 
         if (secCurrent < 0) {
@@ -364,7 +400,7 @@ static void ina_Process(virtual_timer_t *vtp, void *p) {
         }
         tmp1 = secCurrent / 1000;
         tmp2 = secCurrent % 1000;
-        chsnprintf(buf, 10, "I:%2d.%03u", tmp1, tmp2);
+        chsnprintf(buf, 10, "Is:%2d.%03u", tmp1, tmp2);
         gdispFillStringBox(160, 183, 106, 29, buf, font2, Red, Gray, gJustifyCenter);
       }
     }
@@ -376,17 +412,27 @@ static void ina_Process(virtual_timer_t *vtp, void *p) {
  */
 static void gpt_cb(GPTDriver *gptp) {
   (void)gptp;
-
-  palToggleLine(LINE_LED1);
-  if (Timer.ss < 59) {
-    Timer.ss ++;
-  } else {
-    Timer.ss = 0;
-    if (Timer.mm < 59) {
-      Timer.mm ++;
+  static int cnt = 0;
+
+  chSysLockFromISR();
+  chBSemSignalI(&ina_bsem);
+  cnt ++;
+  if (cnt >= INA_AVG_FACTOR) {
+    /* One second */
+    palToggleLine(LINE_LED1);
+    cnt = 0;
+    chBSemSignalI(&charger_bsem);
+    if (Timer.ss < 59) {
+      Timer.ss ++;
     } else {
-      Timer.mm = 0;
-      Timer.hh ++;
+      Timer.ss = 0;
+      if (Timer.mm < 59) {
+        Timer.mm ++;
+      } else {
+        Timer.mm = 0;
+        Timer.hh ++;
+      }
     }
   }
+  chSysUnlockFromISR();
 }