Skip to content

Commit 1f2daf4

Browse files
author
danicampora
committed
cc3200: Correct ticks_cpu and ticks_us functions in time module.
1 parent 1c7f9b1 commit 1f2daf4

File tree

3 files changed

+9
-37
lines changed

3 files changed

+9
-37
lines changed

cc3200/misc/mpsystick.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,14 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
5858

5959
// The SysTick timer counts down at HAL_FCPU_HZ, so we can use that knowledge
6060
// to grab a microsecond counter.
61-
//
6261
// We assume that HAL_GetTick returns milliseconds.
6362
uint32_t sys_tick_get_microseconds(void) {
6463
mp_uint_t irq_state = disable_irq();
6564
uint32_t counter = SysTickValueGet();
6665
uint32_t milliseconds = HAL_GetTick();
67-
uint32_t status = (HWREG(NVIC_ST_CTRL));
6866
enable_irq(irq_state);
6967

70-
// It's still possible for the countflag bit to get set if the counter was
71-
// reloaded between reading VAL and reading CTRL. With interrupts disabled
72-
// it definitely takes less than 50 HCLK cycles between reading VAL and
73-
// reading CTRL, so the test (counter > 50) is to cover the case where VAL
74-
// is +ve and very close to zero, and the COUNTFLAG bit is also set.
75-
if ((status & NVIC_ST_CTRL_COUNT) && counter > 50) {
76-
// This means that the HW reloaded VAL between the time we read VAL and the
77-
// time we read CTRL, which implies that there is an interrupt pending
78-
// to increment the tick counter.
79-
milliseconds++;
80-
}
81-
uint32_t load = (HWREG(NVIC_ST_RELOAD));
68+
uint32_t load = SysTickPeriodGet();
8269
counter = load - counter; // Convert from decrementing to incrementing
83-
84-
// ((load + 1) / 1000) is the number of counts per microsecond.
85-
//
86-
// counter / ((load + 1) / 1000) scales from the systick clock to microseconds
87-
// and is the same thing as (counter * 1000) / (load + 1)
88-
return milliseconds * 1000 + (counter * 1000) / (load + 1);
70+
return (milliseconds * 1000) + ((counter * 1000) / load);
8971
}

cc3200/mods/modutime.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,33 +150,25 @@ STATIC mp_obj_t time_sleep_us (mp_obj_t usec_in) {
150150
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us);
151151

152152
STATIC mp_obj_t time_ticks_ms(void) {
153-
// We want to "cast" the 32 bit unsigned into a small-int. This means
154-
// copying the MSB down 1 bit (extending the sign down), which is
155-
// equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
153+
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
156154
return MP_OBJ_NEW_SMALL_INT(HAL_GetTick() & MP_SMALL_INT_POSITIVE_MASK);
157155
}
158156
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_ms_obj, time_ticks_ms);
159157

160158
STATIC mp_obj_t time_ticks_us(void) {
161-
// We want to "cast" the 32 bit unsigned into a small-int. This means
162-
// copying the MSB down 1 bit (extending the sign down), which is
163-
// equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
159+
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
164160
return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds() & MP_SMALL_INT_POSITIVE_MASK);
165161
}
166162
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_us_obj, time_ticks_us);
167163

168164
STATIC mp_obj_t time_ticks_cpu(void) {
169-
// We want to "cast" the 32 bit unsigned into a small-int. This means
170-
// copying the MSB down 1 bit (extending the sign down), which is
171-
// equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
172-
return MP_OBJ_NEW_SMALL_INT(SysTickValueGet() & MP_SMALL_INT_POSITIVE_MASK);
165+
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
166+
return MP_OBJ_NEW_SMALL_INT((SysTickPeriodGet() - SysTickValueGet()) & MP_SMALL_INT_POSITIVE_MASK);
173167
}
174168
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_cpu_obj, time_ticks_cpu);
175169

176170
STATIC mp_obj_t time_ticks_diff(mp_obj_t t0, mp_obj_t t1) {
177-
// We want to "cast" the 32 bit unsigned into a small-int. This means
178-
// copying the MSB down 1 bit (extending the sign down), which is
179-
// equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
171+
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
180172
uint32_t start = mp_obj_get_int(t0);
181173
uint32_t end = mp_obj_get_int(t1);
182174
return MP_OBJ_NEW_SMALL_INT((end - start) & MP_SMALL_INT_POSITIVE_MASK);

tests/wipy/time.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ def spot_test(seconds, expected_time):
7070
t1 = time.ticks_us()
7171
time.sleep_us(1000)
7272
t2 = time.ticks_us()
73-
print(time.ticks_diff(t1, t2) < 2000)
73+
print(time.ticks_diff(t1, t2) < 1500)
7474

75-
t1 = time.ticks_cpu()
76-
t2 = time.ticks_cpu()
77-
print(time.ticks_diff(t1, t2) >= 0)
75+
print(time.ticks_diff(time.ticks_cpu(), time.ticks_cpu()) < 16384)

0 commit comments

Comments
 (0)