1
0

pm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-06-02 Bernard the first version
  9. * 2018-08-02 Tanek split run and sleep modes, support custom mode
  10. * 2019-04-28 Zero-Free improve PM mode and device ops interface
  11. * 2020-11-23 zhangsz update pm mode select
  12. * 2020-11-27 zhangsz update pm 2.0
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include <drivers/pm.h>
  17. #ifdef RT_USING_PM
  18. static struct rt_pm _pm;
  19. /* default mode : system power on */
  20. static rt_uint8_t _pm_default_sleep = RT_PM_DEFAULT_SLEEP_MODE;
  21. /* default deepsleep mode : tick-less mode */
  22. static rt_uint8_t _pm_default_deepsleep = RT_PM_DEFAULT_DEEPSLEEP_MODE;
  23. static struct rt_pm_notify _pm_notify;
  24. static rt_uint8_t _pm_init_flag = 0;
  25. #define RT_PM_TICKLESS_THRESH (2)
  26. RT_WEAK rt_uint32_t rt_pm_enter_critical(rt_uint8_t sleep_mode)
  27. {
  28. return rt_hw_interrupt_disable();
  29. }
  30. RT_WEAK void rt_pm_exit_critical(rt_uint32_t ctx, rt_uint8_t sleep_mode)
  31. {
  32. rt_hw_interrupt_enable(ctx);
  33. }
  34. /**
  35. * This function will suspend all registered devices
  36. */
  37. static int _pm_device_suspend(rt_uint8_t mode)
  38. {
  39. int index, ret = RT_EOK;
  40. for (index = 0; index < _pm.device_pm_number; index++)
  41. {
  42. if (_pm.device_pm[index].ops->suspend != RT_NULL)
  43. {
  44. ret = _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device, mode);
  45. if(ret != RT_EOK)
  46. break;
  47. }
  48. }
  49. return ret;
  50. }
  51. /**
  52. * This function will resume all registered devices
  53. */
  54. static void _pm_device_resume(rt_uint8_t mode)
  55. {
  56. int index;
  57. for (index = 0; index < _pm.device_pm_number; index++)
  58. {
  59. if (_pm.device_pm[index].ops->resume != RT_NULL)
  60. {
  61. _pm.device_pm[index].ops->resume(_pm.device_pm[index].device, mode);
  62. }
  63. }
  64. }
  65. /**
  66. * This function will update the frequency of all registered devices
  67. */
  68. static void _pm_device_frequency_change(rt_uint8_t mode)
  69. {
  70. rt_uint32_t index;
  71. /* make the frequency change */
  72. for (index = 0; index < _pm.device_pm_number; index ++)
  73. {
  74. if (_pm.device_pm[index].ops->frequency_change != RT_NULL)
  75. _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device, mode);
  76. }
  77. }
  78. /**
  79. * This function will update the system clock frequency when idle
  80. */
  81. static void _pm_frequency_scaling(struct rt_pm *pm)
  82. {
  83. rt_base_t level;
  84. if (pm->flags & RT_PM_FREQUENCY_PENDING)
  85. {
  86. level = rt_hw_interrupt_disable();
  87. /* change system runing mode */
  88. pm->ops->run(pm, pm->run_mode);
  89. /* changer device frequency */
  90. _pm_device_frequency_change(pm->run_mode);
  91. pm->flags &= ~RT_PM_FREQUENCY_PENDING;
  92. rt_hw_interrupt_enable(level);
  93. }
  94. }
  95. /**
  96. * This function selects the sleep mode according to the rt_pm_request/rt_pm_release count.
  97. */
  98. static rt_uint8_t _pm_select_sleep_mode(struct rt_pm *pm)
  99. {
  100. int index;
  101. rt_uint8_t mode;
  102. mode = _pm_default_deepsleep;
  103. for (index = PM_SLEEP_MODE_NONE; index < PM_SLEEP_MODE_MAX; index ++)
  104. {
  105. if (pm->modes[index])
  106. {
  107. mode = index;
  108. break;
  109. }
  110. }
  111. pm->sleep_mode = mode;
  112. return mode;
  113. }
  114. /**
  115. * pm module request delay sleep.
  116. */
  117. void rt_pm_module_delay_sleep(rt_uint8_t module_id, rt_tick_t timeout)
  118. {
  119. rt_base_t level;
  120. struct rt_pm *pm;
  121. if (_pm_init_flag == 0)
  122. return;
  123. if (module_id > (PM_MODULE_MAX_ID - 1))
  124. return;
  125. level = rt_hw_interrupt_disable();
  126. pm = &_pm;
  127. pm->module_status[module_id].busy_flag = RT_TRUE;
  128. pm->module_status[module_id].timeout = timeout;
  129. pm->module_status[module_id].start_time = rt_tick_get();
  130. rt_hw_interrupt_enable(level);
  131. }
  132. /**
  133. * This function check if all modules in idle status.
  134. */
  135. static rt_bool_t _pm_device_check_idle(void)
  136. {
  137. struct rt_pm *pm;
  138. if (_pm_init_flag == 0)
  139. return RT_TRUE;
  140. pm = &_pm;
  141. for (int i = 0; i < PM_MODULE_MAX_ID; i++)
  142. {
  143. if (pm->module_status[i].busy_flag == RT_TRUE)
  144. {
  145. if (rt_tick_get() - pm->module_status[i].start_time > pm->module_status[i].timeout)
  146. {
  147. pm->module_status[i].busy_flag = RT_FALSE;
  148. pm->module_status[i].timeout = 0x00;
  149. }
  150. }
  151. if (pm->module_status[i].busy_flag == RT_TRUE)
  152. {
  153. return RT_FALSE;
  154. }
  155. }
  156. return RT_TRUE;
  157. }
  158. /**
  159. * This function changes the power sleep mode base on the result of selection
  160. */
  161. static void _pm_change_sleep_mode(struct rt_pm *pm)
  162. {
  163. rt_tick_t timeout_tick, delta_tick;
  164. rt_base_t level;
  165. int ret = RT_EOK;
  166. level = rt_pm_enter_critical(pm->sleep_mode);
  167. /* module busy request */
  168. if (_pm_device_check_idle() == RT_FALSE)
  169. {
  170. pm->ops->sleep(pm, PM_SLEEP_MODE_NONE);
  171. rt_pm_exit_critical(level, pm->sleep_mode);
  172. return;
  173. }
  174. if (_pm.sleep_mode == PM_SLEEP_MODE_NONE)
  175. {
  176. pm->ops->sleep(pm, PM_SLEEP_MODE_NONE);
  177. rt_pm_exit_critical(level, pm->sleep_mode);
  178. }
  179. else
  180. {
  181. /* Notify app will enter sleep mode */
  182. if (_pm_notify.notify)
  183. _pm_notify.notify(RT_PM_ENTER_SLEEP, pm->sleep_mode, _pm_notify.data);
  184. /* Suspend all peripheral device */
  185. ret = _pm_device_suspend(pm->sleep_mode);
  186. if (ret != RT_EOK)
  187. {
  188. _pm_device_resume(pm->sleep_mode);
  189. if (_pm_notify.notify)
  190. _pm_notify.notify(RT_PM_EXIT_SLEEP, pm->sleep_mode, _pm_notify.data);
  191. rt_pm_exit_critical(level, pm->sleep_mode);
  192. return;
  193. }
  194. /* Tickless*/
  195. if (pm->timer_mask & (0x01 << pm->sleep_mode))
  196. {
  197. timeout_tick = rt_timer_next_timeout_tick();
  198. if (timeout_tick == RT_TICK_MAX)
  199. {
  200. if (pm->ops->timer_start)
  201. {
  202. pm->ops->timer_start(pm, RT_TICK_MAX);
  203. }
  204. }
  205. else
  206. {
  207. timeout_tick = timeout_tick - rt_tick_get();
  208. if (timeout_tick < RT_PM_TICKLESS_THRESH)
  209. {
  210. pm->sleep_mode = PM_SLEEP_MODE_IDLE;
  211. }
  212. else
  213. {
  214. pm->ops->timer_start(pm, timeout_tick);
  215. }
  216. }
  217. }
  218. /* enter lower power state */
  219. pm->ops->sleep(pm, pm->sleep_mode);
  220. /* wake up from lower power state*/
  221. if (pm->timer_mask & (0x01 << pm->sleep_mode))
  222. {
  223. delta_tick = pm->ops->timer_get_tick(pm);
  224. pm->ops->timer_stop(pm);
  225. if (delta_tick)
  226. {
  227. rt_tick_set(rt_tick_get() + delta_tick);
  228. }
  229. }
  230. /* resume all device */
  231. _pm_device_resume(pm->sleep_mode);
  232. if (_pm_notify.notify)
  233. _pm_notify.notify(RT_PM_EXIT_SLEEP, pm->sleep_mode, _pm_notify.data);
  234. rt_pm_exit_critical(level, pm->sleep_mode);
  235. if (pm->timer_mask & (0x01 << pm->sleep_mode))
  236. {
  237. if (delta_tick)
  238. {
  239. rt_timer_check();
  240. }
  241. }
  242. }
  243. }
  244. /**
  245. * This function will enter corresponding power mode.
  246. */
  247. void rt_system_power_manager(void)
  248. {
  249. if (_pm_init_flag == 0)
  250. return;
  251. /* CPU frequency scaling according to the runing mode settings */
  252. _pm_frequency_scaling(&_pm);
  253. /* Low Power Mode Processing */
  254. _pm_change_sleep_mode(&_pm);
  255. }
  256. /**
  257. * Upper application or device driver requests the system
  258. * stall in corresponding power mode.
  259. *
  260. * @param parameter the parameter of run mode or sleep mode
  261. */
  262. void rt_pm_request(rt_uint8_t mode)
  263. {
  264. rt_base_t level;
  265. struct rt_pm *pm;
  266. if (_pm_init_flag == 0)
  267. return;
  268. if (mode > (PM_SLEEP_MODE_MAX - 1))
  269. return;
  270. level = rt_hw_interrupt_disable();
  271. pm = &_pm;
  272. if (pm->modes[mode] < 255)
  273. pm->modes[mode] ++;
  274. _pm_select_sleep_mode(pm);
  275. rt_hw_interrupt_enable(level);
  276. }
  277. /**
  278. * Upper application or device driver releases the stall
  279. * of corresponding power mode.
  280. *
  281. * @param parameter the parameter of run mode or sleep mode
  282. *
  283. */
  284. void rt_pm_release(rt_uint8_t mode)
  285. {
  286. rt_ubase_t level;
  287. struct rt_pm *pm;
  288. if (_pm_init_flag == 0)
  289. return;
  290. if (mode > (PM_SLEEP_MODE_MAX - 1))
  291. return;
  292. level = rt_hw_interrupt_disable();
  293. pm = &_pm;
  294. if (pm->modes[mode] > 0)
  295. pm->modes[mode] --;
  296. _pm_select_sleep_mode(pm);
  297. rt_hw_interrupt_enable(level);
  298. }
  299. /**
  300. * Upper application or device driver releases all the stall
  301. * of corresponding power mode.
  302. *
  303. * @param parameter the parameter of run mode or sleep mode
  304. *
  305. */
  306. void rt_pm_release_all(rt_uint8_t mode)
  307. {
  308. rt_ubase_t level;
  309. struct rt_pm *pm;
  310. if (_pm_init_flag == 0)
  311. return;
  312. if (mode > (PM_SLEEP_MODE_MAX - 1))
  313. return;
  314. level = rt_hw_interrupt_disable();
  315. pm = &_pm;
  316. pm->modes[mode] = 0;
  317. _pm_select_sleep_mode(pm);
  318. rt_hw_interrupt_enable(level);
  319. }
  320. /**
  321. * Upper application or device driver requests the system
  322. * stall in corresponding power mode.
  323. *
  324. * @param module_id the application or device module id
  325. * @param mode the system power sleep mode
  326. */
  327. void rt_pm_module_request(uint8_t module_id, rt_uint8_t mode)
  328. {
  329. rt_base_t level;
  330. struct rt_pm *pm;
  331. if (_pm_init_flag == 0)
  332. return;
  333. if (mode > (PM_SLEEP_MODE_MAX - 1))
  334. return;
  335. if (module_id > (PM_MODULE_MAX_ID - 1))
  336. return;
  337. level = rt_hw_interrupt_disable();
  338. pm = &_pm;
  339. pm->module_status[module_id].req_status = 0x01;
  340. if (pm->modes[mode] < 255)
  341. pm->modes[mode] ++;
  342. _pm_select_sleep_mode(pm);
  343. rt_hw_interrupt_enable(level);
  344. }
  345. /**
  346. * Upper application or device driver releases the stall
  347. * of corresponding power mode.
  348. *
  349. * @param module_id the application or device module id
  350. * @param mode the system power sleep mode
  351. *
  352. */
  353. void rt_pm_module_release(uint8_t module_id, rt_uint8_t mode)
  354. {
  355. rt_ubase_t level;
  356. struct rt_pm *pm;
  357. if (_pm_init_flag == 0)
  358. return;
  359. if (mode > (PM_SLEEP_MODE_MAX - 1))
  360. return;
  361. if (module_id > (PM_MODULE_MAX_ID - 1))
  362. return;
  363. level = rt_hw_interrupt_disable();
  364. pm = &_pm;
  365. if (pm->modes[mode] > 0)
  366. pm->modes[mode] --;
  367. if (pm->modes[mode] == 0)
  368. pm->module_status[module_id].req_status = 0x00;
  369. _pm_select_sleep_mode(pm);
  370. rt_hw_interrupt_enable(level);
  371. }
  372. /**
  373. * Upper application or device driver releases all the stall
  374. * of corresponding power mode.
  375. *
  376. * @param module_id the application or device module id
  377. * @param mode the system power sleep mode
  378. *
  379. */
  380. void rt_pm_module_release_all(uint8_t module_id, rt_uint8_t mode)
  381. {
  382. rt_ubase_t level;
  383. struct rt_pm *pm;
  384. if (_pm_init_flag == 0)
  385. return;
  386. if (mode > (PM_SLEEP_MODE_MAX - 1))
  387. return;
  388. level = rt_hw_interrupt_disable();
  389. pm = &_pm;
  390. pm->modes[mode] = 0;
  391. pm->module_status[module_id].req_status = 0x00;
  392. _pm_select_sleep_mode(pm);
  393. rt_hw_interrupt_enable(level);
  394. }
  395. /**
  396. * Register a device with PM feature
  397. *
  398. * @param device the device with PM feature
  399. * @param ops the PM ops for device
  400. */
  401. void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops)
  402. {
  403. rt_base_t level;
  404. struct rt_device_pm *device_pm;
  405. RT_DEBUG_NOT_IN_INTERRUPT;
  406. level = rt_hw_interrupt_disable();
  407. device_pm = (struct rt_device_pm *)RT_KERNEL_REALLOC(_pm.device_pm,
  408. (_pm.device_pm_number + 1) * sizeof(struct rt_device_pm));
  409. if (device_pm != RT_NULL)
  410. {
  411. _pm.device_pm = device_pm;
  412. _pm.device_pm[_pm.device_pm_number].device = device;
  413. _pm.device_pm[_pm.device_pm_number].ops = ops;
  414. _pm.device_pm_number += 1;
  415. }
  416. rt_hw_interrupt_enable(level);
  417. }
  418. /**
  419. * Unregister device from PM manager.
  420. *
  421. * @param device the device with PM feature
  422. */
  423. void rt_pm_device_unregister(struct rt_device *device)
  424. {
  425. rt_ubase_t level;
  426. rt_uint32_t index;
  427. RT_DEBUG_NOT_IN_INTERRUPT;
  428. level = rt_hw_interrupt_disable();
  429. for (index = 0; index < _pm.device_pm_number; index ++)
  430. {
  431. if (_pm.device_pm[index].device == device)
  432. {
  433. /* remove current entry */
  434. for (; index < _pm.device_pm_number - 1; index ++)
  435. {
  436. _pm.device_pm[index] = _pm.device_pm[index + 1];
  437. }
  438. _pm.device_pm[_pm.device_pm_number - 1].device = RT_NULL;
  439. _pm.device_pm[_pm.device_pm_number - 1].ops = RT_NULL;
  440. _pm.device_pm_number -= 1;
  441. /* break out and not touch memory */
  442. break;
  443. }
  444. }
  445. rt_hw_interrupt_enable(level);
  446. }
  447. /**
  448. * This function set notification callback for application
  449. */
  450. void rt_pm_notify_set(void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data), void *data)
  451. {
  452. _pm_notify.notify = notify;
  453. _pm_notify.data = data;
  454. }
  455. /**
  456. * This function set default sleep mode when no pm_request
  457. */
  458. void rt_pm_default_set(rt_uint8_t sleep_mode)
  459. {
  460. _pm_default_sleep = sleep_mode;
  461. }
  462. /**
  463. * RT-Thread device interface for PM device
  464. */
  465. static rt_size_t _rt_pm_device_read(rt_device_t dev,
  466. rt_off_t pos,
  467. void *buffer,
  468. rt_size_t size)
  469. {
  470. struct rt_pm *pm;
  471. rt_size_t length;
  472. length = 0;
  473. pm = (struct rt_pm *)dev;
  474. RT_ASSERT(pm != RT_NULL);
  475. if (pos < PM_SLEEP_MODE_MAX)
  476. {
  477. int mode;
  478. mode = pm->modes[pos];
  479. length = rt_snprintf(buffer, size, "%d", mode);
  480. }
  481. return length;
  482. }
  483. static rt_size_t _rt_pm_device_write(rt_device_t dev,
  484. rt_off_t pos,
  485. const void *buffer,
  486. rt_size_t size)
  487. {
  488. unsigned char request;
  489. if (size)
  490. {
  491. /* get request */
  492. request = *(unsigned char *)buffer;
  493. if (request == 0x01)
  494. {
  495. rt_pm_request(pos);
  496. }
  497. else if (request == 0x00)
  498. {
  499. rt_pm_release(pos);
  500. }
  501. }
  502. return 1;
  503. }
  504. static rt_err_t _rt_pm_device_control(rt_device_t dev,
  505. int cmd,
  506. void *args)
  507. {
  508. rt_uint32_t mode;
  509. switch (cmd)
  510. {
  511. case RT_PM_DEVICE_CTRL_REQUEST:
  512. mode = (rt_uint32_t)args;
  513. rt_pm_request(mode);
  514. break;
  515. case RT_PM_DEVICE_CTRL_RELEASE:
  516. mode = (rt_uint32_t)args;
  517. rt_pm_release(mode);
  518. break;
  519. }
  520. return RT_EOK;
  521. }
  522. int rt_pm_run_enter(rt_uint8_t mode)
  523. {
  524. rt_base_t level;
  525. struct rt_pm *pm;
  526. if (_pm_init_flag == 0)
  527. return -RT_EIO;
  528. if (mode > PM_RUN_MODE_MAX)
  529. return -RT_EINVAL;
  530. level = rt_hw_interrupt_disable();
  531. pm = &_pm;
  532. if (mode < pm->run_mode)
  533. {
  534. /* change system runing mode */
  535. pm->ops->run(pm, mode);
  536. /* changer device frequency */
  537. _pm_device_frequency_change(mode);
  538. }
  539. else
  540. {
  541. pm->flags |= RT_PM_FREQUENCY_PENDING;
  542. }
  543. pm->run_mode = mode;
  544. rt_hw_interrupt_enable(level);
  545. return RT_EOK;
  546. }
  547. #ifdef RT_USING_DEVICE_OPS
  548. const static struct rt_device_ops pm_ops =
  549. {
  550. RT_NULL,
  551. RT_NULL,
  552. RT_NULL,
  553. _rt_pm_device_read,
  554. _rt_pm_device_write,
  555. _rt_pm_device_control,
  556. };
  557. #endif
  558. /**
  559. * This function will initialize power management.
  560. *
  561. * @param ops the PM operations.
  562. * @param timer_mask indicates which mode has timer feature.
  563. * @param user_data user data
  564. */
  565. void rt_system_pm_init(const struct rt_pm_ops *ops,
  566. rt_uint8_t timer_mask,
  567. void *user_data)
  568. {
  569. struct rt_device *device;
  570. struct rt_pm *pm;
  571. pm = &_pm;
  572. device = &(_pm.parent);
  573. device->type = RT_Device_Class_PM;
  574. device->rx_indicate = RT_NULL;
  575. device->tx_complete = RT_NULL;
  576. #ifdef RT_USING_DEVICE_OPS
  577. device->ops = &pm_ops;
  578. #else
  579. device->init = RT_NULL;
  580. device->open = RT_NULL;
  581. device->close = RT_NULL;
  582. device->read = _rt_pm_device_read;
  583. device->write = _rt_pm_device_write;
  584. device->control = _rt_pm_device_control;
  585. #endif
  586. device->user_data = user_data;
  587. /* register PM device to the system */
  588. rt_device_register(device, "pm", RT_DEVICE_FLAG_RDWR);
  589. rt_memset(pm->modes, 0, sizeof(pm->modes));
  590. pm->sleep_mode = _pm_default_sleep;
  591. /* when system power on, set default sleep modes */
  592. pm->modes[pm->sleep_mode] = 1;
  593. pm->module_status[PM_POWER_ID].req_status = 1;
  594. pm->run_mode = RT_PM_DEFAULT_RUN_MODE;
  595. pm->timer_mask = timer_mask;
  596. pm->ops = ops;
  597. pm->device_pm = RT_NULL;
  598. pm->device_pm_number = 0;
  599. #if IDLE_THREAD_STACK_SIZE <= 256
  600. #error "[pm.c ERR] IDLE Stack Size Too Small!"
  601. #endif
  602. _pm_init_flag = 1;
  603. }
  604. #ifdef RT_USING_FINSH
  605. #include <finsh.h>
  606. static const char *_pm_sleep_str[] = PM_SLEEP_MODE_NAMES;
  607. static const char *_pm_run_str[] = PM_RUN_MODE_NAMES;
  608. static void rt_pm_release_mode(int argc, char **argv)
  609. {
  610. int mode = 0;
  611. if (argc >= 2)
  612. {
  613. mode = atoi(argv[1]);
  614. }
  615. rt_pm_release(mode);
  616. }
  617. MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode, pm_release, release power management mode);
  618. static void rt_pm_release_mode_all(int argc, char **argv)
  619. {
  620. int mode = 0;
  621. if (argc >= 2)
  622. {
  623. mode = atoi(argv[1]);
  624. }
  625. rt_pm_release_all(mode);
  626. }
  627. MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode_all, pm_release_all, release power management mode count);
  628. static void rt_pm_request_mode(int argc, char **argv)
  629. {
  630. int mode = 0;
  631. if (argc >= 2)
  632. {
  633. mode = atoi(argv[1]);
  634. }
  635. rt_pm_request(mode);
  636. }
  637. MSH_CMD_EXPORT_ALIAS(rt_pm_request_mode, pm_request, request power management mode);
  638. static void rt_module_release_mode(int argc, char **argv)
  639. {
  640. int module = 0;
  641. int mode = 0;
  642. if (argc >= 3)
  643. {
  644. module = atoi(argv[1]);
  645. mode = atoi(argv[2]);
  646. }
  647. rt_pm_module_release(module, mode);
  648. }
  649. MSH_CMD_EXPORT_ALIAS(rt_module_release_mode, pm_module_release, release module power mode);
  650. static void rt_module_release_mode_all(int argc, char **argv)
  651. {
  652. int module = 0;
  653. int mode = 0;
  654. if (argc >= 3)
  655. {
  656. module = atoi(argv[1]);
  657. mode = atoi(argv[2]);
  658. }
  659. rt_pm_module_release_all(module, mode);
  660. }
  661. MSH_CMD_EXPORT_ALIAS(rt_module_release_mode_all, pm_module_release_all, release power management mode count);
  662. static void rt_module_request_mode(int argc, char **argv)
  663. {
  664. int module = 0;
  665. int mode = 0;
  666. if (argc >= 3)
  667. {
  668. module = atoi(argv[1]);
  669. mode = atoi(argv[2]);
  670. }
  671. rt_pm_module_request(module, mode);
  672. }
  673. MSH_CMD_EXPORT_ALIAS(rt_module_request_mode, pm_module_request, request power management mode);
  674. static void rt_module_delay_sleep(int argc, char **argv)
  675. {
  676. int module = 0;
  677. unsigned int timeout = 0;
  678. if (argc >= 3)
  679. {
  680. module = atoi(argv[1]);
  681. timeout = atoi(argv[2]);
  682. }
  683. rt_pm_module_delay_sleep(module, timeout);
  684. }
  685. MSH_CMD_EXPORT_ALIAS(rt_module_delay_sleep, pm_module_delay, module request delay sleep);
  686. static void rt_pm_run_mode_switch(int argc, char **argv)
  687. {
  688. int mode = 0;
  689. if (argc >= 2)
  690. {
  691. mode = atoi(argv[1]);
  692. }
  693. rt_pm_run_enter(mode);
  694. }
  695. MSH_CMD_EXPORT_ALIAS(rt_pm_run_mode_switch, pm_run, switch power management run mode);
  696. rt_uint32_t rt_pm_module_get_status(void)
  697. {
  698. rt_uint8_t index = 0;
  699. struct rt_pm *pm;
  700. rt_uint32_t req_status = 0x00;
  701. pm = &_pm;
  702. for (index = 0; index < PM_MODULE_MAX_ID; index ++)
  703. {
  704. if (pm->module_status[index].req_status == 0x01)
  705. req_status |= 1<<index;
  706. }
  707. return req_status;
  708. }
  709. rt_uint8_t rt_pm_get_sleep_mode(void)
  710. {
  711. struct rt_pm *pm;
  712. pm = &_pm;
  713. return pm->sleep_mode;
  714. }
  715. static void rt_pm_dump_status(void)
  716. {
  717. rt_uint32_t index;
  718. struct rt_pm *pm;
  719. pm = &_pm;
  720. rt_kprintf("| Power Management Mode | Counter | Timer |\n");
  721. rt_kprintf("+-----------------------+---------+-------+\n");
  722. for (index = 0; index < PM_SLEEP_MODE_MAX; index ++)
  723. {
  724. int has_timer = 0;
  725. if (pm->timer_mask & (1 << index))
  726. has_timer = 1;
  727. rt_kprintf("| %021s | %7d | %5d |\n", _pm_sleep_str[index], pm->modes[index], has_timer);
  728. }
  729. rt_kprintf("+-----------------------+---------+-------+\n");
  730. rt_kprintf("pm current sleep mode: %s\n", _pm_sleep_str[pm->sleep_mode]);
  731. rt_kprintf("pm current run mode: %s\n", _pm_run_str[pm->run_mode]);
  732. rt_kprintf("\n");
  733. rt_kprintf("| module | busy | start time | timeout |\n");
  734. rt_kprintf("+--------+------+------------+-----------+\n");
  735. for (index = 0; index < PM_MODULE_MAX_ID; index ++)
  736. {
  737. if ((pm->module_status[index].busy_flag == RT_TRUE) ||
  738. (pm->module_status[index].req_status != 0x00))
  739. {
  740. rt_kprintf("| %04d | %d | 0x%08x | 0x%08x |\n",
  741. index, pm->module_status[index].busy_flag,
  742. pm->module_status[index].start_time,
  743. pm->module_status[index].timeout);
  744. }
  745. }
  746. rt_kprintf("+--------+------+------------+-----------+\n");
  747. }
  748. FINSH_FUNCTION_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
  749. MSH_CMD_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
  750. #endif
  751. #endif /* RT_USING_PM */