Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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