user_sleep.c 15 KB

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