Skip to content

Commit 90d792b

Browse files
committed
Update rp2040 gpio driver error returns
Update error returns from the gpio driver to return {error, Reason} rather than just an uninformative `error`. This will complete rp2040 platform requiremments to satisfy the ongoing meta issue atomvm#894. Signed-off-by: Winford <[email protected]>
1 parent c08a24c commit 90d792b

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

src/platforms/rp2040/src/lib/gpiodriver.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
#include "rp2040_sys.h"
3131
#include "trace.h"
3232

33+
#define INVALID_MODE_ATOM globalcontext_make_atom(ctx->global, ATOM_STR("\xC", "invalid_mode"))
34+
#define INVALID_LEVEL_ATOM globalcontext_make_atom(ctx->global, ATOM_STR("\xD", "invalid_level"))
35+
#define INVALID_PIN_ATOM globalcontext_make_atom(ctx->global, ATOM_STR("\xB", "invalid_pin"))
36+
3337
static const struct Nif *gpio_nif_get_nif(const char *nifname);
3438

3539
static const AtomStringIntPair pin_mode_table[] = {
@@ -105,7 +109,11 @@ static term nif_gpio_set_pin_mode(Context *ctx, int argc, term argv[])
105109
int gpio_num = term_to_int(argv[0]);
106110
int mode = interop_atom_term_select_int(pin_mode_table, argv[1], ctx->global);
107111
if (UNLIKELY(mode < 0)) {
108-
return ERROR_ATOM;
112+
BEGIN_WITH_STACK_HEAP(TUPLE_SIZE(2), heap);
113+
term ret = term_alloc_tuple(2, &heap);
114+
term_put_tuple_element(ret, 0, ERROR_ATOM);
115+
term_put_tuple_element(ret, 1, INVALID_MODE_ATOM);
116+
return ret;
109117
}
110118
gpio_set_dir(gpio_num, mode);
111119
return OK_ATOM;
@@ -133,12 +141,20 @@ static term nif_gpio_digital_write(Context *ctx, int argc, term argv[])
133141
if (term_is_integer(level_term)) {
134142
level = term_to_int32(level_term);
135143
if (UNLIKELY((level != 0) && (level != 1))) {
136-
return ERROR_ATOM;
144+
BEGIN_WITH_STACK_HEAP(TUPLE_SIZE(2), heap);
145+
term ret = term_alloc_tuple(2, &heap);
146+
term_put_tuple_element(ret, 0, ERROR_ATOM);
147+
term_put_tuple_element(ret, 1, INVALID_LEVEL_ATOM);
148+
return ret;
137149
}
138150
} else {
139151
level = interop_atom_term_select_int(pin_level_table, level_term, ctx->global);
140152
if (UNLIKELY(level < 0)) {
141-
return ERROR_ATOM;
153+
BEGIN_WITH_STACK_HEAP(TUPLE_SIZE(2), heap);
154+
term ret = term_alloc_tuple(2, &heap);
155+
term_put_tuple_element(ret, 0, ERROR_ATOM);
156+
term_put_tuple_element(ret, 1, INVALID_LEVEL_ATOM);
157+
return ret;
142158
}
143159
}
144160

@@ -150,12 +166,20 @@ static term nif_gpio_digital_write(Context *ctx, int argc, term argv[])
150166
} else if (term_is_atom(gpio_pin)) {
151167
gpio_num = interop_atom_term_select_int(wl_pin_table, gpio_pin, ctx->global);
152168
if (UNLIKELY((gpio_num == -1) || (gpio_num > 1))) {
153-
return ERROR_ATOM;
169+
BEGIN_WITH_STACK_HEAP(TUPLE_SIZE(2), heap);
170+
term ret = term_alloc_tuple(2, &heap);
171+
term_put_tuple_element(ret, 0, ERROR_ATOM);
172+
term_put_tuple_element(ret, 1, INVALID_PIN_ATOM);
173+
return ret;
154174
}
155175
cyw43_arch_gpio_put(gpio_num, level);
156176
#endif
157177
} else {
158-
return ERROR_ATOM;
178+
BEGIN_WITH_STACK_HEAP(TUPLE_SIZE(2), heap);
179+
term ret = term_alloc_tuple(2, &heap);
180+
term_put_tuple_element(ret, 0, ERROR_ATOM)
181+
term_put_tuple_element(ret, 1, INVALID_PIN_ATOM);
182+
return ret;
159183
}
160184

161185
return OK_ATOM;
@@ -176,12 +200,20 @@ static term nif_gpio_digital_read(Context *ctx, int argc, term argv[])
176200
} else if (term_is_atom(gpio_pin)) {
177201
gpio_num = interop_atom_term_select_int(wl_pin_table, gpio_pin, ctx->global);
178202
if (UNLIKELY((gpio_num != 2))) {
179-
return ERROR_ATOM;
203+
BEGIN_WITH_STACK_HEAP(TUPLE_SIZE(2), heap);
204+
term ret = term_alloc_tuple(2, &heap);
205+
term_put_tuple_element(ret, 0, ERROR_ATOM);
206+
term_put_tuple_element(ret, 1, INVALID_PIN_ATOM);
207+
return ret;
180208
}
181209
level = cyw43_arch_gpio_get(gpio_num);
182210
#endif
183211
} else {
184-
return ERROR_ATOM;
212+
BEGIN_WITH_STACK_HEAP(TUPLE_SIZE(2), heap);
213+
term ret = term_alloc_tuple(2, &heap);
214+
term_put_tuple_element(ret, 0, ERROR_ATOM)
215+
term_put_tuple_element(ret, 1, INVALID_PIN_ATOM);
216+
return ret;
185217
}
186218

187219
return level ? globalcontext_make_atom(ctx->global, ATOM_STR("\x4", "high")) : globalcontext_make_atom(ctx->global, ATOM_STR("\x3", "low"));

0 commit comments

Comments
 (0)