From d311655655b72fcb898a8d35bfe926a9adb1bebd Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Sat, 22 Mar 2014 15:06:29 +0000
Subject: [PATCH] stmhal: Add time module with sleep function.

---
 stmhal/Makefile       |  1 +
 stmhal/main.c         |  4 +++-
 stmhal/qstrdefsport.h |  4 ++++
 stmhal/timemodule.c   | 44 +++++++++++++++++++++++++++++++++++++++++++
 stmhal/timemodule.h   |  1 +
 5 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 stmhal/timemodule.c
 create mode 100644 stmhal/timemodule.h

diff --git a/stmhal/Makefile b/stmhal/Makefile
index 9d4640d1f..60e5fde3d 100644
--- a/stmhal/Makefile
+++ b/stmhal/Makefile
@@ -74,6 +74,7 @@ SRC_C = \
 	pyexec.c \
 	pybmodule.c \
 	osmodule.c \
+	timemodule.c \
 	import.c \
 	lexerfatfs.c \
 	gpio.c \
diff --git a/stmhal/main.c b/stmhal/main.c
index 22f7be23e..2b4daa370 100644
--- a/stmhal/main.c
+++ b/stmhal/main.c
@@ -23,6 +23,7 @@
 #include "pyexec.h"
 #include "pybmodule.h"
 #include "osmodule.h"
+#include "timemodule.h"
 #include "usart.h"
 #include "led.h"
 #include "exti.h"
@@ -276,9 +277,10 @@ soft_reset:
     // probably shouldn't do this, so we are compatible with CPython
     rt_store_name(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
 
-    // pre-import the os module
+    // pre-import the os and time modules
     // TODO don't do this! (need a way of registering builtin modules...)
     rt_store_name(MP_QSTR_os, (mp_obj_t)&os_module);
+    rt_store_name(MP_QSTR_time, (mp_obj_t)&time_module);
 
     // check if user switch held (initiates reset of filesystem)
     bool reset_filesystem = false;
diff --git a/stmhal/qstrdefsport.h b/stmhal/qstrdefsport.h
index be30848e3..9961102a3 100644
--- a/stmhal/qstrdefsport.h
+++ b/stmhal/qstrdefsport.h
@@ -66,3 +66,7 @@ Q(rmdir)
 Q(unlink)
 Q(sep)
 Q(urandom)
+
+// for time module
+Q(time)
+Q(sleep)
diff --git a/stmhal/timemodule.c b/stmhal/timemodule.c
new file mode 100644
index 000000000..b2dac6a54
--- /dev/null
+++ b/stmhal/timemodule.c
@@ -0,0 +1,44 @@
+#include <stm32f4xx_hal.h>
+
+#include "nlr.h"
+#include "misc.h"
+#include "mpconfig.h"
+#include "qstr.h"
+#include "obj.h"
+#include "map.h"
+#include "timemodule.h"
+
+STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
+#if MICROPY_ENABLE_FLOAT
+    if (MP_OBJ_IS_INT(seconds_o)) {
+#endif
+        HAL_Delay(1000 * mp_obj_get_int(seconds_o));
+#if MICROPY_ENABLE_FLOAT
+    } else {
+        HAL_Delay((uint32_t)(1000 * mp_obj_get_float(seconds_o)));
+    }
+#endif
+    return mp_const_none;
+}
+
+MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
+
+STATIC const mp_map_elem_t time_module_globals_table[] = {
+    { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_time) },
+
+    { MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
+};
+
+STATIC const mp_map_t time_module_globals = {
+    .all_keys_are_qstrs = 1,
+    .table_is_fixed_array = 1,
+    .used = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
+    .alloc = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
+    .table = (mp_map_elem_t*)time_module_globals_table,
+};
+
+const mp_obj_module_t time_module = {
+    .base = { &mp_type_module },
+    .name = MP_QSTR_time,
+    .globals = (mp_map_t*)&time_module_globals,
+};
diff --git a/stmhal/timemodule.h b/stmhal/timemodule.h
new file mode 100644
index 000000000..70e35b1fd
--- /dev/null
+++ b/stmhal/timemodule.h
@@ -0,0 +1 @@
+extern const mp_obj_module_t time_module;
-- 
GitLab