Skip to content

Commit c95a07b

Browse files
committed
Remove unnecessary GPIO atoms from ESP32 platform_defaultatoms.def
Reduce the number of GPIO related atoms created at boot by using more efficient matching to atom string where possible. Removes `static const char *const` declarations for atoms, replacing with `ATOM_STR()` in place for better readability. Handles the creations of atoms used by the driver in a safer way, checking their validity where necessary. Signed-off-by: Winford <winford@object.stream>
1 parent 74258a1 commit c95a07b

File tree

2 files changed

+36
-51
lines changed

2 files changed

+36
-51
lines changed

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

Lines changed: 34 additions & 45 deletions
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"
@@ -49,6 +48,7 @@
4948

5049
#include "esp32_sys.h"
5150
#include "esp_log.h"
51+
#include "platform_defaultatoms.h"
5252
#include "sys.h"
5353

5454
#define TAG "gpio_driver"
@@ -68,11 +68,6 @@ static void gpio_isr_handler(void *arg);
6868
static Context *gpio_driver_create_port(GlobalContext *global, term opts);
6969
#endif
7070

71-
#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";
74-
#endif
75-
7671
static const AtomStringIntPair pin_mode_table[] = {
7772
{ ATOM_STR("\x5", "input"), GPIO_MODE_INPUT },
7873
{ ATOM_STR("\x6", "output"), GPIO_MODE_OUTPUT },
@@ -101,6 +96,16 @@ static const AtomStringIntPair pin_level_table[] = {
10196
SELECT_INT_DEFAULT(GPIOPinInvalid)
10297
};
10398

99+
static const AtomStringIntPair int_trigger_table[] = {
100+
{ ATOM_STR("\x4", "none"), GPIO_INTR_DISABLE },
101+
{ ATOM_STR("\x6", "rising"), GPIO_INTR_POSEDGE },
102+
{ ATOM_STR("\x7", "falling"), GPIO_INTR_NEGEDGE },
103+
{ ATOM_STR("\x4", "both"), GPIO_INTR_ANYEDGE },
104+
{ ATOM_STR("\x3", "low"), GPIO_INTR_LOW_LEVEL },
105+
{ ATOM_STR("\x4", "high"), GPIO_INTR_HIGH_LEVEL },
106+
SELECT_INT_DEFAULT(GPIO_INTR_MAX)
107+
};
108+
104109
enum gpio_cmd
105110
{
106111
GPIOInvalidCmd = 0,
@@ -286,15 +291,19 @@ static inline term gpio_digital_read(term gpio_num_term)
286291

287292
avm_int_t level = gpio_get_level(gpio_num);
288293

289-
return level ? HIGH_ATOM : LOW_ATOM;
294+
return level ? globalcontext_make_atom(glb, high_atom) : globalcontext_make_atom(glb, low_atom);
290295
}
291296

292297
#ifdef CONFIG_AVM_ENABLE_GPIO_PORT_DRIVER
293298

294299
void gpio_driver_init(GlobalContext *global)
295300
{
296-
int index = globalcontext_insert_atom(global, gpio_driver_atom);
297-
gpio_driver = term_from_atom_index(index);
301+
int index = globalcontext_insert_atom(global, ATOM_STR("\xB", "gpio_driver"));
302+
if (UNLIKELY(index < 0 )){
303+
ESP_LOGE(TAG, "Failed to initialize gpio_driver");
304+
} else {
305+
gpio_driver = term_from_atom_index(index);
306+
}
298307
}
299308

300309
Context *gpio_driver_create_port(GlobalContext *global, term opts)
@@ -312,12 +321,18 @@ Context *gpio_driver_create_port(GlobalContext *global, term opts)
312321
ctx->native_handler = consume_gpio_mailbox;
313322
ctx->platform_data = gpio_data;
314323

315-
term reg_name_term = globalcontext_make_atom(global, gpio_atom);
316-
int atom_index = term_to_atom_index(reg_name_term);
317-
318-
if (UNLIKELY(!globalcontext_register_process(ctx->global, atom_index, ctx->process_id))) {
324+
int gpio_atom_index = globalcontext_insert_atom(global, ATOM_STR("\x4", "gpio"));
325+
if (UNLIKELY(gpio_atom_index < 0)) {
326+
ESP_LOGE(TAG, "Failed to create 'gpio' atom to register the driver!");
327+
free(gpio_data);
319328
scheduler_terminate(ctx);
329+
return NULL;
330+
}
331+
332+
if (UNLIKELY(!globalcontext_register_process(ctx->global, gpio_atom_index, ctx->process_id))) {
320333
ESP_LOGE(TAG, "Only a single GPIO driver can be opened.");
334+
free(gpio_data);
335+
scheduler_terminate(ctx);
321336
return NULL;
322337
}
323338

@@ -327,7 +342,7 @@ Context *gpio_driver_create_port(GlobalContext *global, term opts)
327342
static term gpiodriver_close(Context *ctx)
328343
{
329344
GlobalContext *glb = ctx->global;
330-
int gpio_atom_index = atom_table_ensure_atom(glb->atom_table, gpio_atom, AtomTableNoOpts);
345+
int gpio_atom_index = atom_table_ensure_atom(glb->atom_table, ATOM_STR("\x4", "gpio"), AtomTableNoOpts);
331346
if (UNLIKELY(!globalcontext_get_registered_process(glb, gpio_atom_index))) {
332347
return ERROR_ATOM;
333348
}
@@ -482,36 +497,9 @@ static term gpiodriver_set_int(Context *ctx, int32_t target_pid, term cmd)
482497
target_local_pid = target_pid;
483498
}
484499

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;
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

@@ -749,7 +738,7 @@ static term nif_gpio_digital_write(Context *ctx, int argc, term argv[])
749738

750739
static term nif_gpio_digital_read(Context *ctx, int argc, term argv[])
751740
{
752-
return gpio_digital_read(argv[0]);
741+
return gpio_digital_read(ctx, argv[0]);
753742
}
754743

755744
static const struct Nif gpio_init_nif =

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@
1818
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
1919
*/
2020

21-
X(READ_ATOM, "\x4", "read")
21+
X(ESP32_ATOM, "\x5", "esp32")
22+
2223
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")
2624
X(LOW_ATOM, "\x3", "low")
2725
X(HIGH_ATOM, "\x4", "high")
2826

29-
X(ESP32_ATOM, "\x5", "esp32")
30-
3127
X(PROTO_ATOM, "\x5", "proto")
3228
X(UDP_ATOM, "\x3", "udp")
3329
X(TCP_ATOM, "\x3", "tcp")

0 commit comments

Comments
 (0)