Kaynağa Gözat

【添加】RTC 驱动适配

Signed-off-by: chenyong <1521761801@qq.com>
chenyong 6 yıl önce
ebeveyn
işleme
88886d3297

+ 4 - 3
port/genhdr/qstrdefs.generated.h

@@ -697,7 +697,7 @@ QDEF(MP_QSTR_ffimod, (const byte*)"\xca\x06" "ffimod")
 QDEF(MP_QSTR_ffifunc, (const byte*)"\x92\x07" "ffifunc")
 QDEF(MP_QSTR_fficallback, (const byte*)"\xc5\x0b" "fficallback")
 QDEF(MP_QSTR_ffivar, (const byte*)"\x49\x06" "ffivar")
-QDEF(MP_QSTR_network, (const byte*)"\x5b\x07" "network")
+QDEF(MP_QSTR_now, (const byte*)"\xb3\x03" "now")
 QDEF(MP_QSTR_isconnected, (const byte*)"\x80\x0b" "isconnected")
 QDEF(MP_QSTR_WLAN, (const byte*)"\x11\x04" "WLAN")
 QDEF(MP_QSTR_disconnect, (const byte*)"\xa5\x0a" "disconnect")
@@ -709,7 +709,7 @@ QDEF(MP_QSTR_status, (const byte*)"\x71\x06" "status")
 QDEF(MP_QSTR_rssi, (const byte*)"\x7e\x04" "rssi")
 QDEF(MP_QSTR_config, (const byte*)"\x4f\x06" "config")
 QDEF(MP_QSTR_ifconfig, (const byte*)"\xe0\x08" "ifconfig")
-QDEF(MP_QSTR_mac, (const byte*)"\xaa\x03" "mac")
+QDEF(MP_QSTR_RTC, (const byte*)"\xa0\x03" "RTC")
 QDEF(MP_QSTR_essid, (const byte*)"\x4d\x05" "essid")
 QDEF(MP_QSTR_hidden, (const byte*)"\xef\x06" "hidden")
 QDEF(MP_QSTR_authmode, (const byte*)"\xce\x08" "authmode")
@@ -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

+ 146 - 0
port/machine_rtc.c

@@ -0,0 +1,146 @@
+/*
+ * 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 <rtthread.h>
+#include <drivers/rtc.h>
+#include <time.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
+
+#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 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, 1, 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) {
+        rt_kprintf("ERROR: RTC device %s not found!\n", MP_RTC_DEV_NAME);
+        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);
+
+        //printf("%d %d %d %d %d %d\n", tblock->tm_year + MP_YEAR_BASE, tblock->tm_mon + 1, tblock->tm_mday, tblock->tm_hour, tblock->tm_min, tblock->tm_sec);
+
+        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
+
+        mp_obj_t *items;
+        mp_obj_get_array_fixed_n(args[1], 8, &items);
+        set_date(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]));
+        set_time(mp_obj_get_int(items[4]), mp_obj_get_int(items[5]), mp_obj_get_int(items[6]));
+        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) {
+    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;
+    set_date(tblock.tm_year + MP_YEAR_BASE, tblock.tm_mon + 1, tblock.tm_mday);
+    set_time(tblock.tm_hour, tblock.tm_min, tblock.tm_sec);
+    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                                                             */