From 57a0d60178b4652497fd8e98a3704f1c92a3b352 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 15 Jan 2025 19:47:17 -0500 Subject: [PATCH 01/18] Hopefully Did Fan Batt Box Control Code --- Core/Inc/control.h | 17 +++++++++++++++++ Core/Src/can_handler.c | 4 ++++ Core/Src/control.c | 35 +++++++++++++++++++++++++++++++++++ Core/Src/main.c | 9 +++++++++ 4 files changed, 65 insertions(+) create mode 100644 Core/Inc/control.h create mode 100644 Core/Src/control.c diff --git a/Core/Inc/control.h b/Core/Inc/control.h new file mode 100644 index 0000000..51e661a --- /dev/null +++ b/Core/Inc/control.h @@ -0,0 +1,17 @@ +#include "pdu.h" + +// TODO: replace this temp value with real value +#define CONTROL_CANID_FANBATTBOX = 0xA + +extern osThreadId_t control_handle; +extern const osThreadAttr_t control_attributes; + +typedef struct { + pdu_t *pdu; +} control_args_t; + +void vEval_fanbattbox_state(void *param); + +void control_fanbattbox_record(can_msg_t args); + +int eval_fanbattbox_state(); \ No newline at end of file diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 08d7a15..9c7a6c8 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -16,6 +16,7 @@ #include "fault.h" #include "stdio.h" #include "steeringio.h" +#include "control.h" #include #include #include @@ -163,6 +164,9 @@ void vCanReceive(void *pv_params) case STEERING_CANID_IO: steeringio_update(msg); break; + case CONTROL_CANID_FANBATTBOX: + control_fanbattbox_record(msg); + break; default: break; } diff --git a/Core/Src/control.c b/Core/Src/control.c new file mode 100644 index 0000000..a99bc72 --- /dev/null +++ b/Core/Src/control.c @@ -0,0 +1,35 @@ +#include "control.h" +#include "pdu.h" + +osThreadId_t control_handle; +const osThreadAttr_t control_attributes = { + .name = "Control", + .stack_size = 128 * 8, + .priority = (osPriority_t)osPriorityRealtime, +}; + +static int fanBattBoxState = 0; + +void vEval_fanbattbox_state(void *param) +{ + control_args_t args = (control_args_t *)param; + pdu_t *pdu = args->pdu; + + write_fan_battbox(pdu, eval_fanbattbox_state()); + + osDelay(1000); +} + +void control_fanbattbox_record(can_msg_t msg) +{ + if (msg.data > 0) { + fanBattBoxState = 1; + } else { + fanBattBoxState = 0; + } +} + +int eval_fanbattbox_state() +{ + return fanBattBoxState; +} \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index d7de1b4..6797750 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -39,6 +39,7 @@ #include "dti.h" #include "steeringio.h" #include "pedals.h" +#include "control.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -248,6 +249,14 @@ int main(void) sm_args->mpu = mpu; sm_director_handle = osThreadNew(vStateMachineDirector, sm_args, &sm_director_attributes); assert(sm_director_handle); + + /* Control File Thread */ + control_args_t *control_args = malloc(sizeof(control_args_t)); + control_args->pdu = pdu; + control_handle = + osThreadNew(vEval_fanbattbox_state, control_args, &control_attributes); + assert(control_handle); + /* USER CODE END RTOS_THREADS */ /* USER CODE BEGIN RTOS_EVENTS */ From f4d1b405646fb64a09720d12184c404cca6ed929 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 15 Jan 2025 20:07:15 -0500 Subject: [PATCH 02/18] Small Fixes --- Core/Inc/control.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index 51e661a..e3900da 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -1,13 +1,13 @@ #include "pdu.h" // TODO: replace this temp value with real value -#define CONTROL_CANID_FANBATTBOX = 0xA +#define CONTROL_CANID_FANBATTBOX 0xA extern osThreadId_t control_handle; extern const osThreadAttr_t control_attributes; typedef struct { - pdu_t *pdu; + pdu_t *pdu; } control_args_t; void vEval_fanbattbox_state(void *param); From 03c82061cbdcfefdcf1099edd63ca3140c981aac Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 15 Jan 2025 20:24:06 -0500 Subject: [PATCH 03/18] Trying this --- Core/Inc/control.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index e3900da..a4925a0 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -1,3 +1,6 @@ +#ifndef CONTROL_H +#define CONTROL_H + #include "pdu.h" // TODO: replace this temp value with real value From 43e26b5867a7ee99097f1cbd5157a3b8fad1dbe7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 15 Jan 2025 20:27:42 -0500 Subject: [PATCH 04/18] For got to endif --- Core/Inc/control.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index a4925a0..6dd8d0e 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -17,4 +17,6 @@ void vEval_fanbattbox_state(void *param); void control_fanbattbox_record(can_msg_t args); -int eval_fanbattbox_state(); \ No newline at end of file +int eval_fanbattbox_state(); + +#endif \ No newline at end of file From b9d99325bfbc280d6efff1cb4c9ff6c59117d4b7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 15 Jan 2025 20:33:54 -0500 Subject: [PATCH 05/18] Adding control to make file --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index e28cb1b..f3b4ce4 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,7 @@ Core/Src/bms.c \ Core/Src/nero.c \ Core/Src/pdu.c \ Core/Src/mpu.c \ +Core/Src/control.c \ Core/Src/steeringio.c \ Core/Src/pedals.c \ Core/Src/cerb_utils.c \ From 8036bc56f6ccb95ace6da377fc34baa9e17b1c1b Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 15 Jan 2025 20:41:33 -0500 Subject: [PATCH 06/18] Forgot to include can.h --- Core/Src/control.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/Src/control.c b/Core/Src/control.c index a99bc72..2b61b4c 100644 --- a/Core/Src/control.c +++ b/Core/Src/control.c @@ -1,5 +1,6 @@ #include "control.h" #include "pdu.h" +#include "can.h" osThreadId_t control_handle; const osThreadAttr_t control_attributes = { @@ -13,9 +14,8 @@ static int fanBattBoxState = 0; void vEval_fanbattbox_state(void *param) { control_args_t args = (control_args_t *)param; - pdu_t *pdu = args->pdu; - write_fan_battbox(pdu, eval_fanbattbox_state()); + write_fan_battbox(args->pdu, eval_fanbattbox_state()); osDelay(1000); } From fe623d344adbb4555a2d7590d548cfabce9b0261 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 15 Jan 2025 20:44:39 -0500 Subject: [PATCH 07/18] Small Fix --- Core/Inc/control.h | 1 + Core/Src/control.c | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index 6dd8d0e..5d81b5c 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -2,6 +2,7 @@ #define CONTROL_H #include "pdu.h" +#include "can.h" // TODO: replace this temp value with real value #define CONTROL_CANID_FANBATTBOX 0xA diff --git a/Core/Src/control.c b/Core/Src/control.c index 2b61b4c..318b01f 100644 --- a/Core/Src/control.c +++ b/Core/Src/control.c @@ -1,6 +1,5 @@ #include "control.h" #include "pdu.h" -#include "can.h" osThreadId_t control_handle; const osThreadAttr_t control_attributes = { @@ -13,7 +12,7 @@ static int fanBattBoxState = 0; void vEval_fanbattbox_state(void *param) { - control_args_t args = (control_args_t *)param; + control_args_t *args = (control_args_t *)param; write_fan_battbox(args->pdu, eval_fanbattbox_state()); From e9e86f03ef3554884c20e74088045648cb6f6549 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 16 Jan 2025 20:12:04 -0500 Subject: [PATCH 08/18] Thread Fix --- Core/Inc/control.h | 2 +- Core/Src/control.c | 13 +- Core/Src/main.c | 984 ++++++++++++++++++++++----------------------- 3 files changed, 488 insertions(+), 511 deletions(-) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index 5d81b5c..4587d48 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -14,7 +14,7 @@ typedef struct { pdu_t *pdu; } control_args_t; -void vEval_fanbattbox_state(void *param); +void vControl(void *param); void control_fanbattbox_record(can_msg_t args); diff --git a/Core/Src/control.c b/Core/Src/control.c index 318b01f..49b0151 100644 --- a/Core/Src/control.c +++ b/Core/Src/control.c @@ -10,13 +10,15 @@ const osThreadAttr_t control_attributes = { static int fanBattBoxState = 0; -void vEval_fanbattbox_state(void *param) +void vControl(void *param) { control_args_t *args = (control_args_t *)param; - write_fan_battbox(args->pdu, eval_fanbattbox_state()); + for (;;) { + write_fan_battbox(args->pdu, fanBattBoxState); - osDelay(1000); + osDelay(1000); + } } void control_fanbattbox_record(can_msg_t msg) @@ -27,8 +29,3 @@ void control_fanbattbox_record(can_msg_t msg) fanBattBoxState = 0; } } - -int eval_fanbattbox_state() -{ - return fanBattBoxState; -} \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index 6797750..de44f4c 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -76,9 +76,9 @@ DMA_HandleTypeDef hdma_usart3_tx; /* Definitions for defaultTask */ osThreadId_t defaultTaskHandle; const osThreadAttr_t defaultTask_attributes = { - .name = "defaultTask", - .stack_size = 128 * 4, - .priority = (osPriority_t) osPriorityNormal, + .name = "defaultTask", + .stack_size = 128 * 4, + .priority = (osPriority_t)osPriorityNormal, }; /* USER CODE BEGIN PV */ osMessageQueueId_t imu_queue; @@ -112,17 +112,18 @@ void StartDefaultTask(void *argument); PUTCHAR_PROTOTYPE { - HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, HAL_MAX_DELAY); - return ch; + HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, HAL_MAX_DELAY); + return ch; } -int _write(int file, char* ptr, int len) { - int DataIdx; +int _write(int file, char *ptr, int len) +{ + int DataIdx; - for (DataIdx = 0; DataIdx < len; DataIdx++) { - __io_putchar( *ptr++ ); - } - return len; + for (DataIdx = 0; DataIdx < len; DataIdx++) { + __io_putchar(*ptr++); + } + return len; } /* USER CODE END 0 */ @@ -132,151 +133,159 @@ int _write(int file, char* ptr, int len) { */ int main(void) { - - /* USER CODE BEGIN 1 */ - printf("BOOT\n"); - /* USER CODE END 1 */ - - /* MCU Configuration--------------------------------------------------------*/ - - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ - HAL_Init(); - - /* USER CODE BEGIN Init */ - - HAL_Delay(2000); - - /* USER CODE END Init */ - - /* Configure the system clock */ - SystemClock_Config(); - - /* USER CODE BEGIN SysInit */ - - /* USER CODE END SysInit */ - - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_DMA_Init(); - MX_CAN1_Init(); - MX_I2C1_Init(); - MX_I2C2_Init(); - MX_ADC1_Init(); - MX_USART3_UART_Init(); - MX_ADC3_Init(); - MX_IWDG_Init(); - /* USER CODE BEGIN 2 */ - - /* Create Interfaces to Represent Relevant Hardware */ - mpu_t *mpu = init_mpu(&hadc3, &hadc1, GPIOC, GPIOB); - assert(mpu); - pdu_t *pdu = init_pdu(&hi2c2); - assert(pdu); - dti_t *mc = dti_init(); - assert(mc); - init_can1(&hcan1); - bms_init(); - - printf("\n\n\nInit Success...\n\n\n"); - - /* USER CODE END 2 */ - - /* Init scheduler */ - osKernelInitialize(); - - /* USER CODE BEGIN RTOS_MUTEX */ - - /* USER CODE END RTOS_MUTEX */ - - /* USER CODE BEGIN RTOS_SEMAPHORES */ - /* add semaphores, ... */ - /* USER CODE END RTOS_SEMAPHORES */ - - /* USER CODE BEGIN RTOS_TIMERS */ - /* start timers, add new ones, ... */ - /* USER CODE END RTOS_TIMERS */ - - /* USER CODE BEGIN RTOS_QUEUES */ - /* USER CODE END RTOS_QUEUES */ - - /* Create the thread(s) */ - /* creation of defaultTask */ - defaultTaskHandle = osThreadNew(StartDefaultTask, mpu, &defaultTask_attributes); - - /* USER CODE BEGIN RTOS_THREADS */ - - /* Monitors */ - non_func_data_args_t *nfd_args = malloc(sizeof(non_func_data_args_t)); - nfd_args->mpu = mpu; - nfd_args->pdu = pdu; - non_functional_data_thead = osThreadNew(vNonFunctionalDataCollection, nfd_args, &non_functional_data_attributes); - assert(non_functional_data_thead); - - data_collection_args_t* data_args = malloc(sizeof(data_collection_args_t)); - data_args->pdu = pdu; - data_collection_thread = osThreadNew(vDataCollection, data_args, &data_collection_attributes); - assert(data_collection_thread); - // temp_monitor_handle = osThreadNew(vTempMonitor, mpu, &temp_monitor_attributes); - // assert(temp_monitor_handle); - //imu_monitor_handle = osThreadNew(vIMUMonitor, mpu, &imu_monitor_attributes); - //assert(imu_monitor_handle); - // shutdown_monitor_handle = osThreadNew(vShutdownMonitor, pdu, &shutdown_monitor_attributes); - // assert(shutdown_monitor_handle); - - /* Messaging */ - can_dispatch_handle = osThreadNew(vCanDispatch, &hcan1, &can_dispatch_attributes); - assert(can_dispatch_handle); - can_receive_thread = osThreadNew(vCanReceive, mc, &can_receive_attributes); - assert(can_receive_thread); - - /* Control Logic */ - fault_handle = osThreadNew(vFaultHandler, NULL, &fault_handle_attributes); - assert(fault_handle); - - rtds_thread = osThreadNew(vRTDS, pdu, &rtds_attributes); - assert(rtds_thread); - - pedals_args_t *pedals_args = malloc(sizeof(pedals_args_t)); - pedals_args->mpu = mpu; - pedals_args->mc = mc; - pedals_args->pdu = pdu; - process_pedals_thread = osThreadNew(vProcessPedals, pedals_args, &process_pedals_attributes); - assert(process_pedals_thread); - - sm_director_args_t *sm_args = malloc(sizeof(sm_director_args_t)); - sm_args->pdu = pdu; - sm_args->mc = mc; - sm_args->mpu = mpu; - sm_director_handle = osThreadNew(vStateMachineDirector, sm_args, &sm_director_attributes); - assert(sm_director_handle); + /* USER CODE BEGIN 1 */ + printf("BOOT\n"); + /* USER CODE END 1 */ + + /* MCU Configuration--------------------------------------------------------*/ + + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); + + /* USER CODE BEGIN Init */ + + HAL_Delay(2000); + + /* USER CODE END Init */ + + /* Configure the system clock */ + SystemClock_Config(); + + /* USER CODE BEGIN SysInit */ + + /* USER CODE END SysInit */ + + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_DMA_Init(); + MX_CAN1_Init(); + MX_I2C1_Init(); + MX_I2C2_Init(); + MX_ADC1_Init(); + MX_USART3_UART_Init(); + MX_ADC3_Init(); + MX_IWDG_Init(); + /* USER CODE BEGIN 2 */ + + /* Create Interfaces to Represent Relevant Hardware */ + mpu_t *mpu = init_mpu(&hadc3, &hadc1, GPIOC, GPIOB); + assert(mpu); + pdu_t *pdu = init_pdu(&hi2c2); + assert(pdu); + dti_t *mc = dti_init(); + assert(mc); + init_can1(&hcan1); + bms_init(); + + printf("\n\n\nInit Success...\n\n\n"); + + /* USER CODE END 2 */ + + /* Init scheduler */ + osKernelInitialize(); + + /* USER CODE BEGIN RTOS_MUTEX */ + + /* USER CODE END RTOS_MUTEX */ + + /* USER CODE BEGIN RTOS_SEMAPHORES */ + /* add semaphores, ... */ + /* USER CODE END RTOS_SEMAPHORES */ + + /* USER CODE BEGIN RTOS_TIMERS */ + /* start timers, add new ones, ... */ + /* USER CODE END RTOS_TIMERS */ + + /* USER CODE BEGIN RTOS_QUEUES */ + /* USER CODE END RTOS_QUEUES */ + + /* Create the thread(s) */ + /* creation of defaultTask */ + defaultTaskHandle = + osThreadNew(StartDefaultTask, mpu, &defaultTask_attributes); + + /* USER CODE BEGIN RTOS_THREADS */ + + /* Monitors */ + non_func_data_args_t *nfd_args = malloc(sizeof(non_func_data_args_t)); + nfd_args->mpu = mpu; + nfd_args->pdu = pdu; + non_functional_data_thead = + osThreadNew(vNonFunctionalDataCollection, nfd_args, + &non_functional_data_attributes); + assert(non_functional_data_thead); + + data_collection_args_t *data_args = + malloc(sizeof(data_collection_args_t)); + data_args->pdu = pdu; + data_collection_thread = osThreadNew(vDataCollection, data_args, + &data_collection_attributes); + assert(data_collection_thread); + // temp_monitor_handle = osThreadNew(vTempMonitor, mpu, &temp_monitor_attributes); + // assert(temp_monitor_handle); + //imu_monitor_handle = osThreadNew(vIMUMonitor, mpu, &imu_monitor_attributes); + //assert(imu_monitor_handle); + // shutdown_monitor_handle = osThreadNew(vShutdownMonitor, pdu, &shutdown_monitor_attributes); + // assert(shutdown_monitor_handle); + + /* Messaging */ + can_dispatch_handle = + osThreadNew(vCanDispatch, &hcan1, &can_dispatch_attributes); + assert(can_dispatch_handle); + can_receive_thread = + osThreadNew(vCanReceive, mc, &can_receive_attributes); + assert(can_receive_thread); + + /* Control Logic */ + fault_handle = + osThreadNew(vFaultHandler, NULL, &fault_handle_attributes); + assert(fault_handle); + + rtds_thread = osThreadNew(vRTDS, pdu, &rtds_attributes); + assert(rtds_thread); + + pedals_args_t *pedals_args = malloc(sizeof(pedals_args_t)); + pedals_args->mpu = mpu; + pedals_args->mc = mc; + pedals_args->pdu = pdu; + process_pedals_thread = osThreadNew(vProcessPedals, pedals_args, + &process_pedals_attributes); + assert(process_pedals_thread); + + sm_director_args_t *sm_args = malloc(sizeof(sm_director_args_t)); + sm_args->pdu = pdu; + sm_args->mc = mc; + sm_args->mpu = mpu; + sm_director_handle = osThreadNew(vStateMachineDirector, sm_args, + &sm_director_attributes); + assert(sm_director_handle); /* Control File Thread */ control_args_t *control_args = malloc(sizeof(control_args_t)); control_args->pdu = pdu; control_handle = - osThreadNew(vEval_fanbattbox_state, control_args, &control_attributes); + osThreadNew(vControl, control_args, &control_attributes); assert(control_handle); - - /* USER CODE END RTOS_THREADS */ - /* USER CODE BEGIN RTOS_EVENTS */ - /* add events, ... */ - /* USER CODE END RTOS_EVENTS */ + /* USER CODE END RTOS_THREADS */ + + /* USER CODE BEGIN RTOS_EVENTS */ + /* add events, ... */ + /* USER CODE END RTOS_EVENTS */ - /* Start scheduler */ - osKernelStart(); + /* Start scheduler */ + osKernelStart(); - /* We should never get here as control is now taken by the scheduler */ + /* We should never get here as control is now taken by the scheduler */ - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - while (1) - { - /* USER CODE END WHILE */ + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + while (1) { + /* USER CODE END WHILE */ - /* USER CODE BEGIN 3 */ - } - /* USER CODE END 3 */ + /* USER CODE BEGIN 3 */ + } + /* USER CODE END 3 */ } /** @@ -285,40 +294,41 @@ int main(void) */ void SystemClock_Config(void) { - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + RCC_OscInitTypeDef RCC_OscInitStruct = { 0 }; + RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 }; - /** Configure the main internal regulator output voltage + /** Configure the main internal regulator output voltage */ - __HAL_RCC_PWR_CLK_ENABLE(); - __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + __HAL_RCC_PWR_CLK_ENABLE(); + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); - /** Initializes the RCC Oscillators according to the specified parameters + /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { - Error_Handler(); - } - - /** Initializes the CPU, AHB and APB buses clocks + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | + RCC_OSCILLATORTYPE_LSI; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; + RCC_OscInitStruct.LSIState = RCC_LSI_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + Error_Handler(); + } + + /** Initializes the CPU, AHB and APB buses clocks */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) - { - Error_Handler(); - } + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | + RCC_CLOCKTYPE_SYSCLK | + RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != + HAL_OK) { + Error_Handler(); + } } /** @@ -328,49 +338,45 @@ void SystemClock_Config(void) */ static void MX_ADC1_Init(void) { + /* USER CODE BEGIN ADC1_Init 0 */ - /* USER CODE BEGIN ADC1_Init 0 */ + /* USER CODE END ADC1_Init 0 */ - /* USER CODE END ADC1_Init 0 */ + ADC_ChannelConfTypeDef sConfig = { 0 }; - ADC_ChannelConfTypeDef sConfig = {0}; + /* USER CODE BEGIN ADC1_Init 1 */ - /* USER CODE BEGIN ADC1_Init 1 */ + /* USER CODE END ADC1_Init 1 */ - /* USER CODE END ADC1_Init 1 */ - - /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ - hadc1.Instance = ADC1; - hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; - hadc1.Init.Resolution = ADC_RESOLUTION_12B; - hadc1.Init.ScanConvMode = ENABLE; - hadc1.Init.ContinuousConvMode = ENABLE; - hadc1.Init.DiscontinuousConvMode = DISABLE; - hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; - hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.NbrOfConversion = 1; - hadc1.Init.DMAContinuousRequests = ENABLE; - hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - if (HAL_ADC_Init(&hadc1) != HAL_OK) - { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.ScanConvMode = ENABLE; + hadc1.Init.ContinuousConvMode = ENABLE; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DMAContinuousRequests = ENABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + if (HAL_ADC_Init(&hadc1) != HAL_OK) { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_8; - sConfig.Rank = 1; - sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN ADC1_Init 2 */ - - /* USER CODE END ADC1_Init 2 */ - + sConfig.Channel = ADC_CHANNEL_8; + sConfig.Rank = 1; + sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ } /** @@ -380,76 +386,69 @@ static void MX_ADC1_Init(void) */ static void MX_ADC3_Init(void) { + /* USER CODE BEGIN ADC3_Init 0 */ - /* USER CODE BEGIN ADC3_Init 0 */ + /* USER CODE END ADC3_Init 0 */ - /* USER CODE END ADC3_Init 0 */ + ADC_ChannelConfTypeDef sConfig = { 0 }; - ADC_ChannelConfTypeDef sConfig = {0}; + /* USER CODE BEGIN ADC3_Init 1 */ - /* USER CODE BEGIN ADC3_Init 1 */ + /* USER CODE END ADC3_Init 1 */ - /* USER CODE END ADC3_Init 1 */ - - /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) - */ - hadc3.Instance = ADC3; - hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; - hadc3.Init.Resolution = ADC_RESOLUTION_12B; - hadc3.Init.ScanConvMode = ENABLE; - hadc3.Init.ContinuousConvMode = ENABLE; - hadc3.Init.DiscontinuousConvMode = DISABLE; - hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START; - hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc3.Init.NbrOfConversion = 4; - hadc3.Init.DMAContinuousRequests = ENABLE; - hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - if (HAL_ADC_Init(&hadc3) != HAL_OK) - { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ - sConfig.Channel = ADC_CHANNEL_2; - sConfig.Rank = 1; - sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + hadc3.Instance = ADC3; + hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; + hadc3.Init.Resolution = ADC_RESOLUTION_12B; + hadc3.Init.ScanConvMode = ENABLE; + hadc3.Init.ContinuousConvMode = ENABLE; + hadc3.Init.DiscontinuousConvMode = DISABLE; + hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc3.Init.NbrOfConversion = 4; + hadc3.Init.DMAContinuousRequests = ENABLE; + hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + if (HAL_ADC_Init(&hadc3) != HAL_OK) { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_3; - sConfig.Rank = 2; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + sConfig.Channel = ADC_CHANNEL_2; + sConfig.Rank = 1; + sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; + if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_0; - sConfig.Rank = 3; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. - */ - sConfig.Channel = ADC_CHANNEL_1; - sConfig.Rank = 4; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN ADC3_Init 2 */ + sConfig.Channel = ADC_CHANNEL_3; + sConfig.Rank = 2; + if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { + Error_Handler(); + } - /* USER CODE END ADC3_Init 2 */ + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + */ + sConfig.Channel = ADC_CHANNEL_0; + sConfig.Rank = 3; + if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { + Error_Handler(); + } + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + */ + sConfig.Channel = ADC_CHANNEL_1; + sConfig.Rank = 4; + if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN ADC3_Init 2 */ + + /* USER CODE END ADC3_Init 2 */ } /** @@ -459,34 +458,31 @@ static void MX_ADC3_Init(void) */ static void MX_CAN1_Init(void) { - - /* USER CODE BEGIN CAN1_Init 0 */ - - /* USER CODE END CAN1_Init 0 */ - - /* USER CODE BEGIN CAN1_Init 1 */ - - /* USER CODE END CAN1_Init 1 */ - hcan1.Instance = CAN1; - hcan1.Init.Prescaler = 2; - hcan1.Init.Mode = CAN_MODE_NORMAL; - hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ; - hcan1.Init.TimeSeg1 = CAN_BS1_13TQ; - hcan1.Init.TimeSeg2 = CAN_BS2_2TQ; - hcan1.Init.TimeTriggeredMode = DISABLE; - hcan1.Init.AutoBusOff = ENABLE; - hcan1.Init.AutoWakeUp = DISABLE; - hcan1.Init.AutoRetransmission = DISABLE; - hcan1.Init.ReceiveFifoLocked = DISABLE; - hcan1.Init.TransmitFifoPriority = DISABLE; - if (HAL_CAN_Init(&hcan1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN CAN1_Init 2 */ - - /* USER CODE END CAN1_Init 2 */ - + /* USER CODE BEGIN CAN1_Init 0 */ + + /* USER CODE END CAN1_Init 0 */ + + /* USER CODE BEGIN CAN1_Init 1 */ + + /* USER CODE END CAN1_Init 1 */ + hcan1.Instance = CAN1; + hcan1.Init.Prescaler = 2; + hcan1.Init.Mode = CAN_MODE_NORMAL; + hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ; + hcan1.Init.TimeSeg1 = CAN_BS1_13TQ; + hcan1.Init.TimeSeg2 = CAN_BS2_2TQ; + hcan1.Init.TimeTriggeredMode = DISABLE; + hcan1.Init.AutoBusOff = ENABLE; + hcan1.Init.AutoWakeUp = DISABLE; + hcan1.Init.AutoRetransmission = DISABLE; + hcan1.Init.ReceiveFifoLocked = DISABLE; + hcan1.Init.TransmitFifoPriority = DISABLE; + if (HAL_CAN_Init(&hcan1) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN CAN1_Init 2 */ + + /* USER CODE END CAN1_Init 2 */ } /** @@ -496,31 +492,28 @@ static void MX_CAN1_Init(void) */ static void MX_I2C1_Init(void) { - - /* USER CODE BEGIN I2C1_Init 0 */ - - /* USER CODE END I2C1_Init 0 */ - - /* USER CODE BEGIN I2C1_Init 1 */ - - /* USER CODE END I2C1_Init 1 */ - hi2c1.Instance = I2C1; - hi2c1.Init.ClockSpeed = 100000; - hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; - hi2c1.Init.OwnAddress1 = 0; - hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c1.Init.OwnAddress2 = 0; - hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN I2C1_Init 2 */ - - /* USER CODE END I2C1_Init 2 */ - + /* USER CODE BEGIN I2C1_Init 0 */ + + /* USER CODE END I2C1_Init 0 */ + + /* USER CODE BEGIN I2C1_Init 1 */ + + /* USER CODE END I2C1_Init 1 */ + hi2c1.Instance = I2C1; + hi2c1.Init.ClockSpeed = 100000; + hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; + hi2c1.Init.OwnAddress1 = 0; + hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c1.Init.OwnAddress2 = 0; + hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c1) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN I2C1_Init 2 */ + + /* USER CODE END I2C1_Init 2 */ } /** @@ -530,31 +523,28 @@ static void MX_I2C1_Init(void) */ static void MX_I2C2_Init(void) { - - /* USER CODE BEGIN I2C2_Init 0 */ - - /* USER CODE END I2C2_Init 0 */ - - /* USER CODE BEGIN I2C2_Init 1 */ - - /* USER CODE END I2C2_Init 1 */ - hi2c2.Instance = I2C2; - hi2c2.Init.ClockSpeed = 100000; - hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; - hi2c2.Init.OwnAddress1 = 0; - hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c2.Init.OwnAddress2 = 0; - hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c2) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN I2C2_Init 2 */ - - /* USER CODE END I2C2_Init 2 */ - + /* USER CODE BEGIN I2C2_Init 0 */ + + /* USER CODE END I2C2_Init 0 */ + + /* USER CODE BEGIN I2C2_Init 1 */ + + /* USER CODE END I2C2_Init 1 */ + hi2c2.Instance = I2C2; + hi2c2.Init.ClockSpeed = 100000; + hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; + hi2c2.Init.OwnAddress1 = 0; + hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c2.Init.OwnAddress2 = 0; + hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c2) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN I2C2_Init 2 */ + + /* USER CODE END I2C2_Init 2 */ } /** @@ -564,25 +554,22 @@ static void MX_I2C2_Init(void) */ static void MX_IWDG_Init(void) { + /* USER CODE BEGIN IWDG_Init 0 */ - /* USER CODE BEGIN IWDG_Init 0 */ + /* USER CODE END IWDG_Init 0 */ - /* USER CODE END IWDG_Init 0 */ + /* USER CODE BEGIN IWDG_Init 1 */ - /* USER CODE BEGIN IWDG_Init 1 */ - - /* USER CODE END IWDG_Init 1 */ - hiwdg.Instance = IWDG; - hiwdg.Init.Prescaler = IWDG_PRESCALER_32; - hiwdg.Init.Reload = 4095; - if (HAL_IWDG_Init(&hiwdg) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN IWDG_Init 2 */ - - /* USER CODE END IWDG_Init 2 */ + /* USER CODE END IWDG_Init 1 */ + hiwdg.Instance = IWDG; + hiwdg.Init.Prescaler = IWDG_PRESCALER_32; + hiwdg.Init.Reload = 4095; + if (HAL_IWDG_Init(&hiwdg) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN IWDG_Init 2 */ + /* USER CODE END IWDG_Init 2 */ } /** @@ -592,30 +579,27 @@ static void MX_IWDG_Init(void) */ static void MX_USART3_UART_Init(void) { - - /* USER CODE BEGIN USART3_Init 0 */ - - /* USER CODE END USART3_Init 0 */ - - /* USER CODE BEGIN USART3_Init 1 */ - - /* USER CODE END USART3_Init 1 */ - huart3.Instance = USART3; - huart3.Init.BaudRate = 115200; - huart3.Init.WordLength = UART_WORDLENGTH_8B; - huart3.Init.StopBits = UART_STOPBITS_1; - huart3.Init.Parity = UART_PARITY_NONE; - huart3.Init.Mode = UART_MODE_TX_RX; - huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart3.Init.OverSampling = UART_OVERSAMPLING_16; - if (HAL_UART_Init(&huart3) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN USART3_Init 2 */ - - /* USER CODE END USART3_Init 2 */ - + /* USER CODE BEGIN USART3_Init 0 */ + + /* USER CODE END USART3_Init 0 */ + + /* USER CODE BEGIN USART3_Init 1 */ + + /* USER CODE END USART3_Init 1 */ + huart3.Instance = USART3; + huart3.Init.BaudRate = 115200; + huart3.Init.WordLength = UART_WORDLENGTH_8B; + huart3.Init.StopBits = UART_STOPBITS_1; + huart3.Init.Parity = UART_PARITY_NONE; + huart3.Init.Mode = UART_MODE_TX_RX; + huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart3.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart3) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN USART3_Init 2 */ + + /* USER CODE END USART3_Init 2 */ } /** @@ -623,16 +607,14 @@ static void MX_USART3_UART_Init(void) */ static void MX_DMA_Init(void) { - - /* DMA controller clock enable */ - __HAL_RCC_DMA2_CLK_ENABLE(); - __HAL_RCC_DMA1_CLK_ENABLE(); - - /* DMA interrupt init */ - /* DMA1_Stream3_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 5, 0); - HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn); - + /* DMA controller clock enable */ + __HAL_RCC_DMA2_CLK_ENABLE(); + __HAL_RCC_DMA1_CLK_ENABLE(); + + /* DMA interrupt init */ + /* DMA1_Stream3_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn); } /** @@ -642,78 +624,79 @@ static void MX_DMA_Init(void) */ static void MX_GPIO_Init(void) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; -/* USER CODE BEGIN MX_GPIO_Init_1 */ -/* USER CODE END MX_GPIO_Init_1 */ - - /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOH_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET); - - /*Configure GPIO pins : PC3 PC8 PC9 */ - GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_8|GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - - /*Configure GPIO pins : PA4 PA5 PA6 PA7 */ - GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /*Configure GPIO pin : PC4 */ - GPIO_InitStruct.Pin = GPIO_PIN_4; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - - /*Configure GPIO pin : PB1 */ - GPIO_InitStruct.Pin = GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pin : PB2 */ - GPIO_InitStruct.Pin = GPIO_PIN_2; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF15_EVENTOUT; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pin : PB15 */ - GPIO_InitStruct.Pin = GPIO_PIN_15; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pin : PA9 */ - GPIO_InitStruct.Pin = GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /*Configure GPIO pins : PA11 PA12 */ - GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - -/* USER CODE BEGIN MX_GPIO_Init_2 */ -/* USER CODE END MX_GPIO_Init_2 */ + GPIO_InitTypeDef GPIO_InitStruct = { 0 }; + /* USER CODE BEGIN MX_GPIO_Init_1 */ + /* USER CODE END MX_GPIO_Init_1 */ + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOH_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9, + GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET); + + /*Configure GPIO pins : PC3 PC8 PC9 */ + GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /*Configure GPIO pins : PA4 PA5 PA6 PA7 */ + GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /*Configure GPIO pin : PC4 */ + GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /*Configure GPIO pin : PB1 */ + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pin : PB2 */ + GPIO_InitStruct.Pin = GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF15_EVENTOUT; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pin : PB15 */ + GPIO_InitStruct.Pin = GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pin : PA9 */ + GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /*Configure GPIO pins : PA11 PA12 */ + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN MX_GPIO_Init_2 */ + /* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ @@ -729,29 +712,27 @@ static void MX_GPIO_Init(void) /* USER CODE END Header_StartDefaultTask */ void StartDefaultTask(void *argument) { - /* USER CODE BEGIN 5 */ - mpu_t *mpu = (mpu_t *) argument; - assert(mpu); - - /* Infinite loop */ - for(;;) { - - /* Pet watchdog */ - HAL_IWDG_Refresh(&hiwdg); - /* Toggle LED at certain frequency */ - printf(".\n..\n"); - toggle_yled(mpu); - - // refresh the external watchdog so the car doesnt fault - pet_watchdog(mpu); - - - /* Send NERO state data continuously */ - send_nero_msg(); - osDelay(500); - //osDelay(YELLOW_LED_BLINK_DELAY); - } - /* USER CODE END 5 */ + /* USER CODE BEGIN 5 */ + mpu_t *mpu = (mpu_t *)argument; + assert(mpu); + + /* Infinite loop */ + for (;;) { + /* Pet watchdog */ + HAL_IWDG_Refresh(&hiwdg); + /* Toggle LED at certain frequency */ + printf(".\n..\n"); + toggle_yled(mpu); + + // refresh the external watchdog so the car doesnt fault + pet_watchdog(mpu); + + /* Send NERO state data continuously */ + send_nero_msg(); + osDelay(500); + //osDelay(YELLOW_LED_BLINK_DELAY); + } + /* USER CODE END 5 */ } /** @@ -760,16 +741,15 @@ void StartDefaultTask(void *argument) */ void Error_Handler(void) { - /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ - __disable_irq(); - while (1) - { - } - /* USER CODE END Error_Handler_Debug */ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) { + } + /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. @@ -779,9 +759,9 @@ void Error_Handler(void) */ void assert_failed(uint8_t *file, uint32_t line) { - /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, + /* USER CODE BEGIN 6 */ + /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ - /* USER CODE END 6 */ + /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ From 66940a18510a703979884a936f0aa718cded660a Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 17 Jan 2025 16:52:32 -0500 Subject: [PATCH 09/18] Add CAN ID --- Core/Inc/control.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index 4587d48..043f60f 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -4,8 +4,7 @@ #include "pdu.h" #include "can.h" -// TODO: replace this temp value with real value -#define CONTROL_CANID_FANBATTBOX 0xA +#define CONTROL_CANID_FANBATTBOX 0xAAA extern osThreadId_t control_handle; extern const osThreadAttr_t control_attributes; From 6f69a356d6a706e6935fc5298c70f8ab9001941e Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Jan 2025 19:57:57 -0500 Subject: [PATCH 10/18] Revert main formatting --- Core/Src/main.c | 1005 ++++++++++++++++++++++++----------------------- 1 file changed, 512 insertions(+), 493 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index de44f4c..ec3a1a3 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -76,9 +76,9 @@ DMA_HandleTypeDef hdma_usart3_tx; /* Definitions for defaultTask */ osThreadId_t defaultTaskHandle; const osThreadAttr_t defaultTask_attributes = { - .name = "defaultTask", - .stack_size = 128 * 4, - .priority = (osPriority_t)osPriorityNormal, + .name = "defaultTask", + .stack_size = 128 * 4, + .priority = (osPriority_t) osPriorityNormal, }; /* USER CODE BEGIN PV */ osMessageQueueId_t imu_queue; @@ -112,18 +112,17 @@ void StartDefaultTask(void *argument); PUTCHAR_PROTOTYPE { - HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, HAL_MAX_DELAY); - return ch; + HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, HAL_MAX_DELAY); + return ch; } -int _write(int file, char *ptr, int len) -{ - int DataIdx; +int _write(int file, char* ptr, int len) { + int DataIdx; - for (DataIdx = 0; DataIdx < len; DataIdx++) { - __io_putchar(*ptr++); - } - return len; + for (DataIdx = 0; DataIdx < len; DataIdx++) { + __io_putchar( *ptr++ ); + } + return len; } /* USER CODE END 0 */ @@ -133,159 +132,150 @@ int _write(int file, char *ptr, int len) */ int main(void) { - /* USER CODE BEGIN 1 */ - printf("BOOT\n"); - /* USER CODE END 1 */ - - /* MCU Configuration--------------------------------------------------------*/ - - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ - HAL_Init(); - - /* USER CODE BEGIN Init */ - - HAL_Delay(2000); - - /* USER CODE END Init */ - - /* Configure the system clock */ - SystemClock_Config(); - - /* USER CODE BEGIN SysInit */ - - /* USER CODE END SysInit */ - - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_DMA_Init(); - MX_CAN1_Init(); - MX_I2C1_Init(); - MX_I2C2_Init(); - MX_ADC1_Init(); - MX_USART3_UART_Init(); - MX_ADC3_Init(); - MX_IWDG_Init(); - /* USER CODE BEGIN 2 */ - - /* Create Interfaces to Represent Relevant Hardware */ - mpu_t *mpu = init_mpu(&hadc3, &hadc1, GPIOC, GPIOB); - assert(mpu); - pdu_t *pdu = init_pdu(&hi2c2); - assert(pdu); - dti_t *mc = dti_init(); - assert(mc); - init_can1(&hcan1); - bms_init(); - - printf("\n\n\nInit Success...\n\n\n"); - - /* USER CODE END 2 */ - - /* Init scheduler */ - osKernelInitialize(); - - /* USER CODE BEGIN RTOS_MUTEX */ - - /* USER CODE END RTOS_MUTEX */ - - /* USER CODE BEGIN RTOS_SEMAPHORES */ - /* add semaphores, ... */ - /* USER CODE END RTOS_SEMAPHORES */ - - /* USER CODE BEGIN RTOS_TIMERS */ - /* start timers, add new ones, ... */ - /* USER CODE END RTOS_TIMERS */ - - /* USER CODE BEGIN RTOS_QUEUES */ - /* USER CODE END RTOS_QUEUES */ - - /* Create the thread(s) */ - /* creation of defaultTask */ - defaultTaskHandle = - osThreadNew(StartDefaultTask, mpu, &defaultTask_attributes); - - /* USER CODE BEGIN RTOS_THREADS */ - - /* Monitors */ - non_func_data_args_t *nfd_args = malloc(sizeof(non_func_data_args_t)); - nfd_args->mpu = mpu; - nfd_args->pdu = pdu; - non_functional_data_thead = - osThreadNew(vNonFunctionalDataCollection, nfd_args, - &non_functional_data_attributes); - assert(non_functional_data_thead); - - data_collection_args_t *data_args = - malloc(sizeof(data_collection_args_t)); - data_args->pdu = pdu; - data_collection_thread = osThreadNew(vDataCollection, data_args, - &data_collection_attributes); - assert(data_collection_thread); - // temp_monitor_handle = osThreadNew(vTempMonitor, mpu, &temp_monitor_attributes); - // assert(temp_monitor_handle); - //imu_monitor_handle = osThreadNew(vIMUMonitor, mpu, &imu_monitor_attributes); - //assert(imu_monitor_handle); - // shutdown_monitor_handle = osThreadNew(vShutdownMonitor, pdu, &shutdown_monitor_attributes); - // assert(shutdown_monitor_handle); - - /* Messaging */ - can_dispatch_handle = - osThreadNew(vCanDispatch, &hcan1, &can_dispatch_attributes); - assert(can_dispatch_handle); - can_receive_thread = - osThreadNew(vCanReceive, mc, &can_receive_attributes); - assert(can_receive_thread); - - /* Control Logic */ - fault_handle = - osThreadNew(vFaultHandler, NULL, &fault_handle_attributes); - assert(fault_handle); - - rtds_thread = osThreadNew(vRTDS, pdu, &rtds_attributes); - assert(rtds_thread); - - pedals_args_t *pedals_args = malloc(sizeof(pedals_args_t)); - pedals_args->mpu = mpu; - pedals_args->mc = mc; - pedals_args->pdu = pdu; - process_pedals_thread = osThreadNew(vProcessPedals, pedals_args, - &process_pedals_attributes); - assert(process_pedals_thread); - - sm_director_args_t *sm_args = malloc(sizeof(sm_director_args_t)); - sm_args->pdu = pdu; - sm_args->mc = mc; - sm_args->mpu = mpu; - sm_director_handle = osThreadNew(vStateMachineDirector, sm_args, - &sm_director_attributes); - assert(sm_director_handle); - - /* Control File Thread */ - control_args_t *control_args = malloc(sizeof(control_args_t)); - control_args->pdu = pdu; - control_handle = - osThreadNew(vControl, control_args, &control_attributes); - assert(control_handle); - - /* USER CODE END RTOS_THREADS */ - - /* USER CODE BEGIN RTOS_EVENTS */ - /* add events, ... */ - /* USER CODE END RTOS_EVENTS */ - - /* Start scheduler */ - osKernelStart(); - - /* We should never get here as control is now taken by the scheduler */ - - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - while (1) { - /* USER CODE END WHILE */ - - /* USER CODE BEGIN 3 */ - } - /* USER CODE END 3 */ + + /* USER CODE BEGIN 1 */ + printf("BOOT\n"); + /* USER CODE END 1 */ + + /* MCU Configuration--------------------------------------------------------*/ + + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); + + /* USER CODE BEGIN Init */ + + HAL_Delay(2000); + + /* USER CODE END Init */ + + /* Configure the system clock */ + SystemClock_Config(); + + /* USER CODE BEGIN SysInit */ + + /* USER CODE END SysInit */ + + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_DMA_Init(); + MX_CAN1_Init(); + MX_I2C1_Init(); + MX_I2C2_Init(); + MX_ADC1_Init(); + MX_USART3_UART_Init(); + MX_ADC3_Init(); + MX_IWDG_Init(); + /* USER CODE BEGIN 2 */ + + /* Create Interfaces to Represent Relevant Hardware */ + mpu_t *mpu = init_mpu(&hadc3, &hadc1, GPIOC, GPIOB); + assert(mpu); + pdu_t *pdu = init_pdu(&hi2c2); + assert(pdu); + dti_t *mc = dti_init(); + assert(mc); + init_can1(&hcan1); + bms_init(); + + printf("\n\n\nInit Success...\n\n\n"); + + /* USER CODE END 2 */ + + /* Init scheduler */ + osKernelInitialize(); + + /* USER CODE BEGIN RTOS_MUTEX */ + + /* USER CODE END RTOS_MUTEX */ + + /* USER CODE BEGIN RTOS_SEMAPHORES */ + /* add semaphores, ... */ + /* USER CODE END RTOS_SEMAPHORES */ + + /* USER CODE BEGIN RTOS_TIMERS */ + /* start timers, add new ones, ... */ + /* USER CODE END RTOS_TIMERS */ + + /* USER CODE BEGIN RTOS_QUEUES */ + /* USER CODE END RTOS_QUEUES */ + + /* Create the thread(s) */ + /* creation of defaultTask */ + defaultTaskHandle = osThreadNew(StartDefaultTask, mpu, &defaultTask_attributes); + + /* USER CODE BEGIN RTOS_THREADS */ + + /* Monitors */ + non_func_data_args_t *nfd_args = malloc(sizeof(non_func_data_args_t)); + nfd_args->mpu = mpu; + nfd_args->pdu = pdu; + non_functional_data_thead = osThreadNew(vNonFunctionalDataCollection, nfd_args, &non_functional_data_attributes); + assert(non_functional_data_thead); + + data_collection_args_t* data_args = malloc(sizeof(data_collection_args_t)); + data_args->pdu = pdu; + data_collection_thread = osThreadNew(vDataCollection, data_args, &data_collection_attributes); + assert(data_collection_thread); + // temp_monitor_handle = osThreadNew(vTempMonitor, mpu, &temp_monitor_attributes); + // assert(temp_monitor_handle); + //imu_monitor_handle = osThreadNew(vIMUMonitor, mpu, &imu_monitor_attributes); + //assert(imu_monitor_handle); + // shutdown_monitor_handle = osThreadNew(vShutdownMonitor, pdu, &shutdown_monitor_attributes); + // assert(shutdown_monitor_handle); + + /* Messaging */ + can_dispatch_handle = osThreadNew(vCanDispatch, &hcan1, &can_dispatch_attributes); + assert(can_dispatch_handle); + can_receive_thread = osThreadNew(vCanReceive, mc, &can_receive_attributes); + assert(can_receive_thread); + + /* Control Logic */ + fault_handle = osThreadNew(vFaultHandler, NULL, &fault_handle_attributes); + assert(fault_handle); + + rtds_thread = osThreadNew(vRTDS, pdu, &rtds_attributes); + assert(rtds_thread); + + pedals_args_t *pedals_args = malloc(sizeof(pedals_args_t)); + pedals_args->mpu = mpu; + pedals_args->mc = mc; + pedals_args->pdu = pdu; + process_pedals_thread = osThreadNew(vProcessPedals, pedals_args, &process_pedals_attributes); + assert(process_pedals_thread); + + sm_director_args_t *sm_args = malloc(sizeof(sm_director_args_t)); + sm_args->pdu = pdu; + sm_args->mc = mc; + sm_args->mpu = mpu; + sm_director_handle = osThreadNew(vStateMachineDirector, sm_args, &sm_director_attributes); + assert(sm_director_handle); + + /* Control File Thread */ + control_args_t *control_args = malloc(sizeof(control_args_t)); + control_args->pdu = pdu; + control_handle = osThreadNew(vControl, control_args, &control_attributes); + assert(control_handle); + + /* USER CODE END RTOS_THREADS */ + + /* USER CODE BEGIN RTOS_EVENTS */ + /* add events, ... */ + /* USER CODE END RTOS_EVENTS */ + + /* Start scheduler */ + osKernelStart(); + + /* We should never get here as control is now taken by the scheduler */ + + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + while (1) + { + /* USER CODE END WHILE */ + + /* USER CODE BEGIN 3 */ + } + /* USER CODE END 3 */ } /** @@ -294,41 +284,40 @@ int main(void) */ void SystemClock_Config(void) { - RCC_OscInitTypeDef RCC_OscInitStruct = { 0 }; - RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 }; + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - /** Configure the main internal regulator output voltage + /** Configure the main internal regulator output voltage */ - __HAL_RCC_PWR_CLK_ENABLE(); - __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + __HAL_RCC_PWR_CLK_ENABLE(); + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); - /** Initializes the RCC Oscillators according to the specified parameters + /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | - RCC_OSCILLATORTYPE_LSI; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - Error_Handler(); - } - - /** Initializes the CPU, AHB and APB buses clocks + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; + RCC_OscInitStruct.LSIState = RCC_LSI_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) + { + Error_Handler(); + } + + /** Initializes the CPU, AHB and APB buses clocks */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | - RCC_CLOCKTYPE_SYSCLK | - RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != - HAL_OK) { - Error_Handler(); - } + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK + |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) + { + Error_Handler(); + } } /** @@ -338,45 +327,49 @@ void SystemClock_Config(void) */ static void MX_ADC1_Init(void) { - /* USER CODE BEGIN ADC1_Init 0 */ - /* USER CODE END ADC1_Init 0 */ + /* USER CODE BEGIN ADC1_Init 0 */ - ADC_ChannelConfTypeDef sConfig = { 0 }; + /* USER CODE END ADC1_Init 0 */ - /* USER CODE BEGIN ADC1_Init 1 */ + ADC_ChannelConfTypeDef sConfig = {0}; - /* USER CODE END ADC1_Init 1 */ + /* USER CODE BEGIN ADC1_Init 1 */ - /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) + /* USER CODE END ADC1_Init 1 */ + + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ - hadc1.Instance = ADC1; - hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; - hadc1.Init.Resolution = ADC_RESOLUTION_12B; - hadc1.Init.ScanConvMode = ENABLE; - hadc1.Init.ContinuousConvMode = ENABLE; - hadc1.Init.DiscontinuousConvMode = DISABLE; - hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; - hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.NbrOfConversion = 1; - hadc1.Init.DMAContinuousRequests = ENABLE; - hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - if (HAL_ADC_Init(&hadc1) != HAL_OK) { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.ScanConvMode = ENABLE; + hadc1.Init.ContinuousConvMode = ENABLE; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DMAContinuousRequests = ENABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + if (HAL_ADC_Init(&hadc1) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_8; - sConfig.Rank = 1; - sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN ADC1_Init 2 */ - - /* USER CODE END ADC1_Init 2 */ + sConfig.Channel = ADC_CHANNEL_8; + sConfig.Rank = 1; + sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ + } /** @@ -386,69 +379,76 @@ static void MX_ADC1_Init(void) */ static void MX_ADC3_Init(void) { - /* USER CODE BEGIN ADC3_Init 0 */ - /* USER CODE END ADC3_Init 0 */ + /* USER CODE BEGIN ADC3_Init 0 */ + + /* USER CODE END ADC3_Init 0 */ - ADC_ChannelConfTypeDef sConfig = { 0 }; + ADC_ChannelConfTypeDef sConfig = {0}; - /* USER CODE BEGIN ADC3_Init 1 */ + /* USER CODE BEGIN ADC3_Init 1 */ - /* USER CODE END ADC3_Init 1 */ + /* USER CODE END ADC3_Init 1 */ - /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ - hadc3.Instance = ADC3; - hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; - hadc3.Init.Resolution = ADC_RESOLUTION_12B; - hadc3.Init.ScanConvMode = ENABLE; - hadc3.Init.ContinuousConvMode = ENABLE; - hadc3.Init.DiscontinuousConvMode = DISABLE; - hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START; - hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc3.Init.NbrOfConversion = 4; - hadc3.Init.DMAContinuousRequests = ENABLE; - hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - if (HAL_ADC_Init(&hadc3) != HAL_OK) { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + hadc3.Instance = ADC3; + hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6; + hadc3.Init.Resolution = ADC_RESOLUTION_12B; + hadc3.Init.ScanConvMode = ENABLE; + hadc3.Init.ContinuousConvMode = ENABLE; + hadc3.Init.DiscontinuousConvMode = DISABLE; + hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc3.Init.NbrOfConversion = 4; + hadc3.Init.DMAContinuousRequests = ENABLE; + hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + if (HAL_ADC_Init(&hadc3) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_2; - sConfig.Rank = 1; - sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + sConfig.Channel = ADC_CHANNEL_2; + sConfig.Rank = 1; + sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; + if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_3; - sConfig.Rank = 2; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + sConfig.Channel = ADC_CHANNEL_3; + sConfig.Rank = 2; + if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_0; - sConfig.Rank = 3; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { - Error_Handler(); - } - - /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + sConfig.Channel = ADC_CHANNEL_0; + sConfig.Rank = 3; + if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) + { + Error_Handler(); + } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_1; - sConfig.Rank = 4; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN ADC3_Init 2 */ - - /* USER CODE END ADC3_Init 2 */ + sConfig.Channel = ADC_CHANNEL_1; + sConfig.Rank = 4; + if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN ADC3_Init 2 */ + + /* USER CODE END ADC3_Init 2 */ + } /** @@ -458,31 +458,34 @@ static void MX_ADC3_Init(void) */ static void MX_CAN1_Init(void) { - /* USER CODE BEGIN CAN1_Init 0 */ - - /* USER CODE END CAN1_Init 0 */ - - /* USER CODE BEGIN CAN1_Init 1 */ - - /* USER CODE END CAN1_Init 1 */ - hcan1.Instance = CAN1; - hcan1.Init.Prescaler = 2; - hcan1.Init.Mode = CAN_MODE_NORMAL; - hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ; - hcan1.Init.TimeSeg1 = CAN_BS1_13TQ; - hcan1.Init.TimeSeg2 = CAN_BS2_2TQ; - hcan1.Init.TimeTriggeredMode = DISABLE; - hcan1.Init.AutoBusOff = ENABLE; - hcan1.Init.AutoWakeUp = DISABLE; - hcan1.Init.AutoRetransmission = DISABLE; - hcan1.Init.ReceiveFifoLocked = DISABLE; - hcan1.Init.TransmitFifoPriority = DISABLE; - if (HAL_CAN_Init(&hcan1) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN CAN1_Init 2 */ - - /* USER CODE END CAN1_Init 2 */ + + /* USER CODE BEGIN CAN1_Init 0 */ + + /* USER CODE END CAN1_Init 0 */ + + /* USER CODE BEGIN CAN1_Init 1 */ + + /* USER CODE END CAN1_Init 1 */ + hcan1.Instance = CAN1; + hcan1.Init.Prescaler = 2; + hcan1.Init.Mode = CAN_MODE_NORMAL; + hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ; + hcan1.Init.TimeSeg1 = CAN_BS1_13TQ; + hcan1.Init.TimeSeg2 = CAN_BS2_2TQ; + hcan1.Init.TimeTriggeredMode = DISABLE; + hcan1.Init.AutoBusOff = ENABLE; + hcan1.Init.AutoWakeUp = DISABLE; + hcan1.Init.AutoRetransmission = DISABLE; + hcan1.Init.ReceiveFifoLocked = DISABLE; + hcan1.Init.TransmitFifoPriority = DISABLE; + if (HAL_CAN_Init(&hcan1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN CAN1_Init 2 */ + + /* USER CODE END CAN1_Init 2 */ + } /** @@ -492,28 +495,31 @@ static void MX_CAN1_Init(void) */ static void MX_I2C1_Init(void) { - /* USER CODE BEGIN I2C1_Init 0 */ - - /* USER CODE END I2C1_Init 0 */ - - /* USER CODE BEGIN I2C1_Init 1 */ - - /* USER CODE END I2C1_Init 1 */ - hi2c1.Instance = I2C1; - hi2c1.Init.ClockSpeed = 100000; - hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; - hi2c1.Init.OwnAddress1 = 0; - hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c1.Init.OwnAddress2 = 0; - hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c1) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN I2C1_Init 2 */ - - /* USER CODE END I2C1_Init 2 */ + + /* USER CODE BEGIN I2C1_Init 0 */ + + /* USER CODE END I2C1_Init 0 */ + + /* USER CODE BEGIN I2C1_Init 1 */ + + /* USER CODE END I2C1_Init 1 */ + hi2c1.Instance = I2C1; + hi2c1.Init.ClockSpeed = 100000; + hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; + hi2c1.Init.OwnAddress1 = 0; + hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c1.Init.OwnAddress2 = 0; + hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN I2C1_Init 2 */ + + /* USER CODE END I2C1_Init 2 */ + } /** @@ -523,28 +529,31 @@ static void MX_I2C1_Init(void) */ static void MX_I2C2_Init(void) { - /* USER CODE BEGIN I2C2_Init 0 */ - - /* USER CODE END I2C2_Init 0 */ - - /* USER CODE BEGIN I2C2_Init 1 */ - - /* USER CODE END I2C2_Init 1 */ - hi2c2.Instance = I2C2; - hi2c2.Init.ClockSpeed = 100000; - hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; - hi2c2.Init.OwnAddress1 = 0; - hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c2.Init.OwnAddress2 = 0; - hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c2) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN I2C2_Init 2 */ - - /* USER CODE END I2C2_Init 2 */ + + /* USER CODE BEGIN I2C2_Init 0 */ + + /* USER CODE END I2C2_Init 0 */ + + /* USER CODE BEGIN I2C2_Init 1 */ + + /* USER CODE END I2C2_Init 1 */ + hi2c2.Instance = I2C2; + hi2c2.Init.ClockSpeed = 100000; + hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; + hi2c2.Init.OwnAddress1 = 0; + hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c2.Init.OwnAddress2 = 0; + hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c2) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN I2C2_Init 2 */ + + /* USER CODE END I2C2_Init 2 */ + } /** @@ -554,22 +563,25 @@ static void MX_I2C2_Init(void) */ static void MX_IWDG_Init(void) { - /* USER CODE BEGIN IWDG_Init 0 */ - /* USER CODE END IWDG_Init 0 */ + /* USER CODE BEGIN IWDG_Init 0 */ + + /* USER CODE END IWDG_Init 0 */ - /* USER CODE BEGIN IWDG_Init 1 */ + /* USER CODE BEGIN IWDG_Init 1 */ - /* USER CODE END IWDG_Init 1 */ - hiwdg.Instance = IWDG; - hiwdg.Init.Prescaler = IWDG_PRESCALER_32; - hiwdg.Init.Reload = 4095; - if (HAL_IWDG_Init(&hiwdg) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN IWDG_Init 2 */ + /* USER CODE END IWDG_Init 1 */ + hiwdg.Instance = IWDG; + hiwdg.Init.Prescaler = IWDG_PRESCALER_32; + hiwdg.Init.Reload = 4095; + if (HAL_IWDG_Init(&hiwdg) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN IWDG_Init 2 */ + + /* USER CODE END IWDG_Init 2 */ - /* USER CODE END IWDG_Init 2 */ } /** @@ -579,27 +591,30 @@ static void MX_IWDG_Init(void) */ static void MX_USART3_UART_Init(void) { - /* USER CODE BEGIN USART3_Init 0 */ - - /* USER CODE END USART3_Init 0 */ - - /* USER CODE BEGIN USART3_Init 1 */ - - /* USER CODE END USART3_Init 1 */ - huart3.Instance = USART3; - huart3.Init.BaudRate = 115200; - huart3.Init.WordLength = UART_WORDLENGTH_8B; - huart3.Init.StopBits = UART_STOPBITS_1; - huart3.Init.Parity = UART_PARITY_NONE; - huart3.Init.Mode = UART_MODE_TX_RX; - huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart3.Init.OverSampling = UART_OVERSAMPLING_16; - if (HAL_UART_Init(&huart3) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN USART3_Init 2 */ - - /* USER CODE END USART3_Init 2 */ + + /* USER CODE BEGIN USART3_Init 0 */ + + /* USER CODE END USART3_Init 0 */ + + /* USER CODE BEGIN USART3_Init 1 */ + + /* USER CODE END USART3_Init 1 */ + huart3.Instance = USART3; + huart3.Init.BaudRate = 115200; + huart3.Init.WordLength = UART_WORDLENGTH_8B; + huart3.Init.StopBits = UART_STOPBITS_1; + huart3.Init.Parity = UART_PARITY_NONE; + huart3.Init.Mode = UART_MODE_TX_RX; + huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart3.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart3) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN USART3_Init 2 */ + + /* USER CODE END USART3_Init 2 */ + } /** @@ -607,14 +622,16 @@ static void MX_USART3_UART_Init(void) */ static void MX_DMA_Init(void) { - /* DMA controller clock enable */ - __HAL_RCC_DMA2_CLK_ENABLE(); - __HAL_RCC_DMA1_CLK_ENABLE(); - - /* DMA interrupt init */ - /* DMA1_Stream3_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 5, 0); - HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn); + + /* DMA controller clock enable */ + __HAL_RCC_DMA2_CLK_ENABLE(); + __HAL_RCC_DMA1_CLK_ENABLE(); + + /* DMA interrupt init */ + /* DMA1_Stream3_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn); + } /** @@ -624,79 +641,78 @@ static void MX_DMA_Init(void) */ static void MX_GPIO_Init(void) { - GPIO_InitTypeDef GPIO_InitStruct = { 0 }; - /* USER CODE BEGIN MX_GPIO_Init_1 */ - /* USER CODE END MX_GPIO_Init_1 */ - - /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOH_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9, - GPIO_PIN_RESET); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET); - - /*Configure GPIO pins : PC3 PC8 PC9 */ - GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - - /*Configure GPIO pins : PA4 PA5 PA6 PA7 */ - GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /*Configure GPIO pin : PC4 */ - GPIO_InitStruct.Pin = GPIO_PIN_4; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - - /*Configure GPIO pin : PB1 */ - GPIO_InitStruct.Pin = GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pin : PB2 */ - GPIO_InitStruct.Pin = GPIO_PIN_2; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF15_EVENTOUT; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pin : PB15 */ - GPIO_InitStruct.Pin = GPIO_PIN_15; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pin : PA9 */ - GPIO_InitStruct.Pin = GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /*Configure GPIO pins : PA11 PA12 */ - GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /* USER CODE BEGIN MX_GPIO_Init_2 */ - /* USER CODE END MX_GPIO_Init_2 */ + GPIO_InitTypeDef GPIO_InitStruct = {0}; +/* USER CODE BEGIN MX_GPIO_Init_1 */ +/* USER CODE END MX_GPIO_Init_1 */ + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOH_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET); + + /*Configure GPIO pins : PC3 PC8 PC9 */ + GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_8|GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /*Configure GPIO pins : PA4 PA5 PA6 PA7 */ + GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /*Configure GPIO pin : PC4 */ + GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /*Configure GPIO pin : PB1 */ + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pin : PB2 */ + GPIO_InitStruct.Pin = GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF15_EVENTOUT; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pin : PB15 */ + GPIO_InitStruct.Pin = GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pin : PA9 */ + GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /*Configure GPIO pins : PA11 PA12 */ + GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + +/* USER CODE BEGIN MX_GPIO_Init_2 */ +/* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ @@ -712,27 +728,29 @@ static void MX_GPIO_Init(void) /* USER CODE END Header_StartDefaultTask */ void StartDefaultTask(void *argument) { - /* USER CODE BEGIN 5 */ - mpu_t *mpu = (mpu_t *)argument; - assert(mpu); - - /* Infinite loop */ - for (;;) { - /* Pet watchdog */ - HAL_IWDG_Refresh(&hiwdg); - /* Toggle LED at certain frequency */ - printf(".\n..\n"); - toggle_yled(mpu); - - // refresh the external watchdog so the car doesnt fault - pet_watchdog(mpu); - - /* Send NERO state data continuously */ - send_nero_msg(); - osDelay(500); - //osDelay(YELLOW_LED_BLINK_DELAY); - } - /* USER CODE END 5 */ + /* USER CODE BEGIN 5 */ + mpu_t *mpu = (mpu_t *) argument; + assert(mpu); + + /* Infinite loop */ + for(;;) { + + /* Pet watchdog */ + HAL_IWDG_Refresh(&hiwdg); + /* Toggle LED at certain frequency */ + printf(".\n..\n"); + toggle_yled(mpu); + + // refresh the external watchdog so the car doesnt fault + pet_watchdog(mpu); + + + /* Send NERO state data continuously */ + send_nero_msg(); + osDelay(500); + //osDelay(YELLOW_LED_BLINK_DELAY); + } + /* USER CODE END 5 */ } /** @@ -741,15 +759,16 @@ void StartDefaultTask(void *argument) */ void Error_Handler(void) { - /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ - __disable_irq(); - while (1) { - } - /* USER CODE END Error_Handler_Debug */ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) + { + } + /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. @@ -759,9 +778,9 @@ void Error_Handler(void) */ void assert_failed(uint8_t *file, uint32_t line) { - /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, + /* USER CODE BEGIN 6 */ + /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ - /* USER CODE END 6 */ + /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ From 72de7a0b6adba94aece5cde3368ef616d6576c64 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Jan 2025 20:25:27 -0500 Subject: [PATCH 11/18] small fixes --- Core/Src/can_handler.c | 3 --- Core/Src/main.c | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Core/Src/can_handler.c b/Core/Src/can_handler.c index 499fc47..22dca6c 100644 --- a/Core/Src/can_handler.c +++ b/Core/Src/can_handler.c @@ -10,9 +10,6 @@ #include "fault.h" #include "steeringio.h" #include "control.h" -#include -#include -#include #define CAN_MSG_QUEUE_SIZE 50 /* messages */ diff --git a/Core/Src/main.c b/Core/Src/main.c index 7d356d3..f4d867e 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -769,8 +769,8 @@ void StartDefaultTask(void *argument) printf(".\n..\n"); toggle_yled(mpu); - // refresh the external watchdog so the car doesnt fault - pet_watchdog(mpu); + // refresh the external watchdog so the car doesnt fault + pet_watchdog(mpu); /* Send NERO state data continuously */ send_nero_msg(); From f7cc2d09468db0d5b3503cb3c86233c5bc72c39e Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Jan 2025 20:28:38 -0500 Subject: [PATCH 12/18] Hopefully fix main formatting --- Core/Src/main.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index f4d867e..dd7e4b1 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -769,15 +769,16 @@ void StartDefaultTask(void *argument) printf(".\n..\n"); toggle_yled(mpu); - // refresh the external watchdog so the car doesnt fault - pet_watchdog(mpu); - - /* Send NERO state data continuously */ - send_nero_msg(); - osDelay(500); - //osDelay(YELLOW_LED_BLINK_DELAY); - } - /* USER CODE END 5 */ + // refresh the external watchdog so the car doesnt fault + pet_watchdog(mpu); + + + /* Send NERO state data continuously */ + send_git_version_message(); + osDelay(500); + //osDelay(YELLOW_LED_BLINK_DELAY); + } + /* USER CODE END 5 */ } /** From 755fad4f2718a45f245c03e83ebe18a58c644402 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Jan 2025 20:31:54 -0500 Subject: [PATCH 13/18] changed control thread args --- Core/Inc/control.h | 4 ---- Core/Src/control.c | 4 ++-- Core/Src/main.c | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index 043f60f..e195f29 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -9,10 +9,6 @@ extern osThreadId_t control_handle; extern const osThreadAttr_t control_attributes; -typedef struct { - pdu_t *pdu; -} control_args_t; - void vControl(void *param); void control_fanbattbox_record(can_msg_t args); diff --git a/Core/Src/control.c b/Core/Src/control.c index 49b0151..9d9dfc5 100644 --- a/Core/Src/control.c +++ b/Core/Src/control.c @@ -12,10 +12,10 @@ static int fanBattBoxState = 0; void vControl(void *param) { - control_args_t *args = (control_args_t *)param; + pdu_t *pdu = (pdu_t *)param; for (;;) { - write_fan_battbox(args->pdu, fanBattBoxState); + write_fan_battbox(pdu, fanBattBoxState); osDelay(1000); } diff --git a/Core/Src/main.c b/Core/Src/main.c index dd7e4b1..1964f1b 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -251,9 +251,7 @@ int main(void) assert(sm_director_handle); /* Control File Thread */ - control_args_t *control_args = malloc(sizeof(control_args_t)); - control_args->pdu = pdu; - control_handle = osThreadNew(vControl, control_args, &control_attributes); + control_handle = osThreadNew(vControl, pdu, &control_attributes); assert(control_handle); /* USER CODE END RTOS_THREADS */ From 7e9a12fc335c412aedede6be437a5832991ad832 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Jan 2025 20:33:51 -0500 Subject: [PATCH 14/18] Small formatting thing --- Core/Src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index 1964f1b..c7b8748 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -770,7 +770,7 @@ void StartDefaultTask(void *argument) // refresh the external watchdog so the car doesnt fault pet_watchdog(mpu); - + /* Send NERO state data continuously */ send_git_version_message(); osDelay(500); From 23035b44dd0f8468e931429e4a1627060087e95a Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Jan 2025 20:45:55 -0500 Subject: [PATCH 15/18] Changing ID --- Core/Inc/control.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index e195f29..0ae0eb6 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -4,7 +4,7 @@ #include "pdu.h" #include "can.h" -#define CONTROL_CANID_FANBATTBOX 0xAAA +#define CONTROL_CANID_FANBATTBOX 0xFAC extern osThreadId_t control_handle; extern const osThreadAttr_t control_attributes; From fa54398f609a4178cada51179e84836e55287ce4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 23 Jan 2025 14:34:17 -0500 Subject: [PATCH 16/18] Change CAN ID to 0x4A1 --- Core/Inc/control.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index 0ae0eb6..ad0ff9a 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -4,7 +4,7 @@ #include "pdu.h" #include "can.h" -#define CONTROL_CANID_FANBATTBOX 0xFAC +#define CONTROL_CANID_FANBATTBOX 0x4A1 extern osThreadId_t control_handle; extern const osThreadAttr_t control_attributes; From b6eb4752727c1138b43e3ea44da3324b03c0c703 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 23 Jan 2025 14:37:52 -0500 Subject: [PATCH 17/18] Fix control fanbattbox record function --- Core/Src/control.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Src/control.c b/Core/Src/control.c index 9d9dfc5..b934abf 100644 --- a/Core/Src/control.c +++ b/Core/Src/control.c @@ -23,7 +23,7 @@ void vControl(void *param) void control_fanbattbox_record(can_msg_t msg) { - if (msg.data > 0) { + if (msg.data[0] > 0) { fanBattBoxState = 1; } else { fanBattBoxState = 0; From 417216bdab19bc9c2f2b7ae8f06cb2a7ad4223e7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 23 Jan 2025 20:02:46 -0500 Subject: [PATCH 18/18] Remove old function --- Core/Inc/control.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/Core/Inc/control.h b/Core/Inc/control.h index ad0ff9a..d6c349e 100644 --- a/Core/Inc/control.h +++ b/Core/Inc/control.h @@ -13,6 +13,4 @@ void vControl(void *param); void control_fanbattbox_record(can_msg_t args); -int eval_fanbattbox_state(); - #endif \ No newline at end of file