Skip to content

Commit

Permalink
Merge pull request #12221 from benpicco/mrf24j40-init_check
Browse files Browse the repository at this point in the history
drivers/mrf24j40: allow for basic self-test on init
  • Loading branch information
leandrolanzieri authored Oct 14, 2019
2 parents e236fa1 + 38b35c5 commit 0e0b6f8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
24 changes: 23 additions & 1 deletion drivers/include/mrf24j40.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ extern "C" {

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

/**
* @defgroup drivers_mrf24j40_config mrf24j40 driver compile configuration
* @ingroup drivers_mrf24j40
* @ingroup config
* @{
*/

/**
* @brief Enable external PA/LNA control
*
Expand All @@ -125,6 +132,19 @@ extern "C" {
#define MRF24J40_USE_EXT_PA_LNA (0U)
#endif

/**
* @brief Enable basic self-test on init
*
* Perform a write / read to a known register on startup to detect
* if the device is connected.
* Enable this if you want the boot not to hang if the device is
* not connected / there are SPI errors.
*/
#ifndef MRF24J40_TEST_SPI_CONNECTION
#define MRF24J40_TEST_SPI_CONNECTION (0U)
#endif
/** @} */

/**
* @brief struct holding all params needed for device initialization
*/
Expand Down Expand Up @@ -165,8 +185,10 @@ void mrf24j40_setup(mrf24j40_t *dev, const mrf24j40_params_t *params);
* @brief Trigger a hardware reset and configure radio with default values
*
* @param[in] dev device to reset
*
* @return 0 on success, error otherwise
*/
void mrf24j40_reset(mrf24j40_t *dev);
int mrf24j40_reset(mrf24j40_t *dev);

/**
* @brief Trigger a clear channel assessment & retrieve RSSI
Expand Down
6 changes: 5 additions & 1 deletion drivers/mrf24j40/include/mrf24j40_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ extern "C" {

/**
* @brief initialization as decribed in datasheet
*
* @param[in] dev device to initialize
*
* @return 0 on success, error otherwise
*/
void mrf24j40_init(mrf24j40_t *dev);
int mrf24j40_init(mrf24j40_t *dev);

/**
* @brief Read from a register with a at address `addr` from device `dev`. Register with 8bit address
Expand Down
10 changes: 8 additions & 2 deletions drivers/mrf24j40/mrf24j40.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ void mrf24j40_setup(mrf24j40_t *dev, const mrf24j40_params_t *params)
dev->params = *params;
}

void mrf24j40_reset(mrf24j40_t *dev)
int mrf24j40_reset(mrf24j40_t *dev)
{
eui64_t addr_long;

mrf24j40_init(dev);
int res = mrf24j40_init(dev);

if (res < 0) {
return res;
}

netdev_ieee802154_reset(&dev->netdev);

Expand Down Expand Up @@ -72,6 +76,8 @@ void mrf24j40_reset(mrf24j40_t *dev)
dev->state = 0;
mrf24j40_set_state(dev, MRF24J40_PSEUDO_STATE_IDLE);
DEBUG("mrf24j40_reset(): reset complete.\n");

return 0;
}

bool mrf24j40_cca(mrf24j40_t *dev, int8_t *rssi)
Expand Down
13 changes: 7 additions & 6 deletions drivers/mrf24j40/mrf24j40_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ void mrf24j40_enable_lna(mrf24j40_t *dev)
}
#endif /* MRF24J40_USE_EXT_PA_LNA */

void mrf24j40_init(mrf24j40_t *dev)
int mrf24j40_init(mrf24j40_t *dev)
{

mrf24j40_hardware_reset(dev);

#if ENABLE_DEBUG
#if MRF24J40_TEST_SPI_CONNECTION
/* Check if MRF24J40 is available */
uint8_t txmcr = mrf24j40_reg_read_short(dev, MRF24J40_REG_TXMCR);
if ((txmcr == 0xFF) || (txmcr == 0x00)) {
Expand All @@ -117,11 +114,13 @@ void mrf24j40_init(mrf24j40_t *dev)
MRF24J40_TXMCR_CSMABF2)) {
DEBUG("[mrf24j40] Initialization failure, SPI interface communication failed\n");
/* Return to prevents hangup later in the initialization */
return;
return -ENODEV;
}
}
#endif

mrf24j40_hardware_reset(dev);

/* do a soft reset */
mrf24j40_reg_write_short(dev, MRF24J40_REG_SOFTRST, MRF24J40_SOFTRST_RSTPWR |
MRF24J40_SOFTRST_RSTBB |
Expand Down Expand Up @@ -165,6 +164,8 @@ void mrf24j40_init(mrf24j40_t *dev)

/* mrf24j40_set_interrupts */
mrf24j40_reg_write_short(dev, MRF24J40_REG_INTCON, ~(MRF24J40_INTCON_RXIE | MRF24J40_INTCON_TXNIE));

return 0;
}

uint8_t mrf24j40_reg_read_short(mrf24j40_t *dev, const uint8_t addr)
Expand Down
5 changes: 4 additions & 1 deletion drivers/mrf24j40/mrf24j40_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ static int _init(netdev_t *netdev)
gpio_init_int(dev->params.int_pin, GPIO_IN, GPIO_RISING, _irq_handler, dev);

/* reset device to default values and put it into RX state */
mrf24j40_reset(dev);
if (mrf24j40_reset(dev)) {
return -ENODEV;
}

return 0;
}

Expand Down

0 comments on commit 0e0b6f8

Please sign in to comment.