/**
|
******************************************************************************
|
* @file : debug.c
|
* @brief : debug functions program body
|
******************************************************************************
|
*/
|
#include "debug.h"
|
#include "globaldef.h"
|
#include "functions.h"
|
#include "string.h"
|
#include "modbusRTU.h"
|
|
#include "stm32f0xx_hal.h"
|
|
#define ADCrefAddr 0x1FFFF7BA
|
|
int sprintftime = 0;
|
int putstrtime = 0;
|
const unsigned char buf1[16]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00};
|
char str1[256];
|
int LineCount=0;
|
int Uart1baudval=0;
|
int Uart2baudval=0;
|
|
__asm int add1(int a,int b)
|
{
|
add r0,r1,r0
|
BLX lr
|
}
|
|
void clearscreen()
|
{
|
PutStr("\33[2J\33[0;0H",10);
|
return;
|
}
|
|
void Locate(int y,int x)
|
{
|
char str[16];
|
int len;
|
len=sprintf(str," \33[%d;%dH",y,x);
|
PutStr(str,len);
|
return;
|
}
|
|
int FormatHex(char * buf1, unsigned char * data, int n)
|
{
|
int len1=0;
|
for (int i=0;i<n;i++)
|
{len1+=sprintf(buf1+len1,"%02X ",data[i]);}
|
len1+=sprintf(buf1+len1,"\r\n");
|
return len1;
|
}
|
|
int ShowInitInfo()
|
{
|
int len1=0;
|
clearscreen();
|
uint32_t us1,us2,us3,us4,us5,us6;
|
|
// Locate(1,1);
|
|
/*
|
LoadFlashDatas();
|
|
LoadAndUpdateStoreCfg();
|
|
HAL_StatusTypeDef res;
|
stStoreCfg * pFCfg = (stStoreCfg *) GetCurStoreCfgAddr();
|
stStoreCfg * pFCfg2 = GetNextStoreCfgAddr(pFCfg);
|
|
int t11=GetuS();
|
|
for (int i=0;i<20;i++)
|
{
|
tims[i]=GetuS();
|
}
|
clearscreen();
|
len1+=sprintf(str1+len1," Ver 001 \r\n");
|
len1+=sprintf(str1+len1," Uart1Baud %d Uart2Baud %d UID %08x %08x %08x \r\n",Uart1Baud,Uart2Baud,pUID[0],pUID[1],pUID[2]);
|
len1+=sprintf(str1+len1," Flash = %d %d %d %d res = %d ",FlashDatas[0],FlashDatas[1],FlashDatas[2],FlashDatas[3],res);
|
len1+=sprintf(str1+len1,"flash operation = %u %u %u\r\n",t11-t10,t10,t11);
|
PutStr(str1,len1);
|
len1=0;
|
len1+=sprintf(str1+len1,"%08X %X %X , PowerOn %X UpTime %X %X %X %X \r\n",
|
(uint32_t)pFCfg,pFCfg[0].Sign1,pFCfg[0].SN1,pFCfg[0].PowerCount,pFCfg[0].UpTime,pFCfg[0].UserData1,pFCfg[0].CRC1,pFCfg[0].EndSign1);
|
len1+=sprintf(str1+len1,"%08X %X %X , PowerOn %X UpTime %X %X %X %X \r\n",
|
(uint32_t)pFCfg2,Cfg2.Sign1,Cfg2.SN1,Cfg2.PowerCount,Cfg2.UpTime,Cfg2.UserData1,Cfg2.CRC1,Cfg2.EndSign1);
|
PutStr(str1,len1);
|
*/
|
len1=0;
|
/*
|
for (int i=0;i<8;i++)
|
{
|
len1=0;
|
len1+=sprintf(str1+len1,"%02X:",i*32);
|
for (int j=0;j<8;j++)
|
{
|
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
|
}
|
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+8]);
|
for (int j=9;j<16;j++)
|
{
|
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
|
}
|
len1+=sprintf(str1+len1," | %02X",pFlash1[i*32+16]);
|
for (int j=17;j<24;j++)
|
{
|
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
|
}
|
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+24]);
|
for (int j=25;j<32;j++)
|
{
|
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
|
}
|
len1+=sprintf(str1+len1,"\r\n");
|
PutStr(str1,len1);
|
}
|
*/
|
us1=GetuS();
|
int crc1 = crc_check(buf1,16); //7us
|
us2=GetuS();
|
int crc2 = crc16bitbybit(buf1,16); //45us
|
us3=GetuS();
|
int crc3 = crc16table(buf1, 16); //9us
|
us4=GetuS();
|
int crc4 = crc16tablefast(buf1, 16); //12uS
|
us5=GetuS();
|
LL_CRC_ResetCRCCalculationUnit(CRC);
|
LL_CRC_SetInitialData(CRC,0xFFFFFFFF);
|
LL_CRC_SetInitialData(CRC,0xA001);
|
for (int i=0;i<16;i++)
|
{
|
LL_CRC_FeedData8(CRC,buf1[i]);
|
}
|
int crc5 = LL_CRC_ReadData32(CRC); //5uS
|
us6=GetuS();
|
|
len1+=sprintf(str1+len1,"\r\nCRC %04X %04X %04X %04X %04X\r\n",crc1,crc2,crc3,crc4,crc5);
|
len1+=sprintf(str1+len1,"time %04d %04d %04d %04d %04d\r\n",us2-us1,us3-us2,us4-us3,us5-us4,us6-us5);
|
// Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR;
|
// len1+=sprintf(str1+len1,"PCL1 %d, BRR %d Baud %d \r\n",HAL_RCC_GetPCLK1Freq(),USART1->BRR,Uart1baudval);
|
// int periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART1_CLKSOURCE);
|
// len1+=sprintf(str1+len1,"periphclk %d \r\n",periphclk);
|
LL_RCC_ClocksTypeDef RCC_Clocks;
|
|
LL_RCC_GetSystemClocksFreq(&RCC_Clocks);
|
int pllsource = LL_RCC_PLL_GetMainSource();
|
len1+=sprintf(str1+len1,"MainSource %x %d \r\n",pllsource,pllsource);
|
|
int sysclk = RCC_Clocks.SYSCLK_Frequency;
|
len1+=sprintf(str1+len1,"sysclk %d \r\n",sysclk);
|
|
PutStr(str1,len1);
|
|
|
// InitTimer(0,0);
|
// InitTimer(1,1);
|
// InitTimer(2,2);
|
// InitTimer(3,3);
|
|
// RunTimer(0,1000);
|
// StartTimer(2,1000);
|
Locate(13,1);LineCount=3;
|
return 0;
|
}
|
|
int ShowRunningInfo()
|
{
|
int Clk1=SysTick->VAL;
|
if (Uart1BaudFirstGot)
|
{
|
Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR;
|
Uart1BaudFirstGot=0;
|
}
|
int Reload=SysTick->LOAD;
|
|
int Clk2=SysTick->VAL;
|
//int us2=GetuS();
|
int haltick=HAL_GetTick();
|
int len1=0;
|
uint32_t theUs = GetuS();
|
int nRunCount2=KMem.nRunCount;
|
if (!Uart1Stat.QTx.bEmpty) return 0;
|
|
if ( (nRunCount2 & 0xff) == 0x03)
|
{
|
Locate(13,1);LineCount=3;
|
} else if ((nRunCount2 & 0xff) == 0x0f)
|
{
|
int timeus1;
|
int timeus2;
|
|
len1=sprintf((char *)str1," N %8d Tk %8d %9u CFG %02X R %d M %d S %d %4d IN %04X OUT %04X \r\n",
|
KMem.nRunCount, haltick, theUs, KMem.EffJumperSW, bKBusRepeater, bKBusMaster, bKBusSlave, Clk2, KMem.WX[0],KMem.WY[0]);
|
//len1=sprintf((char *)str1,"U%02X%02XA",x2,x2);
|
// Locate(10,1);
|
timeus1=GetuS();
|
PutStr(str1,len1);
|
timeus2=GetuS();
|
sprintftime = timeus1 - theUs;
|
putstrtime = timeus2 - timeus1;
|
// if (IsTimerOn(0)) {RunTimer(1,1000);StopTimer(3);}
|
// if (IsTimerOn(1)) {RunTimer(2,100);StopTimer(0);}
|
// if (IsTimerOn(2)) {RunTimer(3,10);StopTimer(1);}
|
// if (IsTimerOn(3)) {RunTimer(0,10000);StopTimer(2);}
|
}
|
if ((nRunCount2 & 0xff) == 0x2f && 0)
|
{
|
|
}
|
if ((nRunCount2 & 0xff) == 0x0af)
|
{
|
|
}
|
return 0;
|
}
|
|
|
int ADCProcess()
|
{
|
// ADC channels
|
// 0 -- 24V --> 0
|
// 1 -- 5V --> 2
|
// 2 --
|
// 3 --
|
// 4 --
|
// 5 --
|
// 6 --
|
// 7 --
|
// 8 --
|
// --> 5
|
// 16 -- Temp --> 6
|
// 17 -- Vref --> 7
|
|
uint16_t ADC_ConvertedValue=0;
|
static int CurChannel=LL_ADC_CHANNEL_0;
|
//static int waitcount = 0;
|
|
if (!LL_ADC_REG_IsConversionOngoing(ADC1))
|
{
|
//waitcount++;
|
//if (waitcount<2) return 0;
|
//waitcount=0;
|
ADC_ConvertedValue = LL_ADC_REG_ReadConversionData12(ADC1);
|
|
// ADC_RegularChannelConfig(LL_ADC_CHANNEL_17,);
|
int channels = CurChannel ;//LL_ADC_REG_GetSequencerChannels(ADC1);
|
int nextchannel = LL_ADC_CHANNEL_0;
|
if ((channels & LL_ADC_CHANNEL_0) == LL_ADC_CHANNEL_0)
|
{
|
KMem.ADCValues[0] = ADC_ConvertedValue;
|
nextchannel = LL_ADC_CHANNEL_1;
|
}
|
if ((channels & LL_ADC_CHANNEL_1) == LL_ADC_CHANNEL_1)
|
{
|
KMem.ADCValues[1] = ADC_ConvertedValue;
|
nextchannel = LL_ADC_CHANNEL_8;
|
} else if ((channels & LL_ADC_CHANNEL_8) == LL_ADC_CHANNEL_8)
|
{
|
KMem.ADCValues[2] = ADC_ConvertedValue;
|
nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
|
if (KMem.ADCValues[2] < 2200)
|
{
|
PowerDownEvent=1;
|
}else
|
{
|
PowerDownEvent=0;
|
}
|
}else if ((channels & LL_ADC_CHANNEL_16) == LL_ADC_CHANNEL_16)
|
{
|
KMem.ADCValues[6] = ADC_ConvertedValue;
|
nextchannel = LL_ADC_CHANNEL_VREFINT;
|
}else if ((channels & LL_ADC_CHANNEL_17) == LL_ADC_CHANNEL_17)
|
{
|
KMem.ADCValues[7] = ADC_ConvertedValue;
|
KMem.ADCValues[5] = *((unsigned short *)ADCrefAddr);
|
|
nextchannel = LL_ADC_CHANNEL_0;
|
}else
|
{
|
//ADCValues[0] = ADC_ConvertedValue;
|
}
|
//nextchannel = LL_ADC_CHANNEL_VREFINT;
|
LL_ADC_REG_SetSequencerChannels(ADC1,nextchannel);
|
LL_ADC_REG_StartConversion(ADC1);
|
CurChannel = nextchannel;
|
}
|
return 0;
|
}
|
|
int PowerDownProcess(void )
|
{
|
AddEventLog(KMem.CurTimeSec,EventTypePowerDown,1,12345);
|
SaveRunStat(&KMRunStat);
|
KMem.PwrFailCount++;
|
KMem.LastPwrFailTime = KMem.CurTimeSec;
|
return 0;
|
}
|
|
int PowerRecoverProcess(void)
|
{
|
KMem.PwrFailCount++;
|
|
return 0;
|
}
|