1""" 2Timer test for the CC3200 based boards. 3""" 4 5from machine import Timer 6import os 7import time 8 9mch = os.uname().machine 10if "LaunchPad" in mch: 11 pwm_pin = "GP24" 12elif "WiPy" in mch: 13 pwm_pin = "GP24" 14else: 15 raise Exception("Board not supported!") 16 17for 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 39for i in range(4): 40 tim = Timer(i, mode=Timer.PERIODIC) 41 tim.deinit() 42 43 44class 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 53timer_test = TimerTest() 54ch = timer_test.tim.channel(Timer.A, freq=5) 55print(ch.freq() == 5) 56ch.irq(handler=timer_test.timer_isr, trigger=Timer.TIMEOUT) 57time.sleep_ms(1001) 58print(timer_test.int_count == 5) 59 60ch.freq(100) 61timer_test.int_count = 0 62time.sleep_ms(1001) 63print(timer_test.int_count == 100) 64 65ch.freq(1000) 66time.sleep_ms(1500) 67timer_test.int_count = 0 68time.sleep_ms(2000) 69print(timer_test.int_count == 2000) 70 71timer_test.tim.deinit() 72timer_test.tim.init(mode=Timer.ONE_SHOT) 73ch = timer_test.tim.channel(Timer.A, period=100000) 74ch.irq(handler=timer_test.timer_isr, trigger=Timer.TIMEOUT) 75timer_test.int_count = 0 76time.sleep_ms(101) 77print(timer_test.int_count == 1) 78time.sleep_ms(101) 79print(timer_test.int_count == 1) 80timer_test.tim.deinit() 81print(timer_test.tim) 82 83# 32 bit modes 84tim = Timer(0, mode=Timer.PERIODIC, width=32) 85ch = tim.channel(Timer.A | Timer.B, period=5000000) 86 87# check for memory leaks... 88for i in range(1000): 89 tim = Timer(0, mode=Timer.PERIODIC) 90 ch = tim.channel(Timer.A, freq=5) 91 92# next ones must fail 93try: 94 tim = Timer(0, mode=12) 95except: 96 print("Exception") 97 98try: 99 tim = Timer(4, mode=Timer.ONE_SHOT) 100except: 101 print("Exception") 102 103try: 104 tim = Timer(0, mode=Timer.PWM, width=32) 105except: 106 print("Exception") 107 108tim = Timer(0, mode=Timer.PWM) 109 110try: 111 ch = tim.channel(TIMER_A | TIMER_B, freq=10) 112except: 113 print("Exception") 114 115try: 116 ch = tim.channel(TIMER_A, freq=4) 117except: 118 print("Exception") 119