services.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. } sleep_config;
  45. static EXT_RAM_ATTR void (*sleep_hooks[16])(void);
  46. static const char *TAG = "services";
  47. /****************************************************************************************
  48. *
  49. */
  50. void set_chip_power_gpio(int gpio, char *value) {
  51. bool parsed = true;
  52. // we only parse on-chip GPIOs
  53. if (gpio >= GPIO_NUM_MAX) return;
  54. if (!strcasecmp(value, "vcc") ) {
  55. gpio_pad_select_gpio(gpio);
  56. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  57. gpio_set_level(gpio, 1);
  58. } else if (!strcasecmp(value, "gnd")) {
  59. gpio_pad_select_gpio(gpio);
  60. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  61. gpio_set_level(gpio, 0);
  62. } else parsed = false;
  63. if (parsed) ESP_LOGI(TAG, "set GPIO %u to %s", gpio, value);
  64. }
  65. /****************************************************************************************
  66. *
  67. */
  68. void set_exp_power_gpio(int gpio, char *value) {
  69. bool parsed = true;
  70. // we only parse on-chip GPIOs
  71. if (gpio < GPIO_NUM_MAX) return;
  72. if (!strcasecmp(value, "vcc") ) {
  73. gpio_exp_set_direction(gpio, GPIO_MODE_OUTPUT, NULL);
  74. gpio_exp_set_level(gpio, 1, true, NULL);
  75. } else if (!strcasecmp(value, "gnd")) {
  76. gpio_exp_set_direction(gpio, GPIO_MODE_OUTPUT, NULL);
  77. gpio_exp_set_level(gpio, 0, true, NULL);
  78. } else parsed = false;
  79. if (parsed) ESP_LOGI(TAG, "set expanded GPIO %u to %s", gpio, value);
  80. }
  81. /****************************************************************************************
  82. *
  83. */
  84. static void sleep_gpio_handler(void *id, button_event_e event, button_press_e mode, bool long_press) {
  85. if (event == BUTTON_PRESSED) services_sleep_activate(SLEEP_ONGPIO);
  86. }
  87. /****************************************************************************************
  88. *
  89. */
  90. static void sleep_init(void) {
  91. char *config = config_alloc_get(NVS_TYPE_STR, "sleep_config");
  92. char *p;
  93. // do we want delay sleep
  94. PARSE_PARAM(config, "delay", '=', sleep_config.delay);
  95. sleep_config.delay *= 60*1000;
  96. if (sleep_config.delay) {
  97. ESP_LOGI(TAG, "Sleep inactivity of %d minute(s)", sleep_config.delay / (60*1000));
  98. }
  99. // get the wake criteria
  100. if ((p = strcasestr(config, "wake"))) {
  101. char list[32] = "", item[8];
  102. sscanf(p, "%*[^=]=%31[^,]", list);
  103. p = list - 1;
  104. while (p++ && sscanf(p, "%7[^|]", item)) {
  105. int level = 0, gpio = atoi(item);
  106. if (!rtc_gpio_is_valid_gpio(gpio)) {
  107. ESP_LOGE(TAG, "invalid wake GPIO %d (not in RTC domain)", gpio);
  108. } else {
  109. sleep_config.wake_gpio |= 1LL << gpio;
  110. }
  111. if (sscanf(item, "%*[^:]:%d", &level)) sleep_config.wake_level |= level << gpio;
  112. p = strchr(p, '|');
  113. }
  114. // when moving to esp-idf more recent than 4.4.x, multiple gpio wake-up with level specific can be done
  115. if (sleep_config.wake_gpio) {
  116. ESP_LOGI(TAG, "Sleep wake-up gpio bitmap 0x%llx (active 0x%llx)", sleep_config.wake_gpio, sleep_config.wake_level);
  117. }
  118. }
  119. // get the rtc-pull criteria
  120. if ((p = strcasestr(config, "rtc"))) {
  121. char list[32] = "", item[8];
  122. sscanf(p, "%*[^=]=%31[^,]", list);
  123. p = list - 1;
  124. while (p++ && sscanf(p, "%7[^|]", item)) {
  125. int level = 0, gpio = atoi(item);
  126. if (!rtc_gpio_is_valid_gpio(gpio)) {
  127. ESP_LOGE(TAG, "invalid rtc GPIO %d", gpio);
  128. } else {
  129. sleep_config.rtc_gpio |= 1LL << gpio;
  130. }
  131. if (sscanf(item, "%*[^:]:%d", &level)) sleep_config.rtc_level |= level << gpio;
  132. p = strchr(p, '|');
  133. }
  134. // when moving to esp-idf more recent than 4.4.x, multiple gpio wake-up with level specific can be done
  135. if (sleep_config.rtc_gpio) {
  136. ESP_LOGI(TAG, "RTC forced gpio bitmap 0x%llx (active 0x%llx)", sleep_config.rtc_gpio, sleep_config.rtc_level);
  137. }
  138. }
  139. // then get the gpio that activate sleep (we could check that we have a valid wake)
  140. if ((p = strcasestr(config, "sleep"))) {
  141. int gpio, level = 0;
  142. char sleep[8] = "";
  143. sscanf(p, "%*[^=]=%7[^,]", sleep);
  144. gpio = atoi(sleep);
  145. if ((p = strchr(sleep, ':')) != NULL) level = atoi(p + 1);
  146. ESP_LOGI(TAG, "Sleep activation gpio %d (active %d)", gpio, level);
  147. button_create(NULL, gpio, level ? BUTTON_HIGH : BUTTON_LOW, true, 0, sleep_gpio_handler, 0, -1);
  148. }
  149. }
  150. /****************************************************************************************
  151. *
  152. */
  153. void services_sleep_callback(uint32_t elapsed) {
  154. if (sleep_config.delay && elapsed >= sleep_config.delay) {
  155. services_sleep_activate(SLEEP_ONTIMER);
  156. }
  157. }
  158. /****************************************************************************************
  159. *
  160. */
  161. void services_sleep_activate(sleep_cause_e cause) {
  162. // call all sleep hooks that might want to do something
  163. for (void (**hook)(void) = sleep_hooks; *hook; hook++) (*hook)();
  164. // isolate all possible GPIOs, except the wake-up and RTC-maintaines ones
  165. esp_sleep_config_gpio_isolate();
  166. // keep RTC domain up if we need to maintain pull-up/down of some GPIO from RTC
  167. if (sleep_config.rtc_gpio) esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  168. for (int i = 0; i < GPIO_NUM_MAX; i++) {
  169. // must be a RTC GPIO
  170. if (!rtc_gpio_is_valid_gpio(i)) continue;
  171. // do we need to maintain a pull-up or down of that GPIO
  172. if ((1LL << i) & sleep_config.rtc_gpio) {
  173. if ((sleep_config.rtc_level >> i) & 0x01) rtc_gpio_pullup_en(i);
  174. else rtc_gpio_pulldown_en(i);
  175. // or is this not wake-up GPIO, just isolate it
  176. } else if (!((1LL << i) & sleep_config.wake_gpio)) {
  177. rtc_gpio_isolate(i);
  178. }
  179. }
  180. // is there just one GPIO
  181. if (sleep_config.wake_gpio & (sleep_config.wake_gpio - 1)) {
  182. ESP_LOGI(TAG, "going to sleep cause %d, wake-up on multiple GPIO, any '1' wakes up 0x%llx", cause, sleep_config.wake_gpio);
  183. esp_sleep_enable_ext1_wakeup(sleep_config.wake_gpio, ESP_EXT1_WAKEUP_ANY_HIGH);
  184. } else if (sleep_config.wake_gpio) {
  185. int gpio = __builtin_ctz(sleep_config.wake_gpio);
  186. int level = (sleep_config.wake_level >> gpio) & 0x01;
  187. ESP_LOGI(TAG, "going to sleep cause %d, wake-up on GPIO %d level %d", cause, gpio, level);
  188. esp_sleep_enable_ext0_wakeup(gpio, level);
  189. } else {
  190. ESP_LOGW(TAG, "going to sleep cause %d, no wake-up option", cause);
  191. }
  192. // 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
  193. if (cause == SLEEP_ONKEY) xTimerStart(xTimerCreate("sleepTimer", pdMS_TO_TICKS(1000), pdFALSE, NULL, (void (*)(void*)) esp_deep_sleep_start), 0);
  194. else esp_deep_sleep_start();
  195. }
  196. /****************************************************************************************
  197. *
  198. */
  199. void services_sleep_sethook(void (*hook)(void)) {
  200. for (int i = 0; i < sizeof(sleep_hooks)/sizeof(void(*)(void)); i++) {
  201. if (!sleep_hooks[i]) {
  202. sleep_hooks[i] = hook;
  203. return;
  204. }
  205. }
  206. }
  207. /****************************************************************************************
  208. *
  209. */
  210. void services_init(void) {
  211. messaging_service_init();
  212. gpio_install_isr_service(0);
  213. #ifdef CONFIG_I2C_LOCKED
  214. if (i2c_system_port == 0) {
  215. i2c_system_port = 1;
  216. ESP_LOGE(TAG, "Port 0 is reserved for internal DAC use");
  217. }
  218. #endif
  219. // set potential power GPIO on chip first in case expanders are power using these
  220. parse_set_GPIO(set_chip_power_gpio);
  221. // shared I2C bus
  222. const i2c_config_t * i2c_config = config_i2c_get(&i2c_system_port);
  223. 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);
  224. if (i2c_config->sda_io_num != -1 && i2c_config->scl_io_num != -1) {
  225. i2c_param_config(i2c_system_port, i2c_config);
  226. i2c_driver_install(i2c_system_port, i2c_config->mode, 0, 0, 0 );
  227. } else {
  228. i2c_system_port = -1;
  229. ESP_LOGW(TAG, "no I2C configured");
  230. }
  231. const spi_bus_config_t * spi_config = config_spi_get((spi_host_device_t*) &spi_system_host);
  232. 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);
  233. if (spi_config->mosi_io_num != -1 && spi_config->sclk_io_num != -1) {
  234. spi_bus_initialize( spi_system_host, spi_config, 1 );
  235. if (spi_system_dc_gpio != -1) {
  236. gpio_reset_pin(spi_system_dc_gpio);
  237. gpio_set_direction( spi_system_dc_gpio, GPIO_MODE_OUTPUT );
  238. gpio_set_level( spi_system_dc_gpio, 0 );
  239. } else {
  240. ESP_LOGW(TAG, "No DC GPIO set, SPI display will not work");
  241. }
  242. } else {
  243. spi_system_host = -1;
  244. ESP_LOGW(TAG, "no SPI configured");
  245. }
  246. // create GPIO expanders
  247. const gpio_exp_config_t* gpio_exp_config;
  248. for (int count = 0; (gpio_exp_config = config_gpio_exp_get(count)); count++) gpio_exp_create(gpio_exp_config);
  249. // now set potential power GPIO on expander
  250. parse_set_GPIO(set_exp_power_gpio);
  251. // system-wide PWM timer configuration
  252. ledc_timer_config_t pwm_timer = {
  253. .duty_resolution = LEDC_TIMER_13_BIT,
  254. .freq_hz = 5000,
  255. #ifdef CONFIG_IDF_TARGET_ESP32S3
  256. .speed_mode = LEDC_LOW_SPEED_MODE,
  257. #else
  258. .speed_mode = LEDC_HIGH_SPEED_MODE,
  259. #endif
  260. .timer_num = pwm_system.timer,
  261. };
  262. ledc_timer_config(&pwm_timer);
  263. led_svc_init();
  264. battery_svc_init();
  265. monitor_svc_init();
  266. sleep_init();
  267. }