2 #include <r0ketports.h>
4 #include <badge/pinconfig.h>
6 #define RB_SPI_NRF_CS HOB_PORT(HOB_RADIO_CS),HOB_PIN(HOB_RADIO_CS)
7 #define RB_NRF_CE HOB_PORT(HOB_RADIO_CE),HOB_PIN(HOB_RADIO_CE)
8 #define RB_NRF_IRQ HOB_PORT(HOB_RADIO_IRQ),HOB_PIN(HOB_RADIO_IRQ)
10 #define RB_SPI_NRF_CS_IO HOB_IOCON(HOB_RADIO_CS)
11 #define RB_NRF_CE_IO HOB_IOCON(HOB_RADIO_CE)
12 #define RB_NRF_IRQ_IO HOB_IOCON(HOB_RADIO_IRQ)
15 #include "nrf24l01p.h"
16 #include <core/ssp/ssp.h>
17 #include <core/systick/systick.h>
19 #define DEFAULT_SPEED R_RF_SETUP_DR_2M
23 /*-----------------------------------------------------------------------*/
24 /* Transmit a byte via SPI */
25 /*-----------------------------------------------------------------------*/
26 static inline void xmit_spi(uint8_t dat
) {
27 sspSend(0, (uint8_t*) &dat
, 1);
30 static inline void rcv_spi(uint8_t *dat
) {
31 sspReceive(0, dat
, 1);
34 #define CS_LOW() gpioSetValue(RB_SPI_NRF_CS, 0)
35 #define CS_HIGH() gpioSetValue(RB_SPI_NRF_CS, 1)
36 #define CE_LOW() gpioSetValue(RB_NRF_CE, 0)
37 #define CE_HIGH() gpioSetValue(RB_NRF_CE, 1)
39 void nrf_cmd(uint8_t cmd
){
45 uint8_t nrf_cmd_status(uint8_t cmd
){
47 sspSendReceive(0, &cmd
, 1);
52 void nrf_cmd_rw_long(uint8_t* data
, int len
){
54 sspSendReceive(0,data
,len
);
59 void nrf_write_reg(const uint8_t reg
, const uint8_t val
){
61 xmit_spi(C_W_REGISTER
| reg
);
66 uint8_t nrf_read_reg(const uint8_t reg
){
69 xmit_spi(C_R_REGISTER
| reg
);
75 void nrf_read_long(const uint8_t cmd
, int len
, uint8_t* data
){
78 for(int i
=0;i
<len
;i
++)
80 sspSendReceive(0,data
,len
);
84 void nrf_read_pkt(int len
, uint8_t* data
){
86 xmit_spi(C_R_RX_PAYLOAD
);
87 sspReceive(0,data
,len
);
91 void nrf_read_pkt_crc(int len
, uint8_t* data
, uint8_t* crc
){
93 xmit_spi(C_R_RX_PAYLOAD
);
94 sspReceive(0,data
,len
);
99 void nrf_write_long(const uint8_t cmd
, int len
, const uint8_t* data
){
106 #define nrf_write_reg_long(reg, len, data) \
107 nrf_write_long(C_W_REGISTER|(reg), len, data)
110 void nrf_rcv_pkt_start(void){
112 nrf_write_reg(R_CONFIG
,
113 R_CONFIG_PRIM_RX
| // Receive mode
114 R_CONFIG_PWR_UP
| // Power on
115 R_CONFIG_EN_CRC
// CRC on, single byte
119 nrf_write_reg(R_STATUS
,0);
124 int nrf_rcv_pkt_poll(int maxsize
, uint8_t * pkt
){
128 for(int i
=0;i
<maxsize
;i
++) pkt
[i
] = 0x00; // Sanity: clear packet buffer
130 status
=nrf_cmd_status(C_NOP
);
132 if((status
& R_STATUS_RX_P_NO
) == R_STATUS_RX_FIFO_EMPTY
){
133 if( (status
& R_STATUS_RX_DR
) == R_STATUS_RX_DR
){
135 puts("FIFO empty, but RX?\r\n");
137 nrf_write_reg(R_STATUS
,R_STATUS_RX_DR
);
142 nrf_read_long(C_R_RX_PL_WID
,1,&len
);
144 nrf_write_reg(R_STATUS
,R_STATUS_RX_DR
);
145 if(len
>32 || len
==0){
146 return -2; // no packet error
150 return -1; // packet too large
153 nrf_read_pkt(len
,pkt
);
158 void nrf_rcv_pkt_end(void){
161 nrf_write_reg(R_STATUS
,R_STATUS_RX_DR
);
164 void nrf_set_rx_mac(int pipe
, int rxlen
, int maclen
, const uint8_t * mac
){
166 assert(maclen
>=1 || maclen
<=5);
167 assert(rxlen
>=1 || rxlen
<=32);
168 assert(pipe
>=0 || pipe
<=5);
173 nrf_write_reg(R_RX_PW_P0
+pipe
,rxlen
);
175 nrf_write_reg_long(R_RX_ADDR_P0
+pipe
,maclen
,mac
);
176 nrf_write_reg(R_EN_RXADDR
,
177 nrf_read_reg(R_EN_RXADDR
) | (1<<pipe
)
181 void nrf_set_tx_mac(int maclen
, const uint8_t * mac
){
183 assert(maclen
>=1 || maclen
<=5);
186 nrf_write_reg_long(R_TX_ADDR
,maclen
,mac
);
189 void nrf_disable_pipe(int pipe
){
191 assert(pipe
>=0 || pipe
<=5);
193 nrf_write_reg(R_EN_RXADDR
,
194 nrf_read_reg(R_EN_RXADDR
) & ~(1<<pipe
)
198 void nrf_set_channel(int channel
){
200 assert(channel
&~R_RF_CH_BITS
==0);
202 nrf_write_reg(R_RF_CH
, channel
);
205 void nrf_config_set(nrfconfig config
){
206 nrf_write_reg(R_SETUP_AW
,R_SETUP_AW_5
);
208 nrf_set_channel(config
->channel
);
210 for(int i
=0;i
<config
->nrmacs
;i
++){
211 nrf_write_reg(R_RX_PW_P0
+i
,config
->maclen
[i
]);
213 nrf_write_reg_long(R_RX_ADDR_P0
,5,config
->mac0
);
215 nrf_write_reg_long(R_RX_ADDR_P1
,5,config
->mac1
);
217 nrf_write_reg_long(R_RX_ADDR_P0
+i
,1,config
->mac2345
+i
-2);
221 nrf_write_reg_long(R_TX_ADDR
,5,config
->txmac
);
223 nrf_write_reg(R_EN_RXADDR
,(1<<config
->nrmacs
)-1);
226 void nrf_config_get(nrfconfig config
){
227 // nrf_write_reg(R_SETUP_AW,R_SETUP_AW_5);
229 config
->channel
=nrf_read_reg(R_RF_CH
);
231 config
->nrmacs
=nrf_read_reg(R_EN_RXADDR
);
232 if(config
->nrmacs
& R_EN_RXADDR_ERX_P5
)
234 else if(config
->nrmacs
& R_EN_RXADDR_ERX_P4
)
236 else if(config
->nrmacs
& R_EN_RXADDR_ERX_P3
)
238 else if(config
->nrmacs
& R_EN_RXADDR_ERX_P2
)
240 else if(config
->nrmacs
& R_EN_RXADDR_ERX_P1
)
247 for(int i
=0;i
<config
->nrmacs
;i
++){
248 config
->maclen
[i
]=nrf_read_reg(R_RX_PW_P0
+i
);
250 nrf_read_long(R_RX_ADDR_P0
,5,config
->mac0
);
252 nrf_read_long(R_RX_ADDR_P1
,5,config
->mac1
);
254 nrf_read_long(R_RX_ADDR_P0
+i
,1,config
->mac2345
+i
-2);
258 nrf_read_long(R_TX_ADDR
,5,config
->txmac
);
262 void nrf_set_strength(unsigned char strength
){
265 nrf_write_reg(R_RF_SETUP
,DEFAULT_SPEED
|(strength
<<1));
269 // Enable SPI correctly
270 sspInit(0, sspClockPolarity_Low
, sspClockPhase_RisingEdge
);
272 // Enable CS & CE pins
273 gpioSetDir(RB_SPI_NRF_CS
, gpioDirection_Output
);
274 gpioSetPullup(&RB_SPI_NRF_CS_IO
, gpioPullupMode_Inactive
);
275 gpioSetDir(RB_NRF_CE
, gpioDirection_Output
);
276 gpioSetPullup(&RB_NRF_CE_IO
, gpioPullupMode_PullUp
);
279 // Setup for nrf24l01+
280 // power up takes 1.5ms - 3.5ms (depending on crystal)
282 nrf_write_reg(R_CONFIG
,
283 R_CONFIG_PRIM_RX
| // Receive mode
284 R_CONFIG_PWR_UP
| // Power on
285 R_CONFIG_EN_CRC
// CRC on, single byte
288 nrf_write_reg(R_EN_AA
, 0); // Disable Enhanced ShockBurst;
290 // Set speed / strength
291 nrf_write_reg(R_RF_SETUP
,DEFAULT_SPEED
|R_RF_SETUP_RF_PWR_3
);
293 // Clear MAX_RT, just in case.
294 nrf_write_reg(R_STATUS
,R_STATUS_MAX_RT
);
298 nrf_write_reg(R_CONFIG
,
302 ); // Most important: no R_CONFIG_PWR_UP
306 // Enable SPI correctly
307 sspInit(0, sspClockPolarity_Low
, sspClockPhase_RisingEdge
);
309 // Enable CS & CE pins
310 gpioSetDir(RB_SPI_NRF_CS
, gpioDirection_Output
);
311 gpioSetPullup(&RB_SPI_NRF_CS_IO
, gpioPullupMode_Inactive
);
312 gpioSetDir(RB_NRF_CE
, gpioDirection_Output
);
313 gpioSetPullup(&RB_NRF_CE_IO
, gpioPullupMode_PullUp
);
316 // Setup for nrf24l01+
317 // power up takes 1.5ms - 3.5ms (depending on crystal)
320 nrf_write_reg(R_CONFIG
, R_CONFIG_PWR_UP
);
322 nrf_write_reg(R_RF_SETUP
, R_RF_SETUP_CONT_WAVE
|
323 R_RF_SETUP_PLL_LOCK
|
324 R_RF_SETUP_RF_PWR_3
);
325 nrf_write_reg(R_RF_CH
, 81);
329 void nrf_check_reset(void){
330 if(nrf_cmd_status(C_NOP
) & R_STATUS_MAX_RT
){
This page took 0.080633 seconds and 5 git commands to generate.