QuakeGod
2022-09-29 e1f35018c4dec304b00f50d9dbe12204fd57a623
Src/main.c
@@ -49,22 +49,23 @@
#include "KLink.h"
#include "string.h"
#include "BSP.h"
#include "ModbusRTU.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
#define ADCrefAddr 0x1FFFF7BA
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
#define RXBUFSIZE 128
#define TXBUFSIZE 128
#define RX2BUFSIZE 128
#define TX2BUFSIZE 128
unsigned char Uart1RxBuf[256];
unsigned char Uart1TxBuf[512];
unsigned char Uart2RxBuf[RXBUFSIZE];
unsigned char Uart2TxBuf[TXBUFSIZE];
unsigned char Uart2RxBuf[RX2BUFSIZE];
unsigned char Uart2TxBuf[TX2BUFSIZE];
//unsigned char buf1[128];   
@@ -92,6 +93,7 @@
int LastCircleStartTime=0;
int CircleTime=0;
stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE;
//volatile unsigned int nRunCount=0;
volatile int nCount2=0;
@@ -109,6 +111,12 @@
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
const unsigned char LEDSEGTAB[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,   //0-F
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1,  //0.-F.
0x00,0x40,         //  ,-,_,~,o,n,N,<,>,J,r,
};
/* USER CODE END PFP */
@@ -254,12 +262,12 @@
      
      PutStr(str1,len1);
   InitTimer(0,0);
   InitTimer(1,1);
   InitTimer(2,2);
   InitTimer(3,3);
//   InitTimer(0,0);
//   InitTimer(1,1);
//   InitTimer(2,2);
//   InitTimer(3,3);
   
   StartTimer(0,1000);
//   RunTimer(0,1000);
//   StartTimer(2,1000);
   Locate(13,1);LineCount=3;
   return 0;
@@ -269,9 +277,24 @@
int ADCProcess()
{
               uint16_t ADC_ConvertedValue=0;
   // 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++;
@@ -285,8 +308,8 @@
               if ((channels & LL_ADC_CHANNEL_0) == LL_ADC_CHANNEL_0)
               {
                  KMem.ADCValues[0] = ADC_ConvertedValue;
                  nextchannel = LL_ADC_CHANNEL_7;
               }else if ((channels & LL_ADC_CHANNEL_7) == LL_ADC_CHANNEL_7)
                  nextchannel = LL_ADC_CHANNEL_8;
               }else if ((channels & LL_ADC_CHANNEL_8) == LL_ADC_CHANNEL_8)
               {
                  KMem.ADCValues[2] = ADC_ConvertedValue;                  
                  nextchannel = LL_ADC_CHANNEL_TEMPSENSOR;
@@ -304,6 +327,8 @@
               }else if ((channels & LL_ADC_CHANNEL_17) == LL_ADC_CHANNEL_17)
               {
                  KMem.ADCValues[7] = ADC_ConvertedValue;                  
                  KMem.ADCValues[5] = *((unsigned short *)ADCrefAddr);
                  nextchannel = LL_ADC_CHANNEL_0;
               }else
               {
@@ -372,10 +397,10 @@
            timeus2=GetuS();
            sprintftime = timeus1 - theUs;
            putstrtime = timeus2 - timeus1;
            if (IsTimerOn(0)) {StartTimer(1,1000);StopTimer(3);}
            if (IsTimerOn(1)) {StartTimer(2,100);StopTimer(0);}
            if (IsTimerOn(2)) {StartTimer(3,10);StopTimer(1);}
            if (IsTimerOn(3)) {StartTimer(0,10000);StopTimer(2);}
            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)
         {
@@ -395,21 +420,22 @@
   
         int len1=0;
         if ((MasterRecved && thisuS-SendTimeuS>50) || thisuS-SendTimeuS>1500u)
         if ((MasterRecved && MasterRecvOK && thisuS-SendTimeuS>50) || thisuS-SendTimeuS>1000u)
         {
            if (!MasterRecved)
            if (!MasterRecvOK)
            {
               TimeOutCount++;
               Uart2Stat.TimeOutErr++; 
               ChnStats[nCurPollId].LostPackets++;
               ChnStats[nCurPollId].CtnLstPkts++;
               if (!MasterRecved) {ChnStats[nCurPollId].TimeOutErr++;}
               if (ChnStats[nCurPollId].CtnLstPkts>ChnStats[nCurPollId].MaxCtnLstPkts)
               {ChnStats[nCurPollId].MaxCtnLstPkts=ChnStats[nCurPollId].CtnLstPkts;}
               if (ChnStats[nCurPollId].CtnLstPkts>3)
               {
                  ChnStats[nCurPollId].Stat=0;
                  KMem.ErrStat=200;
                  {BufferIn[nCurPollId]=0;}
               }
            //   LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
@@ -426,7 +452,16 @@
               LastCircleStartTime=thisuS;
               nSeq++;
               nCurPollId=1;
            }
            }
            if (KMRunStat.WorkMode==0)
            {
               KMem.WX[0]= GetInput();
               KMem.WY[1]=KMem.WX[0]&0xff;
               KMem.WY[2]=(KMem.WX[0]>>8)&0xff;
            }
            BufferOut[1]=KMem.WY[1];
            BufferOut[2]=KMem.WY[2];
            Datas[0]=BufferOut[nCurPollId];
            Datas[1]=BufferOut[nCurPollId+1];;
            Datas[2]=ChnStats[nCurPollId].Stat;
@@ -446,6 +481,7 @@
            SendTime=tick1;
            MasterRecved=0;
            MasterRecvOK=0;
         //   LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);      
            //ToggleErrLed();
//            ToggleOut8();
@@ -464,17 +500,17 @@
      int thisRecvTime=RecvTimeuS;
      if (SlaveRecved)
      {
         KMem.RunStat=100;
         KMem.RunStat=8000;
         SlaveRecved=0;
      }else if ((ThisuS - thisRecvTime) >12000u)
      {
         KMem.ErrStat=200;
         KMem.ErrStat=8000;
         KMem.SDD[17]=1;
         KMem.SDD[18]=ThisuS;
         KMem.SDD[19]=RecvTimeuS;
      }else if ( ThisuS > (thisRecvTime + 12000u))
      {
         KMem.ErrStat=200;
         KMem.ErrStat=8000;
         KMem.SDD[17]=2;
         KMem.SDD[18]=ThisuS;
         KMem.SDD[19]=RecvTimeuS;
@@ -544,7 +580,7 @@
  /* USER CODE BEGIN SysInit */
   TickFreq=10000;      //Tick频率
   InituS(TickFreq);   
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq);   //ÖØж¨ÒåSysTickµÄƵÂÊÎ
 // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq);   //ÖØж¨ÒåSysTickµÄƵÂÊÎ
  /* USER CODE END SysInit */
@@ -565,7 +601,8 @@
   if (KMem.EffJumperSW == 0x00) Uart1Baud = DefaultUart1Baud;
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
//   MX_SPI1_Init();
   MX_SPI1_Init();
   MX_SPI2_Init();
  MX_ADC_Init();
   MX_IWDG_Init();
@@ -575,7 +612,8 @@
   LL_USART_EnableIT_IDLE(USART1);
   LL_USART_EnableIT_TC(USART1);
   LL_USART_EnableIT_RXNE(USART2);
//   LL_USART_EnableIT_RXNE(USART2);
   Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));
   LL_USART_EnableIT_IDLE(USART2);
   LL_USART_EnableIT_TC(USART2);
   
@@ -605,8 +643,34 @@
         EnableDisIn(1);            //Input Diaplay Enable 595 
      }
   SetOutStat(0);         //OK Good, signal
//   ShowInitInfo();
   ShowInitInfo();
   KMem.LastScanTime = GetuS();
/*
   int time1=GetuS();
   PutOutput(0x01);
   int x;
   while (1)
   {
      x = GetInput();
      if (x&0x1) break;
   }
   int time2=GetuS();
   KMem.SDD[17]=time2-time1;
   PutOutput(0x00);
   while (1)
   {
      x = GetInput();
      if ((x&0x1) == 0) break;
   }
   int time3=GetuS();
   KMem.SDD[18]=time3-time2;
*/
   InitPLC();
   StartPLC();
   KMRunStat.WorkMode=1;
   KMem.WX[5]=0x5a;
  while (1)
  {
      //int MyKeyStat1,MyKeyStat2;
@@ -620,15 +684,17 @@
      us1=GetuS();
      int haltick=HAL_GetTick();
      
      int CurJumperSW=ReadJumperSW();
      KMem.CurJumperSW=CurJumperSW;
//      int CurJumperSW=ReadJumperSW();
//      KMem.CurJumperSW=CurJumperSW;
      KMem.haltick=haltick;
//      KMem.TotalRunTime=TotalRunTime;
//      KMem.ThisRunTime=ThisRunTime;      
      
//      *((unsigned int *)&(PLCMem.SDT[2]))=nChilds;
      KMem.WX[0]= GetInput();
//      KMem.SDD[13]=PendSvCount;
//      KMem.SDD[14]=RCC->CSR;
      KMem.WX[0]= GetInput();
      
      if (GetBoardType() == 7 || GetBoardType() ==8 
         || GetBoardType() == 9 || GetBoardType() ==10 ) 
@@ -636,13 +702,27 @@
         displayInput(KMem.WX[0]);
      }
      us2=GetuS();
//      ProcessPLCPROG(prog1, nSizeProg1);
//      ScanTimeuS=us2-LastScanTime;
//      LastScanTime = us2;
//      pProgs = (stBinProg1 *) STORE_PRG_BASE;
      if (   KMRunStat.WorkMode==1)
      {
      if (KMRunStat.nBinProgBank == 0){
         pProgs=(stBinProg1 *)STORE_PRG_BASE;
      }else {
         pProgs=(stBinProg1 *)ALT_PRG_BASE;
      }
      nSizeProg1=KMRunStat.nBinProgSize;
      ProcessPLCBinProg(pProgs, nSizeProg1);
      }
      KMem.ScanTimeuS=us2-KMem.LastScanTime;
      KMem.LastScanTime = us2;
      if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
      if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
      if (repeater)      {   RepeaterFunc();   }
//      if (repeater)      {   RepeaterFunc();   }
      us3=GetuS();
      if ((KMem.nRunCount &0x1f) == 0x02)
      {
@@ -667,11 +747,11 @@
      }
      if (bMaster)      
      {
         BufferOut[1]=KMem.WX[0]&0xff;
         BufferOut[2]=(KMem.WX[0]>>8)&0xff;
//         BufferOut[1]=KMem.WX[0]&0xff;
//         BufferOut[2]=(KMem.WX[0]>>8)&0xff;
         MasterFunc();
         
         KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8);
//         KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8);
         if (haltick&0x00002000) SlowFlicker=1;
         else SlowFlicker=0;
@@ -681,7 +761,7 @@
      }
      if (bSlave)      
      {
         BufferOut[0]=KMem.WX[0];
//         BufferOut[0]=KMem.WX[0];
         SlaveFunc();   
         if (! KMem.RunStat) {BufferIn[0]=0;}
         KMem.WY[0]=BufferIn[0];
@@ -738,7 +818,12 @@
//      SDT[48]=55;
      if (Uart1RecvBuf1DataLen >0 && Uart1Stat.bPacketRecved)
      {
         KLParsePacket(Uart1RecvBuf1,Uart1RecvBuf1DataLen);
         int res1 = -1;
         res1 = ModBusSlaveParsePkg(Uart1RecvBuf1,Uart1RecvBuf1DataLen);
         if (res1 !=0)
         {
            KLParsePacket(Uart1RecvBuf1,Uart1RecvBuf1DataLen);
         }
         Uart1RecvBuf1DataLen=0;
         Uart1Stat.bPacketRecved=0;
      }
@@ -752,7 +837,23 @@
            clearscreen();
         }
      }
*/
*/
      unsigned char pos,seg;
      unsigned short val;
      pos=((KMem.nRunCount)&0x3);
      //val=(KMem.nRunCount)&0xfff;
      val=KMem.ErrStat;
      char buf5[20];
      sprintf(buf5,"%4d",val);
      val=buf5[3-pos];
      if (val <'0' || val >'9') {seg=0;}
      else {seg=LEDSEGTAB[val-'0'];}
      pos=1<<pos;
      //pos=1;
      //seg=2;
      seg=~seg;
      PutOutputSPI1(pos|(seg<<8));
    LL_IWDG_ReloadCounter(IWDG);
      
  }   //while (1) ;