diff --git a/examples/sandbox/Makefile b/examples/sandbox/Makefile index d626a3809d6818e66b6801b24f555d89a1245ea6..d2c3451040d0ad87ecbd1bef27a9780bd1c19d44 100644 --- a/examples/sandbox/Makefile +++ b/examples/sandbox/Makefile @@ -6,7 +6,7 @@ PREFIX:=riscv64-unknown-elf GPIO_Toggle:=EXAM/GPIO/GPIO_Toggle/User -CH32V003FUN:=../../ch32v003CH32V003FUN +CH32V003FUN:=../../ch32v003fun MINICHLINK:=../../minichlink CFLAGS:= \ @@ -35,8 +35,11 @@ $(TARGET).bin : $(TARGET).elf flash : $(TARGET).bin make -C $(MINICHLINK) all - $(MINICHLINK)/minichlink -w $< -r + $(MINICHLINK)/minichlink -w $< flash -b + +monitor : flash + $(MINICHLINK)/minichlink -T + clean : rm -rf $(TARGET).elf $(TARGET).bin $(TARGET).hex $(TARGET).lst $(TARGET).map $(TARGET).hex - diff --git a/examples/sandbox/sandbox.c b/examples/sandbox/sandbox.c index 05365be064812c3b7366d21e686c4742e3f78b5d..742254cd6ac53cd22462a0c758a4a517a0c89634 100644 --- a/examples/sandbox/sandbox.c +++ b/examples/sandbox/sandbox.c @@ -1,50 +1,51 @@ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK +/* Small example showing how to use the SWIO programming pin to + do printf through the debug interface */ -#include "ch32v00x.h" +#include "ch32v003fun.h" #include <stdio.h> -#include <string.h> -// Working on WS2812 driving. +uint32_t count; + + + +// Tell the compiler to put this code in the .data section. That +// will cause the startup code to copy it from flash into RAM where +// it can be easily modified at runtime. +void SRAMCode( ) __attribute__(( section(".data"))) __attribute__((noinline)) __attribute__((noreturn)); +void SRAMCode( ) +{ + asm volatile( +"li a0, 0x40011410\n" +"li a1, (1 | (1<<4))\n" +"li a2, (1 | (1<<4))<<16\n" +"1: c.sw a1, 0(a0)\n" +" c.sw a2, 0(a0)\n" +" j 1b\n" ); +} int main() { SystemInit48HSI(); - SetupUART( UART_BRR ); + SetupDebugPrintf(); - int k; + // Boost CPU supply. + EXTEN->EXTEN_CTR = EXTEN_LDO_TRIM; - // Enable GPIOD (for debugging) - RCC->APB2PCENR |= RCC_APB2Periph_GPIOD; + // Enable GPIOs + RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; + + // GPIO D0 Push-Pull GPIOD->CFGLR &= ~(0xf<<(4*0)); GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); - GPIOD->BSHR = 1; // Turn on GPIOD0 - GPIOD->BSHR = 1<<16; // Turn off GPIOD0 - - - //DCSR - asm volatile("\n\ - li t0, 0x4\n\ - csrw 0x7B0, t0\n\ - "); - - while(1) - { - Delay_Ms( 2 ); - *(uint32_t*)(0xe0000100) = 2; //Hopefully enable debug (dmcontrol .0) --> Doesn't work. - uint32_t val = *(uint32_t*)0xe00000f4; - *(uint32_t*)0xe00000f4 = 0xaabbccdd; + // GPIO D4 Push-Pull + GPIOD->CFGLR &= ~(0xf<<(4*4)); + GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*4); - printf( "0xe00000f4: %08x %08x\n", val, __get_dscratch0() ); + // GPIO C0 Push-Pull + GPIOC->CFGLR &= ~(0xf<<(4*0)); + GPIOC->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0); - // Write to dscratch0 - asm volatile("\n\ - li t0, 0xa8b8c8d8\n\ - csrw 0x7B2, t0\n\ - csrw 0x7B3, t0\n\ - "); - } + SRAMCode(); } diff --git a/examples/self_modify_code/self_modify_code.c b/examples/self_modify_code/self_modify_code.c index bad54d8a01f208ee999f0f42b1d9366022e16154..5e40b5f9787a1e857fae4d874c197ee3acbdba87 100644 --- a/examples/self_modify_code/self_modify_code.c +++ b/examples/self_modify_code/self_modify_code.c @@ -8,7 +8,9 @@ uint32_t count; - +// Tell the compiler to put this code in the .data section. That +// will cause the startup code to copy it from flash into RAM where +// it can be easily modified at runtime. uint32_t ReadCSRSelfModify( uint16_t whichcsr ) __attribute__(( section(".data"))) __attribute__((noinline)); uint32_t ReadCSRSelfModify( uint16_t whichcsr ) { @@ -20,7 +22,7 @@ uint32_t ReadCSRSelfModify( uint16_t whichcsr ) // We have to put this here to "force" the compiler to order the // instructions in this way. Otherwise, the compiler will try - // to optimize the code and inline the assembly int something where + // to optimize the code and inline the assembly into something where // our global handle into assembly code becomes meaningless. // Annoyingly, it has to contain at least one instruction :( asm volatile( "nop" );