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