2009-12-07 16:13:01 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
|
|
|
2011 Giovanni Di Sirio.
|
2009-12-07 16:13:01 +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-02-06 16:17:30 +00:00
|
|
|
* @file pwm.c
|
|
|
|
* @brief PWM Driver code.
|
2010-08-05 10:30:37 +00:00
|
|
|
*
|
2009-12-07 16:13:01 +00:00
|
|
|
* @addtogroup PWM
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#if HAL_USE_PWM || defined(__DOXYGEN__)
|
2009-12-08 08:47:14 +00:00
|
|
|
|
2009-12-29 11:12:05 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-12-07 16:13:01 +00:00
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief PWM Driver initialization.
|
2010-12-18 08:31:56 +00:00
|
|
|
* @note This function is implicitly invoked by @p halInit(), there is
|
|
|
|
* no need to explicitly initialize the driver.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @init
|
2009-12-07 16:13:01 +00:00
|
|
|
*/
|
|
|
|
void pwmInit(void) {
|
|
|
|
|
|
|
|
pwm_lld_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Initializes the standard part of a @p PWMDriver structure.
|
2009-12-07 16:13:01 +00:00
|
|
|
*
|
2011-01-04 15:08:29 +00:00
|
|
|
* @param[out] pwmp pointer to a @p PWMDriver object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @init
|
2009-12-07 16:13:01 +00:00
|
|
|
*/
|
|
|
|
void pwmObjectInit(PWMDriver *pwmp) {
|
|
|
|
|
2011-03-08 10:09:57 +00:00
|
|
|
pwmp->state = PWM_STOP;
|
|
|
|
pwmp->config = NULL;
|
2010-10-11 11:48:03 +00:00
|
|
|
#if defined(PWM_DRIVER_EXT_INIT_HOOK)
|
|
|
|
PWM_DRIVER_EXT_INIT_HOOK(pwmp);
|
|
|
|
#endif
|
2009-12-07 16:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Configures and activates the PWM peripheral.
|
2009-12-07 16:13:01 +00:00
|
|
|
*
|
2009-12-13 13:37:06 +00:00
|
|
|
* @param[in] pwmp pointer to a @p PWMDriver object
|
|
|
|
* @param[in] config pointer to a @p PWMConfig object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-12-07 16:13:01 +00:00
|
|
|
*/
|
|
|
|
void pwmStart(PWMDriver *pwmp, const PWMConfig *config) {
|
|
|
|
|
|
|
|
chDbgCheck((pwmp != NULL) && (config != NULL), "pwmStart");
|
|
|
|
|
|
|
|
chSysLock();
|
2011-03-08 10:09:57 +00:00
|
|
|
chDbgAssert((pwmp->state == PWM_STOP) || (pwmp->state == PWM_READY),
|
2010-10-13 11:45:07 +00:00
|
|
|
"pwmStart(), #1", "invalid state");
|
2011-03-08 10:09:57 +00:00
|
|
|
pwmp->config = config;
|
2009-12-07 16:13:01 +00:00
|
|
|
pwm_lld_start(pwmp);
|
2011-03-08 10:09:57 +00:00
|
|
|
pwmp->state = PWM_READY;
|
2009-12-07 16:13:01 +00:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Deactivates the PWM peripheral.
|
2009-12-07 16:13:01 +00:00
|
|
|
*
|
2009-12-13 13:37:06 +00:00
|
|
|
* @param[in] pwmp pointer to a @p PWMDriver object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-12-07 16:13:01 +00:00
|
|
|
*/
|
|
|
|
void pwmStop(PWMDriver *pwmp) {
|
|
|
|
|
|
|
|
chDbgCheck(pwmp != NULL, "pwmStop");
|
|
|
|
|
|
|
|
chSysLock();
|
2011-03-08 10:09:57 +00:00
|
|
|
chDbgAssert((pwmp->state == PWM_STOP) || (pwmp->state == PWM_READY),
|
2010-10-13 11:45:07 +00:00
|
|
|
"pwmStop(), #1", "invalid state");
|
2009-12-07 16:13:01 +00:00
|
|
|
pwm_lld_stop(pwmp);
|
2011-03-08 10:09:57 +00:00
|
|
|
pwmp->state = PWM_STOP;
|
2009-12-07 16:13:01 +00:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Enables a PWM channel.
|
2010-08-05 10:30:37 +00:00
|
|
|
* @details Programs (or reprograms) a PWM channel.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2009-12-13 13:37:06 +00:00
|
|
|
* @param[in] pwmp pointer to a @p PWMDriver object
|
2010-11-21 13:45:22 +00:00
|
|
|
* @param[in] channel PWM channel identifier (0...PWM_CHANNELS-1)
|
2010-11-21 21:31:35 +00:00
|
|
|
* @param[in] width PWM pulse width as clock pulses number
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-12-07 16:13:01 +00:00
|
|
|
*/
|
|
|
|
void pwmEnableChannel(PWMDriver *pwmp,
|
|
|
|
pwmchannel_t channel,
|
|
|
|
pwmcnt_t width) {
|
|
|
|
|
|
|
|
chDbgCheck((pwmp != NULL) && (channel < PWM_CHANNELS),
|
|
|
|
"pwmEnableChannel");
|
|
|
|
|
|
|
|
chSysLock();
|
2011-03-08 10:09:57 +00:00
|
|
|
chDbgAssert(pwmp->state == PWM_READY,
|
2010-10-13 11:45:07 +00:00
|
|
|
"pwmEnableChannel(), #1", "not ready");
|
2010-11-21 21:31:35 +00:00
|
|
|
pwm_lld_enable_channel(pwmp, channel, width);
|
2009-12-07 16:13:01 +00:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-11-21 13:45:22 +00:00
|
|
|
* @brief Disables a PWM channel.
|
2009-12-08 07:42:49 +00:00
|
|
|
* @details The channel is disabled and its output line returned to the
|
|
|
|
* idle state.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2009-12-13 13:37:06 +00:00
|
|
|
* @param[in] pwmp pointer to a @p PWMDriver object
|
2010-11-21 13:45:22 +00:00
|
|
|
* @param[in] channel PWM channel identifier (0...PWM_CHANNELS-1)
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-12-07 16:13:01 +00:00
|
|
|
*/
|
|
|
|
void pwmDisableChannel(PWMDriver *pwmp, pwmchannel_t channel) {
|
|
|
|
|
|
|
|
chDbgCheck((pwmp != NULL) && (channel < PWM_CHANNELS),
|
|
|
|
"pwmEnableChannel");
|
|
|
|
|
|
|
|
chSysLock();
|
2011-03-08 10:09:57 +00:00
|
|
|
chDbgAssert(pwmp->state == PWM_READY,
|
2010-10-13 11:45:07 +00:00
|
|
|
"pwmDisableChannel(), #1", "not ready");
|
2010-11-21 21:31:35 +00:00
|
|
|
pwm_lld_disable_channel(pwmp, channel);
|
2009-12-07 16:13:01 +00:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#endif /* HAL_USE_PWM */
|
2009-12-08 08:47:14 +00:00
|
|
|
|
2009-12-07 16:13:01 +00:00
|
|
|
/** @} */
|