Skip to content

Commit

Permalink
Merge pull request #9199 from tannewt/renode
Browse files Browse the repository at this point in the history
Add minimal Renode port
  • Loading branch information
dhalbert committed May 1, 2024
2 parents 49954d8 + a86a9b7 commit 8734076
Show file tree
Hide file tree
Showing 62 changed files with 1,990 additions and 80 deletions.
1 change: 1 addition & 0 deletions docs/shared_bindings_matrix.py
Expand Up @@ -40,6 +40,7 @@
"mimxrt10xx",
"nordic",
"raspberrypi",
"renode",
"silabs",
"stm",
]
Expand Down
1 change: 1 addition & 0 deletions docs/supported_ports.rst
Expand Up @@ -20,6 +20,7 @@ Additional testing is limited.
../ports/mimxrt10xx/README
../ports/nordic/README
../ports/raspberrypi/README
../ports/renode/README
../ports/silabs/README
../ports/stm/README
../ports/unix/README
11 changes: 7 additions & 4 deletions main.c
Expand Up @@ -58,7 +58,6 @@
#include "supervisor/shared/tick.h"
#include "supervisor/shared/traceback.h"
#include "supervisor/shared/workflow.h"
#include "supervisor/usb.h"
#include "supervisor/workflow.h"
#include "supervisor/shared/external_flash/external_flash.h"

Expand Down Expand Up @@ -115,10 +114,14 @@
#include "supervisor/shared/status_bar.h"
#endif

#if CIRCUITPY_USB_HID
#if CIRCUITPY_USB_DEVICE && CIRCUITPY_USB_HID
#include "shared-module/usb_hid/__init__.h"
#endif

#if CIRCUITPY_TINYUSB
#include "supervisor/usb.h"
#endif

#if CIRCUITPY_WIFI
#include "shared-bindings/wifi/__init__.h"
#endif
Expand Down Expand Up @@ -429,7 +432,7 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
}
}

STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
STATIC bool __attribute__((noinline)) run_code_py(safe_mode_t safe_mode, bool *simulate_reset) {
bool serial_connected_at_start = serial_connected();
bool printed_safe_mode_message = false;
#if CIRCUITPY_AUTORELOAD_DELAY_MS > 0
Expand Down Expand Up @@ -1160,7 +1163,7 @@ void gc_collect(void) {
common_hal_bleio_gc_collect();
#endif

#if CIRCUITPY_USB_HID
#if CIRCUITPY_USB_DEVICE && CIRCUITPY_USB_HID
usb_hid_gc_collect();
#endif

Expand Down
5 changes: 4 additions & 1 deletion ports/espressif/common-hal/_bleio/Adapter.c
Expand Up @@ -37,7 +37,6 @@
#include "supervisor/shared/bluetooth/bluetooth.h"
#include "supervisor/shared/safe_mode.h"
#include "supervisor/shared/tick.h"
#include "supervisor/usb.h"
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Adapter.h"
#include "shared-bindings/_bleio/Address.h"
Expand All @@ -59,6 +58,10 @@
#include "esp_bt.h"
#include "esp_nimble_hci.h"

#if CIRCUITPY_TINYUSB
#include "supervisor/usb.h"
#endif

#if CIRCUITPY_OS_GETENV
#include "shared-module/os/__init__.h"
#endif
Expand Down
3 changes: 3 additions & 0 deletions ports/espressif/supervisor/internal_flash.c
Expand Up @@ -43,7 +43,10 @@

#include "supervisor/filesystem.h"
#include "supervisor/flash.h"

#if CIRCUITPY_USB_DEVICE
#include "supervisor/usb.h"
#endif

#define OP_READ 0
#define OP_WRITE 1
Expand Down
3 changes: 0 additions & 3 deletions ports/nordic/background.c
Expand Up @@ -27,10 +27,7 @@
#include "background.h"

#include "py/runtime.h"
#include "supervisor/filesystem.h"
#include "supervisor/port.h"
#include "supervisor/shared/stack.h"
#include "supervisor/usb.h"

#if CIRCUITPY_DISPLAYIO
#include "shared-module/displayio/__init__.h"
Expand Down
8 changes: 4 additions & 4 deletions ports/nordic/common-hal/_bleio/Adapter.c
Expand Up @@ -26,7 +26,6 @@
* THE SOFTWARE.
*/

#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
Expand All @@ -43,14 +42,15 @@
#include "supervisor/shared/bluetooth/bluetooth.h"
#include "supervisor/shared/safe_mode.h"
#include "supervisor/shared/tick.h"
#include "supervisor/usb.h"
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Adapter.h"
#include "shared-bindings/_bleio/Address.h"
#include "shared-bindings/nvm/ByteArray.h"
#include "shared-bindings/_bleio/Connection.h"
#include "shared-bindings/_bleio/ScanEntry.h"
#include "shared-bindings/time/__init__.h"

#if CIRCUITPY_USB_DEVICE
#include "supervisor/usb.h"
#endif

#if CIRCUITPY_OS_GETENV
#include "shared-bindings/os/__init__.h"
Expand Down
128 changes: 128 additions & 0 deletions ports/renode/Makefile
@@ -0,0 +1,128 @@
# This file is part of the MicroPython project, http://micropython.org/
#
# The MIT License (MIT)
#
# SPDX-FileCopyrightText: Copyright (c) 2019 Dan Halbert for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

include ../../py/circuitpy_mkenv.mk

CROSS_COMPILE = arm-none-eabi-

INC += \
-I. \
-I../.. \
-I../lib/mp-readline \
-I../shared/timeutils \
-Iboards/$(BOARD) \
-Iboards/ \
-isystem ./../../lib/cmsis/inc \
-I$(BUILD)

CFLAGS += -ggdb3 -Os

DISABLE_WARNINGS = -Wno-cast-align
CFLAGS += $(INC) -Wall -Werror -std=gnu11 -fshort-enums $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT) $(DISABLE_WARNINGS) -Werror=missing-prototypes

CFLAGS += \
-march=armv6-m \
-mthumb \
-mabi=aapcs \
-mcpu=cortex-m0plus \
-msoft-float \
-mfloat-abi=soft \
--specs=nano.specs

# Use toolchain libm if we're not using our own.
ifndef INTERNAL_LIBM
LIBS += -lm
endif

LIBS += -lc

SRC_C += \
boards/$(BOARD)/board.c \
boards/$(BOARD)/pins.c \
background.c \
mphalport.c \


SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
$(addprefix common-hal/, $(SRC_COMMON_HAL))

SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
$(addprefix shared-module/, $(SRC_SHARED_MODULE)) \
$(addprefix shared-module/, $(SRC_SHARED_MODULE_INTERNAL))

# There may be duplicates between SRC_COMMON_HAL_EXPANDED and SRC_SHARED_MODULE_EXPANDED,
# because a few modules have files both in common-hal/ and shared-module/.
# Doing a $(sort ...) removes duplicates as part of sorting.
SRC_COMMON_HAL_SHARED_MODULE_EXPANDED = $(sort $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED))

SRC_S = supervisor/$(CPU)_cpu.s

OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED:.c=.o))
ifeq ($(INTERNAL_LIBM),1)
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
endif
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))

$(BUILD)/%.o: $(BUILD)/%.S
$(STEPECHO) "CC $<"
$(Q)$(CC) $(CFLAGS) -c -o $@ $<

SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) $(SRC_CIRCUITPY_COMMON)

all: $(BUILD)/firmware.elf $(BUILD)/circuitpy.img

BOARD_LD := $(wildcard boards/$(BOARD)/link.ld)

ifneq ($(BOARD_LD),)
LINKER_SCRIPTS = -Wl,-T,$(BOARD_LD)
endif

LINKER_SCRIPTS += -Wl,-T,link.ld

$(BUILD)/circuitpy.img: circuitpy/code.py
$(STEPECHO) "Create $@"
$(Q)dd if=/dev/zero of=$(BUILD)/circuitpy.img bs=1 count=0 seek=512K
$(Q)mkfs.fat -n CIRCUITPY --offset=0 $(BUILD)/circuitpy.img
$(Q)mcopy -i $(BUILD)/circuitpy.img circuitpy/* ::

ifeq ($(VALID_BOARD),)
$(BUILD)/firmware.elf: invalid-board
else
$(BUILD)/firmware.elf: $(OBJ) $(BOARD_LD) link.ld
$(STEPECHO) "LINK $@"
$(Q)echo $(OBJ) > $(BUILD)/firmware.objs
$(Q)echo $(PICO_LDFLAGS) > $(BUILD)/firmware.ldflags
$(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
endif

$(BUILD)/firmware.bin: $(BUILD)/firmware.elf
$(STEPECHO) "Create $@"
$(Q)$(OBJCOPY) -O binary -R .dtcm_bss $^ $@

include $(TOP)/py/mkrules.mk
49 changes: 49 additions & 0 deletions ports/renode/README.md
@@ -0,0 +1,49 @@
# Renode

Renode is an emulator targeting microcontroller-class devices. This port is a
minimal version of CircuitPython that runs under Renode. Renode is designed to
mimic full microcontrollers but CP uses more peripherals than what Renode has
implemented so far. This port allows us to run on a variety of CPUs without
worrying about peripherals.

## Running

1. Get Renode: https://renode.io/#downloads
2. `cd ports/renode`
3. `make BOARD=renode_cortex_m0plus`
4. In another tab: `tio /tmp/cp-uart`
5. `renode`
6. In renode: `include @renode.resc`
7. <Any other setup>
8. `start`
9. `pause`
10. `quit`

Step 4 sets up `tio` to talk to CircuitPython via UART <-> PTY bridge.

## Other stuff

### Emulator logging
Renode modules have debug logging that can be enabled with `logLevel` with an int
between `-1` for `NOISY` and `3` for errors only.

### GDB

Renode can provide a GDB server. It is very useful for precisely controlling the
emulator's execution.

```
machine StartGdbServer 3333 true
```

### Execution profiling

In renode do `cpu EnableProfiler CollapsedStack $ORIGIN/profile.folded` before starting
the emulation. You can view it using [Speedscope](https://www.speedscope.app/). CircuitPython calls
a lot of functions and may overwhelm speedscope. You can enable this tracing over a specific
section of CircuitPython execution to limit the capture size.

[Related Renode Docs](https://renode.readthedocs.io/en/latest/advanced/execution-tracing.html)

### Execution tracing
If you want to see every instruction run you can do: `cpu CreateExecutionTracing "tracer_name" $ORIGIN/instruction_trace.txt Disassembly`.
50 changes: 50 additions & 0 deletions ports/renode/Simple32kHz.cs
@@ -0,0 +1,50 @@
//
// Copyright (c) 2010-2022 Antmicro
// Copyright (c) 2011-2015 Realtime Embedded
//
// This file is licensed under the MIT License.
// Full license text is available in 'licenses/MIT.txt'.
//
// This is modified for CircuitPython to tick at 32kHz like a slow external
// crystal would.
using System;
using Antmicro.Renode.Core;
using Antmicro.Renode.Peripherals.Bus;
using Antmicro.Renode.Time;
using Antmicro.Renode.Logging;
using System.Threading;

namespace Antmicro.Renode.Peripherals.Timers
{
public class Simple32kHz : IDoubleWordPeripheral, IKnownSize
{
public long Size { get { return 0x4; } }

public Simple32kHz(IMachine machine)
{
machine.ClockSource.AddClockEntry(new ClockEntry(1, 32768, OnTick, this, String.Empty));
}

public virtual uint ReadDoubleWord(long offset)
{
return (uint)counter;
}

public virtual void WriteDoubleWord(long offset, uint value)
{
this.LogUnhandledWrite(offset, value);
}

public virtual void Reset()
{
Interlocked.Exchange(ref counter, 0);
}

private void OnTick()
{
Interlocked.Increment(ref counter);
}

private int counter;
}
}
39 changes: 39 additions & 0 deletions ports/renode/background.c
@@ -0,0 +1,39 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "supervisor/port.h"

void port_start_background_tick(void) {
}

void port_finish_background_tick(void) {
}

void port_background_tick(void) {
}

void port_background_task(void) {
}

0 comments on commit 8734076

Please sign in to comment.