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 May 15 16:19:21 2017 +0200
Revision:
59:5dbfc9faf1c5
Parent:
58:4208689e447e
Child:
61:0e6aa2ea086f
Improve comment regarding macro `HEAVY_TRACING`

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