diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 8a31ea3889771..b6b02271ad137 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -18,6 +18,7 @@ add_subdirectory_ifdef(CONFIG_ADC adc) add_subdirectory_ifdef(CONFIG_ARM_SIP_SVC_DRIVER sip_svc) add_subdirectory_ifdef(CONFIG_AUDIO audio) add_subdirectory_ifdef(CONFIG_AUXDISPLAY auxdisplay) +add_subdirectory_ifdef(CONFIG_BASIC_RADIO basic_radio) add_subdirectory_ifdef(CONFIG_BBRAM bbram) add_subdirectory_ifdef(CONFIG_BIOMETRICS biometrics) add_subdirectory_ifdef(CONFIG_BT_DRIVERS bluetooth) diff --git a/drivers/Kconfig b/drivers/Kconfig index 6db7929240704..6259d82e7913f 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -10,6 +10,7 @@ source "drivers/adc/Kconfig" source "drivers/audio/Kconfig" source "drivers/audio/mic_privacy/intel/Kconfig.mic_privacy" source "drivers/auxdisplay/Kconfig" +source "drivers/basic_radio/Kconfig" source "drivers/bbram/Kconfig" source "drivers/biometrics/Kconfig" source "drivers/bluetooth/Kconfig" diff --git a/drivers/basic_radio/CMakeLists.txt b/drivers/basic_radio/CMakeLists.txt new file mode 100644 index 0000000000000..e1869759a9d28 --- /dev/null +++ b/drivers/basic_radio/CMakeLists.txt @@ -0,0 +1,5 @@ +# Copyright (c) 2026 Texas Instruments Incorporated +# SPDX-License-Identifier: Apache-2.0 + +# Each vendor backend registers its own zephyr_library() inside its subdirectory. +add_subdirectory_ifdef(CONFIG_BASIC_RADIO_CC23X0 backends/ti/cc23x0) diff --git a/drivers/basic_radio/Kconfig b/drivers/basic_radio/Kconfig new file mode 100644 index 0000000000000..0960b9d31df35 --- /dev/null +++ b/drivers/basic_radio/Kconfig @@ -0,0 +1,42 @@ +# Copyright (c) 2026 Texas Instruments Incorporated +# SPDX-License-Identifier: Apache-2.0 + +menuconfig BASIC_RADIO + bool "Basic Radio Drivers [EXPERIMENTAL]" + select EXPERIMENTAL + select POLL + help + Vendor-agnostic API for basic radio commands + +if BASIC_RADIO + +module = BASIC_RADIO +module-str = basic_radio +source "subsys/logging/Kconfig.template.log_config" + +config BASIC_RADIO_INIT_PRIORITY + int "basic_radio initialization priority" + default 90 + help + POST_KERNEL init priority for basic_radio drivers. Must be higher + than the priority of any underlying vendor HAL init. + +config BASIC_RADIO_MAX_PACKET_LEN + int "Maximum payload length" + range 1 2047 + default 255 + help + Largest payload in bytes any PHY of the radio can send or receive. + Backends may support a lower maximum. + +config BASIC_RADIO_RX_BUFFER_COUNT + int "Number of RX buffers" + range 1 16 + default 3 + help + Number of RX buffers preallocated by the backend. + +rsource "backends/ti/cc23x0/Kconfig" +# add here more "backends/..." + +endif # BASIC_RADIO diff --git a/drivers/basic_radio/backends/ti/cc23x0/CMakeLists.txt b/drivers/basic_radio/backends/ti/cc23x0/CMakeLists.txt new file mode 100644 index 0000000000000..363bed6734867 --- /dev/null +++ b/drivers/basic_radio/backends/ti/cc23x0/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (c) 2026 Texas Instruments Incorporated +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library() +zephyr_library_sources(basic_radio_cc23x0.c) + +# Certified PHY catalog of the SoC, one file per device. To add a device, +# add basic_radio_cc23x0_phy_catalog_.c and its SoC to +# BASIC_RADIO_CC23X0_PHY_CATALOG in Kconfig. +zephyr_library_sources_ifdef( + CONFIG_BASIC_RADIO_CC23X0_PHY_CATALOG + basic_radio_cc23x0_phy_catalog_${CONFIG_SOC}.c +) + +zephyr_library_include_directories(.) diff --git a/drivers/basic_radio/backends/ti/cc23x0/Kconfig b/drivers/basic_radio/backends/ti/cc23x0/Kconfig new file mode 100644 index 0000000000000..96e503fe75b91 --- /dev/null +++ b/drivers/basic_radio/backends/ti/cc23x0/Kconfig @@ -0,0 +1,56 @@ +# Copyright (c) 2026 Texas Instruments Incorporated +# SPDX-License-Identifier: Apache-2.0 + +config BASIC_RADIO_CC23X0 + bool "TI CC23x0 RCL backend" + default y + depends on SOC_SERIES_CC23X0 + depends on DT_HAS_TI_CC23X0_BASIC_RADIO_ENABLED + depends on RCL_DRIVER + select SIMPLELINK_F3_RADIO_PHYS + select PM + help + Backend for TI CC23x0 SimpleLink SoCs. + +config BASIC_RADIO_CC23X0_RX_BUFFER_SIZE + int "RX MultiBuffer size in bytes" + depends on BASIC_RADIO_CC23X0 + default 512 + help + Size of each RCL MultiBuffer used for RX. One buffer can hold + several received packets. + +config BASIC_RADIO_CC23X0_MAX_HDR_LEN + int "Maximum frame header length in bytes" + depends on BASIC_RADIO_CC23X0 + range 1 4 + default 2 + help + Largest frame header, up to and including the length field, of any + PHY passed to basic_radio_config(). TX buffers are sized for it, and + basic_radio_config() rejects a PHY with a longer header. + +config BASIC_RADIO_CC23X0_PHY_CATALOG + bool + depends on BASIC_RADIO_CC23X0 + default y if SOC_CC2340R5 + help + The SoC has a certified PHY catalog, + basic_radio_cc23x0_phy_catalog_.c, built when this is set. It + defines the certified PHYs, the default PHY and the PHYs of the + standard PHY profiles. + +config BASIC_RADIO_CC23X0_DEFAULT_PHY + bool "Apply the default certified PHY at init" + depends on BASIC_RADIO_CC23X0_PHY_CATALOG + default y + help + Apply basic_radio_cc23x0_phy_default and its default runtime + parameters at init, so the radio is usable without calling + basic_radio_config(). + + Disable it for an application that selects its own PHY: the + default PHY's configuration is then not linked. No PHY is active + after init and operations return -ENODEV until + basic_radio_config() selects one. The certified PHYs stay + available to basic_radio_config() either way. diff --git a/drivers/basic_radio/backends/ti/cc23x0/basic_radio_cc23x0.c b/drivers/basic_radio/backends/ti/cc23x0/basic_radio_cc23x0.c new file mode 100644 index 0000000000000..706c59475ec02 --- /dev/null +++ b/drivers/basic_radio/backends/ti/cc23x0/basic_radio_cc23x0.c @@ -0,0 +1,1402 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * + * SPDX-License-Identifier: Apache-2.0 + * + * TI CC23x0 Basic Radio backend. + * + * Wraps the TI Radio Control Layer (RCL) commands behind the + * Zephyr basic_radio driver API. + * + * A PHY is a struct basic_radio_cc23x0_phy passed in basic_radio_config::phy + * (). It names the LRF_Config + * generated by SysConfig, the phy_features word and the default runtime + * parameters. Certified PHYs come from basic_radio_cc23x0_phy_catalog_.c, + * custom PHYs from the application. The frame header layout and the TX power + * table are read from the register list of the LRF_Config. The catalog's + * default PHY is applied at init. + * Devicetree describes the radio device only. + */ + +#define DT_DRV_COMPAT ti_cc23x0_basic_radio + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "basic_radio_cc23x0.h" + +LOG_MODULE_REGISTER(basic_radio_cc23x0, CONFIG_BASIC_RADIO_LOG_LEVEL); + +BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1, + "basic_radio_cc23x0 backend supports at most one enabled instance"); + +#define INST 0 +#define RX_BUFFER_COUNT CONFIG_BASIC_RADIO_RX_BUFFER_COUNT +#define RX_BUFFER_SIZE CONFIG_BASIC_RADIO_CC23X0_RX_BUFFER_SIZE +#define MAX_PACKET_LEN CONFIG_BASIC_RADIO_MAX_PACKET_LEN +#define MAX_HDR_LEN CONFIG_BASIC_RADIO_CC23X0_MAX_HDR_LEN + +/* RCL start times and timestamps count in 250 ns SYSTIM steps */ +#define SYSTIM_TICKS_PER_US 4U + +/* Longest tone: its hard stop time is a 32-bit count of 250 ns steps, about 1073 s */ +#define TONE_DURATION_MAX_US (UINT32_MAX / SYSTIM_TICKS_PER_US) + +/* + * RCL compares start times as signed 32-bit differences of 250 ns steps, + * a window of +-536 s. Keep a margin below it. + */ +#define SCHED_HORIZON_US 500000000U + +/* Synthesizer step: 48 MHz reference / 2^18, in millihertz */ +#define SYNTH_STEP_MILLIHZ ((48000000ULL * 1000ULL) >> 18) + +/* Worst-case time to retune the synthesizer */ +#define RETUNE_TIME_US 116U + +/* Shorter than the 71 min wrap period of the 1 us TIME1U count */ +#define TIME_REFRESH_PERIOD K_MINUTES(30) + +/* The driver writes the length field as one byte */ +BUILD_ASSERT(MAX_PACKET_LEN <= UINT8_MAX, + "CONFIG_BASIC_RADIO_MAX_PACKET_LEN exceeds 255, the CC23x0 maximum"); + +/* Default PHY of the device catalog, unless the application selects its own */ +#if defined(CONFIG_BASIC_RADIO_CC23X0_DEFAULT_PHY) +#define DEFAULT_PHY basic_radio_cc23x0_phy_default +#else +#define DEFAULT_PHY NULL +#endif + +/* + * Frame header layout. The generic PBE reads the length field position and + * size from PBE_GENERIC_RAM.LENCFG, which the register list of the + * LRF_Config writes. The driver reads it from that list, so every PHY gets + * its own layout and PHYs with different layouts can be switched at + * runtime. TX buffers are sized for MAX_HDR_LEN. + */ + +/* LENCFG as addressed by SW region headers (offset from PBE_RAM_BASE_ADDR) */ +#define LENCFG_OFFSET ((LRFD_BUFRAM_BASE - PBE_RAM_BASE_ADDR) + PBE_GENERIC_RAM_O_LENCFG) + +/* Region types of LRF_RegionOperation that carry no data words */ +#define REGION_IS_CLEAR(op) \ + (((op) == HW_Region_Clear) || ((op) == SW_Region_Clear) || ((op) == Par_Region_Clear)) + +/* Segment selection, same rule as LRF_applySettings() on a full setup */ +static bool segment_is_applied(LRF_ConfigWord hdr, uint16_t phy_features) +{ + uint16_t mask = hdr.segment.featureMask; + + if (hdr.segment.invertedFeatureMask != 0U) { + return (mask & phy_features) == 0U; + } + + return (mask == 0U) || ((mask & phy_features) != 0U); +} + +/* Settings of a PHY read from its register list */ +struct phy_settings { + /* PBE_GENERIC_RAM.LENCFG, valid if is_lencfg_found */ + uint16_t lencfg; + bool is_lencfg_found; + /* LRF_SwParam.txPowerTable, NULL if not set */ + const LRF_TxPowerTable *tx_power_table; +}; + +/* LRF_SwParam.txPowerTable as addressed by Par_Write_32bit region headers */ +#define TX_POWER_TABLE_OFFSET offsetof(LRF_SwParam, txPowerTable) + +/* Record the LENCFG and TX power table writes of one region. Later writes win, as on the radio. */ +static void region_scan(LRF_RegionOperation op, uint32_t start, const uint32_t *data, + uint32_t reg_len, struct phy_settings *settings) +{ + for (uint32_t i = 0; i < reg_len; i++) { + switch (op) { + case SW_Write_16bit: + /* Two halfwords per word, low half first */ + if ((start + (2U * i)) == LENCFG_OFFSET) { + settings->lencfg = + ((const LRF_ConfigWord *)&data[i / 2U])->value16[i % 2U]; + settings->is_lencfg_found = true; + } + break; + + case SW_Write_32bit: + if ((start + (4U * i)) == LENCFG_OFFSET) { + settings->lencfg = ((const LRF_ConfigWord *)&data[i])->value16[0]; + settings->is_lencfg_found = true; + } + break; + + case SW_Write_16bit_sparse: + if ((start + ((const LRF_ConfigWord *)&data[i])->sparse.address) == + LENCFG_OFFSET) { + settings->lencfg = + ((const LRF_ConfigWord *)&data[i])->sparse.value16; + settings->is_lencfg_found = true; + } + break; + + case Par_Write_32bit: + /* Pointer members of LRF_SwParam, start is a byte offset into it */ + if ((start + (4U * i)) == TX_POWER_TABLE_OFFSET) { + settings->tx_power_table = + (const LRF_TxPowerTable *)(uintptr_t)data[i]; + } + break; + + default: + /* HW regions do not touch PBE RAM or the parameter list */ + return; + } + } +} + +/* + * Read the LENCFG and the TX power table of a PHY from its register list, + * applying only the segments its phy_features select. The radio applies the + * list at command start, so neither can be read back from the radio before + * the first command. + */ +static int phy_settings_find(const struct basic_radio_cc23x0_phy *phy, + struct phy_settings *settings) +{ + const LRF_Config *lrf = phy->lrf_config; + + settings->is_lencfg_found = false; + settings->lencfg = 0U; + settings->tx_power_table = NULL; + + if ((lrf == NULL) || (lrf->regConfigList == NULL)) { + return -EINVAL; + } + + /* LENCFG lives at this offset only in the generic PBE RAM map */ + if (lrf->pbeImage != (const LRF_TOPsmImage *)LRF_PBE_binary_generic) { + return -ENOTSUP; + } + + for (uint32_t e = 0; e < lrf->regConfigList->numEntries; e++) { + const uint32_t *word = &lrf->regConfigList->entries[e]->value32; + LRF_ConfigWord entry_hdr = {.value32 = *word}; + uint32_t total_len = entry_hdr.segment.length + 1U; + + /* A compound header is followed by several segments */ + if (entry_hdr.segment.compoundSegment != 0U) { + total_len = entry_hdr.segment.length; + word++; + } + + while (total_len > 0U) { + LRF_ConfigWord hdr = {.value32 = *word}; + uint32_t seg_len = hdr.segment.length; + + word++; + + if ((seg_len == 0U) || (seg_len >= total_len)) { + return -EINVAL; + } + + total_len -= seg_len + 1U; + + if (!segment_is_applied(hdr, phy->phy_features)) { + word += seg_len; + continue; + } + + /* Walk the regions of the segment */ + while (seg_len > 0U) { + LRF_ConfigWord reg = {.value32 = *word}; + LRF_RegionOperation op = (LRF_RegionOperation)reg.region.type; + uint32_t reg_len = reg.region.lengthMinus1 + 1U; + uint32_t num_words = reg_len; + + word++; + seg_len--; + + if (REGION_IS_CLEAR(op)) { + num_words = 0U; + } else if ((op == HW_Write_16bit) || (op == SW_Write_16bit)) { + num_words = DIV_ROUND_UP(reg_len, 2U); + } + + if (num_words > seg_len) { + return -EINVAL; + } + + region_scan(op, reg.region.startAddress, word, reg_len, settings); + + word += num_words; + seg_len -= num_words; + } + } + } + + return 0; +} + +/* + * Frame header layout of a PHY. The driver writes the length as one byte, + * so the field must be byte aligned and at most 8 bits. + */ +static int phy_hdr_get(const struct basic_radio_cc23x0_phy *phy, + const struct phy_settings *settings, struct basic_radio_cc23x0_hdr *hdr) +{ + if (!settings->is_lencfg_found) { + LOG_ERR("PHY %p: no generic PBE LENCFG", (const void *)phy); + + return -ENOTSUP; + } + + uint16_t lencfg = settings->lencfg; + uint32_t len_pos = + (lencfg & PBE_GENERIC_RAM_LENCFG_LENPOS_M) >> PBE_GENERIC_RAM_LENCFG_LENPOS_S; + uint32_t num_len_bits = (lencfg & PBE_GENERIC_RAM_LENCFG_NUMLENBITS_M) >> + PBE_GENERIC_RAM_LENCFG_NUMLENBITS_S; + uint32_t len = DIV_ROUND_UP(len_pos + num_len_bits, CHAR_BIT); + + LOG_DBG("PHY %p: LENPOS=%u NUMLENBITS=%u", (const void *)phy, len_pos, num_len_bits); + + if (((len_pos % CHAR_BIT) != 0U) || (num_len_bits == 0U) || (num_len_bits > CHAR_BIT)) { + return -ENOTSUP; + } + + if (len > MAX_HDR_LEN) { + LOG_ERR("PHY %p: header of %u bytes exceeds CONFIG_BASIC_RADIO_CC23X0_MAX_HDR_LEN", + (const void *)phy, len); + + return -ENOTSUP; + } + + hdr->len_index = (uint8_t)(len_pos / CHAR_BIT); + hdr->len = (uint8_t)len; + hdr->len_mask = (uint8_t)BIT_MASK(num_len_bits); + + return 0; +} + +/* TX power range: the lowest and highest entry of the TX power table */ +static void power_range_get(const LRF_TxPowerTable *table, + struct basic_radio_cc23x0_power_range *range) +{ + range->is_known = false; + range->min_dbm = INT8_MIN; + range->max_dbm = INT8_MAX; + + if ((table == NULL) || (table->numEntries == 0U)) { + return; + } + + range->is_known = true; + range->min_dbm = INT8_MAX; + range->max_dbm = INT8_MIN; + + for (uint32_t i = 0; i < table->numEntries; i++) { + int8_t dbm = table->powerTable[i].power.dBm; + + range->min_dbm = MIN(range->min_dbm, dbm); + range->max_dbm = MAX(range->max_dbm, dbm); + } +} + +/* + * Frame header layout and TX power range of a PHY, read from its register + * list before the PHY is applied + */ +static int phy_info_get(const struct basic_radio_cc23x0_phy *phy, + struct basic_radio_cc23x0_hdr *hdr, + struct basic_radio_cc23x0_power_range *power) +{ + struct phy_settings settings; + int ret = phy_settings_find(phy, &settings); + + if (ret != 0) { + LOG_ERR("PHY %p: register list not supported: %d", (const void *)phy, ret); + } else { + ret = phy_hdr_get(phy, &settings, hdr); + } + + if (ret == 0) { + power_range_get(settings.tx_power_table, power); + } + + return ret; +} + +/* RCL command templates from TI Radio Control Layer (RCL). + * RCL_CmdGenericTx_Default()/RCL_CmdGenericRx_Default() provide zero-initialized + * structs with default cmdId, scheduling, status, etc. Runtime fields + * (frequency, sync word, power, buffers) are filled in by rf_params_set() + * and send()/recv(). + */ +static RCL_CmdGenericTx tx_cmd = RCL_CmdGenericTx_Default(); +static RCL_CmdGenericRx rx_cmd = RCL_CmdGenericRx_Default(); +static RCL_CmdGenericTxTest tone_cmd = RCL_CmdGenericTxTest_Default(); + +static uint32_t rx_bufs[RX_BUFFER_COUNT][RCL_MultiBuffer_len_u32(RX_BUFFER_SIZE)]; +/* One operation runs at a time, so one TX buffer is enough */ +static uint32_t tx_buf_mem[RCL_TxBuffer_len_u32(NUM_PAD_BYTES, MAX_HDR_LEN, MAX_PACKET_LEN)]; + +static const struct device *basic_radio_cc23x0_dev; + +/* 64-bit extension of SYSTIM TIME1U: wrap count and last read value */ +static uint32_t time_hi; +static uint32_t time_last; +static struct k_timer time_refresh_timer; + +/* Forward declarations */ +static void rf_params_set(struct basic_radio_cc23x0_data *data, uint32_t frequency_hz, + uint32_t sync_word, int8_t tx_power_dbm, uint16_t max_packet_len); +static int start_time_set(RCL_Command *cmd, bool is_scheduled, uint64_t start_us); +static uint64_t rx_timestamp_get(void); +static uint64_t basic_radio_cc23x0_time_get(const struct device *dev); +static void rx_callback(RCL_Command *cmd, LRF_Events lrf_evt, RCL_Events rcl_evt); +static void tx_callback(RCL_Command *cmd, LRF_Events lrf_evt, RCL_Events rcl_evt); +static void tone_callback(RCL_Command *cmd, LRF_Events lrf_evt, RCL_Events rcl_evt); +static void time_refresh(struct k_timer *timer); + +/* Additional forward declarations */ +static void rx_entries_report(struct basic_radio_cc23x0_data *data); +static void op_done(struct basic_radio_cc23x0_data *data, enum basic_radio_evt_type type, + RCL_CommandStatus status); +static int status_to_errno(RCL_CommandStatus status); + +/* + * Carrier frequency inside the characterized range of a PHY + */ +static bool frequency_is_valid(const struct basic_radio_cc23x0_phy *phy, uint64_t frequency_hz) +{ + return (frequency_hz >= phy->frequency_min) && (frequency_hz <= phy->frequency_max); +} + +/* RCL commands carry a 32-bit sync word */ +static bool sync_word_is_valid(uint64_t sync_word) +{ + return sync_word <= UINT32_MAX; +} + +/* Largest payload the length field of the active PHY and the TX buffers can carry */ +static uint16_t max_len_get(const struct basic_radio_cc23x0_hdr *hdr) +{ + return (uint16_t)MIN(hdr->len_mask, MAX_PACKET_LEN); +} + +/* + * Make a PHY the active one. Its header layout and TX power range are read + * by the caller before the radio is owned. + * + * Same LRF_Config as the active PHY (sub-PHY change, e.g. BLE 1M to 2M): + * only phy_features differs. RCL applies the feature-gated segments at + * the next command setup; no RCL_close()/RCL_open(). + * Other LRF_Config: RCL_close() + RCL_open() reload the PBE/MCE/RFE + * images and the full register list. + * + * Returns 0 on success, -EIO if RCL reopen fails. + */ +static int phy_select(struct basic_radio_cc23x0_data *data, + const struct basic_radio_cc23x0_phy *phy, + const struct basic_radio_cc23x0_hdr *hdr, + const struct basic_radio_cc23x0_power_range *power) +{ + if ((data->handle == NULL) || (data->phy == NULL) || + (phy->lrf_config != data->phy->lrf_config)) { + if (data->handle != NULL) { + RCL_close(data->handle); + data->handle = NULL; + } + + data->handle = RCL_open(&data->rcl_client, phy->lrf_config); + + if (data->handle == NULL) { + LOG_ERR("RCL_open failed for PHY %p", (const void *)phy); + data->phy = NULL; + + return -EIO; + } + } + + tx_cmd.common.phyFeatures = phy->phy_features; + rx_cmd.common.phyFeatures = phy->phy_features; + tone_cmd.common.phyFeatures = phy->phy_features; + data->phy = phy; + data->hdr = *hdr; + data->power = *power; + + return 0; +} + +/* TX power inside the TX power table of the active PHY, if it has one */ +static bool tx_power_is_valid(const struct basic_radio_cc23x0_power_range *power, + int8_t tx_power_dbm) +{ + if (!power->is_known) { + return true; + } + + return (tx_power_dbm >= power->min_dbm) && (tx_power_dbm <= power->max_dbm); +} + +/* Driver API: Time Get */ +static uint64_t basic_radio_cc23x0_time_get(const struct device *dev) +{ + ARG_UNUSED(dev); + + /* Read and extend in one step, so concurrent callers see one wrap */ + unsigned int key = irq_lock(); + uint32_t now = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U); + + if (now < time_last) { + time_hi++; + } + + time_last = now; + + uint64_t ticks = ((uint64_t)time_hi << 32) | now; + + irq_unlock(key); + + return ticks; +} + +/* Read the time often enough to catch every wrap of TIME1U */ +static void time_refresh(struct k_timer *timer) +{ + ARG_UNUSED(timer); + + (void)basic_radio_cc23x0_time_get(basic_radio_cc23x0_dev); +} + +/* + * Program the start of a command. An absolute start must be in the future + * and within the RCL compare window. + */ +static int start_time_set(RCL_Command *cmd, bool is_scheduled, uint64_t start_us) +{ + if (!is_scheduled) { + cmd->scheduling = RCL_Schedule_Now; + + return 0; + } + + uint64_t now = basic_radio_cc23x0_time_get(basic_radio_cc23x0_dev); + + if (start_us <= now) { + return -ETIME; + } + + if ((start_us - now) > SCHED_HORIZON_US) { + return -EINVAL; + } + + /* Low 32 bits of the 250 ns SYSTIM count */ + cmd->scheduling = RCL_Schedule_AbsTime; + cmd->timing.absStartTime = (uint32_t)(start_us * SYSTIM_TICKS_PER_US); + + return 0; +} + +/* RSSI of the last received packet, BASIC_RADIO_RSSI_INVALID if the radio has none */ +static int8_t rx_rssi_get(void) +{ + int8_t rssi = rx_cmd.stats->lastRssi; + + return (rssi == LRF_RSSI_INVALID) ? BASIC_RADIO_RSSI_INVALID : rssi; +} + +/* + * Sync-found time of the last received packet on the 64-bit time base, 0 + * if the radio has none. RCL captures it when the sync word is found. + */ +static uint64_t rx_timestamp_get(void) +{ + if (!rx_cmd.stats->timestampValid) { + return 0; + } + + /* The packet is in the past: age in 250 ns steps, modulo 2^32 */ + uint64_t now = basic_radio_cc23x0_time_get(basic_radio_cc23x0_dev); + uint32_t age = (uint32_t)(now * SYSTIM_TICKS_PER_US) - rx_cmd.stats->lastTimestamp; + + return now - (age / SYSTIM_TICKS_PER_US); +} + +/* + * Write the runtime parameters into the driver cache and both RCL commands. + * Shared by init and basic_radio_config(). + */ +static void rf_params_set(struct basic_radio_cc23x0_data *data, uint32_t frequency_hz, + uint32_t sync_word, int8_t tx_power_dbm, uint16_t max_packet_len) +{ + data->cfg_frequency_hz = frequency_hz; + data->cfg_sync_word = sync_word; + data->cfg_tx_power_dbm = tx_power_dbm; + data->cfg_max_packet_len = MIN(max_packet_len, MAX_PACKET_LEN); + + tx_cmd.rfFrequency = frequency_hz; + rx_cmd.rfFrequency = frequency_hz; + + tx_cmd.syncWord = sync_word; + rx_cmd.syncWordA = sync_word; + + tx_cmd.txPower.dBm = tx_power_dbm; + tx_cmd.txPower.fraction = 0; + + rx_cmd.maxPktLen = data->cfg_max_packet_len; +} + +/* RCL Rx Callback */ +static void rx_callback(RCL_Command *cmd, LRF_Events lrf_evt, RCL_Events rcl_evt) +{ + struct basic_radio_cc23x0_data *data = basic_radio_cc23x0_dev->data; + + if (lrf_evt.rxOk) { + data->rx_ok_count++; + } + + if (lrf_evt.rxNok) { + data->rx_crc_err_count++; + } + + if (rcl_evt.cmdStarted) { + data->cmd_started_count++; + } + + if (rcl_evt.startRejected || rcl_evt.startDelayed) { + data->sched_reject_count++; + } + + /* Discarded packets are not stored, report them from the LRF event */ + if (rx_cmd.config.discardRxPackets && lrf_evt.rxOk) { + struct basic_radio_evt evt = { + .type = BASIC_RADIO_EVT_RX_PKT, + .timestamp_us = rx_timestamp_get(), + .info = {.rssi = rx_rssi_get()}, + }; + + data->cb(basic_radio_cc23x0_dev, &evt, data->user_data); + } + + /* Packets stored before the end are reported before RX_DONE */ + if (rcl_evt.rxEntryAvail || rcl_evt.lastCmdDone) { + rx_entries_report(data); + } + + if (rcl_evt.lastCmdDone) { + if (status_to_errno(cmd->status) == -ENOBUFS) { + data->rx_overflow_count++; + } + + op_done(data, BASIC_RADIO_EVT_RX_DONE, cmd->status); + } +} + +/* RCL Tx Callback */ +static void tx_callback(RCL_Command *cmd, LRF_Events lrf_evt, RCL_Events rcl_evt) +{ + ARG_UNUSED(lrf_evt); + struct basic_radio_cc23x0_data *data = basic_radio_cc23x0_dev->data; + + if (rcl_evt.cmdStarted) { + data->cmd_started_count++; + } + + if (rcl_evt.startRejected || rcl_evt.startDelayed) { + data->sched_reject_count++; + } + + if (!rcl_evt.lastCmdDone) { + return; + } + + if (cmd->status >= RCL_CommandStatus_Error) { + LOG_ERR("TX error: status=%d", cmd->status); + } + + op_done(data, BASIC_RADIO_EVT_TX_DONE, cmd->status); +} + +/* RCL Tone Callback */ +static void tone_callback(RCL_Command *cmd, LRF_Events lrf_evt, RCL_Events rcl_evt) +{ + ARG_UNUSED(lrf_evt); + struct basic_radio_cc23x0_data *data = basic_radio_cc23x0_dev->data; + + if (rcl_evt.lastCmdDone) { + op_done(data, BASIC_RADIO_EVT_TONE_DONE, cmd->status); + } +} + +/* Report one stored packet, with the RSSI and timestamp of the last received packet if is_last */ +static void rx_entry_report(struct basic_radio_cc23x0_data *data, RCL_Buffer_DataEntry *entry, + bool is_last) +{ + uint8_t *pkt = &entry->pad0 + entry->numPad; + struct basic_radio_evt evt = { + .type = BASIC_RADIO_EVT_RX_PKT, + .timestamp_us = is_last ? rx_timestamp_get() : 0U, + .data = pkt + data->hdr.len, + .len = (uint16_t)(pkt[data->hdr.len_index] & data->hdr.len_mask), + .info = {.rssi = is_last ? rx_rssi_get() : BASIC_RADIO_RSSI_INVALID}, + }; + + data->cb(basic_radio_cc23x0_dev, &evt, data->user_data); +} + +/* Clear the drained buffers and hand them back to RX */ +static void rx_buffers_recycle(List_List *consumed) +{ + RCL_MultiBuffer *mb; + + while ((mb = RCL_MultiBuffer_get(consumed)) != NULL) { + RCL_MultiBuffer_clear(mb); + RCL_MultiBuffer_put(&rx_cmd.rxBuffers, mb); + } +} + +/* + * Report every stored packet. RCL keeps the RSSI and timestamp of the last + * received packet only, so a packet followed by another stored one reports + * them as unknown. + * + * RCL moves a buffer to the consumed list as soon as its last entry is + * taken. The buffer goes back to RX right after that entry is reported, + * not at the end of the drain: packets keep arriving while the callback + * runs, and RCL ends the reception when it finds no free buffer. + */ +static void rx_entries_report(struct basic_radio_cc23x0_data *data) +{ + List_List consumed = {0}; + RCL_Buffer_DataEntry *entry; + + while ((entry = RCL_MultiBuffer_RxEntry_get(&rx_cmd.rxBuffers, &consumed)) != NULL) { + bool is_last = RCL_MultiBuffer_RxEntry_isEmpty(&rx_cmd.rxBuffers); + + rx_entry_report(data, entry, is_last); + rx_buffers_recycle(&consumed); + } +} + +/* + * Report the final event of the active operation. The radio is idle before + * the callback runs, so the callback can start the next operation. + */ +static void op_done(struct basic_radio_cc23x0_data *data, enum basic_radio_evt_type type, + RCL_CommandStatus status) +{ + basic_radio_callback_t cb = data->cb; + void *user_data = data->user_data; + struct basic_radio_evt evt = { + .type = type, + .status = status_to_errno(status), + }; + + atomic_set(&data->state, RADIO_STATE_IDLE); + cb(basic_radio_cc23x0_dev, &evt, user_data); +} + +/* Final status of an RCL command as reported in basic_radio_evt::status */ +static int status_to_errno(RCL_CommandStatus status) +{ + /* A timeout stop is the programmed end of a tone */ + if ((status == RCL_CommandStatus_Finished) || RCL_CommandStatus_isAnyTimeoutStop(status)) { + return 0; + } + + if (RCL_CommandStatus_isAnyApiStop(status)) { + return -ECANCELED; + } + + if ((status == RCL_CommandStatus_RejectedStart) || + (status == RCL_CommandStatus_Error_StartTooLate)) { + return -ETIME; + } + + /* + * RX found no free buffer (the generic handler reports it as buffer + * corruption) or the RX FIFO overflowed: packets came in faster than + * they were drained. + */ + if ((status == RCL_CommandStatus_Error_RxBufferCorruption) || + (status == RCL_CommandStatus_Error_RxFifo)) { + return -ENOBUFS; + } + + return -EIO; +} + +/* Driver API: Configuration */ + +/* Check a configuration against the radio state and apply it; the caller owns the radio */ +static int config_apply(struct basic_radio_cc23x0_data *dd, const struct basic_radio_config *cfg) +{ + const struct basic_radio_cc23x0_phy *phy = dd->phy; + struct basic_radio_cc23x0_hdr hdr = dd->hdr; + struct basic_radio_cc23x0_power_range power = dd->power; + int ret; + + /* Opaque pointer of a PHY defined with BASIC_RADIO_CC23X0_PHY_DEFINE() */ + if (cfg->phy != NULL) { + phy = (const struct basic_radio_cc23x0_phy *)cfg->phy; + } + + if (phy == NULL) { + LOG_ERR("no active PHY"); + + return -ENODEV; + } + + bool is_new_phy = (phy != dd->phy); + + /* Header layout and TX power range of a new PHY, before touching the radio */ + if (is_new_phy) { + ret = phy_info_get(phy, &hdr, &power); + + if (ret != 0) { + return ret; + } + } + + /* A field without its flag keeps its value, or takes the default of a newly selected PHY */ + uint32_t base_freq = is_new_phy ? phy->frequency : dd->cfg_frequency_hz; + uint32_t base_sync_word = is_new_phy ? phy->sync_word : dd->cfg_sync_word; + int8_t base_tx_power = is_new_phy ? phy->tx_power : dd->cfg_tx_power_dbm; + + uint64_t frequency = + ((cfg->flags & BASIC_RADIO_CFG_FREQUENCY) != 0U) ? cfg->frequency_hz : base_freq; + uint64_t sync_word = + ((cfg->flags & BASIC_RADIO_CFG_SYNC_WORD) != 0U) ? cfg->sync_word : base_sync_word; + int8_t tx_power = + ((cfg->flags & BASIC_RADIO_CFG_TX_POWER) != 0U) ? cfg->tx_power_dbm : base_tx_power; + uint16_t max_len; + + /* A kept length is reduced to what the length field of the PHY can carry */ + if ((cfg->flags & BASIC_RADIO_CFG_MAX_PACKET_LEN) != 0U) { + max_len = cfg->max_packet_len; + + if ((max_len == 0U) || (max_len > max_len_get(&hdr))) { + LOG_ERR("max_packet_len %u outside 1..%u for PHY %p", max_len, + max_len_get(&hdr), (const void *)phy); + + return -EINVAL; + } + } else { + max_len = MIN(dd->cfg_max_packet_len, max_len_get(&hdr)); + } + + /* Evaluate the runtime parameters against the PHY */ + if (!frequency_is_valid(phy, frequency)) { + LOG_ERR("frequency %llu outside the characterized range of PHY %p", + (unsigned long long)frequency, (const void *)phy); + + return -EINVAL; + } + + if (!sync_word_is_valid(sync_word)) { + return -EINVAL; + } + + if (!tx_power_is_valid(&power, tx_power)) { + LOG_ERR("TX power %d dBm outside the power table of PHY %p", tx_power, + (const void *)phy); + + return -EINVAL; + } + + /* A new PHY changes the RCL settings */ + if (is_new_phy) { + ret = phy_select(dd, phy, &hdr, &power); + + if (ret != 0) { + return ret; + } + } + + /* + * After changing the PHY, modify the runtime parameters. Frequency and + * sync word were checked against the 32-bit ranges of the PHY and RCL. + */ + rf_params_set(dd, (uint32_t)frequency, (uint32_t)sync_word, tx_power, max_len); + + return 0; +} + +static int basic_radio_cc23x0_config_impl(const struct device *dev, + const struct basic_radio_config *cfg) +{ + static const uint32_t supported_cfg_flags = + BASIC_RADIO_CFG_FREQUENCY | BASIC_RADIO_CFG_SYNC_WORD | BASIC_RADIO_CFG_TX_POWER | + BASIC_RADIO_CFG_MAX_PACKET_LEN; + struct basic_radio_cc23x0_data *dd = dev->data; + int ret; + + if ((cfg->flags & ~supported_cfg_flags) != 0U) { + return -ENOTSUP; + } + + /* Own the radio, so no operation reads the PHY while it is checked and changed */ + if (!atomic_cas(&dd->state, RADIO_STATE_IDLE, RADIO_STATE_CONFIG)) { + return -EBUSY; + } + + ret = config_apply(dd, cfg); + + atomic_set(&dd->state, RADIO_STATE_IDLE); + + return ret; +} + +/* Driver API: Get Caps */ +static int basic_radio_cc23x0_get_caps(const struct device *dev, struct basic_radio_caps *caps) +{ + const struct basic_radio_cc23x0_data *data = dev->data; + const struct basic_radio_cc23x0_phy *phy = data->phy; + + if (phy == NULL) { + return -ENODEV; + } + + *caps = (struct basic_radio_caps){ + .flags = BASIC_RADIO_CAP_SCHED | BASIC_RADIO_CAP_TONE | + BASIC_RADIO_CAP_STOP_GRACEFUL, + .freq_min = phy->frequency_min, + .freq_max = phy->frequency_max, + .freq_resolution_millihz = (uint32_t)SYNTH_STEP_MILLIHZ, + .retune_time_us = RETUNE_TIME_US, + .sched_horizon_us = SCHED_HORIZON_US, + .tx_power_min_dbm = data->power.min_dbm, + .tx_power_max_dbm = data->power.max_dbm, + }; + + return 0; +} + +/* Driver API: Send */ + +/* Check a packet against the active PHY and submit it; the caller owns the radio */ +static int send_start(struct basic_radio_cc23x0_data *dd, const uint8_t *data, uint16_t len, + const struct basic_radio_tx_params *params, uint32_t flags) +{ + const struct basic_radio_cc23x0_phy *phy = dd->phy; + + if (phy == NULL) { + return -ENODEV; + } + + /* The configured maximum also fits the length field of the PHY and the TX buffer */ + if (len > dd->cfg_max_packet_len) { + return -EINVAL; + } + + uint64_t frequency = dd->cfg_frequency_hz; + uint64_t sync_word = dd->cfg_sync_word; + int8_t tx_power_dbm = dd->cfg_tx_power_dbm; + + if ((flags & BASIC_RADIO_TX_FLAG_FREQUENCY) != 0U) { + frequency = params->frequency_hz; + } + + if ((flags & BASIC_RADIO_TX_FLAG_SYNC_WORD) != 0U) { + sync_word = params->sync_word; + } + + if ((flags & BASIC_RADIO_TX_FLAG_POWER) != 0U) { + tx_power_dbm = params->tx_power_dbm; + } + + if (!frequency_is_valid(phy, frequency) || !sync_word_is_valid(sync_word) || + !tx_power_is_valid(&dd->power, tx_power_dbm)) { + return -EINVAL; + } + + RCL_Buffer_TxBuffer *tx_buf = (RCL_Buffer_TxBuffer *)tx_buf_mem; + uint8_t *tx_data = RCL_TxBuffer_init(tx_buf, NUM_PAD_BYTES, dd->hdr.len, len); + + memset(tx_data, 0, dd->hdr.len_index); + tx_data[dd->hdr.len_index] = (uint8_t)len; + memcpy(&tx_data[dd->hdr.len], data, len); + tx_cmd.common.status = RCL_CommandStatus_Idle; + memset(&tx_cmd.txBuffers, 0, sizeof(tx_cmd.txBuffers)); + RCL_TxBuffer_put(&tx_cmd.txBuffers, tx_buf); + + tx_cmd.rfFrequency = (uint32_t)frequency; + tx_cmd.syncWord = (uint32_t)sync_word; + tx_cmd.txPower.dBm = tx_power_dbm; + + int ret = start_time_set(&tx_cmd.common, (flags & BASIC_RADIO_TX_FLAG_SCHED_ABS) != 0U, + (params != NULL) ? params->abs_start_time_us : 0U); + + if (ret != 0) { + return ret; + } + + /* A rejected submit reports no event. Otherwise tx_callback ends the operation */ + if (RCL_Command_submit(dd->handle, &tx_cmd.common) >= RCL_CommandStatus_Error) { + return -EIO; + } + + return 0; +} + +static int basic_radio_cc23x0_send_async(const struct device *dev, const uint8_t *data, + uint16_t len, const struct basic_radio_tx_params *params, + basic_radio_callback_t cb, void *user_data) +{ + static const uint32_t supported_tx_flags = + BASIC_RADIO_TX_FLAG_SCHED_ABS | BASIC_RADIO_TX_FLAG_FREQUENCY | + BASIC_RADIO_TX_FLAG_SYNC_WORD | BASIC_RADIO_TX_FLAG_POWER; + struct basic_radio_cc23x0_data *dd = dev->data; + uint32_t flags = (params != NULL) ? params->flags : 0U; + int ret; + + if ((len == 0U) || (cb == NULL)) { + return -EINVAL; + } + + if ((flags & ~supported_tx_flags) != 0U) { + return -ENOTSUP; + } + + /* Own the radio before reading its state, so a PHY change cannot come in between */ + if (!atomic_cas(&dd->state, RADIO_STATE_IDLE, RADIO_STATE_TX_ACTIVE)) { + return -EBUSY; + } + + dd->cb = cb; + dd->user_data = user_data; + + ret = send_start(dd, data, len, params, flags); + + if (ret != 0) { + atomic_set(&dd->state, RADIO_STATE_IDLE); + } + + return ret; +} + +/* Driver API: Receive */ + +/* Check a reception against the active PHY and submit it; the caller owns the radio */ +static int recv_start(struct basic_radio_cc23x0_data *data, + const struct basic_radio_rx_params *params, uint32_t flags) +{ + const struct basic_radio_cc23x0_phy *phy = data->phy; + + if (phy == NULL) { + return -ENODEV; + } + + uint64_t frequency = data->cfg_frequency_hz; + uint64_t sync_word = data->cfg_sync_word; + uint16_t max_len = data->cfg_max_packet_len; + + if ((flags & BASIC_RADIO_RX_FLAG_FREQUENCY) != 0U) { + frequency = params->frequency_hz; + } + + if ((flags & BASIC_RADIO_RX_FLAG_SYNC_WORD) != 0U) { + sync_word = params->sync_word; + } + + if ((flags & BASIC_RADIO_RX_FLAG_MAX_LEN) != 0U) { + max_len = params->max_packet_len; + } + + if (!frequency_is_valid(phy, frequency) || !sync_word_is_valid(sync_word)) { + return -EINVAL; + } + + if ((max_len == 0U) || (max_len > max_len_get(&data->hdr))) { + return -EINVAL; + } + + rx_cmd.config.repeated = ((flags & BASIC_RADIO_RX_FLAG_ONE_SHOT) != 0U) ? 0U : 1U; + rx_cmd.config.discardRxPackets = + ((flags & BASIC_RADIO_RX_FLAG_DISCARD_PAYL) != 0U) ? 1U : 0U; + rx_cmd.rfFrequency = (uint32_t)frequency; + rx_cmd.syncWordA = (uint32_t)sync_word; + rx_cmd.maxPktLen = max_len; + + int ret = start_time_set(&rx_cmd.common, (flags & BASIC_RADIO_RX_FLAG_SCHED_ABS) != 0U, + (params != NULL) ? params->abs_start_time_us : 0U); + + if (ret != 0) { + return ret; + } + + /* Rebuild the list: the last RX may have ended with buffers still in it */ + List_clearList(&rx_cmd.rxBuffers); + + for (uint8_t i = 0; i < RX_BUFFER_COUNT; i++) { + RCL_MultiBuffer *mb = (RCL_MultiBuffer *)rx_bufs[i]; + + RCL_MultiBuffer_clear(mb); + RCL_MultiBuffer_put(&rx_cmd.rxBuffers, mb); + } + + rx_cmd.common.status = RCL_CommandStatus_Idle; + + /* A rejected submit reports no event. Otherwise rx_callback ends the operation */ + if (RCL_Command_submit(data->handle, &rx_cmd.common) >= RCL_CommandStatus_Error) { + return -EIO; + } + + return 0; +} + +static int basic_radio_cc23x0_recv_async(const struct device *dev, + const struct basic_radio_rx_params *params, + basic_radio_callback_t cb, void *user_data) +{ + static const uint32_t supported_rx_flags = + BASIC_RADIO_RX_FLAG_SCHED_ABS | BASIC_RADIO_RX_FLAG_ONE_SHOT | + BASIC_RADIO_RX_FLAG_DISCARD_PAYL | BASIC_RADIO_RX_FLAG_FREQUENCY | + BASIC_RADIO_RX_FLAG_SYNC_WORD | BASIC_RADIO_RX_FLAG_MAX_LEN; + struct basic_radio_cc23x0_data *data = dev->data; + uint32_t flags = (params != NULL) ? params->flags : 0U; + int ret; + + if (cb == NULL) { + return -EINVAL; + } + + if ((flags & ~supported_rx_flags) != 0U) { + return -ENOTSUP; + } + + /* Own the radio before reading its state, so a PHY change cannot come in between */ + if (!atomic_cas(&data->state, RADIO_STATE_IDLE, RADIO_STATE_RX_ACTIVE)) { + return -EBUSY; + } + + data->cb = cb; + data->user_data = user_data; + + ret = recv_start(data, params, flags); + + if (ret != 0) { + atomic_set(&data->state, RADIO_STATE_IDLE); + } + + return ret; +} + +/* Driver API: Stop */ +static int basic_radio_cc23x0_stop(const struct device *dev, enum basic_radio_stop_mode mode) +{ + struct basic_radio_cc23x0_data *data = dev->data; + RCL_Command *cmd; + RCL_StopType stop_type; + + switch (mode) { + case BASIC_RADIO_STOP_DESCHEDULE: + stop_type = RCL_StopType_DescheduleOnly; + break; + + case BASIC_RADIO_STOP_GRACEFUL: + stop_type = RCL_StopType_Graceful; + break; + + case BASIC_RADIO_STOP_ABORT: + stop_type = RCL_StopType_Hard; + break; + + default: + return -EINVAL; + } + + /* The operation ends in the RCL interrupt; keep it from ending between state and stop */ + unsigned int key = irq_lock(); + + switch (atomic_get(&data->state)) { + case RADIO_STATE_TX_ACTIVE: + cmd = &tx_cmd.common; + break; + + case RADIO_STATE_RX_ACTIVE: + cmd = &rx_cmd.common; + break; + + case RADIO_STATE_TONE_ACTIVE: + cmd = &tone_cmd.common; + break; + + default: + cmd = NULL; + break; + } + + /* The final event follows once the stop takes effect */ + RCL_CommandStatus st = + (cmd != NULL) ? RCL_Command_stop(cmd, stop_type) : RCL_CommandStatus_Idle; + + irq_unlock(key); + + if ((st < RCL_CommandStatus_Scheduled) || (st >= RCL_CommandStatus_Finished)) { + return -EALREADY; + } + + if ((stop_type == RCL_StopType_DescheduleOnly) && (st != RCL_CommandStatus_Scheduled)) { + return -EBUSY; + } + + return 0; +} + +/* Driver API: Tone */ + +/* Check a tone against the active PHY and submit it; the caller owns the radio */ +static int tone_start(struct basic_radio_cc23x0_data *data, + const struct basic_radio_tone_params *params) +{ + /* RCL needs an open PHY to drive the synthesizer */ + const struct basic_radio_cc23x0_phy *phy = data->phy; + + if (phy == NULL) { + return -ENODEV; + } + + uint64_t frequency = ((params->flags & BASIC_RADIO_TONE_FLAG_FREQUENCY) != 0U) + ? params->frequency_hz + : data->cfg_frequency_hz; + int8_t tx_power_dbm = ((params->flags & BASIC_RADIO_TONE_FLAG_POWER) != 0U) + ? params->tx_power_dbm + : data->cfg_tx_power_dbm; + + if (!frequency_is_valid(phy, frequency) || !tx_power_is_valid(&data->power, tx_power_dbm)) { + return -EINVAL; + } + + tone_cmd.rfFrequency = (uint32_t)frequency; + tone_cmd.txPower.dBm = tx_power_dbm; + tone_cmd.txPower.fraction = 0; + tone_cmd.config.sendCw = 1; + /* The hard stop ends the tone; 0 runs it until basic_radio_stop() */ + tone_cmd.common.timing.relHardStopTime = + (uint32_t)(params->duration_us * SYSTIM_TICKS_PER_US); + + int ret = start_time_set(&tone_cmd.common, + ((params->flags & BASIC_RADIO_TONE_FLAG_SCHED_ABS) != 0U), + params->abs_start_time_us); + + if (ret != 0) { + return ret; + } + + tone_cmd.common.status = RCL_CommandStatus_Idle; + + /* A rejected submit reports no event. Otherwise tone_callback ends the operation */ + if (RCL_Command_submit(data->handle, &tone_cmd.common) >= RCL_CommandStatus_Error) { + return -EIO; + } + + return 0; +} + +static int basic_radio_cc23x0_tone_async(const struct device *dev, + const struct basic_radio_tone_params *params, + basic_radio_callback_t cb, void *user_data) +{ + static const uint32_t supported_tone_flags = BASIC_RADIO_TONE_FLAG_SCHED_ABS | + BASIC_RADIO_TONE_FLAG_FREQUENCY | + BASIC_RADIO_TONE_FLAG_POWER; + struct basic_radio_cc23x0_data *data = dev->data; + int ret; + + if ((params == NULL) || (cb == NULL)) { + return -EINVAL; + } + + if ((params->flags & ~supported_tone_flags) != 0U) { + return -ENOTSUP; + } + + /* Longer tones do not fit the 32-bit hard stop time */ + if (params->duration_us > TONE_DURATION_MAX_US) { + return -EINVAL; + } + + /* Own the radio before reading its state, so a PHY change cannot come in between */ + if (!atomic_cas(&data->state, RADIO_STATE_IDLE, RADIO_STATE_TONE_ACTIVE)) { + return -EBUSY; + } + + data->cb = cb; + data->user_data = user_data; + + ret = tone_start(data, params); + + if (ret != 0) { + atomic_set(&data->state, RADIO_STATE_IDLE); + } + + return ret; +} + +/* Driver API: Standard PHY, from the catalog of the SoC */ +static int basic_radio_cc23x0_phy_get_std(const struct device *dev, enum basic_radio_std_phy id, + const struct basic_radio_phy **phy) +{ + ARG_UNUSED(dev); + + if ((uint32_t)id >= (uint32_t)BASIC_RADIO_STD_PHY_COUNT) { + return -EINVAL; + } + +#if defined(CONFIG_BASIC_RADIO_CC23X0_PHY_CATALOG) + const struct basic_radio_cc23x0_phy *std_phy = basic_radio_cc23x0_std_phys[id]; + + if (std_phy != NULL) { + *phy = BASIC_RADIO_CC23X0_PHY(*std_phy); + + return 0; + } +#else + ARG_UNUSED(phy); +#endif + + return -ENOTSUP; +} + +/* Driver API: External Function */ +static int basic_radio_cc23x0_ext_ctl(const struct device *dev, uint32_t cmd, void *arg, + size_t arg_sz) +{ + const struct basic_radio_cc23x0_data *data = dev->data; + + if (cmd != TI_CC23X0_EXT_GET_STATS) { + return -ENOTSUP; + } + + if ((arg == NULL) || (arg_sz != sizeof(struct ti_cc23x0_ext_stats))) { + return -EINVAL; + } + + struct ti_cc23x0_ext_stats *stats = arg; + + /* The counters are written from the RCL interrupt; copy them as one set */ + unsigned int key = irq_lock(); + + *stats = (struct ti_cc23x0_ext_stats){ + .rx_ok = data->rx_ok_count, + .rx_crc_err = data->rx_crc_err_count, + .sched_reject = data->sched_reject_count, + .cmd_started = data->cmd_started_count, + }; + + irq_unlock(key); + + return 0; +} + +/* Driver API: Init */ +static int basic_radio_cc23x0_init(const struct device *dev) +{ + struct basic_radio_cc23x0_data *data = dev->data; + const struct basic_radio_cc23x0_phy *default_phy = DEFAULT_PHY; + + atomic_set(&data->state, RADIO_STATE_IDLE); + + data->phy = NULL; + data->handle = NULL; + + RCL_init(); + + /* Default PHY and its default runtime parameters */ + if (default_phy != NULL) { + struct basic_radio_cc23x0_hdr hdr; + struct basic_radio_cc23x0_power_range power; + int ret = phy_info_get(default_phy, &hdr, &power); + + if (ret == 0) { + ret = phy_select(data, default_phy, &hdr, &power); + } + + if (ret != 0) { + LOG_ERR("default PHY %p failed: %d", (const void *)default_phy, ret); + + return ret; + } + + rf_params_set(data, default_phy->frequency, default_phy->sync_word, + default_phy->tx_power, max_len_get(&hdr)); + } else { + rf_params_set(data, 0U, 0U, 0, MAX_PACKET_LEN); + } + + tone_cmd.common.runtime.callback = tone_callback; + tone_cmd.common.runtime.rclCallbackMask.value = RCL_EventLastCmdDone.value; + + tx_cmd.common.runtime.callback = tx_callback; + tx_cmd.common.runtime.rclCallbackMask.value = + RCL_EventLastCmdDone.value | RCL_EventCmdStarted.value | + RCL_EventStartRejected.value | RCL_EventStartDelayed.value; + + rx_cmd.common.runtime.callback = rx_callback; + rx_cmd.common.runtime.rclCallbackMask.value = + RCL_EventRxEntryAvail.value | RCL_EventLastCmdDone.value | + RCL_EventCmdStarted.value | RCL_EventStartRejected.value | + RCL_EventStartDelayed.value; + + rx_cmd.common.runtime.lrfCallbackMask.value = LRF_EventRxOk.value | LRF_EventRxNok.value; + + data->stats = RCL_StatsGeneric_DefaultRuntime(); + rx_cmd.stats = &data->stats; + rx_cmd.stats->config.activeUpdate = 1; + + for (uint8_t i = 0; i < RX_BUFFER_COUNT; i++) { + RCL_MultiBuffer *mb = (RCL_MultiBuffer *)rx_bufs[i]; + + RCL_MultiBuffer_init(mb, RCL_MultiBuffer_len(RX_BUFFER_SIZE)); + } + + basic_radio_cc23x0_dev = dev; + + k_timer_init(&time_refresh_timer, time_refresh, NULL); + k_timer_start(&time_refresh_timer, TIME_REFRESH_PERIOD, TIME_REFRESH_PERIOD); + + return 0; +} + +static DEVICE_API(basic_radio, basic_radio_cc23x0_api) = { + /* Main Functions */ + .config = basic_radio_cc23x0_config_impl, + .send_async = basic_radio_cc23x0_send_async, + .recv_async = basic_radio_cc23x0_recv_async, + .tone_async = basic_radio_cc23x0_tone_async, + .stop = basic_radio_cc23x0_stop, + .time_get = basic_radio_cc23x0_time_get, + /* Optional Functions */ + .get_caps = basic_radio_cc23x0_get_caps, + .phy_get_std = basic_radio_cc23x0_phy_get_std, + /* Vendor-Specific */ + .ext_ctl = basic_radio_cc23x0_ext_ctl, +}; + +static struct basic_radio_cc23x0_data basic_radio_cc23x0_data_0; + +DEVICE_DT_INST_DEFINE(INST, basic_radio_cc23x0_init, NULL, &basic_radio_cc23x0_data_0, NULL, + POST_KERNEL, CONFIG_BASIC_RADIO_INIT_PRIORITY, &basic_radio_cc23x0_api); diff --git a/drivers/basic_radio/backends/ti/cc23x0/basic_radio_cc23x0.h b/drivers/basic_radio/backends/ti/cc23x0/basic_radio_cc23x0.h new file mode 100644 index 0000000000000..e96ec12e6f0e1 --- /dev/null +++ b/drivers/basic_radio/backends/ti/cc23x0/basic_radio_cc23x0.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * + * SPDX-License-Identifier: Apache-2.0 + * + * Private types for the TI CC23x0 BASIC_RADIO backend. + * Not part of the public driver API. + */ + +#ifndef DRIVERS_BASIC_RADIO_BACKENDS_TI_CC23X0_BASIC_RADIO_CC23X0_H_ +#define DRIVERS_BASIC_RADIO_BACKENDS_TI_CC23X0_BASIC_RADIO_CC23X0_H_ + +#include +#include + +#include +#include +#include +#include + +#include +#include + +/* Padding bytes in front of the TX header for the PBE generic image */ +#define NUM_PAD_BYTES 3 + +/* Frame header layout of a PHY, from its LENCFG */ +struct basic_radio_cc23x0_hdr { + uint8_t len_index; /* Byte holding the length field */ + uint8_t len; /* Header bytes up to and including the length field */ + uint8_t len_mask; /* Length field bits within that byte */ +}; + +/* TX power range of a PHY, from its TX power table */ +struct basic_radio_cc23x0_power_range { + bool is_known; /* False if the PHY has no TX power table */ + int8_t min_dbm; + int8_t max_dbm; +}; + +/* + * Radio state machine. One operation at a time; the radio returns to IDLE + * before the final event of each operation is reported. + */ +enum radio_state { + RADIO_STATE_IDLE = 0, + RADIO_STATE_TX_ACTIVE, + RADIO_STATE_RX_ACTIVE, + RADIO_STATE_TONE_ACTIVE, + RADIO_STATE_CONFIG, +}; + +struct basic_radio_cc23x0_data { + RCL_Client rcl_client; + RCL_Handle handle; + RCL_StatsGeneric stats; + + atomic_t state; + + /* Event callback of the active operation */ + basic_radio_callback_t cb; + void *user_data; + + /* Active PHY, NULL until a PHY is applied */ + const struct basic_radio_cc23x0_phy *phy; + + /* Frame header layout of the active PHY */ + struct basic_radio_cc23x0_hdr hdr; + + /* TX power range of the active PHY */ + struct basic_radio_cc23x0_power_range power; + + /* Layer 1 config cache */ + uint32_t cfg_frequency_hz; + uint32_t cfg_sync_word; + int8_t cfg_tx_power_dbm; + uint16_t cfg_max_packet_len; + + /* Observability counters, written from HWI (single writer) */ + uint32_t rx_ok_count; + uint32_t rx_crc_err_count; + uint32_t sched_reject_count; + uint32_t cmd_started_count; + /* Receptions ended for lack of RX buffer space (-ENOBUFS) */ + uint32_t rx_overflow_count; +}; + +#endif /* DRIVERS_BASIC_RADIO_BACKENDS_TI_CC23X0_BASIC_RADIO_CC23X0_H_ */ diff --git a/drivers/basic_radio/backends/ti/cc23x0/basic_radio_cc23x0_phy_catalog_cc2340r5.c b/drivers/basic_radio/backends/ti/cc23x0/basic_radio_cc23x0_phy_catalog_cc2340r5.c new file mode 100644 index 0000000000000..30562d49ed113 --- /dev/null +++ b/drivers/basic_radio/backends/ti/cc23x0/basic_radio_cc23x0_phy_catalog_cc2340r5.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * + * SPDX-License-Identifier: Apache-2.0 + * + * Certified PHYs of the CC2340R5. Each object wraps an LRF_Config from + * hal_ti (source/ti/radio_phys/cc23x0r5/) and selects one of its sub-PHYs. + * Objects the application does not reference are removed at link time. + * + * One catalog file per device, built only for its SoC (see CMakeLists.txt). + * PHYs are certified per device, so a PHY defined here is not available on + * another CC23x0 part unless its own catalog defines it too. + */ + +#include + +#include + +/* phyFeatures of the BLE 5 generic configuration (sub-PHY selection) */ +#define PHY_FEATURE_BLE5_1M 0x0000U +#define PHY_FEATURE_BLE5_2M 0x0001U +#define PHY_FEATURE_BLE5_CODED 0x0002U + +#define BAND_2G4_MIN_HZ 2400000000U +#define BAND_2G4_MAX_HZ 2483500000U +#define IEEE_2G4_MIN_HZ 2405000000U +#define IEEE_2G4_MAX_HZ 2480000000U + +/* Default runtime parameters: 2440 MHz (IEEE 802.15.4 channel 18), 0 dBm */ +#define DEFAULT_FREQ_HZ 2440000000U +#define DEFAULT_SYNC_WORD 0x930B51DEU +#define DEFAULT_TX_DBM 0 + +/* Exported by hal_ti, no header declares them */ +extern const LRF_Config LRF_config_ble5_1m_2m_coded_generic; +extern const LRF_Config LRF_config_ieee_802_15_4_generic; +extern const LRF_Config LRF_config_prop_250kbps_msk_fec; +extern const LRF_Config LRF_config_prop_2mbps_320khz_gfsk; + +BASIC_RADIO_CC23X0_PHY_DEFINE(basic_radio_cc23x0_phy_ble5_1m, &LRF_config_ble5_1m_2m_coded_generic, + PHY_FEATURE_BLE5_1M, BAND_2G4_MIN_HZ, BAND_2G4_MAX_HZ, + DEFAULT_FREQ_HZ, DEFAULT_SYNC_WORD, DEFAULT_TX_DBM); + +BASIC_RADIO_CC23X0_PHY_DEFINE(basic_radio_cc23x0_phy_ble5_2m, &LRF_config_ble5_1m_2m_coded_generic, + PHY_FEATURE_BLE5_2M, BAND_2G4_MIN_HZ, BAND_2G4_MAX_HZ, + DEFAULT_FREQ_HZ, DEFAULT_SYNC_WORD, DEFAULT_TX_DBM); + +/* Coded TX rate bit not set: S=8 */ +BASIC_RADIO_CC23X0_PHY_DEFINE(basic_radio_cc23x0_phy_ble5_coded, + &LRF_config_ble5_1m_2m_coded_generic, PHY_FEATURE_BLE5_CODED, + BAND_2G4_MIN_HZ, BAND_2G4_MAX_HZ, DEFAULT_FREQ_HZ, DEFAULT_SYNC_WORD, + DEFAULT_TX_DBM); + +BASIC_RADIO_CC23X0_PHY_DEFINE(basic_radio_cc23x0_phy_ieee_802_15_4, + &LRF_config_ieee_802_15_4_generic, 0U, IEEE_2G4_MIN_HZ, + IEEE_2G4_MAX_HZ, DEFAULT_FREQ_HZ, DEFAULT_SYNC_WORD, DEFAULT_TX_DBM); + +BASIC_RADIO_CC23X0_PHY_DEFINE(basic_radio_cc23x0_phy_prop_250kbps_msk_fec, + &LRF_config_prop_250kbps_msk_fec, 0U, BAND_2G4_MIN_HZ, + BAND_2G4_MAX_HZ, DEFAULT_FREQ_HZ, DEFAULT_SYNC_WORD, DEFAULT_TX_DBM); + +BASIC_RADIO_CC23X0_PHY_DEFINE(basic_radio_cc23x0_phy_prop_2mbps_320khz_gfsk, + &LRF_config_prop_2mbps_320khz_gfsk, 0U, BAND_2G4_MIN_HZ, + BAND_2G4_MAX_HZ, DEFAULT_FREQ_HZ, DEFAULT_SYNC_WORD, DEFAULT_TX_DBM); + +/* + * Standard PHY profiles. A PHY is listed only once it is verified to + * implement the profile defined in + * bit-exactly; none of the PHYs above does. Every profile needs an entry: + * the size must match the declaration in . + */ +const struct basic_radio_cc23x0_phy *const basic_radio_cc23x0_std_phys[] = { + [BASIC_RADIO_STD_PHY_2GFSK_1M] = NULL, +}; + +const struct basic_radio_cc23x0_phy *const basic_radio_cc23x0_phy_default = + &basic_radio_cc23x0_phy_ble5_1m; diff --git a/dts/bindings/basic_radio/basic-radio-base.yaml b/dts/bindings/basic_radio/basic-radio-base.yaml new file mode 100644 index 0000000000000..90b3c8be01493 --- /dev/null +++ b/dts/bindings/basic_radio/basic-radio-base.yaml @@ -0,0 +1,14 @@ +# Copyright (c) 2026 Texas Instruments Incorporated +# SPDX-License-Identifier: Apache-2.0 + +description: | + Common properties of a Basic Radio device. Include-only: each radio + binding (for example ti,cc23x0-basic-radio.yaml) includes this file and + adds its device-specific properties. + + The node describes the radio hardware only. The PHY (waveform and frame + format) is a C object of the backend, passed at runtime through + basic_radio_config(). Each radio binding names the header that defines + the backend's PHY objects. + +include: [base.yaml] diff --git a/dts/bindings/basic_radio/ti,cc23x0-basic-radio.yaml b/dts/bindings/basic_radio/ti,cc23x0-basic-radio.yaml new file mode 100644 index 0000000000000..a32a73a901c42 --- /dev/null +++ b/dts/bindings/basic_radio/ti,cc23x0-basic-radio.yaml @@ -0,0 +1,23 @@ +# Copyright (c) 2026 Texas Instruments Incorporated +# SPDX-License-Identifier: Apache-2.0 + +description: | + TI CC23x0 on-chip radio, driven through the Basic Radio API. + + PHYs are struct basic_radio_cc23x0_phy objects declared in + : the certified PHYs of the + device, or custom PHYs defined with BASIC_RADIO_CC23X0_PHY_DEFINE() from + a SysConfig RadioConfig export. The device's default PHY is active at + init unless CONFIG_BASIC_RADIO_CC23X0_DEFAULT_PHY is disabled; + basic_radio_config() selects another one. + + Example: + + basic_radio0: basic_radio { + compatible = "ti,cc23x0-basic-radio"; + status = "okay"; + }; + +compatible: "ti,cc23x0-basic-radio" + +include: [basic-radio-base.yaml] diff --git a/include/zephyr/drivers/basic_radio.h b/include/zephyr/drivers/basic_radio.h new file mode 100644 index 0000000000000..409fdb1ae04a4 --- /dev/null +++ b/include/zephyr/drivers/basic_radio.h @@ -0,0 +1,630 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief Public Basic Radio driver API + */ +#ifndef ZEPHYR_INCLUDE_DRIVERS_BASIC_RADIO_H_ +#define ZEPHYR_INCLUDE_DRIVERS_BASIC_RADIO_H_ + +/** + * @brief Basic Radio APIs + * @defgroup basic_radio_api Basic Radio APIs + * @ingroup io_interfaces + * @{ + */ + +#include +#include +#include +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @brief Optional radio capabilities, bitmask in basic_radio_caps::flags. */ +enum basic_radio_capability { + /** Scheduled operations (absolute start time). */ + BASIC_RADIO_CAP_SCHED = BIT(0), + /** Continuous wave (tone) transmission. */ + BASIC_RADIO_CAP_TONE = BIT(1), + /** Graceful stop (finish the current frame). */ + BASIC_RADIO_CAP_STOP_GRACEFUL = BIT(2), +}; + +/** @brief Stop mode for basic_radio_stop(). */ +enum basic_radio_stop_mode { + /** Cancel a scheduled start only, no effect on a running operation. */ + BASIC_RADIO_STOP_DESCHEDULE = 0, + /** + * Finish the frame in progress, then stop: TX ends after the packet + * being sent, RX after the packet being received, if any. A tone + * stops immediately. + */ + BASIC_RADIO_STOP_GRACEFUL = 1, + /** Stop immediately. */ + BASIC_RADIO_STOP_ABORT = 2, +}; + +/** @brief Event types reported to basic_radio_callback_t. */ +enum basic_radio_evt_type { + /** Transmission finished, final event of basic_radio_send_async(). */ + BASIC_RADIO_EVT_TX_DONE = 0, + /** Packet received during basic_radio_recv_async(). */ + BASIC_RADIO_EVT_RX_PKT = 1, + /** Reception stopped, final event of basic_radio_recv_async(). */ + BASIC_RADIO_EVT_RX_DONE = 2, + /** Tone ended, final event of basic_radio_tone_async(). */ + BASIC_RADIO_EVT_TONE_DONE = 3, +}; + +/** + * @brief Opaque PHY object. + * + * A PHY object fully defines the waveform and frame format the radio uses: + * modulation, data rate, preamble, sync word length, length field, CRC and + * coding. Its type is defined by the backend, because radios of different + * vendors are configured with different data (for example a register list + * exported from a vendor tool). The backend's header provides the macro + * that defines PHY objects, the macro that converts one to a + * const struct basic_radio_phy pointer, and the backend's catalog of + * certified PHYs. The binding of the radio's compatible names that header. + * + * This type is never defined: code that only selects a PHY holds a pointer + * to it and does not need the backend type. A PHY object must be passed + * only to a radio of the backend that defined it. + * + * Custom PHYs: an application may define several PHY objects, custom or + * certified, and switches between them by passing another pointer in + * basic_radio_config::phy while the radio is idle. The driver keeps no + * table of PHYs; an application that selects PHYs by index keeps its own + * array of const struct basic_radio_phy pointers. + */ +struct basic_radio_phy; + +/** + * @brief Standard PHY profiles. + * + * Vendor-neutral PHYs that radios of different vendors implement + * bit-exactly, so that they interoperate. basic_radio_phy_get_std() + * returns the backend's PHY object for a profile. All profiles share: + * + * - Modulation: 2-GFSK, BT = 0.5, modulation index 0.5 (deviation is a + * quarter of the bit rate). + * - Preamble: 32 bits of alternating ones and zeros, starting with one. + * - Sync word: 32 bits, basic_radio_config::sync_word bits 31 to 0. + * - Length field: one byte, the number of payload bytes, excluding the CRC. + * - CRC: CRC-16 with polynomial 0x1021, initial value 0xFFFF, not + * reflected and without final XOR, over the length byte and the payload. + * - No whitening. Every field is sent most significant bit first. + */ +enum basic_radio_std_phy { + /** 1 Mbps, 250 kHz deviation. */ + BASIC_RADIO_STD_PHY_2GFSK_1M = 0, + /** Number of standard PHY profiles, not a profile. */ + BASIC_RADIO_STD_PHY_COUNT, +}; + +/** + * @name Runtime configuration flags + * + * Fields of struct basic_radio_config applied by basic_radio_config(). + * Unknown bits cause basic_radio_config() to return -ENOTSUP. + * @{ + */ +/** Apply basic_radio_config::frequency_hz. */ +#define BASIC_RADIO_CFG_FREQUENCY BIT(0) +/** Apply basic_radio_config::sync_word. */ +#define BASIC_RADIO_CFG_SYNC_WORD BIT(1) +/** Apply basic_radio_config::tx_power_dbm. */ +#define BASIC_RADIO_CFG_TX_POWER BIT(2) +/** Apply basic_radio_config::max_packet_len. */ +#define BASIC_RADIO_CFG_MAX_PACKET_LEN BIT(3) +/** @} */ + +/** + * @brief Runtime radio configuration. + * + * phy selects the PHY object whose waveform is used. The backend switches + * to it, reconfiguring the radio if needed. NULL keeps the active PHY; at + * boot the active PHY is the backend's default PHY, if it has one, with its + * default runtime parameters, so a radio with a default PHY is usable + * without calling basic_radio_config(). + * + * frequency_hz, sync_word, tx_power_dbm and max_packet_len are the common + * runtime parameters and every backend applies them on top of the PHY. A + * field is applied only when its BASIC_RADIO_CFG_* bit is set in flags. + * A field whose bit is clear keeps its current value, except + * frequency_hz, sync_word and tx_power_dbm when phy selects a PHY other + * than the active one: those take the new PHY's defaults. max_packet_len + * is kept across PHY changes and reduced to the largest length the new + * PHY's length field can carry. + */ +struct basic_radio_config { + /** PHY object, or NULL to keep the active PHY. */ + const struct basic_radio_phy *phy; + /** Bitmask of BASIC_RADIO_CFG_* selecting the fields to apply. */ + uint32_t flags; + /** Carrier frequency in Hz. */ + uint64_t frequency_hz; + /** Sync word, LSB-aligned. Its length is defined by the PHY. */ + uint64_t sync_word; + /** Maximum payload length in bytes, for sent and received packets. */ + uint16_t max_packet_len; + /** TX power in dBm. */ + int8_t tx_power_dbm; +}; + +/** RSSI value of a packet whose signal strength is unknown. */ +#define BASIC_RADIO_RSSI_INVALID INT8_MIN + +/** @brief Per-packet RX metadata. */ +struct basic_radio_rx_info { + /** Received signal strength in dBm, or BASIC_RADIO_RSSI_INVALID if unknown. */ + int8_t rssi; +}; + +/** + * @brief Event reported to basic_radio_callback_t. + * + * BASIC_RADIO_EVT_RX_PKT is reported only for packets that passed the + * CRC check of the PHY, if the PHY has one. + */ +struct basic_radio_evt { + /** Event type. */ + enum basic_radio_evt_type type; + /** 0 on success, negative errno on error. Set for the final event of an operation. */ + int status; + /** + * Time on the radio time base, in microseconds, at which the last bit + * of the sync word was received (BASIC_RADIO_EVT_RX_PKT) or sent + * (BASIC_RADIO_EVT_TX_DONE). 0 if unknown. + */ + uint64_t timestamp_us; + /** Received payload, set for BASIC_RADIO_EVT_RX_PKT. */ + const uint8_t *data; + /** Payload length in bytes, set for BASIC_RADIO_EVT_RX_PKT. */ + uint16_t len; + /** RX metadata, set for BASIC_RADIO_EVT_RX_PKT. */ + struct basic_radio_rx_info info; +}; + +/** + * @brief Event callback. + * + * The callback may run in interrupt context, often at a high priority. It + * must not block, and it should return quickly: copy what is needed (for + * example into a k_msgq) and process it in a thread. Logging, printing or + * parsing packets in the callback delays other interrupts and threads. + * + * @p evt and the payload it points to are valid only until the callback + * returns; copy what is needed later. A backend may hold its RX buffer until + * the callback returns. If packets arrive faster than the callback handles + * them, the backend can run out of buffers and end the reception with + * BASIC_RADIO_EVT_RX_DONE and status -ENOBUFS. + * + * The final event of an operation (TX_DONE, RX_DONE, TONE_DONE) is reported + * after the radio is idle, so the callback can start the next operation. + * + * @param dev Radio device. + * @param evt Event. + * @param user_data Pointer given when the operation was started. + */ +typedef void (*basic_radio_callback_t)(const struct device *dev, const struct basic_radio_evt *evt, + void *user_data); + +/** + * @name TX flags + * + * Absolute times are in microseconds on the radio time base, see + * basic_radio_time_get(). + * + * Per-packet overrides in basic_radio_tx_params::flags. A field without its + * flag uses the value set by basic_radio_config(). Unknown bits cause + * basic_radio_send_async() to return -ENOTSUP. + * @{ + */ +/** Start TX at abs_start_time_us instead of immediately. */ +#define BASIC_RADIO_TX_FLAG_SCHED_ABS BIT(0) +/** Use frequency_hz. */ +#define BASIC_RADIO_TX_FLAG_FREQUENCY BIT(1) +/** Use tx_power_dbm. */ +#define BASIC_RADIO_TX_FLAG_POWER BIT(2) +/** Use sync_word. */ +#define BASIC_RADIO_TX_FLAG_SYNC_WORD BIT(3) +/** @} */ + +/** @brief Per-packet parameters of basic_radio_send_async(). */ +struct basic_radio_tx_params { + /** Bitmask of BASIC_RADIO_TX_FLAG_*. */ + uint32_t flags; + /** Absolute start time in microseconds, with BASIC_RADIO_TX_FLAG_SCHED_ABS. */ + uint64_t abs_start_time_us; + /** Carrier frequency in Hz, with BASIC_RADIO_TX_FLAG_FREQUENCY. */ + uint64_t frequency_hz; + /** Sync word, with BASIC_RADIO_TX_FLAG_SYNC_WORD. */ + uint64_t sync_word; + /** TX power in dBm, with BASIC_RADIO_TX_FLAG_POWER. */ + int8_t tx_power_dbm; +}; + +/** + * @name RX flags + * + * Per-operation options in basic_radio_rx_params::flags. A field without its + * flag uses the value set by basic_radio_config(). Unknown bits cause + * basic_radio_recv_async() to return -ENOTSUP. + * @{ + */ +/** Start RX at abs_start_time_us instead of immediately. */ +#define BASIC_RADIO_RX_FLAG_SCHED_ABS BIT(0) +/** Stop after the first packet instead of receiving continuously. */ +#define BASIC_RADIO_RX_FLAG_ONE_SHOT BIT(1) +/** Report packets without their payload (packet error rate tests). */ +#define BASIC_RADIO_RX_FLAG_DISCARD_PAYL BIT(2) +/** Use frequency_hz. */ +#define BASIC_RADIO_RX_FLAG_FREQUENCY BIT(3) +/** Use sync_word. */ +#define BASIC_RADIO_RX_FLAG_SYNC_WORD BIT(4) +/** Use max_packet_len. */ +#define BASIC_RADIO_RX_FLAG_MAX_LEN BIT(5) +/** @} */ + +/** @brief Per-operation parameters of basic_radio_recv_async(). */ +struct basic_radio_rx_params { + /** Bitmask of BASIC_RADIO_RX_FLAG_*. */ + uint32_t flags; + /** Absolute start time in microseconds, with BASIC_RADIO_RX_FLAG_SCHED_ABS. */ + uint64_t abs_start_time_us; + /** Carrier frequency in Hz, with BASIC_RADIO_RX_FLAG_FREQUENCY. */ + uint64_t frequency_hz; + /** Sync word, with BASIC_RADIO_RX_FLAG_SYNC_WORD. */ + uint64_t sync_word; + /** Maximum payload length in bytes, with BASIC_RADIO_RX_FLAG_MAX_LEN. */ + uint16_t max_packet_len; +}; + +/** + * @name Tone flags + * + * Options in basic_radio_tone_params::flags. A field without its flag uses + * the value set by basic_radio_config(). Unknown bits cause + * basic_radio_tone_async() to return -ENOTSUP. + * @{ + */ +/** Start the tone at abs_start_time_us instead of immediately. */ +#define BASIC_RADIO_TONE_FLAG_SCHED_ABS BIT(0) +/** Use frequency_hz. */ +#define BASIC_RADIO_TONE_FLAG_FREQUENCY BIT(1) +/** Use tx_power_dbm. */ +#define BASIC_RADIO_TONE_FLAG_POWER BIT(2) +/** @} */ + +/** @brief Parameters of basic_radio_tone_async(). */ +struct basic_radio_tone_params { + /** Bitmask of BASIC_RADIO_TONE_FLAG_*. */ + uint32_t flags; + /** Absolute start time in microseconds, with BASIC_RADIO_TONE_FLAG_SCHED_ABS. */ + uint64_t abs_start_time_us; + /** Carrier frequency in Hz, with BASIC_RADIO_TONE_FLAG_FREQUENCY. */ + uint64_t frequency_hz; + /** + * Duration in microseconds, 0 to run until basic_radio_stop(). A + * duration longer than the backend supports returns -EINVAL. + */ + uint64_t duration_us; + /** TX power in dBm, with BASIC_RADIO_TONE_FLAG_POWER. */ + int8_t tx_power_dbm; +}; + +/** @brief Radio capabilities returned by basic_radio_get_caps(). */ +struct basic_radio_caps { + /** Bitmask of enum basic_radio_capability. */ + uint32_t flags; + /** Minimum carrier frequency of the active PHY in Hz. */ + uint64_t freq_min; + /** Maximum carrier frequency of the active PHY in Hz. */ + uint64_t freq_max; + /** Frequency step in millihertz. */ + uint32_t freq_resolution_millihz; + /** Worst-case time to retune between frequencies in microseconds. */ + uint32_t retune_time_us; + /** Latest start time of a scheduled operation, in microseconds from now. */ + uint32_t sched_horizon_us; + /** Minimum TX power of the active PHY in dBm. */ + int8_t tx_power_min_dbm; + /** Maximum TX power of the active PHY in dBm. */ + int8_t tx_power_max_dbm; +}; + +/** + * @cond INTERNAL_HIDDEN + * + * Driver API, implemented by each backend and called by the wrappers below. + */ +__subsystem struct basic_radio_driver_api { + int (*config)(const struct device *dev, const struct basic_radio_config *cfg); + int (*send_async)(const struct device *dev, const uint8_t *data, uint16_t len, + const struct basic_radio_tx_params *params, basic_radio_callback_t cb, + void *user_data); + int (*recv_async)(const struct device *dev, const struct basic_radio_rx_params *params, + basic_radio_callback_t cb, void *user_data); + /* Optional */ + int (*tone_async)(const struct device *dev, const struct basic_radio_tone_params *params, + basic_radio_callback_t cb, void *user_data); + int (*stop)(const struct device *dev, enum basic_radio_stop_mode mode); + uint64_t (*time_get)(const struct device *dev); + int (*get_caps)(const struct device *dev, struct basic_radio_caps *caps); + int (*phy_get_std)(const struct device *dev, enum basic_radio_std_phy id, + const struct basic_radio_phy **phy); + /* Vendor-specific */ + int (*ext_ctl)(const struct device *dev, uint32_t cmd, void *arg, size_t arg_sz); +}; +/** @endcond */ + +/** + * @brief Apply a runtime radio configuration. + * + * Switches to the PHY object in @p cfg->phy, if not NULL, and applies the + * runtime parameters selected by @p cfg->flags on top of it. Changing to a + * PHY with another radio configuration may reinitialize the radio. The + * radio must be idle. + * + * @param dev Radio device. + * @param cfg Configuration to apply. + * + * @retval 0 Success. + * @retval -EBUSY Radio is not idle. + * @retval -EINVAL A parameter is out of range for the selected PHY. + * @retval -ENODEV @p cfg->phy is NULL and no PHY is active. + * @retval -ENOTSUP The backend cannot drive the PHY in @p cfg->phy. + * @retval -ENOTSUP @p cfg->flags has an unknown bit set. + * @retval -EIO Radio reconfiguration failed. + */ +static inline int basic_radio_config(const struct device *dev, const struct basic_radio_config *cfg) +{ + const struct basic_radio_driver_api *api = dev->api; + + return api->config(dev, cfg); +} + +/** + * @brief Transmit a packet asynchronously. + * + * @p cb is called with BASIC_RADIO_EVT_TX_DONE when the frame has been sent + * or the operation failed. @p data is copied before the function returns. + * + * @param dev Radio device. + * @param data Payload to transmit. + * @param len Length of @p data in bytes. + * @param params Per-packet parameters, or NULL. + * @param cb Event callback. + * @param user_data Pointer passed to @p cb. + * + * @retval 0 Operation started. + * @retval -EBUSY Radio is not idle. + * @retval -EINVAL @p len exceeds the configured maximum payload length. + * @retval -EINVAL A parameter is out of range for the active PHY. + * @retval -ENODEV No PHY is active. + * @retval -ENOTSUP @p params has an unsupported flag set. + * @retval -ETIME The scheduled start time has passed. + * @retval -EIO The radio rejected the operation. + */ +static inline int basic_radio_send_async(const struct device *dev, const uint8_t *data, + uint16_t len, const struct basic_radio_tx_params *params, + basic_radio_callback_t cb, void *user_data) +{ + const struct basic_radio_driver_api *api = dev->api; + + return api->send_async(dev, data, len, params, cb, user_data); +} + +/** + * @brief Start receiving asynchronously. + * + * @p cb is called with BASIC_RADIO_EVT_RX_PKT for each received packet and + * with BASIC_RADIO_EVT_RX_DONE when reception stops. The status of + * BASIC_RADIO_EVT_RX_DONE is: + * - 0: reception ended normally (for example a one-shot reception). + * - -ECANCELED: reception was stopped with basic_radio_stop(). + * - -ETIME: the scheduled start time was missed. + * - -ENOBUFS: the backend ran out of RX buffer space because packets came + * in faster than they were handled; keep the callback short (see + * basic_radio_callback_t) and start reception again. + * - -EIO: other radio error. + * + * @param dev Radio device. + * @param params Per-operation parameters, or NULL. + * @param cb Event callback. + * @param user_data Pointer passed to @p cb. + * + * @retval 0 Reception started. + * @retval -EBUSY Radio is not idle. + * @retval -EINVAL A parameter is out of range for the active PHY. + * @retval -ENODEV No PHY is active. + * @retval -ENOTSUP @p params has an unsupported flag set. + * @retval -ETIME The scheduled start time has passed. + * @retval -EIO The radio rejected the operation. + */ +static inline int basic_radio_recv_async(const struct device *dev, + const struct basic_radio_rx_params *params, + basic_radio_callback_t cb, void *user_data) +{ + const struct basic_radio_driver_api *api = dev->api; + + return api->recv_async(dev, params, cb, user_data); +} + +/** + * @brief Start a continuous wave (tone) asynchronously. + * + * @p cb is called with BASIC_RADIO_EVT_TONE_DONE when the tone ends, after + * its duration or on basic_radio_stop(). + * + * @param dev Radio device. + * @param params Tone parameters. + * @param cb Event callback. + * @param user_data Pointer passed to @p cb. + * + * @retval 0 Tone started. + * @retval -EBUSY Radio is not idle. + * @retval -EINVAL A parameter is out of range for the active PHY. + * @retval -ENODEV No PHY is active. + * @retval -ENOSYS Backend does not support tones. + * @retval -ENOTSUP @p params has an unsupported flag set. + * @retval -ETIME The scheduled start time has passed. + * @retval -EIO The radio rejected the operation. + */ +static inline int basic_radio_tone_async(const struct device *dev, + const struct basic_radio_tone_params *params, + basic_radio_callback_t cb, void *user_data) +{ + const struct basic_radio_driver_api *api = dev->api; + + if (api->tone_async == NULL) { + return -ENOSYS; + } + + return api->tone_async(dev, params, cb, user_data); +} + +/** + * @brief Stop the active operation. + * + * The final event of the operation follows through its callback. + * + * @param dev Radio device. + * @param mode Stop mode. + * + * @retval 0 Stop initiated. + * @retval -EALREADY No operation in progress. + * @retval -EBUSY @p mode is BASIC_RADIO_STOP_DESCHEDULE and the operation already started. + * @retval -EINVAL @p mode is not a valid stop mode. + * @retval -ENOSYS Backend does not support stop. + */ +static inline int basic_radio_stop(const struct device *dev, enum basic_radio_stop_mode mode) +{ + const struct basic_radio_driver_api *api = dev->api; + + if (api->stop == NULL) { + return -ENOSYS; + } + + return api->stop(dev, mode); +} + +/** + * @brief Get the current radio time. + * + * The radio time base is a 64-bit microsecond counter that starts at init + * and does not wrap in practice. Scheduled start times and event + * timestamps use it. Every backend that reports BASIC_RADIO_CAP_SCHED + * implements it. + * + * @param dev Radio device. + * + * @return Current time in microseconds on the radio time base, 0 if the + * backend has no time base. + */ +static inline uint64_t basic_radio_time_get(const struct device *dev) +{ + const struct basic_radio_driver_api *api = dev->api; + + if (api->time_get == NULL) { + return 0U; + } + + return api->time_get(dev); +} + +/** + * @brief Get the PHY object of a standard PHY profile. + * + * Pass the result in basic_radio_config::phy to select the profile. + * + * @param dev Radio device. + * @param id Standard PHY profile. + * @param phy Set to the backend's PHY object for @p id. + * + * @retval 0 Success. + * @retval -EINVAL @p id is not a standard PHY profile. + * @retval -ENOSYS Backend does not implement standard PHY profiles. + * @retval -ENOTSUP Backend has no PHY for @p id. + */ +static inline int basic_radio_phy_get_std(const struct device *dev, enum basic_radio_std_phy id, + const struct basic_radio_phy **phy) +{ + const struct basic_radio_driver_api *api = dev->api; + + if (api->phy_get_std == NULL) { + return -ENOSYS; + } + + return api->phy_get_std(dev, id, phy); +} + +/** + * @brief Get the radio capabilities with the active PHY. + * + * @param dev Radio device. + * @param caps Filled with the capabilities. + * + * @retval 0 Success. + * @retval -ENODEV No PHY is active. + * @retval -ENOSYS Backend does not implement get_caps. + */ +static inline int basic_radio_get_caps(const struct device *dev, struct basic_radio_caps *caps) +{ + const struct basic_radio_driver_api *api = dev->api; + + if (api->get_caps == NULL) { + return -ENOSYS; + } + + return api->get_caps(dev, caps); +} + +/** + * @brief Run a vendor-specific command. + * + * Commands and their arguments are defined by the backend's header. + * + * @param dev Radio device. + * @param cmd Vendor command. + * @param arg Command argument. + * @param arg_sz Size of @p arg in bytes. + * + * @retval 0 Command handled. + * @retval -EINVAL @p arg or @p arg_sz does not match @p cmd. + * @retval -ENOSYS Backend does not implement vendor commands. + * @retval -ENOTSUP Backend does not recognize @p cmd. + */ +static inline int basic_radio_ext_ctl(const struct device *dev, uint32_t cmd, void *arg, + size_t arg_sz) +{ + const struct basic_radio_driver_api *api = dev->api; + + if (api->ext_ctl == NULL) { + return -ENOSYS; + } + + return api->ext_ctl(dev, cmd, arg, arg_sz); +} + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* ZEPHYR_INCLUDE_DRIVERS_BASIC_RADIO_H_ */ diff --git a/include/zephyr/drivers/basic_radio/ti_cc23x0.h b/include/zephyr/drivers/basic_radio/ti_cc23x0.h new file mode 100644 index 0000000000000..01a241fa9e0f9 --- /dev/null +++ b/include/zephyr/drivers/basic_radio/ti_cc23x0.h @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief TI CC23x0 basic_radio backend: PHY objects and vendor commands. + * + * Include this header only in code that defines or names CC23x0 PHYs, or + * uses the CC23x0 vendor commands. Code that only selects a PHY through a + * const struct basic_radio_phy pointer needs + * only. + * + * A PHY is an LRF_Config generated by SysConfig (RadioConfig), the + * phyFeatures word that selects one of its sub-PHYs and the default runtime + * parameters (frequency, sync word, TX power) applied when the PHY is + * selected. The export holds no runtime parameters: the defaults are chosen + * by whoever defines the PHY. The driver reads the frame header layout and + * the TX power table from the register list of the LRF_Config. The + * LRF_Config must use the generic PBE image; basic_radio_config() returns + * -ENOTSUP for any other. + * + * Custom PHY: + * 1. Add the PHY in SysConfig RadioConfig with the generic PBE image and + * generate ti_radio_config.c/.h. Generate all custom PHYs of an + * application in one RadioConfig project: separate exports define the + * same global symbols (for example LRF_txPowerTable) and do not link + * together. + * 2. Add the export to the application: + * target_sources(app PRIVATE src/ti_radio_config.c). + * 3. Define one PHY object per sub-PHY: + * @code + * #include + * #include "ti_radio_config.h" + * + * static BASIC_RADIO_CC23X0_PHY_DEFINE(my_phy_2m, &LRF_config, + * RCL_PHY_FEATURE_SUB_PHY_2_MBPS, + * 2400000000U, 2483500000U, + * 2440000000U, 0x930B51DEU, 0); + * + * const struct basic_radio_config cfg = { + * .phy = BASIC_RADIO_CC23X0_PHY(my_phy_2m), + * }; + * @endcode + * basic_radio_config::phy is an opaque const struct basic_radio_phy + * pointer: BASIC_RADIO_CC23X0_PHY() converts a CC23x0 PHY object to it. + * + * Several custom PHYs: define one object per PHY from the same export and + * set CONFIG_BASIC_RADIO_CC23X0_MAX_HDR_LEN to the longest frame header. + * Switching between sub-PHYs of one LRF_Config only changes phyFeatures; + * switching to another LRF_Config reopens the RCL client. To select PHYs by + * index, keep an array of their pointers: + * @code + * static const struct basic_radio_phy *const phys[] = { + * BASIC_RADIO_CC23X0_PHY(my_phy_a), + * BASIC_RADIO_CC23X0_PHY(my_phy_b), + * BASIC_RADIO_CC23X0_PHY(basic_radio_cc23x0_phy_ble5_1m), + * }; + * + * const struct basic_radio_config cfg = { .phy = phys[i] }; + * + * ret = basic_radio_config(dev, &cfg); + * @endcode + * + * An application that selects its own PHY at startup disables + * CONFIG_BASIC_RADIO_CC23X0_DEFAULT_PHY, so the default PHY is not linked. + */ + +#ifndef ZEPHYR_INCLUDE_DRIVERS_BASIC_RADIO_TI_CC23X0_H_ +#define ZEPHYR_INCLUDE_DRIVERS_BASIC_RADIO_TI_CC23X0_H_ + +#include + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @brief CC23x0 PHY object, passed as BASIC_RADIO_CC23X0_PHY() in basic_radio_config::phy. */ +struct basic_radio_cc23x0_phy { + /** Radio configuration generated by SysConfig. */ + const LRF_Config *lrf_config; + /** Lowest characterized carrier frequency in Hz. */ + uint32_t frequency_min; + /** Highest characterized carrier frequency in Hz. */ + uint32_t frequency_max; + /** Carrier frequency in Hz applied when the PHY is selected. */ + uint32_t frequency; + /** Sync word applied when the PHY is selected. */ + uint32_t sync_word; + /** phyFeatures word selecting the sub-PHY within lrf_config (RCL_PHY_FEATURE_*). */ + uint16_t phy_features; + /** TX power in dBm applied when the PHY is selected. */ + int8_t tx_power; +}; + +/** + * @brief Define a CC23x0 PHY object. + * + * Expands to a const object definition; prefix it with @c static for a PHY + * used in one file only. + * + * @param _name Name of the object. + * @param _lrf_config Pointer to the const LRF_Config. + * @param _phy_features phyFeatures word, 0 for a configuration without sub-PHYs. + * @param _frequency_min Lowest characterized carrier frequency in Hz. + * @param _frequency_max Highest characterized carrier frequency in Hz. + * @param _frequency Default carrier frequency in Hz, within the characterized range. + * @param _sync_word Default sync word. + * @param _tx_power Default TX power in dBm, within the TX power table of the export. + */ +#define BASIC_RADIO_CC23X0_PHY_DEFINE(_name, _lrf_config, _phy_features, _frequency_min, \ + _frequency_max, _frequency, _sync_word, _tx_power) \ + const struct basic_radio_cc23x0_phy _name = { \ + .lrf_config = (_lrf_config), \ + .frequency_min = (_frequency_min), \ + .frequency_max = (_frequency_max), \ + .frequency = (_frequency), \ + .sync_word = (_sync_word), \ + .phy_features = (_phy_features), \ + .tx_power = (_tx_power), \ + } + +/** + * @brief Opaque PHY pointer of a CC23x0 PHY object, for basic_radio_config::phy. + * + * @param _name Name of the object given to BASIC_RADIO_CC23X0_PHY_DEFINE(). + */ +#define BASIC_RADIO_CC23X0_PHY(_name) ((const struct basic_radio_phy *)&(_name)) + +/** + * @name Certified PHYs + * + * Defined per device in drivers/basic_radio/backends/ti/cc23x0/ + * basic_radio_cc23x0_phy_catalog_.c. Naming a PHY that the device's + * catalog does not define fails to link. + * @{ + */ + +/** @brief 2.4 GHz BLE 5 generic PBE, 1 Mbps. Devices: CC2340R5. */ +extern const struct basic_radio_cc23x0_phy basic_radio_cc23x0_phy_ble5_1m; +/** @brief 2.4 GHz BLE 5 generic PBE, 2 Mbps. Devices: CC2340R5. */ +extern const struct basic_radio_cc23x0_phy basic_radio_cc23x0_phy_ble5_2m; +/** @brief 2.4 GHz BLE 5 generic PBE, LE Coded S=8. Devices: CC2340R5. */ +extern const struct basic_radio_cc23x0_phy basic_radio_cc23x0_phy_ble5_coded; +/** @brief 2.4 GHz IEEE 802.15.4 generic PBE, 250 kbps O-QPSK. Devices: CC2340R5. */ +extern const struct basic_radio_cc23x0_phy basic_radio_cc23x0_phy_ieee_802_15_4; +/** @brief 2.4 GHz proprietary, 250 kbps MSK, 1/2-rate FEC. Devices: CC2340R5. */ +extern const struct basic_radio_cc23x0_phy basic_radio_cc23x0_phy_prop_250kbps_msk_fec; +/** @brief 2.4 GHz proprietary, 2 Mbps GFSK, 320 kHz deviation. Devices: CC2340R5. */ +extern const struct basic_radio_cc23x0_phy basic_radio_cc23x0_phy_prop_2mbps_320khz_gfsk; + +/** + * @brief PHY applied at init, one of the certified PHYs of the device. + * + * Applied only with CONFIG_BASIC_RADIO_CC23X0_DEFAULT_PHY. + */ +extern const struct basic_radio_cc23x0_phy *const basic_radio_cc23x0_phy_default; + +/** + * @brief Catalog PHYs of the standard PHY profiles, indexed by enum basic_radio_std_phy. + * + * NULL for a profile the device's catalog has no PHY for. Returned by + * basic_radio_phy_get_std(). + */ +extern const struct basic_radio_cc23x0_phy *const + basic_radio_cc23x0_std_phys[BASIC_RADIO_STD_PHY_COUNT]; + +/** @} */ + +/** + * @name Vendor commands for basic_radio_ext_ctl() + * + * High byte 0x01 is the TI CC23x0 vendor tag, so a command sent to another + * backend returns -ENOTSUP. + * @{ + */ + +/** @brief Vendor command IDs. */ +enum ti_cc23x0_ext_cmd { + TI_CC23X0_EXT_GET_STATS = 0x01000002, /**< arg: struct ti_cc23x0_ext_stats */ +}; + +/** + * @brief Argument of TI_CC23X0_EXT_GET_STATS. + * + * Counters since init. basic_radio_ext_ctl() returns -EINVAL unless + * arg_sz is sizeof(struct ti_cc23x0_ext_stats). + */ +struct ti_cc23x0_ext_stats { + uint32_t rx_ok; /**< Packets received with a valid CRC. */ + uint32_t rx_crc_err; /**< Packets received with a CRC error. */ + uint32_t sched_reject; /**< Commands rejected or delayed by the scheduler. */ + uint32_t cmd_started; /**< Commands started. */ +}; + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_DRIVERS_BASIC_RADIO_TI_CC23X0_H_ */ diff --git a/modules/Kconfig.simplelink b/modules/Kconfig.simplelink index 9e0cd8313224e..05c9dbba1d240 100644 --- a/modules/Kconfig.simplelink +++ b/modules/Kconfig.simplelink @@ -125,3 +125,11 @@ config SIMPLELINK_LPF3_TRACE_TIMESTAMP help When timestamps are enabled, the log elements have timestamps associated with them. + +config SIMPLELINK_F3_RADIO_PHYS + bool + depends on RCL_DRIVER + help + Build the certified PHY settings (LRF_Config) from hal_ti. Each + PHY exports one LRF_config_ symbol; unreferenced PHYs are + removed at link time. diff --git a/samples/drivers/basic_radio/radio_rx/CMakeLists.txt b/samples/drivers/basic_radio/radio_rx/CMakeLists.txt new file mode 100644 index 0000000000000..accb70002e759 --- /dev/null +++ b/samples/drivers/basic_radio/radio_rx/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (c) 2026 Texas Instruments Incorporated +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(basic_radio_rx) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/drivers/basic_radio/radio_rx/boards/lp_em_cc2340r5.overlay b/samples/drivers/basic_radio/radio_rx/boards/lp_em_cc2340r5.overlay new file mode 100644 index 0000000000000..10d09f1601a95 --- /dev/null +++ b/samples/drivers/basic_radio/radio_rx/boards/lp_em_cc2340r5.overlay @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * SPDX-License-Identifier: Apache-2.0 + * + * basic_radio sample overlay for CC23x0-based LaunchPads. The radio starts + * with the default PHY of the CC2340R5 catalog and that PHY's default + * frequency, sync word and TX power. To use another PHY, see + * . + */ + +/ { + basic_radio0: basic_radio { + compatible = "ti,cc23x0-basic-radio"; + status = "okay"; + }; +}; diff --git a/samples/drivers/basic_radio/radio_rx/prj.conf b/samples/drivers/basic_radio/radio_rx/prj.conf new file mode 100644 index 0000000000000..bd6b56849ecd6 --- /dev/null +++ b/samples/drivers/basic_radio/radio_rx/prj.conf @@ -0,0 +1,4 @@ +CONFIG_BASIC_RADIO=y +CONFIG_GPIO=y +CONFIG_LOG=y +CONFIG_LOG_DEFAULT_LEVEL=3 diff --git a/samples/drivers/basic_radio/radio_rx/src/main.c b/samples/drivers/basic_radio/radio_rx/src/main.c new file mode 100644 index 0000000000000..60002909ded8e --- /dev/null +++ b/samples/drivers/basic_radio/radio_rx/src/main.c @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(basic_radio_rx, LOG_LEVEL_INF); + +#define BASIC_RADIO_NODE DT_NODELABEL(basic_radio0) + +static const struct device *const rf_dev = DEVICE_DT_GET(BASIC_RADIO_NODE); + +#define LED0_NODE DT_ALIAS(led0) +static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); + +#define RX_BUF_LEN 255 +#define RX_QUEUE_LEN 8 + +/* A received packet, copied out of the radio buffer by the callback */ +struct rx_pkt { + uint64_t timestamp_us; + uint16_t len; + int8_t rssi; + uint8_t data[RX_BUF_LEN]; +}; + +K_MSGQ_DEFINE(rx_queue, sizeof(struct rx_pkt), RX_QUEUE_LEN, 4); + +/* + * Staging copy for k_msgq_put(). Static rather than on the stack, which may + * be the interrupt stack; the callback never runs concurrently with itself. + */ +static struct rx_pkt rx_scratch; + +/* Given by the callback whenever main() has something to do */ +static K_SEM_DEFINE(rx_wake, 0, K_SEM_MAX_LIMIT); + +/* Packets the callback could not queue because main() fell behind */ +static atomic_t rx_dropped; + +/* Set by the callback when reception ends, with its status */ +static atomic_t rx_done; +static int rx_done_status; + +/* + * The callback may run in interrupt context, and the radio holds its RX + * buffer until the callback returns. Only copy the packet and wake main(); + * logging and processing happen there. + */ +static void rx_cb(const struct device *dev, const struct basic_radio_evt *evt, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(user_data); + + switch (evt->type) { + case BASIC_RADIO_EVT_RX_PKT: + rx_scratch.timestamp_us = evt->timestamp_us; + rx_scratch.len = MIN(evt->len, RX_BUF_LEN); + rx_scratch.rssi = evt->info.rssi; + memcpy(rx_scratch.data, evt->data, rx_scratch.len); + + if (k_msgq_put(&rx_queue, &rx_scratch, K_NO_WAIT) != 0) { + atomic_inc(&rx_dropped); + } + + k_sem_give(&rx_wake); + break; + + case BASIC_RADIO_EVT_RX_DONE: + rx_done_status = evt->status; + atomic_set(&rx_done, 1); + k_sem_give(&rx_wake); + break; + + default: + break; + } +} + +static void rx_pkt_print(const struct rx_pkt *pkt, uint32_t cnt) +{ + if (pkt->rssi == BASIC_RADIO_RSSI_INVALID) { + LOG_INF("RX #%u: %u bytes at %llu us", cnt, pkt->len, + (unsigned long long)pkt->timestamp_us); + } else { + LOG_INF("RX #%u: %u bytes at %llu us RSSI=%d dBm", cnt, pkt->len, + (unsigned long long)pkt->timestamp_us, pkt->rssi); + } + + LOG_HEXDUMP_INF(pkt->data, pkt->len, "payload"); +} + +int main(void) +{ + struct rx_pkt pkt; + uint32_t cnt = 0U; + int ret; + + if (!device_is_ready(rf_dev)) { + LOG_ERR("basic_radio device not ready"); + return -ENODEV; + } + + if (!device_is_ready(led.port)) { + LOG_ERR("LED device not ready"); + return -ENODEV; + } + + ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE); + if (ret != 0) { + LOG_ERR("LED configure failed: %d", ret); + return ret; + } + + /* + * The radio starts on its default PHY with the PHY's default frequency + * and sync word; only the maximum packet length is set here. + * + * To use another PHY, set .phy to its opaque pointer. PHY objects are + * defined by the radio backend, in the header named by the radio's + * devicetree binding: + * + * .phy = other_phy, (const struct basic_radio_phy *) + * + * The transmitter must use the same PHY. + */ + const struct basic_radio_config cfg = { + .phy = NULL, + .flags = BASIC_RADIO_CFG_MAX_PACKET_LEN, + .max_packet_len = RX_BUF_LEN, + }; + + ret = basic_radio_config(rf_dev, &cfg); + if (ret != 0) { + LOG_ERR("basic_radio_config failed: %d", ret); + return ret; + } + + struct basic_radio_caps caps; + + ret = basic_radio_get_caps(rf_dev, &caps); + if (ret == 0) { + LOG_INF("caps: flags=0x%08x freq=%llu-%llu Hz power=%d..%d dBm", caps.flags, + (unsigned long long)caps.freq_min, (unsigned long long)caps.freq_max, + caps.tx_power_min_dbm, caps.tx_power_max_dbm); + } + + LOG_INF("Listening for packets"); + ret = basic_radio_recv_async(rf_dev, NULL, rx_cb, NULL); + if (ret != 0) { + LOG_ERR("recv_async failed: %d", ret); + return ret; + } + + while (true) { + k_sem_take(&rx_wake, K_FOREVER); + + while (k_msgq_get(&rx_queue, &pkt, K_NO_WAIT) == 0) { + cnt++; + rx_pkt_print(&pkt, cnt); + gpio_pin_toggle_dt(&led); + } + + atomic_val_t dropped = atomic_clear(&rx_dropped); + + if (dropped != 0) { + LOG_WRN("%ld packets dropped: queue full", (long)dropped); + } + + if (!atomic_cas(&rx_done, 1, 0)) { + continue; + } + + /* Continuous reception ends only on stop or error: restart it */ + if (rx_done_status == -ENOBUFS) { + LOG_WRN("RX ran out of buffers, restarting"); + } else { + LOG_INF("RX done: %d, restarting", rx_done_status); + } + + ret = basic_radio_recv_async(rf_dev, NULL, rx_cb, NULL); + if (ret != 0) { + LOG_ERR("recv_async failed: %d", ret); + return ret; + } + } + + return 0; +} diff --git a/samples/drivers/basic_radio/radio_rx/tests.yaml b/samples/drivers/basic_radio/radio_rx/tests.yaml new file mode 100644 index 0000000000000..81ef7286ace1a --- /dev/null +++ b/samples/drivers/basic_radio/radio_rx/tests.yaml @@ -0,0 +1,17 @@ +sample: + name: Basic Radio RX + description: Receive packets through the basic_radio API. +common: + tags: + - basic_radio + harness: console + harness_config: + type: one_line + regex: + - "Listening for packets" +tests: + sample.drivers.basic_radio.radio_rx: + platform_allow: + - lp_em_cc2340r5 + integration_platforms: + - lp_em_cc2340r5 diff --git a/samples/drivers/basic_radio/radio_test_cw/CMakeLists.txt b/samples/drivers/basic_radio/radio_test_cw/CMakeLists.txt new file mode 100644 index 0000000000000..d956270a7e039 --- /dev/null +++ b/samples/drivers/basic_radio/radio_test_cw/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (c) 2026 Texas Instruments Incorporated +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(basic_radio_cw) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/drivers/basic_radio/radio_test_cw/boards/lp_em_cc2340r5.overlay b/samples/drivers/basic_radio/radio_test_cw/boards/lp_em_cc2340r5.overlay new file mode 100644 index 0000000000000..10d09f1601a95 --- /dev/null +++ b/samples/drivers/basic_radio/radio_test_cw/boards/lp_em_cc2340r5.overlay @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * SPDX-License-Identifier: Apache-2.0 + * + * basic_radio sample overlay for CC23x0-based LaunchPads. The radio starts + * with the default PHY of the CC2340R5 catalog and that PHY's default + * frequency, sync word and TX power. To use another PHY, see + * . + */ + +/ { + basic_radio0: basic_radio { + compatible = "ti,cc23x0-basic-radio"; + status = "okay"; + }; +}; diff --git a/samples/drivers/basic_radio/radio_test_cw/prj.conf b/samples/drivers/basic_radio/radio_test_cw/prj.conf new file mode 100644 index 0000000000000..bd6b56849ecd6 --- /dev/null +++ b/samples/drivers/basic_radio/radio_test_cw/prj.conf @@ -0,0 +1,4 @@ +CONFIG_BASIC_RADIO=y +CONFIG_GPIO=y +CONFIG_LOG=y +CONFIG_LOG_DEFAULT_LEVEL=3 diff --git a/samples/drivers/basic_radio/radio_test_cw/src/main.c b/samples/drivers/basic_radio/radio_test_cw/src/main.c new file mode 100644 index 0000000000000..3167f874aa97b --- /dev/null +++ b/samples/drivers/basic_radio/radio_test_cw/src/main.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +LOG_MODULE_REGISTER(basic_radio_cw, LOG_LEVEL_INF); + +#define BASIC_RADIO_NODE DT_NODELABEL(basic_radio0) + +static const struct device *const rf_dev = DEVICE_DT_GET(BASIC_RADIO_NODE); + +#define CW_DURATION_MS 5000U + +static struct k_sem tone_done_sem; + +static void tone_done_cb(const struct device *dev, const struct basic_radio_evt *evt, + void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(user_data); + + if (evt->type == BASIC_RADIO_EVT_TONE_DONE) { + if (evt->status == 0) { + LOG_INF("Tone done"); + } else { + LOG_ERR("Tone failed: %d", evt->status); + } + k_sem_give(&tone_done_sem); + } +} + +int main(void) +{ + int ret; + + if (!device_is_ready(rf_dev)) { + LOG_ERR("basic_radio device not ready"); + return -ENODEV; + } + + struct basic_radio_caps caps; + + ret = basic_radio_get_caps(rf_dev, &caps); + if (ret == 0) { + LOG_INF("caps: flags=0x%08x freq=%llu-%llu Hz power=%d..%d dBm", caps.flags, + (unsigned long long)caps.freq_min, (unsigned long long)caps.freq_max, + caps.tx_power_min_dbm, caps.tx_power_max_dbm); + } + + LOG_INF("Emitting CW for %u ms", CW_DURATION_MS); + k_sem_init(&tone_done_sem, 0, 1); + + /* No CHANNEL or POWER flag: frequency and TX power of the default PHY */ + struct basic_radio_tone_params tone_params = { + .flags = 0U, + .duration_us = CW_DURATION_MS * 1000U, + }; + + ret = basic_radio_tone_async(rf_dev, &tone_params, tone_done_cb, NULL); + if (ret != 0) { + LOG_ERR("tone_async failed: %d", ret); + return ret; + } + + k_sem_take(&tone_done_sem, K_FOREVER); + + LOG_INF("CW done"); + return 0; +} diff --git a/samples/drivers/basic_radio/radio_test_cw/tests.yaml b/samples/drivers/basic_radio/radio_test_cw/tests.yaml new file mode 100644 index 0000000000000..e564cbd5d15de --- /dev/null +++ b/samples/drivers/basic_radio/radio_test_cw/tests.yaml @@ -0,0 +1,17 @@ +sample: + name: Basic Radio Tone + description: Transmit a continuous wave through the basic_radio API. +common: + tags: + - basic_radio + harness: console + harness_config: + type: one_line + regex: + - "CW done" +tests: + sample.drivers.basic_radio.radio_test_cw: + platform_allow: + - lp_em_cc2340r5 + integration_platforms: + - lp_em_cc2340r5 diff --git a/samples/drivers/basic_radio/radio_tx/CMakeLists.txt b/samples/drivers/basic_radio/radio_tx/CMakeLists.txt new file mode 100644 index 0000000000000..b32303a50ab42 --- /dev/null +++ b/samples/drivers/basic_radio/radio_tx/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (c) 2026 Texas Instruments Incorporated +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(basic_radio_tx) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/drivers/basic_radio/radio_tx/boards/lp_em_cc2340r5.overlay b/samples/drivers/basic_radio/radio_tx/boards/lp_em_cc2340r5.overlay new file mode 100644 index 0000000000000..10d09f1601a95 --- /dev/null +++ b/samples/drivers/basic_radio/radio_tx/boards/lp_em_cc2340r5.overlay @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * SPDX-License-Identifier: Apache-2.0 + * + * basic_radio sample overlay for CC23x0-based LaunchPads. The radio starts + * with the default PHY of the CC2340R5 catalog and that PHY's default + * frequency, sync word and TX power. To use another PHY, see + * . + */ + +/ { + basic_radio0: basic_radio { + compatible = "ti,cc23x0-basic-radio"; + status = "okay"; + }; +}; diff --git a/samples/drivers/basic_radio/radio_tx/prj.conf b/samples/drivers/basic_radio/radio_tx/prj.conf new file mode 100644 index 0000000000000..bd6b56849ecd6 --- /dev/null +++ b/samples/drivers/basic_radio/radio_tx/prj.conf @@ -0,0 +1,4 @@ +CONFIG_BASIC_RADIO=y +CONFIG_GPIO=y +CONFIG_LOG=y +CONFIG_LOG_DEFAULT_LEVEL=3 diff --git a/samples/drivers/basic_radio/radio_tx/src/main.c b/samples/drivers/basic_radio/radio_tx/src/main.c new file mode 100644 index 0000000000000..74a528c3c4565 --- /dev/null +++ b/samples/drivers/basic_radio/radio_tx/src/main.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2026 Texas Instruments Incorporated + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(basic_radio_tx, LOG_LEVEL_INF); + +#define BASIC_RADIO_NODE DT_NODELABEL(basic_radio0) + +static const struct device *const rf_dev = DEVICE_DT_GET(BASIC_RADIO_NODE); + +#define LED0_NODE DT_ALIAS(led0) +static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); + +#define PACKET_INTERVAL_MS 2000U + +static const uint8_t payload[] = { + 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, + 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, + 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, + 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, +}; + +static struct k_sem tx_done_sem; +static uint32_t cnt; + +static void tx_done_cb(const struct device *dev, const struct basic_radio_evt *evt, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(user_data); + + if (evt->type == BASIC_RADIO_EVT_TX_DONE) { + if (evt->status == 0) { + cnt++; + LOG_INF("TX #%u: %zu bytes sent", cnt, sizeof(payload)); + LOG_HEXDUMP_INF(payload, sizeof(payload), "payload"); + gpio_pin_set_dt(&led, 1); + } else { + LOG_ERR("TX failed: %d", evt->status); + } + k_sem_give(&tx_done_sem); + } +} + +int main(void) +{ + int ret; + + if (!device_is_ready(rf_dev)) { + LOG_ERR("basic_radio device not ready"); + return -ENODEV; + } + + if (!device_is_ready(led.port)) { + LOG_ERR("LED device not ready"); + return -ENODEV; + } + + ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE); + if (ret != 0) { + LOG_ERR("LED configure failed: %d", ret); + return ret; + } + + k_sem_init(&tx_done_sem, 0, 1); + + /* + * The radio starts on its default PHY with the PHY's default frequency, + * sync word and TX power. To change them, call basic_radio_config(), e.g. + * { .flags = BASIC_RADIO_CFG_FREQUENCY, .frequency_hz = 2410000000U } to + * move to another channel. + * + * To use another PHY, pass its opaque pointer. PHY objects are defined + * by the radio backend, in the header named by the radio's devicetree + * binding: + * + * const struct basic_radio_phy *other_phy = ...; + * const struct basic_radio_config cfg = { .phy = other_phy }; + * + * ret = basic_radio_config(rf_dev, &cfg); + * + * The receiver must use the same PHY. + */ + struct basic_radio_caps caps; + + ret = basic_radio_get_caps(rf_dev, &caps); + if (ret == 0) { + LOG_INF("caps: flags=0x%08x freq=%llu-%llu Hz power=%d..%d dBm", caps.flags, + (unsigned long long)caps.freq_min, (unsigned long long)caps.freq_max, + caps.tx_power_min_dbm, caps.tx_power_max_dbm); + } + + while (true) { + LOG_INF("Transmitting every %u ms", PACKET_INTERVAL_MS); + ret = basic_radio_send_async(rf_dev, payload, sizeof(payload), NULL, tx_done_cb, + NULL); + + if (ret != 0) { + LOG_ERR("send_async failed: %d", ret); + } else { + k_sem_take(&tx_done_sem, K_FOREVER); + k_sleep(K_MSEC(100)); + gpio_pin_set_dt(&led, 0); + } + + k_sleep(K_MSEC(PACKET_INTERVAL_MS)); + } + + return 0; +} diff --git a/samples/drivers/basic_radio/radio_tx/tests.yaml b/samples/drivers/basic_radio/radio_tx/tests.yaml new file mode 100644 index 0000000000000..b66ffa7bbb9e4 --- /dev/null +++ b/samples/drivers/basic_radio/radio_tx/tests.yaml @@ -0,0 +1,17 @@ +sample: + name: Basic Radio TX + description: Transmit packets through the basic_radio API. +common: + tags: + - basic_radio + harness: console + harness_config: + type: one_line + regex: + - "TX #1: \\d+ bytes sent" +tests: + sample.drivers.basic_radio.radio_tx: + platform_allow: + - lp_em_cc2340r5 + integration_platforms: + - lp_em_cc2340r5 diff --git a/west.yml b/west.yml index 5c1c9630dad47..de78e9984633d 100644 --- a/west.yml +++ b/west.yml @@ -276,7 +276,7 @@ manifest: - hal - name: hal_ti remote: downstream - revision: 603fdd64972d02955649b6a95f8102152905f4ef + revision: 2f10346769145bd13b98973b90cc86a1bee66f79 path: modules/hal/ti groups: - hal