Skip to content

Commit 442def3

Browse files
committed
Add minimal Renode port
This runs in the Renode simulator and enables easier tracing and debugging of the CircuitPython core. This port can also serve as a starting point for new ports because it implements the minimal necessary for the CP core to run.
1 parent d8ca842 commit 442def3

Some content is hidden

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

45 files changed

+1930
-36
lines changed

main.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
#include "supervisor/shared/tick.h"
5959
#include "supervisor/shared/traceback.h"
6060
#include "supervisor/shared/workflow.h"
61-
#include "supervisor/usb.h"
6261
#include "supervisor/workflow.h"
6362
#include "supervisor/shared/external_flash/external_flash.h"
6463

@@ -115,10 +114,14 @@
115114
#include "supervisor/shared/status_bar.h"
116115
#endif
117116

118-
#if CIRCUITPY_USB_HID
117+
#if CIRCUITPY_USB && CIRCUITPY_USB_HID
119118
#include "shared-module/usb_hid/__init__.h"
120119
#endif
121120

121+
#if CIRCUITPY_USB
122+
#include "supervisor/usb.h"
123+
#endif
124+
122125
#if CIRCUITPY_WIFI
123126
#include "shared-bindings/wifi/__init__.h"
124127
#endif
@@ -429,7 +432,7 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
429432
}
430433
}
431434

432-
STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
435+
STATIC bool __attribute__((noinline)) run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
433436
bool serial_connected_at_start = serial_connected();
434437
bool printed_safe_mode_message = false;
435438
#if CIRCUITPY_AUTORELOAD_DELAY_MS > 0
@@ -1160,7 +1163,7 @@ void gc_collect(void) {
11601163
common_hal_bleio_gc_collect();
11611164
#endif
11621165

1163-
#if CIRCUITPY_USB_HID
1166+
#if CIRCUITPY_USB && CIRCUITPY_USB_HID
11641167
usb_hid_gc_collect();
11651168
#endif
11661169

ports/renode/Makefile

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# This file is part of the MicroPython project, http://micropython.org/
2+
#
3+
# The MIT License (MIT)
4+
#
5+
# SPDX-FileCopyrightText: Copyright (c) 2019 Dan Halbert for Adafruit Industries
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+
include ../../py/circuitpy_mkenv.mk
26+
27+
CROSS_COMPILE = arm-none-eabi-
28+
29+
INC += \
30+
-I. \
31+
-I../.. \
32+
-I../lib/mp-readline \
33+
-I../shared/timeutils \
34+
-Iboards/$(BOARD) \
35+
-Iboards/ \
36+
-isystem ./../../lib/cmsis/inc \
37+
-I$(BUILD)
38+
39+
OPTIMIZATION_FLAGS ?= -O3
40+
# option to override default optimization level, set in boards/$(BOARD)/mpconfigboard.mk
41+
CFLAGS += $(OPTIMIZATION_FLAGS)
42+
43+
# flags specific to wifi / cyw43
44+
#Debugging/Optimization
45+
ifeq ($(DEBUG), 1)
46+
CFLAGS += -ggdb3 -O3
47+
# No LTO because we may place some functions in RAM instead of flash.
48+
else
49+
CFLAGS += -DNDEBUG
50+
51+
# No LTO because we may place some functions in RAM instead of flash.
52+
53+
ifdef CFLAGS_BOARD
54+
CFLAGS += $(CFLAGS_BOARD)
55+
endif
56+
endif
57+
58+
DISABLE_WARNINGS = -Wno-cast-align
59+
CFLAGS += $(INC) -Wall -Werror -std=gnu11 -fshort-enums $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT) $(DISABLE_WARNINGS) -Werror=missing-prototypes
60+
61+
CFLAGS += \
62+
-march=armv6-m \
63+
-mthumb \
64+
-mabi=aapcs \
65+
-mcpu=cortex-m0plus \
66+
-msoft-float \
67+
-mfloat-abi=soft \
68+
--specs=nano.specs
69+
70+
# Use toolchain libm if we're not using our own.
71+
ifndef INTERNAL_LIBM
72+
LIBS += -lm
73+
endif
74+
75+
LIBS += -lc
76+
77+
SRC_C += \
78+
boards/$(BOARD)/board.c \
79+
boards/$(BOARD)/pins.c \
80+
background.c \
81+
mphalport.c \
82+
83+
84+
SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
85+
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
86+
$(addprefix common-hal/, $(SRC_COMMON_HAL))
87+
88+
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
89+
$(addprefix shared-module/, $(SRC_SHARED_MODULE)) \
90+
$(addprefix shared-module/, $(SRC_SHARED_MODULE_INTERNAL))
91+
92+
# There may be duplicates between SRC_COMMON_HAL_EXPANDED and SRC_SHARED_MODULE_EXPANDED,
93+
# because a few modules have files both in common-hal/ and shared-module/.
94+
# Doing a $(sort ...) removes duplicates as part of sorting.
95+
SRC_COMMON_HAL_SHARED_MODULE_EXPANDED = $(sort $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED))
96+
97+
SRC_S = supervisor/$(CPU)_cpu.s
98+
99+
OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
100+
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED:.c=.o))
101+
ifeq ($(INTERNAL_LIBM),1)
102+
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
103+
endif
104+
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
105+
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
106+
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
107+
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
108+
109+
$(BUILD)/%.o: $(BUILD)/%.S
110+
$(STEPECHO) "CC $<"
111+
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
112+
113+
SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) $(SRC_CIRCUITPY_COMMON)
114+
115+
all: $(BUILD)/firmware.elf $(BUILD)/circuitpy.img
116+
117+
BOARD_LD := $(wildcard boards/$(BOARD)/link.ld)
118+
119+
ifneq ($(BOARD_LD),)
120+
LINKER_SCRIPTS = -Wl,-T,$(BOARD_LD)
121+
endif
122+
123+
LINKER_SCRIPTS += -Wl,-T,link.ld
124+
125+
$(BUILD)/circuitpy.img: circuitpy/code.py
126+
$(STEPECHO) "Create $@"
127+
$(Q)dd if=/dev/zero of=$(BUILD)/circuitpy.img bs=1 count=0 seek=512K
128+
$(Q)mkfs.fat -n CIRCUITPY --offset=0 $(BUILD)/circuitpy.img
129+
$(Q)mcopy -i $(BUILD)/circuitpy.img circuitpy/* ::
130+
131+
ifeq ($(VALID_BOARD),)
132+
$(BUILD)/firmware.elf: invalid-board
133+
else
134+
$(BUILD)/firmware.elf: $(OBJ) $(BOARD_LD) link.ld
135+
$(STEPECHO) "LINK $@"
136+
$(Q)echo $(OBJ) > $(BUILD)/firmware.objs
137+
$(Q)echo $(PICO_LDFLAGS) > $(BUILD)/firmware.ldflags
138+
$(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
139+
endif
140+
141+
$(BUILD)/firmware.bin: $(BUILD)/firmware.elf
142+
$(STEPECHO) "Create $@"
143+
$(Q)$(OBJCOPY) -O binary -R .dtcm_bss $^ $@
144+
145+
include $(TOP)/py/mkrules.mk

ports/renode/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Renode
2+
3+
Renode is an emulator targeting microcontroller-class devices. This port is a
4+
minimal version of CircuitPython that runs under Renode. Renode is designed to
5+
mimic full microcontrollers but CP uses more registers than what Renode has
6+
implemented so far. This port allows us to run on a variety of CPUs without
7+
worrying about peripherals.
8+
9+
## Running
10+
11+
1. Get Renode: https://renode.io/#downloads
12+
2. `cd ports/renode`
13+
3. `make BOARD=renode_cortex_m0plus`
14+
4. In another tab: `tio /tmp/cp-uart`
15+
5. `renode`
16+
6. In renode: `include @renode.resc`
17+
7. <Any other setup>
18+
8. `start`
19+
9. `pause`
20+
10. `quit`
21+
22+
Step 4 sets up `tio` to talk to CircuitPython via UART <-> PTY bridge.
23+
24+
## Other stuff
25+
26+
### Emulator logging
27+
Renode modules have debug logging that can be enabled with `logLevel` with an int
28+
between `-1` for `NOISY` and `3` for errors only.
29+
30+
### GDB
31+
32+
Renode can provide a GDB server. It is very useful for precisely controlling the
33+
emulator's execution.
34+
35+
```
36+
machine StartGdbServer 3333 true
37+
```
38+
39+
### Execution profiling
40+
41+
In renode do `cpu EnableProfiler CollapsedStack $ORIGIN/profile.folded` before starting
42+
the emulation. You can view it using [Speedscope](https://www.speedscope.app/). CircuitPython calls
43+
a lot of functions and may overwhelm speedscope. You can enable this tracing over a specific
44+
section of CircuitPython execution to limit the capture size.
45+
46+
[Related Renode Docs](https://renode.readthedocs.io/en/latest/advanced/execution-tracing.html)
47+
48+
### Execution tracing
49+
If you want to see every instruction run you can do: `cpu CreateExecutionTracing "tracer_name" $ORIGIN/instruction_trace.txt Disassembly`.

ports/renode/Simple32Khz.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Copyright (c) 2010-2022 Antmicro
3+
// Copyright (c) 2011-2015 Realtime Embedded
4+
//
5+
// This file is licensed under the MIT License.
6+
// Full license text is available in 'licenses/MIT.txt'.
7+
//
8+
// This is modified for CircuitPython to tick at 32khz like a slow external
9+
// crystal would.
10+
using System;
11+
using Antmicro.Renode.Core;
12+
using Antmicro.Renode.Peripherals.Bus;
13+
using Antmicro.Renode.Time;
14+
using Antmicro.Renode.Logging;
15+
using System.Threading;
16+
17+
namespace Antmicro.Renode.Peripherals.Timers
18+
{
19+
public class Simple32Khz : IDoubleWordPeripheral, IKnownSize
20+
{
21+
public long Size { get { return 0x4; } }
22+
23+
public Simple32Khz(IMachine machine)
24+
{
25+
machine.ClockSource.AddClockEntry(new ClockEntry(1, 32768, OnTick, this, String.Empty));
26+
}
27+
28+
public virtual uint ReadDoubleWord(long offset)
29+
{
30+
return (uint)counter;
31+
}
32+
33+
public virtual void WriteDoubleWord(long offset, uint value)
34+
{
35+
this.LogUnhandledWrite(offset, value);
36+
}
37+
38+
public virtual void Reset()
39+
{
40+
Interlocked.Exchange(ref counter, 0);
41+
}
42+
43+
private void OnTick()
44+
{
45+
Interlocked.Increment(ref counter);
46+
}
47+
48+
private int counter;
49+
}
50+
}

ports/renode/background.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "supervisor/port.h"
28+
29+
void port_start_background_tick(void) {
30+
}
31+
32+
void port_finish_background_tick(void) {
33+
}
34+
35+
void port_background_tick(void) {
36+
}
37+
38+
void port_background_task(void) {
39+
}

ports/renode/background.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#pragma once

ports/renode/boards/peripherals.repl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
flash_firmware: Memory.MappedMemory @ sysbus 0x00000000
2+
size: 0x80000 // 512k
3+
4+
flash_filesystem: Memory.MappedMemory @ sysbus 0x10000000
5+
size: 0x80000 // 512k
6+
7+
sram: Memory.MappedMemory @ sysbus 0x20000000
8+
size: 0x20000 // 128k
9+
10+
11+
uart: UART.PicoSoC_SimpleUART @ sysbus 0x40000000
12+
13+
time: Timers.Simple32Khz @ sysbus 0x40001000

0 commit comments

Comments
 (0)