#include "delay.h" #include "usart.h" #include "led.h" #include "exti.h" #include "rtc.h" #include "LAN8720.h" #include "usart2a3.h" #include "timer.h" #include "sram.h" #include "malloc.h" #include "lwip_comm.h" #include "lwipopts.h" #include "tcp_server_demo.h" #include "w25qxx.h" #include "sdio_sdcard.h" #include "ff.h" #include "exfuns.h" #include "fontupd.h" #include "usbh_usr.h" #include "udisk_interface.h" #include "gz-dc_interface.h" #include "M104_interface.h" #include "usr_infor.h" #include "base64.h" #include "vars.h" #include "relay.h" #include "app_thread.h" #include "protohandle.h" #include "LAN8720.h" #define USE_MALLOC_STK 0 /*******************************Heap********************************/ static OS_STK TCPSERVER_TASK_STK[TCPSERVER_STK_SIZE]; static OS_STK DATA_TASK_STK[DATA_STK_SIZE]; /******************************************************************/ //U盘检测 extern OS_EVENT *outputLogFileSem;//导出日志信号量 main.c extern USBH_HOST USB_Host; extern USB_OTG_CORE_HANDLE USB_OTG_Core; void watch_task(void *pdata) { u8 t = 0; pdata = pdata; //初始化USB主机 USBH_Init(&USB_OTG_Core,USB_OTG_FS_CORE_ID,&USB_Host,&USBH_MSC_cb,&USR_Callbacks); while(1) { USBH_Process(&USB_OTG_Core, &USB_Host); OSTimeDlyHMSM(0,0,0,10); t++; // 1s if(t==100) { LED0 =!LED0; t=0; } } } extern unsigned char validtime[20]; extern unsigned int currentTime; // 获取时间任务 void get_time_task(void *pdata) { OS_CPU_SR cpu_sr; RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct; u8 t = 0; u8 tbuf[40]; RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct); sprintf((char*)validtime,"20%02d-%02d-%02d",RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date); RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct); sprintf((char*)tbuf,"%02d:%02d:%02d",RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds); //printf("Time:%s\r\n",tbuf); strcat(validtime," "); strcat(validtime,tbuf); printf("validtime:%s\r\n",validtime); while(1) { t++; // 1s 更新一次时间 if((t%10) == 0) { memset(tbuf,0,sizeof(tbuf)); //OSMutexPend(timeMutex,0,&err); OS_ENTER_CRITICAL(); //关中断 RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct); sprintf((char*)validtime,"20%02d-%02d-%02d",RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date); RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct); sprintf((char*)tbuf,"%02d:%02d:%02d",RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds); //printf("CurrentTime:%s\r\n",opettime); currentTime = RTC_TimeStruct.RTC_Hours*3600 + RTC_TimeStruct.RTC_Minutes*60 + RTC_TimeStruct.RTC_Seconds; strcat(validtime," "); strcat(validtime,tbuf); //printf("Date:%s\r\n",validtime); //OSMutexPost(timeMutex); OS_EXIT_CRITICAL(); //开中断 //sprintf((char*)tbuf,"Week:%d",RTC_DateStruct.RTC_WeekDay); //printf("%s\r\n",tbuf); //LED1 = !LED1; } OSTimeDlyHMSM(0,0,0,100); //延时100ms } } /*****受保护全局变量*********/ extern u8 M104_length; extern u8 RecvM104; extern u8 USART6_RX_BUF[USART2_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节. extern u8 LCD_length; extern u8 RecvLCD; extern u8 USART3_RX_BUF[USART3_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节. void decode_usart_task(void *pdata) { u8 i = 0; OS_CPU_SR cpu_sr; pdata = pdata ; printf("解析串口数据任务\r\n"); //M104DPC while(1) { OS_ENTER_CRITICAL(); //关中断 if(RecvM104) { if(M104_length >= 9) { #if DEBUG_MODE printf("Recv from M104: "); for(i=0; i < M104_length; i++) printf("0x%X ", USART6_RX_BUF[i]); printf("\r\n"); #endif //开始解析数据 if(USART6_RX_BUF[0] == 0x02 && USART6_RX_BUF[1] == 0x00 && USART6_RX_BUF[2] == 0x00 && USART6_RX_BUF[M104_length-1] == 0x03) { decoder_M104_available_data(USART6_RX_BUF,M104_length); } } else { //printf("Unavailable data from M104DPC && timeOut\r\n"); } RecvM104 = 0; // 清除接收标识 } if(RecvLCD) { if(LCD_length > 5) { #if 1 printf("Recv from LCD:"); for(i = 0; i < LCD_length; i++) printf("0x%X ",USART3_RX_BUF[i]); printf("\r\n"); #endif if(USART3_RX_BUF[0] == 0xEE && USART3_RX_BUF[LCD_length-4] == 0xFF && USART3_RX_BUF[LCD_length-3] == 0xFC && USART3_RX_BUF[LCD_length-2] == 0xFF && USART3_RX_BUF[LCD_length-1] == 0xFF) { //解包 decode_lcd_available(USART3_RX_BUF,LCD_length); } } else { //printf("Unavailable data from LCD && timeOut\r\n"); } RecvLCD = 0; } OS_EXIT_CRITICAL(); //开中断 OSTimeDlyHMSM(0,0,0,10); //延时10ms } } /* 处理数据包任务 */ #if _USE_MBOX extern DataPack pack; extern OS_EVENT *mbox; #endif #if _USE_QUE extern OS_EVENT *que; extern void *msg[QSIZE]; #endif void process_data_task(void *pdata) { INT8U err; DataPack *m_pack; printf("开始数据处理线程\r\n"); while(1) { #if _USE_MBOX //printf("Start Process\r\n"); m_pack = (DataPack *) OSMboxPend(mbox,0,&err); if(err == OS_ERR_NONE) ProtolResponse1(m_pack); else if(err == OS_ERR_EVENT_TYPE) printf("OS_ERR_EVENT_TYPE\r\n"); else if(err == OS_ERR_PEVENT_NULL) printf("OS_ERR_PEVENT_NULL\r\n"); else if(err == OS_ERR_PEND_ISR) printf("OS_ERR_PEND_ISR\r\n"); else if(err == OS_ERR_PEND_LOCKED) printf("OS_ERR_PEND_LOCKED\r\n"); //OSTimeDlyHMSM(0,0,0,150); #endif #if _USE_QUE m_pack = (DataPack *)OSQPend(que,0,&err); if(err == OS_ERR_NONE) { ProtolResponse1(m_pack); } else if(err == OS_ERR_PEVENT_NULL) printf("OS_ERR_PEVENT_NULL\r\n"); else if(err == OS_ERR_EVENT_TYPE) printf("OS_ERR_EVENT_TYPE\r\n"); else if(err == OS_ERR_PEND_ISR) printf("OS_ERR_PEND_ISR\r\n"); // NONE USE else if(err == OS_ERR_PEND_LOCKED) printf("OS_ERR_PEND_LOCKED\r\n"); LED1 = !LED1; OSTimeDlyHMSM(0,0,0,150); #endif } } /* 发送寻卡指令任务 */ void search_card_task(void *pdata) { while(1) { search_card(); OSTimeDlyHMSM(0,0,0,250); //延时 read_card_id(); OSTimeDlyHMSM(0,0,0,250); //延时 } } /* 网络状态监测 */ extern u8 netStatus; void net_status_task(void *pdata) { u8 first = 0; static u8 sta = -1; static u8 createThreadFlag = 0; OS_CPU_SR cpu_sr; pdata = pdata ; while(1)//监测状态 { if(sta != 0 && first == 0) { sta = lwip_comm_init(); if(sta == 1) printf("Malloc ETH\r\n"); else if(sta == 2) { // 释放缓冲区 ETH_Mem_Free(); lwip_comm_mem_free(); printf("LAN8720 Init Failed 1\r\n"); } } if(sta != 0 && first == 1) { sta = lwip_comm_init2(); if(sta == 1) printf("Malloc MEM\r\n"); else if(sta == 2) { ETH_Mem_Free(); lwip_comm_mem_free2(); printf("LAN8720 Init Failed 2\r\n"); } } if( sta == 0 && createThreadFlag == 0 && first == 0) { printf("Create task\r\n"); // 创建任务 OS_ENTER_CRITICAL(); //关中断 OSTaskCreate(tcp_server_thread,(void*)0,(OS_STK*)&TCPSERVER_TASK_STK[TCPSERVER_STK_SIZE-1],TCPSERVER_PRIO); #if (_USE_MBOX || _USE_QUE) OSTaskCreate(process_data_task,(void *)0,(OS_STK*)&DATA_TASK_STK[DATA_STK_SIZE-1],DATA_PRIO); #endif netStatus = DEV_ONLINE;////////////////////////////////// OS_EXIT_CRITICAL(); //开中断 createThreadFlag = 1; first = 1; #if _RAEL_USE upload_all();// TCP 客户端 发送本地日志 #endif } if(sta == 0 && createThreadFlag == 0 && first==1) { printf("Resume task\r\n"); OSTaskResume(TCPSERVER_PRIO); #if (_USE_MBOX || _USE_QUE) OSTaskResume(DATA_PRIO); #endif OS_ENTER_CRITICAL(); //关中断 netStatus = DEV_ONLINE; OS_EXIT_CRITICAL(); //开中断 createThreadFlag = 1; #if _RAEL_USE upload_all();// TCP 客户端 发送本地日志 #endif } //#define PHY_Linked_Status ((uint16_t)0x0004) /*!< Valid link established */ //#define PHY_BCR 0 /*!< Transceiver Basic Control Register */ //#define PHY_BSR 1 /*!< Transceiver Basic Status Register */ //#define LAN8720_PHY_ADDRESS 0x00 //LAN8720 PHY芯片地址. // ETH_ReadPHYRegister(LAN8720_PHY_ADDRESS, PHY_BSR) & 0x0004 = 0100 if(createThreadFlag == 1 && !(ETH_ReadPHYRegister(LAN8720_PHY_ADDRESS, PHY_BSR) & PHY_Linked_Status) ) //&& { printf("suspend server and dataProcess\r\n"); OSTaskSuspend(TCPIP_THREAD_PRIO); OSTaskSuspend(TCPSERVER_PRIO); #if (_USE_MBOX || _USE_QUE) OSTaskSuspend(DATA_PRIO); #endif createThreadFlag = 0; OS_ENTER_CRITICAL(); //关中断 netStatus = DEV_OFFLINE;////////////////////////// OS_EXIT_CRITICAL(); //开中断 // 释放缓冲区 ETH_Mem_Free(); lwip_comm_mem_free2();// // 重置状态标识 sta = -1; createThreadFlag = 0; } } OSTimeDlyHMSM(0,0,0,500); //延时 } /******************************************************************************************************* #if 0 const char *test[] = {"", "f", "fo", "foo", "foob", "fooba", "foobar"}; size_t n,i; char *p ; char *q; printf("Start search Card\r\n"); for (i = 0; i < sizeof (test) / sizeof (*test); ++i) { p = encode_base64(test[i], strlen(test[i])); q = decode_base64(p, &n); printf("BASE64(\"%s\") = \"%s\" = (%zd)\"%.*s\"\r\n", test[i], p, n, n, q); myfree(SRAMIN,p); myfree(SRAMIN,q); } #endif #if 0 char *p = NULL; printf("malloc test\r\n"); p = mymalloc(SRAMIN,1024*3); if(p == NULL) printf("malloc 3K failed\r\n"); else { printf("malloc sucess\r\n"); myfree(SRAMIN,p); } #endif **********************************************************************************************************/