-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharger_statemachine.c
609 lines (536 loc) · 17.5 KB
/
charger_statemachine.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
/*
* Copyright (C) 2023 Xiaomi Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include "charger_statemachine.h"
#include "charger_hwintf.h"
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef CONFIG_CHARGERD_PM
PM_WAKELOCK_DECLARE_STATIC(g_pm_wakelock_chargerd, "chargerd_wakelock", PM_IDLE_DOMAIN, PM_NORMAL);
#endif
static bool delay_lock = false;
#ifdef CONFIG_CHARGERD_PM
static bool pm_lock = false;
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
static int charger_timer_cb(void)
{
charger_msg_t msg;
msg.event = CHARGER_EVENT_CHG_TIMEOUT;
if (!delay_lock) {
send_charger_msg(msg);
}
return 0;
}
static bool check_battery_full(struct charger_manager* manager)
{
int capacity;
int current;
int ret = 0;
static int cnt = 0;
ret = get_battery_capacity(manager, &capacity);
ret |= get_battery_current(manager, ¤t);
if (ret < 0) {
chargererr("can not get battery info , so cutoff\n");
return true;
}
chargerdebug("capacity :%d current:%d\n", capacity, current);
if (capacity >= manager->desc.fullbatt_capacity && current >= 0 && current <= manager->desc.fullbatt_current) {
/*three times of continuous,the condition is satisfying, avoid jitter */
if (cnt++ > 3) {
chargerdebug("battery is full\n");
return true;
}
return false;
}
cnt = 0;
return false;
}
static void clear_fullbatt_timer_cnt(struct charger_manager* manager)
{
manager->fullbatt_timer_cnt = 0;
}
static bool update_fullbatt_timer(struct charger_manager* manager)
{
uint64_t duration;
manager->fullbatt_timer_cnt++;
duration = manager->fullbatt_timer_cnt * manager->desc.polling_interval_ms;
if (duration >= manager->desc.fullbatt_duration_ms) {
return true;
}
return false;
}
static void clear_fault_timer_cnt(struct charger_manager* manager)
{
manager->fault_timer_cnt = 0;
}
static bool update_fault_timer(struct charger_manager* manager)
{
uint64_t duration;
manager->fault_timer_cnt++;
duration = manager->fault_timer_cnt * manager->desc.polling_interval_ms;
if (duration >= manager->desc.fault_duration_ms) {
return true;
}
return false;
}
static int update_charger_protocol(struct charger_manager* manager)
{
int adapter_t = 0;
int ret;
if (is_adapter_exist()) {
ret = get_adapter_type(manager, &adapter_t);
chargerassert_return(ret < 0, "get adapter type failed\n");
} else {
if (manager->desc.chargers > 0) {
ret = get_adapter_type_by_charger(manager, &adapter_t);
chargerassert_return(ret < 0, "get adapter type by charger failed\n");
}
}
chargerdebug("charger protocol is %d\n", adapter_t);
manager->protocol = adapter_t;
return CHARGER_OK;
}
static int charger_state_init(struct charger_manager* data, charger_msg_t* pevent)
{
int ret;
if (NULL == pevent) {
charger_timer_stop(&data->env_timer_id);
set_battery_vbus_state(data, false);
#ifdef CONFIG_CHARGERD_SYNC_CHARGE_STATE
set_battery_charge_state(data, BATTERY_DISCHARGING);
#endif
charger_sleep();
return CHARGER_OK;
}
switch (pevent->event) {
case CHARGER_EVENT_PLUGIN:
if (charger_timer_start(&data->env_timer_id, data->desc.polling_interval_ms) < 0) {
chargererr("creat timer failed;\n");
return CHARGER_FAILED;
}
set_battery_vbus_state(data, true);
charger_wakup();
ret = update_charger_protocol(data);
if (data->temp_protect_lock) {
data->nextstate = CHARGER_STATE_TEMP_PROTECT;
} else if (ret < 0) {
data->nextstate = CHARGER_STATE_FAULT;
} else {
data->nextstate = CHARGER_STATE_CHG;
}
break;
default:
chargerdebug("CHARGER_STATE_INIT ignore %d\n", pevent->event);
}
return CHARGER_OK;
}
static int charger_state_temp_protect(struct charger_manager* data, charger_msg_t* pevent)
{
int seq;
int temp = 0;
int ret;
if (NULL == pevent) {
for (seq = 0; seq < data->desc.chargers; seq++) {
ret = enable_charger(data, seq, false);
chargerassert_noreturn(ret < 0, "disable charger %d failed\n", seq);
}
if (is_adapter_exist()) {
ret = enable_adapter(data, false);
chargerassert_noreturn(ret < 0, "disable adapter failed\n");
}
return CHARGER_OK;
}
switch (pevent->event) {
case CHARGER_EVENT_OVERTEMP_RECOVERY:
if (data->online) {
data->nextstate = CHARGER_STATE_CHG;
} else {
data->nextstate = CHARGER_STATE_INIT;
}
if (is_adapter_exist()) {
ret = enable_adapter(data, true);
chargerassert_noreturn(ret < 0, "enable adapter failed\n");
}
break;
case CHARGER_EVENT_CHG_TIMEOUT:
ret = get_battery_temp(data, &temp);
chargerassert_return(ret < 0, "get battery temp failed\n");
ret = update_battery_temperature(temp);
chargerassert_return(ret < 0, "update battery temperature failed\n");
break;
case CHARGER_EVENT_PLUGOUT:
data->nextstate = CHARGER_STATE_INIT;
break;
default:
chargerdebug("CHARGER_STATE_TEMP_PROTECT ignore %d\n", pevent->event);
}
return CHARGER_OK;
}
static void charger_chg_proc_algostop(struct charger_manager* data)
{
int curr_charger;
struct charger_algo* algo = NULL;
int ret;
curr_charger = data->curr_charger;
if (curr_charger != CHARGER_INDEX_INVAILD) {
algo = &data->algos[curr_charger];
ret = algo->ops->stop(algo);
chargerassert_noreturn(ret < 0, "algo %d stop failed\n", algo->index);
data->curr_charger = CHARGER_INDEX_INVAILD;
}
return;
}
static int charger_chg_proc_fault(struct charger_manager* data)
{
charger_chg_proc_algostop(data);
data->nextstate = CHARGER_STATE_FAULT;
return CHARGER_FAILED;
}
static int charger_chg_proc_plot(struct charger_manager* data, struct charger_plot_parameter* pa)
{
int* curr_charger;
struct charger_algo* algo = NULL;
int ret;
curr_charger = &data->curr_charger;
if (*curr_charger == CHARGER_INDEX_INVAILD) {
*curr_charger = pa->charger_index;
algo = &data->algos[*curr_charger];
ret = algo->ops->start(algo);
chargerassert_return(ret < 0, "algo %d start failed\n", algo->index);
} else if (*curr_charger == pa->charger_index) {
algo = &data->algos[*curr_charger];
ret = algo->ops->update(algo, pa);
chargerassert_return(ret < 0, "algo %d update failed\n", algo->index);
} else {
algo = &data->algos[*curr_charger];
ret = algo->ops->stop(algo);
chargerassert_return(ret < 0, "algo %d stop failed\n", algo->index);
*curr_charger = pa->charger_index;
algo = &data->algos[*curr_charger];
ret = algo->ops->start(algo);
chargerassert_return(ret < 0, "algo %d start failed\n", algo->index);
ret = algo->ops->update(algo, pa);
chargerassert_return(ret < 0, "algo %d update failed\n", algo->index);
}
return CHARGER_OK;
}
static int charger_chg_proc(struct charger_manager* data)
{
int temp = 0;
int vol = 0;
struct charger_plot_parameter* pa = NULL;
if (check_battery_full(data)) {
charger_chg_proc_algostop(data);
data->nextstate = CHARGER_STATE_FULL;
return CHARGER_OK;
}
if (update_charger_protocol(data) < 0) {
chargererr("update_charger_protocol failed\n");
goto fault;
}
if (get_battery_temp(data, &temp) < 0) {
chargererr("get battery temperature failed\n");
goto fault;
}
if (update_battery_temperature(temp) < 0) {
chargererr("update battery temperature failed\n");
goto fault;
}
if (get_battery_voltage(data, &vol) < 0) {
chargererr("get battery voltage failed\n");
goto fault;
}
pa = check_charger_plot(temp, vol, data->protocol);
if (NULL == pa) {
chargerwarn("temp:%d vol:%d were not found in the plot\n",
temp, vol);
pa = &data->desc.fault;
} else if (pa->charger_index == CHARGER_INDEX_INVAILD) {
chargerwarn("charger_index is invaild\n");
charger_chg_proc_algostop(data);
return CHARGER_OK;
}
if (charger_chg_proc_plot(data, pa) < 0) {
chargererr("charger chg proc plot failed\n");
goto fault;
}
return CHARGER_OK;
fault:
return charger_chg_proc_fault(data);
}
static int charger_state_chg(struct charger_manager* data, charger_msg_t* pevent)
{
if (NULL == pevent) {
return charger_chg_proc(data);
}
switch (pevent->event) {
case CHARGER_EVENT_PLUGOUT:
charger_chg_proc_algostop(data);
data->nextstate = CHARGER_STATE_INIT;
break;
case CHARGER_EVENT_CHG_TIMEOUT:
return charger_chg_proc(data);
case CHARGER_EVENT_OVERTEMP:
charger_chg_proc_algostop(data);
data->nextstate = CHARGER_STATE_TEMP_PROTECT;
break;
default:
chargerdebug("CHARGER_STATE_CHG ignore %d\n", pevent->event);
}
return CHARGER_OK;
}
static int charger_state_full(struct charger_manager* data, charger_msg_t* pevent)
{
int ret = 0;
if (NULL == pevent) {
if (is_adapter_exist()) {
ret = enable_adapter(data, false);
chargerassert_noreturn(ret < 0, "disable adapter failed\n");
}
clear_fullbatt_timer_cnt(data);
return CHARGER_OK;
}
switch (pevent->event) {
case CHARGER_EVENT_CHG_TIMEOUT:
if (update_fullbatt_timer(data)) {
if (data->online) {
data->nextstate = CHARGER_STATE_CHG;
} else {
data->nextstate = CHARGER_STATE_INIT;
}
if (is_adapter_exist()) {
ret = enable_adapter(data, true);
chargerassert_noreturn(ret < 0, "enable adapter failed\n");
}
}
break;
case CHARGER_EVENT_OVERTEMP:
data->nextstate = CHARGER_STATE_TEMP_PROTECT;
break;
case CHARGER_EVENT_PLUGOUT:
if (is_adapter_exist()) {
ret = enable_adapter(data, true);
chargerassert_noreturn(ret < 0, "enable adapter failed\n");
}
data->nextstate = CHARGER_STATE_INIT;
break;
default:
chargerdebug("CHARGER_STATE_FULL ignore %d\n", pevent->event);
}
return CHARGER_OK;
}
static int check_fault_plot(struct charger_manager* data)
{
int temp = 0;
int vol = 0;
int ret = 0;
struct charger_plot_parameter* pa = NULL;
struct charger_algo* algo = NULL;
if (data->desc.fault.charger_index == CHARGER_INDEX_INVAILD) {
return CHARGER_FAILED;
}
ret = get_battery_temp(data, &temp);
ret |= get_battery_voltage(data, &vol);
chargerassert_return(ret < 0, "get battery info failed\n");
pa = &data->desc.fault;
if (temp >= pa->temp_range_min && temp <= pa->temp_range_max
&& vol >= pa->vol_range_min && vol <= pa->vol_range_max) {
algo = &data->algos[data->desc.fault.charger_index];
data->curr_charger = data->desc.fault.charger_index;
ret = algo->ops->start(algo);
ret |= algo->ops->update(algo, &data->desc.fault);
if (ret < 0) {
charger_chg_proc_algostop(data);
}
return ret;
}
return CHARGER_FAILED;
}
static int charger_fault_proc(struct charger_manager* data)
{
int seq = 0;
int ret;
if (check_fault_plot(data) < 0) {
for (seq = 0; seq < data->desc.chargers; seq++) {
ret = enable_charger(data, seq, false);
chargerassert_noreturn(ret < 0, "disable charger %d failed\n", seq);
}
if (is_adapter_exist()) {
ret = enable_adapter(data, false);
chargerassert_noreturn(ret < 0, "disable adapter failed\n");
}
}
return CHARGER_OK;
}
static void charger_fault_escape(struct charger_manager* data)
{
int ret;
charger_chg_proc_algostop(data);
if (is_adapter_exist()) {
ret = enable_adapter(data, true);
chargerassert_noreturn(ret < 0, "enable adapter failed\n");
}
return;
}
static int charger_state_fault(struct charger_manager* data, charger_msg_t* pevent)
{
if (NULL == pevent) {
clear_fault_timer_cnt(data);
return charger_fault_proc(data);
}
switch (pevent->event) {
case CHARGER_EVENT_CHG_TIMEOUT:
if (check_battery_full(data)) {
data->nextstate = CHARGER_STATE_FULL;
} else if (update_fault_timer(data)) {
if (data->online) {
data->nextstate = CHARGER_STATE_CHG;
} else {
data->nextstate = CHARGER_STATE_INIT;
}
}
break;
case CHARGER_EVENT_OVERTEMP:
data->nextstate = CHARGER_STATE_TEMP_PROTECT;
break;
case CHARGER_EVENT_PLUGOUT:
data->nextstate = CHARGER_STATE_INIT;
break;
default:
chargerdebug("CHARGER_STATE_FAULT ignore %d\n", pevent->event);
}
if (data->nextstate != CHARGER_STATE_FAULT) {
charger_fault_escape(data);
}
return CHARGER_OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
void charger_wakup(void)
{
#ifdef CONFIG_CHARGERD_PM
if (pm_lock == false) {
pm_wakelock_stay(&g_pm_wakelock_chargerd);
pm_lock = true;
chargerwarn("[charger] power state: stay\n");
}
#endif
}
void charger_sleep(void)
{
#ifdef CONFIG_CHARGERD_PM
if (pm_lock == true) {
pm_wakelock_relax(&g_pm_wakelock_chargerd);
pm_lock = false;
chargerwarn("[charger] power state: relax\n");
}
#endif
}
void charger_delay(unsigned int delay_ms)
{
delay_lock = true;
usleep(1000 * delay_ms);
delay_lock = false;
}
int charger_timer_stop(timer_t* timer)
{
int ret;
if (*timer != 0) {
ret = timer_delete(*timer);
if (ret != 0) {
chargererr("timer_delete fail: %d\r\n", ret);
return -1;
} else {
*timer = 0;
chargerinfo("timer_delete success!\n");
}
} else {
chargerinfo("timer_create null!\n");
}
return 0;
}
int charger_timer_start(timer_t* timer, time_t poll_interval)
{
int ret;
struct sigevent evp;
struct itimerspec it;
memset(&evp, 0, sizeof(struct sigevent));
evp.sigev_value.sival_int = 122;
evp.sigev_notify = SIGEV_THREAD;
evp.sigev_notify_function = (void*)charger_timer_cb;
it.it_interval.tv_sec = poll_interval / 1000;
it.it_interval.tv_nsec = (poll_interval % 1000) * 1000;
it.it_value.tv_sec = poll_interval / 1000;
it.it_value.tv_nsec = (poll_interval % 1000) * 1000;
if (*timer != 0) {
chargererr("timer has created\r\n");
return 0;
}
ret = timer_create(CLOCK_REALTIME, &evp, timer);
if (ret != 0) {
chargererr("timer_create fail: %d\r\n", ret);
return -1;
}
ret = timer_settime(*timer, 0, &it, 0);
if (ret != 0) {
chargererr("timer_start fail: %d\r\n", ret);
return -1;
}
chargerinfo("timer_create success!\n");
return 0;
}
void init_state_func_tables(struct charger_manager* manager)
{
manager->functables[CHARGER_STATE_INIT] = charger_state_init;
manager->functables[CHARGER_STATE_CHG] = charger_state_chg;
manager->functables[CHARGER_STATE_TEMP_PROTECT] = charger_state_temp_protect;
manager->functables[CHARGER_STATE_FULL] = charger_state_full;
manager->functables[CHARGER_STATE_FAULT] = charger_state_fault;
return;
}
int charger_statemachine_state_run(struct charger_manager* data,
charger_msg_t* event, bool* changed)
{
int ret = 0;
chargerdebug("current state %d\n", data->currstate);
DEBUGASSERT(data->functables[data->currstate]);
if (*changed) {
*changed = false;
ret = data->functables[data->currstate](data, NULL);
if (ret < 0) {
chargererr("enter state %d failed\n", data->currstate);
}
} else {
ret = data->functables[data->currstate](data, event);
if (ret < 0) {
chargererr("state %d handle event %d failed\n", data->currstate, event->event);
}
}
if (data->currstate != data->nextstate) {
*changed = true;
chargerinfo("change state %d to %d\n", data->currstate, data->nextstate);
data->prestate = data->currstate;
data->currstate = data->nextstate;
}
return ret;
}