Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers unexpected_reset.py Source File

unexpected_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 UnexpectedResetTest (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     """Number of times to reset the device in this test"""
00032     RESET_COUNT = 20
00033     RESET_DELAY_BASE = 1.0
00034     RESET_DELAY_INC = 0.02
00035     VALUE_PLACEHOLDER = "0"
00036 
00037     def setup (self):
00038         """Register callbacks required for the test"""
00039         self._error  = False
00040         generator = self.unexpected_reset_test ()
00041         generator.next()
00042 
00043         def run_gen(key, value, time):
00044             """Run the generator, and fail testing if the iterator stops"""
00045             if self._error :
00046                 return
00047             try:
00048                 generator.send((key, value, time))
00049             except StopIteration:
00050                 self._error  = True
00051 
00052         for resp in ("start", "read", "format_done", "reset_complete"):
00053             self.register_callback(resp, run_gen)
00054 
00055     def teardown (self):
00056         """No work to do here"""
00057         pass
00058 
00059     def unexpected_reset_test (self):
00060         """Generator for running the reset test
00061         
00062         This function calls yield to wait for the next event from
00063         the device. If the device gives the wrong response, then the
00064         generator terminates by returing which raises a StopIteration
00065         exception and fails the test.
00066         """
00067 
00068         # Wait for start token
00069         key, value, time = yield
00070         if key != "start":
00071             return
00072 
00073         # Format the device before starting the test
00074         self.send_kv("format", self.VALUE_PLACEHOLDER)
00075         key, value, time = yield
00076         if key != "format_done":
00077             return
00078 
00079         for i in range(self.RESET_COUNT):
00080 
00081             self.send_kv("run", self.VALUE_PLACEHOLDER)
00082             sleep(self.RESET_DELAY_BASE + self.RESET_DELAY_INC * i)
00083 
00084             self.reset()
00085 
00086             # Wait for start token
00087             key, value, time = yield
00088             self.log("Key from yield: %s" % key)
00089             if key != "reset_complete":
00090                 return
00091 
00092 
00093             self.send_kv("__sync", "00000000-0000-000000000-000000000000")
00094 
00095             # Wait for start token
00096             key, value, time = yield
00097             if key != "start":
00098                 return
00099 
00100         self.send_kv("exit", "pass")
00101 
00102         yield    # No more events expected
00103