mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-02-21 11:54:15 +01:00
Merge remote branch 'origin/msmith-delay-timer' into next
This commit is contained in:
commit
b70d5ae148
@ -67,10 +67,6 @@ int32_t PIOS_DELAY_WaituS(uint16_t uS)
|
|||||||
while (!nanosleep(&wait,&rest)) {
|
while (!nanosleep(&wait,&rest)) {
|
||||||
wait=rest;
|
wait=rest;
|
||||||
}
|
}
|
||||||
//uint16_t start = PIOS_DELAY_TIMER->CNT;
|
|
||||||
|
|
||||||
/* Note that this event works on 16bit counter wrap-arounds */
|
|
||||||
//while((uint16_t)(PIOS_DELAY_TIMER->CNT - start) <= uS);
|
|
||||||
|
|
||||||
/* No error */
|
/* No error */
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -62,11 +62,7 @@ int32_t PIOS_DELAY_Init(void)
|
|||||||
int32_t PIOS_DELAY_WaituS(uint16_t uS)
|
int32_t PIOS_DELAY_WaituS(uint16_t uS)
|
||||||
{
|
{
|
||||||
Sleep(uS/1000);
|
Sleep(uS/1000);
|
||||||
//uint16_t start = PIOS_DELAY_TIMER->CNT;
|
|
||||||
/* Note that this event works on 16bit counter wrap-arounds */
|
|
||||||
//while((uint16_t)(PIOS_DELAY_TIMER->CNT - start) <= uS);
|
|
||||||
|
|
||||||
/* No error */
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,12 +79,6 @@ TIM8 | | | |
|
|||||||
#define PIOS_LED_PINS { PIOS_LED_LED1_GPIO_PIN }
|
#define PIOS_LED_PINS { PIOS_LED_LED1_GPIO_PIN }
|
||||||
#define PIOS_LED_CLKS { PIOS_LED_LED1_GPIO_CLK }
|
#define PIOS_LED_CLKS { PIOS_LED_LED1_GPIO_CLK }
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
// Delay Timer
|
|
||||||
//-------------------------
|
|
||||||
#define PIOS_DELAY_TIMER TIM2
|
|
||||||
#define PIOS_DELAY_TIMER_RCC_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE)
|
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
// System Settings
|
// System Settings
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
@ -91,12 +91,6 @@ TIM4 | RC In 1 | Servo 3 | Servo 2 | Servo 1
|
|||||||
#define PIOS_LED_PINS { PIOS_LED_LED1_GPIO_PIN }
|
#define PIOS_LED_PINS { PIOS_LED_LED1_GPIO_PIN }
|
||||||
#define PIOS_LED_CLKS { PIOS_LED_LED1_GPIO_CLK }
|
#define PIOS_LED_CLKS { PIOS_LED_LED1_GPIO_CLK }
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
// Delay Timer
|
|
||||||
//-------------------------
|
|
||||||
#define PIOS_DELAY_TIMER TIM3
|
|
||||||
#define PIOS_DELAY_TIMER_RCC_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE)
|
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
// System Settings
|
// System Settings
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
@ -117,14 +117,6 @@ TIM4 | STOPWATCH |
|
|||||||
#define TX_LED_OFF PIOS_LED_Off(LED4)
|
#define TX_LED_OFF PIOS_LED_Off(LED4)
|
||||||
#define TX_LED_TOGGLE PIOS_LED_Toggle(LED4)
|
#define TX_LED_TOGGLE PIOS_LED_Toggle(LED4)
|
||||||
|
|
||||||
// *****************************************************************
|
|
||||||
// Delay Timer
|
|
||||||
|
|
||||||
//#define PIOS_DELAY_TIMER TIM2
|
|
||||||
//#define PIOS_DELAY_TIMER_RCC_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE)
|
|
||||||
#define PIOS_DELAY_TIMER TIM1
|
|
||||||
#define PIOS_DELAY_TIMER_RCC_FUNC RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE)
|
|
||||||
|
|
||||||
// *****************************************************************
|
// *****************************************************************
|
||||||
// Timer interrupt
|
// Timer interrupt
|
||||||
|
|
||||||
|
@ -144,12 +144,6 @@ extern uint32_t pios_com_aux_id;
|
|||||||
#define PIOS_COM_DEBUG PIOS_COM_AUX
|
#define PIOS_COM_DEBUG PIOS_COM_AUX
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
// Delay Timer
|
|
||||||
//-------------------------
|
|
||||||
#define PIOS_DELAY_TIMER TIM2
|
|
||||||
#define PIOS_DELAY_TIMER_RCC_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE)
|
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
// System Settings
|
// System Settings
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
@ -174,12 +174,6 @@ extern uint32_t pios_com_sbus_id;
|
|||||||
#define PIOS_COM_SBUS (pios_com_sbus_id)
|
#define PIOS_COM_SBUS (pios_com_sbus_id)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
// Delay Timer
|
|
||||||
//-------------------------
|
|
||||||
#define PIOS_DELAY_TIMER TIM2
|
|
||||||
#define PIOS_DELAY_TIMER_RCC_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE)
|
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
// System Settings
|
// System Settings
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
@ -7,10 +7,10 @@
|
|||||||
* @{
|
* @{
|
||||||
*
|
*
|
||||||
* @file pios_delay.c
|
* @file pios_delay.c
|
||||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
* @author Michael Smith Copyright (C) 2011
|
||||||
* Parts by Thorsten Klose (tk@midibox.org) (tk@midibox.org)
|
|
||||||
* @brief Delay Functions
|
* @brief Delay Functions
|
||||||
* - Provides a micro-second granular delay using a TIM
|
* - Provides a micro-second granular delay using the CPU
|
||||||
|
* cycle counter.
|
||||||
* @see The GNU Public License (GPL) Version 3
|
* @see The GNU Public License (GPL) Version 3
|
||||||
*
|
*
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
@ -31,72 +31,97 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* Project Includes */
|
/* Project Includes */
|
||||||
#include "pios.h"
|
#include <pios.h>
|
||||||
|
|
||||||
#if defined(PIOS_INCLUDE_DELAY)
|
#if defined(PIOS_INCLUDE_DELAY)
|
||||||
|
|
||||||
|
/* these should be defined by CMSIS, but they aren't */
|
||||||
|
#define DWT_CTRL (*(volatile uint32_t *)0xe0001000)
|
||||||
|
#define CYCCNTENA (1<<0)
|
||||||
|
#define DWT_CYCCNT (*(volatile uint32_t *)0xe0001004)
|
||||||
|
|
||||||
|
|
||||||
|
/* cycles per microsecond */
|
||||||
|
static uint32_t us_ticks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialises the Timer used by PIOS_DELAY functions<BR>
|
* Initialises the Timer used by PIOS_DELAY functions.
|
||||||
* This is called from pios.c as part of the main() function
|
*
|
||||||
* at system start up.
|
* \return always zero (success)
|
||||||
* \return < 0 if initialisation failed
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
int32_t PIOS_DELAY_Init(void)
|
int32_t PIOS_DELAY_Init(void)
|
||||||
{
|
{
|
||||||
/* Enable timer clock */
|
RCC_ClocksTypeDef clocks;
|
||||||
PIOS_DELAY_TIMER_RCC_FUNC;
|
|
||||||
|
|
||||||
/* Time base configuration */
|
/* compute the number of system clocks per microsecond */
|
||||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
RCC_GetClocksFreq(&clocks);
|
||||||
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
|
us_ticks = clocks.SYSCLK_Frequency / 1000000;
|
||||||
TIM_TimeBaseStructure.TIM_Period = 65535; // maximum value
|
PIOS_DEBUG_Assert(us_ticks > 1);
|
||||||
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // for 1 uS accuracy fixed to 72Mhz
|
|
||||||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
||||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
||||||
TIM_TimeBaseInit(PIOS_DELAY_TIMER, &TIM_TimeBaseStructure);
|
|
||||||
|
|
||||||
/* Enable counter */
|
/* turn on access to the DWT registers */
|
||||||
TIM_Cmd(PIOS_DELAY_TIMER, ENABLE);
|
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
||||||
|
|
||||||
|
/* enable the CPU cycle counter */
|
||||||
|
DWT_CTRL |= CYCCNTENA;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits for a specific number of uS
|
||||||
|
*
|
||||||
|
* Example:<BR>
|
||||||
|
* \code
|
||||||
|
* // Wait for 500 uS
|
||||||
|
* PIOS_DELAY_Wait_uS(500);
|
||||||
|
* \endcode
|
||||||
|
* \param[in] uS delay
|
||||||
|
* \return < 0 on errors
|
||||||
|
*/
|
||||||
|
int32_t PIOS_DELAY_WaituS(uint32_t uS)
|
||||||
|
{
|
||||||
|
uint32_t elapsed = 0;
|
||||||
|
uint32_t last_count = DWT_CYCCNT;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
uint32_t current_count = DWT_CYCCNT;
|
||||||
|
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 */
|
/* No error */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Waits for a specific number of uS<BR>
|
* Waits for a specific number of mS
|
||||||
* Example:<BR>
|
*
|
||||||
* \code
|
* Example:<BR>
|
||||||
* // Wait for 500 uS
|
* \code
|
||||||
* PIOS_DELAY_Wait_uS(500);
|
* // Wait for 500 mS
|
||||||
* \endcode
|
* PIOS_DELAY_Wait_mS(500);
|
||||||
* \param[in] uS delay (1..65535 microseconds)
|
* \endcode
|
||||||
* \return < 0 on errors
|
* \param[in] mS delay (1..65535 milliseconds)
|
||||||
*/
|
* \return < 0 on errors
|
||||||
int32_t PIOS_DELAY_WaituS(uint16_t uS)
|
*/
|
||||||
|
int32_t PIOS_DELAY_WaitmS(uint32_t mS)
|
||||||
{
|
{
|
||||||
uint16_t start = PIOS_DELAY_TIMER->CNT;
|
while (mS--) {
|
||||||
|
|
||||||
/* Note that this event works on 16bit counter wrap-arounds */
|
|
||||||
while ((uint16_t) (PIOS_DELAY_TIMER->CNT - start) <= uS) ;
|
|
||||||
|
|
||||||
/* No error */
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
int32_t PIOS_DELAY_WaitmS(uint16_t mS)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < mS; i++) {
|
|
||||||
PIOS_DELAY_WaituS(1000);
|
PIOS_DELAY_WaituS(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,22 +133,9 @@ int32_t PIOS_DELAY_WaitmS(uint16_t mS)
|
|||||||
* @brief Query the Delay timer for the current uS
|
* @brief Query the Delay timer for the current uS
|
||||||
* @return A microsecond value
|
* @return A microsecond value
|
||||||
*/
|
*/
|
||||||
uint16_t PIOS_DELAY_GetuS()
|
uint32_t PIOS_DELAY_GetuS()
|
||||||
{
|
{
|
||||||
return PIOS_DELAY_TIMER->CNT;
|
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
|
#endif
|
||||||
|
@ -34,10 +34,9 @@
|
|||||||
|
|
||||||
/* Public Functions */
|
/* Public Functions */
|
||||||
extern int32_t PIOS_DELAY_Init(void);
|
extern int32_t PIOS_DELAY_Init(void);
|
||||||
extern int32_t PIOS_DELAY_WaituS(uint16_t uS);
|
extern int32_t PIOS_DELAY_WaituS(uint32_t uS);
|
||||||
extern int32_t PIOS_DELAY_WaitmS(uint16_t mS);
|
extern int32_t PIOS_DELAY_WaitmS(uint32_t mS);
|
||||||
extern uint16_t PIOS_DELAY_GetuS();
|
extern uint32_t PIOS_DELAY_GetuS();
|
||||||
extern int32_t PIOS_DELAY_DiffuS(uint16_t ref);
|
|
||||||
|
|
||||||
#endif /* PIOS_DELAY_H */
|
#endif /* PIOS_DELAY_H */
|
||||||
|
|
||||||
|
@ -163,8 +163,8 @@ void TIMER_INT_FUNC(void)
|
|||||||
// Clear timer interrupt pending bit
|
// Clear timer interrupt pending bit
|
||||||
TIM_ClearITPendingBit(TIMER_INT_TIMER, TIM_IT_Update);
|
TIM_ClearITPendingBit(TIMER_INT_TIMER, TIM_IT_Update);
|
||||||
|
|
||||||
// random32 = UpdateCRC32(random32, PIOS_DELAY_TIMER->CNT >> 8);
|
// random32 = UpdateCRC32(random32, PIOS_DELAY_GetuS() >> 8);
|
||||||
// random32 = UpdateCRC32(random32, PIOS_DELAY_TIMER->CNT);
|
// random32 = UpdateCRC32(random32, PIOS_DELAY_GetuS());
|
||||||
|
|
||||||
uptime_ms++;
|
uptime_ms++;
|
||||||
|
|
||||||
@ -785,8 +785,8 @@ int main()
|
|||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
random32 = updateCRC32(random32, PIOS_DELAY_TIMER->CNT >> 8);
|
random32 = updateCRC32(random32, PIOS_DELAY_GetuS() >> 8);
|
||||||
random32 = updateCRC32(random32, PIOS_DELAY_TIMER->CNT);
|
random32 = updateCRC32(random32, PIOS_DELAY_GetuS());
|
||||||
|
|
||||||
if (second_tick)
|
if (second_tick)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user