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