erkin yucel / mbed-os

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers timing_drift_auto.py Source File

timing_drift_auto.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2013 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 
00018 from mbed_host_tests import BaseHostTest
00019 
00020 
00021 class TimingDriftTest (BaseHostTest):
00022     """ This test is reading single characters from stdio
00023         and measures time between their occurrences.
00024     """
00025     __result = None
00026     
00027     # This is calculated later: average_drift_max * number of tick events
00028     total_drift_max = None
00029     
00030     average_drift_max = 0.05
00031     ticks = []
00032     start_time = None
00033     finish_time = None
00034     dut_seconds_passed = None
00035     total_time = None
00036     total_drift = None
00037     average_drift = None
00038     
00039     def _callback_result(self, key, value, timestamp):
00040         # We should not see result data in this test
00041         self.__result  = False
00042 
00043     def _callback_end(self, key, value, timestamp):
00044         """ {{end;%s}}} """
00045         self.log("Received end event, timestamp: %f" % timestamp)
00046         self.notify_complete(result=self.result (print_stats=True))
00047 
00048 
00049     def _callback_tick(self, key, value, timestamp):
00050         """ {{tick;%d}}} """
00051         self.log("tick! %f" % timestamp)
00052         self.ticks .append((key, value, timestamp))
00053 
00054 
00055     def setup(self):
00056         self.register_callback("end", self._callback_end )
00057         self.register_callback('tick', self._callback_tick )
00058 
00059 
00060     def result(self, print_stats=True):        
00061         self.dut_seconds_passed  = len(self.ticks ) - 1
00062         
00063         if self.dut_seconds_passed  < 1:
00064             if print_stats:
00065                 self.log("FAIL: failed to receive at least two tick events")
00066             self.__result  = False
00067             return self.__result 
00068 
00069         self.total_drift_max  = self.dut_seconds_passed  * self.average_drift_max 
00070 
00071         self.start_time  = self.ticks [0][2]
00072         self.finish_time  = self.ticks [-1][2]
00073         self.total_time  = self.finish_time  - self.start_time 
00074         self.total_drift  = self.total_time  - self.dut_seconds_passed 
00075         self.average_drift  = self.total_drift  / self.dut_seconds_passed 
00076         
00077         if print_stats:
00078             self.log("Start: %f" % self.start_time )
00079             self.log("Finish: %f" % self.finish_time )
00080             self.log("Total time taken: %f" % self.total_time )
00081         
00082             total_drift_ratio_string = "Total drift/Max total drift: %f/%f"
00083             self.log(total_drift_ratio_string % (self.total_drift ,
00084                                                  self.total_drift_max ))
00085                                                  
00086             average_drift_ratio_string = "Average drift/Max average drift: %f/%f"
00087             self.log(average_drift_ratio_string % (self.average_drift ,
00088                                                    self.average_drift_max ))
00089         
00090 
00091         if abs(self.total_drift ) > self.total_drift_max :
00092             if print_stats:
00093                 self.log("FAIL: Total drift exceeded max total drift")
00094             self.__result  = False
00095         elif self.average_drift  > self.average_drift_max :
00096             if print_stats:
00097                 self.log("FAIL: Average drift exceeded max average drift")
00098             self.__result  = False
00099         else:
00100             self.__result  = True
00101         
00102         return self.__result 
00103 
00104 
00105     def teardown(self):
00106         pass