aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/platforms/STM32/pwm_lld.h
diff options
context:
space:
mode:
authorgdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2010-10-13 11:45:07 +0000
committergdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2010-10-13 11:45:07 +0000
commit4fab7c06d1b0c9e61f6106b5b2a5c2c0b5694c34 (patch)
treee7fdadc85d78aa2143d0082d26351da49f7f2d53 /os/hal/platforms/STM32/pwm_lld.h
parentec7455babe131ee0b8a4c228ed00a02396619a7d (diff)
downloadChibiOS-4fab7c06d1b0c9e61f6106b5b2a5c2c0b5694c34.tar.gz
ChibiOS-4fab7c06d1b0c9e61f6106b5b2a5c2c0b5694c34.tar.bz2
ChibiOS-4fab7c06d1b0c9e61f6106b5b2a5c2c0b5694c34.zip
ADC, SPI, PWM driver enhancements.
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2254 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'os/hal/platforms/STM32/pwm_lld.h')
-rw-r--r--os/hal/platforms/STM32/pwm_lld.h67
1 files changed, 66 insertions, 1 deletions
diff --git a/os/hal/platforms/STM32/pwm_lld.h b/os/hal/platforms/STM32/pwm_lld.h
index b86ab1a8f..761c3af39 100644
--- a/os/hal/platforms/STM32/pwm_lld.h
+++ b/os/hal/platforms/STM32/pwm_lld.h
@@ -149,7 +149,6 @@ typedef void (*pwmcallback_t)(PWMDriver *pwmp);
/**
* @brief PWM driver channel configuration structure.
- * @note It could be empty on some architectures.
*/
typedef struct {
/**
@@ -225,6 +224,72 @@ struct PWMDriver {
/* Driver macros. */
/*===========================================================================*/
+/**
+ * @brief PWM clock prescaler initialization utility.
+ * @note The real clock value is rounded to the lower valid value, please
+ * make sure that the source clock frequency is a multiple of the
+ * requested PWM clock frequency.
+ * @note The calculated value must fit into an unsigned 16 bits integer.
+ *
+ * @param[in] clksrc clock source frequency, depending on the target timer
+ * cell it can be one of:
+ * - STM32_TIMCLK1
+ * - STM32_TIMCLK2
+ * .
+ * Please refer to the STM32 HAL driver documentation
+ * and/or the STM32 Reference Manual for the right clock
+ * source.
+ * @param[in] nsec PWM clock cycle time in nanoseconds
+ * @return The value to be stored in the @p pc_psc field of the
+ * @p PWMConfig structure.
+ */
+#define PWM_COMPUTE_PSC(clksrc, nsec) \
+ ((uint16_t)(((clksrc) / (1000000000 / (nsec))) - 1))
+
+/**
+ * @brief PWM cycle period initialization utility.
+ * @note The calculated value must fit into an unsigned 16 bits integer.
+ *
+ * @param[in] clkperiod PWM clock period in nanoseconds
+ * @param[in] pwmperiod PWM cycle period in nanoseconds
+ */
+#define PWM_COMPUTE_ARR(clkperiod, pwmperiod) \
+ ((uint16_t)(((clkperiod) / (1000000000 / (pwmperiod))) - 1))
+
+/**
+ * @brief Converts from degrees to pulse width.
+ * @note Be careful with rounding errors, this is integer math not magic.
+ * You can specify hundredths of degrees 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] pwmp pointer to a @p PWMDriver object
+ * @param[in] degrees degrees as an integer between 0 and 36000
+ * @return The pulse width to be passed to @p pwmEnableChannel().
+ *
+ * @api
+ */
+#define PWM_DEGREES_TO_WIDTH(pwpm, degrees) \
+ ((uint16_t)(((((uint32_t)(pwpm)->pd_config->pc_arr + 1UL) * \
+ (uint32_t)(degrees)) / 36000UL) - 1UL))
+
+/**
+ * @brief Converts from percentage 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] pwmp pointer to a @p PWMDriver object
+ * @param[in] percentage percentage as an integer between 0 and 10000
+ * @return The pulse width to be passed to @p pwmEnableChannel().
+ *
+ * @api
+ */
+#define PWM_PERCENTAGE_TO_WIDTH(pwpm, percentage) \
+ ((uint16_t)(((((uint32_t)(pwpm)->pd_config->pc_arr + 1UL) * \
+ (uint32_t)(percentage)) / 10000UL) - 1UL))
+
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/