/** ****************************************************************************** * @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 "debug.h" #include "Functions.h" #include "KMachine.h" #include "PLCfunctions.h" //#include "KBus.h" #include "KLink.h" #include "string.h" #include "BSP.h" #include "ModbusRTU.h" #if (BOARD_TYPE == 13) #include "w5500_port.h" #include "../src/Ethernet/socket.h" #include "../src/Ethernet/loopback.h" #elif (BOARD_TYPE == 14) #include "FP0.h" #elif (BOARD_TYPE == 15) #include "KWireless.h" //#include "user.h" //#include "../src/radio/inc/sx126x-board.h" #endif /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ #define ADCrefAddr 0x1FFFF7BA /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ #define RX2BUFSIZE 128 #define TX2BUFSIZE 128 unsigned char Uart1RxBuf[256]; unsigned char Uart1TxBuf[280]; unsigned char Uart2RxBuf[RX2BUFSIZE]; unsigned char Uart2TxBuf[TX2BUFSIZE]; unsigned char SlowFlicker=0; unsigned char FastFlicker=0; stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE; uint32_t us1,us2,us3,us4,us5,us6; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ /* 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 */ /* USER CODE BEGIN 0 */ 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; } /* 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<9;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(&storedKMSysCfg); KMem.EffJumperSW=ReadJumperSW(); #if (BOARD_TYPE == 14) KMem.EffJumperSW|=0x10; nAddr=KMem.EffJumperSW&0x0f; if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;} else{bKBusMaster=0;bKBusSlave=1;} nChilds=nAddr; FP0_Init(); #elif (BOARD_TYPE == 15) nAddr=KMem.EffJumperSW&0x0f; if (KMem.EffJumperSW == 0x1f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;} else if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;} else{bKBusMaster=0;bKBusSlave=1;} #else nAddr=KMem.EffJumperSW&0x7; if (KMem.EffJumperSW == 0x0f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;} else if ((KMem.EffJumperSW&0x08)!=0) {bKBusMaster=1;bKBusSlave=0;} else{bKBusMaster=0;bKBusSlave=1;} #endif nChilds=nAddr; nCurPollId=1; //if (KMem.EffJumperSW == 0x00) Uart1Baud = DefaultUart1Baud; MX_USART1_UART_Init(); MX_USART2_UART_Init(); MX_SPI1_Init(); LL_SPI_EnableIT_RXNE(SPI1); #if (BOARD_TYPE == 14) // MX_SPI2_Init(); // MX_ADC_Init(); #else MX_SPI2_Init(); MX_ADC_Init(); #endif MX_IWDG_Init(); MX_TIM6_Init(); LL_TIM_EnableCounter(TIM6); /* USER CODE BEGIN 2 */ LL_USART_EnableIT_RXNE(USART1); LL_USART_EnableIT_IDLE(USART1); LL_USART_EnableIT_TC(USART1); // LL_USART_EnableIT_RXNE(USART2); Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1)); LL_USART_EnableIT_IDLE(USART2); LL_USART_EnableIT_TC(USART2); #if (BOARD_TYPE == 13) int res; res = w5500_init(); KMem.SDD[28]=res; // res=socket(0,Sn_MR_TCP,5000,0); KMem.SDD[29]=res; // res = listen(0); #endif // if (bKBusSlave) { // 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 #if (BOARD_TYPE == 14) // PutOutput (0); //Clear all Output // Enable595(1); //Enable 595 Output #else PutOutput (0); //Clear all Output Enable595(1); //Enable 595 Output #endif if (GetBoardType() == 7 || GetBoardType() ==8 || GetBoardType() == 9 || GetBoardType() ==10 ||GetBoardType() ==13 ||GetBoardType() ==15 ) { displayInput(0xffff); // EnableDisIn(1); //Input Diaplay Enable 595 } SetOutStat(0); //OK Good, signal ShowInitInfo(); KMem.LastScanTime = GetuS(); KMRunStat.WorkMode=0; if (KMRunStat.WorkMode == 1){ InitPLC(); StartPLC(); } KMem.WX[7]=0x5a; #if (BOARD_TYPE == 15) KWireLessInit(KMem.EffJumperSW&0x20); KWireLessStart(); #endif 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.SDD[13]=PendSvCount; // KMem.SDD[14]=RCC->CSR; #if (BOARD_TYPE == 14) // KMem.WX[0]= GetInput(); FP0_Proc(); #else KMem.WX[0]= GetInput(); #endif if (GetBoardType() == 7 || GetBoardType() ==8 || GetBoardType() == 9 || GetBoardType() ==10 || GetBoardType() ==15) { displayInput(KMem.WX[0]); } us2=GetuS(); ///* if ((KMem.nRunCount &0x1f) == 0x02) { ADCProcess(); if (PowerDownEvent) { if (!OldPowerDownEvent) { OldPowerDownEvent = PowerDownEvent; OldPowerDownEventTime = nCurTick; PowerDownProcess(); } }else { if (OldPowerDownEvent) { OldPowerDownEvent=PowerDownEvent; PowerRecoverProcess(); } } } //*/ #if (BOARD_TYPE == 15) Radio.IrqProcess( ); // Process Radio IRQ #endif // pProgs = (stBinProg1 *) STORE_PRG_BASE; if ( KMRunStat.WorkMode==1 && bKBusMaster) { 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 (bKBusRepeater) { KBusRepeaterFunc(); } us3=GetuS(); if (bKBusMaster) { #if (BOARD_TYPE == 14) for (int i=0;i>8)&0xff; #endif if (nChilds>0) { KBusMasterFunc(2); } #if (BOARD_TYPE == 14) // KMem.WX[0]=BufferIn[1]+(BufferIn[2]<<8); #else // KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8); #endif } if (haltick&0x00002000) SlowFlicker=1; else SlowFlicker=0; if (haltick&0x00000800) FastFlicker=1; else FastFlicker=0; if (bKBusSlave) { // BufferOut[0]=KMem.WX[0]; #if (BOARD_TYPE == 15) // KBusSlaveFunc(2); // if (! KMem.RunStat) {BufferIn[0]=0;} // KMem.WY[0]=BufferIn[0]; #else KBusSlaveFunc(2); if (! KMem.RunStat) {BufferIn[0]=0;} KMem.WY[0]=BufferIn[0]; #endif 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; #if (BOARD_TYPE == 14) // PutOutput (KMem.WY[0]); #else PutOutput (KMem.WY[0]); #endif //PutOutput (KMem.nRunCount>>8); //PutOutput(0x0f70); #if (BOARD_TYPE == 15) // KMem.WY[1]=KMem.nRunCount>>6; KMem.WY[1]=KMem.WX[0]; KMem.WY[0]=KMem.WX[1]; #endif us5=GetuS(); // if (bKBusMaster) ShowInfo(); // if (bKBusSlave) 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) { int res1 = -1; res1 = ModBusSlaveParsePkg(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen); if (res1 !=0) { KLParsePacket(1, Uart1RecvBuf1, Uart1RecvBuf1DataLen); } Uart1RecvBuf1DataLen=0; Uart1Stat.bPacketRecved=0; } if (bKBusSlave) HAL_Delay(0); /* if (!IsEmpty(&Uart1Stat.QRx)) { unsigned char k=PopOne(&Uart1Stat.QRx); if (k=='L') { clearscreen(); } } */ #if (BOARD_TYPE == 14) //process 6 output { // mapping bits. if (KMem.WXB[0]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_12);} else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_12);} if (KMem.WXB[1]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_13);} else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_13);} if (KMem.WXB[2]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_14);} else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_14);} if (KMem.WXB[3]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_15);} else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_15);} if (KMem.WXB[4]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_0);} else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_0);} if (KMem.WXB[5]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_1);} else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1);} if (KMem.WXB[5]&0x1){ LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_2);} else {LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_2);} } #endif /* { 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<