Skip to content

Commit

Permalink
drivers/mrf24j40: add support for IEEE 802.15.4 Radio HAL
Browse files Browse the repository at this point in the history
  • Loading branch information
jia200x committed Jan 19, 2023
1 parent dacc4ff commit 91a299c
Show file tree
Hide file tree
Showing 18 changed files with 589 additions and 944 deletions.
119 changes: 30 additions & 89 deletions drivers/include/mrf24j40.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include "net/netdev.h"
#include "net/netdev/ieee802154.h"
#include "net/gnrc/nettype.h"
#include "net/ieee802154/radio.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -105,6 +106,11 @@ extern "C" {

#define MRF24J40_MAX_FRAME_RETRIES (3U) /**< Number of frame retries (fixed) */

#define MRF24J40_MAX_MINBE (3U) /**< Maximum value of minimum
exponential backoff */
#define MRF24J40_MIN_TXPOWER (-36) /**< Minimum transmission power (dBm) */
#define MRF24J40_MAX_TXPOWER (0) /**< Maximum transmission power (dBm) */

/**
* @defgroup drivers_mrf24j40_config mrf24j40 driver compile configuration
* @ingroup drivers_mrf24j40
Expand Down Expand Up @@ -151,29 +157,13 @@ typedef struct mrf24j40_params {
* @brief Device descriptor for MRF24J40 radio devices
*/
typedef struct {
netdev_ieee802154_t netdev; /**< netdev parent struct */
/* device specific fields */
mrf24j40_params_t params; /**< parameters for initialization */
uint8_t state; /**< current state of the radio */
uint8_t idle_state; /**< state to return to after sending */
uint8_t tx_frame_len; /**< length of the current TX frame */
uint8_t header_len; /**< length of the header */
const mrf24j40_params_t *params; /**< parameters for initialization */
uint8_t fcf_low; /**< Low 8 FCF bits of the current TX frame. */
uint8_t pending; /**< Flags for pending tasks */
uint8_t irq_flag; /**< Flags for IRQs */
uint8_t tx_retries; /**< Number of retries needed for last transmission */
bool tx_pending; /**< Whether a transmission is pending or not */
} mrf24j40_t;

/**
* @brief Setup an MRF24J40 based device state
*
* @param[out] dev device descriptor
* @param[in] params parameters for device initialization
* @param[in] index index of @p params in a global parameter struct array.
* If initialized manually, pass a unique identifier instead.
*/
void mrf24j40_setup(mrf24j40_t *dev, const mrf24j40_params_t *params, uint8_t index);

/**
* @brief Trigger a hardware reset and configure radio with default values
*
Expand All @@ -183,26 +173,6 @@ void mrf24j40_setup(mrf24j40_t *dev, const mrf24j40_params_t *params, uint8_t in
*/
int mrf24j40_reset(mrf24j40_t *dev);

/**
* @brief Trigger a clear channel assessment & retrieve RSSI
*
* @param[in] dev device to use
* @param[in] rssi RSSI value from register in dBm
*
* @return true if channel is clear
* @return false if channel is busy
*/
bool mrf24j40_cca(mrf24j40_t *dev, int8_t *rssi);

/**
* @brief Get the short address of the given device
*
* @param[in] dev device to read from
*
* @return the currently set (2-byte) short address
*/
uint16_t mrf24j40_get_addr_short(mrf24j40_t *dev);

/**
* @brief Set the short address of the given device
*
Expand All @@ -211,14 +181,6 @@ uint16_t mrf24j40_get_addr_short(mrf24j40_t *dev);
*/
void mrf24j40_set_addr_short(mrf24j40_t *dev, uint16_t addr);

/**
* @brief Get the configured long address of the given device
*
* @param[in] dev device to read from
* @param[out] addr the currently set (8-byte) long address
*/
void mrf24j40_get_addr_long(mrf24j40_t *dev, uint8_t *addr);

/**
* @brief Set the long address of the given device
*
Expand Down Expand Up @@ -261,15 +223,6 @@ uint16_t mrf24j40_get_pan(mrf24j40_t *dev);
*/
void mrf24j40_set_pan(mrf24j40_t *dev, uint16_t pan);

/**
* @brief Get the configured transmission power of the given device [in dBm]
*
* @param[in] dev device to read from
*
* @return configured transmission power in dBm
*/
int16_t mrf24j40_get_txpower(mrf24j40_t *dev);

/**
* @brief Set the transmission power of the given device [in dBm]
*
Expand All @@ -283,15 +236,6 @@ int16_t mrf24j40_get_txpower(mrf24j40_t *dev);
*/
void mrf24j40_set_txpower(mrf24j40_t *dev, int16_t txpower);

/**
* @brief Get the maximum number of channel access attempts per frame (CSMA)
*
* @param[in] dev device to read from
*
* @return configured number of retries
*/
uint8_t mrf24j40_get_csma_max_retries(mrf24j40_t *dev);

/**
* @brief Set the maximum number of channel access attempts per frame (CSMA)
*
Expand All @@ -317,15 +261,6 @@ void mrf24j40_set_csma_max_retries(mrf24j40_t *dev, int8_t retries);
*/
void mrf24j40_set_csma_backoff_exp(mrf24j40_t *dev, uint8_t min, uint8_t max);

/**
* @brief Get the CCA threshold value
*
* @param[in] dev device to read value from
*
* @return the current CCA threshold value
*/
int8_t mrf24j40_get_cca_threshold(mrf24j40_t *dev);

/**
* @brief Set the CCA threshold value
*
Expand Down Expand Up @@ -364,34 +299,19 @@ void mrf24j40_set_state(mrf24j40_t *dev, uint8_t state);
*/
void mrf24j40_set_turbo(mrf24j40_t *dev, bool enable);

/**
* @brief Query the state of the turbo mode
*
* @param[in] dev device to query
* @return true if Turbo Mode is enabled
*/
bool mrf24j40_get_turbo(mrf24j40_t *dev);

/**
* @brief Put in sleep mode
*
* @param[in] dev device to put to sleep
*/
void mrf24j40_sleep(mrf24j40_t *dev);

/**
* @brief Put in sleep mode if idle_state is sleep
*
* @param[in] dev device to put to sleep
*/
void mrf24j40_assert_sleep(mrf24j40_t *dev);

/**
* @brief Wake up from sleep mode
*
* @param[in] dev device to eventually wake up
*/
void mrf24j40_assert_awake(mrf24j40_t *dev);
void mrf24j40_wake_up(mrf24j40_t *dev);

/**
* @brief Reset the internal state machine to TRX_OFF mode.
Expand Down Expand Up @@ -452,6 +372,27 @@ size_t mrf24j40_tx_load(mrf24j40_t *dev, uint8_t *data, size_t len,
*/
void mrf24j40_tx_exec(mrf24j40_t *dev);

/**
* @brief IRQ Handler for the MRF24J40 device
*
* @param[in] dev pointer to the IEEE 802.15.4 Radio HAL descriptor
*/
void mrf24j40_radio_irq_handler(void *dev);

/**
* @brief Initialize the given MRF24J40 device
* @param[out] dev device descriptor
* @param[in] params parameters for device initialization
* @param[in] hal pointer to IEEE 802.15.4 Radio HAL descriptor
* @param[in] cb ISR callback
* @param[in] ctx context pointer handed to isr
*
* @return 0 on success
* @return <0 on error
*/
int mrf24j40_init(mrf24j40_t *dev, const mrf24j40_params_t *params, ieee802154_dev_t *hal,
gpio_cb_t cb, void *ctx);

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 5 additions & 5 deletions drivers/mrf24j40/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ menuconfig MODULE_MRF24J40
depends on HAS_PERIPH_GPIO
depends on HAS_PERIPH_GPIO_IRQ
depends on HAS_PERIPH_SPI
select MODULE_IEEE802154
select MODULE_NETDEV
select MODULE_NETDEV_IEEE802154
select MODULE_NETDEV_LEGACY_API
select MODULE_PERIPH_GPIO
select MODULE_PERIPH_GPIO_IRQ
select MODULE_PERIPH_SPI
select MODULE_XTIMER
select MODULE_IEEE802154
select MODULE_ZTIMER_USEC
select HAVE_BHP_IRQ_HANDLER
select HAVE_IEEE802154_RADIO_HAL_INTERFACE
select MRF24J40_OQPSK if MODULE_NETDEV

if MODULE_MRF24J40

Expand Down
9 changes: 5 additions & 4 deletions drivers/mrf24j40/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
USEMODULE += ztimer_usec
USEMODULE += ieee802154
USEMODULE += netdev_ieee802154
USEMODULE += netdev_legacy_api
USEMODULE += xtimer
USEMODULE += bhp

FEATURES_REQUIRED += periph_gpio
FEATURES_REQUIRED += periph_gpio_irq
FEATURES_REQUIRED += periph_spi

ifneq (,$(filter mrf24j40m%,$(USEMODULE)))
ifneq (,$(filter netdev,$(USEMODULE)))
USEMODULE += netdev_ieee802154_submac
DEFAULT_MODULE += netdev_ieee802154_oqpsk
endif
9 changes: 8 additions & 1 deletion drivers/mrf24j40/include/mrf24j40_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern "C" {
*
* @return 0 on success, error otherwise
*/
int mrf24j40_init(mrf24j40_t *dev);
int mrf24j40_init_hw(mrf24j40_t *dev);

/**
* @brief Read from a register with a at address `addr` from device `dev`. Register with 8bit address
Expand Down Expand Up @@ -151,6 +151,13 @@ void mrf24j40_enable_lna(mrf24j40_t *dev);
static inline void mrf24j40_enable_lna(mrf24j40_t *dev) { (void) dev; }
#endif

/**
* @brief Flush the RX FIFO
*
* @param[in] dev device to flush the RX FIFO
*/
void mrf24j40_flush_rx(mrf24j40_t *dev);

#ifdef __cplusplus
}
#endif
Expand Down
22 changes: 22 additions & 0 deletions drivers/mrf24j40/include/mrf24j40_registers.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,28 @@ extern "C" {

/** @} */

/**
* @brief Shift offsets for TXMCR register (0x11)
* @{
*/
#define MRF24J40_TXMCR_MACMINBE_SHIFT (3U)
/** @} */

/**
* @brief Bitfield definitions for the ACKTMOUT register (0x12)
* @{
*/
#define MRF24J40_ACKTMOUT_DRPACK (0x80)
#define MRF24J40_ACKTMOUT_MAWD6 (0x40)
#define MRF24J40_ACKTMOUT_MAWD5 (0x20)
#define MRF24J40_ACKTMOUT_MAWD4 (0x10)
#define MRF24J40_ACKTMOUT_MAWD3 (0x08)
#define MRF24J40_ACKTMOUT_MAWD2 (0x04)
#define MRF24J40_ACKTMOUT_MAWD1 (0x02)
#define MRF24J40_ACKTMOUT_MAWD0 (0x01)

/** @} */

/**
* @name Bitfield definitions for the PACON2 register (0x18)
* @{
Expand Down
Loading

0 comments on commit 91a299c

Please sign in to comment.