forked from leaflabs/libmaple
-
Notifications
You must be signed in to change notification settings - Fork 6
/
power.cpp
408 lines (339 loc) · 12.5 KB
/
power.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Sample main.cpp file. Blinks the built-in LED, sends a message out
// USART1.
#include "wirish.h"
#include "power.h"
// for power control support
#include "pwr.h"
#include "scb.h"
#define CAPTOUCH_ADDR 0x5A
#define CAPTOUCH_I2C I2C1
#define CAPTOUCH_GPIO 30
#define MANUAL_WAKEUP_GPIO 18 // PC3
#define CHG_STAT2_GPIO 44 // PC11
#define CHG_STAT1_GPIO 26 // PC10
#define MAGPOWER_GPIO 41 // PA15
#define MEASURE_FET_GPIO 45 // PC12
#define BATT_MEASURE_ADC 28 // PB1
#define MAGSENSE_GPIO 29 // PB10
#define LIMIT_VREF_DAC 10 // PA4 -- should be DAC eventually, but GPIO initially to tied own
#define CHG_TIMEREN_N_GPIO 37 // PC8
#define LED_PWR_ENA_GPIO 16 // PC1 // handled in OLED platform_init
#define WAKEUP_GPIO 2
#define UART_CTS_GPIO 46 // PA12
#define UART_RTS_GPIO 47 // PA11
#define UART_TXD_GPIO 8 // PA10
#define UART_RXD_GPIO 7 // PA9
//#define CHARGE_GPIO 38
#define BUZZ_RATE 250 // in microseconds; set to 4kHz = 250us
// "WASD" cluster as defined by physical arrangement of touch switches
#define W_KEY (1 << 3)
#define A_KEY (1 << 6)
#define S_KEY (1 << 4)
#define D_KEY (1 << 2)
#define Q_KEY (1 << 8)
#define E_KEY (1 << 0)
#define PWRSTATE_DOWN 0 // everything off, no logging; entered when battery is low
#define PWRSTATE_LOG 1 // system is on, listening to geiger and recording; but no UI
#define PWRSTATE_USER 2 // system is on, UI is fully active
#define PWRSTATE_BOOT 3 // during boot
#define PWRSTATE_OFF 4 // power is simply off, or cold reset
#define PWRSTATE_ERROR 5 // an error conditions state
#define FIRMWARE_VERSION "Safecast firmware v0.1 Jan 28 2012"
// maximum range for battery, where the value is "full" and
// 0 means the system should shut down
#define BATT_RANGE 16
// frequency of checking battery voltage during logging state
#define LOG_BATT_FREQ 20
static uint8 powerState = PWRSTATE_BOOT;
static uint8 lastPowerState = PWRSTATE_OFF;
static uint8 dbg_batt = 0;
int
power_init(void)
{
pinMode(MANUAL_WAKEUP_GPIO, INPUT);
pinMode(CHG_STAT2_GPIO, INPUT);
pinMode(CHG_STAT1_GPIO, INPUT);
pinMode(WAKEUP_GPIO, INPUT);
pinMode(BATT_MEASURE_ADC, INPUT_ANALOG);
pinMode(MAGSENSE_GPIO, INPUT);
// setup and initialize the outputs
// initially, don't measure battery voltage
pinMode(MEASURE_FET_GPIO, OUTPUT);
digitalWrite(MEASURE_FET_GPIO, 0);
// initially, turn off the hall effect sensor
pinMode(MAGPOWER_GPIO, OUTPUT);
digitalWrite(MAGPOWER_GPIO, 0);
// as a hack, tie this low to reduce current consumption
// until we hook it up to a proper DAC output
pinMode(LIMIT_VREF_DAC, OUTPUT);
digitalWrite(LIMIT_VREF_DAC, 0);
// initially, charge timer is enabled (active low)
pinMode(CHG_TIMEREN_N_GPIO, OUTPUT);
digitalWrite(CHG_TIMEREN_N_GPIO, 0);
// initially OLED is off
pinMode(LED_PWR_ENA_GPIO, OUTPUT);
digitalWrite(LED_PWR_ENA_GPIO, 0);
return 0;
}
void
power_set_debug(int level)
{
dbg_batt = level;
}
// returns a calibrated ADC code for the current battery voltage
uint16
power_battery_level(void) {
uint32 battVal;
uint32 vrefVal;
uint32 ratio;
uint16 retcode = 0;
uint32 cr2 = ADC1->regs->CR2;
cr2 |= ADC_CR2_TSEREFE; // enable reference voltage only for this measurement
ADC1->regs->CR2 = cr2;
digitalWrite(MEASURE_FET_GPIO, 1);
battVal = (uint32) analogRead(BATT_MEASURE_ADC) * 1000;
digitalWrite(MEASURE_FET_GPIO, 0);
vrefVal = (uint32) adc_read(ADC1, 17);
cr2 &= ~ADC_CR2_TSEREFE; // power down reference to save battery power
ADC1->regs->CR2 = cr2;
// calibrate
// this is important because VDDA = VMCU which is proportional to battery voltage
// VREF is independent of battery voltage, and is 1.2V +/- 3.4%
// we want to indicate system should shut down at 3.1V; 4.2V is full
// this is a ratio from 1750 (= 4.2V) to 1292 (=3.1V)
ratio = battVal / vrefVal;
if (dbg_batt) {
Serial1.print( "BattVal: " );
Serial1.println( battVal );
Serial1.print( "VrefVal: " );
Serial1.println( vrefVal );
Serial1.print( "Raw ratio: " );
Serial1.println( ratio );
}
if( ratio < 1292 )
return 0;
ratio = ratio - 1292; // should always be positive now due to test above
retcode = ratio / (459 / BATT_RANGE);
if (dbg_batt) {
Serial1.print( "Rebased ratio: " );
Serial1.println( ratio );
Serial1.print( "Retcode: " );
Serial1.println( retcode );
}
return retcode;
}
// power_is_battery_low should measure ADC and determine if the battery voltage is
// too low to continue operation. When that happens, we should immediately
// power down to prevent over-discharge of the battery.
int
power_is_battery_low(void)
{
static uint32 count = 0;
count++;
if( powerState == PWRSTATE_LOG ) { ////////// PWRSTATE_LOG TEST STATUS: THIS CODE IS UNTESTED
if( (count % LOG_BATT_FREQ) == 0 ) {
// only once every LOG_BATT_FREQ events do we actually measure the battery
// this is to reduce power consumption
gpio_init_all();
afio_init();
// init ADC
rcc_set_prescaler(RCC_PRESCALER_ADC, RCC_ADCPRE_PCLK_DIV_6);
adc_init(ADC1);
// this is from "adcDefaultConfig" inside boards.cpp
// lifted and modified here so *only* ADC1 is initialized
// the default routine "does them all"
adc_set_extsel(ADC1, ADC_SWSTART);
adc_set_exttrig(ADC1, true);
adc_enable(ADC1);
adc_calibrate(ADC1);
adc_set_sample_rate(ADC1, ADC_SMPR_55_5);
// again, a minimal set of operations done to save power; these are lifted from
// setup_gpio()
pinMode(BATT_MEASURE_ADC, INPUT_ANALOG);
pinMode(MEASURE_FET_GPIO, OUTPUT);
digitalWrite(MEASURE_FET_GPIO, 0);
} else {
// on the fall-through just lie and assume battery isn't low. close enough.
return 0;
}
}
if( power_battery_level() <= 5 ) // normally 0, 5 for testing
return 1;
else
return 0;
}
void
power_standby(void) {
// clear wakup flag
PWR_BASE->CR |= PWR_CR_CWUF;
// select standby mode
PWR_BASE->CR |= PWR_CR_PDDS;
// set sleepdeep in cortex system control register
SCB_BASE->SCR |= SCB_SCR_SLEEPDEEP;
power_wfi();
}
void
power_wfi(void)
{
// request wait for interrupt (in-line assembly)
asm volatile (
"WFI\n\t" // note for WFE, just replace this with WFE
"BX r14"
);
}
int
power_deinit(void)
{
// disable wake on interrupt
PWR_BASE->CSR &= ~PWR_CSR_EWUP;
power_standby();
return 0;
}
int
power_switch_state(void)
{
return digitalRead(MANUAL_WAKEUP_GPIO) == HIGH;
}
#if 0
int
main(void)
{
int t = 0;
while (true) {
switch(powerState) {
case PWRSTATE_DOWN: /////////// PWRSTATE_DOWN TEST STATUS: THIS CODE FUNCTIONS BUT NEEDS VALIDATION WITH AMMETER TO CONFIRM LOW POWER OPERATION.
Serial1.println ( "Entering DOWN powerstate." );
while(1) {
powerDown();
// system resets when power is plugged in no matter what, so this is sort of irrelevant
lastPowerState = PWRSTATE_DOWN;
powerState = PWRSTATE_DOWN;
}
break;
case PWRSTATE_LOG: ////////// PWRSTATE_LOG TEST STATUS: THIS CODE IS UNTESTED
if( isBattLow() ) {
lastPowerState = PWRSTATE_LOG;
powerState = PWRSTATE_DOWN;
break;
}
if( lastPowerState != PWRSTATE_LOG ) {
Serial1.println ( "Entering LOG powerstate." );
// we are just entering, so do things like turn off beeping, LED flashing, etc.
prepSleep();
// once it's all setup, re-enter the loop so we go into the next branch
lastPowerState = PWRSTATE_LOG;
powerState = PWRSTATE_LOG;
break;
} else {
// first, we sleep and wait for an interrupt
logStandby();
// when we get here, we got a wakeup event
// we'll wake up due to a switch or geiger event, so determine which and
// then re-enter the loop
gpio_init(GPIOC); // just init the bare minimum to read the GPIO
pinMode(MANUAL_WAKEUP_GPIO, INPUT);
// test code
gpio_init(GPIOD);
pinMode(LED_GPIO, OUTPUT);
digitalWrite(LED_GPIO, 1);
// end test code
if( digitalRead(MANUAL_WAKEUP_GPIO) == HIGH ) {
init(); // need to clean up everything we shut down
setup_gpio();
touchInit = 0; // can't assume anything about the touch interface
setup();
powerState = PWRSTATE_USER;
lastPowerState = PWRSTATE_LOG;
break;
} else {
// this is a geiger event. for now, just make a beep and go back to sleep
// eventually, we'll want to log the vent with a timestamp to flash
short_init(); // special-case init for minimal operational parameters
setup_buzzer();
blockingBeep();
// TODO: put logging infos here...
powerState = PWRSTATE_LOG;
lastPowerState = PWRSTATE_LOG;
break;
}
}
break;
case PWRSTATE_USER: ////////// PWRSTATE_LOG TEST STATUS: THIS CODE IS ROUTINELY USED FOR DEVELOPMENT AND IS LIGHTLY TESTED
// check for events from the touchscreen
if( lastPowerState != PWRSTATE_USER ) {
Serial1.println ( "Entering USER powerstate." );
// setup anything specific to this state, i.e. turn on LED flashing and beeping on
// radiation events
setup_lcd();
fill_oled(0); // eventually this can go away i think.
/* Set up PB11 to be an IRQ that triggers cap_down */
attachInterrupt(CAPTOUCH_GPIO, cap_down, FALLING);
allowBeep = 1;
}
if( !touchInit ) {
Serial1.println("Initializing captouch..." );
setup_captouch();
Serial1.println("Done.");
delay(100);
} else {
if(touchService) {
touchStat = 0;
touchStat = mpr121Read(TCH_STATL);
touchStat |= mpr121Read(TCH_STATH) << 8;
touchService = 0;
}
}
// call the event loop
loop(t++);
if( isBattLow() ) {
powerState = PWRSTATE_DOWN;
lastPowerState = PWRSTATE_USER;
break;
} else if( digitalRead(MANUAL_WAKEUP_GPIO) == HIGH ) {
powerState = PWRSTATE_USER;
lastPowerState = PWRSTATE_USER;
} else {
powerState = PWRSTATE_LOG;
lastPowerState = PWRSTATE_USER;
}
break;
case PWRSTATE_BOOT: ////////// PWRSTATE_BOOT TEST STATUS: THIS CODE HAS BEEN LIGHTLY TESTED
Serial1.begin(115200);
Serial1.println(FIRMWARE_VERSION);
Serial1.println ( "Entering BOOT powerstate." );
dbg_batt = 0;
setup();
allowBeep = 1;
blockingBeep();
// set up Flash, etc. and interrupt handlers for logging. At this point
// we can start receiving radiation events
setupLogging();
if( digitalRead(MANUAL_WAKEUP_GPIO) == HIGH ) {
powerState = PWRSTATE_USER;
} else {
powerState = PWRSTATE_LOG;
touchInit = 0;
}
lastPowerState = PWRSTATE_BOOT;
break;
default:
Serial1.println("Entering ERROR powerstate." );
powerState = PWRSTATE_BOOT;
lastPowerState = PWRSTATE_ERROR;
}
}
return 0;
}
#endif
int
power_get_state(void)
{
return powerState;
}
int
power_set_state(int state)
{
lastPowerState = powerState;
powerState = state;
return lastPowerState;
}