Skip to content

Commit

Permalink
* Add debugprintf and unify makefiles
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlohr committed Feb 15, 2024
1 parent 998edd6 commit 8e8091f
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 169 deletions.
16 changes: 14 additions & 2 deletions ch32v003fun/ch32v003fun.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
#define FUNCONF_DEBUGPRINTF_TIMEOUT 160000 // Arbitrary time units
*/

// Sanity check for when porting old code.
#if defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
#if defined(CH32V003)
#error Cannot define CH32V003 and another arch.
#endif
#endif

#if !defined(FUNCONF_USE_DEBUGPRINTF) && !defined(FUNCONF_USE_UARTPRINTF)
#define FUNCONF_USE_DEBUGPRINTF 1
#endif
Expand Down Expand Up @@ -12361,8 +12368,13 @@ void DefaultIRQHandler( void ) __attribute__((section(".text.vector_handler")))
#endif

// For debug writing to the debug interface.
#define DMDATA0 ((volatile uint32_t*)0xe00000f4)
#define DMDATA1 ((volatile uint32_t*)0xe00000f8)
#if defined(CH32V003)
#define DMDATA0 ((volatile uint32_t*)0xe00000f4)
#define DMDATA1 ((volatile uint32_t*)0xe00000f8)
#else
#define DMDATA0 ((volatile uint32_t*)0xe0000380)
#define DMDATA1 ((volatile uint32_t*)0xe0000384)
#endif

#endif

Expand Down
139 changes: 115 additions & 24 deletions ch32v003fun/ch32v003fun.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,136 @@ MINICHLINK?=$(CH32V003FUN)/../minichlink
WRITE_SECTION?=flash
SYSTEM_C?=$(CH32V003FUN)/ch32v003fun.c

ifeq ($(TARGET_MCU),CH32V003)
CFLAGS+= \
-g -Os -flto -ffunction-sections -fdata-sections \
-static-libgcc \
-march=rv32ec \
-mabi=ilp32e \
-I/usr/include/newlib \
-I$(CH32V003FUN)/../extralibs \
-I$(CH32V003FUN) \
-nostdlib \
-DCH32V003=1 \
-I. -Wall
CFLAGS?=-g -Os -flto -ffunction-sections -fdata-sections

ifeq ($(TARGET_MCU),CH32V003)
CFLAGS_ARCH+=-march=rv32ec -mabi=ilp32e -DCH32V003=1
GENERATED_LD_FILE?=$(CH32V003FUN)/generated_ch32v003.ld
TARGET_MCU_LD:=0
LINKER_SCRIPT?=$(GENERATED_LD_FILE)
LDFLAGS+=-L$(CH32V003FUN)/../misc -lgcc
else
ifeq ($(findstring CH32V10,$(TARGET_MCU)),CH32V10)
include $(CH32V003FUN)/ch32v10xfun.mk
else ifeq ($(findstring CH32V20,$(TARGET_MCU)),CH32V20)
include $(CH32V003FUN)/ch32v20xfun.mk
else ifeq ($(findstring CH32V30,$(TARGET_MCU)),CH32V30)
include $(CH32V003FUN)/ch32v30xfun.mk
MCU_PACKAGE?=1

ifeq ($(findstring CH32V10,$(TARGET_MCU)),CH32V10) # CH32V103
TARGET_MCU_PACKAGE?=CH32V103R8T6
CFLAGS_ARCH+= -march=rv32imac \
-mabi=ilp32 \
-fmessage-length=0 \
-msmall-data-limit=8 \
-mno-save-restore \
-DCH32V10x=1

# MCU Flash/RAM split
ifeq ($(findstring R8, $(TARGET_MCU_PACKAGE)), R8)
MCU_PACKAGE:=1
else ifeq ($(findstring C8, $(TARGET_MCU_PACKAGE)), C8)
MCU_PACKAGE:=1
else ifeq ($(findstring C6, $(TARGET_MCU_PACKAGE)), C6)
MCU_PACKAGE:=2
endif

TARGET_MCU_LD:=1
else ifeq ($(findstring CH32V20,$(TARGET_MCU)),CH32V20) # CH32V203
TARGET_MCU_PACKAGE?=CH32V203F8P6
CFLAGS_ARCH+= -march=rv32imac \
-mabi=ilp32 \
-fmessage-length=0 \
-msmall-data-limit=8 \
-mno-save-restore \
-DCH32V20x=1

# MCU Flash/RAM split
ifeq ($(findstring F8, $(TARGET_MCU_PACKAGE)), F8)
MCU_PACKAGE:=1
else ifeq ($(findstring G8, $(TARGET_MCU_PACKAGE)), G8)
MCU_PACKAGE:=1
else ifeq ($(findstring K8, $(TARGET_MCU_PACKAGE)), K8)
MCU_PACKAGE:=1
else ifeq ($(findstring C8, $(TARGET_MCU_PACKAGE)), C8)
MCU_PACKAGE:=1
else ifeq ($(findstring F6, $(TARGET_MCU_PACKAGE)), F6)
MCU_PACKAGE:=2
else ifeq ($(findstring G6, $(TARGET_MCU_PACKAGE)), G6)
MCU_PACKAGE:=2
else ifeq ($(findstring K6, $(TARGET_MCU_PACKAGE)), K6)
MCU_PACKAGE:=2
else ifeq ($(findstring C6, $(TARGET_MCU_PACKAGE)), C6)
MCU_PACKAGE:=2
else ifeq ($(findstring RB, $(TARGET_MCU_PACKAGE)), RB)
MCU_PACKAGE:=3
else ifeq ($(findstring GB, $(TARGET_MCU_PACKAGE)), GB)
MCU_PACKAGE:=3
else ifeq ($(findstring CB, $(TARGET_MCU_PACKAGE)), CB)
MCU_PACKAGE:=3
else ifeq ($(findstring WB, $(TARGET_MCU_PACKAGE)), WB)
MCU_PACKAGE:=3
endif

# Package
ifeq ($(findstring 203RB, $(TARGET_MCU_PACKAGE)), 203RB)
CFLAGS+=-DCH32V20x_D8
else ifeq ($(findstring 208, $(TARGET_MCU_PACKAGE)), 208)
CFLAGS+=-DCH32V20x_D8W
else
CFLAGS+=-DCH32V20x_D6
endif

TARGET_MCU_LD:=2
else ifeq ($(findstring CH32V30,$(TARGET_MCU)),CH32V30) #CH32V307
TARGET_MCU_PACKAGE?=CH32V307VCT6
MCU_PACKAGE?=1

CFLAGS_ARCH+= -march=rv32imafc \
-mabi=ilp32f \
-msmall-data-limit=8 \
-mno-save-restore \
-fmessage-length=0 \
-DCH32V30x=1

# MCU Flash/RAM split
ifeq ($(findstring RC, $(TARGET_MCU_PACKAGE)), RC)
MCU_PACKAGE:=1
else ifeq ($(findstring VC, $(TARGET_MCU_PACKAGE)), VC)
MCU_PACKAGE:=1
else ifeq ($(findstring WC, $(TARGET_MCU_PACKAGE)), WC)
MCU_PACKAGE:=1
else ifeq ($(findstring CB, $(TARGET_MCU_PACKAGE)), CB)
MCU_PACKAGE:=2
else ifeq ($(findstring FB, $(TARGET_MCU_PACKAGE)), FB)
MCU_PACKAGE:=2
else ifeq ($(findstring RB, $(TARGET_MCU_PACKAGE)), RB)
MCU_PACKAGE:=2
endif

# Package
ifeq ($(findstring 303, $(TARGET_MCU_PACKAGE)), 303)
CFLAGS+=-DCH32V30x_D8
else
CFLAGS+=-DCH32V30x_D8C
endif

TARGET_MCU_LD:=3
else
$(error Unknown MCU $(TARGET_MCU))
endif

LDFLAGS+=-lgcc
GENERATED_LD_FILE:=$(CH32V003FUN)/generated_$(TARGET_MCU_PACKAGE).ld
LINKER_SCRIPT:=$(GENERATED_LD_FILE)
endif

CFLAGS+= $(EXTRA_CFLAGS)
CFLAGS+= \
$(CFLAGS_ARCH) -static-libgcc \
-I/usr/include/newlib \
-I$(CH32V003FUN)/../extralibs \
-I$(CH32V003FUN) \
-nostdlib \
-I. -Wall $(EXTRA_CFLAGS)

LDFLAGS+=-T $(LINKER_SCRIPT) -Wl,--gc-sections

ifeq ($(TARGET_MCU), CH32V003)

FILES_TO_COMPILE:=$(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)

endif

$(TARGET).bin : $(TARGET).elf
$(PREFIX)-size $^
$(PREFIX)-objdump -S $^ > $(TARGET).lst
Expand Down
34 changes: 0 additions & 34 deletions ch32v003fun/ch32v10xfun.mk

This file was deleted.

62 changes: 0 additions & 62 deletions ch32v003fun/ch32v20xfun.mk

This file was deleted.

47 changes: 0 additions & 47 deletions ch32v003fun/ch32v30xfun.mk

This file was deleted.

10 changes: 10 additions & 0 deletions examples_v20x/debugprintfdemo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all : flash

TARGET:=debugprintfdemo
TARGET_MCU:=CH32V203

include ../../ch32v003fun/ch32v003fun.mk

flash : cv_flash
clean : cv_clean

46 changes: 46 additions & 0 deletions examples_v20x/debugprintfdemo/debugprintfdemo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Small example showing how to use the SWIO programming pin to
do printf through the debug interface */

#include "ch32v003fun.h"
#include <stdio.h>

uint32_t count;

int last = 0;
void handle_debug_input( int numbytes, uint8_t * data )
{
last = data[0];
count += numbytes;
}

int main()
{
SystemInit();

// 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);

// 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; // Turn on GPIOs
GPIOC->BSHR = 1;
printf( "+%lu\n", count++ );
Delay_Ms(100);
int i;
for( i = 0; i < 10000; i++ )
poll_input();
GPIOD->BSHR = (1<<16); // Turn off GPIODs
GPIOC->BSHR = (1<<16);
printf( "-%lu[%c]\n", count++, last );
Delay_Ms(100);
}
}

Loading

0 comments on commit 8e8091f

Please sign in to comment.