Contiki 3.x
halbb.c
Go to the documentation of this file.
1 /* Copyright (c) 2009, 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 
45 /**
46  * \addtogroup wireless
47  * @{
48 */
49 
50 /**
51  * \defgroup hal RF230 hardware level drivers
52  * @{
53  */
54 
55 /**
56  * \file
57  * This file contains low-level radio driver code.
58  * This version is optimized for use with the "barebones" RF230bb driver,
59  * which communicates directly with the contiki core MAC layer.
60  * It is optimized for speed at the expense of generality.
61  */
62 #include "contiki-conf.h"
63 #if DEBUGFLOWSIZE
64 extern uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE];
65 #define DEBUGFLOW(c) if (debugflowsize<(DEBUGFLOWSIZE-1)) debugflow[debugflowsize++]=c
66 #else
67 #define DEBUGFLOW(c)
68 #endif
69 
70 
71 /*============================ INCLUDE =======================================*/
72 #include <stdlib.h>
73 
74 #include "hal.h"
75 
76 #if defined(__AVR_ATmega128RFA1__)
77 #include <avr/io.h>
78 #include "atmega128rfa1_registermap.h"
79 #else
80 #include "at86rf230_registermap.h"
81 #endif
82 
83 /*============================ VARIABLES =====================================*/
84 
85 volatile extern signed char rf230_last_rssi;
86 
87 /*============================ CALLBACKS =====================================*/
88 
89 
90 /*============================ IMPLEMENTATION ================================*/
91 #if defined(__AVR_ATmega128RFA1__)
92 
93 /* AVR1281 with internal RF231 radio */
94 #include <avr/interrupt.h>
95 
96 #elif defined(__AVR__)
97 /*
98  * AVR with hardware SPI tranfers (TODO: move to hw spi hal for avr cpu)
99  */
100 #include <avr/io.h>
101 #include <avr/interrupt.h>
102 
103 #define HAL_SPI_TRANSFER_OPEN() { \
104  HAL_ENTER_CRITICAL_REGION(); \
105  HAL_SS_LOW(); /* Start the SPI transaction by pulling the Slave Select low. */
106 #define HAL_SPI_TRANSFER_WRITE(to_write) (SPDR = (to_write))
107 #define HAL_SPI_TRANSFER_WAIT() ({while ((SPSR & (1 << SPIF)) == 0) {;}}) /* gcc extension, alternative inline function */
108 #define HAL_SPI_TRANSFER_READ() (SPDR)
109 #define HAL_SPI_TRANSFER_CLOSE() \
110  HAL_SS_HIGH(); /* End the transaction by pulling the Slave Select High. */ \
111  HAL_LEAVE_CRITICAL_REGION(); \
112  }
113 #define HAL_SPI_TRANSFER(to_write) ( \
114  HAL_SPI_TRANSFER_WRITE(to_write), \
115  HAL_SPI_TRANSFER_WAIT(), \
116  HAL_SPI_TRANSFER_READ() )
117 
118 #else /* __AVR__ */
119 /*
120  * Other SPI architecture (parts to core, parts to m16c6Xp
121  */
122 #include "contiki-mulle.h" // MULLE_ENTER_CRITICAL_REGION
123 
124 // Software SPI transfers
125 #define HAL_SPI_TRANSFER_OPEN() { uint8_t spiTemp; \
126  HAL_ENTER_CRITICAL_REGION(); \
127  HAL_SS_LOW(); /* Start the SPI transaction by pulling the Slave Select low. */
128 #define HAL_SPI_TRANSFER_WRITE(to_write) (spiTemp = spiWrite(to_write))
129 #define HAL_SPI_TRANSFER_WAIT() ({0;})
130 #define HAL_SPI_TRANSFER_READ() (spiTemp)
131 #define HAL_SPI_TRANSFER_CLOSE() \
132  HAL_SS_HIGH(); /* End the transaction by pulling the Slave Select High. */ \
133  HAL_LEAVE_CRITICAL_REGION(); \
134  }
135 #define HAL_SPI_TRANSFER(to_write) (spiTemp = spiWrite(to_write))
136 
137 inline uint8_t spiWrite(uint8_t byte)
138 {
139  uint8_t data = 0;
140  uint8_t mask = 0x80;
141  do
142  {
143  if( (byte & mask) != 0 )
144  HAL_PORT_MOSI |= (1 << HAL_MOSI_PIN); //call MOSI.set();
145  else
146  HAL_PORT_MOSI &= ~(1 << HAL_MOSI_PIN); //call MOSI.clr();
147 
148  if( (HAL_PORT_MISO & (1 << HAL_MISO_PIN)) > 0) //call MISO.get() )
149  data |= mask;
150 
151  HAL_PORT_SCK &= ~(1 << HAL_SCK_PIN); //call SCLK.clr();
152  HAL_PORT_SCK |= (1 << HAL_SCK_PIN); //call SCLK.set();
153  } while( (mask >>= 1) != 0 );
154  return data;
155 }
156 
157 #endif /* !__AVR__ */
158 
159 /** \brief This function initializes the Hardware Abstraction Layer.
160  */
161 #if defined(__AVR_ATmega128RFA1__)
162 
163 void
164 hal_init(void)
165 {
166  /*Reset variables used in file.*/
167  /* (none at the moment) */
168 }
169 
170 #elif defined(__AVR__)
171 
172 #define HAL_RF230_ISR() ISR(RADIO_VECT)
173 
174 void
175 hal_init(void)
176 {
177  /*Reset variables used in file.*/
178 
179  /*IO Specific Initialization - sleep and reset pins. */
180  /* Set pins low before they are initialized as output? Does not seem to matter */
181 // hal_set_rst_low();
182 // hal_set_slptr_low();
183  DDR_SLP_TR |= (1 << SLP_TR); /* Enable SLP_TR as output. */
184  DDR_RST |= (1 << RST); /* Enable RST as output. */
185 
186  /*SPI Specific Initialization.*/
187  /* Set SS, CLK and MOSI as output. */
188  /* To avoid a SPI glitch, the port register shall be set before the DDR register */
189  HAL_PORT_SPI |= (1 << HAL_DD_SS) | (1 << HAL_DD_SCK); /* Set SS and CLK high */
190  HAL_DDR_SPI |= (1 << HAL_DD_SS) | (1 << HAL_DD_SCK) | (1 << HAL_DD_MOSI);
191  HAL_DDR_SPI &=~ (1<< HAL_DD_MISO); /* MISO input */
192 
193  /* Run SPI at max speed */
194  SPCR = (1 << SPE) | (1 << MSTR); /* Enable SPI module and master operation. */
195  SPSR = (1 << SPI2X); /* Enable doubled SPI speed in master mode. */
196 
197  /* Enable interrupts from the radio transceiver. */
199 }
200 
201 #else /* __AVR__ */
202 
203 #define HAL_RF230_ISR() M16C_INTERRUPT(M16C_INT1)
204 #define HAL_TIME_ISR() M16C_INTERRUPT(M16C_TMRB4)
205 #define HAL_TICK_UPCNT() (0xFFFF-TB4) // TB4 counts down so we need to convert it to upcounting
206 
207 void
208 hal_init(void)
209 {
210  /*Reset variables used in file.*/
211 
212  /*IO Specific Initialization - sleep and reset pins. */
213  DDR_SLP_TR |= (1 << SLP_TR); /* Enable SLP_TR as output. */
214  DDR_RST |= (1 << RST); /* Enable RST as output. */
215 
216  /*SPI Specific Initialization.*/
217  /* Set SS, CLK and MOSI as output. */
218  HAL_DDR_SS |= (1 << HAL_SS_PIN);
219  HAL_DDR_SCK |= (1 << HAL_SCK_PIN);
220  HAL_DDR_MOSI |= (1 << HAL_MOSI_PIN);
221  HAL_DDR_MISO &= ~(1 << HAL_MISO_PIN);
222 
223  /* Set SS */
224  HAL_PORT_SS |= (1 << HAL_SS_PIN); // HAL_SS_HIGH()
225  HAL_PORT_SCK &= ~(1 << HAL_SCK_PIN); // SCLK.clr()
226 
227  /*TIMER Specific Initialization.*/
228  // Init count source (Timer B3)
229  TB3 = ((16*10) - 1); // 16 us ticks
230  TB3MR.BYTE = 0b00000000; // Timer mode, F1
231  TBSR.BIT.TB3S = 1; // Start Timer B3
232 
233  TB4 = 0xFFFF; //
234  TB4MR.BYTE = 0b10000001; // Counter mode, count TB3
235  TBSR.BIT.TB4S = 1; // Start Timer B4
236  INT1IC.BIT.POL = 1; // Select rising edge
237  HAL_ENABLE_OVERFLOW_INTERRUPT(); /* Enable Timer overflow interrupt. */
238 
239  /* Enable interrupts from the radio transceiver. */
241 }
242 #endif /* !__AVR__ */
243 
244 
245 #if defined(__AVR_ATmega128RFA1__)
246 /* Hack for internal radio registers. hal_register_read and hal_register_write are
247  handled through defines, but the preprocesser can't parse a macro containing
248  another #define with multiple arguments, e.g. using
249  #define hal_subregister_read( address, mask, position ) (address&mask)>>position
250  #define SR_TRX_STATUS TRX_STATUS, 0x1f, 0
251  the following only sees 1 argument to the macro
252  return hal_subregister_read(SR_TRX_STATUS);
253 
254  Possible fix is through two defines:
255  #define x_hal_subregister_read(x) hal_subregister_read(x);
256  #define hal_subregister_read( address, mask, position ) (address&mask)>>position
257  but the subregister defines in atmega128rfa1_registermap.h are currently set up without
258  the _SFR_MEM8 attribute, for use by hal_subregister_write.
259 
260  */
261 uint8_t
262 hal_subregister_read(uint16_t address, uint8_t mask, uint8_t position)
263 {
264  return (_SFR_MEM8(address)&mask)>>position;
265 }
266 void
267 hal_subregister_write(uint16_t address, uint8_t mask, uint8_t position,
268  uint8_t value)
269 {
271 
272  uint8_t register_value = _SFR_MEM8(address);
273  register_value &= ~mask;
274  value <<= position;
275  value &= mask;
276  value |= register_value;
277  _SFR_MEM8(address) = value;
278 
280 }
281 
282 #else /* defined(__AVR_ATmega128RFA1__) */
283 /*----------------------------------------------------------------------------*/
284 /** \brief This function reads data from one of the radio transceiver's registers.
285  *
286  * \param address Register address to read from. See datasheet for register
287  * map.
288  *
289  * \see Look at the at86rf230_registermap.h file for register address definitions.
290  *
291  * \returns The actual value of the read register.
292  */
293 uint8_t
294 hal_register_read(uint8_t address)
295 {
296  uint8_t register_value;
297  /* Add the register read command to the register address. */
298  /* Address should be < 0x2f so no need to mask */
299 // address &= 0x3f;
300  address |= 0x80;
301 
303 
304  /*Send Register address and read register content.*/
305  HAL_SPI_TRANSFER(address);
306  register_value = HAL_SPI_TRANSFER(0);
307 
308  HAL_SPI_TRANSFER_CLOSE();
309 
310  return register_value;
311 }
312 
313 /*----------------------------------------------------------------------------*/
314 /** \brief This function writes a new value to one of the radio transceiver's
315  * registers.
316  *
317  * \see Look at the at86rf230_registermap.h file for register address definitions.
318  *
319  * \param address Address of register to write.
320  * \param value Value to write.
321  */
322 void
323 hal_register_write(uint8_t address, uint8_t value)
324 {
325  /* Add the Register Write (short mode) command to the address. */
326  address = 0xc0 | address;
327 
329 
330  /*Send Register address and write register content.*/
331  HAL_SPI_TRANSFER(address);
332  HAL_SPI_TRANSFER(value);
333 
334  HAL_SPI_TRANSFER_CLOSE();
335 }
336 /*----------------------------------------------------------------------------*/
337 /** \brief This function reads the value of a specific subregister.
338  *
339  * \see Look at the at86rf230_registermap.h file for register and subregister
340  * definitions.
341  *
342  * \param address Main register's address.
343  * \param mask Bit mask of the subregister.
344  * \param position Bit position of the subregister
345  * \retval Value of the read subregister.
346  */
347 uint8_t
348 hal_subregister_read(uint8_t address, uint8_t mask, uint8_t position)
349 {
350  /* Read current register value and mask out subregister. */
351  uint8_t register_value = hal_register_read(address);
352  register_value &= mask;
353  register_value >>= position; /* Align subregister value. */
354 
355  return register_value;
356 }
357 /*----------------------------------------------------------------------------*/
358 /** \brief This function writes a new value to one of the radio transceiver's
359  * subregisters.
360  *
361  * \see Look at the at86rf230_registermap.h file for register and subregister
362  * definitions.
363  *
364  * \param address Main register's address.
365  * \param mask Bit mask of the subregister.
366  * \param position Bit position of the subregister
367  * \param value Value to write into the subregister.
368  */
369 void
370 hal_subregister_write(uint8_t address, uint8_t mask, uint8_t position,
371  uint8_t value)
372 {
373  /* Read current register value and mask area outside the subregister. */
374  volatile uint8_t register_value = hal_register_read(address);
375  register_value &= ~mask;
376 
377  /* Start preparing the new subregister value. shift in place and mask. */
378  value <<= position;
379  value &= mask;
380 
381  value |= register_value; /* Set the new subregister value. */
382 
383  /* Write the modified register value. */
384  hal_register_write(address, value);
385 }
386 #endif /* defined(__AVR_ATmega128RFA1__) */
387 /*----------------------------------------------------------------------------*/
388 /** \brief Transfer a frame from the radio transceiver to a RAM buffer
389  *
390  * This version is optimized for use with contiki RF230BB driver.
391  * The callback routine and CRC are left out for speed in reading the rx buffer.
392  * Any delays here can lead to overwrites by the next packet!
393  *
394  * If the frame length is out of the defined bounds, the length, lqi and crc
395  * are set to zero.
396  *
397  * \param rx_frame Pointer to the data structure where the frame is stored.
398  */
399 void
401 {
402 #if defined(__AVR_ATmega128RFA1__)
403 
404  uint8_t frame_length,*rx_data,*rx_buffer;
405 
406  /* Get length from the TXT_RX_LENGTH register, not including LQI
407  * Bypassing the length check can result in overrun if buffer is < 256 bytes.
408  */
409  frame_length = TST_RX_LENGTH;
410  if ((frame_length < HAL_MIN_FRAME_LENGTH) || (frame_length > HAL_MAX_FRAME_LENGTH)) {
411  /* Length test failed */
412  rx_frame->length = 0;
413  rx_frame->lqi = 0;
414  rx_frame->crc = false;
415  return;
416  }
417  rx_frame->length = frame_length;
418 
419  /* Start of buffer in I/O space, pointer to RAM buffer */
420  rx_buffer=(uint8_t *)0x180;
421  rx_data = (rx_frame->data);
422 
423  do{
424  *rx_data++ = _SFR_MEM8(rx_buffer++);
425  } while (--frame_length > 0);
426 
427  /*Read LQI value for this frame.*/
428  rx_frame->lqi = *rx_buffer;
429 
430  /* If crc was calculated set crc field in hal_rx_frame_t accordingly.
431  * Else show the crc has passed the hardware check.
432  */
433  rx_frame->crc = true;
434 
435 #else /* defined(__AVR_ATmega128RFA1__) */
436 
437  uint8_t frame_length, *rx_data;
438 
439  /*Send frame read (long mode) command.*/
441  HAL_SPI_TRANSFER(0x20);
442 
443  /*Read frame length. This includes the checksum. */
444  frame_length = HAL_SPI_TRANSFER(0);
445 
446  /*Check for correct frame length. Bypassing this test can result in a buffer overrun! */
447  if ((frame_length < HAL_MIN_FRAME_LENGTH) || (frame_length > HAL_MAX_FRAME_LENGTH)) {
448  /* Length test failed */
449  rx_frame->length = 0;
450  rx_frame->lqi = 0;
451  rx_frame->crc = false;
452  }
453  else {
454  rx_data = (rx_frame->data);
455  rx_frame->length = frame_length;
456 
457  /*Transfer frame buffer to RAM buffer */
458 
459  HAL_SPI_TRANSFER_WRITE(0);
460  HAL_SPI_TRANSFER_WAIT();
461  do{
462  *rx_data++ = HAL_SPI_TRANSFER_READ();
463  HAL_SPI_TRANSFER_WRITE(0);
464 
465  /* CRC was checked in hardware, but redoing the checksum here ensures the rx buffer
466  * is not being overwritten by the next packet. Since that lengthy computation makes
467  * such overwrites more likely, we skip it and hope for the best.
468  * Without the check a full buffer is read in 320us at 2x spi clocking.
469  * The 802.15.4 standard requires 640us after a greater than 18 byte frame.
470  * With a low interrupt latency overwrites should never occur.
471  */
472  // crc = _crc_ccitt_update(crc, tempData);
473 
474  HAL_SPI_TRANSFER_WAIT();
475 
476  } while (--frame_length > 0);
477 
478 
479  /*Read LQI value for this frame.*/
480  rx_frame->lqi = HAL_SPI_TRANSFER_READ();
481 
482  /* If crc was calculated set crc field in hal_rx_frame_t accordingly.
483  * Else show the crc has passed the hardware check.
484  */
485  rx_frame->crc = true;
486  }
487 
488  HAL_SPI_TRANSFER_CLOSE();
489 
490 #endif /* defined(__AVR_ATmega128RFA1__) */
491 }
492 
493 /*----------------------------------------------------------------------------*/
494 /** \brief This function will download a frame to the radio transceiver's frame
495  * buffer.
496  *
497  * \param write_buffer Pointer to data that is to be written to frame buffer.
498  * \param length Length of data. The maximum length is 127 bytes.
499  */
500 void
501 hal_frame_write(uint8_t *write_buffer, uint8_t length)
502 {
503 #if defined(__AVR_ATmega128RFA1__)
504  uint8_t *tx_buffer;
505  tx_buffer=(uint8_t *)0x180; //start of fifo in i/o space
506  /* Write frame length, including the two byte checksum */
507  /* The top bit of the length field shall be set to 0 for IEEE 802.15.4 compliant frames */
508  /* It should already be clear, so bypassing the masking is sanity check of the uip stack */
509 // length &= 0x7f;
510  _SFR_MEM8(tx_buffer++) = length;
511 
512  /* Download to the Frame Buffer.
513  * When the FCS is autogenerated there is no need to transfer the last two bytes
514  * since they will be overwritten.
515  */
516 #if !RF230_CONF_CHECKSUM
517  length -= 2;
518 #endif
519  do _SFR_MEM8(tx_buffer++)= *write_buffer++; while (--length);
520 
521 #else /* defined(__AVR_ATmega128RFA1__) */
522  /* Optionally truncate length to maximum frame length.
523  * Not doing this is a fast way to know when the application needs fixing!
524  */
525 // length &= 0x7f;
526 
528 
529  /* Send Frame Transmit (long mode) command and frame length */
530  HAL_SPI_TRANSFER(0x60);
531  HAL_SPI_TRANSFER(length);
532 
533  /* Download to the Frame Buffer.
534  * When the FCS is autogenerated there is no need to transfer the last two bytes
535  * since they will be overwritten.
536  */
537 #if !RF230_CONF_CHECKSUM
538  length -= 2;
539 #endif
540  do HAL_SPI_TRANSFER(*write_buffer++); while (--length);
541 
542  HAL_SPI_TRANSFER_CLOSE();
543 #endif /* defined(__AVR_ATmega128RFA1__) */
544 }
545 
546 /*----------------------------------------------------------------------------*/
547 /** \brief Read SRAM
548  *
549  * This function reads from the SRAM of the radio transceiver.
550  *
551  * \param address Address in the TRX's SRAM where the read burst should start
552  * \param length Length of the read burst
553  * \param data Pointer to buffer where data is stored.
554  */
555 #if 0 //Uses 80 bytes (on Raven) omit unless needed
556 void
557 hal_sram_read(uint8_t address, uint8_t length, uint8_t *data)
558 {
560 
561  /*Send SRAM read command and address to start*/
562  HAL_SPI_TRANSFER(0x00);
563  HAL_SPI_TRANSFER(address);
564 
565  HAL_SPI_TRANSFER_WRITE(0);
566  HAL_SPI_TRANSFER_WAIT();
567 
568  /*Upload the chosen memory area.*/
569  do{
570  *data++ = HAL_SPI_TRANSFER_READ();
571  HAL_SPI_TRANSFER_WRITE(0);
572  HAL_SPI_TRANSFER_WAIT();
573  } while (--length > 0);
574 
575  HAL_SPI_TRANSFER_CLOSE();
576 }
577 #endif
578 /*----------------------------------------------------------------------------*/
579 /** \brief Write SRAM
580  *
581  * This function writes into the SRAM of the radio transceiver. It can reduce
582  * SPI transfers if only part of a frame is to be changed before retransmission.
583  *
584  * \param address Address in the TRX's SRAM where the write burst should start
585  * \param length Length of the write burst
586  * \param data Pointer to an array of bytes that should be written
587  */
588 #if 0 //omit unless needed
589 void
590 hal_sram_write(uint8_t address, uint8_t length, uint8_t *data)
591 {
593 
594  /*Send SRAM write command.*/
595  HAL_SPI_TRANSFER(0x40);
596 
597  /*Send address where to start writing to.*/
598  HAL_SPI_TRANSFER(address);
599 
600  /*Upload the chosen memory area.*/
601  do{
602  HAL_SPI_TRANSFER(*data++);
603  } while (--length > 0);
604 
605  HAL_SPI_TRANSFER_CLOSE();
606 
607 }
608 #endif
609 
610 /*----------------------------------------------------------------------------*/
611 /* This #if compile switch is used to provide a "standard" function body for the */
612 /* doxygen documentation. */
613 #if defined(DOXYGEN)
614 /** \brief ISR for the radio IRQ line, triggered by the input capture.
615  * This is the interrupt service routine for timer1.ICIE1 input capture.
616  * It is triggered of a rising edge on the radio transceivers IRQ line.
617  */
618 void RADIO_VECT(void);
619 #else /* !DOXYGEN */
620 /* These link to the RF230BB driver in rf230.c */
621 void rf230_interrupt(void);
622 
623 extern hal_rx_frame_t rxframe[RF230_CONF_RX_BUFFERS];
624 extern uint8_t rxframe_head,rxframe_tail;
625 
626 /* rf230interruptflag can be printed in the main idle loop for debugging */
627 #define DEBUG 0
628 #if DEBUG
629 volatile char rf230interruptflag;
630 #define INTERRUPTDEBUG(arg) rf230interruptflag=arg
631 #else
632 #define INTERRUPTDEBUG(arg)
633 #endif
634 
635 #if defined(__AVR_ATmega128RFA1__)
636 /* The atmega128rfa1 has individual interrupts for the integrated radio'
637  * Whichever are enabled by the RF230 driver must be present even if not used!
638  */
639 /* Received packet interrupt */
640 ISR(TRX24_RX_END_vect)
641 {
642 /* Get the rssi from ED if extended mode */
643 #if RF230_CONF_AUTOACK
644  rf230_last_rssi=hal_register_read(RG_PHY_ED_LEVEL);
645 #endif
646 
647 /* Buffer the frame and call rf230_interrupt to schedule poll for rf230 receive process */
648 /* Is a ram buffer available? */
649  if (rxframe[rxframe_tail].length) {DEBUGFLOW('0');} else /*DEBUGFLOW('1')*/;
650 
651 #ifdef RF230_MIN_RX_POWER
652 /* Discard packets weaker than the minimum if defined. This is for testing miniature meshes */
653 /* This does not prevent an autoack. TODO:rfa1 radio can be set up to not autoack weak packets */
654  if (rf230_last_rssi >= RF230_MIN_RX_POWER) {
655 #else
656  if (1) {
657 #endif
658 // DEBUGFLOW('2');
659  hal_frame_read(&rxframe[rxframe_tail]);
660  rxframe_tail++;if (rxframe_tail >= RF230_CONF_RX_BUFFERS) rxframe_tail=0;
661  rf230_interrupt();
662  }
663 }
664 /* Preamble detected, starting frame reception */
665 ISR(TRX24_RX_START_vect)
666 {
667 // DEBUGFLOW('3');
668 /* Save RSSI for this packet if not in extended mode, scaling to 1dB resolution */
669 #if !RF230_CONF_AUTOACK
670  rf230_last_rssi = 3 * hal_subregister_read(SR_RSSI);
671 #endif
672 
673 }
674 
675 /* PLL has locked, either from a transition out of TRX_OFF or a channel change while on */
676 ISR(TRX24_PLL_LOCK_vect)
677 {
678 // DEBUGFLOW('4');
679 }
680 
681 /* PLL has unexpectedly unlocked */
682 ISR(TRX24_PLL_UNLOCK_vect)
683 {
684  DEBUGFLOW('5');
685 }
686 /* Flag is set by the following interrupts */
687 extern volatile uint8_t rf230_wakewait, rf230_txendwait,rf230_ccawait;
688 
689 /* Wake has finished */
690 ISR(TRX24_AWAKE_vect)
691 {
692 // DEBUGFLOW('6');
693  rf230_wakewait=0;
694 }
695 
696 /* Transmission has ended */
697 ISR(TRX24_TX_END_vect)
698 {
699 // DEBUGFLOW('7');
700  rf230_txendwait=0;
701 }
702 
703 /* Frame address has matched ours */
704 ISR(TRX24_XAH_AMI_vect)
705 {
706 // DEBUGFLOW('8');
707 }
708 
709 /* CCAED measurement has completed */
710 ISR(TRX24_CCA_ED_DONE_vect)
711 {
712  DEBUGFLOW('4');
713  rf230_ccawait=0;
714 }
715 
716 #else /* defined(__AVR_ATmega128RFA1__) */
717 /* Separate RF230 has a single radio interrupt and the source must be read from the IRQ_STATUS register */
719 {
720  volatile uint8_t state;
721  uint8_t interrupt_source; /* used after HAL_SPI_TRANSFER_OPEN/CLOSE block */
722 
723  INTERRUPTDEBUG(1);
724 
725 
726  /* Using SPI bus from ISR is generally a bad idea... */
727  /* Note: all IRQ are not always automatically disabled when running in ISR */
729 
730  /*Read Interrupt source.*/
731  /*Send Register address and read register content.*/
732  HAL_SPI_TRANSFER_WRITE(0x80 | RG_IRQ_STATUS);
733 
734  HAL_SPI_TRANSFER_WAIT(); /* AFTER possible interleaved processing */
735 
736  interrupt_source = HAL_SPI_TRANSFER(0);
737 
738  HAL_SPI_TRANSFER_CLOSE();
739 
740  /*Handle the incomming interrupt. Prioritized.*/
741  if ((interrupt_source & HAL_RX_START_MASK)){
742  INTERRUPTDEBUG(10);
743  /* Save RSSI for this packet if not in extended mode, scaling to 1dB resolution */
744 #if !RF230_CONF_AUTOACK
745 #if 0 // 3-clock shift and add is faster on machines with no hardware multiply
746  // With -Os avr-gcc saves a byte by using the general routine for multiply by 3
747  rf230_last_rssi = hal_subregister_read(SR_RSSI);
748  rf230_last_rssi = (rf230_last_rssi <<1) + rf230_last_rssi;
749 #else // Faster with 1-clock multiply. Raven and Jackdaw have 2-clock multiply so same speed while saving 2 bytes of program memory
750  rf230_last_rssi = 3 * hal_subregister_read(SR_RSSI);
751 #endif
752 #endif
753 
754  } else if (interrupt_source & HAL_TRX_END_MASK){
755  INTERRUPTDEBUG(11);
756 
758  if((state == BUSY_RX_AACK) || (state == RX_ON) || (state == BUSY_RX) || (state == RX_AACK_ON)){
759  /* Received packet interrupt */
760  /* Buffer the frame and call rf230_interrupt to schedule poll for rf230 receive process */
761  if (rxframe[rxframe_tail].length) INTERRUPTDEBUG(42); else INTERRUPTDEBUG(12);
762 
763 #ifdef RF230_MIN_RX_POWER
764  /* Discard packets weaker than the minimum if defined. This is for testing miniature meshes.*/
765  /* Save the rssi for printing in the main loop */
766 #if RF230_CONF_AUTOACK
767  //rf230_last_rssi=hal_subregister_read(SR_ED_LEVEL);
768  rf230_last_rssi=hal_register_read(RG_PHY_ED_LEVEL);
769 #endif
770  if (rf230_last_rssi >= RF230_MIN_RX_POWER) {
771 #endif
772  hal_frame_read(&rxframe[rxframe_tail]);
773  rxframe_tail++;if (rxframe_tail >= RF230_CONF_RX_BUFFERS) rxframe_tail=0;
774  rf230_interrupt();
775 #ifdef RF230_MIN_RX_POWER
776  }
777 #endif
778 
779  }
780 
781  } else if (interrupt_source & HAL_TRX_UR_MASK){
782  INTERRUPTDEBUG(13);
783  ;
784  } else if (interrupt_source & HAL_PLL_UNLOCK_MASK){
785  INTERRUPTDEBUG(14);
786  ;
787  } else if (interrupt_source & HAL_PLL_LOCK_MASK){
788  INTERRUPTDEBUG(15);
789  ;
790  } else if (interrupt_source & HAL_BAT_LOW_MASK){
791  /* Disable BAT_LOW interrupt to prevent endless interrupts. The interrupt */
792  /* will continously be asserted while the supply voltage is less than the */
793  /* user-defined voltage threshold. */
794  uint8_t trx_isr_mask = hal_register_read(RG_IRQ_MASK);
795  trx_isr_mask &= ~HAL_BAT_LOW_MASK;
796  hal_register_write(RG_IRQ_MASK, trx_isr_mask);
797  INTERRUPTDEBUG(16);
798  ;
799  } else {
800  INTERRUPTDEBUG(99);
801  ;
802  }
803 }
804 #endif /* defined(__AVR_ATmega128RFA1__) */
805 # endif /* defined(DOXYGEN) */
806 
807 /** @} */
808 /** @} */
809 
810 /*EOF*/
#define RX_ON
Constant RX_ON for sub-register SR_TRX_STATUS.
#define SR_TRX_STATUS
Access parameters for sub-register TRX_STATUS in register RG_TRX_STATUS.
#define HAL_DD_MISO
Data Direction bit for MISO.
Definition: hal.h:264
#define HAL_DD_SCK
Data Direction bit for SCK.
Definition: hal.h:262
#define DDR_RST
Data Direction Register that corresponds to the port where RST is.
Definition: hal.h:252
uint8_t lqi
LQI value for received frame.
Definition: hal.h:354
#define RG_IRQ_MASK
Offset for register IRQ_MASK.
#define hal_enable_trx_interrupt()
Enable the interrupt from the radio transceiver.
Definition: hal.h:322
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
#define HAL_PORT_MOSI
The SPI module uses GPIO might be split on different ports.
Definition: hal.h:266
#define RG_IRQ_STATUS
Offset for register IRQ_STATUS.
This file contains the register definitions for the AT86RF230.
#define HAL_BAT_LOW_MASK
Mask for the BAT_LOW interrupt.
Definition: hal.h:335
#define HAL_DDR_SS
Data Direction Register for MISO GPIO pin.
Definition: hal.h:269
#define RX_AACK_ON
Constant RX_AACK_ON for sub-register SR_TRX_STATUS.
#define HAL_DD_SS
Data Direction bit for SS.
Definition: hal.h:261
#define HAL_DDR_MISO
Data Direction Register for MOSI GPIO pin.
Definition: hal.h:272
#define DDR_SLP_TR
Data Direction Register that corresponds to the port where SLP_TR is connected.
Definition: hal.h:245
#define RG_PHY_ED_LEVEL
Offset for register PHY_ED_LEVEL.
#define HAL_SS_PIN
The slave select pin.
Definition: hal.h:258
uint8_t length
Length of frame.
Definition: hal.h:352
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
uint8_t data[HAL_MAX_FRAME_LENGTH]
Actual frame data.
Definition: hal.h:353
void hal_sram_read(uint8_t address, uint8_t length, uint8_t *data)
Read SRAM.
Definition: hal.c:558
#define BUSY_RX
Constant BUSY_RX for sub-register SR_TRX_STATUS.
bool crc
Flag - did CRC pass for received frame?
Definition: hal.h:355
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
#define RST
Pin number that corresponds to the RST pin.
Definition: hal.h:251
#define HAL_LEAVE_CRITICAL_REGION()
This macro must always be used in conjunction with HAL_ENTER_CRITICAL_REGION so that interrupts are e...
Definition: hal.h:322
#define HAL_PLL_LOCK_MASK
Mask for the PLL_LOCK interrupt.
Definition: hal.h:340
void hal_sram_write(uint8_t address, uint8_t length, uint8_t *data)
Write SRAM.
Definition: hal.c:597
#define HAL_RX_START_MASK
Mask for the RX_START interrupt.
Definition: hal.h:338
#define HAL_SPI_TRANSFER_OPEN()
This function is called when a rx_start interrupt is signaled.
Definition: halbb.c:220
#define HAL_TRX_UR_MASK
Mask for the TRX_UR interrupt.
Definition: hal.h:336
#define HAL_PORT_MISO
The SPI module uses GPIO might be split on different ports.
Definition: hal.h:267
#define SR_RSSI
Access parameters for sub-register RSSI in register RG_PHY_RSSI.
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_RF230_ISR()
This function initializes the Hardware Abstraction Layer.
Definition: halbb.c:203
#define BUSY_RX_AACK
Constant BUSY_RX_AACK for sub-register SR_TRX_STATUS.
#define HAL_PLL_UNLOCK_MASK
Mask for the PLL_UNLOCK interrupt.
Definition: hal.h:339
void RADIO_VECT(void)
ISR for the radio IRQ line, triggered by the input capture.
#define HAL_ENTER_CRITICAL_REGION()
This macro will protect the following code from interrupts.
Definition: hal.h:318
#define HAL_MAX_FRAME_LENGTH
A frame should no more than 127 bytes.
Definition: hal.h:343
#define HAL_DDR_SPI
Data Direction Register for PORTB.
Definition: hal.h:260
#define HAL_PORT_SPI
The SPI module is located on PORTB.
Definition: hal.h:259
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
#define HAL_SCK_PIN
Data bit for SCK.
Definition: hal.h:260
#define HAL_DDR_SCK
Data Direction Register for MISO GPIO pin.
Definition: hal.h:270
#define HAL_DDR_MOSI
Data Direction Register for MISO GPIO pin.
Definition: hal.h:271
#define HAL_DD_MOSI
Data Direction bit for MOSI.
Definition: hal.h:263
#define SLP_TR
Pin number that corresponds to the SLP_TR pin.
Definition: hal.h:244
#define HAL_MIN_FRAME_LENGTH
A frame should be at least 3 bytes.
Definition: hal.h:342
#define HAL_TRX_END_MASK
Mask for the TRX_END interrupt.
Definition: hal.h:337
This file contains low-level radio driver code.