pm.c
29.4 KB
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2012-06-02 Bernard the first version
* 2018-08-02 Tanek split run and sleep modes, support custom mode
* 2019-04-28 Zero-Free improve PM mode and device ops interface
* 2020-11-23 zhangsz update pm mode select
* 2020-11-27 zhangsz update pm 2.0
*/
#include <rthw.h>
#include <rtthread.h>
#include <drivers/pm.h>
#include <stdlib.h>
#ifdef RT_USING_PM
/* tickless threshold time */
#ifndef PM_TICKLESS_THRESHOLD_TIME
#define PM_TICKLESS_THRESHOLD_TIME 2
#endif
/* tickless threshold : sleep mode */
#ifndef PM_TICKLESS_THRESHOLD_MODE
#define PM_TICKLESS_THRESHOLD_MODE PM_SLEEP_MODE_IDLE
#endif
/* busy : sleep mode */
#ifndef PM_BUSY_SLEEP_MODE
#define PM_BUSY_SLEEP_MODE PM_SLEEP_MODE_IDLE
#endif
/* suspend : suspend sleep mode */
#ifndef PM_SUSPEND_SLEEP_MODE
#define PM_SUSPEND_SLEEP_MODE PM_SLEEP_MODE_IDLE
#endif
#ifdef PM_ENABLE_THRESHOLD_SLEEP_MODE
#ifndef PM_LIGHT_THRESHOLD_TIME
#define PM_LIGHT_THRESHOLD_TIME 5
#endif
#ifndef PM_DEEP_THRESHOLD_TIME
#define PM_DEEP_THRESHOLD_TIME 20
#endif
#ifndef PM_STANDBY_THRESHOLD_TIME
#define PM_STANDBY_THRESHOLD_TIME 100
#endif
#endif
static struct rt_pm _pm;
/* default mode : system power on */
static rt_uint8_t _pm_default_sleep = RT_PM_DEFAULT_SLEEP_MODE;
/* default deepsleep mode : tick-less mode */
static rt_uint8_t _pm_default_deepsleep = RT_PM_DEFAULT_DEEPSLEEP_MODE;
static struct rt_pm_notify _pm_notify;
static rt_uint8_t _pm_init_flag = 0;
RT_WEAK rt_uint32_t rt_pm_enter_critical(rt_uint8_t sleep_mode)
{
return rt_hw_interrupt_disable();
}
RT_WEAK void rt_pm_exit_critical(rt_uint32_t ctx, rt_uint8_t sleep_mode)
{
rt_hw_interrupt_enable(ctx);
}
/* lptimer start */
static void pm_lptimer_start(struct rt_pm *pm, uint32_t timeout)
{
if (_pm.ops == RT_NULL)
return;
if (_pm.ops->timer_start != RT_NULL)
_pm.ops->timer_start(pm, timeout);
}
/* lptimer stop */
static void pm_lptimer_stop(struct rt_pm *pm)
{
if (_pm.ops == RT_NULL)
return;
if (_pm.ops->timer_stop != RT_NULL)
_pm.ops->timer_stop(pm);
}
/* lptimer get timeout tick */
static rt_tick_t pm_lptimer_get_timeout(struct rt_pm *pm)
{
if (_pm.ops == RT_NULL)
return RT_TICK_MAX;
if (_pm.ops->timer_get_tick != RT_NULL)
return _pm.ops->timer_get_tick(pm);
return RT_TICK_MAX;
}
/* enter sleep mode */
static void pm_sleep(struct rt_pm *pm, uint8_t sleep_mode)
{
if (_pm.ops == RT_NULL)
return;
if (_pm.ops->sleep != RT_NULL)
_pm.ops->sleep(pm, sleep_mode);
}
/**
* This function will suspend all registered devices
*/
static int _pm_device_suspend(rt_uint8_t mode)
{
int index, ret = RT_EOK;
for (index = 0; index < _pm.device_pm_number; index++)
{
if (_pm.device_pm[index].ops->suspend != RT_NULL)
{
ret = _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device, mode);
if(ret != RT_EOK)
break;
}
}
return ret;
}
/**
* This function will resume all registered devices
*/
static void _pm_device_resume(rt_uint8_t mode)
{
int index;
for (index = 0; index < _pm.device_pm_number; index++)
{
if (_pm.device_pm[index].ops->resume != RT_NULL)
{
_pm.device_pm[index].ops->resume(_pm.device_pm[index].device, mode);
}
}
}
/**
* This function will update the frequency of all registered devices
*/
static void _pm_device_frequency_change(rt_uint8_t mode)
{
rt_uint32_t index;
/* make the frequency change */
for (index = 0; index < _pm.device_pm_number; index ++)
{
if (_pm.device_pm[index].ops->frequency_change != RT_NULL)
_pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device, mode);
}
}
/**
* This function will update the system clock frequency when idle
*/
static void _pm_frequency_scaling(struct rt_pm *pm)
{
rt_base_t level;
if (pm->flags & RT_PM_FREQUENCY_PENDING)
{
level = rt_hw_interrupt_disable();
/* change system runing mode */
pm->ops->run(pm, pm->run_mode);
/* changer device frequency */
_pm_device_frequency_change(pm->run_mode);
pm->flags &= ~RT_PM_FREQUENCY_PENDING;
rt_hw_interrupt_enable(level);
}
}
/**
* judge sleep mode from sleep request
*
* @param none
*
* @return sleep mode
*/
static rt_uint8_t _judge_sleep_mode(void)
{
rt_uint16_t index;
rt_uint16_t len;
for (index = 0; index < PM_SLEEP_MODE_MAX -1; index++)
{
for (len = 0; len < ((PM_MODULE_MAX_ID + 31) / 32); len++)
{
if (_pm.sleep_status[index][len] != 0x00)
return index;
}
}
return PM_SLEEP_MODE_MAX; /* default sleep mode */
}
/**
* This function selects the sleep mode according to the rt_pm_request/rt_pm_release count.
*/
static rt_uint8_t _pm_select_sleep_mode(struct rt_pm *pm)
{
int index;
rt_uint8_t mode;
mode = _pm_default_deepsleep;
rt_uint8_t request_mode = _judge_sleep_mode();
for (index = PM_SLEEP_MODE_NONE; index < PM_SLEEP_MODE_MAX; index ++)
{
if (pm->modes[index])
{
mode = index;
break;
}
}
/* select the high power mode */
if (request_mode < mode)
mode = request_mode;
return mode;
}
/**
* pm module request delay sleep.
*/
void rt_pm_module_delay_sleep(rt_uint8_t module_id, rt_tick_t timeout)
{
rt_base_t level;
struct rt_pm *pm;
if (_pm_init_flag == 0)
return;
if (module_id > (PM_MODULE_MAX_ID - 1))
return;
level = rt_hw_interrupt_disable();
pm = &_pm;
pm->module_status[module_id].busy_flag = RT_TRUE;
pm->module_status[module_id].timeout = timeout;
pm->module_status[module_id].start_time = rt_tick_get();
rt_hw_interrupt_enable(level);
}
/**
* This function check if all modules in idle status.
*/
static rt_bool_t _pm_device_check_idle(void)
{
struct rt_pm *pm;
if (_pm_init_flag == 0)
return RT_TRUE;
pm = &_pm;
for (int i = 0; i < PM_MODULE_MAX_ID; i++)
{
if (pm->module_status[i].busy_flag == RT_TRUE)
{
if (rt_tick_get() - pm->module_status[i].start_time > pm->module_status[i].timeout)
{
pm->module_status[i].busy_flag = RT_FALSE;
pm->module_status[i].timeout = 0x00;
}
}
if (pm->module_status[i].busy_flag == RT_TRUE)
{
return RT_FALSE;
}
}
return RT_TRUE;
}
RT_WEAK rt_tick_t pm_timer_next_timeout_tick(rt_uint8_t mode)
{
switch (mode)
{
case PM_SLEEP_MODE_LIGHT:
return rt_timer_next_timeout_tick();
case PM_SLEEP_MODE_DEEP:
case PM_SLEEP_MODE_STANDBY:
return rt_lptimer_next_timeout_tick();
}
return RT_TICK_MAX;
}
/**
* This function will judge sleep mode from threshold timeout.
*
* @param cur_mode the current pm sleep mode
* @param timeout_tick the threshold timeout
*
* @return none
*/
RT_WEAK rt_uint8_t pm_get_sleep_threshold_mode(rt_uint8_t cur_mode, rt_tick_t timeout_tick)
{
rt_uint8_t sleep_mode = cur_mode;
if (_pm_init_flag == 0)
return sleep_mode;
if (cur_mode >= PM_SLEEP_MODE_MAX)
return sleep_mode;
#ifdef PM_ENABLE_THRESHOLD_SLEEP_MODE
switch (cur_mode)
{
case PM_SLEEP_MODE_NONE:
case PM_SLEEP_MODE_IDLE:
break;
case PM_SLEEP_MODE_LIGHT:
if (timeout_tick < PM_LIGHT_THRESHOLD_TIME)
sleep_mode = PM_SLEEP_MODE_IDLE;
break;
case PM_SLEEP_MODE_DEEP:
if (timeout_tick < PM_LIGHT_THRESHOLD_TIME)
sleep_mode = PM_SLEEP_MODE_IDLE;
else if (timeout_tick < PM_DEEP_THRESHOLD_TIME)
sleep_mode = PM_SLEEP_MODE_LIGHT;
break;
case PM_SLEEP_MODE_STANDBY:
if (timeout_tick < PM_LIGHT_THRESHOLD_TIME)
sleep_mode = PM_SLEEP_MODE_IDLE;
else if (timeout_tick < PM_DEEP_THRESHOLD_TIME)
sleep_mode = PM_SLEEP_MODE_LIGHT;
else if (timeout_tick < PM_STANDBY_THRESHOLD_TIME)
sleep_mode = PM_SLEEP_MODE_DEEP;
}
#else
if (timeout_tick < PM_TICKLESS_THRESHOLD_TIME)
{
cur_mode = PM_SLEEP_MODE_IDLE;
}
#endif
return cur_mode;
}
/**
* This function changes the power sleep mode base on the result of selection
*/
static void _pm_change_sleep_mode(struct rt_pm *pm)
{
rt_tick_t timeout_tick, delta_tick;
rt_base_t level;
uint8_t sleep_mode = PM_SLEEP_MODE_DEEP;
level = rt_pm_enter_critical(pm->sleep_mode);
/* judge sleep mode from module request */
pm->sleep_mode = _pm_select_sleep_mode(pm);
/* module busy request check */
if (_pm_device_check_idle() == RT_FALSE)
{
sleep_mode = PM_BUSY_SLEEP_MODE;
if (sleep_mode < pm->sleep_mode)
{
pm->sleep_mode = sleep_mode; /* judge the highest sleep mode */
}
}
if (_pm.sleep_mode == PM_SLEEP_MODE_NONE)
{
pm->ops->sleep(pm, PM_SLEEP_MODE_NONE);
rt_pm_exit_critical(level, pm->sleep_mode);
}
else
{
/* Notify app will enter sleep mode */
if (_pm_notify.notify)
_pm_notify.notify(RT_PM_ENTER_SLEEP, pm->sleep_mode, _pm_notify.data);
/* Suspend all peripheral device */
#ifdef PM_ENABLE_SUSPEND_SLEEP_MODE
int ret = _pm_device_suspend(pm->sleep_mode);
if (ret != RT_EOK)
{
_pm_device_resume(pm->sleep_mode);
if (_pm_notify.notify)
_pm_notify.notify(RT_PM_EXIT_SLEEP, pm->sleep_mode, _pm_notify.data);
if (pm->sleep_mode > PM_SUSPEND_SLEEP_MODE)
{
pm->sleep_mode = PM_SUSPEND_SLEEP_MODE;
}
pm->ops->sleep(pm, pm->sleep_mode); /* suspend failed */
rt_pm_exit_critical(level, pm->sleep_mode);
return;
}
#else
_pm_device_suspend(pm->sleep_mode);
#endif
/* Tickless*/
if (pm->timer_mask & (0x01 << pm->sleep_mode))
{
timeout_tick = pm_timer_next_timeout_tick(pm->sleep_mode);
timeout_tick = timeout_tick - rt_tick_get();
/* Judge sleep_mode from threshold time */
pm->sleep_mode = pm_get_sleep_threshold_mode(pm->sleep_mode, timeout_tick);
if (pm->timer_mask & (0x01 << pm->sleep_mode))
{
if (timeout_tick == RT_TICK_MAX)
{
pm_lptimer_start(pm, RT_TICK_MAX);
}
else
{
pm_lptimer_start(pm, timeout_tick);
}
}
}
/* enter lower power state */
pm_sleep(pm, pm->sleep_mode);
/* wake up from lower power state*/
if (pm->timer_mask & (0x01 << pm->sleep_mode))
{
delta_tick = pm_lptimer_get_timeout(pm);
pm_lptimer_stop(pm);
if (delta_tick)
{
rt_tick_set(rt_tick_get() + delta_tick);
}
}
/* resume all device */
_pm_device_resume(pm->sleep_mode);
if (_pm_notify.notify)
_pm_notify.notify(RT_PM_EXIT_SLEEP, pm->sleep_mode, _pm_notify.data);
rt_pm_exit_critical(level, pm->sleep_mode);
if (pm->timer_mask & (0x01 << pm->sleep_mode))
{
if (delta_tick)
{
rt_timer_check();
}
}
}
}
/**
* This function will enter corresponding power mode.
*/
void rt_system_power_manager(void)
{
if (_pm_init_flag == 0)
return;
/* CPU frequency scaling according to the runing mode settings */
_pm_frequency_scaling(&_pm);
/* Low Power Mode Processing */
_pm_change_sleep_mode(&_pm);
}
/**
* Upper application or device driver requests the system
* stall in corresponding power mode.
*
* @param parameter the parameter of run mode or sleep mode
*/
void rt_pm_request(rt_uint8_t mode)
{
rt_base_t level;
struct rt_pm *pm;
if (_pm_init_flag == 0)
return;
if (mode > (PM_SLEEP_MODE_MAX - 1))
return;
level = rt_hw_interrupt_disable();
pm = &_pm;
if (pm->modes[mode] < 255)
pm->modes[mode] ++;
rt_hw_interrupt_enable(level);
}
/**
* Upper application or device driver releases the stall
* of corresponding power mode.
*
* @param parameter the parameter of run mode or sleep mode
*
*/
void rt_pm_release(rt_uint8_t mode)
{
rt_ubase_t level;
struct rt_pm *pm;
if (_pm_init_flag == 0)
return;
if (mode > (PM_SLEEP_MODE_MAX - 1))
return;
level = rt_hw_interrupt_disable();
pm = &_pm;
if (pm->modes[mode] > 0)
pm->modes[mode] --;
rt_hw_interrupt_enable(level);
}
/**
* Upper application or device driver releases all the stall
* of corresponding power mode.
*
* @param parameter the parameter of run mode or sleep mode
*
*/
void rt_pm_release_all(rt_uint8_t mode)
{
rt_ubase_t level;
struct rt_pm *pm;
if (_pm_init_flag == 0)
return;
if (mode > (PM_SLEEP_MODE_MAX - 1))
return;
level = rt_hw_interrupt_disable();
pm = &_pm;
pm->modes[mode] = 0;
rt_hw_interrupt_enable(level);
}
/**
* Upper application or device driver requests the system
* stall in corresponding power mode.
*
* @param module_id the application or device module id
* @param mode the system power sleep mode
*/
void rt_pm_module_request(uint8_t module_id, rt_uint8_t mode)
{
rt_base_t level;
struct rt_pm *pm;
if (_pm_init_flag == 0)
return;
if (mode > (PM_SLEEP_MODE_MAX - 1))
return;
if (module_id > (PM_MODULE_MAX_ID - 1))
return;
level = rt_hw_interrupt_disable();
pm = &_pm;
pm->module_status[module_id].req_status = 0x01;
if (pm->modes[mode] < 255)
pm->modes[mode] ++;
rt_hw_interrupt_enable(level);
}
/**
* Upper application or device driver releases the stall
* of corresponding power mode.
*
* @param module_id the application or device module id
* @param mode the system power sleep mode
*
*/
void rt_pm_module_release(uint8_t module_id, rt_uint8_t mode)
{
rt_ubase_t level;
struct rt_pm *pm;
if (_pm_init_flag == 0)
return;
if (mode > (PM_SLEEP_MODE_MAX - 1))
return;
if (module_id > (PM_MODULE_MAX_ID - 1))
return;
level = rt_hw_interrupt_disable();
pm = &_pm;
if (pm->modes[mode] > 0)
pm->modes[mode] --;
if (pm->modes[mode] == 0)
pm->module_status[module_id].req_status = 0x00;
rt_hw_interrupt_enable(level);
}
/**
* Upper application or device driver releases all the stall
* of corresponding power mode.
*
* @param module_id the application or device module id
* @param mode the system power sleep mode
*
*/
void rt_pm_module_release_all(uint8_t module_id, rt_uint8_t mode)
{
rt_ubase_t level;
struct rt_pm *pm;
if (_pm_init_flag == 0)
return;
if (mode > (PM_SLEEP_MODE_MAX - 1))
return;
level = rt_hw_interrupt_disable();
pm = &_pm;
pm->modes[mode] = 0;
pm->module_status[module_id].req_status = 0x00;
rt_hw_interrupt_enable(level);
}
/**
* This function will let current module work with specified sleep mode.
*
* @param module_id the pm module id
* @param mode the pm sleep mode
*
* @return none
*/
void rt_pm_sleep_request(rt_uint16_t module_id, rt_uint8_t mode)
{
rt_uint32_t level;
if (module_id >= PM_MODULE_MAX_ID)
{
return;
}
if (mode >= (PM_SLEEP_MODE_MAX - 1))
{
return;
}
level = rt_hw_interrupt_disable();
_pm.sleep_status[mode][module_id / 32] |= 1 << (module_id % 32);
rt_hw_interrupt_enable(level);
}
/**
* This function will let current module work with PM_SLEEP_MODE_NONE mode.
*
* @param module_id the pm module id
*
* @return NULL
*/
void rt_pm_sleep_none_request(rt_uint16_t module_id)
{
rt_pm_sleep_request(module_id, PM_SLEEP_MODE_NONE);
}
/**
* This function will let current module work with PM_SLEEP_MODE_IDLE mode.
*
* @param module_id the pm module id
*
* @return NULL
*/
void rt_pm_sleep_idle_request(rt_uint16_t module_id)
{
rt_pm_sleep_request(module_id, PM_SLEEP_MODE_IDLE);
}
/**
* This function will let current module work with PM_SLEEP_MODE_LIGHT mode.
*
* @param module_id the pm module id
*
* @return NULL
*/
void rt_pm_sleep_light_request(rt_uint16_t module_id)
{
rt_pm_sleep_request(module_id, PM_SLEEP_MODE_LIGHT);
}
/**
* When current module don't work, release requested sleep mode.
*
* @param module_id the pm module id
* @param mode the pm sleep mode
*
* @return NULL
*/
void rt_pm_sleep_release(rt_uint16_t module_id, rt_uint8_t mode)
{
rt_uint32_t level;
if (module_id >= PM_MODULE_MAX_ID)
{
return;
}
if (mode >= (PM_SLEEP_MODE_MAX - 1))
{
return;
}
level = rt_hw_interrupt_disable();
_pm.sleep_status[mode][module_id / 32] &= ~(1 << (module_id % 32));
rt_hw_interrupt_enable(level);
}
/**
* The specified module release the requested PM_SLEEP_MODE_NONE mode
*
* @param module_id the pm module id
*
* @return none
*/
void rt_pm_sleep_none_release(rt_uint16_t module_id)
{
rt_pm_sleep_release(module_id, PM_SLEEP_MODE_NONE);
}
/**
* The specified module release the requested PM_SLEEP_MODE_IDLE mode
*
* @param module_id the pm module id
*
* @return none
*/
void rt_pm_sleep_idle_release(rt_uint16_t module_id)
{
rt_pm_sleep_release(module_id, PM_SLEEP_MODE_IDLE);
}
/**
* The specified module release the requested PM_SLEEP_MODE_LIGHT mode
*
* @param module_id the pm module id
*
* @return none
*/
void rt_pm_sleep_light_release(rt_uint16_t module_id)
{
rt_pm_sleep_release(module_id, PM_SLEEP_MODE_LIGHT);
}
/**
* Register a device with PM feature
*
* @param device the device with PM feature
* @param ops the PM ops for device
*/
void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops)
{
rt_base_t level;
struct rt_device_pm *device_pm;
RT_DEBUG_NOT_IN_INTERRUPT;
level = rt_hw_interrupt_disable();
device_pm = (struct rt_device_pm *)RT_KERNEL_REALLOC(_pm.device_pm,
(_pm.device_pm_number + 1) * sizeof(struct rt_device_pm));
if (device_pm != RT_NULL)
{
_pm.device_pm = device_pm;
_pm.device_pm[_pm.device_pm_number].device = device;
_pm.device_pm[_pm.device_pm_number].ops = ops;
_pm.device_pm_number += 1;
}
rt_hw_interrupt_enable(level);
}
/**
* Unregister device from PM manager.
*
* @param device the device with PM feature
*/
void rt_pm_device_unregister(struct rt_device *device)
{
rt_ubase_t level;
rt_uint32_t index;
RT_DEBUG_NOT_IN_INTERRUPT;
level = rt_hw_interrupt_disable();
for (index = 0; index < _pm.device_pm_number; index ++)
{
if (_pm.device_pm[index].device == device)
{
/* remove current entry */
for (; index < _pm.device_pm_number - 1; index ++)
{
_pm.device_pm[index] = _pm.device_pm[index + 1];
}
_pm.device_pm[_pm.device_pm_number - 1].device = RT_NULL;
_pm.device_pm[_pm.device_pm_number - 1].ops = RT_NULL;
_pm.device_pm_number -= 1;
/* break out and not touch memory */
break;
}
}
rt_hw_interrupt_enable(level);
}
/**
* This function set notification callback for application
*/
void rt_pm_notify_set(void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data), void *data)
{
_pm_notify.notify = notify;
_pm_notify.data = data;
}
/**
* This function set default sleep mode when no pm_request
*/
void rt_pm_default_set(rt_uint8_t sleep_mode)
{
_pm_default_sleep = sleep_mode;
}
/**
* RT-Thread device interface for PM device
*/
static rt_size_t _rt_pm_device_read(rt_device_t dev,
rt_off_t pos,
void *buffer,
rt_size_t size)
{
struct rt_pm *pm;
rt_size_t length;
length = 0;
pm = (struct rt_pm *)dev;
RT_ASSERT(pm != RT_NULL);
if (pos < PM_SLEEP_MODE_MAX)
{
int mode;
mode = pm->modes[pos];
length = rt_snprintf(buffer, size, "%d", mode);
}
return length;
}
static rt_size_t _rt_pm_device_write(rt_device_t dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
unsigned char request;
if (size)
{
/* get request */
request = *(unsigned char *)buffer;
if (request == 0x01)
{
rt_pm_request(pos);
}
else if (request == 0x00)
{
rt_pm_release(pos);
}
}
return 1;
}
static rt_err_t _rt_pm_device_control(rt_device_t dev,
int cmd,
void *args)
{
rt_uint32_t mode;
switch (cmd)
{
case RT_PM_DEVICE_CTRL_REQUEST:
mode = (rt_uint32_t)args;
rt_pm_request(mode);
break;
case RT_PM_DEVICE_CTRL_RELEASE:
mode = (rt_uint32_t)args;
rt_pm_release(mode);
break;
}
return RT_EOK;
}
int rt_pm_run_enter(rt_uint8_t mode)
{
rt_base_t level;
struct rt_pm *pm;
if (_pm_init_flag == 0)
return -RT_EIO;
if (mode > PM_RUN_MODE_MAX)
return -RT_EINVAL;
level = rt_hw_interrupt_disable();
pm = &_pm;
if (mode < pm->run_mode)
{
/* change system runing mode */
pm->ops->run(pm, mode);
/* changer device frequency */
_pm_device_frequency_change(mode);
}
else
{
pm->flags |= RT_PM_FREQUENCY_PENDING;
}
pm->run_mode = mode;
rt_hw_interrupt_enable(level);
return RT_EOK;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops pm_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
_rt_pm_device_read,
_rt_pm_device_write,
_rt_pm_device_control,
};
#endif
/**
* This function will initialize power management.
*
* @param ops the PM operations.
* @param timer_mask indicates which mode has timer feature.
* @param user_data user data
*/
void rt_system_pm_init(const struct rt_pm_ops *ops,
rt_uint8_t timer_mask,
void *user_data)
{
struct rt_device *device;
struct rt_pm *pm;
pm = &_pm;
device = &(_pm.parent);
device->type = RT_Device_Class_PM;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &pm_ops;
#else
device->init = RT_NULL;
device->open = RT_NULL;
device->close = RT_NULL;
device->read = _rt_pm_device_read;
device->write = _rt_pm_device_write;
device->control = _rt_pm_device_control;
#endif
device->user_data = user_data;
/* register PM device to the system */
rt_device_register(device, "pm", RT_DEVICE_FLAG_RDWR);
rt_memset(pm->modes, 0, sizeof(pm->modes));
pm->sleep_mode = _pm_default_sleep;
/* when system power on, set default sleep modes */
pm->modes[pm->sleep_mode] = 1;
pm->module_status[PM_POWER_ID].req_status = 1;
pm->run_mode = RT_PM_DEFAULT_RUN_MODE;
pm->timer_mask = timer_mask;
pm->ops = ops;
pm->device_pm = RT_NULL;
pm->device_pm_number = 0;
#if IDLE_THREAD_STACK_SIZE <= 256
#error "[pm.c ERR] IDLE Stack Size Too Small!"
#endif
_pm_init_flag = 1;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
static const char *_pm_sleep_str[] = PM_SLEEP_MODE_NAMES;
static const char *_pm_run_str[] = PM_RUN_MODE_NAMES;
static void rt_pm_release_mode(int argc, char **argv)
{
int mode = 0;
if (argc >= 2)
{
mode = atoi(argv[1]);
}
rt_pm_release(mode);
}
MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode, pm_release, release power management mode);
static void rt_pm_release_mode_all(int argc, char **argv)
{
int mode = 0;
if (argc >= 2)
{
mode = atoi(argv[1]);
}
rt_pm_release_all(mode);
}
MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode_all, pm_release_all, release power management mode count);
static void rt_pm_request_mode(int argc, char **argv)
{
int mode = 0;
if (argc >= 2)
{
mode = atoi(argv[1]);
}
rt_pm_request(mode);
}
MSH_CMD_EXPORT_ALIAS(rt_pm_request_mode, pm_request, request power management mode);
static void rt_module_release_mode(int argc, char **argv)
{
int module = 0;
int mode = 0;
if (argc >= 3)
{
module = atoi(argv[1]);
mode = atoi(argv[2]);
}
rt_pm_module_release(module, mode);
}
MSH_CMD_EXPORT_ALIAS(rt_module_release_mode, pm_module_release, release module power mode);
static void rt_module_release_mode_all(int argc, char **argv)
{
int module = 0;
int mode = 0;
if (argc >= 3)
{
module = atoi(argv[1]);
mode = atoi(argv[2]);
}
rt_pm_module_release_all(module, mode);
}
MSH_CMD_EXPORT_ALIAS(rt_module_release_mode_all, pm_module_release_all, release power management mode count);
static void rt_module_request_mode(int argc, char **argv)
{
int module = 0;
int mode = 0;
if (argc >= 3)
{
module = atoi(argv[1]);
mode = atoi(argv[2]);
}
rt_pm_module_request(module, mode);
}
MSH_CMD_EXPORT_ALIAS(rt_module_request_mode, pm_module_request, request power management mode);
static void rt_module_delay_sleep(int argc, char **argv)
{
int module = 0;
unsigned int timeout = 0;
if (argc >= 3)
{
module = atoi(argv[1]);
timeout = atoi(argv[2]);
}
rt_pm_module_delay_sleep(module, timeout);
}
MSH_CMD_EXPORT_ALIAS(rt_module_delay_sleep, pm_module_delay, module request delay sleep);
static void rt_pm_run_mode_switch(int argc, char **argv)
{
int mode = 0;
if (argc >= 2)
{
mode = atoi(argv[1]);
}
rt_pm_run_enter(mode);
}
MSH_CMD_EXPORT_ALIAS(rt_pm_run_mode_switch, pm_run, switch power management run mode);
rt_uint32_t rt_pm_module_get_status(void)
{
rt_uint8_t index = 0;
struct rt_pm *pm;
rt_uint32_t req_status = 0x00;
pm = &_pm;
for (index = 0; index < PM_MODULE_MAX_ID; index ++)
{
if (pm->module_status[index].req_status == 0x01)
req_status |= 1<<index;
}
return req_status;
}
rt_uint8_t rt_pm_get_sleep_mode(void)
{
struct rt_pm *pm;
pm = &_pm;
return pm->sleep_mode;
}
/* get pm entity pointer */
struct rt_pm *rt_pm_get_handle(void)
{
return &_pm;
}
#ifdef PM_ENABLE_DEBUG
/**
* print current module sleep request list
*
* @param none
*
* @return none
*/
void pm_sleep_dump(void)
{
uint8_t index;
uint16_t len;
rt_kprintf("+-------------+--------------+\n");
rt_kprintf("| Sleep Mode | Request List |\n");
rt_kprintf("+-------------+--------------+\n");
for (index = 0; index < PM_SLEEP_MODE_MAX -1; index++)
{
for (len = 0; len < ((PM_MODULE_MAX_ID + 31) / 32); len++)
{
rt_kprintf("| Mode[%d] : %d | 0x%08x |\n", index, len,
_pm.sleep_status[index][len]);
}
}
rt_kprintf("+-------------+--------------+\n");
}
MSH_CMD_EXPORT(pm_sleep_dump, dump pm request list);
static void pm_sleep_request(int argc, char **argv)
{
int module = 0;
int mode = 0;
if (argc >= 3)
{
module = atoi(argv[1]);
mode = atoi(argv[2]);
rt_pm_sleep_request(module, mode);
}
}
MSH_CMD_EXPORT(pm_sleep_request, pm_sleep_request module sleep_mode);
static void pm_sleep_release(int argc, char **argv)
{
int module = 0;
int mode = 0;
if (argc >= 3)
{
module = atoi(argv[1]);
mode = atoi(argv[2]);
rt_pm_sleep_release(module, mode);
}
}
MSH_CMD_EXPORT(pm_sleep_release, pm_sleep_release module sleep_mode);
#endif
static void rt_pm_dump_status(void)
{
rt_uint32_t index;
struct rt_pm *pm;
pm = &_pm;
rt_kprintf("| Power Management Mode | Counter | Timer |\n");
rt_kprintf("+-----------------------+---------+-------+\n");
for (index = 0; index < PM_SLEEP_MODE_MAX; index ++)
{
int has_timer = 0;
if (pm->timer_mask & (1 << index))
has_timer = 1;
rt_kprintf("| %021s | %7d | %5d |\n", _pm_sleep_str[index], pm->modes[index], has_timer);
}
rt_kprintf("+-----------------------+---------+-------+\n");
rt_kprintf("pm current sleep mode: %s\n", _pm_sleep_str[pm->sleep_mode]);
rt_kprintf("pm current run mode: %s\n", _pm_run_str[pm->run_mode]);
rt_kprintf("\n");
rt_kprintf("| module | busy | start time | timeout |\n");
rt_kprintf("+--------+------+------------+-----------+\n");
for (index = 0; index < PM_MODULE_MAX_ID; index ++)
{
if ((pm->module_status[index].busy_flag == RT_TRUE) ||
(pm->module_status[index].req_status != 0x00))
{
rt_kprintf("| %04d | %d | 0x%08x | 0x%08x |\n",
index, pm->module_status[index].busy_flag,
pm->module_status[index].start_time,
pm->module_status[index].timeout);
}
}
rt_kprintf("+--------+------+------------+-----------+\n");
}
FINSH_FUNCTION_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
MSH_CMD_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
#endif
#endif /* RT_USING_PM */