Contiki 2.6

cc2420.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * $Id: cc2420.h,v 1.14 2010/12/16 22:39:50 adamdunkels Exp $
00032  */
00033 
00034 /**
00035  * \file
00036  *         CC2420 driver header file
00037  * \author
00038  *         Adam Dunkels <adam@sics.se>
00039  *         Joakim Eriksson <joakime@sics.se>
00040  */
00041 
00042 #ifndef __CC2420_H__
00043 #define __CC2420_H__
00044 
00045 #include "contiki.h"
00046 #include "dev/spi.h"
00047 #include "dev/radio.h"
00048 #include "dev/cc2420_const.h"
00049 
00050 int cc2420_init(void);
00051 
00052 #define CC2420_MAX_PACKET_LEN      127
00053 
00054 int cc2420_set_channel(int channel);
00055 int cc2420_get_channel(void);
00056 
00057 void cc2420_set_pan_addr(unsigned pan,
00058                                 unsigned addr,
00059                                 const uint8_t *ieee_addr);
00060 
00061 extern signed char cc2420_last_rssi;
00062 extern uint8_t cc2420_last_correlation;
00063 
00064 int cc2420_rssi(void);
00065 
00066 extern const struct radio_driver cc2420_driver;
00067 
00068 /**
00069  * \param power Between 1 and 31.
00070  */
00071 void cc2420_set_txpower(uint8_t power);
00072 int cc2420_get_txpower(void);
00073 #define CC2420_TXPOWER_MAX  31
00074 #define CC2420_TXPOWER_MIN   0
00075 
00076 /**
00077  * Interrupt function, called from the simple-cc2420-arch driver.
00078  *
00079  */
00080 int cc2420_interrupt(void);
00081 
00082 /* XXX hack: these will be made as Chameleon packet attributes */
00083 extern rtimer_clock_t cc2420_time_of_arrival,
00084   cc2420_time_of_departure;
00085 extern int cc2420_authority_level_of_sender;
00086 
00087 int cc2420_on(void);
00088 int cc2420_off(void);
00089 
00090 void cc2420_set_cca_threshold(int value);
00091 
00092 /************************************************************************/
00093 /* Additional SPI Macros for the CC2420 */
00094 /************************************************************************/
00095 /* Send a strobe to the CC2420 */
00096 #define CC2420_STROBE(s)                                   \
00097   do {                                                  \
00098     CC2420_SPI_ENABLE();                                \
00099     SPI_WRITE(s);                                       \
00100     CC2420_SPI_DISABLE();                               \
00101   } while (0)
00102 
00103 /* Write to a register in the CC2420                         */
00104 /* Note: the SPI_WRITE(0) seems to be needed for getting the */
00105 /* write reg working on the Z1 / MSP430X platform            */
00106 #define CC2420_WRITE_REG(adr,data)                              \
00107   do {                                                       \
00108     CC2420_SPI_ENABLE();                                     \
00109     SPI_WRITE_FAST(adr);                                     \
00110     SPI_WRITE_FAST((uint8_t)((data) >> 8));                  \
00111     SPI_WRITE_FAST((uint8_t)(data & 0xff));                  \
00112     SPI_WAITFORTx_ENDED();                                   \
00113     SPI_WRITE(0);                                            \
00114     CC2420_SPI_DISABLE();                                    \
00115   } while(0)
00116 
00117 /* Read a register in the CC2420 */
00118 #define CC2420_READ_REG(adr,data)                          \
00119   do {                                                  \
00120     CC2420_SPI_ENABLE();                                \
00121     SPI_WRITE(adr | 0x40);                              \
00122     data = (uint8_t)SPI_RXBUF;                          \
00123     SPI_TXBUF = 0;                                      \
00124     SPI_WAITFOREORx();                                  \
00125     data = SPI_RXBUF << 8;                              \
00126     SPI_TXBUF = 0;                                      \
00127     SPI_WAITFOREORx();                                  \
00128     data |= SPI_RXBUF;                                  \
00129     CC2420_SPI_DISABLE();                               \
00130   } while(0)
00131 
00132 #define CC2420_READ_FIFO_BYTE(data)                        \
00133   do {                                                  \
00134     CC2420_SPI_ENABLE();                                \
00135     SPI_WRITE(CC2420_RXFIFO | 0x40);                    \
00136     (void)SPI_RXBUF;                                    \
00137     SPI_READ(data);                                     \
00138     clock_delay(1);                                     \
00139     CC2420_SPI_DISABLE();                               \
00140   } while(0)
00141 
00142 #define CC2420_READ_FIFO_BUF(buffer,count)                                 \
00143   do {                                                                  \
00144     uint8_t i;                                                          \
00145     CC2420_SPI_ENABLE();                                                \
00146     SPI_WRITE(CC2420_RXFIFO | 0x40);                                    \
00147     (void)SPI_RXBUF;                                                    \
00148     for(i = 0; i < (count); i++) {                                      \
00149       SPI_READ(((uint8_t *)(buffer))[i]);                               \
00150     }                                                                   \
00151     clock_delay(1);                                                     \
00152     CC2420_SPI_DISABLE();                                               \
00153   } while(0)
00154 
00155 #define CC2420_WRITE_FIFO_BUF(buffer,count)                                \
00156   do {                                                                  \
00157     uint8_t i;                                                          \
00158     CC2420_SPI_ENABLE();                                                \
00159     SPI_WRITE_FAST(CC2420_TXFIFO);                                           \
00160     for(i = 0; i < (count); i++) {                                      \
00161       SPI_WRITE_FAST(((uint8_t *)(buffer))[i]);                              \
00162     }                                                                   \
00163     SPI_WAITFORTx_ENDED();                                              \
00164     CC2420_SPI_DISABLE();                                               \
00165   } while(0)
00166 
00167 /* Write to RAM in the CC2420 */
00168 #define CC2420_WRITE_RAM(buffer,adr,count)                 \
00169   do {                                                       \
00170     uint8_t i;                                               \
00171     CC2420_SPI_ENABLE();                                     \
00172     SPI_WRITE_FAST(0x80 | ((adr) & 0x7f));                   \
00173     SPI_WRITE_FAST(((adr) >> 1) & 0xc0);                     \
00174     for(i = 0; i < (count); i++) {                           \
00175       SPI_WRITE_FAST(((uint8_t*)(buffer))[i]);               \
00176     }                                                        \
00177     SPI_WAITFORTx_ENDED();                                   \
00178     CC2420_SPI_DISABLE();                                    \
00179   } while(0)
00180 
00181 /* Read from RAM in the CC2420 */
00182 #define CC2420_READ_RAM(buffer,adr,count)                    \
00183   do {                                                       \
00184     uint8_t i;                                               \
00185     CC2420_SPI_ENABLE();                                     \
00186     SPI_WRITE(0x80 | ((adr) & 0x7f));                        \
00187     SPI_WRITE((((adr) >> 1) & 0xc0) | 0x20);                 \
00188     SPI_RXBUF;                                               \
00189     for(i = 0; i < (count); i++) {                           \
00190       SPI_READ(((uint8_t*)(buffer))[i]);                     \
00191     }                                                        \
00192     CC2420_SPI_DISABLE();                                    \
00193   } while(0)
00194 
00195 /* Read status of the CC2420 */
00196 #define CC2420_GET_STATUS(s)                       \
00197   do {                                          \
00198     CC2420_SPI_ENABLE();                        \
00199     SPI_WRITE(CC2420_SNOP);                     \
00200     s = SPI_RXBUF;                              \
00201     CC2420_SPI_DISABLE();                       \
00202   } while (0)
00203 
00204 #endif /* __CC2420_H__ */