rtc.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import pyb, stm
  2. from pyb import RTC
  3. rtc = RTC()
  4. rtc.init()
  5. print(rtc)
  6. # make sure that 1 second passes correctly
  7. rtc.datetime((2014, 1, 1, 1, 0, 0, 0, 0))
  8. pyb.delay(1002)
  9. print(rtc.datetime()[:7])
  10. def set_and_print(datetime):
  11. rtc.datetime(datetime)
  12. print(rtc.datetime()[:7])
  13. # make sure that setting works correctly
  14. set_and_print((2000, 1, 1, 1, 0, 0, 0, 0))
  15. set_and_print((2000, 1, 31, 1, 0, 0, 0, 0))
  16. set_and_print((2000, 12, 31, 1, 0, 0, 0, 0))
  17. set_and_print((2016, 12, 31, 1, 0, 0, 0, 0))
  18. set_and_print((2016, 12, 31, 7, 0, 0, 0, 0))
  19. set_and_print((2016, 12, 31, 7, 1, 0, 0, 0))
  20. set_and_print((2016, 12, 31, 7, 12, 0, 0, 0))
  21. set_and_print((2016, 12, 31, 7, 13, 0, 0, 0))
  22. set_and_print((2016, 12, 31, 7, 23, 0, 0, 0))
  23. set_and_print((2016, 12, 31, 7, 23, 1, 0, 0))
  24. set_and_print((2016, 12, 31, 7, 23, 59, 0, 0))
  25. set_and_print((2016, 12, 31, 7, 23, 59, 1, 0))
  26. set_and_print((2016, 12, 31, 7, 23, 59, 59, 0))
  27. set_and_print((2099, 12, 31, 7, 23, 59, 59, 0))
  28. # check that calibration works correctly
  29. # save existing calibration value:
  30. cal_tmp = rtc.calibration()
  31. def set_and_print_calib(cal):
  32. rtc.calibration(cal)
  33. print(rtc.calibration())
  34. set_and_print_calib(512)
  35. set_and_print_calib(511)
  36. set_and_print_calib(345)
  37. set_and_print_calib(1)
  38. set_and_print_calib(0)
  39. set_and_print_calib(-1)
  40. set_and_print_calib(-123)
  41. set_and_print_calib(-510)
  42. set_and_print_calib(-511)
  43. # restore existing calibration value
  44. rtc.calibration(cal_tmp)
  45. # Check register settings for wakeup
  46. def set_and_print_wakeup(ms):
  47. try:
  48. rtc.wakeup(ms)
  49. wucksel = stm.mem32[stm.RTC + stm.RTC_CR] & 7
  50. wut = stm.mem32[stm.RTC + stm.RTC_WUTR] & 0xffff
  51. except ValueError:
  52. wucksel = -1
  53. wut = -1
  54. print((wucksel, wut))
  55. set_and_print_wakeup(0)
  56. set_and_print_wakeup(1)
  57. set_and_print_wakeup(4000)
  58. set_and_print_wakeup(4001)
  59. set_and_print_wakeup(8000)
  60. set_and_print_wakeup(8001)
  61. set_and_print_wakeup(16000)
  62. set_and_print_wakeup(16001)
  63. set_and_print_wakeup(32000)
  64. set_and_print_wakeup(32001)
  65. set_and_print_wakeup(0x10000*1000)
  66. set_and_print_wakeup(0x10001*1000)
  67. set_and_print_wakeup(0x1ffff*1000)
  68. set_and_print_wakeup(0x20000*1000)
  69. set_and_print_wakeup(0x20001*1000) # exception