/****************************************************************************** * Function: void ProcessIO(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This function is a place holder for other user routines. * It is a mixture of both USB and non-USB tasks. * * Note: None *****************************************************************************/ void USBUpdate(void) { byte data[2]; byte i; byte paramNum; ulong tempLong; USBDeviceTasks(); if ((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl==1)) { return; } //As the device completes the enumeration process, the USBCBInitEP() function will //get called. In this function, we initialize the user application endpoints (in this //example code, the user application makes use of endpoint 1 IN and endpoint 1 OUT). //The USBGenRead() function call in the USBCBInitEP() function initializes endpoint 1 OUT //and "arms" it so that it can receive a packet of data from the host. Once the endpoint //has been armed, the host can then send data to it (assuming some kind of application software //is running on the host, and the application software tries to send data to the USB device). //If the host sends a packet of data to the endpoint 1 OUT buffer, the hardware of the SIE will //automatically receive it and store the data at the memory location pointed to when we called //USBGenRead(). Additionally, the endpoint handle (in this case USBGenericOutHandle) will indicate //that the endpoint is no longer busy. At this point, it is safe for this firmware to begin reading //from the endpoint buffer, and processing the data. In this example, we have implemented a few very //simple commands. For example, if the host sends a packet of data to the endpoint 1 OUT buffer, with the //first byte = 0x80, this is being used as a command to indicate that the firmware should "Toggle LED(s)". if(!USBHandleBusy(USBGenericOutHandle) && //Check if the endpoint has received any data from the host. //Now check to make sure no previous attempts to send data to the host are still pending. If any attemps are still //pending, we do not want to write to the endpoint 1 IN buffer again, until the previous transaction is complete. //Otherwise the unsent data waiting in the buffer will get overwritten and will result in unexpected behavior. (!USBGenericInHandle || !USBHandleBusy(USBGenericInHandle))) { if (OUTPacket[USB_PACKET_LEN] < 7) { // message too short } else { usbActivityTimeout = 3000; // reset timeout (in ms) 3 seconds switch(OUTPacket[USB_PACKET_CMD]) { //Data arrived, check what kind of command might be in the packet of data. case USB_CMD_NULL: // 0x00 if (OUTPacket[USB_PACKET_LEN] != 7) { data[0] = USB_ERR_BAD_LEN; UsbSendResp(USB_RESP_BAD_MSG, data); } else { UsbSendResp(USB_RESP_NULL, data); } break; case USB_CMD_GET_VSTRING: if (OUTPacket[USB_PACKET_LEN] != 7) { data[0] = USB_ERR_BAD_LEN; UsbSendResp(USB_RESP_BAD_MSG, data); } else { UsbSendResp(USB_RESP_GET_VSTRING, data); } break; case USB_CMD_GET_CHAN: if (OUTPacket[USB_PACKET_LEN] != 7) { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } else if (OUTPacket[USB_PACKET_STR] >= NUM_STRINGS) { data[0] = USB_ERR_BAD_PRM; data[1] = 0; UsbSendResp(USB_RESP_BAD_MSG, data); } else if (!OUTPacket[USB_PACKET_MCT] || OUTPacket[USB_PACKET_MCT] > NUM_MCT) { data[0] = USB_ERR_BAD_PRM; data[1] = 1; UsbSendResp(USB_RESP_BAD_MSG, data); } else { UsbSendResp(USB_RESP_GET_CHAN, data); } break; case USB_CMD_GET_MIRRORS: if (OUTPacket[USB_PACKET_LEN] != 7) { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } else if (OUTPacket[USB_PACKET_STR] >= NUM_STRINGS) { data[0] = USB_ERR_BAD_PRM; data[1] = 0; UsbSendResp(USB_RESP_BAD_MSG, data); } else if (!OUTPacket[USB_PACKET_MCT] || OUTPacket[USB_PACKET_MCT] > NUM_MCT) { data[0] = USB_ERR_BAD_PRM; data[1] = 1; UsbSendResp(USB_RESP_BAD_MSG, data); } else { UsbSendResp(USB_RESP_GET_MIRRORS, data); } break; case USB_CMD_GET_STRING: if (OUTPacket[USB_PACKET_LEN] != 7) { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } else if (OUTPacket[USB_PACKET_STR] >= NUM_STRINGS) { data[0] = USB_ERR_BAD_PRM; data[1] = 0; UsbSendResp(USB_RESP_BAD_MSG, data); } else { UsbSendResp(USB_RESP_GET_STRING, data); } break; case USB_CMD_FIELD_STATE: if (OUTPacket[USB_PACKET_LEN] == 7) { UsbSendResp(USB_RESP_FIELD_STATE, data); } else if (OUTPacket[USB_PACKET_LEN] == 8) { FieldNewState(OUTPacket[USB_PACKET_DATA]); UsbSendResp(USB_RESP_FIELD_STATE, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_GET_FCE: if (OUTPacket[USB_PACKET_LEN] == 7) { UsbSendResp(USB_RESP_GET_FCE, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_GET_RTU: if (OUTPacket[USB_PACKET_LEN] == 7) { UsbSendResp(USB_RESP_GET_RTU, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_SEND_MCT485: // 0x64 if (OUTPacket[USB_PACKET_LEN] >= 11 && OUTPacket[USB_PACKET_LEN] < MCT485_MAX_INDEX + 7) { //Mct485CannedMsg(MSG_ORIGIN_USB, // OUTPacket[USB_PACKET_STR], // OUTPacket[USB_PACKET_LEN]-7, // &OUTPacket[USB_PACKET_DATA]); usb485TxString = OUTPacket[USB_PACKET_STR]; usb485TxLen = OUTPacket[USB_PACKET_LEN]-7; for (i = 0; i < usb485TxLen; i++) { usb485TxBuffer[i] = OUTPacket[USB_PACKET_DATA+i]; } // clear 485 response buffer usb485RxLen = 0; UsbSendResp(USB_RESP_SEND_MCT485, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_GET_MCT485: // 0x65 if (OUTPacket[USB_PACKET_LEN] == 7) { UsbSendResp(USB_RESP_GET_MCT485, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_RTC: // 0x66 if (OUTPacket[USB_PACKET_LEN] == 7) { UsbSendResp(USB_RESP_RTC, data); } else if (OUTPacket[USB_PACKET_LEN] == 13) { RtcSetClock(&OUTPacket[USB_PACKET_DATA]); UsbSendResp(USB_RESP_RTC, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_LOG: // 0x67 if (OUTPacket[USB_PACKET_LEN] == 7) { UsbSendResp(USB_RESP_LOG, data); } else if (OUTPacket[USB_PACKET_LEN] == 8) { if (OUTPacket[USB_PACKET_DATA] == 0) { DataLogFindFirstEntry(); } else if (OUTPacket[USB_PACKET_DATA] == 0xFF) { DataLogErase(); } UsbSendResp(USB_RESP_LOG, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_DESICCANT: // 0x68 if (OUTPacket[USB_PACKET_LEN] == 7) { UsbSendResp(USB_RESP_DESICCANT, data); } else if (OUTPacket[USB_PACKET_LEN] == 9) { // manual force dessicant to set of outputs or state DesiccantNewState(OUTPacket[USB_PACKET_DATA], OUTPacket[USB_PACKET_DATA+1]); UsbSendResp(USB_RESP_DESICCANT, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_SFC_PARAM: // 0x69 if (OUTPacket[USB_PACKET_LEN] == 8) { UsbSendResp(USB_RESP_SFC_PARAM, data); } else if (OUTPacket[USB_PACKET_LEN] > 8 && !(OUTPacket[USB_PACKET_LEN] & 0x03)) { // process one param at a time, prevents I2C queue entry overflow and avoids // I2C page boundary problems paramNum = OUTPacket[USB_PACKET_DATA]; i = USB_PACKET_DATA+1; while (i < OUTPacket[USB_PACKET_LEN]-2) { tempLong = OUTPacket[i++]; tempLong *= 256; tempLong += OUTPacket[i++]; tempLong *= 256; tempLong += OUTPacket[i++]; tempLong *= 256; tempLong += OUTPacket[i++]; ParamWrite(paramNum, tempLong); paramNum++; } UsbSendResp(USB_RESP_SFC_PARAM, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_MEMORY: // 0x6A if (OUTPacket[USB_PACKET_LEN] == 11) { data[4] = 16; UsbSendResp(USB_RESP_MEMORY, data); } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; case USB_CMD_TEST: // 0x6B if (OUTPacket[USB_PACKET_LEN] == 8) { UsbSendResp(USB_RESP_TEST, data); for (tempLong = 0; tempLong < 1000000; tempLong++) { } if (OUTPacket[USB_PACKET_DATA] == 0x01) { Mct485Init(); } else if (OUTPacket[USB_PACKET_DATA] == 0x02) { StringInit(); } else if (OUTPacket[USB_PACKET_DATA] == 0x03) { FieldInit(); } else if (OUTPacket[USB_PACKET_DATA] == 0x04) { SoftReset(); } } else { data[0] = USB_ERR_BAD_LEN; data[1] = 7; UsbSendResp(USB_RESP_BAD_MSG, data); } break; } // switch (cmd) } // else process messages //Re-arm the OUT endpoint for the next packet: //The USBGenRead() function call "arms" the endpoint (and makes it "busy"). If the endpoint is armed, the SIE will //automatically accept data from the host, if the host tries to send a packet of data to the endpoint. Once a data //packet addressed to this endpoint is received from the host, the endpoint will no longer be busy, and the application //can read the data which will be sitting in the buffer. USBGenericOutHandle = USBGenRead(USBGEN_EP_NUM,(BYTE*)&OUTPacket,USBGEN_EP_SIZE); } // if }// UsbUpdate()
int main(void) { MainInit(); I2C1init(); RtcInit(); LcdInit(); DataLogInit(); StringInit(); Mct485Init(); FieldInit(); Ads1115Init(); Ads1244Init(); USBInit(); RtuInit(); AdcInit(); TC74Init(); // enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); MainDelay(50); DataLogDateTime(DLOG_SFC_POWERUP); // init param after interrupts are enabled ParamInit(); // wait to init desiccant until after ParamInit DesiccantInit(); string[0].mct[0].chan[4] = 0x7FFF; // // Begin Main Loop // for (;;) { if (test == 1) { test = 0; FieldNewState((FIELD_STATE_m)t1); } USBUpdate(); // called as often as possible //sysTickEvent every ms if (sysTickEvent) { sysTickEvent = 0; sysTicks++; UsbTimeoutUpdate(); LcdUpdate(); // fill time before dropping LCD_E if (sysTicks >= 1000) { sysSec++; sysTicks = 0; mPORTDToggleBits(PD_LED_HEARTBEAT); // These Updates are // called once a second //TODO if any of these are long, we could split them on separate milliseconds. DesiccantUpdate(); AdcUpdate(); TC74Update(); }// end 1 Hz else if (sysTicks == 250) { mPORTDToggleBits(PD_LED_HEARTBEAT); } else if (sysTicks == 500) { mPORTDToggleBits(PD_LED_HEARTBEAT); } else if (sysTicks == 750) { mPORTDToggleBits(PD_LED_HEARTBEAT); } // Complete LcdUpdate() by dropping LCD_E) PORTClearBits(IOPORT_G, PG_LCD_E); // These Updates called once each millisecond RtcUpdate(); I2C1update(); StringUpdate(); Mct485Update(); FieldUpdate(); RtuUpdate(); DessicantFanPWMupdate(); } // if (sysTickEvent) } // for (;;) } // main()
task main() { SensorsInit(); MechanismInit(); FieldInit(); /* while ( true ) { nxtDisplayCenteredTextLine(0, (string)LEFT_LIGHT_SENSOR()); nxtDisplayCenteredTextLine(1, (string)RIGHT_LIGHT_SENSOR()); } */ StartTask(log); Node start = NodeLine3BottomEnd; Node end = NodeFriendBridgeCenter; //RobotFindWhiteLine(); //wait10Msec(1000); ///RobotFindWhiteLine(); //RobotFollowWhiteLineForDistance(15, false); //wait10Msec(1000); CurrentRobotPosition.orientation = PI / 2.0; FieldGetNodeLocation(start, CurrentRobotPosition.location); //wait10Msec(1000); RobotTravelFromNodeToNode(start, end, false); //MechanismElevatorSetHeight(8); //RobotMoveDistance(-20, false); PlaySound(soundBeepBeep); wait10Msec(100); }