Skip to content

Commit 0c34a13

Browse files
author
Arto Kinnunen
committed
Nanostack 802.15.4 RF drivers update
Sync Nanostack RF drivers from master copies: -Atmel: v3.0.7 -MCR20A: v1.0.3 -S2LP: v0.0.4
1 parent 6b84b14 commit 0c34a13

File tree

5 files changed

+74
-68
lines changed

5 files changed

+74
-68
lines changed

components/802.15.4_RF/atmel-rf-driver/source/NanostackRfPhyAtmel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
#include <string.h>
1717

18-
#if defined(MBED_CONF_NANOSTACK_CONFIGURATION) && DEVICE_SPI && DEVICE_I2C && defined(MBED_CONF_RTOS_PRESENT)
18+
#if defined(MBED_CONF_NANOSTACK_CONFIGURATION) && DEVICE_SPI && DEVICE_INTERRUPTIN && DEVICE_I2C && defined(MBED_CONF_RTOS_PRESENT)
1919

2020
#include "platform/arm_hal_interrupt.h"
2121
#include "nanostack/platform/arm_hal_phy.h"

components/802.15.4_RF/mcr20a-rf-driver/source/NanostackRfPhyMcr20a.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
#include "NanostackRfPhyMcr20a.h"
1717

18-
#if defined(MBED_CONF_NANOSTACK_CONFIGURATION) && DEVICE_SPI && defined(MBED_CONF_RTOS_PRESENT)
18+
#if defined(MBED_CONF_NANOSTACK_CONFIGURATION) && DEVICE_SPI && DEVICE_INTERRUPTIN && defined(MBED_CONF_RTOS_PRESENT)
1919

2020
#include "ns_types.h"
2121
#include "platform/arm_hal_interrupt.h"

components/802.15.4_RF/stm-s2lp-rf-driver/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@
33
Support for:
44
* S2-LP
55

6-
This driver is used with 6LoWPAN and Wi-SUN stacks.
6+
This driver is used with 6LoWPAN and Wi-SUN stacks.
7+
8+
Driver is tested with X-Nucleo-S2868A1 RF expansion board. https://www.st.com/en/ecosystems/x-nucleo-s2868a1.html
9+
10+
NOTE: Default SPI SCLK pin configuration is D13. On X-Nucleo-S2868A1, resistor R6 must be soldered instead of R11. For more information, see user manual UM2405. https://www.st.com/resource/en/user_manual/dm00498153.pdf
11+

components/802.15.4_RF/stm-s2lp-rf-driver/source/NanostackRfPhys2lp.cpp

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
#include <string.h>
17-
#if defined(MBED_CONF_NANOSTACK_CONFIGURATION) && DEVICE_SPI && defined(MBED_CONF_RTOS_PRESENT)
17+
#if defined(MBED_CONF_NANOSTACK_CONFIGURATION) && DEVICE_SPI && DEVICE_INTERRUPTIN && defined(MBED_CONF_RTOS_PRESENT)
1818
#include "platform/arm_hal_interrupt.h"
1919
#include "nanostack/platform/arm_hal_phy.h"
2020
#include "ns_types.h"
@@ -140,11 +140,9 @@ class RFPins {
140140
Timeout cca_timer;
141141
Timeout backup_timer;
142142
Timer tx_timer;
143-
#ifdef MBED_CONF_RTOS_PRESENT
144143
Thread irq_thread;
145144
Mutex mutex;
146145
void rf_irq_task();
147-
#endif //MBED_CONF_RTOS_PRESENT
148146
};
149147

150148
RFPins::RFPins(PinName spi_sdi, PinName spi_sdo,
@@ -167,14 +165,10 @@ RFPins::RFPins(PinName spi_sdi, PinName spi_sdo,
167165
RF_S2LP_GPIO0(spi_gpio0),
168166
RF_S2LP_GPIO1(spi_gpio1),
169167
RF_S2LP_GPIO2(spi_gpio2),
170-
RF_S2LP_GPIO3(spi_gpio3)
171-
#ifdef MBED_CONF_RTOS_PRESENT
172-
, irq_thread(osPriorityRealtime, 1024)
173-
#endif //MBED_CONF_RTOS_PRESENT
168+
RF_S2LP_GPIO3(spi_gpio3),
169+
irq_thread(osPriorityRealtime, 1024)
174170
{
175-
#ifdef MBED_CONF_RTOS_PRESENT
176171
irq_thread.start(mbed::callback(this, &RFPins::rf_irq_task));
177-
#endif //MBED_CONF_RTOS_PRESENT
178172
}
179173

180174
static uint8_t rf_read_register(uint8_t addr);
@@ -215,6 +209,7 @@ static uint8_t s2lp_short_address[2];
215209
static uint8_t s2lp_MAC[8];
216210
static rf_mode_e rf_mode = RF_MODE_NORMAL;
217211
static bool rf_update_config = false;
212+
static uint16_t cur_packet_len = 0xffff;
218213

219214
/* Channel configurations for sub-GHz */
220215
static phy_rf_channel_configuration_s phy_subghz = {
@@ -232,7 +227,6 @@ static const phy_device_channel_page_s phy_channel_pages[] = {
232227
{ CHANNEL_PAGE_0, NULL}
233228
};
234229

235-
#ifdef MBED_CONF_RTOS_PRESENT
236230

237231
#include "rtos.h"
238232

@@ -243,7 +237,6 @@ static void rf_irq_task_process_irq();
243237
#define SIG_TIMER_CCA 2
244238
#define SIG_TIMER_BACKUP 4
245239

246-
#endif //MBED_CONF_RTOS_PRESENT
247240

248241
#define ACK_FRAME_LENGTH 3
249242
// Give some additional time for processing, PHY headers, CRC etc.
@@ -292,7 +285,6 @@ static void rf_unlock(void)
292285
platform_exit_critical();
293286
}
294287

295-
#ifdef MBED_CONF_RTOS_PRESENT
296288
static void rf_cca_timer_signal(void)
297289
{
298290
rf->irq_thread.flags_set(SIG_TIMER_CCA);
@@ -302,7 +294,6 @@ static void rf_backup_timer_signal(void)
302294
{
303295
rf->irq_thread.flags_set(SIG_TIMER_BACKUP);
304296
}
305-
#endif //MBED_CONF_RTOS_PRESENT
306297

307298
/*
308299
* \brief Function writes/read data in SPI interface
@@ -365,22 +356,36 @@ static void rf_enable_gpio_signal(uint8_t gpio_pin, uint8_t interrupt_signal, ui
365356

366357
static void rf_enable_interrupt(uint8_t event)
367358
{
359+
if (enabled_interrupts & (1 << event)) {
360+
return;
361+
}
368362
rf_write_register_field(IRQ_MASK0 - (event / 8), 1 << (event % 8), 1 << (event % 8));
369363
enabled_interrupts |= (1 << event);
370364
}
371365

372366
static void rf_disable_interrupt(uint8_t event)
373367
{
368+
if (!(enabled_interrupts & (1 << event))) {
369+
return;
370+
}
374371
rf_write_register_field(IRQ_MASK0 - (event / 8), 1 << (event % 8), 0 << (event % 8));
375372
enabled_interrupts &= ~(1 << event);
376373
}
377374

378375
static void rf_disable_all_interrupts(void)
379376
{
380-
rf_write_register(IRQ_MASK0, 0);
381-
rf_write_register(IRQ_MASK1, 0);
382-
rf_write_register(IRQ_MASK2, 0);
383-
rf_write_register(IRQ_MASK3, 0);
377+
if ((uint8_t)enabled_interrupts & 0xff) {
378+
rf_write_register(IRQ_MASK0, 0);
379+
}
380+
if ((uint8_t)(enabled_interrupts >> 8) & 0xff) {
381+
rf_write_register(IRQ_MASK1, 0);
382+
}
383+
if ((uint8_t)(enabled_interrupts >> 16) & 0xff) {
384+
rf_write_register(IRQ_MASK2, 0);
385+
}
386+
if ((uint8_t)(enabled_interrupts >> 24) & 0xff) {
387+
rf_write_register(IRQ_MASK3, 0);
388+
}
384389
enabled_interrupts = 0;
385390
read_irq_status();
386391
}
@@ -459,10 +464,29 @@ static int rf_read_rx_fifo(uint16_t rx_index, uint16_t length)
459464
return length;
460465
}
461466

467+
static void rf_flush_tx_fifo(void)
468+
{
469+
if (rf_read_register(TX_FIFO_STATUS)) {
470+
rf_send_command(S2LP_CMD_FLUSHTXFIFO);
471+
}
472+
}
473+
474+
static void rf_flush_rx_fifo(void)
475+
{
476+
if (rf_read_register(RX_FIFO_STATUS)) {
477+
rf_send_command(S2LP_CMD_FLUSHRXFIFO);
478+
}
479+
}
480+
462481
static void rf_write_packet_length(uint16_t packet_length)
463482
{
464-
rf_write_register(PCKTLEN1, packet_length / 256);
465-
rf_write_register(PCKTLEN0, packet_length % 256);
483+
if ((uint8_t)(cur_packet_len >> 8) != (packet_length / 256)) {
484+
rf_write_register(PCKTLEN1, packet_length / 256);
485+
}
486+
if ((uint8_t)cur_packet_len != (packet_length % 256)) {
487+
rf_write_register(PCKTLEN0, packet_length % 256);
488+
}
489+
cur_packet_len = packet_length;
466490
}
467491

468492
static uint32_t read_irq_status(void)
@@ -514,12 +538,6 @@ static void rf_set_channel_configuration_registers(void)
514538
rf_channel_multiplier++;
515539
}
516540
rf_write_register(CHSPACE, ch_space);
517-
tr_info("RF config update:");
518-
tr_info("Frequency(ch0): %luHz", phy_subghz.channel_0_center_frequency);
519-
tr_info("Channel spacing: %luHz", phy_subghz.channel_spacing);
520-
tr_info("Datarate: %lubps", phy_subghz.datarate);
521-
tr_info("Deviation: %luHz", deviation);
522-
tr_info("RX BW M: %u, E: %u", chflt_m, chflt_e);
523541
}
524542

525543
static void rf_init_registers(void)
@@ -733,19 +751,17 @@ static void rf_start_tx(void)
733751
static void rf_cca_timer_interrupt(void)
734752
{
735753
if (device_driver.phy_tx_done_cb(rf_radio_driver_id, mac_tx_handle, PHY_LINK_CCA_PREPARE, 0, 0) != 0) {
736-
if (rf_read_register(TX_FIFO_STATUS)) {
737-
rf_send_command(S2LP_CMD_FLUSHTXFIFO);
754+
rf_flush_tx_fifo();
755+
if (rf_state == RF_CSMA_STARTED) {
756+
rf_state = RF_IDLE;
738757
}
739-
rf_state = RF_IDLE;
740758
return;
741759
}
742760
if ((cca_enabled == true) && ((rf_state != RF_CSMA_STARTED && rf_state != RF_IDLE) || (read_irq_status() & (1 << SYNC_WORD)) || (rf_read_register(LINK_QUALIF1) & CARRIER_SENSE))) {
743761
if (rf_state == RF_CSMA_STARTED) {
744762
rf_state = RF_IDLE;
745763
}
746-
if (rf_read_register(TX_FIFO_STATUS)) {
747-
rf_send_command(S2LP_CMD_FLUSHTXFIFO);
748-
}
764+
rf_flush_tx_fifo();
749765
tx_finnish_time = rf_get_timestamp();
750766
if (device_driver.phy_tx_done_cb) {
751767
device_driver.phy_tx_done_cb(rf_radio_driver_id, mac_tx_handle, PHY_LINK_CCA_FAIL, 0, 0);
@@ -764,11 +780,7 @@ static void rf_cca_timer_stop(void)
764780

765781
static void rf_cca_timer_start(uint32_t slots)
766782
{
767-
#ifdef MBED_CONF_RTOS_PRESENT
768783
rf->cca_timer.attach_us(rf_cca_timer_signal, slots);
769-
#else //MBED_CONF_RTOS_PRESENT
770-
rf->cca_timer.attach_us(rf_cca_timer_interrupt, slots);
771-
#endif //MBED_CONF_RTOS_PRESENT
772784
}
773785

774786
static void rf_backup_timer_interrupt(void)
@@ -779,9 +791,7 @@ static void rf_backup_timer_interrupt(void)
779791
device_driver.phy_tx_done_cb(rf_radio_driver_id, mac_tx_handle, PHY_LINK_TX_SUCCESS, 0, 0);
780792
}
781793
}
782-
if (rf_read_register(TX_FIFO_STATUS)) {
783-
rf_send_command(S2LP_CMD_FLUSHTXFIFO);
784-
}
794+
rf_flush_tx_fifo();
785795
TEST_TX_DONE
786796
TEST_RX_DONE
787797
rf_state = RF_IDLE;
@@ -795,11 +805,7 @@ static void rf_backup_timer_stop(void)
795805

796806
static void rf_backup_timer_start(uint32_t slots)
797807
{
798-
#ifdef MBED_CONF_RTOS_PRESENT
799808
rf->backup_timer.attach_us(rf_backup_timer_signal, slots);
800-
#else //MBED_CONF_RTOS_PRESENT
801-
rf->backup_timer.attach_us(rf_backup_timer_interrupt, slots);
802-
#endif //MBED_CONF_RTOS_PRESENT
803809
}
804810

805811
static int8_t rf_start_cca(uint8_t *data_ptr, uint16_t data_length, uint8_t tx_handle, data_protocol_e data_protocol)
@@ -842,13 +848,6 @@ static int8_t rf_start_cca(uint8_t *data_ptr, uint16_t data_length, uint8_t tx_h
842848

843849
static void rf_send_ack(uint8_t seq)
844850
{
845-
// If the reception started during CCA process, the TX FIFO may already contain a data packet
846-
if (rf_read_register(TX_FIFO_STATUS)) {
847-
rf_send_command(S2LP_CMD_SABORT);
848-
rf_poll_state_change(S2LP_STATE_READY);
849-
rf_send_command(S2LP_CMD_FLUSHTXFIFO);
850-
rf_poll_state_change(S2LP_STATE_READY);
851-
}
852851
rf_state = RF_TX_ACK;
853852
uint8_t ack_frame[3] = {MAC_TYPE_ACK, 0, seq};
854853
rf_write_tx_fifo(ack_frame, sizeof(ack_frame));
@@ -880,6 +879,7 @@ static void rf_rx_ready_handler(void)
880879
{
881880
rf_backup_timer_stop();
882881
TEST_RX_DONE
882+
rf_flush_tx_fifo();
883883
int rx_read_length = rf_read_rx_fifo(rx_data_length, rf_read_register(RX_FIFO_STATUS));
884884
if (rx_read_length < 0) {
885885
rf_receive(rf_rx_channel);
@@ -931,6 +931,8 @@ static void rf_sync_detected_handler(void)
931931
rf_state = RF_RX_STARTED;
932932
TEST_RX_STARTED
933933
rf_disable_interrupt(SYNC_WORD);
934+
rf_enable_interrupt(RX_FIFO_ALMOST_FULL);
935+
rf_enable_interrupt(RX_DATA_READY);
934936
rf_backup_timer_start(MAX_PACKET_SENDING_TIME);
935937
}
936938

@@ -940,10 +942,10 @@ static void rf_receive(uint8_t rx_channel)
940942
return;
941943
}
942944
rf_lock();
945+
rf_send_command(S2LP_CMD_SABORT);
943946
rf_disable_all_interrupts();
944-
rf_state_change(S2LP_STATE_READY, false);
945-
rf_send_command(S2LP_CMD_FLUSHRXFIFO);
946947
rf_poll_state_change(S2LP_STATE_READY);
948+
rf_flush_rx_fifo();
947949
if (rf_update_config == true) {
948950
rf_update_config = false;
949951
rf_set_channel_configuration_registers();
@@ -952,18 +954,17 @@ static void rf_receive(uint8_t rx_channel)
952954
rf_write_register(CHNUM, rx_channel * rf_channel_multiplier);
953955
rf_rx_channel = rf_new_channel = rx_channel;
954956
}
955-
rf_state_change(S2LP_STATE_LOCK, false);
956-
rf_state_change(S2LP_STATE_RX, false);
957+
rf_send_command(S2LP_CMD_RX);
957958
rf_enable_interrupt(SYNC_WORD);
958-
rf_enable_interrupt(RX_FIFO_ALMOST_FULL);
959-
rf_enable_interrupt(RX_DATA_READY);
960959
rf_enable_interrupt(RX_FIFO_UNF_OVF);
961960
rx_data_length = 0;
962-
rf_state = RF_IDLE;
961+
if (rf_state != RF_CSMA_STARTED) {
962+
rf_state = RF_IDLE;
963+
}
964+
rf_poll_state_change(S2LP_STATE_RX);
963965
rf_unlock();
964966
}
965967

966-
#ifdef MBED_CONF_RTOS_PRESENT
967968
static void rf_interrupt_handler(void)
968969
{
969970
rf->irq_thread.flags_set(SIG_RADIO);
@@ -988,9 +989,6 @@ void RFPins::rf_irq_task(void)
988989
}
989990

990991
static void rf_irq_task_process_irq(void)
991-
#else //MBED_CONF_RTOS_PRESENT
992-
static void rf_interrupt_handler(void)
993-
#endif //MBED_CONF_RTOS_PRESENT
994992
{
995993
rf_lock();
996994
uint32_t irq_status = read_irq_status();
@@ -1027,6 +1025,7 @@ static void rf_interrupt_handler(void)
10271025
} else {
10281026
TEST_RX_DONE
10291027
rf_backup_timer_stop();
1028+
rf_flush_tx_fifo();
10301029
rf_state = RF_IDLE;
10311030
// In case the channel change was called during reception, driver is responsible to change the channel if CRC failed.
10321031
rf_receive(rf_new_channel);
@@ -1044,10 +1043,12 @@ static void rf_interrupt_handler(void)
10441043
}
10451044
if ((irq_status & (1 << RX_FIFO_UNF_OVF)) && (enabled_interrupts & (1 << RX_FIFO_UNF_OVF))) {
10461045
TEST_RX_DONE
1046+
rf_backup_timer_stop();
10471047
rf_send_command(S2LP_CMD_SABORT);
10481048
rf_poll_state_change(S2LP_STATE_READY);
10491049
rf_send_command(S2LP_CMD_FLUSHRXFIFO);
10501050
rf_poll_state_change(S2LP_STATE_READY);
1051+
rf_flush_tx_fifo();
10511052
rf_state = RF_IDLE;
10521053
rf_receive(rf_rx_channel);
10531054
}
@@ -1126,7 +1127,7 @@ void NanostackRfPhys2lp::get_mac_address(uint8_t *mac)
11261127
rf_unlock();
11271128
return;
11281129
}
1129-
memcpy((void*)mac, (void*)_mac_addr, sizeof(_mac_addr));
1130+
memcpy((void *)mac, (void *)_mac_addr, sizeof(_mac_addr));
11301131

11311132
rf_unlock();
11321133
}
@@ -1140,7 +1141,7 @@ void NanostackRfPhys2lp::set_mac_address(uint8_t *mac)
11401141
rf_unlock();
11411142
return;
11421143
}
1143-
memcpy((void*)_mac_addr, (void*)mac, sizeof(_mac_addr));
1144+
memcpy((void *)_mac_addr, (void *)mac, sizeof(_mac_addr));
11441145
_mac_set = true;
11451146

11461147
rf_unlock();
@@ -1162,7 +1163,7 @@ int8_t NanostackRfPhys2lp::rf_register()
11621163
randLIB_get_n_bytes_random(s2lp_MAC, 8);
11631164
s2lp_MAC[0] |= 2; //Set Local Bit
11641165
s2lp_MAC[0] &= ~1; //Clear multicast bit
1165-
tr_info("Generated random MAC %s", trace_array(s2lp_MAC,8));
1166+
tr_info("Generated random MAC %s", trace_array(s2lp_MAC, 8));
11661167
set_mac_address(s2lp_MAC);
11671168
}
11681169
rf = _rf;

components/802.15.4_RF/stm-s2lp-rf-driver/source/rf_configuration.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ uint32_t rf_conf_calculate_deviation(phy_modulation_index_e modulation_index, ui
161161
{
162162
uint32_t deviation = 0;
163163
if (modulation_index == MODULATION_INDEX_0_5) {
164-
deviation = datarate/4;
164+
deviation = datarate / 4;
165165
} else if (modulation_index == MODULATION_INDEX_1_0) {
166-
deviation = datarate/2;
166+
deviation = datarate / 2;
167167
}
168168
return deviation;
169169
}

0 commit comments

Comments
 (0)