From f2ca15af943a89ebfc4c087207a20643bd0f1957 Mon Sep 17 00:00:00 2001 From: cnlohr <lohr85@gmail.com> Date: Wed, 28 Jun 2023 04:59:36 -0400 Subject: [PATCH] Major change for adding funconfig. * No more needing to decide between printf and UART in code. * Better debugprintf input * Unified code for all clock sources. * TINYVECTOR now saves like another 120 bytes of space. * None of the `Makefile`s except `bootloader` need anything extra * All configuration via `funconfig.h` * Sane defaults. * `SystemInit()` also initializes debugging (UART or SWIO depending on configuration) * Tunable timeout on debug printf. * Configurable UART baud rate. * `funconfig.h` is automatically included by `ch32v003fun.h` * Paves the way for other, non-003 processors. --- ch32v003fun/ch32v003fun.c | 160 +++++++++------- ch32v003fun/ch32v003fun.h | 177 +++++++++++------- examples/GPIO/GPIO.c | 6 +- examples/GPIO/Makefile | 3 - examples/GPIO/funconfig.h | 8 + examples/MCOtest/MCOtest.c | 3 +- examples/MCOtest/Makefile | 2 - examples/MCOtest/funconfig.h | 8 + examples/adc_dma_opamp/Makefile | 2 - examples/adc_dma_opamp/adc_dma_opamp.c | 7 +- examples/adc_dma_opamp/funconfig.h | 7 + examples/adc_polled/Makefile | 2 - examples/adc_polled/adc_polled.c | 8 +- examples/adc_polled/funconfig.h | 7 + examples/blink/blink.bin | Bin 500 -> 520 bytes examples/blink/blink.c | 5 +- examples/blink/funconfig.h | 7 + examples/bootload/bootload.c | 4 +- examples/bootload/funconfig.h | 8 + examples/cpp_virtual_methods/Makefile | 4 +- .../cpp_virtual_methods.cpp | 6 +- examples/cpp_virtual_methods/funconfig.h | 7 + examples/debugprintfdemo/debugprintfdemo.c | 4 +- examples/debugprintfdemo/funconfig.h | 12 ++ examples/direct_gpio/direct_gpio.c | 7 +- examples/direct_gpio/funconfig.h | 7 + examples/dma_gpio/dma_gpio.c | 4 +- examples/dma_gpio/funconfig.h | 7 + examples/external_crystal/external_crystal.c | 7 +- examples/external_crystal/funconfig.h | 9 + .../exti_pin_change_isr/exti_pin_change_isr.c | 7 +- examples/exti_pin_change_isr/funconfig.h | 7 + examples/flashtest/flashtest.c | 32 ++-- examples/flashtest/funconfig.h | 8 + examples/hsitrim/Makefile | 2 - examples/hsitrim/funconfig.h | 8 + examples/hsitrim/hsitrim.c | 28 +-- examples/i2c_oled/Makefile | 2 - examples/i2c_oled/funconfig.h | 7 + examples/i2c_oled/i2c_oled.c | 12 +- examples/i2c_oled/ssd1306_i2c.h | 8 +- examples/i2c_slave/funconfig.h | 7 + examples/i2c_slave/i2c_slave.c | 6 +- examples/i2c_slave/i2c_slave.h | 2 +- examples/input_capture/funconfig.h | 7 + examples/input_capture/input_capture.c | 9 +- examples/iwdg/funconfig.h | 7 + examples/iwdg/iwdg.c | 5 +- examples/optionbytes/Makefile | 2 - examples/optionbytes/funconfig.h | 8 + examples/optionbytes/optionbytes.c | 8 +- examples/optiondata/funconfig.h | 7 + examples/optiondata/optiondata.c | 5 +- examples/run_from_ram/Makefile | 2 - examples/run_from_ram/funconfig.h | 8 + examples/run_from_ram/run_from_ram.c | 4 +- examples/self_modify_code/funconfig.h | 8 + examples/self_modify_code/self_modify_code.c | 3 +- examples/spi_24L01_rx/Makefile | 2 - examples/spi_24L01_rx/funconfig.h | 7 + examples/spi_24L01_rx/spi_24L01_rx.c | 7 +- examples/spi_24L01_tx/Makefile | 2 - examples/spi_24L01_tx/funconfig.h | 7 + examples/spi_24L01_tx/spi_24L01_tx.c | 8 +- examples/spi_dac/Makefile | 2 - examples/spi_dac/funconfig.h | 7 + examples/spi_dac/spi_dac.c | 7 +- examples/spi_oled/Makefile | 1 - examples/spi_oled/spi_oled.c | 12 +- examples/standby_autowake/Makefile | 1 - examples/standby_autowake/funconfig.h | 7 + examples/standby_autowake/standby_autowake.c | 6 +- examples/standby_btn/Makefile | 2 - examples/standby_btn/funconfig.h | 7 + examples/standby_btn/standby_btn.c | 12 +- examples/struct_direct_gpio/funconfig.h | 7 + .../struct_direct_gpio/struct_direct_gpio.c | 8 +- examples/struct_gpio/funconfig.h | 7 + examples/struct_gpio/struct_gpio.c | 9 +- examples/systick_irq/funconfig.h | 8 + examples/systick_irq/systick_irq.c | 13 +- examples/template/funconfig.h | 7 + examples/template/template.c | 4 +- examples/tim1_pwm/Makefile | 2 - examples/tim1_pwm/funconfig.h | 7 + examples/tim1_pwm/tim1_pwm.c | 10 +- examples/tim2_pwm/Makefile | 2 - examples/tim2_pwm/funconfig.h | 7 + examples/tim2_pwm/tim2_pwm.c | 10 +- examples/tim2_pwm_remap/Makefile | 1 - examples/tim2_pwm_remap/funconfig.h | 7 + examples/tim2_pwm_remap/tim2_pwm_remap.c | 10 +- examples/uartdemo/funconfig.h | 10 + examples/uartdemo/uartdemo.c | 7 +- examples/ws2812bdemo/funconfig.h | 7 + examples/ws2812bdemo/ws2812bdemo.c | 2 +- 96 files changed, 573 insertions(+), 386 deletions(-) create mode 100644 examples/GPIO/funconfig.h create mode 100644 examples/MCOtest/funconfig.h create mode 100644 examples/adc_dma_opamp/funconfig.h create mode 100644 examples/adc_polled/funconfig.h create mode 100644 examples/blink/funconfig.h create mode 100644 examples/bootload/funconfig.h create mode 100644 examples/cpp_virtual_methods/funconfig.h create mode 100644 examples/debugprintfdemo/funconfig.h create mode 100644 examples/direct_gpio/funconfig.h create mode 100644 examples/dma_gpio/funconfig.h create mode 100644 examples/external_crystal/funconfig.h create mode 100644 examples/exti_pin_change_isr/funconfig.h create mode 100644 examples/flashtest/funconfig.h create mode 100644 examples/hsitrim/funconfig.h create mode 100644 examples/i2c_oled/funconfig.h create mode 100644 examples/i2c_slave/funconfig.h create mode 100644 examples/input_capture/funconfig.h create mode 100644 examples/iwdg/funconfig.h create mode 100644 examples/optionbytes/funconfig.h create mode 100644 examples/optiondata/funconfig.h create mode 100644 examples/run_from_ram/funconfig.h create mode 100644 examples/self_modify_code/funconfig.h create mode 100644 examples/spi_24L01_rx/funconfig.h create mode 100644 examples/spi_24L01_tx/funconfig.h create mode 100644 examples/spi_dac/funconfig.h create mode 100644 examples/standby_autowake/funconfig.h create mode 100644 examples/standby_btn/funconfig.h create mode 100644 examples/struct_direct_gpio/funconfig.h create mode 100644 examples/struct_gpio/funconfig.h create mode 100644 examples/systick_irq/funconfig.h create mode 100644 examples/template/funconfig.h create mode 100644 examples/tim1_pwm/funconfig.h create mode 100644 examples/tim2_pwm/funconfig.h create mode 100644 examples/tim2_pwm_remap/funconfig.h create mode 100644 examples/uartdemo/funconfig.h create mode 100644 examples/ws2812bdemo/funconfig.h diff --git a/ch32v003fun/ch32v003fun.c b/ch32v003fun/ch32v003fun.c index d030306..7373c7c 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,73 @@ 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 + + #define HSEBYP (FUNCONF_HSE_BYPASS)? + + 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 b65a5a2..6a2d6ef 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 @@ -5009,18 +5048,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 ) @@ -5054,16 +5091,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 @@ -5072,9 +5100,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. @@ -5150,7 +5176,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 e788f78..d8313bb 100644 --- a/examples/GPIO/GPIO.c +++ b/examples/GPIO/GPIO.c @@ -12,10 +12,6 @@ #error "please enable ONE of the demos by setting it to 1 and the others to 0" #endif - -#define SYSTEM_CORE_CLOCK 48000000 -#define APB_CLOCK SYSTEM_CORE_CLOCK - #include "ch32v003fun.h" #include "ch32v003_GPIO_branchless.h" @@ -25,7 +21,7 @@ int main() { - SystemInit48HSI(); + SystemInit(); #if DEMO_GPIO_blink == 1 GPIO_port_enable(GPIO_port_C); diff --git a/examples/GPIO/Makefile b/examples/GPIO/Makefile index 7e60f81..63ac24f 100644 --- a/examples/GPIO/Makefile +++ b/examples/GPIO/Makefile @@ -2,9 +2,6 @@ all : flash TARGET:=GPIO -CFLAGS+=-DTINYVECTOR -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/GPIO/funconfig.h b/examples/GPIO/funconfig.h new file mode 100644 index 0000000..16f4a82 --- /dev/null +++ b/examples/GPIO/funconfig.h @@ -0,0 +1,8 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +#define FUNCONF_TINYVECTOR 1 +#define CH32V003 1 + +#endif + diff --git a/examples/MCOtest/MCOtest.c b/examples/MCOtest/MCOtest.c index 9ded288..aaf3b34 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 20c15bf..92a4808 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 0000000..16f4a82 --- /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 55bdd4e..c8fd9d8 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 e56bc2a..638089c 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 0000000..998cf76 --- /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 42f8091..b284c6d 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 65ed99c..06bbccf 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 0000000..998cf76 --- /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 GIT binary patch literal 520 zcmb_Wze~eF82!?6xjI-{P7{X?DH-h8LCl&ONVbYY{0EBd=n!-hTA?7=Y9&e!3tC(h zad9XC7b!6~x;iMtrX{nJlhLH5po^0aKHiUeci(M-y8s~?%w!_{7ouK-pFF+$-yH3+ z_?RqVV8jay1oH`M*HvjTCePUtJ_q_p6_cxg_sRw{+OutOF*&QftlaK<XMSK|aK#&( zBp4FnwV&LruBF+B9Z3q`%&c1M``0ID`Z%6>vUo<0Ut-yjky7Z0U4QJXSQ`2zQsH5m zY#mNjzpRfnRV<E?Nu8Ao9coi0YExVCIj}y!B^WP@xp@$;%Fxj6PFmXRfmdV$F{g*w z_OI;ip6Rw35X@S(LTU^U1PLRB@=cXT5ISe1H;t5@51l~JXxI+ftZxh%wY1)YR6mWp tVlN=5DojqC0ndg_Rvnb~9GEK{hm9lTojO7EGo(@5#BS1P@k<5>`EOdQjpqOW literal 500 zcmd02P!MMTf)9)k3}WL0Aax*sOvCh2n+DkvD9CnXyEv1BIXeTR@*;KyVRmtj8HZcl z%+;A3L=Ky?&u5r%+#Dz`<2-p<I>Y0;Z2w}M7rK3%J^8shL(E~2&Ku5#-H+w2|C`}& z&i3(tt(b5+JM)b5+r=3jCbzRYOmQ{`vdl#p9S+W#a(IgKQRh~N$<M?XKx}hnL571# zANxRT$Fz$;TEzL_ED<0HQXz79JCHR0&hTJz8=IN(igE^J4N(Rj_ia-J+<E&R`?38~ zUXczI3q=uoxPLk$gTmy-hZ(|+j10o83>?J*tOmVq#S`2b#90}DBCgLvdy54^4S+la zVb;`V(}DEl)~8Py-IBw$G6)<LWPR+n^`9`SgFHmW^@%teP$k>3$H^T>8AKeX098z6 heLU;vzwJP`vAWMVJei$=t9XLANil<h0pkJ&1^`1%k{|#8 diff --git a/examples/blink/blink.c b/examples/blink/blink.c index 4a67a26..c6ecdae 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 0000000..998cf76 --- /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 67847cf..5032b55 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 0000000..5a4fc16 --- /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 749b3b5..718f170 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 e2c1d69..f6f1fc3 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 0000000..998cf76 --- /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 df41951..d7243d5 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 0000000..9e8d7f5 --- /dev/null +++ b/examples/debugprintfdemo/funconfig.h @@ -0,0 +1,12 @@ +#ifndef _FUNCONFIG_H +#define _FUNCONFIG_H + +// Though this should be on by default we can extra force it on. +#define FUNCONF_USE_DEBUGPRINTF 0 +#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 47d56f9..313ef15 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 0000000..998cf76 --- /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 bc88433..5f7a698 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 0000000..998cf76 --- /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 d568775..36474b7 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 0000000..8ea009f --- /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 d6f3b21..c5efa07 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 0000000..998cf76 --- /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 435dcc1..012696a 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 0000000..2f5a579 --- /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 4dd4bad..bfa95b3 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 0000000..16f4a82 --- /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 da7e114..5a7562d 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 d525389..e0d78d5 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 0000000..998cf76 --- /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 fd7a3bc..2de90a1 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 3ae7ce2..023157f 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 0000000..998cf76 --- /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 dbfaf80..3f79362 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 a488eaf..9b5f3f9 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 0000000..998cf76 --- /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 ed3b92b..f36522e 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); @@ -106,4 +101,4 @@ int main() GPIOD->BSHR = 1<<16 | 1<<4; Delay_Ms(1); } -} \ No newline at end of file +} diff --git a/examples/iwdg/funconfig.h b/examples/iwdg/funconfig.h new file mode 100644 index 0000000..998cf76 --- /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 886f277..911893e 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 5e49195..2406eb5 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 0000000..16f4a82 --- /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 24b10b8..cbc5d09 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 0000000..998cf76 --- /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 a3c8f27..a3627f7 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 4743de5..d29c744 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 0000000..16f4a82 --- /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 34c2512..8d52590 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 0000000..16f4a82 --- /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 e4a78ae..c0155ea 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 5b54c64..26764f3 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 0000000..998cf76 --- /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 f7c8149..81d3257 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 6ad3d51..c657924 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 0000000..998cf76 --- /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 16a1c7e..b25b7a8 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 c7ef196..18ab741 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 0000000..998cf76 --- /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 aaed51f..0298ef8 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 77b7065..805e3ae 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 f5231e8..c0cbf5a 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 7822714..f289aec 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 0000000..998cf76 --- /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 cc0fe42..301e163 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 36f69f6..82b40f2 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 0000000..998cf76 --- /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 3a6cd72..2c86bc1 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 0000000..998cf76 --- /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 9c05c6b..f8ab71a 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 0000000..998cf76 --- /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 dee2655..27cf715 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 0000000..bff4e04 --- /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 26d5de5..59c58d7 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 0000000..998cf76 --- /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 d81e4eb..f4ff7fa 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 8ece937..a885035 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 0000000..998cf76 --- /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 1ead833..b063f8e 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 7b3d0c0..44ebf7b 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 0000000..998cf76 --- /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 37e949d..2e0911e 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 36df10e..ea40f6a 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 0000000..998cf76 --- /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 1d2c4bb..b48fd3b 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 0000000..8c1e7c7 --- /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 3907a46..9d9bf0a 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 0000000..998cf76 --- /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 2f462d0..f1b1a60 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; -- GitLab