-
Notifications
You must be signed in to change notification settings - Fork 0
/
nRF24L01.c
302 lines (289 loc) · 9.22 KB
/
nRF24L01.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "stm8s.h"
#include "my_board.h"
#include "nRF24L01.h"
#include "uart2_user.h"
/*******************************************************************************
Initialization SPI
SCK clock = OSC / 4
******************************************************************************/
extern void nRF24L01_Pin_Conf(void)
{/* Enable SPI Clock*/
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, ENABLE);
/*Set MOSI, MISO, SCK at high level */
GPIO_ExternalPullUpConfig(GPIOC, (GPIO_Pin_TypeDef)(GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7), ENABLE);
SPI_DeInit(); //RESET
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_4,
SPI_MODE_MASTER,
SPI_CLOCKPOLARITY_LOW,
SPI_CLOCKPHASE_1EDGE,
SPI_DATADIRECTION_2LINES_FULLDUPLEX,
SPI_NSS_SOFT, 0x07);
SPI_Cmd(ENABLE);
/*For RF24 module. */
//GPIO_DeInit(RF24_GPIO_PORT);
//GPIO_DeInit(GPIOG); // reset
GPIO_Init(GPIOG, RF24_IRQ_GPIO_PINS, GPIO_MODE_IN_PU_NO_IT);// IRQ input active low
GPIO_Init(RF24_GPIO_PORT, RF24_CSN_GPIO_PINS, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(RF24_GPIO_PORT, RF24_CE_GPIO_PINS, GPIO_MODE_OUT_PP_HIGH_FAST);
}
/*******************************************************************************
RF24 emptied out RX FIFO , TX FIFO
******************************************************************************/
extern void flush_rx(void)
{
LOW_CSN;
delay_us(1);
u8 tatus = SPI_Transfer(FLUSH_RX);
HIGH_CSN;
delay_us(1);
}
extern void flush_tx(void)
{
LOW_CSN;
delay_us(1);
u8 status = SPI_Transfer(FLUSH_TX);
HIGH_CSN;
delay_us(1);
}
/*******************************************************************************
RF24 read STATUS register to check data-RX ready
******************************************************************************/
extern u8 RF24_get_status(void)
{
LOW_CSN;
delay_us(1);
u8 status = SPI_Transfer(NOP);
HIGH_CSN;
delay_us(1);
return status;
}
/*******************************************************************************/
extern void RF24_reset_status(void)
{
u8 status = RF24_get_status();
Write_register_Single(STATUS, (status | (BIT(RX_DR) | BIT(TX_DS) | BIT(MAX_RT))));
}
/*******************************************************************************
Configure NRF24L01
NOTE: Simplex
******************************************************************************/
extern void RF24_Setup(void)
{
Delayms_TIMER4(15);
Write_register_Single(EN_AA, RF24_AA_DISABLE);//Disable auto-ack
Write_register_Single(EN_RXADDR, RF24_PIPE0_ENABLE);// Enable pipe0
Write_register_Single(SETUP_AW, RF24_AW_5_BYTE);//5 Byte RX/TX address field width
Write_register_Single(SETUP_RETR, RF24_ARC_DISABLE);// Disable auto-retransmiss
Write_register_Single(RX_PW_P0, 10);//Number of bytes in RX payload in data pipe 0
Write_register_Single(RF_SETUP, (Read_register_Single(RF_SETUP) | RF24_PWR_Max) & (~(1 << 3))); // Power Max,1Mbps,NLA on
Write_register_Single(CONFIG,
RF24_PRIM_RX|RF24_PWR_DOWN|RF24_CRC_ENABLE|RF24_CRC_2_BYTE|BIT(RX_DR)|BIT(TX_DS)|BIT(MAX_RT));
//Mode RX default,Power down,CRC 2byte, Disable IRQ
flush_rx();
flush_tx();
LOW_CE;
HIGH_CSN;
}
/*******************************************************************************
RF24 Set chanel
******************************************************************************/
extern void RF24_Chanel(u8 chanel)
{
Write_register_Single(RF_CH,chanel);
}
/*******************************************************************************
RF24 TX Address
******************************************************************************/
extern void RF24_TX_Address(u8 *buf, u8 lenght_byte)
{
Write_register_Multi(TX_ADDR, buf,lenght_byte);
}
/*******************************************************************************
RF24 RX Address
******************************************************************************/
extern void RF24_RX_Address(u8 *buf, u8 lenght_byte)
{
Write_register_Multi(RX_ADDR_P0, buf, lenght_byte);
}
/*******************************************************************************
Power Up , Power Down
******************************************************************************/
static void powerDown(void)
{
Write_register_Single(CONFIG,Read_register_Single(CONFIG)&(~BIT(PWR_UP)));
}
static void powerUp(void)
{
Write_register_Single(CONFIG, Read_register_Single(CONFIG) | BIT(PWR_UP));
}
/*******************************************************************************
into RX state
******************************************************************************/
extern void RF24_startListening(void)
{
/* Go to Standby-I first */
LOW_CE;
/* Set to reciever mode , Power up if needed */
Write_register_Single(CONFIG,Read_register_Single(CONFIG)|BIT(PWR_UP)|BIT(PRIM_RX));
RF24_reset_status();
Delayms_TIMER4(3);
flush_rx();
flush_tx();
HIGH_CE; //Go
delay_us(170);
}
extern void RF24_stopListening(void)
{
LOW_CE; //stop
powerDown();
flush_rx();
flush_tx();
}
/*******************************************************************************
******************************************************************************/
extern void RF24_write_data(u8 *buf, u8 len)
{
/* Go to Standby-I first */
LOW_CE;
/* Set to transmitter mode , Power up if needed */
Write_register_Single(CONFIG,
(Read_register_Single(CONFIG)|BIT(PWR_UP))&(~BIT(PRIM_RX)));
flush_tx();
Delayms_TIMER4(3);
Write_payload(buf,len);
HIGH_CE;
delay_us(25);// Pulse 10us to start transmission
LOW_CE;
}
/*******************************************************************************
******************************************************************************/
extern void RF24_read_data(u8 *buf, u8 len)
{
Read_payload(buf,len);
RF24_reset_status();
}
/*******************************************************************************/
extern u8 RF24_available(void)
{
//UART2_Send_Byte(Read_register_Single(0x17));
return !(Read_register_Single(FIFO_STATUS)&BIT(RX_EMPTY));
}
extern void RF24_Information(void)
{
UART2_Send_Byte(Read_register_Single(CONFIG));
UART2_Send_Byte(Read_register_Single(EN_AA));
UART2_Send_Byte(Read_register_Single(EN_RXADDR));
UART2_Send_Byte(Read_register_Single(SETUP_AW));
UART2_Send_Byte(Read_register_Single(SETUP_RETR));
UART2_Send_Byte(Read_register_Single(RF_CH)); // Channel
UART2_Send_Byte(Read_register_Single(RF_SETUP));
UART2_Send_Byte(Read_register_Single(STATUS));
UART2_Send_Byte(Read_register_Single(RX_ADDR_P0));
UART2_Send_Byte(Read_register_Single(0x17)); // FIFO status
//UART2_Send_Byte('\n');
UART2_Send_Byte('\r');
}
//_________________________________________________________LOW lEVEL
/*******************************************************************************
SPI Write and Read 1 Byte Parallely
******************************************************************************/
static u8 SPI_Transfer(u8 data)
{
while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET)
{
};
SPI_SendData(data);
while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET)
{
};
return SPI_ReceiveData();
}
/*******************************************************************************
RF24 Read multi register from 1 array.
******************************************************************************/
static void Read_register_Multi(u8 reg, u8 *buffer, u8 lenght)
{
u8 status;
LOW_CSN;
delay_us(1);
status = SPI_Transfer(R_REGISTER | (REGISTER_MASK & reg));
while (lenght--)
{
*buffer++ = SPI_Transfer(NOP);
}
HIGH_CSN;
delay_us(1);
}
/*******************************************************************************
RF24 Read 1 register
******************************************************************************/
static u8 Read_register_Single(u8 reg)
{
LOW_CSN;
delay_us(1);
u8 status = SPI_Transfer(R_REGISTER | (REGISTER_MASK & reg));
u8 result = SPI_Transfer(NOP);
HIGH_CSN;
delay_us(1);
return result;
}
/*******************************************************************************
RF24 write multi register to array
******************************************************************************/
static void Write_register_Multi(u8 reg, u8 *buf, u8 lenght)
{
LOW_CSN;
delay_us(1);
u8 status = SPI_Transfer(W_REGISTER | (REGISTER_MASK & reg));
while (lenght--)
{
status = SPI_Transfer(*buf++);
}
HIGH_CSN;
delay_us(1);
}
/*******************************************************************************
RF24 write 1 register
******************************************************************************/
static void Write_register_Single(u8 reg, u8 value)
{
u8 status;
LOW_CSN;
delay_us(1);
status = SPI_Transfer(W_REGISTER | (REGISTER_MASK & reg));
status = SPI_Transfer(value);
HIGH_CSN;
delay_us(1);
}
/*******************************************************************************
RF24 write to payload
******************************************************************************/
static void Write_payload(u8 *buf, u8 len)
{
u8 status;
LOW_CSN;
delay_us(1);
status = SPI_Transfer(W_TX_PAYLOAD);
while (len--)
{
SPI_Transfer(*buf++);
}
HIGH_CSN;
delay_us(1);
}
/*******************************************************************************
RF24 Reaf from payload
******************************************************************************/
static void Read_payload(u8 *buf, u8 len)
{
u8 status;
LOW_CSN;
delay_us(1);
status = SPI_Transfer(R_RX_PAYLOAD);
while (len--)
{
*buf++ = SPI_Transfer(NOP);
}
HIGH_CSN;
delay_us(1);
}