mirror of
https://github.com/gosticks/RIOT.git
synced 2025-10-16 12:05:37 +00:00
drivers/sx127x: refactor error management
This commit is contained in:
parent
0a8590c78b
commit
1bee32c75f
@ -100,8 +100,8 @@ extern "C" {
|
||||
enum {
|
||||
SX127X_INIT_OK = 0, /**< Initialization was successful */
|
||||
SX127X_ERR_SPI, /**< Failed to initialize SPI bus or CS line */
|
||||
SX127X_ERR_TEST_FAILED, /**< SX127X testing failed during initialization (check chip) */
|
||||
SX127X_ERR_THREAD /**< Unable to create DIO handling thread (check amount of free memory) */
|
||||
SX127X_ERR_GPIOS, /**< Failed to initialize GPIOs */
|
||||
SX127X_ERR_NODEV /**< No valid device version found */
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -42,12 +42,14 @@ extern "C" {
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Tests the transceiver version type.
|
||||
* @brief Check the transceiver version
|
||||
*
|
||||
* @param[in] dev The sx127x device descriptor
|
||||
* @return true if test passed, false otherwise
|
||||
*
|
||||
* @return 0 when a valid device version is found
|
||||
* @return -1 when no valid device version is found
|
||||
*/
|
||||
bool sx127x_test(const sx127x_t *dev);
|
||||
int sx127x_check_version(const sx127x_t *dev);
|
||||
|
||||
/**
|
||||
* @brief Writes the radio register at specified address.
|
||||
|
||||
@ -41,9 +41,9 @@
|
||||
#include "debug.h"
|
||||
|
||||
/* Internal functions */
|
||||
static void _init_isrs(sx127x_t *dev);
|
||||
static int _init_spi(sx127x_t *dev);
|
||||
static int _init_gpios(sx127x_t *dev);
|
||||
static void _init_timers(sx127x_t *dev);
|
||||
static int _init_peripherals(sx127x_t *dev);
|
||||
static void _on_tx_timeout(void *arg);
|
||||
static void _on_rx_timeout(void *arg);
|
||||
|
||||
@ -90,14 +90,15 @@ void sx127x_reset(const sx127x_t *dev)
|
||||
int sx127x_init(sx127x_t *dev)
|
||||
{
|
||||
/* Do internal initialization routines */
|
||||
if (!_init_peripherals(dev)) {
|
||||
if (_init_spi(dev) < 0) {
|
||||
DEBUG("[sx127x] error: failed to initialize SPI\n");
|
||||
return -SX127X_ERR_SPI;
|
||||
}
|
||||
|
||||
/* Check presence of SX127X */
|
||||
if (!sx127x_test(dev)) {
|
||||
DEBUG("[sx127x] init: sx127x test failed\n");
|
||||
return -SX127X_ERR_TEST_FAILED;
|
||||
if (sx127x_check_version(dev) < 0) {
|
||||
DEBUG("[sx127x] error: no valid device found\n");
|
||||
return -SX127X_ERR_NODEV;
|
||||
}
|
||||
|
||||
_init_timers(dev);
|
||||
@ -110,14 +111,17 @@ int sx127x_init(sx127x_t *dev)
|
||||
#endif
|
||||
sx127x_set_op_mode(dev, SX127X_RF_OPMODE_SLEEP);
|
||||
|
||||
_init_isrs(dev);
|
||||
if (_init_gpios(dev) < 0) {
|
||||
DEBUG("[sx127x] error: failed to initialize GPIOs\n");
|
||||
return -SX127X_ERR_GPIOS;
|
||||
}
|
||||
|
||||
return SX127X_INIT_OK;
|
||||
}
|
||||
|
||||
void sx127x_init_radio_settings(sx127x_t *dev)
|
||||
{
|
||||
DEBUG("[sx127x] initialize radio settings\n");
|
||||
DEBUG("[sx127x] initializing radio settings\n");
|
||||
sx127x_set_channel(dev, SX127X_CHANNEL_DEFAULT);
|
||||
sx127x_set_modem(dev, SX127X_MODEM_DEFAULT);
|
||||
sx127x_set_tx_power(dev, SX127X_RADIO_TX_POWER);
|
||||
@ -204,27 +208,37 @@ static void sx127x_on_dio3_isr(void *arg)
|
||||
}
|
||||
|
||||
/* Internal event handlers */
|
||||
static void _init_isrs(sx127x_t *dev)
|
||||
static int _init_gpios(sx127x_t *dev)
|
||||
{
|
||||
if (gpio_init_int(dev->params.dio0_pin, GPIO_IN, GPIO_RISING,
|
||||
sx127x_on_dio0_isr, dev) < 0) {
|
||||
DEBUG("[sx127x] error: cannot initialize DIO0 pin\n");
|
||||
int res = gpio_init_int(dev->params.dio0_pin, GPIO_IN, GPIO_RISING,
|
||||
sx127x_on_dio0_isr, dev);
|
||||
if (res < 0) {
|
||||
DEBUG("[sx127x] error: failed to initialize DIO0 pin\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
if (gpio_init_int(dev->params.dio1_pin, GPIO_IN, GPIO_RISING,
|
||||
sx127x_on_dio1_isr, dev) < 0) {
|
||||
DEBUG("[sx127x] error: cannot initialize DIO1 pin\n");
|
||||
res = gpio_init_int(dev->params.dio1_pin, GPIO_IN, GPIO_RISING,
|
||||
sx127x_on_dio1_isr, dev);
|
||||
if (res < 0) {
|
||||
DEBUG("[sx127x] error: failed to initialize DIO1 pin\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
if (gpio_init_int(dev->params.dio2_pin, GPIO_IN, GPIO_RISING,
|
||||
sx127x_on_dio2_isr, dev) < 0) {
|
||||
DEBUG("[sx127x] error: cannot initialize DIO2 pin\n");
|
||||
res = gpio_init_int(dev->params.dio2_pin, GPIO_IN, GPIO_RISING,
|
||||
sx127x_on_dio2_isr, dev);
|
||||
if (res < 0) {
|
||||
DEBUG("[sx127x] error: failed to initialize DIO2 pin\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
if (gpio_init_int(dev->params.dio3_pin, GPIO_IN, GPIO_RISING,
|
||||
sx127x_on_dio3_isr, dev) < 0) {
|
||||
DEBUG("[sx127x] error: cannot initialize DIO3 pin\n");
|
||||
res = gpio_init_int(dev->params.dio3_pin, GPIO_IN, GPIO_RISING,
|
||||
sx127x_on_dio3_isr, dev);
|
||||
if (res < 0) {
|
||||
DEBUG("[sx127x] error: failed to initialize DIO3 pin\n");
|
||||
return res;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static void _on_tx_timeout(void *arg)
|
||||
@ -250,7 +264,7 @@ static void _init_timers(sx127x_t *dev)
|
||||
dev->_internal.rx_timeout_timer.callback = _on_rx_timeout;
|
||||
}
|
||||
|
||||
static int _init_peripherals(sx127x_t *dev)
|
||||
static int _init_spi(sx127x_t *dev)
|
||||
{
|
||||
int res;
|
||||
|
||||
@ -258,11 +272,11 @@ static int _init_peripherals(sx127x_t *dev)
|
||||
res = spi_init_cs(dev->params.spi, dev->params.nss_pin);
|
||||
|
||||
if (res != SPI_OK) {
|
||||
DEBUG("[sx127x] error initializing SPI_%i device (code %i)\n",
|
||||
dev->params.spi, res);
|
||||
return 0;
|
||||
DEBUG("[sx127x] error: failed to initialize SPI_%i device (code %i)\n",
|
||||
dev->params.spi, res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
DEBUG("[sx127x] peripherals initialized with success\n");
|
||||
return 1;
|
||||
DEBUG("[sx127x] SPI_%i initialized with success\n", dev->params.spi);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@
|
||||
#define SX127X_SPI_MODE (SPI_MODE_0)
|
||||
|
||||
|
||||
bool sx127x_test(const sx127x_t *dev)
|
||||
int sx127x_check_version(const sx127x_t *dev)
|
||||
{
|
||||
/* Read version number and compare with sx127x assigned revision */
|
||||
uint8_t version = sx127x_reg_read(dev, SX127X_REG_VERSION);
|
||||
@ -51,19 +51,19 @@ bool sx127x_test(const sx127x_t *dev)
|
||||
if (version != VERSION_SX1272) {
|
||||
DEBUG("[sx127x] sx1272 test failed, invalid version number: %d\n",
|
||||
version);
|
||||
return false;
|
||||
return -1;
|
||||
}
|
||||
DEBUG("[sx127x] SX1272 transceiver detected.\n");
|
||||
DEBUG("[sx127x] SX1272 transceiver detected\n");
|
||||
#else /* MODULE_SX1276) */
|
||||
if (version != VERSION_SX1276) {
|
||||
DEBUG("[sx127x] sx1276 test failed, invalid version number: %d\n",
|
||||
version);
|
||||
return false;
|
||||
return -1;
|
||||
}
|
||||
DEBUG("[sx127x] SX1276 transceiver detected.\n");
|
||||
DEBUG("[sx127x] SX1276 transceiver detected\n");
|
||||
#endif
|
||||
|
||||
return true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sx127x_reg_write(const sx127x_t *dev, uint8_t addr, uint8_t data)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user