/** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** ** This notice applies to any and all portions of this file * that are not between comment pairs USER CODE BEGIN and * USER CODE END. Other portions of this file, whether * inserted by the user or by software development tools * are owned by their respective copyright owners. * * COPYRIGHT(c) 2018 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "stm32f0xx_hal.h" /* USER CODE BEGIN Includes */ #include "Globaldef.h" #include "Functions.h" #include "KMachine.h" #include "PLCfunctions.h" //#include "KBus.h" #include "KLink.h" #include "string.h" #include "BSP.h" /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ #define RXBUFSIZE 128 #define TXBUFSIZE 128 unsigned char Uart1RxBuf[256]; unsigned char Uart1TxBuf[512]; unsigned char Uart2RxBuf[RXBUFSIZE]; unsigned char Uart2TxBuf[TXBUFSIZE]; //unsigned char buf1[128]; unsigned char Datas[128]; unsigned int SlowFlicker=0; unsigned int FastFlicker=0; volatile int PacketLength = 0; char str1[256]; int TimeOutCount=0; int Clk3=0; int ContinueSend=0; int Uart1baudval=0; int Uart2baudval=0; int repeater=0; int SendTime,Latancy,LatancyClk,SendClk; int LineCount=0; int LastCircleStartTime=0; int CircleTime=0; //volatile unsigned int nRunCount=0; volatile int nCount2=0; uint32_t us1,us2,us3,us4,us5,us6; const unsigned char buf1[16]={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00}; volatile int PowerDownEvent=0; volatile int OldPowerDownEvent=0; volatile int OldPowerDownEventTime=0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ __asm int add1(int a,int b) { add r0,r1,r0 BLX lr } int HexToInt(char ch) { if (ch>='0' && ch <='9') return ch-'0'; if (ch>='A' && ch <='F') return ch-'A'+10; if (ch>='a' && ch <='f') return ch-'a'+10; return 0; } void HAL_SYSTICK_Callback(void) { static int Count=0; CurTickuS += 100; nCurTick++; nSlaveTick++; Count++; if (Count>=10000) { Count=0; KMem.CurTimeSec++; KMem.ThisRunTime++; KMem.TotalRunTime++; } return; } int FormatHex(char * buf1,uchar * data,int n) { int len1=0; for (int i=0;iVAL; if (Uart1BaudFirstGot) { Uart1baudval = HAL_RCC_GetPCLK1Freq() / USART1->BRR; Uart1BaudFirstGot=0; } if (Uart2BaudFirstGot) { Uart2baudval = HAL_RCC_GetPCLK1Freq() / USART2->BRR; Uart2BaudFirstGot=0; } int Reload=SysTick->LOAD; int Clk2=SysTick->VAL; //int us2=GetuS(); int haltick=HAL_GetTick(); int len1=0; uint32_t theUs = GetuS(); int nRunCount2=KMem.nRunCount; if (!Uart1Stat.QTx.bEmpty) return 0; if ( (nRunCount2 & 0xff) == 0x03) { Locate(13,1);LineCount=3; } else if ((nRunCount2 & 0xff) == 0x0f) { int timeus1; int timeus2; len1=sprintf((char *)str1," N %8d Tk %8d %9u CFG %02X R %d M %d S %d %4d IN %04X OUT %04X \r\n", KMem.nRunCount, haltick, theUs, KMem.EffJumperSW, repeater, bMaster, bSlave, Clk2, KMem.WX[0],KMem.WY[0]); //len1=sprintf((char *)str1,"U%02X%02XA",x2,x2); // Locate(10,1); timeus1=GetuS(); PutStr(str1,len1); 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 ((nRunCount2 & 0xff) == 0x2f && 0) { } if ((nRunCount2 & 0xff) == 0x0af) { } return 0; } int MasterFunc() { uint32_t tick1=HAL_GetTick(); uint32_t thisuS=GetuS(); int len1=0; if ((MasterRecved && thisuS-SendTimeuS>50) || thisuS-SendTimeuS>1500u) { if (!MasterRecved) { TimeOutCount++; Uart2Stat.TimeOutErr++; ChnStats[nCurPollId].LostPackets++; ChnStats[nCurPollId].CtnLstPkts++; 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); }else { ChnStats[nCurPollId].Stat=1; KMem.RunStat=100; } nCurPollId ++; if (nCurPollId > nChilds) { CircleTime=thisuS-LastCircleStartTime; LastCircleStartTime=thisuS; nSeq++; nCurPollId=1; } Datas[0]=BufferOut[nCurPollId]; Datas[1]=BufferOut[nCurPollId+1];; Datas[2]=ChnStats[nCurPollId].Stat; Datas[3]=0; Datas[4]=tick1&0xff; Datas[5]=(tick1>>8)&0xff; Datas[6]=(tick1>>16)&0xff; Datas[7]=(tick1>>24)&0xff; SendTimeuS=thisuS; len1=MakePacket((pPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas); SendPacket2((pPacket)PacketBuf1,len1); ChnStats[nCurPollId].SendPackets++; ChnStats[nCurPollId].SendTimeInterval=SendTimeuS-ChnStats[nCurPollId].LastSentTimeuS; ChnStats[nCurPollId].LastSentTimeuS=SendTimeuS; PacketLength = len1; SendTime=tick1; MasterRecved=0; // LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5); //ToggleErrLed(); // ToggleOut8(); } Clk3=SysTick->VAL; // LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_4); // HAL_Delay(1); return 0; } int SlaveFunc() { int ThisuS=GetuS(); int thisRecvTime=RecvTimeuS; if (SlaveRecved) { KMem.RunStat=100; SlaveRecved=0; }else if ((ThisuS - thisRecvTime) >12000u) { KMem.ErrStat=200; KMem.SDD[17]=1; KMem.SDD[18]=ThisuS; KMem.SDD[19]=RecvTimeuS; }else if ( ThisuS > (thisRecvTime + 12000u)) { KMem.ErrStat=200; KMem.SDD[17]=2; KMem.SDD[18]=ThisuS; KMem.SDD[19]=RecvTimeuS; } return 0; } /* USER CODE END 0 */ /** * @brief The application entry point. * * @retval None */ int main(void) { /* USER CODE BEGIN 1 */ InitUartstat(&Uart1Stat,Uart1TxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf)); InitUartstat(&Uart2Stat,Uart2TxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf)); /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ for (int i=0;i<16;i++) { // memset(ChnStats[i],0,0); ChnStats[i].SendPackets=0; ChnStats[i].RecvPackets=0; ChnStats[i].LostPackets=0; ChnStats[i].CtnLstPkts=0; ChnStats[i].MaxCtnLstPkts=0; ChnStats[i].NotPkgErr=0; ChnStats[i].PkgLenErr=0; ChnStats[i].TimeOutErr=0; ChnStats[i].BCCErr=0; ChnStats[i].Delay=0; ChnStats[i].MaxDelay=0; } KMem.LastScanTime=0; KMem.ScanTimeuS=0; KMem.MinScanTimeuS=99999; KMem.MaxScanTimeuS=0; // KMem.SDD[14]=(unsigned int)&KMStoreSysCfg; // KMem.SDD[15]=(unsigned int)&KMStoreSysCfg1; KMem.SDD[12]=((uint32_t *)UID_BASE)[0]; // KMem.SDD[13]=((uint32_t *)UID_BASE)[1]; // KMem.SDD[14]=((uint32_t *)UID_BASE)[2]; KMem.SDD[13]=PendSvCount; KMem.SDD[14]=RCC->CSR; // KMem.SDD[15]=*(uint32_t *)FLASHSIZE_BASE; // KMem.SDD[16]=(unsigned int)&KMSysCfg; /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ TickFreq=10000; //TickƵÂÊ InituS(TickFreq); HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq); //ÖØж¨ÒåSysTickµÄƵÂÊÎ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); KMachineInit(); ReadSysCfgFromFlash(&KMSysCfg); KMem.EffJumperSW=ReadJumperSW(); nAddr=KMem.EffJumperSW&0x7; if (KMem.EffJumperSW == 0x0f) {repeater=1;bMaster=1;bSlave=0;} else if ((KMem.EffJumperSW&0x08)!=0) {bMaster=1;bSlave=0;} else{bMaster=0;bSlave=1;} nChilds=nAddr; nCurPollId=1; if (KMem.EffJumperSW == 0x00) Uart1Baud = DefaultUart1Baud; MX_USART1_UART_Init(); MX_USART2_UART_Init(); MX_SPI1_Init(); MX_ADC_Init(); MX_IWDG_Init(); /* USER CODE BEGIN 2 */ LL_USART_EnableIT_RXNE(USART1); LL_USART_EnableIT_IDLE(USART1); LL_USART_EnableIT_TC(USART1); LL_USART_EnableIT_RXNE(USART2); LL_USART_EnableIT_IDLE(USART2); LL_USART_EnableIT_TC(USART2); // if (bSlave) { // LL_USART_EnableAutoBaudRate(USART1); // LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE); // LL_USART_EnableAutoBaudRate(USART2); // LL_USART_SetAutoBaudRateMode(USART2, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE); } //LL_USART_EnableIT_TXE(USART1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ HAL_Delay(10); SetRunLed(1); //Turn On Run Led SetErrLed(0); //Turn Off Err Led PutOutput (0); //Clear all Output Enable595(1); //Enable 595 Output displayInput(0xffff); // EnableDisIn(1); //Input Diaplay Enable 595 SetOutStat(0); //OK Good, signal // ShowInitInfo(); KMem.LastScanTime = GetuS(); while (1) { //int MyKeyStat1,MyKeyStat2; //MyKeyStat1=GetInput(); //*((unsigned int *)&(PLCMem.SDT[10]))=nRunCount; // KMem.nRunCount=nRunCount; SlowFlicker=0; FastFlicker=1; us1=GetuS(); int haltick=HAL_GetTick(); 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(); if (GetBoardType() == 7 || GetBoardType() ==8 || GetBoardType() == 9 || GetBoardType() ==10 ) { displayInput(KMem.WX[0]); } us2=GetuS(); // ProcessPLCPROG(prog1, nSizeProg1); // ScanTimeuS=us2-LastScanTime; // LastScanTime = us2; if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;} if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;} if (repeater) { RepeaterFunc(); } us3=GetuS(); if ((KMem.nRunCount &0x1f) == 0x02) { ADCProcess(); if (PowerDownEvent) { if (!OldPowerDownEvent) { OldPowerDownEvent = PowerDownEvent; OldPowerDownEventTime = nCurTick; PowerDownProcess(); } }else { if (OldPowerDownEvent) { OldPowerDownEvent=PowerDownEvent; PowerRecoverProcess(); } } } if (bMaster) { BufferOut[1]=KMem.WX[0]&0xff; BufferOut[2]=(KMem.WX[0]>>8)&0xff; MasterFunc(); KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8); if (haltick&0x00002000) SlowFlicker=1; else SlowFlicker=0; if (haltick&0x00000800) FastFlicker=1; else FastFlicker=0; } if (bSlave) { BufferOut[0]=KMem.WX[0]; SlaveFunc(); if (! KMem.RunStat) {BufferIn[0]=0;} KMem.WY[0]=BufferIn[0]; if (nSlaveTick&0x00002000) SlowFlicker=1; else SlowFlicker=0; if (nSlaveTick&0x00000800) FastFlicker=1; else FastFlicker=0; } // KMem.WY[0]=nCount2>>5; if (KMem.RunStat) {KMem.RunStat--;} if (KMem.ErrStat) {KMem.ErrStat--;} if (!KMem.RunStat) SetRunLed(SlowFlicker); else SetRunLed(FastFlicker); if (!KMem.ErrStat) { SetErrLed(0); SetOutStat(1); } else { SetErrLed(FastFlicker); SetOutStat(0); } // SetRunLed(RunStat); // SetErrLed(ErrStat); us4=GetuS(); // EffJumperSW = GetInput(20)&0xff; // KMem.WY[0]=KMem.WY[0]; PutOutput (KMem.WY[0]); //PutOutput(0x0f70); us5=GetuS(); // if (bMaster) ShowInfo(); // if (bSlave) ShowInfo(); us6=GetuS(); add1(10,10); for (int i=0;i<64;i++) { // ProcessTimer(i); } KMem.nRunCount++; int nSize=sizeof(stChnStat); memcpy(&KMem.SDT[64],&ChnStats[1],nSize); memcpy(&KMem.SDT[64+nSize/2],&ChnStats[2],nSize); // for (int i=0;i<128;i++) { SDT[i]=i; } // SDT[48]=55; if (Uart1RecvBuf1DataLen >0 && Uart1Stat.bPacketRecved) { KLParsePacket(Uart1RecvBuf1,Uart1RecvBuf1DataLen); Uart1RecvBuf1DataLen=0; Uart1Stat.bPacketRecved=0; } if (bSlave) HAL_Delay(0); /* if (!IsEmpty(&Uart1Stat.QRx)) { unsigned char k=PopOne(&Uart1Stat.QRx); if (k=='L') { clearscreen(); } } */ LL_IWDG_ReloadCounter(IWDG); } //while (1) ; /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ /* USER CODE END 3 */ } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @param file: The file name as string. * @param line: The line in file as a number. * @retval None */ void _Error_Handler(char *file, int line) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ while(1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/