hal.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 /**
00042  *    \addtogroup hal
00043  *    @{
00044  */
00045 
00046 /**
00047  *  \file
00048  *  \brief This file contains low-level radio driver code.
00049  *
00050  *   $Id: hal.h,v 1.5 2010/02/26 21:15:28 dak664 Exp $
00051 */
00052 
00053 #ifndef HAL_AVR_H
00054 #define HAL_AVR_H
00055 /*============================ INCLUDE =======================================*/
00056 #include <stdint.h>
00057 #include <stdbool.h>
00058 #include <avr/io.h>
00059 #include <avr/interrupt.h>
00060 #include <util/crc16.h>
00061 #include "contiki-conf.h"
00062 /*============================ MACROS ========================================*/
00063 
00064 // TEST CODE
00065 #define TRIG1 DDRB |= 0x04, PINB |= 0x04
00066 #define TRIG2 DDRD |= 0x80, PIND |= 0x80
00067 
00068 /** \name This is the list of pin configurations needed for a given platform.
00069  * \brief Change these values to port to other platforms.
00070  * \{
00071  */
00072 /* Define all possible revisions here */
00073 // Don't use zero, it will match if undefined!
00074 // RAVEN_D : Raven kit with LCD display
00075 // RAVENUSB_C : used for USB key or Raven card 
00076 // RCB_B : RZ200 kit from Atmel based on 1281V
00077 // ZIGBIT : Zigbit module from Meshnetics
00078 #define RAVEN_D     4
00079 #define RAVENUSB_C  1
00080 #define RCB_B           2
00081 #define ZIGBIT                  3
00082 
00083 
00084 
00085 
00086 #if RCB_REVISION == RCB_B
00087 /* 1281 rcb */
00088 #   define SSPORT     B
00089 #   define SSPIN      (0x00)
00090 #   define SPIPORT    B
00091 #   define MOSIPIN    (0x02)
00092 #   define MISOPIN    (0x03)
00093 #   define SCKPIN     (0x01)
00094 #   define RSTPORT    B
00095 #   define RSTPIN     (0x05)
00096 #   define IRQPORT    D
00097 #   define IRQPIN     (0x04)
00098 #   define SLPTRPORT  B
00099 #   define SLPTRPIN   (0x04)
00100 #   define USART      1
00101 #   define USARTVECT  USART1_RX_vect
00102 #   define TICKTIMER  3
00103 #   define HAS_SPARE_TIMER
00104 
00105 #elif HARWARE_REVISION == ZIGBIT
00106 /* 1281V Zigbit */
00107 #   define SSPORT     B
00108 #   define SSPIN      (0x00)
00109 #   define SPIPORT    B
00110 #   define MOSIPIN    (0x02)
00111 #   define MISOPIN    (0x03)
00112 #   define SCKPIN     (0x01)
00113 #   define RSTPORT    A
00114 #   define RSTPIN     (0x07)
00115 #   define IRQPORT    E
00116 #   define IRQPIN     (0x05)
00117 #   define SLPTRPORT  B
00118 #   define SLPTRPIN   (0x04)
00119 #   define TXCWPORT   B
00120 #   define TXCWPIN    (0x07)
00121 #   define USART      1
00122 #   define USARTVECT  USART1_RX_vect
00123 //#   define TICKTIMER  3
00124 //#   define HAS_SPARE_TIMER // Not used
00125 
00126 
00127 #elif RAVEN_REVISION == RAVEN_D
00128 /* 1284 raven */
00129 #   define SSPORT     B
00130 #   define SSPIN      (0x04)
00131 #   define SPIPORT    B
00132 #   define MOSIPIN    (0x05)
00133 #   define MISOPIN    (0x06)
00134 #   define SCKPIN     (0x07)
00135 #   define RSTPORT    B
00136 #   define RSTPIN     (0x01)
00137 #   define IRQPORT    D
00138 #   define IRQPIN     (0x06)
00139 #   define SLPTRPORT  B
00140 #   define SLPTRPIN   (0x03)
00141 #   define TXCWPORT   B
00142 #   define TXCWPIN    (0x00)
00143 #   define USART      1
00144 #   define USARTVECT  USART1_RX_vect
00145 #   define TICKTIMER  3
00146 #   define HAS_CW_MODE
00147 #   define HAS_SPARE_TIMER
00148 
00149 #elif RAVEN_REVISION == RAVENUSB_C
00150 /* 1287USB raven */
00151 #   define SSPORT     B
00152 #   define SSPIN      (0x00)
00153 #   define SPIPORT    B
00154 #   define MOSIPIN    (0x02)
00155 #   define MISOPIN    (0x03)
00156 #   define SCKPIN     (0x01)
00157 #   define RSTPORT    B
00158 #   define RSTPIN     (0x05)
00159 #   define IRQPORT    D
00160 #   define IRQPIN     (0x04)
00161 #   define SLPTRPORT  B
00162 #   define SLPTRPIN   (0x04)
00163 #   define TXCWPORT   B
00164 #   define TXCWPIN    (0x07)
00165 #   define USART      1
00166 #   define USARTVECT  USART1_RX_vect
00167 #   define TICKTIMER  3
00168 #   define HAS_CW_MODE
00169 #   define HAS_SPARE_TIMER
00170 
00171 #else
00172 
00173 #error "Platform undefined in hal.h"
00174 
00175 #endif
00176 
00177 /** \} */
00178 
00179 /**
00180  * \name Macros used to generate read register names from platform-specific definitions of ports.
00181  * \brief The various CAT macros (DDR, PORT, and PIN) are used to
00182  * assign port/pin/DDR names to various macro variables.  The
00183  * variables are assigned based on the specific connections made in
00184  * the hardware.  For example TCCR(TICKTIMER,A) can be used in place of TCCR0A
00185  * if TICKTIMER is defined as 0.
00186  * \{
00187  */
00188 #define CAT(x, y)      x##y
00189 #define CAT2(x, y, z)  x##y##z
00190 #define DDR(x)         CAT(DDR,  x)
00191 #define PORT(x)        CAT(PORT, x)
00192 #define PIN(x)         CAT(PIN,  x)
00193 #define UCSR(num, let) CAT2(UCSR,num,let)
00194 #define RXEN(x)        CAT(RXEN,x)
00195 #define TXEN(x)        CAT(TXEN,x)
00196 #define TXC(x)         CAT(TXC,x)
00197 #define RXC(x)         CAT(RXC,x)
00198 #define RXCIE(x)       CAT(RXCIE,x)
00199 #define UCSZ(x,y)      CAT2(UCSZ,x,y)
00200 #define UBRR(x,y)      CAT2(UBRR,x,y)
00201 #define UDRE(x)        CAT(UDRE,x)
00202 #define UDRIE(x)       CAT(UDRIE,x)
00203 #define UDR(x)         CAT(UDR,x)
00204 #define TCNT(x)        CAT(TCNT,x)
00205 #define TIMSK(x)       CAT(TIMSK,x)
00206 #define TCCR(x,y)      CAT2(TCCR,x,y)
00207 #define COM(x,y)       CAT2(COM,x,y)
00208 #define OCR(x,y)       CAT2(OCR,x,y)
00209 #define CS(x,y)        CAT2(CS,x,y)
00210 #define WGM(x,y)       CAT2(WGM,x,y)
00211 #define OCIE(x,y)      CAT2(OCIE,x,y)
00212 #define COMPVECT(x)    CAT2(TIMER,x,_COMPA_vect)
00213 #define UDREVECT(x)    CAT2(USART,x,_UDRE_vect)
00214 #define RXVECT(x)      CAT2(USART,x,_RX_vect)
00215 /** \} */
00216 
00217 /**
00218  * \name Pin macros
00219  * \brief These macros convert the platform-specific pin defines into names and functions
00220  *       that the source code can directly use.
00221  * \{
00222  */
00223 #define SLP_TR                SLPTRPIN            /**< Pin number that corresponds to the SLP_TR pin. */
00224 #define DDR_SLP_TR            DDR( SLPTRPORT )    /**< Data Direction Register that corresponds to the port where SLP_TR is connected. */
00225 #define PORT_SLP_TR           PORT( SLPTRPORT )   /**< Port (Write Access) where SLP_TR is connected. */
00226 #define PIN_SLP_TR            PIN( SLPTRPORT )    /**< Pin (Read Access) where SLP_TR is connected. */
00227 #define hal_set_slptr_high( ) ( PORT_SLP_TR |= ( 1 << SLP_TR ) )      /**< This macro pulls the SLP_TR pin high. */
00228 #define hal_set_slptr_low( )  ( PORT_SLP_TR &= ~( 1 << SLP_TR ) )     /**< This macro pulls the SLP_TR pin low. */
00229 #define hal_get_slptr( ) (    ( PIN_SLP_TR & ( 1 << SLP_TR ) ) >> SLP_TR )  /**< Read current state of the SLP_TR pin (High/Low). */
00230 #define RST                   RSTPIN              /**< Pin number that corresponds to the RST pin. */
00231 #define DDR_RST               DDR( RSTPORT )      /**< Data Direction Register that corresponds to the port where RST is */
00232 #define PORT_RST              PORT( RSTPORT )     /**< Port (Write Access) where RST is connected. */
00233 #define PIN_RST               PIN( RSTPORT )      /**< Pin (Read Access) where RST is connected. */
00234 #define hal_set_rst_high( )   ( PORT_RST |= ( 1 << RST ) )  /**< This macro pulls the RST pin high. */
00235 #define hal_set_rst_low( )    ( PORT_RST &= ~( 1 << RST ) ) /**< This macro pulls the RST pin low. */
00236 #define hal_get_rst( )        ( ( PIN_RST & ( 1 << RST )  ) >> RST )  /**< Read current state of the RST pin (High/Low). */
00237 #define HAL_SS_PIN            SSPIN               /**< The slave select pin. */
00238 #define HAL_PORT_SPI          PORT( SPIPORT )     /**< The SPI module is located on PORTB. */
00239 #define HAL_DDR_SPI           DDR( SPIPORT )      /**< Data Direction Register for PORTB. */
00240 #define HAL_DD_SS             SSPIN               /**< Data Direction bit for SS. */
00241 #define HAL_DD_SCK            SCKPIN              /**< Data Direction bit for SCK. */
00242 #define HAL_DD_MOSI           MOSIPIN             /**< Data Direction bit for MOSI. */
00243 #define HAL_DD_MISO           MISOPIN             /**< Data Direction bit for MISO. */
00244 /** \} */
00245 
00246 
00247 #define HAL_SS_HIGH( ) (HAL_PORT_SPI |= ( 1 << HAL_SS_PIN )) /**< MACRO for pulling SS high. */
00248 #define HAL_SS_LOW( )  (HAL_PORT_SPI &= ~( 1 << HAL_SS_PIN )) /**< MACRO for pulling SS low. */
00249 
00250 /** \brief Macros defined for HAL_TIMER1.
00251  *
00252  *  These macros are used to define the correct setupt of the AVR's Timer1, and
00253  *  to ensure that the hal_get_system_time function returns the system time in
00254  *  symbols (16 us ticks).
00255  */
00256 
00257 #if ( F_CPU == 16000000UL )
00258     #define HAL_TCCR1B_CONFIG ( ( 1 << ICES1 ) | ( 1 << CS12 ) )
00259     #define HAL_US_PER_SYMBOL ( 1 )
00260     #define HAL_SYMBOL_MASK   ( 0xFFFFffff )
00261 #elif ( F_CPU == 8000000UL )
00262     #define HAL_TCCR1B_CONFIG ( ( 1 << ICES1 ) | ( 1 << CS11 ) | ( 1 << CS10 ) )
00263     #define HAL_US_PER_SYMBOL ( 2 )
00264     #define HAL_SYMBOL_MASK   ( 0x7FFFffff )
00265 #elif ( F_CPU == 4000000UL )
00266     #define HAL_TCCR1B_CONFIG ( ( 1 << ICES1 ) | ( 1 << CS11 ) | ( 1 << CS10 ) )
00267     #define HAL_US_PER_SYMBOL ( 1 )
00268     #define HAL_SYMBOL_MASK   ( 0xFFFFffff )
00269 #elif ( F_CPU == 1000000UL )
00270     #define HAL_TCCR1B_CONFIG ( ( 1 << ICES1 ) | ( 1 << CS11 ) )
00271     #define HAL_US_PER_SYMBOL ( 2 )
00272     #define HAL_SYMBOL_MASK   ( 0x7FFFffff )
00273 #else
00274     #error "Clock speed not supported."
00275 #endif
00276 
00277 #if HARWARE_REVISION == ZIGBIT
00278 // IRQ E5 for Zigbit example
00279 #define RADIO_VECT INT5_vect
00280 #define HAL_ENABLE_RADIO_INTERRUPT( ) { ( EIMSK |= ( 1 << INT5 ) ) ; EICRB |= 0x0C ; PORTE &= ~(1<<PE5);  DDRE &= ~(1<<DDE5); }
00281 #define HAL_DISABLE_RADIO_INTERRUPT( ) ( EIMSK &= ~( 1 << INT5 ) )
00282 #else
00283 #define RADIO_VECT TIMER1_CAPT_vect
00284 #define HAL_ENABLE_RADIO_INTERRUPT( ) ( TIMSK1 |= ( 1 << ICIE1 ) )
00285 #define HAL_DISABLE_RADIO_INTERRUPT( ) ( TIMSK1 &= ~( 1 << ICIE1 ) )
00286 #endif
00287 
00288 #define HAL_ENABLE_OVERFLOW_INTERRUPT( ) ( TIMSK1 |= ( 1 << TOIE1 ) )
00289 #define HAL_DISABLE_OVERFLOW_INTERRUPT( ) ( TIMSK1 &= ~( 1 << TOIE1 ) )
00290 
00291 /** This macro will protect the following code from interrupts.*/
00292 #define AVR_ENTER_CRITICAL_REGION( ) {uint8_t volatile saved_sreg = SREG; cli( )
00293 
00294 /** This macro must always be used in conjunction with AVR_ENTER_CRITICAL_REGION
00295     so that interrupts are enabled again.*/
00296 #define AVR_LEAVE_CRITICAL_REGION( ) SREG = saved_sreg;}
00297 
00298 
00299 /** \brief  Enable the interrupt from the radio transceiver.
00300  */
00301 #define hal_enable_trx_interrupt( ) HAL_ENABLE_RADIO_INTERRUPT( )
00302 
00303 /** \brief  Disable the interrupt from the radio transceiver.
00304  *
00305  *  \retval 0 if the pin is low, 1 if the pin is high.
00306  */
00307 #define hal_disable_trx_interrupt( ) HAL_DISABLE_RADIO_INTERRUPT( )
00308 /*============================ TYPDEFS =======================================*/
00309 /*============================ PROTOTYPES ====================================*/
00310 /*============================ MACROS ========================================*/
00311 /** \name Macros for radio operation.
00312  * \{ 
00313  */
00314 #define HAL_BAT_LOW_MASK       ( 0x80 ) /**< Mask for the BAT_LOW interrupt. */
00315 #define HAL_TRX_UR_MASK        ( 0x40 ) /**< Mask for the TRX_UR interrupt. */
00316 #define HAL_TRX_END_MASK       ( 0x08 ) /**< Mask for the TRX_END interrupt. */
00317 #define HAL_RX_START_MASK      ( 0x04 ) /**< Mask for the RX_START interrupt. */
00318 #define HAL_PLL_UNLOCK_MASK    ( 0x02 ) /**< Mask for the PLL_UNLOCK interrupt. */
00319 #define HAL_PLL_LOCK_MASK      ( 0x01 ) /**< Mask for the PLL_LOCK interrupt. */
00320 
00321 #define HAL_MIN_FRAME_LENGTH   ( 0x03 ) /**< A frame should be at least 3 bytes. */
00322 #define HAL_MAX_FRAME_LENGTH   ( 0x7F ) /**< A frame should no more than 127 bytes. */
00323 /** \} */
00324 /*============================ TYPDEFS =======================================*/
00325 /** \struct hal_rx_frame_t
00326  *  \brief  This struct defines the rx data container.
00327  *
00328  *  \see hal_frame_read
00329  */
00330 typedef struct{
00331     uint8_t length;                       /**< Length of frame. */
00332     uint8_t data[ HAL_MAX_FRAME_LENGTH ]; /**< Actual frame data. */
00333     uint8_t lqi;                          /**< LQI value for received frame. */
00334     bool crc;                             /**< Flag - did CRC pass for received frame? */
00335 } hal_rx_frame_t;
00336 
00337 /** RX_START event handler callback type. Is called with timestamp in IEEE 802.15.4 symbols and frame length. See hal_set_rx_start_event_handler(). */
00338 typedef void (*hal_rx_start_isr_event_handler_t)(uint32_t const isr_timestamp, uint8_t const frame_length);
00339 
00340 /** RRX_END event handler callback type. Is called with timestamp in IEEE 802.15.4 symbols and frame length. See hal_set_trx_end_event_handler(). */
00341 typedef void (*hal_trx_end_isr_event_handler_t)(uint32_t const isr_timestamp);
00342 
00343 typedef void (*rx_callback_t) (uint16_t data);
00344 
00345 /*============================ PROTOTYPES ====================================*/
00346 void hal_init( void );
00347 
00348 void hal_reset_flags( void );
00349 uint8_t hal_get_bat_low_flag( void );
00350 void hal_clear_bat_low_flag( void );
00351 
00352 hal_trx_end_isr_event_handler_t hal_get_trx_end_event_handler( void );
00353 void hal_set_trx_end_event_handler( hal_trx_end_isr_event_handler_t trx_end_callback_handle );
00354 void hal_clear_trx_end_event_handler( void );
00355 
00356 hal_rx_start_isr_event_handler_t hal_get_rx_start_event_handler( void );
00357 void hal_set_rx_start_event_handler( hal_rx_start_isr_event_handler_t rx_start_callback_handle );
00358 void hal_clear_rx_start_event_handler( void );
00359 
00360 uint8_t hal_get_pll_lock_flag( void );
00361 void hal_clear_pll_lock_flag( void );
00362 
00363 uint8_t hal_register_read( uint8_t address );
00364 void hal_register_write( uint8_t address, uint8_t value );
00365 uint8_t hal_subregister_read( uint8_t address, uint8_t mask, uint8_t position );
00366 void hal_subregister_write( uint8_t address, uint8_t mask, uint8_t position,
00367                             uint8_t value );
00368 void hal_frame_read(hal_rx_frame_t *rx_frame, rx_callback_t rx_callback);
00369 void hal_frame_write( uint8_t *write_buffer, uint8_t length );
00370 void hal_sram_read( uint8_t address, uint8_t length, uint8_t *data );
00371 void hal_sram_write( uint8_t address, uint8_t length, uint8_t *data );
00372 
00373 #endif
00374 /** @} */
00375 /*EOF*/

Generated on Mon Apr 11 14:23:37 2011 for Contiki 2.5 by  doxygen 1.6.1