Skip to content

Commit

Permalink
codal_port: Update to build with latest micropython.
Browse files Browse the repository at this point in the history
The only user-facing change is the addition of time.ticks_cpu().

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Jan 15, 2024
1 parent c648f52 commit fdaf840
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 136 deletions.
7 changes: 1 addition & 6 deletions src/codal_port/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ SRC_C += \
modradio.c \
modspeech.c \
modthis.c \
modutime.c \
mphalport.c \

SRC_C += \
Expand Down Expand Up @@ -135,11 +134,7 @@ all: lib $(MBIT_VER_FILE)
# Also rebuild MicroPython version header in correct directory to pick up git hash.
$(MBIT_VER_FILE): FORCE
(cd $(TOP) && $(PYTHON) py/makeversionhdr.py $(abspath $(MP_VER_FILE)))
$(PYTHON) $(TOP)/py/makeversionhdr.py $(MBIT_VER_FILE).pre
$(CAT) $(MBIT_VER_FILE).pre | $(SED) s/MICROPY_/MICROBIT_/ > $(MBIT_VER_FILE)

# Suppress warnings bulding stackctrl.c (fixed post v1.20.0).
$(BUILD)/py/stackctrl.o: CWARN += -Wno-dangling-pointer
$(PYTHON) make_microbit_version_hdr.py $(MBIT_VER_FILE)

# Suppress warnings from SAM library.
$(BUILD)/$(abspath $(LOCAL_LIB_DIR))/sam/sam.o: CWARN += -Wno-array-bounds
Expand Down
2 changes: 1 addition & 1 deletion src/codal_port/drv_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ STATIC uint8_t monochrome_get_pixel(monochrome_5by5_t *self, mp_int_t x, mp_int_
}

greyscale_t *greyscale_new(mp_int_t w, mp_int_t h) {
greyscale_t *result = m_new_obj_var(greyscale_t, uint8_t, (w*h+1)>>1);
greyscale_t *result = m_new_obj_var(greyscale_t, byte_data, uint8_t, (w*h+1)>>1);
result->base.type = &microbit_image_type;
result->five = 0;
result->width = w;
Expand Down
12 changes: 6 additions & 6 deletions src/codal_port/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "shared/readline/readline.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"
#include "ports/nrf/modules/uos/microbitfs.h"
#include "ports/nrf/modules/os/microbitfs.h"
#include "drv_softtimer.h"
#include "drv_system.h"
#include "drv_display.h"
Expand Down Expand Up @@ -136,7 +136,7 @@ void microbit_pyexec_file(const char *filename) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
// Parse and comple the file.
mp_lexer_t *lex = mp_lexer_new_from_file(filename);
mp_lexer_t *lex = mp_lexer_new_from_file(qstr_from_str(filename));
qstr source_name = lex->source_name;
mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false);
Expand Down Expand Up @@ -194,16 +194,16 @@ void microbit_file_opened_for_writing(const char *name, size_t name_len) {
}

#if MICROPY_MBFS
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
return uos_mbfs_new_reader(filename);
mp_lexer_t *mp_lexer_new_from_file(qstr filename) {
return os_mbfs_new_reader(qstr_str(filename));
}

mp_import_stat_t mp_import_stat(const char *path) {
return uos_mbfs_import_stat(path);
return os_mbfs_import_stat(path);
}

mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
return uos_mbfs_open(n_args, args);
return os_mbfs_open(n_args, args);
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
#endif
Expand Down
120 changes: 120 additions & 0 deletions src/codal_port/make_microbit_version_hdr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""
Generate header file with macros defining micro:bit version info.
Adapted from MicroPython's py/makeversionhdr.py file at v1.21.0.
"""

import argparse
import sys
import os
import datetime
import subprocess


def get_version_info_from_git(repo_path):
# Note: git describe doesn't work if no tag is available
try:
git_tag = subprocess.check_output(
["git", "describe", "--tags", "--dirty", "--always", "--match", "v[1-9].*"],
cwd=repo_path,
stderr=subprocess.STDOUT,
universal_newlines=True,
).strip()
except subprocess.CalledProcessError as er:
if er.returncode == 128:
# git exit code of 128 means no repository found
return None
git_tag = ""
except OSError:
return None
try:
git_hash = subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
cwd=repo_path,
stderr=subprocess.STDOUT,
universal_newlines=True,
).strip()
except subprocess.CalledProcessError:
git_hash = "unknown"
except OSError:
return None

try:
# Check if there are any modified files.
subprocess.check_call(
["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"],
cwd=repo_path,
stderr=subprocess.STDOUT,
)
# Check if there are any staged files.
subprocess.check_call(
["git", "diff-index", "--cached", "--quiet", "HEAD", "--"],
cwd=repo_path,
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError:
git_hash += "-dirty"
except OSError:
return None

return git_tag, git_hash


def make_version_header(repo_path, filename):
info = None
if "MICROBIT_GIT_TAG" in os.environ:
info = [os.environ["MICROBIT_GIT_TAG"], os.environ["MICROBIT_GIT_HASH"]]
if info is None:
info = get_version_info_from_git(repo_path)

git_tag, git_hash = info

build_date = datetime.date.today()
if "SOURCE_DATE_EPOCH" in os.environ:
build_date = datetime.datetime.utcfromtimestamp(
int(os.environ["SOURCE_DATE_EPOCH"])
).date()

# Generate the file with the git and version info
file_data = """\
// This file was generated by py/makeversionhdr.py
#define MICROBIT_GIT_TAG "%s"
#define MICROBIT_GIT_HASH "%s"
#define MICROBIT_BUILD_DATE "%s"
""" % (
git_tag,
git_hash,
build_date.strftime("%Y-%m-%d"),
)

# Check if the file contents changed from last time
write_file = True
if os.path.isfile(filename):
with open(filename, "r") as f:
existing_data = f.read()
if existing_data == file_data:
write_file = False

# Only write the file if we need to
if write_file:
print("GEN %s" % filename)
with open(filename, "w") as f:
f.write(file_data)


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-r",
"--repo-path",
default=os.path.join(os.path.dirname(sys.argv[0]), ".."),
help="path to git repo to query for version",
)
parser.add_argument("dest", nargs=1, help="output file path")
args = parser.parse_args()

make_version_header(args.repo_path, args.dest[0])


if __name__ == "__main__":
main()
Loading

0 comments on commit fdaf840

Please sign in to comment.