Skip to content

Commit

Permalink
SPI: shorten names: e.g. mosi_io_num -> mosi
Browse files Browse the repository at this point in the history
Signed-off-by: Davide Bettio <davide@uninstall.it>
  • Loading branch information
bettio committed Dec 15, 2023
1 parent c9f3279 commit fe445ea
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
8 changes: 4 additions & 4 deletions examples/erlang/esp32/sx127x.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@

-define(SPISettings, [
{bus_config, [
{miso_io_num, 19},
{mosi_io_num, 27},
{sclk_io_num, 5}
{miso, 19},
{mosi, 27},
{sclk, 5}
]},
{device_config, [
{my_device, [
{spi_clock_hz, 1000000},
{mode, 0},
{spi_cs_io_num, 18},
{cs, 18},
{address_len_bits, 8}
]}
]}
Expand Down
50 changes: 25 additions & 25 deletions libs/eavmlib/src/spi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@

-export([open/1, close/1, read_at/4, write_at/5, write/3, write_read/3]).

-type spi_peripheral() :: hspi | vspi.
-type peripheral() :: hspi | vspi.
-type bus_config() :: [
{miso_io_num, non_neg_integer()}
| {mosi_io_num, non_neg_integer()}
| {sclk_io_num, non_neg_integer()}
| {spi_peripheral, spi_peripheral()}
{miso, non_neg_integer()}
| {mosi, non_neg_integer()}
| {sclk, non_neg_integer()}
| {peripheral, peripheral()}
].
-type device_config() :: [
{clock_speed_hz, non_neg_integer()}
| {mode, 0..3}
| {spi_cs_io_num, non_neg_integer()}
| {cs, non_neg_integer()}
| {address_len_bits, 0..64}
| {command_len_bits, 0..16}
].
Expand Down Expand Up @@ -93,10 +93,10 @@
%%
%% <table>
%% <tr> <th>Key</th> <th>Type</th> <th>Default</th> <th>Description</th> </tr>
%% <tr> <td>`miso_io_num'</td> <td>`non_neg_integer()'</td> <td>-</td> <td>MISO pin number</td></tr>
%% <tr> <td>`mosi_io_num'</td> <td>`non_neg_integer()'</td> <td>-</td> <td>MOSI pin number</td></tr>
%% <tr> <td>`sclk_io_num'</td> <td>`non_neg_integer()'</td> <td>-</td> <td>SCLK pin number</td></tr>
%% <tr> <td>`spi_peripheral'</td> <td>`hspi | vspi'</td> <td>`hspi'</td> <td>SPI Peripheral (ESP32 only)</td></tr>
%% <tr> <td>`miso'</td> <td>`non_neg_integer()'</td> <td>-</td> <td>MISO pin number</td></tr>
%% <tr> <td>`mosi'</td> <td>`non_neg_integer()'</td> <td>-</td> <td>MOSI pin number</td></tr>
%% <tr> <td>`sclk'</td> <td>`non_neg_integer()'</td> <td>-</td> <td>SCLK pin number</td></tr>
%% <tr> <td>`peripheral'</td> <td>`hspi | vspi'</td> <td>`hspi'</td> <td>SPI Peripheral (ESP32 only)</td></tr>
%% </table>
%%
%% Each device configuration is a properties list containing the following entries:
Expand All @@ -105,24 +105,24 @@
%% <tr> <th>Key</th> <th>Type</th> <th>Default</th> <th>Description</th> </tr>
%% <tr> <td>`clock_speed_hz'</td> <td>`non_neg_integer()'</td> <td>`1000000'</td> <td>Clock speed for the SPI device (in hz)</td></tr>
%% <tr> <td>`mode'</td> <td>`0..3'</td> <td>`0'</td> <td>SPI device mode</td></tr>
%% <tr> <td>`spi_cs_io_num'</td> <td>`non_neg_integer()'</td> <td>-</td> <td>SPI Chip Select pin number</td></tr>
%% <tr> <td>`cs'</td> <td>`non_neg_integer()'</td> <td>-</td> <td>SPI Chip Select pin number</td></tr>
%% <tr> <td>`address_len_bits'</td> <td>`non_neg_integer()'</td> <td>8</td> <td>Number of bits in the device address</td></tr>
%% </table>
%%
%% Example:
%% <pre>
%% Params = [
%% {bus_config, [
%% {miso_io_num, 16},
%% {mosi_io_num, 17},
%% {sclk_io_num, 5}
%% {miso, 16},
%% {mosi, 17},
%% {sclk, 5}
%% },
%% {device_config, [
%% {device1, [
%% {spi_cs_io_num, 18}
%% {cs, 18}
%% ]},
%% {device2, [
%% {spi_cs_io_num, 19}
%% {cs, 19}
%% ]}
%% ]}
%% ]
Expand Down Expand Up @@ -300,10 +300,10 @@ validate_params(Params) ->
%% @private
validate_bus_config(BusConfig) when is_map(BusConfig) orelse is_list(BusConfig) ->
#{
miso_io_num => validate_integer_entry(miso_io_num, BusConfig),
mosi_io_num => validate_integer_entry(mosi_io_num, BusConfig),
sclk_io_num => validate_integer_entry(sclk_io_num, BusConfig),
spi_peripheral => validate_spi_peripheral(get_value(spi_peripheral, BusConfig, hspi))
miso => validate_integer_entry(miso, BusConfig),
mosi => validate_integer_entry(mosi, BusConfig),
sclk => validate_integer_entry(sclk, BusConfig),
peripheral => validate_peripheral(get_value(peripheral, BusConfig, hspi))
};
validate_bus_config(undefined) ->
throw({badarg, missing_bus_config});
Expand All @@ -325,9 +325,9 @@ validate_is_integer(Key, Value) ->
throw({badarg, {not_an_integer_value, {Key, Value}}}).

%% @private
validate_spi_peripheral(hspi) -> hspi;
validate_spi_peripheral(vspi) -> vspi;
validate_spi_peripheral(Value) -> throw({bardarg, {spi_peripheral, Value}}).
validate_peripheral(hspi) -> hspi;
validate_peripheral(vspi) -> vspi;
validate_peripheral(Value) -> throw({bardarg, {peripheral, Value}}).

%% @private
validate_device_config(DeviceConfig) when is_map(DeviceConfig) ->
Expand Down Expand Up @@ -362,9 +362,9 @@ validate_device_config_fold(E, _Accum) ->
%% @private
validate_device_config_entries(Entries) when is_map(Entries) orelse is_list(Entries) ->
#{
spi_clock_hz => validate_integer_entry(spi_clock_hz, Entries),
clock_speed_hz => validate_integer_entry(clock_speed_hz, Entries),
mode => validate_mode(get_value(mode, Entries, undefined)),
spi_cs_io_num => validate_integer_entry(spi_cs_io_num, Entries, undefined),
cs => validate_integer_entry(cs, Entries, undefined),
address_len_bits => validate_address_len_bits(
get_value(address_len_bits, Entries, undefined)
),
Expand Down
32 changes: 16 additions & 16 deletions src/platforms/esp32/components/avm_builtins/spi_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ static void debug_buscfg(spi_bus_config_t *buscfg)
{
TRACE("Bus Config\n");
TRACE("==========\n");
TRACE(" miso_io_num: %i\n", buscfg->miso_io_num);
TRACE(" mosi_io_num: %i\n", buscfg->mosi_io_num);
TRACE(" sclk_io_num: %i\n", buscfg->sclk_io_num);
TRACE(" miso_io_num: %i\n", buscfg->miso_io_num);
TRACE(" miso: %i\n", buscfg->miso_io_num);
TRACE(" mosi: %i\n", buscfg->mosi_io_num);
TRACE(" sclk: %i\n", buscfg->sclk_io_num);
TRACE(" miso: %i\n", buscfg->miso_io_num);
TRACE(" quadwp_io_num: %i\n", buscfg->quadwp_io_num);
TRACE(" quadhd_io_num: %i\n", buscfg->quadhd_io_num);
}
Expand All @@ -176,7 +176,7 @@ static void debug_devcfg(spi_device_interface_config_t *devcfg)
TRACE("==========\n");
TRACE(" clock_speed_hz: %i\n", devcfg->clock_speed_hz);
TRACE(" mode: %i\n", devcfg->mode);
TRACE(" spics_io_num: %i\n", devcfg->spics_io_num);
TRACE(" cs: %i\n", devcfg->spics_io_num);
TRACE(" queue_size: %i\n", devcfg->queue_size);
TRACE(" address_bits: %i\n", devcfg->address_bits);
}
Expand All @@ -195,16 +195,16 @@ Context *spi_driver_create_port(GlobalContext *global, term opts)
term hspi_atom = globalcontext_make_atom(global, ATOM_STR("\x4", "hspi"));

term bus_config = interop_kv_get_value(opts, ATOM_STR("\xA", "bus_config"), global);
term miso_io_num_term = interop_kv_get_value(bus_config, ATOM_STR("\xB", "miso_io_num"), global);
term mosi_io_num_term = interop_kv_get_value(bus_config, ATOM_STR("\xB", "mosi_io_num"), global);
term sclk_io_num_term = interop_kv_get_value(bus_config, ATOM_STR("\xB", "sclk_io_num"), global);
term spi_peripheral_term = interop_kv_get_value_default(bus_config, ATOM_STR("\xB", "spi_peripheral"), hspi_atom, global);
spi_host_device_t host_device = get_spi_host_device(spi_peripheral_term, global);
term miso_term = interop_kv_get_value(bus_config, ATOM_STR("\x4", "miso"), global);
term mosi_term = interop_kv_get_value(bus_config, ATOM_STR("\x4", "mosi"), global);
term sclk_term = interop_kv_get_value(bus_config, ATOM_STR("\x4", "sclk"), global);
term peripheral_term = interop_kv_get_value_default(bus_config, ATOM_STR("\xA", "peripheral"), hspi_atom, global);
spi_host_device_t host_device = get_spi_host_device(peripheral_term, global);

spi_bus_config_t buscfg = { 0 };
buscfg.miso_io_num = term_to_int32(miso_io_num_term);
buscfg.mosi_io_num = term_to_int32(mosi_io_num_term);
buscfg.sclk_io_num = term_to_int32(sclk_io_num_term);
buscfg.miso_io_num = term_to_int32(miso_term);
buscfg.mosi_io_num = term_to_int32(mosi_term);
buscfg.sclk_io_num = term_to_int32(sclk_term);
buscfg.quadwp_io_num = -1;
buscfg.quadhd_io_num = -1;

Expand Down Expand Up @@ -237,16 +237,16 @@ Context *spi_driver_create_port(GlobalContext *global, term opts)
term device_name = term_get_tuple_element(device_names, i);
term device_config = term_get_map_assoc(device_map, device_name, ctx->global);

term clock_speed_hz_term = interop_kv_get_value(device_config, ATOM_STR("\xC", "spi_clock_hz"), global);
term clock_speed_hz_term = interop_kv_get_value(device_config, ATOM_STR("\xE", "clock_speed_hz"), global);
term mode_term = interop_kv_get_value(device_config, ATOM_STR("\x4", "mode"), global);
term spics_io_num_term = interop_kv_get_value(device_config, ATOM_STR("\xD", "spi_cs_io_num"), global);
term cs_term = interop_kv_get_value(device_config, ATOM_STR("\x2", "cs"), global);
term address_bits_term = interop_kv_get_value(device_config, ATOM_STR("\x10", "address_len_bits"), global);
term command_bits_term = interop_kv_get_value(device_config, ATOM_STR("\x10", "command_len_bits"), global);

spi_device_interface_config_t devcfg = { 0 };
devcfg.clock_speed_hz = term_to_int32(clock_speed_hz_term);
devcfg.mode = term_to_int32(mode_term);
devcfg.spics_io_num = term_to_int32(spics_io_num_term);
devcfg.spics_io_num = term_to_int32(cs_term);
devcfg.queue_size = 4;
devcfg.address_bits = term_to_int32(address_bits_term);
devcfg.command_bits = term_to_int32(command_bits_term);
Expand Down

0 comments on commit fe445ea

Please sign in to comment.