ChibiOS/RT Architecture - Reference Manual - Guides |
00001 /* 00002 ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio. 00003 00004 This file is part of ChibiOS/RT. 00005 00006 ChibiOS/RT is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 ChibiOS/RT is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 00019 --- 00020 00021 A special exception to the GPL can be applied should you wish to distribute 00022 a combined work that includes ChibiOS/RT, without being obliged to provide 00023 the source code for any proprietary components. See the file exception.txt 00024 for full details of how and when the exception can be applied. 00025 */ 00026 00027 /** 00028 * @file pwm.c 00029 * @brief PWM Driver code. 00030 * 00031 * @addtogroup PWM 00032 * @{ 00033 */ 00034 00035 #include "ch.h" 00036 #include "hal.h" 00037 00038 #if CH_HAL_USE_PWM || defined(__DOXYGEN__) 00039 00040 /*===========================================================================*/ 00041 /* Driver exported variables. */ 00042 /*===========================================================================*/ 00043 00044 /*===========================================================================*/ 00045 /* Driver local variables. */ 00046 /*===========================================================================*/ 00047 00048 /*===========================================================================*/ 00049 /* Driver local functions. */ 00050 /*===========================================================================*/ 00051 00052 /*===========================================================================*/ 00053 /* Driver exported functions. */ 00054 /*===========================================================================*/ 00055 00056 /** 00057 * @brief PWM Driver initialization. 00058 */ 00059 void pwmInit(void) { 00060 00061 pwm_lld_init(); 00062 } 00063 00064 /** 00065 * @brief Initializes the standard part of a @p PWMDriver structure. 00066 * 00067 * @param[in] pwmp pointer to a @p PWMDriver object 00068 */ 00069 void pwmObjectInit(PWMDriver *pwmp) { 00070 00071 pwmp->pd_state = PWM_STOP; 00072 pwmp->pd_config = NULL; 00073 #if defined(PWM_DRIVER_EXT_INIT_HOOK) 00074 PWM_DRIVER_EXT_INIT_HOOK(pwmp); 00075 #endif 00076 } 00077 00078 /** 00079 * @brief Configures and activates the PWM peripheral. 00080 * 00081 * @param[in] pwmp pointer to a @p PWMDriver object 00082 * @param[in] config pointer to a @p PWMConfig object 00083 */ 00084 void pwmStart(PWMDriver *pwmp, const PWMConfig *config) { 00085 00086 chDbgCheck((pwmp != NULL) && (config != NULL), "pwmStart"); 00087 00088 chSysLock(); 00089 chDbgAssert((pwmp->pd_state == PWM_STOP) || (pwmp->pd_state == PWM_READY), 00090 "pwmStart(), #1", "invalid state"); 00091 pwmp->pd_config = config; 00092 pwm_lld_start(pwmp); 00093 pwmp->pd_state = PWM_READY; 00094 chSysUnlock(); 00095 } 00096 00097 /** 00098 * @brief Deactivates the PWM peripheral. 00099 * 00100 * @param[in] pwmp pointer to a @p PWMDriver object 00101 */ 00102 void pwmStop(PWMDriver *pwmp) { 00103 00104 chDbgCheck(pwmp != NULL, "pwmStop"); 00105 00106 chSysLock(); 00107 chDbgAssert((pwmp->pd_state == PWM_STOP) || (pwmp->pd_state == PWM_READY), 00108 "pwmStop(), #1", "invalid state"); 00109 pwm_lld_stop(pwmp); 00110 pwmp->pd_state = PWM_STOP; 00111 chSysUnlock(); 00112 } 00113 00114 /** 00115 * @brief Enables a PWM channel. 00116 * @details Programs (or reprograms) a PWM channel. 00117 * 00118 * @param[in] pwmp pointer to a @p PWMDriver object 00119 * @param[in] channel PWM channel identifier (0...PWM_CHANNELS-1) 00120 * @param[in] width PWM pulse width as clock pulses number 00121 */ 00122 void pwmEnableChannel(PWMDriver *pwmp, 00123 pwmchannel_t channel, 00124 pwmcnt_t width) { 00125 00126 chDbgCheck((pwmp != NULL) && (channel < PWM_CHANNELS), 00127 "pwmEnableChannel"); 00128 00129 chSysLock(); 00130 chDbgAssert(pwmp->pd_state == PWM_READY, 00131 "pwmEnableChannel(), #1", "not ready"); 00132 pwm_lld_enable_channel(pwmp, channel, width); 00133 chSysUnlock(); 00134 } 00135 00136 /** 00137 * @brief Disables a PWM channel. 00138 * @details The channel is disabled and its output line returned to the 00139 * idle state. 00140 * 00141 * @param[in] pwmp pointer to a @p PWMDriver object 00142 * @param[in] channel PWM channel identifier (0...PWM_CHANNELS-1) 00143 */ 00144 void pwmDisableChannel(PWMDriver *pwmp, pwmchannel_t channel) { 00145 00146 chDbgCheck((pwmp != NULL) && (channel < PWM_CHANNELS), 00147 "pwmEnableChannel"); 00148 00149 chSysLock(); 00150 chDbgAssert(pwmp->pd_state == PWM_READY, 00151 "pwmDisableChannel(), #1", "not ready"); 00152 pwm_lld_disable_channel(pwmp, channel); 00153 chSysUnlock(); 00154 } 00155 00156 #endif /* CH_HAL_USE_PWM */ 00157 00158 /** @} */