test/wipy: Add Timer class tests.

This commit is contained in:
danicampora 2016-02-21 20:38:26 +01:00
parent 73c9f85b4c
commit fe9620a2bd
3 changed files with 173 additions and 17 deletions

View File

@ -252,17 +252,6 @@ STATIC void timer_channel_init (pyb_timer_channel_obj_t *ch) {
MAP_TimerMatchSet(ch->timer->timer, ch->channel, match);
MAP_TimerPrescaleMatchSet(ch->timer->timer, ch->channel, match >> 16);
}
// configure the event edge type if we are in such mode
else if ((ch->timer->config & 0x0F) == TIMER_CFG_A_CAP_COUNT || (ch->timer->config & 0x0F) == TIMER_CFG_A_CAP_TIME) {
uint32_t polarity = TIMER_EVENT_BOTH_EDGES;
if (ch->polarity == PYBTIMER_POLARITY_POS) {
polarity = TIMER_EVENT_POS_EDGE;
}
else if (ch->polarity == PYBTIMER_POLARITY_NEG) {
polarity = TIMER_EVENT_NEG_EDGE;
}
MAP_TimerControlEvent(ch->timer->timer, ch->channel, polarity);
}
#ifdef DEBUG
// 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_
default:
break;
}
mp_printf(print, "Timer(%u, mode=Timer.%q)", (tim->id + 1), mode_qst);
mp_printf(print, "Timer(%u, mode=Timer.%q)", tim->id, mode_qst);
}
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
}
bool is16bit = (args[1].u_int == 16);
if (!is16bit && (_mode != TIMER_CFG_A_ONE_SHOT_UP && _mode != TIMER_CFG_A_PERIODIC_UP)) {
if (!is16bit && _mode == TIMER_CFG_A_PWM) {
// 32-bit mode is only available when in free running modes
goto error;
}
@ -544,7 +533,7 @@ STATIC void pyb_timer_channel_print(const mp_print_t *print, mp_obj_t self_in, m
mp_printf(print, "timer.channel(Timer.%s, %q=%u", ch_id, MP_QSTR_freq, ch->frequency);
uint32_t mode = ch->timer->config & 0xFF;
if (mode == TIMER_CFG_A_CAP_COUNT || mode == TIMER_CFG_A_CAP_TIME || mode == TIMER_CFG_A_PWM) {
if (mode == TIMER_CFG_A_PWM) {
mp_printf(print, ", %q=Timer.", MP_QSTR_polarity);
switch (ch->polarity) {
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
mp_printf(print, "BOTH");
break;
}
if (mode == TIMER_CFG_A_PWM) {
mp_printf(print, ", %q=%u.%02u", MP_QSTR_duty_cycle, ch->duty_cycle / 100, ch->duty_cycle % 100);
}
mp_printf(print, ", %q=%u.%02u", MP_QSTR_duty_cycle, ch->duty_cycle / 100, ch->duty_cycle % 100);
}
mp_printf(print, ")");
}

117
tests/wipy/timer.py Normal file
View File

@ -0,0 +1,117 @@
'''
Timer test for the CC3200 based boards.
'''
from machine import Timer
import os
import time
mch = os.uname().machine
if 'LaunchPad' in mch:
pwm_pin = ('GP24')
elif 'WiPy' in mch:
pwm_pin = ('GP24')
else:
raise Exception('Board not supported!')
for i in range(4):
tim = Timer(i, mode=Timer.PERIODIC)
print(tim)
ch = tim.channel(Timer.A, freq=5)
print(ch)
ch = tim.channel(Timer.B, freq=5)
print(ch)
tim = Timer(i, mode=Timer.ONE_SHOT)
print(tim)
ch = tim.channel(Timer.A, freq=50)
print(ch)
ch = tim.channel(Timer.B, freq=50)
print(ch)
tim = Timer(i, mode=Timer.PWM)
print(tim)
ch = tim.channel(Timer.A, freq=50000, duty_cycle=2000, polarity=Timer.POSITIVE)
print(ch)
ch = tim.channel(Timer.B, freq=50000, duty_cycle=8000, polarity=Timer.NEGATIVE)
print(ch)
tim.deinit()
print(tim)
for i in range(4):
tim = Timer(i, mode=Timer.PERIODIC)
tim.deinit()
class TimerTest:
def __init__(self):
self.tim = Timer(0, mode=Timer.PERIODIC)
self.int_count = 0
def timer_isr(self, tim_ch):
self.int_count += 1
timer_test = TimerTest()
ch = timer_test.tim.channel(Timer.A, freq=5)
print(ch.freq() == 5)
ch.irq(handler=timer_test.timer_isr, trigger=Timer.TIMEOUT)
time.sleep_ms(1001)
print(timer_test.int_count == 5)
ch.freq(100)
timer_test.int_count = 0
time.sleep_ms(1001)
print(timer_test.int_count == 100)
ch.freq(1000)
time.sleep_ms(1500)
timer_test.int_count = 0
time.sleep_ms(2000)
print(timer_test.int_count == 2000)
timer_test.tim.deinit()
timer_test.tim.init(mode=Timer.ONE_SHOT)
ch = timer_test.tim.channel(Timer.A, period=100000)
ch.irq(handler=timer_test.timer_isr, trigger=Timer.TIMEOUT)
timer_test.int_count = 0
time.sleep_ms(101)
print(timer_test.int_count == 1)
time.sleep_ms(101)
print(timer_test.int_count == 1)
timer_test.tim.deinit()
print(timer_test.tim)
# 32 bit modes
tim = Timer(0, mode=Timer.PERIODIC, width=32)
ch = tim.channel(Timer.A | Timer.B, period=5000000)
# check for memory leaks...
for i in range(1000):
tim = Timer(0, mode=Timer.PERIODIC)
ch = tim.channel(Timer.A, freq=5)
# next ones must fail
try:
tim = Timer(0, mode=12)
except:
print('Exception')
try:
tim = Timer(4, mode=Timer.ONE_SHOT)
except:
print('Exception')
try:
tim = Timer(0, mode=Timer.PWM, width=32)
except:
print('Exception')
tim = Timer(0, mode=Timer.PWM)
try:
ch = tim.channel(TIMER_A | TIMER_B, freq=10)
except:
print('Exception')
try:
ch = tim.channel(TIMER_A, freq=4)
except:
print('Exception')

52
tests/wipy/timer.py.exp Normal file
View File

@ -0,0 +1,52 @@
Timer(0, mode=Timer.PERIODIC)
timer.channel(Timer.A, freq=5)
timer.channel(Timer.B, freq=5)
Timer(0, mode=Timer.ONE_SHOT)
timer.channel(Timer.A, freq=50)
timer.channel(Timer.B, freq=50)
Timer(0, mode=Timer.PWM)
timer.channel(Timer.A, freq=50000, polarity=Timer.POSITIVE, duty_cycle=20.00)
timer.channel(Timer.B, freq=50000, polarity=Timer.NEGATIVE, duty_cycle=80.00)
Timer(0, mode=Timer.PWM)
Timer(1, mode=Timer.PERIODIC)
timer.channel(Timer.A, freq=5)
timer.channel(Timer.B, freq=5)
Timer(1, mode=Timer.ONE_SHOT)
timer.channel(Timer.A, freq=50)
timer.channel(Timer.B, freq=50)
Timer(1, mode=Timer.PWM)
timer.channel(Timer.A, freq=50000, polarity=Timer.POSITIVE, duty_cycle=20.00)
timer.channel(Timer.B, freq=50000, polarity=Timer.NEGATIVE, duty_cycle=80.00)
Timer(1, mode=Timer.PWM)
Timer(2, mode=Timer.PERIODIC)
timer.channel(Timer.A, freq=5)
timer.channel(Timer.B, freq=5)
Timer(2, mode=Timer.ONE_SHOT)
timer.channel(Timer.A, freq=50)
timer.channel(Timer.B, freq=50)
Timer(2, mode=Timer.PWM)
timer.channel(Timer.A, freq=50000, polarity=Timer.POSITIVE, duty_cycle=20.00)
timer.channel(Timer.B, freq=50000, polarity=Timer.NEGATIVE, duty_cycle=80.00)
Timer(2, mode=Timer.PWM)
Timer(3, mode=Timer.PERIODIC)
timer.channel(Timer.A, freq=5)
timer.channel(Timer.B, freq=5)
Timer(3, mode=Timer.ONE_SHOT)
timer.channel(Timer.A, freq=50)
timer.channel(Timer.B, freq=50)
Timer(3, mode=Timer.PWM)
timer.channel(Timer.A, freq=50000, polarity=Timer.POSITIVE, duty_cycle=20.00)
timer.channel(Timer.B, freq=50000, polarity=Timer.NEGATIVE, duty_cycle=80.00)
Timer(3, mode=Timer.PWM)
True
True
True
True
True
True
Timer(0, mode=Timer.ONE_SHOT)
Exception
Exception
Exception
Exception
Exception