/** * Renvoie une trame qui finit par "\r" ! * FrameBuffer : tableau pour recevoir les byte. De taille this->bufferSize ! * Revoie true si l'on a une trame disponible */ bool iUSB::getFullFrame(char* FrameBuffer) { //Tester si l'on a recu qqch if (*this->dataReceived) { char pieceOfString[MAX_BUFFERSIZE] = ""; //Holds the new addition to the string //Add bytes in USB buffer to theCommand cdcReceiveDataInBuffer((BYTE*) pieceOfString, this->bufferSize, CDC0_INTFNUM); //Get the next piece of the string if ((MAX_BUFFERSIZE - strlen(this->usbSerialBuffer)) > strlen(pieceOfString)) { strcat(this->usbSerialBuffer, pieceOfString); } else { this->clearSerialBuffer(); } // Test \r if (retInString(this->usbSerialBuffer)) { strcpy(FrameBuffer, this->usbSerialBuffer); this->clearSerialBuffer(); *(this->dataReceived) = FALSE; return true; } else { *(this->dataReceived) = FALSE; return false; } } else { return false; } }
/* * ======== main ======== */ VOID main (VOID) { WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer Init_Ports(); //Init ports (do first ports because clocks do change ports) SetVCore(3); Init_Clock(); //Init clocks USB_init(); //Init USB Init_TimerA1(); //Enable various USB event handling routines USB_setEnabledEvents( kUSB_VbusOnEvent + kUSB_VbusOffEvent + kUSB_receiveCompletedEvent + kUSB_dataReceivedEvent + kUSB_UsbSuspendEvent + kUSB_UsbResumeEvent + kUSB_UsbResetEvent); //See if we're already attached physically to USB, and if so, connect to it //Normally applications don't invoke the event handlers, but this is an exception. if (USB_connectionInfo() & kUSB_vbusPresent){ USB_handleVbusOnEvent(); } __enable_interrupt(); //Enable interrupts globally while (1) { BYTE i; //Check the USB state and directly main loop accordingly switch (USB_connectionState()) { case ST_USB_DISCONNECTED: __bis_SR_register(LPM3_bits + GIE); //Enter LPM3 w/ interrupts enabled _NOP(); //For Debugger break; case ST_USB_CONNECTED_NO_ENUM: break; case ST_ENUM_ACTIVE: __bis_SR_register(LPM0_bits + GIE); //Enter LPM0 (can't do LPM3 when active) _NOP(); //For Debugger //Exit LPM on USB receive and perform a receive //operation if (bCDCDataReceived_event){ //Some data is in the buffer; begin receiving a //command char pieceOfString[MAX_STR_LENGTH] = ""; //Holds the new addition to the string char outString[MAX_STR_LENGTH] = ""; //Holds the outgoing string //Add bytes in USB buffer to theCommand cdcReceiveDataInBuffer((BYTE*)pieceOfString, MAX_STR_LENGTH, CDC0_INTFNUM); //Get the next piece of the string strcat(wholeString,pieceOfString); cdcSendDataInBackground((BYTE*)pieceOfString, strlen(pieceOfString),CDC0_INTFNUM,0); //Echoes back the characters received (needed //for Hyperterm) if (retInString(wholeString)){ //Has the user pressed return yet? if (!(strcmp(wholeString, "LED ON"))){ //Compare to string #1, and respond TA1CTL &= ~MC_1; //Turn off Timer P1OUT |= BIT0; //Turn on LED P1.0 strcpy(outString,"\r\nLED is ON\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } else if (!(strcmp(wholeString, "LED OFF"))){ //Compare to string #2, and respond TA1CTL &= ~MC_1; //Turn off Timer P1OUT &= ~BIT0; //Turn off LED P1.0 strcpy(outString,"\r\nLED is OFF\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } else if (!(strcmp(wholeString, "LED TOGGLE - SLOW"))){ //Compare to string #3, and respond TA1CTL &= ~MC_1; //Turn off Timer TA1CCR0 = SlowToggle_Period; //Set Timer Period for slow LED toggle TA1CTL |= MC_1; //Start Timer strcpy(outString, "\r\nLED is toggling slowly\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } else if (!(strcmp(wholeString, "LED TOGGLE - FAST"))){ //Compare to string #4, and respond TA1CTL &= ~MC_1; //Turn off Timer TA1CCR0 = FastToggle_Period; //Set Timer Period for fast LED toggle TA1CTL |= MC_1; strcpy(outString,"\r\nLED is toggling fast\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } else { //Handle other strcpy(outString,"\r\nNo such command!\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } for (i = 0; i < MAX_STR_LENGTH; i++){ //Clear the string in preparation for the next //one wholeString[i] = 0x00; } } bCDCDataReceived_event = FALSE; } break; case ST_ENUM_SUSPENDED: P1OUT &= ~BIT0; //When suspended, turn off LED __bis_SR_register(LPM3_bits + GIE); //Enter LPM3 w/ interrupts _NOP(); break; case ST_ENUM_IN_PROGRESS: break; case ST_NOENUM_SUSPENDED: P1OUT &= ~BIT0; __bis_SR_register(LPM3_bits + GIE); _NOP(); break; case ST_ERROR: _NOP(); break; default:; } } //while(1) } //main()