//#include <stdio.h>
//#include <rt_misc.h>
//#include <stdint.h>
//#include "BAT32G139.h"
//#include "userdefine.h"
//#include "sci.h"
//#if defined ( __CC_ARM )
//#if (__ARMCC_VERSION < 400000)
//#else
//// Insist on keeping widthprec, to avoid X propagation by benign code in C-lib
//#pragma import _printf_widthprec
//#endif
//#endif
////
//// C library retargetting
////
//struct __FILE { int handle; /* Add whatever you need here */ };
//FILE __stdout;
//FILE __stdin;
//void _ttywrch(int ch)
//{
// UART0_Send(ch);
// return;
//}
//int fputc(int ch, FILE *f)
//{
// UART0_Send(ch);
// if(ch == '\n')
// {
// UART0_Send('\r');
// }
// return ch;
//}
//int fgetc( FILE *f)
//{
// uint8_t ch;
// ch = UART0_Receive();
//#if 1
// if(ch == 0x0D) // 回车键
// {
// UART0_Send('\r'); // echo
// UART0_Send('\n'); // echo
// }
// else {
// UART0_Send(ch); // echo
// }
//#endif
// return ch;
//}
//int ferror(FILE *f) {
// return EOF;
//}
//void _sys_exit(int return_code) {
// if(return_code)
// {
// *((volatile uint32_t*)0x40028020) = 0x00000BAD; /* simulation NG */
// }
// else
// {
// *((volatile uint32_t*)0x40028020) = 0x00000001; /* simulation OK */
// }
// while (1); /* endless loop */
//}
// 注意:此文件与 retarget.c中内容冲突,已经注释掉retarget.c中内容
#include <stdio.h>
#include <stdint.h>
#include "BAT32G139.h"
#include "userdefine.h"
#include "sci.h"
#include "sys_mcu_header.h"
//--------------------------------------------
// 标准 C 库要求的 FILE 结构体
//--------------------------------------------
struct __FILE { int handle; };
FILE __stdout; // printf 需要
//--------------------------------------------
// 串口单字节发送函数 (使用 UART1)
//--------------------------------------------
static void UART1_Printf_Send(uint8_t ch)
{
SCI0->TXD1 = ch;
while (SCI0->SSR02 & (_0040_SCI_UNDER_EXECUTE | _0020_SCI_VALID_STORED))// 用户手册19.7.1 UART发送 UART1 SCI0的通道2
{
; // 等待发送完成
}
}
//--------------------------------------------
// printf 输出底层实现
//--------------------------------------------
int fputc(int ch, FILE *f)
{
UART1_Printf_Send((uint8_t)ch);
// 如果遇到 '\n',自动补 '\r',保证终端换行正常
if (ch == '\n')
{
UART1_Printf_Send('\r');
}
return ch;
}
//--------------------------------------------
// Keil/ARM C 库要求的调试输出函数
//--------------------------------------------
void _ttywrch(int ch)
{
UART1_Printf_Send((uint8_t)ch);
}
///*
// *-----------------------------------------------------------------------------
// * The confidential and proprietary information contained in this file may
// * only be used by a person authorised under and to the extent permitted
// * by a subsisting licensing agreement from ARM Limited.
// *
// * (C) COPYRIGHT 2011-2012 ARM Limited.
// * ALL RIGHTS RESERVED
// *
// * This entire notice must be reproduced on all copies of this file
// * and copies of this file may only be made by a person if such person is
// * permitted to do so under the terms of a subsisting license agreement
// * from ARM Limited.
// *
// * SVN Information
// *
// * Checked In : $Date: 2012-01-11 17:13:57 +0000 (Wed, 11 Jan 2012) $
// *
// * Revision : $Revision: 197600 $
// *
// * Release Information : Cortex-M0+ AT590-r0p1-00rel0
// *-----------------------------------------------------------------------------
// */
////
//// printf retargetting functions
////
//#include <stdio.h>
////#include <rt_misc.h>
//#include <stdint.h>
//#include "BAT32G139.h"
//#include "userdefine.h"
//#include "sci.h"
//#if defined ( __CC_ARM )
//#if (__ARMCC_VERSION < 400000)
//#else
//// Insist on keeping widthprec, to avoid X propagation by benign code in C-lib
//#pragma import _printf_widthprec
//#endif
//#endif
////
//// C library retargetting
////
//#if defined ( __CC_ARM )
//struct __FILE { int handle; /* Add whatever you need here */ };
//#endif
//FILE __stdout;
//FILE __stdin;
//#if 1
//void _ttywrch(int ch)
//{
// UART0_Send(ch);
// return;
//}
//int fputc(int ch, FILE *f)
//{
// UART0_Send(ch);
// if(ch == '\n')
// {
// UART0_Send('\r');
// }
// return ch;
//}
//#else
//extern void UART3_Sendbyte(uint8_t ch);
//void _ttywrch(int ch)
//{
// UART3_Sendbyte(ch);
// return;
//}
//int fputc(int ch, FILE *f)
//{
// UART3_Sendbyte(ch);
// if(ch == '\n')
// {
// UART3_Sendbyte('\r');
// }
// return ch;
//}
//#endif
//int fgetc( FILE *f)
//{
// uint8_t ch;
// ch = UART0_Receive();
//#if 1
// if(ch == 0x0D) // 回车键
// {
// UART0_Send('\r'); // echo
// UART0_Send('\n'); // echo
// }
// else {
// UART0_Send(ch); // echo
// }
//#endif
// return ch;
//}
//int ferror(FILE *f) {
// return EOF;
//}
//void _sys_exit(int return_code) {
// if(return_code)
// {
// *((volatile uint32_t*)0x40028020) = 0x00000BAD; /* simulation NG */
// }
// else
// {
// *((volatile uint32_t*)0x40028020) = 0x00000001; /* simulation OK */
// }
// while (1); /* endless loop */
//}
/*************************************************
Description: Function_TxSendDebug
Input :
Return :
Others : 调试串口数据处理
*************************************************/
uint8_t u8_senddataarraydebug[10] = {0};
#if 0
void Function_TxSendDebug(int32_t data1,int32_t data2,int32_t data3,int32_t data4)
{
uint8_t u8_senddataindexdebug = 0;
u8_senddataarraydebug[0] = (uint8_t)(data1); u8_senddataarraydebug[1] = (uint8_t)(data1>>8);
u8_senddataarraydebug[2] = (uint8_t)(data2); u8_senddataarraydebug[3] = (uint8_t)(data2>>8);
u8_senddataarraydebug[4] = (uint8_t)(data3); u8_senddataarraydebug[5] = (uint8_t)(data3>>8);
u8_senddataarraydebug[6] = (uint8_t)(data4); u8_senddataarraydebug[7] = (uint8_t)(data4>>8);
u8_senddataarraydebug[8] = 0; //校验字节清零
for(u8_senddataindexdebug = 0; u8_senddataindexdebug <= 7; u8_senddataindexdebug++)
{
u8_senddataarraydebug[8] += u8_senddataarraydebug[u8_senddataindexdebug];
}
// DMAVEC->CTRL[0].DMSAR = (uint32_t)(u8_senddataarraydebug+1);
DMAVEC->CTRL[0].DMACT = 8; //传输8个数据
DMA->DMAEN1 |= 1<<6; //uart1 //使能传输(UART1)
// DMA->DMAEN1 |= 1<<4; //uart0
SCI0->TXD1 = u8_senddataarraydebug[0];
}
#else
/**
* @brief 通过UART1发送调试数据(逐字节方式)
* @param data1~data4 四个待发送的16位数据
* @note 实际帧格式 = 9字节:
* Byte0 : 帧头 (0xFF)
* Byte1 : data1 L
* Byte2 : data1 H
* Byte3 : data2 L
* Byte4 : data2 H
* Byte5 : data3 L
* Byte6 : data3 H
* Byte7 : data4 L
* Byte8 : data4 H
*/
void Function_TxSendDebug(int32_t data1,int32_t data2,int32_t data3,int32_t data4)
{
static uint8_t u8_senddataindexdebug = 0;
// 帧头
u8_senddataarraydebug[0] = 0xFF;
// 逐字节发送
SCI0->TXD1 = u8_senddataarraydebug[u8_senddataindexdebug];
// 发送索引递增
if(++u8_senddataindexdebug >= 9)
{
u8_senddataindexdebug = 0; // 发满一帧后重新开始
u8_senddataarraydebug[8] = 0; // 清空最后一字节(可去掉)
}
// 更新数据内容(小端存储)
u8_senddataarraydebug[1] = (uint8_t)(data1);
u8_senddataarraydebug[2] = (uint8_t)(data1>>8);
u8_senddataarraydebug[3] = (uint8_t)(data2);
u8_senddataarraydebug[4] = (uint8_t)(data2>>8);
u8_senddataarraydebug[5] = (uint8_t)(data3);
u8_senddataarraydebug[6] = (uint8_t)(data3>>8);
u8_senddataarraydebug[7] = (uint8_t)(data4);
u8_senddataarraydebug[8] = (uint8_t)(data4>>8);
}
#endif
// 位名字数组(和位定义顺序对应,bit0 → bit15) 20250825
const char *ProtectFlagNames[16] = {
"VBUS_OVP", // bit0 母线过压
"VBUS_OLP", // bit1 母线过流/过载保护
"INDUC_OCP", // bit2 电感过流保护
"OLP", // bit3 过载保护
"ACOUT_OVP", // bit4 流输出过压保护
"ACOUT_OLP", // bit5 交流输出过载保护
"ACOUT_SCP", // bit6 交流输出短路保护
"ACOUT_DCIM", // bit7 直流偏置保护
"OTP", // bit8 过温保护
"RPM_OVER", // bit9 转速过高
"AUX_POWER_OVP", // bit10 辅助电源过压
"AUX_POWER_OLP", // bit11 辅助电源过流/过载
"System_Init_Fail", // bit12 系统初始化失败
"RPM_LOW", // bit13 转速过低
"Vref_Error", // bit14 基准电压异常
"rsvd" // bit15 保留
};
void Print_SystemProtectFlags(void)
{
printf("系统保护标志 (bit[15..0]): \n");
for(int i = 15; i >= 0; i--) {
printf("位%-2d (%-16s): %d\n",
i,
ProtectFlagNames[i],
(System_ProtectFlag_Info.all >> i) & 1);
}
//System_Protect_Flag_u flag;
//flag.all = 0x0001; // 只置最低位
//printf("VBUS_OVP = %d\n", flag.bit.VBUS_OVP); // VBUS_OVP = 1 说明环境里位域是从低位开始排
//printf("rsvd = %d\n", flag.bit.rsvd);
}
const char *StartCheckFlagNames[16] = {
"vBus_Up_OK", // bit0 母线电压上升正常
"vBus_Dn_OK", // bit1 母线电压下降正常
"RPM_Up_OK", // bit2 转速上升正常
"auxPower_Up_OK", // bit3 辅助电源上升正常
"auxPower_Dn_OK", // bit4 辅助电源下降正常
"temp_Up_OK", // bit5 温度检测正常
"rsvd6", // bit6 保留
"rsvd7", // bit7
"rsvd8", // bit8
"rsvd9", // bit9
"rsvd10", // bit10
"rsvd11", // bit11
"rsvd12", // bit12
"rsvd13", // bit13
"rsvd14", // bit14
"rsvd15" // bit15
};
void Print_StartCheckFlags(void)
{
printf("系统启动检查标志 (bit[15..0]): \n");
for(int i = 15; i >= 0; i--) {
printf("位%-2d (%-16s): %d\n",
i,
StartCheckFlagNames[i],
(StartCheck_Flag_Info.all >> i) & 1);
}
}
/*************************************************
Description: User_UART_View
Input :
Return :
Others : 用于串口调试
*************************************************/
extern const short int Sine_Table_50Hz[];
extern const short int Sine_Table_60Hz[];
void User_UART_View(void)
{
/*------------------------------------------------------------------------------------*/
/*---------------------UART0串口调试--------------------------------------------------*/
//User_UART_View();
//Function_TxSendDebug( ADSample_RefCorrect_InvCur.ADRef_Correct_Value, ADSample_Info.Bus_Vol_Filiter_AD, ADSample_Info.AC_Vol_Peak,ADSample_Info.Inv_Cur_Peak );//
//Function_TxSendDebug( ADSample_Store_Info.InvCur_Store_One,Sine_Table_50Hz[Inv_SineCtrl_Info.SineTab_Index],ADSample_Store_Info.InvVol_Store_One,Inv_SineCtrl_Info.InvVol_SPWM_Period);//
//Function_TxSendDebug( ADSample_Store_Info.InvVol_Store_One, ADSample_Store_Info.InvCur_Store_One, ADSample_Info.AC_Vol_RMS,ADSample_Info.Inv_Cur_RMS);//
//Function_TxSendDebug( System_Power_Info.Active_Power_Total, System_Power_Info.Apparent_Power_Total, ADSample_Info.AC_Vol_RMS,ADSample_Info.Inv_Cur_RMS);//
//Function_TxSendDebug( System_Power_Info.Active_Power_Total, System_Power_Info.Apparent_Power_Total , ADSample_Info.AC_Vol_RMS,ADSample_Info.Inv_Cur_RMS);//
//Function_TxSendDebug( ADSample_Info.AC_Vol_Peak, ADSample_Info.Inv_Cur_Peak, ADSample_Info.AC_Vol_RMS,ADSample_Info.Inv_Cur_RMS);//
//Function_TxSendDebug( StepMotor_Ctrl.ref , StepMotor_Ctrl.fdb, Lookup_Table_Speed.y_Axis_Out,StepMotor_Ctrl.stepNum * 1000);//
//Function_TxSendDebug( System_Power_Info.Active_Power_Total , StepMotor_Ctrl.ref, StepMotor_Ctrl.fdb,StepMotor_Ctrl.stepNum * 1000);//
//Function_TxSendDebug( System_ProtectFlag_Info.all , Ki_B >>9, StepMotor_Ctrl.err,StepMotor_Ctrl.stepNum * 1000);//
//Function_TxSendDebug( System_ProtectFlag_Info.all ,StartCheck_Flag_Info.all, Inv_SineCtrl_Info.InvVol_AM_Target,ADSample_Info.Bus_Vol_AD);//
//Function_TxSendDebug( System_Power_Info.Active_Power_Total, System_Power_Info.Apparent_Power_Total, ADSample_Info.AC_Vol_RMS,ADSample_Info.Inv_Cur_RMS);//
//Function_TxSendDebug( System_Power_Info.Active_Power_Total, ADSample_Info.AC_Vol_RMS,ADSample_Info.Inv_Cur_RMS, StepMotor_Ctrl.fdb);//
//Function_TxSendDebug( PID_Vol_Loop.Ref, PID_Vol_Loop.Fdb,PID_Vol_Loop.Out,PID_Vol_Loop.Ui>> 15 );//
//Function_TxSendDebug( 0, System_Power_Info.Active_Power_Total, ADSample_Info.AC_Vol_RMS,ADSample_Info.Inv_Cur_RMS );//
//Function_TxSendDebug( System_ProtectFlag_Info.all ,StartCheck_Flag_Info.all,State_Context.State_Identifier*1000,ADSample_Info.Bus_Vol_AD);//
//Function_TxSendDebug( PID_Vol_Loop.Ref, PID_Vol_Loop.Fdb,PID_Curr_Loop.Ref,PID_Curr_Loop.Fdb);//
//Function_TxSendDebug( PID_Vol_Loop.Ref, PID_Vol_Loop.Fdb,PID_Curr_Loop.Ref,PID_Curr_Loop.Fdb);//
//Function_TxSendDebug( PID_Vol_Loop.Ref, PID_Vol_Loop.Fdb,PID_Curr_Loop.Ref,PID_Curr_Loop.Fdb);//
//
// Function_TxSendDebug(
// System_ProtectFlag_Info.all, // 参数1:系统保护标志位(16位,每一位表示一种故障保护状态)
// StartCheck_Flag_Info.all, // 参数2:上电/启动检查标志位(16位,每一位表示一次上电检查的结果)
// State_Context.State_Identifier * 1000, // 参数3:状态机当前状态编号(*1000 是为了放大方便显示/调试)
// ADSample_Info.Bus_Vol_AD // 参数4:母线电压AD采样值(未滤波的ADC原始值) Power_AuxVcc_AD 辅助电压
// );
Print_SystemProtectFlags();
Print_StartCheckFlags();
printf("当前状态编号:%d\n",State_Context.State_Identifier);
printf("直流母线电压_滤波:%d\n",ADSample_Info.Bus_Vol_Filiter_AD);
printf("参考ADC_滤波:%d\n",ADSample_Info.ref_AD_Fir);
printf("交流电压有效值:%d\n",ADSample_Info.AC_Vol_RMS);
printf("交流电流有效值:%d\n",ADSample_Info.Inv_Cur_RMS);
printf("负载电流有效值:%d\n",ADSample_Info.Load_Cur_RMS);
//typedef struct
//{
// int AC_Vol_Peak; // 交流输出电压的峰值(|v|最大值,窗内更新;单位:ADC码或工程量)
// int Inv_Cur_Peak; // 逆变输出电流峰值
// int Load_Cur_Peak; // 负载电流峰值
// int AC_Vol_Sum; // 交流电压瞬时值的求和(∑v);可用于平均/直流分量估计
// int Inv_Cur_Sum; // 逆变电流瞬时值的求和(∑i)
// int Load_Cur_Sum; // 负载电流瞬时值的求和(∑i_load)
// int AC_Vol_Sum_Back; // 上一窗口/备份的 ∑v(“Back”常用作双缓冲,提高读写并行性)
// int Inv_Cur_Sum_Back; // 上一窗口/备份的 ∑i
// int Load_Cur_Sum_Back; // 上一窗口/备份的 ∑i_load
// int AC_Vol_RMS; // 交流电压有效值(√(∑v2/N) 的结果,已完成开方与缩放)
// int Inv_Cur_RMS; // 逆变电流有效值
// int Load_Cur_RMS; // 负载电流有效值
// int AC_Vol_DCIM; // 交流电压的直流偏置即时估计(如v的均值/低通结果,用于DC偏置保护)
// int AC_Vol_DCIM_Sum; // 直流偏置估计的累加(∑v 或低频积分)
// int AC_Vol_DCIM_Sum_Back; // 上一窗口的直流偏置累加备份
// int AC_Vol_DCIM_RMS; // 若做直流分量的RMS/均方指标,这里存放计算结果(用于门限判据)
// int Bus_Vol_AD; // 直流母线电压-当前原始ADC值(未滤波/即时值)
// int Bus_Vol_Sum; // 母线电压的累加(∑vbus,用于平均/滑动滤波)
// int Bus_Vol_Filiter_AD; // 母线电压的滤波结果(低通/移动平均后的AD值,供环路/阈值判断)
// int Bus_Vol_Avg_Sum; // 母线电压平均的累加器(为提高数值精度,先累计再右移/除法)
// int Bus_Vol_Avg_AD_AM; // 母线电压的平均值(Average Measure),常作慢速监视/显示/保护
// int AC_Active_Power_Sum; // 有功功率的累加(∑(v*i),可能按采样点逐点累加)
// int AC_Active_Power_Back; // 上一窗口的有功累加备份(用于整周期计算后一次性读取)
// int Power_AuxVcc_AD; // 辅助电源电压通道的ADC值(如控制板5V/12V监测)
// int Temp_External_AD; // 外部温度传感器ADC值(NTC/PT等,经查表/线性化换算出温度)
// int ref_AD; // 参考通道ADC值(当前/最近一次,供标定使用)
// int ref_AD_Fir; // 参考通道的滤波结果(FIR/IIR后),用于补偿通道增益/零点漂移
//} ADSample_VAR;
//Function_TxSendDebug( ADSample_Info.ref_AD_Fir ,AD_Correct_Vref.ADRef_Correct_Value,ADSample_Info.AC_Vol_Peak,System_ProtectFlag_Info.all);//
// Function_TxSendDebug( System_ProtectFlag_Info.all ,StartCheck_Flag_Info.all,\
// State_Context.State_Identifier*1000,\
// AD_Correct_V_ACOUT.Flag.bit.ADRef_Correct_Ok+\
// AD_Correct_I_Induc.Flag.bit.ADRef_Correct_Ok*10+\
// AD_Correct_Vref.Flag.bit.ADRef_Correct_Ok*100);//
//Function_TxSendDebug( ADSample_Info.AC_Vol_Sum_Back>>12, System_Power_Info.Active_Power_Total, ADSample_Info.AC_Vol_RMS,ADSample_Info.Inv_Cur_RMS );//
}