From 3bd0e863036592faf8457253966185d645281474 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Tue, 18 Jun 2019 23:40:02 +0200
Subject: [PATCH] feat(pycardium): Enable custom modules

Signed-off-by: Rahix <rahix@rahix.de>
---
 pycardium/meson.build        |  5 +++--
 pycardium/modules/qstrdefs.h | 12 ++++++++++++
 pycardium/modules/utime.c    | 28 ++++++++++++++++++++++++++++
 pycardium/mpconfigport.h     |  4 ++++
 pycardium/mphalport.c        | 14 ++++++++++++++
 5 files changed, 61 insertions(+), 2 deletions(-)
 create mode 100644 pycardium/modules/qstrdefs.h
 create mode 100644 pycardium/modules/utime.c

diff --git a/pycardium/meson.build b/pycardium/meson.build
index de4d35be..a8de25d1 100644
--- a/pycardium/meson.build
+++ b/pycardium/meson.build
@@ -1,6 +1,7 @@
 name = 'pycardium'
 
 modsrc = files(
+  'modules/utime.c',
 )
 
 #################################
@@ -24,7 +25,7 @@ qstr_h = custom_target(
   'qstrdefs.generated.h',
   output: 'qstrdefs.generated.h',
   input: [
-    # 'modules/qstrdefs.h',
+    'modules/qstrdefs.h',
     'mpconfigport.h',
     micropython_sources,
   ],
@@ -54,8 +55,8 @@ executable(
   mp_headers,
   include_directories: micropython_includes,
   dependencies: [max32665_startup_core1, board_card10, periphdriver, api_caller],
-  link_whole: [max32665_startup_core1_lib, board_card10_lib],
   link_with: upy,
+  link_whole: [max32665_startup_core1_lib, board_card10_lib],
   link_args: [
     '-Wl,-Map=' + meson.current_build_dir() + '/' + name + '.map',
   ],
diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h
new file mode 100644
index 00000000..26644086
--- /dev/null
+++ b/pycardium/modules/qstrdefs.h
@@ -0,0 +1,12 @@
+#include "py/mpconfig.h"
+
+/* utime */
+Q(utime)
+Q(sleep)
+Q(sleep_ms)
+Q(sleep_us)
+Q(ticks_ms)
+Q(ticks_us)
+Q(ticks_cpu)
+Q(ticks_add)
+Q(ticks_diff)
diff --git a/pycardium/modules/utime.c b/pycardium/modules/utime.c
new file mode 100644
index 00000000..7cf3d93f
--- /dev/null
+++ b/pycardium/modules/utime.c
@@ -0,0 +1,28 @@
+#include "py/mpconfig.h"
+#include "extmod/utime_mphal.h"
+
+#include "mxc_delay.h"
+
+static const mp_rom_map_elem_t time_module_globals_table[] = {
+	{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime)},
+	{MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj)},
+	{MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj)},
+	{MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj)},
+#if 0
+	/* TODO: Implement those */
+	{MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj)},
+	{MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj)},
+	{MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj)},
+	{MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj)},
+	{MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj)},
+#endif
+};
+static MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
+
+const mp_obj_module_t mp_module_utime = {
+	.base = { &mp_type_module },
+	.globals = (mp_obj_dict_t*)&time_module_globals,
+};
+
+/* Register the module to make it available in Python */
+MP_REGISTER_MODULE(MP_QSTR_utime, mp_module_utime, MODULE_UTIME_ENABLED);
diff --git a/pycardium/mpconfigport.h b/pycardium/mpconfigport.h
index 3418da71..48bdf1c8 100644
--- a/pycardium/mpconfigport.h
+++ b/pycardium/mpconfigport.h
@@ -17,6 +17,10 @@
 #define MICROPY_LONGINT_IMPL                (MICROPY_LONGINT_IMPL_LONGLONG)
 #define MICROPY_PY_BUILTINS_HELP            (1)
 #define MICROPY_PY_BUILTINS_HELP_MODULES    (1)
+#define MICROPY_PY_UTIME_MP_HAL             (1)
+
+/* Modules */
+#define MODULE_UTIME_ENABLED                (1)
 
 /*
  * This port is intended to be 32-bit, but unfortunately, int32_t for
diff --git a/pycardium/mphalport.c b/pycardium/mphalport.c
index 26521235..336ccce2 100644
--- a/pycardium/mphalport.c
+++ b/pycardium/mphalport.c
@@ -24,6 +24,20 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len)
 	epic_uart_write_str(str, len);
 }
 
+/******************************************************************************
+ * Time & Delay
+ */
+
+void mp_hal_delay_ms(mp_uint_t ms)
+{
+	mxc_delay(ms * 1000);
+}
+
+void mp_hal_delay_us(mp_uint_t us)
+{
+	mxc_delay(us);
+}
+
 /******************************************************************************
  * Fatal Errors
  */
-- 
GitLab