diff --git a/examples/blink/blink.c b/examples/blink/blink.c index 81a2760f328c43977bb161c7b43715d15f696d6b..e65639989d63a04cef910d2ae4fd5b238e15407e 100644 --- a/examples/blink/blink.c +++ b/examples/blink/blink.c @@ -12,29 +12,30 @@ int main() { SystemInit48HSI(); - // Enable GPIOD. + // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; - // GPIO D0 Push-Pull, 10MHz Output + // GPIO D0 Push-Pull GPIOD->CFGLR &= ~(0xf<<(4*0)); GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); - // GPIO D0 Push-Pull, 10MHz Output + // GPIO D4 Push-Pull GPIOD->CFGLR &= ~(0xf<<(4*4)); GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*4); - // GPIO D0 Push-Pull, 10MHz Output - GPIOC->CFGLR &= ~(0xf<<(4*4)); - GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*4); + // GPIO C0 Push-Pull + GPIOC->CFGLR &= ~(0xf<<(4*0)); + GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); while(1) { - GPIOD->BSHR = 1 | (1<<4); // Turn on GPIOD0&D4 - GPIOC->BSHR = 1; // Turn on GPIOC0 + GPIOD->BSHR = 1 | (1<<4); // Turn on GPIOs + GPIOC->BSHR = 1; Delay_Ms( 250 ); - GPIOD->BSHR = (1<<16) | (1<<(16+4)); // Turn off GPIOD0 - GPIOC->BSHR = (1<<16); // Turn off GPIOC0 + GPIOD->BSHR = (1<<16) | (1<<(16+4)); // Turn off GPIODs + GPIOC->BSHR = (1<<16); Delay_Ms( 250 ); count++; } } + diff --git a/examples/bootload/bootload.c b/examples/bootload/bootload.c index 02b2385b74f0562f307f28b882b7dc12138c1fba..a5975940f73027cf267f591b70fca7ff582b05f0 100644 --- a/examples/bootload/bootload.c +++ b/examples/bootload/bootload.c @@ -27,18 +27,18 @@ int main() // From here, you can do whatever you'd like! // This code will live up at 0x1ffff000. - // Enable GPIOD. + // Enable GPIOD + C RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; // GPIO D0 Push-Pull, 10MHz Output GPIOD->CFGLR &= ~(0xf<<(4*0)); GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); - // GPIO D0 Push-Pull, 10MHz Output + // GPIO D4 Push-Pull, 10MHz Output GPIOD->CFGLR &= ~(0xf<<(4*4)); GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*4); - // GPIO D0 Push-Pull, 10MHz Output + // GPIO C0 Push-Pull, 10MHz Output GPIOC->CFGLR &= ~(0xf<<(4*0)); GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*4); diff --git a/examples/debugprintfdemo/debugprintfdemo.c b/examples/debugprintfdemo/debugprintfdemo.c index 576f199d9dd336c06f9236dfbc399f2d9f430cb9..80ed6830718e8c677691c5e3db2d9e5dd65e2d58 100644 --- a/examples/debugprintfdemo/debugprintfdemo.c +++ b/examples/debugprintfdemo/debugprintfdemo.c @@ -11,21 +11,30 @@ int main() SystemInit48HSI(); SetupDebugPrintf(); - // Enable GPIOD. - RCC->APB2PCENR |= RCC_APB2Periph_GPIOD; + // Enable GPIOs + RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; - // GPIO D0, D4 Push-Pull, 10MHz Output + // GPIO D0 Push-Pull GPIOD->CFGLR &= ~(0xf<<(4*0)); GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); + // GPIO D4 Push-Pull GPIOD->CFGLR &= ~(0xf<<(4*4)); GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*4); + // GPIO C0 Push-Pull + GPIOC->CFGLR &= ~(0xf<<(4*0)); + GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); + while(1) { - GPIOD->BSHR = 1 | (1<<4); // Turn on GPIOD0 + GPIOD->BSHR = 1 | (1<<4); // Turn on GPIOs + GPIOC->BSHR = 1; printf( "+%d\n", count++ ); - GPIOD->BSHR = (1<<16) | ( 1<<(4+16) ); // Turn off GPIOD0 + GPIOD->BSHR = (1<<16) | (1<<(16+4)); // Turn off GPIODs + GPIOC->BSHR = (1<<16); printf( "-%d\n", count++ ); + count++; } } + diff --git a/examples/external_crystal/external_crystal.c b/examples/external_crystal/external_crystal.c index 55de9d22f741f4dc8aca5ba7cb9f6fbdf463aded..d568775b3bc9d6d539be16a65dc720af08af8842 100644 --- a/examples/external_crystal/external_crystal.c +++ b/examples/external_crystal/external_crystal.c @@ -1,5 +1,3 @@ -// XXX XXX XXX XXX XXX THIS IS UNTESTED XXX XXX XXX XXX XXX - // Could be defined here, or in the processor defines. #define SYSTEM_CORE_CLOCK 24000000 @@ -14,23 +12,29 @@ int main() { SystemInitHSE( 0 ); - // Enable GPIOD. - RCC->APB2PCENR |= RCC_APB2Periph_GPIOD; + // Enable GPIOs + RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; - // GPIO D0 Push-Pull, 10MHz Output + // GPIO D0 Push-Pull GPIOD->CFGLR &= ~(0xf<<(4*0)); GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); - // GPIO D0 Push-Pull, 10MHz Output + // GPIO D4 Push-Pull GPIOD->CFGLR &= ~(0xf<<(4*4)); GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*4); + // GPIO C0 Push-Pull + GPIOC->CFGLR &= ~(0xf<<(4*0)); + GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); + while(1) { - GPIOD->BSHR = 1 | (1<<4); // Turn on GPIOD0 - Delay_Ms( 200 ); - GPIOD->BSHR = (1<<16) | (1<<(16+4)); // Turn off GPIOD0 - Delay_Ms( 200 ); + GPIOD->BSHR = 1 | (1<<4); // Turn on GPIOs + GPIOC->BSHR = 1; + Delay_Ms( 250 ); + GPIOD->BSHR = (1<<16) | (1<<(16+4)); // Turn off GPIODs + GPIOC->BSHR = (1<<16); + Delay_Ms( 250 ); count++; } } diff --git a/examples/run_from_ram/Makefile b/examples/run_from_ram/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..41dd42d3616849f0e9271561530dcb6926610b6a --- /dev/null +++ b/examples/run_from_ram/Makefile @@ -0,0 +1,51 @@ +TARGET:=run_from_ram + +all : flash + +PREFIX:=riscv64-unknown-elf + +GPIO_Toggle:=EXAM/GPIO/GPIO_Toggle/User + +EVT:=../../ch32v003evt + +MINICHLINK:=../../minichlink + +ifeq ($(OS),Windows_NT) +# On Windows, all the major RISC-V GCC installs are missing the -ec libgcc. +LIB_GCC=../../misc/libgcc.a +else +LIB_GCC=-lgcc +endif + +CH32V003FUN:=../../ch32v003fun + +CFLAGS:= \ + -g -Os -flto -ffunction-sections \ + -static-libgcc $(LIB_GCC) \ + -march=rv32ec \ + -mabi=ilp32e \ + -I/usr/include/newlib \ + -I$(CH32V003FUN) \ + -nostdlib \ + -I. -DTINYVECTOR + +LDFLAGS:=-T $(CH32V003FUN)/ch32v003fun.ld -Wl,--gc-sections + +SYSTEM_C:=$(CH32V003FUN)/ch32v003fun.c + +$(TARGET).elf : $(SYSTEM_C) $(TARGET).c + $(PREFIX)-gcc -o $@ $^ $(CFLAGS) $(LDFLAGS) + +$(TARGET).bin : $(TARGET).elf + $(PREFIX)-size $^ + $(PREFIX)-objdump -S $^ > $(TARGET).lst + $(PREFIX)-objdump -t $^ > $(TARGET).map + $(PREFIX)-objcopy -O binary $< $(TARGET).bin + $(PREFIX)-objcopy -O ihex $< $(TARGET).hex + +flash : $(TARGET).bin + $(MINICHLINK)/minichlink -w $< flash -b + +clean : + rm -rf $(TARGET).elf $(TARGET).bin $(TARGET).hex $(TARGET).lst $(TARGET).map $(TARGET).hex + diff --git a/examples/run_from_ram/run_from_ram.c b/examples/run_from_ram/run_from_ram.c new file mode 100644 index 0000000000000000000000000000000000000000..f7a8d4fa293d22533dd18fdce4512aa7bc573b23 --- /dev/null +++ b/examples/run_from_ram/run_from_ram.c @@ -0,0 +1,51 @@ +// Could be defined here, or in the processor defines. +#define SYSTEM_CORE_CLOCK 48000000 + +#include "ch32v003fun.h" +#include <stdio.h> + +uint32_t count; + +// There's a few reasons you might want to run from RAM, for instance +// it's faster than running from flash, especially if you're running +// on PLL. Or maybe you want to power down the flash for some reaso. +// +// Well, no worries! You can just stick it in the .data segment! + +void RamFunction() __attribute__((section(".data"))) __attribute__((used)); +void RamFunction() +{ + // GPIO D0 Push-Pull + GPIOD->CFGLR &= ~(0xf<<(4*0)); + GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); + + // GPIO D4 Push-Pull + GPIOD->CFGLR &= ~(0xf<<(4*4)); + GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*4); + + // GPIO C0 Push-Pull + GPIOC->CFGLR &= ~(0xf<<(4*0)); + GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); + + while(1) + { + GPIOD->BSHR = 1 | (1<<4); // Turn on GPIOs + GPIOC->BSHR = 1; + Delay_Ms( 250 ); + GPIOD->BSHR = (1<<16) | (1<<(16+4)); // Turn off GPIODs + GPIOC->BSHR = (1<<16); + Delay_Ms( 250 ); + count++; + } +} + +int main() +{ + SystemInit48HSI(); + + // Enable GPIOs + RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; + + RamFunction(); +} +