Browse Source

Use spi functions

Bernd Gottschlag 5 years ago
parent
commit
01dff7cfd3
1 changed files with 19 additions and 34 deletions
  1. 19
    34
      weather-sensor/firmware/nrf24l01.c

+ 19
- 34
weather-sensor/firmware/nrf24l01.c View File

220
 
220
 
221
 uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents)
221
 uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents)
222
 {
222
 {
223
+	/* TODO: simplify this function, as the registers with more than one byte are accessed with other functions */
223
 	uint8_t numberOfBytes = 0;
224
 	uint8_t numberOfBytes = 0;
224
 
225
 
225
 	if ((address == 0x0A) ||
226
 	if ((address == 0x0A) ||
291
 
292
 
292
 void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer)
293
 void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer)
293
 {
294
 {
294
-	NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
295
+	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
295
 
296
 
296
 	/* Issue the write command: */
297
 	/* Issue the write command: */
297
-	SPDR = 0xA0;
298
-
299
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
298
+	SPI_Transfer_Byte(0xA0);
300
 
299
 
301
 	/* Write the data bytes */
300
 	/* Write the data bytes */
302
 	for (uint8_t i = 0; i < length; i++)
301
 	for (uint8_t i = 0; i < length; i++)
303
 	{
302
 	{
304
-		SPDR = buffer[i];
305
-		while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
303
+		SPI_Transfer_Byte(buffer[i]);
306
 	}
304
 	}
307
 
305
 
308
-	NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
306
+	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
309
 }
307
 }
310
 
308
 
311
 void Set_TX_Address(uint32_t txAddress)
309
 void Set_TX_Address(uint32_t txAddress)
312
 {
310
 {
313
 	uint8_t * buffer = (uint8_t*) &txAddress;
311
 	uint8_t * buffer = (uint8_t*) &txAddress;
314
-	/* First write the write command with the address */
315
-	NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
316
-	SPDR = 0x10 | 0x20;
317
-	
318
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
319
 
312
 
313
+	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
314
+
315
+	SPI_Transfer_Byte(TX_ADDR_ADDRESS | 0x20);
320
 	/* Write the data byte */
316
 	/* Write the data byte */
321
 	for (uint8_t i = 0; i < 4; i ++)
317
 	for (uint8_t i = 0; i < 4; i ++)
322
 	{
318
 	{
323
-		SPDR = buffer[i];
324
-
325
-		while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
319
+		SPI_Transfer_Byte(buffer[i]);
326
 	}
320
 	}
321
+	SPI_Transfer_Byte(0x0);
327
 
322
 
328
-	SPDR = 0x0;
329
-
330
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
331
-
332
-	NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
323
+	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
333
 }
324
 }
334
 
325
 
335
 void Set_RX_P0_Address(uint32_t rxAddress)
326
 void Set_RX_P0_Address(uint32_t rxAddress)
336
 {
327
 {
337
 	uint8_t * buffer = (uint8_t*) &rxAddress;
328
 	uint8_t * buffer = (uint8_t*) &rxAddress;
338
-	/* First write the write command with the address */
339
-	NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
340
-	SPDR = 0x0A | 0x20;
341
-	
342
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
329
+
330
+	SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
331
+
332
+	SPI_Transfer_Byte(RX_ADDR_P0_ADDRESS | 0x20);
343
 
333
 
344
 	/* Write the data byte */
334
 	/* Write the data byte */
345
 	for (uint8_t i = 0; i < 4; i ++)
335
 	for (uint8_t i = 0; i < 4; i ++)
346
 	{
336
 	{
347
-		SPDR = buffer[i];
348
-
349
-		while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
337
+		SPI_Transfer_Byte(buffer[i]);
350
 	}
338
 	}
339
+	SPI_Transfer_Byte(0x0);
351
 
340
 
352
-	SPDR = 0x0;
353
-
354
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
355
-
356
-	NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
341
+	SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
357
 }
342
 }
358
 
343
 
359
-//TODO: only write the used bytes into the address registers
344
+//TODO: only write the used bytes into the address registers & add generic write functions

Loading…
Cancel
Save