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 17, 2024
1 parent 34856b2 commit a1f27ac
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 28 deletions.
33 changes: 33 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,39 @@ config SIDEWALK_PAL_RADIO_SOURCE
bool "Build sub-GHz radio driver from sources [EXPERIMENTAL]"
select EXPERIMENTAL

if SIDEWALK_SPI_BUS

choice SIDEWALK_NRFX_SPI_INSTANCE_ID
prompt "Instance of the NRX SPI driver"
depends on SOC_NRF52840
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 SOC_NRF52840
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;
};
};
18 changes: 14 additions & 4 deletions subsys/config/common/src/app_subGHz_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <sid_gpio_utils.h>
#include <sx126x_config.h>

#if CONFIG_SOC_NRF52840
#include <nrfx_spi.h>
#endif /* CONFIG_SOC_NRF52840 */

#include <app_subGHz_config.h>

#define REGION_US915
Expand Down Expand Up @@ -160,10 +164,16 @@ const radio_sx126x_device_config_t *get_radio_cfg(void)
radio_sx1262_cfg.bus_selector.client_selector =
sid_gpio_utils_register_gpio(
(struct gpio_dt_spec)GPIO_DT_SPEC_GET_OR(DT_NODELABEL(semtech_sx1262_cs), gpios, INVALID_DT_GPIO));
radio_sx1262_cfg.bus_selector.speed_hz = DT_PROP_OR(DT_NODELABEL(sid_semtech), clock_frequency, SPI_FREQUENCY_DEFAULT);

radio_sx1262_cfg.gpio_tx_bypass = sid_gpio_utils_register_gpio(
(struct gpio_dt_spec)GPIO_DT_SPEC_GET_OR(DT_NODELABEL(semtech_sx1262_tx_bypass), gpios, INVALID_DT_GPIO));
radio_sx1262_cfg.bus_selector.speed_hz =
#if CONFIG_SOC_NRF52840
NRF_SPI_FREQ_8M;
#else /* CONFIG_SOC_NRF52840 */
DT_PROP_OR(DT_NODELABEL(sid_semtech), clock_frequency, SPI_FREQUENCY_DEFAULT);
#endif /* CONFIG_SOC_NRF52840 */

radio_sx1262_cfg.gpio_tx_bypass =
sid_gpio_utils_register_gpio((struct gpio_dt_spec)GPIO_DT_SPEC_GET_OR(
DT_NODELABEL(semtech_sx1262_tx_bypass), gpios, INVALID_DT_GPIO));
return &radio_sx1262_cfg;
}

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(CONFIG_SOC_NRF52840)
zephyr_library_sources_ifdef(CONFIG_SIDEWALK_SPI_BUS sid_pal_serial_bus_nrfx_spi.c)
else()
zephyr_library_sources_ifdef(CONFIG_SIDEWALK_SPI_BUS sid_pal_serial_bus_spi.c)
endif() # CONFIG_SOC_NRF52840

zephyr_library_sources_ifdef(CONFIG_SIDEWALK sid_common.c)

Expand Down
89 changes: 89 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,89 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <app_subGHz_config.h>
#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,
};

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)
{
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 = get_radio_cfg()->bus_selector.speed_hz;

LOG_DBG("%s(%p, %p, %d)", __func__, (void *)tx, (void *)rx, xfer_size);

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_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 a1f27ac

Please sign in to comment.