Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hamilton xtimer improvement #44

Open
wants to merge 4 commits into
base: hamilton-xtimer-improvement
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpu/samd21/periph/pm.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include "periph/pm.h"
#include "xtimer.h"

#define ENABLE_DEBUG (0)
#include "debug.h"
Expand Down Expand Up @@ -54,6 +55,9 @@ enum system_sleepmode {
void pm_set(unsigned mode)
{
int deep = 0;
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
xtimer_sync = false;
#endif

switch (mode) {
case 0:
Expand Down
28 changes: 16 additions & 12 deletions sys/include/xtimer/implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern volatile uint32_t _xtimer_high_cnt;
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
extern volatile uint32_t prev_s;
extern volatile uint32_t prev_x;
extern volatile bool xtimer_sync;
#endif


Expand Down Expand Up @@ -79,14 +80,13 @@ static inline uint32_t _xtimer_lltimer_mask(uint32_t val)
*/
uint64_t _xtimer_now64(void);
int _xtimer_set_absolute(xtimer_t *timer, uint32_t target, uint32_t now);
void _xtimer_set64(xtimer_t *timer, uint32_t offset, uint32_t long_offset);
void _xtimer_set(xtimer_t *timer, uint32_t offset);
void _xtimer_set64(xtimer_t *timer, uint32_t offset, uint32_t long_offset);
void _xtimer_periodic_wakeup(uint32_t *last_wakeup, uint32_t period);
void _xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
void _xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
void _xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
void _xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
void _xtimer_set(xtimer_t *timer, uint32_t offset);
int _xtimer_msg_receive_timeout(msg_t *msg, uint32_t ticks);
int _xtimer_msg_receive_timeout64(msg_t *msg, uint64_t ticks);

Expand Down Expand Up @@ -123,19 +123,22 @@ static inline uint32_t _xtimer_now(void)
return latched_high_cnt | now;
#else
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
uint64_t diff_s;
uint32_t now_s;
if (!xtimer_sync) {
prev_x = _xtimer_lltimer_now();
prev_s = _stimer_lltimer_now();
xtimer_sync = true;
return prev_x;
} else {
uint64_t diff_s;
uint32_t now_s;

do {
now_s = _stimer_lltimer_now();
if (now_s >= prev_s) {
do {
now_s = _stimer_lltimer_now();
diff_s = now_s - prev_s;
} else {
diff_s = (0xFFFFFFFF-prev_s) + now_s;
}
} while (diff_s < STIMER_HZ/XTIMER_HZ);
} while (diff_s < STIMER_HZ/XTIMER_HZ);

return _xtimer_lltimer_mask(prev_x + (uint32_t)(diff_s*XTIMER_HZ/STIMER_HZ));
return _xtimer_lltimer_mask(prev_x + (uint32_t)(diff_s*XTIMER_HZ/STIMER_HZ));
}
#else
return _xtimer_lltimer_now();
#endif
Expand Down Expand Up @@ -177,6 +180,7 @@ static inline void _xtimer_spin(uint32_t offset) {
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
prev_x = _xtimer_lltimer_now();
prev_s = _stimer_lltimer_now();
xtimer_sync = true;
#endif
}

Expand Down
8 changes: 8 additions & 0 deletions sys/xtimer/xtimer_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ volatile uint32_t _xtimer_high_cnt = 0;
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
volatile uint32_t prev_s = 0xffffffff;
volatile uint32_t prev_x = 0xffffffff;
volatile bool xtimer_sync = false;
#endif

static inline void xtimer_spin_until(uint32_t value);
Expand Down Expand Up @@ -73,6 +74,7 @@ static inline void xtimer_spin_until(uint32_t target) {
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
prev_s = _stimer_lltimer_now();
prev_x = _xtimer_lltimer_now();
xtimer_sync = true;
#endif
}

Expand All @@ -88,6 +90,7 @@ void xtimer_init(void)
timer_init(STIMER_DEV, STIMER_HZ, _periph_timer_callback, NULL);
prev_s = _stimer_lltimer_now();
prev_x = _xtimer_lltimer_now();
xtimer_sync = true;
#endif
/* register initial overflow tick */
_lltimer_set(0xFFFFFFFF);
Expand Down Expand Up @@ -511,6 +514,7 @@ static void _timer_callback(void)
now = reference;
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
prev_x = now;
xtimer_sync = true;
#endif

overflow:
Expand Down Expand Up @@ -570,6 +574,7 @@ static void _timer_callback(void)
now = _xtimer_lltimer_now();
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
prev_x = now;
xtimer_sync = true;
#endif
goto overflow;
}
Expand All @@ -586,6 +591,7 @@ static void _timer_callback(void)
now = _xtimer_lltimer_now();
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
prev_x = now;
xtimer_sync = true;
#endif
goto overflow;
}
Expand Down Expand Up @@ -614,6 +620,7 @@ static void _timer_callback(void)
now = _xtimer_lltimer_now();
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
prev_x = now;
xtimer_sync = true;
#endif
goto overflow;
}
Expand All @@ -630,6 +637,7 @@ static void _timer_callback(void)
now = _xtimer_lltimer_now();
#if (XTIMER_HZ < 1000000ul) && (STIMER_HZ >= 1000000ul)
prev_x = now;
xtimer_sync = true;
#endif
goto overflow;
}
Expand Down