61 #include "contiki-conf.h"
63 extern uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE];
64 #define DEBUGFLOW(c) if (debugflowsize<(DEBUGFLOWSIZE-1)) debugflow[debugflowsize++]=c
75 #if defined(__AVR_ATmega128RFA1__)
94 static uint16_t hal_system_time = 0;
95 volatile extern signed char rf230_last_rssi;
128 #if defined(__AVR_ATmega128RFA1__)
130 #include <avr/interrupt.h>
132 #define HAL_SPI_TRANSFER_OPEN()
134 #define HAL_SPI_TRANSFER_WAIT()
135 #define HAL_SPI_TRANSFER_READ() (SPDR)
136 #define HAL_SPI_TRANSFER_CLOSE()
138 #define HAL_SPI_TRANSFER(to_write) ( \
139 HAL_SPI_TRANSFER_WRITE(to_write), \
140 HAL_SPI_TRANSFER_WAIT(), \
141 HAL_SPI_TRANSFER_READ() )
143 #elif defined(__AVR__)
148 #include <avr/interrupt.h>
150 #define HAL_SPI_TRANSFER_OPEN() { \
151 HAL_ENTER_CRITICAL_REGION(); \
153 #define HAL_SPI_TRANSFER_WRITE(to_write) (SPDR = (to_write))
154 #define HAL_SPI_TRANSFER_WAIT() ({while ((SPSR & (1 << SPIF)) == 0) {;}})
155 #define HAL_SPI_TRANSFER_READ() (SPDR)
156 #define HAL_SPI_TRANSFER_CLOSE() \
158 HAL_LEAVE_CRITICAL_REGION(); \
160 #define HAL_SPI_TRANSFER(to_write) ( \
161 HAL_SPI_TRANSFER_WRITE(to_write), \
162 HAL_SPI_TRANSFER_WAIT(), \
163 HAL_SPI_TRANSFER_READ() )
169 #include "contiki-mulle.h"
172 #define HAL_SPI_TRANSFER_OPEN() { uint8_t spiTemp; \
173 HAL_ENTER_CRITICAL_REGION(); \
175 #define HAL_SPI_TRANSFER_WRITE(to_write) (spiTemp = spiWrite(to_write))
176 #define HAL_SPI_TRANSFER_WAIT() ({0;})
177 #define HAL_SPI_TRANSFER_READ() (spiTemp)
178 #define HAL_SPI_TRANSFER_CLOSE() \
180 HAL_LEAVE_CRITICAL_REGION(); \
182 #define HAL_SPI_TRANSFER(to_write) (spiTemp = spiWrite(to_write))
184 inline uint8_t spiWrite(uint8_t byte)
190 if( (byte & mask) != 0 )
200 }
while( (mask >>= 1) != 0 );
208 #if defined(__AVR_ATmega128RFA1__)
210 #define HAL_TIME_ISR() ISR(TIMER1_OVF_vect)
211 #define HAL_TICK_UPCNT() (TCNT1)
223 #elif defined(__AVR__)
224 #define HAL_RF230_ISR() ISR(RADIO_VECT)
225 #define HAL_TIME_ISR() ISR(TIMER1_OVF_vect)
226 #define HAL_TICK_UPCNT() (TCNT1)
243 SPCR = (1 << SPE) | (1 << MSTR);
247 TCCR1B = HAL_TCCR1B_CONFIG;
248 TIFR1 |= (1 << ICF1);
249 HAL_ENABLE_OVERFLOW_INTERRUPT();
255 #define HAL_RF230_ISR() M16C_INTERRUPT(M16C_INT1)
256 #define HAL_TIME_ISR() M16C_INTERRUPT(M16C_TMRB4)
257 #define HAL_TICK_UPCNT() (0xFFFF-TB4) // TB4 counts down so we need to convert it to upcounting
284 TB3MR.BYTE = 0b00000000;
288 TB4MR.BYTE = 0b10000001;
291 HAL_ENABLE_OVERFLOW_INTERRUPT();
431 #if defined(__AVR_ATmega128RFA1__)
450 return (_SFR_MEM8(address)&mask)>>position;
457 uint8_t register_value = _SFR_MEM8(address);
458 register_value &= ~mask;
461 value |= register_value;
462 _SFR_MEM8(address) = value;
480 uint8_t register_value;
489 HAL_SPI_TRANSFER(address);
490 register_value = HAL_SPI_TRANSFER(0);
492 HAL_SPI_TRANSFER_CLOSE();
494 return register_value;
510 address = 0xc0 | address;
515 HAL_SPI_TRANSFER(address);
516 HAL_SPI_TRANSFER(value);
518 HAL_SPI_TRANSFER_CLOSE();
536 register_value &= mask;
537 register_value >>= position;
539 return register_value;
559 register_value &= ~mask;
565 value |= register_value;
589 #if defined(__AVR_ATmega128RFA1__)
591 uint8_t frame_length,*rx_data,*rx_buffer;
593 rx_data = (rx_frame->
data);
594 frame_length = TST_RX_LENGTH;
595 rx_frame->
length = frame_length;
596 rx_buffer=(uint8_t *)0x180;
599 *rx_data++ = _SFR_MEM8(rx_buffer++);
601 }
while (--frame_length > 0);
604 rx_frame->
lqi = *rx_buffer;
618 HAL_SPI_TRANSFER(0x20);
621 uint8_t frame_length = HAL_SPI_TRANSFER(0);
628 rx_data = (rx_frame->
data);
629 rx_frame->
length = frame_length;
635 HAL_SPI_TRANSFER_WRITE(0);
636 HAL_SPI_TRANSFER_WAIT();
639 *rx_data++ = HAL_SPI_TRANSFER_READ();
640 HAL_SPI_TRANSFER_WRITE(0);
652 HAL_SPI_TRANSFER_WAIT();
654 }
while (--frame_length > 0);
658 rx_frame->
lqi = HAL_SPI_TRANSFER_READ();
675 rx_frame->
crc =
false;
679 HAL_SPI_TRANSFER_CLOSE();
692 #if defined(__AVR_ATmega128RFA1__)
694 tx_buffer=(uint8_t *)0x180;
699 _SFR_MEM8(tx_buffer++) = length;
705 #if !RF230_CONF_CHECKSUM
708 do _SFR_MEM8(tx_buffer++)= *write_buffer++;
while (--length);
719 HAL_SPI_TRANSFER(0x60);
720 HAL_SPI_TRANSFER(length);
726 #if !RF230_CONF_CHECKSUM
729 do HAL_SPI_TRANSFER(*write_buffer++);
while (--length);
731 HAL_SPI_TRANSFER_CLOSE();
744 #if 0 //Uses 80 bytes (on Raven) omit unless needed
746 hal_sram_read(uint8_t address, uint8_t length, uint8_t *data)
751 HAL_SPI_TRANSFER(0x00);
752 HAL_SPI_TRANSFER(address);
754 HAL_SPI_TRANSFER_WRITE(0);
755 HAL_SPI_TRANSFER_WAIT();
759 *data++ = HAL_SPI_TRANSFER_READ();
760 HAL_SPI_TRANSFER_WRITE(0);
761 HAL_SPI_TRANSFER_WAIT();
762 }
while (--length > 0);
764 HAL_SPI_TRANSFER_CLOSE();
808 void rf230_interrupt(
void);
811 extern uint8_t rxframe_head,rxframe_tail;
816 volatile char rf230interruptflag;
817 #define INTERRUPTDEBUG(arg) rf230interruptflag=arg
819 #define INTERRUPTDEBUG(arg)
822 #if defined(__AVR_ATmega128RFA1__)
827 ISR(TRX24_RX_END_vect)
830 #if RF230_CONF_AUTOACK
836 if (rxframe[rxframe_tail].length) {DEBUGFLOW(
'0');}
else ;
838 #ifdef RF230_MIN_RX_POWER
841 if (rf230_last_rssi >= RF230_MIN_RX_POWER) {
847 rxframe_tail++;
if (rxframe_tail >= RF230_CONF_RX_BUFFERS) rxframe_tail=0;
852 ISR(TRX24_RX_START_vect)
856 #if !RF230_CONF_AUTOACK
863 ISR(TRX24_PLL_LOCK_vect)
869 ISR(TRX24_PLL_UNLOCK_vect)
874 extern volatile uint8_t rf230_interruptwait;
877 ISR(TRX24_AWAKE_vect)
880 rf230_interruptwait=0;
884 ISR(TRX24_TX_END_vect)
887 rf230_interruptwait=0;
891 extern volatile uint8_t rf230_pending;
892 ISR(TRX24_XAH_AMI_vect)
899 ISR(TRX24_CCA_ED_DONE_vect)
902 rf230_interruptwait=0;
917 volatile uint8_t state;
918 uint8_t interrupt_source;
938 HAL_SPI_TRANSFER_WAIT();
941 interrupt_source = HAL_SPI_TRANSFER_READ();
943 interrupt_source = HAL_SPI_TRANSFER(interrupt_source);
945 interrupt_source = HAL_SPI_TRANSFER(0);
947 HAL_SPI_TRANSFER_CLOSE();
953 #if !RF230_CONF_AUTOACK
954 #if 0 // 3-clock shift and add is faster on machines with no hardware multiply
957 rf230_last_rssi = (rf230_last_rssi <<1) + rf230_last_rssi;
958 #else // Faster with 1-clock multiply. Raven and Jackdaw have 2-clock multiply so same speed while saving 2 bytes of program memory
983 if (rxframe[rxframe_tail].length) INTERRUPTDEBUG(42);
else INTERRUPTDEBUG(12);
985 #ifdef RF230_MIN_RX_POWER
988 #if RF230_CONF_AUTOACK
992 if (rf230_last_rssi >= RF230_MIN_RX_POWER) {
995 rxframe_tail++;
if (rxframe_tail >= RF230_CONF_RX_BUFFERS) rxframe_tail=0;
998 #ifdef RF230_MIN_RX_POWER
1019 trx_isr_mask &= ~HAL_BAT_LOW_MASK;
1035 #if defined(DOXYGEN)