183 lines
5.3 KiB
C
183 lines
5.3 KiB
C
/**
|
|
* @file board.c
|
|
* @author jimingqing
|
|
* @date 2023-08-14
|
|
* @version 0.0.1
|
|
* @copyright XiazhiTech Copyright (c) 2023
|
|
*
|
|
* @brief
|
|
*/
|
|
|
|
#include "board.h"
|
|
|
|
/**
|
|
* @brief System Tick
|
|
* @retval None
|
|
*/
|
|
void SysTick_Handler(void)
|
|
{
|
|
HAL_IncTick();
|
|
}
|
|
|
|
/**
|
|
* @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_HSE;
|
|
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
RCC_OscInitStruct.PLL.PLLM = 25;
|
|
RCC_OscInitStruct.PLL.PLLN = 336;
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLQ = 4;
|
|
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_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)
|
|
{
|
|
/* ADC_IRQn interrupt configuration */
|
|
// HAL_NVIC_SetPriority(ADC_IRQn, 2, 0);
|
|
// HAL_NVIC_EnableIRQ(ADC_IRQn);
|
|
/* TIM1_UP_TIM10_IRQn interrupt configuration */
|
|
HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0);
|
|
HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
|
|
/* TIM1_BRK_TIM9_IRQn interrupt configuration */
|
|
HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 4, 1);
|
|
HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
|
|
/* TIM3_IRQn interrupt configuration */
|
|
HAL_NVIC_SetPriority(TIM3_IRQn, 3, 0);
|
|
HAL_NVIC_EnableIRQ(TIM3_IRQn);
|
|
/* USART1_IRQn interrupt configuration */
|
|
// HAL_NVIC_SetPriority(USART1_IRQn, 3, 1);
|
|
// HAL_NVIC_EnableIRQ(USART1_IRQn);
|
|
/* EXTI2_IRQn interrupt configuration */
|
|
// HAL_NVIC_SetPriority(EXTI2_IRQn, 3, 0);
|
|
// HAL_NVIC_EnableIRQ(EXTI2_IRQn);
|
|
}
|
|
|
|
void FOC_Init()
|
|
{
|
|
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // 开启三个PWM通道输出
|
|
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
|
|
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); // 开启三个PWM通道输出
|
|
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
|
|
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); // 开启三个PWM通道输出
|
|
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);
|
|
|
|
// LL_TIM_OC_SetCompareCH1(TIM1, 42 * 1000000 / 16000);
|
|
// LL_TIM_OC_SetCompareCH2(TIM1, 42 * 1000000 / 16000);
|
|
// LL_TIM_OC_SetCompareCH3(TIM1, 42 * 1000000 / 16000);
|
|
// LL_TIM_OC_SetCompareCH1(TIM1, 42 * 1000000 / 16000);
|
|
// __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2400);
|
|
// __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2400);
|
|
|
|
// HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
|
|
// HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
|
|
|
|
/* wait for a new PWM period */
|
|
// LL_TIM_ClearFlag_UPDATE(TIM1);
|
|
// // while (LL_TIM_IsActiveFlag_UPDATE(TIM1) == 0)
|
|
// // {
|
|
// // }
|
|
// /* Clear Update Flag */
|
|
// LL_TIM_ClearFlag_UPDATE(TIM1);
|
|
|
|
/* Main PWM Output Enable */
|
|
// TIM1->BDTR |= LL_TIM_OSSI_ENABLE;
|
|
// LL_TIM_EnableAllOutputs(TIM1);
|
|
|
|
// /* Clear Update Flag */
|
|
// LL_TIM_ClearFlag_UPDATE(TIM1);
|
|
// /* Enable Update IRQ */
|
|
// LL_TIM_EnableIT_UPDATE(TIM1);
|
|
|
|
// LL_TIM_OC_SetCompareCH1(TIM1, 42 * 1000000 / 16000);
|
|
// LL_TIM_OC_SetCompareCH2(TIM1, 42 * 1000000 / 16000);
|
|
// LL_TIM_OC_SetCompareCH3(TIM1, 42 * 1000000 / 16000);
|
|
}
|
|
|
|
/**
|
|
* This function will initial your board.
|
|
*/
|
|
void board_init(void)
|
|
{
|
|
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
|
__HAL_RCC_PWR_CLK_ENABLE();
|
|
|
|
HAL_Init();
|
|
SystemClock_Config();
|
|
SystemCoreClockUpdate();
|
|
|
|
MX_GPIO_Init();
|
|
MX_USART1_UART_Init();
|
|
MX_TIM1_Init();
|
|
// MX_TIM3_Init();
|
|
MX_NVIC_Init();
|
|
|
|
FOC_Init();
|
|
}
|
|
|
|
/**
|
|
* @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 */
|