Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rtc_auto.py Source File

rtc_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 import re
00019 from time import time, strftime, gmtime
00020 
00021 class RTCTest():
00022     PATTERN_RTC_VALUE = "\[(\d+)\] \[(\d+-\d+-\d+ \d+:\d+:\d+ [AaPpMm]{2})\]"
00023     re_detect_rtc_value = re.compile(PATTERN_RTC_VALUE)
00024 
00025     def test(self, selftest):
00026         test_result = True
00027         start = time()
00028         sec_prev = 0
00029         for i in range(0, 5):
00030             # Timeout changed from default: we need to wait longer for some boards to start-up
00031             c = selftest.mbed.serial_readline(timeout=10)
00032             if c is None:
00033                 return selftest.RESULT_IO_SERIAL
00034             selftest.notify(c.strip())
00035             delta = time() - start
00036             m = self.re_detect_rtc_value.search(c)
00037             if m and len(m.groups()):
00038                 sec = int(m.groups()[0])
00039                 time_str = m.groups()[1]
00040                 correct_time_str = strftime("%Y-%m-%d %H:%M:%S %p", gmtime(float(sec)))
00041                 single_result = time_str == correct_time_str and sec > 0 and sec > sec_prev
00042                 test_result = test_result and single_result
00043                 result_msg = "OK" if single_result else "FAIL"
00044                 selftest.notify("HOST: [%s] [%s] received time %+d sec after %.2f sec... %s"% (sec, time_str, sec - sec_prev, delta, result_msg))
00045                 sec_prev = sec
00046             else:
00047                 test_result = False
00048                 break
00049             start = time()
00050         return selftest.RESULT_SUCCESS if test_result else selftest.RESULT_FAILURE