|
39 | 39 | #include "mailbox.h"
|
40 | 40 | #include "module.h"
|
41 | 41 | #include "nifs.h"
|
42 |
| -#include "platform_defaultatoms.h" |
43 | 42 | #include "port.h"
|
44 | 43 | #include "scheduler.h"
|
45 | 44 | #include "term.h"
|
@@ -69,10 +68,14 @@ static Context *gpio_driver_create_port(GlobalContext *global, term opts);
|
69 | 68 | #endif
|
70 | 69 |
|
71 | 70 | #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"); |
74 | 74 | #endif
|
75 | 75 |
|
| 76 | +static const char *const high_atom = ATOM_STR("\x4", "high"); |
| 77 | +static const char *const low_atom = ATOM_STR("\x3", "low"); |
| 78 | + |
76 | 79 | static const AtomStringIntPair pin_mode_table[] = {
|
77 | 80 | { ATOM_STR("\x5", "input"), GPIO_MODE_INPUT },
|
78 | 81 | { ATOM_STR("\x6", "output"), GPIO_MODE_OUTPUT },
|
@@ -101,6 +104,16 @@ static const AtomStringIntPair pin_level_table[] = {
|
101 | 104 | SELECT_INT_DEFAULT(GPIOPinInvalid)
|
102 | 105 | };
|
103 | 106 |
|
| 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 | + |
104 | 117 | enum gpio_cmd
|
105 | 118 | {
|
106 | 119 | GPIOInvalidCmd = 0,
|
@@ -286,7 +299,7 @@ static inline term gpio_digital_read(term gpio_num_term)
|
286 | 299 |
|
287 | 300 | avm_int_t level = gpio_get_level(gpio_num);
|
288 | 301 |
|
289 |
| - return level ? HIGH_ATOM : LOW_ATOM; |
| 302 | + return level ? globalcontext_make_atom(glb, high_atom) : globalcontext_make_atom(glb, low_atom); |
290 | 303 | }
|
291 | 304 |
|
292 | 305 | #ifdef CONFIG_AVM_ENABLE_GPIO_PORT_DRIVER
|
@@ -371,7 +384,7 @@ EventListener *gpio_interrupt_callback(GlobalContext *glb, EventListener *listen
|
371 | 384 | BEGIN_WITH_STACK_HEAP(1 + 2, heap);
|
372 | 385 |
|
373 | 386 | 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)); |
375 | 388 | term_put_tuple_element(int_msg, 1, term_from_int32(gpio_num));
|
376 | 389 |
|
377 | 390 | 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)
|
482 | 495 | target_local_pid = target_pid;
|
483 | 496 | }
|
484 | 497 |
|
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; |
515 | 501 | }
|
516 | 502 |
|
517 | 503 | if (trigger != NONE_ATOM) {
|
@@ -570,6 +556,7 @@ static term gpiodriver_remove_int(Context *ctx, term cmd)
|
570 | 556 | return ERROR_ATOM;
|
571 | 557 | }
|
572 | 558 |
|
| 559 | + |
573 | 560 | return unregister_interrupt_listener(ctx, gpio_num);
|
574 | 561 | }
|
575 | 562 |
|
|
0 commit comments