Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
stm32f7xx_it.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
18/* USER CODE END Header */
19
20/* Includes ------------------------------------------------------------------*/
21#include "main.h"
22#include "stm32f7xx_it.h"
23#include "FreeRTOS.h"
24#include "task.h"
25/* Private includes ----------------------------------------------------------*/
26/* USER CODE BEGIN Includes */
27#include "../Inc/Sensors/AnalogSensor.h"
28#include "../Inc/Sensors/DigitalSensor.h"
29#include "../Inc/Outputs/DigitalOutput.h"
30#include "../Inc/Utils/Telemetry.h"
31#include "../Inc/Utils/MessageFormat.h"
32/* USER CODE END Includes */
33
34/* Private typedef -----------------------------------------------------------*/
35/* USER CODE BEGIN TD */
36
37/* USER CODE END TD */
38
39/* Private define ------------------------------------------------------------*/
40/* USER CODE BEGIN PD */
41
42/* USER CODE END PD */
43
44/* Private macro -------------------------------------------------------------*/
45/* USER CODE BEGIN PM */
46
47/* USER CODE END PM */
48
49/* Private variables ---------------------------------------------------------*/
50/* USER CODE BEGIN PV */
51// Receive message buffers
52CAN_RxHeaderTypeDef RxHeader1;
53CAN_RxHeaderTypeDef RxHeader2;
54uint8_t RxData1[8];
55uint8_t RxData2[8];
56
57// UART config reception buffer
58static uint8_t uart_rx_char;
59static char uart_cmd[16]; // Only need "CONFIG_REQUEST"
60static uint8_t cmd_index = 0;
61/* USER CODE END PV */
62
63/* Private function prototypes -----------------------------------------------*/
64/* USER CODE BEGIN PFP */
65
66/* USER CODE END PFP */
67
68/* Private user code ---------------------------------------------------------*/
69/* USER CODE BEGIN 0 */
70
71/* USER CODE END 0 */
72
73/* External variables --------------------------------------------------------*/
74extern DMA_HandleTypeDef hdma_adc1;
75extern DMA_HandleTypeDef hdma_adc2;
76extern DMA_HandleTypeDef hdma_adc3;
77extern CAN_HandleTypeDef hcan1;
78extern CAN_HandleTypeDef hcan2;
79extern DMA_HandleTypeDef hdma_dac1;
80extern DMA_HandleTypeDef hdma_dac2;
81extern TIM_HandleTypeDef htim2;
82extern UART_HandleTypeDef huart3;
83/* USER CODE BEGIN EV */
84
85/* USER CODE END EV */
86
87/******************************************************************************/
88/* Cortex-M7 Processor Interruption and Exception Handlers */
89/******************************************************************************/
93void NMI_Handler(void)
94{
95 /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
96
97 /* USER CODE END NonMaskableInt_IRQn 0 */
98 /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
99 while (1)
100 {
101 }
102 /* USER CODE END NonMaskableInt_IRQn 1 */
103}
104
109{
110 /* USER CODE BEGIN HardFault_IRQn 0 */
111
112 /* USER CODE END HardFault_IRQn 0 */
113 while (1)
114 {
115 /* USER CODE BEGIN W1_HardFault_IRQn 0 */
116 /* USER CODE END W1_HardFault_IRQn 0 */
117 }
118}
119
124{
125 /* USER CODE BEGIN MemoryManagement_IRQn 0 */
126
127 /* USER CODE END MemoryManagement_IRQn 0 */
128 while (1)
129 {
130 /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
131 /* USER CODE END W1_MemoryManagement_IRQn 0 */
132 }
133}
134
139{
140 /* USER CODE BEGIN BusFault_IRQn 0 */
141
142 /* USER CODE END BusFault_IRQn 0 */
143 while (1)
144 {
145 /* USER CODE BEGIN W1_BusFault_IRQn 0 */
146 /* USER CODE END W1_BusFault_IRQn 0 */
147 }
148}
149
154{
155 /* USER CODE BEGIN UsageFault_IRQn 0 */
156
157 /* USER CODE END UsageFault_IRQn 0 */
158 while (1)
159 {
160 /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
161 /* USER CODE END W1_UsageFault_IRQn 0 */
162 }
163}
164
169{
170 /* USER CODE BEGIN DebugMonitor_IRQn 0 */
171
172 /* USER CODE END DebugMonitor_IRQn 0 */
173 /* USER CODE BEGIN DebugMonitor_IRQn 1 */
174
175 /* USER CODE END DebugMonitor_IRQn 1 */
176}
177
182{
183 /* USER CODE BEGIN SysTick_IRQn 0 */
184
185 /* USER CODE END SysTick_IRQn 0 */
186 HAL_IncTick();
187#if (INCLUDE_xTaskGetSchedulerState == 1 )
188 if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
189 {
190#endif /* INCLUDE_xTaskGetSchedulerState */
191 xPortSysTickHandler();
192#if (INCLUDE_xTaskGetSchedulerState == 1 )
193 }
194#endif /* INCLUDE_xTaskGetSchedulerState */
195 /* USER CODE BEGIN SysTick_IRQn 1 */
196
197 /* USER CODE END SysTick_IRQn 1 */
198}
199
200/******************************************************************************/
201/* STM32F7xx Peripheral Interrupt Handlers */
202/* Add here the Interrupt Handlers for the used peripherals. */
203/* For the available peripheral interrupt handler names, */
204/* please refer to the startup file (startup_stm32f7xx.s). */
205/******************************************************************************/
206
211{
212 /* USER CODE BEGIN DMA1_Stream5_IRQn 0 */
213
214 /* USER CODE END DMA1_Stream5_IRQn 0 */
215 HAL_DMA_IRQHandler(&hdma_dac1);
216 /* USER CODE BEGIN DMA1_Stream5_IRQn 1 */
217
218 /* USER CODE END DMA1_Stream5_IRQn 1 */
219}
220
225{
226 /* USER CODE BEGIN DMA1_Stream6_IRQn 0 */
227
228 /* USER CODE END DMA1_Stream6_IRQn 0 */
229 HAL_DMA_IRQHandler(&hdma_dac2);
230 /* USER CODE BEGIN DMA1_Stream6_IRQn 1 */
231
232 /* USER CODE END DMA1_Stream6_IRQn 1 */
233}
234
239{
240 /* USER CODE BEGIN CAN1_RX0_IRQn 0 */
241
242 /* USER CODE END CAN1_RX0_IRQn 0 */
243 HAL_CAN_IRQHandler(&hcan1);
244 /* USER CODE BEGIN CAN1_RX0_IRQn 1 */
245
246 /* USER CODE END CAN1_RX0_IRQn 1 */
247}
248
253{
254 /* USER CODE BEGIN TIM2_IRQn 0 */
255
256 /* USER CODE END TIM2_IRQn 0 */
257 HAL_TIM_IRQHandler(&htim2);
258 /* USER CODE BEGIN TIM2_IRQn 1 */
259
260 /* USER CODE END TIM2_IRQn 1 */
261}
262
267{
268 /* USER CODE BEGIN USART3_IRQn 0 */
269
270 /* USER CODE END USART3_IRQn 0 */
271 HAL_UART_IRQHandler(&huart3);
272 /* USER CODE BEGIN USART3_IRQn 1 */
273
274 /* USER CODE END USART3_IRQn 1 */
275}
276
281{
282 /* USER CODE BEGIN DMA2_Stream0_IRQn 0 */
283
284 /* USER CODE END DMA2_Stream0_IRQn 0 */
285 HAL_DMA_IRQHandler(&hdma_adc1);
286 /* USER CODE BEGIN DMA2_Stream0_IRQn 1 */
287
288 /* USER CODE END DMA2_Stream0_IRQn 1 */
289}
290
295{
296 /* USER CODE BEGIN DMA2_Stream1_IRQn 0 */
297
298 /* USER CODE END DMA2_Stream1_IRQn 0 */
299 HAL_DMA_IRQHandler(&hdma_adc3);
300 /* USER CODE BEGIN DMA2_Stream1_IRQn 1 */
301
302 /* USER CODE END DMA2_Stream1_IRQn 1 */
303}
304
309{
310 /* USER CODE BEGIN DMA2_Stream2_IRQn 0 */
311
312 /* USER CODE END DMA2_Stream2_IRQn 0 */
313 HAL_DMA_IRQHandler(&hdma_adc2);
314 /* USER CODE BEGIN DMA2_Stream2_IRQn 1 */
315
316 /* USER CODE END DMA2_Stream2_IRQn 1 */
317}
318
323{
324 /* USER CODE BEGIN CAN2_RX0_IRQn 0 */
325
326 /* USER CODE END CAN2_RX0_IRQn 0 */
327 HAL_CAN_IRQHandler(&hcan2);
328 /* USER CODE BEGIN CAN2_RX0_IRQn 1 */
329
330 /* USER CODE END CAN2_RX0_IRQn 1 */
331}
332
333/* USER CODE BEGIN 1 */
334volatile uint32_t timer_flag = 0;
335
337 // Start UART interrupt reception
338 HAL_UART_Receive_IT(&huart3, &uart_rx_char, 1);
339}
340
341void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
342{
343 if (htim->Instance == TIM1)
344 {
345 timer_flag++;
346 } else if (htim->Instance == TIM2) {
347 // OUTPUTS
348 // D10
349 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, digital_out_buffer[0]);
350 // D9
351 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, digital_out_buffer[1]);
352 // D8
353 HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, digital_out_buffer[2]);
354 // D7
355 HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, digital_out_buffer[3]);
356 // D6
357 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_9 , digital_out_buffer[4]);
358 // D5
359 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_11, digital_out_buffer[5]);
360 // D4 for I2C
361 // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_14, digital_out_buffer[6]);
362 // D3
363 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_13, digital_out_buffer[6]);
364 // D2
365 // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_15, digital_out_buffer[8]);
366 // D1
367 // HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, digital_out_buffer[9]);
368 // D0
369 HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, digital_out_buffer[7]);
370
371 // INPUTS
372 // D42
373 digital_in_buffer[0] = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_8);
374 // D41
375 digital_in_buffer[1] = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_7);
376 // D40
377 digital_in_buffer[2] = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_10);
378 }
379}
380
381// Polling for sending CAN messages
382int send_CAN_message_helper(CANBus bus, CAN_TxHeaderTypeDef *TxHeader, uint8_t *data)
383{
384 uint32_t mailbox;
385 if (bus == CAN_1) {
386 if (HAL_CAN_AddTxMessage(&hcan1, TxHeader, data, &mailbox) != HAL_OK) {
387 uint32_t error = HAL_CAN_GetError(&hcan1);
388 printf("CAN1 Transmission Error: %lx\n", error);
389 return -1;
390 }
391 } else if (bus == CAN_2) {
392 if (HAL_CAN_AddTxMessage(&hcan2, TxHeader, data, &mailbox) != HAL_OK) {
393 uint32_t error = HAL_CAN_GetError(&hcan2);
394 printf("CAN2 Transmission Error: %lx\n", error);
395 return -1;
396 }
397 } else {
398 return -1;
399 }
400 return 0;
401}
402
403// Callback for receiving CAN messages
404void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
405{
406 if (hcan == &hcan1) {
407 HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader1, RxData1);
409 } else if (hcan == &hcan2) {
410 HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader2, RxData2);
412 }
413}
414
415// Callback for UART reception complete
416void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
417{
418 if (huart->Instance == USART3) {
419 if (uart_rx_char == '\n' || uart_rx_char == '\r') {
420 // Check if it's CONFIG_REQUEST
421 uart_cmd[cmd_index] = '\0';
422 if (strcmp(uart_cmd, "CONFIG_REQUEST") == 0) {
424 }
425 cmd_index = 0;
426 }
427 else if (uart_rx_char >= 'A' && uart_rx_char <= 'Z' && cmd_index < 15) {
428 // Only store uppercase letters for CONFIG_REQUEST
430 }
431 else if (uart_rx_char == '_' && cmd_index < 15) {
432 // Store underscore for CONFIG_REQUEST
434 }
435 else if (uart_rx_char < 'A' || uart_rx_char > 'Z') {
436 // Reset on any invalid character
437 cmd_index = 0;
438 }
439
440 // Re-enable interrupt
441 HAL_UART_Receive_IT(&huart3, &uart_rx_char, 1);
442 }
443}
444
445// Callback for ADC conversion complete
446void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) {
447 if (hadc == &hadc1) {
449 } else if (hadc == &hadc2) {
451 } else if (hadc == &hadc3) {
453 }
454}
455
456// CAN TX Complete Callbacks to generate telemetry messages
457void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
458{
459 // Create a telemetry message for TX confirmation using proper sendMessage function
460 if (hcan == &hcan1) {
461 sendMessage("CAN", MSG_CAN_TX, "ID:0x123;DLC:8;Data:AABBCCDD55667788");
462 } else if (hcan == &hcan2) {
463 sendMessage("CAN", MSG_CAN_TX, "ID:0x123;DLC:8;Data:AABBCCDD55667788");
464 }
465}
466
467void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
468{
469 if (hcan == &hcan1) {
470 sendMessage("CAN", MSG_CAN_TX, "ID:0x456;DLC:8;Data:1122334455667788");
471 } else if (hcan == &hcan2) {
472 sendMessage("CAN", MSG_CAN_TX, "ID:0x456;DLC:8;Data:1122334455667788");
473 }
474}
475
476void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
477{
478 if (hcan == &hcan1) {
479 sendMessage("CAN", MSG_CAN_TX, "ID:0x789;DLC:8;Data:DEADBEEFCAFEBABE");
480 } else if (hcan == &hcan2) {
481 sendMessage("CAN", MSG_CAN_TX, "ID:0x789;DLC:8;Data:DEADBEEFCAFEBABE");
482 }
483}
484
485/* USER CODE END 1 */
ADC_HandleTypeDef hadc1
Definition: main.c:63
uint32_t adc3_buffer[8]
Definition: main.c:127
void ProcessADCData(uint32_t *adc1_buffer, uint32_t *adc2_buffer, uint32_t *adc3_buffer)
Processes ADC data from all three ADCs and stores it in the circular buffer.
Definition: AnalogSensor.c:58
uint32_t adc1_buffer[7]
Definition: main.c:125
ADC_HandleTypeDef hadc3
Definition: AnalogSensor.h:68
ADC_HandleTypeDef hadc2
Definition: AnalogSensor.h:68
uint32_t adc2_buffer[7]
Definition: main.c:126
CANBus
Definition: Can.h:98
@ CAN_1
Definition: Can.h:99
@ CAN_2
Definition: Can.h:100
void receive_CAN_message(CAN_RxHeaderTypeDef *header, uint8_t *data, CANBus bus)
Receives a CAN message.
Definition: Can.c:113
uint8_t digital_out_buffer[NUM_DIGITAL_OUTPUTS]
Definition: main.c:130
uint8_t digital_in_buffer[NUM_DIGITAL_INPUTS]
Definition: main.c:131
@ MSG_CAN_TX
Definition: MessageFormat.h:15
void sendMessage(const char *sender, MessageType type, const char *format,...)
Definition: MessageFormat.c:5
void handleTelemetryConfigRequest(void)
Definition: Telemetry.c:130
: Header for main.c file. This file contains the common defines of the application.
static uint8_t uart_rx_char
Definition: stm32f7xx_it.c:58
DMA_HandleTypeDef hdma_dac1
Definition: main.c:75
void USART3_IRQHandler(void)
This function handles USART3 global interrupt.
Definition: stm32f7xx_it.c:266
uint8_t RxData1[8]
Definition: stm32f7xx_it.c:54
CAN_HandleTypeDef hcan1
Definition: main.c:70
volatile uint32_t timer_flag
Definition: stm32f7xx_it.c:334
DMA_HandleTypeDef hdma_adc1
Definition: main.c:66
void UsageFault_Handler(void)
This function handles Undefined instruction or illegal state.
Definition: stm32f7xx_it.c:153
int send_CAN_message_helper(CANBus bus, CAN_TxHeaderTypeDef *TxHeader, uint8_t *data)
Definition: stm32f7xx_it.c:382
void HardFault_Handler(void)
This function handles Hard fault interrupt.
Definition: stm32f7xx_it.c:108
TIM_HandleTypeDef htim2
Definition: main.c:85
uint8_t RxData2[8]
Definition: stm32f7xx_it.c:55
void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
Definition: stm32f7xx_it.c:476
void MemManage_Handler(void)
This function handles Memory management fault.
Definition: stm32f7xx_it.c:123
DMA_HandleTypeDef hdma_adc3
Definition: AnalogSensor.h:69
void TIM2_IRQHandler(void)
This function handles TIM2 global interrupt.
Definition: stm32f7xx_it.c:252
void CAN1_RX0_IRQHandler(void)
This function handles CAN1 RX0 interrupts.
Definition: stm32f7xx_it.c:238
void NMI_Handler(void)
This function handles Non maskable interrupt.
Definition: stm32f7xx_it.c:93
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
Definition: stm32f7xx_it.c:404
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
Definition: stm32f7xx_it.c:457
CAN_RxHeaderTypeDef RxHeader1
Definition: stm32f7xx_it.c:52
void DMA2_Stream2_IRQHandler(void)
This function handles DMA2 stream2 global interrupt.
Definition: stm32f7xx_it.c:308
static uint8_t cmd_index
Definition: stm32f7xx_it.c:60
void BusFault_Handler(void)
This function handles Pre-fetch fault, memory access fault.
Definition: stm32f7xx_it.c:138
void initUartConfigListener(void)
Definition: stm32f7xx_it.c:336
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
Definition: stm32f7xx_it.c:341
CAN_RxHeaderTypeDef RxHeader2
Definition: stm32f7xx_it.c:53
void DMA1_Stream6_IRQHandler(void)
This function handles DMA1 stream6 global interrupt.
Definition: stm32f7xx_it.c:224
DMA_HandleTypeDef hdma_dac2
Definition: main.c:76
void DMA2_Stream0_IRQHandler(void)
This function handles DMA2 stream0 global interrupt.
Definition: stm32f7xx_it.c:280
void SysTick_Handler(void)
This function handles System tick timer.
Definition: stm32f7xx_it.c:181
CAN_HandleTypeDef hcan2
Definition: main.c:71
UART_HandleTypeDef huart3
Definition: main.c:87
void CAN2_RX0_IRQHandler(void)
This function handles CAN2 RX0 interrupts.
Definition: stm32f7xx_it.c:322
void DMA1_Stream5_IRQHandler(void)
This function handles DMA1 stream5 global interrupt.
Definition: stm32f7xx_it.c:210
DMA_HandleTypeDef hdma_adc2
Definition: AnalogSensor.h:69
void DMA2_Stream1_IRQHandler(void)
This function handles DMA2 stream1 global interrupt.
Definition: stm32f7xx_it.c:294
void DebugMon_Handler(void)
This function handles Debug monitor.
Definition: stm32f7xx_it.c:168
void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
Definition: stm32f7xx_it.c:467
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
Definition: stm32f7xx_it.c:416
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
Definition: stm32f7xx_it.c:446
static char uart_cmd[16]
Definition: stm32f7xx_it.c:59
This file contains the headers of the interrupt handlers.