1 /*******************************************************************
2 Copyright (C) 2009 FreakLabs
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the the copyright holder nor the names of its contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 Originally written by Christopher Wang aka Akiba.
31 Please post support questions to the FreakLabs forum.
33 *******************************************************************/
38 #include <avr/interrupt.h>
41 #define CHB_EEPROM_IEEE_ADDR 0x00
42 #define CHB_EEPROM_SHORT_ADDR 0x09
44 #define CHB_SPI_CMD_RW 0xC0 /**< Register Write (short mode). */
45 #define CHB_SPI_CMD_RR 0x80 /**< Register Read (short mode). */
46 #define CHB_SPI_CMD_FW 0x60 /**< Frame Transmit Mode (long mode). */
47 #define CHB_SPI_CMD_FR 0x20 /**< Frame Receive Mode (long mode). */
48 #define CHB_SPI_CMD_SW 0x40 /**< SRAM Write. */
49 #define CHB_SPI_CMD_SR 0x00 /**< SRAM Read. */
50 #define CHB_SPI_CMD_RADDRM 0x7F /**< Register Address Mask. */
52 #define CHB_IRQ_BAT_LOW_MASK 0x80 /**< Mask for the BAT_LOW interrupt. */
53 #define CHB_IRQ_TRX_UR_MASK 0x40 /**< Mask for the TRX_UR interrupt. */
54 #define CHB_IRQ_TRX_END_MASK 0x08 /**< Mask for the TRX_END interrupt. */
55 #define CHB_IRQ_RX_START_MASK 0x04 /**< Mask for the RX_START interrupt. */
56 #define CHB_IRQ_PLL_UNLOCK_MASK 0x02 /**< Mask for the PLL_UNLOCK interrupt. */
57 #define CHB_IRQ_PLL_LOCK_MASK 0x01 /**< Mask for the PLL_LOCK interrupt. */
59 #define CHB_RSTPORT PORTB
61 #define CHB_SLPTRPORT PORTB
62 #define CHB_SLPTRPIN 5
63 #define CHB_DDR_SLPTR DDRB
64 #define CHB_DDR_RST DDRB
65 #define CHB_RADIO_IRQ INT4_vect
67 #define CHB_ENTER_CRIT() {U8 volatile saved_sreg = SREG; cli()
68 #define CHB_LEAVE_CRIT() SREG = saved_sreg;}
69 #define CHB_RST_ENABLE() do {CHB_RSTPORT &= ~(_BV(CHB_RSTPIN));} while (0)
70 #define CHB_RST_DISABLE() do {CHB_RSTPORT |= (_BV(CHB_RSTPIN));} while (0)
71 #define CHB_SLPTR_ENABLE() do {CHB_SLPTRPORT |= (_BV(CHB_SLPTRPIN));} while (0)
72 #define CHB_SLPTR_DISABLE() do {CHB_SLPTRPORT &= ~(_BV(CHB_SLPTRPIN));} while (0)
76 CCA_ED
= 1, /**< Use energy detection above threshold mode. */
77 CCA_CARRIER_SENSE
= 2, /**< Use carrier sense mode. */
78 CCA_CARRIER_SENSE_WITH_ED
= 3 /**< Use a combination of both energy detection and carrier sense. */
88 CHB_MAX_FRAME_RETRIES
= 3,
89 CHB_MAX_CSMA_RETRIES
= 4,
91 CHB_CCA_MODE
= CCA_ED
,
92 CHB_CCA_ED_THRES
= 0x7,
139 CHB_MAX_FRAME_RETRIES_POS
= 4,
140 CHB_MAX_CSMA_RETIRES_POS
= 1,
142 CHB_CSMA_SEED1_POS
= 0,
143 CHB_CCA_MODE_POS
= 5,
144 CHB_AUTO_CRC_POS
= 7,
146 CHB_TRAC_STATUS_POS
= 5,
147 CHB_MIN_FRAME_LENGTH
= 3,
148 CHB_MAX_FRAME_LENGTH
= 0x7f
152 TIME_TO_ENTER_P_ON
= 510, /**< Transition time from VCC is applied to P_ON. */
153 TIME_P_ON_TO_TRX_OFF
= 510, /**< Transition time from P_ON to TRX_OFF. */
154 TIME_SLEEP_TO_TRX_OFF
= 880, /**< Transition time from SLEEP to TRX_OFF. */
155 TIME_RESET
= 6, /**< Time to hold the RST pin low during reset */
156 TIME_ED_MEASUREMENT
= 140, /**< Time it takes to do a ED measurement. */
157 TIME_CCA
= 140, /**< Time it takes to do a CCA. */
158 TIME_PLL_LOCK
= 150, /**< Maximum time it should take for the PLL to lock. */
159 TIME_FTN_TUNING
= 25, /**< Maximum time it should take to do the filter tuning. */
160 TIME_NOCLK_TO_WAKE
= 6, /**< Transition time from *_NOCLK to being awake. */
161 TIME_CMD_FORCE_TRX_OFF
= 1, /**< Time it takes to execute the FORCE_TRX_OFF command. */
162 TIME_TRX_OFF_TO_PLL_ACTIVE
= 180, /**< Transition time from TRX_OFF to: RX_ON, PLL_ON, TX_ARET_ON and RX_AACK_ON. */
163 TIME_STATE_TRANSITION_PLL_ACTIVE
= 1, /**< Transition time from PLL active state to another. */
167 RADIO_SUCCESS
= 0x40, /**< The requested service was performed successfully. */
168 RADIO_UNSUPPORTED_DEVICE
, /**< The connected device is not an Atmel AT86RF230. */
169 RADIO_INVALID_ARGUMENT
, /**< One or more of the supplied function arguments are invalid. */
170 RADIO_TIMED_OUT
, /**< The requested service timed out. */
171 RADIO_WRONG_STATE
, /**< The end-user tried to do an invalid state transition. */
172 RADIO_BUSY_STATE
, /**< The radio transceiver is busy receiving or transmitting. */
173 RADIO_STATE_TRANSITION_FAILED
, /**< The requested state transition could not be completed. */
174 RADIO_CCA_IDLE
, /**< Channel is clear, available to transmit a new frame. */
175 RADIO_CCA_BUSY
, /**< Channel busy. */
176 RADIO_TRX_BUSY
, /**< Transceiver is busy receiving or transmitting data. */
177 RADIO_BAT_LOW
, /**< Measured battery voltage is lower than voltage threshold. */
178 RADIO_BAT_OK
, /**< Measured battery voltage is above the voltage threshold. */
179 RADIO_CRC_FAILED
, /**< The CRC failed for the actual frame. */
180 RADIO_CHANNEL_ACCESS_FAILURE
, /**< The channel access failed during the auto mode. */
181 RADIO_NO_ACK
, /**< No acknowledge frame was received. */
188 CMD_FORCE_TRX_OFF
= 3,
210 RX_AACK_ON_NOCLK
= 29,
211 BUSY_RX_AACK_NOCLK
= 30
215 void chb_drvr_init();
218 U8
chb_reg_read(U8 addr
);
219 U16
chb_reg_read16(U8 addr
);
220 void chb_reg_write(U8 addr
, U8 val
);
221 void chb_reg_write16(U8 addr
, U16 val
);
222 void chb_reg_write64(U8 addr
, U8
*val
);
223 void chb_reg_read_mod_write(U8 addr
, U8 val
, U8 mask
);
224 void chb_frame_write(U8
*hdr
, U8 hdr_len
, U8
*data
, U8 data_len
);
226 // general configuration
227 U8
chb_set_channel(U8 channel
);
228 void chb_set_ieee_addr(U8
*addr
);
229 void chb_get_ieee_addr(U8
*addr
);
230 void chb_set_short_addr(U16 addr
);
231 U16
chb_get_short_addr();
234 U8
chb_tx(U8
*hdr
, U8
*data
, U8 len
);
238 void chb_sram_read(U8 addr
, U8 len
, U8
*data
);
239 void chb_sram_write(U8 addr
, U8 len
, U8
*data
);