1.优化PWM定时器PSC和ARR计算算法,大幅提升计算效率(通常只需迭代2-3次),优先逼近目标频率,并在误差允许范围内(万分之一到百分之一,误差范围随目标频率提高而增加)获得最佳的占空比分辨率。
2.PWM-info中clk从MHz改为Hz。
This commit is contained in:
@@ -144,8 +144,8 @@ static int drv_pwm_configure(struct mr_pwm *pwm, int state)
|
|||||||
pclk = RCC_ClockStructure.PCLK1_Frequency;
|
pclk = RCC_ClockStructure.PCLK1_Frequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Update pwm clock(MHz) */
|
/* Update pwm clock(Hz) */
|
||||||
pwm->info->clk = pclk / 1000000;
|
pwm->info->clk = pclk;
|
||||||
|
|
||||||
/* Configure remap */
|
/* Configure remap */
|
||||||
if (pwm_data->remap != 0)
|
if (pwm_data->remap != 0)
|
||||||
|
|||||||
76
device/pwm.c
76
device/pwm.c
@@ -65,74 +65,62 @@ static int pwm_channel_get_configure(struct mr_pwm *pwm, int channel, struct mr_
|
|||||||
static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq)
|
static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq)
|
||||||
{
|
{
|
||||||
uint32_t clk = pwm->info->clk, psc_max = pwm->info->prescaler_max, per_max = pwm->info->period_max;
|
uint32_t clk = pwm->info->clk, psc_max = pwm->info->prescaler_max, per_max = pwm->info->period_max;
|
||||||
uint32_t psc_best = 0, per_best = 0;
|
uint32_t psc_best = 1, per_best = 1;
|
||||||
int error_min = INT32_MAX;
|
int error_min = INT32_MAX;
|
||||||
|
|
||||||
/* Check the clock */
|
/* Check the clock and frequency */
|
||||||
if (clk == 0 || freq == 0)
|
if (clk == 0 || freq == 0)
|
||||||
{
|
{
|
||||||
return MR_EINVAL;
|
return MR_EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Calculate the timeout */
|
/* Calculate the prescaler and period product */
|
||||||
uint32_t timeout = (clk * 1000000) / freq;
|
uint32_t product = clk / freq;
|
||||||
|
|
||||||
/* Calculate the Least error period */
|
/* If the product is within the maximum period, set it as the period */
|
||||||
for (uint32_t per = (timeout <= per_max) ? timeout : (timeout / (per_max + 1)); per > 0; per--)
|
if (product <= per_max)
|
||||||
{
|
{
|
||||||
uint32_t psc = timeout / per;
|
psc_best = 1;
|
||||||
|
per_best = product;
|
||||||
/* Calculate the error */
|
} else
|
||||||
int error = (int)timeout - (int)(psc * per);
|
|
||||||
if (error == 0)
|
|
||||||
{
|
|
||||||
psc_best = psc;
|
|
||||||
per_best = per;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (error <= error_min)
|
|
||||||
{
|
|
||||||
error_min = error;
|
|
||||||
psc_best = psc;
|
|
||||||
per_best = per;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Optimize the prescaler and period */
|
|
||||||
for (uint32_t divisor = 9; divisor > 1; divisor--)
|
|
||||||
{
|
{
|
||||||
/* Check if reload value can be divided by current divisor */
|
/* Calculate the Least error prescaler and period */
|
||||||
while ((psc_best % divisor) == 0)
|
for (uint32_t psc = MR_BOUND(product / per_max, 1, psc_max); psc < psc_max; psc++)
|
||||||
{
|
{
|
||||||
uint32_t per_temp = per_best * divisor;
|
uint32_t per = MR_BOUND(product / psc, 1, per_max);
|
||||||
|
|
||||||
/* Check if new period or prescaler is valid */
|
/* Calculate the frequency error */
|
||||||
if (per_temp <= per_max)
|
int error = (int)freq - (int)(clk / psc / per);
|
||||||
{
|
|
||||||
per_best = per_temp;
|
|
||||||
psc_best /= divisor;
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check if prescaler can be used as period */
|
/* Found a potentially optimal prescaler and period combination */
|
||||||
if ((psc_best > per_best) && (psc_best < per_max))
|
if (error == 0)
|
||||||
{
|
{
|
||||||
MR_SWAP(per_best, psc_best);
|
psc_best = psc;
|
||||||
|
per_best = per;
|
||||||
|
|
||||||
|
/* Found a valid and optimal solution */
|
||||||
|
if (per_best < per_max)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (error < error_min)
|
||||||
|
{
|
||||||
|
error_min = error;
|
||||||
|
psc_best = psc;
|
||||||
|
per_best = per;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check prescaler is valid */
|
/* Check period is valid */
|
||||||
if (psc_best > psc_max)
|
if (per_best > per_max)
|
||||||
{
|
{
|
||||||
return MR_EINVAL;
|
return MR_EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pwm->prescaler = psc_best;
|
pwm->prescaler = psc_best;
|
||||||
pwm->period = per_best;
|
pwm->period = per_best;
|
||||||
pwm->freq = (clk * 1000000) / psc_best / per_best;
|
pwm->freq = clk / psc_best / per_best;
|
||||||
return MR_EOK;
|
return MR_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ typedef uint32_t mr_pwm_data_t; /**< PWM rea
|
|||||||
*/
|
*/
|
||||||
struct mr_pwm_info
|
struct mr_pwm_info
|
||||||
{
|
{
|
||||||
uint32_t clk; /**< Clock(MHz) */
|
uint32_t clk; /**< Clock(Hz) */
|
||||||
uint32_t prescaler_max; /**< Prescaler max */
|
uint32_t prescaler_max; /**< Prescaler max */
|
||||||
uint32_t period_max; /**< Period max */
|
uint32_t period_max; /**< Period max */
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user