From ec9fa4bb0fd158ac9c2af9165565f0e410566639 Mon Sep 17 00:00:00 2001 From: Jasper Date: Sun, 29 May 2022 00:47:46 +0200 Subject: [PATCH] Add .clang-format and .editorconfig --- .clang-format | 137 +++++++ .editorconfig | 8 + .gitignore | 2 +- Core/Inc/events.h | 4 +- Core/Inc/leds.h | 2 +- Core/Inc/main.h | 212 +++++------ Core/Inc/rpi.h | 2 +- Core/Inc/user_inputs.h | 7 +- Core/Inc/vehicle.h | 5 +- Core/Src/events.c | 6 +- Core/Src/leds.c | 4 +- Core/Src/main.c | 798 +++++++++++++++++++++-------------------- Core/Src/rpi.c | 17 +- Core/Src/state.c | 2 +- Core/Src/user_inputs.c | 13 +- Core/Src/vehicle.c | 14 +- 16 files changed, 697 insertions(+), 536 deletions(-) create mode 100644 .clang-format create mode 100644 .editorconfig diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0486d45 --- /dev/null +++ b/.clang-format @@ -0,0 +1,137 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: false +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: "^ IWYU pragma:" +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^[<"]stm' + Priority: 2 + SortPriority: 0 + - Regex: "^ static volatile EventQueue event_queue = {.read_idx = 0, .write_idx = 0}; -void event_push(Event *ev) { +void event_push(Event* ev) { if (event_num_free() == 0) { // TODO: How do we handle this? return; @@ -61,4 +63,4 @@ void handle_events() { Error_Handler(); } } -} \ No newline at end of file +} diff --git a/Core/Src/leds.c b/Core/Src/leds.c index aea30c5..7102a18 100644 --- a/Core/Src/leds.c +++ b/Core/Src/leds.c @@ -1,5 +1,7 @@ #include "leds.h" + #include "main.h" + #include "stm32g4xx_hal.h" #include "stm32g4xx_hal_gpio.h" @@ -24,4 +26,4 @@ void leds_init() { HAL_GPIO_WritePin(LED_OCLK_GPIO_Port, LED_OCLK_Pin, GPIO_PIN_SET); HAL_Delay(10); HAL_GPIO_WritePin(LED_OCLK_GPIO_Port, LED_OCLK_Pin, GPIO_PIN_RESET); -} \ No newline at end of file +} diff --git a/Core/Src/main.c b/Core/Src/main.c index 4d16741..23191b1 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -1,401 +1,405 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file : main.c - * @brief : Main program body - ****************************************************************************** - * @attention - * - * Copyright (c) 2022 STMicroelectronics. - * All rights reserved. - * - * This software is licensed under terms that can be found in the LICENSE file - * in the root directory of this software component. - * If no LICENSE file comes with this software, it is provided AS-IS. - * - ****************************************************************************** - */ -/* USER CODE END Header */ -/* Includes ------------------------------------------------------------------*/ -#include "main.h" - -/* Private includes ----------------------------------------------------------*/ -/* USER CODE BEGIN Includes */ -#include "events.h" +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2022 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +#include "events.h" #include "leds.h" -#include "rpi.h" -#include "state.h" +#include "rpi.h" +#include "state.h" #include "vehicle.h" - -#include "stm32g4xx_hal.h" -#include "stm32g4xx_hal_gpio.h" - -#include - -/* USER CODE END Includes */ - -/* Private typedef -----------------------------------------------------------*/ -/* USER CODE BEGIN PTD */ - -/* USER CODE END PTD */ - -/* Private define ------------------------------------------------------------*/ -/* USER CODE BEGIN PD */ -/* USER CODE END PD */ - -/* Private macro -------------------------------------------------------------*/ -/* USER CODE BEGIN PM */ - -/* USER CODE END PM */ - -/* Private variables ---------------------------------------------------------*/ -ADC_HandleTypeDef hadc1; - -FDCAN_HandleTypeDef hfdcan1; - -I2C_HandleTypeDef hi2c2; - -/* USER CODE BEGIN PV */ - -/* USER CODE END PV */ - -/* Private function prototypes -----------------------------------------------*/ -void SystemClock_Config(void); -static void MX_GPIO_Init(void); -static void MX_ADC1_Init(void); -static void MX_FDCAN1_Init(void); -static void MX_I2C2_Init(void); -/* USER CODE BEGIN PFP */ - -/* USER CODE END PFP */ - -/* Private user code ---------------------------------------------------------*/ -/* USER CODE BEGIN 0 */ - -/* USER CODE END 0 */ - -/** - * @brief The application entry point. - * @retval int - */ -int main(void) { - /* USER CODE BEGIN 1 */ - - /* USER CODE END 1 */ - - /* MCU Configuration--------------------------------------------------------*/ - - /* Reset of all peripherals, Initializes the Flash interface and the Systick. - */ - HAL_Init(); - - /* USER CODE BEGIN Init */ - - /* 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_ADC1_Init(); - MX_FDCAN1_Init(); - MX_I2C2_Init(); - /* USER CODE BEGIN 2 */ + +#include "stm32g4xx_hal.h" +#include "stm32g4xx_hal_gpio.h" + +#include + +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN PTD */ + +/* USER CODE END PTD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ +ADC_HandleTypeDef hadc1; + +FDCAN_HandleTypeDef hfdcan1; + +I2C_HandleTypeDef hi2c2; + +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +void SystemClock_Config(void); +static void MX_GPIO_Init(void); +static void MX_ADC1_Init(void); +static void MX_FDCAN1_Init(void); +static void MX_I2C2_Init(void); +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/** + * @brief The application entry point. + * @retval int + */ +int main(void) { + /* USER CODE BEGIN 1 */ + + /* USER CODE END 1 */ + + /* MCU Configuration--------------------------------------------------------*/ + + /* Reset of all peripherals, Initializes the Flash interface and the Systick. + */ + HAL_Init(); + + /* USER CODE BEGIN Init */ + + /* 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_ADC1_Init(); + MX_FDCAN1_Init(); + MX_I2C2_Init(); + /* USER CODE BEGIN 2 */ leds_init(); - state_init(); + state_init(); vehicle_init(&hfdcan1); - rpi_init(&hi2c2); - /* USER CODE END 2 */ - - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - uint32_t rpi_active_since = 0; - while (1) { - /* USER CODE END WHILE */ - - /* USER CODE BEGIN 3 */ - uint32_t now = HAL_GetTick(); - if (now < 1000) { - HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_RESET); - } else { - HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_SET); - } - handle_events(); - } - /* USER CODE END 3 */ -} - -/** - * @brief System Clock Configuration - * @retval None - */ -void SystemClock_Config(void) { - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - - /** Configure the main internal regulator output voltage - */ - HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); - - /** Initializes the RCC Oscillators according to the specified parameters - * in the RCC_OscInitTypeDef structure. - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; - RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1; - RCC_OscInitStruct.PLL.PLLN = 10; - RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; - RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; - RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; - 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_PLLCLK; - 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_2) != HAL_OK) { - Error_Handler(); - } -} - -/** - * @brief ADC1 Initialization Function - * @param None - * @retval None - */ -static void MX_ADC1_Init(void) { - - /* USER CODE BEGIN ADC1_Init 0 */ - - /* USER CODE END ADC1_Init 0 */ - - ADC_MultiModeTypeDef multimode = {0}; - ADC_ChannelConfTypeDef sConfig = {0}; - - /* USER CODE BEGIN ADC1_Init 1 */ - - /* USER CODE END ADC1_Init 1 */ - - /** Common config - */ - hadc1.Instance = ADC1; - hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; - hadc1.Init.Resolution = ADC_RESOLUTION_12B; - hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.GainCompensation = 0; - hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; - hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - hadc1.Init.LowPowerAutoWait = DISABLE; - hadc1.Init.ContinuousConvMode = DISABLE; - hadc1.Init.NbrOfConversion = 1; - hadc1.Init.DiscontinuousConvMode = DISABLE; - hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; - hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - hadc1.Init.DMAContinuousRequests = DISABLE; - hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; - hadc1.Init.OversamplingMode = DISABLE; - if (HAL_ADC_Init(&hadc1) != HAL_OK) { - Error_Handler(); - } - - /** Configure the ADC multi-mode - */ - multimode.Mode = ADC_MODE_INDEPENDENT; - if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) { - Error_Handler(); - } - - /** Configure Regular Channel - */ - sConfig.Channel = ADC_CHANNEL_14; - sConfig.Rank = ADC_REGULAR_RANK_1; - sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5; - sConfig.SingleDiff = ADC_SINGLE_ENDED; - sConfig.OffsetNumber = ADC_OFFSET_NONE; - sConfig.Offset = 0; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN ADC1_Init 2 */ - - /* USER CODE END ADC1_Init 2 */ -} - -/** - * @brief FDCAN1 Initialization Function - * @param None - * @retval None - */ -static void MX_FDCAN1_Init(void) { - - /* USER CODE BEGIN FDCAN1_Init 0 */ - - /* USER CODE END FDCAN1_Init 0 */ - - /* USER CODE BEGIN FDCAN1_Init 1 */ - - /* USER CODE END FDCAN1_Init 1 */ - hfdcan1.Instance = FDCAN1; - hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1; - hfdcan1.Init.FrameFormat = FDCAN_FRAME_FD_BRS; - hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; - hfdcan1.Init.AutoRetransmission = ENABLE; - hfdcan1.Init.TransmitPause = ENABLE; - hfdcan1.Init.ProtocolException = DISABLE; - hfdcan1.Init.NominalPrescaler = 1; - hfdcan1.Init.NominalSyncJumpWidth = 16; - hfdcan1.Init.NominalTimeSeg1 = 63; - hfdcan1.Init.NominalTimeSeg2 = 16; - hfdcan1.Init.DataPrescaler = 1; - hfdcan1.Init.DataSyncJumpWidth = 4; - hfdcan1.Init.DataTimeSeg1 = 5; - hfdcan1.Init.DataTimeSeg2 = 4; - hfdcan1.Init.StdFiltersNbr = 2; - hfdcan1.Init.ExtFiltersNbr = 0; - hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; - if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN FDCAN1_Init 2 */ - - /* USER CODE END FDCAN1_Init 2 */ -} - -/** - * @brief I2C2 Initialization Function - * @param None - * @retval None - */ -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.Timing = 0x10909CEC; - hi2c2.Init.OwnAddress1 = 64; - hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c2.Init.OwnAddress2 = 0; - hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK; - hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c2) != HAL_OK) { - Error_Handler(); - } - - /** Configure Analogue filter - */ - if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK) { - Error_Handler(); - } - - /** Configure Digital filter - */ - if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK) { - Error_Handler(); - } - /* USER CODE BEGIN I2C2_Init 2 */ - - /* USER CODE END I2C2_Init 2 */ -} - -/** - * @brief GPIO Initialization Function - * @param None - * @retval None - */ -static void MX_GPIO_Init(void) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - - /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOA, - TOP_PWM_Pin | LOGO_PWM_Pin | LED_ICLK_Pin | LED_OCLK_Pin | - LED_NCLR_Pin | LED_SER_Pin | LED_NOE_Pin | - DISP_RESET_Pin, - GPIO_PIN_RESET); - - /*Configure GPIO pins : TOP_PWM_Pin LOGO_PWM_Pin LED_ICLK_Pin LED_OCLK_Pin - LED_NCLR_Pin LED_SER_Pin LED_NOE_Pin DISP_RESET_Pin - */ - GPIO_InitStruct.Pin = TOP_PWM_Pin | LOGO_PWM_Pin | LED_ICLK_Pin | - LED_OCLK_Pin | LED_NCLR_Pin | LED_SER_Pin | - LED_NOE_Pin | DISP_RESET_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /*Configure GPIO pins : BTN0_Pin BTN1_Pin BTN2_Pin BTN3_Pin */ - GPIO_InitStruct.Pin = BTN0_Pin | BTN1_Pin | BTN2_Pin | BTN3_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; - GPIO_InitStruct.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pins : ENC0A_Pin ENC0B_Pin ENC1A_Pin ENC1B_Pin */ - GPIO_InitStruct.Pin = ENC0A_Pin | ENC0B_Pin | ENC1A_Pin | ENC1B_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_PULLUP; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /* EXTI interrupt init*/ - HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); -} - -/* USER CODE BEGIN 4 */ - -/* USER CODE END 4 */ - -/** - * @brief This function is executed in case of error occurrence. - * @retval None - */ -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 */ -} - -#ifdef USE_FULL_ASSERT -/** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -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, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, - line) */ - /* USER CODE END 6 */ -} -#endif /* USE_FULL_ASSERT */ + rpi_init(&hi2c2); + /* USER CODE END 2 */ + + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + uint32_t last_can = 0; + while (1) { + /* USER CODE END WHILE */ + + /* USER CODE BEGIN 3 */ + uint32_t now = HAL_GetTick(); + if (now < 1000) { + HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_RESET); + } else { + HAL_GPIO_WritePin(DISP_RESET_GPIO_Port, DISP_RESET_Pin, GPIO_PIN_SET); + } + handle_events(); + if (now - last_can > 100) { + // vehicle_send_mission_select(MISSION_ACCELERATION); + last_can = now; + } + } + /* USER CODE END 3 */ +} + +/** + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) { + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + + /** Configure the main internal regulator output voltage + */ + HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1); + + /** Initializes the RCC Oscillators according to the specified parameters + * in the RCC_OscInitTypeDef structure. + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; + RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1; + RCC_OscInitStruct.PLL.PLLN = 10; + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; + RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; + RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; + 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_PLLCLK; + 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_2) != HAL_OK) { + Error_Handler(); + } +} + +/** + * @brief ADC1 Initialization Function + * @param None + * @retval None + */ +static void MX_ADC1_Init(void) { + + /* USER CODE BEGIN ADC1_Init 0 */ + + /* USER CODE END ADC1_Init 0 */ + + ADC_MultiModeTypeDef multimode = {0}; + ADC_ChannelConfTypeDef sConfig = {0}; + + /* USER CODE BEGIN ADC1_Init 1 */ + + /* USER CODE END ADC1_Init 1 */ + + /** Common config + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.GainCompensation = 0; + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; + hadc1.Init.OversamplingMode = DISABLE; + if (HAL_ADC_Init(&hadc1) != HAL_OK) { + Error_Handler(); + } + + /** Configure the ADC multi-mode + */ + multimode.Mode = ADC_MODE_INDEPENDENT; + if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) { + Error_Handler(); + } + + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_14; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ +} + +/** + * @brief FDCAN1 Initialization Function + * @param None + * @retval None + */ +static void MX_FDCAN1_Init(void) { + + /* USER CODE BEGIN FDCAN1_Init 0 */ + + /* USER CODE END FDCAN1_Init 0 */ + + /* USER CODE BEGIN FDCAN1_Init 1 */ + + /* USER CODE END FDCAN1_Init 1 */ + hfdcan1.Instance = FDCAN1; + hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1; + hfdcan1.Init.FrameFormat = FDCAN_FRAME_FD_BRS; + hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; + hfdcan1.Init.AutoRetransmission = ENABLE; + hfdcan1.Init.TransmitPause = ENABLE; + hfdcan1.Init.ProtocolException = DISABLE; + hfdcan1.Init.NominalPrescaler = 1; + hfdcan1.Init.NominalSyncJumpWidth = 16; + hfdcan1.Init.NominalTimeSeg1 = 63; + hfdcan1.Init.NominalTimeSeg2 = 16; + hfdcan1.Init.DataPrescaler = 1; + hfdcan1.Init.DataSyncJumpWidth = 4; + hfdcan1.Init.DataTimeSeg1 = 5; + hfdcan1.Init.DataTimeSeg2 = 4; + hfdcan1.Init.StdFiltersNbr = 2; + hfdcan1.Init.ExtFiltersNbr = 0; + hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; + if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN FDCAN1_Init 2 */ + + /* USER CODE END FDCAN1_Init 2 */ +} + +/** + * @brief I2C2 Initialization Function + * @param None + * @retval None + */ +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.Timing = 0x10909CEC; + hi2c2.Init.OwnAddress1 = 64; + hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c2.Init.OwnAddress2 = 0; + hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK; + hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c2) != HAL_OK) { + Error_Handler(); + } + + /** Configure Analogue filter + */ + if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK) { + Error_Handler(); + } + + /** Configure Digital filter + */ + if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN I2C2_Init 2 */ + + /* USER CODE END I2C2_Init 2 */ +} + +/** + * @brief GPIO Initialization Function + * @param None + * @retval None + */ +static void MX_GPIO_Init(void) { + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOA, + TOP_PWM_Pin | LOGO_PWM_Pin | LED_ICLK_Pin | LED_OCLK_Pin | + LED_NCLR_Pin | LED_SER_Pin | LED_NOE_Pin | + DISP_RESET_Pin, + GPIO_PIN_RESET); + + /*Configure GPIO pins : TOP_PWM_Pin LOGO_PWM_Pin LED_ICLK_Pin LED_OCLK_Pin + LED_NCLR_Pin LED_SER_Pin LED_NOE_Pin DISP_RESET_Pin + */ + GPIO_InitStruct.Pin = TOP_PWM_Pin | LOGO_PWM_Pin | LED_ICLK_Pin | + LED_OCLK_Pin | LED_NCLR_Pin | LED_SER_Pin | + LED_NOE_Pin | DISP_RESET_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /*Configure GPIO pins : BTN0_Pin BTN1_Pin BTN2_Pin BTN3_Pin */ + GPIO_InitStruct.Pin = BTN0_Pin | BTN1_Pin | BTN2_Pin | BTN3_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pins : ENC0A_Pin ENC0B_Pin ENC1A_Pin ENC1B_Pin */ + GPIO_InitStruct.Pin = ENC0A_Pin | ENC0B_Pin | ENC1A_Pin | ENC1B_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* EXTI interrupt init*/ + HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); +} + +/* USER CODE BEGIN 4 */ + +/* USER CODE END 4 */ + +/** + * @brief This function is executed in case of error occurrence. + * @retval None + */ +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 */ +} + +#ifdef USE_FULL_ASSERT +/** + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +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, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, + line) */ + /* USER CODE END 6 */ +} +#endif /* USE_FULL_ASSERT */ diff --git a/Core/Src/rpi.c b/Core/Src/rpi.c index c72926b..67ab5d4 100644 --- a/Core/Src/rpi.c +++ b/Core/Src/rpi.c @@ -1,9 +1,12 @@ #include "rpi.h" + #include "main.h" #include "state.h" + #include "stm32g4xx_hal_def.h" #include "stm32g4xx_hal_gpio.h" #include "stm32g4xx_hal_i2c.h" + #include uint8_t i2c_rx_buf[I2C_RX_BUF_SIZE]; @@ -12,10 +15,10 @@ uint8_t i2c_tx_len; RPiI2CState rpi_i2c_state; size_t rpi_num_polls; -static I2C_HandleTypeDef *bus; +static I2C_HandleTypeDef* bus; static uint8_t current_command; -void rpi_init(I2C_HandleTypeDef *handle) { +void rpi_init(I2C_HandleTypeDef* handle) { bus = handle; rpi_i2c_state = RPI_I2C_IDLE; rpi_num_polls = 0; @@ -51,7 +54,7 @@ void rpi_update_tx_buffer_ami() { i2c_tx_buf[1] = stw_state.view_state.ami.current_mission; } -void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *handle) { +void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef* handle) { switch (rpi_i2c_state) { case RPI_I2C_SENDING_DATA_SIZE: if (HAL_I2C_Slave_Seq_Transmit_IT(bus, i2c_tx_buf, i2c_tx_len, @@ -70,7 +73,7 @@ void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *handle) { } } -void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *handle) { +void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef* handle) { if (rpi_i2c_state != RPI_I2C_WAITING_FOR_COMMAND) { // TODO: How do we handle this? return; @@ -81,7 +84,7 @@ void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *handle) { rpi_i2c_state = RPI_I2C_WAITING_FOR_MASTER_RECEIVE; } -void HAL_I2C_AddrCallback(I2C_HandleTypeDef *handle, uint8_t TransferDirection, +void HAL_I2C_AddrCallback(I2C_HandleTypeDef* handle, uint8_t TransferDirection, uint16_t AddrMatchCode) { if (handle != bus) { HAL_GPIO_WritePin(LED_SER_GPIO_Port, LED_SER_Pin, GPIO_PIN_RESET); @@ -116,7 +119,7 @@ void HAL_I2C_AddrCallback(I2C_HandleTypeDef *handle, uint8_t TransferDirection, } } -void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *handle) { +void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef* handle) { if (handle != bus) { return; } @@ -124,4 +127,4 @@ void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *handle) { if (rpi_i2c_state == RPI_I2C_IDLE) { HAL_I2C_EnableListen_IT(bus); } -} \ No newline at end of file +} diff --git a/Core/Src/state.c b/Core/Src/state.c index 06ca1f9..4eae321 100644 --- a/Core/Src/state.c +++ b/Core/Src/state.c @@ -5,4 +5,4 @@ STWState stw_state; void state_init() { stw_state.view = VIEW_MISSION_SELECT; stw_state.view_state.mission_select.selection = MISSION_ACCELERATION; -} \ No newline at end of file +} diff --git a/Core/Src/user_inputs.c b/Core/Src/user_inputs.c index f5d1887..85f96ee 100644 --- a/Core/Src/user_inputs.c +++ b/Core/Src/user_inputs.c @@ -3,9 +3,10 @@ #include "events.h" #include "main.h" #include "state.h" -#include "stm32g4xx_hal_gpio.h" #include "vehicle.h" +#include "stm32g4xx_hal_gpio.h" + void HAL_GPIO_EXTI_Callback(uint16_t pin) { Event ev; @@ -35,7 +36,7 @@ void HAL_GPIO_EXTI_Callback(uint16_t pin) { event_push(&ev); } -void handle_button_press(const ButtonPressEvent *ev) { +void handle_button_press(const ButtonPressEvent* ev) { switch (stw_state.view) { case VIEW_MISSION_SELECT: handle_button_press_mission_select(ev); @@ -47,10 +48,10 @@ void handle_button_press(const ButtonPressEvent *ev) { } } -void handle_button_press_mission_select(const ButtonPressEvent *ev) { +void handle_button_press_mission_select(const ButtonPressEvent* ev) { switch (*ev) { case BTN_PRESS_PREV: { - Mission *selection = &stw_state.view_state.mission_select.selection; + Mission* selection = &stw_state.view_state.mission_select.selection; if (*selection == MISSION_ACCELERATION || *selection == MISSION_NONE) { *selection = MISSION_MANUAL; } else { @@ -64,7 +65,7 @@ void handle_button_press_mission_select(const ButtonPressEvent *ev) { vehicle_send_mission_select(stw_state.view_state.mission_select.selection); break; case BTN_PRESS_NEXT: { - Mission *selection = &stw_state.view_state.mission_select.selection; + Mission* selection = &stw_state.view_state.mission_select.selection; if (*selection == MISSION_MANUAL) { *selection = MISSION_ACCELERATION; } else { @@ -73,4 +74,4 @@ void handle_button_press_mission_select(const ButtonPressEvent *ev) { break; } } -} \ No newline at end of file +} diff --git a/Core/Src/vehicle.c b/Core/Src/vehicle.c index 95235de..41c632d 100644 --- a/Core/Src/vehicle.c +++ b/Core/Src/vehicle.c @@ -1,14 +1,16 @@ #include "vehicle.h" + #include "main.h" #include "state.h" + #include "stm32g4xx_hal_def.h" #include "stm32g4xx_hal_fdcan.h" -FDCAN_HandleTypeDef *can; +FDCAN_HandleTypeDef* can; -void handle_as_mission_fb(FDCAN_RxHeaderTypeDef *header, uint8_t *data); +void handle_as_mission_fb(FDCAN_RxHeaderTypeDef* header, uint8_t* data); -void vehicle_init(FDCAN_HandleTypeDef *handle) { +void vehicle_init(FDCAN_HandleTypeDef* handle) { can = handle; // RX Filter @@ -67,7 +69,7 @@ void vehicle_send_mission_select(Mission mission) { /* BEGIN CAN MESSAGE HANDLERS { */ -void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle, +void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef* handle, uint32_t interrupt_flags) { if (!(interrupt_flags & FDCAN_IT_RX_FIFO0_NEW_MESSAGE)) { return; @@ -91,7 +93,7 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle, } } -void handle_as_mission_fb(FDCAN_RxHeaderTypeDef *header, uint8_t *data) { +void handle_as_mission_fb(FDCAN_RxHeaderTypeDef* header, uint8_t* data) { Mission mission_fb = data[0]; if (mission_fb == MISSION_NONE) { return; @@ -102,4 +104,4 @@ void handle_as_mission_fb(FDCAN_RxHeaderTypeDef *header, uint8_t *data) { stw_state.view = VIEW_AMI; } -/* } END CAN MESSAGE HANDLERS */ \ No newline at end of file +/* } END CAN MESSAGE HANDLERS */