/
pm_stm32f4.c
534 lines (465 loc) · 12 KB
/
pm_stm32f4.c
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2011-2012 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* pm.c - Power Management driver and functions.
*/
#include "stm32fxxx.h"
#include <string.h>
#include <stdbool.h>
#include "FreeRTOS.h"
#include "task.h"
#include "config.h"
#include "system.h"
#include "pm.h"
#include "led.h"
#include "log.h"
#include "param.h"
#include "ledseq.h"
#include "commander.h"
#include "sound.h"
#include "deck.h"
#include "static_mem.h"
#include "worker.h"
#include "platform_defaults.h"
// Battery time limit conversions to ticks
#define PM_BAT_CRITICAL_LOW_TIMEOUT M2T(1000 * DEFAULT_BAT_LOW_DURATION_TO_TRIGGER_SEC)
#define PM_BAT_LOW_TIMEOUT M2T(1000 * DEFAULT_BAT_LOW_DURATION_TO_TRIGGER_SEC)
#define PM_SYSTEM_SHUTDOWN_TIMEOUT M2T(1000 * 60 * DEFAULT_SYSTEM_SHUTDOWN_TIMEOUT_MIN)
typedef struct _PmSyslinkInfo
{
union
{
uint8_t flags;
struct
{
uint8_t chg : 1;
uint8_t pgood : 1;
uint8_t unused : 6;
};
};
float vBat;
float chargeCurrent;
#ifdef PM_SYSTLINK_INLCUDE_TEMP
float temp;
#endif
} __attribute__((packed)) PmSyslinkInfo;
static float batteryVoltage;
static uint16_t batteryVoltageMV;
static float batteryVoltageMin = 6.0;
static float batteryVoltageMax = 0.0;
static float extBatteryVoltage;
static uint16_t extBatteryVoltageMV;
static deckPin_t extBatVoltDeckPin;
static bool isExtBatVoltDeckPinSet = false;
static float extBatVoltMultiplier;
static float extBatteryCurrent;
static deckPin_t extBatCurrDeckPin;
static bool isExtBatCurrDeckPinSet = false;
static float extBatCurrAmpPerVolt;
// Limits
static float batteryCriticalLowVoltage = DEFAULT_BAT_CRITICAL_LOW_VOLTAGE;
static float batteryLowVoltage = DEFAULT_BAT_LOW_VOLTAGE;
#ifdef PM_SYSTLINK_INLCUDE_TEMP
// nRF51 internal temp
static float temp;
#endif
static uint32_t batteryLowTimeStamp;
static uint32_t batteryCriticalLowTimeStamp;
static bool isInit;
static PMStates pmState;
static PmSyslinkInfo pmSyslinkInfo;
static uint8_t batteryLevel;
static void pmSetBatteryVoltage(float voltage);
const static float LiPoTypicalChargeCurve[10] =
{
3.00, // 00%
3.78, // 10%
3.83, // 20%
3.87, // 30%
3.89, // 40%
3.92, // 50%
3.96, // 60%
4.00, // 70%
4.04, // 80%
4.10 // 90%
};
STATIC_MEM_TASK_ALLOC(pmTask, PM_TASK_STACKSIZE);
void pmInit(void)
{
if(isInit) {
return;
}
STATIC_MEM_TASK_CREATE(pmTask, pmTask, PM_TASK_NAME, NULL, PM_TASK_PRI);
isInit = true;
pmSyslinkInfo.vBat = 3.7f;
pmSetBatteryVoltage(pmSyslinkInfo.vBat); //TODO remove
}
bool pmTest(void)
{
return isInit;
}
/**
* Sets the battery voltage and its min and max values
*/
static void pmSetBatteryVoltage(float voltage)
{
batteryVoltage = voltage;
batteryVoltageMV = (uint16_t)(voltage * 1000);
if (batteryVoltageMax < voltage)
{
batteryVoltageMax = voltage;
}
if (batteryVoltageMin > voltage)
{
batteryVoltageMin = voltage;
}
}
/**
* Shutdown system
*/
static void pmSystemShutdown(void)
{
#ifdef CONFIG_PM_AUTO_SHUTDOWN
systemRequestShutdown();
#endif
}
/**
* Returns a number from 0 to 9 where 0 is completely discharged
* and 9 is 90% charged.
*/
static int32_t pmBatteryChargeFromVoltage(float voltage)
{
int charge = 0;
if (voltage < LiPoTypicalChargeCurve[0])
{
return 0;
}
if (voltage > LiPoTypicalChargeCurve[9])
{
return 9;
}
while (voltage > LiPoTypicalChargeCurve[charge])
{
charge++;
}
return charge;
}
float pmGetBatteryVoltage(void)
{
return batteryVoltage;
}
float pmGetBatteryVoltageMin(void)
{
return batteryVoltageMin;
}
float pmGetBatteryVoltageMax(void)
{
return batteryVoltageMax;
}
/*
* When a module wants to register a callback to be called on shutdown they
* call pmRegisterGracefulShutdownCallback(graceful_shutdown_callback_t),
* with a function they which to be run at shutdown. We currently support
* GRACEFUL_SHUTDOWN_MAX_CALLBACKS number of callbacks to be registred.
*/
#define GRACEFUL_SHUTDOWN_MAX_CALLBACKS 5
static int graceful_shutdown_callbacks_index;
static graceful_shutdown_callback_t graceful_shutdown_callbacks[GRACEFUL_SHUTDOWN_MAX_CALLBACKS];
/*
* Please take care in your callback, do not take to long time the nrf
* will not wait for you, it will shutdown.
*/
bool pmRegisterGracefulShutdownCallback(graceful_shutdown_callback_t cb)
{
// To many registered allready! Increase limit if you think you are important
// enough!
if (graceful_shutdown_callbacks_index >= GRACEFUL_SHUTDOWN_MAX_CALLBACKS) {
return false;
}
graceful_shutdown_callbacks[graceful_shutdown_callbacks_index] = cb;
graceful_shutdown_callbacks_index += 1;
return true;
}
/*
* Iterate through all registered shutdown callbacks and call them one after
* the other, when all is done, send the ACK back to nrf to allow power off.
*/
static void pmGracefulShutdown()
{
for (int i = 0; i < graceful_shutdown_callbacks_index; i++) {
graceful_shutdown_callback_t callback = graceful_shutdown_callbacks[i];
callback();
}
SyslinkPacket slp = {
.type = SYSLINK_PM_SHUTDOWN_ACK,
};
syslinkSendPacket(&slp);
}
void pmSyslinkUpdate(SyslinkPacket *slp)
{
if (slp->type == SYSLINK_PM_BATTERY_STATE) {
memcpy(&pmSyslinkInfo, &slp->data[0], sizeof(pmSyslinkInfo));
pmSetBatteryVoltage(pmSyslinkInfo.vBat);
#ifdef PM_SYSTLINK_INLCUDE_TEMP
temp = pmSyslinkInfo.temp;
#endif
} else if (slp->type == SYSLINK_PM_SHUTDOWN_REQUEST) {
workerSchedule(pmGracefulShutdown, NULL);
}
}
void pmSetChargeState(PMChargeStates chgState)
{
// TODO: Send syslink packafe with charge state
}
PMStates pmUpdateState()
{
PMStates state;
bool isCharging = pmSyslinkInfo.chg;
bool isPgood = pmSyslinkInfo.pgood;
uint32_t batteryLowTime;
batteryLowTime = xTaskGetTickCount() - batteryLowTimeStamp;
if (isPgood && !isCharging)
{
state = charged;
}
else if (isPgood && isCharging)
{
state = charging;
}
else if (!isPgood && !isCharging && (batteryLowTime > PM_BAT_LOW_TIMEOUT))
{
state = lowPower;
}
else
{
state = battery;
}
return state;
}
void pmEnableExtBatteryCurrMeasuring(const deckPin_t pin, float ampPerVolt)
{
extBatCurrDeckPin = pin;
isExtBatCurrDeckPinSet = true;
extBatCurrAmpPerVolt = ampPerVolt;
}
float pmMeasureExtBatteryCurrent(void)
{
float current;
if (isExtBatCurrDeckPinSet)
{
current = analogReadVoltage(extBatCurrDeckPin) * extBatCurrAmpPerVolt;
}
else
{
current = 0.0;
}
return current;
}
void pmEnableExtBatteryVoltMeasuring(const deckPin_t pin, float multiplier)
{
extBatVoltDeckPin = pin;
isExtBatVoltDeckPinSet = true;
extBatVoltMultiplier = multiplier;
}
float pmMeasureExtBatteryVoltage(void)
{
float voltage;
if (isExtBatVoltDeckPinSet)
{
voltage = analogReadVoltage(extBatVoltDeckPin) * extBatVoltMultiplier;
}
else
{
voltage = 0.0;
}
return voltage;
}
bool pmIsBatteryLow(void) {
return (pmState == lowPower);
}
bool pmIsChargerConnected(void) {
return (pmState == charging) || (pmState == charged);
}
bool pmIsCharging(void) {
return (pmState == charging);
}
// return true if battery discharging
bool pmIsDischarging(void) {
return (pmState == lowPower) || (pmState == battery);
}
void pmTask(void *param)
{
PMStates pmStateOld = battery;
uint32_t tickCount;
vTaskSetApplicationTaskTag(0, (void*)TASK_PM_ID_NBR);
tickCount = xTaskGetTickCount();
batteryLowTimeStamp = tickCount;
batteryCriticalLowTimeStamp = tickCount;
pmSetChargeState(charge500mA);
systemWaitStart();
while(1)
{
vTaskDelay(100);
tickCount = xTaskGetTickCount();
extBatteryVoltage = pmMeasureExtBatteryVoltage();
extBatteryVoltageMV = (uint16_t)(extBatteryVoltage * 1000);
extBatteryCurrent = pmMeasureExtBatteryCurrent();
batteryLevel = pmBatteryChargeFromVoltage(pmGetBatteryVoltage()) * 10;
if (pmGetBatteryVoltage() > batteryLowVoltage)
{
batteryLowTimeStamp = tickCount;
}
if (pmGetBatteryVoltage() > batteryCriticalLowVoltage)
{
batteryCriticalLowTimeStamp = tickCount;
}
pmState = pmUpdateState();
if (pmState != pmStateOld)
{
// Actions on state change
switch (pmState)
{
case charged:
ledseqStop(&seq_charging);
ledseqRunBlocking(&seq_charged);
soundSetEffect(SND_BAT_FULL);
break;
case charging:
ledseqStop(&seq_lowbat);
ledseqStop(&seq_charged);
ledseqRunBlocking(&seq_charging);
soundSetEffect(SND_USB_CONN);
break;
case lowPower:
ledseqRunBlocking(&seq_lowbat);
soundSetEffect(SND_BAT_LOW);
break;
case battery:
ledseqRunBlocking(&seq_charging);
ledseqRun(&seq_charged);
soundSetEffect(SND_USB_DISC);
break;
default:
break;
}
pmStateOld = pmState;
}
// Actions during state
switch (pmState)
{
case charged:
break;
case charging:
{
// Charge level between 0.0 and 1.0
float chargeLevel = pmBatteryChargeFromVoltage(pmGetBatteryVoltage()) / 10.0f;
ledseqSetChargeLevel(chargeLevel);
}
break;
case lowPower:
{
uint32_t batteryCriticalLowTime;
batteryCriticalLowTime = tickCount - batteryCriticalLowTimeStamp;
if (batteryCriticalLowTime > PM_BAT_CRITICAL_LOW_TIMEOUT)
{
pmSystemShutdown();
}
}
break;
case battery:
{
if ((commanderGetInactivityTime() > PM_SYSTEM_SHUTDOWN_TIMEOUT))
{
pmSystemShutdown();
}
}
break;
default:
break;
}
}
}
/**
* Power management log variables.
*/
LOG_GROUP_START(pm)
/**
* @brief Battery voltage [V]
*/
LOG_ADD_CORE(LOG_FLOAT, vbat, &batteryVoltage)
/**
* @brief Battery voltage [mV]
*/
LOG_ADD(LOG_UINT16, vbatMV, &batteryVoltageMV)
/**
* @brief BigQuad external voltage measurement [V]
*/
LOG_ADD(LOG_FLOAT, extVbat, &extBatteryVoltage)
/**
* @brief BigQuad external voltage measurement [mV]
*/
LOG_ADD(LOG_UINT16, extVbatMV, &extBatteryVoltageMV)
/**
* @brief BigQuad external current measurement [V]
*/
LOG_ADD(LOG_FLOAT, extCurr, &extBatteryCurrent)
/**
* @brief Battery charge current [A]
*/
LOG_ADD(LOG_FLOAT, chargeCurrent, &pmSyslinkInfo.chargeCurrent)
/**
* @brief State of power management
*
* | State | Meaning | \n
* | - | - | \n
* | 0 | Battery | \n
* | 1 | Charging | \n
* | 2 | Charged | \n
* | 3 | Low power | \n
* | 4 | Shutdown | \n
*/
LOG_ADD_CORE(LOG_INT8, state, &pmState)
/**
* @brief Battery charge level [%]
*/
LOG_ADD_CORE(LOG_UINT8, batteryLevel, &batteryLevel)
#ifdef PM_SYSTLINK_INCLUDE_TEMP
/**
* @brief Temperature from nrf51 [degrees]
*/
LOG_ADD(LOG_FLOAT, temp, &temp)
#endif
LOG_GROUP_STOP(pm)
/**
* Power management parameters.
*/
PARAM_GROUP_START(pm)
/**
* @brief At what voltage power management will indicate low battery.
*/
PARAM_ADD_CORE(PARAM_FLOAT | PARAM_PERSISTENT, lowVoltage, &batteryLowVoltage)
/**
* @brief At what voltage power management will indicate critical low battery.
*/
PARAM_ADD_CORE(PARAM_FLOAT | PARAM_PERSISTENT, criticalLowVoltage, &batteryCriticalLowVoltage)
PARAM_GROUP_STOP(pm)