Skip to content

Commit 7d63dc0

Browse files
committed
Merge pull request #1302 from petermm/optional-device_config
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
2 parents d15af16 + 6b6aa4a commit 7d63dc0

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
4141
instead
4242
- `Enum.find_index` and `Enum.find_value` support Enumerable and not just lists
4343
- Install AtomVM libraries source code and binaries for better dialyzer integration
44+
- Made the `device_config` properties list in `spi:open/1` optional (defaults to `[]`), so you can use the function with only a `bus_config`
4445

4546
### Fixed
4647

doc/src/programmers-guide.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,10 +1452,10 @@ The [`spi` module](./apidocs/erlang/eavmlib/spi.md) encapsulates functionality a
14521452
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).
14531453
```
14541454
1455-
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:
1455+
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:
14561456
14571457
* [`bus_config`](./apidocs/erlang/eavmlib/spi.md#bus_config) -- a properties list containing entries for the SPI bus
1458-
* [`device_config`](./apidocs/erlang/eavmlib/spi.md#device_config) -- a properties list containing entries for each device attached to the SPI Bus
1458+
* [`device_config`](./apidocs/erlang/eavmlib/spi.md#device_config) -- an optional properties list containing entries for each device attached to the SPI Bus
14591459
14601460
The `bus_config` properties list contains the following entries:
14611461

libs/eavmlib/src/spi.erl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@
5959
| {command_len_bits, 0..16}
6060
].
6161
-type device_name() :: atom().
62-
-type params() :: [
63-
{bus_config, bus_config()}
64-
| {device_config, [{device_name(), device_config()}]}
65-
].
62+
-type params() ::
63+
[
64+
{bus_config, bus_config()}
65+
]
66+
| [
67+
{bus_config, bus_config()}
68+
| {device_config, [{device_name(), device_config()}]}
69+
].
6670

6771
-type spi() :: pid().
6872
-type address() :: non_neg_integer().
@@ -294,7 +298,7 @@ write_read(SPI, DeviceName, Transaction) when
294298
validate_params(Params) when is_map(Params) orelse is_list(Params) ->
295299
#{
296300
bus_config => validate_bus_config(get_value(bus_config, Params, undefined)),
297-
device_config => validate_device_config(get_value(device_config, Params, undefined))
301+
device_config => validate_device_config(get_value(device_config, Params, []))
298302
};
299303
validate_params(Params) ->
300304
throw({error, {not_a_map_or_list, Params}}).

0 commit comments

Comments
 (0)