Skip to content

Commit b49dd35

Browse files
committed
First working CircuitPython port
1 parent 4f0c9bb commit b49dd35

Some content is hidden

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

42 files changed

+1824
-0
lines changed

advanced/circuitpython/README.md

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 376e3ba1da34d65682ca78e52193d18102d1749d
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
TOP = ../circuitpython
2+
3+
BOARD ?= riscv-emu.py
4+
PORT_DIR := $(CURDIR)
5+
BUILD ?= $(PORT_DIR)/build-$(BOARD)
6+
7+
include circuitpy_mkenv.mk
8+
9+
CROSS_COMPILE = /opt/riscv/bin/riscv32-unknown-elf-
10+
11+
INC += \
12+
-I. \
13+
-I../.. \
14+
-I../lib/mp-readline \
15+
-I../shared/runtime \
16+
-I$(TOP) \
17+
-Iboards/$(BOARD) \
18+
-Iboards/ \
19+
-I$(BUILD)
20+
21+
CFLAGS += -Os
22+
23+
DISABLE_WARNINGS = -Wno-cast-align
24+
CFLAGS += $(INC) -Wall -Werror -std=gnu11 -fshort-enums $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT) $(DISABLE_WARNINGS) -Werror=missing-prototypes
25+
26+
CFLAGS += \
27+
-march=rv32i_zicsr \
28+
-mabi=ilp32 \
29+
-D_REENT_SMALL \
30+
-nostartfiles \
31+
-static \
32+
--specs=nano.specs
33+
34+
CFLAGS += -DCIRCUITPY_CONSOLE_UART_RX -DCIRCUITPY_CONSOLE_UART_TX
35+
# Use toolchain libm if we're not using our own.
36+
ifndef INTERNAL_LIBM
37+
LIBS += -lm
38+
endif
39+
40+
LIBS += -lc
41+
42+
SRC_C += \
43+
boards/$(BOARD)/board.c \
44+
boards/$(BOARD)/pins.c \
45+
background.c \
46+
mphalport.c \
47+
gccollect.c
48+
49+
SRC_ASM = start.S gchelper_rv32i.S trap_handler.S
50+
51+
SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
52+
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
53+
$(addprefix common-hal/, $(SRC_COMMON_HAL))
54+
55+
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
56+
$(addprefix shared-module/, $(SRC_SHARED_MODULE)) \
57+
$(addprefix shared-module/, $(SRC_SHARED_MODULE_INTERNAL))
58+
59+
# There may be duplicates between SRC_COMMON_HAL_EXPANDED and SRC_SHARED_MODULE_EXPANDED,
60+
# because a few modules have files both in common-hal/ and shared-module/.
61+
# Doing a $(sort ...) removes duplicates as part of sorting.
62+
SRC_COMMON_HAL_SHARED_MODULE_EXPANDED = $(sort $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED))
63+
64+
SRC_S = # supervisor/riscvpy_cpu.s
65+
66+
OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
67+
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED:.c=.o))
68+
ifeq ($(INTERNAL_LIBM),1)
69+
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
70+
endif
71+
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
72+
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
73+
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
74+
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
75+
OBJ += $(addprefix $(BUILD)/, $(SRC_ASM:.S=.o))
76+
77+
$(BUILD)/%.o: %.S
78+
$(STEPECHO) "AS $<"
79+
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
80+
81+
SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) $(SRC_CIRCUITPY_COMMON)
82+
83+
all: $(BUILD)/firmware.elf $(BUILD)/circuitpy.img
84+
85+
BOARD_LD := $(wildcard boards/$(BOARD)/link.ld)
86+
87+
ifneq ($(BOARD_LD),)
88+
LINKER_SCRIPTS = -Wl,-T,$(BOARD_LD)
89+
endif
90+
91+
LINKER_SCRIPTS += -Wl,-T,link.ld
92+
93+
$(BUILD)/circuitpy.img: circuitpy/code.py
94+
$(STEPECHO) "Create $@"
95+
$(Q)dd if=/dev/zero of=$(BUILD)/circuitpy.img bs=1 count=0 seek=512K
96+
/opt/homebrew/sbin/mkfs.fat -n CIRCUITPY --offset=0 $(BUILD)/circuitpy.img
97+
$(Q)mcopy -i $(BUILD)/circuitpy.img circuitpy/* ::
98+
99+
ifeq ($(VALID_BOARD),)
100+
$(BUILD)/firmware.elf: invalid-board
101+
else
102+
$(BUILD)/firmware.elf: $(OBJ) $(BOARD_LD) link.ld
103+
$(STEPECHO) "LINK $@"
104+
$(Q)echo $(OBJ) > $(BUILD)/firmware.objs
105+
$(Q)echo $(PICO_LDFLAGS) > $(BUILD)/firmware.ldflags
106+
$(Q)$(CC) -o $@ $(CFLAGS) @$(BUILD)/firmware.ldflags $(LINKER_SCRIPTS) -Wl,--print-memory-usage -Wl,-Map=$@.map -Wl,-cref -Wl,--gc-sections @$(BUILD)/firmware.objs -Wl,-lc
107+
endif
108+
109+
$(BUILD)/firmware.bin: $(BUILD)/firmware.elf
110+
$(STEPECHO) "Create $@"
111+
$(Q)$(OBJCOPY) -O binary -R .dtcm_bss $^ $@
112+
113+
include $(TOP)/py/mkrules.mk
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "supervisor/port.h"
8+
9+
void port_start_background_tick(void) {
10+
}
11+
12+
void port_finish_background_tick(void) {
13+
}
14+
15+
void port_background_tick(void) {
16+
}
17+
18+
void port_background_task(void) {
19+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "supervisor/board.h"
2+
3+
#include "common-hal/busio/UART.h"
4+
#include "shared-bindings/busio/UART.h"
5+
#include "supervisor/shared/serial.h"
6+
#include "supervisor/background_callback.h"
7+
#include "shared/runtime/interrupt_char.h"
8+
#include "py/mphal.h"
9+
#include "py/runtime.h"
10+
#include "supervisor/internal_flash.h"
11+
12+
#include "riscv-py.h"
13+
14+
extern busio_uart_obj_t console_uart;
15+
extern void setup_timer_interrupt(void);
16+
void kbd_interrupt_background_task(void *data);
17+
background_callback_t kbd_interrupt_background_cb;
18+
19+
static uint32_t counter = 0;
20+
21+
void kbd_interrupt_background_task(void *data) {
22+
unsigned char c;
23+
24+
if ((++counter & 0xFF) == 0) {
25+
if (common_hal_busio_uart_rx_characters_available(&console_uart)) {
26+
common_hal_busio_uart_read(&console_uart, &c, 1, NULL);
27+
if ((c == mp_interrupt_char) && !mp_hal_is_interrupted()) {
28+
mp_sched_keyboard_interrupt();
29+
mp_handle_pending(true);
30+
}
31+
}
32+
}
33+
34+
background_callback_add_core(&kbd_interrupt_background_cb);
35+
}
36+
37+
void board_init(void) {
38+
//mp_hal_set_interrupt_char(0x03);
39+
kbd_interrupt_background_cb.fun = &kbd_interrupt_background_task;
40+
kbd_interrupt_background_cb.data = NULL;
41+
background_callback_add_core(&kbd_interrupt_background_cb);
42+
setup_timer_interrupt();
43+
}
44+
45+
void board_serial_write_substring(const char *text, uint32_t len) {
46+
int errcode;
47+
common_hal_busio_uart_write(&console_uart, (const uint8_t *)text, len, &errcode);
48+
}
49+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#define MICROPY_HW_BOARD_NAME "riscv-emu.py"
4+
#define MICROPY_HW_MCU_NAME "riscv-emu.py"
5+

advanced/circuitpython/riscv-emu.py/boards/riscv-emu.py/mpconfigboard.mk

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "shared-bindings/board/__init__.h"
2+
3+
static const mp_rom_map_elem_t board_module_globals_table[] = {
4+
CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS
5+
};
6+
7+
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);
8+
9+
const mp_obj_dict_t mcu_pin_globals = {
10+
// Pin definitions (must exist)
11+
};
12+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print("Hello from RISC-V CircuitPython!")
2+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This file is part of the MicroPython project, http://micropython.org/
2+
#
3+
# The MIT License (MIT)
4+
#
5+
# SPDX-FileCopyrightText: Copyright (c) 2022 MicroDev
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
25+
TOP := ../circuitpython
26+
27+
# Common Makefile items that can be shared across CircuitPython ports.
28+
29+
ALL_BOARDS_IN_PORT := $(patsubst boards/%/mpconfigboard.mk,%,$(wildcard boards/*/mpconfigboard.mk))
30+
# An incorrect BOARD might have been specified, so check against the list.
31+
# There is deliberately no space after the :=
32+
VALID_BOARD :=$(filter $(BOARD),$(ALL_BOARDS_IN_PORT))
33+
34+
# If the flash PORT is not given, use the default /dev/tty.SLAB_USBtoUART.
35+
PORT ?= /dev/tty.SLAB_USBtoUART
36+
37+
# If the build directory is not given, make it reflect the board name.
38+
BUILD ?= build-$(BOARD)
39+
40+
# First makefile with targets. Defines the default target.
41+
include $(TOP)/py/mkenv.mk
42+
43+
# Board-specific. Skip if the rule requested is not board-specific.
44+
ifneq ($(VALID_BOARD),)
45+
include boards/$(BOARD)/mpconfigboard.mk
46+
endif
47+
48+
# Port-specific
49+
include mpconfigport.mk
50+
51+
# Also board-specific. Skip if the rule requested is not board-specific.
52+
ifneq ($(VALID_BOARD),)
53+
# CircuitPython-specific
54+
include $(TOP)/py/circuitpy_mpconfig.mk
55+
endif
56+
57+
# qstr definitions (must come before including py.mk)
58+
QSTR_DEFS = qstrdefsport.h
59+
60+
# include py core make definitions
61+
include $(TOP)/py/py.mk
62+
63+
include $(TOP)/supervisor/supervisor.mk
64+
65+
# Include make rules and variables common across CircuitPython builds.
66+
include $(TOP)/py/circuitpy_defns.mk

0 commit comments

Comments
 (0)