Skip to content

CH57x bringup and blinky, debugprintfdemo #561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions ch32fun/ch32fun.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,14 +986,6 @@ void TMR3_IRQHandler( void ) __attribute__((section(".text.vector_handler")))
void UART2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void UART3_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void WDOG_BAT_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
// CH57x
void SPI_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void TMR_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void UART_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void CMP_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void KEYSCAN_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void ENCODER_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));


void InterruptVector() __attribute__((naked)) __attribute((section(".init"))) __attribute((weak,alias("InterruptVectorDefault"))) __attribute((naked));
void InterruptVectorDefault() __attribute__((naked)) __attribute((section(".init"))) __attribute((naked));
Expand Down Expand Up @@ -1611,7 +1603,7 @@ void SystemInit( void )
#define CLK_SOURCE_CH5XX CLK_SOURCE_PLL_60MHz
#endif
#if defined(CH57x) && (MCU_PACKAGE == 0 || MCU_PACKAGE == 2)
SYS_CLKTypeDef sc = CLK_SOURCE_CH57X;
SYS_CLKTypeDef sc = CLK_SOURCE_CH5XX;

if(sc == RB_CLK_SYS_MOD) // LSI
{
Expand Down Expand Up @@ -1652,21 +1644,21 @@ void SystemInit( void )
ADD_N_NOPS(4);
R8_FLASH_CFG = 0X51;
}

else if(sc & 0x40) // PLL div
{
SYS_SAFE_ACCESS(
R32_CLK_SYS_CFG = (1 << 6) | (sc & 0x1f) | RB_TX_32M_PWR_EN | RB_PLL_PWR_EN;
);
ADD_N_NOPS(4);
R8_FLASH_CFG = 0X52;
R8_FLASH_CFG = 0x52;
}
else
{
SYS_SAFE_ACCESS(
R32_CLK_SYS_CFG |= RB_CLK_SYS_MOD;
);
}

SYS_SAFE_ACCESS(
R8_PLL_CONFIG |= 1 << 7;
);
Expand Down
1 change: 1 addition & 0 deletions ch32fun/ch32fun.mk
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ else ifeq ($(findstring CH57,$(TARGET_MCU)),CH57) # CH570 1 2 3
else ifeq ($(findstring 573, $(TARGET_MCU_PACKAGE)), 573)
MCU_PACKAGE:=3
endif
CFLAGS+=-DMCU_PACKAGE=$(MCU_PACKAGE)

# Package
ifeq ($(findstring D, $(TARGET_MCU_PACKAGE)), D)
Expand Down
12 changes: 12 additions & 0 deletions ch32fun/ch57xhw.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,15 @@ typedef enum
} SYS_CLKTypeDef;

// For debug writing to the debug interface.
#if MCU_PACKAGE == 3
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment explaining what package 3 is.

Copy link
Contributor Author

@biemster biemster May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ch57(3), I thought it worked like that on the other mcu's as well, but thinking of it now that's not really possible ofc 😄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add a comment, for people who are casually reading the source code.

#define DMDATA0 ((vu32*)0xe0000380)
#define DMDATA1 ((vu32*)0xe0000384)
#define DMSTATUS_SENTINEL ((vu32*)0xe0000388)// Reads as 0x00000000 if debugger is attached.
#else
#define DMDATA0 ((vu32*)0xe0000340)
#define DMDATA1 ((vu32*)0xe0000344)
#define DMSTATUS_SENTINEL ((vu32*)0xe0000348)// Reads as 0x00000000 if debugger is attached.
#endif

/* System: clock configuration register */
#define R32_CLK_SYS_CFG (*((vu32*)0x40001008)) // RWA, system clock configuration, SAM
Expand All @@ -252,6 +258,12 @@ typedef enum
#define SAFE_ACCESS_SIG2 0xA8 // WO: safe accessing sign value step 2
#define SAFE_ACCESS_SIG0 0x00 // WO: safe accessing sign value for disable
#define R8_CHIP_ID (*((vu8*)0x40001041)) // RF, chip ID register, always is ID_CH57*
#define SYS_SAFE_ACCESS(a) do { R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG1; \
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG2; \
asm volatile ("nop\nnop"); \
{a} \
R8_SAFE_ACCESS_SIG = SAFE_ACCESS_SIG0; \
asm volatile ("nop\nnop"); } while(0)

/*System: Miscellaneous Control register */
#define R32_MISC_CTRL (*((vu32*)0x40001048)) // RWA, miscellaneous control register
Expand Down
4 changes: 2 additions & 2 deletions examples_ch57x/blink/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
all : flash

TARGET:=blink
TARGET_MCU:=CH573
TARGET_MCU_PACKAGE:=CH573F
TARGET_MCU:=CH570
TARGET_MCU_PACKAGE:=CH570D

include ../../ch32fun/ch32fun.mk

Expand Down
12 changes: 9 additions & 3 deletions examples_ch57x/blink/blink.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#include "ch32fun.h"
#include <stdio.h>

#if MCU_PACKAGE == 0 || MCU_PACKAGE == 2
#define LED PA9
#else
#define LED PA8
#endif

int main()
{
SystemInit();

funPinMode( PA8, GPIO_CFGLR_OUT_2Mhz_PP );
funPinMode( LED, GPIO_CFGLR_OUT_2Mhz_PP );

while(1)
{
funDigitalWrite( PA8, FUN_LOW ); // Turn on LED
funDigitalWrite( LED, FUN_LOW ); // Turn on LED
Delay_Ms( 33 );
funDigitalWrite( PA8, FUN_HIGH ); // Turn off LED
funDigitalWrite( LED, FUN_HIGH ); // Turn off LED
Delay_Ms( 300 );
}
}
241 changes: 0 additions & 241 deletions examples_ch57x/blink/ch573_wchlink.py

This file was deleted.

2 changes: 1 addition & 1 deletion examples_ch57x/blink/funconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#define FUNCONF_USE_HSI 0 // CH57x does not have HSI
#define FUNCONF_USE_HSE 1
#define CLK_SOURCE_CH57X CLK_SOURCE_PLL_60MHz // default so not really needed. CH570/2 can go up to 100MHz
#define CLK_SOURCE_CH5XX CLK_SOURCE_PLL_60MHz // default so not really needed. CH570/2 can go up to 100MHz
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful way to define it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this worked out quite nice!

#define FUNCONF_SYSTEM_CORE_CLOCK 60 * 1000 * 1000 // keep in line with CLK_SOURCE_CH57X

#define FUNCONF_DEBUG_HARDFAULT 0
Expand Down
4 changes: 2 additions & 2 deletions examples_ch57x/debugprintfdemo/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
all : flash

TARGET:=debugprintfdemo
TARGET_MCU:=CH573
TARGET_MCU_PACKAGE:=CH573F
TARGET_MCU:=CH570
TARGET_MCU_PACKAGE:=CH570D

include ../../ch32fun/ch32fun.mk

Expand Down
Loading
Loading