services.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/timers.h"
  10. #include "esp_log.h"
  11. #include "esp_sleep.h"
  12. #include "driver/rtc_io.h"
  13. #include "driver/gpio.h"
  14. #include "driver/ledc.h"
  15. #include "driver/i2c.h"
  16. #include "driver/rmt.h"
  17. #include "platform_config.h"
  18. #include "gpio_exp.h"
  19. #include "battery.h"
  20. #include "led.h"
  21. #include "monitor.h"
  22. #include "globdefs.h"
  23. #include "accessors.h"
  24. #include "messaging.h"
  25. #include "buttons.h"
  26. #include "services.h"
  27. extern void battery_svc_init(void);
  28. extern void monitor_svc_init(void);
  29. extern void led_svc_init(void);
  30. int i2c_system_port = I2C_SYSTEM_PORT;
  31. int i2c_system_speed = 400000;
  32. int spi_system_host = SPI_SYSTEM_HOST;
  33. int spi_system_dc_gpio = -1;
  34. int rmt_system_base_channel = RMT_CHANNEL_0;
  35. pwm_system_t pwm_system = {
  36. .timer = LEDC_TIMER_0,
  37. .base_channel = LEDC_CHANNEL_0,
  38. .max = (1 << LEDC_TIMER_13_BIT),
  39. };
  40. static EXT_RAM_ATTR struct {
  41. uint64_t wake_gpio, wake_level;
  42. uint64_t rtc_gpio, rtc_level;
  43. uint32_t delay;
  44. float battery_level;
  45. int battery_count;
  46. void (*idle_chain)(uint32_t now);
  47. void (*battery_chain)(float level, int cells);
  48. void (*suspend[10])(void);
  49. uint32_t (*sleeper[10])(void);
  50. } sleep_context;
  51. static const char *TAG = "services";
  52. /****************************************************************************************
  53. *
  54. */
  55. void set_chip_power_gpio(int gpio, char *value) {
  56. bool parsed = true;
  57. // we only parse on-chip GPIOs
  58. if (gpio >= GPIO_NUM_MAX) return;
  59. if (!strcasecmp(value, "vcc") ) {
  60. gpio_pad_select_gpio(gpio);
  61. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  62. gpio_set_level(gpio, 1);
  63. } else if (!strcasecmp(value, "gnd")) {
  64. gpio_pad_select_gpio(gpio);
  65. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  66. gpio_set_level(gpio, 0);
  67. } else parsed = false;
  68. if (parsed) ESP_LOGI(TAG, "set GPIO %u to %s", gpio, value);
  69. }
  70. /****************************************************************************************
  71. *
  72. */
  73. void set_exp_power_gpio(int gpio, char *value) {
  74. bool parsed = true;
  75. // we only parse on-chip GPIOs
  76. if (gpio < GPIO_NUM_MAX) return;
  77. if (!strcasecmp(value, "vcc") ) {
  78. gpio_exp_set_direction(gpio, GPIO_MODE_OUTPUT, NULL);
  79. gpio_exp_set_level(gpio, 1, true, NULL);
  80. } else if (!strcasecmp(value, "gnd")) {
  81. gpio_exp_set_direction(gpio, GPIO_MODE_OUTPUT, NULL);
  82. gpio_exp_set_level(gpio, 0, true, NULL);
  83. } else parsed = false;
  84. if (parsed) ESP_LOGI(TAG, "set expanded GPIO %u to %s", gpio, value);
  85. }
  86. /****************************************************************************************
  87. *
  88. */
  89. static void sleep_gpio_handler(void *id, button_event_e event, button_press_e mode, bool long_press) {
  90. if (event == BUTTON_PRESSED) services_sleep_activate(SLEEP_ONGPIO);
  91. }
  92. /****************************************************************************************
  93. *
  94. */
  95. static void sleep_timer(uint32_t now) {
  96. static uint32_t last;
  97. // first chain the calls to psudo_idle function
  98. if (sleep_context.idle_chain) sleep_context.idle_chain(now);
  99. // only query callbacks every 30s if we have at least one sleeper
  100. if (!*sleep_context.sleeper || now < last + 30*1000) return;
  101. last = now;
  102. // call all sleep hooks that might want to do something
  103. for (uint32_t (**sleeper)(void) = sleep_context.sleeper; *sleeper; sleeper++) {
  104. if ((*sleeper)() < sleep_context.delay) return;
  105. }
  106. // if we are here, we are ready to sleep;
  107. services_sleep_activate(SLEEP_ONTIMER);
  108. }
  109. /****************************************************************************************
  110. *
  111. */
  112. static void sleep_battery(float level, int cells) {
  113. // chain if any
  114. if (sleep_context.battery_chain) sleep_context.battery_chain(level, cells);
  115. // then assess if we have to stop because of low batt
  116. if (level < sleep_context.battery_level) {
  117. if (sleep_context.battery_count++ == 2) services_sleep_activate(SLEEP_ONBATTERY);
  118. } else {
  119. sleep_context.battery_count = 0;
  120. }
  121. }
  122. /****************************************************************************************
  123. *
  124. */
  125. static void sleep_init(void) {
  126. char *config = config_alloc_get(NVS_TYPE_STR, "sleep_config");
  127. char *p;
  128. // do we want delay sleep
  129. PARSE_PARAM(config, "delay", '=', sleep_context.delay);
  130. sleep_context.delay *= 60*1000;
  131. if (sleep_context.delay) {
  132. sleep_context.idle_chain = pseudo_idle_svc;
  133. pseudo_idle_svc = sleep_timer;
  134. ESP_LOGI(TAG, "Sleep inactivity of %d minute(s)", sleep_context.delay / (60*1000));
  135. }
  136. // do we want battery safety
  137. PARSE_PARAM_FLOAT(config, "batt", '=', sleep_context.battery_level);
  138. if (sleep_context.battery_level != 0.0) {
  139. sleep_context.battery_chain = battery_handler_svc;
  140. battery_handler_svc = sleep_battery;
  141. ESP_LOGI(TAG, "Sleep on battery level of %.2f", sleep_context.battery_level);
  142. }
  143. // get the wake criteria
  144. if ((p = strcasestr(config, "wake"))) {
  145. char list[32] = "", item[8];
  146. sscanf(p, "%*[^=]=%31[^,]", list);
  147. p = list - 1;
  148. while (p++ && sscanf(p, "%7[^|]", item)) {
  149. int level = 0, gpio = atoi(item);
  150. if (!rtc_gpio_is_valid_gpio(gpio)) {
  151. ESP_LOGE(TAG, "invalid wake GPIO %d (not in RTC domain)", gpio);
  152. } else {
  153. sleep_context.wake_gpio |= 1LL << gpio;
  154. }
  155. if (sscanf(item, "%*[^:]:%d", &level)) sleep_context.wake_level |= level << gpio;
  156. p = strchr(p, '|');
  157. }
  158. // when moving to esp-idf more recent than 4.4.x, multiple gpio wake-up with level specific can be done
  159. if (sleep_context.wake_gpio) {
  160. ESP_LOGI(TAG, "Sleep wake-up gpio bitmap 0x%llx (active 0x%llx)", sleep_context.wake_gpio, sleep_context.wake_level);
  161. }
  162. }
  163. // get the rtc-pull criteria
  164. if ((p = strcasestr(config, "rtc"))) {
  165. char list[32] = "", item[8];
  166. sscanf(p, "%*[^=]=%31[^,]", list);
  167. p = list - 1;
  168. while (p++ && sscanf(p, "%7[^|]", item)) {
  169. int level = 0, gpio = atoi(item);
  170. if (!rtc_gpio_is_valid_gpio(gpio)) {
  171. ESP_LOGE(TAG, "invalid rtc GPIO %d", gpio);
  172. } else {
  173. sleep_context.rtc_gpio |= 1LL << gpio;
  174. }
  175. if (sscanf(item, "%*[^:]:%d", &level)) sleep_context.rtc_level |= level << gpio;
  176. p = strchr(p, '|');
  177. }
  178. // when moving to esp-idf more recent than 4.4.x, multiple gpio wake-up with level specific can be done
  179. if (sleep_context.rtc_gpio) {
  180. ESP_LOGI(TAG, "RTC forced gpio bitmap 0x%llx (active 0x%llx)", sleep_context.rtc_gpio, sleep_context.rtc_level);
  181. }
  182. }
  183. // then get the gpio that activate sleep (we could check that we have a valid wake)
  184. if ((p = strcasestr(config, "sleep"))) {
  185. int gpio, level = 0;
  186. char sleep[8] = "";
  187. sscanf(p, "%*[^=]=%7[^,]", sleep);
  188. gpio = atoi(sleep);
  189. if ((p = strchr(sleep, ':')) != NULL) level = atoi(p + 1);
  190. ESP_LOGI(TAG, "Sleep activation gpio %d (active %d)", gpio, level);
  191. button_create(NULL, gpio, level ? BUTTON_HIGH : BUTTON_LOW, true, 0, sleep_gpio_handler, 0, -1);
  192. }
  193. }
  194. /****************************************************************************************
  195. *
  196. */
  197. void services_sleep_activate(sleep_cause_e cause) {
  198. // call all sleep hooks that might want to do something
  199. for (void (**suspend)(void) = sleep_context.suspend; *suspend; suspend++) (*suspend)();
  200. // isolate all possible GPIOs, except the wake-up and RTC-maintaines ones
  201. esp_sleep_config_gpio_isolate();
  202. // keep RTC domain up if we need to maintain pull-up/down of some GPIO from RTC
  203. if (sleep_context.rtc_gpio) esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  204. for (int i = 0; i < GPIO_NUM_MAX; i++) {
  205. // must be a RTC GPIO
  206. if (!rtc_gpio_is_valid_gpio(i)) continue;
  207. // do we need to maintain a pull-up or down of that GPIO
  208. if ((1LL << i) & sleep_context.rtc_gpio) {
  209. if ((sleep_context.rtc_level >> i) & 0x01) rtc_gpio_pullup_en(i);
  210. else rtc_gpio_pulldown_en(i);
  211. // or is this not wake-up GPIO, just isolate it
  212. } else if (!((1LL << i) & sleep_context.wake_gpio)) {
  213. rtc_gpio_isolate(i);
  214. }
  215. }
  216. // is there just one GPIO
  217. if (sleep_context.wake_gpio & (sleep_context.wake_gpio - 1)) {
  218. ESP_LOGI(TAG, "going to sleep cause %d, wake-up on multiple GPIO, any '1' wakes up 0x%llx", cause, sleep_context.wake_gpio);
  219. #if defined(CONFIG_IDF_TARGET_ESP32S3) && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
  220. if (!sleep_context.wake_level) esp_sleep_enable_ext1_wakeup(sleep_context.wake_gpio, ESP_EXT1_WAKEUP_ANY_LOW);
  221. else
  222. #endif
  223. esp_sleep_enable_ext1_wakeup(sleep_context.wake_gpio, ESP_EXT1_WAKEUP_ANY_HIGH);
  224. } else if (sleep_context.wake_gpio) {
  225. int gpio = __builtin_ctz(sleep_context.wake_gpio);
  226. int level = (sleep_context.wake_level >> gpio) & 0x01;
  227. ESP_LOGI(TAG, "going to sleep cause %d, wake-up on GPIO %d level %d", cause, gpio, level);
  228. esp_sleep_enable_ext0_wakeup(gpio, level);
  229. } else {
  230. ESP_LOGW(TAG, "going to sleep cause %d, no wake-up option", cause);
  231. }
  232. // we need to use a timer in case the same button is used for sleep and wake-up and it's "pressed" vs "released" selected
  233. if (cause == SLEEP_ONKEY) xTimerStart(xTimerCreate("sleepTimer", pdMS_TO_TICKS(1000), pdFALSE, NULL, (void (*)(void*)) esp_deep_sleep_start), 0);
  234. else esp_deep_sleep_start();
  235. }
  236. /****************************************************************************************
  237. *
  238. */
  239. static void register_method(void **store, size_t size, void *method) {
  240. for (int i = 0; i < size; i++, *store++) if (!*store) {
  241. *store = method;
  242. return;
  243. }
  244. }
  245. /****************************************************************************************
  246. *
  247. */
  248. void services_sleep_setsuspend(void (*hook)(void)) {
  249. register_method((void**) sleep_context.suspend, sizeof(sleep_context.suspend)/sizeof(*sleep_context.suspend), (void*) hook);
  250. }
  251. /****************************************************************************************
  252. *
  253. */
  254. void services_sleep_setsleeper(uint32_t (*sleeper)(void)) {
  255. register_method((void**) sleep_context.sleeper, sizeof(sleep_context.sleeper)/sizeof(*sleep_context.sleeper), (void*) sleeper);
  256. }
  257. /****************************************************************************************
  258. *
  259. */
  260. void services_init(void) {
  261. messaging_service_init();
  262. gpio_install_isr_service(0);
  263. #ifdef CONFIG_I2C_LOCKED
  264. if (i2c_system_port == 0) {
  265. i2c_system_port = 1;
  266. ESP_LOGE(TAG, "Port 0 is reserved for internal DAC use");
  267. }
  268. #endif
  269. // set potential power GPIO on chip first in case expanders are power using these
  270. parse_set_GPIO(set_chip_power_gpio);
  271. // shared I2C bus
  272. const i2c_config_t * i2c_config = config_i2c_get(&i2c_system_port);
  273. ESP_LOGI(TAG,"Configuring I2C sda:%d scl:%d port:%u speed:%u", i2c_config->sda_io_num, i2c_config->scl_io_num, i2c_system_port, i2c_config->master.clk_speed);
  274. if (i2c_config->sda_io_num != -1 && i2c_config->scl_io_num != -1) {
  275. i2c_param_config(i2c_system_port, i2c_config);
  276. i2c_driver_install(i2c_system_port, i2c_config->mode, 0, 0, 0 );
  277. } else {
  278. i2c_system_port = -1;
  279. ESP_LOGW(TAG, "no I2C configured");
  280. }
  281. const spi_bus_config_t * spi_config = config_spi_get((spi_host_device_t*) &spi_system_host);
  282. ESP_LOGI(TAG,"Configuring SPI mosi:%d miso:%d clk:%d host:%u dc:%d", spi_config->mosi_io_num, spi_config->miso_io_num, spi_config->sclk_io_num, spi_system_host, spi_system_dc_gpio);
  283. if (spi_config->mosi_io_num != -1 && spi_config->sclk_io_num != -1) {
  284. spi_bus_initialize( spi_system_host, spi_config, 1 );
  285. if (spi_system_dc_gpio != -1) {
  286. gpio_reset_pin(spi_system_dc_gpio);
  287. gpio_set_direction( spi_system_dc_gpio, GPIO_MODE_OUTPUT );
  288. gpio_set_level( spi_system_dc_gpio, 0 );
  289. } else {
  290. ESP_LOGW(TAG, "No DC GPIO set, SPI display will not work");
  291. }
  292. } else {
  293. spi_system_host = -1;
  294. ESP_LOGW(TAG, "no SPI configured");
  295. }
  296. // create GPIO expanders
  297. const gpio_exp_config_t* gpio_exp_config;
  298. for (int count = 0; (gpio_exp_config = config_gpio_exp_get(count)); count++) gpio_exp_create(gpio_exp_config);
  299. // now set potential power GPIO on expander
  300. parse_set_GPIO(set_exp_power_gpio);
  301. // system-wide PWM timer configuration
  302. ledc_timer_config_t pwm_timer = {
  303. .duty_resolution = LEDC_TIMER_13_BIT,
  304. .freq_hz = 5000,
  305. #ifdef CONFIG_IDF_TARGET_ESP32S3
  306. .speed_mode = LEDC_LOW_SPEED_MODE,
  307. #else
  308. .speed_mode = LEDC_HIGH_SPEED_MODE,
  309. #endif
  310. .timer_num = pwm_system.timer,
  311. };
  312. ledc_timer_config(&pwm_timer);
  313. led_svc_init();
  314. battery_svc_init();
  315. monitor_svc_init();
  316. sleep_init();
  317. }