Skip to content

Commit 1d010ab

Browse files
committed
Move GPIO specific atoms from ESP32 platform_defaultatoms.def to gpio_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]>
1 parent 3220e39 commit 1d010ab

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

src/platforms/esp32/components/avm_builtins/gpio_driver.c

+22-35
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#include "mailbox.h"
4040
#include "module.h"
4141
#include "nifs.h"
42-
#include "platform_defaultatoms.h"
4342
#include "port.h"
4443
#include "scheduler.h"
4544
#include "term.h"
@@ -69,10 +68,14 @@ static Context *gpio_driver_create_port(GlobalContext *global, term opts);
6968
#endif
7069

7170
#ifdef CONFIG_AVM_ENABLE_GPIO_PORT_DRIVER
72-
static const char *const gpio_atom = "\x4" "gpio";
73-
static const char *const gpio_driver_atom = "\xB" "gpio_driver";
71+
static const char *const gpio_atom = ATOM_STR("\x4", "gpio");
72+
static const char *const gpio_driver_atom = ATOM_STR("\xB", "gpio_driver");
73+
static const char *const gpio_interrupt_atom = ATOM_STR("\xE", "gpio_interrupt");
7474
#endif
7575

76+
static const char *const high_atom = ATOM_STR("\x4", "high");
77+
static const char *const low_atom = ATOM_STR("\x3", "low");
78+
7679
static const AtomStringIntPair pin_mode_table[] = {
7780
{ ATOM_STR("\x5", "input"), GPIO_MODE_INPUT },
7881
{ ATOM_STR("\x6", "output"), GPIO_MODE_OUTPUT },
@@ -101,6 +104,16 @@ static const AtomStringIntPair pin_level_table[] = {
101104
SELECT_INT_DEFAULT(GPIOPinInvalid)
102105
};
103106

107+
static const AtomStringIntPair int_trigger_table[] = {
108+
{ ATOM_STR("\x4", "none"), GPIO_INTR_DISABLE },
109+
{ ATOM_STR("\x6", "rising"), GPIO_INTR_POSEDGE },
110+
{ ATOM_STR("\x7", "falling"), GPIO_INTR_NEGEDGE },
111+
{ ATOM_STR("\x4", "both"), GPIO_INTR_ANYEDGE },
112+
{ ATOM_STR("\x3", "low"), GPIO_INTR_LOW_LEVEL },
113+
{ ATOM_STR("\x4", "high"), GPIO_INTR_HIGH_LEVEL },
114+
SELECT_INT_DEFAULT(GPIO_INTR_MAX)
115+
};
116+
104117
enum gpio_cmd
105118
{
106119
GPIOInvalidCmd = 0,
@@ -286,7 +299,7 @@ static inline term gpio_digital_read(term gpio_num_term)
286299

287300
avm_int_t level = gpio_get_level(gpio_num);
288301

289-
return level ? HIGH_ATOM : LOW_ATOM;
302+
return level ? globalcontext_make_atom(glb, high_atom) : globalcontext_make_atom(glb, low_atom);
290303
}
291304

292305
#ifdef CONFIG_AVM_ENABLE_GPIO_PORT_DRIVER
@@ -371,7 +384,7 @@ EventListener *gpio_interrupt_callback(GlobalContext *glb, EventListener *listen
371384
BEGIN_WITH_STACK_HEAP(1 + 2, heap);
372385

373386
term int_msg = term_alloc_tuple(2, &heap);
374-
term_put_tuple_element(int_msg, 0, GPIO_INTERRUPT_ATOM);
387+
term_put_tuple_element(int_msg, 0, globalcontext_make_atom(glb, gpio_interrupt_atom));
375388
term_put_tuple_element(int_msg, 1, term_from_int32(gpio_num));
376389

377390
globalcontext_send_message(glb, listening_pid, int_msg);
@@ -482,36 +495,9 @@ static term gpiodriver_set_int(Context *ctx, int32_t target_pid, term cmd)
482495
target_local_pid = target_pid;
483496
}
484497

485-
486-
/* TODO: GPIO specific atoms should be removed from platform_defaultatoms and constructed within this driver */
487-
gpio_int_type_t interrupt_type;
488-
switch (trigger) {
489-
case NONE_ATOM:
490-
interrupt_type = GPIO_INTR_DISABLE;
491-
break;
492-
493-
case RISING_ATOM:
494-
interrupt_type = GPIO_INTR_POSEDGE;
495-
break;
496-
497-
case FALLING_ATOM:
498-
interrupt_type = GPIO_INTR_NEGEDGE;
499-
break;
500-
501-
case BOTH_ATOM:
502-
interrupt_type = GPIO_INTR_ANYEDGE;
503-
break;
504-
505-
case LOW_ATOM:
506-
interrupt_type = GPIO_INTR_LOW_LEVEL;
507-
break;
508-
509-
case HIGH_ATOM:
510-
interrupt_type = GPIO_INTR_HIGH_LEVEL;
511-
break;
512-
513-
default:
514-
return ERROR_ATOM;
498+
gpio_int_type_t interrupt_type = interop_atom_term_select_int(int_trigger_table, trigger, ctx->global);
499+
if(UNLIKELY(interrupt_type == GPIO_INTR_MAX)) {
500+
return BADARG_ATOM;
515501
}
516502

517503
if (trigger != NONE_ATOM) {
@@ -570,6 +556,7 @@ static term gpiodriver_remove_int(Context *ctx, term cmd)
570556
return ERROR_ATOM;
571557
}
572558

559+
573560
return unregister_interrupt_listener(ctx, gpio_num);
574561
}
575562

src/platforms/esp32/components/avm_sys/include/platform_defaultatoms.def

-8
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
1919
*/
2020

21-
X(READ_ATOM, "\x4", "read")
22-
X(GPIO_INTERRUPT_ATOM, "\xE", "gpio_interrupt")
23-
X(RISING_ATOM, "\x6", "rising")
24-
X(FALLING_ATOM, "\x7", "falling")
25-
X(BOTH_ATOM, "\x4", "both")
26-
X(LOW_ATOM, "\x3", "low")
27-
X(HIGH_ATOM, "\x4", "high")
28-
2921
X(ESP32_ATOM, "\x5", "esp32")
3022

3123
X(PROTO_ATOM, "\x5", "proto")

0 commit comments

Comments
 (0)