Skip to content
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

CH59x bringup and blinky #534

Merged
merged 6 commits into from
Mar 12, 2025
Merged
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
Prev Previous commit
Next Next commit
overload pin definitions of ch59x to ch32 defs, add (untested) debugp…
…rintfdemo
biemster committed Mar 1, 2025
commit d6ecbcf849ad1b9bd5bd81abf2df7e7fcbc260d6
8 changes: 0 additions & 8 deletions ch32fun/ch32fun.h
Original file line number Diff line number Diff line change
@@ -841,15 +841,7 @@ extern "C" {
#define GPIO_InverseBits(pin) { if(pin & PB) GPIOB_InverseBits(pin); else GPIOA_InverseBits(pin); }
#define funDigitalRead(pin) ( (pin & PB) ? GPIOB_ReadPortPin(pin) : GPIOA_ReadPortPin(pin) )
#define funDigitalWrite( pin, value ) { if(value==FUN_HIGH){GPIO_SetBits(pin);} else if(value==FUN_LOW){GPIO_ResetBits(pin);} }
typedef enum
{
GPIO_ModeIN_Floating,
GPIO_ModeIN_PU,
GPIO_ModeIN_PD,
GPIO_ModeOut_PP_5mA,
GPIO_ModeOut_PP_20mA,

} GPIOModeTypeDef;
#define GPIO_ModeCfg(pd_drv, pu, dir, pin, mode) { switch(mode) { \
case GPIO_ModeIN_Floating: \
pd_drv &= ~pin; pu &= ~pin; dir &= ~pin; break; \
25 changes: 25 additions & 0 deletions ch32fun/ch59xhw.h
Original file line number Diff line number Diff line change
@@ -168,6 +168,11 @@ typedef enum
CLK_SOURCE_PLL_24MHz = (0x40 | 20),
} SYS_CLKTypeDef;

// For debug writing to the debug interface.
#define DMDATA0 ((vu32*)0xe0000380)
#define DMDATA1 ((vu32*)0xe0000384)
#define DMSTATUS_SENTINEL ((vu32*)0xe0000388)// Reads as 0x00000000 if debugger is attached.

/* System: clock configuration register */
#define R32_CLK_SYS_CFG (*((vu32*)0x40001008)) // RWA, system clock configuration, SAM
#define RB_CLK_PLL_DIV 0x1F // RWA, output clock divider from PLL or CK32M
@@ -290,6 +295,26 @@ typedef enum
#define PB23 (0x10800000) /*!< Pin 23 selected */
#define P_All (0xFFFFFFFF) /*!< All pins selected */

typedef enum
{
GPIO_ModeIN_Floating,
GPIO_ModeIN_PU,
GPIO_ModeIN_PD,
GPIO_ModeOut_PP_5mA,
GPIO_ModeOut_PP_20mA,
} GPIOModeTypeDef;

/* General Purpose I/O */
typedef enum
{
GPIO_CFGLR_IN_FLOAT = GPIO_ModeIN_Floating,
GPIO_CFGLR_IN_PUPD = GPIO_ModeIN_PU, // is most common
GPIO_CFGLR_IN_PU = GPIO_ModeIN_PU,
GPIO_CFGLR_IN_PD = GPIO_ModeIN_PD, // to suppress the -Wswitch warning
GPIO_CFGLR_OUT_10Mhz_PP = GPIO_ModeOut_PP_20mA,
GPIO_CFGLR_OUT_2Mhz_PP = GPIO_ModeOut_PP_5mA,
GPIO_CFGLR_OUT_50Mhz_PP = GPIO_ModeOut_PP_20mA,
} GPIO_CFGLR_PIN_MODE_Typedef;

#define HardFault_IRQn EXC_IRQn

2 changes: 1 addition & 1 deletion examples_ch59x/blink/blink.c
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ int main()
{
SystemInit();

funPinMode( PA8, GPIO_ModeOut_PP_5mA );
funPinMode( PA8, GPIO_CFGLR_OUT_2Mhz_PP );

while(1)
{
2 changes: 1 addition & 1 deletion examples_ch59x/blink/funconfig.h
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
#define FUNCONF_USE_HSE 1
#define FUNCONF_DEBUG_HARDFAULT 0
#define FUNCONF_USE_CLK_SEC 0
#define FUNCONF_USE_DEBUGPRINTF 0 // UART is not implemented yet
#define FUNCONF_USE_DEBUGPRINTF 0 // saves 16 bytes, enable / remove if you want printf over swio
#define FUNCONF_INIT_ANALOG 0 // ADC is not implemented yet

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

TARGET:=debugprintfdemo
TARGET_MCU:=CH592
TARGET_MCU_PACKAGE:=CH592F

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

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

#include "ch32fun.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();

funPinMode( PA8, GPIO_CFGLR_OUT_10Mhz_PP );

while(1)
{
funDigitalWrite( PA8, FUN_LOW ); // Turn on LED
printf( "+%lu\n", count++ );
Delay_Ms(100);
int i;
for( i = 0; i < 10000; i++ )
poll_input();
funDigitalWrite( PA8, FUN_HIGH ); // Turn off LED
printf( "-%lu[%c]\n", count++, last );
Delay_Ms(100);
}
}
10 changes: 10 additions & 0 deletions examples_ch59x/debugprintfdemo/funconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H

#define FUNCONF_USE_HSI 0 // CH592 does not have HSI
#define FUNCONF_USE_HSE 1
#define FUNCONF_DEBUG_HARDFAULT 0
#define FUNCONF_USE_CLK_SEC 0
#define FUNCONF_INIT_ANALOG 0 // ADC is not implemented yet

#endif