755 lines
22 KiB
C
755 lines
22 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2023 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 */
|
|
|
|
/* 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 ---------------------------------------------------------*/
|
|
|
|
CAN_HandleTypeDef hcan2;
|
|
|
|
/* 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_ADC2_Init(void);
|
|
static void MX_ADC3_Init(void);
|
|
static void MX_SPI1_Init(void);
|
|
static void MX_TIM1_Init(void);
|
|
static void MX_USART1_UART_Init(void);
|
|
static void MX_USART2_UART_Init(void);
|
|
static void MX_CAN2_Init(void);
|
|
static void MX_NVIC_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_ADC2_Init();
|
|
MX_ADC3_Init();
|
|
MX_SPI1_Init();
|
|
MX_TIM1_Init();
|
|
MX_USART1_UART_Init();
|
|
MX_USART2_UART_Init();
|
|
MX_CAN2_Init();
|
|
|
|
/* Initialize interrupts */
|
|
MX_NVIC_Init();
|
|
/* USER CODE BEGIN 2 */
|
|
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET);
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1)
|
|
{
|
|
/* USER CODE END WHILE */
|
|
if (HAL_GetTick()%500==499)
|
|
{
|
|
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
|
|
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
|
|
}
|
|
/* USER CODE BEGIN 3 */
|
|
}
|
|
/* 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_RCC_PWR_CLK_ENABLE();
|
|
__HAL_PWR_VOLTAGESCALING_CONFIG(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 = 8;
|
|
RCC_OscInitStruct.PLL.PLLN = 180;
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLQ = 2;
|
|
RCC_OscInitStruct.PLL.PLLR = 2;
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Activate the Over-Drive mode
|
|
*/
|
|
if (HAL_PWREx_EnableOverDrive() != 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_DIV4;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief NVIC Configuration.
|
|
* @retval None
|
|
*/
|
|
static void MX_NVIC_Init(void)
|
|
{
|
|
/* TIM1_BRK_TIM9_IRQn interrupt configuration */
|
|
NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),1, 0));
|
|
NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
|
|
/* TIM1_UP_TIM10_IRQn interrupt configuration */
|
|
NVIC_SetPriority(TIM1_UP_TIM10_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),2, 0));
|
|
NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
|
|
/* CAN2_RX0_IRQn interrupt configuration */
|
|
HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 0, 0);
|
|
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
|
|
/* CAN2_RX1_IRQn interrupt configuration */
|
|
HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 0, 0);
|
|
HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);
|
|
}
|
|
|
|
/**
|
|
* @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 */
|
|
|
|
LL_ADC_InitTypeDef ADC_InitStruct = {0};
|
|
LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
|
|
LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Peripheral clock enable */
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
|
|
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
|
/**ADC1 GPIO Configuration
|
|
PA4 ------> ADC1_IN4
|
|
PA5 ------> ADC1_IN5
|
|
PA6 ------> ADC1_IN6
|
|
PB0 ------> ADC1_IN8
|
|
PB1 ------> ADC1_IN9
|
|
*/
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_4|LL_GPIO_PIN_5|LL_GPIO_PIN_6;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_1;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN ADC1_Init 1 */
|
|
|
|
/* USER CODE END ADC1_Init 1 */
|
|
|
|
/** Common config
|
|
*/
|
|
ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;
|
|
ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
|
|
ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_DISABLE;
|
|
LL_ADC_Init(ADC1, &ADC_InitStruct);
|
|
ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
|
|
ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
|
|
ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
|
|
ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
|
|
ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
|
|
LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
|
|
LL_ADC_REG_SetFlagEndOfConversion(ADC1, LL_ADC_REG_FLAG_EOC_UNITARY_CONV);
|
|
ADC_CommonInitStruct.CommonClock = LL_ADC_CLOCK_SYNC_PCLK_DIV4;
|
|
ADC_CommonInitStruct.Multimode = LL_ADC_MULTI_INDEPENDENT;
|
|
LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);
|
|
|
|
/** Configure Regular Channel
|
|
*/
|
|
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_4);
|
|
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SAMPLINGTIME_3CYCLES);
|
|
/* USER CODE BEGIN ADC1_Init 2 */
|
|
|
|
/* USER CODE END ADC1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief ADC2 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_ADC2_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN ADC2_Init 0 */
|
|
|
|
/* USER CODE END ADC2_Init 0 */
|
|
|
|
LL_ADC_InitTypeDef ADC_InitStruct = {0};
|
|
LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Peripheral clock enable */
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC2);
|
|
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
|
|
/**ADC2 GPIO Configuration
|
|
PC3 ------> ADC2_IN13
|
|
PC4 ------> ADC2_IN14
|
|
PC5 ------> ADC2_IN15
|
|
*/
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_3|LL_GPIO_PIN_4|LL_GPIO_PIN_5;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN ADC2_Init 1 */
|
|
|
|
/* USER CODE END ADC2_Init 1 */
|
|
|
|
/** Common config
|
|
*/
|
|
ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;
|
|
ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
|
|
ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_DISABLE;
|
|
LL_ADC_Init(ADC2, &ADC_InitStruct);
|
|
ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
|
|
ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
|
|
ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
|
|
ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
|
|
ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
|
|
LL_ADC_REG_Init(ADC2, &ADC_REG_InitStruct);
|
|
LL_ADC_REG_SetFlagEndOfConversion(ADC2, LL_ADC_REG_FLAG_EOC_UNITARY_CONV);
|
|
|
|
/** Configure Regular Channel
|
|
*/
|
|
LL_ADC_REG_SetSequencerRanks(ADC2, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_15);
|
|
LL_ADC_SetChannelSamplingTime(ADC2, LL_ADC_CHANNEL_15, LL_ADC_SAMPLINGTIME_3CYCLES);
|
|
/* USER CODE BEGIN ADC2_Init 2 */
|
|
|
|
/* USER CODE END ADC2_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief ADC3 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_ADC3_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN ADC3_Init 0 */
|
|
|
|
/* USER CODE END ADC3_Init 0 */
|
|
|
|
LL_ADC_InitTypeDef ADC_InitStruct = {0};
|
|
LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Peripheral clock enable */
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC3);
|
|
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
|
|
/**ADC3 GPIO Configuration
|
|
PC0 ------> ADC3_IN10
|
|
PC1 ------> ADC3_IN11
|
|
*/
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_1;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN ADC3_Init 1 */
|
|
|
|
/* USER CODE END ADC3_Init 1 */
|
|
|
|
/** Common config
|
|
*/
|
|
ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;
|
|
ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
|
|
ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_DISABLE;
|
|
LL_ADC_Init(ADC3, &ADC_InitStruct);
|
|
ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
|
|
ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_DISABLE;
|
|
ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
|
|
ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
|
|
ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
|
|
LL_ADC_REG_Init(ADC3, &ADC_REG_InitStruct);
|
|
LL_ADC_REG_SetFlagEndOfConversion(ADC3, LL_ADC_REG_FLAG_EOC_UNITARY_CONV);
|
|
|
|
/** Configure Regular Channel
|
|
*/
|
|
LL_ADC_REG_SetSequencerRanks(ADC3, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_10);
|
|
LL_ADC_SetChannelSamplingTime(ADC3, LL_ADC_CHANNEL_10, LL_ADC_SAMPLINGTIME_3CYCLES);
|
|
/* USER CODE BEGIN ADC3_Init 2 */
|
|
|
|
/* USER CODE END ADC3_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief CAN2 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_CAN2_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN CAN2_Init 0 */
|
|
|
|
/* USER CODE END CAN2_Init 0 */
|
|
|
|
/* USER CODE BEGIN CAN2_Init 1 */
|
|
|
|
/* USER CODE END CAN2_Init 1 */
|
|
hcan2.Instance = CAN2;
|
|
hcan2.Init.Prescaler = 16;
|
|
hcan2.Init.Mode = CAN_MODE_NORMAL;
|
|
hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
|
|
hcan2.Init.TimeSeg1 = CAN_BS1_1TQ;
|
|
hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;
|
|
hcan2.Init.TimeTriggeredMode = DISABLE;
|
|
hcan2.Init.AutoBusOff = DISABLE;
|
|
hcan2.Init.AutoWakeUp = DISABLE;
|
|
hcan2.Init.AutoRetransmission = DISABLE;
|
|
hcan2.Init.ReceiveFifoLocked = DISABLE;
|
|
hcan2.Init.TransmitFifoPriority = DISABLE;
|
|
if (HAL_CAN_Init(&hcan2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN CAN2_Init 2 */
|
|
|
|
/* USER CODE END CAN2_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief SPI1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_SPI1_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN SPI1_Init 0 */
|
|
|
|
/* USER CODE END SPI1_Init 0 */
|
|
|
|
LL_SPI_InitTypeDef SPI_InitStruct = {0};
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Peripheral clock enable */
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
|
|
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
|
/**SPI1 GPIO Configuration
|
|
PA7 ------> SPI1_MOSI
|
|
PB3 ------> SPI1_SCK
|
|
PB4 ------> SPI1_MISO
|
|
*/
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_3|LL_GPIO_PIN_4;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
|
|
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN SPI1_Init 1 */
|
|
|
|
/* USER CODE END SPI1_Init 1 */
|
|
/* SPI1 parameter configuration*/
|
|
SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
|
|
SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
|
|
SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
|
|
SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
|
|
SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
|
|
SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
|
|
SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2;
|
|
SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
|
|
SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
|
|
SPI_InitStruct.CRCPoly = 10;
|
|
LL_SPI_Init(SPI1, &SPI_InitStruct);
|
|
LL_SPI_SetStandard(SPI1, LL_SPI_PROTOCOL_MOTOROLA);
|
|
/* USER CODE BEGIN SPI1_Init 2 */
|
|
|
|
/* USER CODE END SPI1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief TIM1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_TIM1_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN TIM1_Init 0 */
|
|
|
|
/* USER CODE END TIM1_Init 0 */
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
|
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
|
|
LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0};
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Peripheral clock enable */
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
|
|
|
/* USER CODE BEGIN TIM1_Init 1 */
|
|
|
|
/* USER CODE END TIM1_Init 1 */
|
|
TIM_InitStruct.Prescaler = 0;
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
TIM_InitStruct.Autoreload = 65535;
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
TIM_InitStruct.RepetitionCounter = 0;
|
|
LL_TIM_Init(TIM1, &TIM_InitStruct);
|
|
LL_TIM_DisableARRPreload(TIM1);
|
|
LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1);
|
|
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
|
|
TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
|
|
TIM_OC_InitStruct.CompareValue = 0;
|
|
TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
|
|
TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
|
|
TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
|
|
TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
|
|
LL_TIM_OC_Init(TIM1, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
|
|
LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
|
|
LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH2);
|
|
LL_TIM_OC_Init(TIM1, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct);
|
|
LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH2);
|
|
LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH3);
|
|
LL_TIM_OC_Init(TIM1, LL_TIM_CHANNEL_CH3, &TIM_OC_InitStruct);
|
|
LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH3);
|
|
LL_TIM_SetTriggerOutput(TIM1, LL_TIM_TRGO_RESET);
|
|
LL_TIM_DisableMasterSlaveMode(TIM1);
|
|
TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
|
|
TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;
|
|
TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
|
|
TIM_BDTRInitStruct.DeadTime = 0;
|
|
TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
|
|
TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
|
|
TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
|
|
LL_TIM_BDTR_Init(TIM1, &TIM_BDTRInitStruct);
|
|
/* USER CODE BEGIN TIM1_Init 2 */
|
|
|
|
/* USER CODE END TIM1_Init 2 */
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
|
|
/**TIM1 GPIO Configuration
|
|
PA8 ------> TIM1_CH1
|
|
PA9 ------> TIM1_CH2
|
|
PA10 ------> TIM1_CH3
|
|
*/
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_8|LL_GPIO_PIN_9|LL_GPIO_PIN_10;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief USART1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_USART1_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART1_Init 0 */
|
|
|
|
/* USER CODE END USART1_Init 0 */
|
|
|
|
LL_USART_InitTypeDef USART_InitStruct = {0};
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Peripheral clock enable */
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
|
|
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
|
/**USART1 GPIO Configuration
|
|
PB6 ------> USART1_TX
|
|
PB7 ------> USART1_RX
|
|
*/
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
|
|
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN USART1_Init 1 */
|
|
|
|
/* USER CODE END USART1_Init 1 */
|
|
USART_InitStruct.BaudRate = 115200;
|
|
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
|
|
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
|
|
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
|
|
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
|
|
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
|
|
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
|
|
LL_USART_Init(USART1, &USART_InitStruct);
|
|
LL_USART_ConfigAsyncMode(USART1);
|
|
LL_USART_Enable(USART1);
|
|
/* USER CODE BEGIN USART1_Init 2 */
|
|
|
|
/* USER CODE END USART1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief USART2 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_USART2_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART2_Init 0 */
|
|
|
|
/* USER CODE END USART2_Init 0 */
|
|
|
|
LL_USART_InitTypeDef USART_InitStruct = {0};
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Peripheral clock enable */
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
|
|
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
|
|
/**USART2 GPIO Configuration
|
|
PA0-WKUP ------> USART2_CTS
|
|
PA1 ------> USART2_RTS
|
|
PA2 ------> USART2_TX
|
|
PA3 ------> USART2_RX
|
|
*/
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_1|LL_GPIO_PIN_2|LL_GPIO_PIN_3;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN USART2_Init 1 */
|
|
|
|
/* USER CODE END USART2_Init 1 */
|
|
USART_InitStruct.BaudRate = 115200;
|
|
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
|
|
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
|
|
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
|
|
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
|
|
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_RTS_CTS;
|
|
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
|
|
LL_USART_Init(USART2, &USART_InitStruct);
|
|
LL_USART_ConfigAsyncMode(USART2);
|
|
LL_USART_Enable(USART2);
|
|
/* USER CODE BEGIN USART2_Init 2 */
|
|
|
|
/* USER CODE END USART2_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_GPIOH_CLK_ENABLE();
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(SD1_GPIO_Port, SD1_Pin, GPIO_PIN_RESET);
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(GPIOC, LED1_Pin|LED2_Pin|LED3_Pin, GPIO_PIN_RESET);
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(spi1_cs_GPIO_Port, spi1_cs_Pin, GPIO_PIN_RESET);
|
|
|
|
/*Configure GPIO pin : SD1_Pin */
|
|
GPIO_InitStruct.Pin = SD1_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(SD1_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : LED1_Pin LED2_Pin LED3_Pin */
|
|
GPIO_InitStruct.Pin = LED1_Pin|LED2_Pin|LED3_Pin;
|
|
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 pin : spi1_cs_Pin */
|
|
GPIO_InitStruct.Pin = spi1_cs_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(spi1_cs_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
}
|
|
|
|
/* 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 */
|