From 66c5a970fa52e53e643ef1f3365b4d30debd5bfe Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 21 Nov 2010 11:12:54 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2406 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/platforms/STM32/pwm_lld.h | 23 +++++++++++++++++++---- os/hal/templates/pwm_lld.h | 21 +++++++++++++++++++-- readme.txt | 1 + 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/os/hal/platforms/STM32/pwm_lld.h b/os/hal/platforms/STM32/pwm_lld.h index b22e3a315..a1a6c905e 100644 --- a/os/hal/platforms/STM32/pwm_lld.h +++ b/os/hal/platforms/STM32/pwm_lld.h @@ -269,6 +269,23 @@ struct PWMDriver { #define PWM_COMPUTE_ARR(pwmclk, pwmperiod) \ ((uint16_t)(((pwmclk) / (1000000 / (pwmperiod))) - 1)) +/** + * @brief Converts from fraction to pulse width. + * @note Be careful with rounding errors, this is integer math not magic. + * You can specify tenths of thousandth but make sure you have the + * proper hardware resolution by carefully choosing the clock source + * and prescaler settings, see @p PWM_COMPUTE_PSC. + * + * @param[in] numerator numerator of the fraction + * @param[in] denominator percentage as an integer between 0 and numerator + * @return The pulse width to be passed to @p pwmEnableChannel(). + * + * @api + */ +#define PWM_FRACTION_TO_WIDTH(pwmp, numerator, denominator) \ + ((uint16_t)(((((uint32_t)(pwmp)->pd_config->pc_arr + 1UL) * \ + (uint32_t)(denominator)) / (uint32_t)(numerator)) - 1UL)) + /** * @brief Converts from degrees to pulse width. * @note Be careful with rounding errors, this is integer math not magic. @@ -283,8 +300,7 @@ struct PWMDriver { * @api */ #define PWM_DEGREES_TO_WIDTH(pwmp, degrees) \ - ((uint16_t)(((((uint32_t)(pwmp)->pd_config->pc_arr + 1UL) * \ - (uint32_t)(degrees)) / 36000UL) - 1UL)) + PWM_FRACTION_TO_WIDTH(pwmp, 36000, degrees) /** * @brief Converts from percentage to pulse width. @@ -300,8 +316,7 @@ struct PWMDriver { * @api */ #define PWM_PERCENTAGE_TO_WIDTH(pwmp, percentage) \ - ((uint16_t)(((((uint32_t)(pwmp)->pd_config->pc_arr + 1UL) * \ - (uint32_t)(percentage)) / 10000UL) - 1UL)) + PWM_FRACTION_TO_WIDTH(pwmp, 10000, percentage) /*===========================================================================*/ /* External declarations. */ diff --git a/os/hal/templates/pwm_lld.h b/os/hal/templates/pwm_lld.h index 1b0b38f75..2b8cf294f 100644 --- a/os/hal/templates/pwm_lld.h +++ b/os/hal/templates/pwm_lld.h @@ -137,6 +137,21 @@ struct PWMDriver { /* Driver macros. */ /*===========================================================================*/ +/** + * @brief Converts from fraction to pulse width. + * @note Be careful with rounding errors, this is integer math not magic. + * You can specify tenths of thousandth but make sure you have the + * proper hardware resolution by carefully choosing the clock source + * and prescaler settings, see @p PWM_COMPUTE_PSC. + * + * @param[in] numerator numerator of the fraction + * @param[in] denominator percentage as an integer between 0 and numerator + * @return The pulse width to be passed to @p pwmEnableChannel(). + * + * @api + */ +#define PWM_FRACTION_TO_WIDTH(pwmp, numerator, denominator) 0 + /** * @brief Converts from degrees to pulse width. * @note Be careful with rounding errors, this is integer math not magic. @@ -150,7 +165,8 @@ struct PWMDriver { * * @api */ -#define PWM_DEGREES_TO_WIDTH(pwmp, degrees) 0 +#define PWM_DEGREES_TO_WIDTH(pwmp, degrees) \ + PWM_FRACTION_TO_WIDTH(pwmp, 36000, degrees) /** * @brief Converts from percentage to pulse width. @@ -165,7 +181,8 @@ struct PWMDriver { * * @api */ -#define PWM_PERCENTAGE_TO_WIDTH(pwmp, percentage) 0 +#define PWM_PERCENTAGE_TO_WIDTH(pwmp, percentage) \ + PWM_FRACTION_TO_WIDTH(pwmp, 10000, percentage) /*===========================================================================*/ /* External declarations. */ diff --git a/readme.txt b/readme.txt index 0ed03f9fc..cc4740a7a 100644 --- a/readme.txt +++ b/readme.txt @@ -70,6 +70,7 @@ - FIX: Fixed typo in board name (bug 3113574)(backported to 2.0.7). - FIX: Fixed defective event wait functions with timeout (bug 3113443) (backported to 2.0.7). +- NEW: Added new macro PWM_FRACTION_TO_WIDTH() to the PWM driver model. - NEW: ARM7 port reorganization following the same pattern of the ARMCMx one, on now the port is also supports the ARM9 architecture (but not tested yet hardware). Removed the dependencies between the port layer