2 Ревизии

Автор SHA1 Съобщение Дата
  Bernd Gottschlag 1a527b5929 Re-enable dynamic packet length преди 5 години
  Bernd Gottschlag 99cd96a42b WIP: request and receive salt from the base station преди 5 години
променени са 4 файла, в които са добавени 132 реда и са изтрити 269 реда
  1. 30
    91
      weather-sensor/firmware/main.c
  2. 92
    145
      weather-sensor/firmware/nrf24l01.c
  3. 10
    27
      weather-sensor/firmware/nrf24l01.h
  4. 0
    6
      weather-sensor/firmware/nrf24l01_definitions.h

+ 30
- 91
weather-sensor/firmware/main.c Целия файл

36
 volatile uint8_t executionFlag;
36
 volatile uint8_t executionFlag;
37
 
37
 
38
 
38
 
39
-static uint8_t messageBuffer[PACKET_LENGTH];
40
-static PACKET * packet = (PACKET*) messageBuffer;
39
+
40
+static PACKET reportPacket;
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
-bool Send_Get_Salt_Message(PACKET * packet, uint64_t salt); //TODO: put into own file
48
-
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
+ */
49
 void Set_Up_Power_Save_Mode(void);
58
 void Set_Up_Power_Save_Mode(void);
50
 void Enter_Power_Save_Mode(void);
59
 void Enter_Power_Save_Mode(void);
51
 
60
 
64
 {
73
 {
65
 	struct bme280_data sensorData;
74
 	struct bme280_data sensorData;
66
 	uint16_t crc;
75
 	uint16_t crc;
67
-	bool saltReceived = false;
68
 
76
 
69
 	/* Enable the debug LED */
77
 	/* Enable the debug LED */
70
 	LED_DDR |= (1 << LED_PIN);
78
 	LED_DDR |= (1 << LED_PIN);
88
 	Set_Up_Power_Save_Mode();
96
 	Set_Up_Power_Save_Mode();
89
 
97
 
90
 	/* Initialize the salt */
98
 	/* Initialize the salt */
91
-	salt = 0x0ull;
99
+	salt = 0xFFFFFFFFFFFFFF00ull;
92
 	salt |= ownId;
100
 	salt |= ownId;
93
 
101
 
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
-
121
 	/* Delay the change of the operating frequency by the function Enter_Power_Save_Mode for the
102
 	/* Delay the change of the operating frequency by the function Enter_Power_Save_Mode for the
122
 	 * first function pass. If it is changed before the ISP can flash the MCU the clocks of the ISP
103
 	 * first function pass. If it is changed before the ISP can flash the MCU the clocks of the ISP
123
 	 * and MCU are mismatched and the flashing will fail.
104
 	 * and MCU are mismatched and the flashing will fail.
139
 			BME280_Get_Measurement(&sensorData);
120
 			BME280_Get_Measurement(&sensorData);
140
 
121
 
141
 
122
 
142
-			/* TODO: put the following into a Send_Report function */
143
-			memset((uint8_t*)packet, 0, PACKET_LENGTH); //Reinitialize the buffer with zeros
123
+			memset((uint8_t*)&reportPacket, 0, sizeof(reportPacket)); //Reinitialize the buffer with zeros
144
 
124
 
145
 			salt &= ~(1ull<<63);
125
 			salt &= ~(1ull<<63);
146
-			packet->salt = salt;
147
-			packet->payload.reportData.packetIdentifier.elementCount = 3;
148
-			packet->payload.reportData.packetIdentifier.packetType = PACKET_TYPE_REPORT;
126
+			reportPacket.salt = salt;
127
+			reportPacket.payload.values.packetIdentifier.elementCount = 3;
128
+			reportPacket.payload.values.packetIdentifier.packetType = PACKET_TYPE_REPORT;
149
 
129
 
150
 			/* Fill in the payload */
130
 			/* Fill in the payload */
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;
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;
157
 
137
 
158
 			/* Calculate the CRC */
138
 			/* Calculate the CRC */
159
-			crc = Calculate_Crc(packet->payload.buffer, PACKET_PAYLOAD_BUFFER_LENGTH);
160
-			packet->crc = crc;
139
+			crc = Calculate_Crc(reportPacket.payload.buffer, PACKET_BUFFER_LENGTH);
140
+			reportPacket.crc = crc;
161
 
141
 
162
 			/* Encrypt the packet */
142
 			/* Encrypt the packet */
163
 			/* TODO: 
143
 			/* TODO: 
164
 			 * - increment the salt for every packet
144
 			 * - increment the salt for every packet
165
 			 * - Receive salt from the base station
145
 			 * - Receive salt from the base station
166
 			 */
146
 			 */
167
-			Encrypt((uint32_t*) packet->payload.buffer,
168
-			        PACKET_PAYLOAD_BUFFER_LENGTH + sizeof(crc),
147
+			Encrypt((uint32_t*) &reportPacket.payload.buffer,
148
+			        PACKET_BUFFER_LENGTH + sizeof(crc),
169
 			        salt, (uint32_t*) encryptionKey);
149
 			        salt, (uint32_t*) encryptionKey);
170
 
150
 
171
-			NRF24L01_Send_Message((uint8_t*)packet, PACKET_LENGTH);
151
+			NRF24L01_Send_Message((uint8_t*)&reportPacket, sizeof(reportPacket));
172
 
152
 
173
 			_delay_ms(100); /* TODO: only for debugging, remove this later! */
153
 			_delay_ms(100); /* TODO: only for debugging, remove this later! */
174
-			//LED_PORT &= ~(1 << LED_PIN);
154
+			LED_PORT &= ~(1 << LED_PIN);
175
 
155
 
176
 			cycle = 0;
156
 			cycle = 0;
177
 		}
157
 		}
245
 	PRR &= ~(1<<PRSPI); // Enable SPI
225
 	PRR &= ~(1<<PRSPI); // Enable SPI
246
 	Initialize_SPI(); // reinitalize SPI
226
 	Initialize_SPI(); // reinitalize SPI
247
 }
227
 }
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
-//}

+ 92
- 145
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
-
21
 void Send_TX_Flush_Command(void);
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
 static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length);
23
 static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length);
27
 
24
 
28
 /* Startup and initial configuration of the NRF24L01 */
25
 /* Startup and initial configuration of the NRF24L01 */
45
 	_delay_ms(2);
42
 	_delay_ms(2);
46
 
43
 
47
 	/* The NRF24L01 is now in the mode Standby-I */
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
 void Set_NRF24L01_Pins(void)
47
 void Set_NRF24L01_Pins(void)
75
 	EN_RXADDR_REGISTER enableRxAddressesRegisterContents = {.byte = 0x0};
68
 	EN_RXADDR_REGISTER enableRxAddressesRegisterContents = {.byte = 0x0};
76
 	RX_PW_Pn_REGISTER rxPwPnRegisterContents = {.byte = 0x0};
69
 	RX_PW_Pn_REGISTER rxPwPnRegisterContents = {.byte = 0x0};
77
 	EN_AA_REGISTER enAaRegister = {.byte = 0x0};
70
 	EN_AA_REGISTER enAaRegister = {.byte = 0x0};
71
+	FEATURE_REGISTER featureRegisterContents = {.byte = 0x0};
72
+	DYNPD_REGISTER dyndpRegisterContents = {.byte = 0x0};
78
 
73
 
79
 	uint8_t txAddress[5] = {0xB3, 0xB3, 0xB3, 0xB3, 0x00};
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
 	/* Set the frequency to 1450 MHz */
98
 	/* Set the frequency to 1450 MHz */
83
 	Write_NRF_Register(RF_CH_ADDRESS, 0x32);
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
 	/* Set up the auto retries */
118
 	/* Set up the auto retries */
119
+
120
+	/* */
86
 	setupRetrRegisterContents.bits.ARC = 0x3;
121
 	setupRetrRegisterContents.bits.ARC = 0x3;
87
 	setupRetrRegisterContents.bits.ARD = 0xF;
122
 	setupRetrRegisterContents.bits.ARD = 0xF;
88
 	Write_NRF_Register(SETUP_RETR_ADDRESS, setupRetrRegisterContents.byte);
123
 	Write_NRF_Register(SETUP_RETR_ADDRESS, setupRetrRegisterContents.byte);
90
 	/* Set the TX address */
125
 	/* Set the TX address */
91
 	Set_TX_Address(txAddress, MAX_ADDRESS_LENGTH);
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
 	PCMSK2 |= (1<<PCINT21); // Set the external interrupt for PD5
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
 	STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
137
 	STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
133
 	
138
 	
134
 	if ((length > 32) || (length == 0))
139
 	if ((length > 32) || (length == 0))
135
 	{
140
 	{
136
-		return success;
141
+		return;
137
 	}
142
 	}
138
 
143
 
139
 	PCICR |= (1<<PCIE2); // Enable the interrupt for the IRQ signal
144
 	PCICR |= (1<<PCIE2); // Enable the interrupt for the IRQ signal
150
 	 * induced by the SPI:
155
 	 * induced by the SPI:
151
 	 * https://forum.mysensors.org/topic/10452/nrf24l01-communication-failure-root-cause-and-solution
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
 	statusRegisterContents.byte = Read_NRF_Status_Register();
160
 	statusRegisterContents.byte = Read_NRF_Status_Register();
155
 
161
 
158
 	{
164
 	{
159
 		Send_TX_Flush_Command(); /* Remove the packet from the TX FIFO as it is not done automatically */
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
 	/* Reset the interrupts */
169
 	/* Reset the interrupts */
174
 	nrfInterruptRaised = false;
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
 void Write_NRF_Register(uint8_t address, uint8_t registerContents)
231
 void Write_NRF_Register(uint8_t address, uint8_t registerContents)
254
 {
232
 {
260
 	Write_Two_Bytes(0x50, 0x73);
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
 	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
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
 	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
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
 	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
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
 	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
258
 	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
293
-	return registerContents;
294
 }
259
 }
295
 
260
 
296
 
261
 
297
 
262
 
298
 void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer)
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
 void Set_TX_Address(uint8_t * txAddress, uint8_t length)
268
 void Set_TX_Address(uint8_t * txAddress, uint8_t length)
325
 	Write_Byte_And_Buffer(RX_ADDR_P0_ADDRESS | 0x20, rxAddress, length);
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
 static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length)
278
 static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length)
334
 {
279
 {
335
 	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
280
 	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
344
 
289
 
345
 	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
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

+ 10
- 27
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_LENGTH 32
5
-
6
-#define PACKET_PAYLOAD_BUFFER_LENGTH 22
4
+#define PACKET_BUFFER_LENGTH 22
7
 
5
 
8
 /* AVR I/O pin definionts */
6
 /* AVR I/O pin definionts */
9
 #define NRF_CE_DDR   DDRD
7
 #define NRF_CE_DDR   DDRD
24
 void Set_NRF24L01_Pins(void);
22
 void Set_NRF24L01_Pins(void);
25
 void Configure_Transmission(uint8_t moduleId);
23
 void Configure_Transmission(uint8_t moduleId);
26
 uint8_t Read_NRF_Status_Register(void);
24
 uint8_t Read_NRF_Status_Register(void);
27
-uint8_t Read_NRF_Register(uint8_t address);
25
+uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents);
28
 void Write_NRF_Register(uint8_t address, uint8_t registerContents);
26
 void Write_NRF_Register(uint8_t address, uint8_t registerContents);
29
 void Send_Activate_Command(void);
27
 void Send_Activate_Command(void);
30
-bool NRF24L01_Send_Message(uint8_t *buffer, uint8_t length);
31
-bool NRF24L01_Receive_Message(uint8_t *buffer, uint8_t duration);
28
+
29
+void NRF24L01_Send_Message(uint8_t *buffer, uint8_t length);
32
 void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer);
30
 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);
34
 void Set_TX_Address(uint8_t * txAddress, uint8_t length);
31
 void Set_TX_Address(uint8_t * txAddress, uint8_t length);
35
 void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length);
32
 void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length);
36
-void Set_RX_P1_Address(uint8_t * rxAddress, uint8_t length);
37
 
33
 
38
 typedef enum
34
 typedef enum
39
 {
35
 {
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,
36
+	PACKET_TYPE_SALT = 0,
37
+	PACKET_TYPE_REPORT = 1,
38
+	PACKET_TYPE_GET_VALUES = 2,
39
+	PACKET_TYPE_VALUES = 3,
45
 } PACKET_TYPE;
40
 } PACKET_TYPE;
46
 
41
 
47
 typedef enum
42
 typedef enum
71
 			uint8_t valueTypeHumidity;
66
 			uint8_t valueTypeHumidity;
72
 			uint16_t humidity;
67
 			uint16_t humidity;
73
 			uint8_t unused[10];
68
 			uint8_t unused[10];
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];
69
+		} values;
70
+		uint8_t buffer[PACKET_BUFFER_LENGTH];
88
 	}payload;
71
 	}payload;
89
 	uint16_t crc;
72
 	uint16_t crc;
90
 } PACKET;
73
 } PACKET;

+ 0
- 6
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
-
40
 /* Register bits definitions */
34
 /* Register bits definitions */
41
 /* CONFIG*/
35
 /* CONFIG*/
42
 typedef union
36
 typedef union

Loading…
Отказ
Запис