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