Contiki 3.x
hal.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  * David Kopf dak664@embarqmail.com
13  * Ivan Delamer delamer@ieee.com
14  *
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are met:
19  *
20  * * Redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer.
22  * * Redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in
24  * the documentation and/or other materials provided with the
25  * distribution.
26  * * Neither the name of the copyright holders nor the names of
27  * contributors may be used to endorse or promote products derived
28  * from this software without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  */
42 
43 /**
44  * \addtogroup hal
45  * @{
46  */
47 
48 /**
49  * \file
50  * \brief This file contains low-level radio driver code.
51  *
52 */
53 
54 #ifndef HAL_AVR_H
55 #define HAL_AVR_H
56 /*============================ INCLUDE =======================================*/
57 #include <stdint.h>
58 #include <stdbool.h>
59 //#include <util/crc16.h>
60 #include "contiki-conf.h"
61 /*============================ MACROS ========================================*/
62 
63 /** \name This is the list of pin configurations needed for a given platform.
64  * \brief Change these values to port to other platforms.
65  * \{
66  */
67 /* Define all possible platform types/revisions here. */
68 // Don't use zero, it will match if undefined!
69 // RAVEN_D : Raven kit with LCD display
70 // RAVENUSB_C : used for RZRAVEN USB key
71 // RCB_B : RZ200 kit from Atmel based on 1281V
72 // ZIGBIT : Zigbit module from Meshnetics
73 // ATMEGA128RFA1 : Bare chip with internal radio
74 // IRIS : IRIS Mote from MEMSIC
75 #define RAVENUSB_C 1
76 #define RAVEN_D 2
77 #define RCB_B 3
78 #define ZIGBIT 4
79 #define IRIS 5
80 #define ATMEGA128RFA1 6
81 
82 #if PLATFORM_TYPE == RCB_B
83 /* 1281 rcb */
84 # define SSPORT B
85 # define SSPIN (0x00)
86 # define SPIPORT B
87 # define MOSIPIN (0x02)
88 # define MISOPIN (0x03)
89 # define SCKPIN (0x01)
90 # define RSTPORT B
91 # define RSTPIN (0x05)
92 # define IRQPORT D
93 # define IRQPIN (0x04)
94 # define SLPTRPORT B
95 # define SLPTRPIN (0x04)
96 
97 #elif PLATFORM_TYPE == ZIGBIT
98 /* 1281V Zigbit */
99 # define SSPORT B
100 # define SSPIN (0x00)
101 # define SPIPORT B
102 # define MOSIPIN (0x02)
103 # define MISOPIN (0x03)
104 # define SCKPIN (0x01)
105 # define RSTPORT A
106 # define RSTPIN (0x07)
107 # define IRQPORT E
108 # define IRQPIN (0x05)
109 # define SLPTRPORT B
110 # define SLPTRPIN (0x04)
111 
112 
113 #elif PLATFORM_TYPE == RAVEN_D
114 /* 1284 raven */
115 # define SSPORT B
116 # define SSPIN (0x04)
117 # define SPIPORT B
118 # define MOSIPIN (0x05)
119 # define MISOPIN (0x06)
120 # define SCKPIN (0x07)
121 # define RSTPORT B
122 # define RSTPIN (0x01)
123 # define IRQPORT D
124 # define IRQPIN (0x06)
125 # define SLPTRPORT B
126 # define SLPTRPIN (0x03)
127 
128 #elif PLATFORM_TYPE == RAVENUSB_C
129 /* 1287USB raven */
130 # define SSPORT B
131 # define SSPIN (0x00)
132 # define SPIPORT B
133 # define MOSIPIN (0x02)
134 # define MISOPIN (0x03)
135 # define SCKPIN (0x01)
136 # define RSTPORT B
137 # define RSTPIN (0x05)
138 # define IRQPORT D
139 # define IRQPIN (0x04)
140 # define SLPTRPORT B
141 # define SLPTRPIN (0x04)
142 
143 #elif PLATFORM_TYPE == ATMEGA128RFA1
144 /* ATmega1281 with internal AT86RF231 radio */
145 # define SLPTRPORT TRXPR
146 # define SLPTRPIN 1
147 
148 #elif CONTIKI_TARGET_MULLE
149 /* mulle 5.2 (TODO: move to platform specific) */
150 # define SSPORT 3
151 # define SSPIN 5
152 # define MOSIPORT 1
153 # define MOSIPIN 1
154 # define MISOPORT 1
155 # define MISOPIN 0
156 # define SCKPORT 3
157 # define SCKPIN 3
158 # define RSTPORT 4
159 # define RSTPIN 3
160 # define IRQPORT 8
161 # define IRQPIN 3
162 # define SLPTRPORT 0
163 # define SLPTRPIN 7
164 
165 #elif PLATFORM_TYPE == IRIS
166 /* 1281 IRIS */
167 # define SSPORT B
168 # define SSPIN (0x00)
169 # define SPIPORT B
170 # define MOSIPIN (0x02)
171 # define MISOPIN (0x03)
172 # define SCKPIN (0x01)
173 # define RSTPORT A
174 # define RSTPIN (0x06)
175 # define IRQPORT D
176 # define IRQPIN (0x04)
177 # define SLPTRPORT B
178 # define SLPTRPIN (0x07)
179 #else
180 
181 #error "PLATFORM_TYPE undefined in hal.h"
182 
183 #endif
184 
185 /* For architectures that have all SPI signals on the same port */
186 #ifndef SSPORT
187 #define SSPORT SPIPORT
188 #endif
189 
190 #ifndef SCKPORT
191 #define SCKPORT SPIPORT
192 #endif
193 
194 #ifndef MOSIPORT
195 #define MOSIPORT SPIPORT
196 #endif
197 
198 #ifndef MISOPORT
199 #define MISOPORT SPIPORT
200 #endif
201 
202 /** \} */
203 
204 /**
205  * \name Macros used to generate read register names from platform-specific definitions of ports.
206  * \brief The various CAT macros (DDR, PORT, and PIN) are used to
207  * assign port/pin/DDR names to various macro variables. The
208  * variables are assigned based on the specific connections made in
209  * the hardware. For example TCCR(TICKTIMER,A) can be used in place of TCCR0A
210  * if TICKTIMER is defined as 0.
211  * \{
212  */
213 #if defined(__AVR__)
214 #define CAT(x, y) x##y
215 #define DDR(x) CAT(DDR, x)
216 #define PORT(x) CAT(PORT, x)
217 #define PIN(x) CAT(PIN, x)
218 #endif
219 
220 /* TODO: Move to CPU specific */
221 #if defined(CONTIKI_TARGET_MULLE)
222 #define CAT(x, y) x##y.BYTE
223 #define DDR(x) CAT(PD, x)
224 #define PORT(x) CAT(P, x)
225 #define PIN(x) CAT(P, x)
226 #endif
227 
228 /** \} */
229 
230 /**
231  * \name Pin macros
232  * \brief These macros convert the platform-specific pin defines into names and functions
233  * that the source code can directly use.
234  * \{
235  */
236 #if defined(__AVR_ATmega128RFA1__)
237 
238 #define hal_set_rst_low( ) ( TRXPR &= ~( 1 << TRXRST ) ) /**< This macro pulls the RST pin low. */
239 #define hal_set_rst_high( ) ( TRXPR |= ( 1 << TRXRST ) ) /**< This macro pulls the RST pin high. */
240 #define hal_set_slptr_high( ) ( TRXPR |= ( 1 << SLPTR ) ) /**< This macro pulls the SLP_TR pin high. */
241 #define hal_set_slptr_low( ) ( TRXPR &= ~( 1 << SLPTR ) ) /**< This macro pulls the SLP_TR pin low. */
242 #define hal_get_slptr( ) ( TRXPR & ( 1 << SLPTR ) ) /**< Read current state of the SLP_TR pin (High/Low). */
243 
244 #else
245 #define SLP_TR SLPTRPIN /**< Pin number that corresponds to the SLP_TR pin. */
246 #define DDR_SLP_TR DDR( SLPTRPORT ) /**< Data Direction Register that corresponds to the port where SLP_TR is connected. */
247 #define PORT_SLP_TR PORT( SLPTRPORT ) /**< Port (Write Access) where SLP_TR is connected. */
248 #define PIN_SLP_TR PIN( SLPTRPORT ) /**< Pin (Read Access) where SLP_TR is connected. */
249 #define hal_set_slptr_high( ) ( PORT_SLP_TR |= ( 1 << SLP_TR ) ) /**< This macro pulls the SLP_TR pin high. */
250 #define hal_set_slptr_low( ) ( PORT_SLP_TR &= ~( 1 << SLP_TR ) ) /**< This macro pulls the SLP_TR pin low. */
251 #define hal_get_slptr( ) ( PIN_SLP_TR & ( 1 << SLP_TR ) ) /**< Read current state of the SLP_TR pin (High/Low). */
252 #define RST RSTPIN /**< Pin number that corresponds to the RST pin. */
253 #define DDR_RST DDR( RSTPORT ) /**< Data Direction Register that corresponds to the port where RST is */
254 #define PORT_RST PORT( RSTPORT ) /**< Port (Write Access) where RST is connected. */
255 #define PIN_RST PIN( RSTPORT /* BUG? */) /**< Pin (Read Access) where RST is connected. */
256 #define hal_set_rst_high( ) ( PORT_RST |= ( 1 << RST ) ) /**< This macro pulls the RST pin high. */
257 #define hal_set_rst_low( ) ( PORT_RST &= ~( 1 << RST ) ) /**< This macro pulls the RST pin low. */
258 #define hal_get_rst( ) ( ( PIN_RST & ( 1 << RST ) ) >> RST ) /**< Read current state of the RST pin (High/Low). */
259 #define HAL_SS_PIN SSPIN /**< The slave select pin. */
260 #define HAL_SCK_PIN SCKPIN /**< Data bit for SCK. */
261 #define HAL_MOSI_PIN MOSIPIN
262 #define HAL_MISO_PIN MISOPIN
263 #define HAL_PORT_SPI PORT( SPIPORT ) /**< The SPI module is located on PORTB. */
264 #define HAL_PORT_SS PORT( SSPORT )
265 #define HAL_PORT_SCK PORT( SCKPORT )
266 #define HAL_PORT_MOSI PORT( MOSIPORT ) /**< The SPI module uses GPIO might be split on different ports. */
267 #define HAL_PORT_MISO PORT( MISOPORT ) /**< The SPI module uses GPIO might be split on different ports. */
268 #define HAL_DDR_SPI DDR( SPIPORT ) /**< Data Direction Register for PORTB. */
269 #define HAL_DDR_SS DDR( SSPORT ) /**< Data Direction Register for MISO GPIO pin. */
270 #define HAL_DDR_SCK DDR( SCKPORT ) /**< Data Direction Register for MISO GPIO pin. */
271 #define HAL_DDR_MOSI DDR( MOSIPORT ) /**< Data Direction Register for MISO GPIO pin. */
272 #define HAL_DDR_MISO DDR( MISOPORT ) /**< Data Direction Register for MOSI GPIO pin. */
273 #define HAL_DD_SS SSPIN /**< Data Direction bit for SS. */
274 #define HAL_DD_SCK SCKPIN /**< Data Direction bit for SCK. */
275 #define HAL_DD_MOSI MOSIPIN /**< Data Direction bit for MOSI. */
276 #define HAL_DD_MISO MISOPIN /**< Data Direction bit for MISO. */
277 #endif /* defined(__AVR_ATmega128RFA1__) */
278 
279 /** \} */
280 
281 
282 #define HAL_SS_HIGH( ) (HAL_PORT_SS |= ( 1 << HAL_SS_PIN )) /**< MACRO for pulling SS high. */
283 #define HAL_SS_LOW( ) (HAL_PORT_SS &= ~( 1 << HAL_SS_PIN )) /**< MACRO for pulling SS low. */
284 
285 #if defined(__AVR__)
286 
287 #if PLATFORM_TYPE == ZIGBIT
288 // IRQ E5 for Zigbit example
289 #define RADIO_VECT INT5_vect
290 #define HAL_ENABLE_RADIO_INTERRUPT( ) { ( EIMSK |= ( 1 << INT5 ) ) ; EICRB |= 0x0C ; PORTE &= ~(1<<PE5); DDRE &= ~(1<<DDE5); }
291 #define HAL_DISABLE_RADIO_INTERRUPT( ) ( EIMSK &= ~( 1 << INT5 ) )
292 #else
293 #define RADIO_VECT TIMER1_CAPT_vect
294 // Raven and Jackdaw
295 #define HAL_ENABLE_RADIO_INTERRUPT( ) { TCCR1B = ( 1 << ICES1 ) | ( 1 << CS10 ); TIFR1 |= (1 << ICF1); TIMSK1 |= ( 1 << ICIE1 ) ; }
296 #define HAL_DISABLE_RADIO_INTERRUPT( ) ( TIMSK1 &= ~( 1 << ICIE1 ) )
297 #endif
298 
299 #define HAL_ENABLE_OVERFLOW_INTERRUPT( ) ( TIMSK1 |= ( 1 << TOIE1 ) )
300 #define HAL_DISABLE_OVERFLOW_INTERRUPT( ) ( TIMSK1 &= ~( 1 << TOIE1 ) )
301 
302 /** This macro will protect the following code from interrupts.*/
303 #define HAL_ENTER_CRITICAL_REGION( ) {uint8_t volatile saved_sreg = SREG; cli( )
304 
305 /** This macro must always be used in conjunction with HAL_ENTER_CRITICAL_REGION
306  so that interrupts are enabled again.*/
307 #define HAL_LEAVE_CRITICAL_REGION( ) SREG = saved_sreg;}
308 
309 #else /* MULLE */
310 
311 #define HAL_ENABLE_RADIO_INTERRUPT( ) ( INT1IC.BYTE |= 1 )
312 #define HAL_DISABLE_RADIO_INTERRUPT( ) ( INT1IC.BYTE &= ~(1) )
313 
314 #define HAL_ENABLE_OVERFLOW_INTERRUPT( ) ( TB4IC.BYTE = 1 )
315 #define HAL_DISABLE_OVERFLOW_INTERRUPT( ) ( TB4IC.BYTE = 0 )
316 
317 /** This macro will protect the following code from interrupts.*/
318 #define HAL_ENTER_CRITICAL_REGION( ) MULLE_ENTER_CRITICAL_REGION( )
319 
320 /** This macro must always be used in conjunction with HAL_ENTER_CRITICAL_REGION
321  so that interrupts are enabled again.*/
322 #define HAL_LEAVE_CRITICAL_REGION( ) MULLE_LEAVE_CRITICAL_REGION( )
323 
324 #endif /* !__AVR__ */
325 
326 
327 /** \brief Enable the interrupt from the radio transceiver.
328  */
329 #define hal_enable_trx_interrupt( ) HAL_ENABLE_RADIO_INTERRUPT( )
330 
331 /** \brief Disable the interrupt from the radio transceiver.
332  *
333  * \retval 0 if the pin is low, 1 if the pin is high.
334  */
335 #define hal_disable_trx_interrupt( ) HAL_DISABLE_RADIO_INTERRUPT( )
336 /*============================ TYPDEFS =======================================*/
337 /*============================ PROTOTYPES ====================================*/
338 /*============================ MACROS ========================================*/
339 /** \name Macros for radio operation.
340  * \{
341  */
342 #define HAL_BAT_LOW_MASK ( 0x80 ) /**< Mask for the BAT_LOW interrupt. */
343 #define HAL_TRX_UR_MASK ( 0x40 ) /**< Mask for the TRX_UR interrupt. */
344 #define HAL_TRX_END_MASK ( 0x08 ) /**< Mask for the TRX_END interrupt. */
345 #define HAL_RX_START_MASK ( 0x04 ) /**< Mask for the RX_START interrupt. */
346 #define HAL_PLL_UNLOCK_MASK ( 0x02 ) /**< Mask for the PLL_UNLOCK interrupt. */
347 #define HAL_PLL_LOCK_MASK ( 0x01 ) /**< Mask for the PLL_LOCK interrupt. */
348 
349 #define HAL_MIN_FRAME_LENGTH ( 0x03 ) /**< A frame should be at least 3 bytes. */
350 #define HAL_MAX_FRAME_LENGTH ( 0x7F ) /**< A frame should no more than 127 bytes. */
351 /** \} */
352 /*============================ TYPDEFS =======================================*/
353 /** \struct hal_rx_frame_t
354  * \brief This struct defines the rx data container.
355  *
356  * \see hal_frame_read
357  */
358 typedef struct{
359  uint8_t length; /**< Length of frame. */
360  uint8_t data[ HAL_MAX_FRAME_LENGTH ]; /**< Actual frame data. */
361  uint8_t lqi; /**< LQI value for received frame. */
362  bool crc; /**< Flag - did CRC pass for received frame? */
364 
365 
366 /*============================ PROTOTYPES ====================================*/
367 void hal_init( void );
368 
369 /* Hack for atmega128rfa1 with integrated radio. Access registers directly, not through SPI */
370 #if defined(__AVR_ATmega128RFA1__)
371 //#define hal_register_read(address) _SFR_MEM8((uint16_t)address)
372 #define hal_register_read(address) address
373 uint8_t hal_subregister_read( uint16_t address, uint8_t mask, uint8_t position );
374 void hal_subregister_write( uint16_t address, uint8_t mask, uint8_t position,
375  uint8_t value );
376 
377 //#define hal_register_write(address, value) _SFR_MEM8((uint16_t)address)=value
378 #define hal_register_write(address, value) address=value
379 //#define hal_subregister_read( address, mask, position ) (_SFR_MEM8((uint16_t)address)&mask)>>position
380 //#define hal_subregister_read1( address, mask, position ) (address&mask)>>position
381 //#define hal_subregister_write( address, mask, position, value ) address=(address<<position)&mask
382 #else
383 uint8_t hal_register_read( uint8_t address );
384 void hal_register_write( uint8_t address, uint8_t value );
385 uint8_t hal_subregister_read( uint8_t address, uint8_t mask, uint8_t position );
386 void hal_subregister_write( uint8_t address, uint8_t mask, uint8_t position,
387  uint8_t value );
388 #endif
389 
390 
391 
392 void hal_frame_read(hal_rx_frame_t *rx_frame);
393 void hal_frame_write( uint8_t *write_buffer, uint8_t length );
394 void hal_sram_read( uint8_t address, uint8_t length, uint8_t *data );
395 void hal_sram_write( uint8_t address, uint8_t length, uint8_t *data );
396 /* Number of receive buffers in RAM. */
397 #ifndef RF230_CONF_RX_BUFFERS
398 #define RF230_CONF_RX_BUFFERS 1
399 #endif
400 
401 #endif
402 /** @} */
403 /*EOF*/
uint8_t hal_register_read(uint8_t address)
This function reads data from one of the radio transceiver&#39;s registers.
Definition: hal.c:304
uint8_t hal_subregister_read(uint8_t address, uint8_t mask, uint8_t position)
This function reads the value of a specific subregister.
Definition: hal.c:377
void hal_init(void)
This function initializes the Hardware Abstraction Layer.
Definition: hal.c:133
This struct defines the rx data container.
Definition: hal.h:351
void hal_subregister_write(uint8_t address, uint8_t mask, uint8_t position, uint8_t value)
This function writes a new value to one of the radio transceiver&#39;s subregisters.
Definition: hal.c:400
void hal_sram_read(uint8_t address, uint8_t length, uint8_t *data)
Read SRAM.
Definition: hal.c:558
void hal_frame_read(hal_rx_frame_t *rx_frame, rx_callback_t rx_callback)
This function will upload a frame from the radio transceiver&#39;s frame buffer.
Definition: hal.c:429
void hal_sram_write(uint8_t address, uint8_t length, uint8_t *data)
Write SRAM.
Definition: hal.c:597
void hal_frame_write(uint8_t *write_buffer, uint8_t length)
This function will download a frame to the radio transceiver&#39;s frame buffer.
Definition: hal.c:516
#define HAL_MAX_FRAME_LENGTH
A frame should no more than 127 bytes.
Definition: hal.h:350
void hal_register_write(uint8_t address, uint8_t value)
This function writes a new value to one of the radio transceiver&#39;s registers.
Definition: hal.c:342