Contiki 2.5
cc2420.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * $Id: cc2420.h,v 1.14 2010/12/16 22:39:50 adamdunkels Exp $
32  */
33 
34 /**
35  * \file
36  * CC2420 driver header file
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  * Joakim Eriksson <joakime@sics.se>
40  */
41 
42 #ifndef __CC2420_H__
43 #define __CC2420_H__
44 
45 #include "contiki.h"
46 #include "dev/spi.h"
47 #include "dev/radio.h"
48 #include "dev/cc2420_const.h"
49 
50 int cc2420_init(void);
51 
52 #define CC2420_MAX_PACKET_LEN 127
53 
54 int cc2420_set_channel(int channel);
55 int cc2420_get_channel(void);
56 
57 void cc2420_set_pan_addr(unsigned pan,
58  unsigned addr,
59  const uint8_t *ieee_addr);
60 
61 extern signed char cc2420_last_rssi;
62 extern uint8_t cc2420_last_correlation;
63 
64 int cc2420_rssi(void);
65 
66 extern const struct radio_driver cc2420_driver;
67 
68 /**
69  * \param power Between 1 and 31.
70  */
71 void cc2420_set_txpower(uint8_t power);
72 int cc2420_get_txpower(void);
73 #define CC2420_TXPOWER_MAX 31
74 #define CC2420_TXPOWER_MIN 0
75 
76 /**
77  * Interrupt function, called from the simple-cc2420-arch driver.
78  *
79  */
80 int cc2420_interrupt(void);
81 
82 /* XXX hack: these will be made as Chameleon packet attributes */
83 extern rtimer_clock_t cc2420_time_of_arrival,
84  cc2420_time_of_departure;
85 extern int cc2420_authority_level_of_sender;
86 
87 int cc2420_on(void);
88 int cc2420_off(void);
89 
90 void cc2420_set_cca_threshold(int value);
91 
92 /************************************************************************/
93 /* Additional SPI Macros for the CC2420 */
94 /************************************************************************/
95 /* Send a strobe to the CC2420 */
96 #define CC2420_STROBE(s) \
97  do { \
98  CC2420_SPI_ENABLE(); \
99  SPI_WRITE(s); \
100  CC2420_SPI_DISABLE(); \
101  } while (0)
102 
103 /* Write to a register in the CC2420 */
104 /* Note: the SPI_WRITE(0) seems to be needed for getting the */
105 /* write reg working on the Z1 / MSP430X platform */
106 #define CC2420_WRITE_REG(adr,data) \
107  do { \
108  CC2420_SPI_ENABLE(); \
109  SPI_WRITE_FAST(adr); \
110  SPI_WRITE_FAST((uint8_t)((data) >> 8)); \
111  SPI_WRITE_FAST((uint8_t)(data & 0xff)); \
112  SPI_WAITFORTx_ENDED(); \
113  SPI_WRITE(0); \
114  CC2420_SPI_DISABLE(); \
115  } while(0)
116 
117 /* Read a register in the CC2420 */
118 #define CC2420_READ_REG(adr,data) \
119  do { \
120  CC2420_SPI_ENABLE(); \
121  SPI_WRITE(adr | 0x40); \
122  data = (uint8_t)SPI_RXBUF; \
123  SPI_TXBUF = 0; \
124  SPI_WAITFOREORx(); \
125  data = SPI_RXBUF << 8; \
126  SPI_TXBUF = 0; \
127  SPI_WAITFOREORx(); \
128  data |= SPI_RXBUF; \
129  CC2420_SPI_DISABLE(); \
130  } while(0)
131 
132 #define CC2420_READ_FIFO_BYTE(data) \
133  do { \
134  CC2420_SPI_ENABLE(); \
135  SPI_WRITE(CC2420_RXFIFO | 0x40); \
136  (void)SPI_RXBUF; \
137  SPI_READ(data); \
138  clock_delay(1); \
139  CC2420_SPI_DISABLE(); \
140  } while(0)
141 
142 #define CC2420_READ_FIFO_BUF(buffer,count) \
143  do { \
144  uint8_t i; \
145  CC2420_SPI_ENABLE(); \
146  SPI_WRITE(CC2420_RXFIFO | 0x40); \
147  (void)SPI_RXBUF; \
148  for(i = 0; i < (count); i++) { \
149  SPI_READ(((uint8_t *)(buffer))[i]); \
150  } \
151  clock_delay(1); \
152  CC2420_SPI_DISABLE(); \
153  } while(0)
154 
155 #define CC2420_WRITE_FIFO_BUF(buffer,count) \
156  do { \
157  uint8_t i; \
158  CC2420_SPI_ENABLE(); \
159  SPI_WRITE_FAST(CC2420_TXFIFO); \
160  for(i = 0; i < (count); i++) { \
161  SPI_WRITE_FAST(((uint8_t *)(buffer))[i]); \
162  } \
163  SPI_WAITFORTx_ENDED(); \
164  CC2420_SPI_DISABLE(); \
165  } while(0)
166 
167 /* Write to RAM in the CC2420 */
168 #define CC2420_WRITE_RAM(buffer,adr,count) \
169  do { \
170  uint8_t i; \
171  CC2420_SPI_ENABLE(); \
172  SPI_WRITE_FAST(0x80 | ((adr) & 0x7f)); \
173  SPI_WRITE_FAST(((adr) >> 1) & 0xc0); \
174  for(i = 0; i < (count); i++) { \
175  SPI_WRITE_FAST(((uint8_t*)(buffer))[i]); \
176  } \
177  SPI_WAITFORTx_ENDED(); \
178  CC2420_SPI_DISABLE(); \
179  } while(0)
180 
181 /* Read from RAM in the CC2420 */
182 #define CC2420_READ_RAM(buffer,adr,count) \
183  do { \
184  uint8_t i; \
185  CC2420_SPI_ENABLE(); \
186  SPI_WRITE(0x80 | ((adr) & 0x7f)); \
187  SPI_WRITE((((adr) >> 1) & 0xc0) | 0x20); \
188  SPI_RXBUF; \
189  for(i = 0; i < (count); i++) { \
190  SPI_READ(((uint8_t*)(buffer))[i]); \
191  } \
192  CC2420_SPI_DISABLE(); \
193  } while(0)
194 
195 /* Read status of the CC2420 */
196 #define CC2420_GET_STATUS(s) \
197  do { \
198  CC2420_SPI_ENABLE(); \
199  SPI_WRITE(CC2420_SNOP); \
200  s = SPI_RXBUF; \
201  CC2420_SPI_DISABLE(); \
202  } while (0)
203 
204 #endif /* __CC2420_H__ */