ulp_riscv_adc_example_main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /* ULP riscv example
  7. This example code is in the Public Domain (or CC0 licensed, at your option.)
  8. Unless required by applicable law or agreed to in writing, this
  9. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  10. CONDITIONS OF ANY KIND, either express or implied.
  11. */
  12. #include <stdio.h>
  13. #include <inttypes.h>
  14. #include "esp_sleep.h"
  15. #include "ulp_riscv.h"
  16. #include "ulp_riscv_adc.h"
  17. #include "ulp_main.h"
  18. #include "ulp/example_config.h"
  19. #include "esp_err.h"
  20. #include "driver/adc.h"
  21. #include "esp_adc/adc_oneshot.h"
  22. #include "esp_adc/adc_cali.h"
  23. #include "esp_adc/adc_cali_scheme.h"
  24. #include "user_sleep.h"
  25. #include "driver/uart.h"
  26. #include "esp_sleep.h"
  27. #include "esp_log.h"
  28. #include "esp_timer.h"
  29. #include "SPIFFS.h"
  30. #include "LORA.h"
  31. #include "FONT_LIB.h"
  32. #include "driver/rtc_io.h"
  33. #include "user_time.h"
  34. #include "Decection.h"
  35. #include "yc_protocol.h"
  36. #include "esp_sleep.h"
  37. #include "soc/sens_reg.h"
  38. #include "driver/gpio.h"
  39. #include "freertos/FreeRTOS.h"
  40. #include "freertos/task.h"
  41. #include "freertos/timers.h"
  42. #include "iot_button.h"
  43. static const char *LOG_TAG = "ulp_riscv_adc";
  44. // extern uint8_t _wakeup_reson; //内存上次唤醒的原因
  45. extern QueueHandle_t screen_queue;
  46. bool is_first_run = false; // 当前时间片只执行一次标志
  47. bool is_first_charge = false; // 当前充电发送只执行一次标志
  48. uint8_t charging_flag;
  49. extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
  50. extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end");
  51. static void init_ulp_program(void);
  52. extern void app_init();
  53. extern RTC_DATA_ATTR uint8_t wakeup_reson; // 内存上次唤醒的原因
  54. extern int64_t sleep_before_us; // 开始进入睡眠时间
  55. extern int64_t sleep_afterr_us; // 唤醒时间
  56. volatile int charge_is_success;
  57. volatile int charge_is_running;
  58. bool read_charge_is_success()
  59. {
  60. return true;
  61. }
  62. bool read_charge_is_running()
  63. {
  64. return true;
  65. }
  66. static void light_sleep_task(void *args)
  67. {
  68. // if(Machine_info.eflagID != 0xff) //如果未分配ID
  69. // {
  70. // esp_light_sleep_start(); //进入休眠
  71. // }
  72. extern bool is_adv;
  73. int bat_times = 0;
  74. int lev_2 = 0;
  75. while (true)
  76. {
  77. if (is_key_press())
  78. {
  79. // ESP_LOGI(LOG_TAG,"按键按下不进行休眠操作");
  80. vTaskDelay(1000 / portTICK_PERIOD_MS);
  81. continue; // 按键按下不进行休眠操作
  82. }
  83. extern bool is_sleep;
  84. if (is_sleep == true)
  85. {
  86. // printf("system is ready sleep\r\n");
  87. #if LORA_SLEEP_ENABLE
  88. // rtc_gpio_hold_dis(LORA_POWER_PIN);
  89. lora_set_power_level(0);
  90. // rtc_gpio_hold_en(LORA_POWER_PIN);
  91. #endif
  92. #if USER_LIGHT_SLEEP_ENABLE
  93. is_first_run = false;
  94. ESP_LOGE(LOG_TAG, "start light_sleep ,is_first_run = %d\r\n", is_first_run);
  95. if (Machine_info.power_status == 0) // 关机状态
  96. {
  97. // #if LORA_SLEEP_ENABLE
  98. // rtc_gpio_hold_dis(LORA_POWER_PIN);
  99. // lora_set_power_level(0);
  100. // rtc_gpio_hold_en(LORA_POWER_PIN);
  101. // #endif
  102. #if 1
  103. font_into_sleep();
  104. #include "EPD.h"
  105. epd_sleep(SCREEN_LEFT);
  106. epd_sleep(SCREEN_RIGHT);
  107. // gpio_hold_en(PIN_L_CS);
  108. // gpio_hold_en(PIN_R_CS);
  109. // gpio_set_level(PIN_L_CS,1);
  110. // gpio_set_level(PIN_R_CS,1);
  111. // gpio_reset_pin(46);
  112. // uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  113. // ESP_ERROR_CHECK(uart_wait_tx_done(UART_NUM_1,portMAX_DELAY));
  114. esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
  115. // gpio_set_level(LORA_TXD_PIN, 0);
  116. // gpio_set_level(LORA_RXD_PIN, 0);
  117. #if 0
  118. //uart_sleep_in_config();
  119. uart_driver_delete(UART_NUM_1);
  120. gpio_reset_pin(LORA_TXD_PIN);
  121. gpio_reset_pin(LORA_RXD_PIN);
  122. gpio_config_t io_conf = {};
  123. io_conf.pin_bit_mask = (1<<LORA_TXD_PIN);
  124. io_conf.mode = GPIO_MODE_OUTPUT;
  125. io_conf.pull_up_en = false;
  126. gpio_config(&io_conf);
  127. io_conf.pin_bit_mask = (1<<LORA_RXD_PIN);
  128. io_conf.mode = GPIO_MODE_OUTPUT;
  129. io_conf.pull_up_en = false;
  130. gpio_config(&io_conf);
  131. gpio_set_level(LORA_TXD_PIN, 0);
  132. gpio_set_level(LORA_RXD_PIN, 0);
  133. gpio_hold_en(LORA_TXD_PIN);
  134. gpio_hold_en(LORA_RXD_PIN);
  135. #endif
  136. // uart_sleep_out_config();
  137. // uart_driver_delete(UART_NUM_1);
  138. // esp_deep_sleep_start();
  139. #endif
  140. // vTaskDelay(100/ portTICK_PERIOD_MS);
  141. #if 1 // 电源按键
  142. gpio_reset_pin(4);
  143. int ext_wakeup_pin_0 = 4;
  144. printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  145. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  146. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  147. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  148. // No need to keep that power domain explicitly, unlike EXT1.
  149. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  150. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  151. #endif
  152. // 充电按键
  153. #if 0
  154. // adc_oneshot_del_unit(adc1_handle);
  155. gpio_reset_pin(2);
  156. int ext_wakeup_pin_1 = 2;
  157. //printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  158. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_1, 0));
  159. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  160. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  161. // No need to keep that power domain explicitly, unlike EXT1.
  162. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_1));
  163. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_1));
  164. //gpio_hold_en(4);
  165. #else
  166. gpio_reset_pin(2);
  167. const int ext_wakeup_pin_1 = 2;
  168. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  169. printf("Enabling EXT1 wakeup on pins GPIO %d\r\n", ext_wakeup_pin_1);
  170. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW));
  171. #endif
  172. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  173. ESP_ERROR_CHECK(esp_sleep_enable_ulp_wakeup());
  174. // if (!is_key_press())
  175. // {
  176. printf("=>deep sleep\r\n");
  177. uart_wait_tx_idle_polling(1); // 抛出剩余串口1消息
  178. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  179. iot_button_stop();
  180. adc_oneshot_del_unit(adc1_handle);
  181. vTaskDelay(100/ portTICK_PERIOD_MS);
  182. esp_deep_sleep_start();
  183. // }
  184. // esp_light_sleep_start();
  185. }
  186. else if ((Machine_info.power_status == 1) && Machine_info.paired == 1) // 开机状态且配对
  187. /*{
  188. ESP_LOGW(LOG_TAG,"//开机状态且配对\r\n");//不进休眠
  189. }
  190. else if(Machine_info.paired ==0 )*/
  191. {
  192. // 重新配置休眠唤醒时间
  193. // printf("esp_sleep_enable_timer_wakeup ret = %d\n",esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US));//配置当前休眠的唤醒时间
  194. font_into_sleep();
  195. #include "EPD.h"
  196. epd_sleep(SCREEN_LEFT);
  197. epd_sleep(SCREEN_RIGHT);
  198. // uart_driver_delete(UART_NUM_1);
  199. // gpio_reset_pin(LORA_TXD_PIN);
  200. // gpio_reset_pin(LORA_RXD_PIN);
  201. // gpio_config_t io_conf = {};
  202. // io_conf.pin_bit_mask = (1<<LORA_TXD_PIN);
  203. // io_conf.mode = GPIO_MODE_OUTPUT;
  204. // io_conf.pull_up_en = false;
  205. // gpio_config(&io_conf);
  206. // io_conf.pin_bit_mask = (1<<LORA_RXD_PIN);
  207. // io_conf.mode = GPIO_MODE_OUTPUT;
  208. // io_conf.pull_up_en = false;
  209. // gpio_config(&io_conf);
  210. // gpio_set_level(LORA_TXD_PIN, 0);
  211. // gpio_set_level(LORA_RXD_PIN, 0);
  212. // gpio_hold_en(LORA_TXD_PIN);
  213. // gpio_hold_en(LORA_RXD_PIN);
  214. #if 0
  215. adc_oneshot_del_unit(adc1_handle);
  216. gpio_reset_pin(8);
  217. int ext_wakeup_pin_0 = 8;
  218. printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  219. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  220. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  221. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  222. // No need to keep that power domain explicitly, unlike EXT1.
  223. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  224. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  225. #else
  226. iot_button_stop();
  227. #if 1 // 电源按键
  228. // adc_oneshot_del_unit(adc1_handle);
  229. gpio_reset_pin(4);
  230. int ext_wakeup_pin_0 = 4;
  231. printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  232. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 0));
  233. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  234. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  235. // No need to keep that power domain explicitly, unlike EXT1.
  236. ESP_ERROR_CHECK(rtc_gpio_pullup_en(ext_wakeup_pin_0));
  237. ESP_ERROR_CHECK(rtc_gpio_pulldown_dis(ext_wakeup_pin_0));
  238. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  239. ESP_ERROR_CHECK(esp_sleep_enable_ulp_wakeup());
  240. #endif
  241. #if 0
  242. //充电按键
  243. gpio_reset_pin(2);
  244. const int ext_wakeup_pin_1 = 2;
  245. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  246. printf("Enabling EXT1 wakeup on pins GPIO %d\r\n", ext_wakeup_pin_1);
  247. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ALL_LOW));
  248. #endif
  249. #endif
  250. esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US); // 配置当前休眠的唤醒时间
  251. // adc_oneshot_del_unit(adc1_handle);
  252. // gpio_hold_en(4);
  253. // vTaskDelay(100/ portTICK_PERIOD_MS);
  254. // OTA 不休眠
  255. if (!is_adv)
  256. {
  257. // if (!is_key_press())
  258. // {
  259. ESP_LOGW(LOG_TAG, "-> sleep\r\n");
  260. uart_wait_tx_idle_polling(1); // 抛出剩余串口1消息
  261. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM); // 抛出剩余串口0消息
  262. vTaskDelay(100/ portTICK_PERIOD_MS);
  263. esp_light_sleep_start();
  264. // }
  265. // else
  266. // {
  267. // ESP_LOGW(LOG_TAG, "power key is press\r\n");
  268. // }
  269. }
  270. else
  271. {
  272. iot_button_stop();
  273. ESP_LOGW(LOG_TAG, "OTA not sleep");
  274. }
  275. }
  276. is_sleep = false;
  277. #endif
  278. // #if USER_DEEP_SLEEP_ENABLE
  279. // printf("start deep sleep\r\n");
  280. // uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  281. // //重新配置休眠唤醒时间
  282. // esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US); //配置当前休眠的唤醒时间
  283. // // enter deep sleep
  284. // esp_deep_sleep_start();
  285. // #endif
  286. }
  287. // char *wakeup_reason = malloc(20*sizeof(uint8_t));
  288. int reson = esp_sleep_get_wakeup_cause();
  289. if (is_btn_timeout())
  290. {
  291. terminal_send_data(); // 添加数据队列
  292. }
  293. ESP_LOGW(LOG_TAG, "wakeup reson = %d\r\n", reson);
  294. switch (reson)
  295. {
  296. case ESP_SLEEP_WAKEUP_TIMER:
  297. iot_button_resume();
  298. // // wakeup_reason = "timer";
  299. // #if LORA_SLEEP_ENABLE
  300. // // rtc_gpio_pullup_dis(LORA_POWER_PIN);
  301. // // rtc_gpio_deinit(LORA_POWER_PIN);
  302. // // rtc_gpio_init(LORA_POWER_PIN);
  303. // // rtc_gpio_set_direction(LORA_POWER_PIN, RTC_GPIO_MODE_OUTPUT_ONLY);
  304. // // rtc_gpio_pullup_en(LORA_POWER_PIN);
  305. // // rtc_gpio_set_level(LORA_POWER_PIN, 1);
  306. // lora_set_power_level(1); // 打开lora电源
  307. // // rtc_gpio_hold_en(LORA_POWER_PIN);
  308. // #endif
  309. // #if FONT_SLEEP_ENABLE // 字库唤醒恢复供电
  310. // font_exit_sleep(); // 退出睡眠
  311. // #endif
  312. // #if LCD_SLEEP_ENABLE
  313. // #endif
  314. if (is_first_run == false)
  315. {
  316. lora_set_power_level(1); // 打开lora电源
  317. font_exit_sleep(); // 退出睡眠
  318. gpio_hold_dis(LORA_TXD_PIN);
  319. gpio_hold_dis(LORA_RXD_PIN);
  320. // 恢复串口i功能
  321. uart_sleep_out_config();
  322. getRtcTime(&Machine_info);
  323. Machine_info.batt_precent = read_battery_voltage();
  324. Machine_info.wait_send_rssi_bat++;
  325. printf("weakup: bat %d wait_send_rssi_bat = %d,\r\n", Machine_info.batt_precent, Machine_info.wait_send_rssi_bat);
  326. // 打印待发数据
  327. printList(Send_list);
  328. // if(Machine_info.wait_send_rssi_bat>(30*60/TIMER_WAKEUP_TIME_S))//30分钟发一次lora
  329. if (Machine_info.wait_send_rssi_bat > 60) // 30分钟唤醒一次->(30*60/TIMER_WAKEUP_TIME_S) = 60
  330. // if (0)
  331. {
  332. ESP_LOGE(LOG_TAG, "-> 30minite send lora info");
  333. Machine_info.wait_send_rssi_bat = 0;
  334. f_send_get_chart_data();
  335. f_send_lora_rssi(Machine_info.rssi);
  336. f_send_battary_vaule(Machine_info.batt_precent);
  337. // f_send_get_time();
  338. }
  339. if ((Machine_info.last_button.Minute != Machine_info.min) /*||(Machine_info.last_button.Hour != Machine_info.Hour)*/) //
  340. {
  341. if (is_sync_time(&Machine_info)) // 同步到时间
  342. {
  343. ESP_LOGW(LOG_TAG, "update left value min %d,last min %d\r\n", Machine_info.min, Machine_info.last_button.Minute);
  344. Machine_info.last_button.time_min += (Machine_info.min - Machine_info.last_button.Minute);
  345. }
  346. else
  347. {
  348. printf("time not sync\r\n");
  349. }
  350. }
  351. // printf("wake reason: %s, slept for %lld ms\n",
  352. // wakeup_reason,(sleep_afterr_us - sleep_before_us) / 1000);
  353. printf("start timer id = %d,%d\r\n", Machine_info.eflagID, Machine_info.eflagID * TIMER_CAN_SEND_TIME);
  354. // Machine_info.eflagID = 0x01;
  355. // Already_send_timer_stop();
  356. Already_send_timer_start(LORA_POWER_TIME + (Machine_info.eflagID * TIMER_CAN_SEND_TIME));
  357. // sleep_timer_start(700); //进入睡眠
  358. is_first_run = true;
  359. }
  360. break;
  361. case ESP_SLEEP_WAKEUP_GPIO:
  362. lev_2 = gpio_get_level(2);
  363. {
  364. if (Machine_info.power_status == 0) // 关机的情况
  365. {
  366. if (!lev_2) // 充电
  367. {
  368. if (is_first_charge == false)
  369. {
  370. Machine_info.is_charge = true; // 充电中标志
  371. is_first_charge = true;
  372. gpio_hold_dis(PIN_R_CS);
  373. set_screen_dis_info_and_send_queue(true, false, false, false,100);
  374. printf("charge in\r\n");
  375. }
  376. }
  377. else // 未充电
  378. {
  379. if (is_first_charge == true)
  380. {
  381. Machine_info.is_charge = false; // 拔掉充电
  382. is_first_charge = false;
  383. gpio_hold_dis(PIN_R_CS);
  384. set_screen_dis_info_and_send_queue(true, false, false, false,100);
  385. // sleep_timer_start(1000);
  386. // user_compare(last_batt_precent,Machine_info.batt_precent);//刷完左屏,比较一次电量。电量图标不同则刷一次右屏
  387. printf("not charge in or charge out\r\n");
  388. }
  389. }
  390. }
  391. else if (Machine_info.power_status == 1) // 开机的情况
  392. {
  393. if (!lev_2) // 充电
  394. {
  395. if (is_first_charge == false)
  396. {
  397. Machine_info.is_charge = true; // 充电中标志
  398. is_first_charge = true;
  399. right_screen_send();
  400. printf("charge in\r\n");
  401. }
  402. }
  403. else
  404. {
  405. if (is_first_charge == true)
  406. {
  407. Machine_info.is_charge = false; // 拔掉充电
  408. is_first_charge = false;
  409. right_screen_send();
  410. // user_compare(last_batt_precent,Machine_info.batt_precent);//刷完左屏,比较一次电量。电量图标不同则刷一次右屏
  411. printf("not charge in or charge out\r\n");
  412. }
  413. }
  414. }
  415. }
  416. break;
  417. case ESP_SLEEP_WAKEUP_UART:
  418. // wakeup_reason = "uart";
  419. /* Hang-up for a while to switch and execuse the uart task
  420. * Otherwise the chip may fall sleep again before running uart task */
  421. vTaskDelay(1);
  422. break;
  423. case ESP_SLEEP_WAKEUP_ULP:
  424. iot_button_resume();
  425. break;
  426. case ESP_SLEEP_WAKEUP_EXT0:
  427. if (!is_adv)
  428. {
  429. iot_button_resume();
  430. }
  431. if (Machine_info.paired && Machine_info.power_status && !is_key_press())
  432. {
  433. vTaskDelay(2000 / portTICK_PERIOD_MS);
  434. is_sleep = true; // 配对且关机,进入休眠
  435. }
  436. break;
  437. default:
  438. break;
  439. }
  440. /* extern bool is_adv;
  441. if(is_adv)//OTA 模式不检测电量
  442. {
  443. printf("is_adv\r\n");
  444. }
  445. else */
  446. if (Machine_info.power_status == 0) // 关机的情况
  447. {
  448. user_compare_power_off(Machine_info.last_batt_precent, Machine_info.batt_precent);
  449. }
  450. else if (Machine_info.power_status == 1) // 开机的情况
  451. {
  452. if (read_battery_voltage() == 0)
  453. {
  454. printf("-->low batt , power off\r\n");
  455. #if 0
  456. set_screen_dis_info_and_send_queue(false, false, false, false,100);
  457. set_screen_dis_info_and_send_queue(true, false, true, false,100);
  458. #endif
  459. }
  460. lev_2 = gpio_get_level(2);
  461. {
  462. if (Machine_info.power_status == 1) // 开机的情况
  463. {
  464. if (!lev_2) // 充电
  465. {
  466. if (is_first_charge == false)
  467. {
  468. Machine_info.is_charge = true; // 充电中标志
  469. is_first_charge = true;
  470. right_screen_send();
  471. printf("charge in\r\n");
  472. }
  473. }
  474. else
  475. {
  476. if (is_first_charge == true)
  477. {
  478. Machine_info.is_charge = false; // 拔掉充电
  479. is_first_charge = false;
  480. right_screen_send();
  481. // user_compare(last_batt_precent,Machine_info.batt_precent);//刷完左屏,比较一次电量。电量图标不同则刷一次右屏
  482. printf("not charge in or charge out\r\n");
  483. }
  484. }
  485. }
  486. }
  487. bat_times++;
  488. if (bat_times > 30)
  489. {
  490. bat_times = 0;
  491. Machine_info.batt_precent = read_battery_voltage();
  492. printf("compare current bat and last bat refresh display,current = %d,last =%d\r\n", Machine_info.batt_precent, Machine_info.last_batt_precent);
  493. user_compare(Machine_info.last_batt_precent, Machine_info.batt_precent); // 刷完左屏,比较一次电量。电量值不同则刷一次右屏
  494. Machine_info.last_batt_precent = Machine_info.batt_precent;
  495. }
  496. }
  497. vTaskDelay(1000 / portTICK_PERIOD_MS);
  498. }
  499. vTaskDelete(NULL);
  500. }
  501. /*********************************************************************************
  502. * function : app_main
  503. * Description : 初始化任务相关
  504. * Input :
  505. * Output :
  506. * Author : Data : 2023 11.08
  507. **********************************************************************************/
  508. void app_main(void)
  509. {
  510. app_init(); // 用户任务相关
  511. sleep_init(); // 使能协处理器相关
  512. // 创建休眠任务
  513. xTaskCreate(light_sleep_task, "light_sleep_task", 4096 * 5, NULL, 1, NULL);
  514. printf("Machine_info.power_status = %d\r\n", Machine_info.power_status);
  515. if (Machine_info.power_status == 0) // 关机状态
  516. {
  517. sleep_timer_start(5000);
  518. }
  519. }
  520. /*********************************************************************************
  521. * function : init_ulp_program
  522. * Description : 初始化ulp处理器相关
  523. * Input :
  524. * Output :
  525. * Author : Data : 2023 11.08
  526. **********************************************************************************/
  527. static void init_ulp_program(void)
  528. {
  529. ulp_riscv_adc_cfg_t cfg = {
  530. .adc_n = EXAMPLE_ADC_UNIT,
  531. .channel = EXAMPLE_ADC_CHANNEL,
  532. .width = EXAMPLE_ADC_WIDTH,
  533. .atten = EXAMPLE_ADC_ATTEN,
  534. };
  535. extern adc_oneshot_unit_handle_t adc1_handle;
  536. ESP_ERROR_CHECK(adc_oneshot_del_unit(adc1_handle));
  537. #if 0
  538. // #include "user_button.h"
  539. // power_button_deinit();
  540. // power_button_init(adc1_handle);
  541. #endif
  542. ESP_ERROR_CHECK(ulp_riscv_adc_init(&cfg));
  543. esp_err_t err = ulp_riscv_load_binary(ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start));
  544. ESP_ERROR_CHECK(err);
  545. /* The first argument is the period index, which is not used by the ULP-RISC-V timer
  546. * The second argument is the period in microseconds, which gives a wakeup time period of: 20ms
  547. */
  548. #if 0
  549. ulp_set_wakeup_period(0, 20000);
  550. #else
  551. ulp_set_wakeup_period(0, 50000);
  552. #endif
  553. /* Start the program */
  554. /* Start the program */
  555. // ulp_riscv_cfg_t cfg1 = {
  556. // .wakeup_source = ULP_RISCV_WAKEUP_SOURCE_GPIO,
  557. // };
  558. // err = ulp_riscv_config_and_run(&cfg1);
  559. err = ulp_riscv_run();
  560. ESP_ERROR_CHECK(err);
  561. }
  562. void sleep_init()
  563. {
  564. #if 1
  565. // 设置协处理器配置相关adc唤醒相关
  566. esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  567. /* not a wakeup from ULP, load the firmware */
  568. if (((cause != ESP_SLEEP_WAKEUP_ULP) && (cause != ESP_SLEEP_WAKEUP_TIMER)) && (cause != ESP_SLEEP_WAKEUP_EXT0))
  569. {
  570. printf("Not a ULP-RISC-V wakeup (cause = %d), initializing it! \n", cause);
  571. init_ulp_program();
  572. }
  573. /* ULP Risc-V read and detected a temperature above the limit */
  574. if (cause == ESP_SLEEP_WAKEUP_ULP)
  575. {
  576. printf("ULP-RISC-V woke up the main CPU\n");
  577. printf("Threshold: high = %" PRIu32 "\n", ulp_adc_threshold);
  578. printf("Value = %" PRIu32 " was above threshold\n", ulp_wakeup_result);
  579. }
  580. /* Go back to sleep, only the ULP Risc-V will run */
  581. // printf("Entering in sleep\n\n");
  582. /* RTC peripheral power domain needs to be kept on to keep SAR ADC related configs during sleep */
  583. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  584. ESP_ERROR_CHECK(esp_sleep_enable_ulp_wakeup());
  585. #endif
  586. #if 0
  587. /* Initialize GPIO */
  588. gpio_config_t config =
  589. {
  590. .pin_bit_mask = (1ULL << 4),
  591. .mode = GPIO_MODE_INPUT,
  592. .pull_down_en = false,
  593. .pull_up_en = true,
  594. .intr_type = GPIO_INTR_NEGEDGE
  595. };
  596. gpio_config_t charge_config =
  597. {
  598. .pin_bit_mask = (1ULL<<2),
  599. .mode = GPIO_MODE_INPUT,
  600. .pull_down_en = false,
  601. .pull_up_en = true,
  602. .intr_type = GPIO_INTR_LOW_LEVEL
  603. };
  604. if(gpio_config(&config)!=ESP_OK)
  605. {
  606. printf("gpio_config fail\r\n");
  607. }
  608. if(gpio_config(&charge_config)!=ESP_OK)
  609. {
  610. printf("gpio_config fail\r\n");
  611. }
  612. // gpio_hold_en(2);
  613. // gpio_hold_en(38);
  614. //配置下降沿触发
  615. if(gpio_wakeup_enable(4,GPIO_INTR_LOW_LEVEL)!=ESP_OK)
  616. {
  617. printf("gpio_wakeup_enable fail\r\n");
  618. }
  619. if(gpio_wakeup_enable(2,GPIO_INTR_LOW_LEVEL)!=ESP_OK)
  620. {
  621. printf("gpio_wakeup_enable fail\r\n");
  622. }
  623. // if(gpio_wakeup_enable(38,GPIO_INTR_LOW_LEVEL)!=ESP_OK)
  624. // {
  625. // printf("gpio_wakeup_enable fail\r\n");
  626. // }
  627. if(esp_sleep_enable_gpio_wakeup()!=ESP_OK) //使能GPIO唤醒
  628. {
  629. printf("esp_sleep_enable_gpio_wakeup fail\r\n");
  630. }
  631. #else
  632. // rtc_gpio_init(4);
  633. // rtc_gpio_set_direction(4, RTC_GPIO_MODE_INPUT_ONLY);
  634. // rtc_gpio_pullup_en(4);
  635. // rtc_gpio_pulldown_dis(4);
  636. // //配置下降沿触发
  637. // if(gpio_wakeup_enable(4,GPIO_INTR_LOW_LEVEL)!=ESP_OK)
  638. // {
  639. // printf("gpio_wakeup_enable fail\r\n");
  640. // }
  641. // if(esp_sleep_enable_gpio_wakeup()!=ESP_OK) //使能GPIO唤醒
  642. // {
  643. // printf("esp_sleep_enable_gpio_wakeup fail\r\n");
  644. // }
  645. #endif
  646. }