Skip to content

Commit

Permalink
Starting to call zephyr
Browse files Browse the repository at this point in the history
  • Loading branch information
tannewt committed Dec 6, 2024
1 parent 406b34a commit 5599ec8
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 58 deletions.
46 changes: 1 addition & 45 deletions ports/nordic/common-hal/os/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@

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

#ifdef BLUETOOTH_SD
#include "nrf_sdm.h"
#endif

#include "nrf_rng.h"

static const qstr os_uname_info_fields[] = {
MP_QSTR_sysname, MP_QSTR_nodename,
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
Expand Down Expand Up @@ -44,43 +38,5 @@ mp_obj_t common_hal_os_uname(void) {
}

bool common_hal_os_urandom(uint8_t *buffer, mp_uint_t length) {
#ifdef BLUETOOTH_SD
uint8_t sd_en = 0;
(void)sd_softdevice_is_enabled(&sd_en);

if (sd_en) {
while (length != 0) {
uint8_t available = 0;
sd_rand_application_bytes_available_get(&available);
if (available) {
uint32_t request = MIN(length, available);
uint32_t result = sd_rand_application_vector_get(buffer, request);
if (result != NRF_SUCCESS) {
return false;
}
buffer += request;
length -= request;
} else {
RUN_BACKGROUND_TASKS;
}
}
return true;
}
#endif

nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);
nrf_rng_task_trigger(NRF_RNG, NRF_RNG_TASK_START);

for (uint32_t i = 0; i < length; i++) {
while (nrf_rng_event_check(NRF_RNG, NRF_RNG_EVENT_VALRDY) == 0) {
;
}
nrf_rng_event_clear(NRF_RNG, NRF_RNG_EVENT_VALRDY);

buffer[i] = nrf_rng_random_value_get(NRF_RNG);
}

nrf_rng_task_trigger(NRF_RNG, NRF_RNG_TASK_STOP);

return true;
return false;
}
1 change: 1 addition & 0 deletions prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_HEAP_MEM_POOL_SIZE=65536
71 changes: 64 additions & 7 deletions supervisor/zephyr/port.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2024 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include "supervisor/port.h"

#include <zephyr/kernel.h>

safe_mode_t port_init(void) {
return SAFE_MODE_NONE;
}

// Reset the microcontroller completely.
void reset_cpu(void) {
while (true) {
}
}

void reset_port(void);

void port_wake_main_task(void) {
}

Expand All @@ -11,21 +33,56 @@ void port_boot_info(void) {
}

void port_heap_init(void) {
uint32_t *heap_bottom = port_heap_get_bottom();
uint32_t *heap_top = port_heap_get_top();
size_t size = (heap_top - heap_bottom) * sizeof(uint32_t);
heap = tlsf_create_with_pool(heap_bottom, size, size);
}

// Get stack limit address
uint32_t *port_stack_get_limit(void) {
return NULL;
}

// Get stack top address
uint32_t *port_stack_get_top(void) {
return NULL;
}

// Get heap bottom address
uint32_t *port_heap_get_bottom(void) {
return NULL;
}

// Get heap top address
uint32_t *port_heap_get_top(void) {
return NULL;
}

// Save and retrieve a word from memory that is preserved over reset. Used for safe mode.
void port_set_saved_word(uint32_t) {

}
uint32_t port_get_saved_word(void) {
return 0;
}


// Enable 1/1024 second tick.
void port_enable_tick(void) {

}

// Disable 1/1024 second tick.
void port_disable_tick(void) {

}

void *port_malloc(size_t size, bool dma_capable) {
void *block = tlsf_malloc(heap, size);
void *block = k_malloc(size);
return block;
}

void port_free(void *ptr) {
tlsf_free(heap, ptr);
k_free(ptr);
}

void *port_realloc(void *ptr, size_t size) {
return tlsf_realloc(heap, ptr, size);
return k_realloc(ptr, size);
}
24 changes: 21 additions & 3 deletions tools/cpbuild/build_circuitpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,22 @@ async def build_circuitpython():

supervisor_source = [
"main.c",
"extmod/vfs_fat.c",
"lib/tlsf/tlsf.c",
f"ports/{port}/supervisor/port.c",
f"ports/{port}/background.c",
f"ports/{port}/common-hal/microcontroller/__init__.c",
f"ports/{port}/common-hal/microcontroller/Processor.c",
f"ports/{port}/common-hal/os/__init__.c",
"supervisor/stub/misc.c",
"shared/readline/readline.c",
"shared/runtime/pyexec.c",
"shared/runtime/interrupt_char.c",
"shared/runtime/stdout_helpers.c",
"shared/runtime/sys_stdio_mphal.c",
"shared-bindings/supervisor/Runtime.c",
"extmod/vfs_reader.c",
"extmod/vfs_blockdev.c",
"extmod/vfs_fat_file.c",
]
top = srcdir
supervisor_source = [pathlib.Path(p) for p in supervisor_source]
Expand Down Expand Up @@ -257,7 +262,7 @@ async def build_circuitpython():
for mpflag in MPCONFIG_FLAGS:
circuitpython_flags.append(f"-DCIRCUITPY_{mpflag.upper()}=0")

source_files = supervisor_source + ["py/modsys.c", "extmod/vfs.c"]
source_files = supervisor_source + ["extmod/vfs.c"]
for file in top.glob("py/*.c"):
source_files.append(file)
qstr_flags = "-DNO_QSTR"
Expand Down Expand Up @@ -287,7 +292,20 @@ async def build_circuitpython():
# This file is generated by the QSTR/translation process.
translation = "en_US"
source_files.append(builddir / f"translations-{translation}.c")
# These files don't include unique QSTRs. They just need to be compiled.
source_files.append(srcdir / "supervisor" / "zephyr" / "flash.c")
source_files.append(srcdir / "supervisor" / "zephyr" / "port.c")
source_files.append(srcdir / "lib" / "oofatfs" / "ff.c")
source_files.append(srcdir / "lib" / "oofatfs" / "ffunicode.c")
source_files.append(srcdir / "extmod" / "vfs_fat_diskio.c")
source_files.append(srcdir / "shared/timeutils/timeutils.c")
source_files.append(srcdir / "shared-module/time/__init__.c")
source_files.append(srcdir / "shared-module/os/__init__.c")

assembly_files = []
assembly_files.append(srcdir / "ports/nordic/supervisor/cpu.s")

source_files.extend(assembly_files)

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

logging.basicConfig(level=logging.DEBUG, handlers=[handler])
logging.basicConfig(level=logging.INFO, handlers=[handler])

asyncio.run(main())
17 changes: 14 additions & 3 deletions tools/cpbuild/cpbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
if _last_build_times.exists():
with open(_last_build_times) as f:
LAST_BUILD_TIMES = json.load(f)
logger.info("Build times loaded.")
print("build times loaded")
else:
logger.warn(
"No last build times found. This is normal if you're running this for the first time."
)


def save_trace():
Expand Down Expand Up @@ -58,6 +64,8 @@ def __init__(self, fifo_path=None, read_fd=None, write_fd=None):
self.read_transport: asyncio.ReadTransport | None = None
self.read_protocol = None

self.started = None

def new_token(self, token):
# Keep a token and reuse it. Ignore cancelled Futures.
if self.pending_futures:
Expand All @@ -73,10 +81,13 @@ def new_token(self, token):

async def __aenter__(self):
loop = asyncio.get_event_loop()
if self.read_transport is None:
if self.started is None:
self.started = asyncio.Event()
self.read_transport, self.read_protocol = await loop.connect_read_pipe(
lambda: _TokenProtocol(self), self.reader
)
self.started.set()
await self.started.wait()
future = loop.create_future()
self.pending_futures.append(future)
self.read_transport.resume_reading()
Expand Down Expand Up @@ -133,6 +144,7 @@ async def run_command(command, working_directory, description=None, check_hash=[

# If a command is run multiple times, then wait for the first one to continue. Don't run it again.
if command_hash in ALREADY_RUN:
logging.debug(f"Already running {command_hash} {command}")
await ALREADY_RUN[command_hash].wait()
return
ALREADY_RUN[command_hash] = asyncio.Event()
Expand All @@ -141,10 +153,9 @@ async def run_command(command, working_directory, description=None, check_hash=[
if command_hash in LAST_BUILD_TIMES and all((p.exists() for p in paths)):
newest_file = max((p.stat().st_mtime_ns for p in paths))
last_build_time = LAST_BUILD_TIMES[command_hash]
if last_build_time <= newest_file:
if newest_file <= last_build_time:
ALREADY_RUN[command_hash].set()
return

else:
newest_file = 0

Expand Down

0 comments on commit 5599ec8

Please sign in to comment.