mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-02-19 09:54:15 +01:00
Restore functions lost due to inept merging, and refactor to take advantage of them.
Address the following review feedback items: - use stdint types - avoid the use of magic numbers (define CYCCNTENA) - remove expository comment about sneakiness and corresponding code, replace with something simpler based on the API - remove commented/#if 0 code
This commit is contained in:
parent
dae4b44100
commit
0df3bb2c37
@ -33,9 +33,13 @@
|
||||
/* Project Includes */
|
||||
#include <pios.h>
|
||||
|
||||
#if defined(PIOS_INCLUDE_DELAY)
|
||||
|
||||
/* these should be defined by CMSIS, but they aren't */
|
||||
#define DWT_CTRL (*(volatile unsigned long *)0xe0001000)
|
||||
#define DWT_CYCCNT (*(volatile unsigned long *)0xe0001004)
|
||||
#define DWT_CTRL (*(volatile uint32_t *)0xe0001000)
|
||||
#define CYCCNTENA (1<<1)
|
||||
#define DWT_CYCCNT (*(volatile uint32_t *)0xe0001004)
|
||||
|
||||
|
||||
/* cycles per microsecond */
|
||||
static uint32_t us_ticks;
|
||||
@ -58,7 +62,7 @@ int32_t PIOS_DELAY_Init(void)
|
||||
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
||||
|
||||
/* enable the CPU cycle counter */
|
||||
DWT_CTRL |= 1;
|
||||
DWT_CTRL |= CYCCENTENA;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -76,23 +80,13 @@ int32_t PIOS_DELAY_Init(void)
|
||||
*/
|
||||
int32_t PIOS_DELAY_WaituS(uint16_t uS)
|
||||
{
|
||||
uint32_t deadline;
|
||||
uint16_t deadline;
|
||||
|
||||
/*
|
||||
* This logic is mildly sneaky and depends on C's casting behaviour from
|
||||
* unsigned to signed when the MSB is set.
|
||||
*
|
||||
* We also depend on the difference between the deadline and DWT_CYCCNT
|
||||
* never starting off at more than half of the counter period. Since we
|
||||
* can't be asked to wait more than 65.5ms, the counter would have to wrap
|
||||
* in 131ms (approx 32THz for the 32-bit counter) for this to be a problem.
|
||||
*
|
||||
* If we are stopped by the debugger for more than half the counter period,
|
||||
* and the cycle counter doesn't stop (it normally does), the delay will be
|
||||
* protracted.
|
||||
*/
|
||||
deadline = DWT_CYCCNT + (uS * us_ticks);
|
||||
while ((int32_t)(deadline - DWT_CYCCNT) > 0) {
|
||||
/* calculate the time at which we should stop waiting */
|
||||
deadline = PIOS_DELAY_GetuS() + uS;
|
||||
|
||||
/* and wait until that time has arrived */
|
||||
while (PIOS_DELAY_DiffuS(deadline) < 0) {
|
||||
}
|
||||
|
||||
/* No error */
|
||||
@ -102,10 +96,6 @@ int32_t PIOS_DELAY_WaituS(uint16_t uS)
|
||||
/**
|
||||
* Waits for a specific number of mS
|
||||
*
|
||||
* If FreeRTOS is configured, and the delay is longer than a tick, wait whole
|
||||
* ticks using the RTOS. Fractional remainders or periods shorter than a tick
|
||||
* are busy-waited.
|
||||
*
|
||||
* Example:<BR>
|
||||
* \code
|
||||
* // Wait for 500 mS
|
||||
@ -116,15 +106,7 @@ int32_t PIOS_DELAY_WaituS(uint16_t uS)
|
||||
*/
|
||||
int32_t PIOS_DELAY_WaitmS(uint16_t mS)
|
||||
{
|
||||
#if 0 // XXX cannot do this if the scheduler hasn't started yet...
|
||||
#ifdef PIOS_INCLUDE_FREERTOS
|
||||
if (mS > portTICK_RATE_MS) {
|
||||
vTaskDelay(mS / portTICK_RATE_MS);
|
||||
mS = mS % portTICK_RATE_MS;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
for (int i = 0; i < mS; i++) {
|
||||
while (mS--) {
|
||||
PIOS_DELAY_WaituS(1000);
|
||||
}
|
||||
|
||||
@ -132,6 +114,29 @@ int32_t PIOS_DELAY_WaitmS(uint16_t mS)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Query the Delay timer for the current uS
|
||||
* @return A microsecond value
|
||||
*/
|
||||
uint16_t PIOS_DELAY_GetuS()
|
||||
{
|
||||
return DWT_CYCCNT / us_ticks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compute the difference between now and a reference time
|
||||
* @param[in] the reference time to compare now to
|
||||
* @return The number of uS since the delay
|
||||
*
|
||||
* @note the user is responsible for worrying about rollover on the 16 bit uS counter
|
||||
*/
|
||||
int32_t PIOS_DELAY_DiffuS(uint16_t ref)
|
||||
{
|
||||
int32_t ret_t = ref;
|
||||
return (int16_t) (PIOS_DELAY_GetuS() - ret_t);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
Loading…
x
Reference in New Issue
Block a user