void __attribute__ ((interrupt(UNMI_VECTOR))) UNMI_ISR (void) #else #error Compiler not found! #endif { switch (__even_in_range(SYSUNIV, SYSUNIV_BUSIFG)) { case SYSUNIV_NONE: __no_operation(); break; case SYSUNIV_NMIIFG: __no_operation(); break; case SYSUNIV_OFIFG: UCS_clearFaultFlag(UCS_XT2OFFG); UCS_clearFaultFlag(UCS_DCOFFG); SFR_clearInterrupt(SFR_OSCILLATOR_FAULT_INTERRUPT); break; case SYSUNIV_ACCVIFG: __no_operation(); break; case SYSUNIV_BUSIFG: // If the CPU accesses USB memory while the USB module is // suspended, a "bus error" can occur. This generates an NMI. If // USB is automatically disconnecting in your software, set a // breakpoint here and see if execution hits it. See the // Programmer's Guide for more information. SYSBERRIV = 0; //clear bus error flag USB_disable(); //Disable } }
__interrupt VOID UNMI_ISR (VOID) { switch (__even_in_range(SYSUNIV, SYSUNIV_BUSIFG)) { case SYSUNIV_NONE: __no_operation(); break; case SYSUNIV_NMIIFG: __no_operation(); break; case SYSUNIV_OFIFG: UCSCTL7 &= ~(DCOFFG + XT1LFOFFG + XT2OFFG); //Clear OSC flaut Flags fault flags SFRIFG1 &= ~OFIFG; //Clear OFIFG fault flag break; case SYSUNIV_ACCVIFG: __no_operation(); break; case SYSUNIV_BUSIFG: //If bus error occured - the cleaning of flag and re-initializing of //USB is required. SYSBERRIV = 0; //clear bus error flag USB_disable(); //Disable } }
__interrupt VOID UNMI_ISR(VOID) { switch (__even_in_range(SYSUNIV, SYSUNIV_SYSBUSIV)) { case SYSUNIV_NONE: __no_operation(); break; case SYSUNIV_NMIIFG: __no_operation(); break; case SYSUNIV_OFIFG: UCSCTL7 &= ~(DCOFFG+0+0+0); // Clear OSC fault source flags SFRIFG1 &= ~OFIFG; // Clear OFIFG flag break; case SYSUNIV_ACCVIFG: __no_operation(); break; case SYSUNIV_SYSBUSIV: // In the rare event of an internal system bus error - must clear the flag and re-initialize USB. SYSBERRIV = 0; // Clear flag USB_disable(); // Disable USB if (USB_connectionInfo() & kUSB_vbusPresent) USB_handleVbusOnEvent(); } }
__interrupt VOID UNMI_ISR(VOID) { switch (__even_in_range(SYSUNIV, SYSUNIV_BUSIFG)) { case SYSUNIV_NONE: __no_operation(); break; case SYSUNIV_NMIIFG: __no_operation(); break; case SYSUNIV_OFIFG: UCSCTL7 &= ~(DCOFFG+0+0+0); // Clear OSC flaut Flags fault flags SFRIFG1 &= ~OFIFG; // Clear OFIFG fault flag break; case SYSUNIV_ACCVIFG: __no_operation(); break; case SYSUNIV_BUSIFG: // This indicates a "bus error"; this is catastrophic for the USB connection, which must // now be shut down. Generally this indicates a software bug -- see the Prog. Guide for more detail. SYSBERRIV = 0; // Clear bus error flag USB_disable(); // Disable } }
/* deinitialize USB */ void USB_close() { UDIEN &= ~(_BV(EORSTE) | _BV(SOFE)); usb_current_conf = 0; UDCON |= _BV(DETACH); USB_disable(); USB_set_pads_regulator(false); }
/* If this function gets executed, it's a sign that the output of the USB PLL has failed. returns TRUE to keep CPU awake */ BYTE USB_handleClockEvent() { // Something happened to the PLL. This might also show up in the system as an oscillator fault on XT2. // The USB connection is probably lost. Software should ensure any faults on XT2 are resolved, // then can attempt to call USB_enable()/USB_connect() again. USB_disconnect(); USB_disable(); return TRUE; // Since this event is associated with a change in state, it's a good practice to return TRUE so the main loop can adapt. }
/* If this function gets executed, it indicates that the USB host has chosen to suspend this device after a period of active operation. returns TRUE to keep CPU awake */ BYTE USB_handleSuspendEvent() { // If this device draws power from the host over VBUS, then this event is the signal for software to ensure that // no more than 2.5mA is drawn over VBUS. Code can be placed here to do this, or it can be placed in the main loop // under ST_ENUM_SUSPENDED (but make sure this handler returns TRUE to wake the main loop, if LPM0 was entered). // Or, if the device was attached to a bus situation that's somehow providing VBUS but has no active host (host is in standby, // or attached to a powered hub with no upstream host), then this will show up as a suspend event without successful enumeration. // A good course of action might be to "cancel" the attempt to enumerate: if(!(USB_connectionInfo() & kUSB_Enumerated)) { USB_disconnect(); USB_disable(); } // Now, if this branch executes, the USB state will shift to ST_USB_DISCONNECTED when the function returns. return TRUE; // Return TRUE so that the main loop can adapt. }
/* * ======== 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; } } }
void MassStorage(void) { buttonsPressed = 0; SFRIE1 &= ~OFIE; disk_initialize(0); // Initialize Disk Drive #0 SFRIE1 |= OFIE; DEBUG("Init clock\r\n"); ClockUSB(); DEBUG("Init USB\r\n"); USB_init(); // Initialize the USB module P1OUT |= BIT1; // Enable all USB events USB_setEnabledEvents(kUSB_allUsbEvents); // Clal Initialization Function DEBUG("Init MSC\r\n"); msc_Init(); P1OUT |= BIT2; // If USB is already connected when the program starts up, then there won't be a // USB_handleVbusOnEvent(). // So we need to check for it, and manually connect if the host is already present. if (USB_connectionInfo() & kUSB_vbusPresent) { if (USB_enable() == kUSB_succeed) { USB_reset(); USB_connect(); P1OUT |= BIT3; } } while (1) { switch (USB_connectionState()) { DEBUG("Connection state: %u\r\n", USB_connectionState()); case ST_USB_DISCONNECTED: //__bis_SR_register(LPM3_bits + GIE); // Enter LPM3 until VBUS-on event _NOP(); break; case ST_USB_CONNECTED_NO_ENUM: break; case ST_ENUM_ACTIVE: msc_Loop(); break; case ST_ENUM_SUSPENDED: //__bis_SR_register(LPM3_bits + GIE); // Enter LPM3, until a resume or VBUS-off // event break; case ST_ENUM_IN_PROGRESS: break; case ST_ERROR: break; default:; } } DEBUG("Done with MassStorage\r\n"); buttonsPressed = 0; Board_ledOff(LED_ALL); USB_disable(); SFRIE1 &= ~OFIE; Init_FLL_Settle(25000, 762); // Return to normal clock settings SFRIE1 |= OFIE; }