/* * ======== txData ======== */ static unsigned int txData(const unsigned char *pStr, int length, unsigned int timeout) { unsigned char sendResult; unsigned int ret = 0; sendResult = USBCDC_sendData(pStr, length, USBCDCD_INTFNUM); if (sendResult == USBCDC_SEND_STARTED) { if (!Semaphore_pend(semTxSerial, timeout)) { USBCDC_abortSend((unsigned int *)&length, USBCDCD_INTFNUM); } ret = length; } return (ret); }
/*----------------------------------------------------------------------------+ | Main Routine | +----------------------------------------------------------------------------*/ VOID main(VOID) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_StartUp(); USB_init(); USB_setEnabledEvents(kUSB_VbusOnEvent+kUSB_VbusOffEvent+kUSB_receiveCompletedEvent +kUSB_dataReceivedEvent+kUSB_UsbSuspendEvent+kUSB_UsbResumeEvent+kUSB_UsbResetEvent); // Check if we're already physically attached to USB, and if so, connect to it // This is the same function that gets called automatically when VBUS gets attached. if (USB_connectionInfo() & kUSB_vbusPresent) USB_handleVbusOnEvent(); //PWRVBUSonHandler(); while(1) { switch(USB_connectionState()) { case ST_USB_DISCONNECTED: __bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/interrupt break; case ST_USB_CONNECTED_NO_ENUM: break; case ST_ENUM_ACTIVE: if(!bCommandBeingProcessed) // If no command is being processed, then make sure there's a rcv operation { // open to receive the start of the "packet" if(!(USBCDC_intfStatus(0,&x,&y) & kUSBCDC_waitingForReceive)) // Only open it if we haven't already done so if(USBCDC_receiveData(buffer,1,0) == kUSBCDC_busNotAvailable) // Start a receive operation for a single byte -- the "size" byte of the "packet" { USBCDC_abortReceive(&x,0); // Abort receive break; // If bus is no longer available, escape out of the loop } } __bis_SR_register(LPM0_bits + GIE); // Wait in LPM0 until a receive operation has completed //__bis_SR_register(LPM0_bits + GIE); if(bDataReceiveCompleted_event) { bDataReceiveCompleted_event = FALSE; if(!bCommandBeingProcessed) // This means that the incoming byte is the start of the "packet" -- the "size" byte { if ((buffer[0]>=0x31) && (buffer[0]<= 0x39)) { size = buffer[0]-0x30; // It's in ASCII, so convert it to a number if(USBCDC_receiveData(buffer,size,0) == kUSBCDC_busNotAvailable) // And then open a rcv operation for that size { USBCDC_abortReceive(&x,0); // Abort receive break; // If bus is no longer available, escape out of the loop } bCommandBeingProcessed = TRUE; // Now we're waiting for the "data" part of the "packet" } else { strcpy(outString,"\r\nEnter a valid number between 1 and 9\r\n\r\n"); // Prepare the outgoing string if(cdcSendDataInBackground((BYTE*)outString,strlen(outString),0,0)) // Send the response over USB { USBCDC_abortSend(&x,0); // Operation may still be open; cancel it break; // If the send fails, escape the main loop } bCommandBeingProcessed = FALSE; // Now we're back to waiting for the "size" byte } } else // This means that the incoming data is the "data" part of the "packet" { strcpy(outString,"\r\nI received your packet with size of "); // Prepare the outgoing string buffer[bufferLen-1]= 0; c[0] = (char)(size+'0'); c[1] = 0; outString[64] = 0; // Convert the size back to ASCII strcat(outString,c); strcat(outString," bytes.\r\n\r\n"); if(cdcSendDataInBackground((BYTE*)outString,strlen(outString),0,0)) // Send the response over USB { USBCDC_abortSend(&x,0); // Operation may still be open; cancel it break; // If the send fails, escape the main loop } bCommandBeingProcessed = FALSE; // Now we're back to waiting for the "size" byte } } break; case ST_ENUM_SUSPENDED: __bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/interrupt break; case ST_ENUM_IN_PROGRESS: break; case ST_NOENUM_SUSPENDED: __bis_SR_register(LPM3_bits + GIE); _NOP(); break; case ST_ERROR: _NOP(); break; default:; } } // while(1) } //main()
VOID main(VOID) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_StartUp(); //initialize device USB_init(); USB_setEnabledEvents(USBEVIE); // 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(); while(1) { switch(USB_connectionState()) { case ST_USB_DISCONNECTED: __bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/interrupt _NOP(); break; case ST_USB_CONNECTED_NO_ENUM: break; case ST_ENUM_ACTIVE: __bis_SR_register(LPM0_bits + GIE); // Enter LPM0 until an event occurs. // This flag would have been set by the handleDataReceived event; this event is only enabled when waiting // for 'press any key' if(bCDCDataReceived_event) { bCDCDataReceived_event = FALSE; // Change the event flags, in preparation for receiving 1K data USBEVIE &= ~kUSB_dataReceivedEvent; // No more data-received. We only used this for 'press any key' USBEVIE |= kUSB_receiveCompletedEvent; // But enable receive-completed; we want to be prompted when 1K data has been received USB_setEnabledEvents(USBEVIE); USBCDC_rejectData(0); // We don't care what char the key-press was // Prompt user for 1K data strcpy(outString,"I'm ready to receive 1K of data.\r\n"); // Prepare the outgoing string if(cdcSendDataWaitTilDone((BYTE*)outString,strlen(outString),0,0)) // Send it over USB { USBCDC_abortSend(&x,0); // It failed for some reason; abort and leave the main loop break; } // Set up rcv operation for 1K data if(USBCDC_receiveData(dataBuff,1024,0) == kUSBCDC_busNotAvailable) //first USBCDC_receiveData { USBCDC_abortReceive(&x,0); // It failed because of surprise removal or suspended by host; break; // abort and leave the main loop } } // This flag would have been set by the handleReceiveCompleted event; this event is only enabled // while receiving 1K data, and signals that all 1K has been received if(bDataReceiveCompleted_event) { bDataReceiveCompleted_event = FALSE; strcpy(outString,"Thanks for the data.\r\n"); // Prepare the outgoing string if(cdcSendDataInBackground((BYTE*)outString,strlen(outString),0,0)) //Send the response over USB { USBCDC_abortSend(&x,0); // It failed for some reason; abort and leave the main loop break; } // Change the event flags, in preparation for 'press any key' USBEVIE &= ~kUSB_receiveCompletedEvent; // No more receive-completed. USBEVIE |= kUSB_dataReceivedEvent; // This will tell us that data -- any data -- has arrived (i.e., a key-press) USB_setEnabledEvents(USBEVIE); } break; case ST_ENUM_SUSPENDED: __bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/ interrupts _NOP(); break; case ST_ENUM_IN_PROGRESS: break; case ST_NOENUM_SUSPENDED: __bis_SR_register(LPM3_bits + GIE); _NOP(); break; case ST_ERROR: _NOP(); break; default:; } } // while(1) } //main()
/*----------------------------------------------------------------------------+ | Main Routine | +----------------------------------------------------------------------------*/ VOID main(VOID) { BYTE y; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_StartUp(); USB_init(); USB_setEnabledEvents(kUSB_VbusOnEvent+kUSB_VbusOffEvent+kUSB_receiveCompletedEvent +kUSB_dataReceivedEvent+kUSB_UsbSuspendEvent+kUSB_UsbResumeEvent+kUSB_UsbResetEvent); // Check if we're already physically attached to USB, and if so, connect to it // This is the same function that gets called automatically when VBUS gets attached. if (USB_connectionInfo() & kUSB_vbusPresent) USB_handleVbusOnEvent(); // Pre-fill the buffers with visible ASCII characters (0x21 to 0x7E) y=0x21; for(w=0;w<MEGA_DATA_LENGTH;w++) { bufferX[w] = y; bufferY[w] = y++; if(y>0x7E) y = 0x21; } while(1) { switch(USB_connectionState()) { case ST_USB_DISCONNECTED: __bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/interrupt break; case ST_USB_CONNECTED_NO_ENUM: break; case ST_ENUM_ACTIVE: if(!bCDCDataReceived_event) // Do this until a key is pressed { strcpy(pakOutString,"Press any key.\r"); // Prepare the outgoing string if(cdcSendDataInBackground((BYTE*)pakOutString,strlen(pakOutString),0,0)) // Send it { USBCDC_abortSend(&w,0); // Operation probably still open; cancel it break; // The break isn't really necessary since it'll go to the end of the loop anyway, but it's shown for completeness } } else { if(cdcSendDataInBackground((BYTE*)bufferX,MEGA_DATA_LENGTH,0,0)) { bCDCDataReceived_event = FALSE; USBCDC_abortSend(&w,0); // Operation probably still open; cancel it break; } // Between these functions, don't modify bufferX. However, bufferY can be modified. if(cdcSendDataInBackground((BYTE*)bufferY,MEGA_DATA_LENGTH,0,0)) { bCDCDataReceived_event = FALSE; USBCDC_abortSend(&w,0); // Operation probably still open; cancel it break; } // Until the next call to sendData_inBackground(), don't modify bufferY. However, bufferX can be modified. if(rounds++>=500) { strcpy(outString,"\r\n\r\nThe test is completed.\r\n\r\n"); // Prepare the outgoing string if(cdcSendDataInBackground((BYTE*)outString,strlen(outString),0,0)) // Send it { USBCDC_abortSend(&w,0); // Operation may still be open; cancel it break; } bCDCDataReceived_event= FALSE; rounds = 0; USBCDC_rejectData(0); // Reject data from previous keypress, in preparation for another one } // It's been in the USB buffer all this time... } break; case ST_ENUM_SUSPENDED: __bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/interrupt break; case ST_ENUM_IN_PROGRESS: break; case ST_NOENUM_SUSPENDED: __bis_SR_register(LPM3_bits + GIE); _NOP(); break; case ST_ERROR: _NOP(); break; default:; } } // while(1) } //main()
/*----------------------------------------------------------------------------+ | Main Routine | +----------------------------------------------------------------------------*/ VOID main(VOID) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_StartUp(); USB_init(); USB_setEnabledEvents(kUSB_VbusOnEvent+kUSB_VbusOffEvent+kUSB_dataReceivedEvent+kUSB_UsbSuspendEvent+kUSB_UsbResumeEvent+kUSB_UsbResetEvent); // Pre-fill the buffers with visible ASCII characters (0x21 to 0x7E) for(w=0;w<MEGA_DATA_LENGTH;w++) { dataBuf[w] = y++; if(y>0x7E) y = 0x21; } // Check if we're already physically attached to USB, and if so, connect to it // This is the same function that gets called automatically when VBUS gets attached. if (USB_connectionInfo() & kUSB_vbusPresent) USB_handleVbusOnEvent(); while(1) { switch(USB_connectionState()) { case ST_USB_DISCONNECTED: __bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/interrupt break; case ST_USB_CONNECTED_NO_ENUM: break; case ST_ENUM_ACTIVE: if(!bCDCDataReceived_event) // Do this until a key is pressed { strcpy(outString,"Press any key.\r"); // Prepare the outgoing string if(cdcSendDataWaitTilDone((BYTE*)outString,strlen(outString),0,0)) // Send it; no timeout { USBCDC_abortSend(&x,0); // Operation may still be open; cancel it break; } } else { bCDCDataReceived_event = FALSE; USBCDC_rejectData(0); for(rounds=0;rounds<600;rounds++) { if(cdcSendDataWaitTilDone(dataBuf,MEGA_DATA_LENGTH,0,0)) // Send all of RAM { USBCDC_abortSend(&x,0); // Operation probably still open; cancel it break; } } strcpy(outString,"\r\n\r\n\r\nThe test is completed.\r\n"); // Prepare the outgoing string if(cdcSendDataWaitTilDone((BYTE*)outString,strlen(outString),0,0)) // Send it; no timeout { USBCDC_abortSend(&x,0); // Operation may still be open; cancel it break; } } break; case ST_ENUM_SUSPENDED: __bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/interrupt break; case ST_ENUM_IN_PROGRESS: break; case ST_NOENUM_SUSPENDED: __bis_SR_register(LPM3_bits + GIE); _NOP(); break; case ST_ERROR: _NOP(); break; default:; } } // while(1) } //main()