123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- /****************************************************************************
- *
- * Copy right: 2020-, Copyrigths of QIXIANG TECH Ltd.
- * File name: app.c
- * Description: QX app source file
- * History: Rev1.0 2020-10-16
- * Athuor: chenjie
- *
- ****************************************************************************/
- //include
- #include "bsp.h"
- #include "bsp_custom.h"
- #include "osasys.h"
- #include "ostask.h"
- #include "queue.h"
- #include "ps_event_callback.h"
- #include "app.h"
- #include "cmisim.h"
- #include "cmimm.h"
- #include "cmips.h"
- #include "sockets.h"
- #include "psifevent.h"
- #include "ps_lib_api.h"
- #include "lwip/netdb.h"
- #include <cis_def.h>
- #include "debug_log.h"
- #include "slpman_ec616.h"
- #include "plat_config.h"
- //define
- // app task static stack and control block
- #define PROC_TASK_STACK_SIZE (1024)
- //uart def
- #define Uart_Send_LEN (8)
- #define Uart_Rece_LEN (16)
- #define RTE_UART_RX_IO_MODE RTE_UART1_RX_IO_MODE
- //statement variable
- extern ARM_DRIVER_USART Driver_USART1;
- static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
- /** \brief usart receive buffer */
- uint8_t Uart_Data_buffer[8];
- /** \brief usart send buffer */
- /** \brief receive timeout flag */
- volatile bool isRecvTimeout = false;
- /** \brief receive complete flag */
- volatile bool isRecvComplete = false;
- uint8_t process0SlpHandler = 0xff;
- uint8_t process1SlpHandler = 0xff;
- uint8_t process2SlpHandler = 0xff;
- uint8_t deepslpTimerID = 7;
- /** \brief 电压传输 */
- uint8_t Uart_Rece_BattCellU1_U4[8];
- uint8_t Uart_Rece_BattCellU5_U8[8];
- uint8_t Uart_Rece_BattCellU9_U12[8];
- uint8_t Uart_Rece_BattCellU13_U14[8];
- uint8_t Uart_Rece_BattT[8];
- int16_t Uart_Rece_BattI=0x0000;
- uint8_t Uart_Rece_Batt_states[6];
- uint16_t Uart_Rece_BattU;
- uint16_t Uart_Rece_Batt_MaxcellU;
- uint16_t Uart_Rece_Batt_MincellU;
- uint8_t Can_Rece_buffer[8];
- typedef enum
- {
- PROCESS_STATE_IDLE = 0,
- PROCESS_STATE_WORK,
- PROCESS_STATE_SLEEP
- }processSM;
- typedef enum
- {
- PROCESS_Uart_STATE_IDLE = 0,
- PROCESS_Uart_STATE_WORK,
- PROCESS_Uart_STATE_CHECK,
- PROCESS_Uart_STATE_SLEEP
- }process_Uart;
- static StaticTask_t gProcessTask0;
- static UINT8 gProcessTaskStack0[PROC_TASK_STACK_SIZE];
- static StaticTask_t gProcessTask1;
- static UINT8 gProcessTaskStack1[PROC_TASK_STACK_SIZE];
- static StaticTask_t gProcessTask2;
- static UINT8 gProcessTaskStack2[PROC_TASK_STACK_SIZE];
- static StaticTask_t gProcessTask3;
- static UINT8 gProcessTaskStack3[PROC_TASK_STACK_SIZE];
- processSM gProc0State = PROCESS_STATE_IDLE;
- process_Uart gProc1State = PROCESS_Uart_STATE_IDLE;
- processSM gProc2State = PROCESS_STATE_IDLE;
- processSM gProc3State = PROCESS_STATE_IDLE;
- #define PROC_Task_STATE_SWITCH(a) (gProc0State = a) //任务调度切换
- #define PROC_Uart_STATE_SWITCH(a) (gProc1State = a) //uart 状态切换
- #define PROC_Can_STATE_SWITCH(a) (gProc2State = a) //can 状态切换
- #define PROC_NB_STATE_SWITCH(a) (gProc3State = a) //can 状态切换
- unsigned int crc_chk(uint8_t* data, uint8_t length)
- {
- int j;
- uint16_t reg_crc=0xFFFF;
- while(length--)
- {
- reg_crc ^= *data++;
- for(j=0;j<8;j++)
- {
- if(reg_crc & 0x01)
- {
- reg_crc=(reg_crc>>1) ^ 0xA001;
- }
- else
- {
- reg_crc=reg_crc >>1;
- }
- }
- }
- return reg_crc;
- }
- uint8_t bcc_chk(uint8_t* data, uint8_t length)
- {
- uint8_t bcc_chk_return = 0x00;
- uint8_t count = 0;
- while (count<length)
- {
- bcc_chk_return^=data[count];
- count++;
- }
- return bcc_chk_return;
- }
- void TcpCallBack(void)
- {
- #ifdef USING_PRINTF
- printf("[%d]TcpCallBacl\n",__LINE__);
- #endif
- }
- static void process0AppTask(void* arg)
- {
- uint32_t inParam = 0xAABBCCDD;
- uint32_t cnt;
- UINT8 Can_index = 0;
- UINT8 Uart_index = 0;
- uint32_t sleep_index = 0;
- PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
- NetSocDisplay(0,LED_TURN_OFF);
- NetSocDisplay(1,LED_TURN_OFF);
- NetSocDisplay(2,LED_TURN_OFF);
- NetSocDisplay(3,LED_TURN_OFF);
- NetSocDisplay(4,LED_TURN_OFF);
- slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
- slpManApplyPlatVoteHandle("process0slp",&process0SlpHandler);
- slpManPlatVoteDisableSleep(process0SlpHandler, SLP_SLP2_STATE);
- slpManSlpState_t slpstate = slpManGetLastSlpState();
- if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE))
- {
- PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
- }
- else
- {
- PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
- }
-
- while(1)
- {
- switch(gProc0State)
- {
- case PROCESS_STATE_IDLE:
- {
- PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_CHECK);
- NetSocDisplay(0,LED_TURN_ON);
- osDelay(100);//delay 100ms
- if(Uart_Rece_BattI!=0x0000)
- {
- PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
- }
- sleep_index++;
- if(sleep_index>=100)
- {
- NetSocDisplay(0,LED_TURN_OFF);
- sleep_index=0;
- PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
- }
- break;
- }
- case PROCESS_STATE_WORK:
- {
- osDelay(10);//10ms
- Can_index++;
- Uart_index++;
- if (Uart_index >10)//Uart 100ms 调用一次
- {
- PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_WORK);
- Uart_index = 0;
- }
- if (Can_index >=100)//Can 1000ms 调用一次
- {
- PROC_Can_STATE_SWITCH(PROCESS_STATE_WORK);
- PROC_NB_STATE_SWITCH(PROCESS_STATE_WORK);
- Can_index = 0;
- }
- if((Uart_Rece_BattI==0x0000)&&(Can_Rece_buffer[0]==0xff))
- {
- sleep_index++;
- }
- else
- {
- sleep_index = 0;
- }
- if (sleep_index >=6000)
- {
- PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
- sleep_index = 0;
- }
- break;
-
- }
- case PROCESS_STATE_SLEEP:
- {
- slpManSlpState_t State;
- uint8_t cnt;
- PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_SLEEP);
- PROC_Can_STATE_SWITCH(PROCESS_STATE_SLEEP);
- slpManPlatVoteEnableSleep(process0SlpHandler,SLP_SLP2_STATE);
- slpManPlatVoteDisableSleep(process0SlpHandler, SLP_HIB_STATE);
- slpManDeepSlpTimerStart(deepslpTimerID, 300000); // create a 10s timer, DeepSleep Timer is always oneshoot
- while(1) // now app can enter hib, but ps and phy maybe not, so wait here
- {
- osDelay(3000);
- }
- }
- }
- }
- }
- static void process3AppTask(void* arg)
- {
- CHAR serverip[] = "47.97.127.222";
- UINT16 serverport = 8712;
- int TcpConnectID = -1;
- int TcpSendID = -1;
- OsaUtcTimeTValue timestracture;
- uint8_t TcpSendBuffer[92] = {0x23,0x23,0x02,0xFE,0x47,0x59,0x54,0x45,0x53,0x54,
- 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,
- 0x32,0x01,0x00,0x43,0x14,0x0B,0x07,0x0E,0x39,0x37,
- 0x80,0x14,0x0B,0x07,0x0E,0x39,0x37,0x1A,0x00,0x00,
- 0x00,0x00,0x00,0x02,0x15,0x02,0x15,0x03,0x43,0x64,
- 0x00,0x00,0x00,0x00,0x0E,0x0E,0x53,0x0E,0x53,0x0E,
- 0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,
- 0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,
- 0x53,0x0E,0x53,0x05,0x13,0x13,0x13,0x13,0x13,0x00,
- 0x00,0x00};
- PROC_NB_STATE_SWITCH(PROCESS_STATE_IDLE);
- while(1)
- {
- switch(gProc3State)
- {
- case PROCESS_STATE_IDLE:
- {
- break;
- }
- case PROCESS_STATE_WORK:
- {
- while(TcpConnectID<0)
- {
- TcpConnectID = tcpipConnectionCreate(1,PNULL,PNULL,serverip,serverport,TcpCallBack);
- }
- appGetSystemTimeUtcSync(×tracture);
- TcpSendBuffer[91] = bcc_chk(TcpSendBuffer,91);
- TcpSendBuffer[24] = timestracture.UTCtimer1>>16;
- TcpSendBuffer[24] = TcpSendBuffer[24]-0x07D0;
- TcpSendBuffer[25] = timestracture.UTCtimer1>>8;
- TcpSendBuffer[26] = timestracture.UTCtimer1;
- TcpSendBuffer[27] = timestracture.UTCtimer2>>24;
- TcpSendBuffer[28] = timestracture.UTCtimer2>>16;
- TcpSendBuffer[29] = timestracture.UTCtimer2>>8;
- TcpSendID = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,92,PNULL,PNULL,PNULL);
- #ifdef USING_PRINTF
- printf("[%d]TcpConnectID:%d \r\n",__LINE__,TcpConnectID);
- #endif
- #ifdef USING_PRINTF
- printf("[%d]TcpSendID:%d \r\n",__LINE__,TcpSendID);
- #endif
- #ifdef USING_PRINTF
- printf("[%d]Time1:%x TIme2:%x \r\n",__LINE__,timestracture.UTCtimer1,timestracture.UTCtimer2);
- #endif
- for (int i = 0; i < 92; i++)
- {
- #ifdef USING_PRINTF
- printf("%x ",TcpSendBuffer[i]);
- #endif
- }
- #ifdef USING_PRINTF
- printf("\r\n ");
- #endif
- PROC_NB_STATE_SWITCH(PROCESS_STATE_IDLE);
- break;
- }
- case PROCESS_STATE_SLEEP:
- {
- while(1)
- {
- osDelay(3000);
- }
- break;
- }
- }
- }
- }
- /**
- \fn process0Init(void)
- \brief process0Init function.
- \return
- */
- void process0Init(void)
- {
- osThreadAttr_t task_attr;
- #ifndef USING_PRINTF
- if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
- {
- HAL_UART_RecvFlowControl(false);
- }
- #endif
- memset(&task_attr,0,sizeof(task_attr));
- memset(gProcessTaskStack0, 0xA5,PROC_TASK_STACK_SIZE);
- task_attr.name = "Process0AppTask";
- task_attr.stack_mem = gProcessTaskStack0;
- task_attr.stack_size = PROC_TASK_STACK_SIZE;
- task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
- task_attr.cb_mem = &gProcessTask0;//task control block
- task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
- osThreadNew(process0AppTask, NULL, &task_attr);
- }
- void process3Init(void)
- {
- osThreadAttr_t task_attr;
- memset(&task_attr,0,sizeof(task_attr));
- memset(gProcessTaskStack3, 0xA5,PROC_TASK_STACK_SIZE);
- task_attr.name = "Process3AppTask";
- task_attr.stack_mem = gProcessTaskStack3;
- task_attr.stack_size = PROC_TASK_STACK_SIZE;
- task_attr.priority = osPriorityNormal;//osPriorityBelowNormal;
- task_attr.cb_mem = &gProcessTask3;//task control block
- task_attr.cb_size = sizeof(StaticTask_t);//size of task control block
- osThreadNew(process3AppTask, NULL, &task_attr);
- }
- /**
- \fn appInit(void)
- \brief appInit function.
- \return
- */
- void appInit(void *arg)
- {
- process0Init();//任务调度和检测程序
- process3Init();//NB程序
- }
- /**
- \fn int main_entry(void)
- \brief main entry function.
- \return
- */
- void main_entry(void) {
- BSP_CommonInit();
- osKernelInitialize();
- registerAppEntry(appInit, NULL);
- if (osKernelGetState() == osKernelReady)
- {
- osKernelStart();
- }
- while(1);
- }
|