user_sleep.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #include "user_sleep.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <sys/time.h>
  7. #include <inttypes.h>
  8. #include "sdkconfig.h"
  9. #include "soc/soc_caps.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_sleep.h"
  13. #include "esp_log.h"
  14. #include "driver/rtc_io.h"
  15. #include "soc/rtc.h"
  16. #include "nvs_flash.h"
  17. #include "nvs.h"
  18. #include "EPD.h"
  19. #include "FONT_LIB.h"
  20. #include "esp_timer.h"
  21. #include "freertos/timers.h"
  22. #include "SPIFFS.h"
  23. #include "ulp_main.h"
  24. #include "LORA.h"
  25. // 定时器选择
  26. #define TIMER_CHOICE 1 // 0:ESP //1 FREERTOS
  27. #if TIMER_CHOICE
  28. TimerHandle_t sleep_timer; // 进入休眠定时器
  29. TimerHandle_t already_send_timer; // 可以发送数据的定时器
  30. #else
  31. esp_timer_handle_t sleep_timer; // 开启睡眠定时器
  32. esp_timer_handle_t already_send_timer; // 可以发送数据的定时器
  33. #endif
  34. static RTC_DATA_ATTR struct timeval sleep_enter_time;
  35. RTC_DATA_ATTR uint8_t _wakeup_reson = 0; // 内存上次唤醒的原因
  36. int64_t sleep_before_us = 0; // 开始进入睡眠时间
  37. int64_t sleep_afterr_us = 0; // 唤醒时间
  38. #define DEEP_SLEEP 0
  39. #if !DEEP_SLEEP
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <time.h>
  44. #include <sys/time.h>
  45. #include "freertos/FreeRTOS.h"
  46. #include "freertos/task.h"
  47. #include "driver/uart.h"
  48. #include "esp_sleep.h"
  49. #include "esp_log.h"
  50. #include "esp_timer.h"
  51. #include "esp_log.h"
  52. static const char *LOG_TAG = "user_sleep";
  53. extern QueueHandle_t sleep_queue;
  54. extern QueueHandle_t wakeup_queue;
  55. extern void light_sleep_task(void *args);
  56. extern void wakeup_sleep_task(void *args);
  57. bool is_sleep = false;
  58. extern void set_screen_dis_info_and_send_queue(bool is_into_sleep, bool is_left, bool is_change_power, bool is_dont_dis, bool sleep_ms);
  59. /*********************************************************************************
  60. * function : sleep_timer_callback
  61. * Description : 进入睡眠回调函数
  62. * Input :
  63. * Output :
  64. * Author : 祁鑫 Data : 2023 10.18
  65. **********************************************************************************/
  66. void sleep_timer_callback(void *arg)
  67. {
  68. #if 0
  69. send_sleep_Queue();
  70. #else
  71. // printf("fall into sleep\r\n");
  72. #include "ulp_riscv.h"
  73. #include "esp_sleep.h"
  74. #include "driver/uart.h"
  75. // printf("error =%d\r\n",err);
  76. sleep_before_us = esp_timer_get_time();
  77. uart_wait_tx_idle_polling(0);
  78. // ulp_riscv_timer_resume();
  79. is_sleep = true;
  80. // #if LORA_SLEEP_ENABLE
  81. // //rtc_gpio_hold_dis(LORA_POWER_PIN);
  82. // lora_set_power_level(0);
  83. // //rtc_gpio_hold_en(LORA_POWER_PIN);
  84. // #endif
  85. // esp_light_sleep_start();
  86. #endif
  87. }
  88. /*********************************************************************************
  89. * function : sleep_timer_start
  90. * Description : 倒计时开始进入睡眠
  91. * Input :
  92. * Output :
  93. * Author : 祁鑫 Data : 2023 10.18
  94. **********************************************************************************/
  95. void sleep_timer_start(int ms)
  96. {
  97. #if TIMER_CHOICE
  98. xTimerStop(sleep_timer, 0); // 停止定时器
  99. // 在这里可以改变定时器的超时时间
  100. // 第一个参数是定时器句柄,第二个参数是新的超时时间(以时钟节拍为单位)
  101. xTimerChangePeriod(sleep_timer, pdMS_TO_TICKS(ms), 0);
  102. is_sleep = false;
  103. xTimerStart(sleep_timer, 0); // 开始定时器
  104. // xTimerStart(sleep_Timerout, 0);
  105. ESP_LOGI(LOG_TAG, "#############%s##############reset sleep_timer [%d]", is_sleep ? "true" : "false", ms);
  106. #else
  107. #if 0
  108. ESP_ERROR_CHECK(esp_timer_start_once(sleep_timer, ms*1000));//500ms
  109. #else
  110. esp_timer_start_once(sleep_timer, ms * 1000); // 500ms
  111. #endif
  112. #endif
  113. }
  114. /*********************************************************************************
  115. * function : sleep_timer_stop
  116. * Description : 停止进入睡眠
  117. * Input :
  118. * Output :
  119. * Author : 祁鑫 Data : 2023 10.18
  120. **********************************************************************************/
  121. void sleep_timer_stop(void)
  122. {
  123. #if TIMER_CHOICE
  124. xTimerStop(sleep_timer, 0); // 停止定时器
  125. #else
  126. #if 0
  127. ESP_ERROR_CHECK(esp_timer_stop(sleep_timer));
  128. #else
  129. esp_timer_stop(sleep_timer);
  130. #endif
  131. #endif
  132. }
  133. //
  134. /*********************************************************************************
  135. * function : Already_send_timer_callback
  136. * Description : 定到达指定时间准备发送数据或者接收数据
  137. * Input :
  138. * Output :
  139. * Author : 祁鑫 Data : 2023 10.18
  140. **********************************************************************************/
  141. void Already_send_timer_callback(void *arg)
  142. {
  143. #if TIMER_CHOICE
  144. // printf("Already_send_timer_callback\r\n");
  145. extern void send_can_I_receive();
  146. if (Machine_info.eflagID != 0xff)
  147. {
  148. send_can_I_receive(); // 发送可以接受的命令
  149. if (Send_list != NULL)
  150. {
  151. vTaskDelay(50 / portTICK_PERIOD_MS);
  152. lora_send_data((char *)Send_list->data, Send_list->len);
  153. printf("->send one data to gateway\r\n");
  154. }
  155. else
  156. {
  157. printf("list not have data\r\n");
  158. }
  159. }
  160. // uart_wait_tx_idle_polling(UART_NUM_1);
  161. // example_register_timer_wakeup();
  162. // esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US*Machine_info.eflagID); //配置当前休眠的唤醒时间
  163. // printf("sleep time = %d\r\n",(TIMER_WAKEUP_TIME_US*Machine_info.eflagID)/1000);
  164. #if 1
  165. sleep_timer_start(100); // 进入睡眠
  166. #endif
  167. #else
  168. #if 0
  169. send_sleep_Queue();
  170. #else
  171. printf("Already_send_timer_callback\r\n");
  172. extern void send_can_I_receive();
  173. send_can_I_receive(); // 发送可以接受的命令
  174. uart_wait_tx_idle_polling(UART_NUM_1);
  175. sleep_timer_start(600); // 进入睡眠
  176. // #include "ulp_riscv.h"
  177. // #include "esp_sleep.h"
  178. // #include "driver/uart.h"
  179. // //printf("error =%d\r\n",err);
  180. // uart_wait_tx_idle_polling(0);
  181. // //ulp_riscv_timer_resume();
  182. // esp_light_sleep_start();
  183. #endif
  184. #endif
  185. }
  186. /*********************************************************************************
  187. * function : Already_send_timer_start
  188. * Description : 定到达指定时间准备发送数据或者接收数据
  189. * Input :
  190. * Output :
  191. * Author : 祁鑫 Data : 2023 10.18
  192. **********************************************************************************/
  193. void Already_send_timer_start(int ms)
  194. {
  195. #if TIMER_CHOICE
  196. xTimerStop(already_send_timer, 0); // 停止定时器
  197. // 在这里可以改变定时器的超时时间
  198. // 第一个参数是定时器句柄,第二个参数是新的超时时间(以时钟节拍为单位)
  199. xTimerChangePeriod(already_send_timer, pdMS_TO_TICKS(ms), 0);
  200. xTimerStart(already_send_timer, 0); // 开始定时器
  201. #else
  202. ESP_ERROR_CHECK(esp_timer_start_once(already_send_timer, ms * 1000)); // 500ms
  203. #endif
  204. }
  205. /*********************************************************************************
  206. * function : Already_send_timer_stop
  207. * Description : 停止定时器
  208. * Input :
  209. * Output :
  210. * Author : 祁鑫 Data : 2023 10.18
  211. **********************************************************************************/
  212. void Already_send_timer_stop(void)
  213. {
  214. #if TIMER_CHOICE
  215. xTimerStop(already_send_timer, 0); // 停止定时器
  216. #else
  217. ESP_ERROR_CHECK(esp_timer_stop(already_send_timer));
  218. #endif
  219. }
  220. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  221. /*定时器唤醒*/
  222. #include "esp_check.h"
  223. #include "esp_sleep.h"
  224. static const char *TAG = "timer_wakeup";
  225. esp_err_t example_register_timer_wakeup(void)
  226. {
  227. ESP_RETURN_ON_ERROR(esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US), TAG, "Configure timer as wakeup source failed");
  228. ESP_LOGI(TAG, "timer wakeup source is ready");
  229. return ESP_OK;
  230. }
  231. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  232. #endif
  233. void user_sleep_into()
  234. {
  235. #if DEEP_SLEEP
  236. struct timeval now;
  237. gettimeofday(&now, NULL);
  238. int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
  239. switch (esp_sleep_get_wakeup_cause())
  240. {
  241. #if CONFIG_EXAMPLE_EXT0_WAKEUP
  242. case ESP_SLEEP_WAKEUP_EXT0:
  243. {
  244. printf("Wake up from ext0\n");
  245. break;
  246. }
  247. #endif // CONFIG_EXAMPLE_EXT0_WAKEUP
  248. #ifdef CONFIG_EXAMPLE_EXT1_WAKEUP
  249. case ESP_SLEEP_WAKEUP_EXT1:
  250. {
  251. uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
  252. if (wakeup_pin_mask != 0)
  253. {
  254. int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
  255. printf("Wake up from GPIO %d\n", pin);
  256. }
  257. else
  258. {
  259. printf("Wake up from GPIO\n");
  260. }
  261. break;
  262. }
  263. #endif // CONFIG_EXAMPLE_EXT1_WAKEUP
  264. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  265. case ESP_SLEEP_WAKEUP_GPIO:
  266. {
  267. uint64_t wakeup_pin_mask = esp_sleep_get_gpio_wakeup_status();
  268. if (wakeup_pin_mask != 0)
  269. {
  270. int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
  271. printf("Wake up from GPIO %d\n", pin);
  272. }
  273. else
  274. {
  275. printf("Wake up from GPIO\n");
  276. }
  277. break;
  278. }
  279. #endif // SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  280. case ESP_SLEEP_WAKEUP_TIMER:
  281. {
  282. printf("Wake up from timer. Time spent in deep sleep: %dms\n", sleep_time_ms);
  283. break;
  284. }
  285. #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
  286. case ESP_SLEEP_WAKEUP_TOUCHPAD:
  287. {
  288. printf("Wake up from touch on pad %d\n", esp_sleep_get_touchpad_wakeup_status());
  289. break;
  290. }
  291. #endif // CONFIG_EXAMPLE_TOUCH_WAKEUP
  292. case ESP_SLEEP_WAKEUP_UNDEFINED:
  293. default:
  294. printf("Not a deep sleep reset\n");
  295. }
  296. vTaskDelay(1000 / portTICK_PERIOD_MS);
  297. const int wakeup_time_sec = 3;
  298. printf("Enabling timer wakeup, %ds\n", wakeup_time_sec);
  299. ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000));
  300. printf("Entering deep sleep\n");
  301. // get deep sleep enter time
  302. gettimeofday(&sleep_enter_time, NULL);
  303. font_into_sleep();
  304. #if 0
  305. int i = 60;
  306. printf("not start deep display\r\n");
  307. while(i--)
  308. {
  309. vTaskDelay(1000 / portTICK_PERIOD_MS);
  310. }
  311. printf("start deep display\r\n");
  312. epd_sleep(SCREEN_LEFT);
  313. epd_sleep(SCREEN_RIGHT);
  314. i = 60;
  315. while(i--)
  316. {
  317. vTaskDelay(1000 / portTICK_PERIOD_MS);
  318. }
  319. #else
  320. epd_sleep(SCREEN_LEFT);
  321. epd_sleep(SCREEN_RIGHT);
  322. #endif
  323. // enter deep sleep
  324. esp_deep_sleep_start();
  325. #else
  326. #if 0
  327. //进入睡眠timer
  328. const esp_timer_create_args_t lora_timer_args = {
  329. .callback = &sleep_timer_callback,
  330. /* argument specified here will be passed to timer callback function */
  331. .name = "sleep_one-shot"
  332. };
  333. ESP_ERROR_CHECK(esp_timer_create(&lora_timer_args, &sleep_timer));
  334. //唤醒后可以准备发送数据的timer
  335. const esp_timer_create_args_t send_timer_args = {
  336. .callback = &Already_send_timer_callback,
  337. /* argument specified here will be passed to timer callback function */
  338. .name = "sleep_one-shot"
  339. };
  340. ESP_ERROR_CHECK(esp_timer_create(&send_timer_args, &already_send_timer));
  341. #else
  342. // 创建
  343. sleep_timer = xTimerCreate(
  344. "sleep_timer", // 定时器名称(可以为NULL)
  345. pdMS_TO_TICKS(800), // 定时器超时时间(以毫秒为单位)
  346. pdFALSE, // 定时器为周期性(pdTRUE)还是单次(pdFALSE)
  347. 0, // 定时器ID(可以为0)
  348. sleep_timer_callback // 定时器回调函数
  349. );
  350. // 创建
  351. already_send_timer = xTimerCreate(
  352. "already_send_timer", // 定时器名称(可以为NULL)
  353. pdMS_TO_TICKS(800), // 定时器超时时间(以毫秒为单位)
  354. pdFALSE, // 定时器为周期性(pdTRUE)还是单次(pdFALSE)
  355. 0, // 定时器ID(可以为0)
  356. Already_send_timer_callback // 定时器回调函数
  357. );
  358. #endif
  359. #endif
  360. }
  361. int is_wake_up_reson() // 返回唤醒的原因
  362. {
  363. _wakeup_reson = esp_sleep_get_wakeup_cause();
  364. // /* not a wakeup from ULP, load the firmware */
  365. // if ((cause != ESP_SLEEP_WAKEUP_ULP) && (cause != ESP_SLEEP_WAKEUP_TIMER)) {
  366. // printf("Not a ULP-RISC-V wakeup (cause = %d), initializing it! \n", cause);
  367. // init_ulp_program();
  368. // }
  369. return _wakeup_reson;
  370. }
  371. #include "user_button.h"
  372. int left_adc_wake_btn_send()
  373. {
  374. uint16_t adc_value[][2] =
  375. {
  376. {BAOYANG_MIN_ADC, BAOYANG_MAX_ADC},
  377. {FENGCUN_MIN_ADC, FENGCUN_MAX_ADC},
  378. {GUZHUANG_MIN_ADC, GUZHANG_MAX_ADC},
  379. {DAILIAO_MIN_ADC, DAILIAO_MAX_ADC},
  380. {TINGJI_MIN_ADC, TINGJI_MAX_ADC},
  381. {YUNXING_MIN_ADC, YUNXING_MAX_ADC},
  382. };
  383. // 获取adc值 返回左界面索引值
  384. #include "user_button.h"
  385. int result = 0xff;
  386. int button_info = 0xfB;
  387. // extern uint16_t adc_value[][2];
  388. #include "ulp/example_config.h"
  389. // extern int32_t wakeup_result;
  390. // int result_adc = wakeup_result;
  391. #if 1
  392. int result_adc = ulp_wakeup_result;
  393. #endif
  394. int mid_result = result_adc; //(int)(((float)(result_adc/4095.0))*1100);
  395. printf("mid adc = %d\r\n", mid_result);
  396. for (int i = 0; i < 6; i++)
  397. {
  398. if ((adc_value[i][0] <= mid_result) && (adc_value[i][1] >= mid_result))
  399. {
  400. result = i;
  401. break;
  402. }
  403. }
  404. switch ((int)result)
  405. {
  406. case BAOYANG_KEY:
  407. button_info = 6;
  408. // ESP_LOGD(LOG_TAG,"bao yang");
  409. break;
  410. case FENGCUN_KEY:
  411. button_info = 5;
  412. // ESP_LOGD(LOG_TAG,"feng cun");
  413. break;
  414. case GUZHUANG_KEY:
  415. button_info = 4;
  416. // ESP_LOGD(LOG_TAG,"gu zhang");
  417. break;
  418. case DAILIAO_KEY:
  419. button_info = 3;
  420. // ESP_LOGD(LOG_TAG,"dai liao");
  421. break;
  422. case TINGJI_KEY:
  423. button_info = 2;
  424. // ESP_LOGD(LOG_TAG,"ting ji");
  425. break;
  426. case YUNXING_KEY:
  427. button_info = 1;
  428. // ESP_LOGD(LOG_TAG,"yun xing");
  429. break;
  430. default:
  431. break;
  432. }
  433. ulp_wakeup_result = 3300;
  434. if (button_info > 6)
  435. {
  436. return 0xff;
  437. }
  438. return button_info;
  439. }