Skip to content

Commit

Permalink
Move GPIO specific atoms from ESP32 platform_defaultatoms.def to gpio…
Browse files Browse the repository at this point in the history
…_driver.c

Moves the atoms only needed by the GPIO driver out of ESP32 platform_defaultatoms.def and into the
gpio driver, where they are created as needed at runtime.

Signed-off-by: Winford <[email protected]>
  • Loading branch information
UncleGrumpy committed Dec 27, 2024
1 parent 3220e39 commit 5ffba7c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 44 deletions.
64 changes: 28 additions & 36 deletions src/platforms/esp32/components/avm_builtins/gpio_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "mailbox.h"
#include "module.h"
#include "nifs.h"
#include "platform_defaultatoms.h"
#include "port.h"
#include "scheduler.h"
#include "term.h"
Expand Down Expand Up @@ -69,10 +68,14 @@ static Context *gpio_driver_create_port(GlobalContext *global, term opts);
#endif

#ifdef CONFIG_AVM_ENABLE_GPIO_PORT_DRIVER
static const char *const gpio_atom = "\x4" "gpio";
static const char *const gpio_driver_atom = "\xB" "gpio_driver";
static const char *const gpio_atom = ATOM_STR("\x4", "gpio");
static const char *const gpio_driver_atom = ATOM_STR("\xB", "gpio_driver");
static const char *const gpio_interrupt_atom = ATOM_STR("\xE", "gpio_interrupt");
#endif

static const char *const high_atom = ATOM_STR("\x4", "high");
static const char *const low_atom = ATOM_STR("\x3", "low");

static const AtomStringIntPair pin_mode_table[] = {
{ ATOM_STR("\x5", "input"), GPIO_MODE_INPUT },
{ ATOM_STR("\x6", "output"), GPIO_MODE_OUTPUT },
Expand Down Expand Up @@ -101,6 +104,16 @@ static const AtomStringIntPair pin_level_table[] = {
SELECT_INT_DEFAULT(GPIOPinInvalid)
};

static const AtomStringIntPair int_trigger_table[] = {
{ ATOM_STR("\x4", "none"), GPIO_INTR_DISABLE },
{ ATOM_STR("\x6", "rising"), GPIO_INTR_POSEDGE },
{ ATOM_STR("\x7", "falling"), GPIO_INTR_NEGEDGE },
{ ATOM_STR("\x4", "both"), GPIO_INTR_ANYEDGE },
{ ATOM_STR("\x3", "low"), GPIO_INTR_LOW_LEVEL },
{ ATOM_STR("\x4", "high"), GPIO_INTR_HIGH_LEVEL },
SELECT_INT_DEFAULT(GPIO_INTR_MAX)
};

enum gpio_cmd
{
GPIOInvalidCmd = 0,
Expand Down Expand Up @@ -286,7 +299,7 @@ static inline term gpio_digital_read(term gpio_num_term)

avm_int_t level = gpio_get_level(gpio_num);

return level ? HIGH_ATOM : LOW_ATOM;
return level ? globalcontext_make_atom(glb, high_atom) : globalcontext_make_atom(glb, low_atom);
}

#ifdef CONFIG_AVM_ENABLE_GPIO_PORT_DRIVER
Expand Down Expand Up @@ -371,7 +384,7 @@ EventListener *gpio_interrupt_callback(GlobalContext *glb, EventListener *listen
BEGIN_WITH_STACK_HEAP(1 + 2, heap);

term int_msg = term_alloc_tuple(2, &heap);
term_put_tuple_element(int_msg, 0, GPIO_INTERRUPT_ATOM);
term_put_tuple_element(int_msg, 0, globalcontext_make_atom(glb, gpio_interrupt_atom));
term_put_tuple_element(int_msg, 1, term_from_int32(gpio_num));

globalcontext_send_message(glb, listening_pid, int_msg);
Expand Down Expand Up @@ -482,36 +495,9 @@ static term gpiodriver_set_int(Context *ctx, int32_t target_pid, term cmd)
target_local_pid = target_pid;
}


/* TODO: GPIO specific atoms should be removed from platform_defaultatoms and constructed within this driver */
gpio_int_type_t interrupt_type;
switch (trigger) {
case NONE_ATOM:
interrupt_type = GPIO_INTR_DISABLE;
break;

case RISING_ATOM:
interrupt_type = GPIO_INTR_POSEDGE;
break;

case FALLING_ATOM:
interrupt_type = GPIO_INTR_NEGEDGE;
break;

case BOTH_ATOM:
interrupt_type = GPIO_INTR_ANYEDGE;
break;

case LOW_ATOM:
interrupt_type = GPIO_INTR_LOW_LEVEL;
break;

case HIGH_ATOM:
interrupt_type = GPIO_INTR_HIGH_LEVEL;
break;

default:
return ERROR_ATOM;
gpio_int_type_t interrupt_type = interop_atom_term_select_int(int_trigger_table, trigger, ctx->global);
if(UNLIKELY(interrupt_type == GPIO_INTR_MAX)) {
return BADARG_ATOM;
}

if (trigger != NONE_ATOM) {
Expand Down Expand Up @@ -570,6 +556,7 @@ static term gpiodriver_remove_int(Context *ctx, term cmd)
return ERROR_ATOM;
}


return unregister_interrupt_listener(ctx, gpio_num);
}

Expand Down Expand Up @@ -749,7 +736,12 @@ static term nif_gpio_digital_write(Context *ctx, int argc, term argv[])

static term nif_gpio_digital_read(Context *ctx, int argc, term argv[])
{
return gpio_digital_read(argv[0]);
GlobalContext glb = ctx->global; // needed for HIGH_ATOM & LOW_ATOM macros
term result = gpio_digital_read(ctx, argv[0]);
if (UNLIKELY((result != HIGH_ATOM) && (result != LOW_ATOM))) {
RAISE_ERROR(result);
}
return result;
}

static const struct Nif gpio_init_nif =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/

X(READ_ATOM, "\x4", "read")
X(GPIO_INTERRUPT_ATOM, "\xE", "gpio_interrupt")
X(RISING_ATOM, "\x6", "rising")
X(FALLING_ATOM, "\x7", "falling")
X(BOTH_ATOM, "\x4", "both")
X(LOW_ATOM, "\x3", "low")
X(HIGH_ATOM, "\x4", "high")

X(ESP32_ATOM, "\x5", "esp32")

X(PROTO_ATOM, "\x5", "proto")
Expand Down

0 comments on commit 5ffba7c

Please sign in to comment.