Browse Source

Use spi functions

Bernd Gottschlag 5 years ago
parent
commit
93fd7841a9
1 changed files with 23 additions and 36 deletions
  1. 23
    36
      weather-sensor/firmware/nrf24l01.c

+ 23
- 36
weather-sensor/firmware/nrf24l01.c View File

210
 
210
 
211
 uint8_t Read_NRF_Status_Register(void)
211
 uint8_t Read_NRF_Status_Register(void)
212
 {
212
 {
213
-	NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
214
-	SPDR = 0XFF;
215
-
216
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
213
+	uint8_t registerContents;
217
 
214
 
218
-	NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
219
-
220
-	return SPDR;
215
+	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
216
+	registerContents = SPI_Transfer_Byte(0x00);
217
+	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
218
+	return registerContents;
221
 }
219
 }
222
 
220
 
223
 uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents)
221
 uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents)
236
 	}
234
 	}
237
 
235
 
238
 	/* First write the address */
236
 	/* First write the address */
239
-	NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
240
-	SPDR = address;
237
+	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
241
 
238
 
242
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
239
+
240
+	SPI_Transfer_Byte(address);
243
 
241
 
244
 	/* Read the register bytes */
242
 	/* Read the register bytes */
245
 	for (uint8_t i = 0; i < numberOfBytes; i++)
243
 	for (uint8_t i = 0; i < numberOfBytes; i++)
246
 	{
244
 	{
247
 		/* Write dummy data to shift in the register content */
245
 		/* Write dummy data to shift in the register content */
248
-		SPDR = 0x0;
249
-		while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
250
-		registerContents[i] = SPDR;
246
+		registerContents[i] = SPI_Transfer_Byte(0x0);
251
 	}
247
 	}
252
 
248
 
253
-	NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
249
+	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
254
 
250
 
255
-	// TODO: registers with more than one byte
256
 	return numberOfBytes;
251
 	return numberOfBytes;
257
 }
252
 }
258
 
253
 
259
 void Write_NRF_Register(uint8_t address, uint8_t registerContents)
254
 void Write_NRF_Register(uint8_t address, uint8_t registerContents)
260
 {
255
 {
261
 	/* First write the write command with the address */
256
 	/* First write the write command with the address */
262
-	NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
263
-	SPDR = address | 0x20;
264
-	
265
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
257
+	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
266
 
258
 
267
-	/* Write the data byte */
268
-	SPDR = registerContents;
259
+	SPI_Transfer_Byte(address | 0x20);
269
 
260
 
270
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
261
+	/* Write the data byte */
262
+	SPI_Transfer_Byte(registerContents);
271
 
263
 
272
-	NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
264
+	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
273
 }
265
 }
274
 
266
 
275
 // TODO: clean up functions
267
 // TODO: clean up functions
276
 void Send_Activate_Command(void)
268
 void Send_Activate_Command(void)
277
 {
269
 {
278
 	/* First write the write command with the address */
270
 	/* First write the write command with the address */
279
-	NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
280
-	SPDR = 0x50;
281
-	
282
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
271
+	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
272
+	SPI_Transfer_Byte(0x50);
283
 
273
 
284
 	/* Write the data byte */
274
 	/* Write the data byte */
285
-	SPDR = 0x73;
286
-
287
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
275
+	SPI_Transfer_Byte(0x73);
288
 
276
 
289
-	NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
277
+	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
290
 }
278
 }
291
 
279
 
292
 void Send_TX_Flush_Command(void)
280
 void Send_TX_Flush_Command(void)
293
 {
281
 {
294
 	/* First write the write command with the address */
282
 	/* First write the write command with the address */
295
-	NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
296
-	SPDR = 0xE1;
297
-	
298
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
283
+	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
299
 
284
 
300
-	NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
285
+	SPI_Transfer_Byte(0xE1);
286
+
287
+	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
301
 }
288
 }
302
 
289
 
303
 
290
 

Loading…
Cancel
Save