Просмотр исходного кода

Merge pull request #67 from Lawlieta/master

【添加】machine.RTC 模块
朱天龙 (Armink) 6 лет назад
Родитель
Сommit
8d4ae548c0

+ 56 - 0
docs/04-Hardware_Control_Module/07-machine-RTC.md

@@ -0,0 +1,56 @@
+## machine.RTC
+
+**machine.RTC** 类是 machine 模块下面的一个硬件类,用于对指定 RTC 设备的配置和控制,提供对 RTC 设备的操作方法。
+
+- RTC(Real-Time Clock )实时时钟可以提供精确的实时时间,它可以用于产生年、月、日、时、分、秒等信息。
+
+### 构造函数
+
+在 RT-Thread MicroPython 中 `RTC` 对象的构造函数如下:
+
+#### **class machine.RTC**()
+
+所以在给定的总线上构造一个 `RTC` 对象,无入参对象,使用方式可参考 [示例](#_3)。 
+
+### 方法
+
+#### **RTC.init**(datetime)
+
+根据传入的参数初始化 RTC 设备起始时间。入参 `datetime` 为一个时间元组,格式如下:
+
+```
+(year, month, day, wday, hour, minute, second, yday)
+```
+参数介绍如下所示:
+
+- **year**:年份;
+- **month**:月份,范围 [1, 12];
+- **day**:日期,范围 [1, 31];
+- **wday**:星期,范围 [0, 6],0 表示星期一,以此类推;
+- **hour**:小时,范围 [0, 23];
+- **minute**:分钟,范围[0, 59];
+- **second**:秒,范围[0, 59];
+- **yday**:从当前年份 1 月 1 日开始的天数,范围 [0, 365],一般置位 0 未实现。
+
+使用的方式可参考 [示例](#_3)。
+
+#### **RTC.deinit**()
+
+重置 RTC 设备时间到 2015 年 1 月 1日,重新运行 RTC 设备。
+
+#### **RTC.now**()
+
+获取当前时间,返回值为上述 `datetime` 时间元组格式。
+
+### 示例
+
+```python
+>>> from machine import RTC
+>>> rtc = RTC()                        # 创建 RTC 设备对象
+>>> rtc.init((2019,6,5,2,10,22,30,0))  # 设置初始化时间
+>>> rtc.now()                          # 获取当前时间
+(2019, 6, 5, 2, 10, 22, 40, 0)
+>>> rtc.deinit()                       # 重置时间到2015年1月1日
+>>> rtc.now()                          # 获取当前时间
+(2015, 1, 1, 3, 0, 0, 1, 0)
+```

+ 2 - 1
port/genhdr/qstrdefs.generated.h

@@ -721,5 +721,6 @@ QDEF(MP_QSTR_STAT_NO_AP_FOUND, (const byte*)"\xee\x10" "STAT_NO_AP_FOUND")
 QDEF(MP_QSTR_STAT_WRONG_PASSWORD, (const byte*)"\x0b\x13" "STAT_WRONG_PASSWORD")
 QDEF(MP_QSTR_STAT_CONNECTING, (const byte*)"\xf6\x0f" "STAT_CONNECTING")
 QDEF(MP_QSTR_STAT_IDLE, (const byte*)"\x0c\x09" "STAT_IDLE")
-
+QDEF(MP_QSTR_now, (const byte*)"\xb3\x03" "now")
+QDEF(MP_QSTR_RTC, (const byte*)"\xa0\x03" "RTC")
 // This file was automatically generated by makeqstrdata.py

+ 155 - 0
port/machine_rtc.c

@@ -0,0 +1,155 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "py/nlr.h"
+#include "py/obj.h"
+#include "py/runtime.h"
+#include "py/mphal.h"
+#include "lib/timeutils/timeutils.h"
+#include "modmachine.h"
+
+#ifdef MICROPYTHON_USING_MACHINE_RTC
+
+#include <rtthread.h>
+#include <drivers/rtc.h>
+#include <time.h>
+
+#define MP_YEAR_BASE   1900
+
+const mp_obj_type_t machine_rtc_type;
+
+// singleton RTC object
+STATIC const mp_obj_base_t machine_rtc_obj = {&machine_rtc_type};
+
+STATIC void error_check(bool status, const char *msg) {
+    if (!status) {
+        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, msg));
+    }
+}
+
+STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
+#define MP_RTC_DEV_NAME "rtc"
+    rt_device_t rtc_deivce = RT_NULL;
+
+    // check arguments
+    mp_arg_check_num(n_args, n_kw, 0, 0, false);
+
+    // check RTC device
+    rtc_deivce = rt_device_find(MP_RTC_DEV_NAME);
+    if (rtc_deivce == RT_NULL || rtc_deivce->type != RT_Device_Class_RTC) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "RTC(%s) don't exist", MP_RTC_DEV_NAME)); 
+    }
+
+    // return constant object
+    return (mp_obj_t)&machine_rtc_obj;
+}
+
+STATIC mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *args) {
+    if (n_args == 1) {
+        struct tm *tblock;
+        time_t t;
+        // Get time
+        t = time(RT_NULL);
+        tblock = localtime(&t);
+
+        mp_uint_t seconds = timeutils_mktime(tblock->tm_year + MP_YEAR_BASE, tblock->tm_mon + 1, tblock->tm_mday,
+                                             tblock->tm_hour, tblock->tm_min, tblock->tm_sec);
+        timeutils_struct_time_t tm;
+        timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
+
+        mp_obj_t tuple[8] = {
+            mp_obj_new_int(tm.tm_year),
+            mp_obj_new_int(tm.tm_mon),
+            mp_obj_new_int(tm.tm_mday),
+            mp_obj_new_int(tm.tm_wday),
+            mp_obj_new_int(tm.tm_hour),
+            mp_obj_new_int(tm.tm_min),
+            mp_obj_new_int(tm.tm_sec),
+            mp_obj_new_int(0)
+        };
+
+        return mp_obj_new_tuple(8, tuple);
+    } else {
+        // Set time
+        rt_err_t result;
+        mp_obj_t *items;
+
+        mp_obj_get_array_fixed_n(args[1], 8, &items);
+        result = set_date(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]));
+        error_check(result == RT_EOK, "Set date error");
+        result = set_time(mp_obj_get_int(items[4]), mp_obj_get_int(items[5]), mp_obj_get_int(items[6]));
+        error_check(result == RT_EOK, "Set time error");
+        return mp_const_none;
+    }
+}
+
+STATIC mp_obj_t machine_rtc_now(mp_uint_t n_args, const mp_obj_t *args) {
+    return machine_rtc_datetime_helper(1, args);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_now_obj, 0, 1, machine_rtc_now);
+
+STATIC mp_obj_t machine_rtc_init(mp_uint_t n_args, const mp_obj_t *args) {
+    return machine_rtc_datetime_helper(n_args, args);
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_init_obj, 1, 2, machine_rtc_init);
+
+STATIC mp_obj_t machine_rtc_deinit(mp_uint_t n_args, const mp_obj_t *args) {
+    rt_err_t result;
+    struct tm tblock;
+
+    tblock.tm_year = 2015 - MP_YEAR_BASE;
+    tblock.tm_mon = 0;
+    tblock.tm_mday = 1;
+    tblock.tm_hour = 0;
+    tblock.tm_min = 0;
+    tblock.tm_sec = 0;
+    result = set_date(tblock.tm_year + MP_YEAR_BASE, tblock.tm_mon + 1, tblock.tm_mday);
+    error_check(result == RT_EOK, "Set date error");
+    result = set_time(tblock.tm_hour, tblock.tm_min, tblock.tm_sec);
+    error_check(result == RT_EOK, "Set time error");
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_deinit_obj, 0, 1, machine_rtc_deinit);
+
+STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
+    { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_rtc_init_obj) },
+    { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_rtc_deinit_obj) },
+    { MP_ROM_QSTR(MP_QSTR_now), MP_ROM_PTR(&machine_rtc_now_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);
+
+const mp_obj_type_t machine_rtc_type = {
+    { &mp_type_type },
+    .name = MP_QSTR_RTC,
+    .make_new = machine_rtc_make_new,
+    .locals_dict = (mp_obj_t) &machine_rtc_locals_dict,
+};
+
+#endif // MICROPYTHON_USING_MACHINE_RTC

+ 35 - 0
port/machine_rtc.h

@@ -0,0 +1,35 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_MACHINE_RTC_H
+#define MICROPY_INCLUDED_MACHINE_RTC_H
+
+#include "py/obj.h"
+#include <rtthread.h>
+
+extern const mp_obj_type_t machine_rtc_type;
+
+#endif // MICROPY_INCLUDED_MACHINE_RTC_H

+ 5 - 2
port/modmachine.c

@@ -38,6 +38,7 @@
 #include "extmod/machine_spi.h"
 #include "modmachine.h"
 #include "machine_uart.h"
+#include "machine_rtc.h"
 
 #include <rthw.h>
 
@@ -202,9 +203,11 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
     { MP_ROM_QSTR(MP_QSTR_SPI),                 MP_ROM_PTR(&mp_machine_soft_spi_type) },
 #endif
 #if MICROPY_PY_MACHINE_UART
-    { MP_ROM_QSTR(MP_QSTR_UART),                MP_ROM_PTR(&machine_uart_type ) },
+    { MP_ROM_QSTR(MP_QSTR_UART),                MP_ROM_PTR(&machine_uart_type) },
+#endif
+#if MICROPY_PY_MACHINE_RTC
+    { MP_ROM_QSTR(MP_QSTR_RTC),                MP_ROM_PTR(&machine_rtc_type) },
 #endif
-
 };
 
 STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);

+ 4 - 0
port/mpconfigport.h

@@ -143,6 +143,10 @@
 #define MICROPY_PY_MACHINE_UART      (1)
 #endif
 
+#ifdef MICROPYTHON_USING_MACHINE_RTC
+#define MICROPY_PY_MACHINE_RTC       (1)
+#endif
+
 /*****************************************************************************/
 /* System Module                                                             */