diff --git a/board/edu-unt-rv/inc/board.h b/board/edu-unt-rv/inc/board.h new file mode 100644 index 0000000..372c565 --- /dev/null +++ b/board/edu-unt-rv/inc/board.h @@ -0,0 +1,86 @@ +/************************************************************************************************ +Copyright (c) 2022-2024, Laboratorio de Microprocesadores +Facultad de Ciencias Exactas y Tecnología, Universidad Nacional de Tucumán +https://www.microprocesadores.unt.edu.ar/ + +Copyright (c) 2022-2024, Esteban Volentini + +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. + +SPDX-License-Identifier: MIT +*************************************************************************************************/ + +#ifndef BOARD_H +#define BOARD_H + +/** \brief Longan NANO board declarations + ** + ** \addtogroup board Board support + ** \brief Board agnostic configuration module + ** @{ */ + +/* === Headers files inclusions ================================================================ */ + +#if defined(USE_HAL) +#include "hal.h" +#endif +#if defined(USE_DRIVERS) +#include "gd32vf103.h" +#endif + +/* === Cabecera C++ ============================================================================ */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* === Public macros definitions =============================================================== */ + +#if defined(USE_HAL) + +#elif defined(USE_DRIVERS) + +// #define LED_R_GPIO GPIOA +// #define LED_R_PIN GPIO_PIN_0 + +// #define LED_G_GPIO GPIOA +// #define LED_G_PIN GPIO_PIN_1 + +// #define LED_B_GPIO GPIOA +// #define LED_B_PIN GPIO_PIN_2 + +#endif + +/* === Public data type declarations =========================================================== */ + +/* === Public variable declarations ============================================================ */ + +/* === Public function declarations ============================================================ */ + +/** + * @brief Function to initilize board configuration + */ +void BoardSetup(void); + +/* === End of documentation ==================================================================== */ + +#ifdef __cplusplus +} +#endif + +/** @} End of module definition for doxygen */ + +#endif /* BOARD_H */ diff --git a/board/edu-unt-rv/makefile b/board/edu-unt-rv/makefile new file mode 100644 index 0000000..0ac472d --- /dev/null +++ b/board/edu-unt-rv/makefile @@ -0,0 +1,28 @@ +################################################################################################## +# Copyright (c) 2022-2024, Laboratorio de Microprocesadores +# Facultad de Ciencias Exactas y Tecnología, Universidad Nacional de Tucumán +# https://www.microprocesadores.unt.edu.ar/ +# +# Copyright (c) 2022-2024, Esteban Volentini +# +# 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. +# +# SPDX-License-Identifier: MIT +################################################################################################## + +MCU ?= gd32vf103cbt6 + +USE_DRIVERS ?= y diff --git a/board/edu-unt-rv/src/board.c b/board/edu-unt-rv/src/board.c new file mode 100644 index 0000000..daf18e2 --- /dev/null +++ b/board/edu-unt-rv/src/board.c @@ -0,0 +1,63 @@ +/************************************************************************************************ +Copyright (c) 2022-2024, Laboratorio de Microprocesadores +Facultad de Ciencias Exactas y Tecnología, Universidad Nacional de Tucumán +https://www.microprocesadores.unt.edu.ar/ + +Copyright (c) 2022-2024, Esteban Volentini + +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. + +SPDX-License-Identifier: MIT +*************************************************************************************************/ + +/** \brief Longan NANO board configuration implementation + ** + ** \addtogroup board Board support + ** \brief Board agnostic configuration module + ** @{ */ + +/* === Headers files inclusions =============================================================== */ + +#include "board.h" +#include "gd32vf103.h" + +/* === Macros definitions ====================================================================== */ + +/* === Private data type declarations ========================================================== */ + +/* === Private variable declarations =========================================================== */ + +/* === Private function declarations =========================================================== */ + +/* === Public variable definitions ============================================================= */ + +/* === Private variable definitions ============================================================ */ + +/* === Private function implementation ========================================================= */ + +/* === Public function implementation ========================================================== */ + +void BoardSetup(void) { +#ifdef USE_DRIVERS + SystemInit(); + SystemCoreClockUpdate(); +#endif +} + +/* === End of documentation ==================================================================== + */ + +/** @} End of module definition for doxygen */ diff --git a/external/base/arch/rv32/core/src/write.c b/external/base/arch/rv32/core/src/write.c index 79dfe53..f2539b1 100644 --- a/external/base/arch/rv32/core/src/write.c +++ b/external/base/arch/rv32/core/src/write.c @@ -20,26 +20,22 @@ extern int _put_char(int ch) __attribute__((weak)); size_t __write(int handle, const unsigned char *buf, size_t bufSize) { size_t nChars = 0; - if (handle == -1) { return 0; } - for (; bufSize > 0; --bufSize) { _put_char((uint8_t) *buf); ++buf; ++nChars; } - return nChars; } int puts(const char* string) { return __write(0, (const void *) string, strlen(string)); } - #elif defined ( __GNUC__ ) ssize_t _write(int fd, const void* ptr, size_t len) { const uint8_t * current = (const uint8_t *) ptr; diff --git a/external/base/soc/gd32vf103/openocd/gd32vf103.cfg b/external/base/soc/gd32vf103/openocd/gd32vf103.cfg new file mode 100644 index 0000000..d41f7c9 --- /dev/null +++ b/external/base/soc/gd32vf103/openocd/gd32vf103.cfg @@ -0,0 +1,99 @@ +adapter speed 1000 +transport select jtag + +reset_config srst_nogate + +set _CHIPNAME gd32vf103 +# The vendor's configuration expects an ID of 0x1e200a6d, but this one is what +# I have on my board (Sipeed Longan Nano, GD32VF103CBT6). +jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x1000563d +jtag newtap $_CHIPNAME bs -irlen 5 -expected-id 0x790007a3 + +set _TARGETNAME $_CHIPNAME.cpu +target create $_TARGETNAME riscv -chain-position $_TARGETNAME +$_TARGETNAME riscv set_enable_virt2phys off + +proc default_mem_access {} { + riscv set_mem_access progbuf +} + +default_mem_access + +$_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size 0x1000 -work-area-backup 1 + +set _FLASHNAME $_CHIPNAME.flash +flash bank $_FLASHNAME stm32f1x 0x08000000 0 0 0 $_TARGETNAME + +# Address 0 is only aliased to main flash when the chip is not running its +# built-in bootloader. When it is, it's instead aliased to a read only section +# of flash at 0x1fffb000. However, we can't detect or dynamically switch this, +# so just pretend it's always aliased to main flash. We need to tell OpenOCD +# about this alias because otherwise we'll try to use software breakpoints on +# code in flash, which don't work because flash mappings are read-only. +flash bank $_CHIPNAME.flashalias virtual 0x0 0 0 0 $_TARGETNAME $_FLASHNAME + +# On this chip, ndmreset (the debug module bit that triggers a software reset) +# doesn't work. So for JTAG connections without an SRST, we need to trigger a +# reset manually. This is an undocumented reset sequence that's used by the +# JTAG flashing script in the vendor-supplied GD32VF103 PlatformIO plugin: +# +# https://github.com/sipeed/platform-gd32v/commit/f9cbb44819bc05dd2010cc815c32be0486800cc2 +# +$_TARGETNAME configure -event reset-assert { + set dmcontrol 0x10 + set dmcontrol_dmactive [expr {1 << 0}] + set dmcontrol_haltreq [expr {1 << 31}] + + global _RESETMODE + global _TARGETNAME + + # Halt the core so that we can write to memory. We do this first so + # that it doesn't clobber our dmcontrol configuration. + halt + + # Set haltreq appropriately for the type of reset we're doing. This + # replicates what the generic RISC-V reset_assert() function would + # do if we weren't overriding it. The $_RESETMODE hack sucks, but + # it's the least invasive way to determine whether we need to halt, + # and psoc6.cfg already uses the same trick. (reset_deassert(), which + # does run, also does this, but at that point it may be too late: the + # reset has already been triggered, so there's a race between it and + # the haltreq write.) + # + # If we didn't override the generic handler, we'd actually still have + # to do this: the default handler sets ndmreset, which prevents memory + # access even though it doesn't actually trigger a reset on this chip. + # So we'd need to unset it here, which involves a write to dmcontrol, + # Since haltreq is write-only and there's no way to leave it unchanged, + # we'd have to figure out its proper value anyway. + set val $dmcontrol_dmactive + if {$_RESETMODE ne "run"} { + set val [expr {$val | $dmcontrol_haltreq}] + } + $_TARGETNAME riscv dmi_write $dmcontrol $val + + # Unlock 0xe0042008 so that the next write triggers a reset + $_TARGETNAME mww 0xe004200c 0x4b5a6978 + + # We need to trigger the reset using abstract memory access, since + # progbuf access tries to read a status code out of a core register + # after the write happens, which fails when the core is in reset. + riscv set_mem_access abstract + + # Go! + $_TARGETNAME mww 0xe0042008 0x1 + + # Put the memory access mode back to what it was. + default_mem_access +} + +# Capture the mode of a given reset so that we can use it later in the +# reset-assert handler. +proc init_reset { mode } { + global _RESETMODE + set _RESETMODE $mode + + if {[using_jtag]} { + jtag arp_init-reset + } +} diff --git a/module/base/makefile b/module/base/makefile index a9050d1..9f6bd7d 100644 --- a/module/base/makefile +++ b/module/base/makefile @@ -269,6 +269,8 @@ DEFINES += $(call convert_defines, $(BOARD) $(SOC) $(MCU) $(CPU) $(ARCH) $(RTOS) TARGET_NAME ?= $(BIN_DIR)/$(PROJECT_NAME) TARGET_ELF = $(TARGET_NAME).$(LD_EXTENSION) +-include $(patsubst %.o,%.d,$(PROJECT_OBJ)) + ################################################################################################## # PROJECT_OBJ += $(call objects_list,$(PROJECT_SRC),c) diff --git a/module/base/mcu/gd32vf103cbt6/makefile b/module/base/mcu/gd32vf103cbt6/makefile index 8f95c10..5d93c34 100644 --- a/module/base/mcu/gd32vf103cbt6/makefile +++ b/module/base/mcu/gd32vf103cbt6/makefile @@ -31,6 +31,4 @@ DEFINES += HXTAL_VALUE=8000000U LINKSCRIPT = external/base/mcu/$(MCU)/linker/release.ld # Flash commandfor OpenOCD download target -FLASH_WRITER_COMMANDS = -c "init" -c "reset halt" -c "flash protect 0 0 last off" -FLASH_WRITER_COMMANDS += -c "program $(TARGET_ELF) verify;" -FLASH_WRITER_COMMANDS += -c "reset run; exit 0;" +FLASH_WRITER_COMMANDS = -c "program $(TARGET_ELF) verify reset exit" diff --git a/module/base/soc/gd32vf103/makefile b/module/base/soc/gd32vf103/makefile index 75dd788..5b9557a 100644 --- a/module/base/soc/gd32vf103/makefile +++ b/module/base/soc/gd32vf103/makefile @@ -59,8 +59,8 @@ DAP ?= ftdi/um232h # Command flags for gdb server GDB_SERVER_FLAGS += $(if $(USB_LOCATION), -c "adapter usb location $(USB_LOCATION)") -GDB_SERVER_FLAGS += -f interface/$(DAP).cfg -f target/gd32vf103.cfg +GDB_SERVER_FLAGS += -f interface/$(DAP).cfg -f $(MUJU)/$(SOC_FOLDER)/openocd/gd32vf103.cfg # Command flags for download target FLASH_WRITER_FLAGS += $(if $(USB_LOCATION), -c "adapter usb location $(USB_LOCATION)") -FLASH_WRITER_FLAGS += -f interface/$(DAP).cfg -f target/gd32vf103.cfg +FLASH_WRITER_FLAGS += -f interface/$(DAP).cfg -f $(MUJU)/$(SOC_FOLDER)/openocd/gd32vf103.cfg