QuakeGod
2024-07-27 842bb64195f958b050867c50db66fc0aa413dafb
提交 | 用户 | 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};
842bb6 20 char str1[128];
483170 21 int LineCount=0;
Q 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 {
842bb6 57 /*    
483170 58     int len1=0;
Q 59     clearscreen();
60     uint32_t us1,us2,us3,us4,us5,us6;
61
62
63     us1=GetuS();
64     int crc1 = crc_check(buf1,16);        //7us
65     us2=GetuS();
66     int crc2 = crc16bitbybit(buf1,16);    //45us
67     us3=GetuS();
68     int crc3 = crc16table(buf1, 16);        //9us
69     us4=GetuS();
70   int crc4 = crc16tablefast(buf1, 16);    //12uS
71     us5=GetuS();
72     LL_CRC_ResetCRCCalculationUnit(CRC);
73     LL_CRC_SetInitialData(CRC,0xFFFFFFFF);
74     LL_CRC_SetInitialData(CRC,0xA001);
75     for (int i=0;i<16;i++)
76     {
77         LL_CRC_FeedData8(CRC,buf1[i]);
78     }
79     int crc5 = LL_CRC_ReadData32(CRC);        //5uS
80     us6=GetuS();
81     
82         len1+=sprintf(str1+len1,"\r\nCRC  %04X  %04X  %04X  %04X  %04X\r\n",crc1,crc2,crc3,crc4,crc5);
83         len1+=sprintf(str1+len1,"time %04d  %04d  %04d  %04d  %04d\r\n",us2-us1,us3-us2,us4-us3,us5-us4,us6-us5);
84 //        Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR;    
85 //        len1+=sprintf(str1+len1,"PCL1 %d, BRR %d Baud %d \r\n",HAL_RCC_GetPCLK1Freq(),USART1->BRR,Uart1baudval);
86 //        int periphclk = LL_RCC_GetUSARTClockFreq(LL_RCC_USART1_CLKSOURCE);
87 //        len1+=sprintf(str1+len1,"periphclk %d \r\n",periphclk);
88       LL_RCC_ClocksTypeDef RCC_Clocks;
89     
90         LL_RCC_GetSystemClocksFreq(&RCC_Clocks);
91         int pllsource = LL_RCC_PLL_GetMainSource();
92         len1+=sprintf(str1+len1,"MainSource %x %d \r\n",pllsource,pllsource);
93         
94         int sysclk = RCC_Clocks.SYSCLK_Frequency;
95         len1+=sprintf(str1+len1,"sysclk %d \r\n",sysclk);
96     
97         PutStr(str1,len1);
98
99         
100 //    InitTimer(0,0);
101 //    InitTimer(1,1);
102 //    InitTimer(2,2);
103 //    InitTimer(3,3);
104     
105 //    RunTimer(0,1000);
106 //    StartTimer(2,1000);
107     Locate(13,1);LineCount=3;
842bb6 108     */
483170 109     return 0;
Q 110 }
111
112 int ShowRunningInfo()
113 {
114             int Clk1=SysTick->VAL;            
115             if (Uart1BaudFirstGot)
116             {
117                 Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR;
118                 Uart1BaudFirstGot=0;
119             }
120             int Reload=SysTick->LOAD;
121
122             int Clk2=SysTick->VAL;
123             //int us2=GetuS();
124             int haltick=HAL_GetTick();
125             int len1=0;
126             uint32_t theUs = GetuS();
127             int nRunCount2=KMem.nRunCount;
128             if (!Uart1Stat.QTx.bEmpty) return 0;
129             
130             if ( (nRunCount2 & 0xff) == 0x03)
131             {
132                 Locate(13,1);LineCount=3;
133             } else    if ((nRunCount2 & 0xff) == 0x0f)
134             {
135                 int timeus1;
136                 int timeus2;
137         
5dd1b7 138 //                len1=sprintf((char *)str1," N %8d Tk %8d %9u CFG %02X R %d M %d S %d %4d IN %04X OUT %04X  \r\n",
Q 139 //                    KMem.nRunCount, haltick, theUs, KMem.EffJumperSW, bKBusRepeater, bKBusMaster, bKBusSlave, Clk2, KMem.WX[0],KMem.WY[0]);
483170 140                 //len1=sprintf((char *)str1,"U%02X%02XA",x2,x2);
Q 141                 // Locate(10,1);
142                 timeus1=GetuS();
143                 PutStr(str1,len1);
144                 timeus2=GetuS();
145                 sprintftime = timeus1 - theUs;
146                 putstrtime = timeus2 - timeus1;
147 //                if (IsTimerOn(0)) {RunTimer(1,1000);StopTimer(3);}
148 //                if (IsTimerOn(1)) {RunTimer(2,100);StopTimer(0);}
149 //                if (IsTimerOn(2)) {RunTimer(3,10);StopTimer(1);}
150 //                if (IsTimerOn(3)) {RunTimer(0,10000);StopTimer(2);}
151           }
152             if ((nRunCount2 & 0xff) == 0x2f && 0)
153             {
154
155           }    
156             if ((nRunCount2 & 0xff) == 0x0af)
157             {
158                 
159           }                
160             return 0;
161 }
162
5dd1b7 163 #define AVGCount 8
Q 164     
165 int nCount5 = 0;
166 unsigned short ADC_TEMP[16] ={0};
483170 167
Q 168 int ADCProcess()
169 {
170     // ADC channels
171     //  0 -- 24V  --> 0
172     //  1 -- 5V   --> 2
173     //    2 -- 
174     //  3 -- 
175     //  4 -- 
176     //  5 -- 
177     //  6 -- 
178     //  7 -- 
179     //  8 -- 
180     //                         --> 5
181     // 16 -- Temp  --> 6
182     // 17 -- Vref  --> 7
183
5dd1b7 184
Q 185     
186 uint16_t ADC_ConvertedValue=0;
483170 187 static int CurChannel=LL_ADC_CHANNEL_0;
Q 188 //static int waitcount = 0;
189     
190                 if (!LL_ADC_REG_IsConversionOngoing(ADC1))
191                 {
192                     //waitcount++;
193                     //if (waitcount<2) return 0;
194                     //waitcount=0;
195                     ADC_ConvertedValue = LL_ADC_REG_ReadConversionData12(ADC1);
196                     
197         //            ADC_RegularChannelConfig(LL_ADC_CHANNEL_17,);
198                     int channels = CurChannel ;//LL_ADC_REG_GetSequencerChannels(ADC1);
199                     int nextchannel = LL_ADC_CHANNEL_0;
200                     if ((channels & LL_ADC_CHANNEL_0) == LL_ADC_CHANNEL_0)
201                     {
202                         KMem.ADCValues[0] = ADC_ConvertedValue;
5dd1b7 203                         nextchannel = LL_ADC_CHANNEL_7;
Q 204                         if (KMem.ADCValues[0] < 1500) 
483170 205                         {
Q 206                             PowerDownEvent=1;
207                         }else 
208                         {
209                             PowerDownEvent=0;
210                         }                        
5dd1b7 211                     }
Q 212                     else if ((channels & LL_ADC_CHANNEL_7) == LL_ADC_CHANNEL_7)
483170 213                     {
5dd1b7 214                         ADC_TEMP[0] += ADC_ConvertedValue;
Q 215                         
216                         
217                         nextchannel = LL_ADC_CHANNEL_8;
218                     }
219                     else if ((channels & LL_ADC_CHANNEL_8) == LL_ADC_CHANNEL_8)
220                     {
221                         ADC_TEMP[1] += ADC_ConvertedValue;
222                         
223                         nCount5++;
224                         
225                         if (nCount5 >= AVGCount) {
226                             
227                             KMem.ADCValues[3] = ADC_TEMP[0]/AVGCount;
228             
229                             KMem.ADCValues[4] = ADC_TEMP[1]/AVGCount;
230                             ADC_TEMP[0]=0;
231                             ADC_TEMP[1]=0;
232                             nCount5 = 0 ;
233                                 
234                         }
235                         //    KMem.ADCValues[3] = 255;//ADC_TEMP[0]/16;
236                         //    KMem.ADCValues[4] = 768; //ADC_TEMP[1]/16;                        
237                         nextchannel = LL_ADC_CHANNEL_9;
238
239                     }else if ((channels & LL_ADC_CHANNEL_9) == LL_ADC_CHANNEL_9)
240                     {
241                         KMem.ADCValues[5] = ADC_ConvertedValue;                        
483170 242                         nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
Q 243
244                     }else if ((channels & LL_ADC_CHANNEL_16) == LL_ADC_CHANNEL_16)
245                     {
246                         KMem.ADCValues[6] = ADC_ConvertedValue;        
247                             nextchannel = LL_ADC_CHANNEL_VREFINT;
248                     }else if ((channels & LL_ADC_CHANNEL_17) == LL_ADC_CHANNEL_17)
249                     {
250                         KMem.ADCValues[7] = ADC_ConvertedValue;                        
5dd1b7 251                         KMem.ADCValues[8] = *((unsigned short *)ADCrefAddr);
483170 252                         
Q 253                         nextchannel = LL_ADC_CHANNEL_0;
254                     }else
255                     {
256                         //ADCValues[0] = ADC_ConvertedValue;                        
257                     }
258                     //nextchannel = LL_ADC_CHANNEL_VREFINT;
259                     LL_ADC_REG_SetSequencerChannels(ADC1,nextchannel);                    
260                     LL_ADC_REG_StartConversion(ADC1);
261                     CurChannel = nextchannel;
262                 }    
263     return 0;
264 }
265
266 int PowerDownProcess(void )
267 {
268     AddEventLog(KMem.CurTimeSec,EventTypePowerDown,1,12345);
269     SaveRunStat(&KMRunStat);
270     KMem.PwrFailCount++;
271     KMem.LastPwrFailTime = KMem.CurTimeSec;
272     return 0;
273 }
274
275 int PowerRecoverProcess(void)
276 {
277     KMem.PwrFailCount++;
278     
279     return 0;
280 }
281