Contiki-Inga 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*/