Переглянути джерело

Add a new packet format with the calculation of the CRC

Bernd Gottschlag 5 роки тому
джерело
коміт
e435620c91

+ 1
- 1
weather-sensor/firmware/crc.c Переглянути файл

@@ -45,7 +45,7 @@ uint16_t Calculate_Crc(const uint8_t *buffer, uint8_t size)
45 45
 
46 46
 	for (uint8_t i = 0; i < size; i++)
47 47
 	{
48
-		crc = (crc >> 8) ^ crcTable[ (crc ^ (uint16_t) *tempPointer) & 0x00FF ];
48
+		crc = (crc >> 8) ^ pgm_read_word(&crcTable[ (crc ^ (uint16_t) *tempPointer) & 0x00FF ]);
49 49
 		tempPointer++;
50 50
 	}
51 51
 

+ 4
- 0
weather-sensor/firmware/crc.h Переглянути файл

@@ -1 +1,5 @@
1
+#ifndef CRC_H
2
+#define CRC_H
3
+
1 4
 uint16_t Calculate_Crc(const uint8_t *buffer, uint8_t size);
5
+#endif

+ 30
- 10
weather-sensor/firmware/main.c Переглянути файл

@@ -15,6 +15,7 @@
15 15
 #include "bme280_interface.h"
16 16
 #include "bme280_defs.h"
17 17
 #include "pin_programming.h"
18
+#include "crc.h"
18 19
 
19 20
 
20 21
 
@@ -32,6 +33,10 @@ volatile uint8_t interruptCounter;
32 33
 volatile uint8_t executionFlag;
33 34
 
34 35
 
36
+
37
+static PACKET reportPacket;
38
+
39
+
35 40
 void Enter_Power_Save_Mode(void);
36 41
 void Exit_Power_Save_Mode(void);
37 42
 
@@ -64,11 +69,7 @@ ISR(PCINT2_vect)
64 69
 int main (void)
65 70
 {
66 71
 	struct bme280_data sensorData;
67
-	struct DATA_PACKET dataPacket;
68
-//	uint8_t testRegisterContent = 0x0A;
69
-//	uint8_t registerContent[5];
70
-//	char registerContentString[30];
71
-//	uint8_t lengthRead;
72
+	uint16_t crc;
72 73
 
73 74
 	/* Enable the debug LED */
74 75
 	LED_DDR |= (1 << LED_PIN);
@@ -112,11 +113,30 @@ int main (void)
112 113
 			/* Get measurement and send it */
113 114
 			BME280_Get_Measurement(&sensorData);
114 115
 
115
-			dataPacket.senderId = ownId;
116
-			dataPacket.temperature = sensorData.temperature;
117
-			dataPacket.pressure = sensorData.pressure;
118
-			dataPacket.humidity = sensorData.humidity;
119
-			NRF24L01_Send_Message((uint8_t*)&dataPacket, sizeof(dataPacket));
116
+
117
+			memset((uint8_t*)&reportPacket, 0, sizeof(reportPacket)); //Reinitialize the buffer with zeros
118
+
119
+			reportPacket.senderId = ownId;
120
+			//reportPacket.salt; /* TODO */
121
+			reportPacket.payload.values.packetIdentifier.elementCount = 3;
122
+			reportPacket.payload.values.packetIdentifier.packetType = 0; /* TODO: report type ? */
123
+
124
+			/* Fill in the payload */
125
+			reportPacket.payload.values.valueTypeTemperature = VALUE_TYPE_TEMPERATURE;
126
+			reportPacket.payload.values.temperature = sensorData.temperature;
127
+			reportPacket.payload.values.valueTypePressure = VALUE_TYPE_PRESSURE;
128
+			reportPacket.payload.values.pressure = sensorData.pressure;
129
+			reportPacket.payload.values.valueTypeHumidity = VALUE_TYPE_HUMIDITY;
130
+			reportPacket.payload.values.humidity = sensorData.humidity;
131
+
132
+			/* Calculate the CRC */
133
+			crc = Calculate_Crc(reportPacket.payload.buffer, PACKET_BUFFER_LENGTH);
134
+			reportPacket.crc = crc;
135
+
136
+			/* Encrypt the packet */
137
+			/* TODO */
138
+
139
+			NRF24L01_Send_Message((uint8_t*)&reportPacket, sizeof(reportPacket));
120 140
 
121 141
 			_delay_ms(100); /* TODO: only for debugging, remove this later! */
122 142
 			LED_PORT &= ~(1 << LED_PIN);

+ 35
- 6
weather-sensor/firmware/nrf24l01.h Переглянути файл

@@ -1,7 +1,7 @@
1 1
 #ifndef NRF24L01_H
2 2
 #define NRF24L01_H
3 3
 
4
-
4
+#define PACKET_BUFFER_LENGTH 22
5 5
 
6 6
 /* AVR I/O pin definionts */
7 7
 #define NRF_CE_DDR   DDRD
@@ -17,6 +17,7 @@
17 17
 #define NRF_IRQ_PIN     PD5
18 18
 #define NRF_IRQ_PORTIN  PIND
19 19
 
20
+
20 21
 void Initialize_NRF24L01(void);
21 22
 void Set_NRF24L01_Pins(void);
22 23
 void Configure_Transmission(uint8_t moduleId);
@@ -30,12 +31,40 @@ void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer);
30 31
 void Set_TX_Address(uint8_t * txAddress, uint8_t length);
31 32
 void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length);
32 33
 
33
-typedef struct __attribute__ ((packed)) DATA_PACKET
34
+typedef enum
35
+{
36
+	VALUE_TYPE_TIME = 0,
37
+	VALUE_TYPE_TEMPERATURE = 1,
38
+	VALUE_TYPE_PRESSURE = 2,
39
+	VALUE_TYPE_HUMIDITY = 3,
40
+} VALUE_TYPES;
41
+
42
+typedef struct __attribute__((packed)) BITFIELD_PACKET_COUNT_ELEMENT
43
+{
44
+	uint8_t elementCount : 3;
45
+	uint8_t packetType : 5;
46
+} BITFIELD_PACKET_COUNT_ELEMENT;
47
+
48
+typedef struct __attribute__((packed)) PACKET
34 49
 {
35 50
 	uint8_t senderId;
36
-	uint32_t pressure;
37
-	int32_t temperature;
38
-	uint32_t humidity;
39
-} DATA_PACKET;
51
+	uint8_t salt[7];
52
+	union {
53
+		struct {
54
+			BITFIELD_PACKET_COUNT_ELEMENT packetIdentifier;
55
+			uint8_t valueTypeTemperature;
56
+			int16_t temperature;
57
+			uint8_t valueTypePressure;
58
+			uint32_t pressure;
59
+			uint8_t valueTypeHumidity;
60
+			uint16_t humidity;
61
+			uint8_t unused[10];
62
+		} values;
63
+		uint8_t buffer[PACKET_BUFFER_LENGTH];
64
+	}payload;
65
+	//uint8_t buffer[PACKET_BUFFER_LENGTH];
66
+	uint16_t crc;
67
+} PACKET;
68
+
40 69
 
41 70
 #endif

Завантаження…
Відмінити
Зберегти