user_sleep.c 15 KB

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