/**
|
******************************************************************************
|
* @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[128];
|
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;
|
|
|
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;
|
}
|
|
#define AVGCount 8
|
|
int nCount5 = 0;
|
unsigned short ADC_TEMP[16] ={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_7;
|
if (KMem.ADCValues[0] < 1500)
|
{
|
PowerDownFlag=1;
|
}else
|
{
|
PowerDownFlag=0;
|
}
|
}
|
else if ((channels & LL_ADC_CHANNEL_7) == LL_ADC_CHANNEL_7)
|
{
|
ADC_TEMP[0] += ADC_ConvertedValue;
|
|
|
nextchannel = LL_ADC_CHANNEL_8;
|
}
|
else if ((channels & LL_ADC_CHANNEL_8) == LL_ADC_CHANNEL_8)
|
{
|
ADC_TEMP[1] += ADC_ConvertedValue;
|
|
nCount5++;
|
|
if (nCount5 >= AVGCount) {
|
|
KMem.ADCValues[3] = ADC_TEMP[0]/AVGCount;
|
|
KMem.ADCValues[4] = ADC_TEMP[1]/AVGCount;
|
ADC_TEMP[0]=0;
|
ADC_TEMP[1]=0;
|
nCount5 = 0 ;
|
|
}
|
// KMem.ADCValues[3] = 255;//ADC_TEMP[0]/16;
|
// KMem.ADCValues[4] = 768; //ADC_TEMP[1]/16;
|
nextchannel = LL_ADC_CHANNEL_9;
|
|
}else if ((channels & LL_ADC_CHANNEL_9) == LL_ADC_CHANNEL_9)
|
{
|
KMem.ADCValues[5] = ADC_ConvertedValue;
|
nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
|
|
}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[8] = *((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;
|
}
|