/** ****************************************************************************** * @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" #if (ENABLE_PLC) #include "PLCfunctions.h" #endif //#include "KBus.h" #include "KLink.h" #include "string.h" #include "BSP.h" #include "ModbusRTU.h" #if (ENABLE_NET) #include "w5500_port.h" #include "../src/Ethernet/socket.h" #include "../src/Ethernet/loopback.h" #endif #if (ENABLE_FPX) #include "FPx.h" #endif #if (ENABLE_RF) #include "KWireless.h" #endif //#include "user.h" //#include "../src/radio/inc/sx126x-board.h" /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ #define RX2BUFSIZE 64 #define TX2BUFSIZE 64 unsigned char Uart1RxBuf[128]; unsigned char Uart1TxBuf[260]; unsigned char Uart2RxBuf[RX2BUFSIZE]; unsigned char Uart2TxBuf[TX2BUFSIZE]; unsigned char SlowFlicker=0; unsigned char FastFlicker=0; unsigned int Uart1IdelTimer = 0; #if (ENABLE_PLC) stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE; #endif uint32_t us1,us2,us3,us4,us5,us6; stKBusDef KBus1; // /* 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++; KBus1.nSlaveTick++; Count++; if (Count>=10000) // 0.1mS, 10000次, 秒脉冲 { Count=0; KMem.CurTimeSec++; KMem.ThisRunTime++; KMem.TotalRunTime++; if (KMRunStat.bLEDFlick) KMRunStat.bLEDFlick--; if (KMRunStat.bLEDFlick >120) KMRunStat.bLEDFlick=120; } return; } void PendSvCallBack() { #if (ENABLE_FPX) ///* if (bSPI1RecvDone) { bSPI1RecvDone=0; FPxParsePkt(SPI1RecvBuf,nSPI1RecvLenInBuf); } //*/ #endif if (Uart2Stat.bPacketRecved) { KBusParsePacket(&KBus1, (pKBPacket)Uart2RecvBuf1, Uart2RecvBuf1DataLen); Uart2RecvBuf1DataLen=0; Uart2Stat.bPacketRecved=0; Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1)); KMem.WDT[2]++; } } /* KBus通讯回调函数,当通讯状态改变或数据更新时被调用。 或者系统请求时。 */ void * KBusEvCallBackFunc(void* pParam, int nEvent, void *pBuf, int nLen1) { switch (nEvent){ case KBusEvNone: break; case KBusEvCreate: break; case KBusEvConnected: break; case KBusEvDisConnected: break; case KBusEvClosed: break; case KBusEvStateChange: break; case KBusEvTimeSync: break; case KBusEvDataUpdate: KMem.WDT[10]++; if (KBus1.bMaster) { KMem.WY[0]=KBusMem.WLX[0]; //KBus Master KBusMem.WLY[0]=KMem.WX[0]; } else if (KBus1.bSlave) { KMem.WY[0]=KBusMem.WLY[0]; //KBus Slave KBusMem.WLX[0]=KMem.WX[0]; } break; case KBusEvCmdResponse: break; default: break; } return 0; } extern stDeviceInfo MyDeviceInfo; /* USER CODE END 0 */ /** * @brief The application entry point. * * @retval None */ int main(void) { /* USER CODE BEGIN 1 */ KMRunStat.bLEDFlick = 1; InitUartstat(&Uart1Stat,Uart1RxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf)); InitUartstat(&Uart2Stat,Uart2RxBuf,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 */ 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); KMRunStat.bLEDFlick = 1; int bKBusMaster,bKBusSlave,bKBusRepeater;; int nChilds; int nKBusStationID; KMem.CurJumperSW=ReadJumperSW(); KMem.EffJumperSW=KMem.CurJumperSW; // Uart2Baud = AlterUart2Baud; nChilds=KMem.EffJumperSW&0x0f; // Uart2Baud = AlterUart2Baud; nKBusStationID = KMem.EffJumperSW&0x0f; nChilds=nKBusStationID; #if (BOARD_TYPE == 14) KMem.EffJumperSW|=0x10; nStationID=KMem.EffJumperSW&0x0f; if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;} else{bKBusMaster=0;bKBusSlave=1;} FPxSetCallBackFunc(&FPxCallBackFunc); FPx_Init(nChilds); int IOByteCount = nChilds; FPx_SetIOCount(IOByteCount,IOByteCount); #elif (BOARD_TYPE == 15 || BOARD_TYPE == 16) nStationID=1 ;//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 nKBusStationID=nChilds; 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; } #endif UNUSED(bKBusRepeater); if (bKBusMaster) { KBusInitMaster(&KBus1, (KBusSendPktFuncDef)PutStr2, nChilds); } else if (bKBusSlave) { KBusInitSlave(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusStationID,&MyDeviceInfo); } KBusSetEvCallBackFunc(&KBus1, &KBusEvCallBackFunc); KMem.WDT[0]= nKBusStationID; KMem.WDT[1] = KBus1.nStationId; //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 || BOARD_TYPE == 16) { displayInput(0xffff); // EnableDisIn(1); //Input Diaplay Enable 595 } SetOutStat(0); //OK Good, signal ShowInitInfo(); KMem.LastScanTime = GetuS(); KMRunStat.WorkMode=0; KMRunStat.WorkMode2=0; KMRunStat.WorkMode = storedKMSysCfg.theKMSysCfg.workmode; #if (ENABLE_PLC) if (KMRunStat.WorkMode == 1){ InitPLC(); KMRunStat.WorkMode2 = KMem.CurJumperSW&0x20 ; if (KMRunStat.WorkMode2) { StartPLC(); } } #endif KMem.WX[7]=0x5a; #if (BOARD_TYPE == 15 || BOARD_TYPE == 16) KWireLessInit(KMem.EffJumperSW&0x20,KMem.EffJumperSW&0x0f); KWireLessStart(); #endif KMem.WY[0]=0; 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 thisJumperSW=ReadJumperSW(); #if (ENABLE_PLC) if (KMRunStat.WorkMode&1){ if (thisJumperSW&0x20 && !(KMem.CurJumperSW&0x20)) // Run 开关 正 跳变。 {StartPLC();} if (!(thisJumperSW&0x20) && (KMem.CurJumperSW&0x20)) // Run 开关 负 跳变。 {StopPLC();} } #endif KMem.CurJumperSW=thisJumperSW; KMem.haltick=haltick; // KMem.TotalRunTime=TotalRunTime; // KMem.ThisRunTime=ThisRunTime; // *((unsigned int *)&(PLCMem.SDT[2]))=nChilds; // KMem.SDD[13]=PendSvCount; // KMem.SDD[14]=RCC->CSR; int a; a = LL_GPIO_ReadInputPort(GPIOA); KMem.WDT[120]=a; a = LL_GPIO_ReadInputPort(GPIOB); KMem.WDT[121]=a; a = LL_GPIO_ReadInputPort(GPIOC); KMem.WDT[122]=a; a = LL_GPIO_ReadInputPort(GPIOD); KMem.WDT[123]=a; #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 || GetBoardType() ==16) { displayInput(KMem.WX[0]); } us2=GetuS(); if (PowerDownEvent) { KMem.WX[0]=0;} ///* if ((KMem.nRunCount &0x1f) == 0x02) { ADCProcess(); if (PowerDownEvent) { KMem.WX[0]=0; if (!OldPowerDownEvent) { OldPowerDownEvent = PowerDownEvent; OldPowerDownEventTime = nCurTick; PowerDownProcess(); } }else { if (OldPowerDownEvent) { OldPowerDownEvent=PowerDownEvent; PowerRecoverProcess(); } } } //*/ #if (BOARD_TYPE == 15 || BOARD_TYPE == 16) Radio.IrqProcess( ); // Process Radio IRQ KWL_Process(1); #endif // pProgs = (stBinProg1 *) STORE_PRG_BASE; #if (ENABLE_PLC) if ( KMRunStat.WorkMode==1 ) //&& bKBusMaster) { if (KMRunStat.nBinProgBank == 0){ pProgs=(stBinProg1 *)STORE_PRG_BASE; }else { pProgs=(stBinProg1 *)ALT_PRG_BASE; } nSizeProg1=KMRunStat.nBinProgSize; // pProgs=(stBinProg1 *)prog1; ProcessPLCBinProg(pProgs, nSizeProg1); } #endif 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); //PutOutput(0x0f70); // 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(stKBusChnStat); // memcpy(&KMem.SDT[64],&KBusChnStats[1],nSize); // memcpy(&KMem.SDT[64+nSize/2],&KBusChnStats[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; Uart1IdelTimer = 0; }else { if (Uart1IdelTimer>600000) { // 超过60秒没有数据传输,重新进入自适应波特率状态 LL_USART_EnableAutoBaudRate(USART1); LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE); }else { Uart1IdelTimer++; } } if (bKBusSlave) HAL_Delay(0); /* if (!IsEmpty(&Uart1Stat.QRx)) { unsigned char k=PopOne(&Uart1Stat.QRx); if (k=='L') { clearscreen(); } } */ #if (BOARD_TYPE == 14) const unsigned int pins[6]= { LL_GPIO_PIN_10,LL_GPIO_PIN_11,LL_GPIO_PIN_12,LL_GPIO_PIN_13,LL_GPIO_PIN_14,LL_GPIO_PIN_15}; //process 6 output { // mapping bits. for (int i=0;i<6;i++) { USHORT bitaddr = storedKMSysCfg.theKMSysCfg.OutMappings[i]; UCHAR type = (bitaddr&0xf000) >>12; USHORT byteaddr = (bitaddr&0x0ff0) >>4; UCHAR bitpos = bitaddr &0x0f; UCHAR bitvalue = 0 ; if (byteaddr>0) { if (type == 0) bitvalue = KMem.WXB[byteaddr-1] & ( 1 << bitpos ); else if (type == 1 ) bitvalue = KMem.WYB[byteaddr-1] & ( 1 << bitpos ); } if (bitvalue){ LL_GPIO_SetOutputPin(GPIOB,pins[i]);} else {LL_GPIO_ResetOutputPin(GPIOB,pins[i]);} } } #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<