ulp_riscv_adc_example_main.c 34 KB

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