QuakeGod
2023-02-01 7a2ff321965f1f24f3047fedd44a83f451f1f793
提交 | 用户 | age
a7db3c 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             if (Uart2BaudFirstGot)
177             {
178                 Uart2baudval = HAL_RCC_GetPCLK1Freq() / USART2->BRR;
179                 Uart2BaudFirstGot=0;
180             }        
181             int Reload=SysTick->LOAD;
182
183             int Clk2=SysTick->VAL;
184             //int us2=GetuS();
185             int haltick=HAL_GetTick();
186             int len1=0;
187             uint32_t theUs = GetuS();
188             int nRunCount2=KMem.nRunCount;
189             if (!Uart1Stat.QTx.bEmpty) return 0;
190             
191             if ( (nRunCount2 & 0xff) == 0x03)
192             {
193                 Locate(13,1);LineCount=3;
194             } else    if ((nRunCount2 & 0xff) == 0x0f)
195             {
196                 int timeus1;
197                 int timeus2;
198         
199                 len1=sprintf((char *)str1," N %8d Tk %8d %9u CFG %02X R %d M %d S %d %4d IN %04X OUT %04X  \r\n",
200                     KMem.nRunCount, haltick, theUs, KMem.EffJumperSW, bKBusRepeater, bKBusMaster, bKBusSlave, Clk2, KMem.WX[0],KMem.WY[0]);
201                 //len1=sprintf((char *)str1,"U%02X%02XA",x2,x2);
202                 // Locate(10,1);
203                 timeus1=GetuS();
204                 PutStr(str1,len1);
205                 timeus2=GetuS();
206                 sprintftime = timeus1 - theUs;
207                 putstrtime = timeus2 - timeus1;
208 //                if (IsTimerOn(0)) {RunTimer(1,1000);StopTimer(3);}
209 //                if (IsTimerOn(1)) {RunTimer(2,100);StopTimer(0);}
210 //                if (IsTimerOn(2)) {RunTimer(3,10);StopTimer(1);}
211 //                if (IsTimerOn(3)) {RunTimer(0,10000);StopTimer(2);}
212           }
213             if ((nRunCount2 & 0xff) == 0x2f && 0)
214             {
215
216           }    
217             if ((nRunCount2 & 0xff) == 0x0af)
218             {
219                 
220           }                
221             return 0;
222 }
223
224
225 int ADCProcess()
226 {
227     // ADC channels
228     //  0 -- 24V  --> 0
229     //  1 -- 5V   --> 2
230     //    2 -- 
231     //  3 -- 
232     //  4 -- 
233     //  5 -- 
234     //  6 -- 
235     //  7 -- 
236     //  8 -- 
237     //                         --> 5
238     // 16 -- Temp  --> 6
239     // 17 -- Vref  --> 7
240
241     uint16_t ADC_ConvertedValue=0;
242 static int CurChannel=LL_ADC_CHANNEL_0;
243 //static int waitcount = 0;
244     
245                 if (!LL_ADC_REG_IsConversionOngoing(ADC1))
246                 {
247                     //waitcount++;
248                     //if (waitcount<2) return 0;
249                     //waitcount=0;
250                     ADC_ConvertedValue = LL_ADC_REG_ReadConversionData12(ADC1);
251                     
252         //            ADC_RegularChannelConfig(LL_ADC_CHANNEL_17,);
253                     int channels = CurChannel ;//LL_ADC_REG_GetSequencerChannels(ADC1);
254                     int nextchannel = LL_ADC_CHANNEL_0;
255                     if ((channels & LL_ADC_CHANNEL_0) == LL_ADC_CHANNEL_0)
256                     {
257                         KMem.ADCValues[0] = ADC_ConvertedValue;
258                         nextchannel = LL_ADC_CHANNEL_8;
259                     }else if ((channels & LL_ADC_CHANNEL_8) == LL_ADC_CHANNEL_8)
260                     {
261                         KMem.ADCValues[2] = ADC_ConvertedValue;                        
262                         nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
263                         if (KMem.ADCValues[2] < 2200) 
264                         {
265                             PowerDownEvent=1;
266                         }else 
267                         {
268                             PowerDownEvent=0;
269                         }
270                     }else if ((channels & LL_ADC_CHANNEL_16) == LL_ADC_CHANNEL_16)
271                     {
272                         KMem.ADCValues[6] = ADC_ConvertedValue;        
273                             nextchannel = LL_ADC_CHANNEL_VREFINT;
274                     }else if ((channels & LL_ADC_CHANNEL_17) == LL_ADC_CHANNEL_17)
275                     {
276                         KMem.ADCValues[7] = ADC_ConvertedValue;                        
277                         KMem.ADCValues[5] = *((unsigned short *)ADCrefAddr);
278                         
279                         nextchannel = LL_ADC_CHANNEL_0;
280                     }else
281                     {
282                         //ADCValues[0] = ADC_ConvertedValue;                        
283                     }
284                     //nextchannel = LL_ADC_CHANNEL_VREFINT;
285                     LL_ADC_REG_SetSequencerChannels(ADC1,nextchannel);                    
286                     LL_ADC_REG_StartConversion(ADC1);
287                     CurChannel = nextchannel;
288                 }    
289     return 0;
290 }
291
292 int PowerDownProcess(void )
293 {
294     AddEventLog(KMem.CurTimeSec,EventTypePowerDown,1,12345);
295     SaveRunStat(&KMRunStat);
296     KMem.PwrFailCount++;
297     KMem.LastPwrFailTime = KMem.CurTimeSec;
298     return 0;
299 }
300
301 int PowerRecoverProcess(void)
302 {
303     KMem.PwrFailCount++;
304     
305     return 0;
306 }
307