Skip to content
Snippets Groups Projects
Commit f63b60ab authored by cnlohr's avatar cnlohr
Browse files

Add extra noe to self-modify code bit. And also playing with OC'ing on sandbox.

parent 5d6783a9
No related branches found
No related tags found
No related merge requests found
......@@ -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
// 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();
}
......@@ -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" );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment