user_sleep.c 16 KB

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