Skip to content

Commit fbbcef7

Browse files
committed
Add Zephyr port
This port is meant to grow to encompass all existing boards. For now, it is a port while we transition over. It is named `zephyr-cp` to differentiate it from the MicroPython `zephyr` port. They are separate implementations.
1 parent d4fb4e8 commit fbbcef7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2222
-29
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,6 @@
410410
[submodule "ports/analog/msdk"]
411411
path = ports/analog/msdk
412412
url = https://github.com/analogdevicesinc/msdk.git
413+
[submodule "lib/zephyr"]
414+
path = ports/zephyr-cp/lib/zephyr
415+
url = https://github.com/zephyrproject-rtos/zephyr.git

CONTRIBUTING.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ Build Documentation:
4444

4545
- For SAMD21 debugging workflow tips check out [this learn guide](https://learn.adafruit.com/debugging-the-samd21-with-gdb) from Scott (@tannewt).
4646

47+
### CircuitPython is built on top of Zephyr
48+
49+
First, install Zephyr tools (see [Zephyr's Getting Started Guide](https://docs.zephyrproject.org/4.0.0/develop/getting_started/index.html)). (These are `fish` commands because that's what Scott uses.)
50+
51+
52+
```sh
53+
pip install west
54+
west update
55+
west zephyr-export
56+
pip install -r lib/zephyr/scripts/requirements.txt
57+
env ZEPHYR_BASE=lib/zephyr west sdk install
58+
```
59+
60+
Now to build from the top level:
61+
62+
```sh
63+
west build -b pca10056 -o=-j32 -- -G'Unix Makefiles' -DCMAKE_VERBOSE_MAKEFILE=ON
64+
```
65+
66+
This uses Zephyr's cmake to generate Makefiles that then delegate to
67+
`tools/cpbuild/build_circuitpython.py` to build the CircuitPython bits in parallel.
68+
4769
## Developer contacts
4870
Scott Shawcroft ([@tannewt](https://github.com/tannewt)) is the lead developer of CircuitPython
4971
and is sponsored by [Adafruit Industries LLC](https://adafruit.com). Scott is usually available

main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,13 @@ static int run_repl(safe_mode_t safe_mode) {
974974
return exit_code;
975975
}
976976

977+
#if defined(__ZEPHYR__) && __ZEPHYR__ == 1
978+
#include <zephyr/console/console.h>
979+
980+
int circuitpython_main(void) {
981+
#else
977982
int __attribute__((used)) main(void) {
983+
#endif
978984

979985
// initialise the cpu and peripherals
980986
set_safe_mode(port_init());
@@ -991,14 +997,19 @@ int __attribute__((used)) main(void) {
991997
common_hal_nvm_bytearray_set_bytes(&common_hal_mcu_nvm_obj, 0, &value_out, 1);
992998
#endif
993999

1000+
printk("serial_early_init\r\n");
1001+
9941002
// Start the debug serial
9951003
serial_early_init();
1004+
9961005
mp_hal_stdout_tx_str(line_clear);
9971006

9981007
// Wait briefly to give a reset window where we'll enter safe mode after the reset.
9991008
if (get_safe_mode() == SAFE_MODE_NONE) {
10001009
set_safe_mode(wait_for_safe_mode_reset());
10011010
}
1011+
printk("safe_mode: %d\r\n", get_safe_mode());
1012+
10021013

10031014
stack_init();
10041015

ports/zephyr-cp/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# West manages these folders.
2+
bootloader
3+
build
4+
modules
5+
tools

ports/zephyr-cp/.west/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[manifest]
2+
path = lib/zephyr
3+
file = west.yml

ports/zephyr-cp/CMakeLists.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
set(ZEPHYR_BOARD_ALIASES boards/board_aliases.cmake)
4+
5+
find_package(Zephyr REQUIRED HINTS ../../lib/zephyr)
6+
project(circuitpython)
7+
8+
target_sources(app PRIVATE zephyr_main.c)
9+
10+
# From: https://github.com/zephyrproject-rtos/zephyr/blob/main/samples/application_development/external_lib/CMakeLists.txt
11+
# The external static library that we are linking with does not know
12+
# how to build for this platform so we export all the flags used in
13+
# this zephyr build to the external build system.
14+
#
15+
# Other external build systems may be self-contained enough that they
16+
# do not need any build information from zephyr. Or they may be
17+
# incompatible with certain zephyr options and need them to be
18+
# filtered out.
19+
zephyr_get_include_directories_for_lang_as_string( C includes)
20+
zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
21+
zephyr_get_compile_definitions_for_lang_as_string( C definitions)
22+
zephyr_get_compile_options_for_lang_as_string( C options)
23+
24+
if(DEFINED CMAKE_C_COMPILER_TARGET)
25+
set(target_flag "--target=${CMAKE_C_COMPILER_TARGET}")
26+
endif()
27+
28+
set(external_project_cflags
29+
"${target_flag} ${includes} ${definitions} ${options} ${system_includes}"
30+
)
31+
32+
ExternalProject_Add(circuitpython
33+
DOWNLOAD_COMMAND ""
34+
CONFIGURE_COMMAND ""
35+
BUILD_COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/cptools/build_circuitpython.py
36+
CC=${CMAKE_C_COMPILER}
37+
AR=${CMAKE_AR}
38+
CFLAGS=${external_project_cflags}
39+
BOARD=${BOARD}
40+
BOARD_ALIAS=${BOARD_ALIAS}
41+
BOARD_REVISION=${BOARD_REVISION}
42+
BOARD_QUALIFIERS=${BOARD_QUALIFIERS}
43+
SOC_DIRECTORIES=${SOC_DIRECTORIES}
44+
OUTPUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/libcircuitpython.a
45+
PORT_SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
46+
BUILD_BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/libcircuitpython.a
47+
BUILD_JOB_SERVER_AWARE TRUE
48+
BUILD_ALWAYS TRUE
49+
DEPENDS zephyr
50+
INSTALL_COMMAND ""
51+
)
52+
53+
add_library(circuitpython_wrapper STATIC IMPORTED GLOBAL)
54+
add_dependencies(
55+
circuitpython_wrapper
56+
circuitpython
57+
)
58+
set_target_properties(circuitpython_wrapper PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/libcircuitpython.a)
59+
target_link_libraries(circuitpython_wrapper INTERFACE kernel)
60+
target_link_libraries(app PRIVATE circuitpython_wrapper)

ports/zephyr-cp/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Zephyr
2+
3+
This is an initial port of CircuitPython onto Zephyr. We intend on migrating all
4+
existing boards onto Zephyr. To start, we'll only support new boards. Existing
5+
boards will be switched as the Zephyr port reaches parity with the existing
6+
implementation.
7+
8+
## Getting Started
9+
10+
First, install Zephyr tools (see [Zephyr's Getting Started Guide](https://docs.zephyrproject.org/4.0.0/develop/getting_started/index.html)). (These are `fish` commands because that's what Scott uses.)
11+
12+
13+
```sh
14+
pip install west
15+
west update
16+
west zephyr-export
17+
pip install -r lib/zephyr/scripts/requirements.txt
18+
env ZEPHYR_BASE=lib/zephyr west sdk install
19+
```
20+
21+
Now to build from the top level:
22+
23+
```sh
24+
west build -b pca10056 -o=-j32 -- -G'Unix Makefiles'
25+
```
26+
27+
This uses Zephyr's cmake to generate Makefiles that then delegate to
28+
`tools/cpbuild/build_circuitpython.py` to build the CircuitPython bits in parallel.

ports/zephyr-cp/background.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "background.h"
8+
9+
#include "py/runtime.h"
10+
#include "supervisor/port.h"
11+
12+
#if CIRCUITPY_DISPLAYIO
13+
#include "shared-module/displayio/__init__.h"
14+
#endif
15+
16+
#if CIRCUITPY_AUDIOBUSIO
17+
#include "common-hal/audiobusio/I2SOut.h"
18+
#endif
19+
20+
#if CIRCUITPY_AUDIOPWMIO
21+
#include "common-hal/audiopwmio/PWMAudioOut.h"
22+
#endif
23+
24+
void port_start_background_tick(void) {
25+
}
26+
27+
void port_finish_background_tick(void) {
28+
}
29+
30+
void port_background_tick(void) {
31+
#if CIRCUITPY_AUDIOPWMIO
32+
audiopwmout_background();
33+
#endif
34+
#if CIRCUITPY_AUDIOBUSIO
35+
i2s_background();
36+
#endif
37+
}
38+
39+
// Allow boards to override this.
40+
MP_WEAK void board_background_task(void) {
41+
}
42+
43+
void port_background_task(void) {
44+
board_background_task();
45+
}

ports/zephyr-cp/background.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2019 Dan Halbert for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#pragma once
8+
9+
void board_background_task(void);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
set(pca10056_BOARD_ALIAS nrf52840dk/nrf52840)

0 commit comments

Comments
 (0)