FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:dbad57390bd1 1 /*
ram54288 0:dbad57390bd1 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
ram54288 0:dbad57390bd1 3 * SPDX-License-Identifier: Apache-2.0
ram54288 0:dbad57390bd1 4 * Licensed under the Apache License, Version 2.0 (the License); you may
ram54288 0:dbad57390bd1 5 * not use this file except in compliance with the License.
ram54288 0:dbad57390bd1 6 * You may obtain a copy of the License at
ram54288 0:dbad57390bd1 7 *
ram54288 0:dbad57390bd1 8 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:dbad57390bd1 9 *
ram54288 0:dbad57390bd1 10 * Unless required by applicable law or agreed to in writing, software
ram54288 0:dbad57390bd1 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
ram54288 0:dbad57390bd1 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:dbad57390bd1 13 * See the License for the specific language governing permissions and
ram54288 0:dbad57390bd1 14 * limitations under the License.
ram54288 0:dbad57390bd1 15 */
ram54288 0:dbad57390bd1 16 #include <string.h>
ram54288 0:dbad57390bd1 17 #include "platform/arm_hal_interrupt.h"
ram54288 0:dbad57390bd1 18 #include "nanostack/platform/arm_hal_phy.h"
ram54288 0:dbad57390bd1 19 #include "ns_types.h"
ram54288 0:dbad57390bd1 20 #include "NanostackRfPhyAtmel.h"
ram54288 0:dbad57390bd1 21 #include "randLIB.h"
ram54288 0:dbad57390bd1 22 #include "AT86RFReg.h"
ram54288 0:dbad57390bd1 23 #include "nanostack/platform/arm_hal_phy.h"
ram54288 0:dbad57390bd1 24 #include "toolchain.h"
ram54288 0:dbad57390bd1 25
ram54288 0:dbad57390bd1 26 /*Worst case sensitivity*/
ram54288 0:dbad57390bd1 27 #define RF_DEFAULT_SENSITIVITY -88
ram54288 0:dbad57390bd1 28 /*Run calibration every 5 minutes*/
ram54288 0:dbad57390bd1 29 #define RF_CALIBRATION_INTERVAL 6000000
ram54288 0:dbad57390bd1 30 /*Wait ACK for 2.5ms*/
ram54288 0:dbad57390bd1 31 #define RF_ACK_WAIT_DEFAULT_TIMEOUT 50
ram54288 0:dbad57390bd1 32 /*Base CCA backoff (50us units) - substitutes for Inter-Frame Spacing*/
ram54288 0:dbad57390bd1 33 #define RF_CCA_BASE_BACKOFF 13 /* 650us */
ram54288 0:dbad57390bd1 34 /*CCA random backoff (50us units)*/
ram54288 0:dbad57390bd1 35 #define RF_CCA_RANDOM_BACKOFF 51 /* 2550us */
ram54288 0:dbad57390bd1 36
ram54288 0:dbad57390bd1 37 #define RF_MTU 127
ram54288 0:dbad57390bd1 38
ram54288 0:dbad57390bd1 39 #define RF_PHY_MODE OQPSK_SIN_250
ram54288 0:dbad57390bd1 40
ram54288 0:dbad57390bd1 41 /*Radio RX and TX state definitions*/
ram54288 0:dbad57390bd1 42 #define RFF_ON 0x01
ram54288 0:dbad57390bd1 43 #define RFF_RX 0x02
ram54288 0:dbad57390bd1 44 #define RFF_TX 0x04
ram54288 0:dbad57390bd1 45 #define RFF_CCA 0x08
ram54288 0:dbad57390bd1 46 #define RFF_PROT 0x10
ram54288 0:dbad57390bd1 47
ram54288 0:dbad57390bd1 48 typedef enum
ram54288 0:dbad57390bd1 49 {
ram54288 0:dbad57390bd1 50 RF_MODE_NORMAL = 0,
ram54288 0:dbad57390bd1 51 RF_MODE_SNIFFER = 1,
ram54288 0:dbad57390bd1 52 RF_MODE_ED = 2
ram54288 0:dbad57390bd1 53 }rf_mode_t;
ram54288 0:dbad57390bd1 54
ram54288 0:dbad57390bd1 55 /*Atmel RF Part Type*/
ram54288 0:dbad57390bd1 56 typedef enum
ram54288 0:dbad57390bd1 57 {
ram54288 0:dbad57390bd1 58 ATMEL_UNKNOW_DEV = 0,
ram54288 0:dbad57390bd1 59 ATMEL_AT86RF212,
ram54288 0:dbad57390bd1 60 ATMEL_AT86RF231, // No longer supported (doesn't give ED+status on frame read)
ram54288 0:dbad57390bd1 61 ATMEL_AT86RF233
ram54288 0:dbad57390bd1 62 }rf_trx_part_e;
ram54288 0:dbad57390bd1 63
ram54288 0:dbad57390bd1 64 /*Atmel RF states*/
ram54288 0:dbad57390bd1 65 typedef enum
ram54288 0:dbad57390bd1 66 {
ram54288 0:dbad57390bd1 67 NOP = 0x00,
ram54288 0:dbad57390bd1 68 BUSY_RX = 0x01,
ram54288 0:dbad57390bd1 69 RF_TX_START = 0x02,
ram54288 0:dbad57390bd1 70 FORCE_TRX_OFF = 0x03,
ram54288 0:dbad57390bd1 71 FORCE_PLL_ON = 0x04,
ram54288 0:dbad57390bd1 72 RX_ON = 0x06,
ram54288 0:dbad57390bd1 73 TRX_OFF = 0x08,
ram54288 0:dbad57390bd1 74 PLL_ON = 0x09,
ram54288 0:dbad57390bd1 75 BUSY_RX_AACK = 0x11,
ram54288 0:dbad57390bd1 76 SLEEP = 0x0F,
ram54288 0:dbad57390bd1 77 RX_AACK_ON = 0x16,
ram54288 0:dbad57390bd1 78 TX_ARET_ON = 0x19
ram54288 0:dbad57390bd1 79 }rf_trx_states_t;
ram54288 0:dbad57390bd1 80
ram54288 0:dbad57390bd1 81 static const uint8_t *rf_tx_data; // Points to Nanostack's buffer
ram54288 0:dbad57390bd1 82 static uint8_t rf_tx_length;
ram54288 0:dbad57390bd1 83 /*ACK wait duration changes depending on data rate*/
ram54288 0:dbad57390bd1 84 static uint16_t rf_ack_wait_duration = RF_ACK_WAIT_DEFAULT_TIMEOUT;
ram54288 0:dbad57390bd1 85
ram54288 0:dbad57390bd1 86 static int8_t rf_sensitivity = RF_DEFAULT_SENSITIVITY;
ram54288 0:dbad57390bd1 87 static rf_mode_t rf_mode = RF_MODE_NORMAL;
ram54288 0:dbad57390bd1 88 static uint8_t radio_tx_power = 0x00; // Default to +4dBm
ram54288 0:dbad57390bd1 89 static uint8_t rf_phy_channel = 12;
ram54288 0:dbad57390bd1 90 static uint8_t rf_tuned = 1;
ram54288 0:dbad57390bd1 91 static uint8_t rf_use_antenna_diversity = 0;
ram54288 0:dbad57390bd1 92 static int16_t expected_ack_sequence = -1;
ram54288 0:dbad57390bd1 93 static uint8_t rf_rx_mode = 0;
ram54288 0:dbad57390bd1 94 static uint8_t rf_flags = 0;
ram54288 0:dbad57390bd1 95 static int8_t rf_radio_driver_id = -1;
ram54288 0:dbad57390bd1 96 static phy_device_driver_s device_driver;
ram54288 0:dbad57390bd1 97 static uint8_t mac_tx_handle = 0;
ram54288 0:dbad57390bd1 98
ram54288 0:dbad57390bd1 99 /* Channel configurations for 2.4 and sub-GHz */
ram54288 0:dbad57390bd1 100 static const phy_rf_channel_configuration_s phy_24ghz = {2405000000U, 5000000U, 250000U, 16U, M_OQPSK};
ram54288 0:dbad57390bd1 101 static const phy_rf_channel_configuration_s phy_subghz = {868300000U, 2000000U, 250000U, 11U, M_OQPSK};
ram54288 0:dbad57390bd1 102
ram54288 0:dbad57390bd1 103 static const phy_device_channel_page_s phy_channel_pages[] = {
ram54288 0:dbad57390bd1 104 { CHANNEL_PAGE_0, &phy_24ghz},
ram54288 0:dbad57390bd1 105 { CHANNEL_PAGE_2, &phy_subghz},
ram54288 0:dbad57390bd1 106 { CHANNEL_PAGE_0, NULL}
ram54288 0:dbad57390bd1 107 };
ram54288 0:dbad57390bd1 108
ram54288 0:dbad57390bd1 109 /**
ram54288 0:dbad57390bd1 110 * RF output power write
ram54288 0:dbad57390bd1 111 *
ram54288 0:dbad57390bd1 112 * \brief TX power has to be set before network start.
ram54288 0:dbad57390bd1 113 *
ram54288 0:dbad57390bd1 114 * \param power
ram54288 0:dbad57390bd1 115 * AT86RF233
ram54288 0:dbad57390bd1 116 * 0 = 4 dBm
ram54288 0:dbad57390bd1 117 * 1 = 3.7 dBm
ram54288 0:dbad57390bd1 118 * 2 = 3.4 dBm
ram54288 0:dbad57390bd1 119 * 3 = 3 dBm
ram54288 0:dbad57390bd1 120 * 4 = 2.5 dBm
ram54288 0:dbad57390bd1 121 * 5 = 2 dBm
ram54288 0:dbad57390bd1 122 * 6 = 1 dBm
ram54288 0:dbad57390bd1 123 * 7 = 0 dBm
ram54288 0:dbad57390bd1 124 * 8 = -1 dBm
ram54288 0:dbad57390bd1 125 * 9 = -2 dBm
ram54288 0:dbad57390bd1 126 * 10 = -3 dBm
ram54288 0:dbad57390bd1 127 * 11 = -4 dBm
ram54288 0:dbad57390bd1 128 * 12 = -6 dBm
ram54288 0:dbad57390bd1 129 * 13 = -8 dBm
ram54288 0:dbad57390bd1 130 * 14 = -12 dBm
ram54288 0:dbad57390bd1 131 * 15 = -17 dBm
ram54288 0:dbad57390bd1 132 *
ram54288 0:dbad57390bd1 133 * AT86RF212B
ram54288 0:dbad57390bd1 134 * See datasheet for TX power settings
ram54288 0:dbad57390bd1 135 *
ram54288 0:dbad57390bd1 136 * \return 0, Supported Value
ram54288 0:dbad57390bd1 137 * \return -1, Not Supported Value
ram54288 0:dbad57390bd1 138 */
ram54288 0:dbad57390bd1 139 static int8_t rf_tx_power_set(uint8_t power);
ram54288 0:dbad57390bd1 140 static rf_trx_part_e rf_radio_type_read(void);
ram54288 0:dbad57390bd1 141 static void rf_ack_wait_timer_start(uint16_t slots);
ram54288 0:dbad57390bd1 142 static void rf_ack_wait_timer_stop(void);
ram54288 0:dbad57390bd1 143 static void rf_handle_cca_ed_done(void);
ram54288 0:dbad57390bd1 144 static void rf_handle_tx_end(void);
ram54288 0:dbad57390bd1 145 static void rf_handle_rx_end(void);
ram54288 0:dbad57390bd1 146 static void rf_on(void);
ram54288 0:dbad57390bd1 147 static void rf_receive(void);
ram54288 0:dbad57390bd1 148 static void rf_poll_trx_state_change(rf_trx_states_t trx_state);
ram54288 0:dbad57390bd1 149 static void rf_init(void);
ram54288 0:dbad57390bd1 150 static int8_t rf_device_register(const uint8_t *mac_addr);
ram54288 0:dbad57390bd1 151 static void rf_device_unregister(void);
ram54288 0:dbad57390bd1 152 static void rf_enable_static_frame_buffer_protection(void);
ram54288 0:dbad57390bd1 153 static void rf_disable_static_frame_buffer_protection(void);
ram54288 0:dbad57390bd1 154 static int8_t rf_start_cca(uint8_t *data_ptr, uint16_t data_length, uint8_t tx_handle, data_protocol_e data_protocol );
ram54288 0:dbad57390bd1 155 static void rf_cca_abort(void);
ram54288 0:dbad57390bd1 156 static void rf_calibration_cb(void);
ram54288 0:dbad57390bd1 157 static void rf_init_phy_mode(void);
ram54288 0:dbad57390bd1 158 static void rf_ack_wait_timer_interrupt(void);
ram54288 0:dbad57390bd1 159 static void rf_calibration_timer_interrupt(void);
ram54288 0:dbad57390bd1 160 static void rf_calibration_timer_start(uint32_t slots);
ram54288 0:dbad57390bd1 161 static void rf_cca_timer_interrupt(void);
ram54288 0:dbad57390bd1 162 static void rf_cca_timer_start(uint32_t slots);
ram54288 0:dbad57390bd1 163 static uint8_t rf_scale_lqi(int8_t rssi);
ram54288 0:dbad57390bd1 164
ram54288 0:dbad57390bd1 165 static int8_t rf_interface_state_control(phy_interface_state_e new_state, uint8_t rf_channel);
ram54288 0:dbad57390bd1 166 static int8_t rf_extension(phy_extension_type_e extension_type,uint8_t *data_ptr);
ram54288 0:dbad57390bd1 167 static int8_t rf_address_write(phy_address_type_e address_type,uint8_t *address_ptr);
ram54288 0:dbad57390bd1 168
ram54288 0:dbad57390bd1 169 static void rf_if_cca_timer_start(uint32_t slots);
ram54288 0:dbad57390bd1 170 static void rf_if_enable_promiscuous_mode(void);
ram54288 0:dbad57390bd1 171 static void rf_if_lock(void);
ram54288 0:dbad57390bd1 172 static void rf_if_unlock(void);
ram54288 0:dbad57390bd1 173 static uint8_t rf_if_read_rnd(void);
ram54288 0:dbad57390bd1 174 static void rf_if_calibration_timer_start(uint32_t slots);
ram54288 0:dbad57390bd1 175 static void rf_if_interrupt_handler(void);
ram54288 0:dbad57390bd1 176 static void rf_if_ack_wait_timer_start(uint16_t slots);
ram54288 0:dbad57390bd1 177 static void rf_if_ack_wait_timer_stop(void);
ram54288 0:dbad57390bd1 178 static void rf_if_ack_pending_ctrl(uint8_t state);
ram54288 0:dbad57390bd1 179 static void rf_if_calibration(void);
ram54288 0:dbad57390bd1 180 static uint8_t rf_if_read_register(uint8_t addr);
ram54288 0:dbad57390bd1 181 static void rf_if_set_bit(uint8_t addr, uint8_t bit, uint8_t bit_mask);
ram54288 0:dbad57390bd1 182 static void rf_if_clear_bit(uint8_t addr, uint8_t bit);
ram54288 0:dbad57390bd1 183 static void rf_if_write_register(uint8_t addr, uint8_t data);
ram54288 0:dbad57390bd1 184 static void rf_if_reset_radio(void);
ram54288 0:dbad57390bd1 185 static void rf_if_enable_ant_div(void);
ram54288 0:dbad57390bd1 186 static void rf_if_disable_ant_div(void);
ram54288 0:dbad57390bd1 187 static void rf_if_enable_slptr(void);
ram54288 0:dbad57390bd1 188 static void rf_if_disable_slptr(void);
ram54288 0:dbad57390bd1 189 static void rf_if_write_antenna_diversity_settings(void);
ram54288 0:dbad57390bd1 190 static void rf_if_write_set_tx_power_register(uint8_t value);
ram54288 0:dbad57390bd1 191 static void rf_if_write_rf_settings(void);
ram54288 0:dbad57390bd1 192 static uint8_t rf_if_check_cca(void);
ram54288 0:dbad57390bd1 193 static uint8_t rf_if_read_trx_state(void);
ram54288 0:dbad57390bd1 194 static uint16_t rf_if_read_packet(uint8_t data[RF_MTU], uint8_t *lqi_out, uint8_t *ed_out, bool *crc_good);
ram54288 0:dbad57390bd1 195 static void rf_if_write_short_addr_registers(uint8_t *short_address);
ram54288 0:dbad57390bd1 196 static uint8_t rf_if_last_acked_pending(void);
ram54288 0:dbad57390bd1 197 static void rf_if_write_pan_id_registers(uint8_t *pan_id);
ram54288 0:dbad57390bd1 198 static void rf_if_write_ieee_addr_registers(uint8_t *address);
ram54288 0:dbad57390bd1 199 static void rf_if_write_frame_buffer(const uint8_t *ptr, uint8_t length);
ram54288 0:dbad57390bd1 200 static void rf_if_change_trx_state(rf_trx_states_t trx_state);
ram54288 0:dbad57390bd1 201 static void rf_if_enable_tx_end_interrupt(void);
ram54288 0:dbad57390bd1 202 static void rf_if_enable_rx_end_interrupt(void);
ram54288 0:dbad57390bd1 203 static void rf_if_enable_cca_ed_done_interrupt(void);
ram54288 0:dbad57390bd1 204 static void rf_if_start_cca_process(void);
ram54288 0:dbad57390bd1 205 static int8_t rf_if_scale_rssi(uint8_t ed_level);
ram54288 0:dbad57390bd1 206 static void rf_if_set_channel_register(uint8_t channel);
ram54288 0:dbad57390bd1 207 static void rf_if_enable_promiscuous_mode(void);
ram54288 0:dbad57390bd1 208 static void rf_if_disable_promiscuous_mode(void);
ram54288 0:dbad57390bd1 209 static uint8_t rf_if_read_part_num(void);
ram54288 0:dbad57390bd1 210 static void rf_if_enable_irq(void);
ram54288 0:dbad57390bd1 211 static void rf_if_disable_irq(void);
ram54288 0:dbad57390bd1 212
ram54288 0:dbad57390bd1 213 #ifdef MBED_CONF_RTOS_PRESENT
ram54288 0:dbad57390bd1 214 #include "mbed.h"
ram54288 0:dbad57390bd1 215 #include "rtos.h"
ram54288 0:dbad57390bd1 216
ram54288 0:dbad57390bd1 217 static void rf_if_irq_task_process_irq();
ram54288 0:dbad57390bd1 218
ram54288 0:dbad57390bd1 219 #define SIG_RADIO 1
ram54288 0:dbad57390bd1 220 #define SIG_TIMER_ACK 2
ram54288 0:dbad57390bd1 221 #define SIG_TIMER_CAL 4
ram54288 0:dbad57390bd1 222 #define SIG_TIMER_CCA 8
ram54288 0:dbad57390bd1 223
ram54288 0:dbad57390bd1 224 #define SIG_TIMERS (SIG_TIMER_ACK|SIG_TIMER_CAL|SIG_TIMER_CCA)
ram54288 0:dbad57390bd1 225 #define SIG_ALL (SIG_RADIO|SIG_TIMERS)
ram54288 0:dbad57390bd1 226 #endif
ram54288 0:dbad57390bd1 227
ram54288 0:dbad57390bd1 228 // HW pins to RF chip
ram54288 0:dbad57390bd1 229 #define SPI_SPEED 7500000
ram54288 0:dbad57390bd1 230
ram54288 0:dbad57390bd1 231 class UnlockedSPI : public SPI {
ram54288 0:dbad57390bd1 232 public:
ram54288 0:dbad57390bd1 233 UnlockedSPI(PinName mosi, PinName miso, PinName sclk) :
ram54288 0:dbad57390bd1 234 SPI(mosi, miso, sclk) { }
ram54288 0:dbad57390bd1 235 virtual void lock() { }
ram54288 0:dbad57390bd1 236 virtual void unlock() { }
ram54288 0:dbad57390bd1 237 };
ram54288 0:dbad57390bd1 238
ram54288 0:dbad57390bd1 239 class RFBits {
ram54288 0:dbad57390bd1 240 public:
ram54288 0:dbad57390bd1 241 RFBits(PinName spi_mosi, PinName spi_miso,
ram54288 0:dbad57390bd1 242 PinName spi_sclk, PinName spi_cs,
ram54288 0:dbad57390bd1 243 PinName spi_rst, PinName spi_slp, PinName spi_irq);
ram54288 0:dbad57390bd1 244 UnlockedSPI spi;
ram54288 0:dbad57390bd1 245 DigitalOut CS;
ram54288 0:dbad57390bd1 246 DigitalOut RST;
ram54288 0:dbad57390bd1 247 DigitalOut SLP_TR;
ram54288 0:dbad57390bd1 248 InterruptIn IRQ;
ram54288 0:dbad57390bd1 249 Timeout ack_timer;
ram54288 0:dbad57390bd1 250 Timeout cal_timer;
ram54288 0:dbad57390bd1 251 Timeout cca_timer;
ram54288 0:dbad57390bd1 252 #ifdef MBED_CONF_RTOS_PRESENT
ram54288 0:dbad57390bd1 253 Thread irq_thread;
ram54288 0:dbad57390bd1 254 Mutex mutex;
ram54288 0:dbad57390bd1 255 void rf_if_irq_task();
ram54288 0:dbad57390bd1 256 #endif
ram54288 0:dbad57390bd1 257 };
ram54288 0:dbad57390bd1 258
ram54288 0:dbad57390bd1 259 RFBits::RFBits(PinName spi_mosi, PinName spi_miso,
ram54288 0:dbad57390bd1 260 PinName spi_sclk, PinName spi_cs,
ram54288 0:dbad57390bd1 261 PinName spi_rst, PinName spi_slp, PinName spi_irq)
ram54288 0:dbad57390bd1 262 : spi(spi_mosi, spi_miso, spi_sclk),
ram54288 0:dbad57390bd1 263 CS(spi_cs),
ram54288 0:dbad57390bd1 264 RST(spi_rst),
ram54288 0:dbad57390bd1 265 SLP_TR(spi_slp),
ram54288 0:dbad57390bd1 266 IRQ(spi_irq)
ram54288 0:dbad57390bd1 267 #ifdef MBED_CONF_RTOS_PRESENT
ram54288 0:dbad57390bd1 268 ,irq_thread(osPriorityRealtime, 1024)
ram54288 0:dbad57390bd1 269 #endif
ram54288 0:dbad57390bd1 270 {
ram54288 0:dbad57390bd1 271 #ifdef MBED_CONF_RTOS_PRESENT
ram54288 0:dbad57390bd1 272 irq_thread.start(mbed::callback(this, &RFBits::rf_if_irq_task));
ram54288 0:dbad57390bd1 273 #endif
ram54288 0:dbad57390bd1 274 }
ram54288 0:dbad57390bd1 275
ram54288 0:dbad57390bd1 276 static RFBits *rf;
ram54288 0:dbad57390bd1 277 static uint8_t rf_part_num = 0;
ram54288 0:dbad57390bd1 278 /*TODO: RSSI Base value setting*/
ram54288 0:dbad57390bd1 279 static int8_t rf_rssi_base_val = -91;
ram54288 0:dbad57390bd1 280
ram54288 0:dbad57390bd1 281 static uint8_t rf_if_spi_exchange(uint8_t out);
ram54288 0:dbad57390bd1 282
ram54288 0:dbad57390bd1 283 static void rf_if_lock(void)
ram54288 0:dbad57390bd1 284 {
ram54288 0:dbad57390bd1 285 platform_enter_critical();
ram54288 0:dbad57390bd1 286 }
ram54288 0:dbad57390bd1 287
ram54288 0:dbad57390bd1 288 static void rf_if_unlock(void)
ram54288 0:dbad57390bd1 289 {
ram54288 0:dbad57390bd1 290 platform_exit_critical();
ram54288 0:dbad57390bd1 291 }
ram54288 0:dbad57390bd1 292
ram54288 0:dbad57390bd1 293 #ifdef MBED_CONF_RTOS_PRESENT
ram54288 0:dbad57390bd1 294 static void rf_if_cca_timer_signal(void)
ram54288 0:dbad57390bd1 295 {
ram54288 0:dbad57390bd1 296 rf->irq_thread.signal_set(SIG_TIMER_CCA);
ram54288 0:dbad57390bd1 297 }
ram54288 0:dbad57390bd1 298
ram54288 0:dbad57390bd1 299 static void rf_if_cal_timer_signal(void)
ram54288 0:dbad57390bd1 300 {
ram54288 0:dbad57390bd1 301 rf->irq_thread.signal_set(SIG_TIMER_CAL);
ram54288 0:dbad57390bd1 302 }
ram54288 0:dbad57390bd1 303
ram54288 0:dbad57390bd1 304 static void rf_if_ack_timer_signal(void)
ram54288 0:dbad57390bd1 305 {
ram54288 0:dbad57390bd1 306 rf->irq_thread.signal_set(SIG_TIMER_ACK);
ram54288 0:dbad57390bd1 307 }
ram54288 0:dbad57390bd1 308 #endif
ram54288 0:dbad57390bd1 309
ram54288 0:dbad57390bd1 310
ram54288 0:dbad57390bd1 311 /* Delay functions for RF Chip SPI access */
ram54288 0:dbad57390bd1 312 #ifdef __CC_ARM
ram54288 0:dbad57390bd1 313 __asm static void delay_loop(uint32_t count)
ram54288 0:dbad57390bd1 314 {
ram54288 0:dbad57390bd1 315 1
ram54288 0:dbad57390bd1 316 SUBS a1, a1, #1
ram54288 0:dbad57390bd1 317 BCS %BT1
ram54288 0:dbad57390bd1 318 BX lr
ram54288 0:dbad57390bd1 319 }
ram54288 0:dbad57390bd1 320 #elif defined (__ICCARM__)
ram54288 0:dbad57390bd1 321 static void delay_loop(uint32_t count)
ram54288 0:dbad57390bd1 322 {
ram54288 0:dbad57390bd1 323 __asm volatile(
ram54288 0:dbad57390bd1 324 "loop: \n"
ram54288 0:dbad57390bd1 325 " SUBS %0, %0, #1 \n"
ram54288 0:dbad57390bd1 326 " BCS.n loop\n"
ram54288 0:dbad57390bd1 327 : "+r" (count)
ram54288 0:dbad57390bd1 328 :
ram54288 0:dbad57390bd1 329 : "cc"
ram54288 0:dbad57390bd1 330 );
ram54288 0:dbad57390bd1 331 }
ram54288 0:dbad57390bd1 332 #else // GCC
ram54288 0:dbad57390bd1 333 static void delay_loop(uint32_t count)
ram54288 0:dbad57390bd1 334 {
ram54288 0:dbad57390bd1 335 __asm__ volatile (
ram54288 0:dbad57390bd1 336 "%=:\n\t"
ram54288 0:dbad57390bd1 337 #if defined(__thumb__) && !defined(__thumb2__)
ram54288 0:dbad57390bd1 338 "SUB %0, #1\n\t"
ram54288 0:dbad57390bd1 339 #else
ram54288 0:dbad57390bd1 340 "SUBS %0, %0, #1\n\t"
ram54288 0:dbad57390bd1 341 #endif
ram54288 0:dbad57390bd1 342 "BCS %=b\n\t"
ram54288 0:dbad57390bd1 343 : "+l" (count)
ram54288 0:dbad57390bd1 344 :
ram54288 0:dbad57390bd1 345 : "cc"
ram54288 0:dbad57390bd1 346 );
ram54288 0:dbad57390bd1 347 }
ram54288 0:dbad57390bd1 348 #endif
ram54288 0:dbad57390bd1 349
ram54288 0:dbad57390bd1 350 static void delay_ns(uint32_t ns)
ram54288 0:dbad57390bd1 351 {
ram54288 0:dbad57390bd1 352 uint32_t cycles_per_us = SystemCoreClock / 1000000;
ram54288 0:dbad57390bd1 353 // Cortex-M0 takes 4 cycles per loop (SUB=1, BCS=3)
ram54288 0:dbad57390bd1 354 // Cortex-M3 and M4 takes 3 cycles per loop (SUB=1, BCS=2)
ram54288 0:dbad57390bd1 355 // Cortex-M7 - who knows?
ram54288 0:dbad57390bd1 356 // Cortex M3-M7 have "CYCCNT" - would be better than a software loop, but M0 doesn't
ram54288 0:dbad57390bd1 357 // Assume 3 cycles per loop for now - will be 33% slow on M0. No biggie,
ram54288 0:dbad57390bd1 358 // as original version of code was 300% slow on M4.
ram54288 0:dbad57390bd1 359 // [Note that this very calculation, plus call overhead, will take multiple
ram54288 0:dbad57390bd1 360 // cycles. Could well be 100ns on its own... So round down here, startup is
ram54288 0:dbad57390bd1 361 // worth at least one loop iteration.]
ram54288 0:dbad57390bd1 362 uint32_t count = (cycles_per_us * ns) / 3000;
ram54288 0:dbad57390bd1 363
ram54288 0:dbad57390bd1 364 delay_loop(count);
ram54288 0:dbad57390bd1 365 }
ram54288 0:dbad57390bd1 366
ram54288 0:dbad57390bd1 367 // t1 = 180ns, SEL falling edge to MISO active [SPI setup assumed slow enough to not need manual delay]
ram54288 0:dbad57390bd1 368 #define CS_SELECT() {rf->CS = 0; /* delay_ns(180); */}
ram54288 0:dbad57390bd1 369 // t9 = 250ns, last clock to SEL rising edge, t8 = 250ns, SPI idle time between consecutive access
ram54288 0:dbad57390bd1 370 #define CS_RELEASE() {delay_ns(250); rf->CS = 1; delay_ns(250);}
ram54288 0:dbad57390bd1 371
ram54288 0:dbad57390bd1 372 /*
ram54288 0:dbad57390bd1 373 * \brief Function sets the TX power variable.
ram54288 0:dbad57390bd1 374 *
ram54288 0:dbad57390bd1 375 * \param power TX power setting
ram54288 0:dbad57390bd1 376 *
ram54288 0:dbad57390bd1 377 * \return 0 Success
ram54288 0:dbad57390bd1 378 * \return -1 Fail
ram54288 0:dbad57390bd1 379 */
ram54288 0:dbad57390bd1 380 MBED_UNUSED static int8_t rf_tx_power_set(uint8_t power)
ram54288 0:dbad57390bd1 381 {
ram54288 0:dbad57390bd1 382 int8_t ret_val = -1;
ram54288 0:dbad57390bd1 383
ram54288 0:dbad57390bd1 384 radio_tx_power = power;
ram54288 0:dbad57390bd1 385 rf_if_lock();
ram54288 0:dbad57390bd1 386 rf_if_write_set_tx_power_register(radio_tx_power);
ram54288 0:dbad57390bd1 387 rf_if_unlock();
ram54288 0:dbad57390bd1 388 ret_val = 0;
ram54288 0:dbad57390bd1 389
ram54288 0:dbad57390bd1 390 return ret_val;
ram54288 0:dbad57390bd1 391 }
ram54288 0:dbad57390bd1 392
ram54288 0:dbad57390bd1 393 /*
ram54288 0:dbad57390bd1 394 * \brief Read connected radio part.
ram54288 0:dbad57390bd1 395 *
ram54288 0:dbad57390bd1 396 * This function only return valid information when rf_init() is called
ram54288 0:dbad57390bd1 397 *
ram54288 0:dbad57390bd1 398 * \return
ram54288 0:dbad57390bd1 399 */
ram54288 0:dbad57390bd1 400 static rf_trx_part_e rf_radio_type_read(void)
ram54288 0:dbad57390bd1 401 {
ram54288 0:dbad57390bd1 402 rf_trx_part_e ret_val = ATMEL_UNKNOW_DEV;
ram54288 0:dbad57390bd1 403
ram54288 0:dbad57390bd1 404 switch (rf_part_num)
ram54288 0:dbad57390bd1 405 {
ram54288 0:dbad57390bd1 406 case PART_AT86RF212:
ram54288 0:dbad57390bd1 407 ret_val = ATMEL_AT86RF212;
ram54288 0:dbad57390bd1 408 break;
ram54288 0:dbad57390bd1 409 case PART_AT86RF233:
ram54288 0:dbad57390bd1 410 ret_val = ATMEL_AT86RF233;
ram54288 0:dbad57390bd1 411 break;
ram54288 0:dbad57390bd1 412 default:
ram54288 0:dbad57390bd1 413 break;
ram54288 0:dbad57390bd1 414 }
ram54288 0:dbad57390bd1 415
ram54288 0:dbad57390bd1 416 return ret_val;
ram54288 0:dbad57390bd1 417 }
ram54288 0:dbad57390bd1 418
ram54288 0:dbad57390bd1 419
ram54288 0:dbad57390bd1 420 /*
ram54288 0:dbad57390bd1 421 * \brief Function starts the ACK wait timeout.
ram54288 0:dbad57390bd1 422 *
ram54288 0:dbad57390bd1 423 * \param slots Given slots, resolution 50us
ram54288 0:dbad57390bd1 424 *
ram54288 0:dbad57390bd1 425 * \return none
ram54288 0:dbad57390bd1 426 */
ram54288 0:dbad57390bd1 427 static void rf_if_ack_wait_timer_start(uint16_t slots)
ram54288 0:dbad57390bd1 428 {
ram54288 0:dbad57390bd1 429 #ifdef MBED_CONF_RTOS_PRESENT
ram54288 0:dbad57390bd1 430 rf->ack_timer.attach_us(rf_if_ack_timer_signal, slots*50);
ram54288 0:dbad57390bd1 431 #else
ram54288 0:dbad57390bd1 432 rf->ack_timer.attach_us(rf_ack_wait_timer_interrupt, slots*50);
ram54288 0:dbad57390bd1 433 #endif
ram54288 0:dbad57390bd1 434 }
ram54288 0:dbad57390bd1 435
ram54288 0:dbad57390bd1 436 /*
ram54288 0:dbad57390bd1 437 * \brief Function starts the calibration interval.
ram54288 0:dbad57390bd1 438 *
ram54288 0:dbad57390bd1 439 * \param slots Given slots, resolution 50us
ram54288 0:dbad57390bd1 440 *
ram54288 0:dbad57390bd1 441 * \return none
ram54288 0:dbad57390bd1 442 */
ram54288 0:dbad57390bd1 443 static void rf_if_calibration_timer_start(uint32_t slots)
ram54288 0:dbad57390bd1 444 {
ram54288 0:dbad57390bd1 445 #ifdef MBED_CONF_RTOS_PRESENT
ram54288 0:dbad57390bd1 446 rf->cal_timer.attach_us(rf_if_cal_timer_signal, slots*50);
ram54288 0:dbad57390bd1 447 #else
ram54288 0:dbad57390bd1 448 rf->cal_timer.attach_us(rf_calibration_timer_interrupt, slots*50);
ram54288 0:dbad57390bd1 449 #endif
ram54288 0:dbad57390bd1 450 }
ram54288 0:dbad57390bd1 451
ram54288 0:dbad57390bd1 452 /*
ram54288 0:dbad57390bd1 453 * \brief Function starts the CCA interval.
ram54288 0:dbad57390bd1 454 *
ram54288 0:dbad57390bd1 455 * \param slots Given slots, resolution 50us
ram54288 0:dbad57390bd1 456 *
ram54288 0:dbad57390bd1 457 * \return none
ram54288 0:dbad57390bd1 458 */
ram54288 0:dbad57390bd1 459 static void rf_if_cca_timer_start(uint32_t slots)
ram54288 0:dbad57390bd1 460 {
ram54288 0:dbad57390bd1 461 #ifdef MBED_CONF_RTOS_PRESENT
ram54288 0:dbad57390bd1 462 rf->cca_timer.attach_us(rf_if_cca_timer_signal, slots*50);
ram54288 0:dbad57390bd1 463 #else
ram54288 0:dbad57390bd1 464 rf->cca_timer.attach_us(rf_cca_timer_interrupt, slots*50);
ram54288 0:dbad57390bd1 465 #endif
ram54288 0:dbad57390bd1 466 }
ram54288 0:dbad57390bd1 467
ram54288 0:dbad57390bd1 468 /*
ram54288 0:dbad57390bd1 469 * \brief Function stops the CCA interval.
ram54288 0:dbad57390bd1 470 *
ram54288 0:dbad57390bd1 471 * \return none
ram54288 0:dbad57390bd1 472 */
ram54288 0:dbad57390bd1 473 static void rf_if_cca_timer_stop(void)
ram54288 0:dbad57390bd1 474 {
ram54288 0:dbad57390bd1 475 rf->cca_timer.detach();
ram54288 0:dbad57390bd1 476 }
ram54288 0:dbad57390bd1 477
ram54288 0:dbad57390bd1 478 /*
ram54288 0:dbad57390bd1 479 * \brief Function stops the ACK wait timeout.
ram54288 0:dbad57390bd1 480 *
ram54288 0:dbad57390bd1 481 * \param none
ram54288 0:dbad57390bd1 482 *
ram54288 0:dbad57390bd1 483 * \return none
ram54288 0:dbad57390bd1 484 */
ram54288 0:dbad57390bd1 485 static void rf_if_ack_wait_timer_stop(void)
ram54288 0:dbad57390bd1 486 {
ram54288 0:dbad57390bd1 487 rf->ack_timer.detach();
ram54288 0:dbad57390bd1 488 }
ram54288 0:dbad57390bd1 489
ram54288 0:dbad57390bd1 490 /*
ram54288 0:dbad57390bd1 491 * \brief Function sets bit(s) in given RF register.
ram54288 0:dbad57390bd1 492 *
ram54288 0:dbad57390bd1 493 * \param addr Address of the register to set
ram54288 0:dbad57390bd1 494 * \param bit Bit(s) to set
ram54288 0:dbad57390bd1 495 * \param bit_mask Masks the field inside the register
ram54288 0:dbad57390bd1 496 *
ram54288 0:dbad57390bd1 497 * \return none
ram54288 0:dbad57390bd1 498 */
ram54288 0:dbad57390bd1 499 static void rf_if_set_bit(uint8_t addr, uint8_t bit, uint8_t bit_mask)
ram54288 0:dbad57390bd1 500 {
ram54288 0:dbad57390bd1 501 uint8_t reg = rf_if_read_register(addr);
ram54288 0:dbad57390bd1 502 reg &= ~bit_mask;
ram54288 0:dbad57390bd1 503 reg |= bit;
ram54288 0:dbad57390bd1 504 rf_if_write_register(addr, reg);
ram54288 0:dbad57390bd1 505 }
ram54288 0:dbad57390bd1 506
ram54288 0:dbad57390bd1 507 /*
ram54288 0:dbad57390bd1 508 * \brief Function clears bit(s) in given RF register.
ram54288 0:dbad57390bd1 509 *
ram54288 0:dbad57390bd1 510 * \param addr Address of the register to clear
ram54288 0:dbad57390bd1 511 * \param bit Bit(s) to clear
ram54288 0:dbad57390bd1 512 *
ram54288 0:dbad57390bd1 513 * \return none
ram54288 0:dbad57390bd1 514 */
ram54288 0:dbad57390bd1 515 static void rf_if_clear_bit(uint8_t addr, uint8_t bit)
ram54288 0:dbad57390bd1 516 {
ram54288 0:dbad57390bd1 517 rf_if_set_bit(addr, 0, bit);
ram54288 0:dbad57390bd1 518 }
ram54288 0:dbad57390bd1 519
ram54288 0:dbad57390bd1 520 /*
ram54288 0:dbad57390bd1 521 * \brief Function writes register in RF.
ram54288 0:dbad57390bd1 522 *
ram54288 0:dbad57390bd1 523 * \param addr Address on the RF
ram54288 0:dbad57390bd1 524 * \param data Written data
ram54288 0:dbad57390bd1 525 *
ram54288 0:dbad57390bd1 526 * \return none
ram54288 0:dbad57390bd1 527 */
ram54288 0:dbad57390bd1 528 static void rf_if_write_register(uint8_t addr, uint8_t data)
ram54288 0:dbad57390bd1 529 {
ram54288 0:dbad57390bd1 530 uint8_t cmd = 0xC0;
ram54288 0:dbad57390bd1 531 CS_SELECT();
ram54288 0:dbad57390bd1 532 rf_if_spi_exchange(cmd | addr);
ram54288 0:dbad57390bd1 533 rf_if_spi_exchange(data);
ram54288 0:dbad57390bd1 534 CS_RELEASE();
ram54288 0:dbad57390bd1 535 }
ram54288 0:dbad57390bd1 536
ram54288 0:dbad57390bd1 537 /*
ram54288 0:dbad57390bd1 538 * \brief Function reads RF register.
ram54288 0:dbad57390bd1 539 *
ram54288 0:dbad57390bd1 540 * \param addr Address on the RF
ram54288 0:dbad57390bd1 541 *
ram54288 0:dbad57390bd1 542 * \return Read data
ram54288 0:dbad57390bd1 543 */
ram54288 0:dbad57390bd1 544 static uint8_t rf_if_read_register(uint8_t addr)
ram54288 0:dbad57390bd1 545 {
ram54288 0:dbad57390bd1 546 uint8_t cmd = 0x80;
ram54288 0:dbad57390bd1 547 uint8_t data;
ram54288 0:dbad57390bd1 548 CS_SELECT();
ram54288 0:dbad57390bd1 549 rf_if_spi_exchange(cmd | addr);
ram54288 0:dbad57390bd1 550 data = rf_if_spi_exchange(0);
ram54288 0:dbad57390bd1 551 CS_RELEASE();
ram54288 0:dbad57390bd1 552 return data;
ram54288 0:dbad57390bd1 553 }
ram54288 0:dbad57390bd1 554
ram54288 0:dbad57390bd1 555 /*
ram54288 0:dbad57390bd1 556 * \brief Function resets the RF.
ram54288 0:dbad57390bd1 557 *
ram54288 0:dbad57390bd1 558 * \param none
ram54288 0:dbad57390bd1 559 *
ram54288 0:dbad57390bd1 560 * \return none
ram54288 0:dbad57390bd1 561 */
ram54288 0:dbad57390bd1 562 static void rf_if_reset_radio(void)
ram54288 0:dbad57390bd1 563 {
ram54288 0:dbad57390bd1 564 rf->spi.frequency(SPI_SPEED);
ram54288 0:dbad57390bd1 565 rf->IRQ.rise(0);
ram54288 0:dbad57390bd1 566 rf->RST = 1;
ram54288 0:dbad57390bd1 567 wait_ms(1);
ram54288 0:dbad57390bd1 568 rf->RST = 0;
ram54288 0:dbad57390bd1 569 wait_ms(10);
ram54288 0:dbad57390bd1 570 CS_RELEASE();
ram54288 0:dbad57390bd1 571 rf->SLP_TR = 0;
ram54288 0:dbad57390bd1 572 wait_ms(10);
ram54288 0:dbad57390bd1 573 rf->RST = 1;
ram54288 0:dbad57390bd1 574 wait_ms(10);
ram54288 0:dbad57390bd1 575
ram54288 0:dbad57390bd1 576 rf->IRQ.rise(&rf_if_interrupt_handler);
ram54288 0:dbad57390bd1 577 }
ram54288 0:dbad57390bd1 578
ram54288 0:dbad57390bd1 579 /*
ram54288 0:dbad57390bd1 580 * \brief Function enables the promiscuous mode.
ram54288 0:dbad57390bd1 581 *
ram54288 0:dbad57390bd1 582 * \param none
ram54288 0:dbad57390bd1 583 *
ram54288 0:dbad57390bd1 584 * \return none
ram54288 0:dbad57390bd1 585 */
ram54288 0:dbad57390bd1 586 static void rf_if_enable_promiscuous_mode(void)
ram54288 0:dbad57390bd1 587 {
ram54288 0:dbad57390bd1 588 /*Set AACK_PROM_MODE to enable the promiscuous mode*/
ram54288 0:dbad57390bd1 589 rf_if_set_bit(XAH_CTRL_1, AACK_PROM_MODE, AACK_PROM_MODE);
ram54288 0:dbad57390bd1 590 }
ram54288 0:dbad57390bd1 591
ram54288 0:dbad57390bd1 592 /*
ram54288 0:dbad57390bd1 593 * \brief Function enables the promiscuous mode.
ram54288 0:dbad57390bd1 594 *
ram54288 0:dbad57390bd1 595 * \param none
ram54288 0:dbad57390bd1 596 *
ram54288 0:dbad57390bd1 597 * \return none
ram54288 0:dbad57390bd1 598 */
ram54288 0:dbad57390bd1 599 static void rf_if_disable_promiscuous_mode(void)
ram54288 0:dbad57390bd1 600 {
ram54288 0:dbad57390bd1 601 /*Set AACK_PROM_MODE to enable the promiscuous mode*/
ram54288 0:dbad57390bd1 602 rf_if_clear_bit(XAH_CTRL_1, AACK_PROM_MODE);
ram54288 0:dbad57390bd1 603 }
ram54288 0:dbad57390bd1 604
ram54288 0:dbad57390bd1 605 /*
ram54288 0:dbad57390bd1 606 * \brief Function enables the Antenna diversity usage.
ram54288 0:dbad57390bd1 607 *
ram54288 0:dbad57390bd1 608 * \param none
ram54288 0:dbad57390bd1 609 *
ram54288 0:dbad57390bd1 610 * \return none
ram54288 0:dbad57390bd1 611 */
ram54288 0:dbad57390bd1 612 static void rf_if_enable_ant_div(void)
ram54288 0:dbad57390bd1 613 {
ram54288 0:dbad57390bd1 614 /*Set ANT_EXT_SW_EN to enable controlling of antenna diversity*/
ram54288 0:dbad57390bd1 615 rf_if_set_bit(ANT_DIV, ANT_EXT_SW_EN, ANT_EXT_SW_EN);
ram54288 0:dbad57390bd1 616 }
ram54288 0:dbad57390bd1 617
ram54288 0:dbad57390bd1 618 /*
ram54288 0:dbad57390bd1 619 * \brief Function disables the Antenna diversity usage.
ram54288 0:dbad57390bd1 620 *
ram54288 0:dbad57390bd1 621 * \param none
ram54288 0:dbad57390bd1 622 *
ram54288 0:dbad57390bd1 623 * \return none
ram54288 0:dbad57390bd1 624 */
ram54288 0:dbad57390bd1 625 static void rf_if_disable_ant_div(void)
ram54288 0:dbad57390bd1 626 {
ram54288 0:dbad57390bd1 627 rf_if_clear_bit(ANT_DIV, ANT_EXT_SW_EN);
ram54288 0:dbad57390bd1 628 }
ram54288 0:dbad57390bd1 629
ram54288 0:dbad57390bd1 630 /*
ram54288 0:dbad57390bd1 631 * \brief Function sets the SLP TR pin.
ram54288 0:dbad57390bd1 632 *
ram54288 0:dbad57390bd1 633 * \param none
ram54288 0:dbad57390bd1 634 *
ram54288 0:dbad57390bd1 635 * \return none
ram54288 0:dbad57390bd1 636 */
ram54288 0:dbad57390bd1 637 static void rf_if_enable_slptr(void)
ram54288 0:dbad57390bd1 638 {
ram54288 0:dbad57390bd1 639 rf->SLP_TR = 1;
ram54288 0:dbad57390bd1 640 }
ram54288 0:dbad57390bd1 641
ram54288 0:dbad57390bd1 642 /*
ram54288 0:dbad57390bd1 643 * \brief Function clears the SLP TR pin.
ram54288 0:dbad57390bd1 644 *
ram54288 0:dbad57390bd1 645 * \param none
ram54288 0:dbad57390bd1 646 *
ram54288 0:dbad57390bd1 647 * \return none
ram54288 0:dbad57390bd1 648 */
ram54288 0:dbad57390bd1 649 static void rf_if_disable_slptr(void)
ram54288 0:dbad57390bd1 650 {
ram54288 0:dbad57390bd1 651 rf->SLP_TR = 0;
ram54288 0:dbad57390bd1 652 }
ram54288 0:dbad57390bd1 653
ram54288 0:dbad57390bd1 654 /*
ram54288 0:dbad57390bd1 655 * \brief Function writes the antenna diversity settings.
ram54288 0:dbad57390bd1 656 *
ram54288 0:dbad57390bd1 657 * \param none
ram54288 0:dbad57390bd1 658 *
ram54288 0:dbad57390bd1 659 * \return none
ram54288 0:dbad57390bd1 660 */
ram54288 0:dbad57390bd1 661 static void rf_if_write_antenna_diversity_settings(void)
ram54288 0:dbad57390bd1 662 {
ram54288 0:dbad57390bd1 663 /*Recommended setting of PDT_THRES is 3 when antenna diversity is used*/
ram54288 0:dbad57390bd1 664 rf_if_set_bit(RX_CTRL, 0x03, 0x0f);
ram54288 0:dbad57390bd1 665 rf_if_write_register(ANT_DIV, ANT_DIV_EN | ANT_EXT_SW_EN | ANT_CTRL_DEFAULT);
ram54288 0:dbad57390bd1 666 }
ram54288 0:dbad57390bd1 667
ram54288 0:dbad57390bd1 668 /*
ram54288 0:dbad57390bd1 669 * \brief Function writes the TX output power register.
ram54288 0:dbad57390bd1 670 *
ram54288 0:dbad57390bd1 671 * \param value Given register value
ram54288 0:dbad57390bd1 672 *
ram54288 0:dbad57390bd1 673 * \return none
ram54288 0:dbad57390bd1 674 */
ram54288 0:dbad57390bd1 675 static void rf_if_write_set_tx_power_register(uint8_t value)
ram54288 0:dbad57390bd1 676 {
ram54288 0:dbad57390bd1 677 rf_if_write_register(PHY_TX_PWR, value);
ram54288 0:dbad57390bd1 678 }
ram54288 0:dbad57390bd1 679
ram54288 0:dbad57390bd1 680 /*
ram54288 0:dbad57390bd1 681 * \brief Function returns the RF part number.
ram54288 0:dbad57390bd1 682 *
ram54288 0:dbad57390bd1 683 * \param none
ram54288 0:dbad57390bd1 684 *
ram54288 0:dbad57390bd1 685 * \return part number
ram54288 0:dbad57390bd1 686 */
ram54288 0:dbad57390bd1 687 static uint8_t rf_if_read_part_num(void)
ram54288 0:dbad57390bd1 688 {
ram54288 0:dbad57390bd1 689 return rf_if_read_register(PART_NUM);
ram54288 0:dbad57390bd1 690 }
ram54288 0:dbad57390bd1 691
ram54288 0:dbad57390bd1 692 /*
ram54288 0:dbad57390bd1 693 * \brief Function writes the RF settings and initialises SPI interface.
ram54288 0:dbad57390bd1 694 *
ram54288 0:dbad57390bd1 695 * \param none
ram54288 0:dbad57390bd1 696 *
ram54288 0:dbad57390bd1 697 * \return none
ram54288 0:dbad57390bd1 698 */
ram54288 0:dbad57390bd1 699 static void rf_if_write_rf_settings(void)
ram54288 0:dbad57390bd1 700 {
ram54288 0:dbad57390bd1 701 /*Reset RF module*/
ram54288 0:dbad57390bd1 702 rf_if_reset_radio();
ram54288 0:dbad57390bd1 703
ram54288 0:dbad57390bd1 704 rf_part_num = rf_if_read_part_num();
ram54288 0:dbad57390bd1 705
ram54288 0:dbad57390bd1 706 rf_if_write_register(XAH_CTRL_0,0);
ram54288 0:dbad57390bd1 707 rf_if_write_register(TRX_CTRL_1, 0x20);
ram54288 0:dbad57390bd1 708
ram54288 0:dbad57390bd1 709 /*CCA Mode - Carrier sense OR energy above threshold. Channel list is set separately*/
ram54288 0:dbad57390bd1 710 rf_if_write_register(PHY_CC_CCA, 0x05);
ram54288 0:dbad57390bd1 711
ram54288 0:dbad57390bd1 712 /*Read transceiver PART_NUM*/
ram54288 0:dbad57390bd1 713 rf_part_num = rf_if_read_register(PART_NUM);
ram54288 0:dbad57390bd1 714
ram54288 0:dbad57390bd1 715 /*Sub-GHz RF settings*/
ram54288 0:dbad57390bd1 716 if(rf_part_num == PART_AT86RF212)
ram54288 0:dbad57390bd1 717 {
ram54288 0:dbad57390bd1 718 /*GC_TX_OFFS mode-dependent setting - OQPSK*/
ram54288 0:dbad57390bd1 719 rf_if_write_register(RF_CTRL_0, 0x32);
ram54288 0:dbad57390bd1 720
ram54288 0:dbad57390bd1 721 if(rf_if_read_register(VERSION_NUM) == VERSION_AT86RF212B)
ram54288 0:dbad57390bd1 722 {
ram54288 0:dbad57390bd1 723 /*TX Output Power setting - 0 dBm North American Band*/
ram54288 0:dbad57390bd1 724 rf_if_write_register(PHY_TX_PWR, 0x03);
ram54288 0:dbad57390bd1 725 }
ram54288 0:dbad57390bd1 726 else
ram54288 0:dbad57390bd1 727 {
ram54288 0:dbad57390bd1 728 /*TX Output Power setting - 0 dBm North American Band*/
ram54288 0:dbad57390bd1 729 rf_if_write_register(PHY_TX_PWR, 0x24);
ram54288 0:dbad57390bd1 730 }
ram54288 0:dbad57390bd1 731
ram54288 0:dbad57390bd1 732 /*PHY Mode: IEEE 802.15.4-2006/2011 - OQPSK-SIN-250*/
ram54288 0:dbad57390bd1 733 rf_if_write_register(TRX_CTRL_2, RF_PHY_MODE);
ram54288 0:dbad57390bd1 734 /*Based on receiver Characteristics. See AT86RF212B Datasheet where RSSI BASE VALUE in range -97 - -100 dBm*/
ram54288 0:dbad57390bd1 735 rf_rssi_base_val = -98;
ram54288 0:dbad57390bd1 736 }
ram54288 0:dbad57390bd1 737 /*2.4GHz RF settings*/
ram54288 0:dbad57390bd1 738 else
ram54288 0:dbad57390bd1 739 {
ram54288 0:dbad57390bd1 740 #if 0
ram54288 0:dbad57390bd1 741 /* Disable power saving functions for now - can only impact reliability,
ram54288 0:dbad57390bd1 742 * and don't have any users demanding it. */
ram54288 0:dbad57390bd1 743 /*Set RPC register*/
ram54288 0:dbad57390bd1 744 rf_if_write_register(TRX_RPC, RX_RPC_CTRL|RX_RPC_EN|PLL_RPC_EN|XAH_TX_RPC_EN|IPAN_RPC_EN|TRX_RPC_RSVD_1);
ram54288 0:dbad57390bd1 745 #endif
ram54288 0:dbad57390bd1 746 /*PHY Mode: IEEE 802.15.4 - Data Rate 250 kb/s*/
ram54288 0:dbad57390bd1 747 rf_if_write_register(TRX_CTRL_2, 0);
ram54288 0:dbad57390bd1 748 rf_rssi_base_val = -91;
ram54288 0:dbad57390bd1 749 }
ram54288 0:dbad57390bd1 750 }
ram54288 0:dbad57390bd1 751
ram54288 0:dbad57390bd1 752 /*
ram54288 0:dbad57390bd1 753 * \brief Function checks the channel availability
ram54288 0:dbad57390bd1 754 *
ram54288 0:dbad57390bd1 755 * \param none
ram54288 0:dbad57390bd1 756 *
ram54288 0:dbad57390bd1 757 * \return 1 Channel clear
ram54288 0:dbad57390bd1 758 * \return 0 Channel not clear
ram54288 0:dbad57390bd1 759 */
ram54288 0:dbad57390bd1 760 static uint8_t rf_if_check_cca(void)
ram54288 0:dbad57390bd1 761 {
ram54288 0:dbad57390bd1 762 uint8_t retval = 0;
ram54288 0:dbad57390bd1 763 if(rf_if_read_register(TRX_STATUS) & CCA_STATUS)
ram54288 0:dbad57390bd1 764 {
ram54288 0:dbad57390bd1 765 retval = 1;
ram54288 0:dbad57390bd1 766 }
ram54288 0:dbad57390bd1 767 return retval;
ram54288 0:dbad57390bd1 768 }
ram54288 0:dbad57390bd1 769
ram54288 0:dbad57390bd1 770 /*
ram54288 0:dbad57390bd1 771 * \brief Function returns the RF state
ram54288 0:dbad57390bd1 772 *
ram54288 0:dbad57390bd1 773 * \param none
ram54288 0:dbad57390bd1 774 *
ram54288 0:dbad57390bd1 775 * \return RF state
ram54288 0:dbad57390bd1 776 */
ram54288 0:dbad57390bd1 777 static uint8_t rf_if_read_trx_state(void)
ram54288 0:dbad57390bd1 778 {
ram54288 0:dbad57390bd1 779 return rf_if_read_register(TRX_STATUS) & 0x1F;
ram54288 0:dbad57390bd1 780 }
ram54288 0:dbad57390bd1 781
ram54288 0:dbad57390bd1 782 /*
ram54288 0:dbad57390bd1 783 * \brief Function reads packet buffer.
ram54288 0:dbad57390bd1 784 *
ram54288 0:dbad57390bd1 785 * \param data_out Output buffer
ram54288 0:dbad57390bd1 786 * \param lqi_out LQI output
ram54288 0:dbad57390bd1 787 * \param ed_out ED output
ram54288 0:dbad57390bd1 788 * \param crc_good CRC good indication
ram54288 0:dbad57390bd1 789 *
ram54288 0:dbad57390bd1 790 * \return PSDU length [0..RF_MTU]
ram54288 0:dbad57390bd1 791 */
ram54288 0:dbad57390bd1 792 static uint16_t rf_if_read_packet(uint8_t data_out[RF_MTU], uint8_t *lqi_out, uint8_t *ed_out, bool *crc_good)
ram54288 0:dbad57390bd1 793 {
ram54288 0:dbad57390bd1 794 CS_SELECT();
ram54288 0:dbad57390bd1 795 rf_if_spi_exchange(0x20);
ram54288 0:dbad57390bd1 796 uint8_t len = rf_if_spi_exchange(0) & 0x7F;
ram54288 0:dbad57390bd1 797 uint8_t *ptr = data_out;
ram54288 0:dbad57390bd1 798 for (uint_fast8_t i = 0; i < len; i++) {
ram54288 0:dbad57390bd1 799 *ptr++ = rf_if_spi_exchange(0);
ram54288 0:dbad57390bd1 800 }
ram54288 0:dbad57390bd1 801
ram54288 0:dbad57390bd1 802 *lqi_out = rf_if_spi_exchange(0);
ram54288 0:dbad57390bd1 803 *ed_out = rf_if_spi_exchange(0);
ram54288 0:dbad57390bd1 804 *crc_good = rf_if_spi_exchange(0) & 0x80;
ram54288 0:dbad57390bd1 805 CS_RELEASE();
ram54288 0:dbad57390bd1 806
ram54288 0:dbad57390bd1 807 return len;
ram54288 0:dbad57390bd1 808 }
ram54288 0:dbad57390bd1 809
ram54288 0:dbad57390bd1 810 /*
ram54288 0:dbad57390bd1 811 * \brief Function writes RF short address registers
ram54288 0:dbad57390bd1 812 *
ram54288 0:dbad57390bd1 813 * \param short_address Given short address
ram54288 0:dbad57390bd1 814 *
ram54288 0:dbad57390bd1 815 * \return none
ram54288 0:dbad57390bd1 816 */
ram54288 0:dbad57390bd1 817 static void rf_if_write_short_addr_registers(uint8_t *short_address)
ram54288 0:dbad57390bd1 818 {
ram54288 0:dbad57390bd1 819 rf_if_write_register(SHORT_ADDR_1, *short_address++);
ram54288 0:dbad57390bd1 820 rf_if_write_register(SHORT_ADDR_0, *short_address);
ram54288 0:dbad57390bd1 821 }
ram54288 0:dbad57390bd1 822
ram54288 0:dbad57390bd1 823 /*
ram54288 0:dbad57390bd1 824 * \brief Function sets the frame pending in ACK message
ram54288 0:dbad57390bd1 825 *
ram54288 0:dbad57390bd1 826 * \param state Given frame pending state
ram54288 0:dbad57390bd1 827 *
ram54288 0:dbad57390bd1 828 * \return none
ram54288 0:dbad57390bd1 829 */
ram54288 0:dbad57390bd1 830 static void rf_if_ack_pending_ctrl(uint8_t state)
ram54288 0:dbad57390bd1 831 {
ram54288 0:dbad57390bd1 832 rf_if_lock();
ram54288 0:dbad57390bd1 833 if(state)
ram54288 0:dbad57390bd1 834 {
ram54288 0:dbad57390bd1 835 rf_if_set_bit(CSMA_SEED_1, (1 << AACK_SET_PD), (1 << AACK_SET_PD));
ram54288 0:dbad57390bd1 836 }
ram54288 0:dbad57390bd1 837 else
ram54288 0:dbad57390bd1 838 {
ram54288 0:dbad57390bd1 839 rf_if_clear_bit(CSMA_SEED_1, (1 << AACK_SET_PD));
ram54288 0:dbad57390bd1 840 }
ram54288 0:dbad57390bd1 841 rf_if_unlock();
ram54288 0:dbad57390bd1 842 }
ram54288 0:dbad57390bd1 843
ram54288 0:dbad57390bd1 844 /*
ram54288 0:dbad57390bd1 845 * \brief Function returns the state of frame pending control
ram54288 0:dbad57390bd1 846 *
ram54288 0:dbad57390bd1 847 * \param none
ram54288 0:dbad57390bd1 848 *
ram54288 0:dbad57390bd1 849 * \return Frame pending state
ram54288 0:dbad57390bd1 850 */
ram54288 0:dbad57390bd1 851 static uint8_t rf_if_last_acked_pending(void)
ram54288 0:dbad57390bd1 852 {
ram54288 0:dbad57390bd1 853 uint8_t last_acked_data_pending;
ram54288 0:dbad57390bd1 854
ram54288 0:dbad57390bd1 855 rf_if_lock();
ram54288 0:dbad57390bd1 856 if(rf_if_read_register(CSMA_SEED_1) & 0x20)
ram54288 0:dbad57390bd1 857 last_acked_data_pending = 1;
ram54288 0:dbad57390bd1 858 else
ram54288 0:dbad57390bd1 859 last_acked_data_pending = 0;
ram54288 0:dbad57390bd1 860 rf_if_unlock();
ram54288 0:dbad57390bd1 861
ram54288 0:dbad57390bd1 862 return last_acked_data_pending;
ram54288 0:dbad57390bd1 863 }
ram54288 0:dbad57390bd1 864
ram54288 0:dbad57390bd1 865 /*
ram54288 0:dbad57390bd1 866 * \brief Function calibrates the RF part.
ram54288 0:dbad57390bd1 867 *
ram54288 0:dbad57390bd1 868 * \param none
ram54288 0:dbad57390bd1 869 *
ram54288 0:dbad57390bd1 870 * \return none
ram54288 0:dbad57390bd1 871 */
ram54288 0:dbad57390bd1 872 static void rf_if_calibration(void)
ram54288 0:dbad57390bd1 873 {
ram54288 0:dbad57390bd1 874 rf_if_set_bit(FTN_CTRL, FTN_START, FTN_START);
ram54288 0:dbad57390bd1 875 /*Wait while calibration is running*/
ram54288 0:dbad57390bd1 876 while(rf_if_read_register(FTN_CTRL) & FTN_START);
ram54288 0:dbad57390bd1 877 }
ram54288 0:dbad57390bd1 878
ram54288 0:dbad57390bd1 879 /*
ram54288 0:dbad57390bd1 880 * \brief Function writes RF PAN Id registers
ram54288 0:dbad57390bd1 881 *
ram54288 0:dbad57390bd1 882 * \param pan_id Given PAN Id
ram54288 0:dbad57390bd1 883 *
ram54288 0:dbad57390bd1 884 * \return none
ram54288 0:dbad57390bd1 885 */
ram54288 0:dbad57390bd1 886 static void rf_if_write_pan_id_registers(uint8_t *pan_id)
ram54288 0:dbad57390bd1 887 {
ram54288 0:dbad57390bd1 888 rf_if_write_register(PAN_ID_1, *pan_id++);
ram54288 0:dbad57390bd1 889 rf_if_write_register(PAN_ID_0, *pan_id);
ram54288 0:dbad57390bd1 890 }
ram54288 0:dbad57390bd1 891
ram54288 0:dbad57390bd1 892 /*
ram54288 0:dbad57390bd1 893 * \brief Function writes RF IEEE Address registers
ram54288 0:dbad57390bd1 894 *
ram54288 0:dbad57390bd1 895 * \param address Given IEEE Address
ram54288 0:dbad57390bd1 896 *
ram54288 0:dbad57390bd1 897 * \return none
ram54288 0:dbad57390bd1 898 */
ram54288 0:dbad57390bd1 899 static void rf_if_write_ieee_addr_registers(uint8_t *address)
ram54288 0:dbad57390bd1 900 {
ram54288 0:dbad57390bd1 901 uint8_t i;
ram54288 0:dbad57390bd1 902 uint8_t temp = IEEE_ADDR_0;
ram54288 0:dbad57390bd1 903
ram54288 0:dbad57390bd1 904 for(i=0; i<8; i++)
ram54288 0:dbad57390bd1 905 rf_if_write_register(temp++, address[7-i]);
ram54288 0:dbad57390bd1 906 }
ram54288 0:dbad57390bd1 907
ram54288 0:dbad57390bd1 908 /*
ram54288 0:dbad57390bd1 909 * \brief Function writes data in RF frame buffer.
ram54288 0:dbad57390bd1 910 *
ram54288 0:dbad57390bd1 911 * \param ptr Pointer to data (PSDU, except FCS)
ram54288 0:dbad57390bd1 912 * \param length Pointer to length (PSDU length, minus 2 for FCS)
ram54288 0:dbad57390bd1 913 *
ram54288 0:dbad57390bd1 914 * \return none
ram54288 0:dbad57390bd1 915 */
ram54288 0:dbad57390bd1 916 static void rf_if_write_frame_buffer(const uint8_t *ptr, uint8_t length)
ram54288 0:dbad57390bd1 917 {
ram54288 0:dbad57390bd1 918 uint8_t i;
ram54288 0:dbad57390bd1 919 uint8_t cmd = 0x60;
ram54288 0:dbad57390bd1 920
ram54288 0:dbad57390bd1 921 CS_SELECT();
ram54288 0:dbad57390bd1 922 rf_if_spi_exchange(cmd);
ram54288 0:dbad57390bd1 923 rf_if_spi_exchange(length + 2);
ram54288 0:dbad57390bd1 924 for(i=0; i<length; i++)
ram54288 0:dbad57390bd1 925 rf_if_spi_exchange(*ptr++);
ram54288 0:dbad57390bd1 926
ram54288 0:dbad57390bd1 927 CS_RELEASE();
ram54288 0:dbad57390bd1 928 }
ram54288 0:dbad57390bd1 929
ram54288 0:dbad57390bd1 930 /*
ram54288 0:dbad57390bd1 931 * \brief Function returns 8-bit random value.
ram54288 0:dbad57390bd1 932 *
ram54288 0:dbad57390bd1 933 * \param none
ram54288 0:dbad57390bd1 934 *
ram54288 0:dbad57390bd1 935 * \return random value
ram54288 0:dbad57390bd1 936 */
ram54288 0:dbad57390bd1 937 static uint8_t rf_if_read_rnd(void)
ram54288 0:dbad57390bd1 938 {
ram54288 0:dbad57390bd1 939 uint8_t temp;
ram54288 0:dbad57390bd1 940 uint8_t tmp_rpc_val = 0;
ram54288 0:dbad57390bd1 941 /*RPC must be disabled while reading the random number*/
ram54288 0:dbad57390bd1 942 if(rf_part_num == PART_AT86RF233)
ram54288 0:dbad57390bd1 943 {
ram54288 0:dbad57390bd1 944 tmp_rpc_val = rf_if_read_register(TRX_RPC);
ram54288 0:dbad57390bd1 945 rf_if_write_register(TRX_RPC, RX_RPC_CTRL|TRX_RPC_RSVD_1);
ram54288 0:dbad57390bd1 946 }
ram54288 0:dbad57390bd1 947
ram54288 0:dbad57390bd1 948 wait_ms(1);
ram54288 0:dbad57390bd1 949 temp = ((rf_if_read_register(PHY_RSSI)>>5) << 6);
ram54288 0:dbad57390bd1 950 wait_ms(1);
ram54288 0:dbad57390bd1 951 temp |= ((rf_if_read_register(PHY_RSSI)>>5) << 4);
ram54288 0:dbad57390bd1 952 wait_ms(1);
ram54288 0:dbad57390bd1 953 temp |= ((rf_if_read_register(PHY_RSSI)>>5) << 2);
ram54288 0:dbad57390bd1 954 wait_ms(1);
ram54288 0:dbad57390bd1 955 temp |= ((rf_if_read_register(PHY_RSSI)>>5));
ram54288 0:dbad57390bd1 956 wait_ms(1);
ram54288 0:dbad57390bd1 957 if(rf_part_num == PART_AT86RF233)
ram54288 0:dbad57390bd1 958 rf_if_write_register(TRX_RPC, tmp_rpc_val);
ram54288 0:dbad57390bd1 959 return temp;
ram54288 0:dbad57390bd1 960 }
ram54288 0:dbad57390bd1 961
ram54288 0:dbad57390bd1 962 /*
ram54288 0:dbad57390bd1 963 * \brief Function changes the state of the RF.
ram54288 0:dbad57390bd1 964 *
ram54288 0:dbad57390bd1 965 * \param trx_state Given RF state
ram54288 0:dbad57390bd1 966 *
ram54288 0:dbad57390bd1 967 * \return none
ram54288 0:dbad57390bd1 968 */
ram54288 0:dbad57390bd1 969 static void rf_if_change_trx_state(rf_trx_states_t trx_state)
ram54288 0:dbad57390bd1 970 {
ram54288 0:dbad57390bd1 971 // XXX Lock claim apparently not required
ram54288 0:dbad57390bd1 972 rf_if_lock();
ram54288 0:dbad57390bd1 973 rf_if_write_register(TRX_STATE, trx_state);
ram54288 0:dbad57390bd1 974 /*Wait while not in desired state*/
ram54288 0:dbad57390bd1 975 rf_poll_trx_state_change(trx_state);
ram54288 0:dbad57390bd1 976 rf_if_unlock();
ram54288 0:dbad57390bd1 977 }
ram54288 0:dbad57390bd1 978
ram54288 0:dbad57390bd1 979 /*
ram54288 0:dbad57390bd1 980 * \brief Function enables the TX END interrupt
ram54288 0:dbad57390bd1 981 *
ram54288 0:dbad57390bd1 982 * \param none
ram54288 0:dbad57390bd1 983 *
ram54288 0:dbad57390bd1 984 * \return none
ram54288 0:dbad57390bd1 985 */
ram54288 0:dbad57390bd1 986 static void rf_if_enable_tx_end_interrupt(void)
ram54288 0:dbad57390bd1 987 {
ram54288 0:dbad57390bd1 988 rf_if_set_bit(IRQ_MASK, TRX_END, TRX_END);
ram54288 0:dbad57390bd1 989 }
ram54288 0:dbad57390bd1 990
ram54288 0:dbad57390bd1 991 /*
ram54288 0:dbad57390bd1 992 * \brief Function enables the RX END interrupt
ram54288 0:dbad57390bd1 993 *
ram54288 0:dbad57390bd1 994 * \param none
ram54288 0:dbad57390bd1 995 *
ram54288 0:dbad57390bd1 996 * \return none
ram54288 0:dbad57390bd1 997 */
ram54288 0:dbad57390bd1 998 static void rf_if_enable_rx_end_interrupt(void)
ram54288 0:dbad57390bd1 999 {
ram54288 0:dbad57390bd1 1000 rf_if_set_bit(IRQ_MASK, TRX_END, TRX_END);
ram54288 0:dbad57390bd1 1001 }
ram54288 0:dbad57390bd1 1002
ram54288 0:dbad57390bd1 1003 /*
ram54288 0:dbad57390bd1 1004 * \brief Function enables the CCA ED interrupt
ram54288 0:dbad57390bd1 1005 *
ram54288 0:dbad57390bd1 1006 * \param none
ram54288 0:dbad57390bd1 1007 *
ram54288 0:dbad57390bd1 1008 * \return none
ram54288 0:dbad57390bd1 1009 */
ram54288 0:dbad57390bd1 1010 static void rf_if_enable_cca_ed_done_interrupt(void)
ram54288 0:dbad57390bd1 1011 {
ram54288 0:dbad57390bd1 1012 rf_if_set_bit(IRQ_MASK, CCA_ED_DONE, CCA_ED_DONE);
ram54288 0:dbad57390bd1 1013 }
ram54288 0:dbad57390bd1 1014
ram54288 0:dbad57390bd1 1015 /*
ram54288 0:dbad57390bd1 1016 * \brief Function starts the CCA process
ram54288 0:dbad57390bd1 1017 *
ram54288 0:dbad57390bd1 1018 * \param none
ram54288 0:dbad57390bd1 1019 *
ram54288 0:dbad57390bd1 1020 * \return none
ram54288 0:dbad57390bd1 1021 */
ram54288 0:dbad57390bd1 1022 static void rf_if_start_cca_process(void)
ram54288 0:dbad57390bd1 1023 {
ram54288 0:dbad57390bd1 1024 rf_if_set_bit(PHY_CC_CCA, CCA_REQUEST, CCA_REQUEST);
ram54288 0:dbad57390bd1 1025 }
ram54288 0:dbad57390bd1 1026
ram54288 0:dbad57390bd1 1027 /*
ram54288 0:dbad57390bd1 1028 * \brief Function scales RSSI
ram54288 0:dbad57390bd1 1029 *
ram54288 0:dbad57390bd1 1030 * \param ed_level ED level read from chip
ram54288 0:dbad57390bd1 1031 *
ram54288 0:dbad57390bd1 1032 * \return appropriately scaled RSSI dBm
ram54288 0:dbad57390bd1 1033 */
ram54288 0:dbad57390bd1 1034 static int8_t rf_if_scale_rssi(uint8_t ed_level)
ram54288 0:dbad57390bd1 1035 {
ram54288 0:dbad57390bd1 1036 if (rf_part_num == PART_AT86RF212) {
ram54288 0:dbad57390bd1 1037 /* Data sheet says to multiply by 1.03 - this is 1.03125, rounding down */
ram54288 0:dbad57390bd1 1038 ed_level += ed_level >> 5;
ram54288 0:dbad57390bd1 1039 }
ram54288 0:dbad57390bd1 1040 return rf_rssi_base_val + ed_level;
ram54288 0:dbad57390bd1 1041 }
ram54288 0:dbad57390bd1 1042
ram54288 0:dbad57390bd1 1043 /*
ram54288 0:dbad57390bd1 1044 * \brief Function sets the RF channel field
ram54288 0:dbad57390bd1 1045 *
ram54288 0:dbad57390bd1 1046 * \param Given channel
ram54288 0:dbad57390bd1 1047 *
ram54288 0:dbad57390bd1 1048 * \return none
ram54288 0:dbad57390bd1 1049 */
ram54288 0:dbad57390bd1 1050 static void rf_if_set_channel_register(uint8_t channel)
ram54288 0:dbad57390bd1 1051 {
ram54288 0:dbad57390bd1 1052 rf_if_set_bit(PHY_CC_CCA, channel, 0x1f);
ram54288 0:dbad57390bd1 1053 }
ram54288 0:dbad57390bd1 1054
ram54288 0:dbad57390bd1 1055 /*
ram54288 0:dbad57390bd1 1056 * \brief Function enables RF irq pin interrupts in RF interface.
ram54288 0:dbad57390bd1 1057 *
ram54288 0:dbad57390bd1 1058 * \param none
ram54288 0:dbad57390bd1 1059 *
ram54288 0:dbad57390bd1 1060 * \return none
ram54288 0:dbad57390bd1 1061 */
ram54288 0:dbad57390bd1 1062 static void rf_if_enable_irq(void)
ram54288 0:dbad57390bd1 1063 {
ram54288 0:dbad57390bd1 1064 rf->IRQ.enable_irq();
ram54288 0:dbad57390bd1 1065 }
ram54288 0:dbad57390bd1 1066
ram54288 0:dbad57390bd1 1067 /*
ram54288 0:dbad57390bd1 1068 * \brief Function disables RF irq pin interrupts in RF interface.
ram54288 0:dbad57390bd1 1069 *
ram54288 0:dbad57390bd1 1070 * \param none
ram54288 0:dbad57390bd1 1071 *
ram54288 0:dbad57390bd1 1072 * \return none
ram54288 0:dbad57390bd1 1073 */
ram54288 0:dbad57390bd1 1074 static void rf_if_disable_irq(void)
ram54288 0:dbad57390bd1 1075 {
ram54288 0:dbad57390bd1 1076 rf->IRQ.disable_irq();
ram54288 0:dbad57390bd1 1077 }
ram54288 0:dbad57390bd1 1078
ram54288 0:dbad57390bd1 1079 #ifdef MBED_CONF_RTOS_PRESENT
ram54288 0:dbad57390bd1 1080 static void rf_if_interrupt_handler(void)
ram54288 0:dbad57390bd1 1081 {
ram54288 0:dbad57390bd1 1082 rf->irq_thread.signal_set(SIG_RADIO);
ram54288 0:dbad57390bd1 1083 }
ram54288 0:dbad57390bd1 1084
ram54288 0:dbad57390bd1 1085 // Started during construction of rf, so variable
ram54288 0:dbad57390bd1 1086 // rf isn't set at the start. Uses 'this' instead.
ram54288 0:dbad57390bd1 1087 void RFBits::rf_if_irq_task(void)
ram54288 0:dbad57390bd1 1088 {
ram54288 0:dbad57390bd1 1089 for (;;) {
ram54288 0:dbad57390bd1 1090 osEvent event = irq_thread.signal_wait(0);
ram54288 0:dbad57390bd1 1091 if (event.status != osEventSignal) {
ram54288 0:dbad57390bd1 1092 continue;
ram54288 0:dbad57390bd1 1093 }
ram54288 0:dbad57390bd1 1094 rf_if_lock();
ram54288 0:dbad57390bd1 1095 if (event.value.signals & SIG_RADIO) {
ram54288 0:dbad57390bd1 1096 rf_if_irq_task_process_irq();
ram54288 0:dbad57390bd1 1097 }
ram54288 0:dbad57390bd1 1098 if (event.value.signals & SIG_TIMER_ACK) {
ram54288 0:dbad57390bd1 1099 rf_ack_wait_timer_interrupt();
ram54288 0:dbad57390bd1 1100 }
ram54288 0:dbad57390bd1 1101 if (event.value.signals & SIG_TIMER_CCA) {
ram54288 0:dbad57390bd1 1102 rf_cca_timer_interrupt();
ram54288 0:dbad57390bd1 1103 }
ram54288 0:dbad57390bd1 1104 if (event.value.signals & SIG_TIMER_CAL) {
ram54288 0:dbad57390bd1 1105 rf_calibration_timer_interrupt();
ram54288 0:dbad57390bd1 1106 }
ram54288 0:dbad57390bd1 1107 rf_if_unlock();
ram54288 0:dbad57390bd1 1108 }
ram54288 0:dbad57390bd1 1109 }
ram54288 0:dbad57390bd1 1110
ram54288 0:dbad57390bd1 1111 static void rf_if_irq_task_process_irq(void)
ram54288 0:dbad57390bd1 1112 #else
ram54288 0:dbad57390bd1 1113 /*
ram54288 0:dbad57390bd1 1114 * \brief Function is a RF interrupt vector. End of frame in RX and TX are handled here as well as CCA process interrupt.
ram54288 0:dbad57390bd1 1115 *
ram54288 0:dbad57390bd1 1116 * \param none
ram54288 0:dbad57390bd1 1117 *
ram54288 0:dbad57390bd1 1118 * \return none
ram54288 0:dbad57390bd1 1119 */
ram54288 0:dbad57390bd1 1120 static void rf_if_interrupt_handler(void)
ram54288 0:dbad57390bd1 1121 #endif
ram54288 0:dbad57390bd1 1122 {
ram54288 0:dbad57390bd1 1123 uint8_t irq_status;
ram54288 0:dbad57390bd1 1124
ram54288 0:dbad57390bd1 1125 /*Read interrupt flag*/
ram54288 0:dbad57390bd1 1126 irq_status = rf_if_read_register(IRQ_STATUS);
ram54288 0:dbad57390bd1 1127
ram54288 0:dbad57390bd1 1128 /*Disable interrupt on RF*/
ram54288 0:dbad57390bd1 1129 rf_if_clear_bit(IRQ_MASK, irq_status);
ram54288 0:dbad57390bd1 1130 /*RX start interrupt*/
ram54288 0:dbad57390bd1 1131 if(irq_status & RX_START)
ram54288 0:dbad57390bd1 1132 {
ram54288 0:dbad57390bd1 1133 }
ram54288 0:dbad57390bd1 1134 /*Address matching interrupt*/
ram54288 0:dbad57390bd1 1135 if(irq_status & AMI)
ram54288 0:dbad57390bd1 1136 {
ram54288 0:dbad57390bd1 1137 }
ram54288 0:dbad57390bd1 1138 if(irq_status & TRX_UR)
ram54288 0:dbad57390bd1 1139 {
ram54288 0:dbad57390bd1 1140 }
ram54288 0:dbad57390bd1 1141 /*Frame end interrupt (RX and TX)*/
ram54288 0:dbad57390bd1 1142 if(irq_status & TRX_END)
ram54288 0:dbad57390bd1 1143 {
ram54288 0:dbad57390bd1 1144 /*TX done interrupt*/
ram54288 0:dbad57390bd1 1145 if(rf_if_read_trx_state() == PLL_ON || rf_if_read_trx_state() == TX_ARET_ON)
ram54288 0:dbad57390bd1 1146 {
ram54288 0:dbad57390bd1 1147 rf_handle_tx_end();
ram54288 0:dbad57390bd1 1148 }
ram54288 0:dbad57390bd1 1149 /*Frame received interrupt*/
ram54288 0:dbad57390bd1 1150 else
ram54288 0:dbad57390bd1 1151 {
ram54288 0:dbad57390bd1 1152 rf_handle_rx_end();
ram54288 0:dbad57390bd1 1153 }
ram54288 0:dbad57390bd1 1154 }
ram54288 0:dbad57390bd1 1155 if(irq_status & CCA_ED_DONE)
ram54288 0:dbad57390bd1 1156 {
ram54288 0:dbad57390bd1 1157 rf_handle_cca_ed_done();
ram54288 0:dbad57390bd1 1158 }
ram54288 0:dbad57390bd1 1159 }
ram54288 0:dbad57390bd1 1160
ram54288 0:dbad57390bd1 1161 /*
ram54288 0:dbad57390bd1 1162 * \brief Function writes/read data in SPI interface
ram54288 0:dbad57390bd1 1163 *
ram54288 0:dbad57390bd1 1164 * \param out Output data
ram54288 0:dbad57390bd1 1165 *
ram54288 0:dbad57390bd1 1166 * \return Input data
ram54288 0:dbad57390bd1 1167 */
ram54288 0:dbad57390bd1 1168 static uint8_t rf_if_spi_exchange(uint8_t out)
ram54288 0:dbad57390bd1 1169 {
ram54288 0:dbad57390bd1 1170 uint8_t v;
ram54288 0:dbad57390bd1 1171 v = rf->spi.write(out);
ram54288 0:dbad57390bd1 1172 // t9 = t5 = 250ns, delay between LSB of last byte to next MSB or delay between LSB & SEL rising
ram54288 0:dbad57390bd1 1173 // [SPI setup assumed slow enough to not need manual delay]
ram54288 0:dbad57390bd1 1174 // delay_ns(250);
ram54288 0:dbad57390bd1 1175 return v;
ram54288 0:dbad57390bd1 1176 }
ram54288 0:dbad57390bd1 1177
ram54288 0:dbad57390bd1 1178 /*
ram54288 0:dbad57390bd1 1179 * \brief Function sets given RF flag on.
ram54288 0:dbad57390bd1 1180 *
ram54288 0:dbad57390bd1 1181 * \param x Given RF flag
ram54288 0:dbad57390bd1 1182 *
ram54288 0:dbad57390bd1 1183 * \return none
ram54288 0:dbad57390bd1 1184 */
ram54288 0:dbad57390bd1 1185 static void rf_flags_set(uint8_t x)
ram54288 0:dbad57390bd1 1186 {
ram54288 0:dbad57390bd1 1187 rf_flags |= x;
ram54288 0:dbad57390bd1 1188 }
ram54288 0:dbad57390bd1 1189
ram54288 0:dbad57390bd1 1190 /*
ram54288 0:dbad57390bd1 1191 * \brief Function clears given RF flag on.
ram54288 0:dbad57390bd1 1192 *
ram54288 0:dbad57390bd1 1193 * \param x Given RF flag
ram54288 0:dbad57390bd1 1194 *
ram54288 0:dbad57390bd1 1195 * \return none
ram54288 0:dbad57390bd1 1196 */
ram54288 0:dbad57390bd1 1197 static void rf_flags_clear(uint8_t x)
ram54288 0:dbad57390bd1 1198 {
ram54288 0:dbad57390bd1 1199 rf_flags &= ~x;
ram54288 0:dbad57390bd1 1200 }
ram54288 0:dbad57390bd1 1201
ram54288 0:dbad57390bd1 1202 /*
ram54288 0:dbad57390bd1 1203 * \brief Function checks if given RF flag is on.
ram54288 0:dbad57390bd1 1204 *
ram54288 0:dbad57390bd1 1205 * \param x Given RF flag
ram54288 0:dbad57390bd1 1206 *
ram54288 0:dbad57390bd1 1207 * \return states of the given flags
ram54288 0:dbad57390bd1 1208 */
ram54288 0:dbad57390bd1 1209 static uint8_t rf_flags_check(uint8_t x)
ram54288 0:dbad57390bd1 1210 {
ram54288 0:dbad57390bd1 1211 return (rf_flags & x);
ram54288 0:dbad57390bd1 1212 }
ram54288 0:dbad57390bd1 1213
ram54288 0:dbad57390bd1 1214 /*
ram54288 0:dbad57390bd1 1215 * \brief Function clears all RF flags.
ram54288 0:dbad57390bd1 1216 *
ram54288 0:dbad57390bd1 1217 * \param none
ram54288 0:dbad57390bd1 1218 *
ram54288 0:dbad57390bd1 1219 * \return none
ram54288 0:dbad57390bd1 1220 */
ram54288 0:dbad57390bd1 1221 static void rf_flags_reset(void)
ram54288 0:dbad57390bd1 1222 {
ram54288 0:dbad57390bd1 1223 rf_flags = 0;
ram54288 0:dbad57390bd1 1224 }
ram54288 0:dbad57390bd1 1225
ram54288 0:dbad57390bd1 1226 /*
ram54288 0:dbad57390bd1 1227 * \brief Function initialises and registers the RF driver.
ram54288 0:dbad57390bd1 1228 *
ram54288 0:dbad57390bd1 1229 * \param none
ram54288 0:dbad57390bd1 1230 *
ram54288 0:dbad57390bd1 1231 * \return rf_radio_driver_id Driver ID given by NET library
ram54288 0:dbad57390bd1 1232 */
ram54288 0:dbad57390bd1 1233 static int8_t rf_device_register(const uint8_t *mac_addr)
ram54288 0:dbad57390bd1 1234 {
ram54288 0:dbad57390bd1 1235 rf_trx_part_e radio_type;
ram54288 0:dbad57390bd1 1236
ram54288 0:dbad57390bd1 1237 rf_init();
ram54288 0:dbad57390bd1 1238
ram54288 0:dbad57390bd1 1239 radio_type = rf_radio_type_read();
ram54288 0:dbad57390bd1 1240 if(radio_type != ATMEL_UNKNOW_DEV)
ram54288 0:dbad57390bd1 1241 {
ram54288 0:dbad57390bd1 1242 /*Set pointer to MAC address*/
ram54288 0:dbad57390bd1 1243 device_driver.PHY_MAC = (uint8_t *)mac_addr;
ram54288 0:dbad57390bd1 1244 device_driver.driver_description = (char*)"ATMEL_MAC";
ram54288 0:dbad57390bd1 1245 //Create setup Used Radio chips
ram54288 0:dbad57390bd1 1246 if(radio_type == ATMEL_AT86RF212)
ram54288 0:dbad57390bd1 1247 {
ram54288 0:dbad57390bd1 1248 device_driver.link_type = PHY_LINK_15_4_SUBGHZ_TYPE;
ram54288 0:dbad57390bd1 1249 }
ram54288 0:dbad57390bd1 1250 else
ram54288 0:dbad57390bd1 1251 {
ram54288 0:dbad57390bd1 1252 device_driver.link_type = PHY_LINK_15_4_2_4GHZ_TYPE;
ram54288 0:dbad57390bd1 1253 }
ram54288 0:dbad57390bd1 1254 device_driver.phy_channel_pages = phy_channel_pages;
ram54288 0:dbad57390bd1 1255 /*Maximum size of payload is 127*/
ram54288 0:dbad57390bd1 1256 device_driver.phy_MTU = 127;
ram54288 0:dbad57390bd1 1257 /*No header in PHY*/
ram54288 0:dbad57390bd1 1258 device_driver.phy_header_length = 0;
ram54288 0:dbad57390bd1 1259 /*No tail in PHY*/
ram54288 0:dbad57390bd1 1260 device_driver.phy_tail_length = 0;
ram54288 0:dbad57390bd1 1261 /*Set address write function*/
ram54288 0:dbad57390bd1 1262 device_driver.address_write = &rf_address_write;
ram54288 0:dbad57390bd1 1263 /*Set RF extension function*/
ram54288 0:dbad57390bd1 1264 device_driver.extension = &rf_extension;
ram54288 0:dbad57390bd1 1265 /*Set RF state control function*/
ram54288 0:dbad57390bd1 1266 device_driver.state_control = &rf_interface_state_control;
ram54288 0:dbad57390bd1 1267 /*Set transmit function*/
ram54288 0:dbad57390bd1 1268 device_driver.tx = &rf_start_cca;
ram54288 0:dbad57390bd1 1269 /*NULLIFY rx and tx_done callbacks*/
ram54288 0:dbad57390bd1 1270 device_driver.phy_rx_cb = NULL;
ram54288 0:dbad57390bd1 1271 device_driver.phy_tx_done_cb = NULL;
ram54288 0:dbad57390bd1 1272 /*Register device driver*/
ram54288 0:dbad57390bd1 1273 rf_radio_driver_id = arm_net_phy_register(&device_driver);
ram54288 0:dbad57390bd1 1274 }
ram54288 0:dbad57390bd1 1275 return rf_radio_driver_id;
ram54288 0:dbad57390bd1 1276 }
ram54288 0:dbad57390bd1 1277
ram54288 0:dbad57390bd1 1278 /*
ram54288 0:dbad57390bd1 1279 * \brief Function unregisters the RF driver.
ram54288 0:dbad57390bd1 1280 *
ram54288 0:dbad57390bd1 1281 * \param none
ram54288 0:dbad57390bd1 1282 *
ram54288 0:dbad57390bd1 1283 * \return none
ram54288 0:dbad57390bd1 1284 */
ram54288 0:dbad57390bd1 1285 static void rf_device_unregister()
ram54288 0:dbad57390bd1 1286 {
ram54288 0:dbad57390bd1 1287 if (rf_radio_driver_id >= 0) {
ram54288 0:dbad57390bd1 1288 arm_net_phy_unregister(rf_radio_driver_id);
ram54288 0:dbad57390bd1 1289 rf_radio_driver_id = -1;
ram54288 0:dbad57390bd1 1290 }
ram54288 0:dbad57390bd1 1291 }
ram54288 0:dbad57390bd1 1292
ram54288 0:dbad57390bd1 1293 /*
ram54288 0:dbad57390bd1 1294 * \brief Enable frame buffer protection
ram54288 0:dbad57390bd1 1295 *
ram54288 0:dbad57390bd1 1296 * If protection is enabled, reception cannot start - the radio will
ram54288 0:dbad57390bd1 1297 * not go into RX_BUSY or write into the frame buffer if in receive mode.
ram54288 0:dbad57390bd1 1298 * Setting this won't abort an already-started reception.
ram54288 0:dbad57390bd1 1299 * We can still write the frame buffer ourselves.
ram54288 0:dbad57390bd1 1300 */
ram54288 0:dbad57390bd1 1301 static void rf_enable_static_frame_buffer_protection(void)
ram54288 0:dbad57390bd1 1302 {
ram54288 0:dbad57390bd1 1303 if (!rf_flags_check(RFF_PROT)) {
ram54288 0:dbad57390bd1 1304 /* This also writes RX_PDT_LEVEL to 0 - maximum RX sensitivity */
ram54288 0:dbad57390bd1 1305 /* Would need to modify this function if messing with that */
ram54288 0:dbad57390bd1 1306 rf_if_write_register(RX_SYN, RX_PDT_DIS);
ram54288 0:dbad57390bd1 1307 rf_flags_set(RFF_PROT);
ram54288 0:dbad57390bd1 1308 }
ram54288 0:dbad57390bd1 1309 }
ram54288 0:dbad57390bd1 1310
ram54288 0:dbad57390bd1 1311 /*
ram54288 0:dbad57390bd1 1312 * \brief Disable frame buffer protection
ram54288 0:dbad57390bd1 1313 */
ram54288 0:dbad57390bd1 1314 static void rf_disable_static_frame_buffer_protection(void)
ram54288 0:dbad57390bd1 1315 {
ram54288 0:dbad57390bd1 1316 if (rf_flags_check(RFF_PROT)) {
ram54288 0:dbad57390bd1 1317 /* This also writes RX_PDT_LEVEL to 0 - maximum RX sensitivity */
ram54288 0:dbad57390bd1 1318 /* Would need to modify this function if messing with that */
ram54288 0:dbad57390bd1 1319 rf_if_write_register(RX_SYN, 0);
ram54288 0:dbad57390bd1 1320 rf_flags_clear(RFF_PROT);
ram54288 0:dbad57390bd1 1321 }
ram54288 0:dbad57390bd1 1322 }
ram54288 0:dbad57390bd1 1323
ram54288 0:dbad57390bd1 1324
ram54288 0:dbad57390bd1 1325 /*
ram54288 0:dbad57390bd1 1326 * \brief Function is a call back for ACK wait timeout.
ram54288 0:dbad57390bd1 1327 *
ram54288 0:dbad57390bd1 1328 * \param none
ram54288 0:dbad57390bd1 1329 *
ram54288 0:dbad57390bd1 1330 * \return none
ram54288 0:dbad57390bd1 1331 */
ram54288 0:dbad57390bd1 1332 static void rf_ack_wait_timer_interrupt(void)
ram54288 0:dbad57390bd1 1333 {
ram54288 0:dbad57390bd1 1334 rf_if_lock();
ram54288 0:dbad57390bd1 1335 expected_ack_sequence = -1;
ram54288 0:dbad57390bd1 1336 /*Force PLL state*/
ram54288 0:dbad57390bd1 1337 rf_if_change_trx_state(FORCE_PLL_ON);
ram54288 0:dbad57390bd1 1338 rf_poll_trx_state_change(PLL_ON);
ram54288 0:dbad57390bd1 1339 /*Start receiver in RX_AACK_ON state*/
ram54288 0:dbad57390bd1 1340 rf_rx_mode = 0;
ram54288 0:dbad57390bd1 1341 rf_flags_clear(RFF_RX);
ram54288 0:dbad57390bd1 1342 rf_receive();
ram54288 0:dbad57390bd1 1343 rf_if_unlock();
ram54288 0:dbad57390bd1 1344 }
ram54288 0:dbad57390bd1 1345
ram54288 0:dbad57390bd1 1346 /*
ram54288 0:dbad57390bd1 1347 * \brief Function is a call back for calibration interval timer.
ram54288 0:dbad57390bd1 1348 *
ram54288 0:dbad57390bd1 1349 * \param none
ram54288 0:dbad57390bd1 1350 *
ram54288 0:dbad57390bd1 1351 * \return none
ram54288 0:dbad57390bd1 1352 */
ram54288 0:dbad57390bd1 1353 static void rf_calibration_timer_interrupt(void)
ram54288 0:dbad57390bd1 1354 {
ram54288 0:dbad57390bd1 1355 /*Calibrate RF*/
ram54288 0:dbad57390bd1 1356 rf_calibration_cb();
ram54288 0:dbad57390bd1 1357 /*Start new calibration timeout*/
ram54288 0:dbad57390bd1 1358 rf_calibration_timer_start(RF_CALIBRATION_INTERVAL);
ram54288 0:dbad57390bd1 1359 }
ram54288 0:dbad57390bd1 1360
ram54288 0:dbad57390bd1 1361 /*
ram54288 0:dbad57390bd1 1362 * \brief Function is a call back for cca interval timer.
ram54288 0:dbad57390bd1 1363 *
ram54288 0:dbad57390bd1 1364 * \param none
ram54288 0:dbad57390bd1 1365 *
ram54288 0:dbad57390bd1 1366 * \return none
ram54288 0:dbad57390bd1 1367 */
ram54288 0:dbad57390bd1 1368 static void rf_cca_timer_interrupt(void)
ram54288 0:dbad57390bd1 1369 {
ram54288 0:dbad57390bd1 1370 /*Disable reception - locks against entering BUSY_RX and overwriting frame buffer*/
ram54288 0:dbad57390bd1 1371 rf_enable_static_frame_buffer_protection();
ram54288 0:dbad57390bd1 1372
ram54288 0:dbad57390bd1 1373 if(rf_if_read_trx_state() == BUSY_RX_AACK)
ram54288 0:dbad57390bd1 1374 {
ram54288 0:dbad57390bd1 1375 /*Reception already started - re-enable reception and say CCA fail*/
ram54288 0:dbad57390bd1 1376 rf_disable_static_frame_buffer_protection();
ram54288 0:dbad57390bd1 1377 if(device_driver.phy_tx_done_cb){
ram54288 0:dbad57390bd1 1378 device_driver.phy_tx_done_cb(rf_radio_driver_id, mac_tx_handle, PHY_LINK_CCA_FAIL, 0, 0);
ram54288 0:dbad57390bd1 1379 }
ram54288 0:dbad57390bd1 1380 }
ram54288 0:dbad57390bd1 1381 else
ram54288 0:dbad57390bd1 1382 {
ram54288 0:dbad57390bd1 1383 /*Load the frame buffer with frame to transmit */
ram54288 0:dbad57390bd1 1384 rf_if_write_frame_buffer(rf_tx_data, rf_tx_length);
ram54288 0:dbad57390bd1 1385 /*Make sure we're in RX state to read channel (any way we could not be?)*/
ram54288 0:dbad57390bd1 1386 rf_receive();
ram54288 0:dbad57390bd1 1387 rf_flags_set(RFF_CCA);
ram54288 0:dbad57390bd1 1388 /*Start CCA process*/
ram54288 0:dbad57390bd1 1389 rf_if_enable_cca_ed_done_interrupt();
ram54288 0:dbad57390bd1 1390 rf_if_start_cca_process();
ram54288 0:dbad57390bd1 1391 }
ram54288 0:dbad57390bd1 1392 }
ram54288 0:dbad57390bd1 1393
ram54288 0:dbad57390bd1 1394 /*
ram54288 0:dbad57390bd1 1395 * \brief Function starts the ACK wait timeout.
ram54288 0:dbad57390bd1 1396 *
ram54288 0:dbad57390bd1 1397 * \param slots Given slots, resolution 50us
ram54288 0:dbad57390bd1 1398 *
ram54288 0:dbad57390bd1 1399 * \return none
ram54288 0:dbad57390bd1 1400 */
ram54288 0:dbad57390bd1 1401 static void rf_ack_wait_timer_start(uint16_t slots)
ram54288 0:dbad57390bd1 1402 {
ram54288 0:dbad57390bd1 1403 rf_if_ack_wait_timer_start(slots);
ram54288 0:dbad57390bd1 1404 }
ram54288 0:dbad57390bd1 1405
ram54288 0:dbad57390bd1 1406 /*
ram54288 0:dbad57390bd1 1407 * \brief Function starts the calibration interval.
ram54288 0:dbad57390bd1 1408 *
ram54288 0:dbad57390bd1 1409 * \param slots Given slots, resolution 50us
ram54288 0:dbad57390bd1 1410 *
ram54288 0:dbad57390bd1 1411 * \return none
ram54288 0:dbad57390bd1 1412 */
ram54288 0:dbad57390bd1 1413 static void rf_calibration_timer_start(uint32_t slots)
ram54288 0:dbad57390bd1 1414 {
ram54288 0:dbad57390bd1 1415 rf_if_calibration_timer_start(slots);
ram54288 0:dbad57390bd1 1416 }
ram54288 0:dbad57390bd1 1417
ram54288 0:dbad57390bd1 1418 /*
ram54288 0:dbad57390bd1 1419 * \brief Function starts the CCA backoff.
ram54288 0:dbad57390bd1 1420 *
ram54288 0:dbad57390bd1 1421 * \param slots Given slots, resolution 50us
ram54288 0:dbad57390bd1 1422 *
ram54288 0:dbad57390bd1 1423 * \return none
ram54288 0:dbad57390bd1 1424 */
ram54288 0:dbad57390bd1 1425 static void rf_cca_timer_start(uint32_t slots)
ram54288 0:dbad57390bd1 1426 {
ram54288 0:dbad57390bd1 1427 rf_if_cca_timer_start(slots);
ram54288 0:dbad57390bd1 1428 }
ram54288 0:dbad57390bd1 1429
ram54288 0:dbad57390bd1 1430 /*
ram54288 0:dbad57390bd1 1431 * \brief Function stops the CCA backoff.
ram54288 0:dbad57390bd1 1432 *
ram54288 0:dbad57390bd1 1433 * \return none
ram54288 0:dbad57390bd1 1434 */
ram54288 0:dbad57390bd1 1435 static void rf_cca_timer_stop(void)
ram54288 0:dbad57390bd1 1436 {
ram54288 0:dbad57390bd1 1437 rf_if_cca_timer_stop();
ram54288 0:dbad57390bd1 1438 }
ram54288 0:dbad57390bd1 1439
ram54288 0:dbad57390bd1 1440 /*
ram54288 0:dbad57390bd1 1441 * \brief Function stops the ACK wait timeout.
ram54288 0:dbad57390bd1 1442 *
ram54288 0:dbad57390bd1 1443 * \param none
ram54288 0:dbad57390bd1 1444 *
ram54288 0:dbad57390bd1 1445 * \return none
ram54288 0:dbad57390bd1 1446 */
ram54288 0:dbad57390bd1 1447 static void rf_ack_wait_timer_stop(void)
ram54288 0:dbad57390bd1 1448 {
ram54288 0:dbad57390bd1 1449 rf_if_ack_wait_timer_stop();
ram54288 0:dbad57390bd1 1450 }
ram54288 0:dbad57390bd1 1451
ram54288 0:dbad57390bd1 1452 /*
ram54288 0:dbad57390bd1 1453 * \brief Function writes various RF settings in startup.
ram54288 0:dbad57390bd1 1454 *
ram54288 0:dbad57390bd1 1455 * \param none
ram54288 0:dbad57390bd1 1456 *
ram54288 0:dbad57390bd1 1457 * \return none
ram54288 0:dbad57390bd1 1458 */
ram54288 0:dbad57390bd1 1459 static void rf_write_settings(void)
ram54288 0:dbad57390bd1 1460 {
ram54288 0:dbad57390bd1 1461 rf_if_lock();
ram54288 0:dbad57390bd1 1462 rf_if_write_rf_settings();
ram54288 0:dbad57390bd1 1463 /*Set output power*/
ram54288 0:dbad57390bd1 1464 rf_if_write_set_tx_power_register(radio_tx_power);
ram54288 0:dbad57390bd1 1465 /*Initialise Antenna Diversity*/
ram54288 0:dbad57390bd1 1466 if(rf_use_antenna_diversity)
ram54288 0:dbad57390bd1 1467 rf_if_write_antenna_diversity_settings();
ram54288 0:dbad57390bd1 1468 rf_if_unlock();
ram54288 0:dbad57390bd1 1469 }
ram54288 0:dbad57390bd1 1470
ram54288 0:dbad57390bd1 1471 /*
ram54288 0:dbad57390bd1 1472 * \brief Function writes 16-bit address in RF address filter.
ram54288 0:dbad57390bd1 1473 *
ram54288 0:dbad57390bd1 1474 * \param short_address Given short address
ram54288 0:dbad57390bd1 1475 *
ram54288 0:dbad57390bd1 1476 * \return none
ram54288 0:dbad57390bd1 1477 */
ram54288 0:dbad57390bd1 1478 static void rf_set_short_adr(uint8_t * short_address)
ram54288 0:dbad57390bd1 1479 {
ram54288 0:dbad57390bd1 1480 rf_if_lock();
ram54288 0:dbad57390bd1 1481 /*Wake up RF if sleeping*/
ram54288 0:dbad57390bd1 1482 if(rf_flags_check(RFF_ON) == 0)
ram54288 0:dbad57390bd1 1483 {
ram54288 0:dbad57390bd1 1484 rf_if_disable_slptr();
ram54288 0:dbad57390bd1 1485 rf_poll_trx_state_change(TRX_OFF);
ram54288 0:dbad57390bd1 1486 }
ram54288 0:dbad57390bd1 1487 /*Write address filter registers*/
ram54288 0:dbad57390bd1 1488 rf_if_write_short_addr_registers(short_address);
ram54288 0:dbad57390bd1 1489 /*RF back to sleep*/
ram54288 0:dbad57390bd1 1490 if(rf_flags_check(RFF_ON) == 0)
ram54288 0:dbad57390bd1 1491 {
ram54288 0:dbad57390bd1 1492 rf_if_enable_slptr();
ram54288 0:dbad57390bd1 1493 }
ram54288 0:dbad57390bd1 1494 rf_if_unlock();
ram54288 0:dbad57390bd1 1495 }
ram54288 0:dbad57390bd1 1496
ram54288 0:dbad57390bd1 1497 /*
ram54288 0:dbad57390bd1 1498 * \brief Function writes PAN Id in RF PAN Id filter.
ram54288 0:dbad57390bd1 1499 *
ram54288 0:dbad57390bd1 1500 * \param pan_id Given PAN Id
ram54288 0:dbad57390bd1 1501 *
ram54288 0:dbad57390bd1 1502 * \return none
ram54288 0:dbad57390bd1 1503 */
ram54288 0:dbad57390bd1 1504 static void rf_set_pan_id(uint8_t *pan_id)
ram54288 0:dbad57390bd1 1505 {
ram54288 0:dbad57390bd1 1506 rf_if_lock();
ram54288 0:dbad57390bd1 1507 /*Wake up RF if sleeping*/
ram54288 0:dbad57390bd1 1508 if(rf_flags_check(RFF_ON) == 0)
ram54288 0:dbad57390bd1 1509 {
ram54288 0:dbad57390bd1 1510 rf_if_disable_slptr();
ram54288 0:dbad57390bd1 1511 rf_poll_trx_state_change(TRX_OFF);
ram54288 0:dbad57390bd1 1512 }
ram54288 0:dbad57390bd1 1513 /*Write address filter registers*/
ram54288 0:dbad57390bd1 1514 rf_if_write_pan_id_registers(pan_id);
ram54288 0:dbad57390bd1 1515 /*RF back to sleep*/
ram54288 0:dbad57390bd1 1516 if(rf_flags_check(RFF_ON) == 0)
ram54288 0:dbad57390bd1 1517 {
ram54288 0:dbad57390bd1 1518 rf_if_enable_slptr();
ram54288 0:dbad57390bd1 1519 }
ram54288 0:dbad57390bd1 1520 rf_if_unlock();
ram54288 0:dbad57390bd1 1521 }
ram54288 0:dbad57390bd1 1522
ram54288 0:dbad57390bd1 1523 /*
ram54288 0:dbad57390bd1 1524 * \brief Function writes 64-bit address in RF address filter.
ram54288 0:dbad57390bd1 1525 *
ram54288 0:dbad57390bd1 1526 * \param address Given 64-bit address
ram54288 0:dbad57390bd1 1527 *
ram54288 0:dbad57390bd1 1528 * \return none
ram54288 0:dbad57390bd1 1529 */
ram54288 0:dbad57390bd1 1530 static void rf_set_address(uint8_t *address)
ram54288 0:dbad57390bd1 1531 {
ram54288 0:dbad57390bd1 1532 rf_if_lock();
ram54288 0:dbad57390bd1 1533 /*Wake up RF if sleeping*/
ram54288 0:dbad57390bd1 1534 if(rf_flags_check(RFF_ON) == 0)
ram54288 0:dbad57390bd1 1535 {
ram54288 0:dbad57390bd1 1536 rf_if_disable_slptr();
ram54288 0:dbad57390bd1 1537 rf_poll_trx_state_change(TRX_OFF);
ram54288 0:dbad57390bd1 1538 }
ram54288 0:dbad57390bd1 1539 /*Write address filter registers*/
ram54288 0:dbad57390bd1 1540 rf_if_write_ieee_addr_registers(address);
ram54288 0:dbad57390bd1 1541 /*RF back to sleep*/
ram54288 0:dbad57390bd1 1542 if(rf_flags_check(RFF_ON) == 0)
ram54288 0:dbad57390bd1 1543 {
ram54288 0:dbad57390bd1 1544 rf_if_enable_slptr();
ram54288 0:dbad57390bd1 1545 }
ram54288 0:dbad57390bd1 1546 rf_if_unlock();
ram54288 0:dbad57390bd1 1547 }
ram54288 0:dbad57390bd1 1548
ram54288 0:dbad57390bd1 1549 /*
ram54288 0:dbad57390bd1 1550 * \brief Function sets the RF channel.
ram54288 0:dbad57390bd1 1551 *
ram54288 0:dbad57390bd1 1552 * \param ch New channel
ram54288 0:dbad57390bd1 1553 *
ram54288 0:dbad57390bd1 1554 * \return none
ram54288 0:dbad57390bd1 1555 */
ram54288 0:dbad57390bd1 1556 static void rf_channel_set(uint8_t ch)
ram54288 0:dbad57390bd1 1557 {
ram54288 0:dbad57390bd1 1558 rf_if_lock();
ram54288 0:dbad57390bd1 1559 rf_phy_channel = ch;
ram54288 0:dbad57390bd1 1560 if(ch < 0x1f)
ram54288 0:dbad57390bd1 1561 rf_if_set_channel_register(ch);
ram54288 0:dbad57390bd1 1562 rf_if_unlock();
ram54288 0:dbad57390bd1 1563 }
ram54288 0:dbad57390bd1 1564
ram54288 0:dbad57390bd1 1565
ram54288 0:dbad57390bd1 1566 /*
ram54288 0:dbad57390bd1 1567 * \brief Function initialises the radio driver and resets the radio.
ram54288 0:dbad57390bd1 1568 *
ram54288 0:dbad57390bd1 1569 * \param none
ram54288 0:dbad57390bd1 1570 *
ram54288 0:dbad57390bd1 1571 * \return none
ram54288 0:dbad57390bd1 1572 */
ram54288 0:dbad57390bd1 1573 static void rf_init(void)
ram54288 0:dbad57390bd1 1574 {
ram54288 0:dbad57390bd1 1575 /*Reset RF module*/
ram54288 0:dbad57390bd1 1576 rf_if_reset_radio();
ram54288 0:dbad57390bd1 1577
ram54288 0:dbad57390bd1 1578 rf_if_lock();
ram54288 0:dbad57390bd1 1579
ram54288 0:dbad57390bd1 1580 /*Write RF settings*/
ram54288 0:dbad57390bd1 1581 rf_write_settings();
ram54288 0:dbad57390bd1 1582 /*Initialise PHY mode*/
ram54288 0:dbad57390bd1 1583 rf_init_phy_mode();
ram54288 0:dbad57390bd1 1584 /*Clear RF flags*/
ram54288 0:dbad57390bd1 1585 rf_flags_reset();
ram54288 0:dbad57390bd1 1586 /*Set RF in TRX OFF state*/
ram54288 0:dbad57390bd1 1587 rf_if_change_trx_state(TRX_OFF);
ram54288 0:dbad57390bd1 1588 /*Set RF in PLL_ON state*/
ram54288 0:dbad57390bd1 1589 rf_if_change_trx_state(PLL_ON);
ram54288 0:dbad57390bd1 1590 /*Start receiver*/
ram54288 0:dbad57390bd1 1591 rf_receive();
ram54288 0:dbad57390bd1 1592 /*Read randomness, and add to seed*/
ram54288 0:dbad57390bd1 1593 randLIB_add_seed(rf_if_read_rnd());
ram54288 0:dbad57390bd1 1594 /*Start RF calibration timer*/
ram54288 0:dbad57390bd1 1595 rf_calibration_timer_start(RF_CALIBRATION_INTERVAL);
ram54288 0:dbad57390bd1 1596
ram54288 0:dbad57390bd1 1597 rf_if_unlock();
ram54288 0:dbad57390bd1 1598 }
ram54288 0:dbad57390bd1 1599
ram54288 0:dbad57390bd1 1600 /**
ram54288 0:dbad57390bd1 1601 * \brief Function gets called when MAC is setting radio off.
ram54288 0:dbad57390bd1 1602 *
ram54288 0:dbad57390bd1 1603 * \param none
ram54288 0:dbad57390bd1 1604 *
ram54288 0:dbad57390bd1 1605 * \return none
ram54288 0:dbad57390bd1 1606 */
ram54288 0:dbad57390bd1 1607 static void rf_off(void)
ram54288 0:dbad57390bd1 1608 {
ram54288 0:dbad57390bd1 1609 if(rf_flags_check(RFF_ON))
ram54288 0:dbad57390bd1 1610 {
ram54288 0:dbad57390bd1 1611 rf_if_lock();
ram54288 0:dbad57390bd1 1612 rf_cca_abort();
ram54288 0:dbad57390bd1 1613 uint16_t while_counter = 0;
ram54288 0:dbad57390bd1 1614 /*Wait while receiving*/
ram54288 0:dbad57390bd1 1615 while(rf_if_read_trx_state() == BUSY_RX_AACK)
ram54288 0:dbad57390bd1 1616 {
ram54288 0:dbad57390bd1 1617 while_counter++;
ram54288 0:dbad57390bd1 1618 if(while_counter == 0xffff)
ram54288 0:dbad57390bd1 1619 break;
ram54288 0:dbad57390bd1 1620 }
ram54288 0:dbad57390bd1 1621 /*RF state change: RX_AACK_ON->PLL_ON->TRX_OFF->SLEEP*/
ram54288 0:dbad57390bd1 1622 if(rf_if_read_trx_state() == RX_AACK_ON)
ram54288 0:dbad57390bd1 1623 {
ram54288 0:dbad57390bd1 1624 rf_if_change_trx_state(PLL_ON);
ram54288 0:dbad57390bd1 1625 }
ram54288 0:dbad57390bd1 1626 rf_if_change_trx_state(TRX_OFF);
ram54288 0:dbad57390bd1 1627 rf_if_enable_slptr();
ram54288 0:dbad57390bd1 1628
ram54288 0:dbad57390bd1 1629 /*Disable Antenna Diversity*/
ram54288 0:dbad57390bd1 1630 if(rf_use_antenna_diversity)
ram54288 0:dbad57390bd1 1631 rf_if_disable_ant_div();
ram54288 0:dbad57390bd1 1632 rf_if_unlock();
ram54288 0:dbad57390bd1 1633 }
ram54288 0:dbad57390bd1 1634
ram54288 0:dbad57390bd1 1635 /*Clears all flags*/
ram54288 0:dbad57390bd1 1636 rf_flags_reset();
ram54288 0:dbad57390bd1 1637 }
ram54288 0:dbad57390bd1 1638
ram54288 0:dbad57390bd1 1639 /*
ram54288 0:dbad57390bd1 1640 * \brief Function polls the RF state until it has changed to desired state.
ram54288 0:dbad57390bd1 1641 *
ram54288 0:dbad57390bd1 1642 * \param trx_state RF state
ram54288 0:dbad57390bd1 1643 *
ram54288 0:dbad57390bd1 1644 * \return none
ram54288 0:dbad57390bd1 1645 */
ram54288 0:dbad57390bd1 1646 static void rf_poll_trx_state_change(rf_trx_states_t trx_state)
ram54288 0:dbad57390bd1 1647 {
ram54288 0:dbad57390bd1 1648 uint16_t while_counter = 0;
ram54288 0:dbad57390bd1 1649 // XXX lock apparently not needed
ram54288 0:dbad57390bd1 1650 rf_if_lock();
ram54288 0:dbad57390bd1 1651
ram54288 0:dbad57390bd1 1652 if(trx_state != RF_TX_START)
ram54288 0:dbad57390bd1 1653 {
ram54288 0:dbad57390bd1 1654 if(trx_state == FORCE_PLL_ON)
ram54288 0:dbad57390bd1 1655 trx_state = PLL_ON;
ram54288 0:dbad57390bd1 1656 else if(trx_state == FORCE_TRX_OFF)
ram54288 0:dbad57390bd1 1657 trx_state = TRX_OFF;
ram54288 0:dbad57390bd1 1658
ram54288 0:dbad57390bd1 1659 while(rf_if_read_trx_state() != trx_state)
ram54288 0:dbad57390bd1 1660 {
ram54288 0:dbad57390bd1 1661 while_counter++;
ram54288 0:dbad57390bd1 1662 if(while_counter == 0x1ff)
ram54288 0:dbad57390bd1 1663 break;
ram54288 0:dbad57390bd1 1664 }
ram54288 0:dbad57390bd1 1665 }
ram54288 0:dbad57390bd1 1666 rf_if_unlock();
ram54288 0:dbad57390bd1 1667 }
ram54288 0:dbad57390bd1 1668
ram54288 0:dbad57390bd1 1669 /*
ram54288 0:dbad57390bd1 1670 * \brief Function starts the CCA process before starting data transmission and copies the data to RF TX FIFO.
ram54288 0:dbad57390bd1 1671 *
ram54288 0:dbad57390bd1 1672 * \param data_ptr Pointer to TX data (excluding FCS)
ram54288 0:dbad57390bd1 1673 * \param data_length Length of the TX data (excluding FCS)
ram54288 0:dbad57390bd1 1674 * \param tx_handle Handle to transmission
ram54288 0:dbad57390bd1 1675 * \return 0 Success
ram54288 0:dbad57390bd1 1676 * \return -1 Busy
ram54288 0:dbad57390bd1 1677 */
ram54288 0:dbad57390bd1 1678 static int8_t rf_start_cca(uint8_t *data_ptr, uint16_t data_length, uint8_t tx_handle, data_protocol_e data_protocol )
ram54288 0:dbad57390bd1 1679 {
ram54288 0:dbad57390bd1 1680 (void)data_protocol;
ram54288 0:dbad57390bd1 1681 rf_if_lock();
ram54288 0:dbad57390bd1 1682 /*Check if transmitter is busy*/
ram54288 0:dbad57390bd1 1683 if(rf_if_read_trx_state() == BUSY_RX_AACK || data_length > RF_MTU - 2)
ram54288 0:dbad57390bd1 1684 {
ram54288 0:dbad57390bd1 1685 rf_if_unlock();
ram54288 0:dbad57390bd1 1686 /*Return busy*/
ram54288 0:dbad57390bd1 1687 return -1;
ram54288 0:dbad57390bd1 1688 }
ram54288 0:dbad57390bd1 1689 else
ram54288 0:dbad57390bd1 1690 {
ram54288 0:dbad57390bd1 1691 expected_ack_sequence = -1;
ram54288 0:dbad57390bd1 1692
ram54288 0:dbad57390bd1 1693 /*Nanostack has a static TX buffer, which will remain valid until we*/
ram54288 0:dbad57390bd1 1694 /*generate a callback, so we just note the pointer for reading later.*/
ram54288 0:dbad57390bd1 1695 rf_tx_data = data_ptr;
ram54288 0:dbad57390bd1 1696 rf_tx_length = data_length;
ram54288 0:dbad57390bd1 1697 /*Start CCA timeout*/
ram54288 0:dbad57390bd1 1698 rf_cca_timer_start(RF_CCA_BASE_BACKOFF + randLIB_get_random_in_range(0, RF_CCA_RANDOM_BACKOFF));
ram54288 0:dbad57390bd1 1699 /*Store TX handle*/
ram54288 0:dbad57390bd1 1700 mac_tx_handle = tx_handle;
ram54288 0:dbad57390bd1 1701 rf_if_unlock();
ram54288 0:dbad57390bd1 1702 }
ram54288 0:dbad57390bd1 1703
ram54288 0:dbad57390bd1 1704 /*Return success*/
ram54288 0:dbad57390bd1 1705 return 0;
ram54288 0:dbad57390bd1 1706 }
ram54288 0:dbad57390bd1 1707
ram54288 0:dbad57390bd1 1708 /*
ram54288 0:dbad57390bd1 1709 * \brief Function aborts CCA process.
ram54288 0:dbad57390bd1 1710 *
ram54288 0:dbad57390bd1 1711 * \param none
ram54288 0:dbad57390bd1 1712 *
ram54288 0:dbad57390bd1 1713 * \return none
ram54288 0:dbad57390bd1 1714 */
ram54288 0:dbad57390bd1 1715 static void rf_cca_abort(void)
ram54288 0:dbad57390bd1 1716 {
ram54288 0:dbad57390bd1 1717 rf_cca_timer_stop();
ram54288 0:dbad57390bd1 1718 rf_flags_clear(RFF_CCA);
ram54288 0:dbad57390bd1 1719 rf_disable_static_frame_buffer_protection();
ram54288 0:dbad57390bd1 1720 }
ram54288 0:dbad57390bd1 1721
ram54288 0:dbad57390bd1 1722 /*
ram54288 0:dbad57390bd1 1723 * \brief Function starts the transmission of the frame.
ram54288 0:dbad57390bd1 1724 *
ram54288 0:dbad57390bd1 1725 * \param none
ram54288 0:dbad57390bd1 1726 *
ram54288 0:dbad57390bd1 1727 * \return none
ram54288 0:dbad57390bd1 1728 */
ram54288 0:dbad57390bd1 1729 static void rf_start_tx(void)
ram54288 0:dbad57390bd1 1730 {
ram54288 0:dbad57390bd1 1731 /*Only start transmitting from RX state*/
ram54288 0:dbad57390bd1 1732 uint8_t trx_state = rf_if_read_trx_state();
ram54288 0:dbad57390bd1 1733 if(trx_state != RX_AACK_ON)
ram54288 0:dbad57390bd1 1734 {
ram54288 0:dbad57390bd1 1735 rf_disable_static_frame_buffer_protection();
ram54288 0:dbad57390bd1 1736 if(device_driver.phy_tx_done_cb){
ram54288 0:dbad57390bd1 1737 device_driver.phy_tx_done_cb(rf_radio_driver_id, mac_tx_handle, PHY_LINK_CCA_FAIL, 0, 0);
ram54288 0:dbad57390bd1 1738 }
ram54288 0:dbad57390bd1 1739 }
ram54288 0:dbad57390bd1 1740 else
ram54288 0:dbad57390bd1 1741 {
ram54288 0:dbad57390bd1 1742 /*RF state change: ->PLL_ON->RF_TX_START*/
ram54288 0:dbad57390bd1 1743 rf_if_change_trx_state(FORCE_PLL_ON);
ram54288 0:dbad57390bd1 1744 rf_flags_clear(RFF_RX);
ram54288 0:dbad57390bd1 1745 /*Now we're out of receive mode, can release protection*/
ram54288 0:dbad57390bd1 1746 rf_disable_static_frame_buffer_protection();
ram54288 0:dbad57390bd1 1747 rf_if_enable_tx_end_interrupt();
ram54288 0:dbad57390bd1 1748 rf_flags_set(RFF_TX);
ram54288 0:dbad57390bd1 1749 rf_if_change_trx_state(RF_TX_START);
ram54288 0:dbad57390bd1 1750 }
ram54288 0:dbad57390bd1 1751 }
ram54288 0:dbad57390bd1 1752
ram54288 0:dbad57390bd1 1753 /*
ram54288 0:dbad57390bd1 1754 * \brief Function sets the RF in RX state.
ram54288 0:dbad57390bd1 1755 *
ram54288 0:dbad57390bd1 1756 * \param none
ram54288 0:dbad57390bd1 1757 *
ram54288 0:dbad57390bd1 1758 * \return none
ram54288 0:dbad57390bd1 1759 */
ram54288 0:dbad57390bd1 1760 static void rf_receive(void)
ram54288 0:dbad57390bd1 1761 {
ram54288 0:dbad57390bd1 1762 uint16_t while_counter = 0;
ram54288 0:dbad57390bd1 1763 if(rf_flags_check(RFF_ON) == 0)
ram54288 0:dbad57390bd1 1764 {
ram54288 0:dbad57390bd1 1765 rf_on();
ram54288 0:dbad57390bd1 1766 }
ram54288 0:dbad57390bd1 1767 /*If not yet in RX state set it*/
ram54288 0:dbad57390bd1 1768 if(rf_flags_check(RFF_RX) == 0)
ram54288 0:dbad57390bd1 1769 {
ram54288 0:dbad57390bd1 1770 rf_if_lock();
ram54288 0:dbad57390bd1 1771 /*Wait while receiving data*/
ram54288 0:dbad57390bd1 1772 while(rf_if_read_trx_state() == BUSY_RX_AACK)
ram54288 0:dbad57390bd1 1773 {
ram54288 0:dbad57390bd1 1774 while_counter++;
ram54288 0:dbad57390bd1 1775 if(while_counter == 0xffff)
ram54288 0:dbad57390bd1 1776 {
ram54288 0:dbad57390bd1 1777 break;
ram54288 0:dbad57390bd1 1778 }
ram54288 0:dbad57390bd1 1779 }
ram54288 0:dbad57390bd1 1780
ram54288 0:dbad57390bd1 1781 rf_if_change_trx_state(PLL_ON);
ram54288 0:dbad57390bd1 1782
ram54288 0:dbad57390bd1 1783 if((rf_mode == RF_MODE_SNIFFER) || (rf_mode == RF_MODE_ED))
ram54288 0:dbad57390bd1 1784 {
ram54288 0:dbad57390bd1 1785 rf_if_change_trx_state(RX_ON);
ram54288 0:dbad57390bd1 1786 }
ram54288 0:dbad57390bd1 1787 else
ram54288 0:dbad57390bd1 1788 {
ram54288 0:dbad57390bd1 1789 /*ACK is always received in promiscuous mode to bypass address filters*/
ram54288 0:dbad57390bd1 1790 if(rf_rx_mode)
ram54288 0:dbad57390bd1 1791 {
ram54288 0:dbad57390bd1 1792 rf_rx_mode = 0;
ram54288 0:dbad57390bd1 1793 rf_if_enable_promiscuous_mode();
ram54288 0:dbad57390bd1 1794 }
ram54288 0:dbad57390bd1 1795 else
ram54288 0:dbad57390bd1 1796 {
ram54288 0:dbad57390bd1 1797 rf_if_disable_promiscuous_mode();
ram54288 0:dbad57390bd1 1798 }
ram54288 0:dbad57390bd1 1799 rf_if_change_trx_state(RX_AACK_ON);
ram54288 0:dbad57390bd1 1800 }
ram54288 0:dbad57390bd1 1801 /*If calibration timer was unable to calibrate the RF, run calibration now*/
ram54288 0:dbad57390bd1 1802 if(!rf_tuned)
ram54288 0:dbad57390bd1 1803 {
ram54288 0:dbad57390bd1 1804 /*Start calibration. This can be done in states TRX_OFF, PLL_ON or in any receive state*/
ram54288 0:dbad57390bd1 1805 rf_if_calibration();
ram54288 0:dbad57390bd1 1806 /*RF is tuned now*/
ram54288 0:dbad57390bd1 1807 rf_tuned = 1;
ram54288 0:dbad57390bd1 1808 }
ram54288 0:dbad57390bd1 1809
ram54288 0:dbad57390bd1 1810 rf_channel_set(rf_phy_channel);
ram54288 0:dbad57390bd1 1811 rf_flags_set(RFF_RX);
ram54288 0:dbad57390bd1 1812 // Don't receive packets when ED mode enabled
ram54288 0:dbad57390bd1 1813 if (rf_mode != RF_MODE_ED)
ram54288 0:dbad57390bd1 1814 {
ram54288 0:dbad57390bd1 1815 rf_if_enable_rx_end_interrupt();
ram54288 0:dbad57390bd1 1816 }
ram54288 0:dbad57390bd1 1817 rf_if_unlock();
ram54288 0:dbad57390bd1 1818 }
ram54288 0:dbad57390bd1 1819 }
ram54288 0:dbad57390bd1 1820
ram54288 0:dbad57390bd1 1821 /*
ram54288 0:dbad57390bd1 1822 * \brief Function calibrates the radio.
ram54288 0:dbad57390bd1 1823 *
ram54288 0:dbad57390bd1 1824 * \param none
ram54288 0:dbad57390bd1 1825 *
ram54288 0:dbad57390bd1 1826 * \return none
ram54288 0:dbad57390bd1 1827 */
ram54288 0:dbad57390bd1 1828 static void rf_calibration_cb(void)
ram54288 0:dbad57390bd1 1829 {
ram54288 0:dbad57390bd1 1830 /*clear tuned flag to start tuning in rf_receive*/
ram54288 0:dbad57390bd1 1831 rf_tuned = 0;
ram54288 0:dbad57390bd1 1832 /*If RF is in default receive state, start calibration*/
ram54288 0:dbad57390bd1 1833 if(rf_if_read_trx_state() == RX_AACK_ON)
ram54288 0:dbad57390bd1 1834 {
ram54288 0:dbad57390bd1 1835 rf_if_lock();
ram54288 0:dbad57390bd1 1836 /*Set RF in PLL_ON state*/
ram54288 0:dbad57390bd1 1837 rf_if_change_trx_state(PLL_ON);
ram54288 0:dbad57390bd1 1838 /*Set RF in TRX_OFF state to start PLL tuning*/
ram54288 0:dbad57390bd1 1839 rf_if_change_trx_state(TRX_OFF);
ram54288 0:dbad57390bd1 1840 /*Set RF in RX_ON state to calibrate*/
ram54288 0:dbad57390bd1 1841 rf_if_change_trx_state(RX_ON);
ram54288 0:dbad57390bd1 1842 /*Calibrate FTN*/
ram54288 0:dbad57390bd1 1843 rf_if_calibration();
ram54288 0:dbad57390bd1 1844 /*RF is tuned now*/
ram54288 0:dbad57390bd1 1845 rf_tuned = 1;
ram54288 0:dbad57390bd1 1846 /*Back to default receive state*/
ram54288 0:dbad57390bd1 1847 rf_flags_clear(RFF_RX);
ram54288 0:dbad57390bd1 1848 rf_receive();
ram54288 0:dbad57390bd1 1849 rf_if_unlock();
ram54288 0:dbad57390bd1 1850 }
ram54288 0:dbad57390bd1 1851 }
ram54288 0:dbad57390bd1 1852
ram54288 0:dbad57390bd1 1853 /*
ram54288 0:dbad57390bd1 1854 * \brief Function sets RF_ON flag when radio is powered.
ram54288 0:dbad57390bd1 1855 *
ram54288 0:dbad57390bd1 1856 * \param none
ram54288 0:dbad57390bd1 1857 *
ram54288 0:dbad57390bd1 1858 * \return none
ram54288 0:dbad57390bd1 1859 */
ram54288 0:dbad57390bd1 1860 static void rf_on(void)
ram54288 0:dbad57390bd1 1861 {
ram54288 0:dbad57390bd1 1862 /*Set RFF_ON flag*/
ram54288 0:dbad57390bd1 1863 if(rf_flags_check(RFF_ON) == 0)
ram54288 0:dbad57390bd1 1864 {
ram54288 0:dbad57390bd1 1865 rf_if_lock();
ram54288 0:dbad57390bd1 1866 rf_flags_set(RFF_ON);
ram54288 0:dbad57390bd1 1867 /*Enable Antenna diversity*/
ram54288 0:dbad57390bd1 1868 if(rf_use_antenna_diversity)
ram54288 0:dbad57390bd1 1869 /*Set ANT_EXT_SW_EN to enable controlling of antenna diversity*/
ram54288 0:dbad57390bd1 1870 rf_if_enable_ant_div();
ram54288 0:dbad57390bd1 1871
ram54288 0:dbad57390bd1 1872 /*Wake up from sleep state*/
ram54288 0:dbad57390bd1 1873 rf_if_disable_slptr();
ram54288 0:dbad57390bd1 1874 rf_poll_trx_state_change(TRX_OFF);
ram54288 0:dbad57390bd1 1875 rf_if_unlock();
ram54288 0:dbad57390bd1 1876 }
ram54288 0:dbad57390bd1 1877 }
ram54288 0:dbad57390bd1 1878
ram54288 0:dbad57390bd1 1879 /*
ram54288 0:dbad57390bd1 1880 * \brief Function handles the received ACK frame.
ram54288 0:dbad57390bd1 1881 *
ram54288 0:dbad57390bd1 1882 * \param seq_number Sequence number of received ACK
ram54288 0:dbad57390bd1 1883 * \param data_pending Pending bit state in received ACK
ram54288 0:dbad57390bd1 1884 *
ram54288 0:dbad57390bd1 1885 * \return none
ram54288 0:dbad57390bd1 1886 */
ram54288 0:dbad57390bd1 1887 static void rf_handle_ack(uint8_t seq_number, uint8_t data_pending)
ram54288 0:dbad57390bd1 1888 {
ram54288 0:dbad57390bd1 1889 phy_link_tx_status_e phy_status;
ram54288 0:dbad57390bd1 1890 rf_if_lock();
ram54288 0:dbad57390bd1 1891 /*Received ACK sequence must be equal with transmitted packet sequence*/
ram54288 0:dbad57390bd1 1892 if(expected_ack_sequence == seq_number)
ram54288 0:dbad57390bd1 1893 {
ram54288 0:dbad57390bd1 1894 rf_ack_wait_timer_stop();
ram54288 0:dbad57390bd1 1895 expected_ack_sequence = -1;
ram54288 0:dbad57390bd1 1896 /*When data pending bit in ACK frame is set, inform NET library*/
ram54288 0:dbad57390bd1 1897 if(data_pending)
ram54288 0:dbad57390bd1 1898 phy_status = PHY_LINK_TX_DONE_PENDING;
ram54288 0:dbad57390bd1 1899 else
ram54288 0:dbad57390bd1 1900 phy_status = PHY_LINK_TX_DONE;
ram54288 0:dbad57390bd1 1901 /*Call PHY TX Done API*/
ram54288 0:dbad57390bd1 1902 if(device_driver.phy_tx_done_cb){
ram54288 0:dbad57390bd1 1903 device_driver.phy_tx_done_cb(rf_radio_driver_id, mac_tx_handle,phy_status, 0, 0);
ram54288 0:dbad57390bd1 1904 }
ram54288 0:dbad57390bd1 1905 }
ram54288 0:dbad57390bd1 1906 rf_if_unlock();
ram54288 0:dbad57390bd1 1907 }
ram54288 0:dbad57390bd1 1908
ram54288 0:dbad57390bd1 1909 /*
ram54288 0:dbad57390bd1 1910 * \brief Function is a call back for RX end interrupt.
ram54288 0:dbad57390bd1 1911 *
ram54288 0:dbad57390bd1 1912 * \param none
ram54288 0:dbad57390bd1 1913 *
ram54288 0:dbad57390bd1 1914 * \return none
ram54288 0:dbad57390bd1 1915 */
ram54288 0:dbad57390bd1 1916 static void rf_handle_rx_end(void)
ram54288 0:dbad57390bd1 1917 {
ram54288 0:dbad57390bd1 1918 /*Start receiver*/
ram54288 0:dbad57390bd1 1919 rf_flags_clear(RFF_RX);
ram54288 0:dbad57390bd1 1920 rf_receive();
ram54288 0:dbad57390bd1 1921
ram54288 0:dbad57390bd1 1922 /*Frame received interrupt*/
ram54288 0:dbad57390bd1 1923 if(!rf_flags_check(RFF_RX)) {
ram54288 0:dbad57390bd1 1924 return;
ram54288 0:dbad57390bd1 1925 }
ram54288 0:dbad57390bd1 1926
ram54288 0:dbad57390bd1 1927 static uint8_t rf_buffer[RF_MTU];
ram54288 0:dbad57390bd1 1928 uint8_t rf_lqi, rf_ed;
ram54288 0:dbad57390bd1 1929 int8_t rf_rssi;
ram54288 0:dbad57390bd1 1930 bool crc_good;
ram54288 0:dbad57390bd1 1931
ram54288 0:dbad57390bd1 1932 /*Read received packet*/
ram54288 0:dbad57390bd1 1933 uint8_t len = rf_if_read_packet(rf_buffer, &rf_lqi, &rf_ed, &crc_good);
ram54288 0:dbad57390bd1 1934 if (len < 5 || !crc_good) {
ram54288 0:dbad57390bd1 1935 return;
ram54288 0:dbad57390bd1 1936 }
ram54288 0:dbad57390bd1 1937
ram54288 0:dbad57390bd1 1938 /* Convert raw ED to dBm value (chip-dependent) */
ram54288 0:dbad57390bd1 1939 rf_rssi = rf_if_scale_rssi(rf_ed);
ram54288 0:dbad57390bd1 1940
ram54288 0:dbad57390bd1 1941 /* Create a virtual LQI using received RSSI, forgetting actual HW LQI */
ram54288 0:dbad57390bd1 1942 /* (should be done through PHY_EXTENSION_CONVERT_SIGNAL_INFO) */
ram54288 0:dbad57390bd1 1943 rf_lqi = rf_scale_lqi(rf_rssi);
ram54288 0:dbad57390bd1 1944
ram54288 0:dbad57390bd1 1945 /*Handle received ACK*/
ram54288 0:dbad57390bd1 1946 if((rf_buffer[0] & 0x07) == 0x02 && rf_mode != RF_MODE_SNIFFER)
ram54288 0:dbad57390bd1 1947 {
ram54288 0:dbad57390bd1 1948 /*Check if data is pending*/
ram54288 0:dbad57390bd1 1949 bool pending = (rf_buffer[0] & 0x10);
ram54288 0:dbad57390bd1 1950
ram54288 0:dbad57390bd1 1951 /*Send sequence number in ACK handler*/
ram54288 0:dbad57390bd1 1952 rf_handle_ack(rf_buffer[2], pending);
ram54288 0:dbad57390bd1 1953 } else {
ram54288 0:dbad57390bd1 1954 if( device_driver.phy_rx_cb ){
ram54288 0:dbad57390bd1 1955 device_driver.phy_rx_cb(rf_buffer, len - 2, rf_lqi, rf_rssi, rf_radio_driver_id);
ram54288 0:dbad57390bd1 1956 }
ram54288 0:dbad57390bd1 1957 }
ram54288 0:dbad57390bd1 1958 }
ram54288 0:dbad57390bd1 1959
ram54288 0:dbad57390bd1 1960 /*
ram54288 0:dbad57390bd1 1961 * \brief Function is called when MAC is shutting down the radio.
ram54288 0:dbad57390bd1 1962 *
ram54288 0:dbad57390bd1 1963 * \param none
ram54288 0:dbad57390bd1 1964 *
ram54288 0:dbad57390bd1 1965 * \return none
ram54288 0:dbad57390bd1 1966 */
ram54288 0:dbad57390bd1 1967 static void rf_shutdown(void)
ram54288 0:dbad57390bd1 1968 {
ram54288 0:dbad57390bd1 1969 /*Call RF OFF*/
ram54288 0:dbad57390bd1 1970 rf_off();
ram54288 0:dbad57390bd1 1971 }
ram54288 0:dbad57390bd1 1972
ram54288 0:dbad57390bd1 1973 /*
ram54288 0:dbad57390bd1 1974 * \brief Function is a call back for TX end interrupt.
ram54288 0:dbad57390bd1 1975 *
ram54288 0:dbad57390bd1 1976 * \param none
ram54288 0:dbad57390bd1 1977 *
ram54288 0:dbad57390bd1 1978 * \return none
ram54288 0:dbad57390bd1 1979 */
ram54288 0:dbad57390bd1 1980 static void rf_handle_tx_end(void)
ram54288 0:dbad57390bd1 1981 {
ram54288 0:dbad57390bd1 1982 rf_rx_mode = 0;
ram54288 0:dbad57390bd1 1983 /*If ACK is needed for this transmission*/
ram54288 0:dbad57390bd1 1984 if((rf_tx_data[0] & 0x20) && rf_flags_check(RFF_TX))
ram54288 0:dbad57390bd1 1985 {
ram54288 0:dbad57390bd1 1986 expected_ack_sequence = rf_tx_data[2];
ram54288 0:dbad57390bd1 1987 rf_ack_wait_timer_start(rf_ack_wait_duration);
ram54288 0:dbad57390bd1 1988 rf_rx_mode = 1;
ram54288 0:dbad57390bd1 1989 }
ram54288 0:dbad57390bd1 1990 rf_flags_clear(RFF_RX);
ram54288 0:dbad57390bd1 1991 /*Start receiver*/
ram54288 0:dbad57390bd1 1992 rf_receive();
ram54288 0:dbad57390bd1 1993
ram54288 0:dbad57390bd1 1994 /*Call PHY TX Done API*/
ram54288 0:dbad57390bd1 1995 if(device_driver.phy_tx_done_cb){
ram54288 0:dbad57390bd1 1996 device_driver.phy_tx_done_cb(rf_radio_driver_id, mac_tx_handle, PHY_LINK_TX_SUCCESS, 0, 0);
ram54288 0:dbad57390bd1 1997 }
ram54288 0:dbad57390bd1 1998 }
ram54288 0:dbad57390bd1 1999
ram54288 0:dbad57390bd1 2000 /*
ram54288 0:dbad57390bd1 2001 * \brief Function is a call back for CCA ED done interrupt.
ram54288 0:dbad57390bd1 2002 *
ram54288 0:dbad57390bd1 2003 * \param none
ram54288 0:dbad57390bd1 2004 *
ram54288 0:dbad57390bd1 2005 * \return none
ram54288 0:dbad57390bd1 2006 */
ram54288 0:dbad57390bd1 2007 static void rf_handle_cca_ed_done(void)
ram54288 0:dbad57390bd1 2008 {
ram54288 0:dbad57390bd1 2009 if (!rf_flags_check(RFF_CCA)) {
ram54288 0:dbad57390bd1 2010 return;
ram54288 0:dbad57390bd1 2011 }
ram54288 0:dbad57390bd1 2012 rf_flags_clear(RFF_CCA);
ram54288 0:dbad57390bd1 2013 /*Check the result of CCA process*/
ram54288 0:dbad57390bd1 2014 if(rf_if_check_cca())
ram54288 0:dbad57390bd1 2015 {
ram54288 0:dbad57390bd1 2016 rf_start_tx();
ram54288 0:dbad57390bd1 2017 }
ram54288 0:dbad57390bd1 2018 else
ram54288 0:dbad57390bd1 2019 {
ram54288 0:dbad57390bd1 2020 /*Re-enable reception*/
ram54288 0:dbad57390bd1 2021 rf_disable_static_frame_buffer_protection();
ram54288 0:dbad57390bd1 2022 /*Send CCA fail notification*/
ram54288 0:dbad57390bd1 2023 if(device_driver.phy_tx_done_cb){
ram54288 0:dbad57390bd1 2024 device_driver.phy_tx_done_cb(rf_radio_driver_id, mac_tx_handle, PHY_LINK_CCA_FAIL, 0, 0);
ram54288 0:dbad57390bd1 2025 }
ram54288 0:dbad57390bd1 2026 }
ram54288 0:dbad57390bd1 2027 }
ram54288 0:dbad57390bd1 2028
ram54288 0:dbad57390bd1 2029 /*
ram54288 0:dbad57390bd1 2030 * \brief Function returns the TX power variable.
ram54288 0:dbad57390bd1 2031 *
ram54288 0:dbad57390bd1 2032 * \param none
ram54288 0:dbad57390bd1 2033 *
ram54288 0:dbad57390bd1 2034 * \return radio_tx_power TX power variable
ram54288 0:dbad57390bd1 2035 */
ram54288 0:dbad57390bd1 2036 MBED_UNUSED static uint8_t rf_tx_power_get(void)
ram54288 0:dbad57390bd1 2037 {
ram54288 0:dbad57390bd1 2038 return radio_tx_power;
ram54288 0:dbad57390bd1 2039 }
ram54288 0:dbad57390bd1 2040
ram54288 0:dbad57390bd1 2041 /*
ram54288 0:dbad57390bd1 2042 * \brief Function enables the usage of Antenna diversity.
ram54288 0:dbad57390bd1 2043 *
ram54288 0:dbad57390bd1 2044 * \param none
ram54288 0:dbad57390bd1 2045 *
ram54288 0:dbad57390bd1 2046 * \return 0 Success
ram54288 0:dbad57390bd1 2047 */
ram54288 0:dbad57390bd1 2048 MBED_UNUSED static int8_t rf_enable_antenna_diversity(void)
ram54288 0:dbad57390bd1 2049 {
ram54288 0:dbad57390bd1 2050 int8_t ret_val = 0;
ram54288 0:dbad57390bd1 2051 rf_use_antenna_diversity = 1;
ram54288 0:dbad57390bd1 2052 return ret_val;
ram54288 0:dbad57390bd1 2053 }
ram54288 0:dbad57390bd1 2054
ram54288 0:dbad57390bd1 2055 /*
ram54288 0:dbad57390bd1 2056 * \brief Function gives the control of RF states to MAC.
ram54288 0:dbad57390bd1 2057 *
ram54288 0:dbad57390bd1 2058 * \param new_state RF state
ram54288 0:dbad57390bd1 2059 * \param rf_channel RF channel
ram54288 0:dbad57390bd1 2060 *
ram54288 0:dbad57390bd1 2061 * \return 0 Success
ram54288 0:dbad57390bd1 2062 */
ram54288 0:dbad57390bd1 2063 static int8_t rf_interface_state_control(phy_interface_state_e new_state, uint8_t rf_channel)
ram54288 0:dbad57390bd1 2064 {
ram54288 0:dbad57390bd1 2065 int8_t ret_val = 0;
ram54288 0:dbad57390bd1 2066 switch (new_state)
ram54288 0:dbad57390bd1 2067 {
ram54288 0:dbad57390bd1 2068 /*Reset PHY driver and set to idle*/
ram54288 0:dbad57390bd1 2069 case PHY_INTERFACE_RESET:
ram54288 0:dbad57390bd1 2070 break;
ram54288 0:dbad57390bd1 2071 /*Disable PHY Interface driver*/
ram54288 0:dbad57390bd1 2072 case PHY_INTERFACE_DOWN:
ram54288 0:dbad57390bd1 2073 rf_shutdown();
ram54288 0:dbad57390bd1 2074 break;
ram54288 0:dbad57390bd1 2075 /*Enable PHY Interface driver*/
ram54288 0:dbad57390bd1 2076 case PHY_INTERFACE_UP:
ram54288 0:dbad57390bd1 2077 rf_mode = RF_MODE_NORMAL;
ram54288 0:dbad57390bd1 2078 rf_channel_set(rf_channel);
ram54288 0:dbad57390bd1 2079 rf_receive();
ram54288 0:dbad57390bd1 2080 rf_if_enable_irq();
ram54288 0:dbad57390bd1 2081 break;
ram54288 0:dbad57390bd1 2082 /*Enable wireless interface ED scan mode*/
ram54288 0:dbad57390bd1 2083 case PHY_INTERFACE_RX_ENERGY_STATE:
ram54288 0:dbad57390bd1 2084 rf_mode = RF_MODE_ED;
ram54288 0:dbad57390bd1 2085 rf_channel_set(rf_channel);
ram54288 0:dbad57390bd1 2086 rf_receive();
ram54288 0:dbad57390bd1 2087 rf_if_disable_irq();
ram54288 0:dbad57390bd1 2088 // Read status to clear pending flags.
ram54288 0:dbad57390bd1 2089 rf_if_read_register(IRQ_STATUS);
ram54288 0:dbad57390bd1 2090 // Must set interrupt mask to be able to read IRQ status. GPIO interrupt is disabled.
ram54288 0:dbad57390bd1 2091 rf_if_enable_cca_ed_done_interrupt();
ram54288 0:dbad57390bd1 2092 // ED can be initiated by writing arbitrary value to PHY_ED_LEVEL
ram54288 0:dbad57390bd1 2093 rf_if_write_register(PHY_ED_LEVEL, 0xff);
ram54288 0:dbad57390bd1 2094 break;
ram54288 0:dbad57390bd1 2095 case PHY_INTERFACE_SNIFFER_STATE: /**< Enable Sniffer state */
ram54288 0:dbad57390bd1 2096 rf_mode = RF_MODE_SNIFFER;
ram54288 0:dbad57390bd1 2097 rf_channel_set(rf_channel);
ram54288 0:dbad57390bd1 2098 rf_flags_clear(RFF_RX);
ram54288 0:dbad57390bd1 2099 rf_receive();
ram54288 0:dbad57390bd1 2100 rf_if_enable_irq();
ram54288 0:dbad57390bd1 2101 break;
ram54288 0:dbad57390bd1 2102 }
ram54288 0:dbad57390bd1 2103 return ret_val;
ram54288 0:dbad57390bd1 2104 }
ram54288 0:dbad57390bd1 2105
ram54288 0:dbad57390bd1 2106 /*
ram54288 0:dbad57390bd1 2107 * \brief Function controls the ACK pending, channel setting and energy detection.
ram54288 0:dbad57390bd1 2108 *
ram54288 0:dbad57390bd1 2109 * \param extension_type Type of control
ram54288 0:dbad57390bd1 2110 * \param data_ptr Data from NET library
ram54288 0:dbad57390bd1 2111 *
ram54288 0:dbad57390bd1 2112 * \return 0 Success
ram54288 0:dbad57390bd1 2113 */
ram54288 0:dbad57390bd1 2114 static int8_t rf_extension(phy_extension_type_e extension_type, uint8_t *data_ptr)
ram54288 0:dbad57390bd1 2115 {
ram54288 0:dbad57390bd1 2116 switch (extension_type)
ram54288 0:dbad57390bd1 2117 {
ram54288 0:dbad57390bd1 2118 /*Control MAC pending bit for Indirect data transmission*/
ram54288 0:dbad57390bd1 2119 case PHY_EXTENSION_CTRL_PENDING_BIT:
ram54288 0:dbad57390bd1 2120 if(*data_ptr)
ram54288 0:dbad57390bd1 2121 {
ram54288 0:dbad57390bd1 2122 rf_if_ack_pending_ctrl(1);
ram54288 0:dbad57390bd1 2123 }
ram54288 0:dbad57390bd1 2124 else
ram54288 0:dbad57390bd1 2125 {
ram54288 0:dbad57390bd1 2126 rf_if_ack_pending_ctrl(0);
ram54288 0:dbad57390bd1 2127 }
ram54288 0:dbad57390bd1 2128 break;
ram54288 0:dbad57390bd1 2129 /*Return frame pending status*/
ram54288 0:dbad57390bd1 2130 case PHY_EXTENSION_READ_LAST_ACK_PENDING_STATUS:
ram54288 0:dbad57390bd1 2131 *data_ptr = rf_if_last_acked_pending();
ram54288 0:dbad57390bd1 2132 break;
ram54288 0:dbad57390bd1 2133 /*Set channel*/
ram54288 0:dbad57390bd1 2134 case PHY_EXTENSION_SET_CHANNEL:
ram54288 0:dbad57390bd1 2135 break;
ram54288 0:dbad57390bd1 2136 /*Read energy on the channel*/
ram54288 0:dbad57390bd1 2137 case PHY_EXTENSION_READ_CHANNEL_ENERGY:
ram54288 0:dbad57390bd1 2138 // End of the ED measurement is indicated by CCA_ED_DONE
ram54288 0:dbad57390bd1 2139 while (!(rf_if_read_register(IRQ_STATUS) & CCA_ED_DONE));
ram54288 0:dbad57390bd1 2140 // RF input power: RSSI base level + 1[db] * PHY_ED_LEVEL
ram54288 0:dbad57390bd1 2141 *data_ptr = rf_sensitivity + rf_if_read_register(PHY_ED_LEVEL);
ram54288 0:dbad57390bd1 2142 // Read status to clear pending flags.
ram54288 0:dbad57390bd1 2143 rf_if_read_register(IRQ_STATUS);
ram54288 0:dbad57390bd1 2144 // Next ED measurement is started, next PHY_EXTENSION_READ_CHANNEL_ENERGY call will return the result.
ram54288 0:dbad57390bd1 2145 rf_if_write_register(PHY_ED_LEVEL, 0xff);
ram54288 0:dbad57390bd1 2146 break;
ram54288 0:dbad57390bd1 2147 /*Read status of the link*/
ram54288 0:dbad57390bd1 2148 case PHY_EXTENSION_READ_LINK_STATUS:
ram54288 0:dbad57390bd1 2149 break;
ram54288 0:dbad57390bd1 2150 default:
ram54288 0:dbad57390bd1 2151 break;
ram54288 0:dbad57390bd1 2152 }
ram54288 0:dbad57390bd1 2153 return 0;
ram54288 0:dbad57390bd1 2154 }
ram54288 0:dbad57390bd1 2155
ram54288 0:dbad57390bd1 2156 /*
ram54288 0:dbad57390bd1 2157 * \brief Function sets the addresses to RF address filters.
ram54288 0:dbad57390bd1 2158 *
ram54288 0:dbad57390bd1 2159 * \param address_type Type of address
ram54288 0:dbad57390bd1 2160 * \param address_ptr Pointer to given address
ram54288 0:dbad57390bd1 2161 *
ram54288 0:dbad57390bd1 2162 * \return 0 Success
ram54288 0:dbad57390bd1 2163 */
ram54288 0:dbad57390bd1 2164 static int8_t rf_address_write(phy_address_type_e address_type, uint8_t *address_ptr)
ram54288 0:dbad57390bd1 2165 {
ram54288 0:dbad57390bd1 2166 int8_t ret_val = 0;
ram54288 0:dbad57390bd1 2167 switch (address_type)
ram54288 0:dbad57390bd1 2168 {
ram54288 0:dbad57390bd1 2169 /*Set 48-bit address*/
ram54288 0:dbad57390bd1 2170 case PHY_MAC_48BIT:
ram54288 0:dbad57390bd1 2171 break;
ram54288 0:dbad57390bd1 2172 /*Set 64-bit address*/
ram54288 0:dbad57390bd1 2173 case PHY_MAC_64BIT:
ram54288 0:dbad57390bd1 2174 rf_set_address(address_ptr);
ram54288 0:dbad57390bd1 2175 break;
ram54288 0:dbad57390bd1 2176 /*Set 16-bit address*/
ram54288 0:dbad57390bd1 2177 case PHY_MAC_16BIT:
ram54288 0:dbad57390bd1 2178 rf_set_short_adr(address_ptr);
ram54288 0:dbad57390bd1 2179 break;
ram54288 0:dbad57390bd1 2180 /*Set PAN Id*/
ram54288 0:dbad57390bd1 2181 case PHY_MAC_PANID:
ram54288 0:dbad57390bd1 2182 rf_set_pan_id(address_ptr);
ram54288 0:dbad57390bd1 2183 break;
ram54288 0:dbad57390bd1 2184 }
ram54288 0:dbad57390bd1 2185 return ret_val;
ram54288 0:dbad57390bd1 2186 }
ram54288 0:dbad57390bd1 2187
ram54288 0:dbad57390bd1 2188 /*
ram54288 0:dbad57390bd1 2189 * \brief Function initialises the ACK wait time and returns the used PHY mode.
ram54288 0:dbad57390bd1 2190 *
ram54288 0:dbad57390bd1 2191 * \param none
ram54288 0:dbad57390bd1 2192 *
ram54288 0:dbad57390bd1 2193 * \return tmp Used PHY mode
ram54288 0:dbad57390bd1 2194 */
ram54288 0:dbad57390bd1 2195 static void rf_init_phy_mode(void)
ram54288 0:dbad57390bd1 2196 {
ram54288 0:dbad57390bd1 2197 uint8_t tmp = 0;
ram54288 0:dbad57390bd1 2198 uint8_t part = rf_if_read_part_num();
ram54288 0:dbad57390bd1 2199 /*Read used PHY Mode*/
ram54288 0:dbad57390bd1 2200 tmp = rf_if_read_register(TRX_CTRL_2);
ram54288 0:dbad57390bd1 2201 /*Set ACK wait time for used data rate*/
ram54288 0:dbad57390bd1 2202 if(part == PART_AT86RF212)
ram54288 0:dbad57390bd1 2203 {
ram54288 0:dbad57390bd1 2204 if((tmp & 0x1f) == 0x00)
ram54288 0:dbad57390bd1 2205 {
ram54288 0:dbad57390bd1 2206 rf_sensitivity = -110;
ram54288 0:dbad57390bd1 2207 rf_ack_wait_duration = 938;
ram54288 0:dbad57390bd1 2208 tmp = BPSK_20;
ram54288 0:dbad57390bd1 2209 }
ram54288 0:dbad57390bd1 2210 else if((tmp & 0x1f) == 0x04)
ram54288 0:dbad57390bd1 2211 {
ram54288 0:dbad57390bd1 2212 rf_sensitivity = -108;
ram54288 0:dbad57390bd1 2213 rf_ack_wait_duration = 469;
ram54288 0:dbad57390bd1 2214 tmp = BPSK_40;
ram54288 0:dbad57390bd1 2215 }
ram54288 0:dbad57390bd1 2216 else if((tmp & 0x1f) == 0x14)
ram54288 0:dbad57390bd1 2217 {
ram54288 0:dbad57390bd1 2218 rf_sensitivity = -108;
ram54288 0:dbad57390bd1 2219 rf_ack_wait_duration = 469;
ram54288 0:dbad57390bd1 2220 tmp = BPSK_40_ALT;
ram54288 0:dbad57390bd1 2221 }
ram54288 0:dbad57390bd1 2222 else if((tmp & 0x1f) == 0x08)
ram54288 0:dbad57390bd1 2223 {
ram54288 0:dbad57390bd1 2224 rf_sensitivity = -101;
ram54288 0:dbad57390bd1 2225 rf_ack_wait_duration = 50;
ram54288 0:dbad57390bd1 2226 tmp = OQPSK_SIN_RC_100;
ram54288 0:dbad57390bd1 2227 }
ram54288 0:dbad57390bd1 2228 else if((tmp & 0x1f) == 0x09)
ram54288 0:dbad57390bd1 2229 {
ram54288 0:dbad57390bd1 2230 rf_sensitivity = -99;
ram54288 0:dbad57390bd1 2231 rf_ack_wait_duration = 30;
ram54288 0:dbad57390bd1 2232 tmp = OQPSK_SIN_RC_200;
ram54288 0:dbad57390bd1 2233 }
ram54288 0:dbad57390bd1 2234 else if((tmp & 0x1f) == 0x18)
ram54288 0:dbad57390bd1 2235 {
ram54288 0:dbad57390bd1 2236 rf_sensitivity = -102;
ram54288 0:dbad57390bd1 2237 rf_ack_wait_duration = 50;
ram54288 0:dbad57390bd1 2238 tmp = OQPSK_RC_100;
ram54288 0:dbad57390bd1 2239 }
ram54288 0:dbad57390bd1 2240 else if((tmp & 0x1f) == 0x19)
ram54288 0:dbad57390bd1 2241 {
ram54288 0:dbad57390bd1 2242 rf_sensitivity = -100;
ram54288 0:dbad57390bd1 2243 rf_ack_wait_duration = 30;
ram54288 0:dbad57390bd1 2244 tmp = OQPSK_RC_200;
ram54288 0:dbad57390bd1 2245 }
ram54288 0:dbad57390bd1 2246 else if((tmp & 0x1f) == 0x0c)
ram54288 0:dbad57390bd1 2247 {
ram54288 0:dbad57390bd1 2248 rf_sensitivity = -100;
ram54288 0:dbad57390bd1 2249 rf_ack_wait_duration = 20;
ram54288 0:dbad57390bd1 2250 tmp = OQPSK_SIN_250;
ram54288 0:dbad57390bd1 2251 }
ram54288 0:dbad57390bd1 2252 else if((tmp & 0x1f) == 0x0d)
ram54288 0:dbad57390bd1 2253 {
ram54288 0:dbad57390bd1 2254 rf_sensitivity = -98;
ram54288 0:dbad57390bd1 2255 rf_ack_wait_duration = 25;
ram54288 0:dbad57390bd1 2256 tmp = OQPSK_SIN_500;
ram54288 0:dbad57390bd1 2257 }
ram54288 0:dbad57390bd1 2258 else if((tmp & 0x1f) == 0x0f)
ram54288 0:dbad57390bd1 2259 {
ram54288 0:dbad57390bd1 2260 rf_sensitivity = -98;
ram54288 0:dbad57390bd1 2261 rf_ack_wait_duration = 25;
ram54288 0:dbad57390bd1 2262 tmp = OQPSK_SIN_500_ALT;
ram54288 0:dbad57390bd1 2263 }
ram54288 0:dbad57390bd1 2264 else if((tmp & 0x1f) == 0x1c)
ram54288 0:dbad57390bd1 2265 {
ram54288 0:dbad57390bd1 2266 rf_sensitivity = -101;
ram54288 0:dbad57390bd1 2267 rf_ack_wait_duration = 20;
ram54288 0:dbad57390bd1 2268 tmp = OQPSK_RC_250;
ram54288 0:dbad57390bd1 2269 }
ram54288 0:dbad57390bd1 2270 else if((tmp & 0x1f) == 0x1d)
ram54288 0:dbad57390bd1 2271 {
ram54288 0:dbad57390bd1 2272 rf_sensitivity = -99;
ram54288 0:dbad57390bd1 2273 rf_ack_wait_duration = 25;
ram54288 0:dbad57390bd1 2274 tmp = OQPSK_RC_500;
ram54288 0:dbad57390bd1 2275 }
ram54288 0:dbad57390bd1 2276 else if((tmp & 0x1f) == 0x1f)
ram54288 0:dbad57390bd1 2277 {
ram54288 0:dbad57390bd1 2278 rf_sensitivity = -99;
ram54288 0:dbad57390bd1 2279 rf_ack_wait_duration = 25;
ram54288 0:dbad57390bd1 2280 tmp = OQPSK_RC_500_ALT;
ram54288 0:dbad57390bd1 2281 }
ram54288 0:dbad57390bd1 2282 else if((tmp & 0x3f) == 0x2A)
ram54288 0:dbad57390bd1 2283 {
ram54288 0:dbad57390bd1 2284 rf_sensitivity = -91;
ram54288 0:dbad57390bd1 2285 rf_ack_wait_duration = 25;
ram54288 0:dbad57390bd1 2286 tmp = OQPSK_SIN_RC_400_SCR_ON;
ram54288 0:dbad57390bd1 2287 }
ram54288 0:dbad57390bd1 2288 else if((tmp & 0x3f) == 0x0A)
ram54288 0:dbad57390bd1 2289 {
ram54288 0:dbad57390bd1 2290 rf_sensitivity = -91;
ram54288 0:dbad57390bd1 2291 rf_ack_wait_duration = 25;
ram54288 0:dbad57390bd1 2292 tmp = OQPSK_SIN_RC_400_SCR_OFF;
ram54288 0:dbad57390bd1 2293 }
ram54288 0:dbad57390bd1 2294 else if((tmp & 0x3f) == 0x3A)
ram54288 0:dbad57390bd1 2295 {
ram54288 0:dbad57390bd1 2296 rf_sensitivity = -97;
ram54288 0:dbad57390bd1 2297 rf_ack_wait_duration = 25;
ram54288 0:dbad57390bd1 2298 tmp = OQPSK_RC_400_SCR_ON;
ram54288 0:dbad57390bd1 2299 }
ram54288 0:dbad57390bd1 2300 else if((tmp & 0x3f) == 0x1A)
ram54288 0:dbad57390bd1 2301 {
ram54288 0:dbad57390bd1 2302 rf_sensitivity = -97;
ram54288 0:dbad57390bd1 2303 rf_ack_wait_duration = 25;
ram54288 0:dbad57390bd1 2304 tmp = OQPSK_RC_400_SCR_OFF;
ram54288 0:dbad57390bd1 2305 }
ram54288 0:dbad57390bd1 2306 else if((tmp & 0x3f) == 0x2E)
ram54288 0:dbad57390bd1 2307 {
ram54288 0:dbad57390bd1 2308 rf_sensitivity = -93;
ram54288 0:dbad57390bd1 2309 rf_ack_wait_duration = 13;
ram54288 0:dbad57390bd1 2310 tmp = OQPSK_SIN_1000_SCR_ON;
ram54288 0:dbad57390bd1 2311 }
ram54288 0:dbad57390bd1 2312 else if((tmp & 0x3f) == 0x0E)
ram54288 0:dbad57390bd1 2313 {
ram54288 0:dbad57390bd1 2314 rf_sensitivity = -93;
ram54288 0:dbad57390bd1 2315 rf_ack_wait_duration = 13;
ram54288 0:dbad57390bd1 2316 tmp = OQPSK_SIN_1000_SCR_OFF;
ram54288 0:dbad57390bd1 2317 }
ram54288 0:dbad57390bd1 2318 else if((tmp & 0x3f) == 0x3E)
ram54288 0:dbad57390bd1 2319 {
ram54288 0:dbad57390bd1 2320 rf_sensitivity = -95;
ram54288 0:dbad57390bd1 2321 rf_ack_wait_duration = 13;
ram54288 0:dbad57390bd1 2322 tmp = OQPSK_RC_1000_SCR_ON;
ram54288 0:dbad57390bd1 2323 }
ram54288 0:dbad57390bd1 2324 else if((tmp & 0x3f) == 0x1E)
ram54288 0:dbad57390bd1 2325 {
ram54288 0:dbad57390bd1 2326 rf_sensitivity = -95;
ram54288 0:dbad57390bd1 2327 rf_ack_wait_duration = 13;
ram54288 0:dbad57390bd1 2328 tmp = OQPSK_RC_1000_SCR_OFF;
ram54288 0:dbad57390bd1 2329 }
ram54288 0:dbad57390bd1 2330 }
ram54288 0:dbad57390bd1 2331 else
ram54288 0:dbad57390bd1 2332 {
ram54288 0:dbad57390bd1 2333 rf_sensitivity = -101;
ram54288 0:dbad57390bd1 2334 rf_ack_wait_duration = 20;
ram54288 0:dbad57390bd1 2335 }
ram54288 0:dbad57390bd1 2336 /*Board design might reduces the sensitivity*/
ram54288 0:dbad57390bd1 2337 //rf_sensitivity += RF_SENSITIVITY_CALIBRATION;
ram54288 0:dbad57390bd1 2338 }
ram54288 0:dbad57390bd1 2339
ram54288 0:dbad57390bd1 2340
ram54288 0:dbad57390bd1 2341 static uint8_t rf_scale_lqi(int8_t rssi)
ram54288 0:dbad57390bd1 2342 {
ram54288 0:dbad57390bd1 2343 uint8_t scaled_lqi;
ram54288 0:dbad57390bd1 2344
ram54288 0:dbad57390bd1 2345 /*rssi < RF sensitivity*/
ram54288 0:dbad57390bd1 2346 if(rssi < rf_sensitivity)
ram54288 0:dbad57390bd1 2347 scaled_lqi=0;
ram54288 0:dbad57390bd1 2348 /*-91 dBm < rssi < -81 dBm (AT86RF233 XPro)*/
ram54288 0:dbad57390bd1 2349 /*-90 dBm < rssi < -80 dBm (AT86RF212B XPro)*/
ram54288 0:dbad57390bd1 2350 else if(rssi < (rf_sensitivity + 10))
ram54288 0:dbad57390bd1 2351 scaled_lqi=31;
ram54288 0:dbad57390bd1 2352 /*-81 dBm < rssi < -71 dBm (AT86RF233 XPro)*/
ram54288 0:dbad57390bd1 2353 /*-80 dBm < rssi < -70 dBm (AT86RF212B XPro)*/
ram54288 0:dbad57390bd1 2354 else if(rssi < (rf_sensitivity + 20))
ram54288 0:dbad57390bd1 2355 scaled_lqi=207;
ram54288 0:dbad57390bd1 2356 /*-71 dBm < rssi < -61 dBm (AT86RF233 XPro)*/
ram54288 0:dbad57390bd1 2357 /*-70 dBm < rssi < -60 dBm (AT86RF212B XPro)*/
ram54288 0:dbad57390bd1 2358 else if(rssi < (rf_sensitivity + 30))
ram54288 0:dbad57390bd1 2359 scaled_lqi=255;
ram54288 0:dbad57390bd1 2360 /*-61 dBm < rssi < -51 dBm (AT86RF233 XPro)*/
ram54288 0:dbad57390bd1 2361 /*-60 dBm < rssi < -50 dBm (AT86RF212B XPro)*/
ram54288 0:dbad57390bd1 2362 else if(rssi < (rf_sensitivity + 40))
ram54288 0:dbad57390bd1 2363 scaled_lqi=255;
ram54288 0:dbad57390bd1 2364 /*-51 dBm < rssi < -41 dBm (AT86RF233 XPro)*/
ram54288 0:dbad57390bd1 2365 /*-50 dBm < rssi < -40 dBm (AT86RF212B XPro)*/
ram54288 0:dbad57390bd1 2366 else if(rssi < (rf_sensitivity + 50))
ram54288 0:dbad57390bd1 2367 scaled_lqi=255;
ram54288 0:dbad57390bd1 2368 /*-41 dBm < rssi < -31 dBm (AT86RF233 XPro)*/
ram54288 0:dbad57390bd1 2369 /*-40 dBm < rssi < -30 dBm (AT86RF212B XPro)*/
ram54288 0:dbad57390bd1 2370 else if(rssi < (rf_sensitivity + 60))
ram54288 0:dbad57390bd1 2371 scaled_lqi=255;
ram54288 0:dbad57390bd1 2372 /*-31 dBm < rssi < -21 dBm (AT86RF233 XPro)*/
ram54288 0:dbad57390bd1 2373 /*-30 dBm < rssi < -20 dBm (AT86RF212B XPro)*/
ram54288 0:dbad57390bd1 2374 else if(rssi < (rf_sensitivity + 70))
ram54288 0:dbad57390bd1 2375 scaled_lqi=255;
ram54288 0:dbad57390bd1 2376 /*rssi > RF saturation*/
ram54288 0:dbad57390bd1 2377 else if(rssi > (rf_sensitivity + 80))
ram54288 0:dbad57390bd1 2378 scaled_lqi=111;
ram54288 0:dbad57390bd1 2379 /*-21 dBm < rssi < -11 dBm (AT86RF233 XPro)*/
ram54288 0:dbad57390bd1 2380 /*-20 dBm < rssi < -10 dBm (AT86RF212B XPro)*/
ram54288 0:dbad57390bd1 2381 else
ram54288 0:dbad57390bd1 2382 scaled_lqi=255;
ram54288 0:dbad57390bd1 2383
ram54288 0:dbad57390bd1 2384 return scaled_lqi;
ram54288 0:dbad57390bd1 2385 }
ram54288 0:dbad57390bd1 2386
ram54288 0:dbad57390bd1 2387 NanostackRfPhyAtmel::NanostackRfPhyAtmel(PinName spi_mosi, PinName spi_miso,
ram54288 0:dbad57390bd1 2388 PinName spi_sclk, PinName spi_cs, PinName spi_rst, PinName spi_slp, PinName spi_irq,
ram54288 0:dbad57390bd1 2389 PinName i2c_sda, PinName i2c_scl)
ram54288 0:dbad57390bd1 2390 : _mac(i2c_sda, i2c_scl), _mac_addr(), _rf(NULL), _mac_set(false),
ram54288 0:dbad57390bd1 2391 _spi_mosi(spi_mosi), _spi_miso(spi_miso), _spi_sclk(spi_sclk),
ram54288 0:dbad57390bd1 2392 _spi_cs(spi_cs), _spi_rst(spi_rst), _spi_slp(spi_slp), _spi_irq(spi_irq)
ram54288 0:dbad57390bd1 2393 {
ram54288 0:dbad57390bd1 2394 _rf = new RFBits(_spi_mosi, _spi_miso, _spi_sclk, _spi_cs, _spi_rst, _spi_slp, _spi_irq);
ram54288 0:dbad57390bd1 2395 }
ram54288 0:dbad57390bd1 2396
ram54288 0:dbad57390bd1 2397 NanostackRfPhyAtmel::~NanostackRfPhyAtmel()
ram54288 0:dbad57390bd1 2398 {
ram54288 0:dbad57390bd1 2399 delete _rf;
ram54288 0:dbad57390bd1 2400 }
ram54288 0:dbad57390bd1 2401
ram54288 0:dbad57390bd1 2402 int8_t NanostackRfPhyAtmel::rf_register()
ram54288 0:dbad57390bd1 2403 {
ram54288 0:dbad57390bd1 2404 if (NULL == _rf) {
ram54288 0:dbad57390bd1 2405 return -1;
ram54288 0:dbad57390bd1 2406 }
ram54288 0:dbad57390bd1 2407
ram54288 0:dbad57390bd1 2408 rf_if_lock();
ram54288 0:dbad57390bd1 2409
ram54288 0:dbad57390bd1 2410 if (rf != NULL) {
ram54288 0:dbad57390bd1 2411 rf_if_unlock();
ram54288 0:dbad57390bd1 2412 error("Multiple registrations of NanostackRfPhyAtmel not supported");
ram54288 0:dbad57390bd1 2413 return -1;
ram54288 0:dbad57390bd1 2414 }
ram54288 0:dbad57390bd1 2415
ram54288 0:dbad57390bd1 2416 // Read the mac address if it hasn't been set by a user
ram54288 0:dbad57390bd1 2417 rf = _rf;
ram54288 0:dbad57390bd1 2418 if (!_mac_set) {
ram54288 0:dbad57390bd1 2419 int ret = _mac.read_eui64((void*)_mac_addr);
ram54288 0:dbad57390bd1 2420 if (ret < 0) {
ram54288 0:dbad57390bd1 2421 rf = NULL;
ram54288 0:dbad57390bd1 2422 rf_if_unlock();
ram54288 0:dbad57390bd1 2423 return -1;
ram54288 0:dbad57390bd1 2424 }
ram54288 0:dbad57390bd1 2425 }
ram54288 0:dbad57390bd1 2426
ram54288 0:dbad57390bd1 2427 int8_t radio_id = rf_device_register(_mac_addr);
ram54288 0:dbad57390bd1 2428 if (radio_id < 0) {
ram54288 0:dbad57390bd1 2429 rf = NULL;
ram54288 0:dbad57390bd1 2430 }
ram54288 0:dbad57390bd1 2431
ram54288 0:dbad57390bd1 2432 rf_if_unlock();
ram54288 0:dbad57390bd1 2433 return radio_id;
ram54288 0:dbad57390bd1 2434 }
ram54288 0:dbad57390bd1 2435
ram54288 0:dbad57390bd1 2436 void NanostackRfPhyAtmel::rf_unregister()
ram54288 0:dbad57390bd1 2437 {
ram54288 0:dbad57390bd1 2438 rf_if_lock();
ram54288 0:dbad57390bd1 2439
ram54288 0:dbad57390bd1 2440 if (NULL == rf) {
ram54288 0:dbad57390bd1 2441 rf_if_unlock();
ram54288 0:dbad57390bd1 2442 return;
ram54288 0:dbad57390bd1 2443 }
ram54288 0:dbad57390bd1 2444
ram54288 0:dbad57390bd1 2445 rf_device_unregister();
ram54288 0:dbad57390bd1 2446 rf = NULL;
ram54288 0:dbad57390bd1 2447
ram54288 0:dbad57390bd1 2448 rf_if_unlock();
ram54288 0:dbad57390bd1 2449 }
ram54288 0:dbad57390bd1 2450
ram54288 0:dbad57390bd1 2451 void NanostackRfPhyAtmel::get_mac_address(uint8_t *mac)
ram54288 0:dbad57390bd1 2452 {
ram54288 0:dbad57390bd1 2453 rf_if_lock();
ram54288 0:dbad57390bd1 2454
ram54288 0:dbad57390bd1 2455 if (NULL == rf) {
ram54288 0:dbad57390bd1 2456 error("NanostackRfPhyAtmel Must be registered to read mac address");
ram54288 0:dbad57390bd1 2457 rf_if_unlock();
ram54288 0:dbad57390bd1 2458 return;
ram54288 0:dbad57390bd1 2459 }
ram54288 0:dbad57390bd1 2460 memcpy((void*)mac, (void*)_mac_addr, sizeof(_mac_addr));
ram54288 0:dbad57390bd1 2461
ram54288 0:dbad57390bd1 2462 rf_if_unlock();
ram54288 0:dbad57390bd1 2463 }
ram54288 0:dbad57390bd1 2464
ram54288 0:dbad57390bd1 2465 void NanostackRfPhyAtmel::set_mac_address(uint8_t *mac)
ram54288 0:dbad57390bd1 2466 {
ram54288 0:dbad57390bd1 2467 rf_if_lock();
ram54288 0:dbad57390bd1 2468
ram54288 0:dbad57390bd1 2469 if (NULL != rf) {
ram54288 0:dbad57390bd1 2470 error("NanostackRfPhyAtmel cannot change mac address when running");
ram54288 0:dbad57390bd1 2471 rf_if_unlock();
ram54288 0:dbad57390bd1 2472 return;
ram54288 0:dbad57390bd1 2473 }
ram54288 0:dbad57390bd1 2474 memcpy((void*)_mac_addr, (void*)mac, sizeof(_mac_addr));
ram54288 0:dbad57390bd1 2475 _mac_set = true;
ram54288 0:dbad57390bd1 2476
ram54288 0:dbad57390bd1 2477 rf_if_unlock();
ram54288 0:dbad57390bd1 2478 }
ram54288 0:dbad57390bd1 2479