Skip to content

Commit

Permalink
properly sync different timers
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyungsin committed Jan 5, 2018
1 parent 23c64b0 commit a6c2c52
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
2 changes: 2 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 @@ -61,6 +62,7 @@ void pm_set(unsigned mode)
* Potential Wake Up sources: asynchronous
*/
deep = 1;
xtimer_sync = false;
break;
case 1:
/* Sleep mode Idle 2
Expand Down
33 changes: 21 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 @@ -123,19 +124,26 @@ 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) {
diff_s = now_s - prev_s;
} else {
diff_s = (0xFFFFFFFF-prev_s) + now_s;
}
} while (diff_s < STIMER_HZ/XTIMER_HZ);

return _xtimer_lltimer_mask(prev_x + (uint32_t)(diff_s*XTIMER_HZ/STIMER_HZ));
do {
now_s = _stimer_lltimer_now();
if (now_s >= prev_s) {
diff_s = now_s - prev_s;
} else {
diff_s = (0xFFFFFFFF-prev_s) + now_s;
}
} while (diff_s < STIMER_HZ/XTIMER_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 +185,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

0 comments on commit a6c2c52

Please sign in to comment.