Project changes
1/ Added Spi that used to read data from AS5045b position sensor 2/ Added Base FOC code to project 3/ Added Standart DSP lib with optimazes sincos cals Need for FOC
This commit is contained in:
parent
ee1319b10e
commit
526b689693
43 changed files with 10985 additions and 410 deletions
107
Src/adc.c
107
Src/adc.c
|
@ -110,113 +110,6 @@ void MX_ADC1_Init(void)
|
|||
|
||||
/* USER CODE END ADC1_Init 2 */
|
||||
|
||||
}
|
||||
/* ADC2 init function */
|
||||
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 */
|
||||
|
||||
}
|
||||
/* ADC3 init function */
|
||||
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 */
|
||||
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
|
35
Src/gpio.c
35
Src/gpio.c
|
@ -52,21 +52,40 @@ void MX_GPIO_Init(void)
|
|||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOD);
|
||||
|
||||
/**/
|
||||
LL_GPIO_ResetOutputPin(SD1_GPIO_Port, SD1_Pin);
|
||||
LL_GPIO_SetOutputPin(AS5045_CS_GPIO_Port, AS5045_CS_Pin);
|
||||
|
||||
/**/
|
||||
LL_GPIO_ResetOutputPin(GPIOC, LED1_Pin|LED2_Pin|LED3_Pin);
|
||||
LL_GPIO_ResetOutputPin(GPIOC, EN_W_Pin|LED1_Pin|LED2_Pin|LED3_Pin);
|
||||
|
||||
/**/
|
||||
LL_GPIO_ResetOutputPin(GPIOA, EN_U_Pin|EN_V_Pin);
|
||||
|
||||
/**/
|
||||
LL_GPIO_ResetOutputPin(spi1_cs_GPIO_Port, spi1_cs_Pin);
|
||||
|
||||
/**/
|
||||
GPIO_InitStruct.Pin = SD1_Pin;
|
||||
GPIO_InitStruct.Pin = AS5045_CS_Pin;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(SD1_GPIO_Port, &GPIO_InitStruct);
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
||||
LL_GPIO_Init(AS5045_CS_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/**/
|
||||
GPIO_InitStruct.Pin = EN_W_Pin;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
|
||||
LL_GPIO_Init(EN_W_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/**/
|
||||
GPIO_InitStruct.Pin = EN_U_Pin|EN_V_Pin;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/**/
|
||||
GPIO_InitStruct.Pin = LED1_Pin|LED2_Pin|LED3_Pin;
|
||||
|
@ -79,9 +98,9 @@ void MX_GPIO_Init(void)
|
|||
/**/
|
||||
GPIO_InitStruct.Pin = spi1_cs_Pin;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||||
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.Pull = LL_GPIO_PULL_DOWN;
|
||||
LL_GPIO_Init(spi1_cs_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
}
|
||||
|
|
308
Src/main.c
308
Src/main.c
|
@ -1,20 +1,20 @@
|
|||
/* 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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
******************************************************************************
|
||||
* @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"
|
||||
|
@ -27,7 +27,7 @@
|
|||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "InitDrive.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
|
@ -47,7 +47,12 @@
|
|||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
volatile uint16_t asSpiWordLow = 0;
|
||||
volatile uint16_t asSpiWordHigh = 0;
|
||||
volatile uint32_t asResultWord = 0;
|
||||
typedef struct AS5045_data {
|
||||
|
||||
};
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
|
@ -64,144 +69,169 @@ static void MX_NVIC_Init(void);
|
|||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* USER CODE BEGIN 1 */
|
||||
__disable_irq();
|
||||
/* USER CODE END 1 */
|
||||
* @brief The application entry point.
|
||||
* @retval int
|
||||
*/
|
||||
int main(void) {
|
||||
/* USER CODE BEGIN 1 */
|
||||
__disable_irq();
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
HAL_Init();
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
/* USER CODE BEGIN Init */
|
||||
SysTick_Config(0xFFFFFF);
|
||||
LL_DBGMCU_APB1_GRP1_FreezePeriph( LL_DBGMCU_APB1_GRP1_TIM3_STOP);
|
||||
LL_DBGMCU_APB1_GRP1_FreezePeriph( LL_DBGMCU_APB1_GRP1_TIM2_STOP);
|
||||
LL_DBGMCU_APB2_GRP1_FreezePeriph( LL_DBGMCU_APB2_GRP1_TIM1_STOP);
|
||||
LL_DBGMCU_APB2_GRP1_FreezePeriph( LL_DBGMCU_APB2_GRP1_TIM8_STOP);
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
/* USER CODE BEGIN SysInit */
|
||||
SystemCoreClockUpdate();
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
SystemCoreClockUpdate();
|
||||
/* USER CODE END SysInit */
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_ADC1_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_USART1_UART_Init();
|
||||
MX_USART2_UART_Init();
|
||||
MX_CAN2_Init();
|
||||
MX_TIM2_Init();
|
||||
MX_SPI2_Init();
|
||||
|
||||
/* 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();
|
||||
MX_TIM2_Init();
|
||||
/* Initialize interrupts */
|
||||
MX_NVIC_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* Initialize interrupts */
|
||||
MX_NVIC_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
LL_GPIO_ResetOutputPin(LED1_GPIO_Port, LED1_Pin);
|
||||
LL_GPIO_ResetOutputPin(LED2_GPIO_Port, LED2_Pin);
|
||||
LL_GPIO_ResetOutputPin(LED1_GPIO_Port, LED1_Pin);
|
||||
LL_GPIO_ResetOutputPin(LED2_GPIO_Port, LED2_Pin);
|
||||
|
||||
LL_ADC_EnableIT_JEOS(ADC1);
|
||||
LL_ADC_Enable(ADC1);
|
||||
LL_ADC_EnableIT_JEOS(ADC1);
|
||||
LL_ADC_Enable(ADC1);
|
||||
|
||||
LL_TIM_SetAutoReload(TIM1, SystemCoreClock/PWM_FRQ_HZ);
|
||||
LL_TIM_OC_SetCompareCH4(TIM1, (SystemCoreClock/PWM_FRQ_HZ)-1);
|
||||
LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH4);
|
||||
/*Used center alogned mode so set ARR to Half of Calculated period*/
|
||||
uint32_t TIM_ARR_value = (SystemCoreClock / PWM_FRQ_HZ) >> 1;
|
||||
LL_TIM_SetAutoReload(TIM1, TIM_ARR_value - 1);
|
||||
LL_TIM_OC_SetCompareCH4(TIM1, LL_TIM_GetAutoReload(TIM1) - 1);
|
||||
LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH4);
|
||||
|
||||
LL_TIM_EnableIT_CC4(TIM1);
|
||||
LL_TIM_EnableCounter(TIM1);
|
||||
/*TIM2 on Half clock bus so divide SystemClock by 2 */
|
||||
LL_TIM_SetAutoReload(TIM2, ((SystemCoreClock/ALG_TIMER_HZ)>>1)-1);
|
||||
LL_TIM_EnableIT_UPDATE(TIM2);
|
||||
LL_TIM_EnableCounter(TIM2);
|
||||
__enable_irq();
|
||||
/* USER CODE END 2 */
|
||||
LL_TIM_EnableIT_CC4(TIM1);
|
||||
LL_TIM_EnableCounter(TIM1);
|
||||
/*TIM2 on Half clock bus so divide SystemClock by 2 */
|
||||
LL_TIM_SetAutoReload(TIM2, ((SystemCoreClock / ALG_TIMER_HZ) >> 1) - 1);
|
||||
LL_TIM_EnableIT_UPDATE(TIM2);
|
||||
LL_TIM_EnableCounter(TIM2);
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
LL_GPIO_SetOutputPin(AS5045_CS_GPIO_Port, AS5045_CS_Pin);
|
||||
LL_SPI_SetBaudRatePrescaler(SPI2,LL_SPI_BAUDRATEPRESCALER_DIV64 );
|
||||
LL_SPI_Enable(SPI2);
|
||||
/*Init Control structure*/
|
||||
float TSAMPLE_FAST = 1.f / PWM_FRQ_HZ;
|
||||
float TSAMPLE_SLOW = 1.f / ALG_TIMER_HZ;
|
||||
DriveInit(TSAMPLE_FAST, TSAMPLE_SLOW);
|
||||
__enable_irq();
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1) {
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
if (!LL_SPI_IsActiveFlag_OVR(SPI2)) {
|
||||
LL_GPIO_ResetOutputPin(AS5045_CS_GPIO_Port, AS5045_CS_Pin);
|
||||
while (!LL_SPI_IsActiveFlag_TXE(SPI2)){};
|
||||
LL_SPI_TransmitData16(SPI2, 0xFFFF);
|
||||
while (!LL_SPI_IsActiveFlag_RXNE(SPI2)) {
|
||||
}
|
||||
uint16_t buffer = LL_SPI_ReceiveData16(SPI2);
|
||||
asSpiWordLow = (buffer >> 4) & 0xFFF;
|
||||
LL_GPIO_SetOutputPin(AS5045_CS_GPIO_Port, AS5045_CS_Pin);
|
||||
|
||||
} else {
|
||||
LL_SPI_ClearFlag_OVR(SPI2);
|
||||
}
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
LL_FLASH_SetLatency(LL_FLASH_LATENCY_5);
|
||||
while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_5)
|
||||
{
|
||||
}
|
||||
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
|
||||
LL_PWR_EnableOverDriveMode();
|
||||
LL_RCC_HSE_Enable();
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void) {
|
||||
LL_FLASH_SetLatency(LL_FLASH_LATENCY_5);
|
||||
while (LL_FLASH_GetLatency() != LL_FLASH_LATENCY_5) {
|
||||
}
|
||||
LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1);
|
||||
LL_PWR_EnableOverDriveMode();
|
||||
LL_RCC_HSE_Enable();
|
||||
|
||||
/* Wait till HSE is ready */
|
||||
while(LL_RCC_HSE_IsReady() != 1)
|
||||
{
|
||||
/* Wait till HSE is ready */
|
||||
while (LL_RCC_HSE_IsReady() != 1) {
|
||||
|
||||
}
|
||||
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_4, 180, LL_RCC_PLLP_DIV_2);
|
||||
LL_RCC_PLL_Enable();
|
||||
}
|
||||
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_4, 180,
|
||||
LL_RCC_PLLP_DIV_2);
|
||||
LL_RCC_PLL_Enable();
|
||||
|
||||
/* Wait till PLL is ready */
|
||||
while(LL_RCC_PLL_IsReady() != 1)
|
||||
{
|
||||
/* Wait till PLL is ready */
|
||||
while (LL_RCC_PLL_IsReady() != 1) {
|
||||
|
||||
}
|
||||
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
|
||||
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_4);
|
||||
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_2);
|
||||
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
|
||||
}
|
||||
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
|
||||
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_4);
|
||||
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_2);
|
||||
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
|
||||
|
||||
/* Wait till System clock is ready */
|
||||
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
|
||||
{
|
||||
/* Wait till System clock is ready */
|
||||
while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
|
||||
|
||||
}
|
||||
LL_SetSystemCoreClock(180000000);
|
||||
}
|
||||
LL_SetSystemCoreClock(180000000);
|
||||
|
||||
/* Update the time base */
|
||||
if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
LL_RCC_SetTIMPrescaler(LL_RCC_TIM_PRESCALER_TWICE);
|
||||
/* Update the time base */
|
||||
if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
LL_RCC_SetTIMPrescaler(LL_RCC_TIM_PRESCALER_TWICE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
/* 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);
|
||||
/* ADC_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(ADC_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
NVIC_EnableIRQ(ADC_IRQn);
|
||||
/* TIM1_CC_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(TIM1_CC_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
NVIC_EnableIRQ(TIM1_CC_IRQn);
|
||||
* @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_CC_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(TIM1_CC_IRQn,
|
||||
NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 3, 0));
|
||||
NVIC_EnableIRQ(TIM1_CC_IRQn);
|
||||
/* TIM2_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(TIM2_IRQn,
|
||||
NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 4, 0));
|
||||
NVIC_EnableIRQ(TIM2_IRQn);
|
||||
/* ADC_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(ADC_IRQn,
|
||||
NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 2, 0));
|
||||
NVIC_EnableIRQ(ADC_IRQn);
|
||||
/* CAN2_RX0_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 5, 0);
|
||||
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
|
||||
/* CAN2_RX1_IRQn interrupt configuration */
|
||||
HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 5, 0);
|
||||
HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
@ -209,18 +239,16 @@ static void MX_NVIC_Init(void)
|
|||
/* 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 */
|
||||
* @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
|
||||
|
|
46
Src/spi.c
46
Src/spi.c
|
@ -24,37 +24,37 @@
|
|||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/* SPI1 init function */
|
||||
void MX_SPI1_Init(void)
|
||||
/* SPI2 init function */
|
||||
void MX_SPI2_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN SPI1_Init 0 */
|
||||
/* USER CODE BEGIN SPI2_Init 0 */
|
||||
|
||||
/* USER CODE END SPI1_Init 0 */
|
||||
/* USER CODE END SPI2_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_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
|
||||
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
|
||||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
||||
/**SPI1 GPIO Configuration
|
||||
PA7 ------> SPI1_MOSI
|
||||
PB3 ------> SPI1_SCK
|
||||
PB4 ------> SPI1_MISO
|
||||
/**SPI2 GPIO Configuration
|
||||
PC1 ------> SPI2_MOSI
|
||||
PB10 ------> SPI2_SCK
|
||||
PB14 ------> SPI2_MISO
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
|
||||
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.Alternate = LL_GPIO_AF_7;
|
||||
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_3|LL_GPIO_PIN_4;
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_14;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
|
@ -62,24 +62,24 @@ void MX_SPI1_Init(void)
|
|||
GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN SPI1_Init 1 */
|
||||
/* USER CODE BEGIN SPI2_Init 1 */
|
||||
|
||||
/* USER CODE END SPI1_Init 1 */
|
||||
/* USER CODE END SPI2_Init 1 */
|
||||
SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
|
||||
SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
|
||||
SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
|
||||
SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_16BIT;
|
||||
SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
|
||||
SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
|
||||
SPI_InitStruct.ClockPhase = LL_SPI_PHASE_2EDGE;
|
||||
SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
|
||||
SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2;
|
||||
SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV64;
|
||||
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 */
|
||||
LL_SPI_Init(SPI2, &SPI_InitStruct);
|
||||
LL_SPI_SetStandard(SPI2, LL_SPI_PROTOCOL_MOTOROLA);
|
||||
/* USER CODE BEGIN SPI2_Init 2 */
|
||||
|
||||
/* USER CODE END SPI1_Init 2 */
|
||||
/* USER CODE END SPI2_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f4xx_it.c
|
||||
* @brief Interrupt Service Routines.
|
||||
******************************************************************************
|
||||
* @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.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
******************************************************************************
|
||||
* @file stm32f4xx_it.c
|
||||
* @brief Interrupt Service Routines.
|
||||
******************************************************************************
|
||||
* @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 ------------------------------------------------------------------*/
|
||||
|
@ -72,9 +72,8 @@ void NMI_Handler(void)
|
|||
|
||||
/* USER CODE END NonMaskableInt_IRQn 0 */
|
||||
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
while (1) {
|
||||
}
|
||||
/* USER CODE END NonMaskableInt_IRQn 1 */
|
||||
}
|
||||
|
||||
|
@ -245,7 +244,7 @@ void TIM1_CC_IRQHandler(void)
|
|||
void TIM2_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM2_IRQn 0 */
|
||||
|
||||
tim1msCallBack();
|
||||
/* USER CODE END TIM2_IRQn 0 */
|
||||
/* USER CODE BEGIN TIM2_IRQn 1 */
|
||||
|
||||
|
|
|
@ -112,10 +112,6 @@ void MX_TIM2_Init(void)
|
|||
/* Peripheral clock enable */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
||||
|
||||
/* TIM2 interrupt Init */
|
||||
NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
NVIC_EnableIRQ(TIM2_IRQn);
|
||||
|
||||
/* USER CODE BEGIN TIM2_Init 1 */
|
||||
|
||||
/* USER CODE END TIM2_Init 1 */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue