mirror of
https://github.com/paparazzi/paparazzi.git
synced 2026-05-22 12:28:03 +08:00
[sys_time] some performance improvements
stm32 now uses AHB_CLK / 8 as clock input for systick
This commit is contained in:
@@ -138,13 +138,17 @@ void sys_time_arch_init( void ) {
|
||||
// 12 hours at 100khz
|
||||
//
|
||||
static inline void sys_tick_irq_handler(void) {
|
||||
|
||||
static const uint32_t ticks_resolution = SYS_TIME_RESOLUTION_CPU_TICKS;
|
||||
static const uint32_t ticks_per_sec = CPU_TICKS_OF_SEC(1.0);
|
||||
|
||||
/* set match register for next interrupt */
|
||||
T0MR0 += SYS_TIME_RESOLUTION_CPU_TICKS - 1;
|
||||
T0MR0 += ticks_resolution - 1;
|
||||
|
||||
sys_time.nb_tick++;
|
||||
sys_time.nb_sec_rem += SYS_TIME_RESOLUTION_CPU_TICKS;
|
||||
if (sys_time.nb_sec_rem >= CPU_TICKS_PER_SEC) {
|
||||
sys_time.nb_sec_rem -= CPU_TICKS_PER_SEC;
|
||||
if (sys_time.nb_sec_rem >= ticks_per_sec) {
|
||||
sys_time.nb_sec_rem -= ticks_per_sec;
|
||||
sys_time.nb_sec++;
|
||||
#ifdef SYS_TIME_LED
|
||||
LED_TOGGLE(SYS_TIME_LED);
|
||||
|
||||
@@ -34,10 +34,13 @@ void sys_time_arch_init( void ) {
|
||||
|
||||
void sys_tick_handler( void ) {
|
||||
|
||||
static const unsigned int ticks_resolution = SYS_TIME_RESOLUTION_CPU_TICKS;
|
||||
static const unsigned int ticks_per_sec = CPU_TICKS_OF_SEC(1.0);
|
||||
|
||||
sys_time.nb_tick++;
|
||||
sys_time.nb_sec_rem += SYS_TIME_RESOLUTION_CPU_TICKS;
|
||||
if (sys_time.nb_sec_rem >= CPU_TICKS_PER_SEC) {
|
||||
sys_time.nb_sec_rem -= CPU_TICKS_PER_SEC;
|
||||
sys_time.nb_sec_rem += ticks_resolution;
|
||||
if (sys_time.nb_sec_rem >= ticks_per_sec) {
|
||||
sys_time.nb_sec_rem -= ticks_per_sec;
|
||||
sys_time.nb_sec++;
|
||||
}
|
||||
for (unsigned int i=0; i<SYS_TIME_NB_TIMER; i++) {
|
||||
|
||||
@@ -41,7 +41,10 @@
|
||||
* therefore it activates every n+1 clock ticks.
|
||||
*/
|
||||
void sys_time_arch_init( void ) {
|
||||
systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB);
|
||||
/* 72MHz / 8 => 9000000 counts per second */
|
||||
systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8);
|
||||
|
||||
/* 8999 would be one interrupt every 1ms */
|
||||
systick_set_reload(SYS_TIME_RESOLUTION_CPU_TICKS-1);
|
||||
|
||||
systick_interrupt_enable();
|
||||
@@ -56,10 +59,13 @@ void sys_time_arch_init( void ) {
|
||||
//
|
||||
void sys_tick_handler(void) {
|
||||
|
||||
static const uint32_t ticks_resolution = SYS_TIME_RESOLUTION_CPU_TICKS;
|
||||
static const uint32_t ticks_per_sec = CPU_TICKS_OF_SEC(1.0);
|
||||
|
||||
sys_time.nb_tick++;
|
||||
sys_time.nb_sec_rem += SYS_TIME_RESOLUTION_CPU_TICKS;
|
||||
if (sys_time.nb_sec_rem >= CPU_TICKS_PER_SEC) {
|
||||
sys_time.nb_sec_rem -= CPU_TICKS_PER_SEC;
|
||||
sys_time.nb_sec_rem += ticks_resolution;
|
||||
if (sys_time.nb_sec_rem >= ticks_per_sec) {
|
||||
sys_time.nb_sec_rem -= ticks_per_sec;
|
||||
sys_time.nb_sec++;
|
||||
#ifdef SYS_TIME_LED
|
||||
LED_TOGGLE(SYS_TIME_LED);
|
||||
|
||||
@@ -41,12 +41,12 @@
|
||||
|
||||
extern void sys_tick_handler(void);
|
||||
|
||||
#define CPU_TICKS_OF_SEC(s) (uint32_t)((s) * AHB_CLK + 0.5)
|
||||
#define SIGNED_CPU_TICKS_OF_SEC(s) (int32_t)((s) * AHB_CLK + 0.5)
|
||||
#define CPU_TICKS_OF_SEC(s) (uint32_t)((s) * AHB_CLK / 8 + 0.5)
|
||||
#define SIGNED_CPU_TICKS_OF_SEC(s) (int32_t)((s) * AHB_CLK / 8 + 0.5)
|
||||
|
||||
#define SEC_OF_CPU_TICKS(t) ((t) / AHB_CLK)
|
||||
#define MSEC_OF_CPU_TICKS(t) ((t) / (AHB_CLK/1000))
|
||||
#define USEC_OF_CPU_TICKS(t) ((t) / (AHB_CLK/1000000))
|
||||
#define SEC_OF_CPU_TICKS(t) ((t) / AHB_CLK * 8)
|
||||
#define MSEC_OF_CPU_TICKS(t) ((t) / AHB_CLK * 8000)
|
||||
#define USEC_OF_CPU_TICKS(t) ((t) / AHB_CLK * 8000000)
|
||||
|
||||
#define GET_CUR_TIME_USEC() (sys_time.nb_sec * 1000000 + \
|
||||
USEC_OF_CPU_TICKS(sys_time.nb_sec_rem) + \
|
||||
@@ -58,11 +58,25 @@ extern void sys_tick_handler(void);
|
||||
|
||||
|
||||
/** Busy wait in microseconds.
|
||||
* FIXME: directly use the SysTick->VAL here
|
||||
* Limited to ((2^24)-1)/9000000 = 1.86s
|
||||
*/
|
||||
static inline void sys_time_usleep(uint32_t us) {
|
||||
uint32_t end = GET_CUR_TIME_USEC() + us;
|
||||
while ((uint32_t)GET_CUR_TIME_USEC() < end);
|
||||
uint32_t start = systick_get_value();
|
||||
uint32_t ticks = CPU_TICKS_OF_USEC(us);
|
||||
/* cortex systick counts backwards */
|
||||
int32_t d = start - ticks;
|
||||
uint32_t end = 0;
|
||||
/* check if it wraps around zero */
|
||||
if (d >= 0) {
|
||||
end = d;
|
||||
while (systick_get_value() > end);
|
||||
} else {
|
||||
/* wait to zero */
|
||||
while (systick_get_value() > 0);
|
||||
/* wrap to reload value, wait the rest */
|
||||
end = STK_LOAD + d;
|
||||
while (systick_get_value() > end);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* SYS_TIME_ARCH_H */
|
||||
|
||||
Reference in New Issue
Block a user