uint16_t SendBuffer(char *buf,uint16_t length ) { uint8_t ReceiveError = 0, SendError = 0; uint16_t count; // Check the USB state and directly main loop accordingly switch (USB_getConnectionState()) { // This case is executed while your device is enumerated on the // USB host case ST_ENUM_ACTIVE: if (USBCDC_sendDataInBackground((uint8_t*)buf, length, CDC0_INTFNUM, 1)) { SendError = 0x01; break; } break; default:; } if (ReceiveError || SendError){ // TO DO: User can place code here to handle error } return 0; }
/* * ======== main ======== */ void main(void) { WDT_A_hold(WDT_A_BASE); //Stop watchdog timer // Minimum Vcore setting required for the USB API is PMM_CORE_LEVEL_2 PMM_setVCore(PMM_CORE_LEVEL_2); USBHAL_initPorts(); // Config GPIOS for low-power (output low) USBHAL_initClocks(8000000); // Config clocks. MCLK=SMCLK=FLL=8MHz; ACLK=REFO=32kHz USB_setup(TRUE,TRUE); // Init USB & events; if a host is present, connect initRTC(); // Start the real-time clock __enable_interrupt(); // Enable interrupts globally int prevLeft = getLeft(); int prevRight = getRight(); int left; int right; while (1) { // Enter LPM0, which keeps the DCO/FLL active but shuts off the // CPU. For USB, you can't go below LPM0! __bis_SR_register(LPM0_bits + GIE); // If USB is present, sent the time to the host. Flag is set every sec left = getLeft(); right = getRight(); timeStr[0] = '['; itoa(left, &timeStr[1], 10); itoa(right, &timeStr[2], 10); timeStr[3] = ']'; timeStr[4] = '\n'; timeStr[5] = '\0'; // // if (bSendTimeToHost) // { // bSendTimeToHost = FALSE; // convertTimeBinToASCII(timeStr); // This function begins the USB send operation, and immediately // returns, while the sending happens in the background. // Send timeStr, 9 bytes, to intf #0 (which is enumerated as a // COM port). 1000 retries. (Retries will be attempted if the // previous send hasn't completed yet). If the bus isn't present, // it simply returns and does nothing. if (USBCDC_sendDataInBackground(timeStr, 6, CDC0_INTFNUM, 1000)) { _NOP(); // If it fails, it'll end up here. Could happen if // the cable was detached after the connectionState() } // check, or if somehow the retries failed // } } //while(1) } //main()
uint16_t checkUSB(char * buf) { uint8_t ReceiveError = 0, SendError = 0; uint16_t count; // Check the USB state and directly main loop accordingly switch (USB_getConnectionState()) { // This case is executed while your device is enumerated on the // USB host case ST_ENUM_ACTIVE: if (bCDCDataReceived_event){ // Clear flag early -- just in case execution breaks // below because of an error bCDCDataReceived_event = FALSE; count = USBCDC_receiveDataInBuffer((uint8_t*)buf, BUFFER_SIZE, CDC0_INTFNUM); // Count has the number of bytes received into dataBuffer // Echo back to the host. if (USBCDC_sendDataInBackground((uint8_t*)buf, count, CDC0_INTFNUM, 1)){ // Exit if something went wrong. SendError = 0x01; break; } } break; default:; } if (ReceiveError || SendError){ // TO DO: User can place code here to handle error } }