Skip to content

Commit

Permalink
pal: spi: NRFX SPI bus implementation for Sidewalk.
Browse files Browse the repository at this point in the history
SPI bus implementation based on the NRFX for nRF52840.

Signed-off-by: Marcin Gasiorek <[email protected]>
  • Loading branch information
MarGasiorek committed Sep 16, 2024
1 parent 34856b2 commit 054ef29
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 24 deletions.
40 changes: 40 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,46 @@ config SIDEWALK_PAL_RADIO_SOURCE
bool "Build sub-GHz radio driver from sources [EXPERIMENTAL]"
select EXPERIMENTAL

if SIDEWALK_SPI_BUS

config SIDEWALK_NRF52840_NRFX_SPI
bool "Sidewalk NRFX SPI driver for nRF52840 [EXPERIMENTAL]"
select EXPERIMENTAL
depends on SOC_NRF52840
help
Serial bus implementation for Sidewalk based on the NRFX SPI.

choice SIDEWALK_NRFX_SPI_INSTANCE_ID
prompt "Instance of the NRX SPI driver"
depends on SIDEWALK_NRF52840_NRFX_SPI
default SIDEWALK_NRFX_SPI2
help
Select the NRFX SPI instance.
The same SPI instance must be disabled in the DTS.

config SIDEWALK_NRFX_SPI0
bool "SPI0"
select NRFX_SPI0

config SIDEWALK_NRFX_SPI1
bool "SPI1"
select NRFX_SPI1

config SIDEWALK_NRFX_SPI2
bool "SPI2"
select NRFX_SPI2

endchoice # SIDEWALK_NRFX_SPI_INSTANCE_ID

config SIDEWALK_NRFX_SPI_ID
int
depends on SIDEWALK_NRF52840_NRFX_SPI
default 2
default 1 if SIDEWALK_NRFX_SPI1
default 0 if SIDEWALK_NRFX_SPI0

endif # SIDEWALK_SPI_BUS

rsource "Kconfig.dependencies"
rsource "utils/Kconfig"

Expand Down
46 changes: 22 additions & 24 deletions samples/sid_end_device/boards/nrf52840dk_nrf52840.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@
status = "disabled";
};

sid_semtech: &spi2 {
compatible = "nordic,nrf-spim";
status = "okay";
pinctrl-0 = <&spi2_default>;
pinctrl-1 = <&spi2_sleep>;
pinctrl-names = "default", "sleep";
clock-frequency = <DT_FREQ_M(8)>;
&spi2 {
status = "disabled";
};

/{
nrfx_spi_gpios{
compatible = "gpio-keys";
nrfx_spi_sck: sck {
gpios = <&gpio1 0xF GPIO_ACTIVE_LOW>;
label = "spi_sck";
};
nrfx_spi_miso: miso {
gpios = <&gpio1 0xE GPIO_ACTIVE_HIGH>;
label = "spi_miso";
};
nrfx_spi_mosi: mosi {
gpios = <&gpio1 0xD GPIO_ACTIVE_HIGH>;
label = "spi_mosi";
};
nrfx_spi_cs: cs {
gpios = <&gpio1 0x8 GPIO_ACTIVE_HIGH>;
label = "spi_cs";
};
};

state_notifier_gpios{
compatible = "gpio-keys";
state_notifier_error: error {
Expand Down Expand Up @@ -77,20 +92,3 @@ sid_semtech: &spi2 {
nordic,pm-ext-flash = &mx25r64;
};
};

&spi2_default {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 1, 15)>,
<NRF_PSEL(SPIM_MISO, 1, 14)>,
<NRF_PSEL(SPIM_MOSI, 1, 13)>;
};
};

&spi2_sleep {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 1, 15)>,
<NRF_PSEL(SPIM_MISO, 1, 14)>,
<NRF_PSEL(SPIM_MOSI, 1, 13)>;
low-power-enable;
};
};
1 change: 1 addition & 0 deletions scripts/ci/license.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ignore_license_in_files:
- version.conf$
- tools/.*
- twister-out.*
- sidewalk.*/snippets/*


license_header_size: 30
Expand Down
4 changes: 4 additions & 0 deletions subsys/sal/sid_pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ zephyr_library_sources_ifdef(CONFIG_SIDEWALK_SW_INTERRUPTS sid_sw_interrupts.c)

zephyr_library_sources_ifdef(CONFIG_SIDEWALK_DELAY sid_delay.c)

if(NOT (CONFIG_SOC_NRF52840 AND CONFIG_SIDEWALK_NRF52840_NRFX_SPI))
zephyr_library_sources_ifdef(CONFIG_SIDEWALK_SPI_BUS sid_pal_serial_bus_spi.c)
else()
zephyr_library_sources_ifdef(CONFIG_SIDEWALK_SPI_BUS sid_pal_serial_bus_nrfx_spi.c)
endif() # CONFIG_SOC_NRF52840 AND CONFIG_NRF52840_NRFX_SPI

zephyr_library_sources_ifdef(CONFIG_SIDEWALK sid_common.c)

Expand Down
97 changes: 97 additions & 0 deletions subsys/sal/sid_pal/src/sid_pal_serial_bus_nrfx_spi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <sid_pal_serial_bus_ifc.h>
#include <nrfx_spi.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(sid_nrfx_spi_bus, CONFIG_SPI_BUS_LOG_LEVEL);

#define SPI_INSTANCE_ID CONFIG_SIDEWALK_NRFX_SPI_ID
#define SPI_SCK_PIN NRF_DT_GPIOS_TO_PSEL(DT_NODELABEL(nrfx_spi_sck), gpios)
#define SPI_MOSI_PIN NRF_DT_GPIOS_TO_PSEL(DT_NODELABEL(nrfx_spi_mosi), gpios)
#define SPI_MISO_PIN NRF_DT_GPIOS_TO_PSEL(DT_NODELABEL(nrfx_spi_miso), gpios)
#define SPI_CS_PIN NRF_DT_GPIOS_TO_PSEL(DT_NODELABEL(nrfx_spi_cs), gpios)

static const nrfx_spi_t spi = NRFX_SPI_INSTANCE(SPI_INSTANCE_ID);

static sid_error_t bus_serial_spi_xfer(const struct sid_pal_serial_bus_iface *iface,
const struct sid_pal_serial_bus_client *client, uint8_t *tx,
uint8_t *rx, size_t xfer_size);
static sid_error_t bus_serial_spi_destroy(const struct sid_pal_serial_bus_iface *iface);

static const struct sid_pal_serial_bus_iface bus_ops = {
.xfer = bus_serial_spi_xfer,
.destroy = bus_serial_spi_destroy,
};

inline static sid_error_t spi_transfer(const struct sid_pal_serial_bus_iface *iface,
const struct sid_pal_serial_bus_client *client, uint8_t *tx,
uint8_t *rx, size_t xfer_size)
{
ARG_UNUSED(iface);
ARG_UNUSED(client);

sid_error_t ret = SID_ERROR_NONE;
nrfx_err_t result;
nrfx_spi_xfer_desc_t trx_set =
NRFX_SPI_XFER_TRX(tx, tx ? xfer_size : 0, rx, rx ? xfer_size : 0);

nrfx_spi_config_t config =
NRFX_SPI_DEFAULT_CONFIG(SPI_SCK_PIN, SPI_MOSI_PIN, SPI_MISO_PIN, SPI_CS_PIN);

config.frequency = NRF_SPI_FREQ_8M;

result = nrfx_spi_init(&spi, &config, NULL, NULL);
if (result != NRFX_SUCCESS) {
LOG_ERR("NRFX driver init fail: %08x", result);
return SID_ERROR_IO_ERROR;
}

result = nrfx_spi_xfer(&spi, &trx_set, 0);
if (result != NRFX_SUCCESS) {
LOG_ERR("NRFX driver transfer fail: %08x", result);
ret = SID_ERROR_GENERIC;
}

nrfx_spi_uninit(&spi);

return ret;
}

static sid_error_t bus_serial_spi_xfer(const struct sid_pal_serial_bus_iface *iface,
const struct sid_pal_serial_bus_client *client, uint8_t *tx,
uint8_t *rx, size_t xfer_size)
{
LOG_DBG("%s(%p, %p, %p, %p, %d)", __func__, iface, client, (void *)tx, (void *)rx,
xfer_size);

return spi_transfer(iface, client, tx, rx, xfer_size);
}

static sid_error_t bus_serial_spi_destroy(const struct sid_pal_serial_bus_iface *iface)
{
LOG_DBG("%s(%p)", __func__, iface);
if (!iface) {
return SID_ERROR_INVALID_ARGS;
}

return SID_ERROR_NONE;
}

sid_error_t sid_pal_serial_bus_nordic_spi_create(const struct sid_pal_serial_bus_iface **iface,
const void *cfg)
{
ARG_UNUSED(cfg);

if (!iface) {
return SID_ERROR_INVALID_ARGS;
}

*iface = &bus_ops;

return SID_ERROR_NONE;
}

0 comments on commit 054ef29

Please sign in to comment.