1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

netdev2: use params parameter for setup

This commit is contained in:
Oleg Hahm 2016-03-27 21:36:00 +02:00
parent 3ba99aabe4
commit 59674a679b
17 changed files with 146 additions and 157 deletions

View File

@ -46,6 +46,14 @@ typedef struct netdev2_tap {
uint8_t promiscous; /**< Flag for promiscous mode */
} netdev2_tap_t;
/**
* @brief tap interface initialization parameters
*/
typedef struct {
char **tap_name; /**< Name of the host system's tap
inteface to bind to. */
} netdev2_tap_params_t;
/**
* @brief global device struct. driver only supports one tap device as of now.
*/
@ -54,10 +62,10 @@ extern netdev2_tap_t netdev2_tap;
/**
* @brief Setup netdev2_tap_t structure.
*
* @param dev the preallocated netdev2_tap device handle to setup
* @param name Name of the host system's tap inteface to bind to.
* @param dev the preallocated netdev2_tap device handle to setup
* @param params initialization parameters
*/
void netdev2_tap_setup(netdev2_tap_t *dev, const char *name);
void netdev2_tap_setup(netdev2_tap_t *dev, const netdev2_tap_params_t *params);
/**
* @brief Cleanup tap resources

View File

@ -265,9 +265,9 @@ static int _send(netdev2_t *netdev, const struct iovec *vector, int n)
return _native_writev(dev->tap_fd, vector, n);
}
void netdev2_tap_setup(netdev2_tap_t *dev, const char *name) {
void netdev2_tap_setup(netdev2_tap_t *dev, const netdev2_tap_params_t *params) {
dev->netdev.driver = &netdev2_driver_tap;
strncpy(dev->tap_name, name, IFNAMSIZ);
strncpy(dev->tap_name, *(params->tap_name), IFNAMSIZ);
}
static void _tap_isr(void) {

View File

@ -313,7 +313,9 @@ __attribute__((constructor)) static void startup(int argc, char **argv)
native_cpu_init();
native_interrupt_init();
#ifdef MODULE_NETDEV2_TAP
netdev2_tap_setup(&netdev2_tap, argv[1]);
netdev2_tap_params_t p;
p.tap_name = &(argv[1]);
netdev2_tap_setup(&netdev2_tap, &p);
#endif
board_init();

View File

@ -35,23 +35,17 @@
#include "debug.h"
void at86rf2xx_setup(at86rf2xx_t *dev, spi_t spi, spi_speed_t spi_speed,
gpio_t cs_pin, gpio_t int_pin, gpio_t sleep_pin,
gpio_t reset_pin)
void at86rf2xx_setup(at86rf2xx_t *dev, const at86rf2xx_params_t *params)
{
netdev2_t *netdev = (netdev2_t *)dev;
netdev->driver = &at86rf2xx_driver;
/* initialize device descriptor */
dev->spi = spi;
dev->cs_pin = cs_pin;
dev->int_pin = int_pin;
dev->sleep_pin = sleep_pin;
dev->reset_pin = reset_pin;
memcpy(&dev->params, params, sizeof(at86rf2xx_params_t));
dev->idle_state = AT86RF2XX_STATE_TRX_OFF;
dev->state = AT86RF2XX_STATE_SLEEP;
/* initialise SPI */
spi_init_master(dev->spi, SPI_CONF_FIRST_RISING, spi_speed);
spi_init_master(dev->params.spi, SPI_CONF_FIRST_RISING, params->spi_speed);
}
void at86rf2xx_reset(at86rf2xx_t *dev)

View File

@ -455,7 +455,7 @@ void at86rf2xx_set_state(at86rf2xx_t *dev, uint8_t state)
/* Discard all IRQ flags, framebuffer is lost anyway */
at86rf2xx_reg_read(dev, AT86RF2XX_REG__IRQ_STATUS);
/* Go to SLEEP mode from TRX_OFF */
gpio_set(dev->sleep_pin);
gpio_set(dev->params.sleep_pin);
dev->state = state;
} else {
_set_state(dev, state);

View File

@ -32,26 +32,26 @@ void at86rf2xx_reg_write(const at86rf2xx_t *dev,
const uint8_t addr,
const uint8_t value)
{
spi_acquire(dev->spi);
gpio_clear(dev->cs_pin);
spi_transfer_reg(dev->spi,
spi_acquire(dev->params.spi);
gpio_clear(dev->params.cs_pin);
spi_transfer_reg(dev->params.spi,
AT86RF2XX_ACCESS_REG | AT86RF2XX_ACCESS_WRITE | addr,
value, 0);
gpio_set(dev->cs_pin);
spi_release(dev->spi);
gpio_set(dev->params.cs_pin);
spi_release(dev->params.spi);
}
uint8_t at86rf2xx_reg_read(const at86rf2xx_t *dev, const uint8_t addr)
{
char value;
spi_acquire(dev->spi);
gpio_clear(dev->cs_pin);
spi_transfer_reg(dev->spi,
spi_acquire(dev->params.spi);
gpio_clear(dev->params.cs_pin);
spi_transfer_reg(dev->params.spi,
AT86RF2XX_ACCESS_REG | AT86RF2XX_ACCESS_READ | addr,
0, &value);
gpio_set(dev->cs_pin);
spi_release(dev->spi);
gpio_set(dev->params.cs_pin);
spi_release(dev->params.spi);
return (uint8_t)value;
}
@ -61,14 +61,14 @@ void at86rf2xx_sram_read(const at86rf2xx_t *dev,
uint8_t *data,
const size_t len)
{
spi_acquire(dev->spi);
gpio_clear(dev->cs_pin);
spi_transfer_reg(dev->spi,
spi_acquire(dev->params.spi);
gpio_clear(dev->params.cs_pin);
spi_transfer_reg(dev->params.spi,
AT86RF2XX_ACCESS_SRAM | AT86RF2XX_ACCESS_READ,
(char)offset, NULL);
spi_transfer_bytes(dev->spi, NULL, (char *)data, len);
gpio_set(dev->cs_pin);
spi_release(dev->spi);
spi_transfer_bytes(dev->params.spi, NULL, (char *)data, len);
gpio_set(dev->params.cs_pin);
spi_release(dev->params.spi);
}
void at86rf2xx_sram_write(const at86rf2xx_t *dev,
@ -76,21 +76,21 @@ void at86rf2xx_sram_write(const at86rf2xx_t *dev,
const uint8_t *data,
const size_t len)
{
spi_acquire(dev->spi);
gpio_clear(dev->cs_pin);
spi_transfer_reg(dev->spi,
spi_acquire(dev->params.spi);
gpio_clear(dev->params.cs_pin);
spi_transfer_reg(dev->params.spi,
AT86RF2XX_ACCESS_SRAM | AT86RF2XX_ACCESS_WRITE,
(char)offset, NULL);
spi_transfer_bytes(dev->spi, (char *)data, NULL, len);
gpio_set(dev->cs_pin);
spi_release(dev->spi);
spi_transfer_bytes(dev->params.spi, (char *)data, NULL, len);
gpio_set(dev->params.cs_pin);
spi_release(dev->params.spi);
}
void at86rf2xx_fb_start(const at86rf2xx_t *dev)
{
spi_acquire(dev->spi);
gpio_clear(dev->cs_pin);
spi_transfer_byte(dev->spi,
spi_acquire(dev->params.spi);
gpio_clear(dev->params.cs_pin);
spi_transfer_byte(dev->params.spi,
AT86RF2XX_ACCESS_FB | AT86RF2XX_ACCESS_READ,
NULL);
}
@ -99,13 +99,13 @@ void at86rf2xx_fb_read(const at86rf2xx_t *dev,
uint8_t *data,
const size_t len)
{
spi_transfer_bytes(dev->spi, NULL, (char *)data, len);
spi_transfer_bytes(dev->params.spi, NULL, (char *)data, len);
}
void at86rf2xx_fb_stop(const at86rf2xx_t *dev)
{
gpio_set(dev->cs_pin);
spi_release(dev->spi);
gpio_set(dev->params.cs_pin);
spi_release(dev->params.spi);
}
uint8_t at86rf2xx_get_status(const at86rf2xx_t *dev)
@ -123,7 +123,7 @@ void at86rf2xx_assert_awake(at86rf2xx_t *dev)
if(at86rf2xx_get_status(dev) == AT86RF2XX_STATE_SLEEP) {
/* wake up and wait for transition to TRX_OFF */
gpio_clear(dev->sleep_pin);
gpio_clear(dev->params.sleep_pin);
xtimer_usleep(AT86RF2XX_WAKEUP_DELAY);
/* update state */
@ -138,9 +138,9 @@ void at86rf2xx_hardware_reset(at86rf2xx_t *dev)
at86rf2xx_assert_awake(dev);
/* trigger hardware reset */
gpio_clear(dev->reset_pin);
gpio_clear(dev->params.reset_pin);
xtimer_usleep(AT86RF2XX_RESET_PULSE_WIDTH);
gpio_set(dev->reset_pin);
gpio_set(dev->params.reset_pin);
xtimer_usleep(AT86RF2XX_RESET_DELAY);
}

View File

@ -69,13 +69,13 @@ static int _init(netdev2_t *netdev)
at86rf2xx_t *dev = (at86rf2xx_t *)netdev;
/* initialise GPIOs */
gpio_init(dev->cs_pin, GPIO_OUT);
gpio_set(dev->cs_pin);
gpio_init(dev->sleep_pin, GPIO_OUT);
gpio_clear(dev->sleep_pin);
gpio_init(dev->reset_pin, GPIO_OUT);
gpio_set(dev->reset_pin);
gpio_init_int(dev->int_pin, GPIO_IN, GPIO_RISING, _irq_handler, dev);
gpio_init(dev->params.cs_pin, GPIO_OUT);
gpio_set(dev->params.cs_pin);
gpio_init(dev->params.sleep_pin, GPIO_OUT);
gpio_clear(dev->params.sleep_pin);
gpio_init(dev->params.reset_pin, GPIO_OUT);
gpio_set(dev->params.reset_pin);
gpio_init_int(dev->params.int_pin, GPIO_IN, GPIO_RISING, _irq_handler, dev);
/* make sure device is not sleeping, so we can query part number */
at86rf2xx_assert_awake(dev);

View File

@ -82,12 +82,12 @@ static inline void unlock(encx24j600_t *dev) {
mutex_unlock(&dev->mutex);
}
void encx24j600_setup(encx24j600_t *dev, spi_t spi, gpio_t cs, gpio_t int_pin)
void encx24j600_setup(encx24j600_t *dev, const encx24j600_params_t *params)
{
dev->netdev.driver = &netdev2_driver_encx24j600;
dev->spi = spi;
dev->cs = cs;
dev->int_pin = int_pin;
dev->spi = params->spi;
dev->cs = params->cs;
dev->int_pin = params->int_pin;
dev->rx_next_ptr = RX_BUFFER_START;
mutex_init(&dev->mutex);

View File

@ -47,16 +47,16 @@ static const uint8_t _esc_esc[] = {ETHOS_ESC_CHAR, (ETHOS_ESC_CHAR ^ 0x20)};
static const uint8_t _esc_delim[] = {ETHOS_ESC_CHAR, (ETHOS_FRAME_DELIMITER ^ 0x20)};
void ethos_setup(ethos_t *dev, uart_t uart, uint32_t baudrate, uint8_t *buf, size_t bufsize)
void ethos_setup(ethos_t *dev, const ethos_params_t *params)
{
dev->netdev.driver = &netdev2_driver_ethos;
dev->uart = uart;
dev->uart = params->uart;
dev->state = WAIT_FRAMESTART;
dev->framesize = 0;
dev->frametype = 0;
dev->last_framesize = 0;
tsrb_init(&dev->inbuf, (char*)buf, bufsize);
tsrb_init(&dev->inbuf, (char*)params->buf, params->bufsize);
mutex_init(&dev->out_mutex);
uint32_t a = random_uint32();
@ -67,7 +67,7 @@ void ethos_setup(ethos_t *dev, uart_t uart, uint32_t baudrate, uint8_t *buf, siz
dev->mac_addr[0] &= (0x2); /* unset globally unique bit */
dev->mac_addr[0] &= ~(0x1); /* set unicast bit*/
uart_init(uart, baudrate, ethos_isr, (void*)dev);
uart_init(params->uart, params->baudrate, ethos_isr, (void*)dev);
uint8_t frame_delim = ETHOS_FRAME_DELIMITER;
uart_write(dev->uart, &frame_delim, 1);

View File

@ -132,32 +132,6 @@ extern "C" {
* finished */
/** @} */
/**
* @brief Device descriptor for AT86RF2XX radio devices
*
* @extends netdev2_ieee802154_t
*/
typedef struct {
netdev2_ieee802154_t netdev; /**< netdev2 parent struct */
/**
* @brief device specific fields
* @{
*/
spi_t spi; /**< used SPI device */
gpio_t cs_pin; /**< chip select pin */
gpio_t sleep_pin; /**< sleep pin */
gpio_t reset_pin; /**< reset pin */
gpio_t int_pin; /**< external interrupt pin */
uint8_t state; /**< current state of the radio */
uint8_t tx_frame_len; /**< length of the current TX frame */
#ifdef MODULE_AT86RF212B
/* Only AT86RF212B supports multiple pages (PHY modes) */
uint8_t page; /**< currently used channel page */
#endif
uint8_t idle_state; /**< state to return to after sending */
/** @} */
} at86rf2xx_t;
/**
* @brief struct holding all params needed for device initialization
*/
@ -170,20 +144,35 @@ typedef struct at86rf2xx_params {
gpio_t reset_pin; /**< GPIO pin connected to the reset pin */
} at86rf2xx_params_t;
/**
* @brief Device descriptor for AT86RF2XX radio devices
*
* @extends netdev2_ieee802154_t
*/
typedef struct {
netdev2_ieee802154_t netdev; /**< netdev2 parent struct */
/**
* @brief device specific fields
* @{
*/
at86rf2xx_params_t params; /**< parameters for initialization */
uint8_t state; /**< current state of the radio */
uint8_t tx_frame_len; /**< length of the current TX frame */
#ifdef MODULE_AT86RF212B
/* Only AT86RF212B supports multiple pages (PHY modes) */
uint8_t page; /**< currently used channel page */
#endif
uint8_t idle_state; /**< state to return to after sending */
/** @} */
} at86rf2xx_t;
/**
* @brief Setup an AT86RF2xx based device state
*
* @param[out] dev device descriptor
* @param[in] spi SPI bus the device is connected to
* @param[in] spi_speed SPI speed to use
* @param[in] cs_pin GPIO pin connected to chip select
* @param[in] int_pin GPIO pin connected to the interrupt pin
* @param[in] sleep_pin GPIO pin connected to the sleep pin
* @param[in] reset_pin GPIO pin connected to the reset pin
* @param[in] params parameters for device initialization
*/
void at86rf2xx_setup(at86rf2xx_t *dev, spi_t spi, spi_speed_t spi_speed,
gpio_t cs_pin, gpio_t int_pin, gpio_t sleep_pin,
gpio_t reset_pin);
void at86rf2xx_setup(at86rf2xx_t *dev, const at86rf2xx_params_t *params);
/**
* @brief Trigger a hardware reset and configure radio with default values

View File

@ -44,18 +44,25 @@ typedef struct {
mutex_t mutex; /**< mutex used to lock device access */
} encx24j600_t;
/**
* @brief Struct containing the needed peripheral configuration
*/
typedef struct {
spi_t spi; /**< SPI line */
gpio_t cs_pin; /**< chip select pin */
gpio_t int_pin; /**< interrupt pin */
} encx24j600_params_t;
/**
* @brief Setup an encx24j600 based device state.
*
* This function sets SPI pins, initializes the device state structure.
* It does not initialize the device itself.
*
* @param[out] dev the handle of the device to initialize
* @param[in] spi SPI device the device is connected to
* @param[in] cs_pin SPI chip select pin used to select the device
* @param[in] int_pin pin the device will trigger an interrupt on
* @param[out] dev the handle of the device to initialize
* @param[in] params parameters for device initialization
*/
void encx24j600_setup(encx24j600_t *dev, spi_t spi, gpio_t cs_pin, gpio_t int_pin);
void encx24j600_setup(encx24j600_t *dev, const encx24j600_params_t *params);
#ifdef __cplusplus
}

View File

@ -80,6 +80,16 @@ typedef struct {
mutex_t out_mutex; /**< mutex used for locking concurrent sends */
} ethos_t;
/**
* @brief Struct containing the needed configuration
*/
typedef struct {
uart_t uart; /**< UART device to use */
uint32_t baudrate; /**< baudrate to UART device */
uint8_t buf; /**< buffer for incoming packets */
size_t bufsize; /**< size of ethos_params_t::buf */
} ethos_params_t;
/**
* @brief Setup an ethos based device state.
*
@ -90,12 +100,9 @@ typedef struct {
* E.g., if 1536b ethernet frames are expected, 2048 is probably a good size for @p buf.
*
* @param[out] dev handle of the device to initialize
* @param[in] uart UART device to use
* @param[in] baudrate baudrate for UART device
* @param[in] buf buffer for incoming packets
* @param[in] bufsize size of @p buf
* @param[in] params parameters for device initialization
*/
void ethos_setup(ethos_t *dev, uart_t uart, uint32_t baudrate, uint8_t *buf, size_t bufsize);
void ethos_setup(ethos_t *dev, const ethos_params_t *params);
/**
* @brief send frame over serial port using ethos' framing

View File

@ -141,6 +141,18 @@ typedef struct {
uint16_t rx_limit; /**< size RX frame transferred */
} xbee_t;
/**
* @brief auto_init struct holding Xbee device initalization params
*/
typedef struct xbee_params {
uart_t uart; /**< UART interfaced the device is connected to */
uint32_t baudrate; /**< baudrate to use */
gpio_t sleep_pin; /**< GPIO pin that is connected to the SLEEP pin
set to GPIO_UNDEF if not used */
gpio_t reset_pin; /**< GPIO pin that is connected to the STATUS pin
set to GPIO_UNDEF if not used */
} xbee_params_t;
/**
* @brief Reference to the XBee driver interface
*/
@ -150,31 +162,13 @@ extern const gnrc_netdev_driver_t xbee_driver;
* @brief Initialize the given Xbee device
*
* @param[out] dev Xbee device to initialize
* @param[in] uart UART interfaced the device is connected to
* @param[in] baudrate baudrate to use
* @param[in] sleep_pin GPIO pin that is connected to the SLEEP pin, set to
* GPIO_UNDEF if not used
* @param[in] reset_pin GPIO pin that is connected to the STATUS pin, set to
* GPIO_UNDEF if not used
* @param[in] params parameters for device initialization
*
* @return 0 on success
* @return -ENODEV on invalid device descriptor
* @return -ENXIO on invalid UART or GPIO pins
*/
int xbee_init(xbee_t *dev, uart_t uart, uint32_t baudrate,
gpio_t sleep_pin, gpio_t reset_pin);
/**
* @brief auto_init struct holding Xbee device initalization params
*/
typedef struct xbee_params {
uart_t uart; /**< UART interfaced the device is connected to */
uint32_t baudrate; /**< baudrate to use */
gpio_t sleep_pin; /**< GPIO pin that is connected to the SLEEP pin
set to GPIO_UNDEF if not used */
gpio_t reset_pin; /**< GPIO pin that is connected to the STATUS pin
set to GPIO_UNDEF if not used */
} xbee_params_t;
int xbee_init(xbee_t *dev, const xbee_params_t *params);
#ifdef __cplusplus
}

View File

@ -395,8 +395,7 @@ static int _set_proto(xbee_t *dev, uint8_t *val, size_t len)
/*
* Driver's "public" functions
*/
int xbee_init(xbee_t *dev, uart_t uart, uint32_t baudrate,
gpio_t reset_pin, gpio_t sleep_pin)
int xbee_init(xbee_t *dev, const xbee_params_t *params)
{
uint8_t tmp[2];
@ -404,15 +403,15 @@ int xbee_init(xbee_t *dev, uart_t uart, uint32_t baudrate,
if (dev == NULL) {
return -ENODEV;
}
if (uart >= UART_NUMOF) {
if (params->uart >= UART_NUMOF) {
return -ENXIO;
}
/* set device driver */
dev->driver = &xbee_driver;
/* set peripherals to use */
dev->uart = uart;
dev->reset_pin = reset_pin;
dev->sleep_pin = sleep_pin;
dev->uart = params->uart;
dev->reset_pin = params->reset_pin;
dev->sleep_pin = params->sleep_pin;
/* set default options */
dev->addr_flags = 0;
dev->proto = XBEE_DEFAULT_PROTOCOL;
@ -423,29 +422,29 @@ int xbee_init(xbee_t *dev, uart_t uart, uint32_t baudrate,
dev->resp_limit = 1; /* needs to be greater then 0 initially */
dev->rx_count = 0;
/* initialize UART and GPIO pins */
if (uart_init(uart, baudrate, _rx_cb, dev) < 0) {
if (uart_init(params->uart, params->baudrate, _rx_cb, dev) < 0) {
DEBUG("xbee: Error initializing UART\n");
return -ENXIO;
}
if (reset_pin != GPIO_UNDEF) {
if (gpio_init(reset_pin, GPIO_OUT) < 0) {
if (params->reset_pin != GPIO_UNDEF) {
if (gpio_init(params->reset_pin, GPIO_OUT) < 0) {
DEBUG("xbee: Error initializing RESET pin\n");
return -ENXIO;
}
gpio_set(reset_pin);
gpio_set(params->reset_pin);
}
if (sleep_pin != GPIO_UNDEF) {
if (gpio_init(sleep_pin, GPIO_OUT) < 0) {
if (params->sleep_pin != GPIO_UNDEF) {
if (gpio_init(params->sleep_pin, GPIO_OUT) < 0) {
DEBUG("xbee: Error initializing SLEEP pin\n");
return -ENXIO;
}
gpio_clear(sleep_pin);
gpio_clear(params->sleep_pin);
}
/* if reset pin is connected, do a hardware reset */
if (reset_pin != GPIO_UNDEF) {
gpio_clear(reset_pin);
if (params->reset_pin != GPIO_UNDEF) {
gpio_clear(params->reset_pin);
xtimer_usleep(RESET_DELAY);
gpio_set(reset_pin);
gpio_set(params->reset_pin);
}
/* put the XBee device into command mode */
xtimer_usleep(ENTER_CMD_MODE_DELAY);

View File

@ -50,13 +50,7 @@ void auto_init_at86rf2xx(void)
int res;
DEBUG("Initializing AT86RF2xx radio at SPI_%i\n", p->spi);
at86rf2xx_setup(&at86rf2xx_devs[i],
p->spi,
p->spi_speed,
p->cs_pin,
p->int_pin,
p->sleep_pin,
p->reset_pin);
at86rf2xx_setup(&at86rf2xx_devs[i], (at86rf2xx_params_t*) p);
res = gnrc_netdev2_ieee802154_init(&gnrc_adpt[i],
(netdev2_ieee802154_t *)&at86rf2xx_devs[i]);

View File

@ -50,11 +50,7 @@ void auto_init_xbee(void)
for (size_t i = 0; i < XBEE_NUM; i++) {
const xbee_params_t *p = &xbee_params[i];
DEBUG("Initializing XBee radio at UART_%i\n", p->uart);
int res = xbee_init(&xbee_devs[i],
p->uart,
p->baudrate,
p->sleep_pin,
p->reset_pin);
int res = xbee_init(&xbee_devs[i], (xbee_params_t*) p);
if (res < 0) {
DEBUG("Error initializing XBee radio device!");

View File

@ -96,8 +96,7 @@ int main(void)
netdev2_t *dev = (netdev2_t *)(&devs[i]);
printf("Initializing AT86RF2xx radio at SPI_%d\n", p->spi);
at86rf2xx_setup(&devs[i], p->spi, p->spi_speed, p->cs_pin,
p->int_pin, p->sleep_pin, p->reset_pin);
at86rf2xx_setup(&devs[i], (at86rf2xx_params_t*) p);
dev->event_callback = _event_cb;
dev->driver->init(dev);
}