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:
Mon Jun 26 18:04:11 2017 +0200
Revision:
63:d7530f62ed93
Parent:
62:44ab5af08199
Child:
64:28ef790e4ef7
Correct handling of broadcast pan ids & addresses

Who changed what in which revision?

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