QuakeGod
2023-11-22 eaf5d5b7aa6e4155924d97802f581b7de835f0d8
提交 | 用户 | age
483170 1 /**
Q 2   ******************************************************************************
3   * @file           : debug.c
4   * @brief          : debug functions program body
5   ******************************************************************************
6     */
7 #include "debug.h"
8 #include "globaldef.h"
9 #include "functions.h"
10 #include "string.h"
11 #include "modbusRTU.h"
12
13 #include "stm32f0xx_hal.h"
14
15 #define ADCrefAddr 0x1FFFF7BA
16
17 int sprintftime = 0;
18 int putstrtime = 0;
19 const unsigned char buf1[16]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00};
20 char str1[256];
21 int LineCount=0;
22 int Uart1baudval=0;    
23 int Uart2baudval=0;    
24
25 __asm int add1(int a,int b)
26 {
27     add r0,r1,r0
28     BLX    lr
29 }
30
31 void clearscreen()
32 {
33     PutStr("\33[2J\33[0;0H",10);
34     return;
35 }
36
37 void Locate(int y,int x)
38 {
39         char str[16];
40         int len;
41         len=sprintf(str," \33[%d;%dH",y,x);
42         PutStr(str,len);
43         return;
44 }
45
46 int FormatHex(char * buf1, unsigned char * data, int n)
47 {
48     int len1=0;
49         for (int i=0;i<n;i++)
50         {len1+=sprintf(buf1+len1,"%02X ",data[i]);}
51         len1+=sprintf(buf1+len1,"\r\n");    
52     return len1;
53 }
54
55 int ShowInitInfo()
56 {
57     int len1=0;
58     clearscreen();
59     uint32_t us1,us2,us3,us4,us5,us6;
60
61 //    Locate(1,1);
62
63 /*    
64     LoadFlashDatas();
65     
66     LoadAndUpdateStoreCfg();
67     
68     HAL_StatusTypeDef res;    
69     stStoreCfg * pFCfg = (stStoreCfg *) GetCurStoreCfgAddr();
70     stStoreCfg * pFCfg2 = GetNextStoreCfgAddr(pFCfg);    
71
72 int t11=GetuS();
73     
74     for (int i=0;i<20;i++)
75     {
76         tims[i]=GetuS();
77     }        
78     clearscreen();
79     len1+=sprintf(str1+len1," Ver 001 \r\n");
80     len1+=sprintf(str1+len1," Uart1Baud %d Uart2Baud %d  UID  %08x %08x %08x \r\n",Uart1Baud,Uart2Baud,pUID[0],pUID[1],pUID[2]);
81     len1+=sprintf(str1+len1," Flash = %d %d %d %d  res = %d   ",FlashDatas[0],FlashDatas[1],FlashDatas[2],FlashDatas[3],res);
82     len1+=sprintf(str1+len1,"flash operation = %u %u %u\r\n",t11-t10,t10,t11);
83     PutStr(str1,len1);
84     len1=0;
85     len1+=sprintf(str1+len1,"%08X  %X %X , PowerOn %X  UpTime %X %X %X %X \r\n",
86     (uint32_t)pFCfg,pFCfg[0].Sign1,pFCfg[0].SN1,pFCfg[0].PowerCount,pFCfg[0].UpTime,pFCfg[0].UserData1,pFCfg[0].CRC1,pFCfg[0].EndSign1);
87     len1+=sprintf(str1+len1,"%08X  %X %X , PowerOn %X  UpTime %X %X %X %X \r\n",
88     (uint32_t)pFCfg2,Cfg2.Sign1,Cfg2.SN1,Cfg2.PowerCount,Cfg2.UpTime,Cfg2.UserData1,Cfg2.CRC1,Cfg2.EndSign1);
89     PutStr(str1,len1);
90 */    
91     len1=0;
92 /*    
93     for (int i=0;i<8;i++)
94     {
95         len1=0;
96         len1+=sprintf(str1+len1,"%02X:",i*32);
97         for (int j=0;j<8;j++)
98         {
99                 len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
100         }
101                 len1+=sprintf(str1+len1,"  %02X",pFlash1[i*32+8]);
102         for (int j=9;j<16;j++)
103         {
104                 len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
105         }
106                 len1+=sprintf(str1+len1," | %02X",pFlash1[i*32+16]);
107         for (int j=17;j<24;j++)
108         {
109                 len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
110         }
111                 len1+=sprintf(str1+len1,"  %02X",pFlash1[i*32+24]);
112         for (int j=25;j<32;j++)
113         {
114                 len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
115         }
116         len1+=sprintf(str1+len1,"\r\n");
117         PutStr(str1,len1);
118     }
119 */    
120     us1=GetuS();
121     int crc1 = crc_check(buf1,16);        //7us
122     us2=GetuS();
123     int crc2 = crc16bitbybit(buf1,16);    //45us
124     us3=GetuS();
125     int crc3 = crc16table(buf1, 16);        //9us
126     us4=GetuS();
127   int crc4 = crc16tablefast(buf1, 16);    //12uS
128     us5=GetuS();
129     LL_CRC_ResetCRCCalculationUnit(CRC);
130     LL_CRC_SetInitialData(CRC,0xFFFFFFFF);
131     LL_CRC_SetInitialData(CRC,0xA001);
132     for (int i=0;i<16;i++)
133     {
134         LL_CRC_FeedData8(CRC,buf1[i]);
135     }
136     int crc5 = LL_CRC_ReadData32(CRC);        //5uS
137     us6=GetuS();
138     
139         len1+=sprintf(str1+len1,"\r\nCRC  %04X  %04X  %04X  %04X  %04X\r\n",crc1,crc2,crc3,crc4,crc5);
140         len1+=sprintf(str1+len1,"time %04d  %04d  %04d  %04d  %04d\r\n",us2-us1,us3-us2,us4-us3,us5-us4,us6-us5);
141 //        Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR;    
142 //        len1+=sprintf(str1+len1,"PCL1 %d, BRR %d Baud %d \r\n",HAL_RCC_GetPCLK1Freq(),USART1->BRR,Uart1baudval);
143 //        int periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART1_CLKSOURCE);
144 //        len1+=sprintf(str1+len1,"periphclk %d \r\n",periphclk);
145       LL_RCC_ClocksTypeDef RCC_Clocks;
146     
147         LL_RCC_GetSystemClocksFreq(&RCC_Clocks);
148         int pllsource = LL_RCC_PLL_GetMainSource();
149         len1+=sprintf(str1+len1,"MainSource %x %d \r\n",pllsource,pllsource);
150         
151         int sysclk = RCC_Clocks.SYSCLK_Frequency;
152         len1+=sprintf(str1+len1,"sysclk %d \r\n",sysclk);
153     
154         PutStr(str1,len1);
155
156         
157 //    InitTimer(0,0);
158 //    InitTimer(1,1);
159 //    InitTimer(2,2);
160 //    InitTimer(3,3);
161     
162 //    RunTimer(0,1000);
163 //    StartTimer(2,1000);
164     Locate(13,1);LineCount=3;
165     return 0;
166 }
167
168 int ShowRunningInfo()
169 {
170             int Clk1=SysTick->VAL;            
171             if (Uart1BaudFirstGot)
172             {
173                 Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR;
174                 Uart1BaudFirstGot=0;
175             }
176             int Reload=SysTick->LOAD;
177
178             int Clk2=SysTick->VAL;
179             //int us2=GetuS();
180             int haltick=HAL_GetTick();
181             int len1=0;
182             uint32_t theUs = GetuS();
183             int nRunCount2=KMem.nRunCount;
184             if (!Uart1Stat.QTx.bEmpty) return 0;
185             
186             if ( (nRunCount2 & 0xff) == 0x03)
187             {
188                 Locate(13,1);LineCount=3;
189             } else    if ((nRunCount2 & 0xff) == 0x0f)
190             {
191                 int timeus1;
192                 int timeus2;
193         
194                 len1=sprintf((char *)str1," N %8d Tk %8d %9u CFG %02X R %d M %d S %d %4d IN %04X OUT %04X  \r\n",
195                     KMem.nRunCount, haltick, theUs, KMem.EffJumperSW, bKBusRepeater, bKBusMaster, bKBusSlave, Clk2, KMem.WX[0],KMem.WY[0]);
196                 //len1=sprintf((char *)str1,"U%02X%02XA",x2,x2);
197                 // Locate(10,1);
198                 timeus1=GetuS();
199                 PutStr(str1,len1);
200                 timeus2=GetuS();
201                 sprintftime = timeus1 - theUs;
202                 putstrtime = timeus2 - timeus1;
203 //                if (IsTimerOn(0)) {RunTimer(1,1000);StopTimer(3);}
204 //                if (IsTimerOn(1)) {RunTimer(2,100);StopTimer(0);}
205 //                if (IsTimerOn(2)) {RunTimer(3,10);StopTimer(1);}
206 //                if (IsTimerOn(3)) {RunTimer(0,10000);StopTimer(2);}
207           }
208             if ((nRunCount2 & 0xff) == 0x2f && 0)
209             {
210
211           }    
212             if ((nRunCount2 & 0xff) == 0x0af)
213             {
214                 
215           }                
216             return 0;
217 }
218
219
220 int ADCProcess()
221 {
222     // ADC channels
223     //  0 -- 24V  --> 0
224     //  1 -- 5V   --> 2
225     //    2 -- 
226     //  3 -- 
227     //  4 -- 
228     //  5 -- 
229     //  6 -- 
230     //  7 -- 
231     //  8 -- 
232     //                         --> 5
233     // 16 -- Temp  --> 6
234     // 17 -- Vref  --> 7
235
236     uint16_t ADC_ConvertedValue=0;
237 static int CurChannel=LL_ADC_CHANNEL_0;
238 //static int waitcount = 0;
239     
240                 if (!LL_ADC_REG_IsConversionOngoing(ADC1))
241                 {
242                     //waitcount++;
243                     //if (waitcount<2) return 0;
244                     //waitcount=0;
245                     ADC_ConvertedValue = LL_ADC_REG_ReadConversionData12(ADC1);
246                     
247         //            ADC_RegularChannelConfig(LL_ADC_CHANNEL_17,);
248                     int channels = CurChannel ;//LL_ADC_REG_GetSequencerChannels(ADC1);
249                     int nextchannel = LL_ADC_CHANNEL_0;
250                     if ((channels & LL_ADC_CHANNEL_0) == LL_ADC_CHANNEL_0)
251                     {
252                         KMem.ADCValues[0] = ADC_ConvertedValue;
253                         nextchannel = LL_ADC_CHANNEL_8;
254                         if (KMem.ADCValues[0] < 2200) 
255                         {
256                             PowerDownEvent=1;
257                         }else 
258                         {
259                             PowerDownEvent=0;
260                         }                        
261                     }else if ((channels & LL_ADC_CHANNEL_8) == LL_ADC_CHANNEL_8)
262                     {
263                         KMem.ADCValues[2] = ADC_ConvertedValue;                        
264                         nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
265
266                     }else if ((channels & LL_ADC_CHANNEL_16) == LL_ADC_CHANNEL_16)
267                     {
268                         KMem.ADCValues[6] = ADC_ConvertedValue;        
269                             nextchannel = LL_ADC_CHANNEL_VREFINT;
270                     }else if ((channels & LL_ADC_CHANNEL_17) == LL_ADC_CHANNEL_17)
271                     {
272                         KMem.ADCValues[7] = ADC_ConvertedValue;                        
273                         KMem.ADCValues[5] = *((unsigned short *)ADCrefAddr);
274                         
275                         nextchannel = LL_ADC_CHANNEL_0;
276                     }else
277                     {
278                         //ADCValues[0] = ADC_ConvertedValue;                        
279                     }
280                     //nextchannel = LL_ADC_CHANNEL_VREFINT;
281                     LL_ADC_REG_SetSequencerChannels(ADC1,nextchannel);                    
282                     LL_ADC_REG_StartConversion(ADC1);
283                     CurChannel = nextchannel;
284                 }    
285     return 0;
286 }
287
288 int PowerDownProcess(void )
289 {
290     AddEventLog(KMem.CurTimeSec,EventTypePowerDown,1,12345);
291     SaveRunStat(&KMRunStat);
292     KMem.PwrFailCount++;
293     KMem.LastPwrFailTime = KMem.CurTimeSec;
294     return 0;
295 }
296
297 int PowerRecoverProcess(void)
298 {
299     KMem.PwrFailCount++;
300     
301     return 0;
302 }
303