2 ******************************************************************************
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Thorsten Klose (tk@midibox.org) (tk@midibox.org)
7 * @brief Delay Functions
8 * - Provides a micro-second granular delay using a TIM
9 * @see The GNU Public License (GPL) Version 3
10 * @defgroup PIOS_DELAY Delay Functions
13 *****************************************************************************/
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 /* Project Includes */
34 #if defined(PIOS_INCLUDE_DELAY)
37 * Initialises the Timer used by PIOS_DELAY functions<BR>
38 * This is called from pios.c as part of the main() function
40 * \return < 0 if initialisation failed
44 int32_t PIOS_DELAY_Init(void)
53 * Waits for a specific number of uS<BR>
57 * PIOS_DELAY_Wait_uS(500);
59 * \param[in] uS delay (1..65535 microseconds)
60 * \return < 0 on errors
62 int32_t PIOS_DELAY_WaituS(uint32_t uS
)
64 static struct timespec wait
, rest
;
67 wait
.tv_nsec
= 1000 * uS
;
68 while (nanosleep(&wait
, &rest
) != 0) {
78 * Waits for a specific number of mS<BR>
82 * PIOS_DELAY_Wait_mS(500);
84 * \param[in] mS delay (1..65535 milliseconds)
85 * \return < 0 on errors
87 int32_t PIOS_DELAY_WaitmS(uint32_t mS
)
89 // for(int i = 0; i < mS; i++) {
90 // PIOS_DELAY_WaituS(1000);
91 static struct timespec wait
, rest
;
93 wait
.tv_sec
= mS
/ 1000;
94 wait
.tv_nsec
= (mS
% 1000) * 1000000;
95 while (nanosleep(&wait
, &rest
) != 0) {
105 * @brief Query the Delay timer for the current uS
106 * @return A microsecond value
108 uint32_t PIOS_DELAY_GetuS()
110 static struct timespec current
;
112 clock_gettime(CLOCK_REALTIME
, ¤t
);
113 return (current
.tv_sec
* 1000000) + (current
.tv_nsec
/ 1000);
117 * @brief Calculate time in microseconds since a previous time
118 * @param[in] t previous time
119 * @return time in us since previous time t.
121 uint32_t PIOS_DELAY_GetuSSince(uint32_t t
)
123 return PIOS_DELAY_GetuS() - t
;
127 * @brief Get the raw delay timer, useful for timing
128 * @return Unitless value (uint32 wrap around)
130 uint32_t PIOS_DELAY_GetRaw()
132 return PIOS_DELAY_GetuS();
136 * @brief Compare to raw times to and convert to us
137 * @return A microsecond value
139 uint32_t PIOS_DELAY_DiffuS(uint32_t raw
)
141 return PIOS_DELAY_GetuS() - raw
;
145 #endif /* if defined(PIOS_INCLUDE_DELAY) */