ChibiOS/RT
2.6.0
gpt.c
Go to the documentation of this file.
00001 /*
00002     ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
00003                  2011,2012,2013 Giovanni Di Sirio.
00004 
00005     This file is part of ChibiOS/RT.
00006 
00007     ChibiOS/RT is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ChibiOS/RT is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 
00020                                       ---
00021 
00022     A special exception to the GPL can be applied should you wish to distribute
00023     a combined work that includes ChibiOS/RT, without being obliged to provide
00024     the source code for any proprietary components. See the file exception.txt
00025     for full details of how and when the exception can be applied.
00026 */
00027 
00028 /**
00029  * @file    gpt.c
00030  * @brief   GPT Driver code.
00031  *
00032  * @addtogroup GPT
00033  * @{
00034  */
00035 
00036 #include "ch.h"
00037 #include "hal.h"
00038 
00039 #if HAL_USE_GPT || defined(__DOXYGEN__)
00040 
00041 /*===========================================================================*/
00042 /* Driver local definitions.                                                 */
00043 /*===========================================================================*/
00044 
00045 /*===========================================================================*/
00046 /* Driver exported variables.                                                */
00047 /*===========================================================================*/
00048 
00049 /*===========================================================================*/
00050 /* Driver local variables and types.                                         */
00051 /*===========================================================================*/
00052 
00053 /*===========================================================================*/
00054 /* Driver local functions.                                                   */
00055 /*===========================================================================*/
00056 
00057 /*===========================================================================*/
00058 /* Driver exported functions.                                                */
00059 /*===========================================================================*/
00060 
00061 /**
00062  * @brief   GPT Driver initialization.
00063  * @note    This function is implicitly invoked by @p halInit(), there is
00064  *          no need to explicitly initialize the driver.
00065  *
00066  * @init
00067  */
00068 void gptInit(void) {
00069 
00070   gpt_lld_init();
00071 }
00072 
00073 /**
00074  * @brief   Initializes the standard part of a @p GPTDriver structure.
00075  *
00076  * @param[out] gptp     pointer to the @p GPTDriver object
00077  *
00078  * @init
00079  */
00080 void gptObjectInit(GPTDriver *gptp) {
00081 
00082   gptp->state  = GPT_STOP;
00083   gptp->config = NULL;
00084 }
00085 
00086 /**
00087  * @brief   Configures and activates the GPT peripheral.
00088  *
00089  * @param[in] gptp      pointer to the @p GPTDriver object
00090  * @param[in] config    pointer to the @p GPTConfig object
00091  *
00092  * @api
00093  */
00094 void gptStart(GPTDriver *gptp, const GPTConfig *config) {
00095 
00096   chDbgCheck((gptp != NULL) && (config != NULL), "ptStart");
00097 
00098   chSysLock();
00099   chDbgAssert((gptp->state == GPT_STOP) || (gptp->state == GPT_READY),
00100               "gptStart(), #1", "invalid state");
00101   gptp->config = config;
00102   gpt_lld_start(gptp);
00103   gptp->state = GPT_READY;
00104   chSysUnlock();
00105 }
00106 
00107 /**
00108  * @brief   Deactivates the GPT peripheral.
00109  *
00110  * @param[in] gptp      pointer to the @p GPTDriver object
00111  *
00112  * @api
00113  */
00114 void gptStop(GPTDriver *gptp) {
00115 
00116   chDbgCheck(gptp != NULL, "gptStop");
00117 
00118   chSysLock();
00119   chDbgAssert((gptp->state == GPT_STOP) || (gptp->state == GPT_READY),
00120               "gptStop(), #1", "invalid state");
00121   gpt_lld_stop(gptp);
00122   gptp->state = GPT_STOP;
00123   chSysUnlock();
00124 }
00125 
00126 /**
00127  * @brief   Changes the interval of GPT peripheral.
00128  * @details This function changes the interval of a running GPT unit.
00129  * @pre     The GPT unit must have been activated using @p gptStart().
00130  * @pre     The GPT unit must have been running in continuous mode using
00131  *          @p gptStartContinuous().
00132  * @post    The GPT unit interval is changed to the new value.
00133  *
00134  * @param[in] gptp      pointer to a @p GPTDriver object
00135  * @param[in] interval  new cycle time in timer ticks
00136  *
00137  * @api
00138  */
00139 void gptChangeInterval(GPTDriver *gptp, gptcnt_t interval) {
00140 
00141   chDbgCheck(gptp != NULL, "gptChangeInterval");
00142 
00143   chSysLock();
00144   chDbgAssert(gptp->state == GPT_CONTINUOUS,
00145               "gptChangeInterval(), #1", "invalid state");
00146   gptChangeIntervalI(gptp, interval);
00147   chSysUnlock();
00148 }
00149 
00150 /**
00151  * @brief   Starts the timer in continuous mode.
00152  *
00153  * @param[in] gptp      pointer to the @p GPTDriver object
00154  * @param[in] interval  period in ticks
00155  *
00156  * @api
00157  */
00158 void gptStartContinuous(GPTDriver *gptp, gptcnt_t interval) {
00159 
00160   chSysLock();
00161   gptStartContinuousI(gptp, interval);
00162   chSysUnlock();
00163 }
00164 
00165 /**
00166  * @brief   Starts the timer in continuous mode.
00167  *
00168  * @param[in] gptp      pointer to the @p GPTDriver object
00169  * @param[in] interval  period in ticks
00170  *
00171  * @iclass
00172  */
00173 void gptStartContinuousI(GPTDriver *gptp, gptcnt_t interval) {
00174 
00175   chDbgCheckClassI();
00176   chDbgCheck(gptp != NULL, "gptStartContinuousI");
00177   chDbgAssert(gptp->state == GPT_READY,
00178               "gptStartContinuousI(), #1", "invalid state");
00179 
00180   gptp->state = GPT_CONTINUOUS;
00181   gpt_lld_start_timer(gptp, interval);
00182 }
00183 
00184 /**
00185  * @brief   Starts the timer in one shot mode.
00186  *
00187  * @param[in] gptp      pointer to the @p GPTDriver object
00188  * @param[in] interval  time interval in ticks
00189  *
00190  * @api
00191  */
00192 void gptStartOneShot(GPTDriver *gptp, gptcnt_t interval) {
00193 
00194   chSysLock();
00195   gptStartOneShotI(gptp, interval);
00196   chSysUnlock();
00197 }
00198 
00199 /**
00200  * @brief   Starts the timer in one shot mode.
00201  *
00202  * @param[in] gptp      pointer to the @p GPTDriver object
00203  * @param[in] interval  time interval in ticks
00204  *
00205  * @api
00206  */
00207 void gptStartOneShotI(GPTDriver *gptp, gptcnt_t interval) {
00208 
00209   chDbgCheckClassI();
00210   chDbgCheck(gptp != NULL, "gptStartOneShotI");
00211   chDbgAssert(gptp->state == GPT_READY,
00212               "gptStartOneShotI(), #1", "invalid state");
00213 
00214   gptp->state = GPT_ONESHOT;
00215   gpt_lld_start_timer(gptp, interval);
00216 }
00217 
00218 /**
00219  * @brief   Stops the timer.
00220  *
00221  * @param[in] gptp      pointer to the @p GPTDriver object
00222  *
00223  * @api
00224  */
00225 void gptStopTimer(GPTDriver *gptp) {
00226 
00227   chSysLock();
00228   gptStopTimerI(gptp);
00229   chSysUnlock();
00230 }
00231 
00232 /**
00233  * @brief   Stops the timer.
00234  *
00235  * @param[in] gptp      pointer to the @p GPTDriver object
00236  *
00237  * @api
00238  */
00239 void gptStopTimerI(GPTDriver *gptp) {
00240 
00241   chDbgCheckClassI();
00242   chDbgCheck(gptp != NULL, "gptStopTimerI");
00243   chDbgAssert((gptp->state == GPT_READY) || (gptp->state == GPT_CONTINUOUS) ||
00244               (gptp->state == GPT_ONESHOT),
00245               "gptStopTimerI(), #1", "invalid state");
00246 
00247   gptp->state = GPT_READY;
00248   gpt_lld_stop_timer(gptp);
00249 }
00250 
00251 /**
00252  * @brief   Starts the timer in one shot mode and waits for completion.
00253  * @details This function specifically polls the timer waiting for completion
00254  *          in order to not have extra delays caused by interrupt servicing,
00255  *          this function is only recommended for short delays.
00256  * @note    The configured callback is not invoked when using this function.
00257  *
00258  * @param[in] gptp      pointer to the @p GPTDriver object
00259  * @param[in] interval  time interval in ticks
00260  *
00261  * @api
00262  */
00263 void gptPolledDelay(GPTDriver *gptp, gptcnt_t interval) {
00264 
00265   chDbgAssert(gptp->state == GPT_READY,
00266               "gptPolledDelay(), #1", "invalid state");
00267 
00268   gptp->state = GPT_ONESHOT;
00269   gpt_lld_polled_delay(gptp, interval);
00270   gptp->state = GPT_READY;
00271 }
00272 
00273 #endif /* HAL_USE_GPT */
00274 
00275 /** @} */