浏览代码

WIP: request and receive salt from the base station

Bernd Gottschlag 5 年前
父节点
当前提交
99cd96a42b

+ 91
- 30
weather-sensor/firmware/main.c 查看文件

36
 volatile uint8_t executionFlag;
36
 volatile uint8_t executionFlag;
37
 
37
 
38
 
38
 
39
-
40
-static PACKET reportPacket;
39
+static uint8_t messageBuffer[PACKET_LENGTH];
40
+static PACKET * packet = (PACKET*) messageBuffer;
41
 
41
 
42
 
42
 
43
 void Enter_Power_Save_Mode(void);
43
 void Enter_Power_Save_Mode(void);
44
 void Exit_Power_Save_Mode(void);
44
 void Exit_Power_Save_Mode(void);
45
 
45
 
46
 
46
 
47
-/* TODO Notes for power saving:
48
- * - Power-save-mode needed -> SM2...0 bits written to 011
49
- * - Entering by issuing the SLEEP instruction -> What call in C?
50
- *   - Before executing the SLEEP instruction, write bit SE of the SMCR to 1
51
- * - Let Timer/Counter2 run with the necessary period and enable an interrupt.
52
- *   -> The Global Interrupt Enable bit in SREG has to be set.
53
- * - asynchronous/synchronous clock source?
54
- * - When waking up
55
- *   - Set PRR bits for needed peripherals
56
- * - 
57
- */
47
+bool Send_Get_Salt_Message(PACKET * packet, uint64_t salt); //TODO: put into own file
48
+
58
 void Set_Up_Power_Save_Mode(void);
49
 void Set_Up_Power_Save_Mode(void);
59
 void Enter_Power_Save_Mode(void);
50
 void Enter_Power_Save_Mode(void);
60
 
51
 
73
 {
64
 {
74
 	struct bme280_data sensorData;
65
 	struct bme280_data sensorData;
75
 	uint16_t crc;
66
 	uint16_t crc;
67
+	bool saltReceived = false;
76
 
68
 
77
 	/* Enable the debug LED */
69
 	/* Enable the debug LED */
78
 	LED_DDR |= (1 << LED_PIN);
70
 	LED_DDR |= (1 << LED_PIN);
96
 	Set_Up_Power_Save_Mode();
88
 	Set_Up_Power_Save_Mode();
97
 
89
 
98
 	/* Initialize the salt */
90
 	/* Initialize the salt */
99
-	salt = 0xFFFFFFFFFFFFFF00ull;
91
+	salt = 0x0ull;
100
 	salt |= ownId;
92
 	salt |= ownId;
101
 
93
 
94
+
95
+	/* TODO: Request a salt from the base station:
96
+	 * - loop as long as no reply from the base station was received:
97
+	 *   - send the packet that requests the salt
98
+	 *   - if no ack is received for the packet go direclty to sleep for 100 ms
99
+	 *   - if no salt is received within 100 ms send the message again
100
+	 *   - keep the LED on for the duration to allow the user to see that the device is ready for operation.
101
+	 */
102
+	//LED_PORT |= (1 << LED_PIN);
103
+	do
104
+	{
105
+		// TODO: send the GET_SALT message
106
+		if (Send_Get_Salt_Message(packet, salt) == true)
107
+		{
108
+			if (NRF24L01_Receive_Message(messageBuffer, 100 /*ms*/) == true)
109
+			{
110
+				saltReceived = true;
111
+			}
112
+		}
113
+		else
114
+		{
115
+			/* TODO: enter power save mode instead to make the battery life longer */
116
+			_delay_ms(100);
117
+		}
118
+	} while (saltReceived == false);
119
+
120
+
102
 	/* Delay the change of the operating frequency by the function Enter_Power_Save_Mode for the
121
 	/* Delay the change of the operating frequency by the function Enter_Power_Save_Mode for the
103
 	 * first function pass. If it is changed before the ISP can flash the MCU the clocks of the ISP
122
 	 * first function pass. If it is changed before the ISP can flash the MCU the clocks of the ISP
104
 	 * and MCU are mismatched and the flashing will fail.
123
 	 * and MCU are mismatched and the flashing will fail.
120
 			BME280_Get_Measurement(&sensorData);
139
 			BME280_Get_Measurement(&sensorData);
121
 
140
 
122
 
141
 
123
-			memset((uint8_t*)&reportPacket, 0, sizeof(reportPacket)); //Reinitialize the buffer with zeros
142
+			/* TODO: put the following into a Send_Report function */
143
+			memset((uint8_t*)packet, 0, PACKET_LENGTH); //Reinitialize the buffer with zeros
124
 
144
 
125
 			salt &= ~(1ull<<63);
145
 			salt &= ~(1ull<<63);
126
-			reportPacket.salt = salt;
127
-			reportPacket.payload.values.packetIdentifier.elementCount = 3;
128
-			reportPacket.payload.values.packetIdentifier.packetType = PACKET_TYPE_REPORT;
146
+			packet->salt = salt;
147
+			packet->payload.reportData.packetIdentifier.elementCount = 3;
148
+			packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_REPORT;
129
 
149
 
130
 			/* Fill in the payload */
150
 			/* Fill in the payload */
131
-			reportPacket.payload.values.valueTypeTemperature = VALUE_TYPE_TEMPERATURE;
132
-			reportPacket.payload.values.temperature = sensorData.temperature/10;
133
-			reportPacket.payload.values.valueTypePressure = VALUE_TYPE_PRESSURE;
134
-			reportPacket.payload.values.pressure = sensorData.pressure;
135
-			reportPacket.payload.values.valueTypeHumidity = VALUE_TYPE_HUMIDITY;
136
-			reportPacket.payload.values.humidity = sensorData.humidity * 100/1024;
151
+			packet->payload.reportData.valueTypeTemperature = VALUE_TYPE_TEMPERATURE;
152
+			packet->payload.reportData.temperature = sensorData.temperature/10;
153
+			packet->payload.reportData.valueTypePressure = VALUE_TYPE_PRESSURE;
154
+			packet->payload.reportData.pressure = sensorData.pressure;
155
+			packet->payload.reportData.valueTypeHumidity = VALUE_TYPE_HUMIDITY;
156
+			packet->payload.reportData.humidity = sensorData.humidity * 100/1024;
137
 
157
 
138
 			/* Calculate the CRC */
158
 			/* Calculate the CRC */
139
-			crc = Calculate_Crc(reportPacket.payload.buffer, PACKET_BUFFER_LENGTH);
140
-			reportPacket.crc = crc;
159
+			crc = Calculate_Crc(packet->payload.buffer, PACKET_PAYLOAD_BUFFER_LENGTH);
160
+			packet->crc = crc;
141
 
161
 
142
 			/* Encrypt the packet */
162
 			/* Encrypt the packet */
143
 			/* TODO: 
163
 			/* TODO: 
144
 			 * - increment the salt for every packet
164
 			 * - increment the salt for every packet
145
 			 * - Receive salt from the base station
165
 			 * - Receive salt from the base station
146
 			 */
166
 			 */
147
-			Encrypt((uint32_t*) &reportPacket.payload.buffer,
148
-			        PACKET_BUFFER_LENGTH + sizeof(crc),
167
+			Encrypt((uint32_t*) packet->payload.buffer,
168
+			        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
149
 			        salt, (uint32_t*) encryptionKey);
169
 			        salt, (uint32_t*) encryptionKey);
150
 
170
 
151
-			NRF24L01_Send_Message((uint8_t*)&reportPacket, sizeof(reportPacket));
171
+			NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
152
 
172
 
153
 			_delay_ms(100); /* TODO: only for debugging, remove this later! */
173
 			_delay_ms(100); /* TODO: only for debugging, remove this later! */
154
-			LED_PORT &= ~(1 << LED_PIN);
174
+			//LED_PORT &= ~(1 << LED_PIN);
155
 
175
 
156
 			cycle = 0;
176
 			cycle = 0;
157
 		}
177
 		}
225
 	PRR &= ~(1<<PRSPI); // Enable SPI
245
 	PRR &= ~(1<<PRSPI); // Enable SPI
226
 	Initialize_SPI(); // reinitalize SPI
246
 	Initialize_SPI(); // reinitalize SPI
227
 }
247
 }
248
+
249
+bool Send_Get_Salt_Message(PACKET * packet, uint64_t salt) //TODO: put into own file
250
+{
251
+	bool success = false;
252
+	uint16_t crc;
253
+
254
+	memset((uint8_t*)packet, 0, PACKET_LENGTH); //Reinitialize the buffer with zeros
255
+
256
+	salt &= ~(1ull<<63); /* Set the most significant bit to 0 to indicate a packet from the device to the base station */
257
+
258
+	packet->salt = salt;
259
+	packet->payload.reportData.packetIdentifier.elementCount = 0;
260
+	packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_GET_SALT;
261
+
262
+	/* Calculate the CRC */
263
+	crc = Calculate_Crc(packet->payload.buffer, PACKET_PAYLOAD_BUFFER_LENGTH);
264
+	packet->crc = crc;
265
+
266
+	Encrypt((uint32_t*) packet->payload.buffer,
267
+	        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
268
+	        salt, (uint32_t*) encryptionKey);
269
+
270
+	success = NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
271
+
272
+
273
+	return success;
274
+}
275
+
276
+//bool Read_Salt_Message(PACKET * packet, uint64_t * salt)
277
+//{
278
+//	uint16_t crcRemainder = 0xFFFF;
279
+//	uint64_t baseStationSalt = 0x0;
280
+//
281
+//	baseStationSalt = packet->salt;
282
+////	if (packet->payload.reportData.packetIdentifier != PACKET_TYPE_SALT)
283
+////	{
284
+////		return false;
285
+////	}
286
+//
287
+//	return true;
288
+//}

+ 149
- 74
weather-sensor/firmware/nrf24l01.c 查看文件

17
 extern volatile bool nrfInterruptRaised;
17
 extern volatile bool nrfInterruptRaised;
18
 
18
 
19
 void Print_Register_Contents(uint8_t address);
19
 void Print_Register_Contents(uint8_t address);
20
+
20
 void Send_TX_Flush_Command(void);
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
 static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length);
26
 static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length);
24
 
27
 
25
 /* Startup and initial configuration of the NRF24L01 */
28
 /* Startup and initial configuration of the NRF24L01 */
42
 	_delay_ms(2);
45
 	_delay_ms(2);
43
 
46
 
44
 	/* The NRF24L01 is now in the mode Standby-I */
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
 void Set_NRF24L01_Pins(void)
54
 void Set_NRF24L01_Pins(void)
64
 
71
 
65
 void Configure_Transmission(uint8_t moduleId)
72
 void Configure_Transmission(uint8_t moduleId)
66
 {
73
 {
67
-	DYNPD_REGISTER dyndpRegisterContents = {.byte = 0x0};
68
 	SETUP_RETR_REGISTER setupRetrRegisterContents = {.byte = 0x0};
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
 	uint8_t txAddress[5] = {0xB3, 0xB3, 0xB3, 0xB3, 0x00};
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
 	/* Set the frequency to 1450 MHz */
82
 	/* Set the frequency to 1450 MHz */
95
 	Write_NRF_Register(RF_CH_ADDRESS, 0x32);
83
 	Write_NRF_Register(RF_CH_ADDRESS, 0x32);
96
 
84
 
97
-
98
-	/* */
85
+	/* Set up the auto retries */
99
 	setupRetrRegisterContents.bits.ARC = 0x3;
86
 	setupRetrRegisterContents.bits.ARC = 0x3;
100
 	setupRetrRegisterContents.bits.ARD = 0xF;
87
 	setupRetrRegisterContents.bits.ARD = 0xF;
101
 	Write_NRF_Register(SETUP_RETR_ADDRESS, setupRetrRegisterContents.byte);
88
 	Write_NRF_Register(SETUP_RETR_ADDRESS, setupRetrRegisterContents.byte);
103
 	/* Set the TX address */
90
 	/* Set the TX address */
104
 	Set_TX_Address(txAddress, MAX_ADDRESS_LENGTH);
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
 	PCMSK2 |= (1<<PCINT21); // Set the external interrupt for PD5
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
 	STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
132
 	STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
116
 	
133
 	
117
 	if ((length > 32) || (length == 0))
134
 	if ((length > 32) || (length == 0))
118
 	{
135
 	{
119
-		return;
136
+		return success;
120
 	}
137
 	}
121
 
138
 
122
 	PCICR |= (1<<PCIE2); // Enable the interrupt for the IRQ signal
139
 	PCICR |= (1<<PCIE2); // Enable the interrupt for the IRQ signal
133
 	 * induced by the SPI:
150
 	 * induced by the SPI:
134
 	 * https://forum.mysensors.org/topic/10452/nrf24l01-communication-failure-root-cause-and-solution
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
 	statusRegisterContents.byte = Read_NRF_Status_Register();
154
 	statusRegisterContents.byte = Read_NRF_Status_Register();
139
 
155
 
142
 	{
158
 	{
143
 		Send_TX_Flush_Command(); /* Remove the packet from the TX FIFO as it is not done automatically */
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
 	/* Reset the interrupts */
167
 	/* Reset the interrupts */
154
 	nrfInterruptRaised = false;
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
 void Write_NRF_Register(uint8_t address, uint8_t registerContents)
253
 void Write_NRF_Register(uint8_t address, uint8_t registerContents)
210
 {
254
 {
216
 	Write_Two_Bytes(0x50, 0x73);
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
 	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
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
 	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
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
 	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
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
 	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
292
 	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
293
+	return registerContents;
237
 }
294
 }
238
 
295
 
239
 
296
 
240
 
297
 
241
 void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer)
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
 void Set_TX_Address(uint8_t * txAddress, uint8_t length)
318
 void Set_TX_Address(uint8_t * txAddress, uint8_t length)
253
 	Write_Byte_And_Buffer(RX_ADDR_P0_ADDRESS | 0x20, rxAddress, length);
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
 static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length)
333
 static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length)
257
 {
334
 {
258
 	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
335
 	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
267
 
344
 
268
 	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
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

+ 27
- 10
weather-sensor/firmware/nrf24l01.h 查看文件

1
 #ifndef NRF24L01_H
1
 #ifndef NRF24L01_H
2
 #define NRF24L01_H
2
 #define NRF24L01_H
3
 
3
 
4
-#define PACKET_BUFFER_LENGTH 22
4
+#define PACKET_LENGTH 32
5
+
6
+#define PACKET_PAYLOAD_BUFFER_LENGTH 22
5
 
7
 
6
 /* AVR I/O pin definionts */
8
 /* AVR I/O pin definionts */
7
 #define NRF_CE_DDR   DDRD
9
 #define NRF_CE_DDR   DDRD
22
 void Set_NRF24L01_Pins(void);
24
 void Set_NRF24L01_Pins(void);
23
 void Configure_Transmission(uint8_t moduleId);
25
 void Configure_Transmission(uint8_t moduleId);
24
 uint8_t Read_NRF_Status_Register(void);
26
 uint8_t Read_NRF_Status_Register(void);
25
-uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents);
27
+uint8_t Read_NRF_Register(uint8_t address);
26
 void Write_NRF_Register(uint8_t address, uint8_t registerContents);
28
 void Write_NRF_Register(uint8_t address, uint8_t registerContents);
27
 void Send_Activate_Command(void);
29
 void Send_Activate_Command(void);
28
-
29
-void NRF24L01_Send_Message(uint8_t *buffer, uint8_t length);
30
+bool NRF24L01_Send_Message(uint8_t *buffer, uint8_t length);
31
+bool NRF24L01_Receive_Message(uint8_t *buffer, uint8_t duration);
30
 void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer);
32
 void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer);
33
+void Read_Message_From_RX_FIFO(uint8_t length, uint8_t * buffer);
31
 void Set_TX_Address(uint8_t * txAddress, uint8_t length);
34
 void Set_TX_Address(uint8_t * txAddress, uint8_t length);
32
 void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length);
35
 void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length);
36
+void Set_RX_P1_Address(uint8_t * rxAddress, uint8_t length);
33
 
37
 
34
 typedef enum
38
 typedef enum
35
 {
39
 {
36
-	PACKET_TYPE_SALT = 0,
37
-	PACKET_TYPE_REPORT = 1,
38
-	PACKET_TYPE_GET_VALUES = 2,
39
-	PACKET_TYPE_VALUES = 3,
40
+	PACKET_TYPE_GET_SALT = 0,
41
+	PACKET_TYPE_SALT = 1,
42
+	PACKET_TYPE_REPORT = 2,
43
+	PACKET_TYPE_GET_VALUES = 3,
44
+	PACKET_TYPE_VALUES = 4,
40
 } PACKET_TYPE;
45
 } PACKET_TYPE;
41
 
46
 
42
 typedef enum
47
 typedef enum
66
 			uint8_t valueTypeHumidity;
71
 			uint8_t valueTypeHumidity;
67
 			uint16_t humidity;
72
 			uint16_t humidity;
68
 			uint8_t unused[10];
73
 			uint8_t unused[10];
69
-		} values;
70
-		uint8_t buffer[PACKET_BUFFER_LENGTH];
74
+		} reportData;
75
+
76
+		struct {
77
+			BITFIELD_PACKET_COUNT_ELEMENT packetIdentifier;
78
+			uint8_t salt[7];
79
+			uint8_t unused[14];
80
+		} saltData;
81
+
82
+		struct {
83
+			BITFIELD_PACKET_COUNT_ELEMENT packetIdentifier;
84
+			uint8_t unused[21];
85
+		} getSaltData;
86
+
87
+		uint8_t buffer[PACKET_PAYLOAD_BUFFER_LENGTH];
71
 	}payload;
88
 	}payload;
72
 	uint16_t crc;
89
 	uint16_t crc;
73
 } PACKET;
90
 } PACKET;

+ 6
- 0
weather-sensor/firmware/nrf24l01_definitions.h 查看文件

31
 #define DYNPD_ADDRESS 0x1C
31
 #define DYNPD_ADDRESS 0x1C
32
 #define FEATURE_ADDRESS 0x1D
32
 #define FEATURE_ADDRESS 0x1D
33
 
33
 
34
+/* Commands */
35
+#define FLUSH_TX_COMMAND 0xE1
36
+#define FLUSH_RX_COMMAND 0xE2
37
+#define W_TX_PAYLOAD_COMMAND 0xA0
38
+#define R_RX_PAYLOAD_COMMAND 0xA1
39
+
34
 /* Register bits definitions */
40
 /* Register bits definitions */
35
 /* CONFIG*/
41
 /* CONFIG*/
36
 typedef union
42
 typedef union

正在加载...
取消
保存