ulp_riscv_adc_example_main.c 25 KB

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