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 3 commits
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
55 changes: 44 additions & 11 deletions ch32fun/ch32fun.c
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,6 @@ void UART2_IRQHandler( void ) __attribute__((section(".text.vector_handler")))
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));



void InterruptVector() __attribute__((naked)) __attribute((section(".init"))) __attribute((weak,alias("InterruptVectorDefault"))) __attribute((naked));
void InterruptVectorDefault() __attribute__((naked)) __attribute((section(".init"))) __attribute((naked));
void handle_reset( void ) __attribute__((section(".text.handle_reset")));
Expand Down Expand Up @@ -1081,7 +1079,7 @@ asm volatile(
" mret\n" : : [main]"r"(main) );
}

#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x) || defined(CH58x) || defined(CH59x)
#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x) || defined(CH57x) || defined(CH58x) || defined(CH59x)

void handle_reset( void )
{
Expand Down Expand Up @@ -1478,10 +1476,10 @@ void SetupDebugPrintf( void )
int WaitForDebuggerToAttach( int timeout_ms )
{

#if defined(CH32V20x) || defined(CH32V30x) || defined(CH58x) || defined(CH59x)
#if defined(CH32V20x) || defined(CH32V30x) || (defined(CH57x) && MCU_PACKAGE == 3) || defined(CH58x) || defined(CH59x)
#define systickcnt_t uint64_t
#define SYSTICKCNT SysTick->CNT
#elif defined(CH32V10x) || defined(CH32X03x)
#elif defined(CH32V10x) || defined(CH32X03x) || defined(CH57x)
#define systickcnt_t uint32_t
#define SYSTICKCNT SysTick->CNTL
#else
Expand Down Expand Up @@ -1530,9 +1528,13 @@ void DelaySysTick( uint32_t n )
#elif defined(CH32V20x) || defined(CH32V30x) || defined(CH58x) || defined(CH59x)
uint64_t targend = SysTick->CNT + n;
while( ((int64_t)( SysTick->CNT - targend )) < 0 );
#elif defined(CH32V10x) || defined(CH32X03x)
#elif defined(CH32V10x) || defined(CH32X03x) || (defined(CH57x) && (MCU_PACKAGE == 0 || MCU_PACKAGE == 2))
uint32_t targend = SysTick->CNTL + n;
while( ((int32_t)( SysTick->CNTL - targend )) < 0 );
#elif defined(CH57x) && MCU_PACKAGE == 3
// ch573 insisted on being special, it's counting down
uint64_t targend = SysTick->CNT - n;
while( ((int64_t)( SysTick->CNT - targend )) > 0 );
#else
#error DelaySysTick not defined.
#endif
Expand Down Expand Up @@ -1596,10 +1598,40 @@ void SystemInit( void )
#endif
#endif

#if defined(CH58x) || defined(CH59x) // has no HSI
#if defined(CH57x) || defined(CH58x) || defined(CH59x) // has no HSI
#ifndef CLK_SOURCE_CH5XX
#define CLK_SOURCE_CH5XX CLK_SOURCE_PLL_60MHz
#endif
#if defined(CH57x) && (MCU_PACKAGE == 0 || MCU_PACKAGE == 2)
SYS_CLKTypeDef sc = CLK_SOURCE_CH5XX;

if(sc == RB_CLK_SYS_MOD) // LSI
{
SYS_SAFE_ACCESS(
R8_CLK_SYS_CFG |= RB_CLK_SYS_MOD;
);
}
else
{
if((sc & RB_CLK_SYS_MOD) == 0x40) // PLL div
{
SYS_SAFE_ACCESS(
R8_HFCK_PWR_CTRL |= RB_CLK_PLL_PON;
R8_FLASH_CFG = 0x01;
R8_FLASH_SCK |= 1<<4; //50M
);
}
else // 32M div
{
SYS_SAFE_ACCESS(
R8_FLASH_CFG = (sc & 0x1F) ? 0x02 : 0x07;
);
}
SYS_SAFE_ACCESS(
R8_CLK_SYS_CFG = sc;
);
}
#else
SYS_CLKTypeDef sc = CLK_SOURCE_CH5XX;
SYS_SAFE_ACCESS(
R8_PLL_CONFIG &= ~(1 << 5);
Expand All @@ -1612,24 +1644,25 @@ 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;
);
#endif // switch between ch570/2 and other ch5xx
#elif defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI
#if defined(CH32V30x) || defined(CH32V20x) || defined(CH32V10x)
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
Expand Down Expand Up @@ -1689,11 +1722,11 @@ void SystemInit( void )
#endif
#endif

#if !defined(CH58x) && !defined(CH59x)
#if !defined(CH57x) && !defined(CH58x) && !defined(CH59x)
RCC->INTR = 0x009F0000; // Clear PLL, CSSC, HSE, HSI and LSI ready flags.
#endif

#if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL && !defined(CH58x) && !defined(CH59x)
#if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL && !defined(CH57x) && !defined(CH58x) && !defined(CH59x)
while((RCC->CTLR & RCC_PLLRDY) == 0); // Wait till PLL is ready
uint32_t tmp32 = RCC->CFGR0 & ~(0x03); // clr the SW
RCC->CFGR0 = tmp32 | RCC_SW_PLL; // Select PLL as system clock source
Expand Down
13 changes: 10 additions & 3 deletions ch32fun/ch32fun.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
#endif
#elif defined(CH32V30x)
#define HSE_VALUE (8000000)
#elif defined(CH58x) || defined(CH59x)
#elif defined(CH57x) || defined(CH58x) || defined(CH59x)
#define HSE_VALUE (32000000)
#endif
#endif
Expand Down Expand Up @@ -209,7 +209,7 @@
#endif

#ifndef FUNCONF_SYSTEM_CORE_CLOCK
#if defined(CH58x) || defined(CH59x) // no PLL multiplier, but a divider from the 480 MHz clock
#if defined(CH57x) || defined(CH58x) || defined(CH59x) // no PLL multiplier, but a divider from the 480 MHz clock
#define FUNCONF_SYSTEM_CORE_CLOCK 60000000 // default in ch32fun.c using CLK_SOURCE_PLL_60MHz
#if defined(CLK_SOURCE_CH5XX)
#error Must define FUNCONF_SYSTEM_CORE_CLOCK too if CLK_SOURCE_CH5XX is defined
Expand Down Expand Up @@ -362,6 +362,8 @@ typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
#include "ch32v20xhw.h"
#elif defined( CH32V30x )
#include "ch32v30xhw.h"
#elif defined( CH57x )
#include "ch57xhw.h"
#elif defined( CH58x )
#include "ch58xhw.h"
#elif defined( CH59x )
Expand Down Expand Up @@ -837,8 +839,13 @@ extern "C" {

#define FUN_HIGH 0x1
#define FUN_LOW 0x0
#if defined(CH58x) || defined(CH59x)
#if defined(CH57x) || defined(CH58x) || defined(CH59x)
#if defined( PB ) && defined( R32_PB_PIN )
#define OFFSET_FOR_GPIOB(pin) (((pin & PB) >> 31) * (&R32_PB_PIN - &R32_PA_PIN)) // 0 if GPIOA, 0x20 if GPIOB
#else
#define PB 0
#define OFFSET_FOR_GPIOB(pin) 0
#endif
#define GPIO_ResetBits(pin) (*(&R32_PA_CLR + OFFSET_FOR_GPIOB(pin)) |= (pin & ~PB))
#define GPIO_SetBits(pin) (*(&R32_PA_OUT + OFFSET_FOR_GPIOB(pin)) |= (pin & ~PB))
#define GPIO_InverseBits(pin) (*(&R32_PA_OUT + OFFSET_FOR_GPIOB(pin)) ^= (pin & ~PB))
Expand Down
11 changes: 11 additions & 0 deletions ch32fun/ch32fun.ld
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ MEMORY
#else
#error "Unknown MCU package"
#endif
#elif TARGET_MCU_LD == 10
/* CH57x */
#if MCU_PACKAGE == 0 || MCU_PACKAGE == 2
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 240K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 12K
#elif MCU_PACKAGE == 3
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 512K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 18K
#else
#error "Unknown MCU package, only ch570, 2 and 3 are supported at the moment"
#endif
#else
#error "Unknown MCU target"
#endif
Expand Down
30 changes: 30 additions & 0 deletions ch32fun/ch32fun.mk
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,36 @@ else ifeq ($(findstring CH32V30,$(TARGET_MCU)),CH32V30) #CH32V307
endif

TARGET_MCU_LD:=3
else ifeq ($(findstring CH57,$(TARGET_MCU)),CH57) # CH570 1 2 3
TARGET_MCU_PACKAGE?=CH570E
CFLAGS_ARCH+=-march=rv32imac \
-mabi=ilp32 \
-DCH57x=1

# MCU Flash/RAM split
ifeq ($(findstring 570, $(TARGET_MCU_PACKAGE)), 570)
MCU_PACKAGE:=0
else ifeq ($(findstring 571, $(TARGET_MCU_PACKAGE)), 571)
MCU_PACKAGE:=1
else ifeq ($(findstring 572, $(TARGET_MCU_PACKAGE)), 572)
MCU_PACKAGE:=2
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)
CFLAGS+=-DCH57xD
else ifeq ($(findstring Q, $(TARGET_MCU_PACKAGE)), Q)
CFLAGS+=-DCH57xQ
else ifeq ($(findstring R, $(TARGET_MCU_PACKAGE)), R)
CFLAGS+=-DCH57xR
else ifeq ($(findstring E, $(TARGET_MCU_PACKAGE)), E)
CFLAGS+=-DCH57xE
endif

TARGET_MCU_LD:=10
else ifeq ($(findstring CH58,$(TARGET_MCU)),CH58) # CH582
TARGET_MCU_PACKAGE?=CH582F
CFLAGS_ARCH+=-march=rv32imac \
Expand Down
Loading
Loading