]> code.bitgloo.com Git - clyne/stm-game.git/commitdiff
low-power improvements; starting flappy bird
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 30 Nov 2020 18:38:05 +0000 (13:38 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 30 Nov 2020 18:38:05 +0000 (13:38 -0500)
cfg/chconf.h
cfg/mcuconf.h
dogs.c
dogs.h
main.c

index 1ad623b738ed923a75caa5fee3c9617bbfa93ee6..dc2ccc133c12fb600b3410d63aa27416df10fd50 100644 (file)
  * @note    This macro can be used to activate a power saving mode.\r
  */\r
 #define CH_CFG_IDLE_ENTER_HOOK() {                                          \\r
+    RCC->ICSCR = (RCC->ICSCR & ~RCC_ICSCR_MSIRANGE_Msk); \\r
+    while (!(RCC->CR & RCC_CR_MSIRDY)); \\r
+    PWR->CR &= ~PWR_CR_LPRUN; \\r
+    PWR->CR |= PWR_CR_LPRUN; \\r
 }\r
 \r
 /**\r
  * @note    This macro can be used to deactivate a power saving mode.\r
  */\r
 #define CH_CFG_IDLE_LEAVE_HOOK() {                                          \\r
+    RCC->ICSCR |= 6 << RCC_ICSCR_MSIRANGE_Pos; \\r
+    while (!(RCC->CR & RCC_CR_MSIRDY)); \\r
+    PWR->CR &= ~PWR_CR_LPRUN; \\r
 }\r
 \r
 /**\r
index a5809251a3e57ae47b2073fbc8bfd2a08a1a3fca..217d13fafa8a9e543d065d1ec8d451b5a4bcc17a 100644 (file)
  * HAL driver system settings.\r
  */\r
 #define STM32_NO_INIT                       FALSE\r
-#define STM32_VOS                           STM32_VOS_1P8\r
-#define STM32_PVD_ENABLE                    FALSE\r
-#define STM32_PLS                           STM32_PLS_LEV0\r
+#define STM32_VOS                           STM32_VOS_1P2\r
+#define STM32_PVD_ENABLE                    TRUE\r
+#define STM32_PLS                           STM32_PLS_LEV4\r
 #define STM32_HSI16_ENABLED                 TRUE\r
 #define STM32_HSI16_DIVIDER_ENABLED         FALSE\r
-#define STM32_LSI_ENABLED                   TRUE\r
+#define STM32_LSI_ENABLED                   FALSE\r
 #define STM32_HSE_ENABLED                   FALSE\r
 #define STM32_LSE_ENABLED                   FALSE\r
 #define STM32_ADC_CLOCK_ENABLED             TRUE\r
-#define STM32_MSIRANGE                      STM32_MSIRANGE_512K\r
+#define STM32_MSIRANGE                      STM32_MSIRANGE_4M\r
 #define STM32_SW                            STM32_SW_MSI\r
 #define STM32_PLLSRC                        STM32_PLLSRC_HSI16\r
-#define STM32_PLLMUL_VALUE                  4\r
-#define STM32_PLLDIV_VALUE                  2\r
+#define STM32_PLLMUL_VALUE                  3\r
+#define STM32_PLLDIV_VALUE                  4\r
 #define STM32_HPRE                          STM32_HPRE_DIV1\r
 #define STM32_PPRE1                         STM32_PPRE1_DIV1\r
 #define STM32_PPRE2                         STM32_PPRE2_DIV1\r
 #define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK\r
 #define STM32_MCOPRE                        STM32_MCOPRE_DIV1\r
-#define STM32_RTCSEL                        STM32_RTCSEL_LSI\r
+#define STM32_RTCSEL                        STM32_RTCSEL_NOCLOCK\r
 #define STM32_RTCPRE                        STM32_RTCPRE_DIV2\r
 #define STM32_USART2SEL                     STM32_USART2SEL_APB\r
 #define STM32_LPUART1SEL                    STM32_LPUART1SEL_APB\r
diff --git a/dogs.c b/dogs.c
index a2d3bafc75b1faa9815647fb27b76f43a40fb20a..65a34e7a9d4087a102fdca940fa6d23b56db4f98 100644 (file)
--- a/dogs.c
+++ b/dogs.c
@@ -136,7 +136,7 @@ void dogs_init_display()
     CS_LOW;
     dogs_reset();
     CS_HIGH;
-    chThdSleepMilliseconds(100);
+    chThdSleepS(TIME_MS2I(100) / 64);
     CS_LOW;
     dogs_set_scroll_line(0);
     dogs_set_segdir(true);
@@ -146,7 +146,7 @@ void dogs_init_display()
     dogs_set_bias(true);
     dogs_set_power(0x07);
     dogs_set_vlcd_ratio(7);
-    dogs_set_contrast(0x10);
+    dogs_set_contrast(12);
     dogs_set_advanced(0x83);
     dogs_set_sleep(false);
     CS_HIGH;
@@ -187,12 +187,22 @@ void dogs_flush()
 
 void draw_pixel(int x, int y, bool state)
 {
+    if (x < 0 || y < 0 || x >= DISP_WIDTH || y >= DISP_HEIGHT)
+        return;
     if (state)
         dogs_buffer[y / 8 * DISP_WIDTH + x] |= (1 << (y % 8));
     else
         dogs_buffer[y / 8 * DISP_WIDTH + x] &= ~(1 << (y % 8));
 }
 
+void draw_rect(int x, int y, int w, int h)
+{
+    for (int i = 0; i < w; i++) {
+        for (int j = 0; j < h; j++)
+            draw_pixel(x + i, y + j, true);
+    }
+}
+
 void draw_bitmap(int x, int y, const unsigned char *buffer)
 {
     // Prepare source information
diff --git a/dogs.h b/dogs.h
index d30c6fa671bfd6595d024fe1c9dae6b35417b4b1..c165979064b06eddbc289d8a48944e541085eb1c 100644 (file)
--- a/dogs.h
+++ b/dogs.h
@@ -22,6 +22,7 @@ void dogs_clear();
 void dogs_flush();
 
 void draw_pixel(int x, int y, bool state);
+void draw_rect(int x, int y, int w, int h);
 void draw_bitmap(int x, int y, const unsigned char *buffer);
 void draw_number(int x, int y, int number);
 
diff --git a/main.c b/main.c
index ffd5d3f879c182d9369c8155c68833ac84a45376..bd938d32330122390ee1eaef7f6cab2bf1176b28 100644 (file)
--- a/main.c
+++ b/main.c
@@ -21,7 +21,9 @@
  *  - Can read buttons through PAL (through interrupts now)
  *  - Use ADC to read Vintref, print to screen in mV
  *  - Sleep mode via WFI, saves ~0.5mA (we're running around 1.1mA)
- *  - Run at 512kHz, only use HSI for ADC: 360uA
+ *  - Run at 512kHz, only use HSI for ADC: 360uA (jumpy)
+ *  - Drop to 1.2V Vcore (range 3), enable low-V detector: 375uA (steady) (440uA at 1MHz)
+ *  - Run at 4MHz, drop to low-power run/sleep @ 64kHz for idle: 375uA (also lowered contrast)
  */
 
 static volatile bool adc_is_complete = false;
@@ -67,7 +69,7 @@ static int readVddmv()
     return 3000 * /* CAL */ *((adcsample_t *)0x1FF80078) / reading;
 }
 
-THD_WORKING_AREA(waThread2, 96);
+THD_WORKING_AREA(waThread2, 128);
 THD_FUNCTION(Thread2, arg)
 {
     (void)arg;
@@ -86,32 +88,51 @@ THD_FUNCTION(Thread2, arg)
         0b11000000,
     };
 
+    int t1x = DISP_WIDTH, t1o = 30;
 
-    int x = 0, y = 0;
+    int py = DISP_HEIGHT / 2 - 4;
+    int vy = 0;
     int counter = 0;
     int mv = readVddmv();
     while (1) {
-        chThdSleepMilliseconds(100);
-
-        unsigned char b = button_state;
-        if ((b & (BUTTON_JOYUR | BUTTON_JOYUL)) == (BUTTON_JOYUR | BUTTON_JOYUL))
-            y--;
-        else if ((b & (BUTTON_JOYUR | BUTTON_JOYDR)) == (BUTTON_JOYUR | BUTTON_JOYDR))
-            x++;
-        else if ((b & (BUTTON_JOYDR | BUTTON_JOYDL)) == (BUTTON_JOYDR | BUTTON_JOYDL))
-            y++;
-        else if ((b & (BUTTON_JOYDL | BUTTON_JOYUL)) == (BUTTON_JOYDL | BUTTON_JOYUL))
-            x--;
+        //systime_t old_time = chVTGetSystemTimeX();
+
+        if (py > 0) {
+            py += vy;
+            if (vy > -4)
+                vy--;
+        } else if (py < 0) {
+            py = 0;
+        }
 
-        if (++counter == 50) {
-            counter = 0;
-            mv = readVddmv();
+        if (button_state & BUTTON_2) {
+            vy = 5;
+            if (py <= 0)
+                py = 1;
         }
 
         dogs_clear();
-        draw_bitmap(x, y, testbitmap);
+
+        draw_rect(t1x, 0, 4, t1o - 12);
+        draw_rect(t1x, t1o + 12, 4, DISP_HEIGHT - t1o + 12);
+
+        draw_bitmap(4, py, testbitmap);
+
         draw_number(0, 50, mv);
         dogs_flush();
+
+
+
+        if (++counter == 50) {
+            counter = 0;
+            mv = !(PWR->CSR & PWR_CSR_PVDO) ? readVddmv() : 1;
+        }
+        t1x -= 2;
+        if (t1x <= -5)
+            t1x = DISP_WIDTH;
+
+        //chThdSleepUntilS(chTimeAddX(old_time, TIME_MS2I(100) / 32));
+        chThdSleepS(TIME_MS2I(100) / 64);
     }
 }
 
@@ -125,6 +146,7 @@ int main(void)
     chSysInit();
 
     RCC->CR &= ~RCC_CR_HSION;
+    PWR->CR |= PWR_CR_LPSDSR;
 
     buttons_init();