Skip to content

Commit 5599ec8

Browse files
committed
Starting to call zephyr
1 parent 406b34a commit 5599ec8

File tree

5 files changed

+101
-58
lines changed

5 files changed

+101
-58
lines changed

ports/nordic/common-hal/os/__init__.c

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111

1212
#include "shared-bindings/os/__init__.h"
1313

14-
#ifdef BLUETOOTH_SD
15-
#include "nrf_sdm.h"
16-
#endif
17-
18-
#include "nrf_rng.h"
19-
2014
static const qstr os_uname_info_fields[] = {
2115
MP_QSTR_sysname, MP_QSTR_nodename,
2216
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
@@ -44,43 +38,5 @@ mp_obj_t common_hal_os_uname(void) {
4438
}
4539

4640
bool common_hal_os_urandom(uint8_t *buffer, mp_uint_t length) {
47-
#ifdef BLUETOOTH_SD
48-
uint8_t sd_en = 0;
49-
(void)sd_softdevice_is_enabled(&sd_en);
50-
51-
if (sd_en) {
52-
while (length != 0) {
53-
uint8_t available = 0;
54-
sd_rand_application_bytes_available_get(&available);
55-
if (available) {
56-
uint32_t request = MIN(length, available);
57-
uint32_t result = sd_rand_application_vector_get(buffer, request);
58-
if (result != NRF_SUCCESS) {
59-
return false;
60-
}
61-
buffer += request;
62-
length -= request;
63-
} else {
64-
RUN_BACKGROUND_TASKS;
65-
}
66-
}
67-
return true;
68-
}
69-
#endif
70-
71-
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
72-
nrf_rng_task_trigger(NRF_RNG, NRF_RNG_TASK_START);
73-
74-
for (uint32_t i = 0; i < length; i++) {
75-
while (nrf_rng_event_check(NRF_RNG, NRF_RNG_EVENT_VALRDY) == 0) {
76-
;
77-
}
78-
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
79-
80-
buffer[i] = nrf_rng_random_value_get(NRF_RNG);
81-
}
82-
83-
nrf_rng_task_trigger(NRF_RNG, NRF_RNG_TASK_STOP);
84-
85-
return true;
41+
return false;
8642
}

prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_HEAP_MEM_POOL_SIZE=65536

supervisor/zephyr/port.c

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2024 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "supervisor/port.h"
8+
9+
#include <zephyr/kernel.h>
10+
11+
safe_mode_t port_init(void) {
12+
return SAFE_MODE_NONE;
13+
}
14+
15+
// Reset the microcontroller completely.
16+
void reset_cpu(void) {
17+
while (true) {
18+
}
19+
}
20+
21+
void reset_port(void);
22+
123
void port_wake_main_task(void) {
224
}
325

@@ -11,21 +33,56 @@ void port_boot_info(void) {
1133
}
1234

1335
void port_heap_init(void) {
14-
uint32_t *heap_bottom = port_heap_get_bottom();
15-
uint32_t *heap_top = port_heap_get_top();
16-
size_t size = (heap_top - heap_bottom) * sizeof(uint32_t);
17-
heap = tlsf_create_with_pool(heap_bottom, size, size);
36+
}
37+
38+
// Get stack limit address
39+
uint32_t *port_stack_get_limit(void) {
40+
return NULL;
41+
}
42+
43+
// Get stack top address
44+
uint32_t *port_stack_get_top(void) {
45+
return NULL;
46+
}
47+
48+
// Get heap bottom address
49+
uint32_t *port_heap_get_bottom(void) {
50+
return NULL;
51+
}
52+
53+
// Get heap top address
54+
uint32_t *port_heap_get_top(void) {
55+
return NULL;
56+
}
57+
58+
// Save and retrieve a word from memory that is preserved over reset. Used for safe mode.
59+
void port_set_saved_word(uint32_t) {
60+
61+
}
62+
uint32_t port_get_saved_word(void) {
63+
return 0;
64+
}
65+
66+
67+
// Enable 1/1024 second tick.
68+
void port_enable_tick(void) {
69+
70+
}
71+
72+
// Disable 1/1024 second tick.
73+
void port_disable_tick(void) {
74+
1875
}
1976

2077
void *port_malloc(size_t size, bool dma_capable) {
21-
void *block = tlsf_malloc(heap, size);
78+
void *block = k_malloc(size);
2279
return block;
2380
}
2481

2582
void port_free(void *ptr) {
26-
tlsf_free(heap, ptr);
83+
k_free(ptr);
2784
}
2885

2986
void *port_realloc(void *ptr, size_t size) {
30-
return tlsf_realloc(heap, ptr, size);
87+
return k_realloc(ptr, size);
3188
}

tools/cpbuild/build_circuitpython.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,22 @@ async def build_circuitpython():
193193

194194
supervisor_source = [
195195
"main.c",
196+
"extmod/vfs_fat.c",
196197
"lib/tlsf/tlsf.c",
197-
f"ports/{port}/supervisor/port.c",
198+
f"ports/{port}/background.c",
198199
f"ports/{port}/common-hal/microcontroller/__init__.c",
199200
f"ports/{port}/common-hal/microcontroller/Processor.c",
201+
f"ports/{port}/common-hal/os/__init__.c",
200202
"supervisor/stub/misc.c",
201203
"shared/readline/readline.c",
202204
"shared/runtime/pyexec.c",
203205
"shared/runtime/interrupt_char.c",
204206
"shared/runtime/stdout_helpers.c",
205207
"shared/runtime/sys_stdio_mphal.c",
208+
"shared-bindings/supervisor/Runtime.c",
206209
"extmod/vfs_reader.c",
210+
"extmod/vfs_blockdev.c",
211+
"extmod/vfs_fat_file.c",
207212
]
208213
top = srcdir
209214
supervisor_source = [pathlib.Path(p) for p in supervisor_source]
@@ -257,7 +262,7 @@ async def build_circuitpython():
257262
for mpflag in MPCONFIG_FLAGS:
258263
circuitpython_flags.append(f"-DCIRCUITPY_{mpflag.upper()}=0")
259264

260-
source_files = supervisor_source + ["py/modsys.c", "extmod/vfs.c"]
265+
source_files = supervisor_source + ["extmod/vfs.c"]
261266
for file in top.glob("py/*.c"):
262267
source_files.append(file)
263268
qstr_flags = "-DNO_QSTR"
@@ -287,7 +292,20 @@ async def build_circuitpython():
287292
# This file is generated by the QSTR/translation process.
288293
translation = "en_US"
289294
source_files.append(builddir / f"translations-{translation}.c")
295+
# These files don't include unique QSTRs. They just need to be compiled.
290296
source_files.append(srcdir / "supervisor" / "zephyr" / "flash.c")
297+
source_files.append(srcdir / "supervisor" / "zephyr" / "port.c")
298+
source_files.append(srcdir / "lib" / "oofatfs" / "ff.c")
299+
source_files.append(srcdir / "lib" / "oofatfs" / "ffunicode.c")
300+
source_files.append(srcdir / "extmod" / "vfs_fat_diskio.c")
301+
source_files.append(srcdir / "shared/timeutils/timeutils.c")
302+
source_files.append(srcdir / "shared-module/time/__init__.c")
303+
source_files.append(srcdir / "shared-module/os/__init__.c")
304+
305+
assembly_files = []
306+
assembly_files.append(srcdir / "ports/nordic/supervisor/cpu.s")
307+
308+
source_files.extend(assembly_files)
291309

292310
objects = []
293311
async with asyncio.TaskGroup() as tg:
@@ -313,6 +331,6 @@ async def main():
313331
handler = colorlog.StreamHandler()
314332
handler.setFormatter(colorlog.ColoredFormatter("%(log_color)s%(levelname)s:%(name)s:%(message)s"))
315333

316-
logging.basicConfig(level=logging.DEBUG, handlers=[handler])
334+
logging.basicConfig(level=logging.INFO, handlers=[handler])
317335

318336
asyncio.run(main())

tools/cpbuild/cpbuild.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
if _last_build_times.exists():
2323
with open(_last_build_times) as f:
2424
LAST_BUILD_TIMES = json.load(f)
25+
logger.info("Build times loaded.")
26+
print("build times loaded")
27+
else:
28+
logger.warn(
29+
"No last build times found. This is normal if you're running this for the first time."
30+
)
2531

2632

2733
def save_trace():
@@ -58,6 +64,8 @@ def __init__(self, fifo_path=None, read_fd=None, write_fd=None):
5864
self.read_transport: asyncio.ReadTransport | None = None
5965
self.read_protocol = None
6066

67+
self.started = None
68+
6169
def new_token(self, token):
6270
# Keep a token and reuse it. Ignore cancelled Futures.
6371
if self.pending_futures:
@@ -73,10 +81,13 @@ def new_token(self, token):
7381

7482
async def __aenter__(self):
7583
loop = asyncio.get_event_loop()
76-
if self.read_transport is None:
84+
if self.started is None:
85+
self.started = asyncio.Event()
7786
self.read_transport, self.read_protocol = await loop.connect_read_pipe(
7887
lambda: _TokenProtocol(self), self.reader
7988
)
89+
self.started.set()
90+
await self.started.wait()
8091
future = loop.create_future()
8192
self.pending_futures.append(future)
8293
self.read_transport.resume_reading()
@@ -133,6 +144,7 @@ async def run_command(command, working_directory, description=None, check_hash=[
133144

134145
# If a command is run multiple times, then wait for the first one to continue. Don't run it again.
135146
if command_hash in ALREADY_RUN:
147+
logging.debug(f"Already running {command_hash} {command}")
136148
await ALREADY_RUN[command_hash].wait()
137149
return
138150
ALREADY_RUN[command_hash] = asyncio.Event()
@@ -141,10 +153,9 @@ async def run_command(command, working_directory, description=None, check_hash=[
141153
if command_hash in LAST_BUILD_TIMES and all((p.exists() for p in paths)):
142154
newest_file = max((p.stat().st_mtime_ns for p in paths))
143155
last_build_time = LAST_BUILD_TIMES[command_hash]
144-
if last_build_time <= newest_file:
156+
if newest_file <= last_build_time:
145157
ALREADY_RUN[command_hash].set()
146158
return
147-
148159
else:
149160
newest_file = 0
150161

0 commit comments

Comments
 (0)