1
0

pm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. pm->ops->exit(pm);
  203. /* update current mode */
  204. pm->current_mode = mode;
  205. /* current mode is higher run mode */
  206. if (mode < PM_SLEEP_MODE_START)
  207. {
  208. /* enter run mode */
  209. pm->ops->enter(pm);
  210. #if PM_RUN_MODE_COUNT > 1
  211. /* frequency change */
  212. pm->ops->frequency_change(pm, 0);
  213. _pm_device_frequency_change();
  214. #endif
  215. }
  216. else
  217. {
  218. /* do nothing when request higher sleep mode,
  219. * and swithc to new sleep mode in rt_pm_enter()
  220. */
  221. }
  222. }
  223. rt_hw_interrupt_enable(level);
  224. }
  225. /**
  226. * Upper application or device driver releases the stall
  227. * of corresponding power mode.
  228. *
  229. * @param parameter the parameter of run mode or sleep mode
  230. *
  231. */
  232. void rt_pm_release(rt_ubase_t mode)
  233. {
  234. rt_ubase_t level;
  235. struct rt_pm *pm;
  236. pm = &_pm;
  237. if (mode > PM_MODE_MAX)
  238. return;
  239. level = rt_hw_interrupt_disable();
  240. if (pm->modes[mode] > 0)
  241. pm->modes[mode] --;
  242. rt_hw_interrupt_enable(level);
  243. }
  244. /**
  245. * Register a device with PM feature
  246. *
  247. * @param device the device with PM feature
  248. * @param ops the PM ops for device
  249. */
  250. void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_ops *ops)
  251. {
  252. rt_ubase_t level;
  253. struct rt_device_pm *device_pm;
  254. RT_DEBUG_NOT_IN_INTERRUPT;
  255. level = rt_hw_interrupt_disable();
  256. device_pm = (struct rt_device_pm *)RT_KERNEL_REALLOC(_pm.device_pm,
  257. (_pm.device_pm_number + 1) * sizeof(struct rt_device_pm));
  258. if (device_pm != RT_NULL)
  259. {
  260. _pm.device_pm = device_pm;
  261. _pm.device_pm[_pm.device_pm_number].device = device;
  262. _pm.device_pm[_pm.device_pm_number].ops = ops;
  263. _pm.device_pm_number += 1;
  264. }
  265. rt_sem_release(&(_pm.device_lock));
  266. rt_hw_interrupt_enable(level);
  267. }
  268. /**
  269. * Unregister device from PM manager.
  270. *
  271. * @param device the device with PM feature
  272. */
  273. void rt_pm_unregister_device(struct rt_device *device)
  274. {
  275. rt_ubase_t level;
  276. rt_uint32_t index;
  277. RT_DEBUG_NOT_IN_INTERRUPT;
  278. level = rt_hw_interrupt_disable();
  279. for (index = 0; index < _pm.device_pm_number; index ++)
  280. {
  281. if (_pm.device_pm[index].device == device)
  282. {
  283. /* remove current entry */
  284. for (; index < _pm.device_pm_number - 1; index ++)
  285. {
  286. _pm.device_pm[index] = _pm.device_pm[index + 1];
  287. }
  288. _pm.device_pm[_pm.device_pm_number - 1].device = RT_NULL;
  289. _pm.device_pm[_pm.device_pm_number - 1].ops = RT_NULL;
  290. _pm.device_pm_number -= 1;
  291. /* break out and not touch memory */
  292. break;
  293. }
  294. }
  295. rt_hw_interrupt_enable(level);
  296. }
  297. /**
  298. * RT-Thread device interface for PM device
  299. */
  300. static rt_size_t _rt_pm_device_read(rt_device_t dev,
  301. rt_off_t pos,
  302. void *buffer,
  303. rt_size_t size)
  304. {
  305. struct rt_pm *pm;
  306. rt_size_t length;
  307. length = 0;
  308. pm = (struct rt_pm *)dev;
  309. RT_ASSERT(pm != RT_NULL);
  310. if (pos <= PM_MODE_MAX)
  311. {
  312. int mode;
  313. mode = pm->modes[pos];
  314. length = rt_snprintf(buffer, size, "%d", mode);
  315. }
  316. return length;
  317. }
  318. static rt_size_t _rt_pm_device_write(rt_device_t dev,
  319. rt_off_t pos,
  320. const void *buffer,
  321. rt_size_t size)
  322. {
  323. unsigned char request;
  324. if (size)
  325. {
  326. /* get request */
  327. request = *(unsigned char *)buffer;
  328. if (request == '1')
  329. {
  330. rt_pm_request(pos);
  331. }
  332. else if (request == '0')
  333. {
  334. rt_pm_release(pos);
  335. }
  336. }
  337. return 1;
  338. }
  339. static rt_err_t _rt_pm_device_control(rt_device_t dev,
  340. int cmd,
  341. void *args)
  342. {
  343. rt_uint32_t mode;
  344. switch (cmd)
  345. {
  346. case RT_PM_DEVICE_CTRL_REQUEST:
  347. mode = (rt_uint32_t)args;
  348. rt_pm_request(mode);
  349. break;
  350. case RT_PM_DEVICE_CTRL_RELEASE:
  351. mode = (rt_uint32_t)args;
  352. rt_pm_release(mode);
  353. break;
  354. }
  355. return RT_EOK;
  356. }
  357. /**
  358. * This function will initialize power management.
  359. *
  360. * @param ops the PM operations.
  361. * @param timer_mask indicates which mode has timer feature.
  362. * @param user_data user data
  363. */
  364. void rt_system_pm_init(const struct rt_pm_ops *ops,
  365. rt_uint8_t timer_mask,
  366. void *user_data)
  367. {
  368. struct rt_device *device;
  369. struct rt_pm *pm;
  370. pm = &_pm;
  371. device = &(_pm.parent);
  372. device->type = RT_Device_Class_PM;
  373. device->rx_indicate = RT_NULL;
  374. device->tx_complete = RT_NULL;
  375. device->init = RT_NULL;
  376. device->open = RT_NULL;
  377. device->close = RT_NULL;
  378. device->read = _rt_pm_device_read;
  379. device->write = _rt_pm_device_write;
  380. device->control = _rt_pm_device_control;
  381. device->user_data = user_data;
  382. /* register PM device to the system */
  383. rt_device_register(device, "pm", RT_DEVICE_FLAG_RDWR);
  384. /* todo : add to kernel source code */
  385. rt_thread_idle_sethook(rt_pm_enter);
  386. rt_memset(pm->modes, 0, sizeof(pm->modes));
  387. pm->current_mode = PM_RUN_MODE_DEFAULT;
  388. pm->timer_mask = timer_mask;
  389. pm->ops = ops;
  390. pm->device_pm = RT_NULL;
  391. pm->device_pm_number = 0;
  392. /* initialize semaphore */
  393. rt_sem_init(&(pm->device_lock), "pm", 1, RT_IPC_FLAG_FIFO);
  394. /* request in default running mode */
  395. rt_pm_request(PM_RUN_MODE_DEFAULT);
  396. #ifdef PM_SLEEP_MODE_DEFAULT
  397. /* request in default sleep mode */
  398. rt_pm_request(PM_SLEEP_MODE_DEFAULT);
  399. #endif
  400. /* must hold on deep shutdown mode */
  401. rt_pm_request(PM_MODE_MAX);
  402. }
  403. #ifdef RT_USING_FINSH
  404. #include <finsh.h>
  405. static void rt_pm_release_mode(int argc, char **argv)
  406. {
  407. int mode = 0;
  408. if (argc >= 2)
  409. {
  410. mode = atoi(argv[1]);
  411. }
  412. rt_pm_release(mode);
  413. }
  414. MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode, pm_release, release power management mode);
  415. static void rt_pm_request_mode(int argc, char **argv)
  416. {
  417. int mode = 0;
  418. if (argc >= 2)
  419. {
  420. mode = atoi(argv[1]);
  421. }
  422. rt_pm_request(mode);
  423. }
  424. MSH_CMD_EXPORT_ALIAS(rt_pm_request_mode, pm_request, request power management mode);
  425. static void rt_pm_dump_status(void)
  426. {
  427. static const char *pm_str[] = PM_MODE_NAMES;
  428. rt_uint32_t index;
  429. struct rt_pm *pm;
  430. pm = &_pm;
  431. rt_kprintf("| Power Management Mode | Counter | Timer |\n");
  432. rt_kprintf("+-----------------------+---------+-------+\n");
  433. for (index = 0; index <= PM_MODE_MAX; index ++)
  434. {
  435. int has_timer = 0;
  436. if (pm->timer_mask & (1 << index))
  437. has_timer = 1;
  438. rt_kprintf("| %021s | %7d | %5d |\n", pm_str[index], pm->modes[index], has_timer);
  439. }
  440. rt_kprintf("+-----------------------+---------+-------+\n");
  441. rt_kprintf("pm current mode: %s\n", pm_str[pm->current_mode]);
  442. }
  443. FINSH_FUNCTION_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
  444. MSH_CMD_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status);
  445. #endif
  446. #endif /* RT_USING_PM */