__attribute__((critical)) int16_t usb_printf(const char * fmt, ...) { if(usb_printf_state == USB_DISABLED) return -1; if(usb_printf_state == USB_LOCKED) return -1; usb_printf_state = USB_LOCKED; int16_t len; va_list args; va_start(args, fmt); char msg[64]; len = vsnprintf(msg, 64, fmt, args); cdcSendDataWaitTilDone((BYTE*) msg, len, CDC0_INTFNUM, 1); va_end(args); usb_printf_state = USB_ENABLED; return len; }
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) { 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()