Bernd Gottschlag 5 лет назад
Родитель
Сommit
5ee6bbbf1a

+ 9
- 16
weather-sensor/firmware/bme280_interface.c Просмотреть файл

@@ -1,6 +1,7 @@
1 1
 #include <avr/io.h>
2 2
 #include <util/delay.h>
3 3
 
4
+#include "spi.h"
4 5
 #include "bme280.h"
5 6
 #include "bme280_interface.h"
6 7
 
@@ -98,20 +99,14 @@ int8_t user_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16
98 99
 	/* unused parameters */
99 100
 	(void)dev_id;
100 101
 
101
-	/* SPI routine */
102
-
103
-	/* TODO pack into spi byte transfer function */
104
-	BME_CSN_PORT &= ~(1<<BME_CSN_PIN); // Start the transmission
105
-	SPDR = reg_addr;
106
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
102
+	SPI_Start_Transmission(&BME_CSN_PORT, BME_CSN_PIN);
103
+	SPI_Transfer_Byte(reg_addr);
107 104
 
108 105
 	for (uint16_t i = 0; i < len; i ++)
109 106
 	{
110
-		SPDR = 0x0; /* value doesn't matter */
111
-		while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
112
-		reg_data[i] = SPDR;
107
+		reg_data[i] = SPI_Transfer_Byte(0x0); /* Value to send doesn't matter */
113 108
 	}
114
-	BME_CSN_PORT |= (1<<BME_CSN_PIN); // Stop the transmission
109
+	SPI_Stop_Transmission(&BME_CSN_PORT, BME_CSN_PIN);
115 110
 
116 111
 	return rslt;
117 112
 }
@@ -124,16 +119,14 @@ int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint1
124 119
 	(void)dev_id;
125 120
 
126 121
 	/* SPI routine */
127
-	BME_CSN_PORT &= ~(1<<BME_CSN_PIN); // Start the transmission
128
-	SPDR = reg_addr;
129
-	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
122
+	SPI_Start_Transmission(&BME_CSN_PORT, BME_CSN_PIN);
123
+	SPI_Transfer_Byte(reg_addr);
130 124
 
131 125
 	for (uint16_t i = 0; i < len; i ++)
132 126
 	{
133
-		SPDR = reg_data[i]; /* value doesn't matter */
134
-		while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
127
+		SPI_Transfer_Byte(reg_data[i]);
135 128
 	}
136
-	BME_CSN_PORT |= (1<<BME_CSN_PIN); // Stop the transmission
129
+	SPI_Stop_Transmission(&BME_CSN_PORT, BME_CSN_PIN);
137 130
 
138 131
 	return rslt;
139 132
 }

+ 2
- 2
weather-sensor/firmware/makefile Просмотреть файл

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

+ 18
- 0
weather-sensor/firmware/spi.c Просмотреть файл

@@ -0,0 +1,18 @@
1
+#include <avr/io.h>
2
+
3
+void SPI_Start_Transmission(volatile uint8_t *port, uint8_t pinNumber)
4
+{
5
+	*port &= ~(1<<pinNumber);
6
+}
7
+
8
+uint8_t SPI_Transfer_Byte(uint8_t byteToSend)
9
+{
10
+	SPDR = byteToSend;
11
+	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
12
+	return SPDR;
13
+}
14
+
15
+void SPI_Stop_Transmission(volatile uint8_t *port, uint8_t pinNumber)
16
+{
17
+	*port |= (1<<pinNumber); // Stop the transmission
18
+}

+ 8
- 0
weather-sensor/firmware/spi.h Просмотреть файл

@@ -0,0 +1,8 @@
1
+#ifndef SPI_H
2
+#define SPI_H
3
+
4
+void SPI_Start_Transmission(volatile uint8_t *port, uint8_t pinNumber);
5
+uint8_t SPI_Transfer_Byte(uint8_t byte);
6
+void SPI_Stop_Transmission(volatile uint8_t *port, uint8_t pinNumber);
7
+
8
+#endif

Загрузка…
Отмена
Сохранить