├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── examples ├── LonganNano │ ├── FatFS.txt │ ├── Makefile │ ├── OFL.txt │ ├── diskio.c │ ├── diskio.h │ ├── display.c │ ├── display.h │ ├── ff.c │ ├── ff.h │ ├── ffconf.h │ ├── ffunicode.c │ ├── main.c │ ├── sdcard.c │ ├── sdcard.h │ ├── ter16b.c │ ├── ter16n.c │ ├── ter20b.c │ ├── ter20n.c │ ├── ter24b.c │ ├── ter24n.c │ ├── term.c │ └── term.h └── dfu-bootloader │ ├── Makefile │ ├── dfu.c │ ├── dfu.h │ ├── flash.c │ ├── flash.h │ ├── main.c │ ├── start.S │ ├── test.sh │ ├── usbfs.c │ └── usbfs.h ├── gd32vf103.ld ├── include ├── LonganNano.h ├── gd32vf103.h ├── gd32vf103 │ ├── adc.h │ ├── bkp.h │ ├── can.h │ ├── crc.h │ ├── csr.h │ ├── dac.h │ ├── dbg.h │ ├── dma.h │ ├── eclic.h │ ├── exmc.h │ ├── exti.h │ ├── fmc.h │ ├── fwdgt.h │ ├── gpio.h │ ├── i2c.h │ ├── info.h │ ├── mtimer.h │ ├── pmu.h │ ├── rcu.h │ ├── rtc.h │ ├── spi.h │ ├── timer.h │ ├── usart.h │ ├── usbfs.h │ └── wwdgt.h ├── lib │ ├── eclic.h │ ├── gpio.h │ ├── mtimer.h │ ├── rcu.h │ ├── stdio-uart0.h │ └── stdio-usbacm.h └── riscv │ ├── bits.h │ ├── const.h │ └── csr.h ├── lib ├── eclic.c ├── gpio.c ├── mtimer.c ├── rcu.c ├── std.c ├── stdio-uart0.c └── stdio-usbacm.c ├── main.c ├── start.S └── std ├── alloca.h ├── assert.h ├── stdio.h ├── string.h └── unistd.h /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2019, Emil Renner Berthing 4 | 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without modification, 8 | are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 3. Neither the name of the copyright holder nor the names of its contributors 16 | may be used to endorse or promote products derived from this software without 17 | specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 25 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 28 | OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2019-2020, Emil Renner Berthing 2 | # 3 | # Redistribution and use in source and binary forms, with or without modification, 4 | # are permitted provided that the following conditions are met: 5 | # 6 | # 1. Redistributions of source code must retain the above copyright notice, this 7 | # list of conditions and the following disclaimer. 8 | # 2. Redistributions in binary form must reproduce the above copyright notice, 9 | # this list of conditions and the following disclaimer in the documentation 10 | # and/or other materials provided with the distribution. 11 | # 3. Neither the name of the copyright holder nor the names of its contributors 12 | # may be used to endorse or promote products derived from this software without 13 | # specific prior written permission. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 | # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 19 | # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 | # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 24 | # OF SUCH DAMAGE. 25 | 26 | MAKEFLAGS += rR 27 | TARGET ?= code 28 | 29 | # override these settings for your board/chip 30 | HXTAL ?= 8000000 31 | FLASH_SIZE ?= 128*1024 32 | RAM_SIZE ?= 32*1024 33 | # override these settings to your liking 34 | CORECLOCK ?= 96000000 35 | BOOTLOADER ?= 4*1024 36 | 37 | O = build 38 | HERE := $(dir $(lastword $(MAKEFILE_LIST))) 39 | OS := $(if $(ComSpec),Windows,$(shell uname -s)) 40 | 41 | ARCH = rv32imac 42 | ABI = ilp32 43 | CODEMODEL = medlow 44 | 45 | OPT = -Os -flto 46 | ARCHFLAGS = -march=$(ARCH) -mabi=$(ABI) -mcmodel=$(CODEMODEL) -fno-pie 47 | DEPENDS = -MMD -MP 48 | WARNINGS = -Wall -Wextra -Wshadow -Wpointer-arith -Wformat=2 -Wformat-truncation=2 -Wundef -Wno-unused-parameter 49 | CFLAGS = $(OPT) $(ARCHFLAGS) -ggdb -pipe $(DEPENDS) $(WARNINGS) 50 | CFLAGS += -fno-common -ffunction-sections -fdata-sections 51 | ASFLAGS = $(OPT) $(ARCHFLAGS) -ggdb -pipe $(DEPENDS) $(WARNINGS) 52 | CPPFLAGS = $(if $(HXTAL),-DHXTAL=$(HXTAL) ,)-DCORECLOCK=$(CORECLOCK) 53 | CPPFLAGS += -DBOOTLOADER=$(BOOTLOADER) -DFLASH_SIZE=$(FLASH_SIZE) -DRAM_SIZE=$(RAM_SIZE) 54 | LDFLAGS = $(OPT) $(ARCHFLAGS) -static -Wl,-O1,--gc-sections,--relax,--build-id=none 55 | LDSCRIPT = $(HERE)gd32vf103.ld 56 | 57 | libs = eclic mtimer rcu gpio 58 | 59 | vpath %.S $(HERE) 60 | vpath %.c $(HERE) 61 | 62 | ifeq ($(SPECS),) 63 | CFLAGS += -ffreestanding -ftls-model=local-exec 64 | CPPFLAGS += -I$(HERE)std -D_LIBC_LIMITS_H_ 65 | LDFLAGS += -nostdlib 66 | LIBS += -lgcc 67 | else ifeq ($(SPECS),picolibc) 68 | else 69 | LDFLAGS += -nostartfiles 70 | endif 71 | 72 | CPPFLAGS += -I$(HERE)include 73 | 74 | CROSS_COMPILE = riscv64-unknown-elf- 75 | CC = $(CROSS_COMPILE)gcc $(if $(SPECS),-specs=$(SPECS).specs ,)-std=gnu11 76 | AS = $(CROSS_COMPILE)gcc $(if $(SPECS),-specs=$(SPECS).specs ,)-x assembler-with-cpp 77 | OBJDUMP = $(CROSS_COMPILE)objdump 78 | OBJCOPY = $(CROSS_COMPILE)objcopy 79 | HEX = $(OBJCOPY) -O ihex 80 | BIN = $(OBJCOPY) -O binary --strip-all 81 | 82 | ifdef ComSpec 83 | MKDIR_P = mkdir 84 | RM_RF = rmdir /q /s 85 | echo = @echo $1 86 | size = @cmd /c 'for %I in ($1) do @echo. %~zI bytes' 87 | else 88 | MKDIR_P = mkdir -p 89 | RM_RF = rm -rf 90 | echo = @echo '$1' 91 | ifeq ($(filter-out Linux MSYS%,$(OS)),) 92 | size = @stat --printf ' %s bytes\n' $1 93 | else 94 | size = @stat -f ' %z bytes' $1 95 | endif 96 | endif 97 | 98 | OPENOCD = openocd 99 | STM32FLASH = stm32flash 100 | STTY = stty 101 | CAT = cat 102 | SERIAL = /dev/ttyUSB0 103 | 104 | DFU_UTIL = dfu-util 105 | DFU_DEVICE = 1d50:613e 106 | 107 | asm-objs := $(patsubst %.S,%.o,$(filter-out start.S,$(wildcard *.S))) 108 | c-objs := $(patsubst %.c,%.o,$(filter-out start.c,$(wildcard *.c))) 109 | 110 | objs = start.o 111 | objs += $(patsubst %,lib-%.o,$(sort $(libs))) 112 | objs += $(sort $(asm-objs) $(c-objs)) 113 | 114 | objects = $(addprefix $O/,$(objs))$(if $(SPECS),, $O/lib-std.o) 115 | 116 | # use make V=1 to see raw commands or make -s for silence 117 | ifeq ($V$(findstring s,$(word 1,$(MAKEFLAGS))),) 118 | Q := @ 119 | else 120 | echo = 121 | size = 122 | endif 123 | 124 | .SECONDEXPANSION: 125 | .PHONY: all release clean dump dfu romdfu uart cat 126 | 127 | all: $O/$$(TARGET).bin 128 | 129 | release: CPPFLAGS += -DNDEBUG 130 | release: $O/$$(TARGET).bin 131 | 132 | $O/lib-std.o: CFLAGS += -fno-builtin 133 | 134 | $O/lib-%.o: $(HERE)lib/%.c $(MAKEFILE_LIST) | $O 135 | $(call echo, CC $<) 136 | $Q$(CC) -o $@ $(CFLAGS) $(CPPFLAGS) -c $< 137 | 138 | $O/%.o: %.S $(MAKEFILE_LIST) | $O 139 | $(call echo, AS $<) 140 | $Q$(AS) -o $@ $(ASFLAGS) $(CPPFLAGS) -c $< 141 | 142 | $O/%.o: %.c $(MAKEFILE_LIST) | $O 143 | $(call echo, CC $<) 144 | $Q$(CC) -o $@ $(CFLAGS) $(CPPFLAGS) -c $< 145 | 146 | $O/$(TARGET).elf: $$(objects) $$(LDSCRIPT) 147 | $(call echo, CCLD $@) 148 | $Q$(CC) -o $@ $(LDFLAGS) -T$(LDSCRIPT) $(objects) $(LIBS) 149 | 150 | $O/%.hex: $O/%.elf 151 | $(call echo, HEX $@) 152 | $Q$(HEX) $< $@ 153 | 154 | $O/%.bin: $O/%.elf 155 | $(call echo, BIN $@) 156 | $Q$(BIN) $< $@ 157 | $(call size,$@) 158 | 159 | $O: 160 | $(call echo, MKDIR $@) 161 | $Q$(MKDIR_P) $@ 162 | 163 | clean: 164 | $(call echo, RM $O) 165 | $Q$(RM_RF) $O 166 | 167 | dump: $O/$$(TARGET).elf 168 | $(OBJDUMP) -x -d $< | $(PAGER) 169 | 170 | dfu: $O/$$(TARGET).bin 171 | $Q$(DFU_UTIL) -d $(DFU_DEVICE) -D $< -R 172 | 173 | romdfu: $O/$$(TARGET).bin 174 | $Q$(DFU_UTIL) -d 28e9:0189 -a 0 --dfuse-address 0x08000000:leave -D $< 175 | 176 | cat: | $$(SERIAL) 177 | $(STTY) -F$(SERIAL) raw -echo -hup cs8 -parenb -cstopb 115200 178 | $(CAT) $(SERIAL) 179 | 180 | -include $O/*.d 181 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gd32vf103inator 2 | 3 | ## What is this? 4 | 5 | This is a collection of headers, some code, linkerscript and Makefile to make it 6 | easy to program the [GD32VF103][gigadevice] RISC-V microcontroller from GigaDevice. 7 | It is already supported in [PlatformIO][platformio] and ArduinoIDE is underway, 8 | but sometimes it's nice to just use your favourite editor and make. 9 | This is exactly what this project will help you do. 10 | 11 | The [user manual][usermanual] and [datasheet][datasheet] for the chip as available 12 | from [here][datasheets]. 13 | 14 | [gigadevice]: https://www.gigadevice.com/products/microcontrollers/gd32/risc-v/ 15 | [platformio]: https://github.com/sipeed/platform-gd32v/ 16 | [usermanual]: https://github.com/riscv-mcu/GD32VF103_DataSheets/raw/master/GD32VF103_User_Manual_EN_V1.0.pdf 17 | [datasheet]: https://github.com/riscv-mcu/GD32VF103_DataSheets/raw/master/GD32VF103_Datasheet_Rev_1.0.pdf 18 | [datasheets]: https://github.com/riscv-mcu/GD32VF103_DataSheets 19 | 20 | 21 | ## Using gd32vf103inator 22 | 23 | #### The quick and dirty way 24 | 25 | ```sh 26 | git clone https://github.com/esmil/gd32vf103inator.git 27 | mkdir myproject 28 | cd myproject 29 | echo include ../gd32vf103inator/Makefile > Makefile 30 | cp ../gd32vf103inator/main.c . 31 | make 32 | ``` 33 | 34 | #### The sophisticated way 35 | ```sh 36 | mkdir myproject 37 | cd myproject 38 | git init 39 | git submodule add https://github.com/esmil/gd32vf103inator.git 40 | echo include gd32vf103inator/Makefile > Makefile 41 | echo build/ > .gitignore 42 | cp gd32vf103inator/main.c . 43 | git add . 44 | git commit -m 'initial commit' 45 | ``` 46 | 47 | 48 | #### Local settings 49 | 50 | As seen above the Makefile is meant to be included by the Makefile in 51 | local project. It is made such that most settings should be overwritable. 52 | As an example your Makefile could look like this: 53 | ```makefile 54 | include path/to/gd32vf103inator/Makefile 55 | 56 | # disable link-time optimization, but optimize for speed 57 | OPT = -O2 58 | 59 | # too many annoying warnings, I know how to write code 60 | #WARNINGS = -Wall 61 | # be extra pedantic, and make sure we fix all warnings 62 | WARNINGS += -Werror -pedantic 63 | 64 | # gd32vf103inator defaults to GD32VF103xB chips 65 | # with 32k SRAM and 128k FLASH, but we use an x8 chip 66 | RAM_SIZE = 20*1024 67 | FLASH_SIZE = 64*1024 68 | ``` 69 | 70 | 71 | ## Bootloader 72 | 73 | The chip has a built-in DFU bootloader in ROM but unfortunately it has some 74 | ~~bugs~~ quirks: it only works with a [patched version][gd32-dfu-utils] 75 | of dfu-utils and some [workarounds][bootloader-workaround]. 76 | 77 | That's a bit annoying so this project includes another DFU bootlader 78 | that can be programmed into the first 4k flash. This bootloader does work with 79 | the regular dfu-util and is much faster. The bootloader will activate when 80 | the chip is reset by the external reset pin (aka. the reset button is pressed). 81 | On any other reset (power on, watchdogs etc.) the bootloader will immediately 82 | jump 4k into the flash and run the regular program. 83 | 84 | You can use the built-in bootloader in ROM to flash this bootloader. 85 | First make sure to reboot the chip into the ROM bootloader 86 | (on the LonganNano board hold BOOT and press RESET) and then run: 87 | ```sh 88 | cd gd32vf103inator/examples/dfu-bootloader 89 | make clean 90 | make release 91 | make DFU_UTIL=/path/to/gd32-dfu-utils/src/dfu-util romdfu 92 | ``` 93 | This will show errors even when the chip is succesfully flashed. 94 | Apparently that's just how the built-in bootloader works :/ 95 | 96 | Once the bootloader is flashed the chip can be programmed by just 97 | pressing the reset button and run: 98 | ```sh 99 | make dfu 100 | ``` 101 | This will use the dfu-util in your path, flash the chip and reset it to run 102 | your program. 103 | 104 | For this to work regular programs must be compiled to run from an offset 105 | of 4k into the flash. That happens automatically, but if you're happy with 106 | the built-in bootloader or you have some other means of flashing the chip 107 | you can disable it by adding this line to your Makefile: 108 | ```makefile 109 | BOOTLOADER = 0 110 | ``` 111 | 112 | [gd32-dfu-utils]: https://github.com/riscv-mcu/gd32-dfu-utils 113 | [bootloader-workaround]: https://github.com/esmil/gd32vf103inator/blob/master/start.S#L245 114 | 115 | 116 | ## Getting a RISC-V toolchain 117 | 118 | Ideally you want a toolchain for embedded use. For RISC-V it will typically be called 119 | something containing `riscv64-unknown-elf`. 120 | This is similar to `arm-none-eabi` toolchains in ARM land. 121 | Don't worry that it's called "riscv64" even though the core in the chip is only 32bit. 122 | The toolchain will still be able to produce 32bit code, floating point/non-floating point 123 | code and many other valid RISC-V combinations. 124 | 125 | Unfortunately RISC-V toolchains for embedded use are not yet available in most distributions, 126 | but cross compilers for Linux seem to be. They are usually called something containing 127 | `riscv64-linux-gnu`, and gd32vf103inator has been made to work with such toolchains too. 128 | This is not ideal but with enough options to gcc it can be persuaded to not use any libc bits. 129 | 130 | If your chosen toolchain is not prefixed with `riscv64-unknown-elf-` you can overwrite the 131 | `CROSS_COMPILE` variable in your local Makefile. Eg. append this to your Makefile: 132 | ```makefile 133 | CROSS_COMPILE = riscv64-linux-gnu- 134 | ``` 135 | 136 | Below are some suggestions for specific operating systems. Please make a pull request 137 | to add your favourite OS. 138 | 139 | 140 | ##### Archlinux 141 | 142 | There are packages [riscv64-unknown-elf-binutils][aur-binutils] and 143 | [riscv64-unknown-elf-gcc][aur-gcc] available in AUR. 144 | 145 | Alternatively there are prebuilt riscv64-linux-gnu packages in community repo. Eq. 146 | ```sh 147 | pacman -S riscv64-linux-gnu-gcc make 148 | ``` 149 | 150 | [aur-binutils]: https://aur.archlinux.org/packages/riscv64-unknown-elf-binutils/ 151 | [aur-gcc]: https://aur.archlinux.org/packages/riscv64-unknown-elf-gcc/ 152 | 153 | 154 | ##### Debian/Ubuntu 155 | 156 | If you're running Debian unstable aka. sid or Ubuntu 20.04 (Focal Fossa) you should use the riscv64-unknown-elf toolchain: 157 | 158 | ```sh 159 | apt-get install gcc-riscv64-unknown-elf make 160 | ``` 161 | 162 | On older Ubuntus you might want to try the 163 | [embedded toolchain from SiFive](#prebuilt-toolchains-from-sifive) below. 164 | 165 | Otherwise it should also work with the cross compiler for Linux. Eg.: 166 | 167 | ```sh 168 | apt-get install gcc-riscv64-linux-gnu make 169 | ``` 170 | 171 | 172 | ##### Fedora 173 | 174 | Unfortunately I don't know of any prebuilt riscv64-unknown-elf toolchains for 175 | Fedora, but again the cross-compiler for Linux should work: 176 | 177 | ```sh 178 | dnf install gcc-riscv64-linux-gnu make 179 | ``` 180 | 181 | 182 | ##### OSX 183 | 184 | See [SiFive's toolchain](#prebuilt-toolchains-from-sifive) below. 185 | 186 | 187 | ##### Windows 188 | ###### Option 1 189 | 190 | See [SiFive's toolchain](#prebuilt-toolchains-from-sifive) below for the toolchain. 191 | You'll also need GNU Make to build your code. 192 | 193 | ###### Option 2 194 | 195 | Use Windows Subsystem for Linux and proceed as on Ubuntu above. 196 | 197 | 198 | ##### Prebuilt toolchains from SiFive 199 | 200 | Go to [SiFive's software page][sifive-boards] and search for "GNU Embedded Toolchain". 201 | Download the relevant tarball/zip-file and unpack it. Now you can either update your path 202 | to include the `bin` folder inside, or add the following line to your Makefile: 203 | ```makefile 204 | CROSS_COMPILE = /full/path/to/the/extracted/folder/bin/riscv64-unknown-elf- 205 | ``` 206 | 207 | [sifive-boards]: https://www.sifive.com/boards 208 | 209 | 210 | ## License 211 | 212 | This project is licensed under the [BSD 3-Clause][bsd-3] license just like 213 | the [firmware library][firmware] released by GigaDevice. 214 | Register and bit definitions are copied from there and modified to be usable 215 | without including the rest of the library. 216 | 217 | [bsd-3]: https://opensource.org/licenses/BSD-3-Clause 218 | [firmware]: https://github.com/riscv-mcu/GD32VF103_Firmware_Library/ 219 | -------------------------------------------------------------------------------- /examples/LonganNano/FatFS.txt: -------------------------------------------------------------------------------- 1 | FatFs License 2 | 3 | FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that heading the source files. 4 | 5 | /*----------------------------------------------------------------------------/ 6 | / FatFs - Generic FAT Filesystem Module Rx.xx / 7 | /-----------------------------------------------------------------------------/ 8 | / 9 | / Copyright (C) 20xx, ChaN, all right reserved. 10 | / 11 | / FatFs module is an open source software. Redistribution and use of FatFs in 12 | / source and binary forms, with or without modification, are permitted provided 13 | / that the following condition is met: 14 | / 15 | / 1. Redistributions of source code must retain the above copyright notice, 16 | / this condition and the following disclaimer. 17 | / 18 | / This software is provided by the copyright holder and contributors "AS IS" 19 | / and any warranties related to this software are DISCLAIMED. 20 | / The copyright owner or contributors be NOT LIABLE for any damages caused 21 | / by use of this software. 22 | /----------------------------------------------------------------------------*/ 23 | 24 | Therefore FatFs license is one of the BSD-style licenses but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, does not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses including GNU GPL. When you redistribute the FatFs source code with any changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license. 25 | -------------------------------------------------------------------------------- /examples/LonganNano/Makefile: -------------------------------------------------------------------------------- 1 | include ../../Makefile 2 | 3 | libs += stdio-usbacm stdio-uart0 4 | 5 | # some LonganNano boards comes with the biggest 6 | # GD32VF103CB chip with 32k SRAM and 128k FLASH, 7 | # but some also ship with the smaller C8-chip, 8 | # which only has 20k SRAM and 64k FLASH 9 | # let's error on the safe side by default, 10 | # but remove this if you have the CB version 11 | RAM_SIZE=20*1024 12 | FLASH_SIZE=64*1024 13 | -------------------------------------------------------------------------------- /examples/LonganNano/OFL.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 Dimitar Toshkov Zhekov, 2 | with Reserved Font Name "Terminus Font". 3 | 4 | This Font Software is licensed under the SIL Open Font License, Version 1.1. 5 | This license is copied below, and is also available with a FAQ at: 6 | http://scripts.sil.org/OFL 7 | 8 | 9 | ----------------------------------------------------------- 10 | SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 11 | ----------------------------------------------------------- 12 | 13 | PREAMBLE 14 | The goals of the Open Font License (OFL) are to stimulate worldwide 15 | development of collaborative font projects, to support the font creation 16 | efforts of academic and linguistic communities, and to provide a free and 17 | open framework in which fonts may be shared and improved in partnership 18 | with others. 19 | 20 | The OFL allows the licensed fonts to be used, studied, modified and 21 | redistributed freely as long as they are not sold by themselves. The 22 | fonts, including any derivative works, can be bundled, embedded, 23 | redistributed and/or sold with any software provided that any reserved 24 | names are not used by derivative works. The fonts and derivatives, 25 | however, cannot be released under any other type of license. The 26 | requirement for fonts to remain under this license does not apply 27 | to any document created using the fonts or their derivatives. 28 | 29 | DEFINITIONS 30 | "Font Software" refers to the set of files released by the Copyright 31 | Holder(s) under this license and clearly marked as such. This may 32 | include source files, build scripts and documentation. 33 | 34 | "Reserved Font Name" refers to any names specified as such after the 35 | copyright statement(s). 36 | 37 | "Original Version" refers to the collection of Font Software components as 38 | distributed by the Copyright Holder(s). 39 | 40 | "Modified Version" refers to any derivative made by adding to, deleting, 41 | or substituting -- in part or in whole -- any of the components of the 42 | Original Version, by changing formats or by porting the Font Software to a 43 | new environment. 44 | 45 | "Author" refers to any designer, engineer, programmer, technical 46 | writer or other person who contributed to the Font Software. 47 | 48 | PERMISSION & CONDITIONS 49 | Permission is hereby granted, free of charge, to any person obtaining 50 | a copy of the Font Software, to use, study, copy, merge, embed, modify, 51 | redistribute, and sell modified and unmodified copies of the Font 52 | Software, subject to the following conditions: 53 | 54 | 1) Neither the Font Software nor any of its individual components, 55 | in Original or Modified Versions, may be sold by itself. 56 | 57 | 2) Original or Modified Versions of the Font Software may be bundled, 58 | redistributed and/or sold with any software, provided that each copy 59 | contains the above copyright notice and this license. These can be 60 | included either as stand-alone text files, human-readable headers or 61 | in the appropriate machine-readable metadata fields within text or 62 | binary files as long as those fields can be easily viewed by the user. 63 | 64 | 3) No Modified Version of the Font Software may use the Reserved Font 65 | Name(s) unless explicit written permission is granted by the corresponding 66 | Copyright Holder. This restriction only applies to the primary font name as 67 | presented to the users. 68 | 69 | 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font 70 | Software shall not be used to promote, endorse or advertise any 71 | Modified Version, except to acknowledge the contribution(s) of the 72 | Copyright Holder(s) and the Author(s) or with their explicit written 73 | permission. 74 | 75 | 5) The Font Software, modified or unmodified, in part or in whole, 76 | must be distributed entirely under this license, and must not be 77 | distributed under any other license. The requirement for fonts to 78 | remain under this license does not apply to any document created 79 | using the Font Software. 80 | 81 | TERMINATION 82 | This license becomes null and void if any of the above conditions are 83 | not met. 84 | 85 | DISCLAIMER 86 | THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 87 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF 88 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 89 | OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE 90 | COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 91 | INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL 92 | DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 93 | FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM 94 | OTHER DEALINGS IN THE FONT SOFTWARE. 95 | -------------------------------------------------------------------------------- /examples/LonganNano/diskio.c: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------*/ 2 | /* Low level disk I/O module for FatFs (C)ChaN, 2019 */ 3 | /* Copyright 2020 Emil Renner Berthing */ 4 | /*-----------------------------------------------------------------------*/ 5 | /* If a working storage control module is available, it should be */ 6 | /* attached to the FatFs via a glue function rather than modifying it. */ 7 | /* This is an example of glue functions to attach various exsisting */ 8 | /* storage control modules to the FatFs module with a defined API. */ 9 | /*-----------------------------------------------------------------------*/ 10 | 11 | #include "ff.h" /* Obtains integer types */ 12 | #include "diskio.h" /* Declarations of disk functions */ 13 | 14 | #include "sdcard.h" 15 | 16 | #if 0 17 | #include 18 | #define debug(...) printf(__VA_ARGS__) 19 | #else 20 | #define debug(...) 21 | #endif 22 | 23 | static DSTATUS status = STA_NOINIT; 24 | 25 | /*-----------------------------------------------------------------------*/ 26 | /* Get Drive Status */ 27 | /*-----------------------------------------------------------------------*/ 28 | DSTATUS disk_status(BYTE pdrv) 29 | { 30 | uint8_t stat; 31 | uint8_t ret; 32 | 33 | debug("disk_status(%u):\n", pdrv); 34 | 35 | if (pdrv != 0) 36 | return STA_NOINIT | STA_NODISK; 37 | 38 | if (status & STA_NOINIT) 39 | return status; 40 | 41 | ret = sd_status(&stat); 42 | debug(" sd_status() = %02x (%02x)\n", ret, stat); 43 | if (ret == 0xFF) 44 | status = STA_NOINIT | STA_NODISK; 45 | else if (ret != 0x00 || stat != 0x00) 46 | status = STA_NOINIT; 47 | 48 | return status; 49 | } 50 | 51 | /*-----------------------------------------------------------------------*/ 52 | /* Initialize a Drive */ 53 | /*-----------------------------------------------------------------------*/ 54 | DSTATUS disk_initialize(BYTE pdrv) 55 | { 56 | uint8_t ret; 57 | 58 | debug("disk_initialize(%u):\n", pdrv); 59 | 60 | if (pdrv != 0) 61 | return STA_NOINIT | STA_NODISK; 62 | 63 | ret = sd_wakeup(); 64 | debug(" sd_wakeup() = %x\n", ret); 65 | if (ret == 0x00) 66 | status = 0; 67 | 68 | return status; 69 | } 70 | 71 | /*-----------------------------------------------------------------------*/ 72 | /* Read Sector(s) */ 73 | /*-----------------------------------------------------------------------*/ 74 | DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) 75 | { 76 | uint8_t ret; 77 | 78 | debug("disk_read(%u, buff, %lu, %u):\n", 79 | pdrv, sector, count); 80 | 81 | if (pdrv != 0) 82 | return RES_PARERR; 83 | if (status & STA_NOINIT) 84 | return RES_NOTRDY; 85 | 86 | for (; count > 0; count--) { 87 | ret = sd_readblock(sector, buff); 88 | debug(" sd_readblock(%lu, buff) = %u\n", 89 | sector, ret); 90 | if (ret != 0x00) { 91 | break; 92 | } 93 | sector += 1; 94 | buff += 512; 95 | } 96 | 97 | return (ret == 0x00) ? RES_OK : RES_ERROR; 98 | } 99 | 100 | /*-----------------------------------------------------------------------*/ 101 | /* Write Sector(s) */ 102 | /*-----------------------------------------------------------------------*/ 103 | #if FF_FS_READONLY == 0 104 | DRESULT disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count) 105 | { 106 | uint8_t ret; 107 | 108 | debug("disk_write(%u, buff, %lu, %u):\n", 109 | pdrv, sector, count); 110 | 111 | if (pdrv != 0) 112 | return RES_PARERR; 113 | if (status & STA_NOINIT) 114 | return RES_NOTRDY; 115 | 116 | for (; count > 0; count--) { 117 | ret = sd_writeblock(sector, buff); 118 | debug(" sd_writeblock(%lu, buff) = %u\n", 119 | sector, ret); 120 | if (ret != 0x00) { 121 | break; 122 | } 123 | sector += 1; 124 | buff += 512; 125 | } 126 | 127 | return (ret == 0x00) ? RES_OK : RES_ERROR; 128 | } 129 | #endif 130 | 131 | /*-----------------------------------------------------------------------*/ 132 | /* Miscellaneous Functions */ 133 | /*-----------------------------------------------------------------------*/ 134 | #if FF_FS_READONLY == 0 || FF_MIN_SS != FF_MAX_SS 135 | static DRESULT translate(uint8_t ret) 136 | { 137 | DRESULT res; 138 | 139 | switch (ret) { 140 | case 0x00: res = RES_OK; break; 141 | case 0xFF: res = RES_NOTRDY; break; 142 | default: res = RES_ERROR; break; 143 | } 144 | return res; 145 | } 146 | 147 | DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) 148 | { 149 | DRESULT res = RES_PARERR; 150 | 151 | if (pdrv != 0) 152 | return res; 153 | 154 | switch (cmd) { 155 | /* Generic command (Used by FatFs) */ 156 | #if FF_FS_READONLY == 0 157 | case CTRL_SYNC: /* Complete pending write process */ 158 | res = RES_OK; 159 | break; 160 | #endif 161 | #if FF_USE_MKFS == 1 162 | case GET_SECTOR_COUNT: { /* Get media size */ 163 | LBA_t *count = buff; 164 | res = translate(sd_getblocks(count)); 165 | break; 166 | } 167 | #endif 168 | #if FF_MIN_SS != FF_MAX_SS 169 | case GET_SECTOR_SIZE: { /* Get sector size */ 170 | WORD *size = buff; 171 | *size = 512; 172 | res = RES_OK; 173 | break; 174 | } 175 | #endif 176 | #if FF_USE_MKFS == 1 177 | case GET_BLOCK_SIZE: { /* Get erase block size */ 178 | DWORD *size = buff; 179 | res = translate(sd_geterasesectorsize(size)); 180 | break; 181 | } 182 | #endif 183 | #if FF_USE_TRIM == 1 184 | case CTRL_TRIM: /* Inform device that the data on the block of sectors is no longer used */ 185 | res = RES_OK; 186 | break; 187 | #endif 188 | #if 0 189 | case MMC_GET_CSD: 190 | res = translate(sd_getcsd(buff)); 191 | break; 192 | case MMC_GET_CID: 193 | res = translate(sd_getcid(buff)); 194 | break; 195 | case MMC_GET_OCR: 196 | res = translate(sd_getocr(buff)); 197 | break; 198 | #endif 199 | } 200 | 201 | return res; 202 | } 203 | #endif 204 | -------------------------------------------------------------------------------- /examples/LonganNano/diskio.h: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------/ 2 | / Low level disk interface modlue include file (C)ChaN, 2019 / 3 | /-----------------------------------------------------------------------*/ 4 | 5 | #ifndef _DISKIO_DEFINED 6 | #define _DISKIO_DEFINED 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | /* Status of Disk Functions */ 13 | typedef BYTE DSTATUS; 14 | 15 | /* Results of Disk Functions */ 16 | typedef enum { 17 | RES_OK = 0, /* 0: Successful */ 18 | RES_ERROR, /* 1: R/W Error */ 19 | RES_WRPRT, /* 2: Write Protected */ 20 | RES_NOTRDY, /* 3: Not Ready */ 21 | RES_PARERR /* 4: Invalid Parameter */ 22 | } DRESULT; 23 | 24 | 25 | /*---------------------------------------*/ 26 | /* Prototypes for disk control functions */ 27 | 28 | 29 | DSTATUS disk_initialize (BYTE pdrv); 30 | DSTATUS disk_status (BYTE pdrv); 31 | DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count); 32 | DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count); 33 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); 34 | 35 | 36 | /* Disk Status Bits (DSTATUS) */ 37 | 38 | #define STA_NOINIT 0x01 /* Drive not initialized */ 39 | #define STA_NODISK 0x02 /* No medium in the drive */ 40 | #define STA_PROTECT 0x04 /* Write protected */ 41 | 42 | 43 | /* Command code for disk_ioctrl fucntion */ 44 | 45 | /* Generic command (Used by FatFs) */ 46 | #define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */ 47 | #define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */ 48 | #define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */ 49 | #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */ 50 | #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */ 51 | 52 | /* Generic command (Not used by FatFs) */ 53 | #define CTRL_POWER 5 /* Get/Set power status */ 54 | #define CTRL_LOCK 6 /* Lock/Unlock media removal */ 55 | #define CTRL_EJECT 7 /* Eject media */ 56 | #define CTRL_FORMAT 8 /* Create physical format on the media */ 57 | 58 | /* MMC/SDC specific ioctl command */ 59 | #define MMC_GET_TYPE 10 /* Get card type */ 60 | #define MMC_GET_CSD 11 /* Get CSD */ 61 | #define MMC_GET_CID 12 /* Get CID */ 62 | #define MMC_GET_OCR 13 /* Get OCR */ 63 | #define MMC_GET_SDSTAT 14 /* Get SD status */ 64 | #define ISDIO_READ 55 /* Read data form SD iSDIO register */ 65 | #define ISDIO_WRITE 56 /* Write data to SD iSDIO register */ 66 | #define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */ 67 | 68 | /* ATA/CF specific ioctl command */ 69 | #define ATA_GET_REV 20 /* Get F/W revision */ 70 | #define ATA_GET_MODEL 21 /* Get model name */ 71 | #define ATA_GET_SN 22 /* Get serial number */ 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /examples/LonganNano/display.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2020, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef DISPLAY_H 28 | #define DISPLAY_H 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | /* 35 | * Connection and display configuration 36 | */ 37 | 38 | /* there is no pin for the backlight on the Longan Nano */ 39 | //#define DP_BLK GPIO_PA1 /* backlight */ 40 | #define DP_DC GPIO_PB0 /* D/CX */ 41 | #define DP_RST GPIO_PB1 /* reset */ 42 | #define DP_CS GPIO_PB2 /* chip-select */ 43 | #define DP_SDA GPIO_PA7 /* data */ 44 | #define DP_SCL GPIO_PA5 /* clock */ 45 | 46 | /* spi max write clock rate 1/(66ns) = 15 MHz */ 47 | #define DP_CLOCKDIV_WRITE SPI_CTL0_PSC_DIV8 /* 96MHz / 8 = 12MHz */ 48 | /* spi max read clock rate 1/(150ns) = 6.6 MHz */ 49 | #define DP_CLOCKDIV_READ SPI_CTL0_PSC_DIV32 /* 96MHz / 32 = 3MHz */ 50 | 51 | #define DP_MADCTL 0x68 52 | #define DP_OFFSET_X 1 53 | #define DP_OFFSET_Y 26 54 | 55 | #define DP_WIDTH 160 56 | #define DP_HEIGHT 80 57 | 58 | /* 59 | * Display API 60 | */ 61 | 62 | typedef uint8_t dp_font_data_t; 63 | struct dp_font { 64 | uint8_t width; 65 | uint8_t height; 66 | dp_font_data_t data[]; 67 | }; 68 | 69 | #ifdef DP_BLK 70 | static inline void dp_backlight_on(void) 71 | { 72 | gpio_pin_set(DP_BLK); 73 | } 74 | 75 | static inline void dp_backlight_off(void) 76 | { 77 | gpio_pin_clear(DP_BLK); 78 | } 79 | 80 | static inline void dp_backlight_toggle(void) 81 | { 82 | gpio_pin_toggle(DP_BLK); 83 | } 84 | #else 85 | static inline void dp_backlight_on(void) {} 86 | static inline void dp_backlight_off(void) {} 87 | static inline void dp_backlight_toggle(void) {} 88 | #endif 89 | 90 | void dp_reset(void); 91 | uint8_t dp_read1(uint8_t cmd); 92 | void dp_madctl(uint8_t v); 93 | void dp_cmd(uint8_t cmd); 94 | static inline void dp_sleep_in(void) { dp_cmd(0x10); } 95 | static inline void dp_sleep_out(void) { dp_cmd(0x11); } 96 | static inline void dp_off(void) { dp_cmd(0x28); } 97 | static inline void dp_on(void) { dp_cmd(0x29); } 98 | 99 | void dp_init(void); 100 | void dp_uninit(void); 101 | 102 | void dp_fill(unsigned int x, unsigned int y, unsigned int w, unsigned int h, 103 | unsigned int rgb444); 104 | void dp_fill666(unsigned int x, unsigned int y, unsigned int w, unsigned int h, 105 | unsigned int rgb888); 106 | 107 | void dp_line(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1, 108 | unsigned int rgb444); 109 | 110 | void dp_putchar(const struct dp_font *font, 111 | unsigned int x, unsigned int y, 112 | unsigned int fg444, unsigned int bg444, int ch); 113 | void dp_puts(const struct dp_font *font, 114 | unsigned int x, unsigned int y, 115 | unsigned int fg444, unsigned int bg444, const char *str); 116 | #endif 117 | -------------------------------------------------------------------------------- /examples/LonganNano/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2020, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include 28 | 29 | #include "gd32vf103/csr.h" 30 | 31 | #include "lib/mtimer.h" 32 | #include "lib/eclic.h" 33 | #include "lib/rcu.h" 34 | #include "lib/gpio.h" 35 | #include "lib/stdio-usbacm.h" 36 | #include "lib/stdio-uart0.h" 37 | 38 | #include "LonganNano.h" 39 | #include "display.h" 40 | #include "sdcard.h" 41 | #include "term.h" 42 | 43 | #include "ff.h" 44 | 45 | extern struct dp_font ter16n; 46 | extern struct dp_font ter16b; 47 | extern struct dp_font ter20n; 48 | extern struct dp_font ter20b; 49 | extern struct dp_font ter24n; 50 | extern struct dp_font ter24b; 51 | 52 | #define BLINK (CORECLOCK/4) /* 1 second */ 53 | 54 | void MTIMER_IRQHandler(void) 55 | { 56 | uint64_t next; 57 | 58 | gpio_pin_toggle(LED_BLUE); 59 | 60 | next = mtimer_mtimecmp() + BLINK; 61 | MTIMER->mtimecmp_hi = next >> 32; 62 | MTIMER->mtimecmp_lo = next; 63 | } 64 | 65 | /* if the compiler can't generate functions suitable 66 | * for interrupt handlers, we can't implement this 67 | * function directly in C 68 | */ 69 | #ifdef __interrupt 70 | void trap_entry(void) 71 | { 72 | unsigned long mcause = csr_read(CSR_MCAUSE); 73 | 74 | if ((mcause & CSR_MCAUSE_EXCCODE_Msk) == 0xfffU) 75 | fprintf(uart0, "nmi!\n"); 76 | 77 | fprintf(uart0, "trap: mcause = 0x%08lx\n", mcause); 78 | fprintf(uart0, "trap: mepc = 0x%08lx\n", csr_read(CSR_MEPC)); 79 | fprintf(uart0, "trap: mtval = 0x%08lx\n", csr_read(CSR_MTVAL)); 80 | 81 | while (1) 82 | /* forever */; 83 | } 84 | #endif 85 | 86 | static void mtimer_enable(void) 87 | { 88 | uint64_t next = mtimer_mtime() + BLINK; 89 | 90 | MTIMER->mtimecmp_hi = next >> 32; 91 | MTIMER->mtimecmp_lo = next; 92 | 93 | eclic_config(MTIMER_IRQn, ECLIC_ATTR_TRIG_LEVEL, 1); 94 | eclic_enable(MTIMER_IRQn); 95 | } 96 | 97 | #if FF_MULTI_PARTITION 98 | PARTITION VolToPart[FF_VOLUMES] = { 99 | { 0, 0 }, /* drive 0, autodetect */ 100 | }; 101 | #endif 102 | 103 | uint32_t get_fattime(void) 104 | { 105 | return (2020U - 1980U) << 25 | /* year */ 106 | 1U << 21 | /* month */ 107 | 1U << 16 | /* day */ 108 | 12U << 11 | /* hour */ 109 | 0U << 5 | /* minute */ 110 | 0/2 << 0; /* seconds/2 */ 111 | } 112 | 113 | static FRESULT 114 | listdir(struct term *term, const char *path) 115 | { 116 | FILINFO fi; 117 | DIR dir; 118 | FRESULT res; 119 | 120 | res = f_opendir(&dir, path); 121 | if (res != FR_OK) 122 | return res; 123 | 124 | while (1) { 125 | char *p; 126 | char c; 127 | 128 | res = f_readdir(&dir, &fi); 129 | if (res != FR_OK) 130 | break; 131 | if (fi.fname[0] == '\0') 132 | break; 133 | 134 | p = fi.fname; 135 | for (c = *p++; c != '\0'; c = *p++) 136 | term_putchar(term, c); 137 | term_putchar(term, '\n'); 138 | }; 139 | 140 | f_closedir(&dir); 141 | return res; 142 | } 143 | 144 | int main(void) 145 | { 146 | struct term term; 147 | FATFS fs; 148 | 149 | /* initialize system clock */ 150 | rcu_sysclk_init(); 151 | 152 | /* initialize eclic */ 153 | eclic_init(); 154 | /* enable global interrupts */ 155 | eclic_global_interrupt_enable(); 156 | 157 | uart0_init(CORECLOCK, 115200, 2); 158 | usbacm_init(4); 159 | stdout = usbacm; 160 | 161 | mtimer_enable(); 162 | 163 | RCU->APB2EN |= RCU_APB2EN_PAEN | RCU_APB2EN_PCEN; 164 | 165 | gpio_pin_set(LED_RED); 166 | gpio_pin_set(LED_GREEN); 167 | gpio_pin_set(LED_BLUE); 168 | gpio_pin_config(LED_RED, GPIO_MODE_OD_2MHZ); 169 | gpio_pin_config(LED_GREEN, GPIO_MODE_OD_2MHZ); 170 | gpio_pin_config(LED_BLUE, GPIO_MODE_OD_2MHZ); 171 | 172 | dp_init(); 173 | dp_fill(0,0,160,80,0x000); 174 | dp_puts(&ter16n, 3*ter16n.width, 0*ter16n.height, 0xfff, 0x000, "Hello World!"); 175 | dp_puts(&ter16n, 3*ter16n.width, 1*ter16n.height, 0xf00, 0x000, "Hello World!"); 176 | dp_puts(&ter16n, 3*ter16n.width, 2*ter16n.height, 0x0f0, 0x000, "Hello World!"); 177 | dp_puts(&ter16n, 3*ter16n.width, 3*ter16n.height, 0x00f, 0x000, "Hello World!"); 178 | dp_puts(&ter16n, 3*ter16n.width, 4*ter16n.height, 0xf0f, 0x000, "Hello World!"); 179 | dp_on(); 180 | 181 | dp_line(0,0,160,80,0xf00); 182 | dp_line(160,0,0,80,0xf00); 183 | 184 | term_init(&term, 0xfff, 0x000); 185 | 186 | sd_init(); 187 | if (f_mount(&fs, "", 1) == FR_OK) 188 | listdir(&term, ""); 189 | 190 | while (1) { 191 | int c = usbacm_getchar(); 192 | 193 | switch (c) { 194 | case '\r': 195 | term_putchar(&term, '\n'); 196 | break; 197 | case '\t': 198 | term_putchar(&term, ' '); 199 | term_putchar(&term, ' '); 200 | break; 201 | case 0x7f: 202 | term_delete(&term); 203 | break; 204 | default: 205 | term_putchar(&term, c); 206 | } 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /examples/LonganNano/sdcard.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2020, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef SDCARD_H 28 | #define SDCARD_H 29 | 30 | #include 31 | 32 | void sd_init(void); 33 | void sd_uninit(void); 34 | 35 | /* return values are overlaid on the R1 response codes: 36 | * bit 0: idle state, should not be returned by any function below 37 | * bit 1: erase reset 38 | * bit 2: illegal command 39 | * bit 3: communication crc error 40 | * bit 4: erase sequence error 41 | * bit 5: address error 42 | * bit 6: parameter error 43 | * bit 7: set by library, not by sd card 44 | * the special value 0xff means no reply, no card or timeout 45 | */ 46 | uint8_t sd_wakeup(void); 47 | uint8_t sd_getocr(uint8_t ocr[4]); 48 | uint8_t sd_status(uint8_t *status); 49 | uint8_t sd_getcsd(uint8_t csd[16]); 50 | uint8_t sd_getcid(uint8_t cid[16]); 51 | uint8_t sd_getblocks(uint32_t *blocks); 52 | uint8_t sd_geterasesectorsize(uint32_t *size); 53 | uint8_t sd_readblock(uint32_t lba, uint8_t buf[512]); 54 | uint8_t sd_writeblock(uint32_t lba, const uint8_t buf[512]); 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /examples/LonganNano/ter16b.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Dimitar Toshkov Zhekov 3 | * with Reserved Font Name "Terminus Font" 4 | * 5 | * See OFL.txt for the full license. 6 | */ 7 | #include "display.h" 8 | 9 | const struct dp_font ter16b = { 10 | .width = 8, 11 | .height = 16, 12 | .data = { 13 | 0x00,0x00,0x7f,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x7f, 14 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 15 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18, 16 | 0x18,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00, 17 | 0x00,0x66,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 18 | 0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x36,0x36,0x7f,0x36,0x36, 19 | 0x7f,0x36,0x36,0x36,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x3e, 20 | 0x6b,0x0b,0x0b,0x3e,0x68,0x68,0x6b,0x3e,0x08,0x08,0x00,0x00, 21 | 0x00,0x00,0x66,0x6b,0x36,0x30,0x18,0x18,0x0c,0x6c,0xd6,0x66, 22 | 0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x36,0x36,0x1c,0x6e,0x3b, 23 | 0x33,0x33,0x3b,0x6e,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18, 24 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 25 | 0x00,0x00,0x30,0x18,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x18,0x30, 26 | 0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x18,0x30,0x30,0x30,0x30, 27 | 0x30,0x30,0x18,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 28 | 0x00,0x36,0x1c,0x7f,0x1c,0x36,0x00,0x00,0x00,0x00,0x00,0x00, 29 | 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x7e,0x18,0x18,0x00,0x00, 30 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 31 | 0x00,0x00,0x18,0x18,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 32 | 0x00,0x00,0x00,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 33 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18, 34 | 0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x30,0x30,0x18,0x18, 35 | 0x0c,0x0c,0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63, 36 | 0x63,0x73,0x7b,0x6f,0x67,0x63,0x63,0x3e,0x00,0x00,0x00,0x00, 37 | 0x00,0x00,0x18,0x1c,0x1e,0x18,0x18,0x18,0x18,0x18,0x18,0x7e, 38 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63,0x63,0x60,0x30,0x18, 39 | 0x0c,0x06,0x03,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63, 40 | 0x63,0x60,0x3c,0x60,0x60,0x63,0x63,0x3e,0x00,0x00,0x00,0x00, 41 | 0x00,0x00,0x60,0x70,0x78,0x6c,0x66,0x63,0x7f,0x60,0x60,0x60, 42 | 0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x03,0x03,0x03,0x3f,0x60, 43 | 0x60,0x60,0x63,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x06, 44 | 0x03,0x03,0x3f,0x63,0x63,0x63,0x63,0x3e,0x00,0x00,0x00,0x00, 45 | 0x00,0x00,0x7f,0x60,0x60,0x30,0x30,0x18,0x18,0x0c,0x0c,0x0c, 46 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63,0x63,0x63,0x3e,0x63, 47 | 0x63,0x63,0x63,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63, 48 | 0x63,0x63,0x63,0x7e,0x60,0x60,0x30,0x1e,0x00,0x00,0x00,0x00, 49 | 0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18, 50 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00, 51 | 0x00,0x00,0x18,0x18,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x60, 52 | 0x30,0x18,0x0c,0x06,0x0c,0x18,0x30,0x60,0x00,0x00,0x00,0x00, 53 | 0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00,0x7f,0x00,0x00,0x00, 54 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x0c,0x18,0x30,0x60, 55 | 0x30,0x18,0x0c,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63, 56 | 0x63,0x63,0x30,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00, 57 | 0x00,0x00,0x3e,0x63,0x73,0x6b,0x6b,0x6b,0x6b,0x73,0x03,0x7e, 58 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63,0x63,0x63,0x63,0x7f, 59 | 0x63,0x63,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x3f,0x63, 60 | 0x63,0x63,0x3f,0x63,0x63,0x63,0x63,0x3f,0x00,0x00,0x00,0x00, 61 | 0x00,0x00,0x3e,0x63,0x63,0x03,0x03,0x03,0x03,0x63,0x63,0x3e, 62 | 0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0x33,0x63,0x63,0x63,0x63, 63 | 0x63,0x63,0x33,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x03, 64 | 0x03,0x03,0x1f,0x03,0x03,0x03,0x03,0x7f,0x00,0x00,0x00,0x00, 65 | 0x00,0x00,0x7f,0x03,0x03,0x03,0x1f,0x03,0x03,0x03,0x03,0x03, 66 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63,0x63,0x03,0x03,0x7b, 67 | 0x63,0x63,0x63,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x63, 68 | 0x63,0x63,0x7f,0x63,0x63,0x63,0x63,0x63,0x00,0x00,0x00,0x00, 69 | 0x00,0x00,0x3c,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3c, 70 | 0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x30,0x30,0x30,0x30,0x30, 71 | 0x30,0x33,0x33,0x1e,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x63, 72 | 0x33,0x1b,0x0f,0x0f,0x1b,0x33,0x63,0x63,0x00,0x00,0x00,0x00, 73 | 0x00,0x00,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x7f, 74 | 0x00,0x00,0x00,0x00,0x00,0x00,0x41,0x63,0x77,0x7f,0x6b,0x63, 75 | 0x63,0x63,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x63, 76 | 0x63,0x67,0x6f,0x7b,0x73,0x63,0x63,0x63,0x00,0x00,0x00,0x00, 77 | 0x00,0x00,0x3e,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x3e, 78 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3f,0x63,0x63,0x63,0x63,0x3f, 79 | 0x03,0x03,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63, 80 | 0x63,0x63,0x63,0x63,0x63,0x63,0x7b,0x3e,0x60,0x00,0x00,0x00, 81 | 0x00,0x00,0x3f,0x63,0x63,0x63,0x63,0x3f,0x0f,0x1b,0x33,0x63, 82 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63,0x03,0x03,0x3e,0x60, 83 | 0x60,0x63,0x63,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x18, 84 | 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00, 85 | 0x00,0x00,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x3e, 86 | 0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x63,0x63,0x63,0x63,0x36, 87 | 0x36,0x36,0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x63, 88 | 0x63,0x63,0x63,0x6b,0x7f,0x77,0x63,0x41,0x00,0x00,0x00,0x00, 89 | 0x00,0x00,0x63,0x63,0x36,0x36,0x1c,0x1c,0x36,0x36,0x63,0x63, 90 | 0x00,0x00,0x00,0x00,0x00,0x00,0xc3,0xc3,0x66,0x66,0x3c,0x18, 91 | 0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x60, 92 | 0x60,0x30,0x18,0x0c,0x06,0x03,0x03,0x7f,0x00,0x00,0x00,0x00, 93 | 0x00,0x00,0x3c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x3c, 94 | 0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x0c,0x0c,0x18,0x18, 95 | 0x30,0x30,0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x30, 96 | 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x3c,0x00,0x00,0x00,0x00, 97 | 0x00,0x18,0x3c,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 98 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 99 | 0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00,0x0c,0x18,0x00,0x00, 100 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 101 | 0x00,0x00,0x00,0x00,0x00,0x3e,0x60,0x7e,0x63,0x63,0x63,0x7e, 102 | 0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,0x03,0x3f,0x63,0x63, 103 | 0x63,0x63,0x63,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 104 | 0x00,0x3e,0x63,0x03,0x03,0x03,0x63,0x3e,0x00,0x00,0x00,0x00, 105 | 0x00,0x00,0x60,0x60,0x60,0x7e,0x63,0x63,0x63,0x63,0x63,0x7e, 106 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x63,0x63, 107 | 0x7f,0x03,0x03,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x0c, 108 | 0x0c,0x3f,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x00,0x00,0x00,0x00, 109 | 0x00,0x00,0x00,0x00,0x00,0x7e,0x63,0x63,0x63,0x63,0x63,0x7e, 110 | 0x60,0x60,0x3e,0x00,0x00,0x00,0x03,0x03,0x03,0x3f,0x63,0x63, 111 | 0x63,0x63,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18, 112 | 0x00,0x1c,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00, 113 | 0x00,0x00,0x60,0x60,0x00,0x70,0x60,0x60,0x60,0x60,0x60,0x60, 114 | 0x66,0x66,0x3c,0x00,0x00,0x00,0x03,0x03,0x03,0x63,0x33,0x1b, 115 | 0x0f,0x1b,0x33,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x18, 116 | 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00, 117 | 0x00,0x00,0x00,0x00,0x00,0x3f,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b, 118 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3f,0x63,0x63, 119 | 0x63,0x63,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 120 | 0x00,0x3e,0x63,0x63,0x63,0x63,0x63,0x3e,0x00,0x00,0x00,0x00, 121 | 0x00,0x00,0x00,0x00,0x00,0x3f,0x63,0x63,0x63,0x63,0x63,0x3f, 122 | 0x03,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x63,0x63, 123 | 0x63,0x63,0x63,0x7e,0x60,0x60,0x60,0x00,0x00,0x00,0x00,0x00, 124 | 0x00,0x7b,0x0f,0x07,0x03,0x03,0x03,0x03,0x00,0x00,0x00,0x00, 125 | 0x00,0x00,0x00,0x00,0x00,0x7e,0x03,0x03,0x3e,0x60,0x60,0x3f, 126 | 0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x0c,0x0c,0x3f,0x0c,0x0c, 127 | 0x0c,0x0c,0x0c,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 128 | 0x00,0x63,0x63,0x63,0x63,0x63,0x63,0x7e,0x00,0x00,0x00,0x00, 129 | 0x00,0x00,0x00,0x00,0x00,0x63,0x63,0x63,0x36,0x36,0x1c,0x1c, 130 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x63,0x63,0x6b, 131 | 0x6b,0x6b,0x6b,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 132 | 0x00,0x63,0x63,0x36,0x1c,0x36,0x63,0x63,0x00,0x00,0x00,0x00, 133 | 0x00,0x00,0x00,0x00,0x00,0x63,0x63,0x63,0x63,0x63,0x63,0x7e, 134 | 0x60,0x60,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x30,0x18, 135 | 0x0c,0x06,0x03,0x7f,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x0c, 136 | 0x0c,0x0c,0x06,0x0c,0x0c,0x0c,0x0c,0x38,0x00,0x00,0x00,0x00, 137 | 0x00,0x00,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18, 138 | 0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0x18,0x18,0x18,0x30,0x18, 139 | 0x18,0x18,0x18,0x0e,0x00,0x00,0x00,0x00,0x00,0xce,0xdb,0x73, 140 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 141 | }, 142 | }; 143 | -------------------------------------------------------------------------------- /examples/LonganNano/ter16n.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Dimitar Toshkov Zhekov 3 | * with Reserved Font Name "Terminus Font" 4 | * 5 | * See OFL.txt for the full license. 6 | */ 7 | #include "display.h" 8 | 9 | const struct dp_font ter16n = { 10 | .width = 8, 11 | .height = 16, 12 | .data = { 13 | 0x00,0x00,0x7e,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x7e, 14 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 15 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08, 16 | 0x08,0x08,0x08,0x08,0x08,0x00,0x08,0x08,0x00,0x00,0x00,0x00, 17 | 0x00,0x24,0x24,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 18 | 0x00,0x00,0x00,0x00,0x00,0x00,0x24,0x24,0x24,0x7e,0x24,0x24, 19 | 0x7e,0x24,0x24,0x24,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x3e, 20 | 0x49,0x09,0x09,0x3e,0x48,0x48,0x49,0x3e,0x08,0x08,0x00,0x00, 21 | 0x00,0x00,0x26,0x29,0x16,0x10,0x08,0x08,0x04,0x34,0x4a,0x32, 22 | 0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x24,0x24,0x18,0x0c,0x52, 23 | 0x22,0x22,0x22,0x5c,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x08, 24 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 25 | 0x00,0x00,0x10,0x08,0x04,0x04,0x04,0x04,0x04,0x04,0x08,0x10, 26 | 0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x08,0x10,0x10,0x10,0x10, 27 | 0x10,0x10,0x08,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 28 | 0x00,0x24,0x18,0x7e,0x18,0x24,0x00,0x00,0x00,0x00,0x00,0x00, 29 | 0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x3e,0x08,0x08,0x00,0x00, 30 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 31 | 0x00,0x00,0x08,0x08,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 32 | 0x00,0x00,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 33 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08, 34 | 0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x10,0x10,0x08,0x08, 35 | 0x04,0x04,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42, 36 | 0x42,0x62,0x52,0x4a,0x46,0x42,0x42,0x3c,0x00,0x00,0x00,0x00, 37 | 0x00,0x00,0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10,0x10,0x7c, 38 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42,0x42,0x40,0x20,0x10, 39 | 0x08,0x04,0x02,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42, 40 | 0x42,0x40,0x38,0x40,0x40,0x42,0x42,0x3c,0x00,0x00,0x00,0x00, 41 | 0x00,0x00,0x40,0x60,0x50,0x48,0x44,0x42,0x7e,0x40,0x40,0x40, 42 | 0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x02,0x02,0x02,0x3e,0x40, 43 | 0x40,0x40,0x42,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x04, 44 | 0x02,0x02,0x3e,0x42,0x42,0x42,0x42,0x3c,0x00,0x00,0x00,0x00, 45 | 0x00,0x00,0x7e,0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x08, 46 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42,0x42,0x42,0x3c,0x42, 47 | 0x42,0x42,0x42,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42, 48 | 0x42,0x42,0x42,0x7c,0x40,0x40,0x20,0x1c,0x00,0x00,0x00,0x00, 49 | 0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x00,0x00,0x00,0x08,0x08, 50 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x00, 51 | 0x00,0x00,0x08,0x08,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x20, 52 | 0x10,0x08,0x04,0x02,0x04,0x08,0x10,0x20,0x00,0x00,0x00,0x00, 53 | 0x00,0x00,0x00,0x00,0x00,0x7e,0x00,0x00,0x7e,0x00,0x00,0x00, 54 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x04,0x08,0x10,0x20, 55 | 0x10,0x08,0x04,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42, 56 | 0x42,0x42,0x20,0x10,0x10,0x00,0x10,0x10,0x00,0x00,0x00,0x00, 57 | 0x00,0x00,0x3e,0x41,0x79,0x45,0x45,0x45,0x65,0x59,0x01,0x7e, 58 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42,0x42,0x42,0x42,0x7e, 59 | 0x42,0x42,0x42,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x42, 60 | 0x42,0x42,0x3e,0x42,0x42,0x42,0x42,0x3e,0x00,0x00,0x00,0x00, 61 | 0x00,0x00,0x3c,0x42,0x42,0x02,0x02,0x02,0x02,0x42,0x42,0x3c, 62 | 0x00,0x00,0x00,0x00,0x00,0x00,0x1e,0x22,0x42,0x42,0x42,0x42, 63 | 0x42,0x42,0x22,0x1e,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x02, 64 | 0x02,0x02,0x1e,0x02,0x02,0x02,0x02,0x7e,0x00,0x00,0x00,0x00, 65 | 0x00,0x00,0x7e,0x02,0x02,0x02,0x1e,0x02,0x02,0x02,0x02,0x02, 66 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42,0x42,0x02,0x02,0x72, 67 | 0x42,0x42,0x42,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x42,0x42, 68 | 0x42,0x42,0x7e,0x42,0x42,0x42,0x42,0x42,0x00,0x00,0x00,0x00, 69 | 0x00,0x00,0x1c,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x1c, 70 | 0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x20,0x20,0x20,0x20,0x20, 71 | 0x20,0x22,0x22,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x42,0x22, 72 | 0x12,0x0a,0x06,0x06,0x0a,0x12,0x22,0x42,0x00,0x00,0x00,0x00, 73 | 0x00,0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x7e, 74 | 0x00,0x00,0x00,0x00,0x00,0x00,0x41,0x63,0x55,0x49,0x49,0x41, 75 | 0x41,0x41,0x41,0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x42,0x42, 76 | 0x42,0x46,0x4a,0x52,0x62,0x42,0x42,0x42,0x00,0x00,0x00,0x00, 77 | 0x00,0x00,0x3c,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x3c, 78 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x42,0x42,0x42,0x42,0x3e, 79 | 0x02,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42, 80 | 0x42,0x42,0x42,0x42,0x42,0x42,0x52,0x3c,0x40,0x00,0x00,0x00, 81 | 0x00,0x00,0x3e,0x42,0x42,0x42,0x42,0x3e,0x0a,0x12,0x22,0x42, 82 | 0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42,0x02,0x02,0x3c,0x40, 83 | 0x40,0x42,0x42,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x08, 84 | 0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,0x00,0x00, 85 | 0x00,0x00,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x3c, 86 | 0x00,0x00,0x00,0x00,0x00,0x00,0x42,0x42,0x42,0x42,0x42,0x24, 87 | 0x24,0x24,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x41,0x41, 88 | 0x41,0x41,0x41,0x49,0x49,0x55,0x63,0x41,0x00,0x00,0x00,0x00, 89 | 0x00,0x00,0x42,0x42,0x24,0x24,0x18,0x18,0x24,0x24,0x42,0x42, 90 | 0x00,0x00,0x00,0x00,0x00,0x00,0x41,0x41,0x22,0x22,0x14,0x08, 91 | 0x08,0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x40, 92 | 0x40,0x20,0x10,0x08,0x04,0x02,0x02,0x7e,0x00,0x00,0x00,0x00, 93 | 0x00,0x00,0x1c,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x1c, 94 | 0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x04,0x04,0x08,0x08, 95 | 0x10,0x10,0x20,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x10, 96 | 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1c,0x00,0x00,0x00,0x00, 97 | 0x00,0x08,0x14,0x22,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 98 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 99 | 0x00,0x00,0x00,0x00,0x00,0x7e,0x00,0x00,0x08,0x10,0x00,0x00, 100 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 101 | 0x00,0x00,0x00,0x00,0x00,0x3c,0x40,0x7c,0x42,0x42,0x42,0x7c, 102 | 0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x02,0x3e,0x42,0x42, 103 | 0x42,0x42,0x42,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 104 | 0x00,0x3c,0x42,0x02,0x02,0x02,0x42,0x3c,0x00,0x00,0x00,0x00, 105 | 0x00,0x00,0x40,0x40,0x40,0x7c,0x42,0x42,0x42,0x42,0x42,0x7c, 106 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3c,0x42,0x42, 107 | 0x7e,0x02,0x02,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x08, 108 | 0x08,0x3e,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,0x00,0x00, 109 | 0x00,0x00,0x00,0x00,0x00,0x7c,0x42,0x42,0x42,0x42,0x42,0x7c, 110 | 0x40,0x40,0x3c,0x00,0x00,0x00,0x02,0x02,0x02,0x3e,0x42,0x42, 111 | 0x42,0x42,0x42,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08, 112 | 0x00,0x0c,0x08,0x08,0x08,0x08,0x08,0x1c,0x00,0x00,0x00,0x00, 113 | 0x00,0x00,0x20,0x20,0x00,0x30,0x20,0x20,0x20,0x20,0x20,0x20, 114 | 0x22,0x22,0x1c,0x00,0x00,0x00,0x02,0x02,0x02,0x42,0x22,0x12, 115 | 0x0e,0x12,0x22,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x08, 116 | 0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x1c,0x00,0x00,0x00,0x00, 117 | 0x00,0x00,0x00,0x00,0x00,0x3f,0x49,0x49,0x49,0x49,0x49,0x49, 118 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x42,0x42, 119 | 0x42,0x42,0x42,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 120 | 0x00,0x3c,0x42,0x42,0x42,0x42,0x42,0x3c,0x00,0x00,0x00,0x00, 121 | 0x00,0x00,0x00,0x00,0x00,0x3e,0x42,0x42,0x42,0x42,0x42,0x3e, 122 | 0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x7c,0x42,0x42, 123 | 0x42,0x42,0x42,0x7c,0x40,0x40,0x40,0x00,0x00,0x00,0x00,0x00, 124 | 0x00,0x7a,0x06,0x02,0x02,0x02,0x02,0x02,0x00,0x00,0x00,0x00, 125 | 0x00,0x00,0x00,0x00,0x00,0x7c,0x02,0x02,0x3c,0x40,0x40,0x3e, 126 | 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x08,0x3e,0x08,0x08, 127 | 0x08,0x08,0x08,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 128 | 0x00,0x42,0x42,0x42,0x42,0x42,0x42,0x7c,0x00,0x00,0x00,0x00, 129 | 0x00,0x00,0x00,0x00,0x00,0x42,0x42,0x42,0x24,0x24,0x18,0x18, 130 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x41,0x41,0x49, 131 | 0x49,0x49,0x49,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 132 | 0x00,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x00,0x00,0x00,0x00, 133 | 0x00,0x00,0x00,0x00,0x00,0x42,0x42,0x42,0x42,0x42,0x42,0x7c, 134 | 0x40,0x40,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x20,0x10, 135 | 0x08,0x04,0x02,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x08, 136 | 0x08,0x08,0x04,0x08,0x08,0x08,0x08,0x30,0x00,0x00,0x00,0x00, 137 | 0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08, 138 | 0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x10,0x10,0x10,0x20,0x10, 139 | 0x10,0x10,0x10,0x0c,0x00,0x00,0x00,0x00,0x00,0x46,0x49,0x31, 140 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 141 | }, 142 | }; 143 | -------------------------------------------------------------------------------- /examples/LonganNano/term.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include "display.h" 28 | #include "term.h" 29 | 30 | #ifndef ARRAY_SIZE 31 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) 32 | #endif 33 | 34 | extern struct dp_font ter16n; 35 | 36 | void 37 | term_init(struct term *t, uint16_t fg, uint16_t bg) 38 | { 39 | for (unsigned int i = 0; i < ARRAY_SIZE(t->buf); i++) 40 | t->buf[i][0] = '\n'; 41 | t->fg = fg; 42 | t->bg = bg; 43 | t->cursor_x = 0; 44 | t->cursor_y = 0; 45 | } 46 | 47 | void 48 | term_render(struct term *t) 49 | { 50 | for (unsigned int i = 0; i <= t->cursor_y; i++) { 51 | unsigned int j = 0; 52 | 53 | for (; j < ARRAY_SIZE(t->buf[0]); j++) { 54 | char c = t->buf[i][j]; 55 | 56 | if (c == '\n') 57 | break; 58 | dp_putchar(&ter16n, 8*j, 16*i, t->fg, t->bg, c); 59 | } 60 | for (; j < ARRAY_SIZE(t->buf[0]); j++) 61 | dp_putchar(&ter16n, 8*j, 16*i, t->fg, t->bg, ' '); 62 | } 63 | dp_putchar(&ter16n, 8*t->cursor_x, 16*t->cursor_y, t->fg, t->bg, '_'); 64 | } 65 | 66 | void 67 | term_putchar(struct term *t, char c) 68 | { 69 | t->buf[t->cursor_y][t->cursor_x] = c; 70 | if (c == '\n') { 71 | dp_putchar(&ter16n, 8*t->cursor_x, 16*t->cursor_y, t->fg, t->bg, ' '); 72 | t->cursor_x = ARRAY_SIZE(t->buf[0]); 73 | } else { 74 | dp_putchar(&ter16n, 8*t->cursor_x, 16*t->cursor_y, t->fg, t->bg, c); 75 | t->cursor_x += 1; 76 | } 77 | if (t->cursor_x == ARRAY_SIZE(t->buf[0])) { 78 | t->cursor_x = 0; 79 | if (t->cursor_y == (ARRAY_SIZE(t->buf) - 1)) { 80 | char *d = t->buf[0]; 81 | char *s = t->buf[1]; 82 | 83 | while (s < &t->buf[ARRAY_SIZE(t->buf)][0]) 84 | *d++ = *s++; 85 | *d = '\n'; 86 | term_render(t); 87 | return; 88 | } 89 | t->cursor_y += 1; 90 | } 91 | t->buf[t->cursor_y][t->cursor_x] = '\n'; 92 | dp_putchar(&ter16n, 8*t->cursor_x, 16*t->cursor_y, t->fg, t->bg, '_'); 93 | } 94 | 95 | void 96 | term_delete(struct term *t) 97 | { 98 | dp_putchar(&ter16n, 8*t->cursor_x, 16*t->cursor_y, t->fg, t->bg, ' '); 99 | if (t->cursor_x == 0) { 100 | if (t->cursor_y > 0) { 101 | t->cursor_y -= 1; 102 | for (t->cursor_x = 0; t->cursor_x < (ARRAY_SIZE(t->buf[0]) - 1); t->cursor_x++) { 103 | if (t->buf[t->cursor_y][t->cursor_x] == '\n') 104 | break; 105 | } 106 | } 107 | } else 108 | t->cursor_x -= 1; 109 | t->buf[t->cursor_y][t->cursor_x] = '\n'; 110 | dp_putchar(&ter16n, 8*t->cursor_x, 16*t->cursor_y, t->fg, t->bg, '_'); 111 | } 112 | -------------------------------------------------------------------------------- /examples/LonganNano/term.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef TERM_H 28 | #define TERM_H 29 | 30 | #include 31 | 32 | struct term { 33 | char buf[5][20]; 34 | uint16_t fg; 35 | uint16_t bg; 36 | uint8_t cursor_x; 37 | uint8_t cursor_y; 38 | }; 39 | 40 | void term_init(struct term *t, uint16_t fg, uint16_t bg); 41 | void term_render(struct term *t); 42 | void term_putchar(struct term *t, char c); 43 | void term_delete(struct term *t); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /examples/dfu-bootloader/Makefile: -------------------------------------------------------------------------------- 1 | include ../../Makefile 2 | 3 | libs += stdio-uart0 4 | 5 | # make sure we work even with the smallest 6 | # GD32VF103x4 with only 6k SRAM 7 | # for larger chips this just means we start 8 | # our stack at 6k and ignore SRAM after that 9 | RAM_SIZE=6*1024 10 | 11 | release: BOOTLOADER=0 12 | -------------------------------------------------------------------------------- /examples/dfu-bootloader/dfu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "gd32vf103/info.h" 31 | 32 | #include "dfu.h" 33 | #include "flash.h" 34 | 35 | #ifdef NDEBUG 36 | #define debug(...) 37 | #else 38 | #include 39 | #define debug(...) printf(__VA_ARGS__) 40 | #endif 41 | 42 | enum dfu_status { 43 | DFU_OK, 44 | DFU_errTARGET, 45 | DFU_errFILE, 46 | DFU_errWRITE, 47 | DFU_errERASE, 48 | DFU_errCHECK_ERASED, 49 | DFU_errPROG, 50 | DFU_errVERIFY, 51 | DFU_errADDRESS, 52 | DFU_errNOTDONE, 53 | DFU_errFIRMWARE, 54 | DFU_errVENDOR, 55 | DFU_errUSBR, 56 | DFU_errPOR, 57 | DFU_errUNKNOWN, 58 | DFU_errSTALLEDPKT, 59 | }; 60 | 61 | enum dfu_state { 62 | DFU_appIDLE, 63 | DFU_appDETACH, 64 | DFU_dfuIDLE, 65 | DFU_dfuDNLOAD_SYNC, 66 | DFU_dfuDNBUSY, 67 | DFU_dfuDNLOAD_IDLE, 68 | DFU_dfuMANIFEST_SYNC, 69 | DFU_dfuMANIFEST, 70 | DFU_dfuMANIFEST_WAIT_RESET, 71 | DFU_dfuUPLOAD_IDLE, 72 | DFU_dfuERROR, 73 | }; 74 | 75 | static struct { 76 | uint8_t bStatus; 77 | uint8_t bwPollTimeout[3]; 78 | uint8_t bState; 79 | uint8_t iString; 80 | } dfu_status; 81 | 82 | int 83 | dfu_detach(const struct usb_setup_packet *p, const void **data) 84 | { 85 | debug("DFU_DETACH\n"); 86 | 87 | usbfs_reboot_on_ack = true; 88 | 89 | return 0; 90 | } 91 | 92 | int 93 | dfu_dnload(const struct usb_setup_packet *p, const void **data) 94 | { 95 | static uint32_t addr; 96 | static union { 97 | uint8_t bytes[PAGE_SIZE]; 98 | uint32_t words[PAGE_SIZE/4]; 99 | } buf; 100 | static uint8_t *bp; 101 | const uint8_t *sp; 102 | 103 | debug("DFU_DNLOAD: wValue = %hu, wLength = %hu\n", 104 | p->wValue, p->wLength); 105 | 106 | switch (dfu_status.bState) { 107 | case DFU_dfuIDLE: 108 | #ifdef NDEBUG 109 | addr = FLASH_BASE + 4*PAGE_SIZE; 110 | #else 111 | addr = FLASH_BASE + 32*PAGE_SIZE; 112 | #endif 113 | bp = &buf.bytes[0]; 114 | break; 115 | case DFU_dfuDNLOAD_IDLE: 116 | break; 117 | default: 118 | return -1; 119 | } 120 | 121 | if (p->wLength == 0) { 122 | if (bp != &buf.bytes[0]) { 123 | while (bp < ARRAY_END(buf.bytes)) 124 | *bp++ = 0xFFU; 125 | if (addr >= FLASH_BASE + INFO->FLASH * PAGE_SIZE) 126 | goto err_addr; 127 | if (flash_page(addr, buf.words)) 128 | goto err_write; 129 | } 130 | dfu_status.bState = DFU_dfuMANIFEST_SYNC; 131 | return 0; 132 | } 133 | 134 | sp = *data; 135 | for (unsigned int len = p->wLength; len > 0; len--) { 136 | *bp++ = *sp++; 137 | if (bp < ARRAY_END(buf.bytes)) 138 | continue; 139 | 140 | if (addr >= FLASH_BASE + INFO->FLASH * PAGE_SIZE) 141 | goto err_addr; 142 | if (flash_page(addr, buf.words)) 143 | goto err_write; 144 | 145 | addr += ARRAY_SIZE(buf.bytes); 146 | bp = &buf.bytes[0]; 147 | } 148 | 149 | dfu_status.bState = DFU_dfuDNLOAD_SYNC; 150 | return 0; 151 | err_addr: 152 | dfu_status.bStatus = DFU_errADDRESS; 153 | dfu_status.bState = DFU_dfuERROR; 154 | return 0; 155 | err_write: 156 | dfu_status.bStatus = DFU_errWRITE; 157 | dfu_status.bState = DFU_dfuERROR; 158 | return 0; 159 | } 160 | 161 | int 162 | dfu_upload(const struct usb_setup_packet *p, const void **data) 163 | { 164 | static uint32_t offset; 165 | int ret; 166 | 167 | debug("DFU_UPLOAD: wValue = %hu, wIndex = %hu, wLength = %hu\n", 168 | p->wValue, p->wIndex, p->wLength); 169 | 170 | switch (dfu_status.bState) { 171 | case DFU_dfuIDLE: 172 | #ifdef NDEBUG 173 | offset = 4*PAGE_SIZE; 174 | #else 175 | offset = 32*PAGE_SIZE; 176 | #endif 177 | break; 178 | case DFU_dfuUPLOAD_IDLE: 179 | break; 180 | default: 181 | return -1; 182 | } 183 | 184 | if (offset + p->wLength > INFO->FLASH * PAGE_SIZE) { 185 | dfu_status.bState = DFU_dfuIDLE; 186 | ret = INFO->FLASH * PAGE_SIZE - offset; 187 | } else { 188 | dfu_status.bState = DFU_dfuUPLOAD_IDLE; 189 | ret = p->wLength; 190 | } 191 | *data = (const void *)(FLASH_BASE + offset); 192 | offset += ret; 193 | debug(" returning %d\n", ret); 194 | return ret; 195 | } 196 | 197 | int 198 | dfu_getstatus(const struct usb_setup_packet *p, const void **data) 199 | { 200 | debug("DFU_GETSTATUS\n"); 201 | 202 | switch (dfu_status.bState) { 203 | case DFU_dfuDNLOAD_SYNC: 204 | dfu_status.bState = DFU_dfuDNLOAD_IDLE; 205 | break; 206 | case DFU_dfuMANIFEST_SYNC: 207 | dfu_status.bState = DFU_dfuIDLE; 208 | break; 209 | } 210 | 211 | *data = &dfu_status; 212 | return sizeof(dfu_status); 213 | } 214 | 215 | int 216 | dfu_clrstatus(const struct usb_setup_packet *p, const void **data) 217 | { 218 | debug("DFU_CLRSTATUS\n"); 219 | 220 | if (dfu_status.bState != DFU_dfuERROR) 221 | return -1; 222 | 223 | dfu_status.bStatus = DFU_OK; 224 | dfu_status.bState = DFU_dfuIDLE; 225 | return 0; 226 | } 227 | 228 | int 229 | dfu_getstate(const struct usb_setup_packet *p, const void **data) 230 | { 231 | debug("DFU_GETSTATE\n"); 232 | 233 | *data = &dfu_status.bState; 234 | return 1; 235 | } 236 | 237 | int 238 | dfu_abort(const struct usb_setup_packet *p, const void **data) 239 | { 240 | debug("DFU_ABORT\n"); 241 | 242 | switch (dfu_status.bState) { 243 | case DFU_dfuIDLE: 244 | case DFU_dfuDNLOAD_IDLE: 245 | case DFU_dfuUPLOAD_IDLE: 246 | dfu_status.bStatus = DFU_OK; 247 | dfu_status.bState = DFU_dfuIDLE; 248 | return 0; 249 | } 250 | 251 | return -1; 252 | } 253 | 254 | void 255 | dfu_init(void) 256 | { 257 | dfu_status.bState = DFU_dfuIDLE; 258 | } 259 | -------------------------------------------------------------------------------- /examples/dfu-bootloader/dfu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef DFU_H 28 | #define DFU_H 29 | 30 | #include "usbfs.h" 31 | 32 | #define DFU_INTERFACE 0 33 | #define DFU_TRANSFERSIZE 1024 34 | //#define DFU_TRANSFERSIZE 64 35 | 36 | int dfu_detach(const struct usb_setup_packet *p, const void **data); 37 | int dfu_dnload(const struct usb_setup_packet *p, const void **data); 38 | int dfu_upload(const struct usb_setup_packet *p, const void **data); 39 | int dfu_getstatus(const struct usb_setup_packet *p, const void **data); 40 | int dfu_clrstatus(const struct usb_setup_packet *p, const void **data); 41 | int dfu_getstate(const struct usb_setup_packet *p, const void **data); 42 | int dfu_abort(const struct usb_setup_packet *p, const void **data); 43 | void dfu_init(void); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /examples/dfu-bootloader/flash.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "gd32vf103/fmc.h" 31 | #include "lib/gpio.h" 32 | #include "flash.h" 33 | 34 | #ifdef NDEBUG 35 | #define debug(...) 36 | #else 37 | #include 38 | #define debug(...) printf(__VA_ARGS__) 39 | #endif 40 | 41 | __attribute__((noclone)) 42 | __attribute__((noinline)) 43 | __attribute__((section(".ramtext.flash__page"))) 44 | static int flash__page(uint32_t addr, const uint32_t data[PAGE_SIZE/4]) 45 | { 46 | /* erase page */ 47 | FMC->CTL = (FMC->CTL & ~0x7U) | FMC_CTL_PER; 48 | FMC->ADDR = addr; 49 | FMC->CTL |= FMC_CTL_START; 50 | while (FMC->STAT & FMC_STAT_BUSY) 51 | /* wait */; 52 | if (FMC->STAT != FMC_STAT_ENDF) 53 | return -1; 54 | FMC->STAT = FMC_STAT_ENDF; 55 | 56 | /* write data */ 57 | for (unsigned int i = 0; i < PAGE_SIZE/4; i++, addr += 4) { 58 | uint32_t word = data[i]; 59 | 60 | if (word == 0xffffffffU) 61 | continue; 62 | 63 | FMC->CTL = (FMC->CTL & ~0x7U) | FMC_CTL_PG; 64 | *(volatile uint32_t *)addr = word; 65 | while (FMC->STAT & FMC_STAT_BUSY) 66 | /* wait */; 67 | if (FMC->STAT != FMC_STAT_ENDF) 68 | return -2; 69 | FMC->STAT = FMC_STAT_ENDF; 70 | } 71 | 72 | return 0; 73 | } 74 | 75 | int flash_page(uint32_t addr, const uint32_t data[PAGE_SIZE/4]) 76 | { 77 | int ret; 78 | 79 | debug(" flashing at 0x%08lx\n", addr); 80 | 81 | while (FMC->STAT & FMC_STAT_BUSY) 82 | /* wait */; 83 | 84 | /* clear error bits */ 85 | FMC->STAT = 86 | FMC_STAT_ENDF | 87 | FMC_STAT_WPERR | 88 | FMC_STAT_PGERR; 89 | 90 | /* unlock flash */ 91 | debug("FMC->CTL = 0x%08lx\n", FMC->CTL); 92 | if (FMC->CTL & FMC_CTL_LK) { 93 | FMC->KEY = FMC_KEY_UNLOCK0; 94 | FMC->KEY = FMC_KEY_UNLOCK1; 95 | debug("FMC->CTL = 0x%08lx\n", FMC->CTL); 96 | if (FMC->CTL & FMC_CTL_LK) 97 | return -1; 98 | } 99 | 100 | gpio_pin_set(LED); 101 | ret = flash__page(addr, data); 102 | gpio_pin_clear(LED); 103 | 104 | /* lock flash again */ 105 | FMC->CTL = FMC_CTL_LK; 106 | return ret; 107 | } 108 | -------------------------------------------------------------------------------- /examples/dfu-bootloader/flash.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef FLASH_H 28 | #define FLASH_H 29 | 30 | #include 31 | 32 | #ifdef NDEBUG 33 | #define LED GPIO_PC13 34 | #else 35 | #define LED GPIO_PA1 36 | #endif 37 | 38 | #define PAGE_SIZE 1024U 39 | 40 | int flash_page(uint32_t addr, const uint32_t data[PAGE_SIZE/4]); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /examples/dfu-bootloader/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include "gd32vf103/rcu.h" 28 | 29 | #include "lib/mtimer.h" 30 | #include "lib/eclic.h" 31 | #include "lib/rcu.h" 32 | #include "lib/gpio.h" 33 | 34 | #include "usbfs.h" 35 | #include "dfu.h" 36 | #include "flash.h" 37 | 38 | #ifndef NDEBUG 39 | #include 40 | #include "lib/stdio-uart0.h" 41 | #endif 42 | 43 | int main(void) 44 | { 45 | /* initialize system clock */ 46 | rcu_sysclk_init(); 47 | 48 | /* initialize eclic */ 49 | eclic_init(); 50 | /* enable global interrupts */ 51 | eclic_global_interrupt_enable(); 52 | 53 | #ifdef NDEBUG 54 | RCU->APB2EN |= RCU_APB2EN_PCEN; 55 | #else 56 | uart0_init(CORECLOCK, 115200, 2); 57 | stdout = uart0; 58 | printf("\n*** DFU ***\n"); 59 | 60 | RCU->APB2EN |= RCU_APB2EN_PAEN; 61 | #endif 62 | gpio_pin_clear(LED); 63 | gpio_pin_config(LED, GPIO_MODE_OD_2MHZ); 64 | 65 | dfu_init(); 66 | usbfs_init(); 67 | 68 | while (1) { 69 | #ifdef NDEBUG 70 | wait_for_interrupt(); 71 | #else 72 | int c = uart0_getchar(); 73 | 74 | switch (c) { 75 | case 't': 76 | printf("mtime = 0x%lx%08lx\n", MTIMER->mtime_hi, MTIMER->mtime_lo); 77 | continue; 78 | case 'i': 79 | printf("ECLIC->clicint[%u].ie = %u\n", USBFS_IRQn, 80 | ECLIC->clicint[USBFS_IRQn].ie); 81 | printf("ECLIC->clicint[%u].ip = %u\n", USBFS_IRQn, 82 | ECLIC->clicint[USBFS_IRQn].ip); 83 | printf("GOTGCS = 0x%08lx\n", USBFS->GOTGCS); 84 | printf("GOTGINTF = 0x%08lx\n", USBFS->GOTGINTF); 85 | printf("GAHBCS = 0x%08lx\n", USBFS->GAHBCS); 86 | printf("GUSBCS = 0x%08lx\n", USBFS->GUSBCS); 87 | printf("GRSTCTL = 0x%08lx\n", USBFS->GRSTCTL); 88 | printf("GINTF = 0x%08lx\n", USBFS->GINTF); 89 | printf("GINTEN = 0x%08lx\n", USBFS->GINTEN); 90 | printf("GRSTATR = 0x%08lx\n", USBFS->GRSTATR); 91 | printf("GRFLEN = 0x%08lx\n", USBFS->GRFLEN); 92 | printf("GCCFG = 0x%08lx\n", USBFS->GCCFG); 93 | printf("CID = 0x%08lx\n", USBFS->CID); 94 | printf("DCFG = 0x%08lx\n", USBFS->DCFG); 95 | printf("DCTL = 0x%08lx\n", USBFS->DCTL); 96 | printf("DSTAT = 0x%08lx\n", USBFS->DSTAT); 97 | continue; 98 | } 99 | #endif 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /examples/dfu-bootloader/test.sh: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2019, Emil Renner Berthing 2 | # 3 | # Redistribution and use in source and binary forms, with or without modification, 4 | # are permitted provided that the following conditions are met: 5 | # 6 | # 1. Redistributions of source code must retain the above copyright notice, this 7 | # list of conditions and the following disclaimer. 8 | # 2. Redistributions in binary form must reproduce the above copyright notice, 9 | # this list of conditions and the following disclaimer in the documentation 10 | # and/or other materials provided with the distribution. 11 | # 3. Neither the name of the copyright holder nor the names of its contributors 12 | # may be used to endorse or promote products derived from this software without 13 | # specific prior written permission. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 | # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 19 | # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 | # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 24 | # OF SUCH DAMAGE. 25 | 26 | set -e -o pipefail 27 | 28 | readonly file='test.bin' 29 | readonly size=4001 30 | 31 | dd if=/dev/urandom of="$file" bs=$size count=1 32 | dfu-util -R -D "$file" 33 | sleep 2 34 | dfu-util -s ":$((((size + 1023)/1024)*1024))" -U "${file}.dfu" 35 | diff -Naur <(xxd "$file") <(xxd "${file}.dfu") || true 36 | rm -f "$file" "${file}.dfu" 37 | -------------------------------------------------------------------------------- /examples/dfu-bootloader/usbfs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef USB_H 28 | #define USB_H 29 | 30 | #include 31 | #include 32 | 33 | #include "gd32vf103/usbfs.h" 34 | 35 | #define USB_WORD(x) ((x) & 0xFF),((x) >> 8) 36 | #define USB_TRIPLE(x) ((x) & 0xFF),(((x) >> 8) & 0xFF),((x) >> 16) 37 | #define USB_QUAD(x) ((x) & 0xFF),(((x) >> 8) & 0xFF),(((x) >> 16) & 0xFF),((x) >> 24) 38 | 39 | struct usb_setup_packet { 40 | union { 41 | struct { 42 | uint8_t bmRequestType; 43 | uint8_t bRequest; 44 | }; 45 | uint16_t request; 46 | }; 47 | uint16_t wValue; 48 | uint16_t wIndex; 49 | uint16_t wLength; 50 | }; 51 | 52 | struct usb_descriptor_device { 53 | uint8_t bLength; 54 | uint8_t bDescriptorType; 55 | uint16_t bcdUSB; 56 | uint8_t bDeviceClass; 57 | uint8_t bDeviceSubClass; 58 | uint8_t bDeviceProtocol; 59 | uint8_t bMaxPacketSize0; 60 | uint16_t idVendor; 61 | uint16_t idProduct; 62 | uint16_t bcdDevice; 63 | uint8_t iManufacturer; 64 | uint8_t iProduct; 65 | uint8_t iSerialNumber; 66 | uint8_t bNumConfigurations; 67 | }; 68 | 69 | struct usb_descriptor_device_qualifier { 70 | uint8_t bLength; 71 | uint8_t bDescriptorType; 72 | uint16_t bcdUSB; 73 | uint8_t bDeviceClass; 74 | uint8_t bDeviceSubClass; 75 | uint8_t bDeviceProtocol; 76 | uint8_t bMaxPacketSize0; 77 | uint8_t bNumConfigurations; 78 | uint8_t bReserved; 79 | }; 80 | 81 | struct usb_descriptor_configuration { 82 | uint8_t bLength; 83 | uint8_t bDescriptorType; 84 | uint16_t wTotalLength; 85 | uint8_t bNumInterfaces; 86 | uint8_t bConfigurationValue; 87 | uint8_t iConfiguration; 88 | uint8_t bmAttributes; 89 | uint8_t bMaxPower; 90 | uint8_t rest[]; 91 | }; 92 | 93 | struct usb_descriptor_string { 94 | uint8_t bLength; 95 | uint8_t bDescriptorType; 96 | uint16_t wCodepoint[]; 97 | }; 98 | 99 | struct usb_setup_handler { 100 | uint16_t req; 101 | uint8_t idx; 102 | uint8_t len; 103 | int (*fn)(const struct usb_setup_packet *p, const void **data); 104 | }; 105 | 106 | extern bool usbfs_reboot_on_ack; 107 | 108 | void usbfs_init(void); 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /gd32vf103.ld: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Keith Packard 3 | * Copyright (c) 2019-2020, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | ENTRY(_start) 29 | 30 | MEMORY 31 | { 32 | flash (rxai!w) : ORIGIN = 0x08000000 + __bootloader, LENGTH = __flash_size - __bootloader 33 | ram (wxa!ri) : ORIGIN = 0x20000000, LENGTH = __ram_size 34 | } 35 | 36 | PHDRS 37 | { 38 | text PT_LOAD; 39 | ram PT_LOAD; 40 | ram_init PT_LOAD; 41 | tls PT_TLS; 42 | } 43 | 44 | SECTIONS 45 | { 46 | PROVIDE(__stack = ORIGIN(ram) + LENGTH(ram)); 47 | 48 | .init : { 49 | KEEP(*(.data.init.enter)) 50 | KEEP(*(.text.init.enter)) 51 | KEEP(*(SORT_BY_NAME(.init) SORT_BY_NAME(.init.*))) 52 | } >flash AT>flash :text 53 | 54 | .text : { 55 | *(SORT_BY_ALIGNMENT(.text.unlikely) SORT_BY_ALIGNMENT(.text.unlikely.*)) 56 | *(.text.startup .text.startup.*) 57 | *(.text .text.*) 58 | *(.gnu.linkonce.t.*) 59 | } >flash AT>flash :text 60 | 61 | .fini : { 62 | KEEP(*(.fini)) 63 | __text_end = .; 64 | } >flash AT>flash :text 65 | 66 | PROVIDE(__etext = __text_end); 67 | PROVIDE(_etext = __text_end); 68 | PROVIDE(etext = __text_end); 69 | 70 | .rodata : { 71 | *(.rdata) 72 | *(.rodata .rodata.*) 73 | *(.gnu.linkonce.r.*) 74 | . = ALIGN(8); 75 | *(.srodata.cst16) 76 | *(.srodata.cst8) 77 | *(.srodata.cst4) 78 | *(.srodata.cst2) 79 | *(.srodata .srodata.*) 80 | *(.data.rel.ro .data.rel.ro.*) 81 | *(.got .got.*) 82 | } >flash AT>flash :text 83 | 84 | . = ALIGN(8); 85 | 86 | .preinit_array : { 87 | PROVIDE_HIDDEN(__preinit_array_start = .); 88 | KEEP(*(.preinit_array)) 89 | PROVIDE_HIDDEN(__preinit_array_end = .); 90 | } >flash AT>flash :text 91 | 92 | .init_array : { 93 | PROVIDE_HIDDEN(__init_array_start = .); 94 | KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) 95 | KEEP(*(.init_array EXCLUDE_FILE(*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o) .ctors)) 96 | PROVIDE_HIDDEN(__init_array_end = .); 97 | } >flash AT>flash :text 98 | 99 | .fini_array : { 100 | PROVIDE_HIDDEN(__fini_array_start = .); 101 | KEEP(*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) 102 | KEEP(*(.fini_array EXCLUDE_FILE(*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o) .dtors)) 103 | PROVIDE_HIDDEN(__fini_array_end = .); 104 | } >flash AT>flash :text 105 | 106 | .ctors : { 107 | KEEP(*crtbegin.o(.ctors)) 108 | KEEP(*crtbegin?.o(.ctors)) 109 | KEEP(*(EXCLUDE_FILE(*crtend.o *crtend?.o) .ctors)) 110 | KEEP(*(SORT_BY_INIT_PRIORITY(.ctors.*))) 111 | KEEP(*(.ctors)) 112 | } >flash AT>flash :text 113 | 114 | .dtors : { 115 | KEEP(*crtbegin.o(.dtors)) 116 | KEEP(*crtbegin?.o(.dtors)) 117 | KEEP(*(EXCLUDE_FILE(*crtend.o *crtend?.o) .dtors)) 118 | KEEP(*(SORT_BY_INIT_PRIORITY(.dtors.*))) 119 | KEEP(*(.dtors)) 120 | } >flash AT>flash :text 121 | 122 | /* 123 | * Data values which are preserved across reset 124 | */ 125 | .preserve (NOLOAD) : { 126 | PROVIDE(__preserve_start__ = .); 127 | KEEP(*(SORT_BY_NAME(.preserve.*))) 128 | KEEP(*(.preserve)) 129 | PROVIDE(__preserve_end__ = .); 130 | } >ram AT>ram :ram 131 | 132 | . = ALIGN(4); 133 | 134 | .data : ALIGN_WITH_INPUT { 135 | PROVIDE(__data_start = .); 136 | *(.ramtext .ramtext.*) 137 | *(.data .data.*) 138 | *(.gnu.linkonce.d.*) 139 | . = ALIGN(8); 140 | PROVIDE(__global_pointer$ = . + 0x800); 141 | *(.sdata .sdata.* .sdata2.*) 142 | *(.gnu.linkonce.s.*) 143 | } >ram AT>flash :ram_init 144 | PROVIDE(__data_source = LOADADDR(.data)); 145 | 146 | /* Thread local initialized data. This gets 147 | * space allocated as it is expected to be placed 148 | * in ram to be used as a template for TLS data blocks 149 | * allocated at runtime. We're slightly abusing that 150 | * by placing the data in flash where it will be copied 151 | * into the allocate ram addresses by the existing 152 | * data initialization code in crt0 153 | */ 154 | .tdata : ALIGN_WITH_INPUT { 155 | PROVIDE(__tls_base = .); 156 | *(.tdata .tdata.* .gnu.linkonce.td.*) 157 | PROVIDE(__data_end = .); 158 | } >ram AT>flash :tls :ram_init 159 | PROVIDE(__tdata_source = LOADADDR(.tdata)); 160 | PROVIDE(__tdata_size = SIZEOF(.tdata)); 161 | 162 | PROVIDE(__edata = __data_end); 163 | PROVIDE(_edata = __data_end); 164 | PROVIDE(edata = __data_end); 165 | PROVIDE(__data_size = __data_end - __data_start); 166 | 167 | .tbss (NOLOAD) : { 168 | PROVIDE(__bss_start = .); 169 | *(.tbss .tbss.* .gnu.linkonce.tb.*) 170 | *(.tcommon) 171 | PROVIDE(__tls_end = .); 172 | } >ram AT>ram :tls :ram 173 | PROVIDE(__tbss_size = SIZEOF(.tbss)); 174 | PROVIDE(__tls_size = __tls_end - __tls_base); 175 | 176 | /* 177 | * The linker special cases .tbss segments which are 178 | * identified as segments which are not loaded and are 179 | * thread_local. 180 | * 181 | * For these segments, the linker does not advance 'dot' 182 | * across them. We actually need memory allocated for tbss, 183 | * so we create a special segment here just to make room 184 | */ 185 | .tbss_space (NOLOAD) : { 186 | . = . + __tbss_size; 187 | } >ram AT>ram :ram 188 | 189 | .bss (NOLOAD) : { 190 | *(.sbss*) 191 | *(.gnu.linkonce.sb.*) 192 | *(.bss .bss.*) 193 | *(.gnu.linkonce.b.*) 194 | *(COMMON) 195 | . = ALIGN(8); 196 | __bss_end = .; 197 | } >ram AT>ram :ram 198 | PROVIDE(__end = __bss_end); 199 | PROVIDE(_end = __bss_end); 200 | PROVIDE(end = __bss_end); 201 | PROVIDE(__bss_size = __bss_end - __bss_start); 202 | 203 | /* Make the rest of memory available for heap storage */ 204 | PROVIDE(__heap_start = __end); 205 | PROVIDE(__heap_end = __stack - (DEFINED(__stack_size) ? __stack_size : 0x800)); 206 | PROVIDE(__heap_size = __heap_end - __heap_start); 207 | 208 | /* Define a stack region to make sure it fits in memory */ 209 | .stack __heap_end (NOLOAD) : { 210 | . += (DEFINED(__stack_size) ? __stack_size : 0x800); 211 | } >ram :ram 212 | 213 | /* Throw away C++ exception handling information */ 214 | /DISCARD/ : { 215 | *(.eh_frame .eh_frame.*) 216 | *(.note .note.*) 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /include/LonganNano.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef LONGANNANO_H 28 | #define LONGANNANO_H 29 | 30 | #define LED_RED GPIO_PC13 31 | #define LED_GREEN GPIO_PA1 32 | #define LED_BLUE GPIO_PA2 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/gd32vf103.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef GD32VF103_H 28 | #define GD32VF103_H 29 | 30 | #include "riscv/bits.h" 31 | #include "riscv/const.h" 32 | 33 | #define FLASH_BASE _AC(0x08000000,UL) /*!< main flash base address */ 34 | #define SRAM_BASE _AC(0x20000000,UL) /*!< SRAM base address */ 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /include/gd32vf103/bkp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_BKP_H 29 | #define GD32VF103_BKP_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define BKP_BASE _AC(0x40006C00,UL) /*!< BKP base address */ 34 | 35 | /* register definitions */ 36 | #define BKP_DATA0 _AC(0x04,UL) /*!< BKP data register 0 */ 37 | #define BKP_DATA1 _AC(0x08,UL) /*!< BKP data register 1 */ 38 | #define BKP_DATA2 _AC(0x0C,UL) /*!< BKP data register 2 */ 39 | #define BKP_DATA3 _AC(0x10,UL) /*!< BKP data register 3 */ 40 | #define BKP_DATA4 _AC(0x14,UL) /*!< BKP data register 4 */ 41 | #define BKP_DATA5 _AC(0x18,UL) /*!< BKP data register 5 */ 42 | #define BKP_DATA6 _AC(0x1C,UL) /*!< BKP data register 6 */ 43 | #define BKP_DATA7 _AC(0x20,UL) /*!< BKP data register 7 */ 44 | #define BKP_DATA8 _AC(0x24,UL) /*!< BKP data register 8 */ 45 | #define BKP_DATA9 _AC(0x28,UL) /*!< BKP data register 9 */ 46 | #define BKP_OCTL _AC(0x2C,UL) /*!< RTC signal output control register */ 47 | #define BKP_TPCTL _AC(0x30,UL) /*!< tamper pin control register */ 48 | #define BKP_TPCS _AC(0x34,UL) /*!< tamper control and status register */ 49 | #define BKP_DATA10 _AC(0x40,UL) /*!< BKP data register 10 */ 50 | #define BKP_DATA11 _AC(0x44,UL) /*!< BKP data register 11 */ 51 | #define BKP_DATA12 _AC(0x48,UL) /*!< BKP data register 12 */ 52 | #define BKP_DATA13 _AC(0x4C,UL) /*!< BKP data register 13 */ 53 | #define BKP_DATA14 _AC(0x50,UL) /*!< BKP data register 14 */ 54 | #define BKP_DATA15 _AC(0x54,UL) /*!< BKP data register 15 */ 55 | #define BKP_DATA16 _AC(0x58,UL) /*!< BKP data register 16 */ 56 | #define BKP_DATA17 _AC(0x5C,UL) /*!< BKP data register 17 */ 57 | #define BKP_DATA18 _AC(0x60,UL) /*!< BKP data register 18 */ 58 | #define BKP_DATA19 _AC(0x64,UL) /*!< BKP data register 19 */ 59 | #define BKP_DATA20 _AC(0x68,UL) /*!< BKP data register 20 */ 60 | #define BKP_DATA21 _AC(0x6C,UL) /*!< BKP data register 21 */ 61 | #define BKP_DATA22 _AC(0x70,UL) /*!< BKP data register 22 */ 62 | #define BKP_DATA23 _AC(0x74,UL) /*!< BKP data register 23 */ 63 | #define BKP_DATA24 _AC(0x78,UL) /*!< BKP data register 24 */ 64 | #define BKP_DATA25 _AC(0x7C,UL) /*!< BKP data register 25 */ 65 | #define BKP_DATA26 _AC(0x80,UL) /*!< BKP data register 26 */ 66 | #define BKP_DATA27 _AC(0x84,UL) /*!< BKP data register 27 */ 67 | #define BKP_DATA28 _AC(0x88,UL) /*!< BKP data register 28 */ 68 | #define BKP_DATA29 _AC(0x8C,UL) /*!< BKP data register 29 */ 69 | #define BKP_DATA30 _AC(0x90,UL) /*!< BKP data register 30 */ 70 | #define BKP_DATA31 _AC(0x94,UL) /*!< BKP data register 31 */ 71 | #define BKP_DATA32 _AC(0x98,UL) /*!< BKP data register 32 */ 72 | #define BKP_DATA33 _AC(0x9C,UL) /*!< BKP data register 33 */ 73 | #define BKP_DATA34 _AC(0xA0,UL) /*!< BKP data register 34 */ 74 | #define BKP_DATA35 _AC(0xA4,UL) /*!< BKP data register 35 */ 75 | #define BKP_DATA36 _AC(0xA8,UL) /*!< BKP data register 36 */ 76 | #define BKP_DATA37 _AC(0xAC,UL) /*!< BKP data register 37 */ 77 | #define BKP_DATA38 _AC(0xB0,UL) /*!< BKP data register 38 */ 78 | #define BKP_DATA39 _AC(0xB4,UL) /*!< BKP data register 39 */ 79 | #define BKP_DATA40 _AC(0xB8,UL) /*!< BKP data register 40 */ 80 | #define BKP_DATA41 _AC(0xBC,UL) /*!< BKP data register 41 */ 81 | 82 | /* BKP_DATA */ 83 | #define BKP_DATA_Pos 0 84 | #define BKP_DATA_Msk _AC(0x0000ffff,U) /*!< backup data */ 85 | #define BKP_DATA(x) ((x) << BKP_DATA_Pos) 86 | 87 | /* BKP_OCTL */ 88 | #define BKP_OCTL_ROSEL _BIT(9,U) /*!< RTC output selection */ 89 | #define BKP_OCTL_ASOEN _BIT(8,U) /*!< RTC alarm or second signal output enable */ 90 | #define BKP_OCTL_COEN _BIT(7,U) /*!< RTC clock calibration output enable */ 91 | #define BKP_OCTL_RCCV_Pos 0 92 | #define BKP_OCTL_RCCV_Msk _AC(0x0000007f,U) /*!< RTC clock calibration value */ 93 | #define BKP_OCTL_RCCV(x) ((x) << BKP_OCTL_RCCV_Pos) 94 | 95 | /* BKP_TPCTL */ 96 | #define BKP_TPCTL_TPAL _BIT(1,U) /*!< tamper pin active level */ 97 | #define BKP_TPCTL_TPEN _BIT(0,U) /*!< tamper detection enable */ 98 | 99 | /* BKP_TPCS */ 100 | #define BKP_TPCS_TIF _BIT(9,U) /*!< tamper interrupt flag */ 101 | #define BKP_TPCS_TEF _BIT(8,U) /*!< tamper event flag */ 102 | #define BKP_TPCS_TPIE _BIT(2,U) /*!< tamper interrupt enable */ 103 | #define BKP_TPCS_TIR _BIT(1,U) /*!< tamper interrupt reset */ 104 | #define BKP_TPCS_TER _BIT(0,U) /*!< tamper event reset */ 105 | 106 | #ifndef __ASSEMBLER__ 107 | #include 108 | 109 | struct gd32vf103_bkp_data { 110 | uint16_t reserved; 111 | volatile uint16_t DATA; 112 | }; 113 | 114 | struct gd32vf103_bkp { 115 | uint32_t reserved1; 116 | struct gd32vf103_bkp_data DATA0_9[10]; 117 | volatile uint32_t OCTL; 118 | volatile uint32_t TPCTL; 119 | volatile uint32_t TPCS; 120 | uint32_t reserved2; 121 | uint32_t reserved3; 122 | struct gd32vf103_bkp_data DATA10_41[32]; 123 | }; 124 | 125 | static struct gd32vf103_bkp *const BKP = (struct gd32vf103_bkp *)BKP_BASE; 126 | 127 | #endif 128 | #endif 129 | -------------------------------------------------------------------------------- /include/gd32vf103/crc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_CRC_H 29 | #define GD32VF103_CRC_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define CRC_BASE _AC(0x40023000,UL) /*!< CRC base address */ 34 | 35 | /* register definitions */ 36 | #define CRC_DATA _AC(0x00,UL) /*!< CRC data register */ 37 | #define CRC_FDATA _AC(0x04,UL) /*!< CRC free data register */ 38 | #define CRC_CTL _AC(0x08,UL) /*!< CRC control register */ 39 | 40 | /* CRC_DATA */ 41 | #define CRC_DATA_DATA_Pos 0 42 | #define CRC_DATA_DATA_Msk _AC(0xffffffff,U) /*!< CRC calculation result bits */ 43 | #define CRC_DATA_DATA(x) ((x) << CRC_DATA_DATA_Pos) 44 | 45 | /* CRC_FDATA */ 46 | #define CRC_FDATA_FDATA_Pos 0 47 | #define CRC_FDATA_FDATA_Msk _AC(0x000000ff,U) /*!< CRC free data bits */ 48 | #define CRC_FDATA_FDATA(x) ((x) << CRC_FDATA_FDATA_Pos) 49 | 50 | /* CRC_CTL */ 51 | #define CRC_CTL_RST _BIT(0,U) /*!< CRC reset CRC_DATA register bit */ 52 | 53 | #ifndef __ASSEMBLER__ 54 | #include 55 | 56 | struct gd32vf103_crc { 57 | volatile uint32_t DATA; 58 | volatile uint32_t FDATA; 59 | volatile uint32_t CTL; 60 | }; 61 | 62 | static struct gd32vf103_crc *const CRC = (struct gd32vf103_crc *)CRC_BASE; 63 | 64 | #endif 65 | #endif 66 | -------------------------------------------------------------------------------- /include/gd32vf103/csr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef GD32VF103_CSR_H 28 | #define GD32VF103_CSR_H 29 | 30 | #include "riscv/const.h" 31 | #include "riscv/csr.h" 32 | 33 | /* custom CSRs */ 34 | #define CSR_MTVT 0x307 35 | #define CSR_MNXTI 0x345 36 | #define CSR_MINTSTATUS 0x346 37 | #define CSR_MSCRATCHCSW 0x348 38 | #define CSR_MSCRATCHCSWL 0x349 39 | #define CSR_MNVEC 0x7C3 40 | #define CSR_MSUBM 0x7C4 41 | #define CSR_MMISC_CTL 0x7D0 42 | #define CSR_MSAVESTATUS 0x7D6 43 | #define CSR_MSAVEPC1 0x7D7 44 | #define CSR_MSAVECAUSE1 0x7D8 45 | #define CSR_MSAVEPC2 0x7D9 46 | #define CSR_MSAVECAUSE2 0x7DA 47 | #define CSR_PUSHMSUBM 0x7EB 48 | #define CSR_MTVT2 0x7EC 49 | #define CSR_JALMNXTI 0x7ED 50 | #define CSR_PUSHMCAUSE 0x7EE 51 | #define CSR_PUSHMEPC 0x7EF 52 | #define CSR_WFE 0x810 53 | #define CSR_SLEEPVALUE 0x811 54 | #define CSR_TXEVT 0x812 55 | 56 | /* custom mtvec bits */ 57 | #define CSR_MTVEC_MODE_Pos 0 58 | #define CSR_MTVEC_MODE_Msk _AC(0x0000003f,UL) 59 | #define CSR_MTVEC_MODE_ECLIC _AC(0x00000003,UL) 60 | 61 | /* custom mcause bits */ 62 | #define CSR_MCAUSE_MINHV _BIT(30,UL) 63 | #define CSR_MCAUSE_MPP_Pos 28 64 | #define CSR_MCAUSE_MPP_Msk _AC(0x30000000,UL) 65 | #define CSR_MCAUSE_MPP(x) ((x) << CSR_MCAUSE_MPP_Pos) 66 | #define CSR_MCOUSE_MPIE _BIT(27,UL) 67 | #define CSR_MCAUSE_MPIL_Pos 16 68 | #define CSR_MCAUSE_MPIL_Msk _AC(0x00ff0000,UL) 69 | #define CSR_MCAUSE_MPIL(x) ((x) << CSR_MCAUSE_MPIL_Pos) 70 | #define CSR_MCAUSE_EXCCODE_Pos 0 71 | #define CSR_MCAUSE_EXCCODE_Msk _AC(0x00000fff,UL) 72 | #define CSR_MCAUSE_EXCCODE(x) ((x) << CSR_MCAUSE_EXCCODE_Pos) 73 | 74 | /* mintstatus bits */ 75 | #define CSR_MINTSTATUS_MIL_Pos 24 76 | #define CSR_MINTSTATUS_MIL_Msk _AC(0xff000000,UL) 77 | #define CSR_MINTSTATUS_MIL(x) ((x) << CSR_MINTSTATUS_MIL_Pos) 78 | #define CSR_MINTSTATUS_UIL_Pos 0 79 | #define CSR_MINTSTATUS_UIL_Msk _AC(0x000000ff,UL) 80 | #define CSR_MINTSTATUS_UIL(x) ((x) << CSR_MINTSTATUS_UIL_Pos) 81 | 82 | /* mmisc_ctl bits */ 83 | #define CSR_MMISC_CTL_NMI_CAUSE_FFF _BIT(9,UL) 84 | 85 | /* mtvt2 bits */ 86 | #define CSR_MTVT2_MTVT2EN _BIT(0,UL) 87 | 88 | /* wfe bits */ 89 | #define CSR_WFE_WFE _BIT(0,UL) 90 | 91 | /* sleepvalue bits */ 92 | #define CSR_SLEEPVALUE_SLEEPVALUE _BIT(0,UL) 93 | 94 | /* txevt bits */ 95 | #define CSR_TXEVT_TXEVT _BIT(0,UL) 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /include/gd32vf103/dbg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_DBG_H 29 | #define GD32VF103_DBG_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define DBG_BASE _AC(0xe0042000,UL) /*!< DBG base address */ 34 | 35 | /* register definitions */ 36 | #define DBG_ID _AC(0x00,UL) /*!< DBG_ID code register */ 37 | #define DBG_CTL _AC(0x04,UL) /*!< DBG control register */ 38 | /* undocumented registers below, names guessed */ 39 | #define DBG_CMD _AC(0x08,UL) /*!< DBG command register */ 40 | #define DBG_KEY _AC(0x0c,UL) /*!< DBG unlock key register */ 41 | 42 | /* DBG_ID */ 43 | #define DBG_ID_ID_CODE_Pos 0 44 | #define DBG_ID_ID_CODE_Msk _AC(0xffffffff,U) /*!< DBG ID code values */ 45 | #define DBG_ID_ID_CODE(x) ((x) << DBG_ID_ID_CODE_Pos) 46 | 47 | /* DBG_CTL */ 48 | #define DBG_CTL_CAN1_HOLD _BIT(21,U) /*!< debug CAN1 kept when core is halted */ 49 | #define DBG_CTL_TIMER6_HOLD _BIT(20,U) /*!< hold TIMER6 counter when core is halted */ 50 | #define DBG_CTL_TIMER5_HOLD _BIT(19,U) /*!< hold TIMER5 counter when core is halted */ 51 | #define DBG_CTL_TIMER4_HOLD _BIT(18,U) /*!< hold TIMER4 counter when core is halted */ 52 | #define DBG_CTL_I2C1_HOLD _BIT(16,U) /*!< hold I2C1 smbus when core is halted */ 53 | #define DBG_CTL_I2C0_HOLD _BIT(15,U) /*!< hold I2C0 smbus when core is halted */ 54 | #define DBG_CTL_CAN0_HOLD _BIT(14,U) /*!< debug CAN0 kept when core is halted */ 55 | #define DBG_CTL_TIMER3_HOLD _BIT(13,U) /*!< hold TIMER3 counter when core is halted */ 56 | #define DBG_CTL_TIMER2_HOLD _BIT(12,U) /*!< hold TIMER2 counter when core is halted */ 57 | #define DBG_CTL_TIMER1_HOLD _BIT(11,U) /*!< hold TIMER1 counter when core is halted */ 58 | #define DBG_CTL_TIMER0_HOLD _BIT(10,U) /*!< hold TIMER0 counter when core is halted */ 59 | #define DBG_CTL_WWDGT_HOLD _BIT(9,U) /*!< debug WWDGT kept when core is halted */ 60 | #define DBG_CTL_FWDGT_HOLD _BIT(8,U) /*!< debug FWDGT kept when core is halted */ 61 | #define DBG_CTL_STB_HOLD _BIT(2,U) /*!< keep debugger connection during standby mode */ 62 | #define DBG_CTL_DSLP_HOLD _BIT(1,U) /*!< keep debugger connection during deepsleep mode */ 63 | #define DBG_CTL_SLP_HOLD _BIT(0,U) /*!< keep debugger connection during sleep mode */ 64 | 65 | /* DBG_CMD */ 66 | #define DBG_CMD_RESET _BIT(0,U) /*!< software reset */ 67 | 68 | /* DBG_KEY */ 69 | #define DBG_KEY_UNLOCK _AC(0x4b5a6978,U) /*!< unlock key */ 70 | 71 | #ifndef __ASSEMBLER__ 72 | #include 73 | 74 | struct gd32vf103_dbg { 75 | const uint32_t ID; 76 | volatile uint32_t CTL; 77 | volatile uint32_t CMD; 78 | volatile uint32_t KEY; 79 | }; 80 | 81 | static struct gd32vf103_dbg *const DBG = (struct gd32vf103_dbg *)DBG_BASE; 82 | 83 | #endif 84 | #endif 85 | -------------------------------------------------------------------------------- /include/gd32vf103/exmc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_EXMC_H 29 | #define GD32VF103_EXMC_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define EXMC_BASE _AC(0xA0000000,UL) /*!< EXMC register base address */ 34 | 35 | /* register definitions */ 36 | #define EXMC_SNCTL0 _AC(0x000,UL) /*!< EXMC SRAM/NOR flash control register 0 */ 37 | #define EXMC_SNTCFG0 _AC(0x004,UL) /*!< EXMC SRAM/NOR flash timing configuration register 0 */ 38 | #define EXMC_SNWTCFG0 _AC(0x104,UL) /*!< EXMC SRAM/NOR flash write timing configuration register 0 */ 39 | 40 | /* EXMC_SNCTLx, x=0 */ 41 | #define EXMC_SNCTL_ASYNCWAIT _BIT(15,U) /*!< asynchronous wait */ 42 | #define EXMC_SNCTL_NRWTEN _BIT(13,U) /*!< NWAIT signal enable */ 43 | #define EXMC_SNCTL_WREN _BIT(12,U) /*!< write enable */ 44 | #define EXMC_SNCTL_NRWTPOL _BIT(9,U) /*!< NWAIT signal polarity */ 45 | #define EXMC_SNCTL_NREN _BIT(6,U) /*!< NOR flash access enable */ 46 | #define EXMC_SNCTL_NRW_Pos 4 47 | #define EXMC_SNCTL_NRW_Msk _AC(0x00000030,U) /*!< NOR bank memory data bus width */ 48 | #define EXMC_SNCTL_NRW(x) ((x) << EXMC_SNCTL_NRW_Pos) 49 | #define EXMC_SNCTL_NRW_8B EXMC_SNCTL_NRW(0) /*!< NOR data width 8 bits */ 50 | #define EXMC_SNCTL_NRW_16B EXMC_SNCTL_NRW(1) /*!< NOR data width 16 bits */ 51 | #define EXMC_SNCTL_NRTP_Pos 2 52 | #define EXMC_SNCTL_NRTP_Msk _AC(0x0000000c,U) /*!< NOR bank memory type */ 53 | #define EXMC_SNCTL_NRTP(x) ((x) << EXMC_SNCTL_NRTP_Pos) 54 | #define EXMC_SNCTL_NRTP_SRAM EXMC_SNCTL_NRTP(0) /*!< SRAM,ROM */ 55 | #define EXMC_SNCTL_NRTP_PSRAM EXMC_SNCTL_NRTP(1) /*!< PSRAM,CRAM */ 56 | #define EXMC_SNCTL_NRTP_NOR EXMC_SNCTL_NRTP(2) /*!< NOR flash */ 57 | #define EXMC_SNCTL_NRMUX _BIT(1,U) /*!< NOR bank memory address/data multiplexing */ 58 | #define EXMC_SNCTL_NRBKEN _BIT(0,U) /*!< NOR bank enable */ 59 | 60 | /* EXMC_SNTCFGx, x=0 */ 61 | #define EXMC_SNTCFG_BUSLAT_Pos 16 62 | #define EXMC_SNTCFG_BUSLAT_Msk _AC(0x000f0000,U) /*!< bus latency */ 63 | #define EXMC_SNTCFG_BUSLAT(x) ((x) << EXMC_SNTCFG_BUSLAT_Pos) 64 | #define EXMC_SNTCFG_DSET_Pos 8 65 | #define EXMC_SNTCFG_DSET_Msk _AC(0x0000ff00,U) /*!< data setup time */ 66 | #define EXMC_SNTCFG_DSET(x) ((x) << EXMC_SNTCFG_DSET_Pos) 67 | #define EXMC_SNTCFG_AHLD_Pos 4 68 | #define EXMC_SNTCFG_AHLD_Msk _AC(0x000000f0,U) /*!< address hold time */ 69 | #define EXMC_SNTCFG_AHLD(x) ((x) << EXMC_SNTCFG_AHLD_Pos) 70 | #define EXMC_SNTCFG_ASET_Pos 0 71 | #define EXMC_SNTCFG_ASET_Msk _AC(0x0000000f,U) /*!< address setup time */ 72 | #define EXMC_SNTCFG_ASET(x) ((x) << EXMC_SNTCFG_ASET_Pos) 73 | 74 | #ifndef __ASSEMBLER__ 75 | #include 76 | 77 | struct gd32vf103_exmc { 78 | volatile uint32_t SNCTL0; 79 | volatile uint32_t SNTCFG0; 80 | uint32_t reserved1[63]; 81 | volatile uint32_t SNWTCFG0; 82 | }; 83 | 84 | static struct gd32vf103_exmc *const EXMC = (struct gd32vf103_exmc *)EXMC_BASE; 85 | 86 | #endif 87 | #endif 88 | -------------------------------------------------------------------------------- /include/gd32vf103/fmc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_FMC_H 29 | #define GD32VF103_FMC_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define OB_BASE _AC(0x1FFFF800,UL) /*!< OB base address */ 34 | #define FMC_BASE _AC(0x40022000,UL) /*!< FMC base address */ 35 | 36 | /* register definitions */ 37 | #define FMC_WS _AC(0x000,UL) /*!< FMC wait state register */ 38 | #define FMC_KEY _AC(0x004,UL) /*!< FMC unlock key register 0 */ 39 | #define FMC_OBKEY _AC(0x008,UL) /*!< FMC option bytes unlock key register */ 40 | #define FMC_STAT _AC(0x00C,UL) /*!< FMC status register 0 */ 41 | #define FMC_CTL _AC(0x010,UL) /*!< FMC control register 0 */ 42 | #define FMC_ADDR _AC(0x014,UL) /*!< FMC address register 0 */ 43 | #define FMC_OBSTAT _AC(0x01C,UL) /*!< FMC option bytes status register */ 44 | #define FMC_WP _AC(0x020,UL) /*!< FMC erase/program protection register */ 45 | /* Where is this register? It is mentioned, but not defined in the user manual 46 | #define FMC_WSEN _AC(0x024,UL) !< FMC wait state enable register 47 | */ 48 | #define FMC_PID _AC(0x100,UL) /*!< FMC product ID register */ 49 | 50 | #define OB_SPC _AC(0x000,UL) /*!< option byte security protection value */ 51 | #define OB_USER _AC(0x002,UL) /*!< option byte user value*/ 52 | #define OB_WP0 _AC(0x008,UL) /*!< option byte write protection 0 */ 53 | #define OB_WP1 _AC(0x00A,UL) /*!< option byte write protection 1 */ 54 | #define OB_WP2 _AC(0x00C,UL) /*!< option byte write protection 2 */ 55 | #define OB_WP3 _AC(0x00E,UL) /*!< option byte write protection 3 */ 56 | 57 | /* FMC_WS */ 58 | #define FMC_WS_WSCNT_Pos 0 59 | #define FMC_WS_WSCNT_Msk _AC(0x00000007,U) /*!< wait state counter */ 60 | #define FMC_WS_WSCNT(x) ((x) << FMC_WS_WSCNT_Pos) 61 | #define FMC_WS_WSCNT_0 FMC_WS_WSCNT(0) /*!< FMC 0 wait */ 62 | #define FMC_WS_WSCNT_1 FMC_WS_WSCNT(1) /*!< FMC 1 wait */ 63 | #define FMC_WS_WSCNT_2 FMC_WS_WSCNT(2) /*!< FMC 2 wait */ 64 | 65 | /* FMC_KEY */ 66 | #define FMC_KEY_UNLOCK0 _AC(0x45670123,U) /*!< fmc unlock key 0 */ 67 | #define FMC_KEY_UNLOCK1 _AC(0xCDEF89AB,U) /*!< fmc unlock key 1 */ 68 | 69 | /* FMC_OBKEY */ 70 | #define FMC_OBKEY_UNLOCK0 _AC(0x45670123,U) /*!< option bytes unlock key 0 */ 71 | #define FMC_OBKEY_UNLOCK1 _AC(0xCDEF89AB,U) /*!< option bytes unlock key 1 */ 72 | 73 | /* FMC_STAT */ 74 | #define FMC_STAT_ENDF _BIT(5,U) /*!< end of operation flag bit */ 75 | #define FMC_STAT_WPERR _BIT(4,U) /*!< erase/program protection error flag bit */ 76 | #define FMC_STAT_PGERR _BIT(2,U) /*!< flash program error flag bit */ 77 | #define FMC_STAT_BUSY _BIT(0,U) /*!< flash busy flag bit */ 78 | 79 | /* FMC_CTL */ 80 | #define FMC_CTL_ENDIE _BIT(12,U) /*!< end of operation interrupt enable bit */ 81 | #define FMC_CTL_ERRIE _BIT(10,U) /*!< error interrupt enable bit */ 82 | #define FMC_CTL_OBWEN _BIT(9,U) /*!< option bytes erase/program enable bit */ 83 | #define FMC_CTL_LK _BIT(7,U) /*!< FMC_CTL lock bit */ 84 | #define FMC_CTL_START _BIT(6,U) /*!< send erase command to FMC bit */ 85 | #define FMC_CTL_OBER _BIT(5,U) /*!< option bytes erase command bit */ 86 | #define FMC_CTL_OBPG _BIT(4,U) /*!< option bytes program command bit */ 87 | #define FMC_CTL_MER _BIT(2,U) /*!< main flash mass erase for bank0 command bit */ 88 | #define FMC_CTL_PER _BIT(1,U) /*!< main flash page erase for bank0 command bit */ 89 | #define FMC_CTL_PG _BIT(0,U) /*!< main flash program for bank0 command bit */ 90 | 91 | /* FMC_OBSTAT */ 92 | #define FMC_OBSTAT_DATA_Pos 10 93 | #define FMC_OBSTAT_DATA_Msk _AC(0x03fffc00,U) /*!< store DATA of option bytes block after system reset. */ 94 | #define FMC_OBSTAT_DATA(x) ((x) << FMC_OBSTAT_DATA_Pos) 95 | #define FMC_OBSTAT_USER_Pos 2 96 | #define FMC_OBSTAT_USER_Msk _AC(0x000003fc,U) /*!< store USER of option bytes block after system reset */ 97 | #define FMC_OBSTAT_USER(x) ((x) << FMC_OBSTAT_USER_Pos) 98 | #define FMC_OBSTAT_SPC _BIT(1,U) /*!< option bytes security protection code */ 99 | #define FMC_OBSTAT_OBERR _BIT(0,U) /*!< option bytes read error bit. */ 100 | 101 | /* FMC_WSEN */ 102 | #define FMC_WSEN_WSEN _BIT(0,U) /*!< FMC wait state enable bit */ 103 | 104 | /* OB_SPC */ 105 | #define OB_SPC_OFF 0xA5 /*!< no security protection */ 106 | 107 | /* OB_USER */ 108 | #define OB_USER_BB 0x08 /*!< boot from bank1 */ 109 | #define OB_USER_RST_STDBY 0x04 /*!< generate a reset instead of entering standby mode */ 110 | #define OB_USER_RST_DPSLP 0x02 /*!< generate a reset instead of entering deepsleep mode */ 111 | #define OB_USER_WDG_HW 0x01 /*!< hardware free watchdog */ 112 | 113 | #ifndef __ASSEMBLER__ 114 | #include 115 | 116 | struct gd32vf103_fmc { 117 | volatile uint32_t WS; 118 | volatile uint32_t KEY; 119 | volatile uint32_t OBKEY; 120 | volatile uint32_t STAT; 121 | volatile uint32_t CTL; 122 | volatile uint32_t ADDR; 123 | const uint32_t OBSTAT; 124 | const uint32_t WP; 125 | /* Where is this register? It is mentioned, but not defined in the user manual 126 | #define FMC_WSEN _AC(0x024,UL) !< FMC wait state enable register 127 | */ 128 | uint32_t reserved[56]; 129 | const uint32_t PID; 130 | }; 131 | 132 | struct gd32vf103_ob { 133 | volatile uint16_t SPC; 134 | volatile uint16_t USER; 135 | union { 136 | volatile uint32_t DATA; 137 | struct { 138 | volatile uint16_t DATA_LO; 139 | volatile uint16_t DATA_HI; 140 | }; 141 | }; 142 | volatile uint16_t WP0; 143 | volatile uint16_t WP1; 144 | volatile uint16_t WP2; 145 | volatile uint16_t WP3; 146 | }; 147 | 148 | static struct gd32vf103_fmc *const FMC = (struct gd32vf103_fmc *)FMC_BASE; 149 | static struct gd32vf103_ob *const OB = (struct gd32vf103_ob *)OB_BASE; 150 | 151 | #endif 152 | #endif 153 | -------------------------------------------------------------------------------- /include/gd32vf103/fwdgt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_FWDGT_H 29 | #define GD32VF103_FWDGT_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define FWDGT_BASE _AC(0x40003000,UL) /*!< FWDGT base address */ 34 | 35 | /* register definitions */ 36 | #define FWDGT_CTL _AC(0x00,UL) /*!< FWDGT control register */ 37 | #define FWDGT_PSC _AC(0x04,UL) /*!< FWDGT prescaler register */ 38 | #define FWDGT_RLD _AC(0x08,UL) /*!< FWDGT reload register */ 39 | #define FWDGT_STAT _AC(0x0C,UL) /*!< FWDGT status register */ 40 | 41 | /* FWDGT_CTL */ 42 | #define FWDGT_CTL_CMD_Pos 0 43 | #define FWDGT_CTL_CMD_Msk _AC(0x0000ffff,U) /*!< FWDGT command value */ 44 | #define FWDGT_CTL_CMD(x) ((x) << FWDGT_CTL_CMD_Pos) 45 | #define FWDGT_CTL_CMD_WP_DISABLE _AC(0x00005555,U) /*!< disable FWDGT_PSC and FWDGT_RLD write protection */ 46 | #define FWDGT_CTL_CMD_WP_ENABLE _AC(0x00000000,U) /*!< enable FWDGT_PSC and FWDGT_RLD write protection */ 47 | #define FWDGT_CTL_CMD_RELOAD _AC(0x0000aaaa,U) /*!< reload watchdog counter */ 48 | #define FWDGT_CTL_CMD_START _AC(0x0000cccc,U) /*!< start watchdog counter */ 49 | 50 | /* FWDGT_PSC */ 51 | #define FWDGT_PSC_PSC_Pos 0 52 | #define FWDGT_PSC_PSC_Msk _AC(0x00000003,U) /*!< FWDGT prescaler divider value */ 53 | #define FWDGT_PSC_PSC(x) ((x) << FWDGT_PSC_PSC_Pos) 54 | #define FWDGT_PSC_PSC_DIV4 FWDGT_PSC_PSC(0) /*!< FWDGT prescaler set to 4 */ 55 | #define FWDGT_PSC_PSC_DIV8 FWDGT_PSC_PSC(1) /*!< FWDGT prescaler set to 8 */ 56 | #define FWDGT_PSC_PSC_DIV16 FWDGT_PSC_PSC(2) /*!< FWDGT prescaler set to 16 */ 57 | #define FWDGT_PSC_PSC_DIV32 FWDGT_PSC_PSC(3) /*!< FWDGT prescaler set to 32 */ 58 | #define FWDGT_PSC_PSC_DIV64 FWDGT_PSC_PSC(4) /*!< FWDGT prescaler set to 64 */ 59 | #define FWDGT_PSC_PSC_DIV128 FWDGT_PSC_PSC(5) /*!< FWDGT prescaler set to 128 */ 60 | #define FWDGT_PSC_PSC_DIV256 FWDGT_PSC_PSC(6) /*!< FWDGT prescaler set to 256 */ 61 | 62 | /* FWDGT_RLD */ 63 | #define FWDGT_RLD_RLD_Pos 0 64 | #define FWDGT_RLD_RLD_Msk _AC(0x00000fff,U) /*!< FWDGT counter reload value */ 65 | #define FWDGT_RLD_RLD(x) ((x) << FWDGT_RLD_RLD_Pos) 66 | 67 | /* FWDGT_STAT */ 68 | #define FWDGT_STAT_RUD _BIT(1,U) /*!< FWDGT counter reload value update */ 69 | #define FWDGT_STAT_PUD _BIT(0,U) /*!< FWDGT prescaler divider value update */ 70 | 71 | #ifndef __ASSEMBLER__ 72 | #include 73 | 74 | struct gd32vf103_fwdgt { 75 | volatile uint32_t CTL; 76 | volatile uint32_t PSC; 77 | volatile uint32_t RLD; 78 | volatile uint32_t STAT; 79 | }; 80 | 81 | static struct gd32vf103_fwdgt *const FWDGT = (struct gd32vf103_fwdgt *)FWDGT_BASE; 82 | 83 | #endif 84 | #endif 85 | -------------------------------------------------------------------------------- /include/gd32vf103/i2c.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_I2C_H 29 | #define GD32VF103_I2C_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define I2C0_BASE _AC(0x40005400,UL) /*!< I2C0 base address */ 34 | #define I2C1_BASE _AC(0x40005800,UL) /*!< I2C1 base address */ 35 | 36 | /* register definitions */ 37 | #define I2C_CTL0 _AC(0x00,UL) /*!< I2C control register 0 */ 38 | #define I2C_CTL1 _AC(0x04,UL) /*!< I2C control register 1 */ 39 | #define I2C_SADDR0 _AC(0x08,UL) /*!< I2C slave address register 0*/ 40 | #define I2C_SADDR1 _AC(0x0C,UL) /*!< I2C slave address register */ 41 | #define I2C_DATA _AC(0x10,UL) /*!< I2C transfer buffer register */ 42 | #define I2C_STAT0 _AC(0x14,UL) /*!< I2C transfer status register 0 */ 43 | #define I2C_STAT1 _AC(0x18,UL) /*!< I2C transfer status register */ 44 | #define I2C_CKCFG _AC(0x1C,UL) /*!< I2C clock configure register */ 45 | #define I2C_RT _AC(0x20,UL) /*!< I2C rise time register */ 46 | 47 | /* I2C_CTL0 */ 48 | #define I2C_CTL0_SRESET _BIT(15,U) /*!< software reset */ 49 | #define I2C_CTL0_SALT _BIT(13,U) /*!< SMBus alert */ 50 | #define I2C_CTL0_PECTRANS _BIT(12,U) /*!< packet error checking */ 51 | #define I2C_CTL0_POAP _BIT(11,U) /*!< acknowledge/PEC position (for data reception) */ 52 | #define I2C_CTL0_ACKEN _BIT(10,U) /*!< acknowledge enable */ 53 | #define I2C_CTL0_STOP _BIT(9,U) /*!< stop generation */ 54 | #define I2C_CTL0_START _BIT(8,U) /*!< start generation */ 55 | #define I2C_CTL0_SS _BIT(7,U) /*!< clock stretching disable (slave mode) */ 56 | #define I2C_CTL0_GCEN _BIT(6,U) /*!< general call enable */ 57 | #define I2C_CTL0_PECEN _BIT(5,U) /*!< PEC enable */ 58 | #define I2C_CTL0_ARPEN _BIT(4,U) /*!< ARP enable */ 59 | #define I2C_CTL0_SMBSEL _BIT(3,U) /*!< SMBus type */ 60 | #define I2C_CTL0_SMBEN _BIT(1,U) /*!< SMBus mode */ 61 | #define I2C_CTL0_I2CEN _BIT(0,U) /*!< peripheral enable */ 62 | 63 | /* I2C_CTL1 */ 64 | #define I2C_CTL1_DMALST _BIT(12,U) /*!< DMA last transfer */ 65 | #define I2C_CTL1_DMAON _BIT(11,U) /*!< DMA requests enable */ 66 | #define I2C_CTL1_BUFIE _BIT(10,U) /*!< buffer interrupt enable */ 67 | #define I2C_CTL1_EVIE _BIT(9,U) /*!< event interrupt enable */ 68 | #define I2C_CTL1_ERRIE _BIT(8,U) /*!< error interrupt enable */ 69 | #define I2C_CTL1_I2CCLK_Pos 0 70 | #define I2C_CTL1_I2CCLK_Msk _AC(0x0000003f,U) /*!< I2CCLK[5:0] bits (peripheral clock frequency) */ 71 | #define I2C_CTL1_I2CCLK(x) ((x) << I2C_CTL1_I2CCLK_Pos) 72 | 73 | /* I2C_SADDR0 */ 74 | #define I2C_SADDR0_ADDFORMAT _BIT(15,U) /*!< address mode for the I2C slave */ 75 | #define I2C_SADDR0_ADDRESS_10B_Pos 0 76 | #define I2C_SADDR0_ADDRESS_10B_Msk _AC(0x000003ff,U) /*!< 10-bit address */ 77 | #define I2C_SADDR0_ADDRESS_10B(x) ((x) << I2C_SADDR0_ADDRESS_Pos) 78 | #define I2C_SADDR0_ADDRESS_Pos 1 79 | #define I2C_SADDR0_ADDRESS_Msk _AC(0x000000fe,U) /*!< 7-bit address */ 80 | #define I2C_SADDR0_ADDRESS(x) ((x) << I2C_SADDR0_ADDRESS_Pos) 81 | 82 | /* I2C_SADDR1 */ 83 | #define I2C_SADDR1_ADDRESS2_Pos 1 84 | #define I2C_SADDR1_ADDRESS2_Msk _AC(0x000000fe,U) /*!< second I2C address for the slave in dual-address mode */ 85 | #define I2C_SADDR1_ADDRESS2(x) ((x) << I2C_SADDR1_ADDRESS2_Pos) 86 | #define I2C_SADDR1_DUADEN _BIT(0,U) /*!< aual-address mode switch */ 87 | 88 | /* I2C_DATA */ 89 | #define I2C_DATA_TRB_Pos 0 90 | #define I2C_DATA_TRB_Msk _AC(0x000000ff,U) /*!< 8-bit data register */ 91 | #define I2C_DATA_TRB(x) ((x) << I2C_DATA_TRB_Pos) 92 | 93 | /* I2C_STAT0 */ 94 | #define I2C_STAT0_SMBALT _BIT(15,U) /*!< SMBus alert status */ 95 | #define I2C_STAT0_SMBTO _BIT(14,U) /*!< timeout signal in SMBus mode */ 96 | #define I2C_STAT0_PECERR _BIT(12,U) /*!< PEC error in reception */ 97 | #define I2C_STAT0_OUERR _BIT(11,U) /*!< overrun/underrun */ 98 | #define I2C_STAT0_AERR _BIT(10,U) /*!< acknowledge failure */ 99 | #define I2C_STAT0_LOSTARB _BIT(9,U) /*!< arbitration lost (master mode) */ 100 | #define I2C_STAT0_BERR _BIT(8,U) /*!< bus error */ 101 | #define I2C_STAT0_TBE _BIT(7,U) /*!< data register empty (transmitters) */ 102 | #define I2C_STAT0_RBNE _BIT(6,U) /*!< data register not empty (receivers) */ 103 | #define I2C_STAT0_STPDET _BIT(4,U) /*!< stop detection (slave mode) */ 104 | #define I2C_STAT0_ADD10SEND _BIT(3,U) /*!< 10-bit header sent (master mode) */ 105 | #define I2C_STAT0_BTC _BIT(2,U) /*!< byte transfer finished */ 106 | #define I2C_STAT0_ADDSEND _BIT(1,U) /*!< address sent (master mode)/matched (slave mode) */ 107 | #define I2C_STAT0_SBSEND _BIT(0,U) /*!< start bit (master mode) */ 108 | 109 | /* I2C_STAT1 */ 110 | #define I2C_STAT1_PECV_Pos 8 111 | #define I2C_STAT1_PECV_Msk _AC(0x0000ff00,U) /*!< packet error checking value */ 112 | #define I2C_STAT1_PECV(x) ((x) << I2C_STAT1_PECV_Pos) 113 | #define I2C_STAT1_DUMODF _BIT(7,U) /*!< dual flag (slave mode) */ 114 | #define I2C_STAT1_HSTSMB _BIT(6,U) /*!< SMBus host header (slave mode) */ 115 | #define I2C_STAT1_DEFSMB _BIT(5,U) /*!< SMBus device default address (slave mode) */ 116 | #define I2C_STAT1_RXGC _BIT(4,U) /*!< general call address (slave mode) */ 117 | #define I2C_STAT1_TR _BIT(2,U) /*!< transmitter/receiver */ 118 | #define I2C_STAT1_I2CBSY _BIT(1,U) /*!< bus busy */ 119 | #define I2C_STAT1_MASTER _BIT(0,U) /*!< master/slave */ 120 | 121 | /* I2C_CKCFG */ 122 | #define I2C_CKCFG_FAST _BIT(15,U) /*!< I2C speed selection in master mode */ 123 | #define I2C_CKCFG_DTCY _BIT(14,U) /*!< fast mode duty cycle */ 124 | #define I2C_CKCFG_CLKC_Pos 0 125 | #define I2C_CKCFG_CLKC_Msk _AC(0x00000fff,U) /*!< clock control register in fast/standard mode (master mode) */ 126 | #define I2C_CKCFG_CLKC(x) ((x) << I2C_CKCFG_CLKC_Pos) 127 | 128 | /* I2C_RT */ 129 | #define I2C_RT_RISETIME_Pos 0 130 | #define I2C_RT_RISETIME_Msk _AC(0x0000003f,U) /*!< maximum rise time in fast/standard mode (Master mode) */ 131 | #define I2C_RT_RISETIME(x) ((x) << I2C_RT_RISETIME_Pos) 132 | 133 | #ifndef __ASSEMBLER__ 134 | #include 135 | 136 | struct gd32vf103_i2c { 137 | volatile uint32_t CTL0; 138 | volatile uint32_t CTL1; 139 | volatile uint32_t SADDR0; 140 | volatile uint32_t SADDR1; 141 | volatile uint32_t DATA; 142 | volatile uint32_t STAT0; 143 | volatile uint32_t STAT1; 144 | volatile uint32_t CKCFG; 145 | volatile uint32_t RT; 146 | }; 147 | 148 | static struct gd32vf103_i2c *const I2C0 = (struct gd32vf103_i2c *)I2C0_BASE; 149 | static struct gd32vf103_i2c *const I2C1 = (struct gd32vf103_i2c *)I2C1_BASE; 150 | 151 | #endif 152 | #endif 153 | -------------------------------------------------------------------------------- /include/gd32vf103/info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef GD32VF103_INFO_H 28 | #define GD32VF103_INFO_H 29 | 30 | #include "gd32vf103.h" 31 | 32 | #define INFO_BASE _AC(0x1FFFF7E0,UL) /*!< flash size, sram size and id */ 33 | 34 | /* register definitions */ 35 | #define INFO_FLASH _AC(0x00,UL) /*!< flash size in kilobytes */ 36 | #define INFO_SRAM _AC(0x02,UL) /*!< sram size in kilobytes */ 37 | #define INFO_ID0 _AC(0x08,UL) /*!< UNIQUE_ID bits 31:0 */ 38 | #define INFO_ID1 _AC(0x0c,UL) /*!< UNIQUE_ID bits 63:32 */ 39 | #define INFO_ID2 _AC(0x10,UL) /*!< UNIQUE_ID bits 95:64 */ 40 | 41 | #ifndef __ASSEMBLER__ 42 | #include 43 | 44 | struct gd32vf103_info { 45 | const uint16_t FLASH; 46 | const uint16_t SRAM; 47 | uint32_t reserved1; 48 | const uint32_t ID[3]; 49 | }; 50 | 51 | static struct gd32vf103_info *const INFO = (struct gd32vf103_info *)INFO_BASE; 52 | 53 | #endif 54 | #endif 55 | -------------------------------------------------------------------------------- /include/gd32vf103/mtimer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef GD32VF103_MTIMER_H 28 | #define GD32VF103_MTIMER_H 29 | 30 | #include "gd32vf103.h" 31 | 32 | /* in the gd32vf103 the mtime timer increments at coreclk/4 */ 33 | #ifdef CORECLOCK 34 | #define MTIMER_FREQ (CORECLOCK/4) 35 | #endif 36 | 37 | #define MTIMER_BASE _AC(0xD1000000,UL) /*!< MTIMER base address */ 38 | 39 | /* register definitions */ 40 | #define MTIMER_MTIME _AC(0x000,UL) 41 | #define MTIMER_MTIME_LO _AC(0x000,UL) 42 | #define MTIMER_MTIME_HI _AC(0x004,UL) 43 | #define MTIMER_MTIMECMP _AC(0x008,UL) 44 | #define MTIMER_MTIMECMP_LO _AC(0x008,UL) 45 | #define MTIMER_MTIMECMP_HO _AC(0x00c,UL) 46 | #define MTIMER_MSTOP _AC(0xff8,UL) 47 | #define MTIMER_MSIP _AC(0xffc,UL) 48 | 49 | /* mstop bits */ 50 | #define MTIMER_MSTOP_TIMESTOP _BIT(0,U) 51 | 52 | /* msip bits */ 53 | #define MTIMER_MSIP_MSIP _BIT(0,U) 54 | 55 | #ifndef __ASSEMBLER__ 56 | #include 57 | 58 | struct gd32vf103_mtimer { 59 | volatile uint32_t mtime_lo; 60 | volatile uint32_t mtime_hi; 61 | volatile uint32_t mtimecmp_lo; 62 | volatile uint32_t mtimecmp_hi; 63 | uint32_t reserved1[1018]; 64 | volatile uint32_t mstop; 65 | volatile uint32_t msip; 66 | }; 67 | 68 | static struct gd32vf103_mtimer *const MTIMER = (struct gd32vf103_mtimer *)MTIMER_BASE; 69 | 70 | #endif 71 | #endif 72 | -------------------------------------------------------------------------------- /include/gd32vf103/pmu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_PMU_H 29 | #define GD32VF103_PMU_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define PMU_BASE _AC(0x40007000,UL) /*!< PMU base address */ 34 | 35 | /* register definitions */ 36 | #define PMU_CTL _AC(0x00,UL) /*!< PMU control register */ 37 | #define PMU_CS _AC(0x04,UL) /*!< PMU control and status register */ 38 | 39 | /* PMU_CTL */ 40 | #define PMU_CTL_BKPWEN _BIT(8,U) /*!< backup domain write enable */ 41 | #define PMU_CTL_LVDT_Pos 5 42 | #define PMU_CTL_LVDT_Msk _AC(0x000000e0,U) /*!< low voltage detector threshold */ 43 | #define PMU_CTL_LVDT(x) ((x) << PMU_CTL_LVDT_Pos) 44 | #define PMU_CTL_LVDT_2_2V PMU_CTL_LVDT(0) /*!< voltage threshold is 2.2V */ 45 | #define PMU_CTL_LVDT_2_3V PMU_CTL_LVDT(1) /*!< voltage threshold is 2.3V */ 46 | #define PMU_CTL_LVDT_2_4V PMU_CTL_LVDT(2) /*!< voltage threshold is 2.4V */ 47 | #define PMU_CTL_LVDT_2_5V PMU_CTL_LVDT(3) /*!< voltage threshold is 2.5V */ 48 | #define PMU_CTL_LVDT_2_6V PMU_CTL_LVDT(4) /*!< voltage threshold is 2.6V */ 49 | #define PMU_CTL_LVDT_2_7V PMU_CTL_LVDT(5) /*!< voltage threshold is 2.7V */ 50 | #define PMU_CTL_LVDT_2_8V PMU_CTL_LVDT(6) /*!< voltage threshold is 2.8V */ 51 | #define PMU_CTL_LVDT_2_9V PMU_CTL_LVDT(7) /*!< voltage threshold is 2.9V */ 52 | #define PMU_CTL_LVDEN _BIT(4,U) /*!< low voltage detector enable */ 53 | #define PMU_CTL_STBRST _BIT(3,U) /*!< standby flag reset */ 54 | #define PMU_CTL_WURST _BIT(2,U) /*!< wakeup flag reset */ 55 | #define PMU_CTL_STBMOD _BIT(1,U) /*!< standby mode */ 56 | #define PMU_CTL_LDOLP _BIT(0,U) /*!< LDO low power mode */ 57 | 58 | /* PMU_CS */ 59 | #define PMU_CS_WUPEN _BIT(8,U) /*!< wakeup pin enable */ 60 | #define PMU_CS_LVDF _BIT(2,U) /*!< low voltage detector status flag */ 61 | #define PMU_CS_STBF _BIT(1,U) /*!< standby flag */ 62 | #define PMU_CS_WUF _BIT(0,U) /*!< wakeup flag */ 63 | 64 | #ifndef __ASSEMBLER__ 65 | #include 66 | 67 | struct gd32vf103_pmu { 68 | volatile uint32_t CTL; 69 | volatile uint32_t CS; 70 | }; 71 | 72 | static struct gd32vf103_pmu *const PMU = (struct gd32vf103_pmu *)PMU_BASE; 73 | 74 | #endif 75 | #endif 76 | -------------------------------------------------------------------------------- /include/gd32vf103/rtc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_RTC_H 29 | #define GD32VF103_RTC_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define RTC_BASE _AC(0x40002800,UL) /*!< RTC base address */ 34 | 35 | /* register definitions */ 36 | #define RTC_INTEN _AC(0x00,UL) /*!< interrupt enable register */ 37 | #define RTC_CTL _AC(0x04,UL) /*!< control register */ 38 | #define RTC_PSCH _AC(0x08,UL) /*!< prescaler high register */ 39 | #define RTC_PSCL _AC(0x0C,UL) /*!< prescaler low register */ 40 | #define RTC_DIVH _AC(0x10,UL) /*!< divider high register */ 41 | #define RTC_DIVL _AC(0x14,UL) /*!< divider low register */ 42 | #define RTC_CNTH _AC(0x18,UL) /*!< counter high register */ 43 | #define RTC_CNTL _AC(0x1C,UL) /*!< counter low register */ 44 | #define RTC_ALRMH _AC(0x20,UL) /*!< alarm high register */ 45 | #define RTC_ALRML _AC(0x24,UL) /*!< alarm low register */ 46 | 47 | /* RTC_INTEN */ 48 | #define RTC_INTEN_OVIE _BIT(2,U) /*!< overflow interrupt enable */ 49 | #define RTC_INTEN_ALRMIE _BIT(1,U) /*!< alarm interrupt enable */ 50 | #define RTC_INTEN_SCIE _BIT(0,U) /*!< second interrupt enable */ 51 | 52 | /* RTC_CTL */ 53 | #define RTC_CTL_LWOFF _BIT(5,U) /*!< last write operation finished flag */ 54 | #define RTC_CTL_CMF _BIT(4,U) /*!< configuration mode flag */ 55 | #define RTC_CTL_RSYNF _BIT(3,U) /*!< registers synchronized flag */ 56 | #define RTC_CTL_OVIF _BIT(2,U) /*!< overflow interrupt flag */ 57 | #define RTC_CTL_ALRMIF _BIT(1,U) /*!< alarm interrupt flag */ 58 | #define RTC_CTL_SCIF _BIT(0,U) /*!< second interrupt flag */ 59 | 60 | /* RTC_PSCH */ 61 | #define RTC_PSCH_PSC_Pos 0 62 | #define RTC_PSCH_PSC_Msk _AC(0x0000000f,U) /*!< prescaler high value */ 63 | #define RTC_PSCH_PSC(x) ((x) << RTC_PSCH_PSC_Pos) 64 | 65 | /* RTC_PSCL */ 66 | #define RTC_PSCL_PSC_Pos 0 67 | #define RTC_PSCL_PSC_Msk _AC(0x0000ffff,U) /*!< prescaler low value */ 68 | #define RTC_PSCL_PSC(x) ((x) << RTC_PSCL_PSC_Pos) 69 | 70 | /* RTC_DIVH */ 71 | #define RTC_DIVH_DIV_Pos 0 72 | #define RTC_DIVH_DIV_Msk _AC(0x0000000f,U) /*!< divider high value */ 73 | 74 | /* RTC_DIVL */ 75 | #define RTC_DIVL_DIV_Pos 0 76 | #define RTC_DIVL_DIV_Msk _AC(0x0000ffff,U) /*!< divider low value */ 77 | #define RTC_DIVL_DIV(x) ((x) << RTC_DIVL_DIV_Pos) 78 | 79 | /* RTC_CNTH */ 80 | #define RTC_CNTH_CNT_Pos 0 81 | #define RTC_CNTH_CNT_Msk _AC(0x0000ffff,U) /*!< counter high value */ 82 | #define RTC_CNTH_CNT(x) ((x) << RTC_CNTH_CNT_Pos) 83 | 84 | /* RTC_CNTL */ 85 | #define RTC_CNTL_CNT_Pos 0 86 | #define RTC_CNTL_CNT_Msk _AC(0x0000ffff,U) /*!< counter low value */ 87 | #define RTC_CNTL_CNT(x) ((x) << RTC_CNTL_CNT_Pos) 88 | 89 | /* RTC_ALRMH */ 90 | #define RTC_ALRMH_ALRM_Pos 0 91 | #define RTC_ALRMH_ALRM_Msk _AC(0x0000ffff,U) /*!< alarm high value */ 92 | #define RTC_ALRMH_ALRM(x) ((x) << RTC_ALRMH_ALRM_Pos) 93 | 94 | /* RTC_ALRML */ 95 | #define RTC_ALRML_ALRM_Pos 0 96 | #define RTC_ALRML_ALRM_Msk _AC(0x0000ffff,U) /*!< alarm low value */ 97 | #define RTC_ALRML_ALRM(x) ((x) << RTC_ALRML_ALRM_Pos) 98 | 99 | #ifndef __ASSEMBLER__ 100 | #include 101 | 102 | struct gd32vf103_rtc { 103 | volatile uint32_t INTEN; 104 | volatile uint32_t RTL; 105 | volatile uint32_t PSCH; 106 | volatile uint32_t PSCL; 107 | volatile uint32_t DIVH; 108 | volatile uint32_t DIVL; 109 | volatile uint32_t CNTH; 110 | volatile uint32_t CNTL; 111 | volatile uint32_t ALRMH; 112 | volatile uint32_t ALRML; 113 | }; 114 | 115 | static struct gd32vf103_rtc *const RTC = (struct gd32vf103_rtc *)RTC_BASE; 116 | 117 | #endif 118 | #endif 119 | -------------------------------------------------------------------------------- /include/gd32vf103/usart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_USART_H 29 | #define GD32VF103_USART_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define USART0_BASE _AC(0x40013800,UL) /*!< USART0 base address */ 34 | #define USART1_BASE _AC(0x40004400,UL) /*!< USART1 base address */ 35 | #define USART2_BASE _AC(0x40004800,UL) /*!< USART2 base address */ 36 | #define UART3_BASE _AC(0x40004C00,UL) /*!< UART3 base address */ 37 | #define UART4_BASE _AC(0x40005000,UL) /*!< UART4 base address */ 38 | 39 | /* register definitions */ 40 | #define USART_STAT _AC(0x00,UL) /*!< USART status register */ 41 | #define USART_DATA _AC(0x04,UL) /*!< USART data register */ 42 | #define USART_BAUD _AC(0x08,UL) /*!< USART baud rate register */ 43 | #define USART_CTL0 _AC(0x0C,UL) /*!< USART control register 0 */ 44 | #define USART_CTL1 _AC(0x10,UL) /*!< USART control register 1 */ 45 | #define USART_CTL2 _AC(0x14,UL) /*!< USART control register 2 */ 46 | #define USART_GP _AC(0x18,UL) /*!< USART guard time and prescaler register */ 47 | 48 | /* USARTx_STAT */ 49 | #define USART_STAT_CTSF _BIT(9,U) /*!< CTS change flag */ 50 | #define USART_STAT_LBDF _BIT(8,U) /*!< LIN break detected flag */ 51 | #define USART_STAT_TBE _BIT(7,U) /*!< transmit data buffer empty */ 52 | #define USART_STAT_TC _BIT(6,U) /*!< transmission complete */ 53 | #define USART_STAT_RBNE _BIT(5,U) /*!< read data buffer not empty */ 54 | #define USART_STAT_IDLEF _BIT(4,U) /*!< IDLE frame detected flag */ 55 | #define USART_STAT_ORERR _BIT(3,U) /*!< overrun error */ 56 | #define USART_STAT_NERR _BIT(2,U) /*!< noise error flag */ 57 | #define USART_STAT_FERR _BIT(1,U) /*!< frame error flag */ 58 | #define USART_STAT_PERR _BIT(0,U) /*!< parity error flag */ 59 | 60 | /* USARTx_DATA */ 61 | #define USART_DATA_DATA_Pos 0 62 | #define USART_DATA_DATA_Msk _AC(0x000000ff,U) /*!< transmit or read data value */ 63 | #define USART_DATA_DATA(x) ((x) << USART_DATA_DATA_Pos) 64 | 65 | /* USARTx_BAUD */ 66 | #define USART_BAUD_INTDIV_Pos 4 67 | #define USART_BAUD_INTDIV_Msk _AC(0x0000fff0,U) /*!< integer part of baud-rate divider */ 68 | #define USART_BAUD_INTDIV(x) ((x) << USART_BAUD_INTDIV_Pos) 69 | #define USART_BAUD_FRADIV_Pos 0 70 | #define USART_BAUD_FRADIV_Msk _AC(0x0000000f,U) /*!< fraction part of baud-rate divider */ 71 | #define USART_BAUD_FRADIV(x) ((x) << USART_BAUD_FRADIV_Pos) 72 | 73 | /* USARTx_CTL0 */ 74 | #define USART_CTL0_UEN _BIT(13,U) /*!< USART enable */ 75 | #define USART_CTL0_WL _BIT(12,U) /*!< word length */ 76 | #define USART_CTL0_WM _BIT(11,U) /*!< wakeup method in mute mode */ 77 | #define USART_CTL0_PCEN _BIT(10,U) /*!< parity check function enable */ 78 | #define USART_CTL0_PM _BIT(9,U) /*!< parity mode */ 79 | #define USART_CTL0_PERRIE _BIT(8,U) /*!< parity error interrupt enable */ 80 | #define USART_CTL0_TBEIE _BIT(7,U) /*!< transmitter buffer empty interrupt enable */ 81 | #define USART_CTL0_TCIE _BIT(6,U) /*!< transmission complete interrupt enable */ 82 | #define USART_CTL0_RBNEIE _BIT(5,U) /*!< read data buffer not empty interrupt and overrun error interrupt enable */ 83 | #define USART_CTL0_IDLEIE _BIT(4,U) /*!< idle line detected interrupt enable */ 84 | #define USART_CTL0_TEN _BIT(3,U) /*!< transmitter enable */ 85 | #define USART_CTL0_REN _BIT(2,U) /*!< receiver enable */ 86 | #define USART_CTL0_RWU _BIT(1,U) /*!< receiver wakeup from mute mode */ 87 | #define USART_CTL0_SBKCMD _BIT(0,U) /*!< send break command */ 88 | 89 | /* USARTx_CTL1 */ 90 | #define USART_CTL1_LMEN _BIT(14,U) /*!< LIN mode enable */ 91 | #define USART_CTL1_STB_Pos 12 92 | #define USART_CTL1_STB_Msk _AC(0x00000c00,U) /*!< STOP bits length */ 93 | #define USART_CTL1_STB(x) ((x) << USART_CTL1_STB_Pos) 94 | #define USART_CTL1_STB_1 USART_CTL1_STB(0) /*!< 1 bit */ 95 | #define USART_CTL1_STB_0_5 USART_CTL1_STB(1) /*!< 0.5 bit */ 96 | #define USART_CTL1_STB_2 USART_CTL1_STB(2) /*!< 2 bits */ 97 | #define USART_CTL1_STB_1_5 USART_CTL1_STB(3) /*!< 1.5 bits */ 98 | #define USART_CTL1_CKEN _BIT(11,U) /*!< CK pin enable */ 99 | #define USART_CTL1_CPL _BIT(10,U) /*!< CK polarity */ 100 | #define USART_CTL1_CPH _BIT(9,U) /*!< CK phase */ 101 | #define USART_CTL1_CLEN _BIT(8,U) /*!< CK length */ 102 | #define USART_CTL1_LBDIE _BIT(6,U) /*!< LIN break detected interrupt eanble */ 103 | #define USART_CTL1_LBLEN _BIT(5,U) /*!< LIN break frame length */ 104 | #define USART_CTL1_ADDR_Pos 0 105 | #define USART_CTL1_ADDR_Msk _AC(0x0000000f,U) /*!< address of USART */ 106 | #define USART_CTL1_ADDR(x) ((x) << USART_CTL1_ADDR_Pos) 107 | 108 | /* USARTx_CTL2 */ 109 | #define USART_CTL2_CTSIE _BIT(10,U) /*!< CTS interrupt enable */ 110 | #define USART_CTL2_CTSEN _BIT(9,U) /*!< CTS enable */ 111 | #define USART_CTL2_RTSEN _BIT(8,U) /*!< RTS enable */ 112 | #define USART_CTL2_DENT _BIT(7,U) /*!< DMA request enable for transmission */ 113 | #define USART_CTL2_DENR _BIT(6,U) /*!< DMA request enable for reception */ 114 | #define USART_CTL2_SCEN _BIT(5,U) /*!< smartcard mode enable */ 115 | #define USART_CTL2_NKEN _BIT(4,U) /*!< NACK enable in smartcard mode */ 116 | #define USART_CTL2_HDEN _BIT(3,U) /*!< half-duplex enable */ 117 | #define USART_CTL2_IRLP _BIT(2,U) /*!< IrDA low-power */ 118 | #define USART_CTL2_IREN _BIT(1,U) /*!< IrDA mode enable */ 119 | #define USART_CTL2_ERRIE _BIT(0,U) /*!< error interrupt enable */ 120 | 121 | /* USARTx_GP */ 122 | #define USART_GP_GUAT_Pos 8 123 | #define USART_GP_GUAT_Msk _AC(0x0000ff00,U) /*!< guard time value in smartcard mode */ 124 | #define USART_GP_GUAT(x) ((x) << USART_GP_GUAT_Pos) 125 | #define USART_GP_PSC_Pos 0 126 | #define USART_GP_PSC_Msk _AC(0x000000ff,U) /*!< prescaler value for dividing the system clock */ 127 | #define USART_GP_PSC(x) ((x) << USART_GP_PSC_Pos) 128 | 129 | #ifndef __ASSEMBLER__ 130 | #include 131 | 132 | struct gd32vf103_usart { 133 | volatile uint32_t STAT; 134 | volatile uint32_t DATA; 135 | volatile uint32_t BAUD; 136 | volatile uint32_t CTL0; 137 | volatile uint32_t CTL1; 138 | volatile uint32_t CTL2; 139 | volatile uint32_t GP; 140 | }; 141 | 142 | static struct gd32vf103_usart *const USART0 = (struct gd32vf103_usart *)USART0_BASE; 143 | static struct gd32vf103_usart *const USART1 = (struct gd32vf103_usart *)USART1_BASE; 144 | static struct gd32vf103_usart *const USART2 = (struct gd32vf103_usart *)USART2_BASE; 145 | static struct gd32vf103_usart *const UART3 = (struct gd32vf103_usart *)UART3_BASE; 146 | static struct gd32vf103_usart *const UART4 = (struct gd32vf103_usart *)UART4_BASE; 147 | 148 | #endif 149 | #endif 150 | -------------------------------------------------------------------------------- /include/gd32vf103/wwdgt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, GigaDevice Semiconductor Inc. 3 | * Copyright (c) 2019, Emil Renner Berthing 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 3. Neither the name of the copyright holder nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software without 15 | * specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 26 | * OF SUCH DAMAGE. 27 | */ 28 | #ifndef GD32VF103_WWDGT_H 29 | #define GD32VF103_WWDGT_H 30 | 31 | #include "gd32vf103.h" 32 | 33 | #define WWDGT_BASE _AC(0x40002C00,UL) /*!< WWDGT base address */ 34 | 35 | /* register definitions */ 36 | #define WWDGT_CTL _AC(0x00,UL) /*!< WWDGT control register */ 37 | #define WWDGT_CFG _AC(0x04,UL) /*!< WWDGT configuration register */ 38 | #define WWDGT_STAT _AC(0x08,UL) /*!< WWDGT status register */ 39 | 40 | /* WWDGT_CTL */ 41 | #define WWDGT_CTL_WDGTEN _BIT(7,U) /*!< WWDGT counter enable */ 42 | #define WWDGT_CTL_CNT_Pos 0 43 | #define WWDGT_CTL_CNT_Msk _AC(0x0000007f,U) /*!< WWDGT counter value */ 44 | #define WWDGT_CTL_CNT(x) ((x) << WWDGT_CTL_CNT_Pos) 45 | 46 | /* WWDGT_CFG */ 47 | #define WWDGT_CFG_EWIE _BIT(9,U) /*!< early wakeup interrupt enable */ 48 | #define WWDGT_CFG_PSC_Pos 7 49 | #define WWDGT_CFG_PSC_Msk _AC(0x00000180,U) /*!< WWDGT prescaler divider value */ 50 | #define WWDGT_CFG_PSC(x) ((x) << WWDGT_CFG_PSC_Pos) 51 | #define WWDGT_CFG_PSC_DIV1 WWDGT_CFG_PSC(0) /*!< the time base of WWDGT = (PCLK1/4096)/1 */ 52 | #define WWDGT_CFG_PSC_DIV2 WWDGT_CFG_PSC(1) /*!< the time base of WWDGT = (PCLK1/4096)/2 */ 53 | #define WWDGT_CFG_PSC_DIV4 WWDGT_CFG_PSC(2) /*!< the time base of WWDGT = (PCLK1/4096)/4 */ 54 | #define WWDGT_CFG_PSC_DIV8 WWDGT_CFG_PSC(3) /*!< the time base of WWDGT = (PCLK1/4096)/8 */ 55 | #define WWDGT_CFG_WIN_Pos 0 56 | #define WWDGT_CFG_WIN_Msk _AC(0x0000007f,U) /*!< WWDGT counter window value */ 57 | #define WWDGT_CFG_WIN(x) ((x) << WWDGT_CFG_WIN_Pos) 58 | 59 | /* WWDGT_STAT */ 60 | #define WWDGT_STAT_EWIF _BIT(0,U) /*!< early wakeup interrupt flag */ 61 | 62 | #ifndef __ASSEMBLER__ 63 | #include 64 | 65 | struct gd32vf103_wwdgt { 66 | volatile uint32_t CTL; 67 | volatile uint32_t CFG; 68 | volatile uint32_t STAT; 69 | }; 70 | 71 | static struct gd32vf103_wwdgt *const WWDGT = (struct gd32vf103_wwdgt *)WWDGT_BASE; 72 | 73 | #endif 74 | #endif 75 | -------------------------------------------------------------------------------- /include/lib/eclic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef LIB_ECLIB_H 28 | #define LIB_ECLIB_H 29 | 30 | #include 31 | 32 | #include "riscv/csr.h" 33 | #include "gd32vf103/eclic.h" 34 | 35 | extern __attribute__((aligned(512))) 36 | unsigned long vector_base[ECLIC_NUM_INTERRUPTS]; 37 | 38 | #ifdef __interrupt 39 | __interrupt 40 | #endif 41 | __attribute__((aligned(64))) 42 | __attribute__((section(".text.unlikely.trap_entry"))) 43 | void trap_entry(void); 44 | 45 | #ifdef __interrupt 46 | __interrupt 47 | #endif 48 | __attribute__((aligned(4))) 49 | __attribute__((section(".text.unlikely.irq_entry"))) 50 | void irq_entry(void); 51 | 52 | static inline void eclic_enable(unsigned int irq) 53 | { 54 | ECLIC->clicint[irq].ie = ECLIC_IE_IE; 55 | } 56 | 57 | static inline void eclic_disable(unsigned int irq) 58 | { 59 | ECLIC->clicint[irq].ie = 0; 60 | } 61 | 62 | static inline void eclic_pending_set(unsigned int irq) 63 | { 64 | ECLIC->clicint[irq].ip = ECLIC_IP_IP; 65 | } 66 | 67 | static inline void eclic_pending_clear(unsigned int irq) 68 | { 69 | ECLIC->clicint[irq].ip = 0; 70 | } 71 | 72 | static inline void eclic_global_interrupt_enable(void) 73 | { 74 | csr_set(CSR_MSTATUS, CSR_MSTATUS_MIE); 75 | } 76 | 77 | static inline void eclic_global_interrupt_disable(void) 78 | { 79 | csr_clear(CSR_MSTATUS, CSR_MSTATUS_MIE); 80 | } 81 | 82 | static inline unsigned long eclic_global_interrupt_disable_save(void) 83 | { 84 | return csr_read_clear(CSR_MSTATUS, CSR_MSTATUS_MIE); 85 | } 86 | 87 | static inline void eclic_global_interrupt_restore(unsigned long mstatus) 88 | { 89 | csr_write(CSR_MSTATUS, mstatus); 90 | } 91 | 92 | void eclic_init(void); 93 | void eclic_config(unsigned int irq, uint8_t type, uint8_t priority); 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /include/lib/mtimer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef LIB_MTIMER_H 28 | #define LIB_MTIMER_H 29 | 30 | #include 31 | 32 | #include "gd32vf103/mtimer.h" 33 | 34 | static inline uint64_t mtimer_mtime(void) 35 | { 36 | while (1) { 37 | uint32_t hi = MTIMER->mtime_hi; 38 | uint32_t lo = MTIMER->mtime_lo; 39 | if (hi == MTIMER->mtime_hi) 40 | return ((uint64_t)hi << 32) | lo; 41 | } 42 | } 43 | 44 | static inline uint64_t mtimer_mtimecmp(void) 45 | { 46 | uint32_t hi = MTIMER->mtimecmp_hi; 47 | uint32_t lo = MTIMER->mtimecmp_lo; 48 | return ((uint64_t)hi << 32) | lo; 49 | } 50 | 51 | void mtimer_delay(uint32_t ticks); 52 | 53 | #ifdef MTIMER_FREQ 54 | static inline void mtimer_udelay(unsigned int us) 55 | { 56 | mtimer_delay(us * (MTIMER_FREQ/1000000)); 57 | } 58 | #endif 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /include/lib/rcu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef LIB_RCU_H 28 | #define LIB_RCU_H 29 | 30 | #include 31 | 32 | #include "gd32vf103/rcu.h" 33 | 34 | void rcu_sysclk_reset(void); 35 | void rcu_sysclk_pll_irc8m(uint32_t cfg0); 36 | void rcu_sysclk_hxtal(uint32_t cfg0, uint32_t cfg1); 37 | 38 | void rcu_sysclk_init(void); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /include/lib/stdio-uart0.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef LIB_STDIO_UART0_H 28 | #define LIB_STDIO_UART0_H 29 | 30 | #include 31 | #include 32 | 33 | extern const FILE uart0_stream; 34 | static FILE *const uart0 = (FILE *)&uart0_stream; 35 | 36 | void uart0_init(uint32_t tlclk, uint32_t target, uint8_t priority); 37 | int uart0_getchar(void); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/lib/stdio-usbacm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef LIB_STDIO_USBACM_H 28 | #define LIB_STDIO_USBACM_H 29 | 30 | #include 31 | #include 32 | 33 | extern const FILE usbacm_stream; 34 | static FILE *const usbacm = (FILE *)&usbacm_stream; 35 | 36 | void usbacm_init(uint8_t priority); 37 | int usbacm_getchar(void); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/riscv/bits.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef RISCV_BITS_H 28 | #define RISCV_BITS_H 29 | 30 | #define ROUNDUP(a, b) ((((a)-1)/(b)+1)*(b)) 31 | #define ROUNDDOWN(a, b) ((a)/(b)*(b)) 32 | 33 | #ifndef MAX 34 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) 35 | #endif 36 | #ifndef MIN 37 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) 38 | #endif 39 | #ifndef CLAMP 40 | #define CLAMP(a, lo, hi) MIN(MAX(a, lo), hi) 41 | #endif 42 | #ifndef STR 43 | #define STR(x) XSTR(x) 44 | #define XSTR(x) #x 45 | #endif 46 | 47 | #ifdef __ASSEMBLER__ 48 | 49 | #if __riscv_xlen == 64 50 | # define SLL32 sllw 51 | # define STORE sd 52 | # define LOAD ld 53 | # define LWU lwu 54 | # define REGBYTES 8 55 | # define LOG_REGBYTES 3 56 | #elif __riscv_xlen == 32 57 | # define SLL32 sll 58 | # define STORE sw 59 | # define LOAD lw 60 | # define LWU lw 61 | # define REGBYTES 4 62 | # define LOG_REGBYTES 2 63 | #else 64 | #error "Unsupported __riscv_xlen" 65 | #endif 66 | 67 | #else /* C */ 68 | 69 | #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) 70 | #define ARRAY_END(x) (&(x)[ARRAY_SIZE(x)]) 71 | 72 | #define likely(x) __builtin_expect((x), 1) 73 | #define unlikely(x) __builtin_expect((x), 0) 74 | 75 | #if __has_attribute(__naked__) 76 | #define __naked __attribute__((__naked__)) 77 | #endif 78 | 79 | #if __has_attribute(__interrupt__) 80 | #define __interrupt __attribute__((__interrupt__)) 81 | #endif 82 | 83 | #if __has_attribute(__noreturn__) 84 | #define __noreturn __attribute__((__noreturn__)) 85 | #else 86 | #define __noreturn 87 | #endif 88 | 89 | static inline void 90 | wait_for_interrupt(void) 91 | { 92 | __asm__ __volatile__ ("wfi"); 93 | } 94 | 95 | static inline void 96 | nop(void) 97 | { 98 | __asm__ __volatile__ ("nop"); 99 | } 100 | 101 | #endif 102 | #endif 103 | -------------------------------------------------------------------------------- /include/riscv/const.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef RISCV_CONST_H 28 | #define RISCV_CONST_H 29 | 30 | #ifdef __ASSEMBLER__ 31 | #define _AC(X,Y) X 32 | #define _AT(T,X) X 33 | #else 34 | #define _AC(X,Y) (X##Y) 35 | #define _AT(T,X) ((T)(X)) 36 | #endif 37 | 38 | #define _BIT(x,y) (_AC(1,y) << (x)) 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /lib/eclic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include 28 | 29 | #include "gd32vf103/csr.h" 30 | 31 | #include "lib/eclic.h" 32 | 33 | #ifdef ECLIC_NUM_INTERRUPTS 34 | static inline unsigned int eclic_num_interrupts(void) { return ECLIC_NUM_INTERRUPTS; } 35 | #else 36 | static unsigned int eclic_num_interrupts(void) 37 | { 38 | return ECLIC->info & ECLIC_INFO_NUM_INTERRUPT_Msk; 39 | } 40 | #endif 41 | 42 | void eclic_init(void) 43 | { 44 | unsigned int num_irq = eclic_num_interrupts(); 45 | 46 | /* use all available bits as level-bits. ie. 47 | * all interrupts preempt lower level interrupts 48 | */ 49 | ECLIC->cfg = 4 << ECLIC_CFG_NLBITS_Pos; 50 | 51 | /* clear all ie and ip bits for all interrupt sources */ 52 | for (unsigned int i = 0; i < num_irq; i++) { 53 | ECLIC->clicint[i].ie = 0; 54 | ECLIC->clicint[i].ip = 0; 55 | } 56 | 57 | /* clear mth register */ 58 | ECLIC->mth = 0; 59 | 60 | /* set the NMI base to share with mtvec by setting CSR_MMISC_CTL */ 61 | csr_set(CSR_MMISC_CTL, CSR_MMISC_CTL_NMI_CAUSE_FFF); 62 | 63 | /* initialize mtvt */ 64 | csr_write(CSR_MTVT, (unsigned long)&vector_base); 65 | 66 | /* initialize mtvt2 and enable it */ 67 | csr_write(CSR_MTVT2, (unsigned long)&irq_entry + CSR_MTVT2_MTVT2EN); 68 | 69 | /* intialize mtvec for the trap and NMI base addr and enable eclic mode */ 70 | csr_write(CSR_MTVEC, (unsigned long)&trap_entry + CSR_MTVEC_MODE_ECLIC); 71 | } 72 | 73 | void eclic_config(unsigned int irq, uint8_t type, uint8_t priority) 74 | { 75 | ECLIC->clicint[irq].attr = type; 76 | ECLIC->clicint[irq].ctl = ECLIC_CTL_BITS(priority); 77 | } 78 | -------------------------------------------------------------------------------- /lib/gpio.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include "gd32vf103/rcu.h" 28 | #include "lib/gpio.h" 29 | 30 | void gpio_toggle(struct gd32vf103_gpio *port, uint32_t pins) 31 | { 32 | uint32_t current = port->OCTL & pins; 33 | 34 | port->BOP = (current << 16) | (current ^ pins); 35 | } 36 | 37 | void gpio_config(struct gd32vf103_gpio *port, uint32_t pins, enum gpio_mode emode) 38 | { 39 | uint32_t pin = 0x1U; 40 | uint32_t mask; 41 | uint32_t mode; 42 | uint32_t val; 43 | 44 | /* configure the eight low port pins with GPIO_CTL0 */ 45 | mask = 0xfU; 46 | mode = emode; 47 | val = port->CTL0; 48 | for (; mask; pin <<= 1, mask <<= 4, mode <<= 4) { 49 | if (pin & pins) 50 | val = (val & ~mask) | mode; 51 | } 52 | port->CTL0 = val; 53 | 54 | /* configure the eight high port pins with GPIO_CTL1 */ 55 | mask = 0xfU; 56 | mode = emode; 57 | val = port->CTL1; 58 | for (; mask; pin <<= 1, mask <<= 4, mode <<= 4) { 59 | if (pin & pins) 60 | val = (val & ~mask) | mode; 61 | } 62 | port->CTL1 = val; 63 | } 64 | 65 | void gpio_pin_clock_enable(gpio_pin_t pin) 66 | { 67 | RCU->APB2EN |= RCU_APB2EN_PAEN << gpio_pin_port_nr(pin); 68 | } 69 | 70 | void gpio_pin_clock_disable(gpio_pin_t pin) 71 | { 72 | RCU->APB2EN &= ~(RCU_APB2EN_PAEN << gpio_pin_port_nr(pin)); 73 | } 74 | 75 | void gpio_pin_toggle(gpio_pin_t pin) 76 | { 77 | gpio_toggle(gpio_pin_port(pin), 1U << gpio_pin_nr(pin)); 78 | } 79 | 80 | void gpio_pin_config(gpio_pin_t pin, enum gpio_mode emode) 81 | { 82 | uint32_t mode = emode; 83 | #if 0 84 | struct gd32vf103_gpio *port = gpio_pin_port(pin); 85 | unsigned int nr = 4*gpio_pin_nr(pin); 86 | 87 | if (nr < 4*8) { 88 | port->CTL0 = (port->CTL0 & ~(0xfU << nr)) | (mode << nr); 89 | } else { 90 | nr &= 0x1fU; 91 | port->CTL1 = (port->CTL1 & ~(0xfU << nr)) | (mode << nr); 92 | } 93 | #else 94 | volatile uint32_t *reg = &gpio_pin_port(pin)->CTL0; 95 | unsigned int nr = gpio_pin_nr(pin); 96 | 97 | if (nr >= 8) { 98 | nr -= 8; 99 | reg++; 100 | } 101 | nr <<= 2; 102 | *reg = (*reg & ~(0xfU << nr)) | (mode << nr); 103 | #endif 104 | } 105 | 106 | enum gpio_mode gpio_pin_mode(gpio_pin_t pin) 107 | { 108 | struct gd32vf103_gpio *port = gpio_pin_port(pin); 109 | unsigned int nr = 4*gpio_pin_nr(pin); 110 | unsigned int ret; 111 | 112 | if (nr < 4*8) { 113 | ret = port->CTL0 >> nr; 114 | } else { 115 | nr &= 0x1fU; 116 | ret = port->CTL1 >> nr; 117 | } 118 | 119 | return ret & 0xfU; 120 | } 121 | -------------------------------------------------------------------------------- /lib/mtimer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include 28 | 29 | #include "lib/mtimer.h" 30 | 31 | void mtimer_delay(uint32_t ticks) 32 | { 33 | uint32_t zero = MTIMER->mtime_lo; 34 | uint32_t now; 35 | 36 | do { 37 | now = MTIMER->mtime_lo - zero; 38 | } while (now < ticks); 39 | } 40 | -------------------------------------------------------------------------------- /lib/rcu.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include 28 | 29 | #include "lib/rcu.h" 30 | 31 | /* reset the RCC clock configuration to the default reset state */ 32 | void rcu_sysclk_reset(void) 33 | { 34 | /* enable IRC8M */ 35 | RCU->CTL |= RCU_CTL_IRC8MEN; 36 | 37 | /* reset SCS, AHBPSC, APB1PSC, APB2PSC, ADCPSC, CKOUT0SEL bits */ 38 | RCU->CFG0 &= ~(RCU_CFG0_SCS_Msk | 39 | RCU_CFG0_AHBPSC_Msk | 40 | RCU_CFG0_APB1PSC_Msk | 41 | RCU_CFG0_APB2PSC_Msk | 42 | RCU_CFG0_ADCPSC_Msk | 43 | RCU_CFG0_CKOUT0SEL_Msk); 44 | 45 | /* reset HXTALEN, CKMEN, PLLEN bits */ 46 | RCU->CTL &= ~(RCU_CTL_HXTALEN | RCU_CTL_CKMEN | RCU_CTL_PLLEN); 47 | 48 | /* reset HXTALBPS bit */ 49 | RCU->CTL &= ~RCU_CTL_HXTALBPS; 50 | 51 | /* reset PLLSEL, PREDV0_LSB, PLLMF, USBFSPSC bits */ 52 | RCU->CFG0 &= ~(RCU_CFG0_USBFSPSC_Msk | 53 | RCU_CFG0_PLLMF_Msk | 54 | RCU_CFG0_PREDV0_LSB | 55 | RCU_CFG0_PLLSEL); 56 | RCU->CFG1 = 0x00000000U; 57 | 58 | /* reset HXTALEN, CKMEN, PLLEN, PLL1EN and PLL2EN bits */ 59 | RCU->CTL &= ~(RCU_CTL_PLLEN | 60 | RCU_CTL_PLL1EN | 61 | RCU_CTL_PLL2EN | 62 | RCU_CTL_CKMEN | 63 | RCU_CTL_HXTALEN); 64 | 65 | /* disable and clear all interrupts */ 66 | RCU->INT = 0x00ff0000U; 67 | } 68 | 69 | void rcu_sysclk_pll_irc8m(uint32_t cfg0) 70 | { 71 | RCU->CFG0 = (RCU->CFG0 & ~( 72 | RCU_CFG0_PLLMF_Msk | 73 | RCU_CFG0_PLLSEL | 74 | RCU_CFG0_AHBPSC_Msk | 75 | RCU_CFG0_APB2PSC_Msk | 76 | RCU_CFG0_APB1PSC_Msk)) | cfg0; 77 | 78 | /* enable PLL */ 79 | RCU->CTL |= RCU_CTL_PLLEN; 80 | /* wait until PLL is stable */ 81 | while (!(RCU->CTL & RCU_CTL_PLLSTB)) 82 | /* wait */; 83 | 84 | /* select PLL as system clock */ 85 | RCU->CFG0 = (RCU->CFG0 & ~RCU_CFG0_SCS_Msk) | RCU_CFG0_SCS_PLL; 86 | /* wait until PLL is selected as system clock */ 87 | while ((RCU->CFG0 & RCU_CFG0_SCSS_Msk) != RCU_CFG0_SCSS_PLL) 88 | /* wait */; 89 | } 90 | 91 | void rcu_sysclk_hxtal(uint32_t cfg0, uint32_t cfg1) 92 | { 93 | /* enable HXTAL */ 94 | RCU->CTL |= RCU_CTL_HXTALEN; 95 | RCU->CFG0 |= cfg0; 96 | RCU->CFG1 |= cfg1; 97 | 98 | /* wait until HXTAL is stable */ 99 | while (!(RCU->CTL & RCU_CTL_HXTALSTB)) 100 | /* wait */; 101 | 102 | if (cfg1 & RCU_CFG1_PREDV0SEL) { 103 | /* enable PLL1 */ 104 | RCU->CTL |= RCU_CTL_PLL1EN; 105 | /* wait till PLL1 is ready */ 106 | while (!(RCU->CTL & RCU_CTL_PLL1STB)) 107 | /* wait */; 108 | } 109 | 110 | if (cfg0 & RCU_CFG0_PLLSEL) { 111 | /* enable PLL */ 112 | RCU->CTL |= RCU_CTL_PLLEN; 113 | /* wait until PLL is stable */ 114 | while (!(RCU->CTL & RCU_CTL_PLLSTB)) 115 | /* wait */; 116 | 117 | /* select PLL as system clock */ 118 | RCU->CFG0 = (RCU->CFG0 & ~RCU_CFG0_SCS_Msk) | RCU_CFG0_SCS_PLL; 119 | /* wait until PLL is selected as system clock */ 120 | while ((RCU->CFG0 & RCU_CFG0_SCSS_Msk) != RCU_CFG0_SCSS_PLL) 121 | /* wait */; 122 | } else { 123 | /* select HXTAL as system clock */ 124 | RCU->CFG0 = (RCU->CFG0 & ~RCU_CFG0_SCS_Msk) | RCU_CFG0_SCS_HXTAL; 125 | /* wait until HXTAL is selected as system clock */ 126 | while ((RCU->CFG0 & RCU_CFG0_SCSS_Msk) != RCU_CFG0_SCSS_HXTAL) 127 | /* wait */; 128 | } 129 | } 130 | 131 | void rcu_sysclk_init(void) 132 | { 133 | #ifndef HXTAL 134 | #if CORECLOCK == 108000000 135 | rcu_sysclk_pll_irc8m(RCU_CFG0_PLLMF_MUL27 | RCU_CFG0_APB1PSC_DIV2); 136 | #elif CORECLOCK == 96000000 137 | rcu_sysclk_pll_irc8m(RCU_CFG0_PLLMF_MUL24 | RCU_CFG0_APB1PSC_DIV2); 138 | #else 139 | #error "Unsupported CORECLOCK value, please call rcu_sysclk_pll_irc8m() manually." 140 | #endif 141 | #elif HXTAL == 25000000 142 | #if CORECLOCK == 108000000 143 | rcu_sysclk_hxtal(RCU_CFG0_PLLMF_MUL27 | 144 | RCU_CFG0_PLLSEL | 145 | RCU_CFG0_APB1PSC_DIV2, 146 | RCU_CFG1_PLL1MF_MUL8 | 147 | RCU_CFG1_PREDV0SEL | 148 | RCU_CFG1_PREDV1_DIV5 | 149 | RCU_CFG1_PREDV0_DIV10); 150 | #elif CORECLOCK == 96000000 151 | rcu_sysclk_hxtal(RCU_CFG0_PLLMF_MUL24 | 152 | RCU_CFG0_USBFSPSC_DIV2 | 153 | RCU_CFG0_PLLSEL | 154 | RCU_CFG0_APB1PSC_DIV2, 155 | RCU_CFG1_PLL1MF_MUL8 | 156 | RCU_CFG1_PREDV0SEL | 157 | RCU_CFG1_PREDV1_DIV5 | 158 | RCU_CFG1_PREDV0_DIV10); 159 | #else 160 | #error "Unsupported CORECLOCK value, please call rcu_sysclk_hxtal() manually." 161 | #endif 162 | #elif HXTAL == 8000000 163 | #if CORECLOCK == 108000000 164 | rcu_sysclk_hxtal(RCU_CFG0_PLLMF_MUL27 | 165 | RCU_CFG0_PLLSEL | 166 | RCU_CFG0_APB1PSC_DIV2, 167 | RCU_CFG1_PREDV0_DIV2); 168 | #elif CORECLOCK == 96000000 169 | rcu_sysclk_hxtal(RCU_CFG0_PLLMF_MUL24 | 170 | RCU_CFG0_USBFSPSC_DIV2 | 171 | RCU_CFG0_PLLSEL | 172 | RCU_CFG0_APB1PSC_DIV2, 173 | RCU_CFG1_PREDV0_DIV2); 174 | #else 175 | #error "Unsupported CORECLOCK value, please call rcu_sysclk_hxtal() manually." 176 | #endif 177 | #else 178 | #error "Unsupported HXTAL value, please call rcu_sysclk_hxtal() manually." 179 | #endif 180 | } 181 | -------------------------------------------------------------------------------- /lib/stdio-uart0.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | #include 30 | 31 | #include "gd32vf103/rcu.h" 32 | #include "gd32vf103/usart.h" 33 | 34 | #include "lib/eclic.h" 35 | #include "lib/gpio.h" 36 | #include "lib/stdio-uart0.h" 37 | 38 | int uart0_getchar(void) 39 | { 40 | while (!(USART0->STAT & USART_STAT_RBNE)) 41 | /* wait */; 42 | 43 | return USART0->DATA; 44 | } 45 | 46 | #ifdef UART0_ASYNC 47 | static struct { 48 | volatile uint16_t first; 49 | volatile uint16_t last; 50 | char buf[2048]; 51 | } uart0_output; 52 | 53 | void 54 | USART0_IRQHandler(void) 55 | { 56 | unsigned int first = uart0_output.first; 57 | unsigned int last = uart0_output.last; 58 | 59 | while (first != last) { 60 | if (!(USART0->STAT & USART_STAT_TBE)) 61 | goto out; 62 | USART0->DATA = uart0_output.buf[first++]; 63 | first %= ARRAY_SIZE(uart0_output.buf); 64 | } 65 | USART0->CTL0 &= ~USART_CTL0_TBEIE; 66 | out: 67 | uart0_output.first = first; 68 | } 69 | 70 | static void 71 | uart0_putc(FILE *stream, char c) 72 | { 73 | static bool seenr; 74 | unsigned int last = uart0_output.last; 75 | 76 | if (c == '\n' && !seenr) { 77 | uart0_output.buf[last++] = '\r'; 78 | last %= ARRAY_SIZE(uart0_output.buf); 79 | } 80 | 81 | seenr = (c == '\r'); 82 | 83 | uart0_output.buf[last++] = c; 84 | uart0_output.last = last % ARRAY_SIZE(uart0_output.buf); 85 | } 86 | 87 | static int 88 | uart0_done(FILE *stream) 89 | { 90 | USART0->CTL0 |= USART_CTL0_TBEIE; 91 | return 0; 92 | } 93 | #else 94 | static void 95 | uart0_putc(FILE *stream, char c) 96 | { 97 | static bool seenr; 98 | 99 | if (c == '\n' && !seenr) { 100 | while (!(USART0->STAT & USART_STAT_TBE)) 101 | /* wait */; 102 | USART0->DATA = '\r'; 103 | } 104 | 105 | seenr = (c == '\r'); 106 | 107 | while (!(USART0->STAT & USART_STAT_TBE)) 108 | /* wait */; 109 | USART0->DATA = c; 110 | } 111 | 112 | static int uart0_done(FILE *stream) 113 | { 114 | return 0; 115 | } 116 | #endif 117 | 118 | const FILE uart0_stream = { 119 | .putc = uart0_putc, 120 | .done = uart0_done, 121 | }; 122 | 123 | void uart0_init(uint32_t pclk, uint32_t target, uint8_t priority) 124 | { 125 | /* enable GPIO clock */ 126 | RCU->APB2EN |= RCU_APB2EN_PAEN; 127 | /* enable USART clock */ 128 | RCU->APB2EN |= RCU_APB2EN_USART0EN; 129 | 130 | gpio_pin_config(GPIO_PA9, GPIO_MODE_AF_PP_50MHZ); 131 | gpio_pin_config(GPIO_PA10, GPIO_MODE_IN_FLOAT); 132 | 133 | /* reset usart0 */ 134 | RCU->APB2RST |= RCU_APB2RST_USART0RST; 135 | RCU->APB2RST &= ~RCU_APB2RST_USART0RST; 136 | 137 | /* set baudrate */ 138 | USART0->BAUD = (2*pclk + target) / (2*target); 139 | /* set 1 stop bit */ 140 | USART0->CTL1 = USART_CTL1_STB_1; 141 | /* enable rx and tx */ 142 | USART0->CTL0 = USART_CTL0_TEN | USART_CTL0_REN; 143 | /* enable usart0 */ 144 | USART0->CTL0 |= USART_CTL0_UEN; 145 | 146 | #ifdef UART0_ASYNC 147 | eclic_config(USART0_IRQn, ECLIC_ATTR_TRIG_LEVEL, priority); 148 | eclic_enable(USART0_IRQn); 149 | #endif 150 | } 151 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #include "lib/mtimer.h" 28 | #include "lib/eclic.h" 29 | #include "lib/rcu.h" 30 | #include "lib/gpio.h" 31 | 32 | #define BLINK MTIMER_FREQ /* 1 second */ 33 | 34 | #define LED GPIO_PA1 35 | 36 | void MTIMER_IRQHandler(void) 37 | { 38 | uint64_t next; 39 | 40 | gpio_pin_toggle(LED); 41 | 42 | next = mtimer_mtimecmp() + BLINK; 43 | MTIMER->mtimecmp_hi = next >> 32; 44 | MTIMER->mtimecmp_lo = next; 45 | } 46 | 47 | /* if the compiler can't generate functions suitable 48 | * for interrupt handlers, we can't implement this 49 | * function directly in C 50 | */ 51 | #ifdef __interrupt 52 | void trap_entry(void) 53 | { 54 | gpio_pin_clear(LED); 55 | 56 | while (1) 57 | /* forever */; 58 | } 59 | #endif 60 | 61 | static void mtimer_enable(void) 62 | { 63 | uint64_t next = mtimer_mtime() + BLINK; 64 | 65 | MTIMER->mtimecmp_hi = next >> 32; 66 | MTIMER->mtimecmp_lo = next; 67 | 68 | eclic_config(MTIMER_IRQn, ECLIC_ATTR_TRIG_LEVEL, 1); 69 | eclic_enable(MTIMER_IRQn); 70 | } 71 | 72 | int main(void) 73 | { 74 | /* initialize system clock */ 75 | rcu_sysclk_init(); 76 | 77 | /* initialize eclic */ 78 | eclic_init(); 79 | /* enable global interrupts */ 80 | eclic_global_interrupt_enable(); 81 | 82 | /* turn on power to GPIOA */ 83 | RCU->APB2EN |= RCU_APB2EN_PAEN; 84 | 85 | gpio_pin_set(LED); 86 | gpio_pin_config(LED, GPIO_MODE_PP_50MHZ); 87 | 88 | mtimer_enable(); 89 | 90 | while (1) 91 | wait_for_interrupt(); 92 | } 93 | -------------------------------------------------------------------------------- /std/alloca.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef _ALLOCA_H 28 | #define _ALLOCA_H 29 | 30 | #undef alloca 31 | #define alloca(size) __builtin_alloca(size) 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /std/assert.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #undef assert 28 | 29 | #ifdef NDEBUG 30 | #define assert(__e) ((void)0) 31 | #else 32 | #define assert(__e) \ 33 | ((__e) ? (void)0 : __assert_func(__FILE__, __LINE__, __file__, #__e)) 34 | 35 | __attribute__((noreturn)) 36 | void __assert_func(const char *file, int line, const char *func, const char *expr); 37 | #endif 38 | -------------------------------------------------------------------------------- /std/stdio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2020, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef _STDIO_H 28 | #define _STDIO_H 29 | 30 | #include 31 | #include 32 | 33 | typedef struct __file FILE; 34 | 35 | typedef void __putc_t(FILE *stream, char c); 36 | typedef int __done_t(FILE *stream); 37 | 38 | struct __file { 39 | __putc_t *putc; 40 | __done_t *done; 41 | }; 42 | 43 | extern FILE *stdout; 44 | extern FILE *stderr; 45 | 46 | int fputc(int c, FILE *stream); 47 | int fputs(const char *s, FILE *stream); 48 | 49 | int putchar(int c); 50 | int puts(const char *s); 51 | 52 | __attribute__((format(printf, 2, 3))) 53 | int fprintf(FILE *stream, const char *format, ...); 54 | int vfprintf(FILE *stream, const char *format, va_list ap); 55 | 56 | __attribute__((format(printf, 1, 2))) 57 | int printf(const char *restrict, ...); 58 | int vprintf(const char *restrict, va_list ap); 59 | 60 | __attribute__((format(printf, 2, 3))) 61 | int sprintf(char *restrict str, const char *restrict, ...); 62 | int vsprintf(char *restrict str, const char *restrict, va_list ap); 63 | 64 | __attribute__((format(printf, 3, 4))) 65 | int snprintf(char *restrict str, size_t size, const char *restrict, ...); 66 | int vsnprintf(char *restrict str, size_t size, const char *restrict, va_list ap); 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /std/string.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef _STRING_H 28 | #define _STRING_H 29 | 30 | #include 31 | 32 | int memcmp(const void *s1, const void *s2, size_t n); 33 | void *memset(void *s, int c, size_t n); 34 | void *memcpy(void *restrict dest, const void *restrict src, size_t n); 35 | void *memmove(void *dest, const void *src, size_t n); 36 | void *memchr(const void *s, int c, size_t n); 37 | void *memrchr(const void *s, int c, size_t n); 38 | void *rawmemchr(const void *s, int c); 39 | 40 | size_t strlen(const char *s); 41 | size_t strnlen(const char *s, size_t maxlen); 42 | int strcmp(const char *s1, const char *s2); 43 | int strncmp(const char *s1, const char *s2, size_t n); 44 | char *strcpy(char *restrict dest, const char *restrict src); 45 | char *strncpy(char *restrict dest, const char *restrict src, size_t n); 46 | char *stpcpy(char *restrict dest, const char *restrict src); 47 | char *stpncpy(char *restrict dest, const char *restrict src, size_t n); 48 | char *strchr(const char *s, int c); 49 | char *strrchr(const char *s, int c); 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /std/unistd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Emil Renner Berthing 3 | * 4 | * Redistribution and use in source and binary forms, with or without modification, 5 | * are permitted provided that the following conditions are met: 6 | * 7 | * 1. Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, 10 | * this list of conditions and the following disclaimer in the documentation 11 | * and/or other materials provided with the distribution. 12 | * 3. Neither the name of the copyright holder nor the names of its contributors 13 | * may be used to endorse or promote products derived from this software without 14 | * specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 25 | * OF SUCH DAMAGE. 26 | */ 27 | #ifndef _UNISTD_H 28 | #define _UNISTD_H 29 | 30 | #if defined(__SIZEOF_SIZE_T__) && defined(__SIZEOF_INT__) && __SIZEOF_SIZE_T__ == __SIZEOF_INT__ 31 | typedef int ssize_t; 32 | #else 33 | #error Sanity check for ssize_t definition failed 34 | #endif 35 | 36 | #endif 37 | --------------------------------------------------------------------------------