|
38 | 38 | #include <esp_sleep.h> |
39 | 39 | #include <esp_system.h> |
40 | 40 | #include <esp_task_wdt.h> |
| 41 | + |
| 42 | +// driver/gpio.h is required for wakeup from light sleep |
| 43 | +#include <driver/gpio.h> |
41 | 44 | #include <mbedtls/cipher.h> |
42 | 45 | #include <mbedtls/md5.h> |
43 | 46 | #include <mbedtls/sha1.h> |
44 | 47 | #include <mbedtls/sha256.h> |
45 | 48 | #include <mbedtls/sha512.h> |
46 | 49 | #include <soc/soc.h> |
| 50 | + |
47 | 51 | #include <stdlib.h> |
48 | 52 |
|
49 | 53 | // introduced starting with 4.4 |
@@ -340,6 +344,17 @@ static term nif_esp_deep_sleep(Context *ctx, int argc, term argv[]) |
340 | 344 | return OK_ATOM; |
341 | 345 | } |
342 | 346 |
|
| 347 | +static term nif_esp_light_sleep(Context *ctx, int argc, term argv[]) |
| 348 | +{ |
| 349 | + UNUSED(ctx); |
| 350 | + UNUSED(argc); |
| 351 | + UNUSED(argv); |
| 352 | + |
| 353 | + esp_err_t ret = esp_light_sleep_start(); |
| 354 | + |
| 355 | + return (ret == ESP_OK) ? OK_ATOM : ERROR_ATOM; |
| 356 | +} |
| 357 | + |
343 | 358 | #if SOC_PM_SUPPORT_EXT_WAKEUP || SOC_PM_SUPPORT_EXT0_WAKEUP |
344 | 359 | static const char *const sleep_wakeup_ext0_atom = "\x11" "sleep_wakeup_ext0"; |
345 | 360 | #endif |
@@ -513,6 +528,34 @@ static term nif_esp_deep_sleep_enable_gpio_wakeup(Context *ctx, int argc, term a |
513 | 528 | } |
514 | 529 | #endif |
515 | 530 |
|
| 531 | +static term nif_esp_gpio_wakeup_enable(Context *ctx, int argc, term argv[]) |
| 532 | +{ |
| 533 | + UNUSED(ctx); |
| 534 | + UNUSED(argc); |
| 535 | + |
| 536 | + VALIDATE_VALUE(argv[0], term_is_integer); |
| 537 | + if ((argv[1] != LOW_ATOM) && (argv[1] != HIGH_ATOM)) { |
| 538 | + RAISE_ERROR(BADARG_ATOM); |
| 539 | + } |
| 540 | + |
| 541 | + avm_int_t gpio = term_to_int(argv[0]); |
| 542 | + gpio_int_type_t int_type = (argv[1] == LOW_ATOM) ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL; |
| 543 | + |
| 544 | + esp_err_t ret = gpio_wakeup_enable(gpio, int_type); |
| 545 | + |
| 546 | + return (ret == ESP_OK) ? OK_ATOM : ERROR_ATOM; |
| 547 | +} |
| 548 | + |
| 549 | +static term nif_esp_sleep_enable_gpio_wakeup(Context *ctx, int argc, term argv[]) |
| 550 | +{ |
| 551 | + UNUSED(ctx); |
| 552 | + UNUSED(argc); |
| 553 | + UNUSED(argv); |
| 554 | + |
| 555 | + esp_err_t ret = esp_sleep_enable_gpio_wakeup(); |
| 556 | + |
| 557 | + return (ret == ESP_OK) ? OK_ATOM : ERROR_ATOM; |
| 558 | +} |
516 | 559 |
|
517 | 560 | #if SOC_ULP_SUPPORTED |
518 | 561 |
|
@@ -842,6 +885,11 @@ static const struct Nif esp_deep_sleep_nif = |
842 | 885 | .base.type = NIFFunctionType, |
843 | 886 | .nif_ptr = nif_esp_deep_sleep |
844 | 887 | }; |
| 888 | +static const struct Nif esp_light_sleep_nif = |
| 889 | +{ |
| 890 | + .base.type = NIFFunctionType, |
| 891 | + .nif_ptr = nif_esp_light_sleep |
| 892 | +}; |
845 | 893 | static const struct Nif esp_sleep_get_wakeup_cause_nif = |
846 | 894 | { |
847 | 895 | .base.type = NIFFunctionType, |
@@ -880,6 +928,16 @@ static const struct Nif esp_deep_sleep_enable_gpio_wakeup_nif = |
880 | 928 | .nif_ptr = nif_esp_deep_sleep_enable_gpio_wakeup |
881 | 929 | }; |
882 | 930 | #endif |
| 931 | +static const struct Nif esp_gpio_wakeup_enable_nif = |
| 932 | +{ |
| 933 | + .base.type = NIFFunctionType, |
| 934 | + .nif_ptr = nif_esp_gpio_wakeup_enable |
| 935 | +}; |
| 936 | +static const struct Nif esp_sleep_enable_gpio_wakeup_nif = |
| 937 | +{ |
| 938 | + .base.type = NIFFunctionType, |
| 939 | + .nif_ptr = nif_esp_sleep_enable_gpio_wakeup |
| 940 | +}; |
883 | 941 | #if SOC_ULP_SUPPORTED |
884 | 942 | static const struct Nif esp_sleep_ulp_wakeup_nif = |
885 | 943 | { |
@@ -981,6 +1039,10 @@ const struct Nif *platform_nifs_get_nif(const char *nifname) |
981 | 1039 | TRACE("Resolved platform nif %s ...\n", nifname); |
982 | 1040 | return &esp_deep_sleep_nif; |
983 | 1041 | } |
| 1042 | + if (strcmp("esp:light_sleep/0", nifname) == 0) { |
| 1043 | + TRACE("Resolved platform nif %s ...\n", nifname); |
| 1044 | + return &esp_light_sleep_nif; |
| 1045 | + } |
984 | 1046 | if (strcmp("esp:sleep_get_wakeup_cause/0", nifname) == 0) { |
985 | 1047 | TRACE("Resolved platform nif %s ...\n", nifname); |
986 | 1048 | return &esp_sleep_get_wakeup_cause_nif; |
@@ -1013,6 +1075,14 @@ const struct Nif *platform_nifs_get_nif(const char *nifname) |
1013 | 1075 | return &esp_deep_sleep_enable_gpio_wakeup_nif; |
1014 | 1076 | } |
1015 | 1077 | #endif |
| 1078 | + if (strcmp("esp:gpio_wakeup_enable/2", nifname) == 0) { |
| 1079 | + TRACE("Resolved platform nif %s ...\n", nifname); |
| 1080 | + return &esp_gpio_wakeup_enable_nif; |
| 1081 | + } |
| 1082 | + if (strcmp("esp:sleep_enable_gpio_wakeup/0", nifname) == 0) { |
| 1083 | + TRACE("Resolved platform nif %s ...\n", nifname); |
| 1084 | + return &esp_sleep_enable_gpio_wakeup_nif; |
| 1085 | + } |
1016 | 1086 | #if SOC_ULP_SUPPORTED |
1017 | 1087 | if (strcmp("esp:sleep_ulp_wakeup/0", nifname) == 0) { |
1018 | 1088 | TRACE("Resolved platform nif %s ...\n", nifname); |
|
0 commit comments