GCC Code Coverage Report


Directory: ../../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 84.8% 84 / 0 / 99
Functions: 100.0% 2 / 0 / 2
Branches: 40.0% 8 / 0 / 20

app/rev2/main.c
Line Branch Exec Source
1 /*******************************************************************************
2 *
3 * FILE:
4 * main.c
5 *
6 * DESCRIPTION:
7 * APPA - All-Purpose Primary Avionics
8 *
9 * Collects the features from Data Logger, Dual Deploy, and Canard into
10 * one comprehensive & configurable application for the flight computer.
11 *
12 * CRITICALITY:
13 * NFQ - Non-Flight Qualified
14 *
15 * COPYRIGHT:
16 * Copyright (c) 2025 Sun Devil Rocketry.
17 * All rights reserved.
18 *
19 * This software is licensed under terms that can be found in the LICENSE
20 * file in the root directory of this software component.
21 * If no LICENSE file comes with this software, it is covered under the
22 * BSD-3-Clause.
23 *
24 * https://opensource.org/license/bsd-3-clause
25 *
26 *******************************************************************************/
27
28
29 /*------------------------------------------------------------------------------
30 Standard Includes
31 ------------------------------------------------------------------------------*/
32 #include <stdbool.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include "sdr_pin_defines_A0002.h"
36
37
38 /*------------------------------------------------------------------------------
39 Project Includes
40 ------------------------------------------------------------------------------*/
41
42 /* Application Layer */
43 #include "main.h"
44 #include "init.h"
45
46 /* Low-level modules */
47 #include "math_sdr.h"
48 #include "baro.h"
49 #include "buzzer.h"
50 #include "commands.h"
51 #include "debug_sdr.h"
52 #include "error_sdr.h"
53 #include "flash.h"
54 #include "ignition.h"
55 #include "imu.h"
56 #include "led.h"
57 #include "lora.h"
58 #include "timer.h"
59 #include "sensor.h"
60 #include "servo.h"
61 #include "usb.h"
62 #include "gps.h"
63
64 /* debug/emulator includes */
65 #ifdef EMULATOR
66 #include "emulator.h"
67 #endif
68
69
70 /*------------------------------------------------------------------------------
71 MCU Peripheral Handles
72 ------------------------------------------------------------------------------*/
73 I2C_HandleTypeDef hi2c1; /* Baro sensor */
74 I2C_HandleTypeDef hi2c2; /* IMU and GPS */
75 SPI_HandleTypeDef hspi2; /* External flash */
76 TIM_HandleTypeDef htim4; /* Buzzer Timer */
77 UART_HandleTypeDef huart6; /* USB */
78 UART_HandleTypeDef huart4; /* GPS */
79 TIM_HandleTypeDef htim3; /* 123 PWM Timer */
80 TIM_HandleTypeDef htim2; /* 4 PWN Timer */
81 TIM_HandleTypeDef htim5; /* Microsecond Timer */
82 SPI_HandleTypeDef hspi4; /* LORA SPI */
83
84 /* GPS Data */
85 uint8_t gps_mesg_byte = 0;
86 uint8_t rx_buffer[GPSBUFSIZE];
87 uint8_t rx_index = 0;
88 GPS_DATA gps_data;
89
90 /* Presets */
91 IMU_OFFSET imu_offset = {0.00, 0.00, 0.00, 0.00, 0.00, 0.00};
92 BARO_PRESET baro_preset = {0.00, 0.00};
93 SERVO_PRESET servo_preset = { 45, 45, 45, 45 };
94 CONFIG_SETTINGS_TYPE config_settings;
95 PRESET_DATA preset_data;
96
97 /* Sensors */
98 SENSOR_DATA sensor_data; /* All sensor data */
99
100 /* Timing (sensors) */
101 uint32_t previous_time = 0;
102 uint32_t tdelta = 0;
103
104 /* Timing (debug) */
105 #ifdef DEBUG
106 volatile uint32_t debug_previous = 0;
107 volatile uint32_t debug_delta = 0;
108 #endif
109
110
111 /* PID */
112 PID_DATA pid_data = { 0.0f, 0.0f, 0.0f };
113
114 /*------------------------------------------------------------------------------
115 Static Function Declarations
116 ------------------------------------------------------------------------------*/
117 static void debug_writer
118 (
119 void* msg,
120 size_t len
121 );
122
123 /*------------------------------------------------------------------------------
124 Application entry point
125 ------------------------------------------------------------------------------*/
126 #if defined( UNIT_TEST ) || defined( EMULATOR )
127 2 int main_fut
128 #else
129 int main
130 #endif
131 (
132 void
133 )
134 {
135 /*------------------------------------------------------------------------------
136 Local Variables
137 ------------------------------------------------------------------------------*/
138
139 /* General Board configuration */
140 2 uint8_t firmware_code; /* Firmware version code */
141
142 /* FLASH */
143 2 FLASH_STATUS flash_status; /* Status of flash driver */
144 2 HFLASH_BUFFER flash_handle; /* Flash API buffer handle */
145 2 uint8_t flash_buffer[ DEF_FLASH_BUFFER_SIZE ]; /* Flash Data buffer */
146
147 /* Sensors */
148 2 BARO_STATUS baro_status; /* Status of baro sensor */
149 2 BARO_CONFIG baro_configs; /* Baro sensor config settings */
150 2 IMU_STATUS imu_status; /* IMU return codes */
151 2 IMU_CONFIG imu_configs; /* IMU config settings */
152 2 SENSOR_STATUS sensor_status; /* Sensor module return codes */
153
154 /* Servo */
155 2 SERVO_STATUS servo_status;
156
157 /* Flash */
158 2 uint32_t flash_address;
159
160 /*------------------------------------------------------------------------------
161 Variable Initializations
162 ------------------------------------------------------------------------------*/
163
164 /* FLASH */
165 2 flash_handle.write_protected = FLASH_WP_WRITE_ENABLED;
166 2 flash_handle.num_bytes = 0;
167 2 flash_handle.pbuffer = &flash_buffer[0];
168 2 flash_handle.address = 0;
169 2 flash_handle.status_register = 0xFF;
170 2 flash_handle.bpl_bits = FLASH_BPL_NONE;
171 2 flash_handle.bpl_write_protect = FLASH_BPL_READ_WRITE;
172
173 /* Baro sensor configurations */
174 2 baro_configs.enable = BARO_PRESS_TEMP_ENABLED;
175 2 baro_configs.mode = BARO_NORMAL_MODE;
176 2 baro_configs.press_OSR_setting = BARO_PRESS_OSR_X1;
177 2 baro_configs.temp_OSR_setting = BARO_TEMP_OSR_X1;
178 2 baro_configs.ODR_setting = BARO_ODR_200HZ;
179 2 baro_configs.IIR_setting = BARO_IIR_COEF_0;
180
181 /* IMU Configurations */
182 2 imu_configs.sensor_enable = IMU_ENABLE_GYRO_ACC_TEMP;
183 2 imu_configs.acc_odr = IMU_ODR_1K6;
184 2 imu_configs.gyro_odr = IMU_ODR_1K6;
185 2 imu_configs.mag_odr = MAG_ODR_10HZ;
186 2 imu_configs.acc_filter = IMU_FILTER_NORM_AVG4;
187 2 imu_configs.gyro_filter = IMU_FILTER_NORM_AVG4;
188 2 imu_configs.acc_filter_mode = IMU_FILTER_FILTER_MODE;
189 2 imu_configs.gyro_filter_mode = IMU_FILTER_FILTER_MODE;
190 2 imu_configs.acc_range = IMU_ACC_RANGE_16G;
191 2 imu_configs.gyro_range = IMU_GYRO_RANGE_2000;
192 2 imu_configs.mag_op_mode = MAG_NORMAL_MODE;
193 2 imu_configs.mag_xy_repititions = 9; /* BMM150 Regular Preset Recomendation */
194 2 imu_configs.mag_z_repititions = 15;
195
196 /* Initialize config settings to default */
197 2 memset( &config_settings, 0, sizeof( config_settings ) );
198
199 /* Preset initialization */
200 2 preset_data.imu_offset = imu_offset;
201 2 preset_data.baro_preset = baro_preset;
202 2 preset_data.servo_preset = servo_preset;
203 2 preset_data.config_settings = config_settings;
204 2 flash_address = 0;
205
206 /* Module return codes */
207 2 baro_status = BARO_OK;
208 2 flash_status = FLASH_OK;
209 2 sensor_status = SENSOR_OK;
210
211 /* General Board configuration */
212 2 firmware_code = FIRMWARE_APPA;
213
214 /*------------------------------------------------------------------------------
215 MCU/HAL Initialization
216 ------------------------------------------------------------------------------*/
217 2 HAL_Init (); /* Reset peripherals, initialize flash interface
218 and Systick. */
219 2 SystemClock_Config (); /* System clock */
220 2 PeriphCommonClock_Config(); /* Common Peripherals clock */
221 2 GPIO_Init (); /* GPIO */
222 2 USB_UART_Init (); /* USB UART */
223 2 GPS_UART_Init (); /* GPS UART */
224 2 Baro_I2C_Init (); /* Barometric pressure sensor */
225 2 IMU_GPS_I2C_Init (); /* IMU and GPS */
226 2 FLASH_SPI_Init (); /* External flash chip */
227 2 BUZZER_TIM_Init (); /* Buzzer */
228 2 MICRO_TIM_Init (); /* Microsecond timer */
229
230 2 PWM4_TIM_Init (); /* PWM Timer for Servo 4 */
231 2 PWM123_TIM_Init (); /* PWM Timer for Servo 1,2,3 */
232
233 2 LORA_SPI_Init (); /* LoRa SPI */
234
235 /* Initialize the debug interface */
236 2 (void)debug_init( debug_writer, NULL );
237
238 /*------------------------------------------------------------------------------
239 External Hardware Initializations
240 ------------------------------------------------------------------------------*/
241
242 /* Flash Chip */
243 2 flash_status = flash_init( &flash_handle );
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ( flash_status != FLASH_OK )
245 {
246 ✗ error_fail_fast( ERROR_FLASH_INIT_ERROR );
247 }
248
249 /* Barometric pressure sensor */
250 2 baro_status = baro_init( &baro_configs );
251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 while ( baro_status != BARO_OK )
252 {
253 ✗ HAL_Delay(10);
254 ✗ baro_status = baro_init( &baro_configs );
255
256 ✗ if( HAL_GetTick() > I2C_INIT_TIMEOUT )
257 {
258 ✗ error_fail_fast( ERROR_BARO_INIT_ERROR );
259 }
260 }
261
262 /* IMU */
263 2 imu_status = imu_init( &imu_configs );
264
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 while ( imu_status != IMU_OK )
265 {
266 ✗ HAL_Delay(10);
267 ✗ imu_status = imu_init( &imu_configs );
268
269 ✗ if( HAL_GetTick() > I2C_INIT_TIMEOUT )
270 {
271 ✗ error_fail_fast( ERROR_IMU_INIT_ERROR );
272 }
273 }
274
275 /* Servo */
276 2 servo_status = servo_init();
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ( servo_status != SERVO_OK )
278 {
279 ✗ error_fail_fast( ERROR_SERVO_INIT_ERROR );
280 }
281
282 /* Sensor */
283 2 sensor_status = sensor_init( &preset_data );
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ( sensor_status != SENSOR_OK )
285 {
286 ✗ error_fail_fast( ERROR_SENSOR_CMD_ERROR );
287 }
288
289
290 /*------------------------------------------------------------------------------
291 Setup safety checks
292 ------------------------------------------------------------------------------*/
293
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ( ign_switch_cont() ) /* Check switch pin */
294 {
295 ✗ error_fail_fast( ERROR_DATA_HAZARD_ERROR );
296 }
297
298 /*------------------------------------------------------------------------------
299 Load saved parameters
300 ------------------------------------------------------------------------------*/
301 2 FLASH_STATUS read_status;
302 2 read_status = read_preset( &flash_handle, &flash_address );
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ( read_status == FLASH_FAIL )
304 {
305 ✗ error_fail_fast( ERROR_FLASH_CMD_ERROR );
306 }
307
308 /* Set orientation based on saved data */
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ( preset_data.imu_offset.accel_x < 0.0f )
310 {
311 ✗ sensor_set_mount_orientation( MOUNT_ORIENTATION_IMU_INVERTED );
312 }
313 else
314 {
315 2 sensor_set_mount_orientation( MOUNT_ORIENTATION_IMU_NORMAL );
316 }
317
318 /*------------------------------------------------------------------------------
319 End of init // Begin program
320 ------------------------------------------------------------------------------*/
321 2 appa_fsm
322 (
323 firmware_code,
324 &flash_status,
325 &flash_handle,
326 &flash_address,
327 &gps_mesg_byte,
328 &sensor_status
329 );
330
331 ✗ return -1;
332
333 } /* main */
334
335
336 /*******************************************************************************
337 * *
338 * PROCEDURE: *
339 * debug_writer *
340 * *
341 * DESCRIPTION: *
342 * Handle debug console outputs. *
343 * *
344 *******************************************************************************/
345
346 2 static void debug_writer
347 (
348 void* msg,
349 size_t len
350 )
351 {
352 #if defined( DEBUG ) && !defined( EMULATOR )
353 // ETS Temp: Disabled for now, but we'll hopefully
354 // have a tool to debug rev 2 soon!
355 //
356 // The true intention with this one is to use
357 // it on Rev 3.
358 // for( int i = 0; i < len; i++)
359 // {
360 // ITM_SendChar(*((char*)(msg + i)));
361 // }
362 debug_callback_handler();
363
364 #elif defined( EMULATOR )
365 2 emulator_debug_log( (char*)msg, len, EMULATOR_SUBSYSTEM_FIRMWARE );
366 2 debug_callback_handler();
367 #else
368 /* Do nothing in release */
369 debug_callback_handler();
370 #endif
371 2 } /* debug_writer */
372
373 /*******************************************************************************
374 * END OF FILE *
375 *******************************************************************************/
376