Skip to content

Commit f61de80

Browse files
committed
Move GPIO specific atoms from ESP32 platform_defaultatoms.h to gpio_driver.c
Moves the atoms only needed by the GPIO driver out of ESP32 platform_defaultatoms.h and into the gpio driver, where they are created as needed at runtime. Signed-off-by: Winford <[email protected]>
1 parent 63ba966 commit f61de80

File tree

3 files changed

+24
-67
lines changed

3 files changed

+24
-67
lines changed

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

+22-33
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);
@@ -484,34 +497,9 @@ static term gpiodriver_set_int(Context *ctx, int32_t target_pid, term cmd)
484497

485498

486499
/* 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;
500+
gpio_int_type_t interrupt_type = interop_atom_term_select_int(int_trigger_table, trigger, ctx->global);
501+
if(UNLIKELY(interrupt_type == GPIO_INTR_MAX)) {
502+
return BADARG_ATOM;
515503
}
516504

517505
if (trigger != NONE_ATOM) {
@@ -570,6 +558,7 @@ static term gpiodriver_remove_int(Context *ctx, term cmd)
570558
return ERROR_ATOM;
571559
}
572560

561+
573562
return unregister_interrupt_listener(ctx, gpio_num);
574563
}
575564

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

+2-18
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,9 @@
2323

2424
#include "defaultatoms.h"
2525

26-
#define READ_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 0)
27-
#define GPIO_INTERRUPT_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 1)
28-
#define RISING_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 2)
29-
#define FALLING_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 3)
30-
#define BOTH_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 4)
31-
#define LOW_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 5)
32-
#define HIGH_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 6)
26+
#define ESP32_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 0)
3327

34-
#define ESP32_ATOM_INDEX (PLATFORM_ATOMS_BASE_INDEX + 7)
35-
36-
#define SOCKET_ATOMS_BASE_INDEX (PLATFORM_ATOMS_BASE_INDEX + 8)
28+
#define SOCKET_ATOMS_BASE_INDEX (PLATFORM_ATOMS_BASE_INDEX + 1)
3729
#define PROTO_ATOM_INDEX (SOCKET_ATOMS_BASE_INDEX + 0)
3830
#define UDP_ATOM_INDEX (SOCKET_ATOMS_BASE_INDEX + 1)
3931
#define TCP_ATOM_INDEX (SOCKET_ATOMS_BASE_INDEX + 2)
@@ -77,14 +69,6 @@
7769
#define DEFAULT_ATOM_INDEX (UART_ATOMS_BASE_INDEX + 11)
7870
#define EVENT_QUEUE_LEN_ATOM_INDEX (UART_ATOMS_BASE_INDEX + 12)
7971

80-
#define READ_ATOM TERM_FROM_ATOM_INDEX(READ_ATOM_INDEX)
81-
#define GPIO_INTERRUPT_ATOM TERM_FROM_ATOM_INDEX(GPIO_INTERRUPT_ATOM_INDEX)
82-
#define RISING_ATOM TERM_FROM_ATOM_INDEX(RISING_ATOM_INDEX)
83-
#define FALLING_ATOM TERM_FROM_ATOM_INDEX(FALLING_ATOM_INDEX)
84-
#define BOTH_ATOM TERM_FROM_ATOM_INDEX(BOTH_ATOM_INDEX)
85-
#define LOW_ATOM TERM_FROM_ATOM_INDEX(LOW_ATOM_INDEX)
86-
#define HIGH_ATOM TERM_FROM_ATOM_INDEX(HIGH_ATOM_INDEX)
87-
8872
#define ESP32_ATOM TERM_FROM_ATOM_INDEX(ESP32_ATOM_INDEX)
8973

9074
// socket

src/platforms/esp32/components/avm_sys/platform_defaultatoms.c

-16
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@
2020

2121
#include "platform_defaultatoms.h"
2222

23-
static const char *const read_atom = "\x4" "read";
24-
static const char *const gpio_interrupt_atom = "\xE" "gpio_interrupt";
25-
static const char *const rising_atom = "\x6" "rising";
26-
static const char *const falling_atom = "\x7" "falling";
27-
static const char *const both_atom = "\x4" "both";
28-
static const char *const low_atom = "\x3" "low";
29-
static const char *const high_atom = "\x4" "high";
30-
3123
static const char *const esp32_atom = "\x5" "esp32";
3224

3325
static const char *const proto_atom = "\x5" "proto";
@@ -77,14 +69,6 @@ void platform_defaultatoms_init(GlobalContext *glb)
7769
{
7870
int ok = 1;
7971

80-
ok &= globalcontext_insert_atom(glb, read_atom) == READ_ATOM_INDEX;
81-
ok &= globalcontext_insert_atom(glb, gpio_interrupt_atom) == GPIO_INTERRUPT_ATOM_INDEX;
82-
ok &= globalcontext_insert_atom(glb, rising_atom) == RISING_ATOM_INDEX;
83-
ok &= globalcontext_insert_atom(glb, falling_atom) == FALLING_ATOM_INDEX;
84-
ok &= globalcontext_insert_atom(glb, both_atom) == BOTH_ATOM_INDEX;
85-
ok &= globalcontext_insert_atom(glb, low_atom) == LOW_ATOM_INDEX;
86-
ok &= globalcontext_insert_atom(glb, high_atom) == HIGH_ATOM_INDEX;
87-
8872
ok &= globalcontext_insert_atom(glb, esp32_atom) == ESP32_ATOM_INDEX;
8973

9074
ok &= globalcontext_insert_atom(glb, proto_atom) == PROTO_ATOM_INDEX;

0 commit comments

Comments
 (0)