void sim300Task(void *p_arg){ uint8_t simdata[20]; uint32_t simticks; uint8_t len; (void)p_arg; sim300Init(); while(1){ sim300Receive(); simrcv_t.idle_ticks++; if(TRUE ==sim300RcvProc()){ sim300StateMachine(&sim_state_machine); } OSTimeDly(OS_TICKS_PER_SEC>>4); if(SIM_S_CIPSTART_OK == sim_state_machine){ uartWrite(SIM300_PORT,CIPSEND20,strlen(CIPSEND20)); uartWrite(SIM300_PORT,testudp,22); OSTimeDly(OS_TICKS_PER_SEC*2); uartWrite(SIM300_PORT,CIPSEND20,strlen(CIPSEND20)); uartWrite(SIM300_PORT,testudp,22); OSTimeDly(OS_TICKS_PER_SEC*2); sim_state_machine = SIM_S_CIPSEND; } /* if(0 != uartRead(0,simdata,1)){ if(('r' == simdata[0])&&(SIM_S_PWRDOWN == sim_state_machine)){ SIM300_PWR_ON; SIM300_PWRKEY_HIGH; OSTimeDly(OS_TICKS_PER_SEC); SIM300_PWRKEY_LOW; OSTimeDly(OS_TICKS_PER_SEC*3); SIM300_PWRKEY_HIGH; sim_state_machine = SIM_S_IDLE; } } */ if(SIM_S_CIPSEND_OK == sim_state_machine){ simticks = OSTimeGet(); sim_state_machine = SIM_S_WAITDATA; } if(SIM_S_WAITDATA == sim_state_machine){ if(OSTimeGet() - simticks > OS_TICKS_PER_SEC*30){ /* 关闭连接 */ SIM300_PWRKEY_LOW; OSTimeDly(OS_TICKS_PER_SEC); SIM300_PWRKEY_HIGH; OSTimeDly(OS_TICKS_PER_SEC*8); sim_state_machine= SIM_S_PWRDOWN; } } } }
void OS_StatTask (void *p_arg) { #if OS_CFG_DBG_EN > 0u #if OS_CFG_TASK_PROFILE_EN > 0u OS_CPU_USAGE usage; OS_CYCLES cycles_total; OS_CYCLES cycles_div; OS_CYCLES cycles_mult; OS_CYCLES cycles_max; #endif OS_TCB *p_tcb; #endif OS_TICK ctr_max; OS_TICK ctr_mult; OS_TICK ctr_div; OS_ERR err; OS_TICK dly; CPU_TS ts_start; CPU_TS ts_end; CPU_SR_ALLOC(); (void)&p_arg; /* Prevent compiler warning for not using 'p_arg' */ while (OSStatTaskRdy != DEF_TRUE) { OSTimeDly(2u * OSCfg_StatTaskRate_Hz, /* Wait until statistic task is ready */ OS_OPT_TIME_DLY, &err); } OSStatReset(&err); /* Reset statistics */ dly = (OS_TICK)0; /* Compute statistic task sleep delay */ if (OSCfg_TickRate_Hz > OSCfg_StatTaskRate_Hz) { dly = (OS_TICK)(OSCfg_TickRate_Hz / OSCfg_StatTaskRate_Hz); } if (dly == (OS_TICK)0) { dly = (OS_TICK)(OSCfg_TickRate_Hz / (OS_RATE_HZ)10); } while (DEF_ON) { ts_start = OS_TS_GET(); #ifdef CPU_CFG_INT_DIS_MEAS_EN OSIntDisTimeMax = CPU_IntDisMeasMaxGet(); #endif CPU_CRITICAL_ENTER(); /* ----------------- OVERALL CPU USAGE ------------------ */ OSStatTaskCtrRun = OSStatTaskCtr; /* Obtain the of the stat counter for the past .1 second */ OSStatTaskCtr = (OS_TICK)0; /* Reset the stat counter for the next .1 second */ CPU_CRITICAL_EXIT(); if (OSStatTaskCtrMax > OSStatTaskCtrRun) { /* Compute CPU Usage with best resolution */ if (OSStatTaskCtrMax < 400000u) { /* 1 to 400,000 */ ctr_mult = 10000u; ctr_div = 1u; } else if (OSStatTaskCtrMax < 4000000u) { /* 400,000 to 4,000,000 */ ctr_mult = 1000u; ctr_div = 10u; } else if (OSStatTaskCtrMax < 40000000u) { /* 4,000,000 to 40,000,000 */ ctr_mult = 100u; ctr_div = 100u; } else if (OSStatTaskCtrMax < 400000000u) { /* 40,000,000 to 400,000,000 */ ctr_mult = 10u; ctr_div = 1000u; } else { /* 400,000,000 and up */ ctr_mult = 1u; ctr_div = 10000u; } ctr_max = OSStatTaskCtrMax / ctr_div; OSStatTaskCPUUsage = (OS_CPU_USAGE)((OS_TICK)10000u - ctr_mult * OSStatTaskCtrRun / ctr_max); if (OSStatTaskCPUUsageMax < OSStatTaskCPUUsage) { OSStatTaskCPUUsageMax = OSStatTaskCPUUsage; } } else { OSStatTaskCPUUsage = (OS_CPU_USAGE)10000u; } OSStatTaskHook(); /* Invoke user definable hook */ #if OS_CFG_DBG_EN > 0u #if OS_CFG_TASK_PROFILE_EN > 0u cycles_total = (OS_CYCLES)0; CPU_CRITICAL_ENTER(); p_tcb = OSTaskDbgListPtr; CPU_CRITICAL_EXIT(); while (p_tcb != (OS_TCB *)0) { /* ----------------- TOTAL CYCLES COUNT ----------------- */ OS_CRITICAL_ENTER(); p_tcb->CyclesTotalPrev = p_tcb->CyclesTotal; /* Save accumulated # cycles into a temp variable */ p_tcb->CyclesTotal = (OS_CYCLES)0; /* Reset total cycles for task for next run */ OS_CRITICAL_EXIT(); cycles_total += p_tcb->CyclesTotalPrev;/* Perform sum of all task # cycles */ CPU_CRITICAL_ENTER(); p_tcb = p_tcb->DbgNextPtr; CPU_CRITICAL_EXIT(); } #endif #if OS_CFG_TASK_PROFILE_EN > 0u /* ------------- INDIVIDUAL TASK CPU USAGE -------------- */ if (cycles_total > (OS_CYCLES)0u) { /* 'cycles_total' scaling ... */ if (cycles_total < 400000u) { /* 1 to 400,000 */ cycles_mult = 10000u; cycles_div = 1u; } else if (cycles_total < 4000000u) { /* 400,000 to 4,000,000 */ cycles_mult = 1000u; cycles_div = 10u; } else if (cycles_total < 40000000u) { /* 4,000,000 to 40,000,000 */ cycles_mult = 100u; cycles_div = 100u; } else if (cycles_total < 400000000u) { /* 40,000,000 to 400,000,000 */ cycles_mult = 10u; cycles_div = 1000u; } else { /* 400,000,000 and up */ cycles_mult = 1u; cycles_div = 10000u; } cycles_max = cycles_total / cycles_div; } else { cycles_mult = 0u; cycles_max = 1u; } #endif CPU_CRITICAL_ENTER(); p_tcb = OSTaskDbgListPtr; CPU_CRITICAL_EXIT(); while (p_tcb != (OS_TCB *)0) { #if OS_CFG_TASK_PROFILE_EN > 0u /* Compute execution time of each task */ usage = (OS_CPU_USAGE)(cycles_mult * p_tcb->CyclesTotalPrev / cycles_max); if (usage > 10000u) { usage = 10000u; } p_tcb->CPUUsage = usage; if (p_tcb->CPUUsageMax < usage) { /* Detect peak CPU usage */ p_tcb->CPUUsageMax = usage; } #endif #if OS_CFG_STAT_TASK_STK_CHK_EN > 0u OSTaskStkChk( p_tcb, /* Compute stack usage of active tasks only */ &p_tcb->StkFree, &p_tcb->StkUsed, &err); #endif CPU_CRITICAL_ENTER(); p_tcb = p_tcb->DbgNextPtr; CPU_CRITICAL_EXIT(); } #endif if (OSStatResetFlag == DEF_TRUE) { /* Check if need to reset statistics */ OSStatResetFlag = DEF_FALSE; OSStatReset(&err); } ts_end = OS_TS_GET() - ts_start; /* Measure execution time of statistic task */ if (OSStatTaskTimeMax < ts_end) { OSStatTaskTimeMax = ts_end; } OSTimeDly(dly, OS_OPT_TIME_DLY, &err); } }
void OSStatTaskCPUUsageInit (OS_ERR *p_err) { OS_ERR err; OS_TICK dly; CPU_SR_ALLOC(); #ifdef OS_SAFETY_CRITICAL if (p_err == (OS_ERR *)0) { OS_SAFETY_CRITICAL_EXCEPTION(); return; } #endif #if ((OS_CFG_TMR_EN > 0u) && (OS_CFG_TASK_SUSPEND_EN > 0u)) OSTaskSuspend(&OSTmrTaskTCB, &err); if (err != OS_ERR_NONE) { *p_err = err; return; } #endif OSTimeDly((OS_TICK )2, /* Synchronize with clock tick */ (OS_OPT )OS_OPT_TIME_DLY, (OS_ERR *)&err); if (err != OS_ERR_NONE) { *p_err = err; return; } CPU_CRITICAL_ENTER(); OSStatTaskCtr = (OS_TICK)0; /* Clear idle counter */ CPU_CRITICAL_EXIT(); dly = (OS_TICK)0; if (OSCfg_TickRate_Hz > OSCfg_StatTaskRate_Hz) { dly = (OS_TICK)(OSCfg_TickRate_Hz / OSCfg_StatTaskRate_Hz); } if (dly == (OS_TICK)0) { dly = (OS_TICK)(OSCfg_TickRate_Hz / (OS_RATE_HZ)10); } OSTimeDly(dly, /* Determine MAX. idle counter value */ OS_OPT_TIME_DLY, &err); #if ((OS_CFG_TMR_EN > 0u) && (OS_CFG_TASK_SUSPEND_EN > 0u)) OSTaskResume(&OSTmrTaskTCB, &err); if (err != OS_ERR_NONE) { *p_err = err; return; } #endif CPU_CRITICAL_ENTER(); OSStatTaskTimeMax = (CPU_TS)0; OSStatTaskCtrMax = OSStatTaskCtr; /* Store maximum idle counter count */ OSStatTaskRdy = OS_STATE_RDY; CPU_CRITICAL_EXIT(); *p_err = OS_ERR_NONE; }
int set_bt_dfu(void) { int ret = -1; U16 tx_size = 1000; U16 cmd_type = 1; U32 bt_dfu_file_size=0; U32 org_baudrate = SERIAL_BAUDRATE_OLD; U32 high_speed_baudrate = SERIAL_BAUDRATE_NEW; COMPortString = (U8*)SERIAL_BT_NAME; /*设定模組使用协议 CmdType,处理不同协议用的. COW 协议设为 0, FIH 协议设为 1, FLC 协议设为 2, GOC 协议设为 3, DEG 协议设为 4 */ if (!SetDFUCmd(cmd_type)) { DLOGD("Command Set Set Fail\n"); ret = -1; goto fail; } CurrCmdType = cmd_type; //设定程式需使用到的 buffer memset(UARTRawBuf, 0, 256); setup_DataBuffer(UARTRawBuf, BCSPRxBuf, BCSPTxBuf, BCSPAckBuf); //设定 UART if (Setup_UART(org_baudrate, curDFU.UARTRawBuffer) < 0) { DLOGD("Setup_UART err\n"); goto fail; } //Setup_Timer(); //打开DFU升级文件 dfu_fd = open(DUF_FILE_NAME, O_RDONLY, 0777); if (dfu_fd <= 0){ DLOGD("open dfu file err!\n"); ret = -2; goto fail; } bt_dfu_file_size = get_file_size(DUF_FILE_NAME); if (!bt_dfu_file_size){ DLOGD("get dfu file size is zero\n"); ret = -3; goto fail; } DLOGD("bt dfu file size is :%d", bt_dfu_file_size); //DFU 功能初始化动作 init_DFU(bt_dfu_file_size, org_baudrate, high_speed_baudrate, tx_size, cmd_type); //OSTimeDly(500); //开始 DFU 更新,直到更新完成或更新错误时結束 DoDFU(); DLOGD("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); DLOGD("----->load:%d/100, fileSizeDownloaded:%d,fileSize:%d", curDFU.fileSizeDownloaded*100/(curDFU.fileSize-16), curDFU.fileSizeDownloaded, curDFU.fileSize); DLOGD("===================%s %s===================", __TIME__, __DATE__); if (curDFU.ErrCode) { unsigned long written = 0; U8 i=0; DLOGD("\nError occurs during DFU Progressing\r\n"); DLOGD("\tCurrent Stage is %u\r\n", curDFU.curStage); DLOGD("\tCurrent EVENT is %u\r\n", curDFU.curEvent); DLOGD("\tError Code is 0x%x\r\n", curDFU.ErrCode); if (curDFU.curEvent == UPGRADE_EVENT_ERROR) { DLOGD("\t\tAdvErrCode is %u\r\n", curDFU.AdvErrCode); } ret = -4; goto fail; } else { DLOGD("\nDevice Upgrade Completed !!, total time:%d sec\r\n\r\n\r\n", CountTimeInterval(StartDFUTime, GetTimeReg())/1000); ret = 1; } fail: DLOGD("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); serial_thread_status = 0; bt_serial_close(serial_fd); OSTimeDly(1000); close(dfu_fd); DLOGD("ret =%d\n", ret); return ret; }
void LineFollow_Task(void* param) { while(1) { switch (readCycle){ case 0: SHIFT_Set(0x81); /* 10000001 => 10001000 */ MUX_Set(0,0); break; case 1: SHIFT_Set(0x42); /* 01000010 => 01000100 */ MUX_Set(0,1); break; case 2: SHIFT_Set(0x24); /* 00100100 => 00100010 */ MUX_Set(1,0); break; case 3: SHIFT_Set(0x18); /* 00011000 => 00010001 */ MUX_Set(1,1); break; } /* Let CNY70 do the work : task delay for 250us */ OSTimeDly(OS_TICKS_PER_SEC/4000); /* Read data from ADC */ /* Sensor 6-pack reading cycle (4 cycle = 24 sensors) */ ADCExt_Read(readCycle); SHIFT_Set(0x00); readCycle++; if(readCycle == 4) { readCycle = 0; /* Process the measured data */ frontAverage /= 20; /* the final front average value */ rearAverage /= 4; /* the final rear average value */ if(lineFollowState == INIT_STATE){ if(searchLine()){ edgeValueVerifyingCounter++; if(edgeValueVerifyingCounter > PEAK_VERIF_CNTR_THRES){ /* TODO can we pass to ON_LINE state? */ lineFollowState = ON_LINE; } } } else{ /* TODO process sensor data */ /* FIXME this is the init line searching. Replace with * runtime line searching (same but max. 3 lines) */ if(searchLine()){ /* Line found, Change the state if needed */ LineFollow_StateMachine(); } } maximumSensorValue = 0; frontAverage = 0; } } }
void UserMain(void * pd) { int x; DWORD flag = 0; int cnt; InitializeStack(); GetDHCPAddressIfNecessary(); if (EthernetIP == 0) GetDHCPAddress(); OSChangePrio(MAIN_PRIO); EnableAutoUpdate(); #ifdef _DEBUG /* InitializeNetworkGDB_and_Wait(); */ InitializeNetworkGDB(); #endif /* note, this may not work in all cases. 1) all systems might boot at the same time 2) when they get to this point, they may not have an ip yet */ /* init our acn stack */ acn_port_protect_startup(); #if CONFIG_NSK srand(GetPreciseTime() + netx_getmyip(0)); netx_init(); netx_startup(); #endif #if CONFIG_SLP slp_init(); slp_open(); slp_active_discovery_start(); #endif #if CONFIG_RLP rlp_init(); #if CONFIG_SDT sdt_init(); /* indirectly calls sdtm_init(), rlp_init(), rlpm_init() */ sdt_startup(true); #endif #if CONFIG_DMP /* dmp_startup(); */ #endif #endif /* RLP */ CreateTasks(); x = OSChangePrio( DEBUG_TERM_PRIORITY ); if (x) { PRINTF("%s","OUCH\n"); } PRINTF("%s","Hello ACN World\n"); ioctl( 0, IOCTL_CLR | IOCTL_RX_ECHO ); /* turn sdtin echo off */ process_keys(); /* shut things down - these require the threads to continue run... */ #if CONFIG_SDT sdt_shutdown(); #endif #if CONFIG_SLP slp_close(); #endif #if CONFIG_DMP /* dmp_shutdown() */ #endif /* shut down receive thread */ OSFlagClear(&recv_flag, 1); PRINTF("%s","Waiting for task to recv to shut down.."); cnt = 0; flag = 0; while (!flag) { cnt++; if (cnt == 80) { cnt = 0; PRINTF("%s","\n"); } PRINTF("%s","."); flag = OSFlagState(&recv_flag); } /* shut down tick thread */ OSFlagClear(&tick_flag, 1); PRINTF("%s","Waiting for task to tick to shut down.."); cnt = 0; flag = 0; while (!flag) { cnt++; if (cnt == 80) { cnt = 0; PRINTF("%s","\n"); } PRINTF("%s","."); flag = OSFlagState(&tick_flag); } #if CONFIG_NSK netx_shutdown(); #endif acn_port_protect_shutdown(); slp_stats(); sdt_stats(); PRINTF("%s","========================\n"); PRINTF("%s","\nDone....\n"); while (1) { OSTimeDly(20); } }