From 87cf8e4729f7de734ede85219a0ef04f9c660e61 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Tue, 24 Jan 2017 00:46:38 +0100 Subject: [PATCH] drivers/mrf24j40: adapted to SPI API changes --- drivers/include/mrf24j40.h | 38 ++++----- drivers/mrf24j40/include/mrf24j40_params.h | 8 +- drivers/mrf24j40/mrf24j40.c | 3 - drivers/mrf24j40/mrf24j40_internal.c | 93 ++++++++++------------ drivers/mrf24j40/mrf24j40_netdev.c | 3 +- 5 files changed, 67 insertions(+), 78 deletions(-) diff --git a/drivers/include/mrf24j40.h b/drivers/include/mrf24j40.h index 98995a32c680f..6a3281a301cd2 100644 --- a/drivers/include/mrf24j40.h +++ b/drivers/include/mrf24j40.h @@ -84,9 +84,9 @@ extern "C" { * @brief Flags for PSEUDO DEVICE INTERNAL STATES * @{ */ -#define MRF24J40_PSEUDO_STATE_IDLE (0x01) /**< Idle, ready to transmit or receive */ -#define MRF24J40_PSEUDO_STATE_SLEEP (0x02) /**< sleep mode, registers functional, but no RF */ -#define MRF24J40_PSEUDO_STATE_RESET (0x04) /**< Reset device, next state is idle */ +#define MRF24J40_PSEUDO_STATE_IDLE (0x01) /**< Idle, ready to transmit or receive */ +#define MRF24J40_PSEUDO_STATE_SLEEP (0x02) /**< sleep mode, registers functional, but no RF */ +#define MRF24J40_PSEUDO_STATE_RESET (0x04) /**< Reset device, next state is idle */ /** @} */ /** @@ -97,20 +97,20 @@ extern "C" { * * @{ */ -#define MRF24J40_OPT_CSMA (0x0100) /**< CSMA active */ -#define MRF24J40_OPT_PROMISCUOUS (0x0200) /**< promiscuous mode - * active */ -#define MRF24J40_OPT_PRELOADING (0x0400) /**< preloading enabled */ -#define MRF24J40_OPT_TELL_TX_START (0x0800) /**< notify MAC layer on TX - * start */ -#define MRF24J40_OPT_TELL_TX_END (0x1000) /**< notify MAC layer on TX - * finished */ -#define MRF24J40_OPT_TELL_RX_START (0x2000) /**< notify MAC layer on RX - * start */ -#define MRF24J40_OPT_TELL_RX_END (0x4000) /**< notify MAC layer on RX - * finished */ -#define MRF24J40_OPT_REQ_AUTO_ACK (0x8000) /**< notify MAC layer on RX - * finished */ +#define MRF24J40_OPT_CSMA (0x0100) /**< CSMA active */ +#define MRF24J40_OPT_PROMISCUOUS (0x0200) /**< promiscuous mode + * active */ +#define MRF24J40_OPT_PRELOADING (0x0400) /**< preloading enabled */ +#define MRF24J40_OPT_TELL_TX_START (0x0800) /**< notify MAC layer on TX + * start */ +#define MRF24J40_OPT_TELL_TX_END (0x1000) /**< notify MAC layer on TX + * finished */ +#define MRF24J40_OPT_TELL_RX_START (0x2000) /**< notify MAC layer on RX + * start */ +#define MRF24J40_OPT_TELL_RX_END (0x4000) /**< notify MAC layer on RX + * finished */ +#define MRF24J40_OPT_REQ_AUTO_ACK (0x8000) /**< notify MAC layer on RX + * finished */ /** @} */ @@ -125,8 +125,8 @@ extern "C" { */ typedef struct mrf24j40_params { spi_t spi; /**< SPI bus the device is connected to */ - spi_speed_t spi_speed; /**< SPI speed to use */ - gpio_t cs_pin; /**< GPIO pin connected to chip select */ + spi_clk_t spi_clk; /**< SPI speed to use */ + spi_cs_t cs_pin; /**< GPIO pin connected to chip select */ gpio_t int_pin; /**< GPIO pin connected to the interrupt pin */ gpio_t reset_pin; /**< GPIO pin connected to the reset pin */ } mrf24j40_params_t; diff --git a/drivers/mrf24j40/include/mrf24j40_params.h b/drivers/mrf24j40/include/mrf24j40_params.h index 2c5bce8eae9a2..01a715d9f5e4e 100644 --- a/drivers/mrf24j40/include/mrf24j40_params.h +++ b/drivers/mrf24j40/include/mrf24j40_params.h @@ -32,10 +32,10 @@ extern "C" { * @{ */ #ifndef MRF24J40_PARAM_SPI -#define MRF24J40_PARAM_SPI (SPI_0) +#define MRF24J40_PARAM_SPI (SPI_DEV(0)) #endif -#ifndef MRF24J40_PARAM_SPI_SPEED -#define MRF24J40_PARAM_SPI_SPEED (SPI_SPEED_5MHZ) +#ifndef MRF24J40_PARAM_SPI_CLK +#define MRF24J40_PARAM_SPI_CLK (SPI_CLK_5MHZ) #endif #ifndef MRF24J40_PARAM_CS #define MRF24J40_PARAM_CS (GPIO_PIN(0, 0)) @@ -48,7 +48,7 @@ extern "C" { #endif #define MRF24J40_PARAMS_DEFAULT { .spi = MRF24J40_PARAM_SPI, \ - .spi_speed = MRF24J40_PARAM_SPI_SPEED, \ + .spi_clk = MRF24J40_PARAM_SPI_CLK, \ .cs_pin = MRF24J40_PARAM_CS, \ .int_pin = MRF24J40_PARAM_INT, \ .reset_pin = MRF24J40_PARAM_RESET } diff --git a/drivers/mrf24j40/mrf24j40.c b/drivers/mrf24j40/mrf24j40.c index 67481164e2d15..8a26a02ab9794 100644 --- a/drivers/mrf24j40/mrf24j40.c +++ b/drivers/mrf24j40/mrf24j40.c @@ -38,9 +38,6 @@ void mrf24j40_setup(mrf24j40_t *dev, const mrf24j40_params_t *params) netdev->driver = &mrf24j40_driver; /* initialize device descriptor */ memcpy(&dev->params, params, sizeof(mrf24j40_params_t)); - /* initialise SPI */ - spi_init_master(dev->params.spi, SPI_CONF_FIRST_RISING, params->spi_speed); - } void mrf24j40_reset(mrf24j40_t *dev) diff --git a/drivers/mrf24j40/mrf24j40_internal.c b/drivers/mrf24j40/mrf24j40_internal.c index f4cd6c4733a1d..91f4f9b330f90 100644 --- a/drivers/mrf24j40/mrf24j40_internal.c +++ b/drivers/mrf24j40/mrf24j40_internal.c @@ -28,6 +28,14 @@ #define ENABLE_DEBUG (0) #include "debug.h" +#define SPIDEV (dev->params.spi) +#define CSPIN (dev->params.cs_pin) + +static inline void getbus(mrf24j40_t *dev) +{ + spi_acquire(SPIDEV, CSPIN, SPI_MODE_0, dev->params.spi_clk); +} + void mrf24j40_init(mrf24j40_t *dev) { uint8_t softrst; @@ -89,26 +97,21 @@ uint8_t mrf24j40_reg_read_short(mrf24j40_t *dev, const uint8_t addr) { char value; - spi_acquire(dev->params.spi); - gpio_clear(dev->params.cs_pin); - spi_transfer_reg(dev->params.spi, MRF24J40_SHORT_ADDR_TRANS | - (addr << MRF24J40_ADDR_OFFSET) | - MRF24J40_ACCESS_READ, 0, &value); - gpio_set(dev->params.cs_pin); - spi_release(dev->params.spi); - + getbus(dev); + value = spi_transfer_reg(SPIDEV, CSPIN, MRF24J40_SHORT_ADDR_TRANS | + (addr << MRF24J40_ADDR_OFFSET) | + MRF24J40_ACCESS_READ, 0); + spi_release(SPIDEV); return (uint8_t)value; } void mrf24j40_reg_write_short(mrf24j40_t *dev, const uint8_t addr, const uint8_t value) { - spi_acquire(dev->params.spi); - gpio_clear(dev->params.cs_pin); - spi_transfer_reg(dev->params.spi, MRF24J40_SHORT_ADDR_TRANS | - (addr << MRF24J40_ADDR_OFFSET) | - MRF24J40_ACCESS_WRITE, value, 0); - gpio_set(dev->params.cs_pin); - spi_release(dev->params.spi); + getbus(dev); + spi_transfer_reg(SPIDEV, CSPIN , MRF24J40_SHORT_ADDR_TRANS | + (addr << MRF24J40_ADDR_OFFSET) | + MRF24J40_ACCESS_WRITE, value); + spi_release(SPIDEV); } uint8_t mrf24j40_reg_read_long(mrf24j40_t *dev, const uint16_t addr) @@ -118,13 +121,11 @@ uint8_t mrf24j40_reg_read_long(mrf24j40_t *dev, const uint16_t addr) reg1 = MRF24J40_LONG_ADDR_TRANS | (addr >> 3); reg2 = (addr << 5) | MRF24J40_ACCESS_READ; char value; - spi_acquire(dev->params.spi); - gpio_clear(dev->params.cs_pin); - spi_transfer_byte(dev->params.spi, reg1, 0); - spi_transfer_byte(dev->params.spi, reg2, &value); - spi_transfer_byte(dev->params.spi, 0, &value); - gpio_set(dev->params.cs_pin); - spi_release(dev->params.spi); + getbus(dev); + spi_transfer_byte(SPIDEV, CSPIN, true, reg1); + spi_transfer_byte(SPIDEV, CSPIN, true, reg2); + value = spi_transfer_byte(SPIDEV, CSPIN, false, 0); + spi_release(SPIDEV); return (uint8_t)value; } @@ -135,13 +136,11 @@ void mrf24j40_reg_write_long(mrf24j40_t *dev, const uint16_t addr, const uint8_t reg1 = MRF24J40_LONG_ADDR_TRANS | (addr >> 3); reg2 = (addr << 5) | MRF24J40_ACCESS_WRITE_LNG; - spi_acquire(dev->params.spi); - gpio_clear(dev->params.cs_pin); - spi_transfer_byte(dev->params.spi, reg1, 0); - spi_transfer_byte(dev->params.spi, reg2, 0); - spi_transfer_byte(dev->params.spi, value, 0); - gpio_set(dev->params.cs_pin); - spi_release(dev->params.spi); + getbus(dev); + spi_transfer_byte(SPIDEV, CSPIN, true, reg1); + spi_transfer_byte(SPIDEV, CSPIN, true, reg2); + spi_transfer_byte(SPIDEV, CSPIN, false, value); + spi_release(SPIDEV); } void mrf24j40_tx_normal_fifo_read(mrf24j40_t *dev, const uint16_t offset, uint8_t *data, const size_t len) @@ -150,13 +149,11 @@ void mrf24j40_tx_normal_fifo_read(mrf24j40_t *dev, const uint16_t offset, uint8_ reg1 = MRF24J40_LONG_ADDR_TRANS | (offset >> 3); reg2 = (offset << 5) | MRF24J40_ACCESS_READ; - spi_acquire(dev->params.spi); - gpio_clear(dev->params.cs_pin); - spi_transfer_byte(dev->params.spi, reg1, NULL); - spi_transfer_byte(dev->params.spi, reg2, (char *)data); - spi_transfer_bytes(dev->params.spi, NULL, (char *)data, len); - gpio_set(dev->params.cs_pin); - spi_release(dev->params.spi); + getbus(dev); + spi_transfer_byte(SPIDEV, CSPIN, true, reg1); + spi_transfer_byte(SPIDEV, CSPIN, true, reg2); + spi_transfer_bytes(SPIDEV, CSPIN, false, NULL, (char *)data, len); + spi_release(SPIDEV); } void mrf24j40_tx_normal_fifo_write(mrf24j40_t *dev, @@ -173,13 +170,11 @@ void mrf24j40_tx_normal_fifo_write(mrf24j40_t *dev, reg1 = MRF24J40_LONG_ADDR_TRANS | (addr >> 3); reg2 = (addr << 5) | MRF24J40_ACCESS_WRITE_LNG; - spi_acquire(dev->params.spi); - gpio_clear(dev->params.cs_pin); - spi_transfer_byte(dev->params.spi, reg1, 0); - spi_transfer_byte(dev->params.spi, reg2, 0); - spi_transfer_bytes(dev->params.spi, (char *)data, NULL, len); - gpio_set(dev->params.cs_pin); - spi_release(dev->params.spi); + getbus(dev); + spi_transfer_byte(SPIDEV, CSPIN, true, reg1); + spi_transfer_byte(SPIDEV, CSPIN, true, reg2); + spi_transfer_bytes(SPIDEV, CSPIN, false, (char *)data, NULL, len); + spi_release(SPIDEV); } void mrf24j40_rx_fifo_read(mrf24j40_t *dev, const uint16_t offset, uint8_t *data, const size_t len) @@ -191,13 +186,11 @@ void mrf24j40_rx_fifo_read(mrf24j40_t *dev, const uint16_t offset, uint8_t *data uint8_t reg1, reg2; reg1 = MRF24J40_LONG_ADDR_TRANS | (rx_addr >> 3); reg2 = (rx_addr << 5) | MRF24J40_ACCESS_READ; - spi_acquire(dev->params.spi); - gpio_clear(dev->params.cs_pin); - spi_transfer_byte(dev->params.spi, reg1, NULL); - spi_transfer_byte(dev->params.spi, reg2, (char *)data); - spi_transfer_bytes(dev->params.spi, NULL, (char *)data, len); - gpio_set(dev->params.cs_pin); - spi_release(dev->params.spi); + getbus(dev); + spi_transfer_byte(SPIDEV, CSPIN, true, reg1); + spi_transfer_byte(SPIDEV, CSPIN, true, reg2); + spi_transfer_bytes(SPIDEV, CSPIN, false, NULL, (char *)data, len); + spi_release(SPIDEV); } void mrf24j40_rx_fifo_write(mrf24j40_t *dev, const uint16_t offset, const uint8_t *data, const size_t len) diff --git a/drivers/mrf24j40/mrf24j40_netdev.c b/drivers/mrf24j40/mrf24j40_netdev.c index ed5e15416daea..7573950cc629e 100644 --- a/drivers/mrf24j40/mrf24j40_netdev.c +++ b/drivers/mrf24j40/mrf24j40_netdev.c @@ -70,8 +70,7 @@ static int _init(netdev2_t *netdev) mrf24j40_t *dev = (mrf24j40_t *)netdev; /* initialise GPIOs */ - gpio_init(dev->params.cs_pin, GPIO_OUT); - gpio_set(dev->params.cs_pin); + spi_init_cs(dev->params.spi, dev->params.cs_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);