Ver código fonte

Generate an ID according to the dip switches

The ID is used as the last byte of the RX address. It is also sent as a
part of the data packet to allow assigning the sending station to a
packet.
Bernd Gottschlag 5 anos atrás
pai
commit
30fe01bb9e

+ 14
- 3
weather-sensor/firmware/main.c Ver arquivo

@@ -14,10 +14,11 @@
14 14
 #include "nrf24l01_definitions.h"
15 15
 #include "bme280_interface.h"
16 16
 #include "bme280_defs.h"
17
+#include "pin_programming.h"
17 18
 
18 19
 
19 20
 
20
-
21
+uint8_t ownId;
21 22
 
22 23
 char bool_case = 0;
23 24
 int timer = 0;
@@ -63,6 +64,7 @@ ISR(PCINT2_vect)
63 64
 int main (void)
64 65
 {
65 66
 	struct bme280_data sensorData;
67
+	struct DATA_PACKET dataPacket;
66 68
 //	uint8_t testRegisterContent = 0x0A;
67 69
 //	uint8_t registerContent[5];
68 70
 //	char registerContentString[30];
@@ -71,6 +73,10 @@ int main (void)
71 73
 	/* Enable the debug LED */
72 74
 	LED_DDR |= (1 << LED_PIN);
73 75
 
76
+	/* Get the own ID */
77
+	Configure_Pin_Programming_Pins();
78
+	ownId = Get_Own_Identifier();
79
+
74 80
 	/* Initialize the SPI */
75 81
 	Initialize_SPI();
76 82
 
@@ -79,7 +85,7 @@ int main (void)
79 85
 	// The NRF24L01 is now in the mode Standby-I.
80 86
 
81 87
 	/* Configure the transmission parameters (Enhanced ShockBurst)*/
82
-	Configure_Transmission();
88
+	Configure_Transmission(ownId);
83 89
 
84 90
 	/* Initialize the BME280 */
85 91
 	Initialize_BME280();
@@ -105,7 +111,12 @@ int main (void)
105 111
 
106 112
 			/* Get measurement and send it */
107 113
 			BME280_Get_Measurement(&sensorData);
108
-			NRF24L01_Send_Message((uint8_t*)&sensorData, sizeof(sensorData));
114
+
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));
109 120
 
110 121
 			_delay_ms(100); /* TODO: only for debugging, remove this later! */
111 122
 			LED_PORT &= ~(1 << LED_PIN);

+ 2
- 2
weather-sensor/firmware/makefile Ver arquivo

@@ -7,8 +7,8 @@ clean:
7 7
 flash: main.hex
8 8
 	sudo avrdude -c buspirate -b 115200 -P /dev/ttyUSB1 -p m88p -v -U flash:w:main.hex
9 9
 
10
-main: main.c spi.c spi.h nrf24l01.c nrf24l01.h nrf24l01_definitions.h bme280_interface.c bme280_interface.h BME280_driver/bme280.c BME280_driver/bme280.h led.h
11
-	avr-gcc main.c spi.c nrf24l01.c bme280_interface.c BME280_driver/bme280.c -I BME280_driver -o main -mmcu=atmega88p -Os -Wall -Wextra -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -DF_CPU=1000000UL -D BME280_32BIT_ENABLE
10
+main: main.c spi.c spi.h nrf24l01.c nrf24l01.h nrf24l01_definitions.h bme280_interface.c bme280_interface.h BME280_driver/bme280.c BME280_driver/bme280.h led.h pin_programming.c pin_programming.h
11
+	avr-gcc main.c spi.c nrf24l01.c bme280_interface.c BME280_driver/bme280.c pin_programming.c -I BME280_driver -o main -mmcu=atmega88p -Os -Wall -Wextra -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -DF_CPU=1000000UL -D BME280_32BIT_ENABLE
12 12
 
13 13
 main.hex: main
14 14
 	avr-objcopy -O ihex -R .eeprom main main.hex

+ 3
- 2
weather-sensor/firmware/nrf24l01.c Ver arquivo

@@ -59,7 +59,7 @@ void Set_NRF24L01_Pins(void)
59 59
 	NRF_IRQ_PORT |= (1 << NRF_IRQ_PORT); // Enable the pullup for the pin
60 60
 }
61 61
 
62
-void Configure_Transmission(void)
62
+void Configure_Transmission(uint8_t moduleId)
63 63
 {
64 64
 	FEATURE_REGISTER featureRegisterContents = {.byte = 0x0};
65 65
 	DYNPD_REGISTER dyndpRegisterContents = {.byte = 0x0};
@@ -102,7 +102,7 @@ void Configure_Transmission(void)
102 102
 	setupRetrRegisterContents.bits.ARD = 0xF;
103 103
 	Write_NRF_Register(SETUP_RETR_ADDRESS, setupRetrRegisterContents.byte);
104 104
 
105
-	/* set dynamic payload length for all data pipes */
105
+	/* set dynamic payload length for all data pipes */ // TODO: only pipe 0 is currently in use -> don't set the other values
106 106
 	dyndpRegisterContents.bits.DPL_P0 = 1;
107 107
 	dyndpRegisterContents.bits.DPL_P1 = 1;
108 108
 	dyndpRegisterContents.bits.DPL_P2 = 1;
@@ -114,6 +114,7 @@ void Configure_Transmission(void)
114 114
 	/* Set the TX address */
115 115
 	Set_TX_Address(txAddress, MAX_ADDRESS_LENGTH);
116 116
 
117
+	rx0Address[4] = moduleId; // The last byte of the address corresponds to the Id set by the pin programming
117 118
 	Set_RX_P0_Address(rx0Address, MAX_ADDRESS_LENGTH);
118 119
 
119 120
 

+ 9
- 1
weather-sensor/firmware/nrf24l01.h Ver arquivo

@@ -19,7 +19,7 @@
19 19
 
20 20
 void Initialize_NRF24L01(void);
21 21
 void Set_NRF24L01_Pins(void);
22
-void Configure_Transmission(void);
22
+void Configure_Transmission(uint8_t moduleId);
23 23
 uint8_t Read_NRF_Status_Register(void);
24 24
 uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents);
25 25
 void Write_NRF_Register(uint8_t address, uint8_t registerContents);
@@ -30,4 +30,12 @@ void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer);
30 30
 void Set_TX_Address(uint8_t * txAddress, uint8_t length);
31 31
 void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length);
32 32
 
33
+typedef struct __attribute__ ((packed)) DATA_PACKET
34
+{
35
+	uint8_t senderId;
36
+	uint32_t pressure;
37
+	int32_t temperature;
38
+	uint32_t humidity;
39
+} DATA_PACKET;
40
+
33 41
 #endif

+ 54
- 0
weather-sensor/firmware/pin_programming.c Ver arquivo

@@ -0,0 +1,54 @@
1
+#include <avr/io.h>
2
+#include <stdint.h>
3
+
4
+#include "pin_programming.h"
5
+
6
+/* Pin definitions */
7
+
8
+
9
+#define PIN_PROG_1_DDR     DDRB
10
+#define PIN_PROG_1_PORT    PORTB
11
+#define PIN_PROG_1_PIN     PB6
12
+#define PIN_PROG_1_PORTIN  PINB
13
+
14
+#define PIN_PROG_2_DDR     DDRB
15
+#define PIN_PROG_2_PORT    PORTB
16
+#define PIN_PROG_2_PIN     PB7
17
+#define PIN_PROG_2_PORTIN  PINB
18
+
19
+#define PIN_PROG_3_DDR     DDRD
20
+#define PIN_PROG_3_PORT    PORTD
21
+#define PIN_PROG_3_PIN     PD4
22
+#define PIN_PROG_3_PORTIN  PIND
23
+
24
+
25
+
26
+void Configure_Pin_Programming_Pins(void)
27
+{
28
+	/* Set pins as input */
29
+	PIN_PROG_1_DDR &= ~(1 << PIN_PROG_1_PIN);
30
+	PIN_PROG_2_DDR &= ~(1 << PIN_PROG_2_PIN);
31
+	PIN_PROG_3_DDR &= ~(1 << PIN_PROG_3_PIN);
32
+
33
+	/* Enable pull-ups */ // TODO: enable once the boards have been fixed!
34
+	PIN_PROG_1_PORT |= (1 << PIN_PROG_1_PIN);
35
+	PIN_PROG_2_PORT |= (1 << PIN_PROG_2_PIN);
36
+	PIN_PROG_3_PORT |= (1 << PIN_PROG_3_PIN);
37
+}
38
+
39
+uint8_t Get_Own_Identifier(void)
40
+{
41
+	/* The upper four bits of the ID are the module ID while the lower four bits are the serial
42
+	 * number set by the pin programming.
43
+	 * Switch 3 corresponds to bit 0
44
+	 * Switch 2 corresponds to bit 1
45
+	 * Switch 1 corresponds to bit 2
46
+	 */
47
+	uint8_t id = (MODULE_ID_SENSOR << 4);
48
+
49
+	id |= ((((PIN_PROG_3_PORTIN & (1 << PIN_PROG_3_PIN)) == 0) << 0) | \
50
+	       (((PIN_PROG_2_PORTIN & (1 << PIN_PROG_2_PIN)) == 0) << 1) | \
51
+	       (((PIN_PROG_1_PORTIN & (1 << PIN_PROG_1_PIN)) == 0) << 2));
52
+
53
+	return id;
54
+}

+ 13
- 0
weather-sensor/firmware/pin_programming.h Ver arquivo

@@ -0,0 +1,13 @@
1
+#ifndef PIN_PROGRAMMING_H
2
+#define PIN_PROGRAMMING_H
3
+
4
+typedef enum
5
+{
6
+	MODULE_ID_BASE_STATION = 1,
7
+	MODULE_ID_DISPLAY      = 2,
8
+	MODULE_ID_SENSOR       = 3
9
+} MODULE_ID;
10
+
11
+void Configure_Pin_Programming_Pins(void);
12
+uint8_t Get_Own_Identifier(void);
13
+#endif

Carregando…
Cancelar
Salvar