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; }
uint16_t ReceiveBuffer(char *buf) { uint8_t ReceiveError = 0, SendError = 0; uint16_t count; // Check the USB state and directly main loop accordingly switch (USB_getConnectionState()) { 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); } break; default:; } if (ReceiveError || SendError){ // TO DO: User can place code here to handle error } return count; }
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 } }
/* * ======== main ======== */ int main (void) { WDT_A_hold(WDT_A_BASE); // Stop watchdog timer // Minumum 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(MCLK_FREQUENCY); // Config clocks. MCLK=SMCLK=FLL=MCLK_FREQUENCY; ACLK=REFO=32kHz hal_sd_pwr_on(); initTimer(); USB_setup(FALSE, TRUE); // Init USB & events; if a host is present, connect __enable_interrupt(); // Enable interrupts globally // GPS_init(); // state machine while (1) { switch( state ) { case sIDLE: hal_led_a(0); hal_led_b(0); hal_gps_pwr_off(); hal_sd_pwr_off(); UCS_turnOffXT2(); /* USB connected */ if(USB_getConnectionInformation() & USB_VBUS_PRESENT) { hal_led_a(CYAN); //PMM_setVCore(PMM_CORE_LEVEL_2); hal_sd_pwr_on(); shortDelay(); USBMSC_initMSC(); // Initialize MSC API, and report media to the host if (USB_enable() == USB_SUCCEED){ state = sUSB; hal_led_a(GREEN); //hal_sd_pwr_on(); //detectCard(); USB_reset(); USB_connect(); //generate rising edge on DP -> the host enumerates our device as full speed device } break; // don't enter sleep } /* start GPS */ if(hal_button_event()) { /* delay for starting */ hal_led_a(RED); uint8_t timeout = 16; while( hal_button_status() == 1 && --timeout ) { shortDelay(); } hal_led_a(0); if( hal_button_status() == 0 ) break; state = sGPS; hal_led_a(CYAN); hal_sd_pwr_on(); timeout = 8; while( --timeout ) { shortDelay(); } detectCard(); Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE); gps_start(); hal_button_event(); break; // don't enter sleep } USB_disable(); //Disable hal_gps_rtc_on(); // saves around 7uA Timer_A_stop(TIMER_A0_BASE); //UCS_turnOffSMCLK(); //PMM_setVCore(PMM_CORE_LEVEL_0); __bis_SR_register(LPM4_bits + GIE); _NOP(); //UCS_turnOnSMCLK(); //PMM_setVCore(PMM_CORE_LEVEL_2); break; case sGPS: /* stop GPS */ if((USB_getConnectionInformation() & USB_VBUS_PRESENT)) { state = sIDLE; gps_stop(); break; } if(hal_button_event()) { /* delay for stopping */ uint8_t timeout = 16; while( hal_button_status() == 1 && --timeout ) { hal_led_a(RED); shortDelay(); hal_led_a(RED); } hal_led_a(0); if( hal_button_status() == 0 ) break; state = sIDLE; gps_stop(); break; } if (bDetectCard){ USBMSC_checkMSCInsertionRemoval(); // Clear the flag, until the next timer ISR bDetectCard = 0x00; } while( gps_check() ) { gps_do(); } __bis_SR_register(LPM0_bits + GIE); _NOP(); break; case sUSB: if(!(USB_getConnectionInformation() & USB_VBUS_PRESENT)) { state = sIDLE; break; } /* check state of chareger? */ if( hal_charge_status()) hal_led_b(RED); else hal_led_b(GREEN); hal_button_event(); // clear button event switch (USB_getConnectionState()) { case ST_ENUM_ACTIVE: USBMSC_processMSCBuffer(); // Handle READ/WRITE cmds from the host // Every second, the Timer_A ISR sets this flag. The // checking can't be done from within the timer ISR, because it // enables interrupts, and this is not a recommended // practice due to the risk of nested interrupts. if (bDetectCard){ USBMSC_checkMSCInsertionRemoval(); // Clear the flag, until the next timer ISR bDetectCard = 0x00; } break; // These cases are executed while your device is disconnected from // the host (meaning, not enumerated); enumerated but suspended // by the host, or connected to a powered hub without a USB host // present. case ST_PHYS_DISCONNECTED: case ST_ENUM_SUSPENDED: case ST_PHYS_CONNECTED_NOENUM_SUSP: hal_led_a(BLUE); //state = sIDLE; break; // The default is executed for the momentary state // ST_ENUM_IN_PROGRESS. Usually, this state only last a few // seconds. Be sure not to enter LPM3 in this state; USB // communication is taking place here, and therefore the mode must // be LPM0 or active-CPU. case ST_ENUM_IN_PROGRESS: default:; } break; } } }