62 #include "contiki-conf.h"
64 extern uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE];
65 #define DEBUGFLOW(c) if (debugflowsize<(DEBUGFLOWSIZE-1)) debugflow[debugflowsize++]=c
76 #if defined(__AVR_ATmega128RFA1__)
78 #include "atmega128rfa1_registermap.h"
85 volatile extern signed char rf230_last_rssi;
91 #if defined(__AVR_ATmega128RFA1__)
94 #include <avr/interrupt.h>
96 #elif defined(__AVR__)
101 #include <avr/interrupt.h>
103 #define HAL_SPI_TRANSFER_OPEN() { \
104 HAL_ENTER_CRITICAL_REGION(); \
106 #define HAL_SPI_TRANSFER_WRITE(to_write) (SPDR = (to_write))
107 #define HAL_SPI_TRANSFER_WAIT() ({while ((SPSR & (1 << SPIF)) == 0) {;}})
108 #define HAL_SPI_TRANSFER_READ() (SPDR)
109 #define HAL_SPI_TRANSFER_CLOSE() \
111 HAL_LEAVE_CRITICAL_REGION(); \
113 #define HAL_SPI_TRANSFER(to_write) ( \
114 HAL_SPI_TRANSFER_WRITE(to_write), \
115 HAL_SPI_TRANSFER_WAIT(), \
116 HAL_SPI_TRANSFER_READ() )
122 #include "contiki-mulle.h"
125 #define HAL_SPI_TRANSFER_OPEN() { uint8_t spiTemp; \
126 HAL_ENTER_CRITICAL_REGION(); \
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() \
133 HAL_LEAVE_CRITICAL_REGION(); \
135 #define HAL_SPI_TRANSFER(to_write) (spiTemp = spiWrite(to_write))
137 inline uint8_t spiWrite(uint8_t byte)
143 if( (byte & mask) != 0 )
153 }
while( (mask >>= 1) != 0 );
161 #if defined(__AVR_ATmega128RFA1__)
170 #elif defined(__AVR__)
172 #define HAL_RF230_ISR() ISR(RADIO_VECT)
194 SPCR = (1 << SPE) | (1 << MSTR);
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
230 TB3MR.BYTE = 0b00000000;
234 TB4MR.BYTE = 0b10000001;
237 HAL_ENABLE_OVERFLOW_INTERRUPT();
245 #if defined(__AVR_ATmega128RFA1__)
264 return (_SFR_MEM8(address)&mask)>>position;
272 uint8_t register_value = _SFR_MEM8(address);
273 register_value &= ~mask;
276 value |= register_value;
277 _SFR_MEM8(address) = value;
296 uint8_t register_value;
305 HAL_SPI_TRANSFER(address);
306 register_value = HAL_SPI_TRANSFER(0);
308 HAL_SPI_TRANSFER_CLOSE();
310 return register_value;
326 address = 0xc0 | address;
331 HAL_SPI_TRANSFER(address);
332 HAL_SPI_TRANSFER(value);
334 HAL_SPI_TRANSFER_CLOSE();
352 register_value &= mask;
353 register_value >>= position;
355 return register_value;
375 register_value &= ~mask;
381 value |= register_value;
402 #if defined(__AVR_ATmega128RFA1__)
404 uint8_t frame_length,*rx_data,*rx_buffer;
409 frame_length = TST_RX_LENGTH;
414 rx_frame->
crc =
false;
417 rx_frame->
length = frame_length;
420 rx_buffer=(uint8_t *)0x180;
421 rx_data = (rx_frame->
data);
424 *rx_data++ = _SFR_MEM8(rx_buffer++);
425 }
while (--frame_length > 0);
428 rx_frame->
lqi = *rx_buffer;
433 rx_frame->
crc =
true;
437 uint8_t frame_length, *rx_data;
441 HAL_SPI_TRANSFER(0x20);
444 frame_length = HAL_SPI_TRANSFER(0);
451 rx_frame->
crc =
false;
454 rx_data = (rx_frame->
data);
455 rx_frame->
length = frame_length;
459 HAL_SPI_TRANSFER_WRITE(0);
460 HAL_SPI_TRANSFER_WAIT();
462 *rx_data++ = HAL_SPI_TRANSFER_READ();
463 HAL_SPI_TRANSFER_WRITE(0);
474 HAL_SPI_TRANSFER_WAIT();
476 }
while (--frame_length > 0);
480 rx_frame->
lqi = HAL_SPI_TRANSFER_READ();
485 rx_frame->
crc =
true;
488 HAL_SPI_TRANSFER_CLOSE();
503 #if defined(__AVR_ATmega128RFA1__)
505 tx_buffer=(uint8_t *)0x180;
510 _SFR_MEM8(tx_buffer++) = length;
516 #if !RF230_CONF_CHECKSUM
519 do _SFR_MEM8(tx_buffer++)= *write_buffer++;
while (--length);
530 HAL_SPI_TRANSFER(0x60);
531 HAL_SPI_TRANSFER(length);
537 #if !RF230_CONF_CHECKSUM
540 do HAL_SPI_TRANSFER(*write_buffer++);
while (--length);
542 HAL_SPI_TRANSFER_CLOSE();
555 #if 0 //Uses 80 bytes (on Raven) omit unless needed
557 hal_sram_read(uint8_t address, uint8_t length, uint8_t *data)
562 HAL_SPI_TRANSFER(0x00);
563 HAL_SPI_TRANSFER(address);
565 HAL_SPI_TRANSFER_WRITE(0);
566 HAL_SPI_TRANSFER_WAIT();
570 *data++ = HAL_SPI_TRANSFER_READ();
571 HAL_SPI_TRANSFER_WRITE(0);
572 HAL_SPI_TRANSFER_WAIT();
573 }
while (--length > 0);
575 HAL_SPI_TRANSFER_CLOSE();
588 #if 0 //omit unless needed
595 HAL_SPI_TRANSFER(0x40);
598 HAL_SPI_TRANSFER(address);
602 HAL_SPI_TRANSFER(*data++);
603 }
while (--length > 0);
605 HAL_SPI_TRANSFER_CLOSE();
621 void rf230_interrupt(
void);
624 extern uint8_t rxframe_head,rxframe_tail;
629 volatile char rf230interruptflag;
630 #define INTERRUPTDEBUG(arg) rf230interruptflag=arg
632 #define INTERRUPTDEBUG(arg)
635 #if defined(__AVR_ATmega128RFA1__)
640 ISR(TRX24_RX_END_vect)
643 #if RF230_CONF_AUTOACK
649 if (rxframe[rxframe_tail].length) {DEBUGFLOW(
'0');}
else ;
651 #ifdef RF230_MIN_RX_POWER
654 if (rf230_last_rssi >= RF230_MIN_RX_POWER) {
660 rxframe_tail++;
if (rxframe_tail >= RF230_CONF_RX_BUFFERS) rxframe_tail=0;
665 ISR(TRX24_RX_START_vect)
669 #if !RF230_CONF_AUTOACK
676 ISR(TRX24_PLL_LOCK_vect)
682 ISR(TRX24_PLL_UNLOCK_vect)
687 extern volatile uint8_t rf230_wakewait, rf230_txendwait,rf230_ccawait;
690 ISR(TRX24_AWAKE_vect)
697 ISR(TRX24_TX_END_vect)
704 ISR(TRX24_XAH_AMI_vect)
710 ISR(TRX24_CCA_ED_DONE_vect)
720 volatile uint8_t state;
721 uint8_t interrupt_source;
734 HAL_SPI_TRANSFER_WAIT();
736 interrupt_source = HAL_SPI_TRANSFER(0);
738 HAL_SPI_TRANSFER_CLOSE();
744 #if !RF230_CONF_AUTOACK
745 #if 0 // 3-clock shift and add is faster on machines with no hardware multiply
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
761 if (rxframe[rxframe_tail].length) INTERRUPTDEBUG(42);
else INTERRUPTDEBUG(12);
763 #ifdef RF230_MIN_RX_POWER
766 #if RF230_CONF_AUTOACK
770 if (rf230_last_rssi >= RF230_MIN_RX_POWER) {
773 rxframe_tail++;
if (rxframe_tail >= RF230_CONF_RX_BUFFERS) rxframe_tail=0;
775 #ifdef RF230_MIN_RX_POWER
795 trx_isr_mask &= ~HAL_BAT_LOW_MASK;
#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.
#define HAL_DD_SCK
Data Direction bit for SCK.
#define DDR_RST
Data Direction Register that corresponds to the port where RST is.
uint8_t lqi
LQI value for received frame.
#define RG_IRQ_MASK
Offset for register IRQ_MASK.
#define hal_enable_trx_interrupt()
Enable the interrupt from the radio transceiver.
uint8_t hal_register_read(uint8_t address)
This function reads data from one of the radio transceiver's registers.
#define HAL_PORT_MOSI
The SPI module uses GPIO might be split on different ports.
#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.
#define HAL_DDR_SS
Data Direction Register for MISO GPIO pin.
#define RX_AACK_ON
Constant RX_AACK_ON for sub-register SR_TRX_STATUS.
#define HAL_DD_SS
Data Direction bit for SS.
#define HAL_DDR_MISO
Data Direction Register for MOSI GPIO pin.
#define DDR_SLP_TR
Data Direction Register that corresponds to the port where SLP_TR is connected.
#define RG_PHY_ED_LEVEL
Offset for register PHY_ED_LEVEL.
#define HAL_SS_PIN
The slave select pin.
uint8_t length
Length of frame.
uint8_t hal_subregister_read(uint8_t address, uint8_t mask, uint8_t position)
This function reads the value of a specific subregister.
void hal_init(void)
This function initializes the Hardware Abstraction Layer.
This struct defines the rx data container.
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's subregisters.
uint8_t data[HAL_MAX_FRAME_LENGTH]
Actual frame data.
void hal_sram_read(uint8_t address, uint8_t length, uint8_t *data)
Read SRAM.
#define BUSY_RX
Constant BUSY_RX for sub-register SR_TRX_STATUS.
bool crc
Flag - did CRC pass for received frame?
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's frame buffer.
#define RST
Pin number that corresponds to the RST pin.
#define HAL_LEAVE_CRITICAL_REGION()
This macro must always be used in conjunction with HAL_ENTER_CRITICAL_REGION so that interrupts are e...
#define HAL_PLL_LOCK_MASK
Mask for the PLL_LOCK interrupt.
void hal_sram_write(uint8_t address, uint8_t length, uint8_t *data)
Write SRAM.
#define HAL_RX_START_MASK
Mask for the RX_START interrupt.
#define HAL_SPI_TRANSFER_OPEN()
This function is called when a rx_start interrupt is signaled.
#define HAL_TRX_UR_MASK
Mask for the TRX_UR interrupt.
#define HAL_PORT_MISO
The SPI module uses GPIO might be split on different ports.
#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's frame buffer.
#define HAL_RF230_ISR()
This function initializes the Hardware Abstraction Layer.
#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.
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.
#define HAL_MAX_FRAME_LENGTH
A frame should no more than 127 bytes.
#define HAL_DDR_SPI
Data Direction Register for PORTB.
#define HAL_PORT_SPI
The SPI module is located on PORTB.
void hal_register_write(uint8_t address, uint8_t value)
This function writes a new value to one of the radio transceiver's registers.
#define HAL_SCK_PIN
Data bit for SCK.
#define HAL_DDR_SCK
Data Direction Register for MISO GPIO pin.
#define HAL_DDR_MOSI
Data Direction Register for MISO GPIO pin.
#define HAL_DD_MOSI
Data Direction bit for MOSI.
#define SLP_TR
Pin number that corresponds to the SLP_TR pin.
#define HAL_MIN_FRAME_LENGTH
A frame should be at least 3 bytes.
#define HAL_TRX_END_MASK
Mask for the TRX_END interrupt.
This file contains low-level radio driver code.