Contiki 2.6

radio.h

Go to the documentation of this file.
00001 /*   Copyright (c) 2008, Swedish Institute of Computer Science
00002  *  All rights reserved.
00003  *
00004  *  Additional fixes for AVR contributed by:
00005  *
00006  *      Colin O'Flynn coflynn@newae.com
00007  *      Eric Gnoske egnoske@gmail.com
00008  *      Blake Leverett bleverett@gmail.com
00009  *      Mike Vidales mavida404@gmail.com
00010  *      Kevin Brown kbrown3@uccs.edu
00011  *      Nate Bohlmann nate@elfwerks.com
00012  *
00013  *   All rights reserved.
00014  *
00015  *   Redistribution and use in source and binary forms, with or without
00016  *   modification, are permitted provided that the following conditions are met:
00017  *
00018  *   * Redistributions of source code must retain the above copyright
00019  *     notice, this list of conditions and the following disclaimer.
00020  *   * Redistributions in binary form must reproduce the above copyright
00021  *     notice, this list of conditions and the following disclaimer in
00022  *     the documentation and/or other materials provided with the
00023  *     distribution.
00024  *   * Neither the name of the copyright holders nor the names of
00025  *     contributors may be used to endorse or promote products derived
00026  *     from this software without specific prior written permission.
00027  *
00028  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00029  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00030  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00031  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00032  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00033  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00034  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00035  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00036  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00037  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038  *  POSSIBILITY OF SUCH DAMAGE.
00039  */
00040 /**
00041  *    \addtogroup radiorf230
00042  *   @{
00043  */
00044 /**
00045  *  \file
00046  *  \brief This file contains radio driver code.
00047  *
00048  *   $Id: radio.h,v 1.2 2008/10/14 18:37:28 c_oflynn Exp $
00049  */
00050 
00051 #ifndef RADIO_H
00052 #define RADIO_H
00053 /*============================ INCLUDE =======================================*/
00054 #include <stdint.h>
00055 #include <stdbool.h>
00056 #include "at86rf230_registermap.h"
00057 #include "hal.h"
00058 
00059 /*============================ MACROS ========================================*/
00060 #define SUPPORTED_PART_NUMBER                   ( 2 )
00061 #define RF230_REVA                              ( 1 )
00062 #define RF230_REVB                              ( 2 )
00063 #define SUPPORTED_MANUFACTURER_ID               ( 31 )
00064 #define RF230_SUPPORTED_INTERRUPT_MASK          ( 0x0C )
00065 
00066 #define RF230_MIN_CHANNEL                       ( 11 )
00067 #define RF230_MAX_CHANNEL                       ( 26 )
00068 #define RF230_MIN_ED_THRESHOLD                  ( 0 )
00069 #define RF230_MAX_ED_THRESHOLD                  ( 15 )
00070 #define RF230_MAX_TX_FRAME_LENGTH               ( 127 ) /**< 127 Byte PSDU. */
00071 
00072 #define TX_PWR_3DBM                             ( 0 )
00073 #define TX_PWR_17_2DBM                          ( 15 )
00074 
00075 #define BATTERY_MONITOR_HIGHEST_VOLTAGE         ( 15 )
00076 #define BATTERY_MONITOR_VOLTAGE_UNDER_THRESHOLD ( 0 )
00077 #define BATTERY_MONITOR_HIGH_VOLTAGE            ( 1 )
00078 #define BATTERY_MONITOR_LOW_VOLTAGE             ( 0 )
00079 
00080 #define FTN_CALIBRATION_DONE                    ( 0 )
00081 #define PLL_DCU_CALIBRATION_DONE                ( 0 )
00082 #define PLL_CF_CALIBRATION_DONE                 ( 0 )
00083 
00084 #define RC_OSC_REFERENCE_COUNT_MAX  (1.005*F_CPU*31250UL/8000000UL)
00085 #define RC_OSC_REFERENCE_COUNT_MIN  (0.995*F_CPU*31250UL/8000000UL)
00086 /*============================ TYPEDEFS ======================================*/
00087 
00088 /** \brief  This macro defines the start value for the RADIO_* status constants.
00089  *
00090  *          It was chosen to have this macro so that the user can define where
00091  *          the status returned from the TAT starts. This can be useful in a
00092  *          system where numerous drivers are used, and some range of status codes
00093  *          are occupied.
00094  *
00095  *  \see radio_status_t
00096  */
00097 #define RADIO_STATUS_START_VALUE                  ( 0x40 )
00098 
00099 /** \brief  This enumeration defines the possible return values for the TAT API
00100  *          functions.
00101  *
00102  *          These values are defined so that they should not collide with the
00103  *          return/status codes defined in the IEEE 802.15.4 standard.
00104  *
00105  */
00106 typedef enum{
00107     RADIO_SUCCESS = RADIO_STATUS_START_VALUE,  /**< The requested service was performed successfully. */
00108     RADIO_UNSUPPORTED_DEVICE,         /**< The connected device is not an Atmel AT86RF230. */
00109     RADIO_INVALID_ARGUMENT,           /**< One or more of the supplied function arguments are invalid. */
00110     RADIO_TIMED_OUT,                  /**< The requested service timed out. */
00111     RADIO_WRONG_STATE,                /**< The end-user tried to do an invalid state transition. */
00112     RADIO_BUSY_STATE,                 /**< The radio transceiver is busy receiving or transmitting. */
00113     RADIO_STATE_TRANSITION_FAILED,    /**< The requested state transition could not be completed. */
00114     RADIO_CCA_IDLE,                   /**< Channel is clear, available to transmit a new frame. */
00115     RADIO_CCA_BUSY,                   /**< Channel busy. */
00116     RADIO_TRX_BUSY,                   /**< Transceiver is busy receiving or transmitting data. */
00117     RADIO_BAT_LOW,                    /**< Measured battery voltage is lower than voltage threshold. */
00118     RADIO_BAT_OK,                     /**< Measured battery voltage is above the voltage threshold. */
00119     RADIO_CRC_FAILED,                 /**< The CRC failed for the actual frame. */
00120     RADIO_CHANNEL_ACCESS_FAILURE,     /**< The channel access failed during the auto mode. */
00121     RADIO_NO_ACK,                     /**< No acknowledge frame was received. */
00122 }radio_status_t;
00123 
00124 
00125 /**
00126  * \name Transaction status codes
00127  * \{
00128  */
00129 #define TRAC_SUCCESS                0
00130 #define TRAC_SUCCESS_DATA_PENDING   1
00131 #define TRAC_SUCCESS_WAIT_FOR_ACK   2
00132 #define TRAC_CHANNEL_ACCESS_FAILURE 3
00133 #define TRAC_NO_ACK                 5
00134 #define TRAC_INVALID                7
00135 /** \} */
00136 
00137 
00138 /** \brief  This enumeration defines the possible modes available for the
00139  *          Clear Channel Assessment algorithm.
00140  *
00141  *          These constants are extracted from the datasheet.
00142  *
00143  */
00144 typedef enum{
00145     CCA_ED                    = 0,    /**< Use energy detection above threshold mode. */
00146     CCA_CARRIER_SENSE         = 1,    /**< Use carrier sense mode. */
00147     CCA_CARRIER_SENSE_WITH_ED = 2     /**< Use a combination of both energy detection and carrier sense. */
00148 }radio_cca_mode_t;
00149 
00150 
00151 /** \brief  This enumeration defines the possible CLKM speeds.
00152  *
00153  *          These constants are extracted from the RF230 datasheet.
00154  *
00155  */
00156 typedef enum{
00157     CLKM_DISABLED      = 0,
00158     CLKM_1MHZ          = 1,
00159     CLKM_2MHZ          = 2,
00160     CLKM_4MHZ          = 3,
00161     CLKM_8MHZ          = 4,
00162     CLKM_16MHZ         = 5
00163 }radio_clkm_speed_t;
00164 
00165 typedef void (*radio_rx_callback) (uint16_t data);
00166 extern uint8_t rxMode;
00167 /*============================ PROTOTYPES ====================================*/
00168 radio_status_t radio_init(bool cal_rc_osc,
00169                           hal_rx_start_isr_event_handler_t rx_event,
00170                           hal_trx_end_isr_event_handler_t trx_end_event,
00171                           radio_rx_callback rx_callback);
00172 uint8_t             radio_get_saved_rssi_value(void);
00173 uint8_t             radio_get_operating_channel( void );
00174 radio_status_t radio_set_operating_channel( uint8_t channel );
00175 uint8_t             radio_get_tx_power_level( void );
00176 radio_status_t radio_set_tx_power_level( uint8_t power_level );
00177 
00178 uint8_t             radio_get_cca_mode( void );
00179 uint8_t             radio_get_ed_threshold( void );
00180 radio_status_t radio_set_cca_mode( uint8_t mode, uint8_t ed_threshold );
00181 radio_status_t radio_do_cca( void );
00182 radio_status_t radio_get_rssi_value( uint8_t *rssi );
00183 
00184 uint8_t             radio_batmon_get_voltage_threshold( void );
00185 uint8_t             radio_batmon_get_voltage_range( void );
00186 radio_status_t radio_batmon_configure( bool range, uint8_t voltage_threshold );
00187 radio_status_t radio_batmon_get_status( void );
00188 
00189 uint8_t             radio_get_clock_speed( void );
00190 radio_status_t radio_set_clock_speed( bool direct, uint8_t clock_speed );
00191 radio_status_t radio_calibrate_filter( void );
00192 radio_status_t radio_calibrate_pll( void );
00193 
00194 uint8_t             radio_get_trx_state( void );
00195 radio_status_t radio_set_trx_state( uint8_t new_state );
00196 radio_status_t radio_enter_sleep_mode( void );
00197 radio_status_t radio_leave_sleep_mode( void );
00198 void           radio_reset_state_machine( void );
00199 void           radio_reset_trx( void );
00200 
00201 void           radio_use_auto_tx_crc( bool auto_crc_on );
00202 radio_status_t radio_send_data( uint8_t data_length, uint8_t *data );
00203 
00204 uint8_t             radio_get_device_role( void );
00205 void           radio_set_device_role( bool i_am_coordinator );
00206 uint16_t            radio_get_pan_id( void );
00207 void           radio_set_pan_id( uint16_t new_pan_id );
00208 uint16_t            radio_get_short_address( void );
00209 void           radio_set_short_address( uint16_t new_short_address );
00210 void           radio_get_extended_address( uint8_t *extended_address );
00211 void           radio_set_extended_address( uint8_t *extended_address );
00212 radio_status_t radio_configure_csma( uint8_t seed0, uint8_t be_csma_seed1 );
00213 bool           calibrate_rc_osc_clkm(void);
00214 void           calibrate_rc_osc_32k(void);
00215 uint8_t * radio_frame_data(void);
00216 uint8_t radio_frame_length(void);
00217 #define delay_us( us )   ( _delay_loop_2( ( F_CPU / 4000000UL ) * ( us ) ) )
00218 
00219 #endif
00220 /** @} */
00221 /*EOF*/