2010-09-27 09:28:45 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_DELAY Delay Functions
|
|
|
|
* @brief PiOS Delay functionality
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file pios_delay.c
|
2011-07-22 08:21:26 +02:00
|
|
|
* @author Michael Smith Copyright (C) 2011
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Delay Functions
|
2011-07-22 08:21:26 +02:00
|
|
|
* - Provides a micro-second granular delay using the CPU
|
|
|
|
* cycle counter.
|
2010-09-27 09:28:45 +02:00
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
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
|
2010-09-27 09:28:45 +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
|
2010-09-27 09:28:45 +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.,
|
2010-09-27 09:28:45 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2011-07-22 08:21:26 +02:00
|
|
|
#include <pios.h>
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-03-15 19:25:30 +01:00
|
|
|
#ifdef PIOS_INCLUDE_DELAY
|
2011-07-23 22:53:16 +02:00
|
|
|
|
2011-07-22 08:21:26 +02:00
|
|
|
/* cycles per microsecond */
|
|
|
|
static uint32_t us_ticks;
|
2017-03-27 15:51:43 +02:00
|
|
|
static uint32_t raw_hz;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
/**
|
2011-07-22 08:21:26 +02:00
|
|
|
* Initialises the Timer used by PIOS_DELAY functions.
|
|
|
|
*
|
|
|
|
* \return always zero (success)
|
|
|
|
*/
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
int32_t PIOS_DELAY_Init(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
RCC_ClocksTypeDef clocks;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* compute the number of system clocks per microsecond */
|
|
|
|
RCC_GetClocksFreq(&clocks);
|
|
|
|
us_ticks = clocks.SYSCLK_Frequency / 1000000;
|
|
|
|
PIOS_DEBUG_Assert(us_ticks > 1);
|
2017-03-27 15:51:43 +02:00
|
|
|
raw_hz = clocks.SYSCLK_Frequency;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* turn on access to the DWT registers */
|
|
|
|
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
2011-07-22 08:21:26 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* enable the CPU cycle counter */
|
|
|
|
DWT_CTRL |= CYCCNTENA;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-22 08:21:26 +02:00
|
|
|
* Waits for a specific number of uS
|
|
|
|
*
|
|
|
|
* Example:<BR>
|
|
|
|
* \code
|
|
|
|
* // Wait for 500 uS
|
|
|
|
* PIOS_DELAY_Wait_uS(500);
|
|
|
|
* \endcode
|
2011-07-26 05:55:48 +02:00
|
|
|
* \param[in] uS delay
|
2011-07-22 08:21:26 +02:00
|
|
|
* \return < 0 on errors
|
|
|
|
*/
|
2011-07-26 05:55:48 +02:00
|
|
|
int32_t PIOS_DELAY_WaituS(uint32_t uS)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t elapsed = 0;
|
|
|
|
uint32_t last_count = DWT_CYCCNT;
|
|
|
|
|
|
|
|
for (;;) {
|
2017-03-27 15:51:43 +02:00
|
|
|
uint32_t current_count = PIOS_DELAY_GetRaw();
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t elapsed_uS;
|
|
|
|
|
|
|
|
/* measure the time elapsed since the last time we checked */
|
|
|
|
elapsed += current_count - last_count;
|
|
|
|
last_count = current_count;
|
|
|
|
|
|
|
|
/* convert to microseconds */
|
|
|
|
elapsed_uS = elapsed / us_ticks;
|
|
|
|
if (elapsed_uS >= uS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reduce the delay by the elapsed time */
|
|
|
|
uS -= elapsed_uS;
|
|
|
|
|
|
|
|
/* keep fractional microseconds for the next iteration */
|
|
|
|
elapsed %= us_ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No error */
|
|
|
|
return 0;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-07-22 08:21:26 +02:00
|
|
|
* Waits for a specific number of mS
|
|
|
|
*
|
|
|
|
* Example:<BR>
|
|
|
|
* \code
|
|
|
|
* // Wait for 500 mS
|
|
|
|
* PIOS_DELAY_Wait_mS(500);
|
|
|
|
* \endcode
|
2011-07-29 19:50:46 +02:00
|
|
|
* \param[in] mS delay
|
2011-07-22 08:21:26 +02:00
|
|
|
* \return < 0 on errors
|
|
|
|
*/
|
2011-07-26 05:55:48 +02:00
|
|
|
int32_t PIOS_DELAY_WaitmS(uint32_t mS)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
while (mS--) {
|
|
|
|
PIOS_DELAY_WaituS(1000);
|
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/* No error */
|
|
|
|
return 0;
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2011-07-23 22:53:16 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Query the Delay timer for the current uS
|
2011-07-23 22:53:16 +02:00
|
|
|
* @return A microsecond value
|
|
|
|
*/
|
2011-07-29 19:50:46 +02:00
|
|
|
uint32_t PIOS_DELAY_GetuS(void)
|
2011-07-23 22:53:16 +02:00
|
|
|
{
|
2017-03-27 15:51:43 +02:00
|
|
|
return PIOS_DELAY_GetRaw() / us_ticks;
|
2011-07-23 22:53:16 +02:00
|
|
|
}
|
|
|
|
|
2011-07-29 19:50:46 +02: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;
|
2011-07-29 19:50:46 +02:00
|
|
|
}
|
|
|
|
|
2011-08-12 11:05:08 +02:00
|
|
|
/**
|
2017-03-27 15:51:43 +02:00
|
|
|
* @brief Get the raw delay timer frequency
|
|
|
|
* @return raw delay timer frequency in Hz
|
2011-08-12 11:05:08 +02:00
|
|
|
*/
|
2017-03-27 15:51:43 +02:00
|
|
|
uint32_t PIOS_DELAY_GetRawHz()
|
2011-08-12 11:05:08 +02:00
|
|
|
{
|
2017-03-27 15:51:43 +02:00
|
|
|
return raw_hz;
|
2011-08-12 11:05:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Compare to raw times to and convert to us
|
2011-08-12 11:05:08 +02:00
|
|
|
* @return A microsecond value
|
|
|
|
*/
|
|
|
|
uint32_t PIOS_DELAY_DiffuS(uint32_t raw)
|
|
|
|
{
|
2017-03-27 15:51:43 +02:00
|
|
|
uint32_t diff = PIOS_DELAY_GetRaw() - raw;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
return diff / us_ticks;
|
2011-08-12 11:05:08 +02:00
|
|
|
}
|
|
|
|
|
2013-03-15 19:25:30 +01:00
|
|
|
#endif /* PIOS_INCLUDE_DELAY */
|
2010-09-27 09:28:45 +02:00
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|