Contiki 3.x
mrf24j40.h
Go to the documentation of this file.
1 /*
2  * Contiki SeedEye Platform project
3  *
4  * Copyright (c) 2012,
5  * Scuola Superiore Sant'Anna (http://www.sssup.it) and
6  * Consorzio Nazionale Interuniversitario per le Telecomunicazioni
7  * (http://www.cnit.it).
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the Institute nor the names of its contributors
20  * may be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 /**
38  * \addtogroup SeedEye
39  * @{ */
40 
41 /**
42  * \defgroup mrf24j40 MRF24J40 Driver
43  *
44  * @{
45  */
46 
47 /**
48  * \file mrf24j40.h
49  * \brief MRF24J40 Driver
50  * \author Giovanni Pellerano <giovanni.pellerano@evilaliv3.org>
51  * \date 2012-03-21
52  */
53 
54 #ifndef MRF24J40_H_
55 #define MRF24J40_H_
56 
57 #include <pic32_irq.h>
58 #include <pic32_spi.h>
59 
60 extern const struct radio_driver mrf24j40_driver;
61 
62 #define MRF24J40_DEFAULT_CHANNEL 11
63 
64 #define MRF24J40_TX_ERR_NONE 0
65 #define MRF24J40_TX_ERR_NOTSPECIFIED 1
66 #define MRF24J40_TX_ERR_COLLISION 2
67 #define MRF24J40_TX_ERR_MAXRETRY 3
68 #define MRF24J40_TX_WAIT 4
69 
70 /* Functions prototypes */
71 void mrf24j40_set_channel(uint16_t ch);
72 void mrf24j40_set_panid(uint16_t id);
73 void mrf24j40_set_short_mac_addr(uint16_t addr);
74 void mrf24j40_set_extended_mac_addr(uint64_t addr);
75 void mrf24j40_get_short_mac_addr(uint16_t * addr);
76 void mrf24j40_get_extended_mac_addr(uint64_t * addr);
77 void mrf24j40_set_tx_power(uint8_t pwr);
78 void mrf24j40_set_csma_par(uint8_t be, uint8_t nb);
79 uint8_t mrf24j40_get_status(void);
80 uint8_t mrf24j40_get_rssi(void);
81 uint8_t mrf24j40_get_last_rssi(void);
82 uint8_t mrf24j40_get_last_lqi(void);
83 int32_t mrf24j40_set_txfifo(const uint8_t * buf, uint8_t buf_len);
84 int32_t mrf24j40_get_rxfifo(uint8_t * buf, uint8_t buf_len);
85 
86 /* Long address registers */
87 #define MRF24J40_RFCON0 (0x200)
88 #define MRF24J40_RFCON1 (0x201)
89 #define MRF24J40_RFCON2 (0x202)
90 #define MRF24J40_RFCON3 (0x203)
91 #define MRF24J40_RFCON5 (0x205)
92 #define MRF24J40_RFCON6 (0x206)
93 #define MRF24J40_RFCON7 (0x207)
94 #define MRF24J40_RFCON8 (0x208)
95 #define MRF24J40_SPCAL0 (0x209)
96 #define MRF24J40_SPCAL1 (0x20A)
97 #define MRF24J40_SPCAL2 (0x20B)
98 #define MRF24J40_RFSTATE (0x20F)
99 #define MRF24J40_RSSI (0x210)
100 #define MRF24J40_SLPCON0 (0x211)
101 #define MRF24J40_SLPCON1 (0x220)
102 #define MRF24J40_WAKETIMEL (0x222)
103 #define MRF24J40_WAKETIMEH (0x223)
104 #define MRF24J40_REMCNTL (0x224)
105 #define MRF24J40_REMCNTH (0x225)
106 #define MRF24J40_MAINCNT0 (0x226)
107 #define MRF24J40_MAINCNT1 (0x227)
108 #define MRF24J40_MAINCNT2 (0x228)
109 #define MRF24J40_MAINCNT3 (0x229)
110 #define MRF24J40_TESTMODE (0x22F)
111 #define MRF24J40_NORMAL_TX_FIFO (0x000)
112 #define MRF24J40_BEACON_TX_FIFO (0x080)
113 #define MRF24J40_GTS1_TX_FIFO (0x100)
114 #define MRF24J40_GTS2_TX_FIFO (0x180)
115 #define MRF24J40_RX_FIFO (0x300)
116 #define MRF24J40_SECURITY_FIFO (0x280)
117 #define MRF24J40_UPNONCE0 (0x240)
118 
119 /* Short address registers */
120 #define MRF24J40_RXMCR (0x00)
121 #define MRF24J40_PANIDL (0x01)
122 #define MRF24J40_PANIDH (0x02)
123 #define MRF24J40_SADRL (0x03)
124 #define MRF24J40_SADRH (0x04)
125 #define MRF24J40_EADR0 (0x05)
126 #define MRF24J40_EADR1 (0x06)
127 #define MRF24J40_EADR2 (0x07)
128 #define MRF24J40_EADR3 (0x08)
129 #define MRF24J40_EADR4 (0x09)
130 #define MRF24J40_EADR5 (0x0A)
131 #define MRF24J40_EADR6 (0x0B)
132 #define MRF24J40_EADR7 (0x0C)
133 #define MRF24J40_RXFLUSH (0x0D)
134 #define MRF24J40_ORDER (0x10)
135 #define MRF24J40_TXMCR (0x11)
136 #define MRF24J40_ACKTMOUT (0x12)
137 #define MRF24J40_ESLOTG1 (0x13)
138 #define MRF24J40_SYMTICKL (0x14)
139 #define MRF24J40_SYMTICKH (0x15)
140 #define MRF24J40_PACON0 (0x16)
141 #define MRF24J40_PACON1 (0x17)
142 #define MRF24J40_PACON2 (0x18)
143 #define MRF24J40_TXBCON0 (0x1A)
144 #define MRF24J40_TXNCON (0x1B)
145 #define MRF24J40_TXG1CON (0x1C)
146 #define MRF24J40_TXG2CON (0x1D)
147 #define MRF24J40_ESLOTG23 (0x1E)
148 #define MRF24J40_ESLOTG45 (0x1F)
149 #define MRF24J40_ESLOTG87 (0x20)
150 #define MRF24J40_TXPEND (0x21)
151 #define MRF24J40_WAKECON (0x22)
152 #define MRF24J40_FRMOFFSET (0x23)
153 #define MRF24J40_TXSTAT (0x24)
154 #define MRF24J40_TXBCON1 (0x25)
155 #define MRF24J40_GATECLK (0x26)
156 #define MRF24J40_TXTIME (0x27)
157 #define MRF24J40_HSYMTMRL (0x28)
158 #define MRF24J40_HSYMTMRH (0x29)
159 #define MRF24J40_SOFTRST (0x2A)
160 #define MRF24J40_SECCON0 (0x2C)
161 #define MRF24J40_SECCON1 (0x2D)
162 #define MRF24J40_TXSTBL (0x2E)
163 #define MRF24J40_RXSR (0x30)
164 #define MRF24J40_INTSTAT (0x31)
165 #define MRF24J40_INTCON (0x32)
166 #define MRF24J40_GPIO (0x33)
167 #define MRF24J40_TRISGPIO (0x34)
168 #define MRF24J40_SLPACK (0x35)
169 #define MRF24J40_RFCTL (0x36)
170 #define MRF24J40_SECCR2 (0x37)
171 #define MRF24J40_BBREG0 (0x38)
172 #define MRF24J40_BBREG1 (0x39)
173 #define MRF24J40_BBREG2 (0x3A)
174 #define MRF24J40_BBREG3 (0x3B)
175 #define MRF24J40_BBREG4 (0x3C)
176 #define MRF24J40_BBREG6 (0x3E)
177 #define MRF24J40_CCAEDTH (0x3F)
178 
179 /*
180  * TX power setting generation:
181  * tx_pwr_set(large_val, small_val) computes the value for the RFCON3 register.
182  * Examples:
183  * - if we want to set tx power to -11,2 dB then:
184  * mrf24j40_tx_pwr_set(#define MRF24J40_PWR_H_MINUS_10dB, #define MRF24J40_PWR_L_MINUS_1.2dB).
185  */
186 
187 #define MRF24J40_PWR_H_MINUS_30dB (0b11)
188 #define MRF24J40_PWR_H_MINUS_20dB (0b10)
189 #define MRF24J40_PWR_H_MINUS_10dB (0b01)
190 #define MRF24J40_PWR_H_0dB (0b00)
191 
192 #define MRF24J40_PWR_L_MINUS_6_3dB (0b111)
193 #define MRF24J40_PWR_L_MINUS_4_9dB (0b110)
194 #define MRF24J40_PWR_L_MINUS_3_7dB (0b101)
195 #define MRF24J40_PWR_L_MINUS_2_8dB (0b100)
196 #define MRF24J40_PWR_L_MINUS_1_9dB (0b011)
197 #define MRF24J40_PWR_L_MINUS_1_2dB (0b010)
198 #define MRF24J40_PWR_L_MINUS_0_5dB (0b001)
199 #define MRF24J40_PWR_L_0dB (0b000)
200 
201 #define MRF24J40_TX_PWR_SET(large_val, small_val) ((large_val << 6) | (small_val << 3))
202 
203 typedef union _TX_status {
204  uint8_t val;
205  struct TX_bits {
206  uint8_t TXNSTAT:1;
207  uint8_t TXG1STAT:1;
208  uint8_t TXG2STAT:1;
209  uint8_t TXG1FNT:1;
210  uint8_t TXG2FNT:1;
211  uint8_t CCAFAIL:1;
212  uint8_t TXNRETRY:2;
213  } bits;
214 } TX_status;
215 
216 typedef union _INT_status {
217  uint8_t val;
218  struct INT_bits {
219  uint8_t TXNIF:1;
220  uint8_t TXG1IF:1;
221  uint8_t TXG2IF:1;
222  uint8_t RXIF:1;
223  uint8_t SECIF:1;
224  uint8_t HSYMTMRIF:1;
225  uint8_t WAKEIF:1;
226  uint8_t SLPIF:1;
227  } bits;
228 } INT_status;
229 
230 #endif /* MRF24J40_H_ */
231 
232 /** @} */
233 /** @} */
void mrf24j40_set_short_mac_addr(uint16_t addr)
Store short MAC address.
Definition: mrf24j40.c:228
uint8_t mrf24j40_get_rssi(void)
Get the RSSI.
Definition: mrf24j40.c:313
SPI interface for PIC32MX (pic32mx795f512l)
void mrf24j40_set_tx_power(uint8_t pwr)
Set TX power.
Definition: mrf24j40.c:288
INTERRUPT interface for PIC32MX (pic32mx795f512l)
void mrf24j40_set_channel(uint16_t ch)
Set the channel.
Definition: mrf24j40.c:203
The structure of a device driver for a radio in Contiki.
Definition: radio.h:225
uint8_t mrf24j40_get_last_rssi(void)
Get the last read RSSI.
Definition: mrf24j40.c:356
uint8_t mrf24j40_get_last_lqi(void)
Get the last read LQI.
Definition: mrf24j40.c:367
void mrf24j40_set_panid(uint16_t id)
Store MAC PAN ID.
Definition: mrf24j40.c:216
void mrf24j40_get_extended_mac_addr(uint64_t *addr)
Gets extended MAC address.
Definition: mrf24j40.c:270
void mrf24j40_set_extended_mac_addr(uint64_t addr)
Store extended MAC address.
Definition: mrf24j40.c:240
void mrf24j40_get_short_mac_addr(uint16_t *addr)
Get short MAC address.
Definition: mrf24j40.c:258
int32_t mrf24j40_get_rxfifo(uint8_t *buf, uint8_t buf_len)
Get message.
Definition: mrf24j40.c:404
int32_t mrf24j40_set_txfifo(const uint8_t *buf, uint8_t buf_len)
Store message.
Definition: mrf24j40.c:379
uint8_t mrf24j40_get_status(void)
Get radio status.
Definition: mrf24j40.c:299