steering-wheel-stm/Core/Src/main.c

392 lines
11 KiB
C
Raw Normal View History

2022-05-27 00:11:56 +02:00
/* 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 */
2022-05-28 02:00:50 +02:00
#include "events.h"
2022-05-27 00:11:56 +02:00
#include "rpi.h"
2022-05-27 17:20:16 +02:00
#include "state.h"
#include "stm32g4xx_hal.h"
2022-05-28 02:00:50 +02:00
#include "stm32g4xx_hal_gpio.h"
2022-05-27 17:20:16 +02:00
#include <stdint.h>
2022-05-27 00:11:56 +02:00
/* 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 ---------------------------------------------------------*/
2022-05-27 17:20:16 +02:00
ADC_HandleTypeDef hadc1;
2022-05-27 00:11:56 +02:00
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 */
/**
2022-05-27 17:20:16 +02:00
* @brief The application entry point.
* @retval int
*/
int main(void) {
2022-05-27 00:11:56 +02:00
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
2022-05-27 17:20:16 +02:00
/* Reset of all peripherals, Initializes the Flash interface and the Systick.
*/
2022-05-27 00:11:56 +02:00
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 */
2022-05-27 17:20:16 +02:00
state_init();
2022-05-27 00:11:56 +02:00
rpi_init(&hi2c2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
2022-05-27 17:20:16 +02:00
uint32_t rpi_active_since = 0;
2022-05-27 00:11:56 +02:00
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
2022-05-27 17:20:16 +02:00
uint32_t now = HAL_GetTick();
if (now < 1000) {
2022-05-27 00:11:56 +02:00
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);
}
2022-05-28 02:00:50 +02:00
handle_events();
2022-05-27 00:11:56 +02:00
}
/* USER CODE END 3 */
}
/**
2022-05-27 17:20:16 +02:00
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void) {
2022-05-27 00:11:56 +02:00
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
2022-05-27 17:20:16 +02:00
*/
2022-05-27 00:11:56 +02:00
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
2022-05-27 17:20:16 +02:00
* in the RCC_OscInitTypeDef structure.
*/
2022-05-27 00:11:56 +02:00
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
2022-05-27 17:20:16 +02:00
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
2022-05-27 00:11:56 +02:00
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
2022-05-27 17:20:16 +02:00
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
2022-05-27 00:11:56 +02:00
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
2022-05-27 17:20:16 +02:00
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
2022-05-27 00:11:56 +02:00
Error_Handler();
}
}
/**
2022-05-27 17:20:16 +02:00
* @brief ADC1 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC1_Init(void) {
2022-05-27 00:11:56 +02:00
/* 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
2022-05-27 17:20:16 +02:00
*/
2022-05-27 00:11:56 +02:00
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;
2022-05-27 17:20:16 +02:00
if (HAL_ADC_Init(&hadc1) != HAL_OK) {
2022-05-27 00:11:56 +02:00
Error_Handler();
}
/** Configure the ADC multi-mode
2022-05-27 17:20:16 +02:00
*/
2022-05-27 00:11:56 +02:00
multimode.Mode = ADC_MODE_INDEPENDENT;
2022-05-27 17:20:16 +02:00
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) {
2022-05-27 00:11:56 +02:00
Error_Handler();
}
/** Configure Regular Channel
2022-05-27 17:20:16 +02:00
*/
2022-05-27 00:11:56 +02:00
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;
2022-05-27 17:20:16 +02:00
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
2022-05-27 00:11:56 +02:00
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
/**
2022-05-27 17:20:16 +02:00
* @brief FDCAN1 Initialization Function
* @param None
* @retval None
*/
static void MX_FDCAN1_Init(void) {
2022-05-27 00:11:56 +02:00
/* 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_CLASSIC;
hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
hfdcan1.Init.AutoRetransmission = DISABLE;
hfdcan1.Init.TransmitPause = DISABLE;
hfdcan1.Init.ProtocolException = DISABLE;
hfdcan1.Init.NominalPrescaler = 1;
hfdcan1.Init.NominalSyncJumpWidth = 1;
hfdcan1.Init.NominalTimeSeg1 = 2;
hfdcan1.Init.NominalTimeSeg2 = 2;
hfdcan1.Init.DataPrescaler = 1;
hfdcan1.Init.DataSyncJumpWidth = 1;
hfdcan1.Init.DataTimeSeg1 = 1;
hfdcan1.Init.DataTimeSeg2 = 1;
hfdcan1.Init.StdFiltersNbr = 0;
hfdcan1.Init.ExtFiltersNbr = 0;
hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
2022-05-27 17:20:16 +02:00
if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) {
2022-05-27 00:11:56 +02:00
Error_Handler();
}
/* USER CODE BEGIN FDCAN1_Init 2 */
/* USER CODE END FDCAN1_Init 2 */
}
/**
2022-05-27 17:20:16 +02:00
* @brief I2C2 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C2_Init(void) {
2022-05-27 00:11:56 +02:00
/* 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 = 0x00303D5B;
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;
2022-05-27 17:20:16 +02:00
if (HAL_I2C_Init(&hi2c2) != HAL_OK) {
2022-05-27 00:11:56 +02:00
Error_Handler();
}
/** Configure Analogue filter
2022-05-27 17:20:16 +02:00
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK) {
2022-05-27 00:11:56 +02:00
Error_Handler();
}
/** Configure Digital filter
2022-05-27 17:20:16 +02:00
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK) {
2022-05-27 00:11:56 +02:00
Error_Handler();
}
/* USER CODE BEGIN I2C2_Init 2 */
/* USER CODE END I2C2_Init 2 */
}
/**
2022-05-27 17:20:16 +02:00
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void) {
2022-05-27 00:11:56 +02:00
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
2022-05-27 17:20:16 +02:00
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);
2022-05-27 00:11:56 +02:00
/*Configure GPIO pins : TOP_PWM_Pin LOGO_PWM_Pin LED_ICLK_Pin LED_OCLK_Pin
2022-05-27 17:20:16 +02:00
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;
2022-05-27 00:11:56 +02:00
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);
2022-05-28 02:00:50 +02:00
/*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;
2022-05-27 00:11:56 +02:00
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
2022-05-28 02:00:50 +02:00
GPIO_InitStruct.Pull = GPIO_PULLUP;
2022-05-27 00:11:56 +02:00
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
2022-05-28 02:00:50 +02:00
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
2022-05-27 00:11:56 +02:00
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
2022-05-27 17:20:16 +02:00
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void) {
2022-05-27 00:11:56 +02:00
/* 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 */
}
2022-05-27 17:20:16 +02:00
#ifdef USE_FULL_ASSERT
2022-05-27 00:11:56 +02:00
/**
2022-05-27 17:20:16 +02:00
* @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) {
2022-05-27 00:11:56 +02:00
/* 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 */