Skip to content

Commit 2cfb11e

Browse files
nandojvejhedberg
authored andcommitted
drivers: ieee802154: rf2xx: Rem trx_state variable
The rx timeout timer callback need update trx_state variable and this variable is protected by a mutex. Because of that, when compiling the system with CONFIG_ASSERT=y the system reports 'ASSERTION FAIL [!arch_is_in_isr()] @ ZEPHYR_BASE/kernel/include/ksched.h:262'. This refactor the driver remove trx_state variable dependency and consequently removes phy_mutex and rx timeout timer to be compliant with kernel rules. Fixes: #23198 Signed-off-by: Gerson Fernando Budke <[email protected]>
1 parent 4c6ee8b commit 2cfb11e

File tree

2 files changed

+21
-70
lines changed

2 files changed

+21
-70
lines changed

drivers/ieee802154/ieee802154_rf2xx.c

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,6 @@ static inline void trx_isr_handler(struct device *port,
7777
k_sem_give(&ctx->trx_isr_lock);
7878
}
7979

80-
static void trx_isr_timeout(struct k_timer *timer_id)
81-
{
82-
struct rf2xx_context *ctx = (struct rf2xx_context *)
83-
k_timer_user_data_get(timer_id);
84-
85-
k_mutex_lock(&ctx->phy_mutex, K_FOREVER);
86-
ctx->trx_state = RF2XX_TRX_PHY_STATE_IDLE;
87-
k_mutex_unlock(&ctx->phy_mutex);
88-
}
89-
9080
static void rf2xx_trx_set_state(struct device *dev,
9181
enum rf2xx_trx_state_cmd_t state)
9282
{
@@ -113,13 +103,18 @@ static void rf2xx_trx_set_tx_state(struct device *dev)
113103
* Datasheet: Chapter 7.2.3 RX_AACK_ON – Receive with Automatic ACK
114104
* Datasheet: Figure 7-13. Timing Example of an RX_AACK Transaction
115105
* for Slotted Operation.
106+
*
107+
* This will create a spin lock that wait transceiver be free from
108+
* current receive frame process
116109
*/
117110
do {
118111
status = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
119112
RF2XX_TRX_PHY_STATUS_MASK);
120113
} while (status == RF2XX_TRX_PHY_STATUS_BUSY_RX_AACK ||
121114
status == RF2XX_TRX_PHY_STATUS_STATE_TRANSITION);
122115

116+
rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
117+
rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
123118
rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TX_ARET_ON);
124119
}
125120

@@ -218,8 +213,6 @@ static void rf2xx_process_rx_frame(struct device *dev)
218213
{
219214
struct rf2xx_context *ctx = dev->driver_data;
220215

221-
k_timer_stop(&ctx->trx_isr_timeout);
222-
223216
if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
224217
rf2xx_trx_rx(dev);
225218
} else {
@@ -252,14 +245,14 @@ static void rf2xx_process_tx_frame(struct device *dev)
252245

253246
static void rf2xx_process_trx_end(struct device *dev)
254247
{
255-
struct rf2xx_context *ctx = dev->driver_data;
248+
u8_t trx_status = (rf2xx_iface_reg_read(dev, RF2XX_TRX_STATUS_REG) &
249+
RF2XX_TRX_PHY_STATUS_MASK);
256250

257-
if (ctx->trx_state == RF2XX_TRX_PHY_BUSY_RX) {
258-
rf2xx_process_rx_frame(dev);
259-
} else {
251+
if (trx_status == RF2XX_TRX_PHY_STATUS_TX_ARET_ON) {
260252
rf2xx_process_tx_frame(dev);
253+
} else {
254+
rf2xx_process_rx_frame(dev);
261255
}
262-
ctx->trx_state = RF2XX_TRX_PHY_STATE_IDLE;
263256
}
264257

265258
static void rf2xx_thread_main(void *arg)
@@ -270,9 +263,9 @@ static void rf2xx_thread_main(void *arg)
270263

271264
while (true) {
272265
k_sem_take(&ctx->trx_isr_lock, K_FOREVER);
273-
k_mutex_lock(&ctx->phy_mutex, K_FOREVER);
274266

275267
isr_status = rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
268+
276269
/*
277270
* IRQ_7 (BAT_LOW) Indicates a supply voltage below the
278271
* programmed threshold. 9.5.4
@@ -300,16 +293,12 @@ static void rf2xx_thread_main(void *arg)
300293
* IRQ_0 (PLL_LOCK) Indicates PLL lock.
301294
*/
302295
if (isr_status & (1 << RF2XX_RX_START)) {
303-
ctx->trx_state = RF2XX_TRX_PHY_BUSY_RX;
304-
k_timer_start(&ctx->trx_isr_timeout, K_MSEC(10), 0);
305-
306296
if (ctx->trx_model != RF2XX_TRX_MODEL_231) {
307297
rf2xx_iface_sram_read(dev, 0, &ctx->rx_phr, 1);
308298
}
309299
} else if (isr_status & (1 << RF2XX_TRX_END)) {
310300
rf2xx_process_trx_end(dev);
311301
}
312-
k_mutex_unlock(&ctx->phy_mutex);
313302
}
314303
}
315304

@@ -483,33 +472,14 @@ static int rf2xx_tx(struct device *dev,
483472
ARG_UNUSED(pkt);
484473

485474
struct rf2xx_context *ctx = dev->driver_data;
486-
bool abort = true;
487475
int response = 0;
488476

489-
k_mutex_lock(&ctx->phy_mutex, K_FOREVER);
490-
/* Reset semaphore in case ACK was received after timeout */
491-
k_sem_reset(&ctx->trx_tx_sync);
492-
493-
if (ctx->trx_state == RF2XX_TRX_PHY_STATE_IDLE) {
494-
ctx->trx_state = RF2XX_TRX_PHY_BUSY_TX;
495-
abort = false;
496-
497-
rf2xx_trx_set_tx_state(dev);
498-
rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
499-
rf2xx_iface_frame_write(dev, frag->data, frag->len);
500-
rf2xx_iface_phy_tx_start(dev);
501-
}
502-
503-
k_mutex_unlock(&ctx->phy_mutex);
504-
505-
if (abort) {
506-
LOG_DBG("TX Abort, TRX isn't idle!");
507-
return -EBUSY;
508-
}
477+
rf2xx_trx_set_tx_state(dev);
478+
rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
509479

510-
/**
511-
* Wait transceiver...
512-
*/
480+
k_sem_reset(&ctx->trx_tx_sync);
481+
rf2xx_iface_frame_write(dev, frag->data, frag->len);
482+
rf2xx_iface_phy_tx_start(dev);
513483
k_sem_take(&ctx->trx_tx_sync, K_FOREVER);
514484

515485
switch (ctx->trx_trac) {
@@ -551,11 +521,11 @@ static int rf2xx_start(struct device *dev)
551521
const struct rf2xx_config *conf = dev->config->config_info;
552522
struct rf2xx_context *ctx = dev->driver_data;
553523

554-
k_mutex_lock(&ctx->phy_mutex, K_FOREVER);
524+
rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
525+
rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
555526
gpio_pin_interrupt_configure(ctx->irq_gpio, conf->irq.pin,
556527
GPIO_INT_EDGE_TO_ACTIVE);
557528
rf2xx_trx_set_rx_state(dev);
558-
k_mutex_unlock(&ctx->phy_mutex);
559529

560530
return 0;
561531
}
@@ -565,11 +535,10 @@ static int rf2xx_stop(struct device *dev)
565535
const struct rf2xx_config *conf = dev->config->config_info;
566536
struct rf2xx_context *ctx = dev->driver_data;
567537

568-
k_mutex_lock(&ctx->phy_mutex, K_FOREVER);
569538
gpio_pin_interrupt_configure(ctx->irq_gpio, conf->irq.pin,
570539
GPIO_INT_DISABLE);
571540
rf2xx_trx_set_state(dev, RF2XX_TRX_PHY_STATE_CMD_TRX_OFF);
572-
k_mutex_unlock(&ctx->phy_mutex);
541+
rf2xx_iface_reg_read(dev, RF2XX_IRQ_STATUS_REG);
573542

574543
return 0;
575544
}
@@ -590,8 +559,6 @@ static int power_on_and_setup(struct device *dev)
590559
struct rf2xx_context *ctx = dev->driver_data;
591560
u8_t config;
592561

593-
ctx->trx_state = RF2XX_TRX_PHY_STATE_IDLE;
594-
595562
rf2xx_iface_phy_rst(dev);
596563

597564
/* Sync transceiver state */
@@ -766,11 +733,8 @@ static int rf2xx_init(struct device *dev)
766733

767734
LOG_DBG("\nInitialize RF2XX Transceiver\n");
768735

769-
k_mutex_init(&ctx->phy_mutex);
770736
k_sem_init(&ctx->trx_tx_sync, 0, 1);
771737
k_sem_init(&ctx->trx_isr_lock, 0, 1);
772-
k_timer_init(&ctx->trx_isr_timeout, trx_isr_timeout, NULL);
773-
k_timer_user_data_set(&ctx->trx_isr_timeout, ctx);
774738

775739
if (configure_gpios(dev) != 0) {
776740
LOG_ERR("Configuring GPIOS failed");
@@ -905,12 +869,10 @@ static struct ieee802154_radio_api rf2xx_radio_api = {
905869
.spi.cs.devname = DT_INST_##n##_ATMEL_RF2XX_CS_GPIOS_CONTROLLER, \
906870
.spi.cs.pin = DT_INST_##n##_ATMEL_RF2XX_CS_GPIOS_PIN, \
907871
.spi.cs.flags = DT_INST_##n##_ATMEL_RF2XX_CS_GPIOS_FLAGS, \
908-
}
872+
};
909873

910874
#define IEEE802154_RF2XX_DEVICE_DATA(n) \
911-
static struct rf2xx_context rf2xx_ctx_data_##n = { \
912-
.trx_state = RF2XX_TRX_PHY_STATE_INITIAL, \
913-
}
875+
static struct rf2xx_context rf2xx_ctx_data_##n;
914876

915877
#define IEEE802154_RF2XX_RAW_DEVICE_INIT(n) \
916878
DEVICE_AND_API_INIT( \

drivers/ieee802154/ieee802154_rf2xx.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ enum rf2xx_trx_state_trac_t {
6565
RF2XX_TRX_PHY_STATE_TRAC_INVALID = 0x07,
6666
};
6767

68-
enum rf2xx_trx_state_t {
69-
RF2XX_TRX_PHY_STATE_INITIAL,
70-
RF2XX_TRX_PHY_STATE_IDLE,
71-
RF2XX_TRX_PHY_STATE_SLEEP,
72-
RF2XX_TRX_PHY_BUSY_RX,
73-
RF2XX_TRX_PHY_BUSY_TX,
74-
};
75-
7668
enum rf2xx_trx_model_t {
7769
RF2XX_TRX_MODEL_INV = 0x00,
7870
RF2XX_TRX_MODEL_230 = 0x02,
@@ -127,11 +119,8 @@ struct rf2xx_context {
127119
CONFIG_IEEE802154_RF2XX_RX_STACK_SIZE);
128120
struct k_sem trx_isr_lock;
129121
struct k_sem trx_tx_sync;
130-
struct k_timer trx_isr_timeout;
131-
struct k_mutex phy_mutex;
132122

133123
enum rf2xx_trx_model_t trx_model;
134-
enum rf2xx_trx_state_t trx_state;
135124
enum rf2xx_trx_state_trac_t trx_trac;
136125

137126
u8_t mac_addr[8];

0 commit comments

Comments
 (0)