Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:0e018d759a2a 1 """
switches 0:0e018d759a2a 2 mbed SDK
switches 0:0e018d759a2a 3 Copyright (c) 2011-2013 ARM Limited
switches 0:0e018d759a2a 4
switches 0:0e018d759a2a 5 Licensed under the Apache License, Version 2.0 (the "License");
switches 0:0e018d759a2a 6 you may not use this file except in compliance with the License.
switches 0:0e018d759a2a 7 You may obtain a copy of the License at
switches 0:0e018d759a2a 8
switches 0:0e018d759a2a 9 http://www.apache.org/licenses/LICENSE-2.0
switches 0:0e018d759a2a 10
switches 0:0e018d759a2a 11 Unless required by applicable law or agreed to in writing, software
switches 0:0e018d759a2a 12 distributed under the License is distributed on an "AS IS" BASIS,
switches 0:0e018d759a2a 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:0e018d759a2a 14 See the License for the specific language governing permissions and
switches 0:0e018d759a2a 15 limitations under the License.
switches 0:0e018d759a2a 16 """
switches 0:0e018d759a2a 17
switches 0:0e018d759a2a 18 from mbed_host_tests import BaseHostTest
switches 0:0e018d759a2a 19
switches 0:0e018d759a2a 20
switches 0:0e018d759a2a 21 class TimingDriftTest(BaseHostTest):
switches 0:0e018d759a2a 22 """ This test is reading single characters from stdio
switches 0:0e018d759a2a 23 and measures time between their occurrences.
switches 0:0e018d759a2a 24 """
switches 0:0e018d759a2a 25 __result = None
switches 0:0e018d759a2a 26
switches 0:0e018d759a2a 27 # This is calculated later: average_drift_max * number of tick events
switches 0:0e018d759a2a 28 total_drift_max = None
switches 0:0e018d759a2a 29
switches 0:0e018d759a2a 30 average_drift_max = 0.05
switches 0:0e018d759a2a 31 ticks = []
switches 0:0e018d759a2a 32 start_time = None
switches 0:0e018d759a2a 33 finish_time = None
switches 0:0e018d759a2a 34 dut_seconds_passed = None
switches 0:0e018d759a2a 35 total_time = None
switches 0:0e018d759a2a 36 total_drift = None
switches 0:0e018d759a2a 37 average_drift = None
switches 0:0e018d759a2a 38
switches 0:0e018d759a2a 39 def _callback_result(self, key, value, timestamp):
switches 0:0e018d759a2a 40 # We should not see result data in this test
switches 0:0e018d759a2a 41 self.__result = False
switches 0:0e018d759a2a 42
switches 0:0e018d759a2a 43 def _callback_end(self, key, value, timestamp):
switches 0:0e018d759a2a 44 """ {{end;%s}}} """
switches 0:0e018d759a2a 45 self.log("Received end event, timestamp: %f" % timestamp)
switches 0:0e018d759a2a 46 self.notify_complete(result=self.result(print_stats=True))
switches 0:0e018d759a2a 47
switches 0:0e018d759a2a 48
switches 0:0e018d759a2a 49 def _callback_tick(self, key, value, timestamp):
switches 0:0e018d759a2a 50 """ {{tick;%d}}} """
switches 0:0e018d759a2a 51 self.log("tick! %f" % timestamp)
switches 0:0e018d759a2a 52 self.ticks.append((key, value, timestamp))
switches 0:0e018d759a2a 53
switches 0:0e018d759a2a 54
switches 0:0e018d759a2a 55 def setup(self):
switches 0:0e018d759a2a 56 self.register_callback("end", self._callback_end)
switches 0:0e018d759a2a 57 self.register_callback('tick', self._callback_tick)
switches 0:0e018d759a2a 58
switches 0:0e018d759a2a 59
switches 0:0e018d759a2a 60 def result(self, print_stats=True):
switches 0:0e018d759a2a 61 self.dut_seconds_passed = len(self.ticks) - 1
switches 0:0e018d759a2a 62
switches 0:0e018d759a2a 63 if self.dut_seconds_passed < 1:
switches 0:0e018d759a2a 64 if print_stats:
switches 0:0e018d759a2a 65 self.log("FAIL: failed to receive at least two tick events")
switches 0:0e018d759a2a 66 self.__result = False
switches 0:0e018d759a2a 67 return self.__result
switches 0:0e018d759a2a 68
switches 0:0e018d759a2a 69 self.total_drift_max = self.dut_seconds_passed * self.average_drift_max
switches 0:0e018d759a2a 70
switches 0:0e018d759a2a 71 self.start_time = self.ticks[0][2]
switches 0:0e018d759a2a 72 self.finish_time = self.ticks[-1][2]
switches 0:0e018d759a2a 73 self.total_time = self.finish_time - self.start_time
switches 0:0e018d759a2a 74 self.total_drift = self.total_time - self.dut_seconds_passed
switches 0:0e018d759a2a 75 self.average_drift = self.total_drift / self.dut_seconds_passed
switches 0:0e018d759a2a 76
switches 0:0e018d759a2a 77 if print_stats:
switches 0:0e018d759a2a 78 self.log("Start: %f" % self.start_time)
switches 0:0e018d759a2a 79 self.log("Finish: %f" % self.finish_time)
switches 0:0e018d759a2a 80 self.log("Total time taken: %f" % self.total_time)
switches 0:0e018d759a2a 81
switches 0:0e018d759a2a 82 total_drift_ratio_string = "Total drift/Max total drift: %f/%f"
switches 0:0e018d759a2a 83 self.log(total_drift_ratio_string % (self.total_drift,
switches 0:0e018d759a2a 84 self.total_drift_max))
switches 0:0e018d759a2a 85
switches 0:0e018d759a2a 86 average_drift_ratio_string = "Average drift/Max average drift: %f/%f"
switches 0:0e018d759a2a 87 self.log(average_drift_ratio_string % (self.average_drift,
switches 0:0e018d759a2a 88 self.average_drift_max))
switches 0:0e018d759a2a 89
switches 0:0e018d759a2a 90
switches 0:0e018d759a2a 91 if abs(self.total_drift) > self.total_drift_max:
switches 0:0e018d759a2a 92 if print_stats:
switches 0:0e018d759a2a 93 self.log("FAIL: Total drift exceeded max total drift")
switches 0:0e018d759a2a 94 self.__result = False
switches 0:0e018d759a2a 95 elif self.average_drift > self.average_drift_max:
switches 0:0e018d759a2a 96 if print_stats:
switches 0:0e018d759a2a 97 self.log("FAIL: Average drift exceeded max average drift")
switches 0:0e018d759a2a 98 self.__result = False
switches 0:0e018d759a2a 99 else:
switches 0:0e018d759a2a 100 self.__result = True
switches 0:0e018d759a2a 101
switches 0:0e018d759a2a 102 return self.__result
switches 0:0e018d759a2a 103
switches 0:0e018d759a2a 104
switches 0:0e018d759a2a 105 def teardown(self):
switches 0:0e018d759a2a 106 pass