ulp_riscv_adc_example_main.c 28 KB

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