Skip to content

Commit

Permalink
SPI: add support to pico and poci terms
Browse files Browse the repository at this point in the history
Add support to alternatives to `mosi` and `miso` as described in
https://www.oshwa.org/a-resolution-to-redefine-spi-signal-names/ .

Signed-off-by: Davide Bettio <davide@uninstall.it>
  • Loading branch information
bettio committed Dec 15, 2023
1 parent f8240f4 commit bb65939
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Added `esp:get_default_mac/0` for retrieving the default MAC address on ESP32.
- Added support for `pico` and `poci` as an alternative to `mosi` and `miso` for SPI

### Changed

Expand Down
10 changes: 5 additions & 5 deletions doc/src/programmers-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1220,11 +1220,11 @@ The AtomVM SPI implementation uses the AtomVM Port mechanism and must be initial

The `bus_config` properties list contains the following entries:

| Key | Value Type | Required | Description |
|-----|------------|----------|---|
| `miso` | `integer()` | yes | SPI leader-in, follower-out pin (MOSI) |
| `mosi` | `integer()` | yes | SPI leader-out, follower-in pin (MISO) |
| `sclk` | `integer()` | yes | SPI clock pin (SCLK) |
| Key | Value Type | Required | Description |
|------------------|-------------|-------------|----------------------------------------------|
| `poci` (`miso`) | `integer()` | yes | SPI peripheral-out, controller-in pin (MOSI) |
| `pico` (`mosi`) | `integer()` | yes | SPI peripheral-in, controller-out pin (MISO) |
| `sclk` | `integer()` | yes | SPI clock pin (SCLK) |

The `device_config` entry is a properties list containing entries for each device attached to the SPI Bus. Each entry in this list contains the user-selected name (as an atom) of the device, followed by configuration for the named device.

Expand Down
36 changes: 20 additions & 16 deletions libs/eavmlib/src/spi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@

-type peripheral() :: hspi | vspi.
-type bus_config() :: [
{miso, non_neg_integer()}
{poci, non_neg_integer()}
| {pico, non_neg_integer()}
| {miso, non_neg_integer()}
| {mosi, non_neg_integer()}
| {sclk, non_neg_integer()}
| {peripheral, peripheral()}
Expand Down Expand Up @@ -330,34 +332,36 @@ migrate_deprecated(MaybeDeprecated) ->
migrate_deprecated_iter(none, Accum) ->
Accum;
migrate_deprecated_iter({K, V, Iter}, Accum) ->
NewK = replace_key(K),
warn_deprecated(K, NewK),
{Status, NewK} = replace_key(K),
warn_deprecated(Status, K, NewK),
migrate_deprecated_iter(maps:next(Iter), Accum#{NewK => V}).

%% @private
migrate_deprecated_fold({K, V}, Accum) ->
NewK = replace_key(K),
warn_deprecated(K, NewK),
{Status, NewK} = replace_key(K),
warn_deprecated(Status, K, NewK),
[{NewK, V} | Accum];
migrate_deprecated_fold(E, Accum) ->
[E | Accum].

%% @private
replace_key(Key) ->
case Key of
miso_io_num -> miso;
mosi_io_num -> mosi;
sclk_io_num -> sclk;
spi_cs_io_num -> cs;
spi_clock_hz -> clock_speed_hz;
spi_peripheral -> peripheral;
Any -> Any
pico -> {rename, mosi};
poci -> {rename, miso};
miso_io_num -> {warning, miso};
mosi_io_num -> {warning, mosi};
sclk_io_num -> {warning, sclk};
spi_cs_io_num -> {warning, cs};
spi_clock_hz -> {warning, clock_speed_hz};
spi_peripheral -> {warning, peripheral};
Any -> {ok, Any}
end.

warn_deprecated(Key, Key) ->
ok;
warn_deprecated(OldKey, NewKey) ->
io:format("SPI: found deprecated ~p, use ~p instead!!!~n", [OldKey, NewKey]).
warn_deprecated(warning, OldKey, NewKey) ->
io:format("SPI: found deprecated ~p, use ~p instead!!!~n", [OldKey, NewKey]);
warn_deprecated(_Status, Key, Key) ->
ok.

%% @private
validate_integer_entry(Key, Map) ->
Expand Down

0 comments on commit bb65939

Please sign in to comment.