The MCR20A Wireless UART application functions as an wireless UART bridge between two (one-to-one) or several (one to many) boards. The application can be used with both a TERM, or with software that is capable of opening a serial port and writing to or reading from it. The characters sent or received are not necessarily ASCII printable characters.

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_wireless_uart by Freescale

By default, the application uses broadcast addresses for OTA communication. This way, the application can be directly downloaded and run without any user intervention. The following use case assumes no changes have been done to the project.

  • Two (or more) MCR20A platforms (plugged into the FRDM-K64F Freescale Freedom Development platform) have to be connected to the PC using the mini/micro-USB cables.
  • The code must be downloaded on the platforms via CMSIS-DAP (or other means).
  • After that, two or more TERM applications must be opened, and the serial ports must be configured with the same baud rate as the one in the project (default baud rate is 115200). Other necessary serial configurations are 8 bit, no parity, and 1 stop bit.
  • To start the setup, each platform must be reset, and one of the (user) push buttons found on the MCR20A platform must be pressed. The user can press any of the non-reset buttons on the FRDM-K64F Freescale Freedom Development platform as well. *This initiates the state machine of the application so user can start.

Documentation

SMAC Demo Applications User Guide

Committer:
sam_grove
Date:
Thu Mar 05 20:35:27 2015 +0000
Revision:
4:d47832caea44
Parent:
RF_Drivers/driverRFPhy.c@2:3e7685cfb2a7
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 2:3e7685cfb2a7 1 /*
sam_grove 2:3e7685cfb2a7 2 * driverRFPhy.c
sam_grove 2:3e7685cfb2a7 3 *
sam_grove 2:3e7685cfb2a7 4 * Created on: 14 July 2014
sam_grove 2:3e7685cfb2a7 5 * Author: mBed team
sam_grove 2:3e7685cfb2a7 6 */
sam_grove 2:3e7685cfb2a7 7 #include "arm_hal_interrupt.h"
sam_grove 2:3e7685cfb2a7 8 #include "arm_hal_phy.h"
sam_grove 2:3e7685cfb2a7 9 #include "driverRFPhy.h"
sam_grove 2:3e7685cfb2a7 10 #include "driverAtmelRFInterface.h"
sam_grove 2:3e7685cfb2a7 11 #include <string.h>
sam_grove 2:3e7685cfb2a7 12
sam_grove 2:3e7685cfb2a7 13 #include <stdio.h>
sam_grove 2:3e7685cfb2a7 14
sam_grove 2:3e7685cfb2a7 15 #include "options.h"
sam_grove 2:3e7685cfb2a7 16
sam_grove 2:3e7685cfb2a7 17 /*RF receive buffer*/
sam_grove 2:3e7685cfb2a7 18 static uint8_t rf_buffer[RF_BUFFER_SIZE];
sam_grove 2:3e7685cfb2a7 19 /*RF ACK receive buffer*/
sam_grove 2:3e7685cfb2a7 20 static uint8_t ack_rx_buf[5];
sam_grove 2:3e7685cfb2a7 21 /*ACK wait duration changes depending on data rate*/
sam_grove 2:3e7685cfb2a7 22 static uint16_t rf_ack_wait_duration = RF_ACK_WAIT_TIMEOUT;
sam_grove 2:3e7685cfb2a7 23
sam_grove 2:3e7685cfb2a7 24 static uint8_t radio_tx_power = 0x07;
sam_grove 2:3e7685cfb2a7 25 static uint8_t rf_channel;
sam_grove 2:3e7685cfb2a7 26 static uint8_t rf_tuned = 1;
sam_grove 2:3e7685cfb2a7 27 static uint8_t radio_rpc_value = 0xef;
sam_grove 2:3e7685cfb2a7 28 static uint8_t rf_use_front_end = 0;
sam_grove 2:3e7685cfb2a7 29 static uint8_t rf_use_antenna_diversity = 0;
sam_grove 2:3e7685cfb2a7 30 static uint8_t rf_csd_port = 0;
sam_grove 2:3e7685cfb2a7 31 static uint8_t rf_csd_pin = 0;
sam_grove 2:3e7685cfb2a7 32 static uint8_t rf_cps_port = 0;
sam_grove 2:3e7685cfb2a7 33 static uint8_t rf_cps_pin = 0;
sam_grove 2:3e7685cfb2a7 34 static uint8_t tx_sequence = 0xff;
sam_grove 2:3e7685cfb2a7 35 static uint8_t need_ack = 0;
sam_grove 2:3e7685cfb2a7 36 static uint8_t rf_rx_mode = 0;
sam_grove 2:3e7685cfb2a7 37 static uint8_t rf_flags = 0;
sam_grove 2:3e7685cfb2a7 38 static uint8_t rf_rnd_rssi = 0;
sam_grove 2:3e7685cfb2a7 39 static int8_t rf_radio_driver_id = -1;
sam_grove 2:3e7685cfb2a7 40 static phy_device_driver_s device_driver;
sam_grove 2:3e7685cfb2a7 41 static uint8_t atmel_MAC[8];
sam_grove 2:3e7685cfb2a7 42 static phy_device_channel_info_s channel_info;
sam_grove 2:3e7685cfb2a7 43 static uint8_t mac_tx_handle = 0;
sam_grove 2:3e7685cfb2a7 44
sam_grove 2:3e7685cfb2a7 45 /*
sam_grove 2:3e7685cfb2a7 46 * \brief Function sets given RF flag on.
sam_grove 2:3e7685cfb2a7 47 *
sam_grove 2:3e7685cfb2a7 48 * \param x Given RF flag
sam_grove 2:3e7685cfb2a7 49 *
sam_grove 2:3e7685cfb2a7 50 * \return none
sam_grove 2:3e7685cfb2a7 51 */
sam_grove 2:3e7685cfb2a7 52 void rf_flags_set(uint8_t x)
sam_grove 2:3e7685cfb2a7 53 {
sam_grove 2:3e7685cfb2a7 54 rf_flags |= x;
sam_grove 2:3e7685cfb2a7 55 }
sam_grove 2:3e7685cfb2a7 56
sam_grove 2:3e7685cfb2a7 57 /*
sam_grove 2:3e7685cfb2a7 58 * \brief Function clears given RF flag on.
sam_grove 2:3e7685cfb2a7 59 *
sam_grove 2:3e7685cfb2a7 60 * \param x Given RF flag
sam_grove 2:3e7685cfb2a7 61 *
sam_grove 2:3e7685cfb2a7 62 * \return none
sam_grove 2:3e7685cfb2a7 63 */
sam_grove 2:3e7685cfb2a7 64 void rf_flags_clear(uint8_t x)
sam_grove 2:3e7685cfb2a7 65 {
sam_grove 2:3e7685cfb2a7 66 rf_flags &= ~x;
sam_grove 2:3e7685cfb2a7 67 }
sam_grove 2:3e7685cfb2a7 68
sam_grove 2:3e7685cfb2a7 69 /*
sam_grove 2:3e7685cfb2a7 70 * \brief Function checks if given RF flag is on.
sam_grove 2:3e7685cfb2a7 71 *
sam_grove 2:3e7685cfb2a7 72 * \param x Given RF flag
sam_grove 2:3e7685cfb2a7 73 *
sam_grove 2:3e7685cfb2a7 74 * \return states of the given flags
sam_grove 2:3e7685cfb2a7 75 */
sam_grove 2:3e7685cfb2a7 76 uint8_t rf_flags_check(uint8_t x)
sam_grove 2:3e7685cfb2a7 77 {
sam_grove 2:3e7685cfb2a7 78 return (rf_flags & x);
sam_grove 2:3e7685cfb2a7 79 }
sam_grove 2:3e7685cfb2a7 80
sam_grove 2:3e7685cfb2a7 81 /*
sam_grove 2:3e7685cfb2a7 82 * \brief Function clears all RF flags.
sam_grove 2:3e7685cfb2a7 83 *
sam_grove 2:3e7685cfb2a7 84 * \param none
sam_grove 2:3e7685cfb2a7 85 *
sam_grove 2:3e7685cfb2a7 86 * \return none
sam_grove 2:3e7685cfb2a7 87 */
sam_grove 2:3e7685cfb2a7 88 void rf_flags_reset(void)
sam_grove 2:3e7685cfb2a7 89 {
sam_grove 2:3e7685cfb2a7 90 rf_flags = 0;
sam_grove 2:3e7685cfb2a7 91 }
sam_grove 2:3e7685cfb2a7 92
sam_grove 2:3e7685cfb2a7 93 /*
sam_grove 2:3e7685cfb2a7 94 * \brief Function sets CPS and CSD pins of the Front end.
sam_grove 2:3e7685cfb2a7 95 *
sam_grove 2:3e7685cfb2a7 96 * \param none
sam_grove 2:3e7685cfb2a7 97 *
sam_grove 2:3e7685cfb2a7 98 * \return none
sam_grove 2:3e7685cfb2a7 99 */
sam_grove 2:3e7685cfb2a7 100 void rf_front_end_rx_lna(void)
sam_grove 2:3e7685cfb2a7 101 {
sam_grove 2:3e7685cfb2a7 102 /* not supported in this version */
sam_grove 2:3e7685cfb2a7 103 }
sam_grove 2:3e7685cfb2a7 104
sam_grove 2:3e7685cfb2a7 105 /*
sam_grove 2:3e7685cfb2a7 106 * \brief Function clears CPS and CSD pins of the Front end.
sam_grove 2:3e7685cfb2a7 107 *
sam_grove 2:3e7685cfb2a7 108 * \param none
sam_grove 2:3e7685cfb2a7 109 *
sam_grove 2:3e7685cfb2a7 110 * \return none
sam_grove 2:3e7685cfb2a7 111 */
sam_grove 2:3e7685cfb2a7 112 void rf_front_end_sleep(void)
sam_grove 2:3e7685cfb2a7 113 {
sam_grove 2:3e7685cfb2a7 114 /* not supported in this version */
sam_grove 2:3e7685cfb2a7 115 }
sam_grove 2:3e7685cfb2a7 116
sam_grove 2:3e7685cfb2a7 117 /*
sam_grove 2:3e7685cfb2a7 118 * \brief Function initialises and registers the RF driver.
sam_grove 2:3e7685cfb2a7 119 *
sam_grove 2:3e7685cfb2a7 120 * \param none
sam_grove 2:3e7685cfb2a7 121 *
sam_grove 2:3e7685cfb2a7 122 * \return rf_radio_driver_id Driver ID given by NET library
sam_grove 2:3e7685cfb2a7 123 */
sam_grove 2:3e7685cfb2a7 124 int8_t rf_device_register(void)
sam_grove 2:3e7685cfb2a7 125 {
sam_grove 2:3e7685cfb2a7 126 rf_init();
sam_grove 2:3e7685cfb2a7 127 /*Set pointer to MAC address*/
sam_grove 2:3e7685cfb2a7 128 device_driver.PHY_MAC = atmel_MAC;
sam_grove 2:3e7685cfb2a7 129 device_driver.driver_description = "ATMEL_MAC";
sam_grove 2:3e7685cfb2a7 130 #if PHY_LINK_15_4_2_4GHZ_TYPE
sam_grove 2:3e7685cfb2a7 131 /*Number of channels in PHY*/
sam_grove 2:3e7685cfb2a7 132 channel_info.channel_count = 16;
sam_grove 2:3e7685cfb2a7 133 /*Channel mask 26-11*/
sam_grove 2:3e7685cfb2a7 134 channel_info.channel_mask = 0x07FFF800;
sam_grove 2:3e7685cfb2a7 135 /*Type of RF PHY is SubGHz*/
sam_grove 2:3e7685cfb2a7 136 device_driver.link_type = PHY_LINK_15_4_2_4GHZ_TYPE;
sam_grove 2:3e7685cfb2a7 137 device_driver.link_channel_info = &channel_info;
sam_grove 2:3e7685cfb2a7 138 #else
sam_grove 2:3e7685cfb2a7 139 /*Number of channels in PHY*/
sam_grove 2:3e7685cfb2a7 140 channel_info.channel_count = 11;
sam_grove 2:3e7685cfb2a7 141 /*Channel mask 0-10*/
sam_grove 2:3e7685cfb2a7 142 channel_info.channel_mask = 0x000007ff;
sam_grove 2:3e7685cfb2a7 143 /*Type of RF PHY is SubGHz*/
sam_grove 2:3e7685cfb2a7 144 device_driver.link_type = PHY_LINK_15_4_SUBGHZ_TYPE;
sam_grove 2:3e7685cfb2a7 145 device_driver.link_channel_info = &channel_info;
sam_grove 2:3e7685cfb2a7 146 #endif
sam_grove 2:3e7685cfb2a7 147 /*Maximum size of payload is 127*/
sam_grove 2:3e7685cfb2a7 148 device_driver.phy_MTU = 127;
sam_grove 2:3e7685cfb2a7 149 /*No header in PHY*/
sam_grove 2:3e7685cfb2a7 150 device_driver.phy_header_length = 0;
sam_grove 2:3e7685cfb2a7 151 /*No tail in PHY*/
sam_grove 2:3e7685cfb2a7 152 device_driver.phy_tail_length = 0;
sam_grove 2:3e7685cfb2a7 153 /*Set address write function*/
sam_grove 2:3e7685cfb2a7 154 device_driver.phy_xx_address_write = &rf_address_write;
sam_grove 2:3e7685cfb2a7 155 /*Set RF extension function*/
sam_grove 2:3e7685cfb2a7 156 device_driver.phy_xx_extension = &rf_extension;
sam_grove 2:3e7685cfb2a7 157 /*Set RF state control function*/
sam_grove 2:3e7685cfb2a7 158 device_driver.phy_xx_state_control = &rf_interface_state_control;
sam_grove 2:3e7685cfb2a7 159 /*Set transmit function*/
sam_grove 2:3e7685cfb2a7 160 device_driver.phy_xx_tx = &rf_start_cca;
sam_grove 2:3e7685cfb2a7 161 printf("RF Device Registration...");
sam_grove 2:3e7685cfb2a7 162 /*Register device driver*/
sam_grove 2:3e7685cfb2a7 163 rf_radio_driver_id = arm_net_phy_register(&device_driver);
sam_grove 2:3e7685cfb2a7 164 printf("OK\r\n");
sam_grove 2:3e7685cfb2a7 165 return rf_radio_driver_id;
sam_grove 2:3e7685cfb2a7 166 }
sam_grove 2:3e7685cfb2a7 167
sam_grove 2:3e7685cfb2a7 168 /*
sam_grove 2:3e7685cfb2a7 169 * \brief Function returns the generated 8-bit random value for seeding Pseudo-random generator. This value was generated by reading noise from RF channel in RF initialisation.
sam_grove 2:3e7685cfb2a7 170 *
sam_grove 2:3e7685cfb2a7 171 * \param none
sam_grove 2:3e7685cfb2a7 172 *
sam_grove 2:3e7685cfb2a7 173 * \return random RSSI value
sam_grove 2:3e7685cfb2a7 174 */
sam_grove 2:3e7685cfb2a7 175 int8_t rf_read_random(void)
sam_grove 2:3e7685cfb2a7 176 {
sam_grove 2:3e7685cfb2a7 177 return rf_rnd_rssi;
sam_grove 2:3e7685cfb2a7 178 }
sam_grove 2:3e7685cfb2a7 179
sam_grove 2:3e7685cfb2a7 180 /*
sam_grove 2:3e7685cfb2a7 181 * \brief Function is a call back for ACK wait timeout.
sam_grove 2:3e7685cfb2a7 182 *
sam_grove 2:3e7685cfb2a7 183 * \param none
sam_grove 2:3e7685cfb2a7 184 *
sam_grove 2:3e7685cfb2a7 185 * \return none
sam_grove 2:3e7685cfb2a7 186 */
sam_grove 2:3e7685cfb2a7 187 void rf_ack_wait_timer_interrupt(void)
sam_grove 2:3e7685cfb2a7 188 {
sam_grove 2:3e7685cfb2a7 189 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 190 /*Force PLL state*/
sam_grove 2:3e7685cfb2a7 191 rf_if_change_trx_state(FORCE_PLL_ON);
sam_grove 2:3e7685cfb2a7 192 rf_poll_trx_state_change(PLL_ON);
sam_grove 2:3e7685cfb2a7 193 /*Start receiver in RX_AACK_ON state*/
sam_grove 2:3e7685cfb2a7 194 rf_rx_mode = 0;
sam_grove 2:3e7685cfb2a7 195 rf_flags_clear(RFF_RX);
sam_grove 2:3e7685cfb2a7 196 rf_receive();
sam_grove 2:3e7685cfb2a7 197 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 198 }
sam_grove 2:3e7685cfb2a7 199
sam_grove 2:3e7685cfb2a7 200 /*
sam_grove 2:3e7685cfb2a7 201 * \brief Function is a call back for calibration interval timer.
sam_grove 2:3e7685cfb2a7 202 *
sam_grove 2:3e7685cfb2a7 203 * \param none
sam_grove 2:3e7685cfb2a7 204 *
sam_grove 2:3e7685cfb2a7 205 * \return none
sam_grove 2:3e7685cfb2a7 206 */
sam_grove 2:3e7685cfb2a7 207 void rf_calibration_timer_interrupt(void)
sam_grove 2:3e7685cfb2a7 208 {
sam_grove 2:3e7685cfb2a7 209 /*Calibrate RF*/
sam_grove 2:3e7685cfb2a7 210 rf_calibration_cb();
sam_grove 2:3e7685cfb2a7 211 /*Start new calibration timeout*/
sam_grove 2:3e7685cfb2a7 212 rf_calibration_timer_start(RF_CALIBRATION_INTERVAL);
sam_grove 2:3e7685cfb2a7 213 }
sam_grove 2:3e7685cfb2a7 214
sam_grove 2:3e7685cfb2a7 215 /*
sam_grove 2:3e7685cfb2a7 216 * \brief Function initialises the RF timer for ACK wait and calibration.
sam_grove 2:3e7685cfb2a7 217 *
sam_grove 2:3e7685cfb2a7 218 * \param none
sam_grove 2:3e7685cfb2a7 219 *
sam_grove 2:3e7685cfb2a7 220 * \return none
sam_grove 2:3e7685cfb2a7 221 */
sam_grove 2:3e7685cfb2a7 222 void rf_timer_init(void)
sam_grove 2:3e7685cfb2a7 223 {
sam_grove 2:3e7685cfb2a7 224 rf_if_timer_init();
sam_grove 2:3e7685cfb2a7 225 }
sam_grove 2:3e7685cfb2a7 226
sam_grove 2:3e7685cfb2a7 227 /*
sam_grove 2:3e7685cfb2a7 228 * \brief Function starts the ACK wait timeout.
sam_grove 2:3e7685cfb2a7 229 *
sam_grove 2:3e7685cfb2a7 230 * \param slots Given slots, resolution 50us
sam_grove 2:3e7685cfb2a7 231 *
sam_grove 2:3e7685cfb2a7 232 * \return none
sam_grove 2:3e7685cfb2a7 233 */
sam_grove 2:3e7685cfb2a7 234 void rf_ack_wait_timer_start(uint16_t slots)
sam_grove 2:3e7685cfb2a7 235 {
sam_grove 2:3e7685cfb2a7 236 rf_if_ack_wait_timer_start(slots);
sam_grove 2:3e7685cfb2a7 237 }
sam_grove 2:3e7685cfb2a7 238
sam_grove 2:3e7685cfb2a7 239 /*
sam_grove 2:3e7685cfb2a7 240 * \brief Function starts the calibration interval.
sam_grove 2:3e7685cfb2a7 241 *
sam_grove 2:3e7685cfb2a7 242 * \param slots Given slots, resolution 50us
sam_grove 2:3e7685cfb2a7 243 *
sam_grove 2:3e7685cfb2a7 244 * \return none
sam_grove 2:3e7685cfb2a7 245 */
sam_grove 2:3e7685cfb2a7 246 void rf_calibration_timer_start(uint32_t slots)
sam_grove 2:3e7685cfb2a7 247 {
sam_grove 2:3e7685cfb2a7 248 rf_if_calibration_timer_start(slots);
sam_grove 2:3e7685cfb2a7 249 }
sam_grove 2:3e7685cfb2a7 250
sam_grove 2:3e7685cfb2a7 251 /*
sam_grove 2:3e7685cfb2a7 252 * \brief Function stops the ACK wait timeout.
sam_grove 2:3e7685cfb2a7 253 *
sam_grove 2:3e7685cfb2a7 254 * \param none
sam_grove 2:3e7685cfb2a7 255 *
sam_grove 2:3e7685cfb2a7 256 * \return none
sam_grove 2:3e7685cfb2a7 257 */
sam_grove 2:3e7685cfb2a7 258 void rf_ack_wait_timer_stop(void)
sam_grove 2:3e7685cfb2a7 259 {
sam_grove 2:3e7685cfb2a7 260 rf_if_ack_wait_timer_stop();
sam_grove 2:3e7685cfb2a7 261 }
sam_grove 2:3e7685cfb2a7 262
sam_grove 2:3e7685cfb2a7 263 /*
sam_grove 2:3e7685cfb2a7 264 * \brief Function reads the MAC address array.
sam_grove 2:3e7685cfb2a7 265 *
sam_grove 2:3e7685cfb2a7 266 * \param ptr Pointer to read array
sam_grove 2:3e7685cfb2a7 267 *
sam_grove 2:3e7685cfb2a7 268 * \return none
sam_grove 2:3e7685cfb2a7 269 */
sam_grove 2:3e7685cfb2a7 270 void rf_read_mac_address(uint8_t *ptr)
sam_grove 2:3e7685cfb2a7 271 {
sam_grove 2:3e7685cfb2a7 272 memcpy(ptr, atmel_MAC, 8);
sam_grove 2:3e7685cfb2a7 273 }
sam_grove 2:3e7685cfb2a7 274
sam_grove 2:3e7685cfb2a7 275 /*
sam_grove 2:3e7685cfb2a7 276 * \brief Function sets the MAC address array.
sam_grove 2:3e7685cfb2a7 277 *
sam_grove 2:3e7685cfb2a7 278 * \param ptr Pointer to given MAC address array
sam_grove 2:3e7685cfb2a7 279 *
sam_grove 2:3e7685cfb2a7 280 * \return none
sam_grove 2:3e7685cfb2a7 281 */
sam_grove 2:3e7685cfb2a7 282 void rf_set_mac_address(const uint8_t *ptr)
sam_grove 2:3e7685cfb2a7 283 {
sam_grove 2:3e7685cfb2a7 284 memcpy(atmel_MAC,ptr,8);
sam_grove 2:3e7685cfb2a7 285 }
sam_grove 2:3e7685cfb2a7 286
sam_grove 2:3e7685cfb2a7 287 /*
sam_grove 2:3e7685cfb2a7 288 * \brief Function writes various RF settings in startup.
sam_grove 2:3e7685cfb2a7 289 *
sam_grove 2:3e7685cfb2a7 290 * \param none
sam_grove 2:3e7685cfb2a7 291 *
sam_grove 2:3e7685cfb2a7 292 * \return none
sam_grove 2:3e7685cfb2a7 293 */
sam_grove 2:3e7685cfb2a7 294 void rf_write_settings(void)
sam_grove 2:3e7685cfb2a7 295 {
sam_grove 2:3e7685cfb2a7 296 int i, j = 0;
sam_grove 2:3e7685cfb2a7 297
sam_grove 2:3e7685cfb2a7 298 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 299
sam_grove 2:3e7685cfb2a7 300 //printf("RF Write Settings: 1\r\n");
sam_grove 2:3e7685cfb2a7 301 rf_if_write_rf_settings();
sam_grove 2:3e7685cfb2a7 302
sam_grove 2:3e7685cfb2a7 303 //printf("RF Write Settings: 2\r\n");
sam_grove 2:3e7685cfb2a7 304 /*Set output power*/
sam_grove 2:3e7685cfb2a7 305 rf_if_write_set_tx_power_register(radio_tx_power);
sam_grove 2:3e7685cfb2a7 306
sam_grove 2:3e7685cfb2a7 307 //printf("RF Write Settings: 3\r\n");
sam_grove 2:3e7685cfb2a7 308 /*Set RPC register*/
sam_grove 2:3e7685cfb2a7 309 rf_if_write_set_trx_rpc_register(radio_rpc_value);
sam_grove 2:3e7685cfb2a7 310
sam_grove 2:3e7685cfb2a7 311 //printf("RF Write Settings: 4\r\n");
sam_grove 2:3e7685cfb2a7 312 /*Initialise Front end*/
sam_grove 2:3e7685cfb2a7 313 if(rf_use_front_end)
sam_grove 2:3e7685cfb2a7 314 {
sam_grove 2:3e7685cfb2a7 315 printf("RF Front End used\r\n");
sam_grove 2:3e7685cfb2a7 316 rf_if_enable_pa_ext();
sam_grove 2:3e7685cfb2a7 317 /* not supported in this version */
sam_grove 2:3e7685cfb2a7 318 }
sam_grove 2:3e7685cfb2a7 319
sam_grove 2:3e7685cfb2a7 320 //printf("RF Write Settings: 5\r\n");
sam_grove 2:3e7685cfb2a7 321 /*Initialise Antenna Diversity*/
sam_grove 2:3e7685cfb2a7 322 if(rf_use_antenna_diversity) {
sam_grove 2:3e7685cfb2a7 323 printf("RF Antenna diversity\r\n");
sam_grove 2:3e7685cfb2a7 324 rf_if_write_antenna_diversity_settings();
sam_grove 2:3e7685cfb2a7 325 }
sam_grove 2:3e7685cfb2a7 326
sam_grove 2:3e7685cfb2a7 327 printf("RF Write Settings: 7\r\n");
sam_grove 2:3e7685cfb2a7 328 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 329 printf("RF Write Settings End\r\n");
sam_grove 2:3e7685cfb2a7 330 }
sam_grove 2:3e7685cfb2a7 331
sam_grove 2:3e7685cfb2a7 332 /*
sam_grove 2:3e7685cfb2a7 333 * \brief Function writes 16-bit address in RF address filter.
sam_grove 2:3e7685cfb2a7 334 *
sam_grove 2:3e7685cfb2a7 335 * \param short_address Given short address
sam_grove 2:3e7685cfb2a7 336 *
sam_grove 2:3e7685cfb2a7 337 * \return none
sam_grove 2:3e7685cfb2a7 338 */
sam_grove 2:3e7685cfb2a7 339 void rf_set_short_adr(uint8_t * short_address)
sam_grove 2:3e7685cfb2a7 340 {
sam_grove 2:3e7685cfb2a7 341 uint8_t rf_off_flag = 0;
sam_grove 2:3e7685cfb2a7 342 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 343 /*Wake up RF if sleeping*/
sam_grove 2:3e7685cfb2a7 344 if(rf_if_read_trx_state() == 0x00 || rf_if_read_trx_state() == 0x1F)
sam_grove 2:3e7685cfb2a7 345 {
sam_grove 2:3e7685cfb2a7 346 rf_if_disable_slptr();
sam_grove 2:3e7685cfb2a7 347 rf_off_flag = 1;
sam_grove 2:3e7685cfb2a7 348 rf_poll_trx_state_change(TRX_OFF);
sam_grove 2:3e7685cfb2a7 349 }
sam_grove 2:3e7685cfb2a7 350 /*Write address filter registers*/
sam_grove 2:3e7685cfb2a7 351 rf_if_write_short_addr_registers(short_address);
sam_grove 2:3e7685cfb2a7 352 /*RF back to sleep*/
sam_grove 2:3e7685cfb2a7 353 if(rf_off_flag)
sam_grove 2:3e7685cfb2a7 354 rf_if_enable_slptr();
sam_grove 2:3e7685cfb2a7 355 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 356 }
sam_grove 2:3e7685cfb2a7 357
sam_grove 2:3e7685cfb2a7 358 /*
sam_grove 2:3e7685cfb2a7 359 * \brief Function writes PAN Id in RF PAN Id filter.
sam_grove 2:3e7685cfb2a7 360 *
sam_grove 2:3e7685cfb2a7 361 * \param pan_id Given PAN Id
sam_grove 2:3e7685cfb2a7 362 *
sam_grove 2:3e7685cfb2a7 363 * \return none
sam_grove 2:3e7685cfb2a7 364 */
sam_grove 2:3e7685cfb2a7 365 void rf_set_pan_id(uint8_t *pan_id)
sam_grove 2:3e7685cfb2a7 366 {
sam_grove 2:3e7685cfb2a7 367 uint8_t rf_off_flag = 0;
sam_grove 2:3e7685cfb2a7 368
sam_grove 2:3e7685cfb2a7 369 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 370 /*Wake up RF if sleeping*/
sam_grove 2:3e7685cfb2a7 371 if(rf_if_read_trx_state() == 0x00 || rf_if_read_trx_state() == 0x1F)
sam_grove 2:3e7685cfb2a7 372 {
sam_grove 2:3e7685cfb2a7 373 rf_if_disable_slptr();
sam_grove 2:3e7685cfb2a7 374 rf_off_flag = 1;
sam_grove 2:3e7685cfb2a7 375 rf_poll_trx_state_change(TRX_OFF);
sam_grove 2:3e7685cfb2a7 376 }
sam_grove 2:3e7685cfb2a7 377 /*Write address filter registers*/
sam_grove 2:3e7685cfb2a7 378 rf_if_write_pan_id_registers(pan_id);
sam_grove 2:3e7685cfb2a7 379 /*RF back to sleep*/
sam_grove 2:3e7685cfb2a7 380 if(rf_off_flag)
sam_grove 2:3e7685cfb2a7 381 rf_if_enable_slptr();
sam_grove 2:3e7685cfb2a7 382 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 383 }
sam_grove 2:3e7685cfb2a7 384
sam_grove 2:3e7685cfb2a7 385 /*
sam_grove 2:3e7685cfb2a7 386 * \brief Function writes 64-bit address in RF address filter.
sam_grove 2:3e7685cfb2a7 387 *
sam_grove 2:3e7685cfb2a7 388 * \param address Given 64-bit address
sam_grove 2:3e7685cfb2a7 389 *
sam_grove 2:3e7685cfb2a7 390 * \return none
sam_grove 2:3e7685cfb2a7 391 */
sam_grove 2:3e7685cfb2a7 392 void rf_set_address(uint8_t *address)
sam_grove 2:3e7685cfb2a7 393 {
sam_grove 2:3e7685cfb2a7 394 uint8_t rf_off_flag = 0;
sam_grove 2:3e7685cfb2a7 395
sam_grove 2:3e7685cfb2a7 396 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 397 /*Wake up RF if sleeping*/
sam_grove 2:3e7685cfb2a7 398 if(rf_if_read_trx_state() == 0x00 || rf_if_read_trx_state() == 0x1F)
sam_grove 2:3e7685cfb2a7 399 {
sam_grove 2:3e7685cfb2a7 400 rf_if_disable_slptr();
sam_grove 2:3e7685cfb2a7 401 rf_off_flag = 1;
sam_grove 2:3e7685cfb2a7 402 rf_poll_trx_state_change(TRX_OFF);
sam_grove 2:3e7685cfb2a7 403 }
sam_grove 2:3e7685cfb2a7 404 /*Write address filter registers*/
sam_grove 2:3e7685cfb2a7 405 rf_if_write_ieee_addr_registers(address);
sam_grove 2:3e7685cfb2a7 406 /*RF back to sleep*/
sam_grove 2:3e7685cfb2a7 407 if(rf_off_flag)
sam_grove 2:3e7685cfb2a7 408 rf_if_enable_slptr();
sam_grove 2:3e7685cfb2a7 409
sam_grove 2:3e7685cfb2a7 410 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 411 }
sam_grove 2:3e7685cfb2a7 412
sam_grove 2:3e7685cfb2a7 413 /*
sam_grove 2:3e7685cfb2a7 414 * \brief Function sets the RF channel.
sam_grove 2:3e7685cfb2a7 415 *
sam_grove 2:3e7685cfb2a7 416 * \param ch New channel
sam_grove 2:3e7685cfb2a7 417 *
sam_grove 2:3e7685cfb2a7 418 * \return none
sam_grove 2:3e7685cfb2a7 419 */
sam_grove 2:3e7685cfb2a7 420 void rf_channel_set(uint8_t ch)
sam_grove 2:3e7685cfb2a7 421 {
sam_grove 2:3e7685cfb2a7 422 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 423 rf_channel = ch;
sam_grove 2:3e7685cfb2a7 424 if(ch < 0x1f)
sam_grove 2:3e7685cfb2a7 425 rf_if_set_channel_register(ch);
sam_grove 2:3e7685cfb2a7 426 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 427 }
sam_grove 2:3e7685cfb2a7 428
sam_grove 2:3e7685cfb2a7 429
sam_grove 2:3e7685cfb2a7 430 /*
sam_grove 2:3e7685cfb2a7 431 * \brief Function initialises the radio driver and resets the radio.
sam_grove 2:3e7685cfb2a7 432 *
sam_grove 2:3e7685cfb2a7 433 * \param none
sam_grove 2:3e7685cfb2a7 434 *
sam_grove 2:3e7685cfb2a7 435 * \return none
sam_grove 2:3e7685cfb2a7 436 */
sam_grove 2:3e7685cfb2a7 437 void rf_init(void)
sam_grove 2:3e7685cfb2a7 438 {
sam_grove 2:3e7685cfb2a7 439 printf("RF Init Start\r\n");
sam_grove 2:3e7685cfb2a7 440 /*Initialise timers*/
sam_grove 2:3e7685cfb2a7 441 rf_timer_init(); //TODO
sam_grove 2:3e7685cfb2a7 442 rf_channel = RF_DEFAULT_CHANNEL;
sam_grove 2:3e7685cfb2a7 443 printf("RF Reset\r\n");
sam_grove 2:3e7685cfb2a7 444 /*Reset RF module*/
sam_grove 2:3e7685cfb2a7 445 rf_if_reset_radio();
sam_grove 2:3e7685cfb2a7 446 printf("RF Write Settings\r\n");
sam_grove 2:3e7685cfb2a7 447 /*Write RF settings*/
sam_grove 2:3e7685cfb2a7 448 rf_write_settings();
sam_grove 2:3e7685cfb2a7 449 printf("RF Init PHY Mode\r\n");
sam_grove 2:3e7685cfb2a7 450 /*Initialise PHY mode*/
sam_grove 2:3e7685cfb2a7 451 rf_init_phy_mode();
sam_grove 2:3e7685cfb2a7 452 /*Clear RF flags*/
sam_grove 2:3e7685cfb2a7 453 rf_flags_reset();
sam_grove 2:3e7685cfb2a7 454 /*Set RF in TRX OFF state*/
sam_grove 2:3e7685cfb2a7 455 rf_if_change_trx_state(TRX_OFF);
sam_grove 2:3e7685cfb2a7 456 /*Set RF in PLL_ON state*/
sam_grove 2:3e7685cfb2a7 457 rf_if_change_trx_state(PLL_ON);
sam_grove 2:3e7685cfb2a7 458 /*Start receiver*/
sam_grove 2:3e7685cfb2a7 459 rf_receive();
sam_grove 2:3e7685cfb2a7 460 /*Read random variable. This will be used when seeding pseudo-random generator*/
sam_grove 2:3e7685cfb2a7 461 rf_rnd_rssi = rf_if_read_rnd();
sam_grove 2:3e7685cfb2a7 462 /*Start RF calibration timer*/
sam_grove 2:3e7685cfb2a7 463 rf_calibration_timer_start(RF_CALIBRATION_INTERVAL); //ACA!
sam_grove 2:3e7685cfb2a7 464 printf("RF Init End\r\n");
sam_grove 2:3e7685cfb2a7 465 }
sam_grove 2:3e7685cfb2a7 466
sam_grove 2:3e7685cfb2a7 467 /**
sam_grove 2:3e7685cfb2a7 468 * \brief Function gets called when MAC is setting radio off.
sam_grove 2:3e7685cfb2a7 469 *
sam_grove 2:3e7685cfb2a7 470 * \param none
sam_grove 2:3e7685cfb2a7 471 *
sam_grove 2:3e7685cfb2a7 472 * \return none
sam_grove 2:3e7685cfb2a7 473 */
sam_grove 2:3e7685cfb2a7 474 void rf_off(void)
sam_grove 2:3e7685cfb2a7 475 {
sam_grove 2:3e7685cfb2a7 476 if(rf_flags_check(RFF_ON))
sam_grove 2:3e7685cfb2a7 477 {
sam_grove 2:3e7685cfb2a7 478 rf_cca_abort();
sam_grove 2:3e7685cfb2a7 479 uint16_t while_counter = 0;
sam_grove 2:3e7685cfb2a7 480 /*Wait while receiving*/
sam_grove 2:3e7685cfb2a7 481 while(rf_if_read_trx_state() == BUSY_RX_AACK || rf_if_read_trx_state() == BUSY_RX)
sam_grove 2:3e7685cfb2a7 482 {
sam_grove 2:3e7685cfb2a7 483 while_counter++;
sam_grove 2:3e7685cfb2a7 484 if(while_counter == 0xffff)
sam_grove 2:3e7685cfb2a7 485 break;
sam_grove 2:3e7685cfb2a7 486 }
sam_grove 2:3e7685cfb2a7 487 /*RF state change: RX_AACK_ON->PLL_ON->TRX_OFF->SLEEP*/
sam_grove 2:3e7685cfb2a7 488 if(rf_if_read_trx_state() == RX_AACK_ON)
sam_grove 2:3e7685cfb2a7 489 {
sam_grove 2:3e7685cfb2a7 490 rf_if_change_trx_state(PLL_ON);
sam_grove 2:3e7685cfb2a7 491 }
sam_grove 2:3e7685cfb2a7 492 rf_if_change_trx_state(TRX_OFF);
sam_grove 2:3e7685cfb2a7 493 rf_if_enable_slptr();
sam_grove 2:3e7685cfb2a7 494 rf_flags_clear(~RFF_ON);
sam_grove 2:3e7685cfb2a7 495 /*Front end in sleep*/
sam_grove 2:3e7685cfb2a7 496 if(rf_use_front_end)
sam_grove 2:3e7685cfb2a7 497 {
sam_grove 2:3e7685cfb2a7 498 rf_if_disable_pa_ext();
sam_grove 2:3e7685cfb2a7 499 rf_front_end_sleep();
sam_grove 2:3e7685cfb2a7 500 }
sam_grove 2:3e7685cfb2a7 501 /*Disable Antenna Diversity*/
sam_grove 2:3e7685cfb2a7 502 if(rf_use_antenna_diversity)
sam_grove 2:3e7685cfb2a7 503 rf_if_disable_ant_div();
sam_grove 2:3e7685cfb2a7 504 }
sam_grove 2:3e7685cfb2a7 505 }
sam_grove 2:3e7685cfb2a7 506
sam_grove 2:3e7685cfb2a7 507 /*
sam_grove 2:3e7685cfb2a7 508 * \brief Function polls the RF state until it has changed to desired state.
sam_grove 2:3e7685cfb2a7 509 *
sam_grove 2:3e7685cfb2a7 510 * \param trx_state RF state
sam_grove 2:3e7685cfb2a7 511 *
sam_grove 2:3e7685cfb2a7 512 * \return none
sam_grove 2:3e7685cfb2a7 513 */
sam_grove 2:3e7685cfb2a7 514 void rf_poll_trx_state_change(rf_trx_states_t trx_state)
sam_grove 2:3e7685cfb2a7 515 {
sam_grove 2:3e7685cfb2a7 516 uint16_t while_counter = 0;
sam_grove 2:3e7685cfb2a7 517 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 518
sam_grove 2:3e7685cfb2a7 519 if(trx_state != RF_TX_START)
sam_grove 2:3e7685cfb2a7 520 {
sam_grove 2:3e7685cfb2a7 521 if(trx_state == FORCE_PLL_ON)
sam_grove 2:3e7685cfb2a7 522 trx_state = PLL_ON;
sam_grove 2:3e7685cfb2a7 523 else if(trx_state == FORCE_TRX_OFF)
sam_grove 2:3e7685cfb2a7 524 trx_state = TRX_OFF;
sam_grove 2:3e7685cfb2a7 525
sam_grove 2:3e7685cfb2a7 526 while(rf_if_read_trx_state() != trx_state)
sam_grove 2:3e7685cfb2a7 527 {
sam_grove 2:3e7685cfb2a7 528 while_counter++;
sam_grove 2:3e7685cfb2a7 529 if(while_counter == 0x1ff)
sam_grove 2:3e7685cfb2a7 530 break;
sam_grove 2:3e7685cfb2a7 531 }
sam_grove 2:3e7685cfb2a7 532 }
sam_grove 2:3e7685cfb2a7 533 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 534 }
sam_grove 2:3e7685cfb2a7 535
sam_grove 2:3e7685cfb2a7 536 /*
sam_grove 2:3e7685cfb2a7 537 * \brief Function starts the CCA process before starting data transmission and copies the data to RF TX FIFO.
sam_grove 2:3e7685cfb2a7 538 *
sam_grove 2:3e7685cfb2a7 539 * \param data_ptr Pointer to TX data
sam_grove 2:3e7685cfb2a7 540 * \param data_length Length of the TX data
sam_grove 2:3e7685cfb2a7 541 * \param tx_handle Handle to transmission
sam_grove 2:3e7685cfb2a7 542 * \return 0 Success
sam_grove 2:3e7685cfb2a7 543 * \return -1 Busy
sam_grove 2:3e7685cfb2a7 544 */
sam_grove 2:3e7685cfb2a7 545 int8_t rf_start_cca(uint8_t *data_ptr, uint16_t data_length, uint8_t tx_handle)
sam_grove 2:3e7685cfb2a7 546 {
sam_grove 2:3e7685cfb2a7 547 /*Check if transmitter is busy*/
sam_grove 2:3e7685cfb2a7 548 if((rf_if_read_trx_state() == BUSY_RX_AACK) || (rf_if_read_trx_state() == BUSY_RX))
sam_grove 2:3e7685cfb2a7 549 {
sam_grove 2:3e7685cfb2a7 550 /*Return busy*/
sam_grove 2:3e7685cfb2a7 551 return -1;
sam_grove 2:3e7685cfb2a7 552 }
sam_grove 2:3e7685cfb2a7 553 else
sam_grove 2:3e7685cfb2a7 554 {
sam_grove 2:3e7685cfb2a7 555 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 556 /*Check if transmitted data needs to be acked*/
sam_grove 2:3e7685cfb2a7 557 if(*data_ptr & 0x20)
sam_grove 2:3e7685cfb2a7 558 need_ack = 1;
sam_grove 2:3e7685cfb2a7 559 else
sam_grove 2:3e7685cfb2a7 560 need_ack = 0;
sam_grove 2:3e7685cfb2a7 561 /*Store the sequence number for ACK handling*/
sam_grove 2:3e7685cfb2a7 562 tx_sequence = *(data_ptr + 2);
sam_grove 2:3e7685cfb2a7 563 /*Set radio in RX state to read channel*/
sam_grove 2:3e7685cfb2a7 564 rf_receive();
sam_grove 2:3e7685cfb2a7 565 /*Write TX FIFO*/
sam_grove 2:3e7685cfb2a7 566 rf_if_write_frame_buffer(data_ptr, (uint8_t)data_length);
sam_grove 2:3e7685cfb2a7 567 rf_flags_set(RFF_CCA);
sam_grove 2:3e7685cfb2a7 568 /*Start CCA process*/
sam_grove 2:3e7685cfb2a7 569 rf_if_enable_cca_ed_done_interrupt();
sam_grove 2:3e7685cfb2a7 570 rf_if_start_cca_process();
sam_grove 2:3e7685cfb2a7 571 /*Store TX handle*/
sam_grove 2:3e7685cfb2a7 572 mac_tx_handle = tx_handle;
sam_grove 2:3e7685cfb2a7 573 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 574 }
sam_grove 2:3e7685cfb2a7 575
sam_grove 2:3e7685cfb2a7 576 /*Return success*/
sam_grove 2:3e7685cfb2a7 577 return 0;
sam_grove 2:3e7685cfb2a7 578 }
sam_grove 2:3e7685cfb2a7 579
sam_grove 2:3e7685cfb2a7 580 /*
sam_grove 2:3e7685cfb2a7 581 * \brief Function aborts CCA process.
sam_grove 2:3e7685cfb2a7 582 *
sam_grove 2:3e7685cfb2a7 583 * \param none
sam_grove 2:3e7685cfb2a7 584 *
sam_grove 2:3e7685cfb2a7 585 * \return none
sam_grove 2:3e7685cfb2a7 586 */
sam_grove 2:3e7685cfb2a7 587 void rf_cca_abort(void)
sam_grove 2:3e7685cfb2a7 588 {
sam_grove 2:3e7685cfb2a7 589 /*Clear RFF_CCA RF flag*/
sam_grove 2:3e7685cfb2a7 590 rf_flags_clear(RFF_CCA);
sam_grove 2:3e7685cfb2a7 591 }
sam_grove 2:3e7685cfb2a7 592
sam_grove 2:3e7685cfb2a7 593
sam_grove 2:3e7685cfb2a7 594
sam_grove 2:3e7685cfb2a7 595 /*
sam_grove 2:3e7685cfb2a7 596 * \brief Function starts the transmission of the frame.
sam_grove 2:3e7685cfb2a7 597 *
sam_grove 2:3e7685cfb2a7 598 * \param none
sam_grove 2:3e7685cfb2a7 599 *
sam_grove 2:3e7685cfb2a7 600 * \return none
sam_grove 2:3e7685cfb2a7 601 */
sam_grove 2:3e7685cfb2a7 602 void rf_start_tx(void)
sam_grove 2:3e7685cfb2a7 603 {
sam_grove 2:3e7685cfb2a7 604 /*Only start transmitting from RX state*/
sam_grove 2:3e7685cfb2a7 605 uint8_t trx_state = rf_if_read_trx_state();
sam_grove 2:3e7685cfb2a7 606 if((trx_state != RX_AACK_ON) && (trx_state != RX_ON))
sam_grove 2:3e7685cfb2a7 607 {
sam_grove 2:3e7685cfb2a7 608 arm_net_phy_tx_done(rf_radio_driver_id, mac_tx_handle, PHY_LINK_CCA_FAIL, 1, 1);
sam_grove 2:3e7685cfb2a7 609 }
sam_grove 2:3e7685cfb2a7 610 else
sam_grove 2:3e7685cfb2a7 611 {
sam_grove 2:3e7685cfb2a7 612 /*RF state change: ->PLL_ON->RF_TX_START*/
sam_grove 2:3e7685cfb2a7 613 rf_if_change_trx_state(FORCE_PLL_ON);
sam_grove 2:3e7685cfb2a7 614 rf_flags_clear(RFF_RX);
sam_grove 2:3e7685cfb2a7 615 rf_if_enable_tx_end_interrupt();
sam_grove 2:3e7685cfb2a7 616 rf_flags_set(RFF_TX);
sam_grove 2:3e7685cfb2a7 617 rf_if_change_trx_state(RF_TX_START);
sam_grove 2:3e7685cfb2a7 618 }
sam_grove 2:3e7685cfb2a7 619 }
sam_grove 2:3e7685cfb2a7 620
sam_grove 2:3e7685cfb2a7 621 /*
sam_grove 2:3e7685cfb2a7 622 * \brief Function sets the RF in RX state.
sam_grove 2:3e7685cfb2a7 623 *
sam_grove 2:3e7685cfb2a7 624 * \param none
sam_grove 2:3e7685cfb2a7 625 *
sam_grove 2:3e7685cfb2a7 626 * \return none
sam_grove 2:3e7685cfb2a7 627 */
sam_grove 2:3e7685cfb2a7 628 void rf_receive(void)
sam_grove 2:3e7685cfb2a7 629 {
sam_grove 2:3e7685cfb2a7 630 uint16_t while_counter = 0;
sam_grove 2:3e7685cfb2a7 631 if(rf_flags_check(RFF_ON) == 0)
sam_grove 2:3e7685cfb2a7 632 {
sam_grove 2:3e7685cfb2a7 633 rf_on();
sam_grove 2:3e7685cfb2a7 634 }
sam_grove 2:3e7685cfb2a7 635 /*If not yet in RX state set it*/
sam_grove 2:3e7685cfb2a7 636 if(rf_flags_check(RFF_RX) == 0)
sam_grove 2:3e7685cfb2a7 637 {
sam_grove 2:3e7685cfb2a7 638 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 639 /*Wait while receiving data*/
sam_grove 2:3e7685cfb2a7 640 while((rf_if_read_trx_state() == BUSY_RX) || (rf_if_read_trx_state() == BUSY_RX_AACK))
sam_grove 2:3e7685cfb2a7 641 {
sam_grove 2:3e7685cfb2a7 642 while_counter++;
sam_grove 2:3e7685cfb2a7 643 if(while_counter == 0xffff)
sam_grove 2:3e7685cfb2a7 644 {
sam_grove 2:3e7685cfb2a7 645 break;
sam_grove 2:3e7685cfb2a7 646 }
sam_grove 2:3e7685cfb2a7 647 }
sam_grove 2:3e7685cfb2a7 648 /*Wake up from sleep state*/
sam_grove 2:3e7685cfb2a7 649 if(rf_if_read_trx_state() == 0x00 || rf_if_read_trx_state() == 0x1f)
sam_grove 2:3e7685cfb2a7 650 {
sam_grove 2:3e7685cfb2a7 651 rf_if_disable_slptr();
sam_grove 2:3e7685cfb2a7 652 rf_poll_trx_state_change(TRX_OFF);
sam_grove 2:3e7685cfb2a7 653 }
sam_grove 2:3e7685cfb2a7 654
sam_grove 2:3e7685cfb2a7 655 rf_if_change_trx_state(PLL_ON);
sam_grove 2:3e7685cfb2a7 656 /*ACK is always received in RX_ON state to bypass address filters*/
sam_grove 2:3e7685cfb2a7 657 if(rf_rx_mode)
sam_grove 2:3e7685cfb2a7 658 {
sam_grove 2:3e7685cfb2a7 659 rf_rx_mode = 0;
sam_grove 2:3e7685cfb2a7 660 rf_if_change_trx_state(RX_ON);
sam_grove 2:3e7685cfb2a7 661 }
sam_grove 2:3e7685cfb2a7 662 else
sam_grove 2:3e7685cfb2a7 663 {
sam_grove 2:3e7685cfb2a7 664 rf_if_change_trx_state(RX_AACK_ON);
sam_grove 2:3e7685cfb2a7 665 /*If calibration timer was unable to calibrate the RF, run calibration now*/
sam_grove 2:3e7685cfb2a7 666 if(!rf_tuned)
sam_grove 2:3e7685cfb2a7 667 {
sam_grove 2:3e7685cfb2a7 668 /*Start calibration. This can be done in states TRX_OFF, PLL_ON or in any receive state*/
sam_grove 2:3e7685cfb2a7 669 rf_if_calibration();
sam_grove 2:3e7685cfb2a7 670 /*RF is tuned now*/
sam_grove 2:3e7685cfb2a7 671 rf_tuned = 1;
sam_grove 2:3e7685cfb2a7 672 }
sam_grove 2:3e7685cfb2a7 673 }
sam_grove 2:3e7685cfb2a7 674 rf_channel_set(rf_channel);
sam_grove 2:3e7685cfb2a7 675 rf_flags_set(RFF_RX);
sam_grove 2:3e7685cfb2a7 676 rf_if_enable_rx_end_interrupt();
sam_grove 2:3e7685cfb2a7 677 /*Enable LNA if Front end used*/
sam_grove 2:3e7685cfb2a7 678 if(rf_use_front_end)
sam_grove 2:3e7685cfb2a7 679 rf_front_end_rx_lna();
sam_grove 2:3e7685cfb2a7 680 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 681 }
sam_grove 2:3e7685cfb2a7 682 /*Stop the running CCA process*/
sam_grove 2:3e7685cfb2a7 683 if(rf_flags_check(RFF_CCA))
sam_grove 2:3e7685cfb2a7 684 rf_cca_abort();
sam_grove 2:3e7685cfb2a7 685 }
sam_grove 2:3e7685cfb2a7 686
sam_grove 2:3e7685cfb2a7 687 /*
sam_grove 2:3e7685cfb2a7 688 * \brief Function calibrates the radio.
sam_grove 2:3e7685cfb2a7 689 *
sam_grove 2:3e7685cfb2a7 690 * \param none
sam_grove 2:3e7685cfb2a7 691 *
sam_grove 2:3e7685cfb2a7 692 * \return none
sam_grove 2:3e7685cfb2a7 693 */
sam_grove 2:3e7685cfb2a7 694 void rf_calibration_cb(void)
sam_grove 2:3e7685cfb2a7 695 {
sam_grove 2:3e7685cfb2a7 696 /*clear tuned flag to start tuning in rf_receive*/
sam_grove 2:3e7685cfb2a7 697 rf_tuned = 0;
sam_grove 2:3e7685cfb2a7 698 /*If RF is in default receive state, start calibration*/
sam_grove 2:3e7685cfb2a7 699 if(rf_if_read_trx_state() == RX_AACK_ON)
sam_grove 2:3e7685cfb2a7 700 {
sam_grove 2:3e7685cfb2a7 701 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 702 /*Set RF in PLL_ON state*/
sam_grove 2:3e7685cfb2a7 703 rf_if_change_trx_state(PLL_ON);
sam_grove 2:3e7685cfb2a7 704 /*Set RF in TRX_OFF state to start PLL tuning*/
sam_grove 2:3e7685cfb2a7 705 rf_if_change_trx_state(TRX_OFF);
sam_grove 2:3e7685cfb2a7 706 /*Set RF in RX_ON state to calibrate*/
sam_grove 2:3e7685cfb2a7 707 rf_if_change_trx_state(RX_ON);
sam_grove 2:3e7685cfb2a7 708 /*Calibrate FTN*/
sam_grove 2:3e7685cfb2a7 709 rf_if_calibration();
sam_grove 2:3e7685cfb2a7 710 /*RF is tuned now*/
sam_grove 2:3e7685cfb2a7 711 rf_tuned = 1;
sam_grove 2:3e7685cfb2a7 712 /*Back to default receive state*/
sam_grove 2:3e7685cfb2a7 713 rf_flags_clear(RFF_RX);
sam_grove 2:3e7685cfb2a7 714 rf_receive();
sam_grove 2:3e7685cfb2a7 715 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 716 }
sam_grove 2:3e7685cfb2a7 717 }
sam_grove 2:3e7685cfb2a7 718
sam_grove 2:3e7685cfb2a7 719 /*
sam_grove 2:3e7685cfb2a7 720 * \brief Function sets RF_ON flag when radio is powered.
sam_grove 2:3e7685cfb2a7 721 *
sam_grove 2:3e7685cfb2a7 722 * \param none
sam_grove 2:3e7685cfb2a7 723 *
sam_grove 2:3e7685cfb2a7 724 * \return none
sam_grove 2:3e7685cfb2a7 725 */
sam_grove 2:3e7685cfb2a7 726 void rf_on(void)
sam_grove 2:3e7685cfb2a7 727 {
sam_grove 2:3e7685cfb2a7 728 /*Set RFF_ON flag*/
sam_grove 2:3e7685cfb2a7 729 if(rf_flags_check(RFF_ON) == 0)
sam_grove 2:3e7685cfb2a7 730 {
sam_grove 2:3e7685cfb2a7 731 rf_flags_set(RFF_ON);
sam_grove 2:3e7685cfb2a7 732 /*Wake up Front end*/
sam_grove 2:3e7685cfb2a7 733 if(rf_use_front_end)
sam_grove 2:3e7685cfb2a7 734 {
sam_grove 2:3e7685cfb2a7 735 /*Set PA_EXT_EN to enable controlling of external front end*/
sam_grove 2:3e7685cfb2a7 736 rf_if_enable_pa_ext();
sam_grove 2:3e7685cfb2a7 737 rf_front_end_rx_lna();
sam_grove 2:3e7685cfb2a7 738 }
sam_grove 2:3e7685cfb2a7 739 /*Enable Antenna diversity*/
sam_grove 2:3e7685cfb2a7 740 if(rf_use_antenna_diversity)
sam_grove 2:3e7685cfb2a7 741 /*Set ANT_EXT_SW_EN to enable controlling of antenna diversity*/
sam_grove 2:3e7685cfb2a7 742 rf_if_enable_ant_div();
sam_grove 2:3e7685cfb2a7 743 }
sam_grove 2:3e7685cfb2a7 744 }
sam_grove 2:3e7685cfb2a7 745
sam_grove 2:3e7685cfb2a7 746 /*
sam_grove 2:3e7685cfb2a7 747 * \brief Function handles the received ACK frame.
sam_grove 2:3e7685cfb2a7 748 *
sam_grove 2:3e7685cfb2a7 749 * \param seq_number Sequence number of received ACK
sam_grove 2:3e7685cfb2a7 750 * \param data_pending Pending bit state in received ACK
sam_grove 2:3e7685cfb2a7 751 *
sam_grove 2:3e7685cfb2a7 752 * \return none
sam_grove 2:3e7685cfb2a7 753 */
sam_grove 2:3e7685cfb2a7 754 void rf_handle_ack(uint8_t seq_number, uint8_t data_pending)
sam_grove 2:3e7685cfb2a7 755 {
sam_grove 2:3e7685cfb2a7 756 phy_link_tx_status_e phy_status;
sam_grove 2:3e7685cfb2a7 757 arm_enter_critical();
sam_grove 2:3e7685cfb2a7 758 /*Received ACK sequence must be equal with transmitted packet sequence*/
sam_grove 2:3e7685cfb2a7 759 if(tx_sequence == seq_number)
sam_grove 2:3e7685cfb2a7 760 {
sam_grove 2:3e7685cfb2a7 761 rf_ack_wait_timer_stop();
sam_grove 2:3e7685cfb2a7 762 /*When data pending bit in ACK frame is set, inform NET library*/
sam_grove 2:3e7685cfb2a7 763 if(data_pending)
sam_grove 2:3e7685cfb2a7 764 phy_status = PHY_LINK_TX_DONE_PENDING;
sam_grove 2:3e7685cfb2a7 765 else
sam_grove 2:3e7685cfb2a7 766 phy_status = PHY_LINK_TX_DONE;
sam_grove 2:3e7685cfb2a7 767 /*Call PHY TX Done API*/
sam_grove 2:3e7685cfb2a7 768 arm_net_phy_tx_done(rf_radio_driver_id, mac_tx_handle,phy_status, 1, 1);
sam_grove 2:3e7685cfb2a7 769 }
sam_grove 2:3e7685cfb2a7 770 arm_exit_critical();
sam_grove 2:3e7685cfb2a7 771 }
sam_grove 2:3e7685cfb2a7 772
sam_grove 2:3e7685cfb2a7 773 /*
sam_grove 2:3e7685cfb2a7 774 * \brief Function is a call back for RX end interrupt.
sam_grove 2:3e7685cfb2a7 775 *
sam_grove 2:3e7685cfb2a7 776 * \param none
sam_grove 2:3e7685cfb2a7 777 *
sam_grove 2:3e7685cfb2a7 778 * \return none
sam_grove 2:3e7685cfb2a7 779 */
sam_grove 2:3e7685cfb2a7 780 void rf_handle_rx_end(void)
sam_grove 2:3e7685cfb2a7 781 {
sam_grove 2:3e7685cfb2a7 782 uint8_t rf_lqi;
sam_grove 2:3e7685cfb2a7 783
sam_grove 2:3e7685cfb2a7 784 /*Frame received interrupt*/
sam_grove 2:3e7685cfb2a7 785 if(rf_flags_check(RFF_RX))
sam_grove 2:3e7685cfb2a7 786 {
sam_grove 2:3e7685cfb2a7 787 /*Check CRC_valid bit*/
sam_grove 2:3e7685cfb2a7 788 if(rf_if_check_crc())
sam_grove 2:3e7685cfb2a7 789 {
sam_grove 2:3e7685cfb2a7 790 uint8_t *rf_rx_ptr;
sam_grove 2:3e7685cfb2a7 791 uint8_t receiving_ack = 0;
sam_grove 2:3e7685cfb2a7 792 /*Read length*/
sam_grove 2:3e7685cfb2a7 793 uint8_t len = rf_if_read_received_frame_length();
sam_grove 2:3e7685cfb2a7 794 /*Not ACK frame*/
sam_grove 2:3e7685cfb2a7 795 if(len > 5)
sam_grove 2:3e7685cfb2a7 796 {
sam_grove 2:3e7685cfb2a7 797 rf_rx_ptr = rf_buffer;
sam_grove 2:3e7685cfb2a7 798 }
sam_grove 2:3e7685cfb2a7 799 /*ACK received*/
sam_grove 2:3e7685cfb2a7 800 else
sam_grove 2:3e7685cfb2a7 801 {
sam_grove 2:3e7685cfb2a7 802 /*Read ACK in static ACK buffer*/
sam_grove 2:3e7685cfb2a7 803 receiving_ack = 1;
sam_grove 2:3e7685cfb2a7 804 rf_rx_ptr = ack_rx_buf;
sam_grove 2:3e7685cfb2a7 805 }
sam_grove 2:3e7685cfb2a7 806 /*Check the length is valid*/
sam_grove 2:3e7685cfb2a7 807 if(len > 1 && len < RF_BUFFER_SIZE)
sam_grove 2:3e7685cfb2a7 808 {
sam_grove 2:3e7685cfb2a7 809 /*Read received packet*/
sam_grove 2:3e7685cfb2a7 810 rf_if_read_packet(rf_rx_ptr, len);
sam_grove 2:3e7685cfb2a7 811 /*Get LQI*/
sam_grove 2:3e7685cfb2a7 812 rf_lqi = rf_if_read_lqi();
sam_grove 2:3e7685cfb2a7 813 /*Handle received ACK*/
sam_grove 2:3e7685cfb2a7 814 if(receiving_ack && ((ack_rx_buf[0] & 0x07) == 0x02))
sam_grove 2:3e7685cfb2a7 815 {
sam_grove 2:3e7685cfb2a7 816 uint8_t pending = 0;
sam_grove 2:3e7685cfb2a7 817 /*Check if data is pending*/
sam_grove 2:3e7685cfb2a7 818 if ((ack_rx_buf[0] & 0x10))
sam_grove 2:3e7685cfb2a7 819 {
sam_grove 2:3e7685cfb2a7 820 pending=1;
sam_grove 2:3e7685cfb2a7 821 }
sam_grove 2:3e7685cfb2a7 822 /*Send sequence number in ACK handler*/
sam_grove 2:3e7685cfb2a7 823 rf_handle_ack(ack_rx_buf[2], pending);
sam_grove 2:3e7685cfb2a7 824 }
sam_grove 2:3e7685cfb2a7 825 /*Handle received data*/
sam_grove 2:3e7685cfb2a7 826 else if(rf_if_read_trx_state() != RX_ON && rf_if_read_trx_state() != BUSY_RX)
sam_grove 2:3e7685cfb2a7 827 {
sam_grove 2:3e7685cfb2a7 828 arm_net_phy_rx(rf_buffer,len - 2, rf_lqi, rf_radio_driver_id);
sam_grove 2:3e7685cfb2a7 829 }
sam_grove 2:3e7685cfb2a7 830 }
sam_grove 2:3e7685cfb2a7 831 }
sam_grove 2:3e7685cfb2a7 832 }
sam_grove 2:3e7685cfb2a7 833 /*Start receiver*/
sam_grove 2:3e7685cfb2a7 834 rf_flags_clear(RFF_RX);
sam_grove 2:3e7685cfb2a7 835 rf_receive();
sam_grove 2:3e7685cfb2a7 836 }
sam_grove 2:3e7685cfb2a7 837
sam_grove 2:3e7685cfb2a7 838 /*
sam_grove 2:3e7685cfb2a7 839 * \brief Function is called when MAC is shutting down the radio.
sam_grove 2:3e7685cfb2a7 840 *
sam_grove 2:3e7685cfb2a7 841 * \param none
sam_grove 2:3e7685cfb2a7 842 *
sam_grove 2:3e7685cfb2a7 843 * \return none
sam_grove 2:3e7685cfb2a7 844 */
sam_grove 2:3e7685cfb2a7 845 void rf_shutdown(void)
sam_grove 2:3e7685cfb2a7 846 {
sam_grove 2:3e7685cfb2a7 847 /*Call RF OFF*/
sam_grove 2:3e7685cfb2a7 848 rf_off();
sam_grove 2:3e7685cfb2a7 849 /*Clear RF flags*/
sam_grove 2:3e7685cfb2a7 850 rf_flags_reset();
sam_grove 2:3e7685cfb2a7 851 }
sam_grove 2:3e7685cfb2a7 852
sam_grove 2:3e7685cfb2a7 853 /*
sam_grove 2:3e7685cfb2a7 854 * \brief Function is a call back for TX end interrupt.
sam_grove 2:3e7685cfb2a7 855 *
sam_grove 2:3e7685cfb2a7 856 * \param none
sam_grove 2:3e7685cfb2a7 857 *
sam_grove 2:3e7685cfb2a7 858 * \return none
sam_grove 2:3e7685cfb2a7 859 */
sam_grove 2:3e7685cfb2a7 860 void rf_handle_tx_end(void)
sam_grove 2:3e7685cfb2a7 861 {
sam_grove 2:3e7685cfb2a7 862 phy_link_tx_status_e phy_status = PHY_LINK_TX_SUCCESS;
sam_grove 2:3e7685cfb2a7 863
sam_grove 2:3e7685cfb2a7 864 rf_rx_mode = 0;
sam_grove 2:3e7685cfb2a7 865 /*If ACK is needed for this transmission*/
sam_grove 2:3e7685cfb2a7 866 if(need_ack && rf_flags_check(RFF_TX))
sam_grove 2:3e7685cfb2a7 867 {
sam_grove 2:3e7685cfb2a7 868 rf_ack_wait_timer_start(rf_ack_wait_duration);
sam_grove 2:3e7685cfb2a7 869 rf_rx_mode = 1;
sam_grove 2:3e7685cfb2a7 870 }
sam_grove 2:3e7685cfb2a7 871 rf_flags_clear(RFF_RX);
sam_grove 2:3e7685cfb2a7 872 /*Start receiver*/
sam_grove 2:3e7685cfb2a7 873 rf_receive();
sam_grove 2:3e7685cfb2a7 874
sam_grove 2:3e7685cfb2a7 875 /*Call PHY TX Done API*/
sam_grove 2:3e7685cfb2a7 876 arm_net_phy_tx_done(rf_radio_driver_id, mac_tx_handle, phy_status, 1, 1);
sam_grove 2:3e7685cfb2a7 877 }
sam_grove 2:3e7685cfb2a7 878
sam_grove 2:3e7685cfb2a7 879 /*
sam_grove 2:3e7685cfb2a7 880 * \brief Function is a call back for CCA ED done interrupt.
sam_grove 2:3e7685cfb2a7 881 *
sam_grove 2:3e7685cfb2a7 882 * \param none
sam_grove 2:3e7685cfb2a7 883 *
sam_grove 2:3e7685cfb2a7 884 * \return none
sam_grove 2:3e7685cfb2a7 885 */
sam_grove 2:3e7685cfb2a7 886 void rf_handle_cca_ed_done(void)
sam_grove 2:3e7685cfb2a7 887 {
sam_grove 2:3e7685cfb2a7 888 rf_flags_clear(RFF_CCA);
sam_grove 2:3e7685cfb2a7 889 /*Check the result of CCA process*/
sam_grove 2:3e7685cfb2a7 890 if(rf_if_check_cca())
sam_grove 2:3e7685cfb2a7 891 {
sam_grove 2:3e7685cfb2a7 892 rf_start_tx();
sam_grove 2:3e7685cfb2a7 893 }
sam_grove 2:3e7685cfb2a7 894 else
sam_grove 2:3e7685cfb2a7 895 {
sam_grove 2:3e7685cfb2a7 896 /*Send CCA fail notification*/
sam_grove 2:3e7685cfb2a7 897 arm_net_phy_tx_done(rf_radio_driver_id, mac_tx_handle, PHY_LINK_CCA_FAIL, 1, 1);
sam_grove 2:3e7685cfb2a7 898 }
sam_grove 2:3e7685cfb2a7 899 }
sam_grove 2:3e7685cfb2a7 900
sam_grove 2:3e7685cfb2a7 901 /*
sam_grove 2:3e7685cfb2a7 902 * \brief Function sets the TX power variable.
sam_grove 2:3e7685cfb2a7 903 *
sam_grove 2:3e7685cfb2a7 904 * \param power TX power setting
sam_grove 2:3e7685cfb2a7 905 *
sam_grove 2:3e7685cfb2a7 906 * \return 0 Success
sam_grove 2:3e7685cfb2a7 907 * \return -1 Fail
sam_grove 2:3e7685cfb2a7 908 */
sam_grove 2:3e7685cfb2a7 909 int8_t rf_tx_power_set(uint8_t power)
sam_grove 2:3e7685cfb2a7 910 {
sam_grove 2:3e7685cfb2a7 911 int8_t ret_val = -1;
sam_grove 2:3e7685cfb2a7 912 if(power < 16)
sam_grove 2:3e7685cfb2a7 913 {
sam_grove 2:3e7685cfb2a7 914 radio_tx_power = power;
sam_grove 2:3e7685cfb2a7 915 ret_val = 0;
sam_grove 2:3e7685cfb2a7 916 }
sam_grove 2:3e7685cfb2a7 917 return ret_val;
sam_grove 2:3e7685cfb2a7 918 }
sam_grove 2:3e7685cfb2a7 919
sam_grove 2:3e7685cfb2a7 920 /*
sam_grove 2:3e7685cfb2a7 921 * \brief Function returns the TX power variable.
sam_grove 2:3e7685cfb2a7 922 *
sam_grove 2:3e7685cfb2a7 923 * \param none
sam_grove 2:3e7685cfb2a7 924 *
sam_grove 2:3e7685cfb2a7 925 * \return radio_tx_power TX power variable
sam_grove 2:3e7685cfb2a7 926 */
sam_grove 2:3e7685cfb2a7 927 uint8_t rf_tx_power_get(void)
sam_grove 2:3e7685cfb2a7 928 {
sam_grove 2:3e7685cfb2a7 929 return radio_tx_power;
sam_grove 2:3e7685cfb2a7 930 }
sam_grove 2:3e7685cfb2a7 931
sam_grove 2:3e7685cfb2a7 932 /*
sam_grove 2:3e7685cfb2a7 933 * \brief Function sets the RF RPC variable.
sam_grove 2:3e7685cfb2a7 934 *
sam_grove 2:3e7685cfb2a7 935 * \param rpc_value RPC setting
sam_grove 2:3e7685cfb2a7 936 *
sam_grove 2:3e7685cfb2a7 937 * \return 0 Success
sam_grove 2:3e7685cfb2a7 938 */
sam_grove 2:3e7685cfb2a7 939 int8_t rf_rpc_set(uint8_t rpc_value)
sam_grove 2:3e7685cfb2a7 940 {
sam_grove 2:3e7685cfb2a7 941 int8_t ret_val = -1;
sam_grove 2:3e7685cfb2a7 942 radio_rpc_value = rpc_value;
sam_grove 2:3e7685cfb2a7 943 ret_val = 0;
sam_grove 2:3e7685cfb2a7 944 return ret_val;
sam_grove 2:3e7685cfb2a7 945 }
sam_grove 2:3e7685cfb2a7 946
sam_grove 2:3e7685cfb2a7 947 /*
sam_grove 2:3e7685cfb2a7 948 * \brief Function enables the usage of Front end.
sam_grove 2:3e7685cfb2a7 949 *
sam_grove 2:3e7685cfb2a7 950 * \param none
sam_grove 2:3e7685cfb2a7 951 *
sam_grove 2:3e7685cfb2a7 952 * \return 0 Success
sam_grove 2:3e7685cfb2a7 953 */
sam_grove 2:3e7685cfb2a7 954 int8_t rf_enable_pa(void)
sam_grove 2:3e7685cfb2a7 955 {
sam_grove 2:3e7685cfb2a7 956 int8_t ret_val = 0;
sam_grove 2:3e7685cfb2a7 957 rf_use_front_end = 1;
sam_grove 2:3e7685cfb2a7 958 return ret_val;
sam_grove 2:3e7685cfb2a7 959 }
sam_grove 2:3e7685cfb2a7 960
sam_grove 2:3e7685cfb2a7 961 /*
sam_grove 2:3e7685cfb2a7 962 * \brief Function enables the usage of Antenna diversity.
sam_grove 2:3e7685cfb2a7 963 *
sam_grove 2:3e7685cfb2a7 964 * \param none
sam_grove 2:3e7685cfb2a7 965 *
sam_grove 2:3e7685cfb2a7 966 * \return 0 Success
sam_grove 2:3e7685cfb2a7 967 */
sam_grove 2:3e7685cfb2a7 968 int8_t rf_enable_antenna_diversity(void)
sam_grove 2:3e7685cfb2a7 969 {
sam_grove 2:3e7685cfb2a7 970 int8_t ret_val = 0;
sam_grove 2:3e7685cfb2a7 971 rf_use_antenna_diversity = 1;
sam_grove 2:3e7685cfb2a7 972 return ret_val;
sam_grove 2:3e7685cfb2a7 973 }
sam_grove 2:3e7685cfb2a7 974
sam_grove 2:3e7685cfb2a7 975 /*
sam_grove 2:3e7685cfb2a7 976 * \brief Function defines the CSD pin of the Front end.
sam_grove 2:3e7685cfb2a7 977 *
sam_grove 2:3e7685cfb2a7 978 * \param port CSD port
sam_grove 2:3e7685cfb2a7 979 * \param port CSD pin
sam_grove 2:3e7685cfb2a7 980 *
sam_grove 2:3e7685cfb2a7 981 * \return 0 Success
sam_grove 2:3e7685cfb2a7 982 */
sam_grove 2:3e7685cfb2a7 983 int8_t rf_set_csd_pin(uint8_t port, uint8_t pin)
sam_grove 2:3e7685cfb2a7 984 {
sam_grove 2:3e7685cfb2a7 985 int8_t ret_val = -1;
sam_grove 2:3e7685cfb2a7 986
sam_grove 2:3e7685cfb2a7 987 rf_csd_port = port;
sam_grove 2:3e7685cfb2a7 988 rf_csd_pin = pin;
sam_grove 2:3e7685cfb2a7 989 ret_val = 0;
sam_grove 2:3e7685cfb2a7 990
sam_grove 2:3e7685cfb2a7 991 return ret_val;
sam_grove 2:3e7685cfb2a7 992 }
sam_grove 2:3e7685cfb2a7 993
sam_grove 2:3e7685cfb2a7 994 /*
sam_grove 2:3e7685cfb2a7 995 * \brief Function defines the CPS pin of the Front end.
sam_grove 2:3e7685cfb2a7 996 *
sam_grove 2:3e7685cfb2a7 997 * \param port CPS port
sam_grove 2:3e7685cfb2a7 998 * \param port CPS pin
sam_grove 2:3e7685cfb2a7 999 *
sam_grove 2:3e7685cfb2a7 1000 * \return 0 Success
sam_grove 2:3e7685cfb2a7 1001 */
sam_grove 2:3e7685cfb2a7 1002 int8_t rf_set_cps_pin(uint8_t port, uint8_t pin)
sam_grove 2:3e7685cfb2a7 1003 {
sam_grove 2:3e7685cfb2a7 1004 int8_t ret_val = -1;
sam_grove 2:3e7685cfb2a7 1005
sam_grove 2:3e7685cfb2a7 1006 rf_cps_port = port;
sam_grove 2:3e7685cfb2a7 1007 rf_cps_pin = pin;
sam_grove 2:3e7685cfb2a7 1008 ret_val = 0;
sam_grove 2:3e7685cfb2a7 1009
sam_grove 2:3e7685cfb2a7 1010 return ret_val;
sam_grove 2:3e7685cfb2a7 1011 }
sam_grove 2:3e7685cfb2a7 1012
sam_grove 2:3e7685cfb2a7 1013 /*
sam_grove 2:3e7685cfb2a7 1014 * \brief Function gives the control of RF states to MAC.
sam_grove 2:3e7685cfb2a7 1015 *
sam_grove 2:3e7685cfb2a7 1016 * \param new_state RF state
sam_grove 2:3e7685cfb2a7 1017 * \param rf_channel RF channel
sam_grove 2:3e7685cfb2a7 1018 *
sam_grove 2:3e7685cfb2a7 1019 * \return 0 Success
sam_grove 2:3e7685cfb2a7 1020 */
sam_grove 2:3e7685cfb2a7 1021 static int8_t rf_interface_state_control(phy_interface_state_e new_state, uint8_t rf_channel)
sam_grove 2:3e7685cfb2a7 1022 {
sam_grove 2:3e7685cfb2a7 1023 int8_t ret_val = 0;
sam_grove 2:3e7685cfb2a7 1024 switch (new_state)
sam_grove 2:3e7685cfb2a7 1025 {
sam_grove 2:3e7685cfb2a7 1026 /*Reset PHY driver and set to idle*/
sam_grove 2:3e7685cfb2a7 1027 case PHY_INTERFACE_RESET:
sam_grove 2:3e7685cfb2a7 1028 break;
sam_grove 2:3e7685cfb2a7 1029 /*Disable PHY Interface driver*/
sam_grove 2:3e7685cfb2a7 1030 case PHY_INTERFACE_DOWN:
sam_grove 2:3e7685cfb2a7 1031 rf_shutdown();
sam_grove 2:3e7685cfb2a7 1032 break;
sam_grove 2:3e7685cfb2a7 1033 /*Enable PHY Interface driver*/
sam_grove 2:3e7685cfb2a7 1034 case PHY_INTERFACE_UP:
sam_grove 2:3e7685cfb2a7 1035 rf_channel_set(rf_channel);
sam_grove 2:3e7685cfb2a7 1036 rf_receive();
sam_grove 2:3e7685cfb2a7 1037 break;
sam_grove 2:3e7685cfb2a7 1038 /*Enable wireless interface ED scan mode*/
sam_grove 2:3e7685cfb2a7 1039 case PHY_INTERFACE_RX_ENERGY_STATE:
sam_grove 2:3e7685cfb2a7 1040 break;
sam_grove 2:3e7685cfb2a7 1041 }
sam_grove 2:3e7685cfb2a7 1042 return ret_val;
sam_grove 2:3e7685cfb2a7 1043 }
sam_grove 2:3e7685cfb2a7 1044
sam_grove 2:3e7685cfb2a7 1045 /*
sam_grove 2:3e7685cfb2a7 1046 * \brief Function controls the ACK pending, channel setting and energy detection.
sam_grove 2:3e7685cfb2a7 1047 *
sam_grove 2:3e7685cfb2a7 1048 * \param extension_type Type of control
sam_grove 2:3e7685cfb2a7 1049 * \param data_ptr Data from NET library
sam_grove 2:3e7685cfb2a7 1050 *
sam_grove 2:3e7685cfb2a7 1051 * \return 0 Success
sam_grove 2:3e7685cfb2a7 1052 */
sam_grove 2:3e7685cfb2a7 1053 static int8_t rf_extension(phy_extension_type_e extension_type, uint8_t *data_ptr)
sam_grove 2:3e7685cfb2a7 1054 {
sam_grove 2:3e7685cfb2a7 1055 switch (extension_type)
sam_grove 2:3e7685cfb2a7 1056 {
sam_grove 2:3e7685cfb2a7 1057 /*Control MAC pending bit for Indirect data transmission*/
sam_grove 2:3e7685cfb2a7 1058 case PHY_EXTENSION_CTRL_PENDING_BIT:
sam_grove 2:3e7685cfb2a7 1059 if(*data_ptr)
sam_grove 2:3e7685cfb2a7 1060 {
sam_grove 2:3e7685cfb2a7 1061 rf_if_ack_pending_ctrl(1);
sam_grove 2:3e7685cfb2a7 1062 }
sam_grove 2:3e7685cfb2a7 1063 else
sam_grove 2:3e7685cfb2a7 1064 {
sam_grove 2:3e7685cfb2a7 1065 rf_if_ack_pending_ctrl(0);
sam_grove 2:3e7685cfb2a7 1066 }
sam_grove 2:3e7685cfb2a7 1067 break;
sam_grove 2:3e7685cfb2a7 1068 /*Return frame pending status*/
sam_grove 2:3e7685cfb2a7 1069 case PHY_EXTENSION_READ_LAST_ACK_PENDING_STATUS:
sam_grove 2:3e7685cfb2a7 1070 *data_ptr = rf_if_last_acked_pending();
sam_grove 2:3e7685cfb2a7 1071 break;
sam_grove 2:3e7685cfb2a7 1072 /*Set channel*/
sam_grove 2:3e7685cfb2a7 1073 case PHY_EXTENSION_SET_CHANNEL:
sam_grove 2:3e7685cfb2a7 1074 break;
sam_grove 2:3e7685cfb2a7 1075 /*Read energy on the channel*/
sam_grove 2:3e7685cfb2a7 1076 case PHY_EXTENSION_READ_CHANNEL_ENERGY:
sam_grove 2:3e7685cfb2a7 1077 break;
sam_grove 2:3e7685cfb2a7 1078 /*Read status of the link*/
sam_grove 2:3e7685cfb2a7 1079 case PHY_EXTENSION_READ_LINK_STATUS:
sam_grove 2:3e7685cfb2a7 1080 break;
sam_grove 2:3e7685cfb2a7 1081 }
sam_grove 2:3e7685cfb2a7 1082 return 0;
sam_grove 2:3e7685cfb2a7 1083 }
sam_grove 2:3e7685cfb2a7 1084
sam_grove 2:3e7685cfb2a7 1085 /*
sam_grove 2:3e7685cfb2a7 1086 * \brief Function sets the addresses to RF address filters.
sam_grove 2:3e7685cfb2a7 1087 *
sam_grove 2:3e7685cfb2a7 1088 * \param address_type Type of address
sam_grove 2:3e7685cfb2a7 1089 * \param address_ptr Pointer to given address
sam_grove 2:3e7685cfb2a7 1090 *
sam_grove 2:3e7685cfb2a7 1091 * \return 0 Success
sam_grove 2:3e7685cfb2a7 1092 */
sam_grove 2:3e7685cfb2a7 1093 static int8_t rf_address_write(phy_address_type_e address_type, uint8_t *address_ptr)
sam_grove 2:3e7685cfb2a7 1094 {
sam_grove 2:3e7685cfb2a7 1095 int8_t ret_val = 0;
sam_grove 2:3e7685cfb2a7 1096 switch (address_type)
sam_grove 2:3e7685cfb2a7 1097 {
sam_grove 2:3e7685cfb2a7 1098 /*Set 48-bit address*/
sam_grove 2:3e7685cfb2a7 1099 case PHY_MAC_48BIT:
sam_grove 2:3e7685cfb2a7 1100 break;
sam_grove 2:3e7685cfb2a7 1101 /*Set 64-bit address*/
sam_grove 2:3e7685cfb2a7 1102 case PHY_MAC_64BIT:
sam_grove 2:3e7685cfb2a7 1103 rf_set_address(address_ptr);
sam_grove 2:3e7685cfb2a7 1104 break;
sam_grove 2:3e7685cfb2a7 1105 /*Set 16-bit address*/
sam_grove 2:3e7685cfb2a7 1106 case PHY_MAC_16BIT:
sam_grove 2:3e7685cfb2a7 1107 rf_set_short_adr(address_ptr);
sam_grove 2:3e7685cfb2a7 1108 break;
sam_grove 2:3e7685cfb2a7 1109 /*Set PAN Id*/
sam_grove 2:3e7685cfb2a7 1110 case PHY_MAC_PANID:
sam_grove 2:3e7685cfb2a7 1111 rf_set_pan_id(address_ptr);
sam_grove 2:3e7685cfb2a7 1112 break;
sam_grove 2:3e7685cfb2a7 1113 }
sam_grove 2:3e7685cfb2a7 1114 return ret_val;
sam_grove 2:3e7685cfb2a7 1115 }
sam_grove 2:3e7685cfb2a7 1116
sam_grove 2:3e7685cfb2a7 1117 /*
sam_grove 2:3e7685cfb2a7 1118 * \brief Function initialises the ACK wait time and returns the used PHY mode.
sam_grove 2:3e7685cfb2a7 1119 *
sam_grove 2:3e7685cfb2a7 1120 * \param none
sam_grove 2:3e7685cfb2a7 1121 *
sam_grove 2:3e7685cfb2a7 1122 * \return tmp Used PHY mode
sam_grove 2:3e7685cfb2a7 1123 */
sam_grove 2:3e7685cfb2a7 1124 uint8_t rf_init_phy_mode(void)
sam_grove 2:3e7685cfb2a7 1125 {
sam_grove 2:3e7685cfb2a7 1126 uint8_t tmp;
sam_grove 2:3e7685cfb2a7 1127 /*Read used PHY Mode*/
sam_grove 2:3e7685cfb2a7 1128 tmp = rf_if_read_register(TRX_CTRL_2);
sam_grove 2:3e7685cfb2a7 1129 /*Set ACK wait time for used data rate*/
sam_grove 2:3e7685cfb2a7 1130 if((tmp & 0x1f) == 0x00)
sam_grove 2:3e7685cfb2a7 1131 {
sam_grove 2:3e7685cfb2a7 1132 rf_ack_wait_duration = 938;
sam_grove 2:3e7685cfb2a7 1133 tmp = BPSK_20;
sam_grove 2:3e7685cfb2a7 1134 }
sam_grove 2:3e7685cfb2a7 1135 else if((tmp & 0x1f) == 0x04)
sam_grove 2:3e7685cfb2a7 1136 {
sam_grove 2:3e7685cfb2a7 1137 rf_ack_wait_duration = 469;
sam_grove 2:3e7685cfb2a7 1138 tmp = BPSK_40;
sam_grove 2:3e7685cfb2a7 1139 }
sam_grove 2:3e7685cfb2a7 1140 else if((tmp & 0x1f) == 0x14)
sam_grove 2:3e7685cfb2a7 1141 {
sam_grove 2:3e7685cfb2a7 1142 rf_ack_wait_duration = 469;
sam_grove 2:3e7685cfb2a7 1143 tmp = BPSK_40_ALT;
sam_grove 2:3e7685cfb2a7 1144 }
sam_grove 2:3e7685cfb2a7 1145 else if((tmp & 0x1f) == 0x08)
sam_grove 2:3e7685cfb2a7 1146 {
sam_grove 2:3e7685cfb2a7 1147 rf_ack_wait_duration = 100;
sam_grove 2:3e7685cfb2a7 1148 tmp = OQPSK_SIN_RC_100;
sam_grove 2:3e7685cfb2a7 1149 }
sam_grove 2:3e7685cfb2a7 1150 else if((tmp & 0x1f) == 0x09)
sam_grove 2:3e7685cfb2a7 1151 {
sam_grove 2:3e7685cfb2a7 1152 rf_ack_wait_duration = 50;
sam_grove 2:3e7685cfb2a7 1153 tmp = OQPSK_SIN_RC_200;
sam_grove 2:3e7685cfb2a7 1154 }
sam_grove 2:3e7685cfb2a7 1155 else if((tmp & 0x1f) == 0x18)
sam_grove 2:3e7685cfb2a7 1156 {
sam_grove 2:3e7685cfb2a7 1157 rf_ack_wait_duration = 100;
sam_grove 2:3e7685cfb2a7 1158 tmp = OQPSK_RC_100;
sam_grove 2:3e7685cfb2a7 1159 }
sam_grove 2:3e7685cfb2a7 1160 else if((tmp & 0x1f) == 0x19)
sam_grove 2:3e7685cfb2a7 1161 {
sam_grove 2:3e7685cfb2a7 1162 rf_ack_wait_duration = 50;
sam_grove 2:3e7685cfb2a7 1163 tmp = OQPSK_RC_200;
sam_grove 2:3e7685cfb2a7 1164 }
sam_grove 2:3e7685cfb2a7 1165 else if((tmp & 0x1f) == 0x0c)
sam_grove 2:3e7685cfb2a7 1166 {
sam_grove 2:3e7685cfb2a7 1167 rf_ack_wait_duration = 50;
sam_grove 2:3e7685cfb2a7 1168 tmp = OQPSK_SIN_250;
sam_grove 2:3e7685cfb2a7 1169 }
sam_grove 2:3e7685cfb2a7 1170 else if((tmp & 0x1f) == 0x0d)
sam_grove 2:3e7685cfb2a7 1171 {
sam_grove 2:3e7685cfb2a7 1172 rf_ack_wait_duration = 25;
sam_grove 2:3e7685cfb2a7 1173 tmp = OQPSK_SIN_500;
sam_grove 2:3e7685cfb2a7 1174 }
sam_grove 2:3e7685cfb2a7 1175 else if((tmp & 0x1f) == 0x0f)
sam_grove 2:3e7685cfb2a7 1176 {
sam_grove 2:3e7685cfb2a7 1177 rf_ack_wait_duration = 25;
sam_grove 2:3e7685cfb2a7 1178 tmp = OQPSK_SIN_500_ALT;
sam_grove 2:3e7685cfb2a7 1179 }
sam_grove 2:3e7685cfb2a7 1180 else if((tmp & 0x1f) == 0x1c)
sam_grove 2:3e7685cfb2a7 1181 {
sam_grove 2:3e7685cfb2a7 1182 rf_ack_wait_duration = 50;
sam_grove 2:3e7685cfb2a7 1183 tmp = OQPSK_RC_250;
sam_grove 2:3e7685cfb2a7 1184 }
sam_grove 2:3e7685cfb2a7 1185 else if((tmp & 0x1f) == 0x1d)
sam_grove 2:3e7685cfb2a7 1186 {
sam_grove 2:3e7685cfb2a7 1187 rf_ack_wait_duration = 25;
sam_grove 2:3e7685cfb2a7 1188 tmp = OQPSK_RC_500;
sam_grove 2:3e7685cfb2a7 1189 }
sam_grove 2:3e7685cfb2a7 1190 else if((tmp & 0x1f) == 0x1f)
sam_grove 2:3e7685cfb2a7 1191 {
sam_grove 2:3e7685cfb2a7 1192 rf_ack_wait_duration = 25;
sam_grove 2:3e7685cfb2a7 1193 tmp = OQPSK_RC_500_ALT;
sam_grove 2:3e7685cfb2a7 1194 }
sam_grove 2:3e7685cfb2a7 1195 else if((tmp & 0x3f) == 0x2A)
sam_grove 2:3e7685cfb2a7 1196 {
sam_grove 2:3e7685cfb2a7 1197 rf_ack_wait_duration = 25;
sam_grove 2:3e7685cfb2a7 1198 tmp = OQPSK_SIN_RC_400_SCR_ON;
sam_grove 2:3e7685cfb2a7 1199 }
sam_grove 2:3e7685cfb2a7 1200 else if((tmp & 0x3f) == 0x0A)
sam_grove 2:3e7685cfb2a7 1201 {
sam_grove 2:3e7685cfb2a7 1202 rf_ack_wait_duration = 25;
sam_grove 2:3e7685cfb2a7 1203 tmp = OQPSK_SIN_RC_400_SCR_OFF;
sam_grove 2:3e7685cfb2a7 1204 }
sam_grove 2:3e7685cfb2a7 1205 else if((tmp & 0x3f) == 0x3A)
sam_grove 2:3e7685cfb2a7 1206 {
sam_grove 2:3e7685cfb2a7 1207 rf_ack_wait_duration = 25;
sam_grove 2:3e7685cfb2a7 1208 tmp = OQPSK_RC_400_SCR_ON;
sam_grove 2:3e7685cfb2a7 1209 }
sam_grove 2:3e7685cfb2a7 1210 else if((tmp & 0x3f) == 0x1A)
sam_grove 2:3e7685cfb2a7 1211 {
sam_grove 2:3e7685cfb2a7 1212 rf_ack_wait_duration = 25;
sam_grove 2:3e7685cfb2a7 1213 tmp = OQPSK_RC_400_SCR_OFF;
sam_grove 2:3e7685cfb2a7 1214 }
sam_grove 2:3e7685cfb2a7 1215 else if((tmp & 0x3f) == 0x2E)
sam_grove 2:3e7685cfb2a7 1216 {
sam_grove 2:3e7685cfb2a7 1217 rf_ack_wait_duration = 13;
sam_grove 2:3e7685cfb2a7 1218 tmp = OQPSK_SIN_1000_SCR_ON;
sam_grove 2:3e7685cfb2a7 1219 }
sam_grove 2:3e7685cfb2a7 1220 else if((tmp & 0x3f) == 0x0E)
sam_grove 2:3e7685cfb2a7 1221 {
sam_grove 2:3e7685cfb2a7 1222 rf_ack_wait_duration = 13;
sam_grove 2:3e7685cfb2a7 1223 tmp = OQPSK_SIN_1000_SCR_OFF;
sam_grove 2:3e7685cfb2a7 1224 }
sam_grove 2:3e7685cfb2a7 1225 else if((tmp & 0x3f) == 0x3E)
sam_grove 2:3e7685cfb2a7 1226 {
sam_grove 2:3e7685cfb2a7 1227 rf_ack_wait_duration = 13;
sam_grove 2:3e7685cfb2a7 1228 tmp = OQPSK_RC_1000_SCR_ON;
sam_grove 2:3e7685cfb2a7 1229 }
sam_grove 2:3e7685cfb2a7 1230 else if((tmp & 0x3f) == 0x1E)
sam_grove 2:3e7685cfb2a7 1231 {
sam_grove 2:3e7685cfb2a7 1232 rf_ack_wait_duration = 13;
sam_grove 2:3e7685cfb2a7 1233 tmp = OQPSK_RC_1000_SCR_OFF;
sam_grove 2:3e7685cfb2a7 1234 }
sam_grove 2:3e7685cfb2a7 1235 return tmp;
sam_grove 2:3e7685cfb2a7 1236 }
sam_grove 2:3e7685cfb2a7 1237