Loading...
Searching...
No Matches
devices.c
1/***********************************************************************************
2 * @file devices.c *
3 * @author Matt Ricci *
4 * @brief Provides device and peripheral driver initialisations. *
5 ***********************************************************************************/
6
7#include "devicelist.h"
8#include "devices.h"
9#include "params.h"
10
11// Peripheral includes
12#include "uart.h"
13#include "gpiopin.h"
14
15// Device includes
16#include "a3g4250d.h"
17#include "bmp581.h"
18#include "w25q128.h"
19#include "sam_m10q.h"
20#include "kx134_1211.h"
21#include "sx1272.h"
22
23static DeviceHandle_t deviceList[DEVICE_MAX_KEYS];
24
25/* ============================================================================================== */
32bool initDevices() {
33 DeviceList_init(deviceList);
34
35 // SPI peripherals and devices
36 initSpiPins();
37 initSensors();
38 initFlash();
39 initLora();
40
41 // UART peripherals and devices
42 initUart();
43
44 // @TODO: add in error checking
45 return true;
46}
47
48/* ============================================================================================== */
57void initSpiPins() {
58 // Sensor configuration
59 GPIO_Config spiSensorConfig = GPIO_CONFIG_DEFAULT;
60 spiSensorConfig.mode = GPIO_MODE_AF;
61 spiSensorConfig.afr = SENSORS_SPI_AF;
62
63 GPIOpin_t sensorSCK = GPIOpin_init(SENSORS_SPI_PORT, SENSORS_SPI_SCK, &spiSensorConfig);
64 GPIOpin_t sensorSDI = GPIOpin_init(SENSORS_SPI_PORT, SENSORS_SPI_SDI, &spiSensorConfig);
65 GPIOpin_t sensorSDO = GPIOpin_init(SENSORS_SPI_PORT, SENSORS_SPI_SDO, &spiSensorConfig);
66
67 // Flash configuration
68 GPIO_Config spiFlashConfig = GPIO_CONFIG_DEFAULT;
69 spiFlashConfig.mode = GPIO_MODE_AF;
70 spiFlashConfig.afr = FLASH_SPI_AF;
71
72 GPIOpin_t flashSCK = GPIOpin_init(FLASH_SPI_PORT, FLASH_SPI_SCK, &spiFlashConfig);
73 GPIOpin_t flashSDI = GPIOpin_init(FLASH_SPI_PORT, FLASH_SPI_SDI, &spiFlashConfig);
74 GPIOpin_t flashSDO = GPIOpin_init(FLASH_SPI_PORT, FLASH_SPI_SDO, &spiFlashConfig);
75
76 // LoRa configuration
77 GPIO_Config spiLoraConfig = GPIO_CONFIG_DEFAULT;
78 spiLoraConfig.mode = GPIO_MODE_AF;
79 spiLoraConfig.afr = LORA_SPI_AF;
80
81 GPIOpin_t loraSCK = GPIOpin_init(LORA_SPI_PORT, LORA_SPI_SCK, &spiLoraConfig);
82 GPIOpin_t loraSDI = GPIOpin_init(LORA_SPI_PORT, LORA_SPI_SDI, &spiLoraConfig);
83 GPIOpin_t loraSDO = GPIOpin_init(LORA_SPI_PORT, LORA_SPI_SDO, &spiLoraConfig);
84}
85
86/* ============================================================================================== */
93bool initSensors() {
94
95 static SPI_t spiSensors;
96 spiSensors = SPI_init(SENSORS_SPI_INTERFACE, NULL);
97
98 // ==========================================================================
99 // HIGH RANGE ACCELEROMETER
100 //
101 // Provides low resolution (high scale) inertial data for 3-axis acceleration.
102 // This is used for data logging as well as state estimation when
103 // the rocket's upward velocity is greater than the maximum high
104 // resolution scale.
105 GPIOpin_t hAccelCS = GPIOpin_init(ACCEL_CS2, NULL);
106 static KX134_1211_t hAccel;
108 &hAccel,
109 &spiSensors,
110 hAccelCS,
111 ACCEL_SCALE_HIGH, // Set to high scale for larger G forces
112 ACCEL_AXES_2, // Accelerometer 2 mounting axes
113 ACCEL_SIGN_2 // +/- for mounting axes
114 );
115 deviceList[DEVICE_ACCEL_HIGH].deviceName = "HAccel";
116 deviceList[DEVICE_ACCEL_HIGH].device = &hAccel;
117
118 // ==========================================================================
119 // LOW RANGE ACCELEROMETER
120 //
121 // Provides high resolution (low scale) inertial data for 3-axis acceleration.
122 // This is used for data logging as well as state estimation when
123 // the rocket's upward velocity is within range of the high resolution.
124 GPIOpin_t lAccelCS = GPIOpin_init(ACCEL_CS1, NULL);
125 static KX134_1211_t lAccel;
127 &lAccel,
128 &spiSensors,
129 lAccelCS,
130 ACCEL_SCALE_LOW, // Set to low scale for smaller G forces
131 ACCEL_AXES_1, // Accelerometer 1 mounting axes
132 ACCEL_SIGN_1 // +/- for mounting axes
133 );
134 deviceList[DEVICE_ACCEL_LOW].deviceName = "LAccel";
135 deviceList[DEVICE_ACCEL_LOW].device = &lAccel;
136
137 // ==========================================================================
138 // BAROMETER
139 //
140 // Measures temperature compensated atmospheric pressure. Data from the
141 // barometer is used in altitude calculation, as well as providing one
142 // metric for detecting apogee (decreasing pressure readings).
143 GPIOpin_t baroCS = GPIOpin_init(BARO_CS, NULL);
144 static BMP581_t baro;
146 &baro,
147 &spiSensors,
148 baroCS,
149 BMP581_TEMP_SENSITIVITY, // Set temperature measurement sensitivity
150 BMP581_PRESS_SENSITIVITY // Set pressure measurement sensitivity
151 );
152 deviceList[DEVICE_BARO].deviceName = "Baro";
153 deviceList[DEVICE_BARO].device = &baro;
154
155 // ==========================================================================
156 // GYROSCOPE
157 //
158 // Measures inertial data for 3-axis rotations. This data is used in
159 // calculations for attitude quaternion to determine rotation during flight
160 // and apply tilt-angle compensation.
161 GPIOpin_t gyroCS = GPIOpin_init(GYRO_CS, NULL);
162 static A3G4250D_t gyro;
164 &gyro,
165 &spiSensors,
166 gyroCS,
167 A3G4250D_SENSITIVITY, // Set measurement sensitivity
168 GYRO_AXES, // Gyroscope mounting axes
169 GYRO_SIGN // +/- for mounting axes
170 );
171 deviceList[DEVICE_GYRO].deviceName = "Gyro";
172 deviceList[DEVICE_GYRO].device = &gyro;
173
174 // ==========================================================================
175 // !! THIS DEVICE HANDLE WILL BE MODIFIED AT RUN-TIME !!
176 // DEVICE_ACCEL represents the current accelerometer selected by the system.
177 //
178 // The intent is that the high frequency data task will determine which of the
179 // two connected accelerometers (configured for high and low resolution) will be
180 // used for data acquisition. Other tasks should interact with the current
181 // accelerometer without directly modifying it.
182 deviceList[DEVICE_ACCEL] = deviceList[DEVICE_ACCEL_LOW];
183
184 // @TODO: add in error checking
185 return true;
186}
187
188/* ============================================================================================== */
195bool initFlash() {
196
197 static SPI_t spiFlash;
198 spiFlash = SPI_init(FLASH_SPI_INTERFACE, NULL);
199
200 // ==========================================================================
201 // FLASH
202 //
203 // Flash storage for measured and calculated data during flight. Data is
204 // written to the device in dataframe chunks. Each interval of high and
205 // low resolution data is appended in a frame to a circular memory buffer
206 // by the system, and is written to flash during idle time once at least a
207 // full page of data is available.
208 GPIOpin_t flashCS = GPIOpin_init(FLASH_CS_PORT, FLASH_CS_PIN, NULL);
209 static W25Q128_t flash;
211 &flash,
212 &spiFlash,
213 flashCS
214 );
215 deviceList[DEVICE_FLASH].deviceName = "Flash";
216 deviceList[DEVICE_FLASH].device = &flash;
217
218 // @TODO: add in error checking
219 return true;
220}
221
222/* ============================================================================================== */
229bool initLora() {
230
231 // Initialise GPIO to SX1272 DIO as input
232 GPIOpin_t loraDIO = GPIOpin_init(GPIOD, GPIO_PIN1, &GPIO_CONFIG_INPUT);
233
234 static SPI_t spiLora;
235 // Configure SX1272 SPI interface
236 SPI_Config spiLoraConfig = SPI_CONFIG_DEFAULT; // Using default settings as base
237 spiLoraConfig.CPHA = SPI_CPHA_FIRST; // Begin on first clock edge
238 spiLoraConfig.CPOL = SPI_CPOL0; // Idle clock low
239 spiLora = SPI_init(LORA_SPI_INTERFACE, &spiLoraConfig);
240
241 // ==========================================================================
242 // LORA
243 //
244 // LoRa transceiver for external wireless communicatons. Can be configured to
245 // either receive or transmit data.
246 GPIOpin_t loraCS = GPIOpin_init(LORA_CS, NULL);
247 static SX1272_t lora;
249 &lora,
250 &spiLora,
251 loraCS,
252 LORA_BW, // Set LoRa bandwidth to 500KHz
253 LORA_SF, // Spreading factor 9
254 LORA_CR // Coding rate 4/5
255 );
256 deviceList[DEVICE_LORA].deviceName = "LoRa";
257 deviceList[DEVICE_LORA].device = &lora;
258
259 // @TODO: add in error checking
260 return true;
261}
262
263/* ============================================================================================== */
270bool initUart() {
271
272 // ==========================================================================
273 // USB UART
274 //
275 // UART device allowing communication via USB through an FTDI bridge. This
276 // particular UART output provides interaction to the system via shell and
277 // debug print output.
278 static UART_t uart;
279 UART_init(
280 &uart,
281 USB_INTERFACE, // Memory mapped address of UART interface for USB
282 USB_PORT, // GPIO port connecting UART data pins
283 USB_PINS, // Position of data pins in GPIO prt
284 USB_BAUD, // Baud rate setting of UART communications
285 USB_OVERSAMPLE // OVER8 mode on/off
286 );
287 deviceList[DEVICE_UART_USB].deviceName = "USB";
288 deviceList[DEVICE_UART_USB].device = &uart;
289
290 // ==========================================================================
291 // GPS
292 //
293 // GPS device for low frequency positional readings. Commands are sent and
294 // data received via the UART interface.
295 static SAM_M10Q_t gps;
296 GPS_init(
297 &gps,
298 GPS_INTERFACE, // Memory mapped address of UART interface for GPS
299 GPS_PORT, // GPIO port connecting UART data pins
300 GPS_PINS, // Position of data pins in GPIO prt
301 GPS_BAUD // Baud rate setting of UART communications
302 );
303 deviceList[DEVICE_GPS].deviceName = "GPS";
304 deviceList[DEVICE_GPS].device = &gps;
305
306 // @TODO: add in error checking
307 return true;
308}
309
310// !! ABANDON HOPE ALL YE WHO ENTER HERE !!
311// 🤢🤮
312
313// TODO: get rid of this shite vvv
314
315extern uint32_t __state_vector_start;
316extern uint32_t __state_vector_end;
317
318StateHandle_t StateHandle_getHandle(char *name) {
319 StateHandle_t *handleRef = StateHandle_getHandleRef(name);
320 return (handleRef == NULL) ? (StateHandle_t){"NULL", NULL} : *handleRef;
321}
322
323StateHandle_t *StateHandle_getHandleRef(char *name) {
324 // Iterate through all handles in State vector
325 for (uint8_t *i = (uint8_t *)&__state_vector_start; i < (uint8_t *)&__state_vector_end; i += sizeof(StateHandle_t)) {
326 StateHandle_t *handle = (StateHandle_t *)i;
327 // Return State handle if names match
328 if (!strcmp(handle->name, name)) {
329 handle->ref = handle;
330 return handle;
331 }
332 }
333 // Return NULL pointer if no matching State is found
334 return NULL;
335}
A3G4250D_t A3G4250D_init(A3G4250D_t *, SPI_t *, GPIOpin_t, const float, const uint8_t *, const int8_t *)
Initialiser for a A3G4250D gyroscope.
Definition a3g4250d.c:28
BMP581_t BMP581_init(BMP581_t *, SPI_t *, GPIOpin_t, const float, const float)
Initialiser for a BMP581 barometer.
Definition bmp581.c:27
void DeviceList_init(DeviceHandle_t deviceList[DEVICE_MAX_KEYS])
Initialise all system devices.
Definition devicelist.c:23
GPIO_Mode mode
Pin I/O direction | (default GPIO_MODE_OUTPUT)
Definition gpiopin.h:139
GPIO_AF afr
Pin alternate function | (default GPIO_AF0)
Definition gpiopin.h:143
GPIOpin_t GPIOpin_init(GPIO_TypeDef *, GPIO_Pin, GPIO_Config *)
Initialiser for a GPIO peripheral pin interface.
Definition gpiopin.c:28
@ GPIO_PIN1
Pin 1.
Definition gpiopin.h:51
@ GPIO_MODE_AF
Alternate function mode.
Definition gpiopin.h:98
Struct definition for GPIO configuration.
Definition gpiopin.h:138
Struct definition for a GPIO pin.
Definition gpiopin.h:151
KX134_1211_t KX134_1211_init(KX134_1211_t *, SPI_t *, GPIOpin_t, const uint8_t, const uint8_t *, const int8_t *)
Initialiser for a KX134-1211 accelerometer.
Definition kx134_1211.c:27
SPI_Polarity CPOL
SPI clock polarity | (default SPI_CPOL1)
Definition spi.h:115
SPI_Phase CPHA
SPI clock phase | (default SPI_CPHA_SECOND)
Definition spi.h:114
SPI_t SPI_init(SPI_TypeDef *, SPI_Config *)
Initialiser for an SPI device interface.
Definition spi.c:36
SPI CR1 configuration struct.
Definition spi.h:113
Struct definition for SPI interface. Provides the interface for API consumers to interact with the SP...
Definition spi.h:134
SX1272_t SX1272_init(SX1272_t *, SPI_t *, GPIOpin_t, SX1272_Bandwidth, SX1272_SpreadingFactor, SX1272_CodingRate)
Initializes the LoRa module with specified configuration parameters.
Definition sx1272.c:27
Struct definition for SX1272. Provides the interface for API consumers to interact with the SX1272 Lo...
Definition sx1272.h:133
UART_t UART_init(UART_t *, USART_TypeDef *, GPIO_TypeDef *, UART_Pins, uint32_t, OversampleMode)
Initialiser for a UART device interface.
Definition uart.c:26
Struct definition for UART interface.
Definition uart.h:52
W25Q128_t W25Q128_init(W25Q128_t *, SPI_t *, GPIOpin_t)
Initialise flash struct.
Definition w25q128.c:23