From e37b57d1a2cb3a621560d148a6dc4820ddfaf71d Mon Sep 17 00:00:00 2001 From: yuquanjun Date: Sun, 5 Jul 2026 04:46:48 +0800 Subject: [PATCH] Fix L012 build errors: restore init.h, pid include, printf retarget - Fix broken init.h that recursively included itself - Use classic struct assignment for PLL init (Keil C99 compatibility) - Fix pid.c include path PID.h -> pid.h - Add __write for MicroLib printf - Add MDK/build.bat for headless Keil build on remote host Co-authored-by: Cursor --- .../SW/12 CW32 BLDC haLL pid OK/MDK/build.bat | 19 ++++++++++++++ .../12 CW32 BLDC haLL pid OK/USER/inc/init.h | 11 +++++++- .../12 CW32 BLDC haLL pid OK/USER/src/init.c | 12 ++++----- .../12 CW32 BLDC haLL pid OK/USER/src/main.c | 26 +++++++++++++++++++ .../12 CW32 BLDC haLL pid OK/USER/src/pid.c | 2 +- 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/MDK/build.bat diff --git a/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/MDK/build.bat b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/MDK/build.bat new file mode 100644 index 0000000..c89dab6 --- /dev/null +++ b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/MDK/build.bat @@ -0,0 +1,19 @@ +@echo off +setlocal + +set "UV4=C:\Keil_v5\UV4\UV4.exe" +if not exist "%UV4%" set "UV4=D:\Keil_v5\UV4\UV4.exe" +if not exist "%UV4%" set "UV4=%ProgramFiles%\Keil_v5\UV4\UV4.exe" +if not exist "%UV4%" set "UV4=%ProgramFiles(x86)%\Keil_v5\UV4\UV4.exe" + +if not exist "%UV4%" ( + echo Keil UV4 not found. Edit build.bat and set UV4 path. + exit /b 1 +) + +pushd "%~dp0" +"%UV4%" -b QQT912_L012.uvprojx -o build.log +set "RC=%ERRORLEVEL%" +type build.log +popd +exit /b %RC% diff --git a/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/inc/init.h b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/inc/init.h index 131dcb0..1573dc8 100644 --- a/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/inc/init.h +++ b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/inc/init.h @@ -1,3 +1,12 @@ -#include "init.h" +#ifndef __INIT_H +#define __INIT_H +#include "main.h" + +void RCC_Configuration(void); +void GPIOInit(void); +void ADC_Configuration(void); +void BTIM_init(void); void ADC1_SampleCallback(void); + +#endif /* __INIT_H */ diff --git a/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/init.c b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/init.c index 9c8374a..6951a3e 100644 --- a/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/init.c +++ b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/init.c @@ -3,12 +3,12 @@ void RCC_Configuration(void) { - PLL_InitTypeDef PLL_InitStruct = { - .ClockSource = SYSCTRL_PLL_SOURCE_HSI_48M, - .PLL_OutFreq = 64000000, - .SourceFreq = 48000000, - .WaitCycle = SYSCTRL_PLL_WAIT_CYCLE_2048 - }; + PLL_InitTypeDef PLL_InitStruct; + + PLL_InitStruct.ClockSource = SYSCTRL_PLL_SOURCE_HSI_48M; + PLL_InitStruct.PLL_OutFreq = 64000000; + PLL_InitStruct.SourceFreq = 48000000; + PLL_InitStruct.WaitCycle = SYSCTRL_PLL_WAIT_CYCLE_2048; SYSCTRL_HSI_Enable(HSIOSC_TO_HSI96MHZ); SYSCTRL_HCLKPRS_Config(SYSCTRL_HCLK_DIV1); diff --git a/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/main.c b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/main.c index 78e4f22..0885e88 100644 --- a/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/main.c +++ b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/main.c @@ -75,6 +75,32 @@ PUTCHAR_PROTOTYPE return ch; } +size_t __write(int handle, const unsigned char *buffer, size_t size) +{ + size_t nChars = 0; + + if (buffer == 0) + { + return 0; + } + + if (handle != 1 && handle != 2) + { + return -1; + } + + for (; size != 0; --size) + { + UART_SendData(DEBUG_UARTx, *buffer++); + while (UART_GetFlagStatus(DEBUG_UARTx, UART_FLAG_TXE) == RESET) + { + } + ++nChars; + } + + return nChars; +} + #ifdef USE_FULL_ASSERT void assert_failed(uint8_t *file, uint32_t line) { diff --git a/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/pid.c b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/pid.c index 52b6e06..d3696bb 100644 --- a/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/pid.c +++ b/新单片机-CW32L012/原项目程序/SW/12 CW32 BLDC haLL pid OK/USER/src/pid.c @@ -1,4 +1,4 @@ -#include "PID.h" +#include "pid.h" float SPEED_P,SPEED_I,SPEED_D=0;