Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rtc_reset.py Source File

rtc_reset.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2017-2017 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 from __future__ import print_function
00018 
00019 from mbed_host_tests import BaseHostTest
00020 from time import sleep
00021 
00022 
00023 class RtcResetTest (BaseHostTest):
00024     """This test checks that a device's RTC keeps count through a reset
00025     
00026     It does this by setting the RTC's time, triggering a reset,
00027     delaying and then reading the RTC's time again to ensure
00028     that the RTC is still counting.
00029     """
00030 
00031     """Start of the RTC"""
00032     START_TIME = 50000
00033     START_TIME_TOLERANCE = 10
00034     """Time to delay after sending reset"""
00035     DELAY_TIME = 5.0
00036     DELAY_TOLERANCE = 1.0
00037     VALUE_PLACEHOLDER = "0"
00038 
00039     def setup (self):
00040         """Register callbacks required for the test"""
00041         self._error  = False
00042         generator = self.rtc_reset_test ()
00043         generator.next()
00044 
00045         def run_gen(key, value, time):
00046             """Run the generator, and fail testing if the iterator stops"""
00047             if self._error :
00048                 return
00049             try:
00050                 generator.send((key, value, time))
00051             except StopIteration:
00052                 self._error  = True
00053 
00054         for resp in ("start", "read", "ack"):
00055             self.register_callback(resp, run_gen)
00056 
00057     def teardown (self):
00058         """No work to do here"""
00059         pass
00060 
00061     def rtc_reset_test (self):
00062         """Generator for running the reset test
00063         
00064         This function calls yield to wait for the next event from
00065         the device. If the device gives the wrong response, then the
00066         generator terminates by returing which raises a StopIteration
00067         exception and fails the test.
00068         """
00069 
00070         # Wait for start token
00071         key, value, time = yield
00072         if key != "start":
00073             return
00074 
00075         # Initialize, and set the time
00076         self.send_kv("init", self.VALUE_PLACEHOLDER)
00077         
00078         # Wait for ack from the device
00079         key, value, time = yield
00080         if key != "ack":
00081             return
00082         
00083         self.send_kv("write", str(self.START_TIME))
00084         
00085         # Wait for ack from the device
00086         key, value, time = yield
00087         if key != "ack":
00088             return
00089         
00090         self.send_kv("read", self.VALUE_PLACEHOLDER)
00091         key, value, time = yield
00092         if key != "read":
00093             return
00094         dev_time_start = int(value)
00095 
00096         # Unitialize, and reset
00097         self.send_kv("free", self.VALUE_PLACEHOLDER)
00098         
00099         # Wait for ack from the device
00100         key, value, time = yield
00101         if key != "ack":
00102             return
00103         
00104         self.send_kv("reset", self.VALUE_PLACEHOLDER)
00105         
00106         # No ack after reset
00107         sleep(self.DELAY_TIME)
00108 
00109         # Restart the test, and send the sync token
00110         self.send_kv("__sync", "00000000-0000-000000000-000000000000")
00111         key, value, time = yield
00112         if key != "start":
00113             return
00114 
00115         # Initialize, and read the time
00116         self.send_kv("init", self.VALUE_PLACEHOLDER)
00117         
00118         # Wait for ack from the device
00119         key, value, time = yield
00120         if key != "ack":
00121             return
00122         
00123         self.send_kv("read", self.VALUE_PLACEHOLDER)
00124         key, value, time = yield
00125         if key != "read":
00126             return
00127         dev_time_end = int(value)
00128 
00129         # Check result
00130         elapsed = dev_time_end - dev_time_start
00131         start_time_valid = (self.START_TIME <= dev_time_start <
00132                             self.START_TIME + self.START_TIME_TOLERANCE)
00133         elapsed_time_valid = elapsed >= self.DELAY_TIME - self.DELAY_TOLERANCE
00134         passed = start_time_valid and elapsed_time_valid
00135         if not start_time_valid:
00136             self.log("FAIL: Expected start time of %i got %i" %
00137                      (self.START_TIME, dev_time_start))
00138         elif not passed:
00139             self.log("FAIL: Delayed for %fs but device "
00140                      "reported elapsed time of %fs" %
00141                      (self.DELAY_TIME, elapsed))
00142         self.send_kv("exit", "pass" if passed else "fail")
00143         yield    # No more events expected
00144