Przeglądaj źródła

add time module support

Oliver Fabel 1 rok temu
rodzic
commit
87cc273d66

+ 1 - 2
Makefile

@@ -3,8 +3,7 @@ MICROPYTHON_TOP = ./lib/micropython
 
 PACKAGE_DIR = ./lib/micropython-build
 
-# Include the main makefile fragment to build the MicroPython component.
-include $(MICROPYTHON_TOP)/ports/embed/embed.mk
+include ./Makefile.micropython
 
 $(PACKAGE_DIR): all
 

+ 68 - 0
Makefile.micropython

@@ -0,0 +1,68 @@
+# This file is part of the MicroPython project, http://micropython.org/
+# The MIT License (MIT)
+# Copyright (c) 2022-2023 Damien P. George
+#
+# This file is intended to be included by a Makefile in a custom project.
+
+# Set the build output directory for the generated files.
+BUILD = build-embed
+
+# Include the core environment definitions; this will set $(TOP).
+include $(MICROPYTHON_TOP)/py/mkenv.mk
+
+# Include py core make definitions.
+include $(TOP)/py/py.mk
+include $(TOP)/extmod/extmod.mk
+
+# Set the location of the MicroPython embed port.
+MICROPYTHON_EMBED_PORT = $(MICROPYTHON_TOP)/ports/embed
+
+# Set default makefile-level MicroPython feature configurations.
+MICROPY_ROM_TEXT_COMPRESSION ?= 0
+
+# Set CFLAGS for the MicroPython build.
+CFLAGS += -I. -I$(TOP) -I$(BUILD) -I$(MICROPYTHON_EMBED_PORT)
+CFLAGS += -Wall -Werror -std=c99
+
+# Define the required generated header files.
+GENHDR_OUTPUT = $(addprefix $(BUILD)/genhdr/, \
+	moduledefs.h \
+	mpversion.h \
+	qstrdefs.generated.h \
+	root_pointers.h \
+	)
+
+# Define the top-level target, the generated output files.
+.PHONY: all
+all: micropython-embed-package
+
+clean: clean-micropython-embed-package
+
+.PHONY: clean-micropython-embed-package
+clean-micropython-embed-package:
+	$(RM) -rf $(PACKAGE_DIR)
+
+PACKAGE_DIR ?= micropython_embed
+PACKAGE_DIR_LIST = $(addprefix $(PACKAGE_DIR)/,py extmod shared/runtime genhdr port)
+
+.PHONY: micropython-embed-package
+micropython-embed-package: $(GENHDR_OUTPUT)
+	$(ECHO) "Generate micropython_embed output:"
+	$(Q)$(RM) -rf $(PACKAGE_DIR_LIST)
+	$(Q)$(MKDIR) -p $(PACKAGE_DIR_LIST)
+	$(ECHO) "- py"
+	$(Q)$(CP) $(TOP)/py/*.[ch] $(PACKAGE_DIR)/py
+	$(ECHO) "- extmod"
+	$(Q)$(CP) $(TOP)/extmod/modplatform.h $(PACKAGE_DIR)/extmod
+	$(Q)$(CP) $(TOP)/extmod/modtime.h $(PACKAGE_DIR)/extmod
+	$(Q)$(CP) $(TOP)/extmod/modtime.c $(PACKAGE_DIR)/extmod
+	$(ECHO) "- shared"
+	$(Q)$(CP) $(TOP)/shared/runtime/gchelper.h $(PACKAGE_DIR)/shared/runtime
+	$(Q)$(CP) $(TOP)/shared/runtime/gchelper_generic.c $(PACKAGE_DIR)/shared/runtime
+	$(ECHO) "- genhdr"
+	$(Q)$(CP) $(GENHDR_OUTPUT) $(PACKAGE_DIR)/genhdr
+	$(ECHO) "- port"
+	$(Q)$(CP) $(MICROPYTHON_EMBED_PORT)/port/*.[ch] $(PACKAGE_DIR)/port
+
+# Include remaining core make rules.
+include $(TOP)/py/mkrules.mk

+ 9 - 0
examples/dict.py

@@ -0,0 +1,9 @@
+d = dict()
+
+for i in range(1,100):
+  d[str(i)] = i
+
+for k,v in d.items():
+  val = '{0} = {1}'.format(k, v)
+
+  print(val)

+ 3 - 0
examples/gc.py

@@ -0,0 +1,3 @@
+import gc
+
+gc.collect()

+ 1 - 0
examples/overflow.py

@@ -0,0 +1 @@
+data = list(range(0,9999))

+ 3 - 0
examples/sleep.py

@@ -0,0 +1,3 @@
+import time
+
+time.sleep(5)

+ 65 - 0
lib/micropython-port/file_reader.c

@@ -0,0 +1,65 @@
+#include <stdio.h>
+
+#include <furi.h>
+#include <storage/storage.h>
+
+#include <py/reader.h>
+
+#include "state.h"
+
+typedef struct {
+    size_t pointer;
+    char* content;
+    size_t size;
+} FileReaderContext;
+
+FileReaderContext* file_reader_context_alloc(File* file) {
+    FileReaderContext* context = malloc(sizeof(FileReaderContext));
+
+    context->pointer = 0;
+    context->size = storage_file_size(file);
+    context->content = malloc(context->size * sizeof(char));
+
+    storage_file_read(file, context->content, context->size);
+
+    return context;
+}
+
+void file_reader_context_free(FileReaderContext* context) {
+    free(context->content);
+    free(context);
+}
+
+mp_uint_t file_reader_read(void* data) {
+    FileReaderContext* context = data;
+
+    if(context->size == 0 || context->pointer >= context->size) {
+        return MP_READER_EOF;
+    }
+
+    return context->content[context->pointer++];
+}
+
+void file_reader_close(void* data) {
+    file_reader_context_free(data);
+}
+
+void mp_reader_new_file(mp_reader_t* reader, qstr filename) {
+    FuriString* file_path = furi_string_alloc();
+    Storage* storage = furi_record_open(RECORD_STORAGE);
+    File* file = storage_file_alloc(storage);
+
+    furi_string_printf(file_path, "%s/%s", root_module_path, qstr_str(filename));
+
+    if(!storage_file_open(file, furi_string_get_cstr(file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
+        furi_crash("unable to open file");
+    }
+
+    reader->data = file_reader_context_alloc(file);
+    reader->readbyte = file_reader_read;
+    reader->close = file_reader_close;
+
+    storage_file_free(file);
+    furi_record_close(RECORD_STORAGE);
+    furi_string_free(file_path);
+}

+ 32 - 0
lib/micropython-port/modtime.c

@@ -0,0 +1,32 @@
+#include <furi.h>
+#include <furi_hal.h>
+
+#include <py/mphal.h>
+
+mp_uint_t mp_time_time_get(void) {
+    return furi_hal_rtc_get_timestamp();
+}
+
+uint64_t mp_hal_time_ns(void) {
+    return furi_hal_rtc_get_timestamp() * 1000;
+}
+
+mp_uint_t mp_hal_ticks_ms(void) {
+    return furi_kernel_get_tick_frequency() / 1000;
+}
+
+mp_uint_t mp_hal_ticks_us(void) {
+    return furi_kernel_get_tick_frequency() / 1000000;
+}
+
+mp_uint_t mp_hal_ticks_cpu(void) {
+    return furi_get_tick();
+}
+
+void mp_hal_delay_ms(mp_uint_t ms) {
+    furi_delay_ms(ms);
+}
+
+void mp_hal_delay_us(mp_uint_t us) {
+    furi_delay_us(us);
+}

+ 2 - 58
lib/micropython-port/mphalport.c

@@ -5,10 +5,11 @@
 
 #include <py/mphal.h>
 #include <py/builtin.h>
-#include <py/reader.h>
 
 #include "state.h"
 
+const mp_obj_fun_builtin_var_t mp_builtin_open_obj;
+
 void mp_hal_stdout_tx_str(const char* str) {
     printf("%s", str);
 }
@@ -31,60 +32,3 @@ mp_import_stat_t mp_import_stat(const char* path) {
 
     return stat;
 }
-
-typedef struct {
-    size_t pointer;
-    char* content;
-    size_t size;
-} FileReaderContext;
-
-FileReaderContext* file_reader_context_alloc(File* file) {
-    FileReaderContext* context = malloc(sizeof(FileReaderContext));
-
-    context->pointer = 0;
-    context->size = storage_file_size(file);
-    context->content = malloc(context->size * sizeof(char));
-
-    storage_file_read(file, context->content, context->size);
-
-    return context;
-}
-
-void file_reader_context_free(FileReaderContext* context) {
-    free(context->content);
-    free(context);
-}
-
-mp_uint_t file_reader_read(void* data) {
-    FileReaderContext* context = data;
-
-    if(context->size == 0 || context->pointer >= context->size) {
-        return MP_READER_EOF;
-    }
-
-    return context->content[context->pointer++];
-}
-
-void file_reader_close(void* data) {
-    file_reader_context_free(data);
-}
-
-void mp_reader_new_file(mp_reader_t* reader, qstr filename) {
-    FuriString* file_path = furi_string_alloc();
-    Storage* storage = furi_record_open(RECORD_STORAGE);
-    File* file = storage_file_alloc(storage);
-
-    furi_string_printf(file_path, "%s/%s", root_module_path, qstr_str(filename));
-
-    if(!storage_file_open(file, furi_string_get_cstr(file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
-        furi_crash("unable to open file");
-    }
-
-    reader->data = file_reader_context_alloc(file);
-    reader->readbyte = file_reader_read;
-    reader->close = file_reader_close;
-
-    storage_file_free(file);
-    furi_record_close(RECORD_STORAGE);
-    furi_string_free(file_path);
-}

+ 3 - 0
mp_flipper_app.c

@@ -56,6 +56,9 @@ int32_t mp_flipper_app(void* p) {
     const size_t stack_size = 2 * 1024;
     uint8_t* memory = malloc(memory_size * sizeof(uint8_t));
 
+    FURI_LOG_I(TAG, "allocated memory is %zu bytes", memory_size);
+    FURI_LOG_I(TAG, "stack size is %zu bytes", stack_size);
+
     FuriString* file_path = furi_string_alloc();
     FuriString* code = furi_string_alloc();
 

+ 4 - 1
mpconfigport.h

@@ -15,7 +15,7 @@ typedef long mp_off_t;
 
 #define MICROPY_MPHALPORT_H "lib/micropython-port/mphalport.h"
 
-#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_MINIMUM)
+#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_BASIC_FEATURES)
 
 #define MICROPY_ENABLE_COMPILER (1)
 #define MICROPY_ENABLE_GC (1)
@@ -28,3 +28,6 @@ typedef long mp_off_t;
 
 #define MICROPY_ENABLE_EXTERNAL_IMPORT (1)
 #define MICROPY_READER_VFS (1)
+
+#define MICROPY_PY_TIME (1)
+#define MICROPY_PY_TIME_TIME_TIME_NS (1)

+ 2 - 0
state.h

@@ -1,3 +1,5 @@
 #pragma once
 
+#define TAG "uPython"
+
 extern const char* root_module_path;