Contiki 2.6

cc2520.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, 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 
00032 /**
00033  * \file
00034  *         CC2520 driver header file
00035  * \author
00036  *         Adam Dunkels <adam@sics.se>
00037  *         Joakim Eriksson <joakime@sics.se>
00038  */
00039 
00040 #ifndef __CC2520_H__
00041 #define __CC2520_H__
00042 
00043 #include "contiki.h"
00044 #include "dev/spi.h"
00045 #include "dev/radio.h"
00046 #include "dev/cc2520_const.h"
00047 
00048 int cc2520_init(void);
00049 
00050 #define CC2520_MAX_PACKET_LEN      127
00051 
00052 int cc2520_set_channel(int channel);
00053 int cc2520_get_channel(void);
00054 
00055 void cc2520_set_pan_addr(unsigned pan,
00056                          unsigned addr,
00057                          const uint8_t *ieee_addr);
00058 
00059 extern signed char cc2520_last_rssi;
00060 extern uint8_t cc2520_last_correlation;
00061 
00062 int cc2520_rssi(void);
00063 
00064 extern const struct radio_driver cc2520_driver;
00065 
00066 /**
00067  * \param power Between 1 and 31.
00068  */
00069 void cc2520_set_txpower(uint8_t power);
00070 int cc2520_get_txpower(void);
00071 #define CC2520_TXPOWER_MAX  31
00072 #define CC2520_TXPOWER_MIN   0
00073 
00074 /**
00075  * Interrupt function, called from the simple-cc2520-arch driver.
00076  *
00077  */
00078 int cc2520_interrupt(void);
00079 
00080 /* XXX hack: these will be made as Chameleon packet attributes */
00081 extern rtimer_clock_t cc2520_time_of_arrival,
00082   cc2520_time_of_departure;
00083 extern int cc2520_authority_level_of_sender;
00084 
00085 int cc2520_on(void);
00086 int cc2520_off(void);
00087 
00088 void cc2520_set_cca_threshold(int value);
00089 
00090 /************************************************************************/
00091 /* Additional SPI Macros for the CC2520 */
00092 /************************************************************************/
00093 /* Send a strobe to the CC2520 */
00094 #define CC2520_STROBE(s)                                \
00095   do {                                                  \
00096     CC2520_SPI_ENABLE();                                \
00097     SPI_WRITE(s);                                       \
00098     CC2520_SPI_DISABLE();                               \
00099   } while (0)
00100 
00101 /* Write to a register in the CC2520                         */
00102 /* Note: the SPI_WRITE(0) seems to be needed for getting the */
00103 /* write reg working on the Z1 / MSP430X platform            */
00104 #define CC2520_WRITE_REG(adr,data)                                      \
00105   do {                                                                  \
00106     CC2520_SPI_ENABLE();                                                \
00107     SPI_WRITE_FAST(CC2520_INS_MEMWR | ((adr>>8)&0xFF));                 \
00108     SPI_WRITE_FAST(adr & 0xff);                                         \
00109     SPI_WRITE_FAST((uint8_t) data);                                     \
00110     SPI_WAITFORTx_ENDED();                                              \
00111     CC2520_SPI_DISABLE();                                               \
00112   } while(0)
00113 
00114 
00115 /* Read a register in the CC2520 */
00116 #define CC2520_READ_REG(adr,data)                                       \
00117   do {                                                                  \
00118     CC2520_SPI_ENABLE();                                                \
00119     SPI_WRITE((CC2520_INS_MEMRD | ((adr>>8)&0xFF)));                    \
00120     SPI_WRITE((adr & 0xFF));                                            \
00121     SPI_READ(data);                                                     \
00122     CC2520_SPI_DISABLE();                                               \
00123   } while(0)
00124 
00125 #define CC2520_READ_FIFO_BYTE(data)                                     \
00126   do {                                                                  \
00127     CC2520_SPI_ENABLE();                                                \
00128     SPI_WRITE(CC2520_INS_RXBUF);                                        \
00129     (void)SPI_RXBUF;                                                    \
00130     SPI_READ(data);                                                     \
00131     clock_delay(1);                                                     \
00132     CC2520_SPI_DISABLE();                                               \
00133   } while(0)
00134 
00135 #define CC2520_READ_FIFO_BUF(buffer,count)                              \
00136   do {                                                                  \
00137     uint8_t i;                                                          \
00138     CC2520_SPI_ENABLE();                                                \
00139     SPI_WRITE(CC2520_INS_RXBUF);                                        \
00140     (void)SPI_RXBUF;                                                    \
00141     for(i = 0; i < (count); i++) {                                      \
00142       SPI_READ(((uint8_t *)(buffer))[i]);                               \
00143     }                                                                   \
00144     clock_delay(1);                                                     \
00145     CC2520_SPI_DISABLE();                                               \
00146   } while(0)
00147 
00148 #define CC2520_WRITE_FIFO_BUF(buffer,count)                             \
00149   do {                                                                  \
00150     uint8_t i;                                                          \
00151     CC2520_SPI_ENABLE();                                                \
00152     SPI_WRITE_FAST(CC2520_INS_TXBUF);                                   \
00153     for(i = 0; i < (count); i++) {                                      \
00154       SPI_WRITE_FAST(((uint8_t *)(buffer))[i]);                         \
00155       SPI_WAITFORTxREADY();                                             \
00156     }                                                                   \
00157     SPI_WAITFORTx_ENDED();                                              \
00158     CC2520_SPI_DISABLE();                                               \
00159   } while(0)
00160 
00161 /* Write to RAM in the CC2520 */
00162 #define CC2520_WRITE_RAM(buffer,adr,count)                              \
00163   do {                                                                  \
00164     uint8_t i;                                                          \
00165     CC2520_SPI_ENABLE();                                                \
00166     SPI_WRITE_FAST(CC2520_INS_MEMWR | (((adr)>>8) & 0xFF));             \
00167     SPI_WRITE_FAST(((adr) & 0xFF));                                     \
00168     for(i = 0; i < (count); i++) {                                      \
00169       SPI_WRITE_FAST(((uint8_t*)(buffer))[i]);                          \
00170     }                                                                   \
00171     SPI_WAITFORTx_ENDED();                                              \
00172     CC2520_SPI_DISABLE();                                               \
00173   } while(0)
00174 
00175 /* Read from RAM in the CC2520 */
00176 #define CC2520_READ_RAM(buffer,adr,count)                               \
00177   do {                                                                  \
00178     uint8_t i;                                                          \
00179     CC2520_SPI_ENABLE();                                                \
00180     SPI_WRITE(CC2520_INS_MEMRD | (((adr)>>8) & 0xFF));                  \
00181     SPI_WRITE(((adr) & 0xFF));                                          \
00182     SPI_RXBUF;                                                          \
00183     for(i = 0; i < (count); i++) {                                      \
00184       SPI_READ(((uint8_t*)(buffer))[i]);                                \
00185     }                                                                   \
00186     CC2520_SPI_DISABLE();                                               \
00187   } while(0)
00188 
00189 /* Read status of the CC2520 */
00190 #define CC2520_GET_STATUS(s)                                            \
00191   do {                                                                  \
00192     CC2520_SPI_ENABLE();                                                \
00193     SPI_WRITE(CC2520_INS_SNOP);                                         \
00194     s = SPI_RXBUF;                                                      \
00195     CC2520_SPI_DISABLE();                                               \
00196   } while (0)
00197 
00198 #endif /* __CC2520_H__ */