Merged in corvusvcorax/librepilot/LP-490_insgps13state_mag_fixes (pull request #398)
[librepilot.git] / flight / pios / posix / pios_delay.c
blobd812613f1e3ef887f70c91c0580a23797d10bd27
1 /**
2 ******************************************************************************
4 * @file pios_delay.c
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
11 * @{
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
23 * for more details.
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 */
32 #include "pios.h"
34 #if defined(PIOS_INCLUDE_DELAY)
36 /**
37 * Initialises the Timer used by PIOS_DELAY functions<BR>
38 * This is called from pios.c as part of the main() function
39 * at system start up.
40 * \return < 0 if initialisation failed
42 #include <time.h>
44 int32_t PIOS_DELAY_Init(void)
46 // stub
48 /* No error */
49 return 0;
52 /**
53 * Waits for a specific number of uS<BR>
54 * Example:<BR>
55 * \code
56 * // Wait for 500 uS
57 * PIOS_DELAY_Wait_uS(500);
58 * \endcode
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;
66 wait.tv_sec = 0;
67 wait.tv_nsec = 1000 * uS;
68 while (nanosleep(&wait, &rest) != 0) {
69 wait = rest;
72 /* No error */
73 return 0;
77 /**
78 * Waits for a specific number of mS<BR>
79 * Example:<BR>
80 * \code
81 * // Wait for 500 mS
82 * PIOS_DELAY_Wait_mS(500);
83 * \endcode
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) {
96 wait = rest;
98 // }
100 /* No error */
101 return 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, &current);
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) */