2013-04-05 22:46:56 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file pios_delay.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
2013-05-19 16:37:30 +02:00
|
|
|
* Parts by Thorsten Klose (tk@midibox.org) (tk@midibox.org)
|
|
|
|
* @brief Delay Functions
|
|
|
|
* - Provides a micro-second granular delay using a TIM
|
2013-04-05 22:46:56 +02:00
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
* @defgroup PIOS_DELAY Delay Functions
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* This program 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
|
2013-04-05 22:46:56 +02:00
|
|
|
* (at your option) any later version.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* This program 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
|
2013-04-05 22:46:56 +02:00
|
|
|
* for more details.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2013-04-05 22:46:56 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Project Includes */
|
|
|
|
#include "pios.h"
|
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_DELAY)
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Initialises the Timer used by PIOS_DELAY functions<BR>
|
|
|
|
* This is called from pios.c as part of the main() function
|
|
|
|
* at system start up.
|
|
|
|
* \return < 0 if initialisation failed
|
|
|
|
*/
|
2013-04-05 22:46:56 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
int32_t PIOS_DELAY_Init(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// stub
|
2013-04-05 22:46:56 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* No error */
|
|
|
|
return 0;
|
2013-04-05 22:46:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Waits for a specific number of uS<BR>
|
|
|
|
* Example:<BR>
|
|
|
|
* \code
|
|
|
|
* // Wait for 500 uS
|
|
|
|
* PIOS_DELAY_Wait_uS(500);
|
|
|
|
* \endcode
|
|
|
|
* \param[in] uS delay (1..65535 microseconds)
|
|
|
|
* \return < 0 on errors
|
|
|
|
*/
|
2012-03-22 18:50:33 +01:00
|
|
|
int32_t PIOS_DELAY_WaituS(uint32_t uS)
|
2013-04-05 22:46:56 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct timespec wait, rest;
|
|
|
|
|
|
|
|
wait.tv_sec = 0;
|
|
|
|
wait.tv_nsec = 1000 * uS;
|
|
|
|
while (nanosleep(&wait, &rest) != 0) {
|
|
|
|
wait = rest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No error */
|
|
|
|
return 0;
|
2013-04-05 22:46:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Waits for a specific number of mS<BR>
|
|
|
|
* Example:<BR>
|
|
|
|
* \code
|
|
|
|
* // Wait for 500 mS
|
|
|
|
* PIOS_DELAY_Wait_mS(500);
|
|
|
|
* \endcode
|
|
|
|
* \param[in] mS delay (1..65535 milliseconds)
|
|
|
|
* \return < 0 on errors
|
|
|
|
*/
|
2012-03-22 18:50:33 +01:00
|
|
|
int32_t PIOS_DELAY_WaitmS(uint32_t mS)
|
2013-04-05 22:46:56 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// for(int i = 0; i < mS; i++) {
|
|
|
|
// PIOS_DELAY_WaituS(1000);
|
|
|
|
static struct timespec wait, rest;
|
|
|
|
|
|
|
|
wait.tv_sec = mS / 1000;
|
|
|
|
wait.tv_nsec = (mS % 1000) * 1000000;
|
|
|
|
while (nanosleep(&wait, &rest) != 0) {
|
|
|
|
wait = rest;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
/* No error */
|
|
|
|
return 0;
|
2013-04-05 22:46:56 +02:00
|
|
|
}
|
|
|
|
|
2012-03-22 18:50:33 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Query the Delay timer for the current uS
|
2012-03-22 18:50:33 +01:00
|
|
|
* @return A microsecond value
|
|
|
|
*/
|
|
|
|
uint32_t PIOS_DELAY_GetuS()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct timespec current;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, ¤t);
|
|
|
|
return (current.tv_sec * 1000000) + (current.tv_nsec / 1000);
|
2012-03-22 18:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Calculate time in microseconds since a previous time
|
|
|
|
* @param[in] t previous time
|
|
|
|
* @return time in us since previous time t.
|
|
|
|
*/
|
|
|
|
uint32_t PIOS_DELAY_GetuSSince(uint32_t t)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return PIOS_DELAY_GetuS() - t;
|
2012-03-22 18:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the raw delay timer, useful for timing
|
|
|
|
* @return Unitless value (uint32 wrap around)
|
|
|
|
*/
|
|
|
|
uint32_t PIOS_DELAY_GetRaw()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return PIOS_DELAY_GetuS();
|
2012-03-22 18:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Compare to raw times to and convert to us
|
2012-03-22 18:50:33 +01:00
|
|
|
* @return A microsecond value
|
|
|
|
*/
|
|
|
|
uint32_t PIOS_DELAY_DiffuS(uint32_t raw)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return PIOS_DELAY_GetuS() - raw;
|
2012-03-22 18:50:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
#endif /* if defined(PIOS_INCLUDE_DELAY) */
|