提交 | 用户 | age
|
bfc108
|
1 |
|
Q |
2 |
/**
|
|
3 |
******************************************************************************
|
|
4 |
* @file : main.c
|
|
5 |
* @brief : Main program body
|
|
6 |
******************************************************************************
|
|
7 |
** This notice applies to any and all portions of this file
|
|
8 |
* that are not between comment pairs USER CODE BEGIN and
|
|
9 |
* USER CODE END. Other portions of this file, whether
|
|
10 |
* inserted by the user or by software development tools
|
|
11 |
* are owned by their respective copyright owners.
|
|
12 |
*
|
|
13 |
* COPYRIGHT(c) 2018 STMicroelectronics
|
|
14 |
*
|
|
15 |
* Redistribution and use in source and binary forms, with or without modification,
|
|
16 |
* are permitted provided that the following conditions are met:
|
|
17 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
18 |
* this list of conditions and the following disclaimer.
|
|
19 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
20 |
* this list of conditions and the following disclaimer in the documentation
|
|
21 |
* and/or other materials provided with the distribution.
|
|
22 |
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
23 |
* may be used to endorse or promote products derived from this software
|
|
24 |
* without specific prior written permission.
|
|
25 |
*
|
|
26 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
27 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
28 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
29 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
30 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
31 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
32 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
33 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
34 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
35 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
36 |
*
|
|
37 |
******************************************************************************
|
|
38 |
*/
|
|
39 |
/* Includes ------------------------------------------------------------------*/
|
|
40 |
#include "main.h"
|
|
41 |
#include "stm32f0xx_hal.h"
|
|
42 |
|
|
43 |
/* USER CODE BEGIN Includes */
|
|
44 |
#include "Globaldef.h"
|
|
45 |
#include "Functions.h"
|
|
46 |
#include "KMachine.h"
|
|
47 |
#include "PLCfunctions.h"
|
|
48 |
//#include "KBus.h"
|
|
49 |
#include "KLink.h"
|
|
50 |
#include "string.h"
|
|
51 |
#include "BSP.h"
|
|
52 |
|
|
53 |
/* USER CODE END Includes */
|
|
54 |
|
|
55 |
/* Private variables ---------------------------------------------------------*/
|
|
56 |
|
|
57 |
/* USER CODE BEGIN PV */
|
|
58 |
/* Private variables ---------------------------------------------------------*/
|
|
59 |
|
|
60 |
#define RXBUFSIZE 128
|
|
61 |
#define TXBUFSIZE 128
|
|
62 |
|
|
63 |
unsigned char Uart1RxBuf[256];
|
|
64 |
unsigned char Uart1TxBuf[512];
|
|
65 |
|
|
66 |
unsigned char Uart2RxBuf[RXBUFSIZE];
|
|
67 |
unsigned char Uart2TxBuf[TXBUFSIZE];
|
|
68 |
|
|
69 |
//unsigned char buf1[128];
|
|
70 |
|
|
71 |
unsigned char Datas[128];
|
|
72 |
|
|
73 |
unsigned int SlowFlicker=0;
|
|
74 |
unsigned int FastFlicker=0;
|
|
75 |
|
|
76 |
volatile int PacketLength = 0;
|
|
77 |
|
|
78 |
char str1[256];
|
|
79 |
|
|
80 |
int TimeOutCount=0;
|
|
81 |
int Clk3=0;
|
|
82 |
int ContinueSend=0;
|
|
83 |
int Uart1baudval=0;
|
|
84 |
int Uart2baudval=0;
|
|
85 |
|
|
86 |
int repeater=0;
|
|
87 |
|
|
88 |
int SendTime,Latancy,LatancyClk,SendClk;
|
|
89 |
|
|
90 |
int LineCount=0;
|
|
91 |
|
|
92 |
int LastCircleStartTime=0;
|
|
93 |
int CircleTime=0;
|
|
94 |
|
|
95 |
//volatile unsigned int nRunCount=0;
|
|
96 |
volatile int nCount2=0;
|
|
97 |
|
|
98 |
uint32_t us1,us2,us3,us4,us5,us6;
|
|
99 |
const unsigned char buf1[16]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00};
|
|
100 |
|
|
101 |
volatile int PowerDownEvent=0;
|
|
102 |
volatile int OldPowerDownEvent=0;
|
|
103 |
volatile int OldPowerDownEventTime=0;
|
|
104 |
|
|
105 |
/* USER CODE END PV */
|
|
106 |
|
|
107 |
/* Private function prototypes -----------------------------------------------*/
|
|
108 |
|
|
109 |
|
|
110 |
/* USER CODE BEGIN PFP */
|
|
111 |
/* Private function prototypes -----------------------------------------------*/
|
|
112 |
|
|
113 |
/* USER CODE END PFP */
|
|
114 |
|
|
115 |
/* USER CODE BEGIN 0 */
|
|
116 |
__asm int add1(int a,int b)
|
|
117 |
{
|
|
118 |
add r0,r1,r0
|
|
119 |
BLX lr
|
|
120 |
}
|
|
121 |
|
|
122 |
int HexToInt(char ch)
|
|
123 |
{
|
|
124 |
if (ch>='0' && ch <='9') return ch-'0';
|
|
125 |
if (ch>='A' && ch <='F') return ch-'A'+10;
|
|
126 |
if (ch>='a' && ch <='f') return ch-'a'+10;
|
|
127 |
return 0;
|
|
128 |
}
|
|
129 |
|
|
130 |
void HAL_SYSTICK_Callback(void)
|
|
131 |
{
|
|
132 |
static int Count=0;
|
|
133 |
CurTickuS += 100;
|
|
134 |
nCurTick++;
|
|
135 |
nSlaveTick++;
|
|
136 |
Count++;
|
|
137 |
if (Count>=10000)
|
|
138 |
{
|
|
139 |
Count=0;
|
|
140 |
KMem.CurTimeSec++;
|
|
141 |
KMem.ThisRunTime++; KMem.TotalRunTime++;
|
|
142 |
}
|
|
143 |
|
|
144 |
return;
|
|
145 |
}
|
|
146 |
|
|
147 |
int FormatHex(char * buf1,uchar * data,int n)
|
|
148 |
{
|
|
149 |
int len1=0;
|
|
150 |
for (int i=0;i<n;i++)
|
|
151 |
{len1+=sprintf(buf1+len1,"%02X ",data[i]);}
|
|
152 |
len1+=sprintf(buf1+len1,"\r\n");
|
|
153 |
return len1;
|
|
154 |
}
|
|
155 |
|
|
156 |
int RepeaterFunc()
|
|
157 |
{
|
|
158 |
KMem.WY[0]=KMem.WX[0];
|
|
159 |
if ((KMem.nRunCount &0x7f) == 88)
|
|
160 |
{
|
|
161 |
nCount2++;
|
|
162 |
ToggleRunLed();
|
|
163 |
// int len1=sprintf(str1,"%d %d Cfg %02X Input %02X \r\n",nCount,nCount2,EffJumperSW,MyKeyStat1);
|
|
164 |
// PutStr(str1,len1);
|
|
165 |
}
|
|
166 |
return 0;
|
|
167 |
}
|
|
168 |
|
|
169 |
|
|
170 |
int ShowInitInfo()
|
|
171 |
{
|
|
172 |
int len1=0;
|
|
173 |
clearscreen();
|
|
174 |
// Locate(1,1);
|
|
175 |
|
|
176 |
/*
|
|
177 |
LoadFlashDatas();
|
|
178 |
|
|
179 |
LoadAndUpdateStoreCfg();
|
|
180 |
|
|
181 |
HAL_StatusTypeDef res;
|
|
182 |
stStoreCfg * pFCfg = (stStoreCfg *) GetCurStoreCfgAddr();
|
|
183 |
stStoreCfg * pFCfg2 = GetNextStoreCfgAddr(pFCfg);
|
|
184 |
|
|
185 |
int t11=GetuS();
|
|
186 |
|
|
187 |
for (int i=0;i<20;i++)
|
|
188 |
{
|
|
189 |
tims[i]=GetuS();
|
|
190 |
}
|
|
191 |
clearscreen();
|
|
192 |
len1+=sprintf(str1+len1," Ver 001 \r\n");
|
|
193 |
len1+=sprintf(str1+len1," Uart1Baud %d Uart2Baud %d UID %08x %08x %08x \r\n",Uart1Baud,Uart2Baud,pUID[0],pUID[1],pUID[2]);
|
|
194 |
len1+=sprintf(str1+len1," Flash = %d %d %d %d res = %d ",FlashDatas[0],FlashDatas[1],FlashDatas[2],FlashDatas[3],res);
|
|
195 |
len1+=sprintf(str1+len1,"flash operation = %u %u %u\r\n",t11-t10,t10,t11);
|
|
196 |
PutStr(str1,len1);
|
|
197 |
len1=0;
|
|
198 |
len1+=sprintf(str1+len1,"%08X %X %X , PowerOn %X UpTime %X %X %X %X \r\n",
|
|
199 |
(uint32_t)pFCfg,pFCfg[0].Sign1,pFCfg[0].SN1,pFCfg[0].PowerCount,pFCfg[0].UpTime,pFCfg[0].UserData1,pFCfg[0].CRC1,pFCfg[0].EndSign1);
|
|
200 |
len1+=sprintf(str1+len1,"%08X %X %X , PowerOn %X UpTime %X %X %X %X \r\n",
|
|
201 |
(uint32_t)pFCfg2,Cfg2.Sign1,Cfg2.SN1,Cfg2.PowerCount,Cfg2.UpTime,Cfg2.UserData1,Cfg2.CRC1,Cfg2.EndSign1);
|
|
202 |
PutStr(str1,len1);
|
|
203 |
*/
|
|
204 |
len1=0;
|
|
205 |
/*
|
|
206 |
for (int i=0;i<8;i++)
|
|
207 |
{
|
|
208 |
len1=0;
|
|
209 |
len1+=sprintf(str1+len1,"%02X:",i*32);
|
|
210 |
for (int j=0;j<8;j++)
|
|
211 |
{
|
|
212 |
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
|
|
213 |
}
|
|
214 |
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+8]);
|
|
215 |
for (int j=9;j<16;j++)
|
|
216 |
{
|
|
217 |
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
|
|
218 |
}
|
|
219 |
len1+=sprintf(str1+len1," | %02X",pFlash1[i*32+16]);
|
|
220 |
for (int j=17;j<24;j++)
|
|
221 |
{
|
|
222 |
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
|
|
223 |
}
|
|
224 |
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+24]);
|
|
225 |
for (int j=25;j<32;j++)
|
|
226 |
{
|
|
227 |
len1+=sprintf(str1+len1," %02X",pFlash1[i*32+j]);
|
|
228 |
}
|
|
229 |
len1+=sprintf(str1+len1,"\r\n");
|
|
230 |
PutStr(str1,len1);
|
|
231 |
}
|
|
232 |
*/
|
|
233 |
us1=GetuS();
|
|
234 |
int crc1 = crc_check(buf1,16);
|
|
235 |
us2=GetuS();
|
|
236 |
int crc2 = crc16bitbybit(buf1,16);
|
|
237 |
us3=GetuS();
|
|
238 |
int crc3 = crc16table(buf1, 16);
|
|
239 |
us4=GetuS();
|
|
240 |
int crc4 = crc16tablefast(buf1, 16);
|
|
241 |
us5=GetuS();
|
|
242 |
LL_CRC_ResetCRCCalculationUnit(CRC);
|
|
243 |
LL_CRC_SetInitialData(CRC,0xFFFFFFFF);
|
|
244 |
LL_CRC_SetInitialData(CRC,0xA001);
|
|
245 |
for (int i=0;i<16;i++)
|
|
246 |
{
|
|
247 |
LL_CRC_FeedData8(CRC,buf1[i]);
|
|
248 |
}
|
|
249 |
int crc5 = LL_CRC_ReadData32(CRC);
|
|
250 |
us6=GetuS();
|
|
251 |
|
|
252 |
len1+=sprintf(str1+len1,"CRC %04X %04X %04X %04X %04X\r\n",crc1,crc2,crc3,crc4,crc5);
|
|
253 |
len1+=sprintf(str1+len1,"time %04d %04d %04d %04d %04d\r\n",us2-us1,us3-us2,us4-us3,us5-us4,us6-us5);
|
|
254 |
|
|
255 |
PutStr(str1,len1);
|
|
256 |
|
|
257 |
InitTimer(0,0);
|
|
258 |
InitTimer(1,1);
|
|
259 |
InitTimer(2,2);
|
|
260 |
InitTimer(3,3);
|
|
261 |
|
|
262 |
StartTimer(0,1000);
|
|
263 |
// StartTimer(2,1000);
|
|
264 |
Locate(13,1);LineCount=3;
|
|
265 |
return 0;
|
|
266 |
}
|
|
267 |
int sprintftime = 0;
|
|
268 |
int putstrtime = 0;
|
|
269 |
|
|
270 |
int ADCProcess()
|
|
271 |
{
|
|
272 |
uint16_t ADC_ConvertedValue=0;
|
|
273 |
static int CurChannel=LL_ADC_CHANNEL_0;
|
0aeaad
|
274 |
//static int waitcount = 0;
|
bfc108
|
275 |
if (!LL_ADC_REG_IsConversionOngoing(ADC1))
|
Q |
276 |
{
|
|
277 |
//waitcount++;
|
|
278 |
//if (waitcount<2) return 0;
|
|
279 |
//waitcount=0;
|
|
280 |
ADC_ConvertedValue = LL_ADC_REG_ReadConversionData12(ADC1);
|
|
281 |
|
|
282 |
// ADC_RegularChannelConfig(LL_ADC_CHANNEL_17,);
|
|
283 |
int channels = CurChannel ;//LL_ADC_REG_GetSequencerChannels(ADC1);
|
|
284 |
int nextchannel = LL_ADC_CHANNEL_0;
|
|
285 |
if ((channels & LL_ADC_CHANNEL_0) == LL_ADC_CHANNEL_0)
|
|
286 |
{
|
|
287 |
KMem.ADCValues[0] = ADC_ConvertedValue;
|
|
288 |
nextchannel = LL_ADC_CHANNEL_7;
|
|
289 |
}else if ((channels & LL_ADC_CHANNEL_7) == LL_ADC_CHANNEL_7)
|
|
290 |
{
|
|
291 |
KMem.ADCValues[2] = ADC_ConvertedValue;
|
|
292 |
nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
|
|
293 |
if (KMem.ADCValues[2] < 2200)
|
|
294 |
{
|
|
295 |
PowerDownEvent=1;
|
|
296 |
}else
|
|
297 |
{
|
|
298 |
PowerDownEvent=0;
|
|
299 |
}
|
|
300 |
}else if ((channels & LL_ADC_CHANNEL_16) == LL_ADC_CHANNEL_16)
|
|
301 |
{
|
|
302 |
KMem.ADCValues[6] = ADC_ConvertedValue;
|
|
303 |
nextchannel = LL_ADC_CHANNEL_VREFINT;
|
|
304 |
}else if ((channels & LL_ADC_CHANNEL_17) == LL_ADC_CHANNEL_17)
|
|
305 |
{
|
|
306 |
KMem.ADCValues[7] = ADC_ConvertedValue;
|
|
307 |
nextchannel = LL_ADC_CHANNEL_0;
|
|
308 |
}else
|
|
309 |
{
|
|
310 |
//ADCValues[0] = ADC_ConvertedValue;
|
|
311 |
}
|
|
312 |
//nextchannel = LL_ADC_CHANNEL_VREFINT;
|
|
313 |
LL_ADC_REG_SetSequencerChannels(ADC1,nextchannel);
|
|
314 |
LL_ADC_REG_StartConversion(ADC1);
|
|
315 |
CurChannel = nextchannel;
|
|
316 |
}
|
|
317 |
return 0;
|
|
318 |
}
|
|
319 |
|
|
320 |
int PowerDownProcess(void )
|
|
321 |
{
|
|
322 |
AddEventLog(KMem.CurTimeSec,EventTypePowerDown,1,12345);
|
|
323 |
SaveRunStat(&KMRunStat);
|
|
324 |
KMem.PwrFailCount++;
|
|
325 |
KMem.LastPwrFailTime = KMem.CurTimeSec;
|
|
326 |
return 0;
|
|
327 |
}
|
|
328 |
|
|
329 |
int PowerRecoverProcess(void)
|
|
330 |
{
|
|
331 |
KMem.PwrFailCount++;
|
|
332 |
|
|
333 |
return 0;
|
|
334 |
}
|
|
335 |
int ShowRunningInfo()
|
|
336 |
{
|
|
337 |
int Clk1=SysTick->VAL;
|
|
338 |
if (Uart1BaudFirstGot)
|
|
339 |
{
|
|
340 |
Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR;
|
|
341 |
Uart1BaudFirstGot=0;
|
|
342 |
}
|
|
343 |
if (Uart2BaudFirstGot)
|
|
344 |
{
|
|
345 |
Uart2baudval = HAL_RCC_GetPCLK1Freq() / USART2->BRR;
|
|
346 |
Uart2BaudFirstGot=0;
|
|
347 |
}
|
|
348 |
int Reload=SysTick->LOAD;
|
|
349 |
|
|
350 |
int Clk2=SysTick->VAL;
|
|
351 |
//int us2=GetuS();
|
|
352 |
int haltick=HAL_GetTick();
|
|
353 |
int len1=0;
|
|
354 |
uint32_t theUs = GetuS();
|
|
355 |
int nRunCount2=KMem.nRunCount;
|
|
356 |
if (!Uart1Stat.QTx.bEmpty) return 0;
|
|
357 |
|
|
358 |
if ( (nRunCount2 & 0xff) == 0x03)
|
|
359 |
{
|
|
360 |
Locate(13,1);LineCount=3;
|
|
361 |
} else if ((nRunCount2 & 0xff) == 0x0f)
|
|
362 |
{
|
|
363 |
int timeus1;
|
|
364 |
int timeus2;
|
|
365 |
|
|
366 |
len1=sprintf((char *)str1," N %8d Tk %8d %9u CFG %02X R %d M %d S %d %4d IN %04X OUT %04X \r\n",
|
|
367 |
KMem.nRunCount, haltick, theUs, KMem.EffJumperSW, repeater, bMaster, bSlave, Clk2, KMem.WX[0],KMem.WY[0]);
|
|
368 |
//len1=sprintf((char *)str1,"U%02X%02XA",x2,x2);
|
|
369 |
// Locate(10,1);
|
|
370 |
timeus1=GetuS();
|
|
371 |
PutStr(str1,len1);
|
|
372 |
timeus2=GetuS();
|
|
373 |
sprintftime = timeus1 - theUs;
|
|
374 |
putstrtime = timeus2 - timeus1;
|
|
375 |
if (IsTimerOn(0)) {StartTimer(1,1000);StopTimer(3);}
|
|
376 |
if (IsTimerOn(1)) {StartTimer(2,100);StopTimer(0);}
|
|
377 |
if (IsTimerOn(2)) {StartTimer(3,10);StopTimer(1);}
|
|
378 |
if (IsTimerOn(3)) {StartTimer(0,10000);StopTimer(2);}
|
|
379 |
}
|
|
380 |
if ((nRunCount2 & 0xff) == 0x2f && 0)
|
|
381 |
{
|
|
382 |
|
|
383 |
}
|
|
384 |
if ((nRunCount2 & 0xff) == 0x0af)
|
|
385 |
{
|
|
386 |
|
|
387 |
}
|
|
388 |
return 0;
|
|
389 |
}
|
|
390 |
|
|
391 |
int MasterFunc()
|
|
392 |
{
|
|
393 |
uint32_t tick1=HAL_GetTick();
|
|
394 |
uint32_t thisuS=GetuS();
|
|
395 |
|
|
396 |
int len1=0;
|
|
397 |
|
|
398 |
if ((MasterRecved && thisuS-SendTimeuS>50) || thisuS-SendTimeuS>1500u)
|
|
399 |
{
|
|
400 |
if (!MasterRecved)
|
|
401 |
{
|
|
402 |
TimeOutCount++;
|
|
403 |
Uart2Stat.TimeOutErr++;
|
|
404 |
ChnStats[nCurPollId].LostPackets++;
|
|
405 |
ChnStats[nCurPollId].CtnLstPkts++;
|
|
406 |
if (ChnStats[nCurPollId].CtnLstPkts>ChnStats[nCurPollId].MaxCtnLstPkts)
|
|
407 |
{ChnStats[nCurPollId].MaxCtnLstPkts=ChnStats[nCurPollId].CtnLstPkts;}
|
|
408 |
if (ChnStats[nCurPollId].CtnLstPkts>3)
|
|
409 |
{
|
|
410 |
ChnStats[nCurPollId].Stat=0;
|
|
411 |
KMem.ErrStat=200;
|
|
412 |
|
|
413 |
{BufferIn[nCurPollId]=0;}
|
|
414 |
}
|
|
415 |
// LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
|
|
416 |
}else
|
|
417 |
{
|
|
418 |
ChnStats[nCurPollId].Stat=1;
|
|
419 |
|
|
420 |
KMem.RunStat=100;
|
|
421 |
}
|
|
422 |
nCurPollId ++;
|
|
423 |
if (nCurPollId > nChilds)
|
|
424 |
{
|
|
425 |
CircleTime=thisuS-LastCircleStartTime;
|
|
426 |
LastCircleStartTime=thisuS;
|
|
427 |
nSeq++;
|
|
428 |
nCurPollId=1;
|
|
429 |
}
|
|
430 |
Datas[0]=BufferOut[nCurPollId];
|
|
431 |
Datas[1]=BufferOut[nCurPollId+1];;
|
|
432 |
Datas[2]=ChnStats[nCurPollId].Stat;
|
|
433 |
Datas[3]=0;
|
|
434 |
Datas[4]=tick1&0xff;
|
|
435 |
Datas[5]=(tick1>>8)&0xff;
|
|
436 |
Datas[6]=(tick1>>16)&0xff;
|
|
437 |
Datas[7]=(tick1>>24)&0xff;
|
|
438 |
|
|
439 |
SendTimeuS=thisuS;
|
|
440 |
len1=MakePacket((pPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas);
|
|
441 |
SendPacket2((pPacket)PacketBuf1,len1);
|
|
442 |
ChnStats[nCurPollId].SendPackets++;
|
|
443 |
ChnStats[nCurPollId].SendTimeInterval=SendTimeuS-ChnStats[nCurPollId].LastSentTimeuS;
|
|
444 |
ChnStats[nCurPollId].LastSentTimeuS=SendTimeuS;
|
|
445 |
PacketLength = len1;
|
|
446 |
SendTime=tick1;
|
|
447 |
|
|
448 |
MasterRecved=0;
|
|
449 |
// LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);
|
|
450 |
//ToggleErrLed();
|
|
451 |
// ToggleOut8();
|
|
452 |
|
|
453 |
}
|
|
454 |
|
|
455 |
Clk3=SysTick->VAL;
|
|
456 |
// LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_4);
|
|
457 |
// HAL_Delay(1);
|
|
458 |
return 0;
|
|
459 |
}
|
|
460 |
|
|
461 |
int SlaveFunc()
|
|
462 |
{
|
|
463 |
int ThisuS=GetuS();
|
|
464 |
int thisRecvTime=RecvTimeuS;
|
|
465 |
if (SlaveRecved)
|
|
466 |
{
|
|
467 |
KMem.RunStat=100;
|
|
468 |
SlaveRecved=0;
|
|
469 |
}else if ((ThisuS - thisRecvTime) >12000u)
|
|
470 |
{
|
|
471 |
KMem.ErrStat=200;
|
|
472 |
KMem.SDD[17]=1;
|
|
473 |
KMem.SDD[18]=ThisuS;
|
|
474 |
KMem.SDD[19]=RecvTimeuS;
|
|
475 |
}else if ( ThisuS > (thisRecvTime + 12000u))
|
|
476 |
{
|
|
477 |
KMem.ErrStat=200;
|
|
478 |
KMem.SDD[17]=2;
|
|
479 |
KMem.SDD[18]=ThisuS;
|
|
480 |
KMem.SDD[19]=RecvTimeuS;
|
|
481 |
}
|
|
482 |
|
|
483 |
return 0;
|
|
484 |
}
|
|
485 |
|
|
486 |
/* USER CODE END 0 */
|
|
487 |
|
|
488 |
/**
|
|
489 |
* @brief The application entry point.
|
|
490 |
*
|
|
491 |
* @retval None
|
|
492 |
*/
|
|
493 |
int main(void)
|
|
494 |
{
|
|
495 |
/* USER CODE BEGIN 1 */
|
|
496 |
|
|
497 |
InitUartstat(&Uart1Stat,Uart1TxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
|
|
498 |
InitUartstat(&Uart2Stat,Uart2TxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
|
|
499 |
/* USER CODE END 1 */
|
|
500 |
|
|
501 |
/* MCU Configuration----------------------------------------------------------*/
|
|
502 |
|
|
503 |
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
|
504 |
HAL_Init();
|
|
505 |
|
|
506 |
/* USER CODE BEGIN Init */
|
|
507 |
|
|
508 |
for (int i=0;i<16;i++)
|
|
509 |
{
|
|
510 |
// memset(ChnStats[i],0,0);
|
|
511 |
ChnStats[i].SendPackets=0;
|
|
512 |
ChnStats[i].RecvPackets=0;
|
|
513 |
ChnStats[i].LostPackets=0;
|
|
514 |
ChnStats[i].CtnLstPkts=0;
|
|
515 |
ChnStats[i].MaxCtnLstPkts=0;
|
|
516 |
ChnStats[i].NotPkgErr=0;
|
|
517 |
ChnStats[i].PkgLenErr=0;
|
|
518 |
ChnStats[i].TimeOutErr=0;
|
|
519 |
ChnStats[i].BCCErr=0;
|
|
520 |
ChnStats[i].Delay=0;
|
|
521 |
ChnStats[i].MaxDelay=0;
|
|
522 |
}
|
|
523 |
|
|
524 |
KMem.LastScanTime=0;
|
|
525 |
KMem.ScanTimeuS=0;
|
|
526 |
KMem.MinScanTimeuS=99999;
|
|
527 |
KMem.MaxScanTimeuS=0;
|
|
528 |
|
|
529 |
// KMem.SDD[14]=(unsigned int)&KMStoreSysCfg;
|
|
530 |
// KMem.SDD[15]=(unsigned int)&KMStoreSysCfg1;
|
|
531 |
KMem.SDD[12]=((uint32_t *)UID_BASE)[0];
|
|
532 |
// KMem.SDD[13]=((uint32_t *)UID_BASE)[1];
|
|
533 |
// KMem.SDD[14]=((uint32_t *)UID_BASE)[2];
|
|
534 |
KMem.SDD[13]=PendSvCount;
|
|
535 |
KMem.SDD[14]=RCC->CSR;
|
|
536 |
// KMem.SDD[15]=*(uint32_t *)FLASHSIZE_BASE;
|
|
537 |
// KMem.SDD[16]=(unsigned int)&KMSysCfg;
|
|
538 |
|
|
539 |
/* USER CODE END Init */
|
|
540 |
|
|
541 |
/* Configure the system clock */
|
|
542 |
SystemClock_Config();
|
|
543 |
|
|
544 |
/* USER CODE BEGIN SysInit */
|
|
545 |
TickFreq=10000; //Tick频率
|
|
546 |
InituS(TickFreq);
|
|
547 |
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq); //ÖØж¨ÒåSysTickµÄƵÂÊÎ
|
|
548 |
|
|
549 |
/* USER CODE END SysInit */
|
|
550 |
|
|
551 |
/* Initialize all configured peripherals */
|
|
552 |
MX_GPIO_Init();
|
|
553 |
MX_DMA_Init();
|
|
554 |
|
|
555 |
KMachineInit();
|
|
556 |
ReadSysCfgFromFlash(&KMSysCfg);
|
|
557 |
|
|
558 |
KMem.EffJumperSW=ReadJumperSW();
|
|
559 |
nAddr=KMem.EffJumperSW&0x7;
|
|
560 |
if (KMem.EffJumperSW == 0x0f) {repeater=1;bMaster=1;bSlave=0;}
|
|
561 |
else if ((KMem.EffJumperSW&0x08)!=0) {bMaster=1;bSlave=0;}
|
|
562 |
else{bMaster=0;bSlave=1;}
|
|
563 |
nChilds=nAddr;
|
|
564 |
nCurPollId=1;
|
|
565 |
if (KMem.EffJumperSW == 0x00) Uart1Baud = DefaultUart1Baud;
|
|
566 |
MX_USART1_UART_Init();
|
|
567 |
MX_USART2_UART_Init();
|
0aeaad
|
568 |
// MX_SPI1_Init();
|
Q |
569 |
MX_SPI2_Init();
|
bfc108
|
570 |
MX_ADC_Init();
|
Q |
571 |
MX_IWDG_Init();
|
|
572 |
|
|
573 |
/* USER CODE BEGIN 2 */
|
|
574 |
LL_USART_EnableIT_RXNE(USART1);
|
|
575 |
LL_USART_EnableIT_IDLE(USART1);
|
|
576 |
LL_USART_EnableIT_TC(USART1);
|
|
577 |
|
|
578 |
LL_USART_EnableIT_RXNE(USART2);
|
|
579 |
LL_USART_EnableIT_IDLE(USART2);
|
|
580 |
LL_USART_EnableIT_TC(USART2);
|
|
581 |
|
|
582 |
// if (bSlave)
|
|
583 |
{
|
|
584 |
// LL_USART_EnableAutoBaudRate(USART1);
|
|
585 |
// LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);
|
|
586 |
// LL_USART_EnableAutoBaudRate(USART2);
|
|
587 |
// LL_USART_SetAutoBaudRateMode(USART2, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);
|
|
588 |
}
|
|
589 |
//LL_USART_EnableIT_TXE(USART1);
|
|
590 |
/* USER CODE END 2 */
|
|
591 |
|
|
592 |
|
|
593 |
/* Infinite loop */
|
|
594 |
/* USER CODE BEGIN WHILE */
|
|
595 |
|
|
596 |
HAL_Delay(10);
|
|
597 |
SetRunLed(1); //Turn On Run Led
|
|
598 |
SetErrLed(0); //Turn Off Err Led
|
|
599 |
PutOutput (0); //Clear all Output
|
|
600 |
Enable595(1); //Enable 595 Output
|
0aeaad
|
601 |
if (GetBoardType() == 7 || GetBoardType() ==8
|
Q |
602 |
|| GetBoardType() == 9 || GetBoardType() ==10 )
|
|
603 |
{
|
|
604 |
displayInput(0xffff); //
|
|
605 |
EnableDisIn(1); //Input Diaplay Enable 595
|
|
606 |
}
|
bfc108
|
607 |
SetOutStat(0); //OK Good, signal
|
Q |
608 |
// ShowInitInfo();
|
|
609 |
KMem.LastScanTime = GetuS();
|
|
610 |
while (1)
|
|
611 |
{
|
|
612 |
//int MyKeyStat1,MyKeyStat2;
|
|
613 |
//MyKeyStat1=GetInput();
|
|
614 |
|
|
615 |
|
|
616 |
//*((unsigned int *)&(PLCMem.SDT[10]))=nRunCount;
|
|
617 |
// KMem.nRunCount=nRunCount;
|
|
618 |
SlowFlicker=0;
|
|
619 |
FastFlicker=1;
|
|
620 |
us1=GetuS();
|
|
621 |
int haltick=HAL_GetTick();
|
|
622 |
|
|
623 |
int CurJumperSW=ReadJumperSW();
|
|
624 |
KMem.CurJumperSW=CurJumperSW;
|
|
625 |
KMem.haltick=haltick;
|
|
626 |
// KMem.TotalRunTime=TotalRunTime;
|
|
627 |
// KMem.ThisRunTime=ThisRunTime;
|
|
628 |
|
|
629 |
// *((unsigned int *)&(PLCMem.SDT[2]))=nChilds;
|
|
630 |
|
|
631 |
KMem.WX[0]= GetInput();
|
|
632 |
|
|
633 |
if (GetBoardType() == 7 || GetBoardType() ==8
|
|
634 |
|| GetBoardType() == 9 || GetBoardType() ==10 )
|
|
635 |
{
|
|
636 |
displayInput(KMem.WX[0]);
|
|
637 |
}
|
|
638 |
us2=GetuS();
|
|
639 |
|
|
640 |
// ProcessPLCPROG(prog1, nSizeProg1);
|
|
641 |
// ScanTimeuS=us2-LastScanTime;
|
|
642 |
// LastScanTime = us2;
|
|
643 |
if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
|
|
644 |
if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
|
|
645 |
if (repeater) { RepeaterFunc(); }
|
|
646 |
us3=GetuS();
|
|
647 |
if ((KMem.nRunCount &0x1f) == 0x02)
|
|
648 |
{
|
|
649 |
ADCProcess();
|
|
650 |
if (PowerDownEvent)
|
|
651 |
{
|
|
652 |
if (!OldPowerDownEvent)
|
|
653 |
{
|
|
654 |
OldPowerDownEvent = PowerDownEvent;
|
|
655 |
OldPowerDownEventTime = nCurTick;
|
|
656 |
PowerDownProcess();
|
|
657 |
}
|
|
658 |
}else
|
|
659 |
{
|
|
660 |
if (OldPowerDownEvent)
|
|
661 |
{
|
|
662 |
OldPowerDownEvent=PowerDownEvent;
|
|
663 |
PowerRecoverProcess();
|
|
664 |
|
|
665 |
}
|
|
666 |
}
|
|
667 |
}
|
|
668 |
if (bMaster)
|
|
669 |
{
|
|
670 |
BufferOut[1]=KMem.WX[0]&0xff;
|
|
671 |
BufferOut[2]=(KMem.WX[0]>>8)&0xff;
|
|
672 |
MasterFunc();
|
|
673 |
|
|
674 |
KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8);
|
|
675 |
|
|
676 |
if (haltick&0x00002000) SlowFlicker=1;
|
|
677 |
else SlowFlicker=0;
|
|
678 |
if (haltick&0x00000800) FastFlicker=1;
|
|
679 |
else FastFlicker=0;
|
|
680 |
|
|
681 |
}
|
|
682 |
if (bSlave)
|
|
683 |
{
|
|
684 |
BufferOut[0]=KMem.WX[0];
|
|
685 |
SlaveFunc();
|
|
686 |
if (! KMem.RunStat) {BufferIn[0]=0;}
|
|
687 |
KMem.WY[0]=BufferIn[0];
|
|
688 |
|
|
689 |
if (nSlaveTick&0x00002000) SlowFlicker=1;
|
|
690 |
else SlowFlicker=0;
|
|
691 |
if (nSlaveTick&0x00000800) FastFlicker=1;
|
|
692 |
else FastFlicker=0;
|
|
693 |
|
|
694 |
}
|
|
695 |
// KMem.WY[0]=nCount2>>5;
|
|
696 |
if (KMem.RunStat) {KMem.RunStat--;}
|
|
697 |
if (KMem.ErrStat) {KMem.ErrStat--;}
|
|
698 |
|
|
699 |
if (!KMem.RunStat) SetRunLed(SlowFlicker);
|
|
700 |
else SetRunLed(FastFlicker);
|
|
701 |
|
|
702 |
if (!KMem.ErrStat)
|
|
703 |
{
|
|
704 |
SetErrLed(0);
|
|
705 |
SetOutStat(1);
|
|
706 |
}
|
|
707 |
else
|
|
708 |
{
|
|
709 |
SetErrLed(FastFlicker);
|
|
710 |
SetOutStat(0);
|
|
711 |
|
|
712 |
}
|
|
713 |
|
|
714 |
// SetRunLed(RunStat);
|
|
715 |
// SetErrLed(ErrStat);
|
|
716 |
|
|
717 |
us4=GetuS();
|
|
718 |
// EffJumperSW = GetInput(20)&0xff;
|
|
719 |
|
|
720 |
PutOutput (KMem.WY[0]);
|
0aeaad
|
721 |
//PutOutput (KMem.nRunCount>>8);
|
bfc108
|
722 |
//PutOutput(0x0f70);
|
Q |
723 |
|
|
724 |
us5=GetuS();
|
|
725 |
// if (bMaster) ShowInfo();
|
|
726 |
// if (bSlave) ShowInfo();
|
|
727 |
us6=GetuS();
|
|
728 |
add1(10,10);
|
|
729 |
for (int i=0;i<64;i++)
|
|
730 |
{
|
|
731 |
// ProcessTimer(i);
|
|
732 |
}
|
|
733 |
KMem.nRunCount++;
|
|
734 |
int nSize=sizeof(stChnStat);
|
|
735 |
memcpy(&KMem.SDT[64],&ChnStats[1],nSize);
|
|
736 |
memcpy(&KMem.SDT[64+nSize/2],&ChnStats[2],nSize);
|
|
737 |
// for (int i=0;i<128;i++) { SDT[i]=i; }
|
|
738 |
// SDT[48]=55;
|
|
739 |
if (Uart1RecvBuf1DataLen >0 && Uart1Stat.bPacketRecved)
|
|
740 |
{
|
|
741 |
KLParsePacket(Uart1RecvBuf1,Uart1RecvBuf1DataLen);
|
|
742 |
Uart1RecvBuf1DataLen=0;
|
|
743 |
Uart1Stat.bPacketRecved=0;
|
|
744 |
}
|
|
745 |
if (bSlave) HAL_Delay(0);
|
|
746 |
/*
|
|
747 |
if (!IsEmpty(&Uart1Stat.QRx))
|
|
748 |
{
|
|
749 |
unsigned char k=PopOne(&Uart1Stat.QRx);
|
|
750 |
if (k=='L')
|
|
751 |
{
|
|
752 |
clearscreen();
|
|
753 |
}
|
|
754 |
}
|
|
755 |
*/
|
|
756 |
LL_IWDG_ReloadCounter(IWDG);
|
|
757 |
|
|
758 |
} //while (1) ;
|
|
759 |
/* USER CODE END WHILE */
|
|
760 |
|
|
761 |
/* USER CODE BEGIN 3 */
|
|
762 |
|
|
763 |
/* USER CODE END 3 */
|
|
764 |
|
|
765 |
}
|
|
766 |
|
|
767 |
|
|
768 |
/* USER CODE BEGIN 4 */
|
|
769 |
|
|
770 |
/* USER CODE END 4 */
|
|
771 |
|
|
772 |
/**
|
|
773 |
* @brief This function is executed in case of error occurrence.
|
|
774 |
* @param file: The file name as string.
|
|
775 |
* @param line: The line in file as a number.
|
|
776 |
* @retval None
|
|
777 |
*/
|
|
778 |
void _Error_Handler(char *file, int line)
|
|
779 |
{
|
|
780 |
/* USER CODE BEGIN Error_Handler_Debug */
|
|
781 |
/* User can add his own implementation to report the HAL error return state */
|
|
782 |
while(1)
|
|
783 |
{
|
|
784 |
}
|
|
785 |
/* USER CODE END Error_Handler_Debug */
|
|
786 |
}
|
|
787 |
|
|
788 |
#ifdef USE_FULL_ASSERT
|
|
789 |
/**
|
|
790 |
* @brief Reports the name of the source file and the source line number
|
|
791 |
* where the assert_param error has occurred.
|
|
792 |
* @param file: pointer to the source file name
|
|
793 |
* @param line: assert_param error line source number
|
|
794 |
* @retval None
|
|
795 |
*/
|
|
796 |
void assert_failed(uint8_t* file, uint32_t line)
|
|
797 |
{
|
|
798 |
/* USER CODE BEGIN 6 */
|
|
799 |
/* User can add his own implementation to report the file name and line number,
|
|
800 |
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
801 |
/* USER CODE END 6 */
|
|
802 |
}
|
|
803 |
#endif /* USE_FULL_ASSERT */
|
|
804 |
|
|
805 |
/**
|
|
806 |
* @}
|
|
807 |
*/
|
|
808 |
|
|
809 |
/**
|
|
810 |
* @}
|
|
811 |
*/
|
|
812 |
|
|
813 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|