Pārlūkot izejas kodu

WIP: use register definitions to access them

Bernd Gottschlag 5 gadus atpakaļ
vecāks
revīzija
278f7f23fe
1 mainītis faili ar 30 papildinājumiem un 11 dzēšanām
  1. 30
    11
      weather-sensor/firmware/nrf24l01.c

+ 30
- 11
weather-sensor/firmware/nrf24l01.c Parādīt failu

5
 #include <stdbool.h>
5
 #include <stdbool.h>
6
 
6
 
7
 #include "nrf24l01.h"
7
 #include "nrf24l01.h"
8
+#include "nrf24l01_definitions.h"
8
 #include "uart_debug.h"
9
 #include "uart_debug.h"
9
 
10
 
10
 /* TODO
11
 /* TODO
19
 /* Startup and initial configuration of the NRF24L01 */
20
 /* Startup and initial configuration of the NRF24L01 */
20
 void Initialize_NRF24L01(void)
21
 void Initialize_NRF24L01(void)
21
 {
22
 {
23
+	CONFIG_REGISTER configRegisterContents = {.byte = 0x0};
24
+
22
 	/* Configure the AVR pins for the nrf24l01 */
25
 	/* Configure the AVR pins for the nrf24l01 */
23
 	/* Set up the NRF24L01 */
26
 	/* Set up the NRF24L01 */
24
 	NRF_CE_DDR |= (1 << NRF_CE_PIN);
27
 	NRF_CE_DDR |= (1 << NRF_CE_PIN);
33
 	_delay_ms(11);
36
 	_delay_ms(11);
34
 
37
 
35
 	/* Write the PWR_UP bit of the CONFIG register (EN_CRC is also set) */
38
 	/* Write the PWR_UP bit of the CONFIG register (EN_CRC is also set) */
36
-	Write_NRF_Register(0x0, 0xA);
39
+	configRegisterContents.bits.EN_CRC = 0x1;
40
+	configRegisterContents.bits.PWR_UP = 0x1;
41
+	Write_NRF_Register(CONFIG_ADDRESS, configRegisterContents.byte);
37
 
42
 
38
 	/* Wait more than 1.5 ms for the change to take effect */
43
 	/* Wait more than 1.5 ms for the change to take effect */
39
 	_delay_ms(2);
44
 	_delay_ms(2);
43
 
48
 
44
 void Configure_Transmission(void)
49
 void Configure_Transmission(void)
45
 {
50
 {
51
+	FEATURE_REGISTER featureRegisterContents = {.byte = 0x0};
52
+	DYNPD_REGISTER dyndpRegisterContents = {.byte = 0x0};
46
 	/* 
53
 	/* 
47
 	 * - Length of CRC (CRCO in CONFIG)
54
 	 * - Length of CRC (CRCO in CONFIG)
48
 	 * - Enable auto acknowledgment (EN_AA)
55
 	 * - Enable auto acknowledgment (EN_AA)
66
 	//Write_NRF_Register(0x03, 0x1);
73
 	//Write_NRF_Register(0x03, 0x1);
67
 
74
 
68
 	/* Set the frequency to 1450 MHz */
75
 	/* Set the frequency to 1450 MHz */
69
-	Write_NRF_Register(0x05, 0x32);
76
+	Write_NRF_Register(RF_CH_ADDRESS, 0x32);
70
 
77
 
71
 	/* Enable dynamic payload length */
78
 	/* Enable dynamic payload length */
72
 	Send_Activate_Command();
79
 	Send_Activate_Command();
73
-	Write_NRF_Register(0x1D, 0x4); // enable dynamic payload length
74
-	Write_NRF_Register(0X1C, 0X3F); // set dynamic payload length for all data pipes
80
+	featureRegisterContents.bits.EN_DPL = 1; // enable dynamic payload length
81
+	Write_NRF_Register(FEATURE_ADDRESS, featureRegisterContents.byte);
82
+
83
+	/* set dynamic payload length for all data pipes */
84
+	dyndpRegisterContents.bits.DPL_P0 = 1;
85
+	dyndpRegisterContents.bits.DPL_P1 = 1;
86
+	dyndpRegisterContents.bits.DPL_P2 = 1;
87
+	dyndpRegisterContents.bits.DPL_P3 = 1;
88
+	dyndpRegisterContents.bits.DPL_P4 = 1;
89
+	dyndpRegisterContents.bits.DPL_P5 = 1;
90
+	Write_NRF_Register(DYNPD_ADDRESS, dyndpRegisterContents.byte);
75
 
91
 
76
 	/* Set the TX address */
92
 	/* Set the TX address */
77
-	//Set_TX_Address(0x563412);
78
 	Set_TX_Address(0x123456);
93
 	Set_TX_Address(0x123456);
79
 
94
 
80
 	Set_RX_P0_Address(0x123456);
95
 	Set_RX_P0_Address(0x123456);
90
 	uint8_t statusContent = 0;
105
 	uint8_t statusContent = 0;
91
 
106
 
92
 	uint8_t registerContent[5];
107
 	uint8_t registerContent[5];
108
+	STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
93
 	uint8_t lengthRead;
109
 	uint8_t lengthRead;
94
 	char debugString[50] = "";
110
 	char debugString[50] = "";
95
 	uint32_t timeout = 0;
111
 	uint32_t timeout = 0;
112
 	do
128
 	do
113
 	{
129
 	{
114
 		_delay_ms(1);
130
 		_delay_ms(1);
115
-		lengthRead = Read_NRF_Register(0x07, registerContent);
131
+		lengthRead = Read_NRF_Register(STATUS_ADDRESS, &(statusRegisterContents.byte)); /* TODO: use funtion to read status register -> no overflow possible, only one NOP transfer needed, not two */
116
 
132
 
117
 		if (lengthRead > 1)
133
 		if (lengthRead > 1)
118
 		{
134
 		{
119
-			sprintf(debugString, "%s\r\n", "read error");
135
+			sprintf(debugString, "%s\r\n", "Read error");
120
 			Print_Debug_String(debugString);
136
 			Print_Debug_String(debugString);
121
 		}
137
 		}
122
 
138
 
123
-		if ((registerContent[0] & (1<<5)) != 0)
139
+		if (statusRegisterContent.bits.TX_DS == true)
124
 		{
140
 		{
125
 			transmissionFinished = true;
141
 			transmissionFinished = true;
126
 			sprintf(debugString, "%s\r\n", "TX fin");
142
 			sprintf(debugString, "%s\r\n", "TX fin");
127
 			Print_Debug_String(debugString);
143
 			Print_Debug_String(debugString);
128
 		}
144
 		}
129
 
145
 
130
-		if ((registerContent[0] & (1<<4)) != 0)
146
+		if (statusRegisterContent.bits.MAX_RT == true)
131
 		{
147
 		{
132
 			transmissionFinished = true;
148
 			transmissionFinished = true;
133
 			sprintf(debugString, "%s\r\n", "max ret");
149
 			sprintf(debugString, "%s\r\n", "max ret");
144
 	}
160
 	}
145
 
161
 
146
 	/* Reset the interrupts */
162
 	/* Reset the interrupts */
147
-	lengthRead = Read_NRF_Register(0x07, registerContent);
163
+	lengthRead = Read_NRF_Register(STATUS_ADDRESS, statusRegisterContents); /* TODO: use status register read function */
148
 	statusContent = registerContent[0] & 0x0F;
164
 	statusContent = registerContent[0] & 0x0F;
149
-	Write_NRF_Register(0x07, statusContent);
165
+	statusRegisterContents.bits.RX_DR = false;
166
+	statusRegisterContents.bits.TX_DS = false;
167
+	statusRegisterContents.bits.MAX_RT = false;
168
+	Write_NRF_Register(STATUS_ADDRESS, statusContent);
150
 
169
 
151
 	// TODO: flush FIFO if an error occured
170
 	// TODO: flush FIFO if an error occured
152
 }
171
 }

Notiek ielāde…
Atcelt
Saglabāt