@@ -124,7 +124,6 @@ extern page_t curr_page;
124124volatile dashboard_input_state_t input_state = {0 }; // Clear all input states
125125
126126/* Function Prototypes */
127- void preflight_sequence (void );
128127void preflightAnimation (void );
129128void heartBeatLED ();
130129void lcdTxUpdate ();
@@ -137,12 +136,13 @@ extern void HardFault_Handler();
137136
138137// Communication queues
139138q_handle_t q_tx_usart ;
140- bool g_is_preflight_complete = false;
141139
142140void preflight_task ();
143141void can_worker_task ();
144142
145- defineThreadStack (preflight_task , 10 , osPriorityHigh , 1024 );
143+ // The preflight thread must not be interrupted, give highest priority
144+ defineThreadStack (preflight_task , 10 , osPriorityRealtime , 1024 );
145+
146146defineThreadStack (pedalsPeriodic , FILT_THROTTLE_BRAKE_PERIOD_MS , osPriorityHigh , 512 );
147147defineThreadStack (can_worker_task , 20 , osPriorityNormal , 512 );
148148
@@ -156,20 +156,29 @@ defineThreadStack(sendTVParameters, DASHBOARD_VCU_PARAMETERS_PERIOD_MS, osPriori
156156void preflight_task () {
157157 static uint8_t counter = 0 ;
158158
159- // run animation until preflight complete for at least 1.5 seconds
160- if (g_is_preflight_complete && ( counter >= 150 ) ) {
159+ // run animation until for at least 1.5 seconds
160+ if (counter >= 150 ) {
161161 PHAL_writeGPIO (HEART_LED_GPIO_Port , HEART_LED_Pin , 0 );
162162 PHAL_writeGPIO (CONN_LED_GPIO_Port , CONN_LED_Pin , 0 );
163163 PHAL_writeGPIO (ERROR_LED_GPIO_Port , ERROR_LED_Pin , 0 );
164+
165+ // spawn the other threads
166+ createThread (pedalsPeriodic );
167+ createThread (can_worker_task );
168+
169+ createThread (updateFaultDisplay )
170+ createThread (heartBeatLED )
171+ createThread (handleDashboardInputs )
172+ createThread (sendVersion )
173+ createThread (updateTelemetryPages )
174+ createThread (sendTVParameters )
164175 osThreadExit (); // Self delete
165176 return ;
166177 }
167178
168179 if (counter % 10 == 0 ) { // Run every 100ms
169180 preflightAnimation ();
170181 }
171-
172- preflight_sequence ();
173182
174183 counter ++ ;
175184}
@@ -185,15 +194,35 @@ void can_worker_task() {
185194}
186195
187196int main (void ) {
188- osKernelInitialize ();
189-
190- /* HAL Initilization */
197+ // Hardware Initialization
191198 if (0 != PHAL_configureClockRates (& clock_config )) {
192199 HardFault_Handler ();
193200 }
194201 if (false == PHAL_initGPIO (gpio_config , sizeof (gpio_config ) / sizeof (GPIOInitConfig_t ))) {
195202 HardFault_Handler ();
196203 }
204+ if (false == PHAL_FDCAN_init (FDCAN2 , false, VCAN_BAUD_RATE )) {
205+ HardFault_Handler ();
206+ }
207+ if (false == PHAL_initUSART (& lcd , APB2ClockRateHz )) {
208+ HardFault_Handler ();
209+ }
210+ if (false == PHAL_initADC (& adc_config , adc_channel_config , sizeof (adc_channel_config ) / sizeof (ADCChannelConfig_t ))) {
211+ HardFault_Handler ();
212+ }
213+ if (false == PHAL_initDMA (& adc_dma_config )) {
214+ HardFault_Handler ();
215+ }
216+ PHAL_startTxfer (& adc_dma_config );
217+ PHAL_startADC (& adc_config );
218+
219+ // Begin Software Init tasks
220+ osKernelInitialize ();
221+
222+ CAN_library_init ();
223+ initLCD ();
224+ NVIC_EnableIRQ (FDCAN2_IT0_IRQn );
225+ enableInterrupts ();
197226
198227 // show signs of life
199228 PHAL_writeGPIO (BMS_LED_GPIO_Port , BMS_LED_Pin , 1 );
@@ -213,67 +242,6 @@ int main(void) {
213242 return 0 ;
214243}
215244
216- /**
217- * @brief Performs sequential initialization and setup of system peripherals and modules.
218- *
219- * @note Called repeatedly until preflight is registered as complete
220- */
221- void preflight_sequence (void ) {
222- static uint8_t step_10ms = 0 ;
223-
224- switch (step_10ms ++ ) {
225- case 0 :
226- if (false == PHAL_FDCAN_init (FDCAN2 , false, VCAN_BAUD_RATE )) {
227- HardFault_Handler ();
228- }
229- NVIC_EnableIRQ (FDCAN2_IT0_IRQn );
230- break ;
231- case 1 :
232- if (false == PHAL_initUSART (& lcd , APB2ClockRateHz )) {
233- HardFault_Handler ();
234- }
235- break ;
236- case 2 :
237- if (false == PHAL_initADC (& adc_config , adc_channel_config , sizeof (adc_channel_config ) / sizeof (ADCChannelConfig_t ))) {
238- HardFault_Handler ();
239- }
240- if (false == PHAL_initDMA (& adc_dma_config )) {
241- HardFault_Handler ();
242- }
243- PHAL_startTxfer (& adc_dma_config );
244- PHAL_startADC (& adc_config );
245- break ;
246- case 3 :
247- /* Module Initialization */
248- CAN_library_init ();
249- break ;
250- case 4 :
251- enableInterrupts ();
252- break ;
253- case 5 :
254- initLCD ();
255- break ;
256- case 6 : {
257- // create the other tasks here
258- createThread (pedalsPeriodic );
259- createThread (can_worker_task );
260-
261- createThread (updateFaultDisplay )
262- createThread (heartBeatLED )
263- createThread (handleDashboardInputs )
264- createThread (sendVersion )
265- createThread (updateTelemetryPages )
266- createThread (sendTVParameters )
267- break ;
268- }
269- default : {
270- g_is_preflight_complete = true;
271- step_10ms = 255 ; // prevent wrap around
272- break ;
273- }
274- }
275- }
276-
277245void preflightAnimation (void ) {
278246 // Controls external LEDs since they are more visible when dash is in car
279247 static uint32_t external_index ;
0 commit comments