Skip to content

Commit fdaf840

Browse files
committed
codal_port: Update to build with latest micropython.
The only user-facing change is the addition of time.ticks_cpu(). Signed-off-by: Damien George <[email protected]>
1 parent c648f52 commit fdaf840

File tree

11 files changed

+198
-136
lines changed

11 files changed

+198
-136
lines changed

src/codal_port/Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ SRC_C += \
9898
modradio.c \
9999
modspeech.c \
100100
modthis.c \
101-
modutime.c \
102101
mphalport.c \
103102

104103
SRC_C += \
@@ -135,11 +134,7 @@ all: lib $(MBIT_VER_FILE)
135134
# Also rebuild MicroPython version header in correct directory to pick up git hash.
136135
$(MBIT_VER_FILE): FORCE
137136
(cd $(TOP) && $(PYTHON) py/makeversionhdr.py $(abspath $(MP_VER_FILE)))
138-
$(PYTHON) $(TOP)/py/makeversionhdr.py $(MBIT_VER_FILE).pre
139-
$(CAT) $(MBIT_VER_FILE).pre | $(SED) s/MICROPY_/MICROBIT_/ > $(MBIT_VER_FILE)
140-
141-
# Suppress warnings bulding stackctrl.c (fixed post v1.20.0).
142-
$(BUILD)/py/stackctrl.o: CWARN += -Wno-dangling-pointer
137+
$(PYTHON) make_microbit_version_hdr.py $(MBIT_VER_FILE)
143138

144139
# Suppress warnings from SAM library.
145140
$(BUILD)/$(abspath $(LOCAL_LIB_DIR))/sam/sam.o: CWARN += -Wno-array-bounds

src/codal_port/drv_image.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ STATIC uint8_t monochrome_get_pixel(monochrome_5by5_t *self, mp_int_t x, mp_int_
4343
}
4444

4545
greyscale_t *greyscale_new(mp_int_t w, mp_int_t h) {
46-
greyscale_t *result = m_new_obj_var(greyscale_t, uint8_t, (w*h+1)>>1);
46+
greyscale_t *result = m_new_obj_var(greyscale_t, byte_data, uint8_t, (w*h+1)>>1);
4747
result->base.type = &microbit_image_type;
4848
result->five = 0;
4949
result->width = w;

src/codal_port/main.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "shared/readline/readline.h"
3737
#include "shared/runtime/gchelper.h"
3838
#include "shared/runtime/pyexec.h"
39-
#include "ports/nrf/modules/uos/microbitfs.h"
39+
#include "ports/nrf/modules/os/microbitfs.h"
4040
#include "drv_softtimer.h"
4141
#include "drv_system.h"
4242
#include "drv_display.h"
@@ -136,7 +136,7 @@ void microbit_pyexec_file(const char *filename) {
136136
nlr_buf_t nlr;
137137
if (nlr_push(&nlr) == 0) {
138138
// Parse and comple the file.
139-
mp_lexer_t *lex = mp_lexer_new_from_file(filename);
139+
mp_lexer_t *lex = mp_lexer_new_from_file(qstr_from_str(filename));
140140
qstr source_name = lex->source_name;
141141
mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
142142
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false);
@@ -194,16 +194,16 @@ void microbit_file_opened_for_writing(const char *name, size_t name_len) {
194194
}
195195

196196
#if MICROPY_MBFS
197-
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
198-
return uos_mbfs_new_reader(filename);
197+
mp_lexer_t *mp_lexer_new_from_file(qstr filename) {
198+
return os_mbfs_new_reader(qstr_str(filename));
199199
}
200200

201201
mp_import_stat_t mp_import_stat(const char *path) {
202-
return uos_mbfs_import_stat(path);
202+
return os_mbfs_import_stat(path);
203203
}
204204

205205
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
206-
return uos_mbfs_open(n_args, args);
206+
return os_mbfs_open(n_args, args);
207207
}
208208
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
209209
#endif
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""
2+
Generate header file with macros defining micro:bit version info.
3+
4+
Adapted from MicroPython's py/makeversionhdr.py file at v1.21.0.
5+
"""
6+
7+
import argparse
8+
import sys
9+
import os
10+
import datetime
11+
import subprocess
12+
13+
14+
def get_version_info_from_git(repo_path):
15+
# Note: git describe doesn't work if no tag is available
16+
try:
17+
git_tag = subprocess.check_output(
18+
["git", "describe", "--tags", "--dirty", "--always", "--match", "v[1-9].*"],
19+
cwd=repo_path,
20+
stderr=subprocess.STDOUT,
21+
universal_newlines=True,
22+
).strip()
23+
except subprocess.CalledProcessError as er:
24+
if er.returncode == 128:
25+
# git exit code of 128 means no repository found
26+
return None
27+
git_tag = ""
28+
except OSError:
29+
return None
30+
try:
31+
git_hash = subprocess.check_output(
32+
["git", "rev-parse", "--short", "HEAD"],
33+
cwd=repo_path,
34+
stderr=subprocess.STDOUT,
35+
universal_newlines=True,
36+
).strip()
37+
except subprocess.CalledProcessError:
38+
git_hash = "unknown"
39+
except OSError:
40+
return None
41+
42+
try:
43+
# Check if there are any modified files.
44+
subprocess.check_call(
45+
["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"],
46+
cwd=repo_path,
47+
stderr=subprocess.STDOUT,
48+
)
49+
# Check if there are any staged files.
50+
subprocess.check_call(
51+
["git", "diff-index", "--cached", "--quiet", "HEAD", "--"],
52+
cwd=repo_path,
53+
stderr=subprocess.STDOUT,
54+
)
55+
except subprocess.CalledProcessError:
56+
git_hash += "-dirty"
57+
except OSError:
58+
return None
59+
60+
return git_tag, git_hash
61+
62+
63+
def make_version_header(repo_path, filename):
64+
info = None
65+
if "MICROBIT_GIT_TAG" in os.environ:
66+
info = [os.environ["MICROBIT_GIT_TAG"], os.environ["MICROBIT_GIT_HASH"]]
67+
if info is None:
68+
info = get_version_info_from_git(repo_path)
69+
70+
git_tag, git_hash = info
71+
72+
build_date = datetime.date.today()
73+
if "SOURCE_DATE_EPOCH" in os.environ:
74+
build_date = datetime.datetime.utcfromtimestamp(
75+
int(os.environ["SOURCE_DATE_EPOCH"])
76+
).date()
77+
78+
# Generate the file with the git and version info
79+
file_data = """\
80+
// This file was generated by py/makeversionhdr.py
81+
#define MICROBIT_GIT_TAG "%s"
82+
#define MICROBIT_GIT_HASH "%s"
83+
#define MICROBIT_BUILD_DATE "%s"
84+
""" % (
85+
git_tag,
86+
git_hash,
87+
build_date.strftime("%Y-%m-%d"),
88+
)
89+
90+
# Check if the file contents changed from last time
91+
write_file = True
92+
if os.path.isfile(filename):
93+
with open(filename, "r") as f:
94+
existing_data = f.read()
95+
if existing_data == file_data:
96+
write_file = False
97+
98+
# Only write the file if we need to
99+
if write_file:
100+
print("GEN %s" % filename)
101+
with open(filename, "w") as f:
102+
f.write(file_data)
103+
104+
105+
def main():
106+
parser = argparse.ArgumentParser()
107+
parser.add_argument(
108+
"-r",
109+
"--repo-path",
110+
default=os.path.join(os.path.dirname(sys.argv[0]), ".."),
111+
help="path to git repo to query for version",
112+
)
113+
parser.add_argument("dest", nargs=1, help="output file path")
114+
args = parser.parse_args()
115+
116+
make_version_header(args.repo_path, args.dest[0])
117+
118+
119+
if __name__ == "__main__":
120+
main()

0 commit comments

Comments
 (0)