pm_sample.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Change Logs:
  19. * Date Author Notes
  20. * 2020-12-15 liuhy first implementation.
  21. */
  22. /*
  23. * 程序清单:这是一个 pm睡眠唤醒的使用例程
  24. * 例程导出了 pm_sample 命令到控制终端
  25. * 命令调用格式:pm_sample
  26. * 命令解释:进入不同的睡眠模式,然后用按键唤醒。
  27. * 程序功能:通过串口输出字符串,告知进入睡眠和唤醒睡眠的情况。
  28. * 注意:进入睡眠前,如果有中断挂起(SYSTICK、UART、EXTI等),睡眠将被瞬间唤醒。
  29. */
  30. #include <rtthread.h>
  31. #include <rtdevice.h>
  32. #ifdef RT_USING_PM
  33. #define PM_NAME "pm" /* 设备名称 */
  34. #define WAKE_UP_PIN 19 /* 唤醒源 */
  35. #define SLEEP_TIMES 12 /* 进入睡眠次数,轮流进入不同的睡眠模式,包括无睡眠模式 */
  36. struct pm_callback_t
  37. {
  38. volatile int in_fun_times; /*进入函数的次数*/
  39. volatile char flag; /*标志*/
  40. volatile int mode; /*需要打印的模式*/
  41. };
  42. volatile struct pm_callback_t g_pm_data;
  43. /*进入睡眠前,睡眠唤醒后,都会进入。*/
  44. /*函数打印睡眠相关的信息*/
  45. void sleep_in_out_callback(rt_uint8_t event, rt_uint8_t mode, void *data)
  46. {
  47. /*没有标志,不处理*/
  48. if(!(g_pm_data.flag))
  49. {
  50. return;
  51. }
  52. /*标志不正常,清空标志*/
  53. if((g_pm_data.flag) > 2)
  54. {
  55. (g_pm_data.flag) = 0;
  56. return;
  57. }
  58. /*模式不匹配*/
  59. if(g_pm_data.mode != mode )
  60. {
  61. return;
  62. }
  63. /*进入的事件*/
  64. switch(event)
  65. {
  66. /*进入睡眠前*/
  67. case RT_PM_ENTER_SLEEP: g_pm_data.flag = 1;
  68. rt_kprintf("\n\r##%d : ENTER ",g_pm_data.in_fun_times);
  69. g_pm_data.in_fun_times++; /*进入睡眠次数+1*/
  70. break;
  71. /*睡眠唤醒后*/
  72. case RT_PM_EXIT_SLEEP: g_pm_data.flag = 0; /*睡眠唤醒后*/
  73. /*从深睡眠唤醒后,等待UART时钟未恢复稳定,输出可能丢失*/
  74. rt_kprintf("\n\rEXIT\n\r");
  75. rt_pm_request(PM_SLEEP_MODE_NONE); /*进无休眠模式*/
  76. return;
  77. default: break;
  78. };
  79. /*当前的睡眠模式*/
  80. switch(mode)
  81. {
  82. case PM_SLEEP_MODE_NONE: rt_kprintf("PM_SLEEP_MODE_NONE\n\r");
  83. break;
  84. case PM_SLEEP_MODE_IDLE: rt_kprintf("PM_SLEEP_MODE_IDLE\n\r");
  85. break;
  86. case PM_SLEEP_MODE_LIGHT: rt_kprintf("PM_SLEEP_MODE_LIGHT\n\r");
  87. break;
  88. case PM_SLEEP_MODE_DEEP: rt_kprintf("PM_SLEEP_MODE_DEEP\n\r");
  89. break;
  90. case PM_SLEEP_MODE_STANDBY: rt_kprintf("PM_SLEEP_MODE_STANDBY\n\r");
  91. break;
  92. case PM_SLEEP_MODE_SHUTDOWN: rt_kprintf("PM_SLEEP_MODE_SHUTDOWN\n\r");
  93. break;
  94. case PM_SLEEP_MODE_MAX: rt_kprintf("PM_SLEEP_MODE_MAX\n\r");
  95. break;
  96. default: break;
  97. }
  98. }
  99. /* pm测试函数 */
  100. static void pm_test(void *parameter)
  101. {
  102. int in_mode[7],i = 0;
  103. g_pm_data.in_fun_times = 0;
  104. g_pm_data.flag = 0;
  105. in_mode[0] = PM_SLEEP_MODE_NONE;
  106. in_mode[1] = PM_SLEEP_MODE_IDLE;
  107. in_mode[2] = PM_SLEEP_MODE_LIGHT;
  108. in_mode[3] = PM_SLEEP_MODE_DEEP;
  109. in_mode[4] = PM_SLEEP_MODE_STANDBY;
  110. in_mode[5] = PM_SLEEP_MODE_SHUTDOWN;
  111. in_mode[6] = PM_SLEEP_MODE_MAX;
  112. /*设置回调函数和私有数据*/
  113. rt_pm_notify_set(sleep_in_out_callback,RT_NULL);
  114. while(i < SLEEP_TIMES)
  115. {
  116. g_pm_data.mode = in_mode[i%6];
  117. /*无休眠模式,不赋予标志*/
  118. if(g_pm_data.mode != PM_SLEEP_MODE_NONE)
  119. {
  120. g_pm_data.flag = 2;
  121. }
  122. /*彻底释放无休眠模式*/
  123. rt_pm_release_all(PM_SLEEP_MODE_NONE);
  124. /*请求选择的休眠模式*/
  125. rt_pm_request(in_mode[i%6]);
  126. rt_thread_mdelay(500);
  127. /*无休眠模式,不需要额外的等待*/
  128. while(( g_pm_data.flag != 0 )&&(g_pm_data.mode != PM_SLEEP_MODE_NONE))
  129. {
  130. rt_thread_mdelay(500);
  131. }
  132. /*释放选择的休眠模式 ,彻底释放*/
  133. rt_pm_release_all(in_mode[i%6]);
  134. i++;
  135. }
  136. /*切换为无睡眠模式*/
  137. rt_pm_request(PM_SLEEP_MODE_NONE);
  138. /*清除回调函数和私有数据*/
  139. rt_pm_notify_set(RT_NULL,RT_NULL);
  140. rt_kprintf("thread pm_test close\n\r");
  141. }
  142. /*按键唤醒的回调函数*/
  143. void wake_by_pin(void *args)
  144. {
  145. }
  146. static int pm_sample(int argc, char *argv[])
  147. {
  148. rt_thread_t thread;
  149. /* 按键引脚为输入模式 */
  150. rt_pin_mode(WAKE_UP_PIN, PIN_MODE_INPUT_PULLUP);
  151. /* 绑定中断,下降沿模式,回调函数名为wake_by_pin */
  152. rt_pin_attach_irq(WAKE_UP_PIN, PIN_IRQ_MODE_RISING, wake_by_pin, RT_NULL);
  153. /* 使能中断 */
  154. rt_pin_irq_enable(WAKE_UP_PIN, PIN_IRQ_ENABLE);
  155. thread = rt_thread_create("pm_test", pm_test, RT_NULL, 1024, 25, 10);
  156. if (thread != RT_NULL)
  157. {
  158. rt_thread_startup(thread);
  159. }
  160. else
  161. {
  162. rt_kprintf("create pm_test thread failed!\n\r");
  163. }
  164. return RT_EOK;
  165. }
  166. /* 导出到 msh 命令列表中 */
  167. MSH_CMD_EXPORT(pm_sample, pm sample);
  168. #endif