Contiki 3.x
radio.h
Go to the documentation of this file.
1 /* Copyright (c) 2008, Swedish Institute of Computer Science
2  * All rights reserved.
3  *
4  * Additional fixes for AVR contributed by:
5  *
6  * Colin O'Flynn coflynn@newae.com
7  * Eric Gnoske egnoske@gmail.com
8  * Blake Leverett bleverett@gmail.com
9  * Mike Vidales mavida404@gmail.com
10  * Kevin Brown kbrown3@uccs.edu
11  * Nate Bohlmann nate@elfwerks.com
12  *
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are met:
17  *
18  * * Redistributions of source code must retain the above copyright
19  * notice, this list of conditions and the following disclaimer.
20  * * Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  * * Neither the name of the copyright holders nor the names of
25  * contributors may be used to endorse or promote products derived
26  * from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 /**
41  * \addtogroup radiorf230
42  * @{
43  */
44 /**
45  * \file
46  * \brief This file contains radio driver code.
47  *
48  */
49 
50 #ifndef RADIO_H
51 #define RADIO_H
52 /*============================ INCLUDE =======================================*/
53 #include <stdint.h>
54 #include <stdbool.h>
55 #include "at86rf230_registermap.h"
56 #include "hal.h"
57 
58 /*============================ MACROS ========================================*/
59 #define SUPPORTED_PART_NUMBER ( 2 )
60 #define RF230_REVA ( 1 )
61 #define RF230_REVB ( 2 )
62 #define SUPPORTED_MANUFACTURER_ID ( 31 )
63 #define RF230_SUPPORTED_INTERRUPT_MASK ( 0x0C )
64 
65 #define RF230_MIN_CHANNEL ( 11 )
66 #define RF230_MAX_CHANNEL ( 26 )
67 #define RF230_MIN_ED_THRESHOLD ( 0 )
68 #define RF230_MAX_ED_THRESHOLD ( 15 )
69 #define RF230_MAX_TX_FRAME_LENGTH ( 127 ) /**< 127 Byte PSDU. */
70 
71 #define TX_PWR_3DBM ( 0 )
72 #define TX_PWR_17_2DBM ( 15 )
73 
74 #define BATTERY_MONITOR_HIGHEST_VOLTAGE ( 15 )
75 #define BATTERY_MONITOR_VOLTAGE_UNDER_THRESHOLD ( 0 )
76 #define BATTERY_MONITOR_HIGH_VOLTAGE ( 1 )
77 #define BATTERY_MONITOR_LOW_VOLTAGE ( 0 )
78 
79 #define FTN_CALIBRATION_DONE ( 0 )
80 #define PLL_DCU_CALIBRATION_DONE ( 0 )
81 #define PLL_CF_CALIBRATION_DONE ( 0 )
82 
83 #define RC_OSC_REFERENCE_COUNT_MAX (1.005*F_CPU*31250UL/8000000UL)
84 #define RC_OSC_REFERENCE_COUNT_MIN (0.995*F_CPU*31250UL/8000000UL)
85 /*============================ TYPEDEFS ======================================*/
86 
87 /** \brief This macro defines the start value for the RADIO_* status constants.
88  *
89  * It was chosen to have this macro so that the user can define where
90  * the status returned from the TAT starts. This can be useful in a
91  * system where numerous drivers are used, and some range of status codes
92  * are occupied.
93  *
94  * \see radio_status_t
95  */
96 #define RADIO_STATUS_START_VALUE ( 0x40 )
97 
98 /** \brief This enumeration defines the possible return values for the TAT API
99  * functions.
100  *
101  * These values are defined so that they should not collide with the
102  * return/status codes defined in the IEEE 802.15.4 standard.
103  *
104  */
105 typedef enum{
106  RADIO_SUCCESS = RADIO_STATUS_START_VALUE, /**< The requested service was performed successfully. */
107  RADIO_UNSUPPORTED_DEVICE, /**< The connected device is not an Atmel AT86RF230. */
108  RADIO_INVALID_ARGUMENT, /**< One or more of the supplied function arguments are invalid. */
109  RADIO_TIMED_OUT, /**< The requested service timed out. */
110  RADIO_WRONG_STATE, /**< The end-user tried to do an invalid state transition. */
111  RADIO_BUSY_STATE, /**< The radio transceiver is busy receiving or transmitting. */
112  RADIO_STATE_TRANSITION_FAILED, /**< The requested state transition could not be completed. */
113  RADIO_CCA_IDLE, /**< Channel is clear, available to transmit a new frame. */
114  RADIO_CCA_BUSY, /**< Channel busy. */
115  RADIO_TRX_BUSY, /**< Transceiver is busy receiving or transmitting data. */
116  RADIO_BAT_LOW, /**< Measured battery voltage is lower than voltage threshold. */
117  RADIO_BAT_OK, /**< Measured battery voltage is above the voltage threshold. */
118  RADIO_CRC_FAILED, /**< The CRC failed for the actual frame. */
119  RADIO_CHANNEL_ACCESS_FAILURE, /**< The channel access failed during the auto mode. */
120  RADIO_NO_ACK, /**< No acknowledge frame was received. */
122 
123 
124 /**
125  * \name Transaction status codes
126  * \{
127  */
128 #define TRAC_SUCCESS 0
129 #define TRAC_SUCCESS_DATA_PENDING 1
130 #define TRAC_SUCCESS_WAIT_FOR_ACK 2
131 #define TRAC_CHANNEL_ACCESS_FAILURE 3
132 #define TRAC_NO_ACK 5
133 #define TRAC_INVALID 7
134 /** \} */
135 
136 
137 /** \brief This enumeration defines the possible modes available for the
138  * Clear Channel Assessment algorithm.
139  *
140  * These constants are extracted from the datasheet.
141  *
142  */
143 typedef enum{
144  CCA_ED = 0, /**< Use energy detection above threshold mode. */
145  CCA_CARRIER_SENSE = 1, /**< Use carrier sense mode. */
146  CCA_CARRIER_SENSE_WITH_ED = 2 /**< Use a combination of both energy detection and carrier sense. */
148 
149 
150 /** \brief This enumeration defines the possible CLKM speeds.
151  *
152  * These constants are extracted from the RF230 datasheet.
153  *
154  */
155 typedef enum{
156  CLKM_DISABLED = 0,
157  CLKM_1MHZ = 1,
158  CLKM_2MHZ = 2,
159  CLKM_4MHZ = 3,
160  CLKM_8MHZ = 4,
161  CLKM_16MHZ = 5
163 
164 typedef void (*radio_rx_callback) (uint16_t data);
165 extern uint8_t rxMode;
166 /*============================ PROTOTYPES ====================================*/
167 radio_status_t radio_init(bool cal_rc_osc,
169  hal_trx_end_isr_event_handler_t trx_end_event,
170  radio_rx_callback rx_callback);
171 uint8_t radio_get_saved_rssi_value(void);
172 uint8_t radio_get_operating_channel( void );
174 uint8_t radio_get_tx_power_level( void );
175 radio_status_t radio_set_tx_power_level( uint8_t power_level );
176 
177 uint8_t radio_get_cca_mode( void );
178 uint8_t radio_get_ed_threshold( void );
179 radio_status_t radio_set_cca_mode( uint8_t mode, uint8_t ed_threshold );
180 radio_status_t radio_do_cca( void );
181 radio_status_t radio_get_rssi_value( uint8_t *rssi );
182 
183 uint8_t radio_batmon_get_voltage_threshold( void );
184 uint8_t radio_batmon_get_voltage_range( void );
185 radio_status_t radio_batmon_configure( bool range, uint8_t voltage_threshold );
187 
188 uint8_t radio_get_clock_speed( void );
189 radio_status_t radio_set_clock_speed( bool direct, uint8_t clock_speed );
192 
193 uint8_t radio_get_trx_state( void );
194 radio_status_t radio_set_trx_state( uint8_t new_state );
197 void radio_reset_state_machine( void );
198 void radio_reset_trx( void );
199 
200 void radio_use_auto_tx_crc( bool auto_crc_on );
201 radio_status_t radio_send_data( uint8_t data_length, uint8_t *data );
202 
203 uint8_t radio_get_device_role( void );
204 void radio_set_device_role( bool i_am_coordinator );
205 uint16_t radio_get_pan_id( void );
206 void radio_set_pan_id( uint16_t new_pan_id );
207 uint16_t radio_get_short_address( void );
208 void radio_set_short_address( uint16_t new_short_address );
209 void radio_get_extended_address( uint8_t *extended_address );
210 void radio_set_extended_address( uint8_t *extended_address );
211 radio_status_t radio_configure_csma( uint8_t seed0, uint8_t be_csma_seed1 );
212 bool calibrate_rc_osc_clkm(void);
213 void calibrate_rc_osc_32k(void);
214 uint8_t * radio_frame_data(void);
215 uint8_t radio_frame_length(void);
216 #define delay_us( us ) ( _delay_loop_2( ( F_CPU / 4000000UL ) * ( us ) ) )
217 
218 #endif
219 /** @} */
220 /*EOF*/
The requested service timed out.
Definition: radio.h:109
radio_status_t radio_batmon_configure(bool range, uint8_t voltage_threshold)
This function is used to configure the battery monitor module.
Definition: radio.c:543
radio_status_t radio_set_clock_speed(bool direct, uint8_t clock_speed)
This function changes the prescaler on the CLKM pin.
Definition: radio.c:627
radio_status_t radio_configure_csma(uint8_t seed0, uint8_t be_csma_seed1)
This function will configure the CSMA algorithm used by the radio transceiver when transmitting data ...
Definition: radio.c:1204
Measured battery voltage is lower than voltage threshold.
Definition: radio.h:116
uint8_t radio_batmon_get_voltage_threshold(void)
This function returns the current threshold volatge used by the battery monitor (BATMON_VTH).
Definition: radio.c:511
radio_status_t radio_get_rssi_value(uint8_t *rssi)
This function returns the Received Signal Strength Indication.
Definition: radio.c:485
radio_status_t radio_send_data(uint8_t data_length, uint8_t *data)
This function will download a frame to the radio transceiver&#39;s transmit buffer and send it...
Definition: radio.c:1015
radio_status_t radio_set_tx_power_level(uint8_t power_level)
This function will change the output power level.
Definition: radio.c:391
uint8_t radio_get_device_role(void)
This function will read the I_AM_COORD sub register.
Definition: radio.c:1056
Use carrier sense mode.
Definition: radio.h:145
radio_status_t radio_calibrate_filter(void)
This function calibrates the Single Side Band Filter.
Definition: radio.c:659
Channel is clear, available to transmit a new frame.
Definition: radio.h:113
void calibrate_rc_osc_32k(void)
Calibrate the internal RC oscillator.
Definition: radio.c:1332
uint8_t radio_get_tx_power_level(void)
This function will read and return the output power level.
Definition: radio.c:374
The connected device is not an Atmel AT86RF230.
Definition: radio.h:107
uint8_t radio_get_ed_threshold(void)
This function returns the current ED threshold used by the CCA algorithm.
Definition: radio.c:426
void radio_use_auto_tx_crc(bool auto_crc_on)
This function will enable or disable automatic CRC during frame transmission.
Definition: radio.c:991
This file contains low-level radio driver code.
#define RADIO_STATUS_START_VALUE
This macro defines the start value for the RADIO_* status constants.
Definition: radio.h:96
uint8_t radio_get_operating_channel(void)
This function will return the channel used by the radio transceiver.
Definition: radio.c:313
bool calibrate_rc_osc_clkm(void)
Calibrate the internal RC oscillator.
Definition: radio.c:1238
uint8_t radio_get_clock_speed(void)
This function returns the current clock setting for the CLKM pin.
Definition: radio.c:601
The requested state transition could not be completed.
Definition: radio.h:112
radio_status_t radio_leave_sleep_mode(void)
This function will take the radio transceiver from sleep mode and put it into the TRX_OFF state...
Definition: radio.c:932
void radio_get_extended_address(uint8_t *extended_address)
This function will read the extended address used by the address filter.
Definition: radio.c:1152
uint8_t radio_get_trx_state(void)
This function return the Radio Transceivers current state.
Definition: radio.c:764
void(* hal_trx_end_isr_event_handler_t)(uint32_t const isr_timestamp)
RRX_END event handler callback type.
Definition: hal.h:362
uint16_t radio_get_pan_id(void)
This function will return the PANID used by the address filter.
Definition: radio.c:1081
This file contains the register definitions for the AT86RF230.
radio_status_t radio_set_cca_mode(uint8_t mode, uint8_t ed_threshold)
This function will configure the Clear Channel Assessment algorithm.
Definition: radio.c:448
The end-user tried to do an invalid state transition.
Definition: radio.h:110
uint16_t radio_get_short_address(void)
This function will return the current short address used by the address filter.
Definition: radio.c:1115
Use a combination of both energy detection and carrier sense.
Definition: radio.h:146
No acknowledge frame was received.
Definition: radio.h:120
radio_status_t radio_init(bool cal_rc_osc, hal_rx_start_isr_event_handler_t rx_event, hal_trx_end_isr_event_handler_t trx_end_event, radio_rx_callback rx_callback)
Initialize the Transceiver Access Toolbox and lower layers.
Definition: radio.c:160
Transceiver is busy receiving or transmitting data.
Definition: radio.h:115
uint8_t radio_get_cca_mode(void)
This function returns the current CCA mode used.
Definition: radio.c:415
radio_status_t radio_calibrate_pll(void)
This function calibrates the PLL.
Definition: radio.c:691
radio_status_t
This enumeration defines the possible return values for the TAT API functions.
Definition: radio.h:105
radio_status_t radio_set_operating_channel(uint8_t channel)
This function will change the operating channel.
Definition: radio.c:329
The requested service was performed successfully.
Definition: radio.h:106
The radio transceiver is busy receiving or transmitting.
Definition: radio.h:111
void radio_set_pan_id(uint16_t new_pan_id)
This function will set the PANID used by the address filter.
Definition: radio.c:1098
void radio_set_device_role(bool i_am_coordinator)
This function will set the I_AM_COORD sub register.
Definition: radio.c:1070
void radio_set_extended_address(uint8_t *extended_address)
This function will set a new extended address to be used by the address filter.
Definition: radio.c:1171
Channel busy.
Definition: radio.h:114
radio_status_t radio_enter_sleep_mode(void)
This function will put the radio transceiver to sleep.
Definition: radio.c:902
radio_clkm_speed_t
This enumeration defines the possible CLKM speeds.
Definition: radio.h:155
void radio_set_short_address(uint16_t new_short_address)
This function will set the short address used by the address filter.
Definition: radio.c:1132
The CRC failed for the actual frame.
Definition: radio.h:118
radio_cca_mode_t
This enumeration defines the possible modes available for the Clear Channel Assessment algorithm...
Definition: radio.h:143
One or more of the supplied function arguments are invalid.
Definition: radio.h:108
uint8_t radio_batmon_get_voltage_range(void)
This function returns if high or low voltage range is used.
Definition: radio.c:526
The channel access failed during the auto mode.
Definition: radio.h:119
radio_status_t radio_set_trx_state(uint8_t new_state)
This function will change the current state of the radio transceiver&#39;s internal state machine...
Definition: radio.c:809
Use energy detection above threshold mode.
Definition: radio.h:144
void radio_reset_state_machine(void)
This function will reset the state machine (to TRX_OFF) from any of its states, except for the SLEEP ...
Definition: radio.c:960
void(* hal_rx_start_isr_event_handler_t)(uint32_t const isr_timestamp, uint8_t const frame_length)
RX_START event handler callback type.
Definition: hal.h:359
void radio_reset_trx(void)
This function will reset all the registers and the state machine of the radio transceiver.
Definition: radio.c:973
Measured battery voltage is above the voltage threshold.
Definition: radio.h:117
radio_status_t radio_batmon_get_status(void)
This function returns the status of the Battery Monitor module.
Definition: radio.c:577