timer.py 2.5 KB

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