rtc_irq.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. '''
  2. RTC IRQ test for the CC3200 based boards.
  3. '''
  4. from machine import RTC
  5. import machine
  6. import os
  7. import time
  8. mch = os.uname().machine
  9. if not 'LaunchPad' in mch and not 'WiPy' in mch:
  10. raise Exception('Board not supported!')
  11. def rtc_ticks_ms(rtc):
  12. timedate = rtc.now()
  13. return (timedate[5] * 1000) + (timedate[6] // 1000)
  14. rtc_irq_count = 0
  15. def alarm_handler (rtc_o):
  16. global rtc_irq
  17. global rtc_irq_count
  18. if rtc_irq.flags() & RTC.ALARM0:
  19. rtc_irq_count += 1
  20. rtc = RTC()
  21. rtc.alarm(time=500, repeat=True)
  22. rtc_irq = rtc.irq(trigger=RTC.ALARM0, handler=alarm_handler)
  23. # active mode
  24. time.sleep_ms(1000)
  25. rtc.alarm_cancel()
  26. print(rtc_irq_count == 2)
  27. rtc_irq_count = 0
  28. rtc.alarm(time=200, repeat=True)
  29. time.sleep_ms(1000)
  30. rtc.alarm_cancel()
  31. print(rtc_irq_count == 5)
  32. rtc_irq_count = 0
  33. rtc.alarm(time=100, repeat=True)
  34. time.sleep_ms(1000)
  35. rtc.alarm_cancel()
  36. print(rtc_irq_count == 10)
  37. # deep sleep mode
  38. rtc.alarm_cancel()
  39. rtc_irq_count = 0
  40. rtc.alarm(time=50, repeat=True)
  41. rtc_irq.init(trigger=RTC.ALARM0, handler=alarm_handler, wake=machine.SLEEP | machine.IDLE)
  42. while rtc_irq_count < 3:
  43. machine.sleep()
  44. print(rtc_irq_count == 3)
  45. # no repetition
  46. rtc.alarm_cancel()
  47. rtc_irq_count = 0
  48. rtc.alarm(time=100, repeat=False)
  49. time.sleep_ms(250)
  50. print(rtc_irq_count == 1)
  51. rtc.alarm_cancel()
  52. t0 = rtc_ticks_ms(rtc)
  53. rtc.alarm(time=500, repeat=False)
  54. machine.sleep()
  55. t1 = rtc_ticks_ms(rtc)
  56. print(abs(t1 - t0 - 500) < 20)
  57. # deep sleep repeated mode
  58. rtc.alarm_cancel()
  59. rtc_irq_count = 0
  60. rtc.alarm(time=500, repeat=True)
  61. t0 = rtc_ticks_ms(rtc)
  62. rtc_irq = rtc.irq(trigger=RTC.ALARM0, handler=alarm_handler, wake=machine.SLEEP)
  63. while rtc_irq_count < 3:
  64. machine.sleep()
  65. t1 = rtc_ticks_ms(rtc)
  66. print(abs(t1 - t0 - (500 * rtc_irq_count)) < 25)
  67. # next ones must raise
  68. try:
  69. rtc_irq = rtc.irq(trigger=10, handler=alarm_handler)
  70. except:
  71. print('Exception')
  72. try:
  73. rtc_irq = rtc.irq(trigger=RTC.ALARM0, wake=1789456)
  74. except:
  75. print('Exception')