Skip to content

Commit 7b1688d

Browse files
committed
Update uart driver with specs, types, and function documentation
Add type definitions for input parameters. Add spec for all exported functions. Add documentation for functions. Signed-off-by: Winford <winford@object.stream>
1 parent 3dd59d5 commit 7b1688d

File tree

2 files changed

+88
-6
lines changed

2 files changed

+88
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
with nodejs and emscripten)
1313
- Added preliminary support for ESP32P4 (no networking support yet).
1414
- Added memory info in `out_of_memory` crash logs to help developers fix memory issues.
15+
- Added documentation and function specs for uart driver
1516

1617
### Fixed
1718

libs/eavmlib/src/uart.erl

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,105 @@
2121
-module(uart).
2222
-export([open/1, open/2, close/1, read/1, write/2]).
2323

24+
-type peripheral() :: string() | binary().
25+
% The peripheral `Name' may be one of: `"UART0"' | `"UART1"' | `"UART2"' | `<<"UART0">>' | `<<"UART1">>' | `<<"UART2">>'.
26+
27+
-type uart_opts() :: [
28+
{tx, Tx_pin :: integer()}
29+
| {rx, Rx_pin :: integer()}
30+
| {rts, Rts_pin :: integer()}
31+
| {cts, Cts_pin :: integer()}
32+
| {speed, Speed :: pos_integer()}
33+
| {data_bits, 5..8}
34+
| {stop_bits, 1 | 2}
35+
| {event_queue_len, Qlen :: pos_integer()}
36+
| {flow_control, none | hardware | software}
37+
| {parity, none | even | odd}
38+
| {peripheral, peripheral()}
39+
| []
40+
].
41+
42+
%%-----------------------------------------------------------------------------
43+
%% @param Name the uart peripheral to be opened
44+
%% @param Opts uart configuration options
45+
%% @returns Pid of the driver.
46+
%% @doc Open a connection to the UART driver
47+
%%
48+
%% This function will open a connection to the UART driver.
49+
%% @end
50+
%%-----------------------------------------------------------------------------
51+
-spec open(Name :: peripheral(), Opts :: uart_opts()) -> Pid :: pid() | {error, _Reason :: term()}.
2452
open(Name, Opts) ->
2553
open([{peripheral, Name} | Opts]).
2654

55+
%%-----------------------------------------------------------------------------
56+
%% @param Opts uart configuration options
57+
%% @returns Pid of the driver.
58+
%% @doc Open a connection to the UART driver default port
59+
%%
60+
%% This function will open a connection to the UART driver.
61+
%% @end
62+
%%-----------------------------------------------------------------------------
63+
-spec open(Opts :: uart_opts()) -> Pid :: pid() | {error, _Reason :: term()}.
2764
open(Opts) ->
2865
open_port({spawn, "uart"}, migrate_config(Opts)).
2966

30-
close(Pid) ->
67+
%%-----------------------------------------------------------------------------
68+
%% @param Pid of the uart port to be closed
69+
%% @returns ok.
70+
%% @doc Close a port connection to the UART driver
71+
%%
72+
%% This function will close the given port connection to the UART driver.
73+
%% @end
74+
%%-----------------------------------------------------------------------------
75+
-spec close(Pid :: pid()) -> ok | {error, _Reason :: term()}.
76+
close(Pid) when is_pid(Pid) ->
3177
port:call(Pid, close).
3278

33-
read(Pid) ->
79+
%%-----------------------------------------------------------------------------
80+
%% @param Pid of the uart port to be read
81+
%% @returns {ok, Data} or {error, Reason}
82+
%% @doc Read data from a UART port
83+
%%
84+
%% This function will return any data that is available, or return
85+
%% a `{error, timeout}' tuple. The driver will sent the next available
86+
%% data from the UART driver to the process that made the last read.
87+
%% Example:
88+
%% ```
89+
%% Data = case uart:read(Uart) of
90+
%% {ok, Binary} -> Binary;
91+
%% {error, timeout} ->
92+
%% receive
93+
%% {ok, RecvBinary} -> RecvBinary;
94+
%% Error -> error(Error)
95+
%% end;
96+
%% Error -> error(Error)
97+
%% end,
98+
%% '''
99+
%% Any attempt by another (or the same process) to read from uart before the
100+
%% next uart payload is sent by the driver will result in `{error, ealready}'.
101+
%% @end
102+
%%-----------------------------------------------------------------------------
103+
-spec read(Pid :: pid()) -> {ok, Data :: iodata()} | {error, _Reason :: term()}.
104+
read(Pid) when is_pid(Pid) ->
34105
port:call(Pid, read).
35106

36-
write(Pid, B) ->
37-
case is_iolist(B) of
107+
%%-----------------------------------------------------------------------------
108+
%% @param Pid of the uart port to be written to
109+
%% @param Data to be written to the given uart port
110+
%% @returns ok or {error, Reason}
111+
%% @doc Write data to a UART port
112+
%%
113+
%% This function will write the given data to the UART port.
114+
%% @end
115+
%%-----------------------------------------------------------------------------
116+
-spec write(Pid :: pid(), Data :: iodata()) -> ok | {error, _Reason :: term()}.
117+
write(Pid, Data) ->
118+
case is_iolist(Data) andalso is_pid(Pid) of
38119
true ->
39-
port:call(Pid, {write, B});
120+
port:call(Pid, {write, Data});
40121
false ->
41-
throw(badarg)
122+
error(badarg)
42123
end.
43124

44125
%% @private

0 commit comments

Comments
 (0)