Loading...
Searching...
No Matches
main.c
1/***********************************************************************************
2 * @file main.c *
3 * @author Matt Ricci *
4 * @brief Main application entry point and system initialization. *
5 ***********************************************************************************/
6
7#include "main.h"
8
9#include "devices.h"
10#include "tasklist.h"
11#include "rcc.h"
12
13#include "tasks.h"
14
15long hDummyIdx = 0;
16long lDummyIdx = 0;
17
18// RTOS event groups
19EventGroupHandle_t xTaskEnableGroup; // 0: FLASH, 1: HIGHRES, 2: LOWRES, 3: LORA, 7: IDLE
20EventGroupHandle_t xSystemStatusGroup; // 0-2: Flight state, 3: Payload, 4: Aerobrakes
21EventGroupHandle_t xMsgReadyGroup; // 0: LORA, 1: USB
22
23// RTOS message buffers
24MessageBufferHandle_t xLoRaTxBuff;
25MessageBufferHandle_t xUsbTxBuff;
26
27StreamBufferHandle_t xUsbRxBuff;
28
29StreamBufferHandle_t xGpsRxBuff;
30
31// RTOS mutexes
32SemaphoreHandle_t xUsbMutex;
33
34/* =============================================================================== */
43
44int main(void) {
45
46 #ifdef TRACE
47 xTraceInitialize();
48 #endif
49
50 // Create and start the system initialization task
51 TaskHandle_t xSystemInitHandle;
52 xTaskCreate(vSystemInit, "SystemInit", 16192, NULL, configMAX_PRIORITIES, &xSystemInitHandle);
53 vTaskStartScheduler();
54
55 // The scheduler should never return
56 return 0;
57}
58
59/* =============================================================================== */
73
74void vSystemInit(void *argument) {
75
76 // Allow time for external devices to finish startup sequences
77 vTaskDelay(pdMS_TO_TICKS(500));
78
79 vTaskSuspendAll();
80
81 // TODO: Replace usage of event groups for xTaskEnableGroup and xMsgReadyGroup
82 // with direct-to-task notifications, for better efficiency and clarity as
83 // these flags aren't shared across multiple tasks.
84
85 // Initialise event groups for task synchronization and message signaling
86 xTaskEnableGroup = xEventGroupCreate(); // 0: FLASH, 1: HIGHRES, 2: LOWRES, 3: LORA, 7: IDLE
87 xSystemStatusGroup = xEventGroupCreate();
88 xMsgReadyGroup = xEventGroupCreate();
89 xEventGroupSetBits(xMsgReadyGroup, GROUP_MESSAGE_READY_LORA);
90
91 // Initialise USB buffers and mutex
92 xUsbTxBuff = xMessageBufferCreate(USB_TX_SIZE);
93 xUsbRxBuff = xStreamBufferCreate(USB_RX_SIZE, 1);
94 xGpsRxBuff = xStreamBufferCreate(GPS_RX_SIZE, 1);
95
96 xUsbMutex = xSemaphoreCreateMutex();
97
98 // Initialise LoRa buffer
99 xLoRaTxBuff = xMessageBufferCreate(LORA_BUFF_SIZE);
100
101 /* -------------------------- Device Initialization ---------------------------- */
102
103 // TODO: Extract RCC initialisation to hardware specific target files in Target/
104 // subdirectories. These would be specified under an initRCC() function
105 // defined in the target specific source, and called by main() here.
106
107 // Make sure all peripherals we will use are enabled
108 RCC_START_PERIPHERAL(AHB1, GPIOA);
109 RCC_START_PERIPHERAL(AHB1, GPIOB);
110 RCC_START_PERIPHERAL(AHB1, GPIOC);
111 RCC_START_PERIPHERAL(AHB1, GPIOD);
112 RCC_START_PERIPHERAL(AHB1, GPIOE);
113 RCC_START_PERIPHERAL(AHB1, GPIOF);
114 RCC_START_PERIPHERAL(APB2, SPI1);
115 RCC_START_PERIPHERAL(APB1, SPI3);
116 RCC_START_PERIPHERAL(APB2, SPI4);
117 RCC_START_PERIPHERAL(APB1, TIM6);
118 RCC_START_PERIPHERAL(APB1, USART3);
119 RCC_START_PERIPHERAL(APB2, USART6);
120 RCC_START_PERIPHERAL(APB2, SYSCFG); // DON'T FORGET TO ENABLE THIS ONE LOL
121
122 // TODO: As with the RCC, extract interrupt configuration to hardware specific
123 // target files in Target/ subdirectories. As some of the IRQ handlers will
124 // need to remain defined in RTOS application source files, the exact names
125 // of the defined functions may be formatted via preprocessor macro.
126
127 // Enable peripheral and external interrupts
129
130 // Start up drivers
131 initDevices();
132
133 // Initialise circular memory buffer
134 MemBuff _mem;
135 static StateHandle_t __attribute__((section(".state_mem"), unused)) mem;
136 mem.state = &_mem;
137 memcpy(mem.name, "Memory", STATE_NAME_LENGTH);
138
139 uint8_t buff[MEM_BUFF_SIZE];
140 MemBuff_init(&_mem, buff, MEM_BUFF_SIZE, FLASH_PAGE_SIZE);
141
142 // Initialise shell
143 static Shell shell;
144 Shell_init(&shell);
145
146 /* --------------------------- State Initialization -----------------------------*/
147
148 // TODO: Get rid of this shit vvv
149
150 // Tilt state variable
151 static StateHandle_t __attribute__((section(".state_tilt"), unused)) tilt;
152 static float _tilt = 0.0f;
153 tilt.state = &_tilt;
154 memcpy(tilt.name, "Tilt", STATE_NAME_LENGTH);
155
156 // Cosine state variable
157 static StateHandle_t __attribute__((section(".state_cosine"), unused)) cosine;
158 static float _cosine = 0.0f;
159 cosine.state = &_cosine;
160 memcpy(cosine.name, "Cosine", STATE_NAME_LENGTH);
161
162 // Altitude state variable
163 static StateHandle_t __attribute__((section(".state_altitude"), unused)) altitude;
164 static float _altitude = 0.0f;
165 altitude.state = &_altitude;
166 memcpy(altitude.name, "Altitude", STATE_NAME_LENGTH);
167
168 // Velocity state variable
169 static StateHandle_t __attribute__((section(".state_velocity"), unused)) velocity;
170 static float _velocity = 0.0f;
171 velocity.state = &_velocity;
172 memcpy(velocity.name, "Velocity", STATE_NAME_LENGTH);
173
174 // Flight state variable
175 static StateHandle_t __attribute__((section(".state_flightState"), unused)) flightState;
176 static enum State _flightState = PRELAUNCH;
177 flightState.state = &_flightState;
178 memcpy(flightState.name, "FlightState", STATE_NAME_LENGTH);
179
180 // Rotation quaternion state variable
181 static StateHandle_t __attribute__((section(".state_qRot"), unused)) qRot;
182 static Quaternion _qRot;
183 Quaternion_init(&_qRot);
184 qRot.state = &_qRot;
185 memcpy(qRot.name, "RotationQuaternion", STATE_NAME_LENGTH);
186
187 // Attitude vector state variable
188 static StateHandle_t __attribute__((section(".state_vAttitude"), unused)) vAttitude;
189 static float _vAttitude[3] = {0, 0, 1};
190 vAttitude.state = _vAttitude;
191 memcpy(vAttitude.name, "AttitudeVector", STATE_NAME_LENGTH);
192
193 // Attitude vector state variable
194 static StateHandle_t __attribute__((section(".state_vLaunch"), unused)) vLaunch;
195 static float _vLaunch[3] = {0, 0, 1};
196 vLaunch.state = _vLaunch;
197 memcpy(vLaunch.name, "LaunchVector", STATE_NAME_LENGTH);
198
199 // Sliding window average velocity
200 static StateHandle_t __attribute__((section(".state_avgVel"), unused)) avgVel;
201 static SlidingWindow _avgVel = {};
202 avgVel.state = &_avgVel;
203 memcpy(avgVel.name, "AvgVelBuffer", STATE_NAME_LENGTH);
204
205 // Sliding window average velocity
206 static StateHandle_t __attribute__((section(".state_avgPress"), unused)) avgPress;
207 static SlidingWindow _avgPress = {};
208 avgPress.state = &_avgPress;
209 memcpy(avgPress.name, "AvgPressBuffer", STATE_NAME_LENGTH);
210
211 // Initialize pressure sliding window average
212 float avgPressBuff[AVG_BUFF_SIZE];
213 SlidingWindow_init(avgPress.state, avgPressBuff, AVG_BUFF_SIZE);
214
215 // Initialize velocity sliding window average
216 float avgVelBuff[AVG_BUFF_SIZE];
217 SlidingWindow_init(avgVel.state, avgVelBuff, AVG_BUFF_SIZE);
218
219 /**********************************************************************************
220 * TASK INIT *
221 **********************************************************************************/
222
223 // TODO: Replace task handle struct with static array of handles, i.e.
224 // TaskHandle_t handles[SIZE]
225
226 // TODO: Extract task initialisation to hardware specific target files in Target/
227 // subdirectories. These would be specified under an initTasks() function
228 // defined in the target specific source, and called by main() here.
229
230 xTaskCreate(vHDataAcquisition, "HDataAcq", 512, &_mem, configMAX_PRIORITIES - 2, TaskList_new());
231 xTaskCreate(vLDataAcquisition, "LDataAcq", 512, &_mem, configMAX_PRIORITIES - 3, TaskList_new());
232 xTaskCreate(vStateUpdate, "StateUpdate", 512, NULL, configMAX_PRIORITIES - 4, TaskList_new());
233 xTaskCreate(vFlashBuffer, "FlashData", 512, &_mem, configMAX_PRIORITIES - 1, TaskList_new());
234 xTaskCreate(vUsbTransmit, "UsbTx", 256, NULL, configMAX_PRIORITIES - 6, TaskList_new());
235 xTaskCreate(vUsbReceive, "UsbRx", 256, &shell, configMAX_PRIORITIES - 6, TaskList_new());
236 xTaskCreate(vIdle, "Idle", 256, &_mem, tskIDLE_PRIORITY, TaskList_new());
237
238 initTasks();
239
240 // TODO: Temporarily disabled due to bug related to use of message buffer.
241 // See gpsacquisition.c todo for more detail.
242
243 // xTaskCreate(vGpsTransmit, "GpsRead", 512, NULL, configMAX_PRIORITIES - 6, &handles.xGpsTransmitHandle);
244
245 xTaskResumeAll();
246
247 #ifdef TRACE
248 xTraceEnable(TRC_START);
249 #endif
250
251 vTaskSuspend(NULL);
252}
253
258 __disable_irq();
259 NVIC_SetPriority(EXTI1_IRQn, 9);
260 NVIC_EnableIRQ(EXTI1_IRQn);
261 NVIC_SetPriority(USART6_IRQn, 10);
262 NVIC_EnableIRQ(USART6_IRQn);
263 NVIC_SetPriority(USART3_IRQn, 10);
264 NVIC_EnableIRQ(USART3_IRQn);
265 EXTI->RTSR |= 0x02;
266 EXTI->IMR |= 0x02;
267 SYSCFG->EXTICR[0] &= ~0xF0;
268 SYSCFG->EXTICR[0] = 0x30;
269 __enable_irq();
270}
@ USART6_IRQn
@ EXTI1_IRQn
Definition stm32f439xx.h:84
@ USART3_IRQn
int Shell_init(Shell *)
Initializes the shell, registering programs from shell vector.
Definition shell.c:53
Struct definition for shell interface.
Definition shell.h:48
TaskHandle_t * TaskList_new()
Retrieve a pointer to the first empty task handle in list.
Definition tasklist.c:42
void vSystemInit(void *pvParameters)
Initialisation of RTOS tasks.
Definition main.c:74
void configure_interrupts()
Definition main.c:257