pm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <drivers/pm.h>
  14. #ifdef RT_USING_PM
  15. static struct rt_pm _pm;
  16. /**
  17. * This function will suspend all registered devices
  18. */
  19. static void _pm_device_suspend(void)
  20. {
  21. int index;
  22. for (index = 0; index < _pm.device_pm_number; index++)
  23. {
  24. if (_pm.device_pm[index].ops->suspend != RT_NULL)
  25. {
  26. _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device);
  27. }
  28. }
  29. }
  30. /**
  31. * This function will resume all registered devices
  32. */
  33. static void _pm_device_resume(void)
  34. {
  35. int index;
  36. for (index = 0; index < _pm.device_pm_number; index++)
  37. {
  38. if (_pm.device_pm[index].ops->resume != RT_NULL)
  39. {
  40. _pm.device_pm[index].ops->resume(_pm.device_pm[index].device);
  41. }
  42. }
  43. }
  44. #if PM_RUN_MODE_COUNT > 1
  45. /**
  46. * This function will update the frequency of all registered devices
  47. */
  48. static void _pm_device_frequency_change(void)
  49. {
  50. rt_uint32_t index;
  51. /* make the frequency change */
  52. for (index = 0; index < _pm.device_pm_number; index ++)
  53. {
  54. if (_pm.device_pm[index].ops->frequency_change != RT_NULL)
  55. _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device);
  56. }
  57. }
  58. #endif
  59. /**
  60. * This function will enter corresponding power mode.
  61. */
  62. void rt_pm_enter(void)
  63. {
  64. rt_ubase_t level;
  65. struct rt_pm *pm;
  66. rt_uint32_t index;
  67. rt_tick_t timeout_tick;
  68. pm = &_pm;
  69. /* disable interrupt before check run modes */
  70. level = rt_hw_interrupt_disable();
  71. /* check each run mode, and decide to swithc to run mode or sleep mode */
  72. for (index = 0; index < PM_RUN_MODE_COUNT; index++)
  73. {
  74. if (pm->modes[index])
  75. {
  76. if (index > pm->current_mode)
  77. {
  78. pm->ops->exit(pm);
  79. pm->current_mode = index;
  80. pm->ops->enter(pm);
  81. #if PM_RUN_MODE_COUNT > 1
  82. pm->ops->frequency_change(pm, 0);
  83. _pm_device_frequency_change();
  84. #endif
  85. }
  86. rt_hw_interrupt_enable(level);
  87. /* The current mode is run mode, no need to check sleep mode */
  88. return ;
  89. }
  90. }
  91. /* enable interrupt after check run modes */
  92. rt_hw_interrupt_enable(level);
  93. level = rt_hw_interrupt_disable();
  94. /* check each sleep mode to decide which mode can system sleep. */
  95. for (index = PM_SLEEP_MODE_START; index < PM_SLEEP_MODE_START + PM_SLEEP_MODE_COUNT; index++)
  96. {
  97. if (pm->modes[index])
  98. {
  99. /* let mcu sleep when system is idle */
  100. /* run mode to sleep mode */
  101. if (pm->current_mode < PM_SLEEP_MODE_START)
  102. {
  103. /* exit run mode */
  104. pm->ops->exit(pm);
  105. }
  106. /* set current power mode */
  107. pm->current_mode = index;
  108. pm->exit_count = 1;
  109. /* suspend all of devices with PM feature */
  110. _pm_device_suspend();
  111. /* should start pm timer */
  112. if (pm->timer_mask & (1 << index))
  113. {
  114. /* get next os tick */
  115. timeout_tick = rt_timer_next_timeout_tick();
  116. if (timeout_tick != RT_TICK_MAX)
  117. {
  118. timeout_tick -= rt_tick_get();
  119. #if defined(PM_MIN_ENTER_SLEEP_TICK) && PM_MIN_ENTER_SLEEP_TICK > 0
  120. if (timeout_tick < PM_MIN_ENTER_SLEEP_TICK)
  121. {
  122. rt_hw_interrupt_enable(level);
  123. /* limit the minimum time to enter timer sleep mode */
  124. return ;
  125. }
  126. #endif
  127. }
  128. /* startup pm timer */
  129. pm->ops->timer_start(pm, timeout_tick);
  130. }
  131. /* enter sleep and wait to be waken up */
  132. pm->ops->enter(pm);
  133. rt_hw_interrupt_enable(level);
  134. /* exit from low power mode */
  135. rt_pm_exit();
  136. return ;
  137. }
  138. }
  139. rt_hw_interrupt_enable(level);
  140. }
  141. /**
  142. * This function exits from sleep mode.
  143. */
  144. void rt_pm_exit(void)
  145. {
  146. rt_ubase_t level;
  147. struct rt_pm *pm;
  148. rt_tick_t delta_tick;
  149. pm = &_pm;
  150. level = rt_hw_interrupt_disable();
  151. if (pm->exit_count)
  152. {
  153. pm->exit_count = 0;
  154. if (pm->current_mode >= PM_SLEEP_MODE_START)
  155. {
  156. /* sleep mode with timer */
  157. if (pm->timer_mask & (1 << pm->current_mode))
  158. {
  159. /* get the tick of pm timer */
  160. delta_tick = pm->ops->timer_get_tick(pm);
  161. /* stop pm timer */
  162. pm->ops->timer_stop(pm);
  163. if (delta_tick)
  164. {
  165. /* adjust OS tick */
  166. rt_tick_set(rt_tick_get() + delta_tick);
  167. /* check system timer */
  168. rt_timer_check();
  169. }
  170. }
  171. /* exit from sleep mode */
  172. pm->ops->exit(pm);
  173. /* resume the device with PM feature */
  174. _pm_device_resume();
  175. }
  176. }
  177. rt_hw_interrupt_enable(level);
  178. }
  179. /**
  180. * Upper application or device driver requests the system
  181. * stall in corresponding power mode.
  182. *
  183. * @param parameter the parameter of run mode or sleep mode
  184. */
  185. void rt_pm_request(rt_ubase_t mode)
  186. {
  187. rt_ubase_t level;
  188. struct rt_pm *pm;
  189. pm = &_pm;
  190. if (mode > PM_MODE_MAX)
  191. return;
  192. level = rt_hw_interrupt_disable();
  193. /* update pm modes table */
  194. pm->modes[mode] ++;
  195. /* request higter mode with a smaller mode value*/
  196. if (mode < pm->current_mode)
  197. {
  198. /* the old current mode is RUN mode, need to all pm->ops->exit(),
  199. * if not, it has already called in rt_pm_exit()
  200. */
  201. if (pm->current_mode < PM_SLEEP_MODE_START)
  202. {
  203. pm->ops->exit(pm);
  204. }
  205. else if (pm->exit_count)
  206. {
  207. /* call exeit when global interrupt is disable */
  208. pm->ops->exit(pm);
  209. pm->exit_count = 0;
  210. }
  211. /* update current mode */
  212. pm->current_mode = mode;
  213. /* current mode is higher run mode */
  214. if (mode < PM_SLEEP_MODE_START)
  215. {
  216. /* enter run mode */
  217. pm->ops->enter(pm);
  218. #if PM_RUN_MODE_COUNT > 1
  219. /* frequency change */
  220. pm->ops->frequency_change(pm, 0);
  221. _pm_device_frequency_change();
  222. #endif
  223. }
  224. else
  225. {
  226. /* do nothing when request higher sleep mode,
  227. * and swithc to new sleep mode in rt_pm_enter()
  228. */
  229. }
  230. }
  231. rt_hw_interrupt_enable(level);
  232. }
  233. /**
  234. * Upper application or device driver releases the stall
  235. * of corresponding power mode.
  236. *
  237. * @param parameter the parameter of run mode or sleep mode
  238. *
  239. */
  240. void rt_pm_release(rt_ubase_t mode)
  241. {
  242. rt_ubase_t level;
  243. struct rt_pm *pm;
  244. pm = &_pm;
  245. if (mode > PM_MODE_MAX)
  246. return;
  247. level = rt_hw_interrupt_disable();
  248. if (pm->modes[mode] > 0)
  249. pm->modes[mode] --;
  250. rt_hw_interrupt_enable(level);
  251. }
  252. /**
  253. * Register a device with PM feature
  254. *
  255. * @param device the device with PM feature
  256. * @param ops the PM ops for device
  257. */
  258. void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_ops *ops)
  259. {
  260. rt_ubase_t level;
  261. struct rt_device_pm *device_pm;
  262. RT_DEBUG_NOT_IN_INTERRUPT;
  263. level = rt_hw_interrupt_disable();
  264. device_pm = (struct rt_device_pm *)RT_KERNEL_REALLOC(_pm.device_pm,
  265. (_pm.device_pm_number + 1) * sizeof(struct rt_device_pm));
  266. if (device_pm != RT_NULL)
  267. {
  268. _pm.device_pm = device_pm;
  269. _pm.device_pm[_pm.device_pm_number].device = device;
  270. _pm.device_pm[_pm.device_pm_number].ops = ops;
  271. _pm.device_pm_number += 1;
  272. }
  273. rt_sem_release(&(_pm.device_lock));
  274. rt_hw_interrupt_enable(level);
  275. }
  276. /**
  277. * Unregister device from PM manager.
  278. *
  279. * @param device the device with PM feature
  280. */
  281. void rt_pm_unregister_device(struct rt_device *device)
  282. {
  283. rt_ubase_t level;
  284. rt_uint32_t index;
  285. RT_DEBUG_NOT_IN_INTERRUPT;
  286. level = rt_hw_interrupt_disable();
  287. for (index = 0; index < _pm.device_pm_number; index ++)
  288. {
  289. if (_pm.device_pm[index].device == device)
  290. {
  291. /* remove current entry */
  292. for (; index < _pm.device_pm_number - 1; index ++)
  293. {
  294. _pm.device_pm[index] = _pm.device_pm[index + 1];
  295. }
  296. _pm.device_pm[_pm.device_pm_number - 1].device = RT_NULL;
  297. _pm.device_pm[_pm.device_pm_number - 1].ops = RT_NULL;
  298. _pm.device_pm_number -= 1;
  299. /* break out and not touch memory */
  300. break;
  301. }
  302. }
  303. rt_hw_interrupt_enable(level);
  304. }
  305. /**
  306. * RT-Thread device interface for PM device
  307. */
  308. static rt_size_t _rt_pm_device_read(rt_device_t dev,
  309. rt_off_t pos,
  310. void *buffer,
  311. rt_size_t size)
  312. {
  313. struct rt_pm *pm;
  314. rt_size_t length;
  315. length = 0;
  316. pm = (struct rt_pm *)dev;
  317. RT_ASSERT(pm != RT_NULL);
  318. if (pos <= PM_MODE_MAX)
  319. {
  320. int mode;
  321. mode = pm->modes[pos];
  322. length = rt_snprintf(buffer, size, "%d", mode);
  323. }
  324. return length;
  325. }
  326. static rt_size_t _rt_pm_device_write(rt_device_t dev,
  327. rt_off_t pos,
  328. const void *buffer,
  329. rt_size_t size)
  330. {
  331. unsigned char request;
  332. if (size)
  333. {
  334. /* get request */
  335. request = *(unsigned char *)buffer;
  336. if (request == '1')
  337. {
  338. rt_pm_request(pos);
  339. }
  340. else if (request == '0')
  341. {
  342. rt_pm_release(pos);
  343. }
  344. }
  345. return 1;
  346. }
  347. static rt_err_t _rt_pm_device_control(rt_device_t dev,
  348. int cmd,
  349. void *args)
  350. {
  351. rt_uint32_t mode;
  352. switch (cmd)
  353. {
  354. case RT_PM_DEVICE_CTRL_REQUEST:
  355. mode = (rt_uint32_t)args;
  356. rt_pm_request(mode);
  357. break;
  358. case RT_PM_DEVICE_CTRL_RELEASE:
  359. mode = (rt_uint32_t)args;
  360. rt_pm_release(mode);
  361. break;
  362. }
  363. return RT_EOK;
  364. }
  365. /**
  366. * This function will initialize power management.
  367. *
  368. * @param ops the PM operations.
  369. * @param timer_mask indicates which mode has timer feature.
  370. * @param user_data user data
  371. */
  372. void rt_system_pm_init(const struct rt_pm_ops *ops,
  373. rt_uint8_t timer_mask,
  374. void *user_data)
  375. {
  376. struct rt_device *device;
  377. struct rt_pm *pm;
  378. pm = &_pm;
  379. device = &(_pm.parent);
  380. device->type = RT_Device_Class_PM;
  381. device->rx_indicate = RT_NULL;
  382. device->tx_complete = RT_NULL;
  383. device->init = RT_NULL;
  384. device->open = RT_NULL;
  385. device->close = RT_NULL;
  386. device->read = _rt_pm_device_read;
  387. device->write = _rt_pm_device_write;
  388. device->control = _rt_pm_device_control;
  389. device->user_data = user_data;
  390. /* register PM device to the system */
  391. rt_device_register(device, "pm", RT_DEVICE_FLAG_RDWR);
  392. /* todo : add to kernel source code */
  393. rt_thread_idle_sethook(rt_pm_enter);
  394. rt_memset(pm->modes, 0, sizeof(pm->modes));
  395. pm->current_mode = PM_RUN_MODE_DEFAULT;
  396. pm->timer_mask = timer_mask;
  397. pm->ops = ops;
  398. pm->device_pm = RT_NULL;
  399. pm->device_pm_number = 0;
  400. /* initialize semaphore */
  401. rt_sem_init(&(pm->device_lock), "pm", 1, RT_IPC_FLAG_FIFO);
  402. /* request in default running mode */
  403. rt_pm_request(PM_RUN_MODE_DEFAULT);
  404. #ifdef PM_SLEEP_MODE_DEFAULT
  405. /* request in default sleep mode */
  406. rt_pm_request(PM_SLEEP_MODE_DEFAULT);
  407. #endif
  408. /* must hold on deep shutdown mode */
  409. rt_pm_request(PM_MODE_MAX);
  410. }
  411. #ifdef RT_USING_FINSH
  412. #include <finsh.h>
  413. static void rt_pm_release_mode(int argc, char **argv)
  414. {
  415. int mode = 0;
  416. if (argc >= 2)
  417. {
  418. mode = atoi(argv[1]);
  419. }
  420. rt_pm_release(mode);
  421. }
  422. MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode, pm_release, release power management mode);
  423. static void rt_pm_request_mode(int argc, char **argv)
  424. {
  425. int mode = 0;
  426. if (argc >= 2)
  427. {
  428. mode = atoi(argv[1]);
  429. }
  430. rt_pm_request(mode);
  431. }
  432. MSH_CMD_EXPORT_ALIAS(rt_pm_request_mode, pm_request, request power management mode);
  433. static void rt_pm_dump_status(void)
  434. {
  435. static const char *pm_str[] = PM_MODE_NAMES;
  436. rt_uint32_t index;
  437. struct rt_pm *pm;
  438. pm = &_pm;
  439. rt_kprintf("| Power Management Mode | Counter | Timer |\n");
  440. rt_kprintf("+-----------------------+---------+-------+\n");
  441. for (index = 0; index <= PM_MODE_MAX; index ++)
  442. {
  443. int has_timer = 0;
  444. if (pm->timer_mask & (1 << index))
  445. has_timer = 1;
  446. rt_kprintf("| %021s | %7d | %5d |\n", pm_str[index], pm->modes[index], has_timer);
  447. }
  448. rt_kprintf("+-----------------------+---------+-------+\n");
  449. rt_kprintf("pm current mode: %s\n", pm_str[pm->current_mode]);
  450. }
  451. FINSH_FUNCTION_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
  452. MSH_CMD_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
  453. #endif
  454. #endif /* RT_USING_PM */