pm.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. /*
  2. * Copyright (c) 2006-2024 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. * 2024-07-04 wdfk-prog The device is registered and uninstalled by linked list
  14. */
  15. #include <rthw.h>
  16. #include <rtthread.h>
  17. #include <drivers/pm.h>
  18. #include <stdlib.h>
  19. #ifdef RT_USING_PM
  20. /* tickless threshold time */
  21. #ifndef PM_TICKLESS_THRESHOLD_TIME
  22. #define PM_TICKLESS_THRESHOLD_TIME 2
  23. #endif
  24. /* tickless threshold : sleep mode */
  25. #ifndef PM_TICKLESS_THRESHOLD_MODE
  26. #define PM_TICKLESS_THRESHOLD_MODE PM_SLEEP_MODE_IDLE
  27. #endif
  28. /* busy : sleep mode */
  29. #ifndef PM_BUSY_SLEEP_MODE
  30. #define PM_BUSY_SLEEP_MODE PM_SLEEP_MODE_IDLE
  31. #endif
  32. /* suspend : suspend sleep mode */
  33. #ifndef PM_SUSPEND_SLEEP_MODE
  34. #define PM_SUSPEND_SLEEP_MODE PM_SLEEP_MODE_IDLE
  35. #endif
  36. #ifdef PM_ENABLE_THRESHOLD_SLEEP_MODE
  37. #ifndef PM_LIGHT_THRESHOLD_TIME
  38. #define PM_LIGHT_THRESHOLD_TIME 5
  39. #endif
  40. #ifndef PM_DEEP_THRESHOLD_TIME
  41. #define PM_DEEP_THRESHOLD_TIME 20
  42. #endif
  43. #ifndef PM_STANDBY_THRESHOLD_TIME
  44. #define PM_STANDBY_THRESHOLD_TIME 100
  45. #endif
  46. #endif
  47. static struct rt_pm _pm;
  48. /* default mode : system power on */
  49. static rt_uint8_t _pm_default_sleep = RT_PM_DEFAULT_SLEEP_MODE;
  50. /* default deepsleep mode : tick-less mode */
  51. static rt_uint8_t _pm_default_deepsleep = RT_PM_DEFAULT_DEEPSLEEP_MODE;
  52. static struct rt_pm_notify _pm_notify;
  53. static rt_uint8_t _pm_init_flag = 0;
  54. rt_weak rt_uint32_t rt_pm_enter_critical(rt_uint8_t sleep_mode)
  55. {
  56. return rt_hw_interrupt_disable();
  57. }
  58. rt_weak void rt_pm_exit_critical(rt_uint32_t ctx, rt_uint8_t sleep_mode)
  59. {
  60. rt_hw_interrupt_enable(ctx);
  61. }
  62. /* lptimer start */
  63. static void pm_lptimer_start(struct rt_pm *pm, uint32_t timeout)
  64. {
  65. if (_pm.ops->timer_start != RT_NULL)
  66. _pm.ops->timer_start(pm, timeout);
  67. }
  68. /* lptimer stop */
  69. static void pm_lptimer_stop(struct rt_pm *pm)
  70. {
  71. if (_pm.ops->timer_stop != RT_NULL)
  72. _pm.ops->timer_stop(pm);
  73. }
  74. /* lptimer get timeout tick */
  75. static rt_tick_t pm_lptimer_get_timeout(struct rt_pm *pm)
  76. {
  77. if (_pm.ops->timer_get_tick != RT_NULL)
  78. return _pm.ops->timer_get_tick(pm);
  79. return RT_TICK_MAX;
  80. }
  81. /* enter sleep mode */
  82. static void pm_sleep(struct rt_pm *pm, uint8_t sleep_mode)
  83. {
  84. if (_pm.ops->sleep != RT_NULL)
  85. _pm.ops->sleep(pm, sleep_mode);
  86. }
  87. /**
  88. * This function will suspend all registered devices
  89. */
  90. static rt_err_t _pm_device_suspend(rt_uint8_t mode)
  91. {
  92. rt_err_t ret = RT_EOK;
  93. struct rt_device_pm *device_pm = RT_NULL;
  94. rt_slist_t *node = RT_NULL;
  95. for (node = rt_slist_first(&_pm.device_list); node; node = rt_slist_next(node))
  96. {
  97. device_pm = rt_slist_entry(node, struct rt_device_pm, list);
  98. if (device_pm->ops != RT_NULL && device_pm->ops->suspend != RT_NULL)
  99. {
  100. ret = device_pm->ops->suspend(device_pm->device, mode);
  101. if(ret != RT_EOK)
  102. {
  103. break;
  104. }
  105. }
  106. }
  107. return ret;
  108. }
  109. /**
  110. * This function will resume all registered devices
  111. */
  112. static void _pm_device_resume(rt_uint8_t mode)
  113. {
  114. struct rt_device_pm *device_pm = RT_NULL;
  115. rt_slist_t *node = RT_NULL;
  116. for (node = rt_slist_first(&_pm.device_list); node; node = rt_slist_next(node))
  117. {
  118. device_pm = rt_slist_entry(node, struct rt_device_pm, list);
  119. if (device_pm->ops != RT_NULL && device_pm->ops->resume != RT_NULL)
  120. {
  121. device_pm->ops->resume(device_pm->device, mode);
  122. }
  123. }
  124. }
  125. /**
  126. * This function will update the frequency of all registered devices
  127. */
  128. static void _pm_device_frequency_change(rt_uint8_t mode)
  129. {
  130. struct rt_device_pm *device_pm = RT_NULL;
  131. rt_slist_t *node = RT_NULL;
  132. for (node = rt_slist_first(&_pm.device_list); node; node = rt_slist_next(node))
  133. {
  134. device_pm = rt_slist_entry(node, struct rt_device_pm, list);
  135. if (device_pm->ops->frequency_change != RT_NULL)
  136. {
  137. device_pm->ops->frequency_change(device_pm->device, mode);
  138. }
  139. }
  140. }
  141. /**
  142. * This function will update the system clock frequency when idle
  143. */
  144. static void _pm_frequency_scaling(struct rt_pm *pm)
  145. {
  146. rt_base_t level = 0;
  147. if (pm->flags & RT_PM_FREQUENCY_PENDING)
  148. {
  149. level = rt_hw_interrupt_disable();
  150. /* change system runing mode */
  151. if(pm->ops->run != RT_NULL)
  152. {
  153. pm->ops->run(pm, pm->run_mode);
  154. }
  155. /* changer device frequency */
  156. _pm_device_frequency_change(pm->run_mode);
  157. pm->flags &= ~RT_PM_FREQUENCY_PENDING;
  158. rt_hw_interrupt_enable(level);
  159. }
  160. }
  161. /**
  162. * judge sleep mode from sleep request
  163. *
  164. * @param none
  165. *
  166. * @return sleep mode
  167. */
  168. static rt_uint8_t _judge_sleep_mode(void)
  169. {
  170. rt_uint16_t index;
  171. rt_uint16_t len;
  172. for (index = 0; index < PM_SLEEP_MODE_MAX -1; index++)
  173. {
  174. for (len = 0; len < ((PM_MODULE_MAX_ID + 31) / 32); len++)
  175. {
  176. if (_pm.sleep_status[index][len] != 0x00)
  177. return index;
  178. }
  179. }
  180. return PM_SLEEP_MODE_MAX; /* default sleep mode */
  181. }
  182. /**
  183. * This function selects the sleep mode according to the rt_pm_request/rt_pm_release count.
  184. */
  185. static rt_uint8_t _pm_select_sleep_mode(struct rt_pm *pm)
  186. {
  187. int index;
  188. rt_uint8_t mode;
  189. mode = _pm_default_deepsleep;
  190. rt_uint8_t request_mode = _judge_sleep_mode();
  191. for (index = PM_SLEEP_MODE_NONE; index < PM_SLEEP_MODE_MAX; index ++)
  192. {
  193. if (pm->modes[index])
  194. {
  195. mode = index;
  196. break;
  197. }
  198. }
  199. /* select the high power mode */
  200. if (request_mode < mode)
  201. mode = request_mode;
  202. return mode;
  203. }
  204. /**
  205. * pm module request delay sleep.
  206. */
  207. void rt_pm_module_delay_sleep(rt_uint8_t module_id, rt_tick_t timeout)
  208. {
  209. rt_base_t level;
  210. struct rt_pm *pm;
  211. if (_pm_init_flag == 0)
  212. return;
  213. if (module_id > (PM_MODULE_MAX_ID - 1))
  214. return;
  215. level = rt_hw_interrupt_disable();
  216. pm = &_pm;
  217. pm->module_status[module_id].busy_flag = RT_TRUE;
  218. pm->module_status[module_id].timeout = timeout;
  219. pm->module_status[module_id].start_time = rt_tick_get();
  220. rt_hw_interrupt_enable(level);
  221. }
  222. /**
  223. * This function check if all modules in idle status.
  224. */
  225. static rt_bool_t _pm_device_check_idle(void)
  226. {
  227. struct rt_pm *pm;
  228. if (_pm_init_flag == 0)
  229. return RT_TRUE;
  230. pm = &_pm;
  231. for (int i = 0; i < PM_MODULE_MAX_ID; i++)
  232. {
  233. if (pm->module_status[i].busy_flag == RT_TRUE)
  234. {
  235. if (rt_tick_get() - pm->module_status[i].start_time > pm->module_status[i].timeout)
  236. {
  237. pm->module_status[i].busy_flag = RT_FALSE;
  238. pm->module_status[i].timeout = 0x00;
  239. }
  240. }
  241. if (pm->module_status[i].busy_flag == RT_TRUE)
  242. {
  243. return RT_FALSE;
  244. }
  245. }
  246. return RT_TRUE;
  247. }
  248. /**
  249. * @brief Get the next system wake-up time
  250. * @note When used by default, it goes into STANDBY mode and sleeps forever. tickless external rewriting is required
  251. * @param mode: sleep mode
  252. * @retval timeout_tick
  253. */
  254. rt_weak rt_tick_t pm_timer_next_timeout_tick(rt_uint8_t mode)
  255. {
  256. switch (mode)
  257. {
  258. case PM_SLEEP_MODE_LIGHT:
  259. return rt_timer_next_timeout_tick();
  260. case PM_SLEEP_MODE_DEEP:
  261. case PM_SLEEP_MODE_STANDBY:
  262. return rt_lptimer_next_timeout_tick();
  263. }
  264. return RT_TICK_MAX;
  265. }
  266. /**
  267. * This function will judge sleep mode from threshold timeout.
  268. *
  269. * @param cur_mode the current pm sleep mode
  270. * @param timeout_tick the threshold timeout
  271. *
  272. * @return none
  273. */
  274. rt_weak rt_uint8_t pm_get_sleep_threshold_mode(rt_uint8_t cur_mode, rt_tick_t timeout_tick)
  275. {
  276. rt_uint8_t sleep_mode = cur_mode;
  277. if (_pm_init_flag == 0)
  278. return sleep_mode;
  279. if (cur_mode >= PM_SLEEP_MODE_MAX)
  280. return sleep_mode;
  281. #ifdef PM_ENABLE_THRESHOLD_SLEEP_MODE
  282. switch (cur_mode)
  283. {
  284. case PM_SLEEP_MODE_NONE:
  285. case PM_SLEEP_MODE_IDLE:
  286. break;
  287. case PM_SLEEP_MODE_LIGHT:
  288. if (timeout_tick < PM_LIGHT_THRESHOLD_TIME)
  289. sleep_mode = PM_SLEEP_MODE_IDLE;
  290. break;
  291. case PM_SLEEP_MODE_DEEP:
  292. if (timeout_tick < PM_LIGHT_THRESHOLD_TIME)
  293. sleep_mode = PM_SLEEP_MODE_IDLE;
  294. else if (timeout_tick < PM_DEEP_THRESHOLD_TIME)
  295. sleep_mode = PM_SLEEP_MODE_LIGHT;
  296. break;
  297. case PM_SLEEP_MODE_STANDBY:
  298. if (timeout_tick < PM_LIGHT_THRESHOLD_TIME)
  299. sleep_mode = PM_SLEEP_MODE_IDLE;
  300. else if (timeout_tick < PM_DEEP_THRESHOLD_TIME)
  301. sleep_mode = PM_SLEEP_MODE_LIGHT;
  302. else if (timeout_tick < PM_STANDBY_THRESHOLD_TIME)
  303. sleep_mode = PM_SLEEP_MODE_DEEP;
  304. }
  305. cur_mode = sleep_mode;
  306. #else
  307. if (timeout_tick < PM_TICKLESS_THRESHOLD_TIME)
  308. {
  309. cur_mode = PM_SLEEP_MODE_IDLE;
  310. }
  311. #endif
  312. return cur_mode;
  313. }
  314. /**
  315. * This function changes the power sleep mode base on the result of selection
  316. */
  317. static void _pm_change_sleep_mode(struct rt_pm *pm)
  318. {
  319. rt_tick_t timeout_tick = 0, delta_tick = 0;
  320. rt_base_t level = 0;
  321. uint8_t sleep_mode = PM_SLEEP_MODE_DEEP;
  322. level = rt_pm_enter_critical(pm->sleep_mode);
  323. /* judge sleep mode from module request */
  324. pm->sleep_mode = _pm_select_sleep_mode(pm);
  325. /* module busy request check */
  326. if (_pm_device_check_idle() == RT_FALSE)
  327. {
  328. sleep_mode = PM_BUSY_SLEEP_MODE;
  329. if (sleep_mode < pm->sleep_mode)
  330. {
  331. pm->sleep_mode = sleep_mode; /* judge the highest sleep mode */
  332. }
  333. }
  334. if (_pm.sleep_mode == PM_SLEEP_MODE_NONE)
  335. {
  336. pm_sleep(pm, PM_SLEEP_MODE_NONE);
  337. rt_pm_exit_critical(level, pm->sleep_mode);
  338. }
  339. else
  340. {
  341. /* Notify app will enter sleep mode */
  342. if (_pm_notify.notify)
  343. {
  344. _pm_notify.notify(RT_PM_ENTER_SLEEP, pm->sleep_mode, _pm_notify.data);
  345. }
  346. /* Suspend all peripheral device */
  347. #ifdef PM_ENABLE_SUSPEND_SLEEP_MODE
  348. rt_err_t ret = _pm_device_suspend(pm->sleep_mode);
  349. if (ret != RT_EOK)
  350. {
  351. _pm_device_resume(pm->sleep_mode);
  352. if (_pm_notify.notify)
  353. {
  354. _pm_notify.notify(RT_PM_EXIT_SLEEP, pm->sleep_mode, _pm_notify.data);
  355. }
  356. if (pm->sleep_mode > PM_SUSPEND_SLEEP_MODE)
  357. {
  358. pm->sleep_mode = PM_SUSPEND_SLEEP_MODE;
  359. }
  360. pm->ops->sleep(pm, pm->sleep_mode); /* suspend failed */
  361. rt_pm_exit_critical(level, pm->sleep_mode);
  362. return;
  363. }
  364. #else
  365. _pm_device_suspend(pm->sleep_mode);
  366. #endif
  367. /* Tickless*/
  368. if (pm->timer_mask & (0x01 << pm->sleep_mode))
  369. {
  370. timeout_tick = pm_timer_next_timeout_tick(pm->sleep_mode);
  371. timeout_tick = timeout_tick - rt_tick_get();
  372. /* Judge sleep_mode from threshold time */
  373. pm->sleep_mode = pm_get_sleep_threshold_mode(pm->sleep_mode, timeout_tick);
  374. if (pm->timer_mask & (0x01 << pm->sleep_mode))
  375. {
  376. pm_lptimer_start(pm, timeout_tick);
  377. }
  378. }
  379. /* enter lower power state */
  380. pm_sleep(pm, pm->sleep_mode);
  381. /* wake up from lower power state*/
  382. if (pm->timer_mask & (0x01 << pm->sleep_mode))
  383. {
  384. delta_tick = pm_lptimer_get_timeout(pm);
  385. pm_lptimer_stop(pm);
  386. if (delta_tick)
  387. {
  388. rt_interrupt_enter();
  389. rt_tick_increase_tick(delta_tick);
  390. rt_interrupt_leave();
  391. }
  392. }
  393. /* resume all device */
  394. _pm_device_resume(pm->sleep_mode);
  395. if (_pm_notify.notify)
  396. _pm_notify.notify(RT_PM_EXIT_SLEEP, pm->sleep_mode, _pm_notify.data);
  397. rt_pm_exit_critical(level, pm->sleep_mode);
  398. }
  399. }
  400. /**
  401. * This function will enter corresponding power mode.
  402. */
  403. void rt_system_power_manager(void)
  404. {
  405. if (_pm_init_flag == 0 || _pm.ops == RT_NULL)
  406. {
  407. return;
  408. }
  409. /* CPU frequency scaling according to the runing mode settings */
  410. _pm_frequency_scaling(&_pm);
  411. /* Low Power Mode Processing */
  412. _pm_change_sleep_mode(&_pm);
  413. }
  414. /**
  415. * Upper application or device driver requests the system
  416. * stall in corresponding power mode.
  417. *
  418. * @param parameter the parameter of run mode or sleep mode
  419. */
  420. rt_err_t rt_pm_request(rt_uint8_t mode)
  421. {
  422. rt_base_t level;
  423. struct rt_pm *pm;
  424. if (_pm_init_flag == 0)
  425. {
  426. return -RT_EPERM;
  427. }
  428. if (mode > (PM_SLEEP_MODE_MAX - 1))
  429. {
  430. return -RT_EINVAL;
  431. }
  432. level = rt_hw_interrupt_disable();
  433. pm = &_pm;
  434. if (pm->modes[mode] < 255)
  435. pm->modes[mode] ++;
  436. rt_hw_interrupt_enable(level);
  437. return RT_EOK;
  438. }
  439. /**
  440. * Upper application or device driver releases the stall
  441. * of corresponding power mode.
  442. *
  443. * @param parameter the parameter of run mode or sleep mode
  444. *
  445. */
  446. rt_err_t rt_pm_release(rt_uint8_t mode)
  447. {
  448. rt_base_t level;
  449. struct rt_pm *pm;
  450. if (_pm_init_flag == 0)
  451. {
  452. return -RT_EPERM;
  453. }
  454. if (mode > (PM_SLEEP_MODE_MAX - 1))
  455. {
  456. return -RT_EINVAL;
  457. }
  458. level = rt_hw_interrupt_disable();
  459. pm = &_pm;
  460. if (pm->modes[mode] > 0)
  461. pm->modes[mode] --;
  462. rt_hw_interrupt_enable(level);
  463. return RT_EOK;
  464. }
  465. /**
  466. * Upper application or device driver releases all the stall
  467. * of corresponding power mode.
  468. *
  469. * @param parameter the parameter of run mode or sleep mode
  470. *
  471. */
  472. rt_err_t rt_pm_release_all(rt_uint8_t mode)
  473. {
  474. rt_base_t level;
  475. struct rt_pm *pm;
  476. if (_pm_init_flag == 0)
  477. {
  478. return -RT_EPERM;
  479. }
  480. if (mode > (PM_SLEEP_MODE_MAX - 1))
  481. {
  482. return -RT_EINVAL;
  483. }
  484. level = rt_hw_interrupt_disable();
  485. pm = &_pm;
  486. pm->modes[mode] = 0;
  487. rt_hw_interrupt_enable(level);
  488. return RT_EOK;
  489. }
  490. /**
  491. * Upper application or device driver requests the system
  492. * stall in corresponding power mode.
  493. *
  494. * @param module_id the application or device module id
  495. * @param mode the system power sleep mode
  496. */
  497. rt_err_t rt_pm_module_request(uint8_t module_id, rt_uint8_t mode)
  498. {
  499. rt_base_t level;
  500. struct rt_pm *pm;
  501. if (_pm_init_flag == 0)
  502. {
  503. return -RT_EPERM;
  504. }
  505. if (mode > (PM_SLEEP_MODE_MAX - 1))
  506. {
  507. return -RT_EINVAL;
  508. }
  509. if (module_id > (PM_MODULE_MAX_ID - 1))
  510. {
  511. return -RT_EINVAL;
  512. }
  513. level = rt_hw_interrupt_disable();
  514. pm = &_pm;
  515. pm->module_status[module_id].req_status = 0x01;
  516. if (pm->modes[mode] < 255)
  517. pm->modes[mode] ++;
  518. rt_hw_interrupt_enable(level);
  519. return RT_EOK;
  520. }
  521. /**
  522. * Upper application or device driver releases the stall
  523. * of corresponding power mode.
  524. *
  525. * @param module_id the application or device module id
  526. * @param mode the system power sleep mode
  527. *
  528. */
  529. rt_err_t rt_pm_module_release(uint8_t module_id, rt_uint8_t mode)
  530. {
  531. rt_base_t level;
  532. struct rt_pm *pm;
  533. if (_pm_init_flag == 0)
  534. {
  535. return -RT_EPERM;
  536. }
  537. if (mode > (PM_SLEEP_MODE_MAX - 1))
  538. {
  539. return -RT_EINVAL;
  540. }
  541. if (module_id > (PM_MODULE_MAX_ID - 1))
  542. {
  543. return -RT_EINVAL;
  544. }
  545. level = rt_hw_interrupt_disable();
  546. pm = &_pm;
  547. if (pm->modes[mode] > 0)
  548. pm->modes[mode] --;
  549. if (pm->modes[mode] == 0)
  550. pm->module_status[module_id].req_status = 0x00;
  551. rt_hw_interrupt_enable(level);
  552. return RT_EOK;
  553. }
  554. /**
  555. * Upper application or device driver releases all the stall
  556. * of corresponding power mode.
  557. *
  558. * @param module_id the application or device module id
  559. * @param mode the system power sleep mode
  560. *
  561. */
  562. rt_err_t rt_pm_module_release_all(uint8_t module_id, rt_uint8_t mode)
  563. {
  564. rt_base_t level;
  565. struct rt_pm *pm;
  566. if (_pm_init_flag == 0)
  567. {
  568. return -RT_EPERM;
  569. }
  570. if (mode > (PM_SLEEP_MODE_MAX - 1))
  571. {
  572. return -RT_EINVAL;
  573. }
  574. level = rt_hw_interrupt_disable();
  575. pm = &_pm;
  576. pm->modes[mode] = 0;
  577. pm->module_status[module_id].req_status = 0x00;
  578. rt_hw_interrupt_enable(level);
  579. return RT_EOK;
  580. }
  581. /**
  582. * This function will let current module work with specified sleep mode.
  583. *
  584. * @param module_id the pm module id
  585. * @param mode the pm sleep mode
  586. *
  587. * @return none
  588. */
  589. rt_err_t rt_pm_sleep_request(rt_uint16_t module_id, rt_uint8_t mode)
  590. {
  591. rt_base_t level;
  592. if (module_id >= PM_MODULE_MAX_ID)
  593. {
  594. return -RT_EINVAL;
  595. }
  596. if (mode >= (PM_SLEEP_MODE_MAX - 1))
  597. {
  598. return -RT_EINVAL;
  599. }
  600. level = rt_hw_interrupt_disable();
  601. _pm.sleep_status[mode][module_id / 32] |= 1 << (module_id % 32);
  602. rt_hw_interrupt_enable(level);
  603. return RT_EOK;
  604. }
  605. /**
  606. * This function will let current module work with PM_SLEEP_MODE_NONE mode.
  607. *
  608. * @param module_id the pm module id
  609. *
  610. * @return NULL
  611. */
  612. rt_err_t rt_pm_sleep_none_request(rt_uint16_t module_id)
  613. {
  614. return rt_pm_sleep_request(module_id, PM_SLEEP_MODE_NONE);
  615. }
  616. /**
  617. * This function will let current module work with PM_SLEEP_MODE_IDLE mode.
  618. *
  619. * @param module_id the pm module id
  620. *
  621. * @return NULL
  622. */
  623. rt_err_t rt_pm_sleep_idle_request(rt_uint16_t module_id)
  624. {
  625. return rt_pm_sleep_request(module_id, PM_SLEEP_MODE_IDLE);
  626. }
  627. /**
  628. * This function will let current module work with PM_SLEEP_MODE_LIGHT mode.
  629. *
  630. * @param module_id the pm module id
  631. *
  632. * @return NULL
  633. */
  634. rt_err_t rt_pm_sleep_light_request(rt_uint16_t module_id)
  635. {
  636. return rt_pm_sleep_request(module_id, PM_SLEEP_MODE_LIGHT);
  637. }
  638. /**
  639. * When current module don't work, release requested sleep mode.
  640. *
  641. * @param module_id the pm module id
  642. * @param mode the pm sleep mode
  643. *
  644. * @return NULL
  645. */
  646. rt_err_t rt_pm_sleep_release(rt_uint16_t module_id, rt_uint8_t mode)
  647. {
  648. rt_base_t level;
  649. if (module_id >= PM_MODULE_MAX_ID)
  650. {
  651. return -RT_EINVAL;
  652. }
  653. if (mode >= (PM_SLEEP_MODE_MAX - 1))
  654. {
  655. return -RT_EINVAL;
  656. }
  657. level = rt_hw_interrupt_disable();
  658. _pm.sleep_status[mode][module_id / 32] &= ~(1 << (module_id % 32));
  659. rt_hw_interrupt_enable(level);
  660. return RT_EOK;
  661. }
  662. /**
  663. * The specified module release the requested PM_SLEEP_MODE_NONE mode
  664. *
  665. * @param module_id the pm module id
  666. *
  667. * @return none
  668. */
  669. rt_err_t rt_pm_sleep_none_release(rt_uint16_t module_id)
  670. {
  671. return rt_pm_sleep_release(module_id, PM_SLEEP_MODE_NONE);
  672. }
  673. /**
  674. * The specified module release the requested PM_SLEEP_MODE_IDLE mode
  675. *
  676. * @param module_id the pm module id
  677. *
  678. * @return none
  679. */
  680. rt_err_t rt_pm_sleep_idle_release(rt_uint16_t module_id)
  681. {
  682. return rt_pm_sleep_release(module_id, PM_SLEEP_MODE_IDLE);
  683. }
  684. /**
  685. * The specified module release the requested PM_SLEEP_MODE_LIGHT mode
  686. *
  687. * @param module_id the pm module id
  688. *
  689. * @return none
  690. */
  691. rt_err_t rt_pm_sleep_light_release(rt_uint16_t module_id)
  692. {
  693. return rt_pm_sleep_release(module_id, PM_SLEEP_MODE_LIGHT);
  694. }
  695. /**
  696. * Register a device with PM feature
  697. *
  698. * @param device the device with PM feature
  699. * @param ops the PM ops for device
  700. */
  701. void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops)
  702. {
  703. struct rt_device_pm *device_pm;
  704. device_pm = RT_KERNEL_MALLOC(sizeof(struct rt_device_pm));
  705. if (device_pm != RT_NULL)
  706. {
  707. rt_slist_append(&_pm.device_list, &device_pm->list);
  708. device_pm->device = device;
  709. device_pm->ops = ops;
  710. }
  711. }
  712. /**
  713. * Unregister device from PM manager.
  714. *
  715. * @param device the device with PM feature
  716. */
  717. void rt_pm_device_unregister(struct rt_device *device)
  718. {
  719. struct rt_device_pm *device_pm = RT_NULL;
  720. rt_slist_t *node = RT_NULL;
  721. for (node = rt_slist_first(&_pm.device_list); node; node = rt_slist_next(node))
  722. {
  723. device_pm = rt_slist_entry(node, struct rt_device_pm, list);
  724. if (device_pm->device == device)
  725. {
  726. rt_slist_remove(&_pm.device_list, &device_pm->list);
  727. RT_KERNEL_FREE(device_pm);
  728. break;
  729. }
  730. }
  731. }
  732. /**
  733. * This function set notification callback for application
  734. */
  735. void rt_pm_notify_set(void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data), void *data)
  736. {
  737. _pm_notify.notify = notify;
  738. _pm_notify.data = data;
  739. }
  740. /**
  741. * This function set default sleep mode when no pm_request
  742. */
  743. void rt_pm_default_set(rt_uint8_t sleep_mode)
  744. {
  745. _pm_default_sleep = sleep_mode;
  746. }
  747. /**
  748. * RT-Thread device interface for PM device
  749. */
  750. static rt_ssize_t _rt_pm_device_read(rt_device_t dev,
  751. rt_off_t pos,
  752. void *buffer,
  753. rt_size_t size)
  754. {
  755. struct rt_pm *pm;
  756. rt_size_t length;
  757. length = 0;
  758. pm = (struct rt_pm *)dev;
  759. RT_ASSERT(pm != RT_NULL);
  760. if (pos < PM_SLEEP_MODE_MAX)
  761. {
  762. int mode;
  763. mode = pm->modes[pos];
  764. length = rt_snprintf(buffer, size, "%d", mode);
  765. }
  766. return length;
  767. }
  768. static rt_ssize_t _rt_pm_device_write(rt_device_t dev,
  769. rt_off_t pos,
  770. const void *buffer,
  771. rt_size_t size)
  772. {
  773. unsigned char request;
  774. if (size)
  775. {
  776. /* get request */
  777. request = *(unsigned char *)buffer;
  778. if (request == 0x01)
  779. {
  780. rt_pm_request(pos);
  781. }
  782. else if (request == 0x00)
  783. {
  784. rt_pm_release(pos);
  785. }
  786. }
  787. return 1;
  788. }
  789. static rt_err_t _rt_pm_device_control(rt_device_t dev,
  790. int cmd,
  791. void *args)
  792. {
  793. rt_uint32_t mode;
  794. switch (cmd)
  795. {
  796. case RT_PM_DEVICE_CTRL_REQUEST:
  797. mode = (rt_uint32_t)(rt_ubase_t)args;
  798. rt_pm_request(mode);
  799. break;
  800. case RT_PM_DEVICE_CTRL_RELEASE:
  801. mode = (rt_uint32_t)(rt_ubase_t)args;
  802. rt_pm_release(mode);
  803. break;
  804. }
  805. return RT_EOK;
  806. }
  807. rt_err_t rt_pm_run_enter(rt_uint8_t mode)
  808. {
  809. rt_base_t level = 0;
  810. struct rt_pm *pm = RT_NULL;
  811. rt_err_t ret = RT_EOK;
  812. if (_pm_init_flag == 0)
  813. return -RT_EIO;
  814. if (mode > PM_RUN_MODE_MAX)
  815. return -RT_EINVAL;
  816. pm = &_pm;
  817. level = rt_hw_interrupt_disable();
  818. if (mode < pm->run_mode)
  819. {
  820. /* change system runing mode */
  821. if(pm->ops != RT_NULL && pm->ops->run != RT_NULL)
  822. {
  823. pm->ops->run(pm, mode);
  824. }
  825. /* changer device frequency */
  826. _pm_device_frequency_change(mode);
  827. }
  828. else
  829. {
  830. pm->flags |= RT_PM_FREQUENCY_PENDING;
  831. }
  832. pm->run_mode = mode;
  833. rt_hw_interrupt_enable(level);
  834. return ret;
  835. }
  836. #ifdef RT_USING_DEVICE_OPS
  837. const static struct rt_device_ops pm_ops =
  838. {
  839. RT_NULL,
  840. RT_NULL,
  841. RT_NULL,
  842. _rt_pm_device_read,
  843. _rt_pm_device_write,
  844. _rt_pm_device_control,
  845. };
  846. #endif
  847. /**
  848. * This function will initialize power management.
  849. *
  850. * @param ops the PM operations.
  851. * @param timer_mask indicates which mode has timer feature.
  852. * @param user_data user data
  853. */
  854. void rt_system_pm_init(const struct rt_pm_ops *ops,
  855. rt_uint8_t timer_mask,
  856. void *user_data)
  857. {
  858. struct rt_device *device;
  859. struct rt_pm *pm;
  860. pm = &_pm;
  861. device = &(_pm.parent);
  862. device->type = RT_Device_Class_PM;
  863. device->rx_indicate = RT_NULL;
  864. device->tx_complete = RT_NULL;
  865. #ifdef RT_USING_DEVICE_OPS
  866. device->ops = &pm_ops;
  867. #else
  868. device->init = RT_NULL;
  869. device->open = RT_NULL;
  870. device->close = RT_NULL;
  871. device->read = _rt_pm_device_read;
  872. device->write = _rt_pm_device_write;
  873. device->control = _rt_pm_device_control;
  874. #endif
  875. device->user_data = user_data;
  876. /* register PM device to the system */
  877. rt_device_register(device, "pm", RT_DEVICE_FLAG_RDWR);
  878. rt_memset(pm->modes, 0, sizeof(pm->modes));
  879. pm->sleep_mode = _pm_default_sleep;
  880. /* when system power on, set default sleep modes */
  881. pm->modes[pm->sleep_mode] = 1;
  882. pm->module_status[PM_POWER_ID].req_status = 1;
  883. pm->run_mode = RT_PM_DEFAULT_RUN_MODE;
  884. pm->timer_mask = timer_mask;
  885. pm->ops = ops;
  886. pm->device_pm = RT_NULL;
  887. rt_slist_init(&pm->device_list);
  888. #if IDLE_THREAD_STACK_SIZE <= 256
  889. #error "[pm.c ERR] IDLE Stack Size Too Small!"
  890. #endif
  891. _pm_init_flag = 1;
  892. }
  893. #ifdef RT_USING_FINSH
  894. #include <finsh.h>
  895. static const char *_pm_sleep_str[] = PM_SLEEP_MODE_NAMES;
  896. static const char *_pm_run_str[] = PM_RUN_MODE_NAMES;
  897. static void rt_pm_release_mode(int argc, char **argv)
  898. {
  899. int mode = 0;
  900. if (argc >= 2)
  901. {
  902. mode = atoi(argv[1]);
  903. }
  904. rt_pm_release(mode);
  905. }
  906. MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode, pm_release, release power management mode);
  907. static void rt_pm_release_mode_all(int argc, char **argv)
  908. {
  909. int mode = 0;
  910. if (argc >= 2)
  911. {
  912. mode = atoi(argv[1]);
  913. }
  914. rt_pm_release_all(mode);
  915. }
  916. MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode_all, pm_release_all, release power management mode count);
  917. static void rt_pm_request_mode(int argc, char **argv)
  918. {
  919. int mode = 0;
  920. if (argc >= 2)
  921. {
  922. mode = atoi(argv[1]);
  923. }
  924. rt_pm_request(mode);
  925. }
  926. MSH_CMD_EXPORT_ALIAS(rt_pm_request_mode, pm_request, request power management mode);
  927. static void rt_module_release_mode(int argc, char **argv)
  928. {
  929. int module = 0;
  930. int mode = 0;
  931. if (argc >= 3)
  932. {
  933. module = atoi(argv[1]);
  934. mode = atoi(argv[2]);
  935. }
  936. rt_pm_module_release(module, mode);
  937. }
  938. MSH_CMD_EXPORT_ALIAS(rt_module_release_mode, pm_module_release, release module power mode);
  939. static void rt_module_release_mode_all(int argc, char **argv)
  940. {
  941. int module = 0;
  942. int mode = 0;
  943. if (argc >= 3)
  944. {
  945. module = atoi(argv[1]);
  946. mode = atoi(argv[2]);
  947. }
  948. rt_pm_module_release_all(module, mode);
  949. }
  950. MSH_CMD_EXPORT_ALIAS(rt_module_release_mode_all, pm_module_release_all, release power management mode count);
  951. static void rt_module_request_mode(int argc, char **argv)
  952. {
  953. int module = 0;
  954. int mode = 0;
  955. if (argc >= 3)
  956. {
  957. module = atoi(argv[1]);
  958. mode = atoi(argv[2]);
  959. }
  960. rt_pm_module_request(module, mode);
  961. }
  962. MSH_CMD_EXPORT_ALIAS(rt_module_request_mode, pm_module_request, request power management mode);
  963. static void rt_module_delay_sleep(int argc, char **argv)
  964. {
  965. int module = 0;
  966. unsigned int timeout = 0;
  967. if (argc >= 3)
  968. {
  969. module = atoi(argv[1]);
  970. timeout = atoi(argv[2]);
  971. }
  972. rt_pm_module_delay_sleep(module, timeout);
  973. }
  974. MSH_CMD_EXPORT_ALIAS(rt_module_delay_sleep, pm_module_delay, module request delay sleep);
  975. static void rt_pm_run_mode_switch(int argc, char **argv)
  976. {
  977. int mode = 0;
  978. if (argc >= 2)
  979. {
  980. mode = atoi(argv[1]);
  981. }
  982. rt_pm_run_enter(mode);
  983. }
  984. MSH_CMD_EXPORT_ALIAS(rt_pm_run_mode_switch, pm_run, switch power management run mode);
  985. rt_uint32_t rt_pm_module_get_status(void)
  986. {
  987. rt_uint8_t index = 0;
  988. struct rt_pm *pm;
  989. rt_uint32_t req_status = 0x00;
  990. pm = &_pm;
  991. for (index = 0; index < PM_MODULE_MAX_ID; index ++)
  992. {
  993. if (pm->module_status[index].req_status == 0x01)
  994. req_status |= 1<<index;
  995. }
  996. return req_status;
  997. }
  998. rt_uint8_t rt_pm_get_sleep_mode(void)
  999. {
  1000. struct rt_pm *pm;
  1001. pm = &_pm;
  1002. return pm->sleep_mode;
  1003. }
  1004. /* get pm entity pointer */
  1005. struct rt_pm *rt_pm_get_handle(void)
  1006. {
  1007. return &_pm;
  1008. }
  1009. #ifdef PM_ENABLE_DEBUG
  1010. /**
  1011. * print current module sleep request list
  1012. *
  1013. * @param none
  1014. *
  1015. * @return none
  1016. */
  1017. void pm_sleep_dump(void)
  1018. {
  1019. uint8_t index;
  1020. uint16_t len;
  1021. rt_kprintf("+-------------+--------------+\n");
  1022. rt_kprintf("| Sleep Mode | Request List |\n");
  1023. rt_kprintf("+-------------+--------------+\n");
  1024. for (index = 0; index < PM_SLEEP_MODE_MAX -1; index++)
  1025. {
  1026. for (len = 0; len < ((PM_MODULE_MAX_ID + 31) / 32); len++)
  1027. {
  1028. rt_kprintf("| Mode[%d] : %d | 0x%08x |\n", index, len,
  1029. _pm.sleep_status[index][len]);
  1030. }
  1031. }
  1032. rt_kprintf("+-------------+--------------+\n");
  1033. }
  1034. MSH_CMD_EXPORT(pm_sleep_dump, dump pm request list);
  1035. static void pm_sleep_request(int argc, char **argv)
  1036. {
  1037. int module = 0;
  1038. int mode = 0;
  1039. if (argc >= 3)
  1040. {
  1041. module = atoi(argv[1]);
  1042. mode = atoi(argv[2]);
  1043. rt_pm_sleep_request(module, mode);
  1044. }
  1045. }
  1046. MSH_CMD_EXPORT(pm_sleep_request, pm_sleep_request module sleep_mode);
  1047. static void pm_sleep_release(int argc, char **argv)
  1048. {
  1049. int module = 0;
  1050. int mode = 0;
  1051. if (argc >= 3)
  1052. {
  1053. module = atoi(argv[1]);
  1054. mode = atoi(argv[2]);
  1055. rt_pm_sleep_release(module, mode);
  1056. }
  1057. }
  1058. MSH_CMD_EXPORT(pm_sleep_release, pm_sleep_release module sleep_mode);
  1059. #endif
  1060. static void rt_pm_dump_status(void)
  1061. {
  1062. rt_uint32_t index;
  1063. struct rt_pm *pm;
  1064. pm = &_pm;
  1065. rt_kprintf("| Power Management Mode | Counter | Timer |\n");
  1066. rt_kprintf("+-----------------------+---------+-------+\n");
  1067. for (index = 0; index < PM_SLEEP_MODE_MAX; index ++)
  1068. {
  1069. int has_timer = 0;
  1070. if (pm->timer_mask & (1 << index))
  1071. has_timer = 1;
  1072. rt_kprintf("| %021s | %7d | %5d |\n", _pm_sleep_str[index], pm->modes[index], has_timer);
  1073. }
  1074. rt_kprintf("+-----------------------+---------+-------+\n");
  1075. rt_kprintf("pm current sleep mode: %s\n", _pm_sleep_str[pm->sleep_mode]);
  1076. rt_kprintf("pm current run mode: %s\n", _pm_run_str[pm->run_mode]);
  1077. rt_kprintf("\n");
  1078. rt_kprintf("| module | busy | start time | timeout |\n");
  1079. rt_kprintf("+--------+------+------------+-----------+\n");
  1080. for (index = 0; index < PM_MODULE_MAX_ID; index ++)
  1081. {
  1082. if ((pm->module_status[index].busy_flag == RT_TRUE) ||
  1083. (pm->module_status[index].req_status != 0x00))
  1084. {
  1085. rt_kprintf("| %04d | %d | 0x%08x | 0x%08x |\n",
  1086. index, pm->module_status[index].busy_flag,
  1087. pm->module_status[index].start_time,
  1088. pm->module_status[index].timeout);
  1089. }
  1090. }
  1091. rt_kprintf("+--------+------+------------+-----------+\n");
  1092. }
  1093. FINSH_FUNCTION_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
  1094. MSH_CMD_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
  1095. #endif
  1096. #endif /* RT_USING_PM */