diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fe3796b257422291f987f66aab73530f5daa60d3..194a9e16f4b1c28251b07fefbbbaa30a9a77589f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,4 +47,4 @@ jobs: - name: Install PlatformIO Core run: pip install --upgrade platformio - name: Build PlatformIO Project - run: pio run \ No newline at end of file + run: pio run -v diff --git a/add_include.py b/add_include.py new file mode 100644 index 0000000000000000000000000000000000000000..f7ae02f8fc33b0c50ad01d84ffd85401744dd1c2 --- /dev/null +++ b/add_include.py @@ -0,0 +1,5 @@ +from os.path import join +Import("env", "projenv") +# propagate to all construction environments +for e in env, projenv, DefaultEnvironment(): + e.Append(CCFLAGS=[("-I", join("examples", env.subst("$PIOENV")))]) diff --git a/ch32v003fun/ch32v003fun.c b/ch32v003fun/ch32v003fun.c index d030306652a65016bbd7726a16ce31fa8927ea2e..eb0ebc497d333c37b8591e49a7d31abf06e5df80 100644 --- a/ch32v003fun/ch32v003fun.c +++ b/ch32v003fun/ch32v003fun.c @@ -722,7 +722,9 @@ void InterruptVectorDefault() .align 2\n\ .option push;\n\ .option norvc;\n\ - j handle_reset\n\ + j handle_reset\n" ); +#if !defined(FUNCONF_TINYVECTOR) || !FUNCONF_TINYVECTOR + asm volatile( "\n\ .word 0\n\ .word NMI_Handler /* NMI Handler */ \n\ .word HardFault_Handler /* Hard Fault Handler */ \n\ @@ -762,7 +764,9 @@ void InterruptVectorDefault() .word TIM1_TRG_COM_IRQHandler /* TIM1 Trigger and Commutation */ \n\ .word TIM1_CC_IRQHandler /* TIM1 Capture Compare */ \n\ .word TIM2_IRQHandler /* TIM2 */ \n\ - .option pop;\n"); +"); +#endif + asm volatile( ".option pop;\n"); } void handle_reset() @@ -817,7 +821,12 @@ asm volatile( #endif ); - SETUP_SYSTICK_HCLK + +#if defined( FUNCONF_SYSTICK_USE_HCLK ) && FUNCONF_SYSTICK_USE_HCLK + SysTick->CTLR = 5; +#else + SysTick->CTLR = 1; +#endif // set mepc to be main as the root app. asm volatile( @@ -825,60 +834,7 @@ asm volatile( " mret\n" : : [main]"r"(main) ); } -void SystemInit48HSI( void ) -{ - // Values lifted from the EVT. There is little to no documentation on what this does. - RCC->CFGR0 = RCC_HPRE_DIV1 | RCC_PLLSRC_HSI_Mul2; // PLLCLK = HSI * 2 = 48 MHz; HCLK = SYSCLK = APB1 - RCC->CTLR = RCC_HSION | RCC_PLLON | ((HSITRIM) << 3); // Use HSI, but enable PLL. - FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // 1 Cycle Latency - RCC->INTR = 0x009F0000; // Clear PLL, CSSC, HSE, HSI and LSI ready flags. - - // From SetSysClockTo_48MHZ_HSI - while((RCC->CTLR & RCC_PLLRDY) == 0); // Wait till PLL is ready - RCC->CFGR0 = ( RCC->CFGR0 & ((uint32_t)~(RCC_SW))) | (uint32_t)RCC_SW_PLL; // Select PLL as system clock source - while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08); // Wait till PLL is used as system clock source -} - -void SystemInit24HSI( void ) -{ - // Values lifted from the EVT. There is little to no documentation on what this does. - RCC->CFGR0 = RCC_HPRE_DIV1; // PLLCLK = HCLK = SYSCLK = APB1 - RCC->CTLR = RCC_HSION | ((HSITRIM) << 3); // Use HSI, Only. - FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; // 1 Cycle Latency - RCC->INTR = 0x009F0000; // Clear PLL, CSSC, HSE, HSI and LSI ready flags. -} - -void SystemInitHSE( int HSEBYP ) -{ - // Values lifted from the EVT. There is little to no documentation on what this does. - RCC->CTLR = RCC_HSION | RCC_HSEON | RCC_PLLON | HSEBYP; // Enable HSE and keep HSI+PLL on. - while(!(RCC->CTLR&RCC_HSERDY)); - // Not using PLL. - FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; // 1 Cycle Latency - RCC->INTR = 0x009F0000; // Clear PLL, CSSC, HSE, HSI and LSI ready flags. - RCC->CFGR0 = RCC_HPRE_DIV1 | RCC_SW_HSE; // HCLK = SYSCLK = APB1 and use HSE for System Clock. - while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04); // Wait till HSE is used as system clock source - RCC->CTLR = RCC_HSEON | HSEBYP; // Turn off HSI + PLL. -} - - -void SystemInitHSEPLL( int HSEBYP ) -{ - // Values lifted from the EVT. There is little to no documentation on what this does. - RCC->CTLR = RCC_HSION | RCC_HSEON | RCC_PLLON | HSEBYP; // Enable HSE and keep HSI+PLL on. - while(!(RCC->CTLR&RCC_HSERDY)); - RCC->CFGR0 = RCC_SW_HSE | RCC_HPRE_DIV1; // HCLK = SYSCLK = APB1 and use HSE for System Clock. - FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; // 1 Cycle Latency - RCC->CTLR = RCC_HSEON | HSEBYP; // Turn off PLL and HSI. - RCC->CFGR0 = RCC_SW_HSE | RCC_HPRE_DIV1 | RCC_PLLSRC_HSE_Mul2; // Use PLL with HSE. - RCC->CTLR = RCC_HSEON | RCC_PLLON | HSEBYP; // Turn PLL Back on.. - while((RCC->CTLR & RCC_PLLRDY) == 0); // Wait till PLL is ready - RCC->CFGR0 = RCC_SW_PLL | RCC_HPRE_DIV1 | RCC_PLLSRC_HSE_Mul2; // Select PLL as system clock source - while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08); // Wait till PLL is used as system clock source -} - - - +#if defined( FUNCONF_USE_UARTPRINTF ) && FUNCONF_USE_UARTPRINTF void SetupUART( int uartBRR ) { // Enable GPIOD and UART. @@ -897,7 +853,6 @@ void SetupUART( int uartBRR ) USART1->CTLR1 |= CTLR1_UE_Set; } -#ifdef STDOUT_UART // For debug writing to the UART. int _write(int fd, const char *buf, int size) { @@ -915,7 +870,9 @@ int putchar(int c) USART1->DATAR = (const char)c; return 1; } -#else +#endif + +#if defined( FUNCONF_USE_DEBUGPRINTF ) && FUNCONF_USE_DEBUGPRINTF void handle_debug_input( int numbytes, uint8_t * data ) __attribute__((weak)); @@ -955,12 +912,12 @@ int _write(int fd, const char *buf, int size) char buffer[4] = { 0 }; int place = 0; uint32_t lastdmd; - uint32_t timeout = 160000; // Give up after ~40ms + uint32_t timeout = FUNCONF_DEBUGPRINTF_TIMEOUT; // Give up after ~40ms + if( size == 0 ) { - // Simply seeking input. lastdmd = (*DMDATA0); - if( lastdmd ) internal_handle_input( (uint32_t*)DMDATA0 ); + if( lastdmd && !(lastdmd&0x80) ) internal_handle_input( (uint32_t*)DMDATA0 ); } while( place < size ) { @@ -972,7 +929,7 @@ int _write(int fd, const char *buf, int size) if( lastdmd ) internal_handle_input( (uint32_t*)DMDATA0 ); - timeout = 160000; + timeout = FUNCONF_DEBUGPRINTF_TIMEOUT; int t = 3; while( t < tosend ) @@ -999,8 +956,16 @@ int _write(int fd, const char *buf, int size) // single to debug intf int putchar(int c) { - int timeout = 16000; + int timeout = FUNCONF_DEBUGPRINTF_TIMEOUT; uint32_t lastdmd = 0; + + while( ( lastdmd = (*DMDATA0) ) & 0x80 ) + if( timeout-- == 0 ) return 0; + + // Simply seeking input. + lastdmd = (*DMDATA0); + if( lastdmd ) internal_handle_input( (uint32_t*)DMDATA0 ); + while( (lastdmd = (*DMDATA0)) & 0x80 ) if( timeout-- == 0 ) return 0; if( lastdmd ) internal_handle_input( (uint32_t*)DMDATA0 ); *DMDATA0 = 0x85 | ((const char)c<<8); @@ -1027,6 +992,71 @@ void DelaySysTick( uint32_t n ) while( ((int32_t)( SysTick->CNT - targend )) < 0 ); } +void SystemInit() +{ +#if FUNCONF_HSE_BYPASS + #define HSEBYP (1<<18) +#else + #define HSEBYP 0 +#endif + + #if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL + #define BASE_CFGR0 RCC_HPRE_DIV1 | RCC_PLLSRC_HSI_Mul2 // HCLK = SYSCLK = APB1 And, enable PLL + #else + #define BASE_CFGR0 RCC_HPRE_DIV1 // HCLK = SYSCLK = APB1 And, no pll. + #endif + +#if defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI + #if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL + RCC->CFGR0 = BASE_CFGR0; + RCC->CTLR = RCC_HSION | RCC_PLLON | ((FUNCONF_HSITRIM) << 3); // Use HSI, but enable PLL. + #else + RCC->CFGR0 = BASE_CFGR0; // PLLCLK = HCLK = SYSCLK = APB1 + RCC->CTLR = RCC_HSION | ((FUNCONF_HSITRIM) << 3); // Use HSI, Only. + #endif +#endif + +#if defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE + + RCC->CTLR = RCC_HSION | RCC_HSEON | RCC_PLLON | HSEBYP; // Keep HSI and PLL on just in case, while turning on HSE + + // Values lifted from the EVT. There is little to no documentation on what this does. + while(!(RCC->CTLR&RCC_HSERDY)); + + #if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL + RCC->CFGR0 = BASE_CFGR0 | RCC_SW_HSE; + RCC->CTLR = RCC_HSEON | RCC_PLLON | HSEBYP; // Turn off HSI. + #else + RCC->CFGR0 = BASE_CFGR0 | RCC_SW_HSE; + RCC->CTLR = RCC_HSEON | HSEBYP; // Turn off PLL and HSI. + #endif +#endif + +#if FUNCONF_SYSTEM_CORE_CLOCK > 25000000 + FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; //+1 Cycle Latency +#else + FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; // +0 Cycle Latency +#endif + + + RCC->INTR = 0x009F0000; // Clear PLL, CSSC, HSE, HSI and LSI ready flags. + +#if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL + // From SetSysClockTo_48MHZ_HSI + while((RCC->CTLR & RCC_PLLRDY) == 0); // Wait till PLL is ready + RCC->CFGR0 = BASE_CFGR0 | RCC_SW_PLL; // Select PLL as system clock source + while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08); // Wait till PLL is used as system clock source +#endif + +#if defined( FUNCONF_USE_UARTPRINTF ) && FUNCONF_USE_UARTPRINTF + SetupUART( FUNCONF_UARTPRINTF_BAUD ); +#endif +#if defined( FUNCONF_USE_DEBUGPRINTF ) && FUNCONF_USE_DEBUGPRINTF + SetupDebugPrintf(); +#endif + +} + // C++ Support #ifdef CPLUSPLUS diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h index d0d04e24c1a85bf35508347b70efdc0c46537c29..dfd5e4efb75f67f3e2d4fe9fb62a0bca8e3d5403 100644 --- a/ch32v003fun/ch32v003fun.h +++ b/ch32v003fun/ch32v003fun.h @@ -1,66 +1,105 @@ -// This contains a copy of ch32v00x.h and core_riscv.h ch32v00x_conf.h and other misc functions +// This contains a copy of ch32v00x.h and core_riscv.h ch32v00x_conf.h and other misc functions See copyright notice at end. -/********************************** (C) COPYRIGHT ******************************* - * File Name : core_riscv.h - * Author : WCH - * Version : V1.0.0 - * Date : 2022/08/08 - * Description : RISC-V Core Peripheral Access Layer Header File - ********************************************************************************* - * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. - * Attention: This software (modified or not) and binary are used for - * microcontroller manufactured by Nanjing Qinheng Microelectronics. - *******************************************************************************/ -/* - * NOTE: This file modified by CNLohr to be fully-header-only. - */ - - -/* IO definitions */ -#ifdef __cplusplus - #define __I volatile /*!< defines 'read only' permissions */ -#else - #define __I volatile const /*!< defines 'read only' permissions */ +#ifndef __CH32V00x_H +#define __CH32V00x_H + +#include "funconfig.h" + +/***************************************************************************** + CH32V003 Fun Configs: + +#define FUNCONF_USE_PLL 1 // Use built-in 2x PLL +#define FUNCONF_USE_HSI 1 // Use HSI Internal Oscillator +#define FUNCONF_USE_HSE 0 // Use External Oscillator +#define FUNCONF_HSITRIM 0x10 // Use factory calibration on HSI Trim. +#define FUNCONF_SYSTEM_CORE_CLOCK 48000000 // Computed Clock in Hz. +#define FUNCONF_HSE_BYPASS 0 // Use HSE Bypass feature (for oscillator input) +#define FUNCONF_USE_DEBUGPRINTF 1 +#define FUNCONF_USE_UARTPRINTF 0 +#define FUNCONF_SYSTICK_USE_HCLK 0 // Should systick be at 48 MHz or 6MHz? +#define FUNCONF_TINYVECTOR 0 // If enabled, Does not allow normal interrupts. +#define FUNCONF_UARTPRINTF_BAUD 115200 // Only used if FUNCONF_USE_UARTPRINTF is set. +#define FUNCONF_DEBUGPRINTF_TIMEOUT 160000 // Arbitrary time units +*/ + +#if !defined(FUNCONF_USE_DEBUGPRINTF) && !defined(FUNCONF_USE_UARTPRINTF) + #define FUNCONF_USE_DEBUGPRINTF 1 #endif -#define __O volatile /*!< defines 'write only' permissions */ -#define __IO volatile /*!< defines 'read / write' permissions */ +#if defined(FUNCONF_USE_UARTPRINTF) && FUNCONF_USE_UARTPRINTF && !defined(FUNCONF_UART_PRINTF_BAUD) + #define FUNCONF_UART_PRINTF_BAUD 115200 +#endif +#if defined(FUNCONF_USE_DEBUGPRINTF) && FUNCONF_USE_DEBUGPRINTF && !defined(FUNCONF_DEBUGPRINTF_TIMEOUT) + #define FUNCONF_DEBUGPRINTF_TIMEOUT 160000 +#endif -/********************************** (C) COPYRIGHT ******************************* - * File Name : ch32v00x.h - * Author : WCH - * Version : V1.0.0 - * Date : 2022/08/08 - * Description : CH32V00x Device Peripheral Access Layer Header File. - ********************************************************************************* - * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. - * Attention: This software (modified or not) and binary are used for - * microcontroller manufactured by Nanjing Qinheng Microelectronics. - *******************************************************************************/ -#ifndef __CH32V00x_H -#define __CH32V00x_H +#if !defined( FUNCONF_USE_HSI ) && !defined( FUNCONF_USE_HSE ) + #define FUNCONF_USE_HSI 1 // Default to use HSI + #define FUNCONF_USE_HSE 0 +#endif -#ifdef __cplusplus -extern "C" { +#if !defined( FUNCONF_USE_PLL ) + #define FUNCONF_USE_PLL 1 // Default to use PLL +#endif + +#ifndef HSE_VALUE + #define HSE_VALUE (24000000) // Value of the External oscillator in Hz, default #endif +#ifndef HSI_VALUE + #define HSI_VALUE (24000000) // Value of the Internal oscillator in Hz, default. +#endif + +#ifndef FUNCONF_HSITRIM + #define FUNCONF_HSITRIM 0x10 // Default (Chip default) +#endif + +#ifndef FUNCONF_USE_PLL + #define FUNCONF_USE_PLL 1 // Default, Use PLL. +#endif + +#if !defined( FUNCONF_PLL_MULTIPLIER ) + #if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL + #define FUNCONF_PLL_MULTIPLIER 2 + #else + #define FUNCONF_PLL_MULTIPLIER 1 + #endif +#endif + +#ifndef FUNCONF_SYSTEM_CORE_CLOCK + #if defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI + #define FUNCONF_SYSTEM_CORE_CLOCK ((HSI_VALUE)*(FUNCONF_PLL_MULTIPLIER)) + #elif defined(FUNCONF_USE_HSE) && FUNCONF_USE_HSE + #define FUNCONF_SYSTEM_CORE_CLOCK ((HSE_VALUE)*(FUNCONF_PLL_MULTIPLIER)) + #else + #error Must define either FUNCONF_USE_HSI or FUNCONF_USE_HSE to be 1. + #endif +#endif + + +///////////////////////////////////////////////////////////////////////////////////////////////// +// Legacy, for EVT, CMSIS + #define __MPU_PRESENT 0 /* Other CH32 devices does not provide an MPU */ #define __Vendor_SysTickConfig 0 /* Set to 1 if different SysTick Config is used */ -#define HSE_VALUE ((uint32_t)24000000) /* Value of the External oscillator in Hz */ - -/* In the following line adjust the External High Speed oscillator (HSE) Startup Timeout value */ -#define HSE_STARTUP_TIMEOUT ((uint16_t)0x2000) /* Time out for HSE start up */ +#ifndef __ASSEMBLER__ // Things before this can be used in assembly. -#define HSI_VALUE ((uint32_t)24000000) /* Value of the Internal oscillator in Hz */ -#ifndef HSITRIM - #define HSITRIM 0x10 +#ifdef __cplusplus + #define __I volatile /*!< defines 'read only' permissions */ +#else + #define __I volatile const /*!< defines 'read only' permissions */ #endif +#define __O volatile /*!< defines 'write only' permissions */ +#define __IO volatile /*!< defines 'read / write' permissions */ -#ifndef __ASSEMBLER__ + +#ifdef __cplusplus +extern "C" { +#endif /* Interrupt Number Definition, according to the selected device */ typedef enum IRQn @@ -5008,18 +5047,16 @@ extern "C" { /* SYSTICK info * time on the ch32v003 is kept by the SysTick counter (32bit) - * by default, it will operate at (SYSTEM_CORE_CLOCK / 8) = 6MHz + * by default, it will operate at (FUNCONF_SYSTEM_CORE_CLOCK / 8) = 6MHz * more info at https://github.com/cnlohr/ch32v003fun/wiki/Time */ -#ifdef SYSTICK_USE_HCLK -#define DELAY_US_TIME ((SYSTEM_CORE_CLOCK)/1000000) -#define DELAY_MS_TIME ((SYSTEM_CORE_CLOCK)/1000) -#define SETUP_SYSTICK_HCLK SysTick->CTLR = 5; +#if defined( FUNCONF_SYSTICK_USE_HCLK ) && FUNCONF_SYSTICK_USE_HCLK +#define DELAY_US_TIME ((FUNCONF_SYSTEM_CORE_CLOCK)/1000000) +#define DELAY_MS_TIME ((FUNCONF_SYSTEM_CORE_CLOCK)/1000) #else // Use systick = hclk/8 -#define DELAY_US_TIME ((SYSTEM_CORE_CLOCK)/8000000) -#define DELAY_MS_TIME ((SYSTEM_CORE_CLOCK)/8000) -#define SETUP_SYSTICK_HCLK SysTick->CTLR = 1; +#define DELAY_US_TIME ((FUNCONF_SYSTEM_CORE_CLOCK)/8000000) +#define DELAY_MS_TIME ((FUNCONF_SYSTEM_CORE_CLOCK)/8000) #endif #define Delay_Us(n) DelaySysTick( (n) * DELAY_US_TIME ) @@ -5053,16 +5090,7 @@ void DelaySysTick( uint32_t n ); // Tricky: We need to make sure main and SystemInit() are preserved. int main() __attribute__((used)); -void SystemInit(void) __attribute__((used)); - -// Initialization functions -void SystemInit48HSI( void ); -void SystemInit24HSI( void ); // No PLL, just raw internal RC oscillator. - -// NOTE: HSEBYP is ORed with RCC_CTLR. Set it to RCC_HSEBYP or 0. -// If you are using an external oscillator, set it to RCC_HSEBYP. Otherwise, if you are using a crystal, it must be 0. -void SystemInitHSE( int HSEBYP ); -void SystemInitHSEPLL( int HSEBYP ); +void SystemInit(void); #define UART_BAUD_RATE 115200 #define OVER8DIV 4 @@ -5071,9 +5099,7 @@ void SystemInitHSEPLL( int HSEBYP ); #define UART_BRR ((((INTEGER_DIVIDER) / 100) << 4) | (((((FRACTIONAL_DIVIDER) * ((OVER8DIV)*2)) + 50)/100)&7)) // Put an output debug UART on Pin D5. // You can write to this with printf(...) or puts(...) -// Call with SetupUART( UART_BRR ) -void SetupUART( int uartBRR ); -void SetupDebugPrintf(); + void WaitForDebuggerToAttach(); // Just a definition to the internal _write function. @@ -5149,7 +5175,22 @@ Examples: +/* Copyright notice from original EVT. + ********************************** (C) COPYRIGHT ******************************* + * File Name : core_riscv.h + ch32v00x.h + * Author : WCH + * Version : V1.0.0 + * Date : 2022/08/08 + * Description : RISC-V Core Peripheral Access Layer Header File + ********************************************************************************* + * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. + * Attention: This software (modified or not) and binary are used for + * microcontroller manufactured by Nanjing Qinheng Microelectronics. + *******************************************************************************/ + + #ifdef __cplusplus }; #endif + diff --git a/examples/GPIO/GPIO.c b/examples/GPIO/GPIO.c index c8682e11eb136b43eaae5449cc498432c0a4ca88..0596daa33d3ac3e62405886916eb7e3e19281ebb 100644 --- a/examples/GPIO/GPIO.c +++ b/examples/GPIO/GPIO.c @@ -12,7 +12,6 @@ #error "please enable ONE of the demos by setting it to 1 and the others to 0" #endif - #include "ch32v003fun.h" #include "ch32v003_GPIO_branchless.h" diff --git a/examples/MCOtest/MCOtest.c b/examples/MCOtest/MCOtest.c index 9ded28816d075fc4ba3cff92dfd55f680c949112..aaf3b343dc38f03e6ba062a6140a1ec2b7b6ab25 100644 --- a/examples/MCOtest/MCOtest.c +++ b/examples/MCOtest/MCOtest.c @@ -8,8 +8,7 @@ int main() { uint32_t count, regtemp; - SystemInit48HSI(); - SetupUART( UART_BRR ); + SystemInit(); Delay_Ms(50); printf("\r\r\n\nTesting MCO output options.\r\n"); diff --git a/examples/MCOtest/Makefile b/examples/MCOtest/Makefile index 20c15bfcfb253f54a0d9aee1564603d1993d430b..92a4808fcc9b5fb990116e4a7f7713d14439d1e6 100644 --- a/examples/MCOtest/Makefile +++ b/examples/MCOtest/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=MCOtest -CFLAGS+=-DTINYVECTOR -DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/MCOtest/funconfig.h b/examples/MCOtest/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..16f4a824105f00c458bd8bf390a3e4ca79dfb7e8 --- /dev/null +++ b/examples/MCOtest/funconfig.h @@ -0,0 +1,8 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_TINYVECTOR 1 +#define CH32V003 1 + +#endif + diff --git a/examples/adc_dma_opamp/Makefile b/examples/adc_dma_opamp/Makefile index 55bdd4e2feca1e51cda9e4d03f6eb7ce97b0efce..c8fd9d8b3be1791af94d251300343d9841564fcf 100644 --- a/examples/adc_dma_opamp/Makefile +++ b/examples/adc_dma_opamp/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=adc_dma_opamp -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/adc_dma_opamp/adc_dma_opamp.c b/examples/adc_dma_opamp/adc_dma_opamp.c index e56bc2a836d46cb12a02812baebab4dde36e435c..638089c8d98e4d227ed8d353a8c827877478896e 100644 --- a/examples/adc_dma_opamp/adc_dma_opamp.c +++ b/examples/adc_dma_opamp/adc_dma_opamp.c @@ -3,10 +3,6 @@ * 04-13-2023 E. Brombaugh */ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> @@ -110,10 +106,9 @@ void opamp_init( void ) */ int main() { - SystemInit48HSI(); + SystemInit(); // start serial @ default 115200bps - SetupUART( UART_BRR ); Delay_Ms(100); printf("\r\r\n\nadc_dma_opamp example\n\r"); diff --git a/examples/adc_dma_opamp/funconfig.h b/examples/adc_dma_opamp/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/adc_dma_opamp/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/adc_polled/Makefile b/examples/adc_polled/Makefile index 42f80910cb00c3b46f4fb603c6d1fbbf959e68bd..b284c6db76b53f18ccb903419f440a35c9948cfb 100644 --- a/examples/adc_polled/Makefile +++ b/examples/adc_polled/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=adc_polled -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/adc_polled/adc_polled.c b/examples/adc_polled/adc_polled.c index 65ed99c9c261c7d7328f5e0e24c83b09bdf4faa9..06bbccf33eff00d1b687cae6864b27fe6a422349 100644 --- a/examples/adc_polled/adc_polled.c +++ b/examples/adc_polled/adc_polled.c @@ -3,10 +3,6 @@ * 03-27-2023 E. Brombaugh */ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> @@ -73,10 +69,8 @@ int main() { uint32_t count = 0; - SystemInit48HSI(); + SystemInit(); - // start serial @ default 115200bps - SetupUART( UART_BRR ); printf("\r\r\n\nadc_polled example\n\r"); // init systick @ 1ms rate diff --git a/examples/adc_polled/funconfig.h b/examples/adc_polled/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/adc_polled/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/blink/blink.bin b/examples/blink/blink.bin index b2c6174d1dac1759b26f345f4dde42f709e0e59e..19337c33b032497dfff8cb60516e83ac2d48e0f3 100755 Binary files a/examples/blink/blink.bin and b/examples/blink/blink.bin differ diff --git a/examples/blink/blink.c b/examples/blink/blink.c index 4a67a26a672cb03301edc4d4911f1259e9468268..c6ecdae5b2e1ddf4e71883865b7f1ee3c4aa644f 100644 --- a/examples/blink/blink.c +++ b/examples/blink/blink.c @@ -1,12 +1,9 @@ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> int main() { - SystemInit48HSI(); + SystemInit(); // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; diff --git a/examples/blink/funconfig.h b/examples/blink/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/blink/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/bootload/bootload.c b/examples/bootload/bootload.c index 67847cfb6486c83be5a87f0fd220cb6895afb2e9..5032b5558096314a1b668fb488de131e51bafb1a 100644 --- a/examples/bootload/bootload.c +++ b/examples/bootload/bootload.c @@ -24,7 +24,7 @@ uint32_t count; int main() { - SystemInit48HSI(); + SystemInit(); // From here, you can do whatever you'd like! // This code will live up at 0x1ffff000. @@ -58,7 +58,7 @@ int main() GPIOC->BSHR = 1<<16; // Turn off GPIOC0 } - for( i = 0; i < 3; i++ ) + for( i = 0; i < 1; i++ ) { GPIOD->BSHR = 1 | (1<<4); GPIOC->BSHR = 1; diff --git a/examples/bootload/funconfig.h b/examples/bootload/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..5a4fc164aff34b90353f9c9b8de0eb760b5a6f2e --- /dev/null +++ b/examples/bootload/funconfig.h @@ -0,0 +1,8 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_USE_DEBUGPRINTF 0 +#define CH32V003 1 + +#endif + diff --git a/examples/cpp_virtual_methods/Makefile b/examples/cpp_virtual_methods/Makefile index 749b3b594dc1d8623b2309ce03d26a0a28b4a39b..718f17038309d47107d5786b56faac01561d078e 100644 --- a/examples/cpp_virtual_methods/Makefile +++ b/examples/cpp_virtual_methods/Makefile @@ -20,7 +20,7 @@ CFLAGS:= \ -nostdlib \ -I. -Wall -CFLAGS+=-fno-rtti -DSTDOUT_UART -DCPLUSPLUS +CFLAGS+=-fno-rtti -DCPLUSPLUS flash : cv_flash -clean : cv_clean \ No newline at end of file +clean : cv_clean diff --git a/examples/cpp_virtual_methods/cpp_virtual_methods.cpp b/examples/cpp_virtual_methods/cpp_virtual_methods.cpp index e2c1d69153af63b6c03fb9cea9b0792ec7fddbf4..f6f1fc308f9de3158c840401a8d25449c7d4960f 100644 --- a/examples/cpp_virtual_methods/cpp_virtual_methods.cpp +++ b/examples/cpp_virtual_methods/cpp_virtual_methods.cpp @@ -12,10 +12,8 @@ #include <stdio.h> int main() { - SystemInit48HSI(); + SystemInit(); - // Setup UART @ 115200 baud - SetupUART(UART_BRR); Delay_Ms(100); printf("Begin example\n"); @@ -27,4 +25,4 @@ int main() { Example.doPrint(10); Delay_Ms(1000); } -} \ No newline at end of file +} diff --git a/examples/cpp_virtual_methods/funconfig.h b/examples/cpp_virtual_methods/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/cpp_virtual_methods/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/debugprintfdemo/debugprintfdemo.c b/examples/debugprintfdemo/debugprintfdemo.c index df4195147bb4bbb0bd8a273741e432c5425fa805..d7243d5e74c0127575a054b46090e646e7f08bd8 100644 --- a/examples/debugprintfdemo/debugprintfdemo.c +++ b/examples/debugprintfdemo/debugprintfdemo.c @@ -1,7 +1,6 @@ /* Small example showing how to use the SWIO programming pin to do printf through the debug interface */ -#define SYSTEM_CORE_CLOCK 48000000 #include "ch32v003fun.h" #include <stdio.h> @@ -16,8 +15,7 @@ void handle_debug_input( int numbytes, uint8_t * data ) int main() { - SystemInit48HSI(); - SetupDebugPrintf(); + SystemInit(); // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; diff --git a/examples/debugprintfdemo/funconfig.h b/examples/debugprintfdemo/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..5d54106e7eb8916fb5c41e2a3d5534fe24cf9907 --- /dev/null +++ b/examples/debugprintfdemo/funconfig.h @@ -0,0 +1,11 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +// Though this should be on by default we can extra force it on. +#define FUNCONF_USE_DEBUGPRINTF 1 +#define FUNCONF_DEBUGPRINTF_TIMEOUT (1<<31) // Wait for a very very long time. + +#define CH32V003 1 + +#endif + diff --git a/examples/direct_gpio/direct_gpio.c b/examples/direct_gpio/direct_gpio.c index 47d56f9f021f8486939bd7195201274c8f2c8cf7..313ef15be1a9d48c6c34154c2eb148005e144044 100644 --- a/examples/direct_gpio/direct_gpio.c +++ b/examples/direct_gpio/direct_gpio.c @@ -1,16 +1,11 @@ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> -#define APB_CLOCK SYSTEM_CORE_CLOCK - uint32_t count; int main() { - SystemInit48HSI(); + SystemInit(); // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOC; diff --git a/examples/direct_gpio/funconfig.h b/examples/direct_gpio/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/direct_gpio/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/dma_gpio/dma_gpio.c b/examples/dma_gpio/dma_gpio.c index bc8843338c0c24ccff8aa7104eac4644a34d553b..5f7a69886e840872d18f3260f325feb9932b1123 100644 --- a/examples/dma_gpio/dma_gpio.c +++ b/examples/dma_gpio/dma_gpio.c @@ -60,13 +60,11 @@ int main() { int i; - SystemInit48HSI(); + SystemInit(); // Reset all the peripherals we care about. RCC->APB2PRSTR = 0xffffffff; RCC->APB2PRSTR = 0; - - SetupDebugPrintf(); // Enable DMA RCC->AHBPCENR = RCC_AHBPeriph_SRAM | RCC_AHBPeriph_DMA1; diff --git a/examples/dma_gpio/funconfig.h b/examples/dma_gpio/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/dma_gpio/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/external_crystal/external_crystal.c b/examples/external_crystal/external_crystal.c index d568775b3bc9d6d539be16a65dc720af08af8842..36474b74a2d9311dc7e2e12882e09e1f395543b4 100644 --- a/examples/external_crystal/external_crystal.c +++ b/examples/external_crystal/external_crystal.c @@ -1,16 +1,11 @@ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 24000000 - #include "ch32v003fun.h" #include <stdio.h> -#define APB_CLOCK SYSTEM_CORE_CLOCK - uint32_t count; int main() { - SystemInitHSE( 0 ); + SystemInit(); // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; diff --git a/examples/external_crystal/funconfig.h b/examples/external_crystal/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..8ea009fff6988aebeb86138d5032a2fa92e3e1cc --- /dev/null +++ b/examples/external_crystal/funconfig.h @@ -0,0 +1,9 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_USE_HSE 1 // Use External Oscillator +#define FUNCONF_USE_PLL 0 // Do not use built-in 2x PLL +#define CH32V003 1 + +#endif + diff --git a/examples/exti_pin_change_isr/exti_pin_change_isr.c b/examples/exti_pin_change_isr/exti_pin_change_isr.c index f6ab80da03a23810c40044bf33fff2a7fa6de9d7..6e391475b98e099a684cdaf87ff78024b6ea5ed6 100644 --- a/examples/exti_pin_change_isr/exti_pin_change_isr.c +++ b/examples/exti_pin_change_isr/exti_pin_change_isr.c @@ -1,13 +1,8 @@ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> uint32_t count; -#define APB_CLOCK SYSTEM_CORE_CLOCK - /* This code uses fast interrupts, but be warned, in this mode your hardware stack is only 2 interrupt calls deep!!! @@ -49,7 +44,7 @@ void EXTI7_0_IRQHandler( void ) int main() { - SystemInit48HSI(); + SystemInit(); // Enable GPIOs RCC->APB2PCENR = RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO; diff --git a/examples/exti_pin_change_isr/funconfig.h b/examples/exti_pin_change_isr/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/exti_pin_change_isr/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/flashtest/flashtest.c b/examples/flashtest/flashtest.c index 435dcc1861cfedab59d74c900fed40efcf8e0811..012696a4388995977a02c8f0839963832ffdb0b4 100644 --- a/examples/flashtest/flashtest.c +++ b/examples/flashtest/flashtest.c @@ -1,21 +1,13 @@ -// DOES NOT WORK HALP!!!!!!!!!!!!!! - -#define SYSTEM_CORE_CLOCK 48000000 -#define SYSTICK_USE_HCLK - #include "ch32v003fun.h" #include <stdio.h> - int main() { int start; int stop; + int testok = 1; - SETUP_SYSTICK_HCLK - - SystemInit48HSI(); - SetupDebugPrintf(); + SystemInit(); Delay_Ms(100); @@ -29,6 +21,7 @@ int main() // FLASH->OBKEYR = 0x45670123; // FLASH->OBKEYR = 0xCDEF89AB; + // For unlocking programming, in general. FLASH->MODEKEYR = 0x45670123; FLASH->MODEKEYR = 0xCDEF89AB; @@ -40,8 +33,7 @@ int main() } uint32_t * ptr = (uint32_t*)0x08003700; - printf( "Memory at: %p: %08lx %08lx\n", ptr, ptr[0], ptr[1] ); - + printf( "Memory at: %08lx: %08lx %08lx\n", (uint32_t)ptr, ptr[0], ptr[1] ); printf( "FLASH->CTLR = %08lx\n", FLASH->CTLR ); @@ -59,6 +51,13 @@ int main() printf( "Memory at %p: %08lx %08lx\n", ptr, ptr[0], ptr[1] ); + if( ptr[0] != 0xffffffff ) + { + printf( "WARNING/FAILURE: Flash general erasure failed\n" ); + testok = 0; + } + + // Clear buffer and prep for flashing. FLASH->CTLR = CR_PAGE_PG; // synonym of FTPG. FLASH->CTLR = CR_BUF_RST | CR_PAGE_PG; @@ -95,9 +94,16 @@ int main() printf( "Memory at: %08lx: %08lx %08lx\n", (uint32_t)ptr, ptr[0], ptr[1] ); + + if( ptr[0] != 0xabcd1234 ) + { + printf( "WARNING/FAILURE: Flash general erasure failed\n" ); + testok = 0; + } + for( i = 0; i < 16; i++ ) printf( "%08lx ", ptr[i] ); - printf( "\n" ); + printf( "\n\nTest results: %s\n", testok?"PASS":"FAIL" ); while(1); } diff --git a/examples/flashtest/funconfig.h b/examples/flashtest/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..2f5a579e07af0ef609d9cf8a1562bb653f8c7227 --- /dev/null +++ b/examples/flashtest/funconfig.h @@ -0,0 +1,8 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 +#define FUNCONF_SYSTICK_USE_HCLK 1 + +#endif + diff --git a/examples/hsitrim/Makefile b/examples/hsitrim/Makefile index 4dd4badc90142f0278f0b17105663e1fee271bc8..bfa95b3aba54c92d883a69d7a02ab97b74fad3b5 100644 --- a/examples/hsitrim/Makefile +++ b/examples/hsitrim/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=hsitrim -CFLAGS+=-DTINYVECTOR -DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/hsitrim/funconfig.h b/examples/hsitrim/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..16f4a824105f00c458bd8bf390a3e4ca79dfb7e8 --- /dev/null +++ b/examples/hsitrim/funconfig.h @@ -0,0 +1,8 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_TINYVECTOR 1 +#define CH32V003 1 + +#endif + diff --git a/examples/hsitrim/hsitrim.c b/examples/hsitrim/hsitrim.c index da7e1141fd56c80827dee74c0ab67e4b6059e3ee..5a7562d975884933e09f98e222a287102e434a4c 100644 --- a/examples/hsitrim/hsitrim.c +++ b/examples/hsitrim/hsitrim.c @@ -1,6 +1,3 @@ -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> @@ -13,14 +10,19 @@ void set_trim(uint32_t trim) RCC->CTLR = regtemp; } +int lastkey = 0; +void handle_debug_input( int numbytes, uint8_t * data ) +{ + if( numbytes > 0 ) + lastkey = data[numbytes-1]; +} int main() { uint32_t trim, regtemp; uint8_t key; - SystemInit48HSI(); - SetupUART( UART_BRR ); + SystemInit(); Delay_Ms(50); printf("\r\r\n\nHSITRIM example.\r\n"); @@ -42,9 +44,9 @@ int main() RCC->APB2PCENR |= RCC_APB2Periph_GPIOC; - // PC4 is T1CH4, 50MHz Output PP CNF = 10: Mux PP, MODE = 11: Out 50MHz - GPIOC->CFGLR &= ~(GPIO_CFGLR_MODE4 | GPIO_CFGLR_CNF4); - GPIOC->CFGLR |= GPIO_CFGLR_CNF4_1 | GPIO_CFGLR_MODE4_0 | GPIO_CFGLR_MODE4_1; + // PC4 is T1CH4, 50MHz Output PP CNF = 10: Mux PP, MODE = 11: Out 50MHz + GPIOC->CFGLR &= ~(GPIO_CFGLR_MODE4 | GPIO_CFGLR_CNF4); + GPIOC->CFGLR |= GPIO_CFGLR_CNF4_1 | GPIO_CFGLR_MODE4_0 | GPIO_CFGLR_MODE4_1; printf("Enabling HSI signal with MCO function on PC4\r\n"); regtemp = (RCC->CFGR0 & ~RCC_CFGR0_MCO) | RCC_CFGR0_MCO_HSI; @@ -52,14 +54,15 @@ int main() // Setup UART RX pin SetupUART() does most of the work. ###beware### if you use DebugPrintf, this will break! // Input, GPIO D6, with AutoFunction - GPIOD->CFGLR &= ~(0xf<<(4*6)); - GPIOD->CFGLR |= (GPIO_CNF_IN_FLOATING)<<(4*6); + GPIOD->CFGLR &= ~(0xf<<(4*6)); + GPIOD->CFGLR |= (GPIO_CNF_IN_FLOATING)<<(4*6); USART1->CTLR1 |= USART_CTLR1_RE; printf("\r\nPress ',' to decrease HSITRIM. Press '.' to increase HSITRIM.\r\n"); while(1){ - if(USART1->STATR & USART_STATR_RXNE){ - key = (uint8_t)USART1->DATAR; + poll_input(); + key = lastkey; + if( key ){ if(key == '.'){ if(trim < 0x1f) trim++; printf("Setting HSITRIM to: 0x%02lX\r\n", trim); @@ -70,6 +73,7 @@ int main() printf("Setting HSITRIM to: 0x%02lX\r\n", trim); set_trim(trim); } + lastkey = 0; } } } diff --git a/examples/i2c_oled/Makefile b/examples/i2c_oled/Makefile index d5253892070f6b59e7b0d1016cc2077fdf5e6220..e0d78d56e50b7b73a3e75270c2c6f0cc5a0f58fb 100644 --- a/examples/i2c_oled/Makefile +++ b/examples/i2c_oled/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=i2c_oled -#CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/i2c_oled/funconfig.h b/examples/i2c_oled/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/i2c_oled/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/i2c_oled/i2c_oled.c b/examples/i2c_oled/i2c_oled.c index fd7a3bc3a2f88cf0dcd895d57a5469b1ea65bb7e..2de90a138394a461256d661cd0a13252494f70cc 100644 --- a/examples/i2c_oled/i2c_oled.c +++ b/examples/i2c_oled/i2c_oled.c @@ -3,10 +3,6 @@ * 03-29-2023 E. Brombaugh */ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - // what type of OLED - uncomment just one //#define SSD1306_64X32 #define SSD1306_128X32 @@ -22,15 +18,9 @@ int main() { // 48MHz internal clock - SystemInit48HSI(); + SystemInit(); - // start serial @ default 115200bps -#ifdef STDOUT_UART - SetupUART( UART_BRR ); Delay_Ms( 100 ); -#else - SetupDebugPrintf(); -#endif printf("\r\r\n\ni2c_oled example\n\r"); // init i2c and oled diff --git a/examples/i2c_oled/ssd1306_i2c.h b/examples/i2c_oled/ssd1306_i2c.h index 3ae7ce23b0277156cfb4571ed757eeb6c4b54c5f..023157f509126ecd1f2442ce42ef4e736c9bef24 100644 --- a/examples/i2c_oled/ssd1306_i2c.h +++ b/examples/i2c_oled/ssd1306_i2c.h @@ -48,22 +48,22 @@ void ssd1306_i2c_setup(void) // set freq tempreg = I2C1->CTLR2; tempreg &= ~I2C_CTLR2_FREQ; - tempreg |= (APB_CLOCK/SSD1306_I2C_PRERATE)&I2C_CTLR2_FREQ; + tempreg |= (FUNCONF_SYSTEM_CORE_CLOCK/SSD1306_I2C_PRERATE)&I2C_CTLR2_FREQ; I2C1->CTLR2 = tempreg; // Set clock config tempreg = 0; #if (SSD1306_I2C_CLKRATE <= 100000) // standard mode good to 100kHz - tempreg = (APB_CLOCK/(2*SSD1306_I2C_CLKRATE))&SSD1306_I2C_CKCFGR_CCR; + tempreg = (FUNCONF_SYSTEM_CORE_CLOCK/(2*SSD1306_I2C_CLKRATE))&SSD1306_I2C_CKCFGR_CCR; #else // fast mode over 100kHz #ifndef SSD1306_I2C_DUTY // 33% duty cycle - tempreg = (APB_CLOCK/(3*SSD1306_I2C_CLKRATE))&SSD1306_I2C_CKCFGR_CCR; + tempreg = (FUNCONF_SYSTEM_CORE_CLOCK/(3*SSD1306_I2C_CLKRATE))&SSD1306_I2C_CKCFGR_CCR; #else // 36% duty cycle - tempreg = (APB_CLOCK/(25*SSD1306_I2C_CLKRATE))&I2C_CKCFGR_CCR; + tempreg = (FUNCONF_SYSTEM_CORE_CLOCK/(25*SSD1306_I2C_CLKRATE))&I2C_CKCFGR_CCR; tempreg |= I2C_CKCFGR_DUTY; #endif tempreg |= I2C_CKCFGR_FS; diff --git a/examples/i2c_slave/funconfig.h b/examples/i2c_slave/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/i2c_slave/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/i2c_slave/i2c_slave.c b/examples/i2c_slave/i2c_slave.c index dbfaf80ec8448a67b4f5ab3e5ece38b1bb6c02d3..3f793628685160a60112e71f7eb0587872bd598a 100644 --- a/examples/i2c_slave/i2c_slave.c +++ b/examples/i2c_slave/i2c_slave.c @@ -1,5 +1,3 @@ -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include "i2c_slave.h" #include <stdio.h> @@ -11,8 +9,8 @@ volatile uint8_t i2c_registers[32] = {0x00}; int main() { - SystemInit48HSI(); - SetupDebugPrintf(); + SystemInit(); + SetupI2CSlave(0x9, i2c_registers, sizeof(i2c_registers)); // Enable GPIOD and set pin 0 to output diff --git a/examples/i2c_slave/i2c_slave.h b/examples/i2c_slave/i2c_slave.h index a488eaf8ea046d5ca80e395a2715650d9213dbeb..9b5f3f93a713e45c6bc8f4bd80be51dbd31d1ca3 100644 --- a/examples/i2c_slave/i2c_slave.h +++ b/examples/i2c_slave/i2c_slave.h @@ -29,7 +29,7 @@ #include <stdint.h> -#define APB_CLOCK SYSTEM_CORE_CLOCK +#define APB_CLOCK FUNCONF_SYSTEM_CORE_CLOCK struct _i2c_slave_state { uint8_t first_write; diff --git a/examples/input_capture/funconfig.h b/examples/input_capture/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/input_capture/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/input_capture/input_capture.c b/examples/input_capture/input_capture.c index 8e7ad5fc96443dd4ad1c15fa474c088763830df7..8d4048c8773723a0d1540d63cc1e8b73cd0c33aa 100644 --- a/examples/input_capture/input_capture.c +++ b/examples/input_capture/input_capture.c @@ -1,12 +1,8 @@ /* Small example showing how to capture timer values on gpio edges */ -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> -#define APB_CLOCK SYSTEM_CORE_CLOCK - #define queuelen 16 volatile uint32_t captureVals[queuelen]; volatile size_t read = 0; @@ -56,8 +52,7 @@ void TIM1_CC_IRQHandler(void) int main() { - SystemInit48HSI(); - SetupDebugPrintf(); + SystemInit(); Delay_Ms(100); diff --git a/examples/iwdg/funconfig.h b/examples/iwdg/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/iwdg/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/iwdg/iwdg.c b/examples/iwdg/iwdg.c index 886f2778eedfa6d29a06c33b9ea204e2c8769554..911893efadc8ee217358d5143f2d26bf67359405 100644 --- a/examples/iwdg/iwdg.c +++ b/examples/iwdg/iwdg.c @@ -1,6 +1,3 @@ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> @@ -30,7 +27,7 @@ static void gpios_off() { int main() { - SystemInit48HSI(); + SystemInit(); // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; diff --git a/examples/optionbytes/Makefile b/examples/optionbytes/Makefile index 5e49195c82302282314686ee3f33112907876366..2406eb55d12711b177fbd50fa19c27797a695753 100644 --- a/examples/optionbytes/Makefile +++ b/examples/optionbytes/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=optionbytes -CFLAGS+=-DTINYVECTOR - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/optionbytes/funconfig.h b/examples/optionbytes/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..16f4a824105f00c458bd8bf390a3e4ca79dfb7e8 --- /dev/null +++ b/examples/optionbytes/funconfig.h @@ -0,0 +1,8 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_TINYVECTOR 1 +#define CH32V003 1 + +#endif + diff --git a/examples/optionbytes/optionbytes.c b/examples/optionbytes/optionbytes.c index 24b10b8694deab8009fdd50c84deaf99591f9635..cbc5d09d0d0a07dc6f71fe7eef0e736f26b774ca 100644 --- a/examples/optionbytes/optionbytes.c +++ b/examples/optionbytes/optionbytes.c @@ -13,9 +13,6 @@ * microcontroller manufactured by Nanjing Qinheng Microelectronics. *******************************************************************************/ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> @@ -25,8 +22,9 @@ int FLASH_WaitForLastOperation(uint32_t Timeout); int main() { - SystemInit48HSI(); - SetupDebugPrintf(); + SystemInit(); + + Delay_Ms( 100 ); FLASH->OBKEYR = FLASH_KEY1; FLASH->OBKEYR = FLASH_KEY2; diff --git a/examples/optiondata/funconfig.h b/examples/optiondata/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/optiondata/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/optiondata/optiondata.c b/examples/optiondata/optiondata.c index a3c8f273c0a07d9416ff30963cacea91651b6b17..a3627f7412b5f3323c01818ce5b6acc47b74c013 100644 --- a/examples/optiondata/optiondata.c +++ b/examples/optiondata/optiondata.c @@ -8,8 +8,6 @@ // June 13, 2023 Mats Engstrom (github.com/mengstr) // -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> void FlashOptionData(uint8_t data0, uint8_t data1) { @@ -61,8 +59,7 @@ void FlashOptionData(uint8_t data0, uint8_t data1) { // // int main() { - SystemInit48HSI(); - SetupDebugPrintf(); + SystemInit(); Delay_Ms(250); uint8_t bootcnt=OB->Data0; diff --git a/examples/run_from_ram/Makefile b/examples/run_from_ram/Makefile index 4743de508184371cd9862e198165dc427a57cb95..d29c74470e82986d8da8322e3eefbafc2797679c 100644 --- a/examples/run_from_ram/Makefile +++ b/examples/run_from_ram/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=run_from_ram -CFLAGS+=-DTINYVECTOR - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/run_from_ram/funconfig.h b/examples/run_from_ram/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..16f4a824105f00c458bd8bf390a3e4ca79dfb7e8 --- /dev/null +++ b/examples/run_from_ram/funconfig.h @@ -0,0 +1,8 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_TINYVECTOR 1 +#define CH32V003 1 + +#endif + diff --git a/examples/run_from_ram/run_from_ram.c b/examples/run_from_ram/run_from_ram.c index 34c25123bb112e46e535919b5766dad3dd2cbfdc..8d5259061355d9fbf7cd6979b9f3cdc0cb056575 100644 --- a/examples/run_from_ram/run_from_ram.c +++ b/examples/run_from_ram/run_from_ram.c @@ -1,6 +1,4 @@ // Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> @@ -59,7 +57,7 @@ void RamFunction() int main() { - SystemInit48HSI(); + SystemInit(); // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; diff --git a/examples/self_modify_code/funconfig.h b/examples/self_modify_code/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..16f4a824105f00c458bd8bf390a3e4ca79dfb7e8 --- /dev/null +++ b/examples/self_modify_code/funconfig.h @@ -0,0 +1,8 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_TINYVECTOR 1 +#define CH32V003 1 + +#endif + diff --git a/examples/self_modify_code/self_modify_code.c b/examples/self_modify_code/self_modify_code.c index e4a78ae67dc7098759bfa10b82d969605fe2ba27..c0155eaf99c98172820bef8bbe4711b06088e3a1 100644 --- a/examples/self_modify_code/self_modify_code.c +++ b/examples/self_modify_code/self_modify_code.c @@ -84,8 +84,7 @@ uint32_t ReadCSRSelfModifySimple( uint16_t whichcsr ) int main() { - SystemInit48HSI(); - SetupDebugPrintf(); + SystemInit(); WaitForDebuggerToAttach(); diff --git a/examples/spi_24L01_rx/Makefile b/examples/spi_24L01_rx/Makefile index 5b54c64b56f41e500746d29d60f3dc740ad132dc..26764f3736482fa708bfdbfdc1d68fd459b88a23 100644 --- a/examples/spi_24L01_rx/Makefile +++ b/examples/spi_24L01_rx/Makefile @@ -3,8 +3,6 @@ all : flash TARGET:=spi_24L01_rx ADDITIONAL_C_FILES+=nrf24l01_low_level.c nrf24l01.c -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/spi_24L01_rx/funconfig.h b/examples/spi_24L01_rx/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/spi_24L01_rx/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/spi_24L01_rx/spi_24L01_rx.c b/examples/spi_24L01_rx/spi_24L01_rx.c index f7c81499f04447821be5d236ab141d88fb89f757..81d325720bedb6eae4f4126eb59754c18a58348d 100644 --- a/examples/spi_24L01_rx/spi_24L01_rx.c +++ b/examples/spi_24L01_rx/spi_24L01_rx.c @@ -3,9 +3,6 @@ * 04-26-2023 recallmenot */ -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> #include "nrf24l01.h" @@ -105,10 +102,8 @@ void receive() { int main() { - SystemInit48HSI(); + SystemInit(); - // start serial @ default 115200bps - SetupUART( UART_BRR ); Delay_Ms( 100 ); printf("\r\r\n\nspi_24L01_RX\n\r"); diff --git a/examples/spi_24L01_tx/Makefile b/examples/spi_24L01_tx/Makefile index 6ad3d51350ca002205f5ad25ed107cfdf4086b2a..c657924a080ce14c674c51daf064630864a89a69 100644 --- a/examples/spi_24L01_tx/Makefile +++ b/examples/spi_24L01_tx/Makefile @@ -3,8 +3,6 @@ all : flash TARGET:=spi_24L01_tx ADDITIONAL_C_FILES+=nrf24l01_low_level.c nrf24l01.c -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/spi_24L01_tx/funconfig.h b/examples/spi_24L01_tx/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/spi_24L01_tx/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/spi_24L01_tx/spi_24L01_tx.c b/examples/spi_24L01_tx/spi_24L01_tx.c index 16a1c7ee4c73aa698446d48a20cdab072113a58d..b25b7a8a48a016237099d48d74fafe78c5030984 100644 --- a/examples/spi_24L01_tx/spi_24L01_tx.c +++ b/examples/spi_24L01_tx/spi_24L01_tx.c @@ -3,9 +3,6 @@ * 04-26-2023 recallmenot */ -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> #include "nrf24l01.h" @@ -116,10 +113,9 @@ void send() { int main() { - SystemInit48HSI(); + SystemInit(); - // start serial @ default 115200bps - SetupUART( UART_BRR ); + Delay_Ms(100); printf("\r\r\n\nspi_24L01_TX\n\r"); diff --git a/examples/spi_dac/Makefile b/examples/spi_dac/Makefile index c7ef196154ca50823da2985083a574352b921fab..18ab741736f368153b5f08fe70d9ed2ca17b7c84 100644 --- a/examples/spi_dac/Makefile +++ b/examples/spi_dac/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=spi_dac -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/spi_dac/funconfig.h b/examples/spi_dac/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/spi_dac/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/spi_dac/spi_dac.c b/examples/spi_dac/spi_dac.c index aaed51fc769d463ec64a9e4d4eb949362b86a6d8..0298ef87c8f07dfa1f93f937228eb98d29c40e35 100644 --- a/examples/spi_dac/spi_dac.c +++ b/examples/spi_dac/spi_dac.c @@ -3,10 +3,6 @@ * 04-10-2023 E. Brombaugh */ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> @@ -192,10 +188,9 @@ void DMA1_Channel4_IRQHandler( void ) */ int main() { - SystemInit48HSI(); + SystemInit(); // start serial @ default 115200bps - SetupUART( UART_BRR ); Delay_Ms( 100 ); printf("\r\r\n\nspi_dac example\n\r"); diff --git a/examples/spi_oled/Makefile b/examples/spi_oled/Makefile index 77b7065cb887fa2af72cdfcb440dcd92c528fee0..805e3ae30ba2f8732459eceb65f1eadfa0e9dd0e 100644 --- a/examples/spi_oled/Makefile +++ b/examples/spi_oled/Makefile @@ -3,7 +3,6 @@ all : flash TARGET:=spi_oled CFLAGS+=-I../i2c_oled -#CFLAGS+=-DSTDOUT_UART include ../../ch32v003fun/ch32v003fun.mk diff --git a/examples/spi_oled/spi_oled.c b/examples/spi_oled/spi_oled.c index f5231e8833bc00c821be9c87fb8749363ed3125e..c0cbf5ad4ef06dc0b50733a67ddbd80644660fc8 100644 --- a/examples/spi_oled/spi_oled.c +++ b/examples/spi_oled/spi_oled.c @@ -3,10 +3,6 @@ * 03-29-2023 E. Brombaugh */ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - // what type of OLED - uncomment just one //#define SSD1306_64X32 //#define SSD1306_128X32 @@ -131,15 +127,9 @@ int count = 0; int main() { // 48MHz internal clock - SystemInit48HSI(); + SystemInit(); - // start serial @ default 115200bps -#ifdef STDOUT_UART - SetupUART( UART_BRR ); Delay_Ms( 100 ); -#else - SetupDebugPrintf(); -#endif printf("\r\r\n\nspi_oled example\n\r"); // init spi and oled diff --git a/examples/standby_autowake/Makefile b/examples/standby_autowake/Makefile index 782271405d709df21ca8a06b9250d2bdf4328c10..f289aec2984de4c877a9aaba1059936d619ee8a4 100644 --- a/examples/standby_autowake/Makefile +++ b/examples/standby_autowake/Makefile @@ -2,7 +2,6 @@ all : flash TARGET:=standby_autowake -CFLAGS+=-DSTDOUT_UART include ../../ch32v003fun/ch32v003fun.mk diff --git a/examples/standby_autowake/funconfig.h b/examples/standby_autowake/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/standby_autowake/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/standby_autowake/standby_autowake.c b/examples/standby_autowake/standby_autowake.c index cc0fe4295b82faadc151d4331f38cc43bbea3959..301e163ff3a7bde192c7f2dfeb3262f348d53f00 100644 --- a/examples/standby_autowake/standby_autowake.c +++ b/examples/standby_autowake/standby_autowake.c @@ -16,8 +16,8 @@ void AWU_IRQHandler( void ) { int main() { - SystemInit48HSI(); - SetupUART( UART_BRR ); + SystemInit(); + Delay_Ms(100); printf("\r\n\r\nlow power example\r\n\r\n"); @@ -64,7 +64,7 @@ int main() for (;;) { __WFE(); // restore clock to full speed - SystemInit48HSI(); + SystemInit(); printf("\r\nawake, %u\r\n", counter++); GPIOD->OUTDR ^= (1 << 4); } diff --git a/examples/standby_btn/Makefile b/examples/standby_btn/Makefile index 36f69f6e37bcf583949d8bbf90a118f928c7a70d..82b40f2bbb9df982ec11f237ff173cebc9d8abcf 100644 --- a/examples/standby_btn/Makefile +++ b/examples/standby_btn/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=standby_btn -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/standby_btn/funconfig.h b/examples/standby_btn/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/standby_btn/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/standby_btn/standby_btn.c b/examples/standby_btn/standby_btn.c index 3a6cd7252127df5407fc5ac4ed54cfd9494de913..2c86bc1973dfc0101ff15aaa9095f4fa9627e5be 100644 --- a/examples/standby_btn/standby_btn.c +++ b/examples/standby_btn/standby_btn.c @@ -16,10 +16,10 @@ void EXTI7_0_IRQHandler( void ) { int main() { - SystemInit48HSI(); - SetupUART( UART_BRR ); + SystemInit(); + Delay_Ms(100); - printf("\r\n\r\nlow power example\r\n\r\n"); + printf("\n\nlow power example\n\n"); RCC->APB2PCENR |= RCC_APB2Periph_GPIOD; // GPIO D4 Push-Pull @@ -53,13 +53,13 @@ int main() PFIC->SCTLR |= (1 << 2); uint16_t counter = 0; - printf("entering sleep loop\r\n"); + printf("entering sleep loop\n"); for (;;) { __WFE(); // restore clock to full speed - SystemInit48HSI(); - printf("\r\nawake, %u\r\n", counter++); + SystemInit(); + printf("\nawake, %u\n", counter++); GPIOD->OUTDR ^= (1 << 4); } } diff --git a/examples/struct_direct_gpio/funconfig.h b/examples/struct_direct_gpio/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/struct_direct_gpio/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/struct_direct_gpio/struct_direct_gpio.c b/examples/struct_direct_gpio/struct_direct_gpio.c index 9c05c6bf3b6460bd9cb21581870125e53ef913a3..f8ab71ae68cd3f05654fa26a11d8e37b2bc740ae 100644 --- a/examples/struct_direct_gpio/struct_direct_gpio.c +++ b/examples/struct_direct_gpio/struct_direct_gpio.c @@ -1,18 +1,12 @@ // This tries to generates exacly the same assambly as direct_gpio but using structs as much as possible - -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> -#define APB_CLOCK SYSTEM_CORE_CLOCK - uint32_t count; int main() { - SystemInit48HSI(); + SystemInit(); // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOC; diff --git a/examples/struct_gpio/funconfig.h b/examples/struct_gpio/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/struct_gpio/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/struct_gpio/struct_gpio.c b/examples/struct_gpio/struct_gpio.c index dee26554c964e1505a474aad4adc34cf35daa53f..27cf7157d51afcd2705aa98701842648cc36eff4 100644 --- a/examples/struct_gpio/struct_gpio.c +++ b/examples/struct_gpio/struct_gpio.c @@ -1,18 +1,11 @@ /* Small example showing how to use structs for controling GPIO pins */ -#define SYSTEM_CORE_CLOCK 48000000 - #include "ch32v003fun.h" #include <stdio.h> -#define APB_CLOCK SYSTEM_CORE_CLOCK - -uint32_t count; - int main() { - SystemInit48HSI(); - SetupDebugPrintf(); + SystemInit(); // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; diff --git a/examples/systick_irq/funconfig.h b/examples/systick_irq/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..bff4e047cf5eddaa25c38b1ff1027636eb9f725d --- /dev/null +++ b/examples/systick_irq/funconfig.h @@ -0,0 +1,8 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_SYSTICK_USE_HCLK 1 +#define CH32V003 1 + +#endif + diff --git a/examples/systick_irq/systick_irq.c b/examples/systick_irq/systick_irq.c index 26d5de556e7119d2c0eaa8e3b1f00f7c7a5b9253..59c58d7b6a9d6d0418f8cef3b2217643bbb41ac1 100644 --- a/examples/systick_irq/systick_irq.c +++ b/examples/systick_irq/systick_irq.c @@ -4,11 +4,6 @@ * 05-12-2023 C. Lohr (Modified to reflect updated sysclk) */ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define SYSTICK_USE_HCLK -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> @@ -34,7 +29,7 @@ void systick_init(void) NVIC_EnableIRQ(SysTicK_IRQn); /* Set the tick interval to 1ms for normal op */ - SysTick->CMP = (SYSTEM_CORE_CLOCK/1000)-1; + SysTick->CMP = (FUNCONF_SYSTEM_CORE_CLOCK/1000)-1; /* Start at zero */ SysTick->CNT = 0; @@ -56,7 +51,7 @@ void SysTick_Handler(void) // as a warning, if more than this length of time // passes before triggering, you may miss your // interrupt. - SysTick->CMP += (SYSTEM_CORE_CLOCK/1000); + SysTick->CMP += (FUNCONF_SYSTEM_CORE_CLOCK/1000); /* clear IRQ */ SysTick->SR = 0; @@ -72,9 +67,9 @@ int main() { uint32_t count = 0; - SystemInit48HSI(); + SystemInit(); + Delay_Ms(100); - SetupDebugPrintf(); printf("\r\r\n\nsystick_irq example\n\r"); // init systick @ 1ms rate diff --git a/examples/template/funconfig.h b/examples/template/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/template/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/template/template.c b/examples/template/template.c index d81e4eb93d7f614b29f42e96753ef74f604a2c13..f4ff7fa8dfdcdf92944cb769f035d95fabff61d3 100644 --- a/examples/template/template.c +++ b/examples/template/template.c @@ -1,5 +1,4 @@ /* Template app on which you can build your own. */ -#define SYSTEM_CORE_CLOCK 48000000 #include "ch32v003fun.h" #include <stdio.h> @@ -8,8 +7,7 @@ uint32_t count; int main() { - SystemInit48HSI(); - SetupDebugPrintf(); + SystemInit(); // Enable GPIOs RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC; diff --git a/examples/tim1_pwm/Makefile b/examples/tim1_pwm/Makefile index 8ece937bcbdd7344cba03fbe07fdf62863e999ff..a88503506b734c699d2b948e89983b77c998a0e6 100644 --- a/examples/tim1_pwm/Makefile +++ b/examples/tim1_pwm/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=tim1_pwm -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/tim1_pwm/funconfig.h b/examples/tim1_pwm/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/tim1_pwm/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/tim1_pwm/tim1_pwm.c b/examples/tim1_pwm/tim1_pwm.c index 1ead833489df1901e18f63e7f28f4009783527e7..b063f8ebb4efa9408fd9b09e9b53f7938aa8fa01 100644 --- a/examples/tim1_pwm/tim1_pwm.c +++ b/examples/tim1_pwm/tim1_pwm.c @@ -3,10 +3,6 @@ * 03-28-2023 E. Brombaugh */ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> @@ -113,11 +109,9 @@ int main() { uint32_t count = 0; - SystemInit48HSI(); - - // start serial @ default 115200bps - SetupUART( UART_BRR ); + SystemInit(); Delay_Ms( 100 ); + printf("\r\r\n\ntim1_pwm example\n\r"); // init TIM1 for PWM diff --git a/examples/tim2_pwm/Makefile b/examples/tim2_pwm/Makefile index 7b3d0c031265e2b750553f45da8dfce649a99c68..44ebf7b02ac62b953d784ce15f51c5dfe7f04458 100644 --- a/examples/tim2_pwm/Makefile +++ b/examples/tim2_pwm/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=tim2_pwm -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/tim2_pwm/funconfig.h b/examples/tim2_pwm/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/tim2_pwm/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/tim2_pwm/tim2_pwm.c b/examples/tim2_pwm/tim2_pwm.c index 37e949d727dfd01883b1271ff9bb9e41bac54b36..2e0911e5f8aa7ad248ba63c6113011476cb15ba8 100644 --- a/examples/tim2_pwm/tim2_pwm.c +++ b/examples/tim2_pwm/tim2_pwm.c @@ -28,10 +28,6 @@ Timer 2 pin mappings by AFIO->PCFR1 D5 T2CH4_ */ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> @@ -107,11 +103,9 @@ int main() { uint32_t count = 0; - SystemInit48HSI(); - - // start serial @ default 115200bps - SetupUART( UART_BRR ); + SystemInit(); Delay_Ms( 100 ); + printf("\r\r\n\ntim2_pwm example\n\r"); // init TIM2 for PWM diff --git a/examples/tim2_pwm_remap/Makefile b/examples/tim2_pwm_remap/Makefile index 36df10e860825f2e0d005debbe3cbcb791b2b591..ea40f6a94644133e6c01115cf117d34f98076dc3 100644 --- a/examples/tim2_pwm_remap/Makefile +++ b/examples/tim2_pwm_remap/Makefile @@ -2,7 +2,6 @@ all : flash TARGET:=tim2_pwm_remap - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/tim2_pwm_remap/funconfig.h b/examples/tim2_pwm_remap/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/tim2_pwm_remap/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/tim2_pwm_remap/tim2_pwm_remap.c b/examples/tim2_pwm_remap/tim2_pwm_remap.c index 1d2c4bbfa43fab930c8ccdbd69b776b7e84e33dd..b48fd3b62f35d25c97ea39758a450f3f7cf47c61 100644 --- a/examples/tim2_pwm_remap/tim2_pwm_remap.c +++ b/examples/tim2_pwm_remap/tim2_pwm_remap.c @@ -42,10 +42,6 @@ Timer 2 pin mappings by AFIO->PCFR1 D5 T2CH4_ */ -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include <stdio.h> @@ -147,11 +143,7 @@ int main() { uint32_t count = 0; - SystemInit48HSI(); - - // start serial @ default 115200bps - SetupDebugPrintf(); - + SystemInit(); Delay_Ms( 100 ); printf("\r\r\n\ntim2_pwm example, with remap\n\r"); diff --git a/examples/uartdemo/funconfig.h b/examples/uartdemo/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..8c1e7c730d1afa5fae2a4d3d117f9d2a16415c7c --- /dev/null +++ b/examples/uartdemo/funconfig.h @@ -0,0 +1,10 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 +#define FUNCONF_USE_DEBUGPRINTF 0 +#define FUNCONF_USE_UARTPRINTF 1 +#define FUNCONF_UARTPRINTF_BAUD 115200 + +#endif + diff --git a/examples/uartdemo/uartdemo.c b/examples/uartdemo/uartdemo.c index 3907a466357e4a8f8eb14b56a6df01b0898c0f96..9d9bf0a2d1179fa3ef8633cf866cfd65fd826b97 100644 --- a/examples/uartdemo/uartdemo.c +++ b/examples/uartdemo/uartdemo.c @@ -2,9 +2,7 @@ // Doesn't rely on any of the weird HAL stuff from CH // Final executable is ~1/4th the size. -// Could be defined here, or in the processor defines. -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK +// See funconfig.h to see how this uses the uart. #include "ch32v003fun.h" #include <stdio.h> @@ -13,8 +11,7 @@ uint32_t count; int main() { - SystemInit48HSI(); - SetupUART( UART_BRR ); + SystemInit(); // Enable GPIOD. RCC->APB2PCENR |= RCC_APB2Periph_GPIOD; diff --git a/examples/ws2812bdemo/funconfig.h b/examples/ws2812bdemo/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..998cf76fede1479f162c72ef34d3a4d747430093 --- /dev/null +++ b/examples/ws2812bdemo/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define CH32V003 1 + +#endif + diff --git a/examples/ws2812bdemo/ws2812bdemo.c b/examples/ws2812bdemo/ws2812bdemo.c index 2f462d0f1cbddba7e7a399299c752c58d45c98f8..f1b1a60bf6f839f23a51314c038fd836a9e30815 100644 --- a/examples/ws2812bdemo/ws2812bdemo.c +++ b/examples/ws2812bdemo/ws2812bdemo.c @@ -38,7 +38,7 @@ uint32_t WS2812BLEDCallback( int ledno ) int main() { int k; - SystemInit48HSI(); + SystemInit(); // Enable GPIOD (for debugging) RCC->APB2PCENR |= RCC_APB2Periph_GPIOD; diff --git a/minichlink/Makefile b/minichlink/Makefile index 964341c11b51b66accf732be82639d887df7c0cd..8f215a19d888b8e592adc1a8d338f80640ce8c21 100644 --- a/minichlink/Makefile +++ b/minichlink/Makefile @@ -1,6 +1,6 @@ TOOLS:=minichlink minichlink.so -CFLAGS:=-O0 -g3 -Wall +CFLAGS:=-O0 -g3 -Wall -I. C_S:=minichlink.c pgm-wch-linke.c pgm-esp32s2-ch32xx.c nhc-link042.c ardulink.c serial_dev.c pgm-b003fun.c minichgdb.c # General Note: To use with GDB, gdb-multiarch @@ -9,7 +9,7 @@ C_S:=minichlink.c pgm-wch-linke.c pgm-esp32s2-ch32xx.c nhc-link042.c ardulink.c ifeq ($(OS),Windows_NT) LDFLAGS:=-L. -lpthread -lusb-1.0 -lsetupapi -lws2_32 - CFLAGS:=-Os -s -Wall -D_WIN32_WINNT=0x0600 + CFLAGS:=-Os -s -Wall -D_WIN32_WINNT=0x0600 -I. TOOLS:=minichlink.exe else OS_NAME := $(shell uname -s | tr A-Z a-z) @@ -18,7 +18,7 @@ else endif ifeq ($(OS_NAME),darwin) LDFLAGS:=-lpthread -lusb-1.0 -framework CoreFoundation -framework IOKit - CFLAGS:=-O0 -Wall -Wno-asm-operand-widths -Wno-deprecated-declarations -Wno-deprecated-non-prototype -D__MACOSX__ + CFLAGS:=-O0 -Wall -Wno-asm-operand-widths -Wno-deprecated-declarations -Wno-deprecated-non-prototype -D__MACOSX__ -I. INCLUDES:=$(shell pkg-config --cflags-only-I libusb-1.0) LIBINCLUDES:=$(shell pkg-config --libs-only-L libusb-1.0) INCS:=$(INCLUDES) $(LIBINCLUDES) diff --git a/minichlink/funconfig.h b/minichlink/funconfig.h new file mode 100644 index 0000000000000000000000000000000000000000..92957a25d8bcd805fa0de7f0a156532ad9b04366 --- /dev/null +++ b/minichlink/funconfig.h @@ -0,0 +1,7 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define MINICHLINK 1 + +#endif + diff --git a/platformio.ini b/platformio.ini index 498c9b2dd0204d159f5d35d9cf586e6c2397753c..2da7e046764f462fbbd88db22cbd698f9bd912c4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,7 +15,9 @@ extends = fun_base ; for examples that use ch32v003fun as their base [fun_base] board_build.ldscript = ch32v003fun/ch32v003fun.ld -build_flags = -flto -Ich32v003fun -I/usr/include/newlib -DTINYVECTOR -lgcc -Iextralibs +build_flags = -flto -Ich32v003fun -I/usr/include/newlib -lgcc -Iextralibs +; dynamically add "examples/$PIOENV" to the include path +extra_scripts = add_include.py build_src_filter = +<ch32v003fun> extra_libs_srcs = +<extralibs>