Skip to content

Commit

Permalink
Merge pull request #1302 from petermm/optional-device_config
Browse files Browse the repository at this point in the history
Make SPI device_config optional

currently it's a required param, even in a scenario where it's meant to be
empty (nif c code handles device_config: [] already)

eg device_config: [] is currently required:

```
    spi_settings = [
      bus_config: [
        miso: 19,
        mosi: 23,
        sclk: 18,
        peripheral: "spi3"
      ],
      device_config: []
    ]

    spi = :spi.open(spi_settings)
    :ok = :esp.mount("sdspi", "/test", :fat, spi_host: spi, cs: 5)
```

PR allows leaving out 'device_config: []' - for simpler/better DX.
(I wasted a ton of time figuring out the empty device_config array requirement)

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
  • Loading branch information
bettio committed Oct 1, 2024
2 parents d15af16 + 6b6aa4a commit 7d63dc0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
instead
- `Enum.find_index` and `Enum.find_value` support Enumerable and not just lists
- Install AtomVM libraries source code and binaries for better dialyzer integration
- Made the `device_config` properties list in `spi:open/1` optional (defaults to `[]`), so you can use the function with only a `bus_config`

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions doc/src/programmers-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1452,10 +1452,10 @@ The [`spi` module](./apidocs/erlang/eavmlib/spi.md) encapsulates functionality a
Information about the ESP32 SPI leader mode interface can be found in the IDF SDK [SPI Documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/spi_master.html).
```
The AtomVM SPI implementation uses the AtomVM Port mechanism and must be initialized using the [`spi:open/1`](./apidocs/erlang/eavmlib/spi.md#open1) function. The single parameter to this function is a properties list containing two elements:
The AtomVM SPI implementation uses the AtomVM Port mechanism and must be initialized using the [`spi:open/1`](./apidocs/erlang/eavmlib/spi.md#open1) function. The single parameter to this function is a properties list containing:
* [`bus_config`](./apidocs/erlang/eavmlib/spi.md#bus_config) -- a properties list containing entries for the SPI bus
* [`device_config`](./apidocs/erlang/eavmlib/spi.md#device_config) -- a properties list containing entries for each device attached to the SPI Bus
* [`device_config`](./apidocs/erlang/eavmlib/spi.md#device_config) -- an optional properties list containing entries for each device attached to the SPI Bus
The `bus_config` properties list contains the following entries:
Expand Down
14 changes: 9 additions & 5 deletions libs/eavmlib/src/spi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@
| {command_len_bits, 0..16}
].
-type device_name() :: atom().
-type params() :: [
{bus_config, bus_config()}
| {device_config, [{device_name(), device_config()}]}
].
-type params() ::
[
{bus_config, bus_config()}
]
| [
{bus_config, bus_config()}
| {device_config, [{device_name(), device_config()}]}
].

-type spi() :: pid().
-type address() :: non_neg_integer().
Expand Down Expand Up @@ -294,7 +298,7 @@ write_read(SPI, DeviceName, Transaction) when
validate_params(Params) when is_map(Params) orelse is_list(Params) ->
#{
bus_config => validate_bus_config(get_value(bus_config, Params, undefined)),
device_config => validate_device_config(get_value(device_config, Params, undefined))
device_config => validate_device_config(get_value(device_config, Params, []))
};
validate_params(Params) ->
throw({error, {not_a_map_or_list, Params}}).
Expand Down

0 comments on commit 7d63dc0

Please sign in to comment.