Jelajahi Sumber

Improve graph. Usage external SRAM.

Vladimir N. Shilov 7 bulan lalu
induk
melakukan
b22d7a54bb
2 mengubah file dengan 61 tambahan dan 2 penghapusan
  1. 2 0
      src/graph.c
  2. 59 2
      src/main.c

+ 2 - 0
src/graph.c

@@ -52,4 +52,6 @@ void graph_Draw(uint32_t u_value, uint32_t i_value) {
     if (pos_X >= WINDOW_W) {
         pos_X = 0;
     }
+
+    gdispDrawLine(pos_X, WINDOW_H, pos_X, WINDOW_Y, GFX_GREEN);
 }

+ 59 - 2
src/main.c

@@ -152,6 +152,7 @@ static const charger_channel_t charger_Channels[INA3221_CH_NUM] = {
 };
 
 /* Privae functions */
+static void SRAM_Init(void);
 static void prepare_Screen(void);
 static void ina_Process(void);
 static void mode_vt_cb(virtual_timer_t *vtp, void *st);
@@ -193,6 +194,15 @@ static FATFS SDC_FS; /* FS object. */
 static bool fs_ready = false; /* FS mounted and ready.*/
 static uint8_t fbuff[1024]; /* Generic large buffer.*/
 
+#define Bank1_SRAM2_ADDR  ((uint32_t)0x68000000)
+static uint16_t * sram_array = (uint16_t *)0x68000000;
+static int arrayIdx = 0;
+static int dechargeIdx = 0;
+static int chargeIdx = 0;
+static uint32_t sCurrent = 0;
+static uint32_t sVoltage = 0;
+static int sIdx = 0;
+
 /*
  * INA process thread.
  */
@@ -318,6 +328,7 @@ int main(void) {
   gfxInit();
   prepare_Screen();
   graph_Init();
+  SRAM_Init();
 
   /*
    * Activates the I2C driver 1.
@@ -421,6 +432,24 @@ int main(void) {
     events = chEvtWaitAny(ALL_EVENTS);
 
     if (events & INA_ALL_VALUSE) {
+      if (charger_State == dech_Capacity_I || charger_State == Charge2) {
+        sVoltage += Voltage;
+        sCurrent += Current;
+        sIdx ++;
+        if (sIdx >= 60) {
+          sIdx = 0;
+          sVoltage /= 60;
+          sCurrent /= 60;
+          sram_array[arrayIdx] = sVoltage;
+          arrayIdx ++;
+          sram_array[arrayIdx] = sCurrent;
+          arrayIdx ++;
+          graph_Draw(sVoltage, sCurrent);
+          sVoltage = 0;
+          sCurrent = 0;
+        }
+      }
+
       tmp1 = Voltage / 1000;
       tmp2 = Voltage % 1000;
       chsnprintf(buf, 11, "U:%2d.%03u V", tmp1, tmp2);
@@ -448,8 +477,6 @@ int main(void) {
       chsnprintf(buf, 13, "CP:%2d.%1u WH", tmp1, tmp2);
       gdispFillStringBox(0, MENU_LINE_7, MENU_LINE_W, MENU_LINE_H, buf, font2, Red, Gray, gJustifyLeft);
 
-      graph_Draw(Voltage, Current);
-
       /* Check charge/decharge conditions */
       switch (charger_State) {
       case Charge1:
@@ -499,6 +526,7 @@ int main(void) {
     if (events & CHRGR_ST_CHANGE) {
       switch (charger_State) {
       case Charge1:
+        gwinClear(GW1);
         if (Profile[charger_Profile].VoltageDechMin_mv > Voltage) {
           gwinPrintf(GW1, "No Battery detected / Low voltage...\n");
           charger_State = Stop;
@@ -545,6 +573,11 @@ int main(void) {
 
       case Decharge:
         oldState = Decharge;
+        arrayIdx = 0;
+        dechargeIdx = 0;
+        sVoltage = 0;
+        sCurrent = 0;
+        sIdx = 0;
         Capacity_I = 0;
         Capacity_P = 0;
         Timer.hh = 0;
@@ -586,6 +619,10 @@ int main(void) {
         break;
 
       case Charge2:
+        chargeIdx = 0;
+        sVoltage = 0;
+        sCurrent = 0;
+        sIdx = 0;
         Capacity_I = 0;
         Capacity_P = 0;
         Timer.hh = 0;
@@ -650,6 +687,26 @@ int main(void) {
   }
 }
 
+/**
+  * @brief  Initializes the SRAM device.
+  * @retval SRAM status
+  */
+static void SRAM_Init(void) { 
+  const unsigned char FSMC_Bank = 0x04; // FSMC_NE3
+
+  /* Initialize SRAM control Interface 16bit, write enable*/
+  FSMC_Bank1->BTCR[FSMC_Bank] |= (uint32_t)(0x10 | 0x1000);
+
+  /* Initialize SRAM timing Interface */
+  FSMC_Bank1->BTCR[FSMC_Bank + 1U] |= (uint32_t)(2 | (1<<4U) | (2<<8U) | (1<<16U) | ((2-1U)<<20U) | ((2-2U)<<24U));
+
+  /* Initialize SRAM extended mode timing Interface */
+  FSMC_Bank1E->BWTR[FSMC_Bank] = 0x0FFFFFFFU;
+  
+  /* Enable the NORSRAM device */
+  FSMC_Bank1->BTCR[FSMC_Bank] |= 0x1; //FMC_BCR1_MBKEN;
+}
+
 /**
  * @brief Prepare screen for information presentation.
  */