Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

at86rf2xx: port to netdev2 #4646

Merged
merged 3 commits into from
Mar 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ ifneq (,$(filter nhdp,$(USEMODULE)))
USEMODULE += oonf_rfc5444
endif

ifneq (,$(filter at86rf2%,$(USEMODULE)))
USEMODULE += netdev2_ieee802154
endif

ifneq (,$(filter netdev2_ieee802154,$(USEMODULE)))
USEMODULE += ieee802154
endif
Expand Down
2 changes: 1 addition & 1 deletion boards/fox/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ifneq (,$(filter gnrc_netif_default,$(USEMODULE)))
USEMODULE += at86rf231
USEMODULE += gnrc_nomac
USEMODULE += gnrc_netdev2
endif

ifneq (,$(filter saul_default,$(USEMODULE)))
Expand Down
2 changes: 1 addition & 1 deletion boards/iotlab-m3/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ifneq (,$(filter gnrc_netif_default,$(USEMODULE)))
USEMODULE += at86rf231
USEMODULE += gnrc_nomac
USEMODULE += gnrc_netdev2
endif

ifneq (,$(filter saul_default,$(USEMODULE)))
Expand Down
2 changes: 1 addition & 1 deletion boards/mulle/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ifneq (,$(filter gnrc_netif_default,$(USEMODULE)))
USEMODULE += at86rf212b
USEMODULE += gnrc_nomac
USEMODULE += gnrc_netdev2
endif

ifneq (,$(filter saul_default,$(USEMODULE)))
Expand Down
2 changes: 1 addition & 1 deletion boards/samr21-xpro/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ifneq (,$(filter gnrc_netif_default,$(USEMODULE)))
USEMODULE += at86rf233
USEMODULE += gnrc_nomac
USEMODULE += gnrc_netdev2
endif

ifneq (,$(filter saul_default,$(USEMODULE)))
Expand Down
73 changes: 22 additions & 51 deletions drivers/at86rf2xx/at86rf2xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,13 @@
#include "debug.h"


static void _irq_handler(void *arg)
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)
{
msg_t msg;
at86rf2xx_t *dev = (at86rf2xx_t *) arg;

/* tell driver thread about the interrupt */
msg.type = GNRC_NETDEV_MSG_TYPE_EVENT;
msg_send(&msg, dev->mac_pid);
}

int at86rf2xx_init(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)
{
dev->driver = &at86rf2xx_driver;
netdev2_t *netdev = (netdev2_t *)dev;

netdev->driver = &at86rf2xx_driver;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't (shouldn't) this be done by the caller?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe not, but this casting back seems strange to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I guess

dev->netdev->netdev->driver = &at86rf2xx_driver;

isn't very pretty, either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other netdev2s are also the setting the driver in the setup and it makes more sense to me. Why expose the driver when it other stuff needs to be set for the device too.

It was your idea to use this kind of inheritance ;P

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and I warned about the implications)

/* initialize device descriptor */
dev->spi = spi;
dev->cs_pin = cs_pin;
Expand All @@ -59,32 +50,8 @@ int at86rf2xx_init(at86rf2xx_t *dev, spi_t spi, spi_speed_t spi_speed,
dev->reset_pin = reset_pin;
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);
/* 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);

/* make sure device is not sleeping, so we can query part number */
at86rf2xx_assert_awake(dev);

/* test if the SPI is set up correctly and the device is responding */
if (at86rf2xx_reg_read(dev, AT86RF2XX_REG__PART_NUM) !=
AT86RF2XX_PARTNUM) {
DEBUG("[at86rf2xx] error: unable to read correct part number\n");
return -1;
}

/* reset device to default values and put it into RX state */
at86rf2xx_reset(dev);

return 0;
}

void at86rf2xx_reset(at86rf2xx_t *dev)
Expand All @@ -100,8 +67,8 @@ void at86rf2xx_reset(at86rf2xx_t *dev)
at86rf2xx_reset_state_machine(dev);

/* reset options and sequence number */
dev->seq_nr = 0;
dev->options = 0;
dev->netdev.seq = 0;
dev->netdev.flags = 0;
/* set short and long address */
#if CPUID_LEN
cpuid_get(cpuid);
Expand Down Expand Up @@ -134,15 +101,16 @@ void at86rf2xx_reset(at86rf2xx_t *dev)
/* set default TX power */
at86rf2xx_set_txpower(dev, AT86RF2XX_DEFAULT_TXPOWER);
/* set default options */
at86rf2xx_set_option(dev, NETDEV2_IEEE802154_PAN_COMP, true);
at86rf2xx_set_option(dev, AT86RF2XX_OPT_AUTOACK, true);
at86rf2xx_set_option(dev, AT86RF2XX_OPT_CSMA, true);
at86rf2xx_set_option(dev, AT86RF2XX_OPT_TELL_RX_START, false);
at86rf2xx_set_option(dev, AT86RF2XX_OPT_TELL_RX_END, true);
/* set default protocol */
#ifdef MODULE_GNRC_SIXLOWPAN
dev->proto = GNRC_NETTYPE_SIXLOWPAN;
#else
dev->proto = GNRC_NETTYPE_UNDEF;
dev->netdev.proto = GNRC_NETTYPE_SIXLOWPAN;
#elif MODULE_GNRC
dev->netdev.proto = GNRC_NETTYPE_UNDEF;
#endif
/* enable safe mode (protect RX FIFO until reading data starts) */
at86rf2xx_reg_write(dev, AT86RF2XX_REG__TRX_CTRL_2,
Expand Down Expand Up @@ -219,39 +187,42 @@ void at86rf2xx_tx_prepare(at86rf2xx_t *dev)
/* make sure ongoing transmissions are finished */
do {
state = at86rf2xx_get_status(dev);
}
while (state == AT86RF2XX_STATE_BUSY_RX_AACK ||
state == AT86RF2XX_STATE_BUSY_TX_ARET);
} while (state == AT86RF2XX_STATE_BUSY_RX_AACK ||
state == AT86RF2XX_STATE_BUSY_TX_ARET);
if (state != AT86RF2XX_STATE_TX_ARET_ON) {
dev->idle_state = state;
}
at86rf2xx_set_state(dev, AT86RF2XX_STATE_TX_ARET_ON);
dev->frame_len = IEEE802154_FCS_LEN;
dev->tx_frame_len = IEEE802154_FCS_LEN;
}

size_t at86rf2xx_tx_load(at86rf2xx_t *dev, uint8_t *data,
size_t len, size_t offset)
{
dev->frame_len += (uint8_t)len;
dev->tx_frame_len += (uint8_t)len;
at86rf2xx_sram_write(dev, offset + 1, data, len);
return offset + len;
}

void at86rf2xx_tx_exec(at86rf2xx_t *dev)
{
netdev2_t *netdev = (netdev2_t *)dev;

/* write frame length field in FIFO */
at86rf2xx_sram_write(dev, 0, &(dev->frame_len), 1);
at86rf2xx_sram_write(dev, 0, &(dev->tx_frame_len), 1);
/* trigger sending of pre-loaded frame */
at86rf2xx_reg_write(dev, AT86RF2XX_REG__TRX_STATE,
AT86RF2XX_TRX_STATE__TX_START);
if (dev->event_cb && (dev->options & AT86RF2XX_OPT_TELL_TX_START)) {
dev->event_cb(NETDEV_EVENT_TX_STARTED, NULL);
if (netdev->event_callback &&
(dev->netdev.flags & AT86RF2XX_OPT_TELL_TX_START)) {
netdev->event_callback(netdev, NETDEV2_EVENT_TX_STARTED, NULL);
}
}

size_t at86rf2xx_rx_len(at86rf2xx_t *dev)
{
uint8_t phr;

at86rf2xx_fb_read(dev, &phr, 1);

/* ignore MSB (refer p.80) and substract length of FCS field */
Expand Down
49 changes: 25 additions & 24 deletions drivers/at86rf2xx/at86rf2xx_getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,57 +94,57 @@ static const uint8_t dbm_to_tx_pow[] = {0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e,

uint16_t at86rf2xx_get_addr_short(at86rf2xx_t *dev)
{
return (dev->addr_short[0] << 8) | dev->addr_short[1];
return (dev->netdev.short_addr[0] << 8) | dev->netdev.short_addr[1];
}

void at86rf2xx_set_addr_short(at86rf2xx_t *dev, uint16_t addr)
{
dev->addr_short[0] = addr >> 8;
dev->addr_short[1] = addr;
dev->netdev.short_addr[0] = (uint8_t)(addr);
dev->netdev.short_addr[1] = (uint8_t)(addr >> 8);
#ifdef MODULE_SIXLOWPAN
/* https://tools.ietf.org/html/rfc4944#section-12 requires the first bit to
* 0 for unicast addresses */
dev->addr_short[1] &= 0x7F;
dev->netdev.short_addr[0] &= 0x7F;
#endif
at86rf2xx_reg_write(dev, AT86RF2XX_REG__SHORT_ADDR_0,
dev->addr_short[0]);
dev->netdev.short_addr[1]);
at86rf2xx_reg_write(dev, AT86RF2XX_REG__SHORT_ADDR_1,
dev->addr_short[1]);
dev->netdev.short_addr[0]);
}

uint64_t at86rf2xx_get_addr_long(at86rf2xx_t *dev)
{
uint64_t addr;
uint8_t *ap = (uint8_t *)(&addr);
for (int i = 0; i < 8; i++) {
ap[i] = dev->addr_long[7 - i];
ap[i] = dev->netdev.long_addr[i];
}
return addr;
}

void at86rf2xx_set_addr_long(at86rf2xx_t *dev, uint64_t addr)
{
for (int i = 0; i < 8; i++) {
dev->addr_long[i] = (addr >> ((7 - i) * 8));
dev->netdev.long_addr[i] = (uint8_t)(addr >> (i * 8));
at86rf2xx_reg_write(dev, (AT86RF2XX_REG__IEEE_ADDR_0 + i),
dev->addr_long[i]);
(addr >> ((7 - i) * 8)));
}
}

uint8_t at86rf2xx_get_chan(at86rf2xx_t *dev)
{
return dev->chan;
return dev->netdev.chan;
}

void at86rf2xx_set_chan(at86rf2xx_t *dev, uint8_t channel)
{
if ((channel < AT86RF2XX_MIN_CHANNEL) ||
(channel > AT86RF2XX_MAX_CHANNEL) ||
(dev->chan == channel)) {
(dev->netdev.chan == channel)) {
return;
}

dev->chan = channel;
dev->netdev.chan = channel;

at86rf2xx_configure_phy(dev);
}
Expand Down Expand Up @@ -172,23 +172,24 @@ void at86rf2xx_set_page(at86rf2xx_t *dev, uint8_t page)

uint16_t at86rf2xx_get_pan(at86rf2xx_t *dev)
{
return dev->pan;
return dev->netdev.pan;
}

void at86rf2xx_set_pan(at86rf2xx_t *dev, uint16_t pan)
{
dev->pan = pan;
DEBUG("pan0: %u, pan1: %u\n", (uint8_t)pan, pan >> 8);
at86rf2xx_reg_write(dev, AT86RF2XX_REG__PAN_ID_0, (uint8_t)pan);
at86rf2xx_reg_write(dev, AT86RF2XX_REG__PAN_ID_1, (pan >> 8));
le_uint16_t le_pan = byteorder_btols(byteorder_htons(pan));
dev->netdev.pan = pan;
DEBUG("pan0: %u, pan1: %u\n", le_pan.u8[0], le_pan.u8[1]);
at86rf2xx_reg_write(dev, AT86RF2XX_REG__PAN_ID_0, le_pan.u8[0]);
at86rf2xx_reg_write(dev, AT86RF2XX_REG__PAN_ID_1, le_pan.u8[1]);
}

int16_t at86rf2xx_get_txpower(at86rf2xx_t *dev)
{
#ifdef MODULE_AT86RF212B
uint8_t txpower = at86rf2xx_reg_read(dev, AT86RF2XX_REG__PHY_TX_PWR);
DEBUG("txpower value: %x\n", txpower);
return _tx_pow_to_dbm_212b(dev->chan, dev->page, txpower);
return _tx_pow_to_dbm_212b(dev->netdev.chan, dev->page, txpower);
#else
uint8_t txpower = at86rf2xx_reg_read(dev, AT86RF2XX_REG__PHY_TX_PWR)
& AT86RF2XX_PHY_TX_PWR_MASK__TX_PWR;
Expand Down Expand Up @@ -220,11 +221,11 @@ void at86rf2xx_set_txpower(at86rf2xx_t *dev, int16_t txpower)
#endif
}
#ifdef MODULE_AT86RF212B
if (dev->chan == 0) {
if (dev->netdev.chan == 0) {
at86rf2xx_reg_write(dev, AT86RF2XX_REG__PHY_TX_PWR,
dbm_to_tx_pow_868[txpower]);
}
else if (dev->chan < 11) {
else if (dev->netdev.chan < 11) {
at86rf2xx_reg_write(dev, AT86RF2XX_REG__PHY_TX_PWR,
dbm_to_tx_pow_915[txpower]);
}
Expand Down Expand Up @@ -331,14 +332,14 @@ void at86rf2xx_set_option(at86rf2xx_t *dev, uint16_t option, bool state)

/* set option field */
if (state) {
dev->options |= option;
dev->netdev.flags |= option;
/* trigger option specific actions */
switch (option) {
case AT86RF2XX_OPT_CSMA:
DEBUG("[at86rf2xx] opt: enabling CSMA mode" \
"(4 retries, min BE: 3 max BE: 5)\n");
/* Initialize CSMA seed with hardware address */
at86rf2xx_set_csma_seed(dev, dev->addr_long);
at86rf2xx_set_csma_seed(dev, dev->netdev.long_addr);
at86rf2xx_set_csma_max_retries(dev, 4);
at86rf2xx_set_csma_backoff_exp(dev, 3, 5);
break;
Expand Down Expand Up @@ -371,7 +372,7 @@ void at86rf2xx_set_option(at86rf2xx_t *dev, uint16_t option, bool state)
}
}
else {
dev->options &= ~(option);
dev->netdev.flags &= ~(option);
/* trigger option specific actions */
switch (option) {
case AT86RF2XX_OPT_CSMA:
Expand All @@ -386,7 +387,7 @@ void at86rf2xx_set_option(at86rf2xx_t *dev, uint16_t option, bool state)
tmp &= ~(AT86RF2XX_XAH_CTRL_1__AACK_PROM_MODE);
at86rf2xx_reg_write(dev, AT86RF2XX_REG__XAH_CTRL_1, tmp);
/* re-enable AUTOACK only if the option is set */
if (dev->options & AT86RF2XX_OPT_AUTOACK) {
if (dev->netdev.flags & AT86RF2XX_OPT_AUTOACK) {
tmp = at86rf2xx_reg_read(dev,
AT86RF2XX_REG__CSMA_SEED_1);
tmp &= ~(AT86RF2XX_CSMA_SEED_1__AACK_DIS_ACK);
Expand Down
4 changes: 2 additions & 2 deletions drivers/at86rf2xx/at86rf2xx_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void at86rf2xx_configure_phy(at86rf2xx_t *dev)
/* Clear previous configuration for GC_TX_OFFS */
rf_ctrl0 &= ~AT86RF2XX_RF_CTRL_0_MASK__GC_TX_OFFS;

if (dev->chan != 0) {
if (dev->netdev.chan != 0) {
/* Set sub mode bit on 915 MHz as recommended by the data sheet */
trx_ctrl2 |= AT86RF2XX_TRX_CTRL_2_MASK__SUB_MODE;
}
Expand All @@ -199,7 +199,7 @@ void at86rf2xx_configure_phy(at86rf2xx_t *dev)
phy_cc_cca &= ~(AT86RF2XX_PHY_CC_CCA_MASK__CHANNEL);

/* Update the channel register */
phy_cc_cca |= (dev->chan & AT86RF2XX_PHY_CC_CCA_MASK__CHANNEL);
phy_cc_cca |= (dev->netdev.chan & AT86RF2XX_PHY_CC_CCA_MASK__CHANNEL);
at86rf2xx_reg_write(dev, AT86RF2XX_REG__PHY_CC_CCA, phy_cc_cca);

#ifdef MODULE_AT86RF212B
Expand Down
Loading