Prototype RF driver for STM Sub-1 GHz RF expansion board based on the SPSGRF-868 module for STM32 Nucleo.

Prototype RF Driver for STM Sub-1 GHz RF Expansion Boards based on the SPSGRF-868 and SPSGRF-915 Modules for STM32 Nucleo

Currently supported boards:

Note, in order to use expansion board X-NUCLEO-IDS01A4 in mbed you need to perform the following HW modifications on the board:

  • Unmount resistor R4
  • Mount resistor R7

Furthermore, on some Nucleo development boards (e.g. the NUCLEO_F429ZI), in order to be able to use Ethernet together with these Sub-1 GHz RF expansion boards, you need to compile this driver with macro SPIRIT1_SPI_MOSI=PB_5 defined, while the development board typically requires some HW modification as e.g. described here!

This driver can be used together with the 6LoWPAN stack (a.k.a. Nanostack).

Committer:
Wolfgang Betz
Date:
Tue Feb 28 15:17:27 2017 +0100
Revision:
53:b036a3a850e6
Parent:
52:053dbc4b570f
Child:
58:4208689e447e
Call device driver callbacks from thread context

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 38:5deec3ab2025 1 #include "NanostackRfPhySpirit1.h"
Wolfgang Betz 38:5deec3ab2025 2 #include "SimpleSpirit1.h"
Wolfgang Betz 38:5deec3ab2025 3 #include "nanostack/platform/arm_hal_phy.h"
Wolfgang Betz 38:5deec3ab2025 4 #include "platform/arm_hal_interrupt.h"
Wolfgang Betz 38:5deec3ab2025 5
Wolfgang Betz 38:5deec3ab2025 6 #include "mbed_trace.h"
Wolfgang Betz 38:5deec3ab2025 7 #define TRACE_GROUP "SPIRIT"
Wolfgang Betz 38:5deec3ab2025 8
Wolfgang Betz 38:5deec3ab2025 9 /* Define beyond macro if you want to perform heavy debug tracing also in IRQ context */
Wolfgang Betz 38:5deec3ab2025 10 // #define HEAVY_TRACING
Wolfgang Betz 38:5deec3ab2025 11
Wolfgang Betz 38:5deec3ab2025 12 static phy_device_driver_s device_driver;
Wolfgang Betz 38:5deec3ab2025 13 static int8_t rf_radio_driver_id = -1;
Wolfgang Betz 38:5deec3ab2025 14
Wolfgang Betz 38:5deec3ab2025 15 const phy_rf_channel_configuration_s phy_subghz = {868000000, 1000000, 250000, 11, M_GFSK};
Wolfgang Betz 38:5deec3ab2025 16
Wolfgang Betz 38:5deec3ab2025 17 static phy_device_channel_page_s phy_channel_pages[] = {
Wolfgang Betz 38:5deec3ab2025 18 {CHANNEL_PAGE_2, &phy_subghz},
Wolfgang Betz 38:5deec3ab2025 19 {CHANNEL_PAGE_0, NULL}
Wolfgang Betz 38:5deec3ab2025 20 };
Wolfgang Betz 38:5deec3ab2025 21
Wolfgang Betz 38:5deec3ab2025 22 static uint8_t tx_sequence = 0xff;
Wolfgang Betz 38:5deec3ab2025 23 static uint8_t mac_tx_handle = 0;
Wolfgang Betz 38:5deec3ab2025 24
Wolfgang Betz 38:5deec3ab2025 25 static SimpleSpirit1 *rf_device = NULL;
Wolfgang Betz 38:5deec3ab2025 26 static uint8_t rf_rx_buf[MAX_PACKET_LEN];
Wolfgang Betz 38:5deec3ab2025 27
Wolfgang Betz 38:5deec3ab2025 28 static uint16_t stored_short_adr;
Wolfgang Betz 38:5deec3ab2025 29 static uint16_t stored_pan_id;
Wolfgang Betz 52:053dbc4b570f 30 static uint8_t stored_mac_address[8] = MBED_CONF_SPIRIT1_MAC_ADDRESS;
Wolfgang Betz 38:5deec3ab2025 31
Wolfgang Betz 38:5deec3ab2025 32 #define RF_SIG_ACK_NEEDED (1<<0)
Wolfgang Betz 53:b036a3a850e6 33 #define RF_SIG_CB_TX_DONE (1<<1)
Wolfgang Betz 53:b036a3a850e6 34 #define RF_SIG_CB_RX_RCVD (1<<2)
Wolfgang Betz 38:5deec3ab2025 35 static Thread rf_ack_sender(osPriorityRealtime);
Wolfgang Betz 38:5deec3ab2025 36 static volatile uint8_t rf_rx_sequence;
Wolfgang Betz 38:5deec3ab2025 37 static volatile bool rf_ack_sent = false;
Wolfgang Betz 38:5deec3ab2025 38
Wolfgang Betz 38:5deec3ab2025 39 /* MAC frame helper macros */
Wolfgang Betz 38:5deec3ab2025 40 #define MAC_FCF_FRAME_TYPE_MASK 0x0007
Wolfgang Betz 38:5deec3ab2025 41 #define MAC_FCF_FRAME_TYPE_SHIFT 0
Wolfgang Betz 38:5deec3ab2025 42 #define MAC_FCF_SECURITY_BIT_MASK 0x0008
Wolfgang Betz 38:5deec3ab2025 43 #define MAC_FCF_SECURITY_BIT_SHIFT 3
Wolfgang Betz 38:5deec3ab2025 44 #define MAC_FCF_PENDING_BIT_MASK 0x0010
Wolfgang Betz 38:5deec3ab2025 45 #define MAC_FCF_PENDING_BIT_SHIFT 4
Wolfgang Betz 38:5deec3ab2025 46 #define MAC_FCF_ACK_REQ_BIT_MASK 0x0020
Wolfgang Betz 38:5deec3ab2025 47 #define MAC_FCF_ACK_REQ_BIT_SHIFT 5
Wolfgang Betz 38:5deec3ab2025 48 #define MAC_FCF_INTRA_PANID_MASK 0x0040
Wolfgang Betz 38:5deec3ab2025 49 #define MAC_FCF_INTRA_PANID_SHIFT 6
Wolfgang Betz 38:5deec3ab2025 50 #define MAC_FCF_DST_ADDR_MASK 0x0c00
Wolfgang Betz 38:5deec3ab2025 51 #define MAC_FCF_DST_ADDR_SHIFT 10
Wolfgang Betz 38:5deec3ab2025 52 #define MAC_FCF_VERSION_MASK 0x3000
Wolfgang Betz 38:5deec3ab2025 53 #define MAC_FCF_VERSION_SHIFT 12
Wolfgang Betz 38:5deec3ab2025 54 #define MAC_FCF_SRC_ADDR_MASK 0xc000
Wolfgang Betz 38:5deec3ab2025 55 #define MAC_FCF_SRC_ADDR_SHIFT 14
Wolfgang Betz 38:5deec3ab2025 56
Wolfgang Betz 38:5deec3ab2025 57 /* MAC supported frame types */
Wolfgang Betz 38:5deec3ab2025 58 #define FC_BEACON_FRAME 0x00
Wolfgang Betz 38:5deec3ab2025 59 #define FC_DATA_FRAME 0x01
Wolfgang Betz 38:5deec3ab2025 60 #define FC_ACK_FRAME 0x02
Wolfgang Betz 38:5deec3ab2025 61 #define FC_CMD_FRAME 0x03
Wolfgang Betz 38:5deec3ab2025 62
Wolfgang Betz 38:5deec3ab2025 63 static void rf_if_lock(void)
Wolfgang Betz 38:5deec3ab2025 64 {
Wolfgang Betz 38:5deec3ab2025 65 platform_enter_critical();
Wolfgang Betz 38:5deec3ab2025 66 }
Wolfgang Betz 38:5deec3ab2025 67
Wolfgang Betz 38:5deec3ab2025 68 static void rf_if_unlock(void)
Wolfgang Betz 38:5deec3ab2025 69 {
Wolfgang Betz 38:5deec3ab2025 70 platform_exit_critical();
Wolfgang Betz 38:5deec3ab2025 71 }
Wolfgang Betz 38:5deec3ab2025 72
Wolfgang Betz 38:5deec3ab2025 73 static inline uint16_t rf_read_16_bit(uint8_t *data_ptr) { // little-endian
Wolfgang Betz 53:b036a3a850e6 74 uint16_t ret;
Wolfgang Betz 38:5deec3ab2025 75
Wolfgang Betz 53:b036a3a850e6 76 ret = ((uint16_t)data_ptr[0]) + (((uint16_t)data_ptr[1]) << 8);
Wolfgang Betz 53:b036a3a850e6 77 return ret;
Wolfgang Betz 38:5deec3ab2025 78 }
Wolfgang Betz 38:5deec3ab2025 79
Wolfgang Betz 38:5deec3ab2025 80 static int8_t rf_trigger_send(uint8_t *data_ptr, uint16_t data_length, uint8_t tx_handle, data_protocol_e data_protocol)
Wolfgang Betz 38:5deec3ab2025 81 {
Wolfgang Betz 38:5deec3ab2025 82 #ifndef NDEBUG
Wolfgang Betz 53:b036a3a850e6 83 debug_if(!(data_length >= 3), "\n\rassert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 84 #endif
Wolfgang Betz 38:5deec3ab2025 85
Wolfgang Betz 53:b036a3a850e6 86 /* Give 'rf_ack_sender' a better chance to run */
Wolfgang Betz 53:b036a3a850e6 87 Thread::yield();
Wolfgang Betz 38:5deec3ab2025 88
Wolfgang Betz 53:b036a3a850e6 89 /* Get Lock */
Wolfgang Betz 53:b036a3a850e6 90 rf_if_lock();
Wolfgang Betz 38:5deec3ab2025 91
Wolfgang Betz 38:5deec3ab2025 92 /*Check if transmitter is busy*/
Wolfgang Betz 38:5deec3ab2025 93 if(rf_device->is_receiving()) { /* betzw - WAS: (rf_device->channel_clear() != 0)), do NOT use this but rather study and enable automatic CCA */
Wolfgang Betz 53:b036a3a850e6 94 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 95
Wolfgang Betz 53:b036a3a850e6 96 /* Release Lock */
Wolfgang Betz 53:b036a3a850e6 97 rf_if_unlock();
Wolfgang Betz 38:5deec3ab2025 98
Wolfgang Betz 53:b036a3a850e6 99 /*Return busy*/
Wolfgang Betz 53:b036a3a850e6 100 return -1;
Wolfgang Betz 38:5deec3ab2025 101 } else {
Wolfgang Betz 38:5deec3ab2025 102 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 103 uint16_t fcf = rf_read_16_bit(data_ptr);
Wolfgang Betz 53:b036a3a850e6 104 uint16_t need_ack;
Wolfgang Betz 38:5deec3ab2025 105
Wolfgang Betz 53:b036a3a850e6 106 /*Check if transmitted data needs to be acked*/
Wolfgang Betz 53:b036a3a850e6 107 if((fcf & MAC_FCF_ACK_REQ_BIT_MASK) >> MAC_FCF_ACK_REQ_BIT_SHIFT)
Wolfgang Betz 53:b036a3a850e6 108 need_ack = 1;
Wolfgang Betz 53:b036a3a850e6 109 else
Wolfgang Betz 53:b036a3a850e6 110 need_ack = 0;
Wolfgang Betz 38:5deec3ab2025 111 #endif
Wolfgang Betz 38:5deec3ab2025 112
Wolfgang Betz 53:b036a3a850e6 113 /*Store the sequence number for ACK handling*/
Wolfgang Betz 53:b036a3a850e6 114 tx_sequence = *(data_ptr + 2);
Wolfgang Betz 38:5deec3ab2025 115
Wolfgang Betz 53:b036a3a850e6 116 /*Store TX handle*/
Wolfgang Betz 53:b036a3a850e6 117 mac_tx_handle = tx_handle;
Wolfgang Betz 38:5deec3ab2025 118
Wolfgang Betz 38:5deec3ab2025 119 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 120 tr_info("%s (%d), len=%d, tx_handle=%x, tx_seq=%x, need_ack=%d (%x:%x, %x:%x, %x:%x, %x:%x)", __func__, __LINE__,
Wolfgang Betz 53:b036a3a850e6 121 data_length, tx_handle, tx_sequence, need_ack,
Wolfgang Betz 53:b036a3a850e6 122 data_ptr[3], data_ptr[4], data_ptr[5], data_ptr[6],
Wolfgang Betz 53:b036a3a850e6 123 data_ptr[7], data_ptr[8], data_ptr[9], data_ptr[10]);
Wolfgang Betz 38:5deec3ab2025 124 #endif
Wolfgang Betz 38:5deec3ab2025 125
Wolfgang Betz 53:b036a3a850e6 126 /*Send the packet*/
Wolfgang Betz 53:b036a3a850e6 127 rf_device->send(data_ptr, data_length);
Wolfgang Betz 38:5deec3ab2025 128
Wolfgang Betz 53:b036a3a850e6 129 /* Release Lock */
Wolfgang Betz 53:b036a3a850e6 130 rf_if_unlock();
Wolfgang Betz 38:5deec3ab2025 131 }
Wolfgang Betz 38:5deec3ab2025 132
Wolfgang Betz 53:b036a3a850e6 133 /*Return success*/
Wolfgang Betz 38:5deec3ab2025 134 return 0;
Wolfgang Betz 38:5deec3ab2025 135 }
Wolfgang Betz 38:5deec3ab2025 136
Wolfgang Betz 38:5deec3ab2025 137 static int8_t rf_interface_state_control(phy_interface_state_e new_state, uint8_t rf_channel)
Wolfgang Betz 38:5deec3ab2025 138 {
Wolfgang Betz 38:5deec3ab2025 139 int8_t ret_val = 0;
Wolfgang Betz 38:5deec3ab2025 140 switch (new_state)
Wolfgang Betz 38:5deec3ab2025 141 {
Wolfgang Betz 53:b036a3a850e6 142 /*Reset PHY driver and set to idle*/
Wolfgang Betz 53:b036a3a850e6 143 case PHY_INTERFACE_RESET:
Wolfgang Betz 53:b036a3a850e6 144 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 145 rf_device->reset_board();
Wolfgang Betz 53:b036a3a850e6 146 break;
Wolfgang Betz 53:b036a3a850e6 147 /*Disable PHY Interface driver*/
Wolfgang Betz 53:b036a3a850e6 148 case PHY_INTERFACE_DOWN:
Wolfgang Betz 53:b036a3a850e6 149 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 150 ret_val = rf_device->off();
Wolfgang Betz 53:b036a3a850e6 151 if(ret_val != 0) ret_val = -1;
Wolfgang Betz 53:b036a3a850e6 152 break;
Wolfgang Betz 53:b036a3a850e6 153 /*Enable PHY Interface driver*/
Wolfgang Betz 53:b036a3a850e6 154 case PHY_INTERFACE_UP:
Wolfgang Betz 53:b036a3a850e6 155 ret_val = rf_device->on();
Wolfgang Betz 53:b036a3a850e6 156 if(ret_val != 0) {
Wolfgang Betz 53:b036a3a850e6 157 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 158 ret_val = -1;
Wolfgang Betz 53:b036a3a850e6 159 break;
Wolfgang Betz 53:b036a3a850e6 160 }
Wolfgang Betz 53:b036a3a850e6 161 tr_debug("%s (%d) - channel: %d", __func__, __LINE__, (int)rf_channel);
Wolfgang Betz 53:b036a3a850e6 162 rf_device->set_channel(rf_channel);
Wolfgang Betz 53:b036a3a850e6 163 break;
Wolfgang Betz 53:b036a3a850e6 164 /*Enable wireless interface ED scan mode*/
Wolfgang Betz 53:b036a3a850e6 165 case PHY_INTERFACE_RX_ENERGY_STATE:
Wolfgang Betz 53:b036a3a850e6 166 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 167 break;
Wolfgang Betz 53:b036a3a850e6 168 /*Enable Sniffer state*/
Wolfgang Betz 53:b036a3a850e6 169 case PHY_INTERFACE_SNIFFER_STATE:
Wolfgang Betz 53:b036a3a850e6 170 // TODO - if we really need this - WAS: rf_setup_sniffer(rf_channel);
Wolfgang Betz 53:b036a3a850e6 171 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 172 ret_val = -1;
Wolfgang Betz 53:b036a3a850e6 173 break;
Wolfgang Betz 53:b036a3a850e6 174 default:
Wolfgang Betz 53:b036a3a850e6 175 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 176 break;
Wolfgang Betz 38:5deec3ab2025 177 }
Wolfgang Betz 38:5deec3ab2025 178 return ret_val;
Wolfgang Betz 38:5deec3ab2025 179 }
Wolfgang Betz 38:5deec3ab2025 180
Wolfgang Betz 38:5deec3ab2025 181 static int8_t rf_extension(phy_extension_type_e extension_type, uint8_t *data_ptr)
Wolfgang Betz 38:5deec3ab2025 182 {
Wolfgang Betz 38:5deec3ab2025 183 switch (extension_type)
Wolfgang Betz 38:5deec3ab2025 184 {
Wolfgang Betz 53:b036a3a850e6 185 /*Control MAC pending bit for Indirect data transmission*/
Wolfgang Betz 53:b036a3a850e6 186 case PHY_EXTENSION_CTRL_PENDING_BIT:
Wolfgang Betz 53:b036a3a850e6 187 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 188 break;
Wolfgang Betz 38:5deec3ab2025 189
Wolfgang Betz 53:b036a3a850e6 190 /*Return frame pending status*/
Wolfgang Betz 53:b036a3a850e6 191 case PHY_EXTENSION_READ_LAST_ACK_PENDING_STATUS:
Wolfgang Betz 53:b036a3a850e6 192 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 193 *data_ptr = 0;
Wolfgang Betz 53:b036a3a850e6 194 break;
Wolfgang Betz 38:5deec3ab2025 195
Wolfgang Betz 53:b036a3a850e6 196 /*Set channel, used for setting channel for energy scan*/
Wolfgang Betz 53:b036a3a850e6 197 case PHY_EXTENSION_SET_CHANNEL:
Wolfgang Betz 53:b036a3a850e6 198 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 199 break;
Wolfgang Betz 38:5deec3ab2025 200
Wolfgang Betz 53:b036a3a850e6 201 /*Read energy on the channel*/
Wolfgang Betz 53:b036a3a850e6 202 case PHY_EXTENSION_READ_CHANNEL_ENERGY:
Wolfgang Betz 53:b036a3a850e6 203 // TODO: *data_ptr = rf_get_channel_energy();
Wolfgang Betz 53:b036a3a850e6 204 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 205 *data_ptr = (int8_t)rf_device->get_last_rssi_dbm();
Wolfgang Betz 53:b036a3a850e6 206 break;
Wolfgang Betz 38:5deec3ab2025 207
Wolfgang Betz 53:b036a3a850e6 208 /*Read status of the link*/
Wolfgang Betz 53:b036a3a850e6 209 case PHY_EXTENSION_READ_LINK_STATUS:
Wolfgang Betz 53:b036a3a850e6 210 // TODO: *data_ptr = rf_get_link_status();
Wolfgang Betz 53:b036a3a850e6 211 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 212 *data_ptr = rf_device->get_last_sqi(); // use SQI as link quality
Wolfgang Betz 53:b036a3a850e6 213 break;
Wolfgang Betz 38:5deec3ab2025 214
Wolfgang Betz 53:b036a3a850e6 215 default:
Wolfgang Betz 53:b036a3a850e6 216 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 217 break;
Wolfgang Betz 38:5deec3ab2025 218 }
Wolfgang Betz 38:5deec3ab2025 219 return 0;
Wolfgang Betz 38:5deec3ab2025 220 }
Wolfgang Betz 38:5deec3ab2025 221
Wolfgang Betz 38:5deec3ab2025 222 static inline void rf_set_mac_address(uint8_t *ptr) {
Wolfgang Betz 53:b036a3a850e6 223 tr_debug("%s (%d), adr0=%x, adr1=%x, adr2=%x, adr3=%x, adr4=%x, adr5=%x, adr6=%x, adr7=%x",
Wolfgang Betz 53:b036a3a850e6 224 __func__, __LINE__,
Wolfgang Betz 53:b036a3a850e6 225 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7]);
Wolfgang Betz 53:b036a3a850e6 226 for(int i = 0; i < 8; i++) {
Wolfgang Betz 53:b036a3a850e6 227 stored_mac_address[i] = ptr[i];
Wolfgang Betz 53:b036a3a850e6 228 }
Wolfgang Betz 38:5deec3ab2025 229 }
Wolfgang Betz 38:5deec3ab2025 230
Wolfgang Betz 38:5deec3ab2025 231 static inline void rf_get_mac_address(uint8_t *ptr) {
Wolfgang Betz 53:b036a3a850e6 232 for(int i = 0; i < 8; i++) {
Wolfgang Betz 53:b036a3a850e6 233 ptr[i] = stored_mac_address[i];
Wolfgang Betz 53:b036a3a850e6 234 }
Wolfgang Betz 53:b036a3a850e6 235 tr_debug("%s (%d), adr0=%x, adr1=%x, adr2=%x, adr3=%x, adr4=%x, adr5=%x, adr6=%x, adr7=%x",
Wolfgang Betz 53:b036a3a850e6 236 __func__, __LINE__,
Wolfgang Betz 53:b036a3a850e6 237 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7]);
Wolfgang Betz 38:5deec3ab2025 238 }
Wolfgang Betz 38:5deec3ab2025 239
Wolfgang Betz 38:5deec3ab2025 240 static inline void rf_set_short_adr(uint8_t *ptr) {
Wolfgang Betz 53:b036a3a850e6 241 stored_short_adr = (ptr[0] << 8) + ptr[1]; // big-endian
Wolfgang Betz 53:b036a3a850e6 242 tr_debug("%s (%d), adr0=%x, adr1=%x, val=%d",
Wolfgang Betz 53:b036a3a850e6 243 __func__, __LINE__,
Wolfgang Betz 53:b036a3a850e6 244 ptr[0], ptr[1], stored_short_adr);
Wolfgang Betz 38:5deec3ab2025 245 }
Wolfgang Betz 38:5deec3ab2025 246
Wolfgang Betz 38:5deec3ab2025 247 static inline void rf_set_pan_id(uint8_t *ptr) {
Wolfgang Betz 53:b036a3a850e6 248 stored_pan_id = (ptr[0] << 8) + ptr[1]; // big-endian
Wolfgang Betz 53:b036a3a850e6 249 tr_debug("%s (%d), adr0=%x, adr1=%x, val=%d",
Wolfgang Betz 53:b036a3a850e6 250 __func__, __LINE__,
Wolfgang Betz 53:b036a3a850e6 251 ptr[0], ptr[1], stored_pan_id);
Wolfgang Betz 38:5deec3ab2025 252 }
Wolfgang Betz 38:5deec3ab2025 253
Wolfgang Betz 38:5deec3ab2025 254 static int8_t rf_address_write(phy_address_type_e address_type, uint8_t *address_ptr)
Wolfgang Betz 38:5deec3ab2025 255 {
Wolfgang Betz 38:5deec3ab2025 256 switch (address_type)
Wolfgang Betz 38:5deec3ab2025 257 {
Wolfgang Betz 53:b036a3a850e6 258 /*Set 48-bit address*/
Wolfgang Betz 53:b036a3a850e6 259 case PHY_MAC_48BIT:
Wolfgang Betz 53:b036a3a850e6 260 /* Not used in this example */
Wolfgang Betz 53:b036a3a850e6 261 // betzw - WAS: rf_set_mac_48bit(address_ptr);
Wolfgang Betz 53:b036a3a850e6 262 break;
Wolfgang Betz 53:b036a3a850e6 263 /*Set 64-bit address*/
Wolfgang Betz 53:b036a3a850e6 264 case PHY_MAC_64BIT:
Wolfgang Betz 53:b036a3a850e6 265 rf_set_mac_address(address_ptr);
Wolfgang Betz 53:b036a3a850e6 266 break;
Wolfgang Betz 53:b036a3a850e6 267 /*Set 16-bit address*/
Wolfgang Betz 53:b036a3a850e6 268 case PHY_MAC_16BIT:
Wolfgang Betz 53:b036a3a850e6 269 rf_set_short_adr(address_ptr);
Wolfgang Betz 53:b036a3a850e6 270 break;
Wolfgang Betz 53:b036a3a850e6 271 /*Set PAN Id*/
Wolfgang Betz 53:b036a3a850e6 272 case PHY_MAC_PANID:
Wolfgang Betz 53:b036a3a850e6 273 rf_set_pan_id(address_ptr);
Wolfgang Betz 53:b036a3a850e6 274 break;
Wolfgang Betz 38:5deec3ab2025 275 }
Wolfgang Betz 38:5deec3ab2025 276
Wolfgang Betz 38:5deec3ab2025 277 return 0;
Wolfgang Betz 38:5deec3ab2025 278 }
Wolfgang Betz 38:5deec3ab2025 279
Wolfgang Betz 38:5deec3ab2025 280 /* Note: we are in IRQ context */
Wolfgang Betz 53:b036a3a850e6 281 static inline void rf_send_signal(int32_t signal) {
Wolfgang Betz 53:b036a3a850e6 282 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 283 tr_info("%s (%d): %d", __func__, __LINE__, signal);
Wolfgang Betz 53:b036a3a850e6 284 #endif
Wolfgang Betz 53:b036a3a850e6 285 rf_ack_sender.signal_set(signal);
Wolfgang Betz 53:b036a3a850e6 286 }
Wolfgang Betz 53:b036a3a850e6 287
Wolfgang Betz 53:b036a3a850e6 288 static phy_link_tx_status_e phy_status;
Wolfgang Betz 53:b036a3a850e6 289 /* Note: we are in IRQ context */
Wolfgang Betz 38:5deec3ab2025 290 static void rf_handle_ack(uint8_t seq_number)
Wolfgang Betz 38:5deec3ab2025 291 {
Wolfgang Betz 38:5deec3ab2025 292 /*Received ACK sequence must be equal with transmitted packet sequence*/
Wolfgang Betz 38:5deec3ab2025 293 if(tx_sequence == seq_number)
Wolfgang Betz 38:5deec3ab2025 294 {
Wolfgang Betz 38:5deec3ab2025 295 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 296 tr_info("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 297 #endif
Wolfgang Betz 38:5deec3ab2025 298
Wolfgang Betz 53:b036a3a850e6 299 /*Call PHY TX Done API*/
Wolfgang Betz 53:b036a3a850e6 300 if(device_driver.phy_tx_done_cb){
Wolfgang Betz 53:b036a3a850e6 301 phy_status = PHY_LINK_TX_DONE;
Wolfgang Betz 53:b036a3a850e6 302 rf_send_signal(RF_SIG_CB_TX_DONE);
Wolfgang Betz 53:b036a3a850e6 303 }
Wolfgang Betz 38:5deec3ab2025 304 } else {
Wolfgang Betz 38:5deec3ab2025 305 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 306 tr_info("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 307 #endif
Wolfgang Betz 38:5deec3ab2025 308 }
Wolfgang Betz 38:5deec3ab2025 309 }
Wolfgang Betz 38:5deec3ab2025 310
Wolfgang Betz 38:5deec3ab2025 311 /* Note: we are in IRQ context */
Wolfgang Betz 38:5deec3ab2025 312 static inline bool rf_check_mac_address(uint8_t *dest) {
Wolfgang Betz 53:b036a3a850e6 313 for(int i = 0; i < 8; i++) {
Wolfgang Betz 53:b036a3a850e6 314 if(dest[i] != stored_mac_address[7-i]) return false;
Wolfgang Betz 53:b036a3a850e6 315 }
Wolfgang Betz 53:b036a3a850e6 316 return true;
Wolfgang Betz 38:5deec3ab2025 317 }
Wolfgang Betz 38:5deec3ab2025 318
Wolfgang Betz 38:5deec3ab2025 319 /* Note: we are in IRQ context */
Wolfgang Betz 38:5deec3ab2025 320 /* Returns true if packet should be accepted */
Wolfgang Betz 38:5deec3ab2025 321 static bool rf_check_destination(int len, uint8_t *ack_requested) {
Wolfgang Betz 53:b036a3a850e6 322 uint8_t frame_type;
Wolfgang Betz 53:b036a3a850e6 323 uint16_t dst_pan_id;
Wolfgang Betz 53:b036a3a850e6 324 uint16_t dst_short_adr;
Wolfgang Betz 53:b036a3a850e6 325 uint8_t dst_addr_mode = 0x0; /*0x00 = no address 0x01 = reserved 0x02 = 16-bit short address 0x03 = 64-bit extended address */
Wolfgang Betz 53:b036a3a850e6 326 uint8_t src_addr_mode = 0x0; /*0x00 = no address 0x01 = reserved 0x02 = 16-bit short address 0x03 = 64-bit extended address */
Wolfgang Betz 53:b036a3a850e6 327 uint8_t min_size = 3; // FCF & SeqNr
Wolfgang Betz 53:b036a3a850e6 328 bool ret = false;
Wolfgang Betz 53:b036a3a850e6 329 #if defined(HEAVY_TRACING)
Wolfgang Betz 53:b036a3a850e6 330 bool panid_compr = false;
Wolfgang Betz 38:5deec3ab2025 331 #endif
Wolfgang Betz 38:5deec3ab2025 332
Wolfgang Betz 53:b036a3a850e6 333 if(len < 3) {
Wolfgang Betz 53:b036a3a850e6 334 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 335 return false;
Wolfgang Betz 53:b036a3a850e6 336 }
Wolfgang Betz 38:5deec3ab2025 337
Wolfgang Betz 53:b036a3a850e6 338 uint16_t fcf = rf_read_16_bit(rf_rx_buf);
Wolfgang Betz 53:b036a3a850e6 339 frame_type = ((fcf & MAC_FCF_FRAME_TYPE_MASK) >> MAC_FCF_FRAME_TYPE_SHIFT);
Wolfgang Betz 53:b036a3a850e6 340 (*ack_requested) = ((fcf & MAC_FCF_ACK_REQ_BIT_MASK) >> MAC_FCF_ACK_REQ_BIT_SHIFT);
Wolfgang Betz 53:b036a3a850e6 341 dst_addr_mode = ((fcf & MAC_FCF_DST_ADDR_MASK) >> MAC_FCF_DST_ADDR_SHIFT);
Wolfgang Betz 53:b036a3a850e6 342 src_addr_mode = ((fcf & MAC_FCF_SRC_ADDR_MASK) >> MAC_FCF_SRC_ADDR_SHIFT);
Wolfgang Betz 53:b036a3a850e6 343 #if defined(HEAVY_TRACING)
Wolfgang Betz 53:b036a3a850e6 344 panid_compr = ((fcf & MAC_FCF_INTRA_PANID_MASK) >> MAC_FCF_INTRA_PANID_SHIFT);
Wolfgang Betz 38:5deec3ab2025 345 #endif
Wolfgang Betz 38:5deec3ab2025 346
Wolfgang Betz 38:5deec3ab2025 347 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 348 tr_info("%s (%d): len=%d, ftype=%x, snr=%x, ack=%d, dst=%x, src=%x, intra=%d", __func__, __LINE__, len, frame_type,
Wolfgang Betz 53:b036a3a850e6 349 rf_rx_buf[2], (*ack_requested), dst_addr_mode, src_addr_mode, panid_compr);
Wolfgang Betz 38:5deec3ab2025 350 #endif
Wolfgang Betz 38:5deec3ab2025 351
Wolfgang Betz 53:b036a3a850e6 352 if(frame_type == FC_ACK_FRAME) { // betzw: we support up to two different forms of ACK frames!
Wolfgang Betz 53:b036a3a850e6 353 if((len == 3) && (dst_addr_mode == 0x0) && (src_addr_mode == 0x0)) {
Wolfgang Betz 53:b036a3a850e6 354 ret = true;
Wolfgang Betz 38:5deec3ab2025 355 }
Wolfgang Betz 38:5deec3ab2025 356
Wolfgang Betz 38:5deec3ab2025 357 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 358 tr_info("%s (%d): ret=%d", __func__, __LINE__, ret);
Wolfgang Betz 53:b036a3a850e6 359 #endif
Wolfgang Betz 53:b036a3a850e6 360 (*ack_requested) = 0; // Never acknowledge ACK frames
Wolfgang Betz 53:b036a3a850e6 361 return ret;
Wolfgang Betz 53:b036a3a850e6 362 }
Wolfgang Betz 53:b036a3a850e6 363
Wolfgang Betz 53:b036a3a850e6 364 switch(dst_addr_mode) {
Wolfgang Betz 53:b036a3a850e6 365 case 0x00:
Wolfgang Betz 53:b036a3a850e6 366 ret = true; // no check possible;
Wolfgang Betz 53:b036a3a850e6 367 break;
Wolfgang Betz 53:b036a3a850e6 368 case 0x02:
Wolfgang Betz 53:b036a3a850e6 369 min_size += 4; // pan id + short dest adr
Wolfgang Betz 53:b036a3a850e6 370
Wolfgang Betz 53:b036a3a850e6 371 if(len < 5) {
Wolfgang Betz 53:b036a3a850e6 372 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 373 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 374 #endif
Wolfgang Betz 53:b036a3a850e6 375 return false;
Wolfgang Betz 53:b036a3a850e6 376 }
Wolfgang Betz 53:b036a3a850e6 377
Wolfgang Betz 53:b036a3a850e6 378 dst_pan_id = rf_read_16_bit(&rf_rx_buf[3]);
Wolfgang Betz 53:b036a3a850e6 379 if(dst_pan_id == 0xFFFF) {
Wolfgang Betz 53:b036a3a850e6 380 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 381 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 382 #endif
Wolfgang Betz 53:b036a3a850e6 383 ret = true;
Wolfgang Betz 53:b036a3a850e6 384 break;
Wolfgang Betz 53:b036a3a850e6 385 }
Wolfgang Betz 53:b036a3a850e6 386
Wolfgang Betz 53:b036a3a850e6 387 if(dst_pan_id == stored_pan_id) {
Wolfgang Betz 53:b036a3a850e6 388 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 389 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 390 #endif
Wolfgang Betz 53:b036a3a850e6 391 ret = true;
Wolfgang Betz 53:b036a3a850e6 392 break;
Wolfgang Betz 53:b036a3a850e6 393 } else {
Wolfgang Betz 53:b036a3a850e6 394 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 395 tr_debug("%s (%d): %d!=%d", __func__, __LINE__, dst_pan_id, stored_pan_id);
Wolfgang Betz 53:b036a3a850e6 396 #endif
Wolfgang Betz 53:b036a3a850e6 397 }
Wolfgang Betz 53:b036a3a850e6 398
Wolfgang Betz 53:b036a3a850e6 399 if(len < 7) {
Wolfgang Betz 53:b036a3a850e6 400 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 401 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 402 #endif
Wolfgang Betz 53:b036a3a850e6 403 return false;
Wolfgang Betz 53:b036a3a850e6 404 }
Wolfgang Betz 53:b036a3a850e6 405
Wolfgang Betz 53:b036a3a850e6 406 dst_short_adr = rf_read_16_bit(&rf_rx_buf[5]);
Wolfgang Betz 53:b036a3a850e6 407 if(dst_short_adr == stored_short_adr) {
Wolfgang Betz 53:b036a3a850e6 408 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 409 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 410 #endif
Wolfgang Betz 53:b036a3a850e6 411 ret = true;
Wolfgang Betz 53:b036a3a850e6 412 break;
Wolfgang Betz 53:b036a3a850e6 413 } else {
Wolfgang Betz 53:b036a3a850e6 414 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 415 tr_debug("%s (%d): %d!=%d", __func__, __LINE__, dst_short_adr, stored_short_adr);
Wolfgang Betz 53:b036a3a850e6 416 #endif
Wolfgang Betz 53:b036a3a850e6 417 }
Wolfgang Betz 53:b036a3a850e6 418 break;
Wolfgang Betz 53:b036a3a850e6 419 case 0x03:
Wolfgang Betz 53:b036a3a850e6 420 min_size += 10; // pan id + dest mac addr
Wolfgang Betz 53:b036a3a850e6 421
Wolfgang Betz 53:b036a3a850e6 422 if(len < 5) {
Wolfgang Betz 53:b036a3a850e6 423 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 424 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 425 #endif
Wolfgang Betz 53:b036a3a850e6 426 return false;
Wolfgang Betz 53:b036a3a850e6 427 }
Wolfgang Betz 53:b036a3a850e6 428
Wolfgang Betz 53:b036a3a850e6 429 dst_pan_id = rf_read_16_bit(&rf_rx_buf[3]);
Wolfgang Betz 53:b036a3a850e6 430 if(dst_pan_id == 0xFFFF) {
Wolfgang Betz 53:b036a3a850e6 431 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 432 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 433 #endif
Wolfgang Betz 53:b036a3a850e6 434 ret = true;
Wolfgang Betz 53:b036a3a850e6 435 break;
Wolfgang Betz 53:b036a3a850e6 436 }
Wolfgang Betz 53:b036a3a850e6 437
Wolfgang Betz 53:b036a3a850e6 438 if(dst_pan_id == stored_pan_id) {
Wolfgang Betz 53:b036a3a850e6 439 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 440 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 441 #endif
Wolfgang Betz 53:b036a3a850e6 442 ret = true;
Wolfgang Betz 53:b036a3a850e6 443 break;
Wolfgang Betz 53:b036a3a850e6 444 }
Wolfgang Betz 53:b036a3a850e6 445
Wolfgang Betz 53:b036a3a850e6 446 if(len < 13) {
Wolfgang Betz 53:b036a3a850e6 447 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 448 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 449 #endif
Wolfgang Betz 53:b036a3a850e6 450 return false;
Wolfgang Betz 53:b036a3a850e6 451 }
Wolfgang Betz 53:b036a3a850e6 452
Wolfgang Betz 53:b036a3a850e6 453 ret = rf_check_mac_address(&rf_rx_buf[5]);
Wolfgang Betz 53:b036a3a850e6 454 break;
Wolfgang Betz 53:b036a3a850e6 455 default:
Wolfgang Betz 53:b036a3a850e6 456 /* not supported */
Wolfgang Betz 53:b036a3a850e6 457 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 458 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 459 #endif
Wolfgang Betz 53:b036a3a850e6 460 return false;
Wolfgang Betz 53:b036a3a850e6 461 }
Wolfgang Betz 53:b036a3a850e6 462
Wolfgang Betz 53:b036a3a850e6 463 if(ret && (*ack_requested)) {
Wolfgang Betz 53:b036a3a850e6 464 rf_rx_sequence = rf_rx_buf[2];
Wolfgang Betz 53:b036a3a850e6 465 }
Wolfgang Betz 53:b036a3a850e6 466
Wolfgang Betz 53:b036a3a850e6 467 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 468 tr_info("%s (%d), ret=%d, ack=%d", __func__, __LINE__, ret, (*ack_requested));
Wolfgang Betz 53:b036a3a850e6 469 #endif
Wolfgang Betz 53:b036a3a850e6 470 return ret;
Wolfgang Betz 38:5deec3ab2025 471 }
Wolfgang Betz 38:5deec3ab2025 472
Wolfgang Betz 53:b036a3a850e6 473 static uint16_t rf_buffer_len = 0;
Wolfgang Betz 53:b036a3a850e6 474 static uint8_t rf_sqi;
Wolfgang Betz 53:b036a3a850e6 475 static int8_t rf_rssi;
Wolfgang Betz 38:5deec3ab2025 476 /* Note: we are in IRQ context */
Wolfgang Betz 38:5deec3ab2025 477 static inline void rf_handle_rx_end(void)
Wolfgang Betz 38:5deec3ab2025 478 {
Wolfgang Betz 38:5deec3ab2025 479 uint8_t ack_requested = 0;
Wolfgang Betz 38:5deec3ab2025 480
Wolfgang Betz 38:5deec3ab2025 481 /* Get received data */
Wolfgang Betz 38:5deec3ab2025 482 rf_buffer_len = rf_device->read(rf_rx_buf, MAX_PACKET_LEN);
Wolfgang Betz 38:5deec3ab2025 483 if(!rf_buffer_len)
Wolfgang Betz 53:b036a3a850e6 484 return;
Wolfgang Betz 38:5deec3ab2025 485
Wolfgang Betz 38:5deec3ab2025 486 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 487 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 488 #endif
Wolfgang Betz 38:5deec3ab2025 489
Wolfgang Betz 53:b036a3a850e6 490 /* Check if packet should be accepted */
Wolfgang Betz 38:5deec3ab2025 491 if(!rf_check_destination(rf_buffer_len, &ack_requested)) {
Wolfgang Betz 38:5deec3ab2025 492 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 493 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 494 #endif
Wolfgang Betz 53:b036a3a850e6 495 return;
Wolfgang Betz 38:5deec3ab2025 496 }
Wolfgang Betz 38:5deec3ab2025 497
Wolfgang Betz 38:5deec3ab2025 498 /* If waiting for ACK, check here if the packet is an ACK to a message previously sent */
Wolfgang Betz 38:5deec3ab2025 499 uint16_t fcf = rf_read_16_bit(rf_rx_buf);
Wolfgang Betz 38:5deec3ab2025 500 if(((fcf & MAC_FCF_FRAME_TYPE_MASK) >> MAC_FCF_FRAME_TYPE_SHIFT) == FC_ACK_FRAME) {
Wolfgang Betz 53:b036a3a850e6 501 /*Send sequence number in ACK handler*/
Wolfgang Betz 38:5deec3ab2025 502 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 503 tr_debug("%s (%d), len=%u", __func__, __LINE__, (unsigned int)rf_buffer_len);
Wolfgang Betz 38:5deec3ab2025 504 #endif
Wolfgang Betz 53:b036a3a850e6 505 rf_handle_ack(rf_rx_buf[2]);
Wolfgang Betz 53:b036a3a850e6 506 return;
Wolfgang Betz 38:5deec3ab2025 507 }
Wolfgang Betz 38:5deec3ab2025 508
Wolfgang Betz 38:5deec3ab2025 509 /* Kick off ACK sending */
Wolfgang Betz 38:5deec3ab2025 510 if(ack_requested) {
Wolfgang Betz 38:5deec3ab2025 511 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 512 tr_debug("%s (%d), len=%u", __func__, __LINE__, (unsigned int)rf_buffer_len);
Wolfgang Betz 38:5deec3ab2025 513 #endif
Wolfgang Betz 53:b036a3a850e6 514 rf_send_signal(RF_SIG_ACK_NEEDED);
Wolfgang Betz 38:5deec3ab2025 515 }
Wolfgang Betz 38:5deec3ab2025 516
Wolfgang Betz 38:5deec3ab2025 517 /* Get link information */
Wolfgang Betz 38:5deec3ab2025 518 rf_rssi = (int8_t)rf_device->get_last_rssi_dbm();
Wolfgang Betz 38:5deec3ab2025 519 rf_sqi = (uint8_t)rf_device->get_last_sqi(); // use SQI as link quality
Wolfgang Betz 38:5deec3ab2025 520
Wolfgang Betz 38:5deec3ab2025 521 /* Note: Checksum of the packet must be checked and removed before entering here */
Wolfgang Betz 38:5deec3ab2025 522 /* TODO - betzw: what to do? */
Wolfgang Betz 38:5deec3ab2025 523
Wolfgang Betz 38:5deec3ab2025 524 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 525 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 526 #endif
Wolfgang Betz 38:5deec3ab2025 527
Wolfgang Betz 53:b036a3a850e6 528 /* Send received data and link information to the network stack */
Wolfgang Betz 38:5deec3ab2025 529 if( device_driver.phy_rx_cb ){
Wolfgang Betz 53:b036a3a850e6 530 rf_send_signal(RF_SIG_CB_RX_RCVD);
Wolfgang Betz 38:5deec3ab2025 531 }
Wolfgang Betz 38:5deec3ab2025 532 }
Wolfgang Betz 38:5deec3ab2025 533
Wolfgang Betz 38:5deec3ab2025 534 /* Note: we are in IRQ context */
Wolfgang Betz 38:5deec3ab2025 535 static inline void rf_handle_tx_end(void)
Wolfgang Betz 38:5deec3ab2025 536 {
Wolfgang Betz 38:5deec3ab2025 537 /* Check if this is an ACK sending which is still pending */
Wolfgang Betz 53:b036a3a850e6 538 if(rf_ack_sent) {
Wolfgang Betz 53:b036a3a850e6 539 rf_ack_sent = false;
Wolfgang Betz 38:5deec3ab2025 540 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 541 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 542 #endif
Wolfgang Betz 53:b036a3a850e6 543 return; // no need to inform stack
Wolfgang Betz 53:b036a3a850e6 544 }
Wolfgang Betz 38:5deec3ab2025 545
Wolfgang Betz 38:5deec3ab2025 546 /*Call PHY TX Done API*/
Wolfgang Betz 38:5deec3ab2025 547 if(device_driver.phy_tx_done_cb){
Wolfgang Betz 53:b036a3a850e6 548 phy_status = PHY_LINK_TX_SUCCESS;
Wolfgang Betz 53:b036a3a850e6 549 rf_send_signal(RF_SIG_CB_TX_DONE);
Wolfgang Betz 38:5deec3ab2025 550 }
Wolfgang Betz 38:5deec3ab2025 551 }
Wolfgang Betz 38:5deec3ab2025 552
Wolfgang Betz 38:5deec3ab2025 553 /* Note: we are in IRQ context */
Wolfgang Betz 38:5deec3ab2025 554 static inline void rf_handle_tx_err(void) {
Wolfgang Betz 38:5deec3ab2025 555 /*Call PHY TX Done API*/
Wolfgang Betz 38:5deec3ab2025 556 if(device_driver.phy_tx_done_cb){
Wolfgang Betz 53:b036a3a850e6 557 phy_status = PHY_LINK_TX_FAIL;
Wolfgang Betz 53:b036a3a850e6 558 rf_send_signal(RF_SIG_CB_TX_DONE);
Wolfgang Betz 38:5deec3ab2025 559 }
Wolfgang Betz 38:5deec3ab2025 560 }
Wolfgang Betz 38:5deec3ab2025 561
Wolfgang Betz 38:5deec3ab2025 562 /* Note: we are in IRQ context */
Wolfgang Betz 38:5deec3ab2025 563 static void rf_callback_func(int event) {
Wolfgang Betz 53:b036a3a850e6 564 switch(event) {
Wolfgang Betz 53:b036a3a850e6 565 case SimpleSpirit1::RX_DONE:
Wolfgang Betz 53:b036a3a850e6 566 rf_handle_rx_end();
Wolfgang Betz 53:b036a3a850e6 567 break;
Wolfgang Betz 53:b036a3a850e6 568 case SimpleSpirit1::TX_DONE:
Wolfgang Betz 53:b036a3a850e6 569 rf_handle_tx_end();
Wolfgang Betz 53:b036a3a850e6 570 break;
Wolfgang Betz 53:b036a3a850e6 571 case SimpleSpirit1::TX_ERR:
Wolfgang Betz 38:5deec3ab2025 572 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 573 tr_debug("%s (%d): TX_ERR!!!", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 574 #endif
Wolfgang Betz 53:b036a3a850e6 575 rf_handle_tx_err();
Wolfgang Betz 53:b036a3a850e6 576 break;
Wolfgang Betz 53:b036a3a850e6 577 }
Wolfgang Betz 38:5deec3ab2025 578 }
Wolfgang Betz 38:5deec3ab2025 579
Wolfgang Betz 38:5deec3ab2025 580 static void rf_ack_loop(void) {
Wolfgang Betz 53:b036a3a850e6 581 static uint16_t buffer[2] = {
Wolfgang Betz 53:b036a3a850e6 582 (FC_ACK_FRAME << MAC_FCF_FRAME_TYPE_SHIFT),
Wolfgang Betz 53:b036a3a850e6 583 0x0
Wolfgang Betz 53:b036a3a850e6 584 };
Wolfgang Betz 53:b036a3a850e6 585
Wolfgang Betz 53:b036a3a850e6 586 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 587
Wolfgang Betz 53:b036a3a850e6 588 do {
Wolfgang Betz 53:b036a3a850e6 589 /* Wait for signal */
Wolfgang Betz 53:b036a3a850e6 590 osEvent event = rf_ack_sender.signal_wait(0);
Wolfgang Betz 38:5deec3ab2025 591
Wolfgang Betz 53:b036a3a850e6 592 if(event.status != osEventSignal) {
Wolfgang Betz 53:b036a3a850e6 593 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 594 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 595 #endif
Wolfgang Betz 53:b036a3a850e6 596 continue;
Wolfgang Betz 53:b036a3a850e6 597 }
Wolfgang Betz 53:b036a3a850e6 598
Wolfgang Betz 53:b036a3a850e6 599 int32_t signals = event.value.signals;
Wolfgang Betz 38:5deec3ab2025 600
Wolfgang Betz 38:5deec3ab2025 601 #ifdef HEAVY_TRACING
Wolfgang Betz 38:5deec3ab2025 602 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 603 #endif
Wolfgang Betz 38:5deec3ab2025 604
Wolfgang Betz 38:5deec3ab2025 605 /* Get Lock */
Wolfgang Betz 53:b036a3a850e6 606 rf_if_lock();
Wolfgang Betz 53:b036a3a850e6 607
Wolfgang Betz 53:b036a3a850e6 608 if(signals & RF_SIG_ACK_NEEDED) {
Wolfgang Betz 53:b036a3a850e6 609 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 610 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 611 #endif
Wolfgang Betz 53:b036a3a850e6 612
Wolfgang Betz 53:b036a3a850e6 613 /* Prepare payload */
Wolfgang Betz 53:b036a3a850e6 614 uint8_t *ptr = (uint8_t*)&buffer[1];
Wolfgang Betz 53:b036a3a850e6 615 ptr[0] = rf_rx_sequence; // Sequence number
Wolfgang Betz 38:5deec3ab2025 616
Wolfgang Betz 53:b036a3a850e6 617 /* Wait for device not receiving */
Wolfgang Betz 53:b036a3a850e6 618 while(rf_device->is_receiving()) {
Wolfgang Betz 53:b036a3a850e6 619 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 620 tr_info("%s (%d)", __func__, __LINE__);
Wolfgang Betz 53:b036a3a850e6 621 #endif
Wolfgang Betz 53:b036a3a850e6 622 wait_us(10);
Wolfgang Betz 53:b036a3a850e6 623 }
Wolfgang Betz 53:b036a3a850e6 624
Wolfgang Betz 53:b036a3a850e6 625 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 626 tr_debug("%s (%d), hdr=%x, nr=%x", __func__, __LINE__, buffer[0], ptr[0]);
Wolfgang Betz 53:b036a3a850e6 627 #endif
Wolfgang Betz 53:b036a3a850e6 628
Wolfgang Betz 53:b036a3a850e6 629 /* Set information that we have sent an ACK */
Wolfgang Betz 53:b036a3a850e6 630 rf_ack_sent = true;
Wolfgang Betz 53:b036a3a850e6 631
Wolfgang Betz 53:b036a3a850e6 632 /*Send the packet*/
Wolfgang Betz 53:b036a3a850e6 633 rf_device->send((uint8_t*)buffer, 3);
Wolfgang Betz 53:b036a3a850e6 634
Wolfgang Betz 53:b036a3a850e6 635 tr_debug("%s (%d), hdr=%x, nr=%x", __func__, __LINE__, buffer[0], ptr[0]);
Wolfgang Betz 53:b036a3a850e6 636 }
Wolfgang Betz 53:b036a3a850e6 637
Wolfgang Betz 53:b036a3a850e6 638 if(signals & RF_SIG_CB_TX_DONE) {
Wolfgang Betz 53:b036a3a850e6 639 device_driver.phy_tx_done_cb(rf_radio_driver_id, mac_tx_handle, phy_status, 0, 0);
Wolfgang Betz 38:5deec3ab2025 640 #ifdef HEAVY_TRACING
Wolfgang Betz 38:5deec3ab2025 641 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 642 #endif
Wolfgang Betz 53:b036a3a850e6 643 }
Wolfgang Betz 38:5deec3ab2025 644
Wolfgang Betz 53:b036a3a850e6 645 if(signals & RF_SIG_CB_RX_RCVD) {
Wolfgang Betz 53:b036a3a850e6 646 device_driver.phy_rx_cb(rf_rx_buf, rf_buffer_len, rf_sqi, rf_rssi, rf_radio_driver_id);
Wolfgang Betz 38:5deec3ab2025 647 #ifdef HEAVY_TRACING
Wolfgang Betz 38:5deec3ab2025 648 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 649 #endif
Wolfgang Betz 53:b036a3a850e6 650 }
Wolfgang Betz 38:5deec3ab2025 651
Wolfgang Betz 53:b036a3a850e6 652 /* Release Lock */
Wolfgang Betz 53:b036a3a850e6 653 rf_if_unlock();
Wolfgang Betz 38:5deec3ab2025 654
Wolfgang Betz 38:5deec3ab2025 655 #ifdef HEAVY_TRACING
Wolfgang Betz 53:b036a3a850e6 656 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 657 #endif
Wolfgang Betz 53:b036a3a850e6 658 } while(true);
Wolfgang Betz 38:5deec3ab2025 659 }
Wolfgang Betz 38:5deec3ab2025 660
Wolfgang Betz 38:5deec3ab2025 661 void NanostackRfPhySpirit1::rf_init(void) {
Wolfgang Betz 38:5deec3ab2025 662 #ifndef NDEBUG
Wolfgang Betz 53:b036a3a850e6 663 osStatus ret;
Wolfgang Betz 38:5deec3ab2025 664 #endif
Wolfgang Betz 38:5deec3ab2025 665
Wolfgang Betz 53:b036a3a850e6 666 if(rf_device == NULL) {
Wolfgang Betz 53:b036a3a850e6 667 rf_device = &SimpleSpirit1::CreateInstance(_spi_mosi, _spi_miso, _spi_sclk, _dev_irq, _dev_cs, _dev_sdn, _brd_led);
Wolfgang Betz 53:b036a3a850e6 668 rf_device->attach_irq_callback(rf_callback_func);
Wolfgang Betz 38:5deec3ab2025 669
Wolfgang Betz 38:5deec3ab2025 670 #ifndef NDEBUG
Wolfgang Betz 53:b036a3a850e6 671 ret =
Wolfgang Betz 38:5deec3ab2025 672 #endif
Wolfgang Betz 53:b036a3a850e6 673 rf_ack_sender.start(rf_ack_loop);
Wolfgang Betz 38:5deec3ab2025 674
Wolfgang Betz 38:5deec3ab2025 675 #ifndef NDEBUG
Wolfgang Betz 53:b036a3a850e6 676 debug_if(!(ret == osOK), "\n\rassert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 677 #endif
Wolfgang Betz 53:b036a3a850e6 678 }
Wolfgang Betz 38:5deec3ab2025 679 }
Wolfgang Betz 38:5deec3ab2025 680
Wolfgang Betz 38:5deec3ab2025 681 NanostackRfPhySpirit1::NanostackRfPhySpirit1(PinName spi_mosi, PinName spi_miso, PinName spi_sclk,
Wolfgang Betz 53:b036a3a850e6 682 PinName dev_irq, PinName dev_cs, PinName dev_sdn, PinName brd_led) :
Wolfgang Betz 53:b036a3a850e6 683 _spi_mosi(spi_mosi),
Wolfgang Betz 53:b036a3a850e6 684 _spi_miso(spi_miso),
Wolfgang Betz 53:b036a3a850e6 685 _spi_sclk(spi_sclk),
Wolfgang Betz 53:b036a3a850e6 686 _dev_irq(dev_irq),
Wolfgang Betz 53:b036a3a850e6 687 _dev_cs(dev_cs),
Wolfgang Betz 53:b036a3a850e6 688 _dev_sdn(dev_sdn),
Wolfgang Betz 53:b036a3a850e6 689 _brd_led(brd_led)
Wolfgang Betz 38:5deec3ab2025 690 {
Wolfgang Betz 53:b036a3a850e6 691 /* Nothing to do */
Wolfgang Betz 53:b036a3a850e6 692 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 693 }
Wolfgang Betz 38:5deec3ab2025 694
Wolfgang Betz 38:5deec3ab2025 695 NanostackRfPhySpirit1::~NanostackRfPhySpirit1()
Wolfgang Betz 38:5deec3ab2025 696 {
Wolfgang Betz 53:b036a3a850e6 697 /* Nothing to do */
Wolfgang Betz 53:b036a3a850e6 698 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 699 }
Wolfgang Betz 38:5deec3ab2025 700
Wolfgang Betz 38:5deec3ab2025 701 int8_t NanostackRfPhySpirit1::rf_register()
Wolfgang Betz 38:5deec3ab2025 702 {
Wolfgang Betz 53:b036a3a850e6 703 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 704
Wolfgang Betz 53:b036a3a850e6 705 /* Get Lock */
Wolfgang Betz 53:b036a3a850e6 706 rf_if_lock();
Wolfgang Betz 38:5deec3ab2025 707
Wolfgang Betz 38:5deec3ab2025 708 /* Do some initialization */
Wolfgang Betz 38:5deec3ab2025 709 rf_init();
Wolfgang Betz 38:5deec3ab2025 710
Wolfgang Betz 38:5deec3ab2025 711 /* Set pointer to MAC address */
Wolfgang Betz 38:5deec3ab2025 712 device_driver.PHY_MAC = stored_mac_address;
Wolfgang Betz 38:5deec3ab2025 713
Wolfgang Betz 38:5deec3ab2025 714 /* Set driver Name */
Wolfgang Betz 38:5deec3ab2025 715 device_driver.driver_description = (char*)"Spirit1 Sub-GHz RF";
Wolfgang Betz 38:5deec3ab2025 716
Wolfgang Betz 38:5deec3ab2025 717 /*Type of RF PHY is SubGHz*/
Wolfgang Betz 38:5deec3ab2025 718 device_driver.link_type = PHY_LINK_15_4_SUBGHZ_TYPE;
Wolfgang Betz 38:5deec3ab2025 719
Wolfgang Betz 39:75481c4c6655 720 /*Maximum size of payload*/
Wolfgang Betz 38:5deec3ab2025 721 device_driver.phy_MTU = MAX_PACKET_LEN;
Wolfgang Betz 38:5deec3ab2025 722
Wolfgang Betz 38:5deec3ab2025 723 /*No header in PHY*/
Wolfgang Betz 38:5deec3ab2025 724 device_driver.phy_header_length = 0;
Wolfgang Betz 38:5deec3ab2025 725
Wolfgang Betz 38:5deec3ab2025 726 /*No tail in PHY*/
Wolfgang Betz 38:5deec3ab2025 727 device_driver.phy_tail_length = 0;
Wolfgang Betz 38:5deec3ab2025 728
Wolfgang Betz 38:5deec3ab2025 729 /*Set up driver functions*/
Wolfgang Betz 38:5deec3ab2025 730 device_driver.address_write = &rf_address_write;
Wolfgang Betz 38:5deec3ab2025 731 device_driver.extension = &rf_extension;
Wolfgang Betz 38:5deec3ab2025 732 device_driver.state_control = &rf_interface_state_control;
Wolfgang Betz 38:5deec3ab2025 733 device_driver.tx = &rf_trigger_send;
Wolfgang Betz 38:5deec3ab2025 734
Wolfgang Betz 38:5deec3ab2025 735 /*Set supported channel pages*/
Wolfgang Betz 38:5deec3ab2025 736 device_driver.phy_channel_pages = phy_channel_pages;
Wolfgang Betz 38:5deec3ab2025 737
Wolfgang Betz 38:5deec3ab2025 738 //Nullify rx/tx callbacks
Wolfgang Betz 38:5deec3ab2025 739 device_driver.phy_rx_cb = NULL;
Wolfgang Betz 38:5deec3ab2025 740 device_driver.phy_tx_done_cb = NULL;
Wolfgang Betz 38:5deec3ab2025 741 device_driver.arm_net_virtual_rx_cb = NULL;
Wolfgang Betz 38:5deec3ab2025 742 device_driver.arm_net_virtual_tx_cb = NULL;
Wolfgang Betz 38:5deec3ab2025 743
Wolfgang Betz 38:5deec3ab2025 744 /*Register device driver*/
Wolfgang Betz 38:5deec3ab2025 745 rf_radio_driver_id = arm_net_phy_register(&device_driver);
Wolfgang Betz 38:5deec3ab2025 746
Wolfgang Betz 53:b036a3a850e6 747 /* Release Lock */
Wolfgang Betz 53:b036a3a850e6 748 rf_if_unlock();
Wolfgang Betz 38:5deec3ab2025 749
Wolfgang Betz 53:b036a3a850e6 750 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 751 return rf_radio_driver_id;
Wolfgang Betz 38:5deec3ab2025 752 }
Wolfgang Betz 38:5deec3ab2025 753
Wolfgang Betz 38:5deec3ab2025 754 void NanostackRfPhySpirit1::rf_unregister()
Wolfgang Betz 38:5deec3ab2025 755 {
Wolfgang Betz 53:b036a3a850e6 756 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 757
Wolfgang Betz 53:b036a3a850e6 758 /* Get Lock */
Wolfgang Betz 53:b036a3a850e6 759 rf_if_lock();
Wolfgang Betz 38:5deec3ab2025 760
Wolfgang Betz 38:5deec3ab2025 761 if (rf_radio_driver_id >= 0) {
Wolfgang Betz 38:5deec3ab2025 762 arm_net_phy_unregister(rf_radio_driver_id);
Wolfgang Betz 38:5deec3ab2025 763 rf_radio_driver_id = -1;
Wolfgang Betz 38:5deec3ab2025 764 }
Wolfgang Betz 38:5deec3ab2025 765
Wolfgang Betz 53:b036a3a850e6 766 /* Release Lock */
Wolfgang Betz 53:b036a3a850e6 767 rf_if_unlock();
Wolfgang Betz 38:5deec3ab2025 768 }
Wolfgang Betz 38:5deec3ab2025 769
Wolfgang Betz 38:5deec3ab2025 770 void NanostackRfPhySpirit1::get_mac_address(uint8_t *mac)
Wolfgang Betz 38:5deec3ab2025 771 {
Wolfgang Betz 53:b036a3a850e6 772 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 773
Wolfgang Betz 53:b036a3a850e6 774 /* Get Lock */
Wolfgang Betz 53:b036a3a850e6 775 rf_if_lock();
Wolfgang Betz 38:5deec3ab2025 776
Wolfgang Betz 53:b036a3a850e6 777 if(rf_radio_driver_id >= 0) {
Wolfgang Betz 53:b036a3a850e6 778 rf_get_mac_address(mac);
Wolfgang Betz 53:b036a3a850e6 779 } else {
Wolfgang Betz 38:5deec3ab2025 780 error("NanostackRfPhySpirit1 must be registered to read mac address");
Wolfgang Betz 53:b036a3a850e6 781 }
Wolfgang Betz 38:5deec3ab2025 782
Wolfgang Betz 53:b036a3a850e6 783 /* Release Lock */
Wolfgang Betz 53:b036a3a850e6 784 rf_if_unlock();
Wolfgang Betz 38:5deec3ab2025 785 }
Wolfgang Betz 38:5deec3ab2025 786
Wolfgang Betz 38:5deec3ab2025 787 void NanostackRfPhySpirit1::set_mac_address(uint8_t *mac)
Wolfgang Betz 38:5deec3ab2025 788 {
Wolfgang Betz 53:b036a3a850e6 789 tr_debug("%s (%d)", __func__, __LINE__);
Wolfgang Betz 38:5deec3ab2025 790
Wolfgang Betz 53:b036a3a850e6 791 /* Get Lock */
Wolfgang Betz 53:b036a3a850e6 792 rf_if_lock();
Wolfgang Betz 38:5deec3ab2025 793
Wolfgang Betz 53:b036a3a850e6 794 if(rf_radio_driver_id < 0) {
Wolfgang Betz 53:b036a3a850e6 795 rf_set_mac_address(mac);
Wolfgang Betz 53:b036a3a850e6 796 } else {
Wolfgang Betz 38:5deec3ab2025 797 error("NanostackRfPhySpirit1 cannot change mac address when running");
Wolfgang Betz 53:b036a3a850e6 798 }
Wolfgang Betz 38:5deec3ab2025 799
Wolfgang Betz 53:b036a3a850e6 800 /* Release Lock */
Wolfgang Betz 53:b036a3a850e6 801 rf_if_unlock();
Wolfgang Betz 38:5deec3ab2025 802 }