Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wait_us_auto.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. mbed SDK
  3. Copyright (c) 2011-2013 ARM Limited
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. """
  14. from time import time
  15. class WaitusTest():
  16. """ This test is reading single characters from stdio
  17. and measures time between their occurrences.
  18. """
  19. TICK_LOOP_COUNTER = 13
  20. TICK_LOOP_SUCCESSFUL_COUNTS = 10
  21. DEVIATION = 0.10 # +/-10%
  22. def test(self, selftest):
  23. test_result = True
  24. # First character to start test (to know after reset when test starts)
  25. if selftest.mbed.set_serial_timeout(None) is None:
  26. return selftest.RESULT_IO_SERIAL
  27. c = selftest.mbed.serial_read(1)
  28. if c is None:
  29. return selftest.RESULT_IO_SERIAL
  30. if c == '$': # target will printout TargetID e.g.: $$$$1040e649d5c09a09a3f6bc568adef61375c6
  31. #Read additional 39 bytes of TargetID
  32. if selftest.mbed.serial_read(39) is None:
  33. return selftest.RESULT_IO_SERIAL
  34. c = selftest.mbed.serial_read(1) # Re-read first 'tick'
  35. if c is None:
  36. return selftest.RESULT_IO_SERIAL
  37. start_serial_pool = time()
  38. start = time()
  39. success_counter = 0
  40. for i in range(0, self.TICK_LOOP_COUNTER):
  41. c = selftest.mbed.serial_read(1)
  42. if c is None:
  43. return selftest.RESULT_IO_SERIAL
  44. delta = time() - start
  45. deviation = abs(delta - 1)
  46. # Round values
  47. delta = round(delta, 2)
  48. deviation = round(deviation, 2)
  49. # Check if time measurements are in given range
  50. deviation_ok = True if delta > 0 and deviation <= self.DEVIATION else False
  51. success_counter = success_counter+1 if deviation_ok else 0
  52. msg = "OK" if deviation_ok else "FAIL"
  53. selftest.notify("%s in %.2f sec (%.2f) [%s]"% (c, delta, deviation, msg))
  54. start = time()
  55. if success_counter >= self.TICK_LOOP_SUCCESSFUL_COUNTS:
  56. break
  57. measurement_time = time() - start_serial_pool
  58. selftest.notify("Consecutive OK timer reads: %d"% success_counter)
  59. selftest.notify("Completed in %.2f sec" % (measurement_time))
  60. test_result = True if success_counter >= self.TICK_LOOP_SUCCESSFUL_COUNTS else False
  61. return selftest.RESULT_SUCCESS if test_result else selftest.RESULT_FAILURE