-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal.c
657 lines (425 loc) · 16.7 KB
/
hal.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#include <csrtypes.h>
#include <battery.h>
#include <pio.h>
#include <panic.h>
#include <boot.h>
#include "spp_dev_b_buttons.h"
#include "spp_dev_b_leds.h"
#include "hal.h"
#include "hal_config.h"
#include "hal_private.h"
/*#include "battery_probe.h"*/
#include "errman.h"
#include "debug.h"
#include "indication.h"
#define BEEP_ON_TIME 300
#define BEEP_OFF_TIME 100
#define BEEP_ONCE_DURATION (BEEP_ON_TIME + BEEP_OFF_TIME)
#define BEEP_TWICE_DURATION ((BEEP_ON_TIME + BEEP_OFF_TIME) * 2)
#define BEEP_THREE_TIME_DURATION ((BEEP_ON_TIME + BEEP_OFF_TIME) * 3)
/** task **/
halTaskData hal;
/** hal task handler **/
static void hal_handler(Task task, MessageId id, Message message);
static void hal_set_state(hal_state_t state);
/** state event handlers **/
static void hal_initialising_handler(Task task, MessageId id, Message message);
static void hal_activating_handler(Task task, MessageId id, Message message);
static void hal_active_handler(Task task, MessageId id, Message message);
static void hal_deactivating_handler(Task task, MessageId id, Message message);
/** state entry/exit functions **/
static void hal_initialising_state_enter(void);
static void hal_initialising_state_exit(void);
static void hal_activating_state_enter(void);
static void hal_activating_state_exit(void);
static void hal_active_state_enter(void);
static void hal_active_state_exit(void);
static void hal_deactivating_state_enter(void);
static void hal_deactivating_state_exit(void);
/** fully initialized means charging state, voltage, and bluetooth status all initialized **/
/** bool isFullyInitialized(void); **/
/** turn on ldo to power myself **/
static void enableLDO(void);
/** turn off ldo to unpower myself **/
static void disableLDO(void);
static void pio_raw_handler(Message message);
static void battery_message_handler(Message message);
static bool powerAllowedToTurnOn(void);
static bool powerAllowedToContinue(void);
/** get hal task **/
Task getHalTask() {
return &hal.task;
}
static void hal_set_state(hal_state_t state) {
switch(hal.state) {
case INITIALISING:
hal_initialising_state_exit();
break;
case ACTIVATING:
hal_activating_state_exit();
break;
case ACTIVE:
hal_active_state_exit();
break;
case DEACTIVATING:
hal_deactivating_state_exit();
break;
default:
break;
}
hal.state = state;
switch(state) {
case INITIALISING:
hal_initialising_state_enter();
break;
case ACTIVATING:
hal_activating_state_enter();
break;
case ACTIVE:
hal_active_state_enter();
break;
case DEACTIVATING:
hal_deactivating_state_enter();
break;
default:
break;
}
}
/** hal task handler **/
static void hal_handler(Task task, MessageId id, Message message) {
/** old pattern **/
switch (hal.state) {
case INITIALISING:
hal_initialising_handler(task, id, message);
break;
case ACTIVATING:
hal_activating_handler(task, id, message);
break;
case ACTIVE:
hal_active_handler(task, id, message);
break;
case DEACTIVATING:
hal_deactivating_handler(task, id, message);
break;
}
}
static void hal_initialising_state_enter(void) {
DEBUG(("hal initialising state enter...\n"));
/** init pio **/
pioInit(&hal.pio_state, getHalTask());
/** init battery lib, set 0 to read once **/
/*BatteryInit(&hal.battery_state, getHalTask(), BATTERY_READING_SOURCE, BATTERY_POLLING_PERIOD);*/
BatteryInit(&hal.battery_state, getHalTask(), BATTERY_READING_SOURCE, 0);
/** floating, if user release button and power cable unplugged, system stop **/
disableLDO();
update_indication();
}
static void hal_initialising_state_exit(void) {
DEBUG(("hal initialising state exit...\n"));
/** battery power working **/
enableLDO();
}
static void hal_initialising_handler(Task task, MessageId id, Message message) {
switch (id) {
case PIO_RAW:
{
DEBUG(("hal initialising state, PIO_RAW message arrived...\n"));
/** update charging state, and no check, even battery low we have nothing to do **/
pio_raw_handler(message);
update_indication();
}
break;
case BATTERY_READING_MESSAGE:
{
DEBUG(("hal warming-up state, BATTERY_READING_MESSAGE message arrived...\n"));
/** update battery reading and no check, even battery low we have nothing to do **/
if (hal.voltage == K_VoltageInit) {
BatteryInit(&hal.battery_state, getHalTask(), BATTERY_READING_SOURCE, BATTERY_POLLING_PERIOD);
}
battery_message_handler(message);
update_indication();
}
break;
case HAL_POWER_BUTTON_HELD_SHORT:
{
DEBUG(("hal initialising state, POWER_BUTTON_HELD_SHORT message arrived...\n"));
/** let error mananger check initialization result, and determing the control flow **/
initialisationFinished();
hal_set_state(ACTIVATING);
}
break;
case POWER_BUTTON_PRESS:
DEBUG(("hal initialising state, POWER_BUTTON_PRESS message arrived...\n"));
MessageSendLater(getHalTask(), HAL_POWER_BUTTON_HELD_SHORT, 0, 2000);
MessageSendLater(getHalTask(), HAL_POWER_BUTTON_HELD_LONG, 0, 8000);
break;
case POWER_BUTTON_RELEASE:
DEBUG(("hal initialising state, POWER_BUTTON_RELEASE message arrived...\n"));
MessageCancelAll(getHalTask(), HAL_POWER_BUTTON_HELD_SHORT);
MessageCancelAll(getHalTask(), HAL_POWER_BUTTON_HELD_LONG);
break;
case FUNCTION_BUTTON_DFU:
DEBUG(("hal initialising state, FUNCTION_BUTTON_DFU message arrived...\n"));
ledsPlay(BEEP_ONCE);/*if need beep we should add a timeout message then to enter DFU mode*/
MessageSendLater(getHalTask(), HAL_FUNCTION_BUTTON_DFU_TIMEOUT, 0, BEEP_ONCE_DURATION + 100);
break;
case HAL_FUNCTION_BUTTON_DFU_TIMEOUT:
DEBUG(("hal initialising state, HAL_FUNCTION_BUTTON_DFU_TIMEOUT message arrived...\n"));
BootSetMode(0);
break;
default:
break;
}
}
static void hal_activating_state_enter(void) {
DEBUG(("hal activating state enter...\n"));
update_indication();
hal.beep_finished = FALSE;
hal.power_button_released = FALSE;
/**
ledsPlay(ALL_LEDS_OFF);
**/
ledsPlay(BEEP_TWICE);
/*ledsPlay(MOTOR_TWICE);*/
/*
PioSetDir(1<<9, 1<<9);
PioSet(1<<9, 1<<9);
*/
MessageSendLater(getHalTask(), HAL_ACTIVATING_TIMEOUT, 0, BEEP_TWICE_DURATION + 100);
}
static void hal_activating_state_exit(void) {
DEBUG(("hal activating state exit...\n"));
if (hal.beep_finished == FALSE) {
MessageCancelAll(getHalTask(), HAL_ACTIVATING_TIMEOUT);
}
}
static void hal_activating_handler(Task task, MessageId id, Message message) {
switch(id) {
case PIO_RAW:
DEBUG(("hal activating state, PIO_RAW message arrived...\n"));
pio_raw_handler(message);
if (!powerAllowedToTurnOn) {
/***** old comment
Theoretically we should do something here because user may unplug the charging cable
right after entering this state and the battery is low. But it seems to not
triggering severe problem, we can let this issue to be solved in next state,
when next battery update message comes, the program will do the checking.
Other possible solutions:
1. in initialising state, device is allowed to be powered on when battery is not too low,
even if it is in charging. This is common design in mobile phone.
2. checking it when entering the next state.
In fact, we should go back to initialising state, but up to now, nothing to do. :)
****/
/**** new comment, UGLEE 20110710
if beep is not finished, we do nothing. beep_finished event should process batt-low condition ****/
if (hal.beep_finished) {
hal_set_state(DEACTIVATING);
}
}
break;
case BATTERY_READING_MESSAGE:
DEBUG(("hal activating state, BATTERY_READING_MESSAGE message arrived...\n"));
battery_message_handler(message);
/** see above comment on PIO_RAW case **/
if (!powerAllowedToTurnOn() && hal.beep_finished) {
hal_set_state(DEACTIVATING);
}
break;
case POWER_BUTTON_PRESS:
DEBUG(("hal initialising state, POWER_BUTTON_PRESS message arrived...\n"));
MessageSendLater(getHalTask(), HAL_POWER_BUTTON_HELD_SHORT, 0, 2000);
MessageSendLater(getHalTask(), HAL_POWER_BUTTON_HELD_LONG, 0, 8000);
break;
case POWER_BUTTON_RELEASE:
DEBUG(("hal initialising state, POWER_BUTTON_RELEASE message arrived...\n"));
MessageCancelAll(getHalTask(), HAL_POWER_BUTTON_HELD_SHORT);
MessageCancelAll(getHalTask(), HAL_POWER_BUTTON_HELD_LONG);
hal.power_button_released = TRUE;
if (hal.beep_finished == TRUE) {
hal_message_switching_on_t* msg1 = (hal_message_switching_on_t*) malloc(sizeof(hal_message_switching_on_t));
hal_message_switching_on_t* msg2 = (hal_message_switching_on_t*) malloc(sizeof(hal_message_switching_on_t));
msg1 -> forced_restart = FALSE;
msg2 -> forced_restart = FALSE;
MessageSend(hal.profile_task, HAL_MESSAGE_SWITCHING_ON, msg1);
MessageSend(hal.function_task, HAL_MESSAGE_SWITCHING_ON, msg2);
hal_set_state(ACTIVE);
}
break;
case HAL_POWER_BUTTON_HELD_LONG:
DEBUG(("hal activating state, HAL_POWER_BUTTON_HELD_LONG message arrived...\n"));
/** this must happen AFTER beep finished and BEFORE POWER_BUTTON_RELEASE, otherwise, correct the time constant settings **/
if (hal.beep_finished == FALSE || hal.power_button_released == TRUE) {
Panic();
}
else {
hal_message_switching_on_t* msg1 = (hal_message_switching_on_t*) malloc(sizeof(hal_message_switching_on_t));
hal_message_switching_on_t* msg2 = (hal_message_switching_on_t*) malloc(sizeof(hal_message_switching_on_t));
msg1 -> forced_restart = TRUE;
msg2 -> forced_restart = TRUE;
MessageSend(hal.profile_task, HAL_MESSAGE_SWITCHING_ON, msg1);
MessageSend(hal.function_task, HAL_MESSAGE_SWITCHING_ON, msg2);
hal_set_state(ACTIVE);
}
break;
case HAL_ACTIVATING_TIMEOUT:
DEBUG(("hal activating state, HAL_ACTIVATING_TIMEOUT message arrived...\n"));
hal.beep_finished = TRUE;
if (!powerAllowedToTurnOn()) {
hal_set_state(DEACTIVATING);
}
else {
if (hal.power_button_released == TRUE) {
hal_message_switching_on_t* msg1 = (hal_message_switching_on_t*) malloc(sizeof(hal_message_switching_on_t));
hal_message_switching_on_t* msg2 = (hal_message_switching_on_t*) malloc(sizeof(hal_message_switching_on_t));
msg1 -> forced_restart = FALSE;
msg2 -> forced_restart = FALSE;
MessageSend(hal.profile_task, HAL_MESSAGE_SWITCHING_ON, msg1);
MessageSend(hal.function_task, HAL_MESSAGE_SWITCHING_ON, msg2);
hal_set_state(ACTIVE);
}
else {
update_indication();
}
}
break;
}
}
static void hal_active_state_enter(void) {
DEBUG(("hal active state enter...\n"));
update_indication();
MessageSendLater(getHalTask(), HAL_ACTIVE_AUTO_SHUTDOWN_TIMEOUT, 0, HAL_ACTIVE_AUTO_SHUTDOWN_DURATION);
}
static void hal_active_state_exit(void) {
DEBUG(("hal active state exit...\n"));
MessageCancelAll(getHalTask(), HAL_ACTIVE_AUTO_SHUTDOWN_TIMEOUT);
}
static void hal_active_handler(Task task, MessageId id, Message message) {
switch (id) {
case PIO_RAW:
DEBUG(("hal active state, PIO_RAW message arrived...\n"));
pio_raw_handler(message);
if (!powerAllowedToContinue()) {
hal_set_state(DEACTIVATING);
}
else {
update_indication();
}
break;
case POWER_BUTTON_PRESS:
DEBUG(("hal active state, POWER_BUTTON_PRESS message arrived...\n"));
MessageSendLater(getHalTask(), HAL_POWER_BUTTON_HELD_SHORT, 0, 2000);
MessageSendLater(getHalTask(), HAL_POWER_BUTTON_HELD_LONG, 0, 8000);
break;
case POWER_BUTTON_RELEASE:
DEBUG(("hal active state, POWER_BUTTON_RELEASE message arrived...\n"));
MessageCancelAll(getHalTask(), HAL_POWER_BUTTON_HELD_SHORT);
MessageCancelAll(getHalTask(), HAL_POWER_BUTTON_HELD_LONG);
break;
case HAL_POWER_BUTTON_HELD_SHORT:
DEBUG(("hal active state, HAL_POWER_BUTTON_HELD_SHORT message arrived...\n"));
/** turning off **/
hal_set_state(DEACTIVATING);
break;
case BATTERY_READING_MESSAGE:
DEBUG(("hal active state, BATTERY_READING_MESSAGE message arrived...\n"));
battery_message_handler(message);
if (!powerAllowedToContinue()) {
DEBUG(("hal active state, powerAllowedToContinue failed...\n"));
hal_set_state(DEACTIVATING);
}
else {
update_indication();
}
break;
case FUNCTION_BUTTON_PRESS:
DEBUG(("hal active state, FUNCTION_BUTTON_PRESS message arrived...\n"));
/** restart timer **/
MessageCancelAll(getHalTask(), HAL_ACTIVE_AUTO_SHUTDOWN_TIMEOUT);
MessageSendLater(getHalTask(), HAL_ACTIVE_AUTO_SHUTDOWN_TIMEOUT, 0, HAL_ACTIVE_AUTO_SHUTDOWN_DURATION);
/** pass event **/
MessageSend(hal.function_task, HAL_MESSAGE_FUNCTION_BUTTON_PRESS, 0);
break;
case HAL_ACTIVE_AUTO_SHUTDOWN_TIMEOUT:
DEBUG(("hal active state, HAL_ACTIVE_AUTO_SHUTDOWN_TIMEOUT message arrived ... \n"));
hal_set_state(DEACTIVATING);
break;
}
}
static void hal_deactivating_state_enter(void) {
DEBUG(("hal deactivating state enter...\n"));
update_indication();
ledsPlay(BEEP_TWICE);
/* ledsPlay(MOTOR_TWICE);*/
MessageSendLater(getHalTask(), HAL_DEACTIVATING_TIMEOUT, 0, BEEP_TWICE_DURATION + 100);
/** send message to profile **/
MessageSend(hal.profile_task, HAL_MESSAGE_SWITCHING_OFF, 0);
MessageSend(hal.function_task, HAL_MESSAGE_SWITCHING_OFF, 0);
}
static void hal_deactivating_state_exit(void) {
DEBUG(("hal deactivating state exit...\n"));
}
static void hal_deactivating_handler(Task task, MessageId id, Message message) {
/** no need to react to any message except timeout, after all, we are going to panic **/
switch(id) {
case HAL_DEACTIVATING_TIMEOUT:
DEBUG(("hal deactivating state, HAL_DEACTIVATING_TIMEOUT message arrived...\n"));
/** brute way !!! */
Panic();
break;
}
}
void hal_init(Task profileTask, Task functionTask) {
/** set task hander **/
hal.task.handler = hal_handler;
/** set profile task **/
hal.profile_task = profileTask;
/** set function task **/
hal.function_task = functionTask;
/** init charging state **/
hal.charging_state = CHARGING_UNKNOWN;
/** set voltage to invalid value **/
hal.voltage = 0xFFFF;
/** no need to init state locals here **/
/** power button press count **/
hal.power_button_press_count = 0;
/** set init state **/
hal.state = INITIALISING;
hal_initialising_state_enter();
}
/************
common functions
************/
static void enableLDO(void) {
/** set output and drive high **/
PioSetDir(PIO_LDO_ENABLE, PIO_LDO_ENABLE);
PioSet(PIO_LDO_ENABLE, PIO_LDO_ENABLE);
}
static void disableLDO(void) {
/** set input, don't know pull-up detail, need to clarify **/
PioSetDir(PIO_LDO_ENABLE, 0);
/** maybe this could change, according the pio.h document, all pios has weak pull-up / pull-down, according the bit value in corresponding output register **/
PioSet(PIO_LDO_ENABLE, 0);
}
/** this function may need further refine **/
static void pio_raw_handler(Message message) {
PIO_RAW_T* pio_raw = (PIO_RAW_T*)message;
hal.charging_state = (pio_raw ->pio & PIO_CHARGE_DETECTION) ? CHARGING_CHARGING : CHARGING_NOT_CHARGING;
}
/** see $bluelab$\src\lib\battery\battery.c, sendReading function for message type **/
static void battery_message_handler(Message message) {
uint32* mV = (uint32*)message;
/** should we need unsigned long ??? **/
hal.voltage = (*mV) * (22 + 15) / 15;
}
static bool powerAllowedToTurnOn(void) {
if (hal.charging_state == CHARGING_NOT_CHARGING && hal.voltage < BATTERY_LOW_HYSTERESIS_HIGH_BOUND) {
return FALSE;
}
return TRUE;
}
static bool powerAllowedToContinue(void) {
if (hal.charging_state == CHARGING_NOT_CHARGING && hal.voltage < BATTERY_LOW_HYSTERESIS_LOW_BOUND) {
return FALSE;
}
return TRUE;
}