Skip to content

Commit

Permalink
refactor(VirtIO): move VirtIOSerial as a built-in library
Browse files Browse the repository at this point in the history
Including OpenAMP.

Signed-off-by: Frederic Pillon <[email protected]>
  • Loading branch information
fpistm committed Jun 19, 2024
1 parent 376232d commit 8136da7
Show file tree
Hide file tree
Showing 47 changed files with 160 additions and 38 deletions.
15 changes: 15 additions & 0 deletions License.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Note: most license information is available on top of each source file

* cores/arduino/stm32/ mainly contains source from STMicroelectronics.
* system/Drivers/STM32*xx_HAL_Driver folders include the STMicroelectronics HAL Drivers
* system/Middlewares/OpenAMP
* libraries/VirtIO - except virtio implementation (see [MIT License](#mit-license))
* libraries/SrcWrapper/inc/PinName*.h

[Ultimate Liberty License](#Ultimate-Liberty-License) is used for:
Expand All @@ -22,6 +24,9 @@ Note: most license information is available on top of each source file
* libraries/SrcWrapper/src/stm32/pinmap.c
* tools/platformio/platformio-build.py

[MIT License](#mit-license)
* libraries/VirtIO/*/virtio*

Note:
* system/STM32*xx/system_stm32*xx.c uses same license than system/Drivers/ subfolders
* system/STM32*xx/stm32*xx_hal_conf_default.h uses same license than system/Drivers/CMSIS
Expand Down Expand Up @@ -776,3 +781,13 @@ the License. You may obtain a copy of the License at:
limitations under the License.

-------------------------------------------------------------------------------
## MIT License
The MIT License (MIT)

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.

-------------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion cmake/set_base_arduino_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ target_include_directories(base_config INTERFACE
"${BUILD_CORE_PATH}/avr"
"${BUILD_CORE_PATH}/stm32"
"${BUILD_CORE_PATH}/stm32/usb"
"${BUILD_CORE_PATH}/stm32/OpenAMP"
"${BUILD_CORE_PATH}/stm32/usb/hid"
"${BUILD_CORE_PATH}/stm32/usb/cdc"
"${BUILD_LIB_PATH}/SrcWrapper/inc"
"${BUILD_LIB_PATH}/SrcWrapper/inc/LL"
"${BUILD_LIB_PATH}/VirtIO/inc"
"${BUILD_SYSTEM_PATH}/Middlewares/ST/STM32_USB_Device_Library/Core/Inc"
"${BUILD_SYSTEM_PATH}/Middlewares/ST/STM32_USB_Device_Library/Core/Src"
"${CMSIS5_PATH}/CMSIS/DSP/Include"
Expand Down
1 change: 1 addition & 0 deletions cmake/templates/easy_cmake.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ build_sketch(TARGET "{{tgtname or "@binary_name_here@"}}"
# SD
# Wire
# SPI
# VirtIO
)

# STEP 4: optional features
Expand Down
24 changes: 0 additions & 24 deletions cores/arduino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,6 @@ add_library(core_bin STATIC EXCLUDE_FROM_ALL
pins_arduino.c
Print.cpp
RingBuffer.cpp
stm32/OpenAMP/libmetal/device.c
stm32/OpenAMP/libmetal/generic/condition.c
stm32/OpenAMP/libmetal/generic/cortexm/sys.c
stm32/OpenAMP/libmetal/generic/generic_device.c
stm32/OpenAMP/libmetal/generic/generic_init.c
stm32/OpenAMP/libmetal/generic/generic_io.c
stm32/OpenAMP/libmetal/generic/generic_shmem.c
stm32/OpenAMP/libmetal/generic/time.c
stm32/OpenAMP/libmetal/init.c
stm32/OpenAMP/libmetal/io.c
stm32/OpenAMP/libmetal/log.c
stm32/OpenAMP/libmetal/shmem.c
stm32/OpenAMP/mbox_ipcc.c
stm32/OpenAMP/open-amp/remoteproc/remoteproc_virtio.c
stm32/OpenAMP/open-amp/rpmsg/rpmsg.c
stm32/OpenAMP/open-amp/rpmsg/rpmsg_virtio.c
stm32/OpenAMP/openamp.c
stm32/OpenAMP/rsc_table.c
stm32/OpenAMP/virt_uart.c
stm32/OpenAMP/virtio/virtio.c
stm32/OpenAMP/virtio/virtqueue.c
stm32/OpenAMP/virtio_buffer.c
stm32/OpenAMP/virtio_log.c
stm32/startup_stm32yyxx.S
stm32/usb/cdc/cdc_queue.c
stm32/usb/cdc/usbd_cdc.c
Expand All @@ -70,7 +47,6 @@ add_library(core_bin STATIC EXCLUDE_FROM_ALL
Stream.cpp
Tone.cpp
USBSerial.cpp
VirtIOSerial.cpp
WInterrupts.cpp
wiring_analog.c
wiring_digital.c
Expand Down
4 changes: 3 additions & 1 deletion cores/arduino/WSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "variant.h"
#include "HardwareSerial.h"
#include "USBSerial.h"
#include "VirtIOSerial.h"
#if defined(VIRTIOCON)
#include "VirtIOSerial.h"
#endif /* VIRTIOCON */

#if defined (USBCON) && defined(USBD_USE_CDC)
#ifndef DISABLE_GENERIC_SERIALUSB
Expand Down
1 change: 1 addition & 0 deletions libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ add_subdirectory(SPI)
add_subdirectory(Servo)
add_subdirectory(SoftwareSerial)
add_subdirectory(SrcWrapper)
add_subdirectory(VirtIO)
add_subdirectory(Wire)
53 changes: 53 additions & 0 deletions libraries/VirtIO/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# v3.21 implemented semantic changes regarding $<TARGET_OBJECTS:...>
# See https://cmake.org/cmake/help/v3.21/command/target_link_libraries.html#linking-object-libraries-via-target-objects
cmake_minimum_required(VERSION 3.21)

add_library(VirtIO INTERFACE)
add_library(VirtIO_usage INTERFACE)

target_include_directories(VirtIO_usage INTERFACE
src
)


target_link_libraries(VirtIO_usage INTERFACE
base_config
)

target_link_libraries(VirtIO INTERFACE VirtIO_usage)



add_library(VirtIO_bin OBJECT EXCLUDE_FROM_ALL
src/libmetal/device.c
src/libmetal/generic/condition.c
src/libmetal/generic/cortexm/sys.c
src/libmetal/generic/generic_device.c
src/libmetal/generic/generic_init.c
src/libmetal/generic/generic_io.c
src/libmetal/generic/generic_shmem.c
src/libmetal/generic/time.c
src/libmetal/init.c
src/libmetal/io.c
src/libmetal/log.c
src/libmetal/shmem.c
src/mbox_ipcc.c
src/open-amp/remoteproc/remoteproc_virtio.c
src/open-amp/rpmsg/rpmsg.c
src/open-amp/rpmsg/rpmsg_virtio.c
src/openamp.c
src/rsc_table.c
src/virt_uart.c
src/virtio/virtio.c
src/virtio/virtqueue.c
src/virtio_buffer.c
src/virtio_log.c
src/VirtIOSerial.cpp
)
target_link_libraries(VirtIO_bin PUBLIC VirtIO_usage)

target_link_libraries(VirtIO INTERFACE
VirtIO_bin
$<TARGET_OBJECTS:VirtIO_bin>
)

25 changes: 25 additions & 0 deletions libraries/VirtIO/examples/loopback/loopback.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
int available;
char buffer[1024];

unsigned long start_time = 0;

void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
available = Serial.available();
while (available > 0) {
int size = min(available, Serial.availableForWrite());
Serial.readBytes(buffer, size);
Serial.write(buffer, size);
available -= size;
}

// Heartbeat. If Arduino stops the LED won't flash anymore.
if ((millis() - start_time) > 1000) {
start_time = millis();
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions libraries/VirtIO/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#######################################
# Syntax Coloring Map For VirtIO
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################
VirtIOSerial KEYWORD1
VIRT_UART_HandleTypeDef KEYWORD1
VirtIOSerialObj_t KEYWORD1
virtio_buffer_t KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
SerialVirtIO KEYWORD2
begin KEYWORD2
available KEYWORD2
availableForWrite KEYWORD2
peek KEYWORD2
read KEYWORD2
readBytes KEYWORD2
write KEYWORD2
flush KEYWORD2
rxGenericCallback KEYWORD2
rxCallback KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
9 changes: 9 additions & 0 deletions libraries/VirtIO/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=VirtIO
version=1.0.0
author=Bumsik kim
maintainer=stm32duino
sentence=Enables serial communication between Cortex-A7 (Linux) and Cortex-M4 (Arduino) on STM32 MPUs via shared memory.
paragraph=Provide VirtIOSerial based on OpenAMP.
category=Communication
url=https://github.com/stm32duino/Arduino_Core_STM32
architectures=stm32
4 changes: 4 additions & 0 deletions libraries/VirtIO/src/VirtIO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef __VIRTIO_H__
#define __VIRTIO_H__

#endif /* __VIRTIO_H__ */
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ cmsis_dir={runtime.tools.CMSIS-5.9.0.path}/CMSIS
cmsis_dev_dir={build.system.path}/Drivers/CMSIS/Device/ST/{build.series}
usbd_core_dir={build.system.path}/Middlewares/ST/STM32_USB_Device_Library/Core
SrcWrapper_include_dir={runtime.platform.path}/libraries/SrcWrapper/inc
VirtIO_include_dir={runtime.platform.path}/libraries/VirtIO/inc

# STM compile variables
# ----------------------
compiler.stm.extra_include="-I{build.source.path}" "-I{build.core.path}/avr" "-I{core_stm32_dir}" "-I{core_stm32_dir}/LL" "-I{SrcWrapper_include_dir}" "-I{core_usb_dir}" "-I{core_stm32_dir}/OpenAMP" "-I{core_usb_dir}/hid" "-I{core_usb_dir}/cdc" "-I{hal_dir}/Inc" "-I{hal_dir}/Src" "-I{build.system.path}/{build.series}" "-I{usbd_core_dir}/Inc" "-I{usbd_core_dir}/Src" {build.virtio_extra_include}
compiler.stm.extra_include="-I{build.source.path}" "-I{build.core.path}/avr" "-I{core_stm32_dir}" "-I{SrcWrapper_include_dir}" "-I{SrcWrapper_include_dir}/LL" "-I{core_usb_dir}" "-I{core_usb_dir}/hid" "-I{core_usb_dir}/cdc" "-I{hal_dir}/Inc" "-I{hal_dir}/Src" "-I{build.system.path}/{build.series}" "-I{usbd_core_dir}/Inc" "-I{usbd_core_dir}/Src" "-I{VirtIO_include_dir}" {build.virtio_extra_include}
compiler.arm.cmsis.c.flags="-I{cmsis_dir}/Core/Include/" "-I{cmsis_dev_dir}/Include/" "-I{cmsis_dev_dir}/Source/Templates/gcc/" "-I{cmsis_dir}/DSP/Include" "-I{cmsis_dir}/DSP/PrivateInclude"

compiler.warning_flags=-w
Expand Down Expand Up @@ -145,7 +146,7 @@ build.opt.path={build.path}/sketch/{build.opt.name}
extras.path={build.system.path}/extras

# Create {build.opt} if not exists in the output sketch dir and force include of SrcWrapper library
recipe.hooks.prebuild.1.pattern="{busybox}" sh "{extras.path}/prebuild.sh" "{build.path}" "{build.source.path}" "{runtime.platform.path}"
recipe.hooks.prebuild.1.pattern="{busybox}" sh "{extras.path}/prebuild.sh" "{build.path}" "{build.source.path}" "{runtime.platform.path}" "{build.enable_virtio}"
recipe.hooks.postbuild.1.pattern="{busybox}" sh "{extras.path}/postbuild.sh" "{build.path}" "{build.series}" "{runtime.platform.path}"

# compile patterns
Expand Down
7 changes: 6 additions & 1 deletion system/extras/prebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
BUILD_PATH="$1"
BUILD_SOURCE_PATH="$2"
BOARD_PLATFORM_PATH="$3"
BUILD_VIRTIO="$4"

# Create sketch dir if not exists
if [ ! -f "$BUILD_PATH/sketch" ]; then
Expand Down Expand Up @@ -43,4 +44,8 @@ esac
printf '\n-fmacro-prefix-map="%s"=.' "${prefix}" >>"$BUILD_PATH/sketch/build.opt"

# Force include of SrcWrapper library
echo "#include <SrcWrapper.h>" >"$BUILD_PATH/sketch/SrcWrapper.cpp"
echo "#include <SrcWrapper.h>" >"$BUILD_PATH/sketch/requiredLibraries.cpp"
# Force include of VirtIO library if required
if [ -n "${BUILD_VIRTIO#*=}" ]; then
echo "#include <VirtIO.h>" >>"$BUILD_PATH/sketch/requiredLibraries.cpp"
fi
2 changes: 1 addition & 1 deletion tools/platformio/platformio-build.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ def get_arduino_board_id(board_config, mcu):
join(FRAMEWORK_DIR, "cores", "arduino", "avr"),
join(FRAMEWORK_DIR, "cores", "arduino", "stm32"),
join(FRAMEWORK_DIR, "cores", "arduino", "stm32", "usb"),
join(FRAMEWORK_DIR, "cores", "arduino", "stm32", "OpenAMP"),
join(FRAMEWORK_DIR, "cores", "arduino", "stm32", "usb", "hid"),
join(FRAMEWORK_DIR, "cores", "arduino", "stm32", "usb", "cdc"),
join(FRAMEWORK_DIR, "libraries", "SrcWrapper", "inc"),
join(FRAMEWORK_DIR, "libraries", "SrcWrapper", "inc", "LL"),
join(FRAMEWORK_DIR, "libraries", "VirtIO", "inc"),
join(FRAMEWORK_DIR, "system", "Drivers", series + "_HAL_Driver", "Inc"),
join(FRAMEWORK_DIR, "system", "Drivers", series + "_HAL_Driver", "Src"),
join(FRAMEWORK_DIR, "system", series),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ See [OpenAMP] and [Linux RPMsg] to learn more.

To increase the performance of SerialVirtIO you can resize the related buffer configurations. There are three definitions you can use:

* [`VRING_NUM_BUFFS`](/cores/arduino/stm32/OpenAMP/virtio_config.h)
* [`RPMSG_BUFFER_SIZE`](/cores/arduino/stm32/OpenAMP/virtio_config.h)
* [`VIRTIO_BUFFER_SIZE`](/cores/arduino/stm32/OpenAMP/virtio_buffer.h)
* [`VRING_NUM_BUFFS`](/libraries/VirtIO/inc/virtio_config.h)
* [`RPMSG_BUFFER_SIZE`](/libraries/VirtIO/inc/virtio_config.h)
* [`VIRTIO_BUFFER_SIZE`](/libraries/VirtIO/inc/virtio_buffer.h)

The recommended option is to resize `VRING_NUM_BUFFS`. Be very cautious when resizing `RPMSG_BUFFER_SIZE`, which must be matched with the Linux kernel definition. Also `VIRTIO_BUFFER_SIZE` has the minimum required size depending on the other two. See their links above for further descriptions.

Expand All @@ -122,7 +122,7 @@ Here is a basic echo example:
int available;
char buffer[1024];

unsigned long time = 0;
unsigned long start_time = 0;

void setup() {
// You can SerialVirtIO.begin() and use SerialVirtIO later instead.
Expand All @@ -140,8 +140,8 @@ void loop() {
}

// Heartbeat. If Arduino stops the LED won't flash anymore.
if ((millis() - time) > 1000) {
time = millis();
if ((millis() - start_time) > 1000) {
start_time = millis();
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
}
Expand Down Expand Up @@ -308,11 +308,11 @@ And then the Device Tree should enable TIM1 for the coprocessor, although this d

[OpenAMP]: https://github.com/OpenAMP/open-amp/wiki/OpenAMP-Overview
[Linux RPMsg]: https://wiki.st.com/stm32mpu/wiki/Linux_RPMsg_framework_overview
[a hard restriction of the write size]: /cores/arduino/VirtIOSerial.cpp#L148
[a hard restriction of the write size]: /libraries/VirtIO/src/VirtIOSerial.cpp#L148

[build_opt.h]: https://github.com/stm32duino/Arduino_Core_STM32/wiki/Customize-build-options-using-build_opt.h
[build_opt.h description in wiki]: https://github.com/stm32duino/Arduino_Core_STM32/wiki/Customize-build-options-using-build_opt.h
[virtio_log.h]: /cores/arduino/stm32/OpenAMP/virtio_log.h
[virtio_log.h]: /libraries/VirtIO/inc/virtio_log.h

[`variant_STM32MP157_DK.h` of the board]: /variants/STM32MP1xx/MP153AAC_MP153CAC_MP153DAC_MP153FAC_MP157AAC_MP157CAC_MP157DAC_MP157FAC/variant_STM32MP157_DK.h

Expand Down

0 comments on commit 8136da7

Please sign in to comment.