void DataBackTest(void) { //double i ; uint8 *msg; for(;;) { LCD_Clear(); LCD_SetXY(0,0); LCD_Printf("Data saved %d" , BackDataCount); LCD_SetXY(0,1); LCD_Printf("Press Enter to Send" ); OSMboxAccept(AppKey2Mbox); //这一句是在清除pevent->OSEventPtr中可能存在的数据 OSTimeDly(20); msg = OSMboxAccept(AppKey2Mbox); switch(*msg) { /*case 1: BackDataInit(); for(i=0 ; i<10000 ; i++) { SaveMovingData(); } LCD_SetXY(0,3); LCD_Printf("Save data %d" , BackDataCount); break;*/ case 18: LCD_Clear(); LCD_SetXY(4,3); LCD_Printf("Waiting....." ); //PC_WriteIntNew(BackDataCount); PC_WriteNumNew(BackDataCount); //PC_WriteInt(BackDataCount); //PC_WriteChar('\n'); SendBackData(); PC_WriteChar('$'); //SendEndData(); LCD_Clear(); LCD_SetXY(4,3); LCD_Printf("Done~!" ); break; } } }
/* Notes :(1) The first line of code is used to prevent a compiler warning because 'p_arg' is not * used. The compiler should not generate any code for this statement. * * ********************************************************************************************************* */ void APP_TaskThree(void *p_arg) { static char taskStringBuffer[APPDEF_LCD_TXT_SIZE+1U] = {'u','C','/','O','S','-','2','\0'}; static int ringPos = 0; char indxChar; char *pMsgContent; (void)p_arg; /* Note(1) */ while (1) { /* Task body, always written as an infinite loop */ /* Turn previous ring segment off */ SegmentLCD_ARing(ringPos, 0); /* Increase ring position variable */ if (8u == ++ringPos) { ringPos = 0; /* 3bit overflow */ } /* Turn updated ring segment on */ SegmentLCD_ARing(ringPos, 1); /* Non-blocking reception of a message */ pMsgContent = OSMboxAccept(pSerialMsgObj); /* If a valid message was received... */ if ((void *)0 != pMsgContent) { /* ...shift left the whole string by one... */ for (indxChar = 0; indxChar < APPDEF_LCD_TXT_SIZE; indxChar++) { taskStringBuffer[indxChar] = taskStringBuffer[indxChar+1]; } /* ...and concatenate the new character to the end. */ taskStringBuffer[APPDEF_LCD_TXT_SIZE-1] = *pMsgContent; /* Write the string on serial port */ printf("\nBuffer: %s", taskStringBuffer); /* Write the string on LCD */ SegmentLCD_Write(taskStringBuffer); } /* Delay task for 1 system tick (uC/OS-II suspends this task and executes * the next most important task) */ OSTimeDly(100); } }
/* ******************************************************************************* ** ** This function waits until a message is available in the given mailbox. ** The suspend argument controls whether this function will be in blocking ** mode or not while waiting for a message. A waiting message will be cleared ** ******************************************************************************* */ RTOS_Message RTOS_MailboxWait( RTOS_Mailbox mailbox, RTOS_Flag suspend ) { INT8U error = OS_NO_ERR; void* message = 0; if( ! RTOS_MailboxIsValid( mailbox ) ) return( (RTOS_Message)0 ); if( suspend ) message = OSMboxPend( (OS_EVENT*)mailbox, (INT16U)0, &error ); else message = OSMboxAccept( (OS_EVENT*)mailbox ); if( ( error != OS_NO_ERR ) || ( message == 0 ) ) return( (RTOS_Message)0 ); return( message ); }
/* Notes :(1) The first line of code is used to prevent a compiler warning because 'p_arg' is not * used. The compiler should not generate any code for this statement. * * ********************************************************************************************************* */ void APP_TaskThree(void *p_arg) { /* As USART connectors are not available on the STK by default, * therefore printf() functions are turned off. * Uncomment the macro definition in includes.h if serial * is connected to your STK board (USART1 or LEUART0)! */ #ifdef USART_CONNECTED static char taskStringBuffer[APPDEF_LCD_TXT_SIZE+1U] = {'u','C','/','O','S','-','2','\0'}; char indxChar; char *pMsgContent; #endif /* end of #ifndef USART_CONNECTED */ static int ringPos = 0; (void)p_arg; /* Note(1) */ while (1) { /* Task body, always written as an infinite loop */ /* Turn previous ring segment off */ SegmentLCD_ARing(ringPos, 0); /* Increase ring position variable */ if (8u == ++ringPos) { ringPos = 0; /* 3bit overflow */ } /* Turn updated ring segment on */ SegmentLCD_ARing(ringPos, 1); /* As USART connectors are not available on the STK by default, * therefore printf() functions are turned off. * Uncomment the macro definition in includes.h if serial * is connected to your STK board (USART1 or LEUART0)! */ #ifdef USART_CONNECTED pMsgContent = OSMboxAccept(pSerialMsgObj); if ((void *)0 != pMsgContent) { /* Shift left the whole string by one... */ for (indxChar = 0; indxChar < APPDEF_LCD_TXT_SIZE; indxChar++) { taskStringBuffer[indxChar] = taskStringBuffer[indxChar+1]; } /* ...and concatenate the new character to the end */ taskStringBuffer[APPDEF_LCD_TXT_SIZE-1] = *pMsgContent; /* Write the string on serial port */ printf("\nBuffer: %s", taskStringBuffer); /* Write the string on LCD */ SegmentLCD_Write(taskStringBuffer); } #endif /* end of #ifndef USART0_CONNECTED */ OSTimeDlyHMSM(0, 0, 0, 100); /* Delay task for 1 system tick (uC/OS-II suspends this task and executes * the next most important task) */ OSTimeDly(1); } }
void TaskTrafficLightsController(void *pdata) { /* * The traffic lights controller is implemented using finite state machine (FSM) * There are 4 states in the FSM: green_red state, yellow_red state, red_green state, and red_flashingGreen state * The controller is simulated based on the real life traffic lights in Finland * However, the small difference is that it controls based on the sensors located at 4 different locations before each of the * crossing lines. */ void *carMsg = ""; void *pedMsg = ""; TtrafLightState currentState = green_red; TtrafLightState nextState = yellow_red; pdata = pdata; while(1) { switch(currentState) { case green_red: carLights = green; pedLights = red; displayCarLights(green); displayPedLights(red); carMsg = OSMboxAccept(CarMbox); pedMsg = OSMboxAccept(PedMbox); if (pedMsg != "PED_REQUEST") { currentState = currentState; } else { nextState = yellow_red; currentState = nextState; } break; case yellow_red: carLights = yellow; pedLights = red; displayCarLights(yellow); displayPedLights(red); pedMsg = OSMboxAccept(PedMbox); carMsg = OSMboxAccept(CarMbox); if (carMsg != "CAR_REQUEST" || carMsg == "CAR_FINISH") { nextState = red_green; currentState = nextState; } else { currentState = currentState; } break; case red_green: carLights = red; pedLights = green; displayCarLights(red); displayPedLights(green); pedMsg = OSMboxAccept(PedMbox); carMsg = OSMboxAccept(CarMbox); if (carMsg != "CAR_REQUEST") { currentState = currentState; } else { nextState = red_flashingGreen; currentState = nextState; } break; case red_flashingGreen: carLights = red; pedLights = flashingGreen; displayCarLights(red); displayPedLights(flashingGreen); carMsg = OSMboxAccept(CarMbox); pedMsg = OSMboxAccept(PedMbox); if (pedMsg == "PED_REQUEST" || pedMsg != "PED_FINISH") { currentState = currentState; } else { nextState = green_red; currentState = nextState; } break; default: PC_DispStr(X_START+1, Y_END-1, "in default state", DISP_FGND_YELLOW + DISP_BGND_LIGHT_GRAY); carLights = red; pedLights = green; displayCarLights(red); displayPedLights(green); pedMsg = OSMboxAccept(PedMbox); carMsg = OSMboxAccept(CarMbox); nextState == red_flashingGreen; currentState = nextState; break; } OSTimeDly(1); //delay here for the kernel to switch to another task } }
void TaskCar3(void *pdata) { INT8U *rxMsg; pdata = pdata; //avoid compiler warning car3Pos.x = X_INTER1 + 3; //set initial car3Pos.y = Y_START; //position for car while(1) { if (car3Direction == DOWN) { if (car3Pos.y == Y_END) { car3Direction = RIGHT; } else { rxMsg = (INT8U *)OSMboxAccept(Car2ToCar3Mbox); if (rxMsg == carSignals[0]) { stopCar3(&car3Pos); } else if (rxMsg == carSignals[1]) { car3Pos.y += 1; moveCar3(&car3Pos, SPEED_FAST); } else { stopCar3(&car3Pos); } } } else if (car3Direction == RIGHT) { if (car3Pos.x == X_INTER2-3) car3Direction = UP; else { rxMsg = (INT8U *)OSMboxAccept(Car2ToCar3Mbox); if (rxMsg == carSignals[0]) { stopCar3(&car3Pos); } else if (rxMsg == carSignals[1]) { car3Pos.x += 1; moveCar3(&car3Pos, SPEED_FAST); } else { stopCar3(&car3Pos); } } } else if (car3Direction == UP) { if (car3Pos.y == Y_START) { car3Direction = LEFT; } else { rxMsg = (INT8U *)OSMboxAccept(Car2ToCar3Mbox); if (rxMsg == carSignals[0]) { stopCar3(&car3Pos); } else if (rxMsg == carSignals[1]) { car3Pos.y -= 1; moveCar3(&car3Pos, SPEED_FAST); } else { stopCar3(&car3Pos); } } } else { if (car3Pos.x == X_INTER1+3) car3Direction = DOWN; else { rxMsg = (INT8U *)OSMboxAccept(Car2ToCar3Mbox); if (rxMsg == carSignals[0]) { stopCar3(&car3Pos); } else if (rxMsg == carSignals[1]) { car3Pos.x -= 1; moveCar3(&car3Pos, SPEED_FAST); } else { stopCar3(&car3Pos); } } } OSTimeDly(1); //delay here for the kernel to switch to another task } //end while(1) }
void TaskCar2(void *pdata) { INT8U err; char *rxMsg; pdata = pdata; //avoid compiler warning car2Pos.x = X_INTER1 + 3; //set initial car2Pos.y = Y_START + 2; //position for car while(1) { if (car2Direction == DOWN) { if (car2Pos.y == Y_END) { car2Direction = RIGHT; } else { rxMsg = (char *)OSMboxAccept(Car1ToCar2Mbox); if (rxMsg == carSignals[0]) { stopCar2(&car2Pos); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[0]); } else if (rxMsg == carSignals[1]) { car2Pos.y += 1; moveCar2(&car2Pos, SPEED_FAST); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[1]); } else { stopCar2(&car2Pos); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[0]); } } } else if (car2Direction == RIGHT) { if (car2Pos.x == X_INTER2-3) car2Direction = UP; else { rxMsg = (char *)OSMboxAccept(Car1ToCar2Mbox); if (rxMsg == carSignals[0]) { stopCar2(&car2Pos); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[0]); } else if (rxMsg == carSignals[1]) { car2Pos.x += 1; moveCar2(&car2Pos, SPEED_FAST); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[1]); } else { stopCar2(&car2Pos); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[0]); } } } else if (car2Direction == UP) { if (car2Pos.y == Y_START) { car2Direction = LEFT; } else { rxMsg = (char *)OSMboxAccept(Car1ToCar2Mbox); if (rxMsg == carSignals[0]) { stopCar2(&car2Pos); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[0]); } else if (rxMsg == carSignals[1]) { car2Pos.y -= 1; moveCar2(&car2Pos, SPEED_FAST); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[1]); } else { stopCar2(&car2Pos); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[0]); } } } else { if (car2Pos.x == X_INTER1+3) car2Direction = DOWN; else { rxMsg = (char *)OSMboxAccept(Car1ToCar2Mbox); if (rxMsg == carSignals[0]) { stopCar2(&car2Pos); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[0]); } else if (rxMsg == carSignals[1]) { car2Pos.x -= 1; moveCar2(&car2Pos, SPEED_FAST); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[1]); } else { stopCar2(&car2Pos); OSMboxPost(Car2ToCar3Mbox, (void *)carSignals[0]); } } } OSTimeDly(1); } //end while(1) }
void DevLineTask(void *pdata) { int i,n; uint8 chk,cmd,t,test; uint16 nDataSize; uint8 err,state; uint8 mBuf[8]; PNODEMSG msg; PTHEADER header; pdata = pdata; msg = (PNODEMSG)mBuf; header = (PTHEADER)uart0_buf; state = CMD_READY; // TargetInit(); while(1) { // 1 /* 等待总线空闲 */ // UART0RXLineClear(); //2 StartWhile: /* 开始接受接受数据,协议分析 */ t = UART0Getch(); if( t != PLT_VERSION ) { if( t == 0xdb) t = UART0Getch(); continue; } chk = 0; i = 0; do{ t = UART0Getch(); if( t == 0xdb ) { t = UART0Getch(); }else if( t == PLT_VERSION ) { continue; } chk ^= t; uart0_buf[i] = t; i++; } while( i < 4 ); if( chk ) continue; //校验错误 /* 为命令包,为本机地址,有数据包继续接受 不是,又有数据包跳空接受 为应答包,跳空接受 */ // 6 if( header->bits.addr != MACHINE_NO ) { t = header->bits.size; OSTimeDly( (t + 2) / 5 ); //为9600 一个字节为1ms } nDataSize = header->bits.size + 1; switch( header->bits.token ) { case TOKEN_REQUESTSIZE: if( nDataSize < MAX_UART_BUFFER ) { header->bits.token = TOKEN_ALLOWSIZE; }else header->bits.token = TOKEN_ERRSIZE; header->bits.token |= 0x08; chk = 0; for( i = 0; i < 3; i++ ) { chk^= uart0_buf[i]; } header->bits.check = chk; UART0Putch(PLT_VERSION); UART0WriteTab(uart0_buf, 4); continue; break; case TOKEN_DATAPACK: if( nDataSize > MAX_UART_BUFFER ) { header->bits.token = TOKEN_ERRSIZE;; }else if( state == CMD_BUSY ) header->bits.token = TOKEN_BUSY; else break; i = 0; do { t = UART0Getch(); if( t == PLT_TAB ) { t = UART0Getch(); }else if( t == PLT_VERSION ) { continue; } i++; }while( i < nDataSize ); header->bits.token |= 0x08; chk = 0; for( i = 0; i < 3; i++ ) { chk^= uart0_buf[i]; } header->bits.check = chk; UART0Putch(PLT_VERSION); UART0WriteTab(uart0_buf, 4); break; case TOKEN_PING: { uint32 nSize; nSize = 0; if( state == CMD_BUSY ) { nSize = (uint32)OSMboxAccept(DevLineMsg); } if( nSize == 0 ) { header->bits.token = 0x08 | TOKEN_PING; header->bits.size = 0; } else { header->bits.token = TOKEN_DATAPACK | 0x8; header->bits.size = nSize; chk = 0; for( i = 0; i < nSize; i++) chk ^= uart0_buf[i+4]; uart0_buf[i+4] = chk; state = CMD_READY; } chk = 0; for( i = 0; i < 3; i++ ) { chk^= uart0_buf[i]; } header->bits.check = chk; nSize += 4; UART0Putch(PLT_VERSION); UART0WriteTab(uart0_buf, nSize); continue; break; } } if( nDataSize - 1 ) { // 8 chk = 0; i = 4; do{ t = UART0Getch(); if( t == PLT_TAB ) { t = UART0Getch(); }else if( t == PLT_VERSION ) { continue; } chk ^= t; uart0_buf[i] = t; i++; } while( i < nDataSize ); if( chk ) continue; } /* 处理协议 */ /*解析命令*/ /* Flash: CMD + ADDRESS + SIZE + Data CMD + ADDRESS + BIT + Data CMD + ADDRESS */ cmd = uart0_buf[4]; switch(cmd) { case SETLONGDATA: case GETLONGDATA: case SETBITDATA: msg->bits.msg = cmd & 0xf; msg->bits.size = 8; msg->bits.data[0] = (uint32)(uart0_buf + 1 + 4); msg->bits.task = 2; msg->bits.node = FLASH_NODE; if( NMsgQWrite(msg) == QUEUE_OK ) { OSSemPost(QNoEmptySem); state = CMD_BUSY; } break; } /* switch( cmd ) { case ADDUSER: { uint16 empty_p,exist_p; nDataSize = uart_buf[CMD_P1]; addr.value[0] = *(uart_buf + DATA_PACK_DATA); addr.value[1] = *(uart_buf + DATA_PACK_DATA + 1); addr.value[2] = *(uart_buf + DATA_PACK_DATA + 2); addr.value[3] = *(uart_buf + DATA_PACK_DATA + 3); if( addr.addr != 0 && nDataSize == 13 ) { at45db_Comp_uint32( USER_ID_PAGE, 0, addr.addr , (USER_ID_NUMBER), &empty_p, &exist_p ); if( exist_p == (USER_ID_NUMBER) ) { exist_p = empty_p; } addr.addr = 0; empty_p = exist_p / 132; addr.bits.pa = empty_p; addr.bits.ba = exist_p % 132; at45db_PagetoBuffer( 1, addr.bits.pa + USER_ID_PAGE); at45db_Buffer_Write( 1, addr.bits.ba, uart_buf + DATA_PACK_DATA , 4); at45db_BuffertoPage( 1, addr.bits.pa + USER_ID_PAGE ); addr.bits.pa *= 2; addr.bits.ba *= 2; at45db_PagetoBuffer( 1, empty_p + USER_GROUP_PAGE ); at45db_Buffer_Write( 1, addr.bits.ba, uart_buf + DATA_PACK_DATA + 4, 8); at45db_BuffertoPage( 1, empty_p + USER_GROUP_PAGE ); addr.bits.pa >>= 2; addr.bits.ba >>= 2; at45db_PagetoBuffer( 1, addr.bits.pa + USER_INFO_PAGE ); at45db_Buffer_Write( 1, addr.bits.ba, uart_buf + DATA_PACK_DATA + 12, 2); at45db_BuffertoPage( 1, addr.bits.pa + USER_INFO_PAGE ); uart_buf[CMD_P4] = 0; nDataSize = 0; }else { uart_buf[CMD_P4] = ERR_CMDPARAM; nDataSize = 0; } } break; case DELUSER: { uint32 id; uint16 empty_p,exist_p; nDataSize = uart_buf[CMD_P1]; addr.value[0] = uart_buf[CMD_P1]; addr.value[1] = uart_buf[CMD_P2]; addr.value[2] = uart_buf[CMD_P3]; addr.value[3] = uart_buf[CMD_P4]; if( addr.addr != 0 ) { at45db_Comp_uint32( USER_ID_PAGE, 0, addr.addr , (USER_ID_NUMBER), &empty_p, &exist_p ); if( exist_p == (USER_ID_NUMBER) ) { uart_buf[CMD_P4] = ERR_USERNOEXIST; nDataSize = 0; break; } addr.addr = 0; empty_p = exist_p / 528; addr.bits.pa = empty_p + USER_ID_PAGE; addr.bits.ba = exist_p % 528; id = 0; at45db_PagetoBuffer( 1, addr.bits.pa ); at45db_Buffer_Write( 1, addr.bits.ba, (uint8*)&id, 4); at45db_BuffertoPageNoErase( 1, addr.bits.pa ); uart_buf[CMD_P4] = 0; nDataSize = 0; }else { uart_buf[CMD_P4] = ERR_CMDPARAM; nDataSize = 0; } } break; case SETSTATUE: switch( uart_buf[CMD_P1] ) { case TEST_NODE: test = uart_buf[CMD_P2]; uart_buf[CMD_P4] = 0; nDataSize = 0; break; default: uart_buf[CMD_P4] = ERR_NODENOEXIST; nDataSize = 0; break; } break; case GETSTATUS: switch( uart_buf[CMD_P1] ) { case TEST_NODE: uart_buf[CMD_P1] = test; uart_buf[CMD_P4] = 0; nDataSize = 0; break; default: uart_buf[CMD_P4] = ERR_NODENOEXIST; nDataSize = 0; break; } break; case DOWNLOADLOG: { uint32 id; uint16 ba,pa; addr.value[3] = uart_buf[CMD_P1]; addr.value[2] = uart_buf[CMD_P2]; addr.value[1] = uart_buf[CMD_P3]; addr.value[0] = uart_buf[CMD_P4]; id = addr.addr; if( log_id - id > MAX_SAVELOGS ) { id = log_id - MAX_SAVELOGS; } ba = (id % 33) << 4; pa = id / 33; pa %= (MAX_PAGE_COUNT - LOG_PAGE); pa += LOG_PAGE; //read log nDataSize = (log_id - id) * 16; if( nDataSize > 256 ) { nDataSize = 256; } if( pa == 4095 && ba + nDataSize > 528 ) { nDataSize = 528 - ba; } at45db_Page_Read( pa, ba, uart_buf + DATA_PACK_DATA, nDataSize); uart_buf[CMD_P4] = 0; nDataSize--; break; } case SETPOINTLOG: break; default: uart_buf[CMD_P4] = ERR_CMD; nDataSize = 0; break; } cmd |= 0x80; cmd &= 0xBF; uart_buf[CMD_CMD] = cmd; uart_buf[0] = PLT_VERSION; uart_buf[CMD_ADDR] = MACHINE_NO; chk = 0; for( i = 1; i < 7; i++) { chk ^= uart_buf[i]; } uart_buf[i] = chk; i++; if( nDataSize ) { uart_buf[CMD_CMD] |= 0x40; uart_buf[CMD_XOR] ^= 0x40; uart_buf[i] = PLT_VERSION; i++; nDataSize += i; chk = 0; for( ; i < nDataSize; i++ ) { chk ^= uart_buf[i]; } uart_buf[i] = chk; i++; } UART0Write(uart_buf, i); */ } }