|
|
@@ -17,9 +17,12 @@
|
|
17
|
17
|
extern volatile bool nrfInterruptRaised;
|
|
18
|
18
|
|
|
19
|
19
|
void Print_Register_Contents(uint8_t address);
|
|
|
20
|
+
|
|
20
|
21
|
void Send_TX_Flush_Command(void);
|
|
|
22
|
+void Send_RX_Flush_Command(void);
|
|
21
|
23
|
|
|
22
|
|
-static void Write_Two_Bytes(uint8_t byte1, uint8_t byte2);
|
|
|
24
|
+static uint8_t Write_One_Byte(uint8_t byte1);
|
|
|
25
|
+static uint8_t Write_Two_Bytes(uint8_t byte1, uint8_t byte2);
|
|
23
|
26
|
static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length);
|
|
24
|
27
|
|
|
25
|
28
|
/* Startup and initial configuration of the NRF24L01 */
|
|
|
@@ -42,6 +45,10 @@ void Initialize_NRF24L01(void)
|
|
42
|
45
|
_delay_ms(2);
|
|
43
|
46
|
|
|
44
|
47
|
/* The NRF24L01 is now in the mode Standby-I */
|
|
|
48
|
+
|
|
|
49
|
+ /* Flush the FIFOs */
|
|
|
50
|
+ Send_TX_Flush_Command();
|
|
|
51
|
+ Send_RX_Flush_Command();
|
|
45
|
52
|
}
|
|
46
|
53
|
|
|
47
|
54
|
void Set_NRF24L01_Pins(void)
|
|
|
@@ -64,38 +71,18 @@ void Set_NRF24L01_Pins(void)
|
|
64
|
71
|
|
|
65
|
72
|
void Configure_Transmission(uint8_t moduleId)
|
|
66
|
73
|
{
|
|
67
|
|
- DYNPD_REGISTER dyndpRegisterContents = {.byte = 0x0};
|
|
68
|
74
|
SETUP_RETR_REGISTER setupRetrRegisterContents = {.byte = 0x0};
|
|
|
75
|
+ EN_RXADDR_REGISTER enableRxAddressesRegisterContents = {.byte = 0x0};
|
|
|
76
|
+ RX_PW_Pn_REGISTER rxPwPnRegisterContents = {.byte = 0x0};
|
|
|
77
|
+ EN_AA_REGISTER enAaRegister = {.byte = 0x0};
|
|
69
|
78
|
|
|
70
|
79
|
uint8_t txAddress[5] = {0xB3, 0xB3, 0xB3, 0xB3, 0x00};
|
|
71
|
|
- uint8_t rx0Address[5] = {0xB3, 0xB3, 0xB3, 0xB3, 0x00};
|
|
72
|
|
- /*
|
|
73
|
|
- * - Length of CRC (CRCO in CONFIG)
|
|
74
|
|
- * - Enable auto acknowledgment (EN_AA)
|
|
75
|
|
- * -> Register already set correctly after reset
|
|
76
|
|
- * - Enable data pipes (EN_RXADDR)?
|
|
77
|
|
- * -> Two pipes are already enabled on reset
|
|
78
|
|
- * - Set up address width (SETUP_AW)
|
|
79
|
|
- * -> 3 bytes
|
|
80
|
|
- * - Automatic Retransmission (SETUP_RETR)
|
|
81
|
|
- * -> ARD = 0b0000
|
|
82
|
|
- * -> 3 retransmits -> ARC = 0b0011
|
|
83
|
|
- * -> Register already set correctly after reset
|
|
84
|
|
- * - RF Channel (RF_CH)
|
|
85
|
|
- * -> RF_CH = 0b1010000
|
|
86
|
|
- * - RF Setup (RF_SETUP)
|
|
87
|
|
- * -> first use reset values, can be fine tuned later
|
|
88
|
|
- * - Enable dynamic payload length (DYNPD) -> command activate + 0x73, then set bits in FEATURE?
|
|
89
|
|
- */
|
|
90
|
|
-
|
|
91
|
|
- /* Set the address width to 3 bytes */
|
|
92
|
|
- //Write_NRF_Register(0x03, 0x1);
|
|
|
80
|
+ uint8_t rx1Address[5] = {0xB3, 0xB3, 0xB3, 0xB3, 0x00};
|
|
93
|
81
|
|
|
94
|
82
|
/* Set the frequency to 1450 MHz */
|
|
95
|
83
|
Write_NRF_Register(RF_CH_ADDRESS, 0x32);
|
|
96
|
84
|
|
|
97
|
|
-
|
|
98
|
|
- /* */
|
|
|
85
|
+ /* Set up the auto retries */
|
|
99
|
86
|
setupRetrRegisterContents.bits.ARC = 0x3;
|
|
100
|
87
|
setupRetrRegisterContents.bits.ARD = 0xF;
|
|
101
|
88
|
Write_NRF_Register(SETUP_RETR_ADDRESS, setupRetrRegisterContents.byte);
|
|
|
@@ -103,20 +90,50 @@ void Configure_Transmission(uint8_t moduleId)
|
|
103
|
90
|
/* Set the TX address */
|
|
104
|
91
|
Set_TX_Address(txAddress, MAX_ADDRESS_LENGTH);
|
|
105
|
92
|
|
|
106
|
|
- /* Set the RX_P0 address to the one of the base station to receive acks */
|
|
107
|
|
- Set_RX_P0_Address(rx0Address, MAX_ADDRESS_LENGTH);
|
|
|
93
|
+ /* Set the address of the RX pipe 0 to the one of the base station to receive acks */
|
|
|
94
|
+ Set_RX_P0_Address(txAddress, MAX_ADDRESS_LENGTH);
|
|
|
95
|
+
|
|
|
96
|
+ /* Set the address of the RX pipe 1 to the own address to receive messages */
|
|
|
97
|
+ rx1Address[4] = moduleId; // The last byte of the address corresponds to the Id set by the pin programming
|
|
|
98
|
+ Set_RX_P1_Address(rx1Address, MAX_ADDRESS_LENGTH);
|
|
|
99
|
+
|
|
|
100
|
+ /* Enable the rx addresses for pipe 0 and pipe 1*/
|
|
|
101
|
+ enableRxAddressesRegisterContents.bits.ERX_P0 = 1;
|
|
|
102
|
+ enableRxAddressesRegisterContents.bits.ERX_P1 = 1;
|
|
|
103
|
+ Write_NRF_Register(EN_RXADDR_ADDRESS, enableRxAddressesRegisterContents.byte);
|
|
|
104
|
+
|
|
|
105
|
+ /* Set the payload witth for pipe 1 */
|
|
|
106
|
+ rxPwPnRegisterContents.bits.RX_PW_Pn = 32;
|
|
|
107
|
+ Write_NRF_Register(RX_PW_P1_ADDRESS, rxPwPnRegisterContents.byte);
|
|
|
108
|
+
|
|
|
109
|
+ rxPwPnRegisterContents.bits.RX_PW_Pn = 0;
|
|
|
110
|
+ Write_NRF_Register(RX_PW_P0_ADDRESS, rxPwPnRegisterContents.byte); // auto-ack pipe
|
|
|
111
|
+ Write_NRF_Register(RX_PW_P2_ADDRESS, rxPwPnRegisterContents.byte); // not used
|
|
|
112
|
+ Write_NRF_Register(RX_PW_P3_ADDRESS, rxPwPnRegisterContents.byte); // not used
|
|
|
113
|
+ Write_NRF_Register(RX_PW_P4_ADDRESS, rxPwPnRegisterContents.byte); // not used
|
|
|
114
|
+ Write_NRF_Register(RX_PW_P5_ADDRESS, rxPwPnRegisterContents.byte); // not used
|
|
|
115
|
+
|
|
|
116
|
+ /* Enable auto acknowledge for pipe 1 */
|
|
|
117
|
+ enAaRegister.bits.ENAA_P0 = 1;
|
|
|
118
|
+ enAaRegister.bits.ENAA_P1 = 1;
|
|
|
119
|
+ enAaRegister.bits.ENAA_P2 = 1;
|
|
|
120
|
+ enAaRegister.bits.ENAA_P3 = 1;
|
|
|
121
|
+ enAaRegister.bits.ENAA_P4 = 1;
|
|
|
122
|
+ enAaRegister.bits.ENAA_P5 = 1;
|
|
|
123
|
+ Write_NRF_Register(EN_AA_ADDRESS, enAaRegister.byte);
|
|
108
|
124
|
|
|
109
|
125
|
|
|
110
|
126
|
PCMSK2 |= (1<<PCINT21); // Set the external interrupt for PD5
|
|
111
|
127
|
}
|
|
112
|
128
|
|
|
113
|
|
-void NRF24L01_Send_Message(uint8_t *buffer, uint8_t length)
|
|
|
129
|
+bool NRF24L01_Send_Message(uint8_t *buffer, uint8_t length)
|
|
114
|
130
|
{
|
|
|
131
|
+ bool success = false;
|
|
115
|
132
|
STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
|
|
116
|
133
|
|
|
117
|
134
|
if ((length > 32) || (length == 0))
|
|
118
|
135
|
{
|
|
119
|
|
- return;
|
|
|
136
|
+ return success;
|
|
120
|
137
|
}
|
|
121
|
138
|
|
|
122
|
139
|
PCICR |= (1<<PCIE2); // Enable the interrupt for the IRQ signal
|
|
|
@@ -133,7 +150,6 @@ void NRF24L01_Send_Message(uint8_t *buffer, uint8_t length)
|
|
133
|
150
|
* induced by the SPI:
|
|
134
|
151
|
* https://forum.mysensors.org/topic/10452/nrf24l01-communication-failure-root-cause-and-solution
|
|
135
|
152
|
*/
|
|
136
|
|
- LED_PORT |= (1 << LED_PIN);
|
|
137
|
153
|
|
|
138
|
154
|
statusRegisterContents.byte = Read_NRF_Status_Register();
|
|
139
|
155
|
|
|
|
@@ -142,6 +158,10 @@ void NRF24L01_Send_Message(uint8_t *buffer, uint8_t length)
|
|
142
|
158
|
{
|
|
143
|
159
|
Send_TX_Flush_Command(); /* Remove the packet from the TX FIFO as it is not done automatically */
|
|
144
|
160
|
}
|
|
|
161
|
+ else
|
|
|
162
|
+ {
|
|
|
163
|
+ success = true;
|
|
|
164
|
+ }
|
|
145
|
165
|
|
|
146
|
166
|
|
|
147
|
167
|
/* Reset the interrupts */
|
|
|
@@ -154,57 +174,81 @@ void NRF24L01_Send_Message(uint8_t *buffer, uint8_t length)
|
|
154
|
174
|
nrfInterruptRaised = false;
|
|
155
|
175
|
|
|
156
|
176
|
|
|
157
|
|
- return;
|
|
|
177
|
+ return success;
|
|
158
|
178
|
}
|
|
159
|
179
|
|
|
160
|
|
-uint8_t Read_NRF_Status_Register(void)
|
|
|
180
|
+bool NRF24L01_Receive_Message(uint8_t *buffer, uint8_t duration)
|
|
161
|
181
|
{
|
|
162
|
|
- uint8_t registerContents;
|
|
163
|
|
-
|
|
164
|
|
- SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
165
|
|
- registerContents = SPI_Transfer_Byte(0x00);
|
|
166
|
|
- SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
167
|
|
- return registerContents;
|
|
168
|
|
-}
|
|
|
182
|
+ uint8_t messageReceived = false;
|
|
|
183
|
+ CONFIG_REGISTER configRegisterContents = {.byte = 0x0};
|
|
|
184
|
+ STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
|
|
|
185
|
+ FIFO_STATUS_REGISTER fifoStatusRegisterContents = {.byte = 0x0};
|
|
169
|
186
|
|
|
|
187
|
+ // Enable the receive mode
|
|
|
188
|
+ configRegisterContents.byte = Read_NRF_Register(CONFIG_ADDRESS);
|
|
|
189
|
+ configRegisterContents.bits.PRIM_RX = 0x1;
|
|
|
190
|
+ Write_NRF_Register(CONFIG_ADDRESS, configRegisterContents.byte);
|
|
170
|
191
|
|
|
171
|
|
-/* TODO: rewrite the read register function if it is needed (remove the read operations for the 5-byte registers)*/
|
|
172
|
|
-#if 0
|
|
173
|
|
-uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents)
|
|
174
|
|
-{
|
|
175
|
|
- /* TODO: simplify this function, as the registers with more than one byte are accessed with other functions */
|
|
176
|
|
- uint8_t numberOfBytes = 0;
|
|
|
192
|
+ NRF_CE_PORT |= (1 << NRF_CE_PIN);
|
|
177
|
193
|
|
|
178
|
|
- if ((address == 0x0A) ||
|
|
179
|
|
- (address == 0x0B) ||
|
|
180
|
|
- (address == 0x10))
|
|
|
194
|
+ while ((nrfInterruptRaised == false) && (duration > 0))
|
|
181
|
195
|
{
|
|
182
|
|
- numberOfBytes = 5;
|
|
183
|
|
- }
|
|
184
|
|
- else
|
|
|
196
|
+ _delay_ms(1);
|
|
|
197
|
+ duration --;
|
|
|
198
|
+ };
|
|
|
199
|
+
|
|
|
200
|
+ if (nrfInterruptRaised == true) // check if a message was received
|
|
185
|
201
|
{
|
|
186
|
|
- numberOfBytes = 1;
|
|
|
202
|
+ /* A message was received */
|
|
|
203
|
+
|
|
|
204
|
+ LED_PORT |= (1 << LED_PIN);
|
|
|
205
|
+ statusRegisterContents.byte = Read_NRF_Status_Register();
|
|
|
206
|
+ if (statusRegisterContents.bits.RX_DR == 1)
|
|
|
207
|
+ {
|
|
|
208
|
+ fifoStatusRegisterContents.byte = Read_NRF_Status_Register();
|
|
|
209
|
+ if (fifoStatusRegisterContents.bits.RX_EMPTY != 1)
|
|
|
210
|
+ {
|
|
|
211
|
+ Read_Message_From_RX_FIFO(PACKET_LENGTH, buffer); /* TODO: only possible after CE = 0? */
|
|
|
212
|
+ messageReceived = true;
|
|
|
213
|
+ }
|
|
|
214
|
+ }
|
|
|
215
|
+
|
|
|
216
|
+ nrfInterruptRaised = false;
|
|
187
|
217
|
}
|
|
188
|
218
|
|
|
189
|
|
- /* First write the address */
|
|
190
|
|
- SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
|
219
|
+ // Set the NRF to standby
|
|
|
220
|
+ NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
|
|
191
|
221
|
|
|
|
222
|
+ configRegisterContents.byte = Read_NRF_Register(CONFIG_ADDRESS);
|
|
|
223
|
+ configRegisterContents.bits.PRIM_RX = 0x0;
|
|
|
224
|
+ Write_NRF_Register(CONFIG_ADDRESS, configRegisterContents.byte);
|
|
192
|
225
|
|
|
193
|
|
- SPI_Transfer_Byte(address);
|
|
|
226
|
+ /* Reset the interrupts */
|
|
|
227
|
+ statusRegisterContents.bits.TX_DS = 1;
|
|
|
228
|
+ statusRegisterContents.bits.MAX_RT = 1;
|
|
|
229
|
+ statusRegisterContents.bits.RX_DR = 1;
|
|
|
230
|
+ Write_NRF_Register(STATUS_ADDRESS, statusRegisterContents.byte);
|
|
194
|
231
|
|
|
195
|
|
- /* Read the register bytes */
|
|
196
|
|
- for (uint8_t i = 0; i < numberOfBytes; i++)
|
|
197
|
|
- {
|
|
198
|
|
- /* Write dummy data to shift in the register content */
|
|
199
|
|
- registerContents[i] = SPI_Transfer_Byte(0x0);
|
|
200
|
|
- }
|
|
|
232
|
+ return messageReceived;
|
|
|
233
|
+}
|
|
201
|
234
|
|
|
202
|
|
- SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
|
235
|
+uint8_t Read_NRF_Status_Register(void)
|
|
|
236
|
+{
|
|
|
237
|
+ uint8_t registerContents;
|
|
203
|
238
|
|
|
204
|
|
- return numberOfBytes;
|
|
|
239
|
+ registerContents = Write_One_Byte(0x0);
|
|
|
240
|
+ return registerContents;
|
|
205
|
241
|
}
|
|
206
|
242
|
|
|
207
|
|
-#endif
|
|
|
243
|
+
|
|
|
244
|
+uint8_t Read_NRF_Register(uint8_t address)
|
|
|
245
|
+{
|
|
|
246
|
+ uint8_t registerContents;
|
|
|
247
|
+
|
|
|
248
|
+ registerContents = Write_Two_Bytes(address, 0x0);
|
|
|
249
|
+
|
|
|
250
|
+ return registerContents;
|
|
|
251
|
+}
|
|
208
|
252
|
|
|
209
|
253
|
void Write_NRF_Register(uint8_t address, uint8_t registerContents)
|
|
210
|
254
|
{
|
|
|
@@ -216,31 +260,59 @@ void Send_Activate_Command(void)
|
|
216
|
260
|
Write_Two_Bytes(0x50, 0x73);
|
|
217
|
261
|
}
|
|
218
|
262
|
|
|
219
|
|
-static void Write_Two_Bytes(uint8_t byte1, uint8_t byte2)
|
|
|
263
|
+void Send_TX_Flush_Command(void)
|
|
|
264
|
+{
|
|
|
265
|
+ Write_One_Byte(FLUSH_TX_COMMAND);
|
|
|
266
|
+}
|
|
|
267
|
+
|
|
|
268
|
+void Send_RX_Flush_Command(void)
|
|
220
|
269
|
{
|
|
|
270
|
+ Write_One_Byte(FLUSH_RX_COMMAND);
|
|
|
271
|
+}
|
|
|
272
|
+
|
|
|
273
|
+static uint8_t Write_One_Byte(uint8_t byte1)
|
|
|
274
|
+{
|
|
|
275
|
+ uint8_t registerContents = 0;
|
|
221
|
276
|
SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
222
|
277
|
|
|
223
|
|
- SPI_Transfer_Byte(byte1);
|
|
224
|
|
- SPI_Transfer_Byte(byte2);
|
|
|
278
|
+ registerContents = SPI_Transfer_Byte(byte1);
|
|
225
|
279
|
|
|
226
|
280
|
SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
|
281
|
+ return registerContents;
|
|
227
|
282
|
}
|
|
228
|
283
|
|
|
229
|
|
-void Send_TX_Flush_Command(void)
|
|
|
284
|
+static uint8_t Write_Two_Bytes(uint8_t byte1, uint8_t byte2)
|
|
230
|
285
|
{
|
|
231
|
|
- /* First write the write command with the address */
|
|
|
286
|
+ uint8_t registerContents = 0;
|
|
232
|
287
|
SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
233
|
288
|
|
|
234
|
|
- SPI_Transfer_Byte(0xE1);
|
|
|
289
|
+ SPI_Transfer_Byte(byte1);
|
|
|
290
|
+ registerContents = SPI_Transfer_Byte(byte2);
|
|
235
|
291
|
|
|
236
|
292
|
SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
|
293
|
+ return registerContents;
|
|
237
|
294
|
}
|
|
238
|
295
|
|
|
239
|
296
|
|
|
240
|
297
|
|
|
241
|
298
|
void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer)
|
|
242
|
299
|
{
|
|
243
|
|
- Write_Byte_And_Buffer(0xA0, buffer, length);
|
|
|
300
|
+ Write_Byte_And_Buffer(W_TX_PAYLOAD_COMMAND, buffer, length);
|
|
|
301
|
+}
|
|
|
302
|
+
|
|
|
303
|
+void Read_Message_From_RX_FIFO(uint8_t length, uint8_t * buffer)
|
|
|
304
|
+{
|
|
|
305
|
+ SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
|
306
|
+
|
|
|
307
|
+ SPI_Transfer_Byte(R_RX_PAYLOAD_COMMAND);
|
|
|
308
|
+
|
|
|
309
|
+ /* Write the data byte */
|
|
|
310
|
+ for (uint8_t i = 0; i < length; i ++)
|
|
|
311
|
+ {
|
|
|
312
|
+ buffer[i] = SPI_Transfer_Byte(0x0);
|
|
|
313
|
+ }
|
|
|
314
|
+
|
|
|
315
|
+ SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
244
|
316
|
}
|
|
245
|
317
|
|
|
246
|
318
|
void Set_TX_Address(uint8_t * txAddress, uint8_t length)
|
|
|
@@ -253,6 +325,11 @@ void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length)
|
|
253
|
325
|
Write_Byte_And_Buffer(RX_ADDR_P0_ADDRESS | 0x20, rxAddress, length);
|
|
254
|
326
|
}
|
|
255
|
327
|
|
|
|
328
|
+void Set_RX_P1_Address(uint8_t * rxAddress, uint8_t length)
|
|
|
329
|
+{
|
|
|
330
|
+ Write_Byte_And_Buffer(RX_ADDR_P1_ADDRESS | 0x20, rxAddress, length);
|
|
|
331
|
+}
|
|
|
332
|
+
|
|
256
|
333
|
static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length)
|
|
257
|
334
|
{
|
|
258
|
335
|
SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
|
@@ -267,5 +344,3 @@ static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length
|
|
267
|
344
|
|
|
268
|
345
|
SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
|
|
269
|
346
|
}
|
|
270
|
|
-
|
|
271
|
|
-//TODO: only write the used bytes into the address registers & add generic write functions
|