1 /* Copyright (c) 2009, Swedish Institute of Computer Science
4 * Additional fixes for AVR contributed by:
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
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are met:
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in
23 * the documentation and/or other materials provided with the
25 * * Neither the name of the copyright holders nor the names of
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
45 * \addtogroup wireless
50 * \defgroup hal RF230 hardware level drivers
56 * This file contains low-level radio driver code.
57 * This version is optimized for use with the "barebones" RF230bb driver,
58 * which communicates directly with the contiki core MAC layer.
63 /*============================ INCLUDE =======================================*/
67 #include "at86rf230_registermap.h"
68 /*============================ MACROS ========================================*/
71 * Macros defined for the radio transceiver's access modes.
73 * These functions are implemented as macros since they are used very often.
75 #define HAL_DUMMY_READ (0x00) /**< Dummy value for the SPI. */
77 #define HAL_TRX_CMD_RW (0xC0) /**< Register Write (short mode). */
78 #define HAL_TRX_CMD_RR (0x80) /**< Register Read (short mode). */
79 #define HAL_TRX_CMD_FW (0x60) /**< Frame Transmit Mode (long mode). */
80 #define HAL_TRX_CMD_FR (0x20) /**< Frame Receive Mode (long mode). */
81 #define HAL_TRX_CMD_SW (0x40) /**< SRAM Write. */
82 #define HAL_TRX_CMD_SR (0x00) /**< SRAM Read. */
83 #define HAL_TRX_CMD_RADDRM (0x7F) /**< Register Address Mask. */
85 #define HAL_CALCULATED_CRC_OK (0) /**< CRC calculated over the frame including the CRC field should be 0. */
86 /*============================ TYPDEFS =======================================*/
87 /*============================ VARIABLES =====================================*/
88 /** \brief This is a file internal variable that contains the 16 MSB of the
91 * The system time (32-bit) is the current time in microseconds. For the
92 * AVR microcontroller implementation this is solved by using a 16-bit
93 * timer (Timer1) with a clock frequency of 1MHz. The hal_system_time is
94 * incremented when the 16-bit timer overflows, representing the 16 MSB.
95 * The timer value it self (TCNT1) is then the 16 LSB.
97 * \see hal_get_system_time
99 static uint16_t hal_system_time
= 0;
102 //static uint8_t volatile hal_bat_low_flag; /**< BAT_LOW flag. */
103 //static uint8_t volatile hal_pll_lock_flag; /**< PLL_LOCK flag. */
107 /** \brief This function is called when a rx_start interrupt is signaled.
109 * If this function pointer is set to something else than NULL, it will
110 * be called when a RX_START event is signaled. The function takes two
111 * parameters: timestamp in IEEE 802.15.4 symbols (16 us resolution) and
112 * frame length. The event handler will be called in the interrupt domain,
113 * so the function must be kept short and not be blocking! Otherwise the
114 * system performance will be greatly degraded.
116 * \see hal_set_rx_start_event_handler
118 //static hal_rx_start_isr_event_handler_t rx_start_callback;
120 /** \brief This function is called when a trx_end interrupt is signaled.
122 * If this function pointer is set to something else than NULL, it will
123 * be called when a TRX_END event is signaled. The function takes one
124 * parameter: timestamp in IEEE 802.15.4 symbols (16 us resolution).
125 * The event handler will be called in the interrupt domain,
126 * so the function must not block!
128 * \see hal_set_trx_end_event_handler
130 //static hal_trx_end_isr_event_handler_t trx_end_callback;
131 /*============================ PROTOTYPES ====================================*/
132 /*============================ IMPLEMENTATION ================================*/
134 /** \brief This function initializes the Hardware Abstraction Layer.
139 /*Reset variables used in file.*/
141 // hal_reset_flags();
143 /*IO Specific Initialization.*/
144 DDR_SLP_TR
|= (1 << SLP_TR
); /* Enable SLP_TR as output. */
145 DDR_RST
|= (1 << RST
); /* Enable RST as output. */
147 /*SPI Specific Initialization.*/
148 /* Set SS, CLK and MOSI as output. */
149 HAL_DDR_SPI
|= (1 << HAL_DD_SS
) | (1 << HAL_DD_SCK
) | (1 << HAL_DD_MOSI
);
150 HAL_PORT_SPI
|= (1 << HAL_DD_SS
) | (1 << HAL_DD_SCK
); /* Set SS and CLK high */
151 /* Run SPI at max speed */
152 SPCR
= (1 << SPE
) | (1 << MSTR
); /* Enable SPI module and master operation. */
153 SPSR
= (1 << SPI2X
); /* Enable doubled SPI speed in master mode. */
155 /*TIMER1 Specific Initialization.*/
156 TCCR1B
= HAL_TCCR1B_CONFIG
; /* Set clock prescaler */
157 TIFR1
|= (1 << ICF1
); /* Clear Input Capture Flag. */
158 HAL_ENABLE_OVERFLOW_INTERRUPT(); /* Enable Timer1 overflow interrupt. */
159 hal_enable_trx_interrupt(); /* Enable interrupts from the radio transceiver. */
162 /*----------------------------------------------------------------------------*/
163 /** \brief This function reset the interrupt flags and interrupt event handlers
164 * (Callbacks) to their default value.
167 //hal_reset_flags(void)
169 // AVR_ENTER_CRITICAL_REGION();
172 // hal_bat_low_flag = 0;
173 // hal_pll_lock_flag = 0;
175 /* Reset Associated Event Handlers. */
176 // rx_start_callback = NULL;
177 // trx_end_callback = NULL;
179 // AVR_LEAVE_CRITICAL_REGION();
182 /*----------------------------------------------------------------------------*/
183 /** \brief This function returns the current value of the BAT_LOW flag.
185 * The BAT_LOW flag is incremented each time a BAT_LOW event is signaled from the
186 * radio transceiver. This way it is possible for the end user to poll the flag
187 * for new event occurances.
190 //hal_get_bat_low_flag(void)
192 // return hal_bat_low_flag;
195 /*----------------------------------------------------------------------------*/
196 /** \brief This function clears the BAT_LOW flag.
199 //hal_clear_bat_low_flag(void)
201 // AVR_ENTER_CRITICAL_REGION();
202 // hal_bat_low_flag = 0;
203 // AVR_LEAVE_CRITICAL_REGION();
206 /*----------------------------------------------------------------------------*/
207 /** \brief This function is used to set new TRX_END event handler, overriding
208 * old handler reference.
210 //hal_trx_end_isr_event_handler_t
211 //hal_get_trx_end_event_handler(void)
213 // return trx_end_callback;
216 /*----------------------------------------------------------------------------*/
217 /** \brief This function is used to set new TRX_END event handler, overriding
218 * old handler reference.
221 //hal_set_trx_end_event_handler(hal_trx_end_isr_event_handler_t trx_end_callback_handle)
223 // AVR_ENTER_CRITICAL_REGION();
224 // trx_end_callback = trx_end_callback_handle;
225 // AVR_LEAVE_CRITICAL_REGION();
228 /*----------------------------------------------------------------------------*/
229 /** \brief Remove event handler reference.
232 //hal_clear_trx_end_event_handler(void)
234 // AVR_ENTER_CRITICAL_REGION();
235 // trx_end_callback = NULL;
236 // AVR_LEAVE_CRITICAL_REGION();
239 /*----------------------------------------------------------------------------*/
240 /** \brief This function returns the active RX_START event handler
242 * \return Current RX_START event handler registered.
244 //hal_rx_start_isr_event_handler_t
245 //hal_get_rx_start_event_handler(void)
247 // return rx_start_callback;
250 /*----------------------------------------------------------------------------*/
251 /** \brief This function is used to set new RX_START event handler, overriding
252 * old handler reference.
255 //hal_set_rx_start_event_handler(hal_rx_start_isr_event_handler_t rx_start_callback_handle)
257 // AVR_ENTER_CRITICAL_REGION();
258 // rx_start_callback = rx_start_callback_handle;
259 // AVR_LEAVE_CRITICAL_REGION();
262 /*----------------------------------------------------------------------------*/
263 /** \brief Remove event handler reference.
266 //hal_clear_rx_start_event_handler(void)
268 // AVR_ENTER_CRITICAL_REGION();
269 // rx_start_callback = NULL;
270 // AVR_LEAVE_CRITICAL_REGION();
273 /*----------------------------------------------------------------------------*/
274 /** \brief This function returns the current value of the PLL_LOCK flag.
276 * The PLL_LOCK flag is incremented each time a PLL_LOCK event is signaled from the
277 * radio transceiver. This way it is possible for the end user to poll the flag
278 * for new event occurances.
281 //hal_get_pll_lock_flag(void)
283 // return hal_pll_lock_flag;
286 /*----------------------------------------------------------------------------*/
287 /** \brief This function clears the PLL_LOCK flag.
290 //hal_clear_pll_lock_flag(void)
292 // AVR_ENTER_CRITICAL_REGION();
293 // hal_pll_lock_flag = 0;
294 // AVR_LEAVE_CRITICAL_REGION();
297 /*----------------------------------------------------------------------------*/
298 /** \brief This function reads data from one of the radio transceiver's registers.
300 * \param address Register address to read from. See datasheet for register
303 * \see Look at the at86rf230_registermap.h file for register address definitions.
305 * \returns The actual value of the read register.
308 hal_register_read(uint8_t address
)
310 /* Add the register read command to the register address. */
311 address
&= HAL_TRX_CMD_RADDRM
;
312 address
|= HAL_TRX_CMD_RR
;
314 uint8_t register_value
= 0;
316 AVR_ENTER_CRITICAL_REGION();
318 HAL_SS_LOW(); /* Start the SPI transaction by pulling the Slave Select low. */
320 /*Send Register address and read register content.*/
322 while ((SPSR
& (1 << SPIF
)) == 0) {;}
323 register_value
= SPDR
;
325 SPDR
= register_value
;
326 while ((SPSR
& (1 << SPIF
)) == 0) {;}
327 register_value
= SPDR
;
329 HAL_SS_HIGH(); /* End the transaction by pulling the Slave Select High. */
331 AVR_LEAVE_CRITICAL_REGION();
333 return register_value
;
336 /*----------------------------------------------------------------------------*/
337 /** \brief This function writes a new value to one of the radio transceiver's
340 * \see Look at the at86rf230_registermap.h file for register address definitions.
342 * \param address Address of register to write.
343 * \param value Value to write.
346 hal_register_write(uint8_t address
, uint8_t value
)
348 /* Add the Register Write command to the address. */
349 address
= HAL_TRX_CMD_RW
| (HAL_TRX_CMD_RADDRM
& address
);
351 AVR_ENTER_CRITICAL_REGION();
353 HAL_SS_LOW(); /* Start the SPI transaction by pulling the Slave Select low. */
355 /*Send Register address and write register content.*/
357 while ((SPSR
& (1 << SPIF
)) == 0) {;}
358 uint8_t dummy_read
= SPDR
;
361 while ((SPSR
& (1 << SPIF
)) == 0) {;}
364 HAL_SS_HIGH(); /* End the transaction by pulling the Slave Slect High. */
366 AVR_LEAVE_CRITICAL_REGION();
369 /*----------------------------------------------------------------------------*/
370 /** \brief This function reads the value of a specific subregister.
372 * \see Look at the at86rf230_registermap.h file for register and subregister
375 * \param address Main register's address.
376 * \param mask Bit mask of the subregister.
377 * \param position Bit position of the subregister
378 * \retval Value of the read subregister.
381 hal_subregister_read(uint8_t address
, uint8_t mask
, uint8_t position
)
383 /* Read current register value and mask out subregister. */
384 uint8_t register_value
= hal_register_read(address
);
385 register_value
&= mask
;
386 register_value
>>= position
; /* Align subregister value. */
388 return register_value
;
391 /*----------------------------------------------------------------------------*/
392 /** \brief This function writes a new value to one of the radio transceiver's
395 * \see Look at the at86rf230_registermap.h file for register and subregister
398 * \param address Main register's address.
399 * \param mask Bit mask of the subregister.
400 * \param position Bit position of the subregister
401 * \param value Value to write into the subregister.
404 hal_subregister_write(uint8_t address
, uint8_t mask
, uint8_t position
,
407 /* Read current register value and mask area outside the subregister. */
408 uint8_t register_value
= hal_register_read(address
);
409 register_value
&= ~mask
;
411 /* Start preparing the new subregister value. shift in place and mask. */
415 value
|= register_value
; /* Set the new subregister value. */
417 /* Write the modified register value. */
418 hal_register_write(address
, value
);
421 /*----------------------------------------------------------------------------*/
422 /** \brief This function will upload a frame from the radio transceiver's frame
425 * If the frame currently available in the radio transceiver's frame buffer
426 * is out of the defined bounds. Then the frame length, lqi value and crc
427 * be set to zero. This is done to indicate an error.
428 * This version is optimized for use with contiki RF230BB driver
430 * \param rx_frame Pointer to the data structure where the frame is stored.
431 * \param rx_callback Pointer to callback function for receiving one byte at a time.
434 hal_frame_read(hal_rx_frame_t
*rx_frame
, rx_callback_t rx_callback
)
438 /* check that we have either valid frame pointer or callback pointer */
439 // if (!rx_frame && !rx_callback)
442 AVR_ENTER_CRITICAL_REGION();
446 /*Send frame read command.*/
447 SPDR
= HAL_TRX_CMD_FR
;
448 while ((SPSR
& (1 << SPIF
)) == 0) {;}
449 uint8_t frame_length
= SPDR
;
451 /*Read frame length.*/
453 while ((SPSR
& (1 << SPIF
)) == 0) {;}
456 /*Check for correct frame length.*/
457 if ((frame_length
>= HAL_MIN_FRAME_LENGTH
) && (frame_length
<= HAL_MAX_FRAME_LENGTH
)){
460 rx_data
= (rx_frame
->data
);
461 rx_frame
->length
= frame_length
;
463 // rx_callback(frame_length);
465 /*Upload frame buffer to data pointer. Calculate CRC.*/
467 while ((SPSR
& (1 << SPIF
)) == 0) {;}
470 uint8_t tempData
= SPDR
;
471 SPDR
= 0; /* dummy write */
474 *rx_data
++ = tempData
;
476 // rx_callback(tempData);
479 crc
= _crc_ccitt_update(crc
, tempData
);
481 while ((SPSR
& (1 << SPIF
)) == 0) {;}
483 } while (--frame_length
> 0);
485 /*Read LQI value for this frame.*/
487 rx_frame
->lqi
= SPDR
;
489 // rx_callback(SPDR);
494 /*Check calculated crc, and set crc field in hal_rx_frame_t accordingly.*/
496 rx_frame
->crc
= (crc
== HAL_CALCULATED_CRC_OK
);
498 // rx_callback(crc != HAL_CALCULATED_CRC_OK);
504 rx_frame
->length
= 0;
506 rx_frame
->crc
= false;
510 AVR_LEAVE_CRITICAL_REGION();
513 /*----------------------------------------------------------------------------*/
514 /** \brief This function will download a frame to the radio transceiver's frame
517 * \param write_buffer Pointer to data that is to be written to frame buffer.
518 * \param length Length of data. The maximum length is 127 bytes.
521 hal_frame_write(uint8_t *write_buffer
, uint8_t length
)
523 length
&= HAL_TRX_CMD_RADDRM
; /* Truncate length to maximum frame length. */
525 AVR_ENTER_CRITICAL_REGION();
527 HAL_SS_LOW(); /* Initiate the SPI transaction. */
529 /*SEND FRAME WRITE COMMAND AND FRAME LENGTH.*/
530 SPDR
= HAL_TRX_CMD_FW
;
531 while ((SPSR
& (1 << SPIF
)) == 0) {;}
532 uint8_t dummy_read
= SPDR
;
535 while ((SPSR
& (1 << SPIF
)) == 0) {;}
538 /* Download to the Frame Buffer. */
540 SPDR
= *write_buffer
++;
543 while ((SPSR
& (1 << SPIF
)) == 0) {;}
546 } while (length
> 0);
548 HAL_SS_HIGH(); /* Terminate SPI transaction. */
550 AVR_LEAVE_CRITICAL_REGION();
553 /*----------------------------------------------------------------------------*/
556 * This function reads from the SRAM of the radio transceiver.
558 * \param address Address in the TRX's SRAM where the read burst should start
559 * \param length Length of the read burst
560 * \param data Pointer to buffer where data is stored.
563 //hal_sram_read(uint8_t address, uint8_t length, uint8_t *data)
565 // AVR_ENTER_CRITICAL_REGION();
567 // HAL_SS_LOW(); /* Initiate the SPI transaction. */
569 /*Send SRAM read command.*/
570 // SPDR = HAL_TRX_CMD_SR;
571 // while ((SPSR & (1 << SPIF)) == 0) {;}
572 // uint8_t dummy_read = SPDR;
574 /*Send address where to start reading.*/
576 // while ((SPSR & (1 << SPIF)) == 0) {;}
578 // dummy_read = SPDR;
580 /*Upload the chosen memory area.*/
582 // SPDR = HAL_DUMMY_READ;
583 // while ((SPSR & (1 << SPIF)) == 0) {;}
585 // } while (--length > 0);
589 // AVR_LEAVE_CRITICAL_REGION();
592 /*----------------------------------------------------------------------------*/
593 /** \brief Write SRAM
595 * This function writes into the SRAM of the radio transceiver.
597 * \param address Address in the TRX's SRAM where the write burst should start
598 * \param length Length of the write burst
599 * \param data Pointer to an array of bytes that should be written
602 //hal_sram_write(uint8_t address, uint8_t length, uint8_t *data)
604 // AVR_ENTER_CRITICAL_REGION();
608 /*Send SRAM write command.*/
609 // SPDR = HAL_TRX_CMD_SW;
610 // while ((SPSR & (1 << SPIF)) == 0) {;}
611 // uint8_t dummy_read = SPDR;
613 /*Send address where to start writing to.*/
615 // while ((SPSR & (1 << SPIF)) == 0) {;}
616 // dummy_read = SPDR;
618 /*Upload the chosen memory area.*/
621 // while ((SPSR & (1 << SPIF)) == 0) {;}
622 // dummy_read = SPDR;
623 // } while (--length > 0);
627 // AVR_LEAVE_CRITICAL_REGION();
630 /*----------------------------------------------------------------------------*/
631 /* This #if compile switch is used to provide a "standard" function body for the */
632 /* doxygen documentation. */
634 /** \brief ISR for the radio IRQ line, triggered by the input capture.
635 * This is the interrupt service routine for timer1.ICIE1 input capture.
636 * It is triggered of a rising edge on the radio transceivers IRQ line.
638 void RADIO_VECT(void);
640 /* These link to the RF230BB driver in rf230.c */
641 void rf230_interrupt(void);
642 extern hal_rx_frame_t rxframe
;
646 volatile int rf230_interrupt_flag
=0;
647 #define INTERRUPTDEBUG(arg) rf230_interrupt_flag=arg
649 #define INTERRUPTDEBUG(arg)
654 /*The following code reads the current system time. This is done by first
655 reading the hal_system_time and then adding the 16 LSB directly from the
658 uint32_t isr_timestamp
= hal_system_time
;
659 isr_timestamp
<<= 16;
660 isr_timestamp
|= TCNT1
;
661 volatile uint8_t state
;
665 /*Read Interrupt source.*/
668 /*Send Register address and read register content.*/
669 SPDR
= RG_IRQ_STATUS
| HAL_TRX_CMD_RR
;
671 /* This is the second part of the convertion of system time to a 16 us time
672 base. The division is moved here so we can spend less time waiting for SPI
675 isr_timestamp
/= HAL_US_PER_SYMBOL
; /* Divide so that we get time in 16us resolution. */
676 isr_timestamp
&= HAL_SYMBOL_MASK
;
678 while ((SPSR
& (1 << SPIF
)) == 0) {;}
679 uint8_t interrupt_source
= SPDR
; /* The interrupt variable is used as a dummy read. */
681 SPDR
= interrupt_source
;
682 while ((SPSR
& (1 << SPIF
)) == 0) {;}
683 interrupt_source
= SPDR
; /* The interrupt source is read. */
687 /*Handle the incomming interrupt. Prioritized.*/
688 if ((interrupt_source
& HAL_RX_START_MASK
)){
690 // if(rx_start_callback != NULL){
691 // /* Read Frame length and call rx_start callback. */
694 // SPDR = HAL_TRX_CMD_FR;
695 // while ((SPSR & (1 << SPIF)) == 0) {;}
696 // uint8_t frame_length = SPDR;
698 // SPDR = frame_length; /* frame_length used for dummy data */
699 // while ((SPSR & (1 << SPIF)) == 0) {;}
700 // frame_length = SPDR;
704 // rx_start_callback(isr_timestamp, frame_length);
706 } else if (interrupt_source
& HAL_TRX_END_MASK
){
708 // if(trx_end_callback != NULL){
709 // INTERRUPTDEBUG(12);
710 // trx_end_callback(isr_timestamp);
713 state
= hal_subregister_read(SR_TRX_STATUS
);
714 if((state
== BUSY_RX_AACK
) || (state
== RX_ON
) || (state
== BUSY_RX
) || (state
== RX_AACK_ON
)){
715 /* Received packet interrupt */
716 /* Buffer the frame and call rf230_interrupt to schedule poll for rf230 receive process */
717 // if (rxframe.length) break; //toss packet if last one not processed yet
719 hal_frame_read(&rxframe
, NULL
);
721 // trx_end_callback(isr_timestamp);
722 /* Enable reception of next packet */
723 hal_subregister_write(SR_TRX_CMD
, RX_AACK_ON
);
726 } else if (interrupt_source
& HAL_TRX_UR_MASK
){
729 } else if (interrupt_source
& HAL_PLL_UNLOCK_MASK
){
732 } else if (interrupt_source
& HAL_PLL_LOCK_MASK
){
734 // hal_pll_lock_flag++;
736 } else if (interrupt_source
& HAL_BAT_LOW_MASK
){
737 /* Disable BAT_LOW interrupt to prevent endless interrupts. The interrupt */
738 /* will continously be asserted while the supply voltage is less than the */
739 /* user-defined voltage threshold. */
740 uint8_t trx_isr_mask
= hal_register_read(RG_IRQ_MASK
);
741 trx_isr_mask
&= ~HAL_BAT_LOW_MASK
;
742 hal_register_write(RG_IRQ_MASK
, trx_isr_mask
);
743 // hal_bat_low_flag++; /* Increment BAT_LOW flag. */
751 # endif /* defined(DOXYGEN) */
753 /*----------------------------------------------------------------------------*/
754 /* This #if compile switch is used to provide a "standard" function body for the */
755 /* doxygen documentation. */
757 /** \brief Timer Overflow ISR
758 * This is the interrupt service routine for timer1 overflow.
760 void TIMER1_OVF_vect(void);