2009-12-13 13:37:06 +00:00
|
|
|
/*
|
2010-02-21 07:24:53 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
|
2009-12-13 13:37:06 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @file STM32/pwm_lld.h
|
|
|
|
* @brief STM32 PWM subsystem low level driver header.
|
|
|
|
*
|
2010-10-25 18:48:13 +00:00
|
|
|
* @addtogroup PWM
|
2009-12-13 13:37:06 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PWM_LLD_H_
|
|
|
|
#define _PWM_LLD_H_
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#if HAL_USE_PWM || defined(__DOXYGEN__)
|
2009-12-13 13:37:06 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief Number of PWM channels per PWM driver.
|
2009-12-13 13:37:06 +00:00
|
|
|
*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#define PWM_CHANNELS 4
|
2009-12-13 13:37:06 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM1 driver enable switch.
|
2009-12-13 13:37:06 +00:00
|
|
|
* @details If set to @p TRUE the support for PWM1 is included.
|
2010-07-27 10:31:19 +00:00
|
|
|
* @note The default is @p TRUE.
|
2009-12-13 13:37:06 +00:00
|
|
|
*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if !defined(STM32_PWM_USE_TIM1) || defined(__DOXYGEN__)
|
|
|
|
#define STM32_PWM_USE_TIM1 TRUE
|
2009-12-13 13:37:06 +00:00
|
|
|
#endif
|
|
|
|
|
2009-12-19 09:05:40 +00:00
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM2 driver enable switch.
|
2009-12-19 09:05:40 +00:00
|
|
|
* @details If set to @p TRUE the support for PWM2 is included.
|
2010-07-27 10:31:19 +00:00
|
|
|
* @note The default is @p TRUE.
|
2009-12-19 09:05:40 +00:00
|
|
|
*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if !defined(STM32_PWM_USE_TIM2) || defined(__DOXYGEN__)
|
|
|
|
#define STM32_PWM_USE_TIM2 TRUE
|
2009-12-19 09:05:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM3 driver enable switch.
|
2009-12-19 09:05:40 +00:00
|
|
|
* @details If set to @p TRUE the support for PWM3 is included.
|
2010-07-27 10:31:19 +00:00
|
|
|
* @note The default is @p TRUE.
|
2009-12-19 09:05:40 +00:00
|
|
|
*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if !defined(STM32_PWM_USE_TIM3) || defined(__DOXYGEN__)
|
|
|
|
#define STM32_PWM_USE_TIM3 TRUE
|
2009-12-19 09:05:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM4 driver enable switch.
|
2009-12-19 09:05:40 +00:00
|
|
|
* @details If set to @p TRUE the support for PWM4 is included.
|
2010-07-27 10:31:19 +00:00
|
|
|
* @note The default is @p TRUE.
|
2009-12-19 09:05:40 +00:00
|
|
|
*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if !defined(STM32_PWM_USE_TIM4) || defined(__DOXYGEN__)
|
|
|
|
#define STM32_PWM_USE_TIM4 TRUE
|
2009-12-19 09:05:40 +00:00
|
|
|
#endif
|
|
|
|
|
2009-12-13 13:37:06 +00:00
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM1 interrupt priority level setting.
|
2009-12-13 13:37:06 +00:00
|
|
|
*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if !defined(STM32_PWM_PWM1_IRQ_PRIORITY) || defined(__DOXYGEN__)
|
|
|
|
#define STM32_PWM_PWM1_IRQ_PRIORITY 7
|
2009-12-13 13:37:06 +00:00
|
|
|
#endif
|
|
|
|
|
2009-12-19 09:05:40 +00:00
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM2 interrupt priority level setting.
|
2009-12-19 09:05:40 +00:00
|
|
|
*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if !defined(STM32_PWM_PWM2_IRQ_PRIORITY) || defined(__DOXYGEN__)
|
|
|
|
#define STM32_PWM_PWM2_IRQ_PRIORITY 7
|
2009-12-19 09:05:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM3 interrupt priority level setting.
|
2009-12-19 09:05:40 +00:00
|
|
|
*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if !defined(STM32_PWM_PWM3_IRQ_PRIORITY) || defined(__DOXYGEN__)
|
|
|
|
#define STM32_PWM_PWM3_IRQ_PRIORITY 7
|
2009-12-19 09:05:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM4 interrupt priority level setting.
|
2009-12-19 09:05:40 +00:00
|
|
|
*/
|
2010-08-08 07:57:28 +00:00
|
|
|
#if !defined(STM32_PWM_PWM4_IRQ_PRIORITY) || defined(__DOXYGEN__)
|
|
|
|
#define STM32_PWM_PWM4_IRQ_PRIORITY 7
|
2009-12-19 09:05:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Configuration checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-11-14 13:29:09 +00:00
|
|
|
#if STM32_PWM_USE_TIM1 && !STM32_HAS_TIM1
|
|
|
|
#error "TIM1 not present in the selected device"
|
2009-12-19 09:05:40 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-14 13:29:09 +00:00
|
|
|
#if STM32_PWM_USE_TIM2 && !STM32_HAS_TIM2
|
|
|
|
#error "TIM2 not present in the selected device"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if STM32_PWM_USE_TIM3 && !STM32_HAS_TIM3
|
|
|
|
#error "TIM3 not present in the selected device"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if STM32_PWM_USE_TIM4 && !STM32_HAS_TIM4
|
|
|
|
#error "TIM4 not present in the selected device"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !STM32_PWM_USE_TIM1 && !STM32_PWM_USE_TIM2 && \
|
|
|
|
!STM32_PWM_USE_TIM3 && !STM32_PWM_USE_TIM4
|
2010-08-08 07:57:28 +00:00
|
|
|
#error "PWM driver activated but no TIM peripheral assigned"
|
|
|
|
#endif
|
|
|
|
|
2009-12-13 13:37:06 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver data structures and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM channel type.
|
2009-12-13 13:37:06 +00:00
|
|
|
*/
|
|
|
|
typedef uint8_t pwmchannel_t;
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM counter type.
|
2009-12-13 13:37:06 +00:00
|
|
|
*/
|
|
|
|
typedef uint16_t pwmcnt_t;
|
|
|
|
|
2010-10-08 18:16:38 +00:00
|
|
|
/**
|
|
|
|
* @brief Type of a structure representing an PWM driver.
|
|
|
|
*/
|
|
|
|
typedef struct PWMDriver PWMDriver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief PWM notification callback type.
|
|
|
|
*
|
|
|
|
* @param[in] pwmp pointer to a @p PWMDriver object
|
|
|
|
*/
|
|
|
|
typedef void (*pwmcallback_t)(PWMDriver *pwmp);
|
|
|
|
|
2009-12-17 15:40:32 +00:00
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM driver channel configuration structure.
|
2009-12-17 15:40:32 +00:00
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/**
|
|
|
|
* @brief Channel active logic level.
|
|
|
|
*/
|
|
|
|
pwmmode_t pcc_mode;
|
|
|
|
/**
|
|
|
|
* @brief Channel callback pointer.
|
2010-07-27 10:31:19 +00:00
|
|
|
* @note This callback is invoked on the channel compare event. If set to
|
|
|
|
* @p NULL then the callback is disabled.
|
2009-12-17 15:40:32 +00:00
|
|
|
*/
|
|
|
|
pwmcallback_t pcc_callback;
|
|
|
|
/* End of the mandatory fields.*/
|
|
|
|
} PWMChannelConfig;
|
|
|
|
|
2009-12-13 13:37:06 +00:00
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief PWM driver configuration structure.
|
2009-12-13 13:37:06 +00:00
|
|
|
*/
|
|
|
|
typedef struct {
|
2009-12-15 22:23:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Periodic callback pointer.
|
2010-07-27 10:31:19 +00:00
|
|
|
* @note This callback is invoked on PWM counter reset. If set to
|
|
|
|
* @p NULL then the callback is disabled.
|
2009-12-15 22:23:25 +00:00
|
|
|
*/
|
|
|
|
pwmcallback_t pc_callback;
|
2009-12-17 15:40:32 +00:00
|
|
|
/**
|
|
|
|
* @brief Channels configurations.
|
|
|
|
*/
|
|
|
|
PWMChannelConfig pc_channels[PWM_CHANNELS];
|
2009-12-15 22:23:25 +00:00
|
|
|
/* End of the mandatory fields.*/
|
2009-12-16 15:48:50 +00:00
|
|
|
/**
|
|
|
|
* @brief TIM PSC (pre-scaler) register initialization data.
|
|
|
|
*/
|
|
|
|
uint16_t pc_psc;
|
|
|
|
/**
|
|
|
|
* @brief TIM ARR (auto-reload) register initialization data.
|
|
|
|
*/
|
|
|
|
uint16_t pc_arr;
|
|
|
|
/**
|
|
|
|
* @brief TIM CR2 register initialization data.
|
2010-07-27 10:31:19 +00:00
|
|
|
* @note The value of this field should normally be equal to zero.
|
2009-12-16 15:48:50 +00:00
|
|
|
*/
|
|
|
|
uint16_t pc_cr2;
|
2009-12-13 13:37:06 +00:00
|
|
|
} PWMConfig;
|
|
|
|
|
|
|
|
/**
|
2010-07-27 10:31:19 +00:00
|
|
|
* @brief Structure representing a PWM driver.
|
2009-12-13 13:37:06 +00:00
|
|
|
*/
|
2010-10-08 18:16:38 +00:00
|
|
|
struct PWMDriver {
|
2009-12-13 13:37:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Driver state.
|
|
|
|
*/
|
|
|
|
pwmstate_t pd_state;
|
|
|
|
/**
|
|
|
|
* @brief Current driver configuration data.
|
|
|
|
*/
|
|
|
|
const PWMConfig *pd_config;
|
2010-10-08 18:16:38 +00:00
|
|
|
#if defined(PWM_DRIVER_EXT_FIELDS)
|
|
|
|
PWM_DRIVER_EXT_FIELDS
|
|
|
|
#endif
|
2009-12-13 13:37:06 +00:00
|
|
|
/* End of the mandatory fields.*/
|
|
|
|
/**
|
2010-02-21 07:24:53 +00:00
|
|
|
* @brief Bit mask of the enabled channels.
|
2009-12-13 13:37:06 +00:00
|
|
|
*/
|
|
|
|
uint32_t pd_enabled_channels;
|
2009-12-16 15:48:50 +00:00
|
|
|
/**
|
2010-02-21 07:24:53 +00:00
|
|
|
* @brief Pointer to the TIMx registers block.
|
2009-12-16 15:48:50 +00:00
|
|
|
*/
|
|
|
|
TIM_TypeDef *pd_tim;
|
2010-10-08 18:16:38 +00:00
|
|
|
};
|
2009-12-13 13:37:06 +00:00
|
|
|
|
2009-12-29 13:15:29 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver macros. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-10-13 11:45:07 +00:00
|
|
|
/**
|
|
|
|
* @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.
|
2010-11-21 09:41:46 +00:00
|
|
|
* @param[in] pwmclk PWM clock frequency in cycles
|
2010-10-13 11:45:07 +00:00
|
|
|
* @return The value to be stored in the @p pc_psc field of the
|
|
|
|
* @p PWMConfig structure.
|
|
|
|
*/
|
2010-11-21 09:41:46 +00:00
|
|
|
#define PWM_COMPUTE_PSC(clksrc, pwmclk) \
|
|
|
|
((uint16_t)(((clksrc) / (pwmclk)) - 1))
|
2010-10-13 11:45:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief PWM cycle period initialization utility.
|
|
|
|
* @note The calculated value must fit into an unsigned 16 bits integer.
|
|
|
|
*
|
2010-11-21 09:41:46 +00:00
|
|
|
* @param[in] pwmclk PWM clock frequency in cycles
|
|
|
|
* @param[in] pwmperiod PWM cycle period in microseconds
|
|
|
|
* @return The value to be stored in the @p pc_arr field of the
|
|
|
|
* @p PWMConfig structure.
|
2010-10-13 11:45:07 +00:00
|
|
|
*/
|
2010-11-21 09:41:46 +00:00
|
|
|
#define PWM_COMPUTE_ARR(pwmclk, pwmperiod) \
|
|
|
|
((uint16_t)(((pwmclk) / (1000000 / (pwmperiod))) - 1))
|
2010-10-13 11:45:07 +00:00
|
|
|
|
2010-11-21 11:12:54 +00:00
|
|
|
/**
|
|
|
|
* @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))
|
|
|
|
|
2010-10-13 11:45:07 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2010-10-25 18:48:13 +00:00
|
|
|
#define PWM_DEGREES_TO_WIDTH(pwmp, degrees) \
|
2010-11-21 11:12:54 +00:00
|
|
|
PWM_FRACTION_TO_WIDTH(pwmp, 36000, degrees)
|
2010-10-13 11:45:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2010-10-25 18:48:13 +00:00
|
|
|
#define PWM_PERCENTAGE_TO_WIDTH(pwmp, percentage) \
|
2010-11-21 11:12:54 +00:00
|
|
|
PWM_FRACTION_TO_WIDTH(pwmp, 10000, percentage)
|
2010-10-13 11:45:07 +00:00
|
|
|
|
2009-12-13 13:37:06 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if defined(STM32_PWM_USE_TIM1) && !defined(__DOXYGEN__)
|
2009-12-13 13:37:06 +00:00
|
|
|
extern PWMDriver PWMD1;
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if defined(STM32_PWM_USE_TIM2) && !defined(__DOXYGEN__)
|
2009-12-19 09:05:40 +00:00
|
|
|
extern PWMDriver PWMD2;
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if defined(STM32_PWM_USE_TIM3) && !defined(__DOXYGEN__)
|
2009-12-19 09:05:40 +00:00
|
|
|
extern PWMDriver PWMD3;
|
|
|
|
#endif
|
|
|
|
|
2010-08-08 07:57:28 +00:00
|
|
|
#if defined(STM32_PWM_USE_TIM4) && !defined(__DOXYGEN__)
|
2009-12-19 09:05:40 +00:00
|
|
|
extern PWMDriver PWMD4;
|
|
|
|
#endif
|
|
|
|
|
2009-12-13 13:37:06 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
void pwm_lld_init(void);
|
|
|
|
void pwm_lld_start(PWMDriver *pwmp);
|
|
|
|
void pwm_lld_stop(PWMDriver *pwmp);
|
|
|
|
void pwm_lld_enable_channel(PWMDriver *pwmp,
|
|
|
|
pwmchannel_t channel,
|
|
|
|
pwmcnt_t width);
|
|
|
|
void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel);
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#endif /* HAL_USE_PWM */
|
2009-12-13 13:37:06 +00:00
|
|
|
|
|
|
|
#endif /* _PWM_LLD_H_ */
|
|
|
|
|
|
|
|
/** @} */
|