Skip to content

Commit fe9620a

Browse files
author
danicampora
committed
test/wipy: Add Timer class tests.
1 parent 73c9f85 commit fe9620a

File tree

3 files changed

+173
-17
lines changed

3 files changed

+173
-17
lines changed

cc3200/mods/pybtimer.c

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,6 @@ STATIC void timer_channel_init (pyb_timer_channel_obj_t *ch) {
252252
MAP_TimerMatchSet(ch->timer->timer, ch->channel, match);
253253
MAP_TimerPrescaleMatchSet(ch->timer->timer, ch->channel, match >> 16);
254254
}
255-
// configure the event edge type if we are in such mode
256-
else if ((ch->timer->config & 0x0F) == TIMER_CFG_A_CAP_COUNT || (ch->timer->config & 0x0F) == TIMER_CFG_A_CAP_TIME) {
257-
uint32_t polarity = TIMER_EVENT_BOTH_EDGES;
258-
if (ch->polarity == PYBTIMER_POLARITY_POS) {
259-
polarity = TIMER_EVENT_POS_EDGE;
260-
}
261-
else if (ch->polarity == PYBTIMER_POLARITY_NEG) {
262-
polarity = TIMER_EVENT_NEG_EDGE;
263-
}
264-
MAP_TimerControlEvent(ch->timer->timer, ch->channel, polarity);
265-
}
266255

267256
#ifdef DEBUG
268257
// stall the timer when the processor is halted while debugging
@@ -292,7 +281,7 @@ STATIC void pyb_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
292281
default:
293282
break;
294283
}
295-
mp_printf(print, "Timer(%u, mode=Timer.%q)", (tim->id + 1), mode_qst);
284+
mp_printf(print, "Timer(%u, mode=Timer.%q)", tim->id, mode_qst);
296285
}
297286

298287
STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *tim, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
@@ -317,7 +306,7 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *tim, mp_uint_t n_args, co
317306
}
318307
bool is16bit = (args[1].u_int == 16);
319308

320-
if (!is16bit && (_mode != TIMER_CFG_A_ONE_SHOT_UP && _mode != TIMER_CFG_A_PERIODIC_UP)) {
309+
if (!is16bit && _mode == TIMER_CFG_A_PWM) {
321310
// 32-bit mode is only available when in free running modes
322311
goto error;
323312
}
@@ -544,7 +533,7 @@ STATIC void pyb_timer_channel_print(const mp_print_t *print, mp_obj_t self_in, m
544533
mp_printf(print, "timer.channel(Timer.%s, %q=%u", ch_id, MP_QSTR_freq, ch->frequency);
545534

546535
uint32_t mode = ch->timer->config & 0xFF;
547-
if (mode == TIMER_CFG_A_CAP_COUNT || mode == TIMER_CFG_A_CAP_TIME || mode == TIMER_CFG_A_PWM) {
536+
if (mode == TIMER_CFG_A_PWM) {
548537
mp_printf(print, ", %q=Timer.", MP_QSTR_polarity);
549538
switch (ch->polarity) {
550539
case PYBTIMER_POLARITY_POS:
@@ -557,9 +546,7 @@ STATIC void pyb_timer_channel_print(const mp_print_t *print, mp_obj_t self_in, m
557546
mp_printf(print, "BOTH");
558547
break;
559548
}
560-
if (mode == TIMER_CFG_A_PWM) {
561-
mp_printf(print, ", %q=%u.%02u", MP_QSTR_duty_cycle, ch->duty_cycle / 100, ch->duty_cycle % 100);
562-
}
549+
mp_printf(print, ", %q=%u.%02u", MP_QSTR_duty_cycle, ch->duty_cycle / 100, ch->duty_cycle % 100);
563550
}
564551
mp_printf(print, ")");
565552
}

tests/wipy/timer.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
'''
2+
Timer test for the CC3200 based boards.
3+
'''
4+
5+
from machine import Timer
6+
import os
7+
import time
8+
9+
mch = os.uname().machine
10+
if 'LaunchPad' in mch:
11+
pwm_pin = ('GP24')
12+
elif 'WiPy' in mch:
13+
pwm_pin = ('GP24')
14+
else:
15+
raise Exception('Board not supported!')
16+
17+
for i in range(4):
18+
tim = Timer(i, mode=Timer.PERIODIC)
19+
print(tim)
20+
ch = tim.channel(Timer.A, freq=5)
21+
print(ch)
22+
ch = tim.channel(Timer.B, freq=5)
23+
print(ch)
24+
tim = Timer(i, mode=Timer.ONE_SHOT)
25+
print(tim)
26+
ch = tim.channel(Timer.A, freq=50)
27+
print(ch)
28+
ch = tim.channel(Timer.B, freq=50)
29+
print(ch)
30+
tim = Timer(i, mode=Timer.PWM)
31+
print(tim)
32+
ch = tim.channel(Timer.A, freq=50000, duty_cycle=2000, polarity=Timer.POSITIVE)
33+
print(ch)
34+
ch = tim.channel(Timer.B, freq=50000, duty_cycle=8000, polarity=Timer.NEGATIVE)
35+
print(ch)
36+
tim.deinit()
37+
print(tim)
38+
39+
for i in range(4):
40+
tim = Timer(i, mode=Timer.PERIODIC)
41+
tim.deinit()
42+
43+
44+
class TimerTest:
45+
def __init__(self):
46+
self.tim = Timer(0, mode=Timer.PERIODIC)
47+
self.int_count = 0
48+
49+
def timer_isr(self, tim_ch):
50+
self.int_count += 1
51+
52+
timer_test = TimerTest()
53+
ch = timer_test.tim.channel(Timer.A, freq=5)
54+
print(ch.freq() == 5)
55+
ch.irq(handler=timer_test.timer_isr, trigger=Timer.TIMEOUT)
56+
time.sleep_ms(1001)
57+
print(timer_test.int_count == 5)
58+
59+
ch.freq(100)
60+
timer_test.int_count = 0
61+
time.sleep_ms(1001)
62+
print(timer_test.int_count == 100)
63+
64+
ch.freq(1000)
65+
time.sleep_ms(1500)
66+
timer_test.int_count = 0
67+
time.sleep_ms(2000)
68+
print(timer_test.int_count == 2000)
69+
70+
timer_test.tim.deinit()
71+
timer_test.tim.init(mode=Timer.ONE_SHOT)
72+
ch = timer_test.tim.channel(Timer.A, period=100000)
73+
ch.irq(handler=timer_test.timer_isr, trigger=Timer.TIMEOUT)
74+
timer_test.int_count = 0
75+
time.sleep_ms(101)
76+
print(timer_test.int_count == 1)
77+
time.sleep_ms(101)
78+
print(timer_test.int_count == 1)
79+
timer_test.tim.deinit()
80+
print(timer_test.tim)
81+
82+
# 32 bit modes
83+
tim = Timer(0, mode=Timer.PERIODIC, width=32)
84+
ch = tim.channel(Timer.A | Timer.B, period=5000000)
85+
86+
# check for memory leaks...
87+
for i in range(1000):
88+
tim = Timer(0, mode=Timer.PERIODIC)
89+
ch = tim.channel(Timer.A, freq=5)
90+
91+
# next ones must fail
92+
try:
93+
tim = Timer(0, mode=12)
94+
except:
95+
print('Exception')
96+
97+
try:
98+
tim = Timer(4, mode=Timer.ONE_SHOT)
99+
except:
100+
print('Exception')
101+
102+
try:
103+
tim = Timer(0, mode=Timer.PWM, width=32)
104+
except:
105+
print('Exception')
106+
107+
tim = Timer(0, mode=Timer.PWM)
108+
109+
try:
110+
ch = tim.channel(TIMER_A | TIMER_B, freq=10)
111+
except:
112+
print('Exception')
113+
114+
try:
115+
ch = tim.channel(TIMER_A, freq=4)
116+
except:
117+
print('Exception')

tests/wipy/timer.py.exp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Timer(0, mode=Timer.PERIODIC)
2+
timer.channel(Timer.A, freq=5)
3+
timer.channel(Timer.B, freq=5)
4+
Timer(0, mode=Timer.ONE_SHOT)
5+
timer.channel(Timer.A, freq=50)
6+
timer.channel(Timer.B, freq=50)
7+
Timer(0, mode=Timer.PWM)
8+
timer.channel(Timer.A, freq=50000, polarity=Timer.POSITIVE, duty_cycle=20.00)
9+
timer.channel(Timer.B, freq=50000, polarity=Timer.NEGATIVE, duty_cycle=80.00)
10+
Timer(0, mode=Timer.PWM)
11+
Timer(1, mode=Timer.PERIODIC)
12+
timer.channel(Timer.A, freq=5)
13+
timer.channel(Timer.B, freq=5)
14+
Timer(1, mode=Timer.ONE_SHOT)
15+
timer.channel(Timer.A, freq=50)
16+
timer.channel(Timer.B, freq=50)
17+
Timer(1, mode=Timer.PWM)
18+
timer.channel(Timer.A, freq=50000, polarity=Timer.POSITIVE, duty_cycle=20.00)
19+
timer.channel(Timer.B, freq=50000, polarity=Timer.NEGATIVE, duty_cycle=80.00)
20+
Timer(1, mode=Timer.PWM)
21+
Timer(2, mode=Timer.PERIODIC)
22+
timer.channel(Timer.A, freq=5)
23+
timer.channel(Timer.B, freq=5)
24+
Timer(2, mode=Timer.ONE_SHOT)
25+
timer.channel(Timer.A, freq=50)
26+
timer.channel(Timer.B, freq=50)
27+
Timer(2, mode=Timer.PWM)
28+
timer.channel(Timer.A, freq=50000, polarity=Timer.POSITIVE, duty_cycle=20.00)
29+
timer.channel(Timer.B, freq=50000, polarity=Timer.NEGATIVE, duty_cycle=80.00)
30+
Timer(2, mode=Timer.PWM)
31+
Timer(3, mode=Timer.PERIODIC)
32+
timer.channel(Timer.A, freq=5)
33+
timer.channel(Timer.B, freq=5)
34+
Timer(3, mode=Timer.ONE_SHOT)
35+
timer.channel(Timer.A, freq=50)
36+
timer.channel(Timer.B, freq=50)
37+
Timer(3, mode=Timer.PWM)
38+
timer.channel(Timer.A, freq=50000, polarity=Timer.POSITIVE, duty_cycle=20.00)
39+
timer.channel(Timer.B, freq=50000, polarity=Timer.NEGATIVE, duty_cycle=80.00)
40+
Timer(3, mode=Timer.PWM)
41+
True
42+
True
43+
True
44+
True
45+
True
46+
True
47+
Timer(0, mode=Timer.ONE_SHOT)
48+
Exception
49+
Exception
50+
Exception
51+
Exception
52+
Exception

0 commit comments

Comments
 (0)