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