Contiki-Inga 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 INGA_12 1
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 #elif INGA_REVISION == INGA_V12
113 /* 1284 raven */
114 # define SSPORT B
115 # define SSPIN (0x04)
116 # define SPIPORT B
117 # define MOSIPIN (0x05)
118 # define MISOPIN (0x06)
119 # define SCKPIN (0x07)
120 # define RSTPORT B
121 # define RSTPIN (0x01)
122 # define IRQPORT D
123 # define IRQPIN (0x06)
124 # define SLPTRPORT B
125 # define SLPTRPIN (0x03)
126 # define TXCWPORT B
127 # define TXCWPIN (0x00)
128 # define USART 0
129 # define USARTVECT USART0_RX_vect
130 # define TICKTIMER 3
131 # define HAS_CW_MODE
132 # define HAS_SPARE_TIMER
133 
134 #elif PLATFORM_TYPE == RAVEN_D
135 /* 1284 raven */
136 # define SSPORT B
137 # define SSPIN (0x04)
138 # define SPIPORT B
139 # define MOSIPIN (0x05)
140 # define MISOPIN (0x06)
141 # define SCKPIN (0x07)
142 # define RSTPORT B
143 # define RSTPIN (0x01)
144 # define IRQPORT D
145 # define IRQPIN (0x06)
146 # define SLPTRPORT B
147 # define SLPTRPIN (0x03)
148 
149 #elif PLATFORM_TYPE == RAVENUSB_C
150 /* 1287USB raven */
151 # define SSPORT B
152 # define SSPIN (0x00)
153 # define SPIPORT B
154 # define MOSIPIN (0x02)
155 # define MISOPIN (0x03)
156 # define SCKPIN (0x01)
157 # define RSTPORT B
158 # define RSTPIN (0x05)
159 # define IRQPORT D
160 # define IRQPIN (0x04)
161 # define SLPTRPORT B
162 # define SLPTRPIN (0x04)
163 
164 #elif PLATFORM_TYPE == ATMEGA128RFA1
165 /* ATmega1281 with internal AT86RF231 radio */
166 # define SLPTRPORT TRXPR
167 # define SLPTRPIN 1
168 
169 #elif CONTIKI_TARGET_MULLE
170 /* mulle 5.2 (TODO: move to platform specific) */
171 # define SSPORT 3
172 # define SSPIN 5
173 # define MOSIPORT 1
174 # define MOSIPIN 1
175 # define MISOPORT 1
176 # define MISOPIN 0
177 # define SCKPORT 3
178 # define SCKPIN 3
179 # define RSTPORT 4
180 # define RSTPIN 3
181 # define IRQPORT 8
182 # define IRQPIN 3
183 # define SLPTRPORT 0
184 # define SLPTRPIN 7
185 
186 #elif PLATFORM_TYPE == IRIS
187 /* 1281 IRIS */
188 # define SSPORT B
189 # define SSPIN (0x00)
190 # define SPIPORT B
191 # define MOSIPIN (0x02)
192 # define MISOPIN (0x03)
193 # define SCKPIN (0x01)
194 # define RSTPORT A
195 # define RSTPIN (0x06)
196 # define IRQPORT D
197 # define IRQPIN (0x04)
198 # define SLPTRPORT B
199 # define SLPTRPIN (0x07)
200 #elif defined(__AVR_ATxmega256A3BU__) || defined(__AVR_ATxmega256A3__) || defined(__AVR_ATxmega256A3U__) || defined(__AVR_ATxmega256A3B__)
201  // these defines are CPU Speficic - So take a look into your platform file!
202 #else
203 
204 #error "PLATFORM_TYPE undefined in hal.h"
205 
206 #endif
207 
208 /* For architectures that have all SPI signals on the same port */
209 #ifndef SSPORT
210 #define SSPORT SPIPORT
211 #endif
212 
213 #ifndef SCKPORT
214 #define SCKPORT SPIPORT
215 #endif
216 
217 #ifndef MOSIPORT
218 #define MOSIPORT SPIPORT
219 #endif
220 
221 #ifndef MISOPORT
222 #define MISOPORT SPIPORT
223 #endif
224 
225 /** \} */
226 
227 /**
228  * \name Macros used to generate read register names from platform-specific definitions of ports.
229  * \brief The various CAT macros (DDR, PORT, and PIN) are used to
230  * assign port/pin/DDR names to various macro variables. The
231  * variables are assigned based on the specific connections made in
232  * the hardware. For example TCCR(TICKTIMER,A) can be used in place of TCCR0A
233  * if TICKTIMER is defined as 0.
234  * \{
235  */
236 #if defined(__AVR_XMEGA__)
237  #define CAT(x, y) x##y
238 
239  #define CAT_DIR(x, y) x##y .DIR
240  #define CAT_OUT(x,y) x##y .OUT
241  #define CAT_IN(x,y) x##y .IN
242 
243  #define CAT2(x, y, z) x##y##z
244 
245  #define CAT2_DIR(x, y, z) x##y##z .DIR
246  #define CAT2_IN(x, y, z) x##y##z .IN
247  #define CAT2_OUT(x, y, z) x##y##z .OUT
248 
249  #define DDR(x) CAT_DIR(PORT, x)
250  #define PORT(x) CAT_OUT(PORT, x)
251  #define PIN(x) CAT_OUT(PORT, x) // Pin is used for READ operations only
252  #define UCSR(num, let) CAT2(UCSR,num,let)
253  #define RXEN(x) CAT(RXEN,x)
254  #define TXEN(x) CAT(TXEN,x)
255  #define TXC(x) CAT(TXC,x)
256  #define RXC(x) CAT(RXC,x)
257  #define RXCIE(x) CAT(RXCIE,x)
258  #define UCSZ(x,y) CAT2(UCSZ,x,y)
259  #define UBRR(x,y) CAT2(UBRR,x,y)
260  #define UDRE(x) CAT(UDRE,x)
261  #define UDRIE(x) CAT(UDRIE,x)
262  #define UDR(x) CAT(UDR,x)
263  #define TCNT(x) CAT(TCNT,x)
264  #define TIMSK(x) CAT(TIMSK,x)
265  #define TCCR(x,y) CAT2(TCCR,x,y)
266  #define COM(x,y) CAT2(COM,x,y)
267  #define OCR(x,y) CAT2(OCR,x,y)
268  #define CS(x,y) CAT2(CS,x,y)
269  #define WGM(x,y) CAT2(WGM,x,y)
270  #define OCIE(x,y) CAT2(OCIE,x,y)
271  #define COMPVECT(x) CAT2(TIMER,x,_COMPA_vect)
272  #define UDREVECT(x) CAT2(USART,x,_UDRE_vect)
273  #define RXVECT(x) CAT2(USART,x,_RX_vect)
274 #elif defined(__AVR__)
275  #define CAT(x, y) x##y
276  #define DDR(x) CAT(DDR, x)
277  #define PORT(x) CAT(PORT, x)
278  #define PIN(x) CAT(PIN, x)
279 #endif
280 
281 /* TODO: Move to CPU specific */
282 #if defined(CONTIKI_TARGET_MULLE)
283  #define CAT(x, y) x##y.BYTE
284  #define DDR(x) CAT(PD, x)
285  #define PORT(x) CAT(P, x)
286  #define PIN(x) CAT(P, x)
287 #endif
288 
289 /** \} */
290 
291 /**
292  * \name Pin macros
293  * \brief These macros convert the platform-specific pin defines into names and functions
294  * that the source code can directly use.
295  * \{
296  */
297 #if defined(__AVR_ATmega128RFA1__)
298 
299 #define hal_set_rst_low( ) ( TRXPR &= ~( 1 << TRXRST ) ) /**< This macro pulls the RST pin low. */
300 #define hal_set_rst_high( ) ( TRXPR |= ( 1 << TRXRST ) ) /**< This macro pulls the RST pin high. */
301 #define hal_set_slptr_high( ) ( TRXPR |= ( 1 << SLPTR ) ) /**< This macro pulls the SLP_TR pin high. */
302 #define hal_set_slptr_low( ) ( TRXPR &= ~( 1 << SLPTR ) ) /**< This macro pulls the SLP_TR pin low. */
303 #define hal_get_slptr( ) ( TRXPR & ( 1 << SLPTR ) ) /**< Read current state of the SLP_TR pin (High/Low). */
304 
305 #elif defined(__AVR_XMEGA__)
306 #define SLP_TR SLPTRPIN /**< Pin number that corresponds to the SLP_TR pin. */
307 #define DDR_SLP_TR DDR( SLPTRPORT ) /**< Data Direction Register that corresponds to the port where SLP_TR is connected. */
308 #define PORT_SLP_TR PORT( SLPTRPORT ) /**< Port (Write Access) where SLP_TR is connected. */
309 #define PIN_SLP_TR PIN( SLPTRPORT ) /**< Pin (Read Access) where SLP_TR is connected. */
310 #define hal_set_slptr_high( ) ( PORT_SLP_TR |= ( 1 << SLP_TR ) ) /**< This macro pulls the SLP_TR pin high. */
311 #define hal_set_slptr_low( ) ( PORT_SLP_TR &= ~( 1 << SLP_TR ) ) /**< This macro pulls the SLP_TR pin low. */
312 //#define hal_get_slptr( ) ( ( PIN_SLP_TR & ( 1 << SLP_TR ) ) >> SLP_TR ) /**< Read current state of the SLP_TR pin (High/Low). */
313 #define hal_get_slptr( ) ( PIN_SLP_TR & ( 1 << SLP_TR ) ) /**< Read current state of the SLP_TR pin (High/Low). */
314 // Not for XMega
315 // #define RST RSTPIN /**< Pin number that corresponds to the RST pin. */
316 #define DDR_RST DDR( RSTPORT ) /**< Data Direction Register that corresponds to the port where RST is */
317 #define PORT_RST PORT( RSTPORT ) /**< Port (Write Access) where RST is connected. */
318 #define PIN_RST PIN( RSTPORT /* BUG? */) /**< Pin (Read Access) where RST is connected. */
319 #define hal_set_rst_high( ) ( PORT_RST |= ( 1 << RSTPIN ) ) /**< This macro pulls the RST pin high. */
320 #define hal_set_rst_low( ) ( PORT_RST &= ~( 1 << RSTPIN ) ) /**< This macro pulls the RST pin low. */
321 #define hal_get_rst( ) ( ( PIN_RST & ( 1 << RSTPIN ) ) >> RST ) /**< Read current state of the RST pin (High/Low). */
322 #define HAL_SS_PIN SSPIN /**< The slave select pin. */
323 #define HAL_SCK_PIN SCKPIN /**< Data bit for SCK. */
324 #define HAL_MOSI_PIN MOSIPIN
325 #define HAL_MISO_PIN MISOPIN
326 #define HAL_PORT_SPI PORT( SPIPORT ) /**< The SPI module is located on PORTB. */
327 #define HAL_PORT_SS PORT( SSPORT )
328 #define HAL_PORT_SCK PORT( SCKPORT )
329 #define HAL_PORT_MOSI PORT( MOSIPORT ) /**< The SPI module uses GPIO might be split on different ports. */
330 #define HAL_PORT_MISO PORT( MISOPORT ) /**< The SPI module uses GPIO might be split on different ports. */
331 #define HAL_DDR_SPI DDR( SPIPORT ) /**< Data Direction Register for PORTB. */
332 #define HAL_DDR_SS DDR( SSPORT ) /**< Data Direction Register for MISO GPIO pin. */
333 #define HAL_DDR_SCK DDR( SCKPORT ) /**< Data Direction Register for MISO GPIO pin. */
334 #define HAL_DDR_MOSI DDR( MOSIPORT ) /**< Data Direction Register for MISO GPIO pin. */
335 #define HAL_DDR_MISO DDR( MISOPORT ) /**< Data Direction Register for MOSI GPIO pin. */
336 #define HAL_DD_SS SSPIN /**< Data Direction bit for SS. */
337 #define HAL_DD_SCK SCKPIN /**< Data Direction bit for SCK. */
338 #define HAL_DD_MOSI MOSIPIN /**< Data Direction bit for MOSI. */
339 #define HAL_DD_MISO MISOPIN /**< Data Direction bit for MISO. */
340 
341 #else
342 
343 #define SLP_TR SLPTRPIN /**< Pin number that corresponds to the SLP_TR pin. */
344 #define DDR_SLP_TR DDR( SLPTRPORT ) /**< Data Direction Register that corresponds to the port where SLP_TR is connected. */
345 #define PORT_SLP_TR PORT( SLPTRPORT ) /**< Port (Write Access) where SLP_TR is connected. */
346 #define PIN_SLP_TR PIN( SLPTRPORT ) /**< Pin (Read Access) where SLP_TR is connected. */
347 #define hal_set_slptr_high( ) ( PORT_SLP_TR |= ( 1 << SLP_TR ) ) /**< This macro pulls the SLP_TR pin high. */
348 #define hal_set_slptr_low( ) ( PORT_SLP_TR &= ~( 1 << SLP_TR ) ) /**< This macro pulls the SLP_TR pin low. */
349 #define hal_get_slptr( ) ( PIN_SLP_TR & ( 1 << SLP_TR ) ) /**< Read current state of the SLP_TR pin (High/Low). */
350 #define RST RSTPIN /**< Pin number that corresponds to the RST pin. */
351 #define DDR_RST DDR( RSTPORT ) /**< Data Direction Register that corresponds to the port where RST is */
352 #define PORT_RST PORT( RSTPORT ) /**< Port (Write Access) where RST is connected. */
353 #define PIN_RST PIN( RSTPORT /* BUG? */) /**< Pin (Read Access) where RST is connected. */
354 #define hal_set_rst_high( ) ( PORT_RST |= ( 1 << RST ) ) /**< This macro pulls the RST pin high. */
355 #define hal_set_rst_low( ) ( PORT_RST &= ~( 1 << RST ) ) /**< This macro pulls the RST pin low. */
356 #define hal_get_rst( ) ( ( PIN_RST & ( 1 << RST ) ) >> RST ) /**< Read current state of the RST pin (High/Low). */
357 #define HAL_SS_PIN SSPIN /**< The slave select pin. */
358 #define HAL_SCK_PIN SCKPIN /**< Data bit for SCK. */
359 #define HAL_MOSI_PIN MOSIPIN
360 #define HAL_MISO_PIN MISOPIN
361 #define HAL_PORT_SPI PORT( SPIPORT ) /**< The SPI module is located on PORTB. */
362 #define HAL_PORT_SS PORT( SSPORT )
363 #define HAL_PORT_SCK PORT( SCKPORT )
364 #define HAL_PORT_MOSI PORT( MOSIPORT ) /**< The SPI module uses GPIO might be split on different ports. */
365 #define HAL_PORT_MISO PORT( MISOPORT ) /**< The SPI module uses GPIO might be split on different ports. */
366 #define HAL_DDR_SPI DDR( SPIPORT ) /**< Data Direction Register for PORTB. */
367 #define HAL_DDR_SS DDR( SSPORT ) /**< Data Direction Register for MISO GPIO pin. */
368 #define HAL_DDR_SCK DDR( SCKPORT ) /**< Data Direction Register for MISO GPIO pin. */
369 #define HAL_DDR_MOSI DDR( MOSIPORT ) /**< Data Direction Register for MISO GPIO pin. */
370 #define HAL_DDR_MISO DDR( MISOPORT ) /**< Data Direction Register for MOSI GPIO pin. */
371 #define HAL_DD_SS SSPIN /**< Data Direction bit for SS. */
372 #define HAL_DD_SCK SCKPIN /**< Data Direction bit for SCK. */
373 #define HAL_DD_MOSI MOSIPIN /**< Data Direction bit for MOSI. */
374 #define HAL_DD_MISO MISOPIN /**< Data Direction bit for MISO. */
375 #endif /* defined(__AVR_ATmega128RFA1__) */
376 
377 /** \} */
378 
379 
380 #define HAL_SS_HIGH( ) (HAL_PORT_SS |= ( 1 << HAL_SS_PIN )) /**< MACRO for pulling SS high. */
381 #define HAL_SS_LOW( ) (HAL_PORT_SS &= ~( 1 << HAL_SS_PIN )) /**< MACRO for pulling SS low. */
382 
383 #if defined(__AVR_XMEGA__)
384 
385  #define HAL_ENABLE_OVERFLOW_INTERRUPT( ) ( /*TIMSK1 |= ( 1 << TOIE1 )*/ )
386  #define HAL_DISABLE_OVERFLOW_INTERRUPT( ) ( /* TIMSK1 &= ~( 1 << TOIE1 )*/ )
387 
388  #define HAL_ENTER_CRITICAL_REGION( ) {uint8_t volatile saved_sreg = SREG; cli( )
389  #define HAL_LEAVE_CRITICAL_REGION( ) SREG = saved_sreg; } /* sei() needed here? */
390 
391  // This is the IRQ Vector we receive on the Port pin IRQPIN
392  #define RADIO_VECT PORTC_INT0_vect
393  #define HAL_ENABLE_RADIO_INTERRUPT( ) { PORTC.INTCTRL = (PORTC.INTCTRL & 0xC) | 0x2; /* Enable med level interrupt0 for portc */ PORTC.INT0MASK = (1 << IRQPIN); /* Enable Interrupt on Pin 2 */ PORTC.DIR = (0xFF & (~(1 << IRQPIN))); /* Set IRQ Pin as Input (force 0) */ PORTC.PIN2CTRL = (PORTC.PIN2CTRL & (~0x7)) | 0x1; /* First Force 000 and then set last bit to 1 -> trigger for rising edge */ PORTC.INTFLAGS = 1; /* Clear interrupt */ } /* ( EIMSK |= ( 1 << INT5 ) ) ; EICRB |= 0x0C ; PORTE &= ~(1<<PE5); DDRE &= ~(1<<DDE5); */
394  #define HAL_DISABLE_RADIO_INTERRUPT( ) ( PORTC.INTCTRL = (PORTC.INTCTRL & (~0x2)); ) /* EIMSK &= ~( 1 << INT5 ) */
395 
396 #elif defined(__AVR__)
397 
398 #if PLATFORM_TYPE == ZIGBIT
399  // IRQ E5 for Zigbit example
400  #define RADIO_VECT INT5_vect
401  #define HAL_ENABLE_RADIO_INTERRUPT( ) { ( EIMSK |= ( 1 << INT5 ) ) ; EICRB |= 0x0C ; PORTE &= ~(1<<PE5); DDRE &= ~(1<<DDE5); }
402  #define HAL_DISABLE_RADIO_INTERRUPT( ) ( EIMSK &= ~( 1 << INT5 ) )
403  #else
404  #define RADIO_VECT TIMER1_CAPT_vect
405  // Raven and Jackdaw
406 #define HAL_ENABLE_RADIO_INTERRUPT( ) { TCCR1B = ( 1 << ICES1 ) | ( 1 << CS10 ); TIFR1 |= (1 << ICF1); TIMSK1 |= ( 1 << ICIE1 ) ; }
407  #define HAL_DISABLE_RADIO_INTERRUPT( ) ( TIMSK1 &= ~( 1 << ICIE1 ) )
408  #endif
409 
410  #define HAL_ENABLE_OVERFLOW_INTERRUPT( ) ( TIMSK1 |= ( 1 << TOIE1 ) )
411  #define HAL_DISABLE_OVERFLOW_INTERRUPT( ) ( TIMSK1 &= ~( 1 << TOIE1 ) )
412 
413  /** This macro will protect the following code from interrupts.*/
414  #define HAL_ENTER_CRITICAL_REGION( ) {uint8_t volatile saved_sreg = SREG; cli( )
415 
416  /** This macro must always be used in conjunction with HAL_ENTER_CRITICAL_REGION
417  so that interrupts are enabled again.*/
418  #define HAL_LEAVE_CRITICAL_REGION( ) SREG = saved_sreg;}
419 
420 #else /* MULLE */
421 
422 #define HAL_ENABLE_RADIO_INTERRUPT( ) ( INT1IC.BYTE |= 1 )
423 #define HAL_DISABLE_RADIO_INTERRUPT( ) ( INT1IC.BYTE &= ~(1) )
424 
425 #define HAL_ENABLE_OVERFLOW_INTERRUPT( ) ( TB4IC.BYTE = 1 )
426 #define HAL_DISABLE_OVERFLOW_INTERRUPT( ) ( TB4IC.BYTE = 0 )
427 
428 /** This macro will protect the following code from interrupts.*/
429 #define HAL_ENTER_CRITICAL_REGION( ) MULLE_ENTER_CRITICAL_REGION( )
430 
431 /** This macro must always be used in conjunction with HAL_ENTER_CRITICAL_REGION
432  so that interrupts are enabled again.*/
433 #define HAL_LEAVE_CRITICAL_REGION( ) MULLE_LEAVE_CRITICAL_REGION( )
434 
435 #endif /* !__AVR__ */
436 
437 
438 /** \brief Enable the interrupt from the radio transceiver.
439  */
440 #define hal_enable_trx_interrupt( ) HAL_ENABLE_RADIO_INTERRUPT( )
441 
442 /** \brief Disable the interrupt from the radio transceiver.
443  *
444  * \retval 0 if the pin is low, 1 if the pin is high.
445  */
446 #define hal_disable_trx_interrupt( ) HAL_DISABLE_RADIO_INTERRUPT( )
447 /*============================ TYPDEFS =======================================*/
448 /*============================ PROTOTYPES ====================================*/
449 /*============================ MACROS ========================================*/
450 /** \name Macros for radio operation.
451  * \{
452  */
453 #define HAL_BAT_LOW_MASK ( 0x80 ) /**< Mask for the BAT_LOW interrupt. */
454 #define HAL_TRX_UR_MASK ( 0x40 ) /**< Mask for the TRX_UR interrupt. */
455 #define HAL_TRX_END_MASK ( 0x08 ) /**< Mask for the TRX_END interrupt. */
456 #define HAL_RX_START_MASK ( 0x04 ) /**< Mask for the RX_START interrupt. */
457 #define HAL_PLL_UNLOCK_MASK ( 0x02 ) /**< Mask for the PLL_UNLOCK interrupt. */
458 #define HAL_PLL_LOCK_MASK ( 0x01 ) /**< Mask for the PLL_LOCK interrupt. */
459 
460 #define HAL_MIN_FRAME_LENGTH ( 0x03 ) /**< A frame should be at least 3 bytes. */
461 #define HAL_MAX_FRAME_LENGTH ( 0x7F ) /**< A frame should no more than 127 bytes. */
462 /** \} */
463 /*============================ TYPDEFS =======================================*/
464 /** \struct hal_rx_frame_t
465  * \brief This struct defines the rx data container.
466  *
467  * \see hal_frame_read
468  */
469 typedef struct{
470  uint8_t length; /**< Length of frame. */
471  uint8_t data[ HAL_MAX_FRAME_LENGTH ]; /**< Actual frame data. */
472  uint8_t lqi; /**< LQI value for received frame. */
473  bool crc; /**< Flag - did CRC pass for received frame? */
475 
476 
477 /*============================ PROTOTYPES ====================================*/
478 void hal_init( void );
479 
480 /* Hack for atmega128rfa1 with integrated radio. Access registers directly, not through SPI */
481 #if defined(__AVR_ATmega128RFA1__)
482 //#define hal_register_read(address) _SFR_MEM8((uint16_t)address)
483 #define hal_register_read(address) address
484 uint8_t hal_subregister_read( uint16_t address, uint8_t mask, uint8_t position );
485 void hal_subregister_write( uint16_t address, uint8_t mask, uint8_t position,
486  uint8_t value );
487 
488 //#define hal_register_write(address, value) _SFR_MEM8((uint16_t)address)=value
489 #define hal_register_write(address, value) address=value
490 //#define hal_subregister_read( address, mask, position ) (_SFR_MEM8((uint16_t)address)&mask)>>position
491 //#define hal_subregister_read1( address, mask, position ) (address&mask)>>position
492 //#define hal_subregister_write( address, mask, position, value ) address=(address<<position)&mask
493 #else
494 uint8_t hal_register_read( uint8_t address );
495 void hal_register_write( uint8_t address, uint8_t value );
496 uint8_t hal_subregister_read( uint8_t address, uint8_t mask, uint8_t position );
497 void hal_subregister_write( uint8_t address, uint8_t mask, uint8_t position,
498  uint8_t value );
499 #endif
500 
501 
502 
503 void hal_frame_read(hal_rx_frame_t *rx_frame);
504 void hal_frame_write( uint8_t *write_buffer, uint8_t length );
505 void hal_sram_read( uint8_t address, uint8_t length, uint8_t *data );
506 void hal_sram_write( uint8_t address, uint8_t length, uint8_t *data );
507 /* Number of receive buffers in RAM. */
508 #ifndef RF230_CONF_RX_BUFFERS
509 #define RF230_CONF_RX_BUFFERS 1
510 #endif
511 
512 #endif
513 /** @} */
514 /*EOF*/