Skip to content

Commit

Permalink
Add support for CH32V203C8 and CH32V203C6 (#23)
Browse files Browse the repository at this point in the history
* Update package.json

* Fix multiple build warnings and errors

* Fix multiple build warnings and errors

* Add PlatformIO builder script

* Fix build with mac toolchain, wtf?

* Add support for CH32V203C6 and CH32V203C6

* fix some warnings

---------

Co-authored-by: Maximilian Gerhardt <[email protected]>
  • Loading branch information
redstonee and maxgerhardt authored Oct 9, 2023
1 parent 29c76ad commit f187ac1
Show file tree
Hide file tree
Showing 29 changed files with 1,385 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
38 changes: 37 additions & 1 deletion boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ CH32V20x_EVT.menu.pnum.CH32V203G8=CH32V203G8 EVT
CH32V20x_EVT.menu.pnum.CH32V203G8.node=NODE_V203G8
CH32V20x_EVT.menu.pnum.CH32V203G8.upload.maximum_size=65536
CH32V20x_EVT.menu.pnum.CH32V203G8.upload.maximum_data_size=20480
CH32V20x_EVT.menu.pnum.CH32V203G8.build.mcu=QingKe-V4C
CH32V20x_EVT.menu.pnum.CH32V203G8.build.mcu=QingKe-V4B
CH32V20x_EVT.menu.pnum.CH32V203G8.build.board=CH32V203G8
CH32V20x_EVT.menu.pnum.CH32V203G8.build.series=CH32V20x
CH32V20x_EVT.menu.pnum.CH32V203G8.build.variant=CH32V20x/CH32V203G8
Expand All @@ -297,6 +297,42 @@ CH32V20x_EVT.menu.pnum.CH32V203G8.build.IQ_math_RV32=
CH32V20x_EVT.menu.pnum.CH32V203G8.build.ch_extra_lib=-lprintf



#Generic CH32V203C8 Board
CH32V20x_EVT.menu.pnum.CH32V203C8=CH32V203C8
CH32V20x_EVT.menu.pnum.CH32V203C8.node=NODE_V203C8
CH32V20x_EVT.menu.pnum.CH32V203C8.upload.maximum_size=65536
CH32V20x_EVT.menu.pnum.CH32V203C8.upload.maximum_data_size=20480
CH32V20x_EVT.menu.pnum.CH32V203C8.build.mcu=QingKe-V4B
CH32V20x_EVT.menu.pnum.CH32V203C8.build.board=CH32V203C8
CH32V20x_EVT.menu.pnum.CH32V203C8.build.series=CH32V20x
CH32V20x_EVT.menu.pnum.CH32V203C8.build.variant=CH32V20x/CH32V203C8
CH32V20x_EVT.menu.pnum.CH32V203C8.build.chip=CH32V203
CH32V20x_EVT.menu.pnum.CH32V203C8.build.march=rv32imacxw
CH32V20x_EVT.menu.pnum.CH32V203C8.build.mabi=ilp32
CH32V20x_EVT.menu.pnum.CH32V203C8.build.math_lib_gcc=-lm
CH32V20x_EVT.menu.pnum.CH32V203C8.build.IQ_math_RV32=
CH32V20x_EVT.menu.pnum.CH32V203C8.build.ch_extra_lib=-lprintf


#Generic CH32V203C6 Board
CH32V20x_EVT.menu.pnum.CH32V203C6=CH32V203C6
CH32V20x_EVT.menu.pnum.CH32V203C6.node=NODE_V203C6
CH32V20x_EVT.menu.pnum.CH32V203C6.upload.maximum_size=32768
CH32V20x_EVT.menu.pnum.CH32V203C6.upload.maximum_data_size=20480
CH32V20x_EVT.menu.pnum.CH32V203C6.build.mcu=QingKe-V4B
CH32V20x_EVT.menu.pnum.CH32V203C6.build.board=CH32V203C6
CH32V20x_EVT.menu.pnum.CH32V203C6.build.series=CH32V20x
CH32V20x_EVT.menu.pnum.CH32V203C6.build.variant=CH32V20x/CH32V203C6
CH32V20x_EVT.menu.pnum.CH32V203C6.build.chip=CH32V203
CH32V20x_EVT.menu.pnum.CH32V203C6.build.march=rv32imacxw
CH32V20x_EVT.menu.pnum.CH32V203C6.build.mabi=ilp32
CH32V20x_EVT.menu.pnum.CH32V203C6.build.math_lib_gcc=-lm
CH32V20x_EVT.menu.pnum.CH32V203C6.build.IQ_math_RV32=
CH32V20x_EVT.menu.pnum.CH32V203C6.build.ch_extra_lib=-lprintf



# Upload menu
CH32V20x_EVT.menu.upload_method.swdMethod=WCH-SWD
CH32V20x_EVT.menu.upload_method.swdMethod.upload.protocol=
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/ch32/backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ static inline void resetBackupDomain(void)
#ifdef PWR_MODULE_ENABLED
PWR_BackupAccessCmd(ENABLE);
#endif
#ifndef CH32V00x
RCC_BackupResetCmd(ENABLE);
RCC_BackupResetCmd(DISABLE);
#endif
}

static inline void enableBackupDomain(void)
Expand Down
1 change: 1 addition & 0 deletions cores/arduino/ch32/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern "C" {
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
WEAK uint64_t GetTick(void);
uint32_t getCurrentMillis(void);
uint32_t getCurrentMicros(void);

Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/ch32/lib/ch32yyxx_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#include "ch32v20x_can.c"
#endif

#ifdef defined(CH32V30x) || defined(CH32V30x_C)
#if defined(CH32V30x) || defined(CH32V30x_C)
#include "ch32v30x_can.c"
#endif
2 changes: 1 addition & 1 deletion cores/arduino/ch32/lib/ch32yyxx_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "ch32v20x_can.h"
#endif

#ifdef defined(CH32V30x) || defined(CH32V30x_C)
#if defined(CH32V30x) || defined(CH32V30x_C)
#include "ch32v30x_can.h"
#endif

Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/ch32/lib/ch32yyxx_crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
#include "ch32v20x_crc.c"
#endif

#ifdef defined(CH32V30x) || defined(CH32V30x_C)
#if defined(CH32V30x) || defined(CH32V30x_C)
#include "ch32v30x_crc.c"
#endif
2 changes: 1 addition & 1 deletion cores/arduino/ch32/lib/ch32yyxx_crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "ch32v20x_crc.h"
#endif

#ifdef defined(CH32V30x) || defined(CH32V30x_C)
#if defined(CH32V30x) || defined(CH32V30x_C)
#include "ch32v30x_crc.h"
#endif

Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/ch32/lib/ch32yyxx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "ch32v20x_it.h"
#endif

#ifdef defined (CH32V30x) || defined(CH32V30x_C)
#if defined (CH32V30x) || defined(CH32V30x_C)
#include "ch32v30x_it.h"
#endif

Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/ch32/lib/ch32yyxx_opa.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
#include "ch32v20x_opa.c"
#endif

#ifdef defined(CH32V30x) || defined(CH32V30x_C)
#if defined(CH32V30x) || defined(CH32V30x_C)
#include "ch32v30x_opa.c"
#endif
2 changes: 1 addition & 1 deletion cores/arduino/ch32/lib/ch32yyxx_opa.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "ch32v20x_opa.h"
#endif

#ifdef defined(CH32V30x) || defined(CH32V30x_C)
#if defined(CH32V30x) || defined(CH32V30x_C)
#include "ch32v30x_opa.h"
#endif

Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/ch32/lib/ch32yyxx_pwr.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
#include "ch32v20x_pwr.c"
#endif

#ifdef defined(CH32V30x) || defined(CH32V30x_C)
#if defined(CH32V30x) || defined(CH32V30x_C)
#include "ch32v30x_pwr.c"
#endif
2 changes: 1 addition & 1 deletion cores/arduino/ch32/lib/ch32yyxx_rcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "ch32v20x_rcc.h"
#endif

#ifdef defined(CH32V30x) || defined(CH32V30x_C)
#if defined(CH32V30x) || defined(CH32V30x_C)
#include "ch32v30x_rcc.h"
#endif

Expand Down
4 changes: 3 additions & 1 deletion cores/arduino/ch32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ static serial_t serial_debug =
.index = UART_NUM,
};

extern uint64_t GetTick(void);

/* Aim of the function is to get serial_s pointer using huart pointer */
/* Highly inspired from magical linux kernel's "container_of" */
serial_t *get_serial_obj(UART_HandleTypeDef *huart)
Expand Down Expand Up @@ -263,7 +265,7 @@ void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par

if (uart_rx == NP)
{
USART_HalfDuplexCmd(huart,ENABLE);
USART_HalfDuplexCmd(huart->Instance,ENABLE);
}

USART_Init(huart->Instance, &(huart->init));
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "framework-arduino-ch32v",
"description": "Arduino Wiring-based Framework (CH32V Core)",
"version": "0.0.0",
"url": "https://github.com/openwch/arduino_core_ch32"
}
"description": "Core library for CH32duino",
"name": "framework-arduino-openwch-ch32",
"system": "all",
"url": "https://github.com/openwch/arduino_core_ch32",
"version": "0.0.0"
}
160 changes: 160 additions & 0 deletions tools/platformio-build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Copyright 2023-present Maximilian Gerhardt <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Arduino
Arduino Wiring-based Framework allows writing cross-platform software to
control devices attached to a wide range of Arduino boards to create all
kinds of creative coding, interactive objects, spaces or physical experiences.
http://arduino.cc/en/Reference/HomePage
"""

from os.path import isdir, join
import sys

from SCons.Script import DefaultEnvironment

IS_MAC = sys.platform.startswith("darwin")

env = DefaultEnvironment()
platform = env.PioPlatform()
board = env.BoardConfig()
mcu = env.BoardConfig().get("build.mcu")
chip_series: str = board.get("build.series", "")[0:-1].upper() + "x"
variant_h = board.get("build.arduino.openwch.variant_h")

FRAMEWORK_DIR = platform.get_package_dir("framework-arduino-openwch-ch32")
assert isdir(FRAMEWORK_DIR)

machine_flags = [
"-march=%s" % board.get("build.march"),
"-mabi=%s" % board.get("build.mabi"),
"-msmall-data-limit=8",
"-msave-restore",
]

env.Append(
ASFLAGS=machine_flags,
ASPPFLAGS=[
"-x", "assembler-with-cpp"
],

CFLAGS=[
"-std=gnu99"
],

CCFLAGS=machine_flags + [
"-Os",
"-Wall",
"-fmessage-length=0",
"-fsigned-char",
"-ffunction-sections",
"-fdata-sections",
"-fno-common",
#"-flto",
],

CXXFLAGS=[
"-fno-threadsafe-statics",
"-fno-rtti",
"-fno-exceptions",
"-fno-use-cxa-atexit",
"-fpermissive",
"-std=gnu++14"
],

LINKFLAGS=machine_flags + [
"-Os",
"-fmessage-length=0",
"-fsigned-char",
"-ffunction-sections",
"-fdata-sections",
"-fno-common",
"-Wl,--gc-sections",
#"-flto",
"--specs=nosys.specs",
"--specs=nano.specs",
"-nostartfiles",
'-Wl,-Map="%s"' % join("${BUILD_DIR}", "${PROGNAME}.map")
],

CPPDEFINES= [
("ARDUINO", 10808),
("VARIANT_H", env.StringifyMacro(variant_h)),
chip_series
],

# LIBS is handled in _LIBFLAGS below

LIBSOURCE_DIRS=[
join(FRAMEWORK_DIR, "libraries")
],

CPPPATH=[
join(FRAMEWORK_DIR, "cores", "arduino"),
join(FRAMEWORK_DIR, "cores", "arduino", "avr"),
join(FRAMEWORK_DIR, "cores", "arduino", "ch32"),
join(FRAMEWORK_DIR, "cores", "arduino", "ch32", "lib"),
join(FRAMEWORK_DIR, "system", chip_series, "USER"),
join(FRAMEWORK_DIR, "system", chip_series, "SRC", "Core"),
join(FRAMEWORK_DIR, "system", chip_series, "SRC", "Debug"),
join(FRAMEWORK_DIR, "system", chip_series, "SRC", "Startup"),
join(FRAMEWORK_DIR, "system", chip_series, "SRC", "Peripheral", "inc"),
join(FRAMEWORK_DIR, "system", chip_series, "SRC", "Peripheral", "src"),
],
)
# Use exact same size regexes as the real core
env.Replace(
SIZEPROGREGEX=r"^(?:\.text|\.data|\.rodata)\s+([0-9]+).*",
# excludes stack
SIZEDATAREGEXP=r"^(?:\.data|\.bss|\.noinit)\s+(\d+).*",
)

#
# Target: Build Core Library
#

libs = []

if "build.variant" in board:
variants_dir = join(
"$PROJECT_DIR", board.get("build.variants_dir")) if board.get(
"build.variants_dir", "") else join(FRAMEWORK_DIR, "variants")

variant = board.get("build.arduino.openwch.variant", board.get("build.variant"))
env.Append(
CPPPATH=[
join(variants_dir, variant)
],
LDSCRIPT_PATH=join(FRAMEWORK_DIR, "system", chip_series, "SRC", "Ld", "Link.ld"),

)
env.BuildSources(
join("$BUILD_DIR", "FrameworkArduinoVariant"),
join(variants_dir, variant)
)

# Startup files and debug.c require this to be built using BuildSources or with -Wl,-whole-archive
pre_libs = "-lprintf" if not IS_MAC else ""
env.Prepend(_LIBFLAGS="%s -Wl,--whole-archive " % pre_libs)
env.Append(_LIBFLAGS=" -Wl,--no-whole-archive -lc")

libs.append(env.BuildLibrary(
join("$BUILD_DIR", "FrameworkArduino"),
join(FRAMEWORK_DIR, "cores", "arduino"),
))

env.Prepend(LIBS=libs)
12 changes: 6 additions & 6 deletions variants/CH32V00x/CH32V003F4/PeripheralPins.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ WEAK const PinMap PinMap_ADC[] = {
#ifdef I2C_MODULE_ENABLED
WEAK const PinMap PinMap_I2C_SDA[] = {
{PC_1, I2C1, CH_PIN_DATA(CH_MODE_OUTPUT_50MHz, CH_CNF_OUTPUT_AFOD, NOPULL, AFIO_NONE)},
{NC, NP, 0, 0}
{NC, NP, 0}
};
#endif

#ifdef I2C_MODULE_ENABLED
WEAK const PinMap PinMap_I2C_SCL[] = {
{PC_2, I2C1, CH_PIN_DATA(CH_MODE_OUTPUT_50MHz, CH_CNF_OUTPUT_AFOD, NOPULL, AFIO_NONE)},
{NC, NP, 0, 0}
{NC, NP, 0}
};
#endif

Expand All @@ -82,28 +82,28 @@ WEAK const PinMap PinMap_TIM[] = {
#ifdef UART_MODULE_ENABLED
WEAK const PinMap PinMap_UART_TX[] = {
{PD_5, USART1, CH_PIN_DATA(CH_MODE_OUTPUT_50MHz, CH_CNF_OUTPUT_AFPP, 0, AFIO_NONE)},
{NC, NP, 0, 0}
{NC, NP, 0}
};
#endif

#ifdef UART_MODULE_ENABLED
WEAK const PinMap PinMap_UART_RX[] = {
{PD_6, USART1, CH_PIN_DATA(CH_MODE_INPUT, CH_CNF_INPUT_PUPD, PULLUP, AFIO_NONE)},
{NC, NP, 0, 0}
{NC, NP, 0}
};
#endif

#ifdef UART_MODULE_ENABLED
WEAK const PinMap PinMap_UART_RTS[] = {
{PC_2, USART1, CH_PIN_DATA(CH_MODE_OUTPUT_50MHz, CH_CNF_OUTPUT_AFPP, 0, AFIO_NONE)},
{NC, NP, 0, 0}
{NC, NP, 0}
};
#endif

#ifdef UART_MODULE_ENABLED
WEAK const PinMap PinMap_UART_CTS[] = {
{PD_3, USART1, CH_PIN_DATA(CH_MODE_INPUT, CH_CNF_INPUT_PUPD, PULLUP, AFIO_NONE)},
{NC, NP, 0, 0}
{NC, NP, 0}
};
#endif

Expand Down
Loading

0 comments on commit f187ac1

Please sign in to comment.