[stm32] fix sys_time_usleep, sorry about that...

This commit is contained in:
Felix Ruess
2013-02-24 16:16:11 +01:00
parent a6827c4441
commit 1c56151c0e
2 changed files with 22 additions and 24 deletions
@@ -59,7 +59,7 @@ extern void sys_tick_handler(void);
static inline uint32_t get_sys_time_usec(void) {
return sys_time.nb_sec * 1000000 +
USEC_OF_CPU_TICKS(sys_time.nb_sec_rem) +
USEC_OF_CPU_TICKS(STK_LOAD - systick_get_value());
USEC_OF_CPU_TICKS(systick_get_reload() - systick_get_value());
}
/* Generic timer macros */
@@ -68,25 +68,23 @@ static inline uint32_t get_sys_time_usec(void) {
#define SysTimeTimerStop(_t) { _t = ( get_sys_time_usec() - (_t)); }
/** Busy wait in microseconds.
* Limited to ((2^24)-1)/9000000 = 1.86s
* @todo: doesn't handle wrap-around at
* 2^32 / 1000000 = 4294s = ~72min
*/
static inline void sys_time_usleep(uint32_t us) {
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);
}
/* duration and end time in SYS_TIME_TICKS */
uint32_t d_sys_ticks = SYS_TIME_TICKS_OF_USEC(us);
uint32_t end_nb_tick = sys_time.nb_tick + d_sys_ticks;
/* remainder in CPU_TICKS */
uint32_t rem_cpu_ticks = CPU_TICKS_OF_USEC(us) - d_sys_ticks * SYS_TIME_RESOLUTION_CPU_TICKS;
/* cortex systick counts backwards, end value is reload_value - remainder */
uint32_t end_cpu_ticks = systick_get_reload() - rem_cpu_ticks;
/* first wait until end time in SYS_TIME_TICKS */
while (sys_time.nb_tick < end_nb_tick);
/* then wait remaining cpu ticks */
while (systick_get_value() > end_cpu_ticks);
}
#endif /* SYS_TIME_ARCH_H */
+6 -6
View File
@@ -47,14 +47,14 @@ struct sys_time_timer {
bool_t in_use;
sys_time_cb cb;
volatile bool_t elapsed;
uint32_t end_time; ///< in SYS_TICKS
uint32_t duration; ///< in SYS_TICKS
uint32_t end_time; ///< in SYS_TIME_TICKS
uint32_t duration; ///< in SYS_TIME_TICKS
};
struct sys_time {
volatile uint32_t nb_sec; ///< full seconds since startup
volatile uint32_t nb_sec_rem; ///< remainder of second in CPU_TICKS
volatile uint32_t nb_tick; ///< SYS_TICKS since startup (with SYS_TIME_RESOLUTION)
volatile uint32_t nb_tick; ///< SYS_TIME_TICKS since startup (with SYS_TIME_RESOLUTION)
struct sys_time_timer timer[SYS_TIME_NB_TIMER];
};
@@ -116,9 +116,9 @@ static inline bool_t sys_time_check_and_ack_timer(tid_t id) {
#define SYS_TIME_TICKS_OF_NSEC(ns) SYS_TIME_TICKS_OF_SEC((ns) * 1e-9)
#define SEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION)
#define MSEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION / 1e-3)
#define USEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION / 1e-6)
#define NSEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION / 1e-9)
#define MSEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION * 1e3)
#define USEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION * 1e6)
#define NSEC_OF_SYS_TIME_TICKS(t) ((t) * SYS_TIME_RESOLUTION * 1e9)
#define USEC_OF_SEC(sec) ((sec) * 1e6)