aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/main.c b/main.c
index e7949a8..9d70010 100644
--- a/main.c
+++ b/main.c
@@ -19,8 +19,47 @@
* - Serial through LPUART1 works (38400 baud, takes over swdio pins)
* - Display comm. over SPI, can clear screen
* - 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)
*/
+static volatile bool adc_is_complete = false;
+static void adc_callback(ADCDriver *adcp)
+{
+ adc_is_complete = true;
+}
+
+static const ADCConfig adccfg = {
+ .dummy = 0
+};
+
+static const ADCConversionGroup adcgrpcfg = {
+ .circular = false,
+ .num_channels = 1,
+ .end_cb = adc_callback,
+ .error_cb = NULL,
+ .cfgr1 = ADC_CFGR1_RES_12BIT, /* CFGR1 */
+ .cfgr2 = 0, /* CFGR2 */
+ .tr = ADC_TR(0, 0), /* TR */
+ .smpr = ADC_SMPR_SMP_1P5, /* SMPR */
+ .chselr = ADC_CHSELR_CHSEL17 /* CHSELR */
+};
+
+static int readVddmv()
+{
+ adcsample_t reading = 0;
+
+ adcStart(&ADCD1, &adccfg);
+ adcSTM32EnableVREF(&ADCD1);
+ adcStartConversion(&ADCD1, &adcgrpcfg, &reading, 1);
+ while (!adc_is_complete);
+ adcStopConversion(&ADCD1);
+ adcSTM32DisableVREF(&ADCD1);
+ adcStop(&ADCD1);
+
+ return 3000 * /* CAL */ *((adcsample_t *)0x1FF80078) / reading;
+}
+
THD_WORKING_AREA(waThread2, 96);
THD_FUNCTION(Thread2, arg)
{
@@ -42,6 +81,8 @@ THD_FUNCTION(Thread2, arg)
int x = 0, y = 0;
+ int counter = 0;
+ int mv = readVddmv();
while (1) {
chThdSleepMilliseconds(100);
@@ -55,8 +96,14 @@ THD_FUNCTION(Thread2, arg)
else if ((b & (BUTTON_JOYDL | BUTTON_JOYUL)) == (BUTTON_JOYDL | BUTTON_JOYUL))
x--;
+ if (++counter == 50) {
+ counter = 0;
+ mv = readVddmv();
+ }
+
dogs_clear();
draw_bitmap(x, y, testbitmap);
+ draw_number(0, 50, mv);
dogs_flush();
}
}
@@ -69,6 +116,7 @@ int main(void)
{
halInit();
chSysInit();
+ RCC->CFGR |= RCC_CFGR_STOPWUCK;
buttons_init();
@@ -85,7 +133,8 @@ int main(void)
task but YOU MUST NEVER TRY TO SLEEP OR WAIT in this loop. Note that
this tasks runs at the lowest priority level so any instruction added
here will be executed after all other tasks have been started. */
- while (1);
+ while (1)
+ asm("wfi");
}
void HardFault_Handler()