180718 HJM : 8 Count sensing data RF send, certTest, temp(cold)Test

Fork of EV-COG-AD3029LZ by JunMo Hong

Committer:
Wolfgang Betz
Date:
Wed May 31 11:59:00 2017 +0200
Revision:
61:0e6aa2ea086f
Parent:
59:5dbfc9faf1c5
Child:
62:44ab5af08199
Exclude nanostack integration from mbed2 compilations

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