|
|
@@ -1,7 +1,7 @@ |
|
|
/** |
|
|
/** |
|
|
* @file plsr_pulse_driver.c |
|
|
* @file plsr_pulse_driver.c |
|
|
* @brief 脉冲输出:Y0/TIM10、Y1/TIM11、Y2/TIM13;方向固定 Y3 |
|
|
|
|
|
* @note TIMxCLK=168MHz;段内锁 PSC 只改 ARR;改频在周期边界生效 |
|
|
|
|
|
|
|
|
* @brief 脉冲输出:Y0/PF6/TIM10、Y1/PF8/TIM13、Y2/PF7/TIM11;方向固定 Y3 |
|
|
|
|
|
* @note TIM10/11=168MHz,TIM13=84MHz;段内锁 PSC 只改 ARR;改频在周期边界生效 |
|
|
*/ |
|
|
*/ |
|
|
#include "plsr_pulse_driver.h" |
|
|
#include "plsr_pulse_driver.h" |
|
|
#include "plsr_param.h" |
|
|
#include "plsr_param.h" |
|
|
@@ -11,14 +11,15 @@ TIM_HandleTypeDef htim10; |
|
|
TIM_HandleTypeDef htim11; |
|
|
TIM_HandleTypeDef htim11; |
|
|
TIM_HandleTypeDef htim13; |
|
|
TIM_HandleTypeDef htim13; |
|
|
|
|
|
|
|
|
#define PLSR_TIM_CLK_HZ 168000000UL |
|
|
|
|
|
|
|
|
#define PLSR_TIM_CLK_APB2_HZ 168000000UL /* TIM10 / TIM11 */ |
|
|
|
|
|
#define PLSR_TIM_CLK_APB1_HZ 84000000UL /* TIM13 */ |
|
|
|
|
|
|
|
|
#define PLSR_Y0_PORT GPIOF |
|
|
#define PLSR_Y0_PORT GPIOF |
|
|
#define PLSR_Y0_PIN GPIO_PIN_6 |
|
|
|
|
|
|
|
|
#define PLSR_Y0_PIN GPIO_PIN_6 /* TIM10 */ |
|
|
#define PLSR_Y1_PORT GPIOF |
|
|
#define PLSR_Y1_PORT GPIOF |
|
|
#define PLSR_Y1_PIN GPIO_PIN_8 |
|
|
|
|
|
|
|
|
#define PLSR_Y1_PIN GPIO_PIN_8 /* TIM13:端子编号与 TIM 交叉 */ |
|
|
#define PLSR_Y2_PORT GPIOF |
|
|
#define PLSR_Y2_PORT GPIOF |
|
|
#define PLSR_Y2_PIN GPIO_PIN_7 |
|
|
|
|
|
|
|
|
#define PLSR_Y2_PIN GPIO_PIN_7 /* TIM11 */ |
|
|
#define PLSR_Y3_PORT GPIOF |
|
|
#define PLSR_Y3_PORT GPIOF |
|
|
#define PLSR_Y3_PIN GPIO_PIN_9 |
|
|
#define PLSR_Y3_PIN GPIO_PIN_9 |
|
|
|
|
|
|
|
|
@@ -36,13 +37,14 @@ static volatile uint8_t s_req_pending; |
|
|
|
|
|
|
|
|
static TIM_HandleTypeDef *PlsrPulseDriverHtimByY(uint16_t pulse_y) |
|
|
static TIM_HandleTypeDef *PlsrPulseDriverHtimByY(uint16_t pulse_y) |
|
|
{ |
|
|
{ |
|
|
|
|
|
/* Y1=PF8→TIM13,Y2=PF7→TIM11,与端子编号交叉 */ |
|
|
if (pulse_y == 1U) |
|
|
if (pulse_y == 1U) |
|
|
{ |
|
|
{ |
|
|
return &htim11; |
|
|
|
|
|
|
|
|
return &htim13; |
|
|
} |
|
|
} |
|
|
if (pulse_y == 2U) |
|
|
if (pulse_y == 2U) |
|
|
{ |
|
|
{ |
|
|
return &htim13; |
|
|
|
|
|
|
|
|
return &htim11; |
|
|
} |
|
|
} |
|
|
return &htim10; |
|
|
return &htim10; |
|
|
} |
|
|
} |
|
|
@@ -59,6 +61,17 @@ static uint8_t PlsrPulseDriverSelectY(void) |
|
|
return (uint8_t)y; |
|
|
return (uint8_t)y; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** TIM10/11:APB2 定时器 168MHz;TIM13:APB1 定时器 84MHz */ |
|
|
|
|
|
static uint32_t PlsrPulseDriverTimClkHz(const TIM_HandleTypeDef *htim) |
|
|
|
|
|
{ |
|
|
|
|
|
if ((htim != (const TIM_HandleTypeDef *)0) && |
|
|
|
|
|
(htim->Instance == TIM13)) |
|
|
|
|
|
{ |
|
|
|
|
|
return PLSR_TIM_CLK_APB1_HZ; |
|
|
|
|
|
} |
|
|
|
|
|
return PLSR_TIM_CLK_APB2_HZ; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
static void PlsrPulseDriverDirGpioInit(void) |
|
|
static void PlsrPulseDriverDirGpioInit(void) |
|
|
{ |
|
|
{ |
|
|
GPIO_InitTypeDef gpio = {0}; |
|
|
GPIO_InitTypeDef gpio = {0}; |
|
|
@@ -130,6 +143,26 @@ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) |
|
|
HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 5, 0); |
|
|
HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 5, 0); |
|
|
HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); |
|
|
HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); |
|
|
} |
|
|
} |
|
|
|
|
|
else if (htim->Instance == TIM11) |
|
|
|
|
|
{ |
|
|
|
|
|
/* TIM11_CH1 = PF7 = Y2 */ |
|
|
|
|
|
__HAL_RCC_TIM11_CLK_ENABLE(); |
|
|
|
|
|
gpio.Pin = PLSR_Y2_PIN; |
|
|
|
|
|
gpio.Alternate = GPIO_AF3_TIM11; |
|
|
|
|
|
HAL_GPIO_Init(PLSR_Y2_PORT, &gpio); |
|
|
|
|
|
HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM11_IRQn, 5, 0); |
|
|
|
|
|
HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM11_IRQn); |
|
|
|
|
|
} |
|
|
|
|
|
else if (htim->Instance == TIM13) |
|
|
|
|
|
{ |
|
|
|
|
|
/* TIM13_CH1 = PF8 = Y1 */ |
|
|
|
|
|
__HAL_RCC_TIM13_CLK_ENABLE(); |
|
|
|
|
|
gpio.Pin = PLSR_Y1_PIN; |
|
|
|
|
|
gpio.Alternate = GPIO_AF9_TIM13; |
|
|
|
|
|
HAL_GPIO_Init(PLSR_Y1_PORT, &gpio); |
|
|
|
|
|
HAL_NVIC_SetPriority(TIM8_UP_TIM13_IRQn, 5, 0); |
|
|
|
|
|
HAL_NVIC_EnableIRQ(TIM8_UP_TIM13_IRQn); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static void PlsrPulseDriverTimInitOne(TIM_HandleTypeDef *htim, TIM_TypeDef *inst) |
|
|
static void PlsrPulseDriverTimInitOne(TIM_HandleTypeDef *htim, TIM_TypeDef *inst) |
|
|
@@ -177,7 +210,10 @@ void PlsrPulseDriverInit(void) |
|
|
s_active_y = 0U; |
|
|
s_active_y = 0U; |
|
|
s_active_htim = &htim10; |
|
|
s_active_htim = &htim10; |
|
|
PlsrPulseDriverDirGpioInit(); |
|
|
PlsrPulseDriverDirGpioInit(); |
|
|
|
|
|
/* 三路都 Init + 空闲拉高,计数器关闭;出脉冲时只 Start 选中那路 */ |
|
|
PlsrPulseDriverTimInitOne(&htim10, TIM10); |
|
|
PlsrPulseDriverTimInitOne(&htim10, TIM10); |
|
|
|
|
|
PlsrPulseDriverTimInitOne(&htim11, TIM11); |
|
|
|
|
|
PlsrPulseDriverTimInitOne(&htim13, TIM13); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void PlsrPulseDriverSetDir(uint8_t forward) |
|
|
void PlsrPulseDriverSetDir(uint8_t forward) |
|
|
@@ -207,13 +243,17 @@ uint8_t PlsrPulseDriverIsPulseTim(TIM_TypeDef *instance) |
|
|
return ((instance == TIM10) || (instance == TIM11) || (instance == TIM13)) ? 1U : 0U; |
|
|
return ((instance == TIM10) || (instance == TIM11) || (instance == TIM13)) ? 1U : 0U; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static void PlsrPulseDriverCalcArrWithPsc(uint32_t freq_hz, uint32_t psc, |
|
|
|
|
|
|
|
|
static void PlsrPulseDriverCalcArrWithPsc(uint32_t clk_hz, uint32_t freq_hz, uint32_t psc, |
|
|
uint32_t *arr, uint32_t *ccr) |
|
|
uint32_t *arr, uint32_t *ccr) |
|
|
{ |
|
|
{ |
|
|
uint32_t ticks; |
|
|
uint32_t ticks; |
|
|
uint32_t a; |
|
|
uint32_t a; |
|
|
uint32_t c; |
|
|
uint32_t c; |
|
|
|
|
|
|
|
|
|
|
|
if (clk_hz < 1U) |
|
|
|
|
|
{ |
|
|
|
|
|
clk_hz = PLSR_TIM_CLK_APB2_HZ; |
|
|
|
|
|
} |
|
|
if (freq_hz < 1U) |
|
|
if (freq_hz < 1U) |
|
|
{ |
|
|
{ |
|
|
freq_hz = 1U; |
|
|
freq_hz = 1U; |
|
|
@@ -223,7 +263,7 @@ static void PlsrPulseDriverCalcArrWithPsc(uint32_t freq_hz, uint32_t psc, |
|
|
freq_hz = 100000U; |
|
|
freq_hz = 100000U; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ticks = PLSR_TIM_CLK_HZ / freq_hz; |
|
|
|
|
|
|
|
|
ticks = clk_hz / freq_hz; |
|
|
if (ticks < 2UL) |
|
|
if (ticks < 2UL) |
|
|
{ |
|
|
{ |
|
|
ticks = 2UL; |
|
|
ticks = 2UL; |
|
|
@@ -250,11 +290,16 @@ static void PlsrPulseDriverCalcArrWithPsc(uint32_t freq_hz, uint32_t psc, |
|
|
*ccr = c; |
|
|
*ccr = c; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static void PlsrPulseDriverCalcPscArr(uint32_t freq_hz, uint32_t *psc, uint32_t *arr, uint32_t *ccr) |
|
|
|
|
|
|
|
|
static void PlsrPulseDriverCalcPscArr(uint32_t clk_hz, uint32_t freq_hz, |
|
|
|
|
|
uint32_t *psc, uint32_t *arr, uint32_t *ccr) |
|
|
{ |
|
|
{ |
|
|
uint32_t ticks; |
|
|
uint32_t ticks; |
|
|
uint32_t p; |
|
|
uint32_t p; |
|
|
|
|
|
|
|
|
|
|
|
if (clk_hz < 1U) |
|
|
|
|
|
{ |
|
|
|
|
|
clk_hz = PLSR_TIM_CLK_APB2_HZ; |
|
|
|
|
|
} |
|
|
if (freq_hz < 1U) |
|
|
if (freq_hz < 1U) |
|
|
{ |
|
|
{ |
|
|
freq_hz = 1U; |
|
|
freq_hz = 1U; |
|
|
@@ -264,7 +309,7 @@ static void PlsrPulseDriverCalcPscArr(uint32_t freq_hz, uint32_t *psc, uint32_t |
|
|
freq_hz = 100000U; |
|
|
freq_hz = 100000U; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ticks = PLSR_TIM_CLK_HZ / freq_hz; |
|
|
|
|
|
|
|
|
ticks = clk_hz / freq_hz; |
|
|
if (ticks < 2UL) |
|
|
if (ticks < 2UL) |
|
|
{ |
|
|
{ |
|
|
ticks = 2UL; |
|
|
ticks = 2UL; |
|
|
@@ -282,7 +327,7 @@ static void PlsrPulseDriverCalcPscArr(uint32_t freq_hz, uint32_t *psc, uint32_t |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
*psc = p; |
|
|
*psc = p; |
|
|
PlsrPulseDriverCalcArrWithPsc(freq_hz, p, arr, ccr); |
|
|
|
|
|
|
|
|
PlsrPulseDriverCalcArrWithPsc(clk_hz, freq_hz, p, arr, ccr); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void PlsrPulseDriverLockPscRange(uint32_t f_min_hz, uint32_t f_max_hz) |
|
|
void PlsrPulseDriverLockPscRange(uint32_t f_min_hz, uint32_t f_max_hz) |
|
|
@@ -292,6 +337,8 @@ void PlsrPulseDriverLockPscRange(uint32_t f_min_hz, uint32_t f_max_hz) |
|
|
uint32_t psc_div_lo; |
|
|
uint32_t psc_div_lo; |
|
|
uint32_t psc_div_hi; |
|
|
uint32_t psc_div_hi; |
|
|
uint32_t div; |
|
|
uint32_t div; |
|
|
|
|
|
uint32_t clk_hz = PlsrPulseDriverTimClkHz( |
|
|
|
|
|
PlsrPulseDriverHtimByY(PlsrPulseDriverSelectY())); |
|
|
|
|
|
|
|
|
if (f_min_hz < 1U) |
|
|
if (f_min_hz < 1U) |
|
|
{ |
|
|
{ |
|
|
@@ -308,8 +355,8 @@ void PlsrPulseDriverLockPscRange(uint32_t f_min_hz, uint32_t f_max_hz) |
|
|
f_max_hz = t; |
|
|
f_max_hz = t; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ticks_lo = PLSR_TIM_CLK_HZ / f_min_hz; |
|
|
|
|
|
ticks_hi = PLSR_TIM_CLK_HZ / f_max_hz; |
|
|
|
|
|
|
|
|
ticks_lo = clk_hz / f_min_hz; |
|
|
|
|
|
ticks_hi = clk_hz / f_max_hz; |
|
|
if (ticks_lo < 2UL) |
|
|
if (ticks_lo < 2UL) |
|
|
{ |
|
|
{ |
|
|
ticks_lo = 2UL; |
|
|
ticks_lo = 2UL; |
|
|
@@ -365,6 +412,7 @@ static void PlsrPulseDriverStartCommon(uint32_t freq_hz, uint8_t enable_update_i |
|
|
uint32_t ccr; |
|
|
uint32_t ccr; |
|
|
uint8_t y = PlsrPulseDriverSelectY(); |
|
|
uint8_t y = PlsrPulseDriverSelectY(); |
|
|
TIM_HandleTypeDef *htim = PlsrPulseDriverHtimByY(y); |
|
|
TIM_HandleTypeDef *htim = PlsrPulseDriverHtimByY(y); |
|
|
|
|
|
uint32_t clk_hz; |
|
|
|
|
|
|
|
|
s_req_pending = 0U; |
|
|
s_req_pending = 0U; |
|
|
|
|
|
|
|
|
@@ -374,6 +422,7 @@ static void PlsrPulseDriverStartCommon(uint32_t freq_hz, uint8_t enable_update_i |
|
|
} |
|
|
} |
|
|
s_active_y = y; |
|
|
s_active_y = y; |
|
|
s_active_htim = htim; |
|
|
s_active_htim = htim; |
|
|
|
|
|
clk_hz = PlsrPulseDriverTimClkHz(htim); |
|
|
|
|
|
|
|
|
if (freq_hz == 0U) |
|
|
if (freq_hz == 0U) |
|
|
{ |
|
|
{ |
|
|
@@ -387,11 +436,11 @@ static void PlsrPulseDriverStartCommon(uint32_t freq_hz, uint8_t enable_update_i |
|
|
if (s_psc_locked != 0U) |
|
|
if (s_psc_locked != 0U) |
|
|
{ |
|
|
{ |
|
|
psc = s_locked_psc; |
|
|
psc = s_locked_psc; |
|
|
PlsrPulseDriverCalcArrWithPsc(freq_hz, psc, &arr, &ccr); |
|
|
|
|
|
|
|
|
PlsrPulseDriverCalcArrWithPsc(clk_hz, freq_hz, psc, &arr, &ccr); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
PlsrPulseDriverCalcPscArr(freq_hz, &psc, &arr, &ccr); |
|
|
|
|
|
|
|
|
PlsrPulseDriverCalcPscArr(clk_hz, freq_hz, &psc, &arr, &ccr); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
s_last_freq = freq_hz; |
|
|
s_last_freq = freq_hz; |
|
|
@@ -436,6 +485,7 @@ void PlsrPulseDriverSetFreq(uint32_t freq_hz) |
|
|
uint32_t psc; |
|
|
uint32_t psc; |
|
|
uint32_t arr; |
|
|
uint32_t arr; |
|
|
uint32_t ccr; |
|
|
uint32_t ccr; |
|
|
|
|
|
uint32_t clk_hz; |
|
|
TIM_HandleTypeDef *htim; |
|
|
TIM_HandleTypeDef *htim; |
|
|
|
|
|
|
|
|
if (freq_hz == s_last_freq) |
|
|
if (freq_hz == s_last_freq) |
|
|
@@ -446,6 +496,7 @@ void PlsrPulseDriverSetFreq(uint32_t freq_hz) |
|
|
htim = (s_active_htim != (TIM_HandleTypeDef *)0) ? |
|
|
htim = (s_active_htim != (TIM_HandleTypeDef *)0) ? |
|
|
s_active_htim : PlsrPulseDriverHtimByY(PlsrPulseDriverSelectY()); |
|
|
s_active_htim : PlsrPulseDriverHtimByY(PlsrPulseDriverSelectY()); |
|
|
s_active_htim = htim; |
|
|
s_active_htim = htim; |
|
|
|
|
|
clk_hz = PlsrPulseDriverTimClkHz(htim); |
|
|
|
|
|
|
|
|
if (freq_hz == 0U) |
|
|
if (freq_hz == 0U) |
|
|
{ |
|
|
{ |
|
|
@@ -459,11 +510,11 @@ void PlsrPulseDriverSetFreq(uint32_t freq_hz) |
|
|
if (s_psc_locked != 0U) |
|
|
if (s_psc_locked != 0U) |
|
|
{ |
|
|
{ |
|
|
psc = s_locked_psc; |
|
|
psc = s_locked_psc; |
|
|
PlsrPulseDriverCalcArrWithPsc(freq_hz, psc, &arr, &ccr); |
|
|
|
|
|
|
|
|
PlsrPulseDriverCalcArrWithPsc(clk_hz, freq_hz, psc, &arr, &ccr); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
PlsrPulseDriverCalcPscArr(freq_hz, &psc, &arr, &ccr); |
|
|
|
|
|
|
|
|
PlsrPulseDriverCalcPscArr(clk_hz, freq_hz, &psc, &arr, &ccr); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ((psc == s_last_psc) && (arr == s_last_arr)) |
|
|
if ((psc == s_last_psc) && (arr == s_last_arr)) |
|
|
|