Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 35b5bb9

Browse files
committedJan 3, 2025·
Fix ESP32 gpio driver error returns to match spec
Changes error returns to match the spec and ofther platforms. Nifs now raise any errors, and port function errors return `{error, Reason}`. Updates gpio.erl to reflect the correct returns, and make it more clear that errors for nifs will be raised. Closes #894 Signed-off-by: Winford <winford@object.stream>
1 parent 15baddc commit 35b5bb9

File tree

3 files changed

+177
-92
lines changed

3 files changed

+177
-92
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
### Fixed
1919
- ESP32: improved sntp sync speed from a cold boot.
20+
- ESP32 GPIO driver now matches spec for error returns, nifs raise `Error`, port functions return `{error, Reason}`
2021

2122
## [0.6.6] - Unreleased
2223

‎libs/eavmlib/src/gpio.erl

+29-29
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
%% Event type that will trigger a `gpio_interrupt'. STM32 only supports `rising', `falling', or `both'.
8383

8484
%%-----------------------------------------------------------------------------
85-
%% @returns Pid | error | {error, Reason}
85+
%% @returns Pid | {error, Reason}
8686
%% @doc Start the GPIO driver port
8787
%%
8888
%% Returns the pid of the active GPIO port driver, otherwise the GPIO
@@ -103,7 +103,7 @@ start() ->
103103
end.
104104

105105
%%-----------------------------------------------------------------------------
106-
%% @returns Pid | error | {error, Reason}
106+
%% @returns Pid | {error, Reason}
107107
%% @doc Start the GPIO driver port
108108
%%
109109
%% The GPIO port driver will be stared and registered as `gpio'. If the
@@ -121,7 +121,7 @@ open() ->
121121

122122
%%-----------------------------------------------------------------------------
123123
%% @param GPIO pid that was returned from gpio:start/0
124-
%% @returns ok | error | {error, Reason}
124+
%% @returns ok | {error, Reason}
125125
%% @doc Stop the GPIO interrupt port
126126
%%
127127
%% This function disables any interrupts that are set, stops
@@ -135,7 +135,7 @@ close(GPIO) ->
135135
port:call(GPIO, {close}).
136136

137137
%%-----------------------------------------------------------------------------
138-
%% @returns ok | error | {error, Reason}
138+
%% @returns ok | {error, Reason}
139139
%% @doc Stop the GPIO interrupt port
140140
%%
141141
%% This function disables any interrupts that are set, stops
@@ -151,7 +151,7 @@ stop() ->
151151
%%-----------------------------------------------------------------------------
152152
%% @param GPIO pid that was returned from gpio:start/0
153153
%% @param Pin number of the pin to read
154-
%% @returns high | low | error | {error, Reason}
154+
%% @returns high | low | {error, Reason}
155155
%% @doc Read the digital state of a GPIO pin
156156
%%
157157
%% Read if an input pin state is `high' or `low'.
@@ -169,7 +169,7 @@ read(GPIO, Pin) ->
169169
%% @param GPIO pid that was returned from `gpio:start/0'
170170
%% @param Pin number of the pin to configure
171171
%% @param Direction is `input', `output', or `output_od'
172-
%% @returns ok | error | {error, Reason}
172+
%% @returns ok | {error, Reason}
173173
%% @doc Set the operational mode of a pin
174174
%%
175175
%% Pins can be used for input, output, or output with open drain.
@@ -199,7 +199,7 @@ set_direction(GPIO, Pin, Direction) ->
199199
%% @param GPIO pid that was returned from `gpio:start/0'
200200
%% @param Pin number of the pin to write
201201
%% @param Level the desired output level to set
202-
%% @returns ok | error | {error, Reason}
202+
%% @returns ok | {error, Reason}
203203
%% @doc Set GPIO digital output level
204204
%%
205205
%% Set a pin to `high' (1) or `low' (0).
@@ -233,7 +233,7 @@ set_level(GPIO, Pin, Level) ->
233233
%% @param GPIO pid that was returned from `gpio:start/0'
234234
%% @param Pin number of the pin to set the interrupt on
235235
%% @param Trigger is the state that will trigger an interrupt
236-
%% @returns ok | error | {error, Reason}
236+
%% @returns ok | {error, Reason}
237237
%% @doc Set a GPIO interrupt
238238
%%
239239
%% Available triggers are `none' (which is the same as disabling an
@@ -257,7 +257,7 @@ set_int(GPIO, Pin, Trigger) ->
257257
%% @param Pin number of the pin to set the interrupt on
258258
%% @param Trigger is the state that will trigger an interrupt
259259
%% @param Pid is the process that will receive the interrupt message
260-
%% @returns ok | error | {error, Reason}
260+
%% @returns ok | {error, Reason}
261261
%% @doc Set a GPIO interrupt
262262
%%
263263
%% Available triggers are `none' (which is the same as disabling an
@@ -280,21 +280,21 @@ set_int(GPIO, Pin, Trigger, Pid) ->
280280
%%-----------------------------------------------------------------------------
281281
%% @param GPIO pid that was returned from `gpio:start/0'
282282
%% @param Pin number of the pin to remove the interrupt
283-
%% @returns ok | error | {error, Reason}
283+
%% @returns ok | {error, Reason}
284284
%% @doc Remove a GPIO interrupt
285285
%%
286286
%% Removes an interrupt from the specified pin.
287287
%%
288288
%% The rp2040 (Pico) port does not support gpio interrupts at this time.
289289
%% @end
290290
%%-----------------------------------------------------------------------------
291-
-spec remove_int(GPIO :: gpio(), Pin :: pin()) -> ok | {error, Reason :: atom()} | error.
291+
-spec remove_int(GPIO :: gpio(), Pin :: pin()) -> ok | {error, Reason :: atom()}.
292292
remove_int(GPIO, Pin) ->
293293
port:call(GPIO, {remove_int, Pin}).
294294

295295
%%-----------------------------------------------------------------------------
296296
%% @param Pin number to initialize
297-
%% @returns ok
297+
%% @returns ok | raise(Error)
298298
%% @doc Initialize a pin to be used as GPIO.
299299
%% This is required on RP2040 and for some pins on ESP32.
300300
%% @end
@@ -305,7 +305,7 @@ init(_Pin) ->
305305

306306
%%-----------------------------------------------------------------------------
307307
%% @param Pin number to deinitialize
308-
%% @returns ok
308+
%% @returns ok | raise(Error)
309309
%% @doc Reset a pin back to the NULL function.
310310
%% Currently only implemented for RP2040 (Pico).
311311
%% @end
@@ -317,7 +317,7 @@ deinit(_Pin) ->
317317
%%-----------------------------------------------------------------------------
318318
%% @param Pin number to set operational mode
319319
%% @param Direction is `input', `output', or `output_od'
320-
%% @returns ok | error | {error, Reason}
320+
%% @returns ok | raise(Error)
321321
%% @doc Set the operational mode of a pin
322322
%%
323323
%% Pins can be used for input, output, or output with open drain.
@@ -336,14 +336,14 @@ deinit(_Pin) ->
336336
%% @end
337337
%%-----------------------------------------------------------------------------
338338
-spec set_pin_mode(Pin :: pin(), Direction :: direction()) ->
339-
ok | {error, Reason :: atom()} | error.
339+
ok.
340340
set_pin_mode(_Pin, _Mode) ->
341341
erlang:nif_error(undefined).
342342

343343
%%-----------------------------------------------------------------------------
344344
%% @param Pin number to set internal resistor direction
345345
%% @param Pull is the internal resistor state
346-
%% @returns ok | error
346+
%% @returns ok | raise(Error)
347347
%% @doc Set the internal resistor of a pin
348348
%%
349349
%% Pins can be internally pulled `up', `down', `up_down' (pulled in
@@ -354,13 +354,13 @@ set_pin_mode(_Pin, _Mode) ->
354354
%% or `set_pin_mode/2'.
355355
%% @end
356356
%%-----------------------------------------------------------------------------
357-
-spec set_pin_pull(Pin :: pin(), Pull :: pull()) -> ok | error.
357+
-spec set_pin_pull(Pin :: pin(), Pull :: pull()) -> ok.
358358
set_pin_pull(_Pin, _Pull) ->
359359
erlang:nif_error(undefined).
360360

361361
%%-----------------------------------------------------------------------------
362362
%% @param Pin number of the pin to be held
363-
%% @returns ok | error
363+
%% @returns ok | raise(Error)
364364
%% @doc Hold the state of a pin
365365
%%
366366
%% The gpio pad hold function works in both input and output modes,
@@ -380,13 +380,13 @@ set_pin_pull(_Pin, _Pull) ->
380380
%% This function is only supported on ESP32.
381381
%% @end
382382
%%-----------------------------------------------------------------------------
383-
-spec hold_en(Pin :: pin()) -> ok | error.
383+
-spec hold_en(Pin :: pin()) -> ok.
384384
hold_en(_Pin) ->
385385
erlang:nif_error(undefined).
386386

387387
%%-----------------------------------------------------------------------------
388388
%% @param Pin number of the pin to be released
389-
%% @returns ok | error
389+
%% @returns ok | raise(Error)
390390
%% @doc Release a pin from a hold state.
391391
%%
392392
%% When the chip is woken up from Deep-sleep, the gpio will be set to
@@ -402,7 +402,7 @@ hold_en(_Pin) ->
402402
%% This function is only supported on ESP32.
403403
%% @end
404404
%%-----------------------------------------------------------------------------
405-
-spec hold_dis(Pin :: pin()) -> ok | error.
405+
-spec hold_dis(Pin :: pin()) -> ok.
406406
hold_dis(_Pin) ->
407407
erlang:nif_error(undefined).
408408

@@ -445,7 +445,7 @@ deep_sleep_hold_dis() ->
445445
%%-----------------------------------------------------------------------------
446446
%% @param Pin number of the pin to write
447447
%% @param Level the desired output level to set
448-
%% @returns ok | error | {error, Reason}
448+
%% @returns ok | raise(Error)
449449
%% @doc Set GPIO digital output level
450450
%%
451451
%% Set a pin to `high' (1) or `low' (0).
@@ -469,13 +469,13 @@ deep_sleep_hold_dis() ->
469469
%% require or accept `set_pin_mode' or `set_pin_pull' before use.
470470
%% @end
471471
%%-----------------------------------------------------------------------------
472-
-spec digital_write(Pin :: pin(), Level :: level()) -> ok | {error, Reason :: atom()} | error.
472+
-spec digital_write(Pin :: pin(), Level :: level()) -> ok.
473473
digital_write(_Pin, _Level) ->
474474
erlang:nif_error(undefined).
475475

476476
%%-----------------------------------------------------------------------------
477477
%% @param Pin number of the pin to read
478-
%% @returns high | low | error | {error, Reason}
478+
%% @returns high | low | raise(Error)
479479
%% @doc Read the digital state of a GPIO pin
480480
%%
481481
%% Read if an input pin state is high or low.
@@ -486,14 +486,14 @@ digital_write(_Pin, _Level) ->
486486
%% and does not require or accept `set_pin_mode' or `set_pin_pull' before use.
487487
%% @end
488488
%%-----------------------------------------------------------------------------
489-
-spec digital_read(Pin :: pin()) -> high | low | {error, Reason :: atom()} | error.
489+
-spec digital_read(Pin :: pin()) -> high | low.
490490
digital_read(_Pin) ->
491491
erlang:nif_error(undefined).
492492

493493
%%-----------------------------------------------------------------------------
494494
%% @param Pin number of the pin to set the interrupt on
495495
%% @param Trigger is the state that will trigger an interrupt
496-
%% @returns ok | error | {error, Reason}
496+
%% @returns ok | {error, Reason}
497497
%% @doc Convenience function for `gpio:set_int/3'
498498
%%
499499
%% This is a convenience function for `gpio:set_int/3' that allows an
@@ -509,13 +509,13 @@ digital_read(_Pin) ->
509509
%% @end
510510
%%-----------------------------------------------------------------------------
511511
-spec attach_interrupt(Pin :: pin(), Trigger :: trigger()) ->
512-
ok | {error, Reason :: atom()} | error.
512+
ok | {error, Reason :: atom()}.
513513
attach_interrupt(Pin, Trigger) ->
514514
set_int(start(), Pin, Trigger).
515515

516516
%-----------------------------------------------------------------------------
517517
%% @param Pin number of the pin to remove the interrupt
518-
%% @returns ok | error | {error, Reason}
518+
%% @returns ok | {error, Reason}
519519
%% @doc Convenience function for `gpio:remove_int/2'
520520
%%
521521
%% This is a convenience function for `gpio:remove_int/2' that allows an
@@ -527,6 +527,6 @@ attach_interrupt(Pin, Trigger) ->
527527
%% The rp2040 (Pico) port does not support gpio interrupts at this time.
528528
%% @end
529529
%%-----------------------------------------------------------------------------
530-
-spec detach_interrupt(Pin :: pin()) -> ok | {error, Reason :: atom()} | error.
530+
-spec detach_interrupt(Pin :: pin()) -> ok | {error, Reason :: atom()}.
531531
detach_interrupt(Pin) ->
532532
remove_int(whereis(gpio), Pin).

0 commit comments

Comments
 (0)
Please sign in to comment.