From 5335fb6e891527a6968654f991b6b2b5676dd675 Mon Sep 17 00:00:00 2001 From: cnlohr <lohr85@gmail.com> Date: Mon, 13 Mar 2023 03:50:43 -0400 Subject: [PATCH] Add example app running from RAM. Cleanup some comments. --- examples/blink/blink.c | 21 ++++---- examples/bootload/bootload.c | 6 +-- examples/debugprintfdemo/debugprintfdemo.c | 19 ++++++-- examples/external_crystal/external_crystal.c | 24 +++++---- examples/run_from_ram/Makefile | 51 ++++++++++++++++++++ examples/run_from_ram/run_from_ram.c | 51 ++++++++++++++++++++ 6 files changed, 144 insertions(+), 28 deletions(-) create mode 100644 examples/run_from_ram/Makefile create mode 100644 examples/run_from_ram/run_from_ram.c diff --git a/examples/blink/blink.c b/examples/blink/blink.c index 81a2760..e656399 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 02b2385..a597594 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 576f199..80ed683 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 55de9d2..d568775 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 0000000..41dd42d --- /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 0000000..f7a8d4f --- /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(); +} + -- GitLab