__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(); } }
void usb_printf_init(void) { SetVCore(3); init_clock(); //Init USB USB_init(); //Enable various USB event handling routines USB_setEnabledEvents(kUSB_VbusOnEvent + kUSB_VbusOffEvent + kUSB_receiveCompletedEvent + kUSB_dataReceivedEvent + kUSB_UsbSuspendEvent + kUSB_UsbResumeEvent + kUSB_UsbResetEvent); // 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_printf_state = USB_ENABLED; USB_handleVbusOnEvent(); } }
/** * Configuration de l'USB et de ses Events */ void iUSB::config() { //Ne pas oublier d'avoir setVcore(3) avant //initialisation de l'USB this->initUSB(); //Enable various USB event handling routines USB_setEnabledEvents( kUSB_VbusOnEvent + kUSB_VbusOffEvent + kUSB_receiveCompletedEvent + kUSB_dataReceivedEvent + kUSB_UsbSuspendEvent + kUSB_UsbResumeEvent + kUSB_UsbResetEvent); //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(); } //desactiver LDO USBPWRCTL &= ~(SLDOEN); // Disable the VUSB LDO and the SLDO }
__interrupt VOID iUsbInterruptHandler(VOID) { BYTE bWakeUp = FALSE; //Check if the setup interrupt is pending. //We need to check it before other interrupts, //to work around that the Setup Int has lower priority then Input Endpoint 0 if (USBIFG & SETUPIFG) { bWakeUp = SetupPacketInterruptHandler(); USBIFG &= ~SETUPIFG; // clear the interrupt bit } switch (__even_in_range(USBVECINT & 0x3f, USBVECINT_OUTPUT_ENDPOINT7)) { case USBVECINT_NONE: break; case USBVECINT_PWR_DROP: __no_operation(); break; case USBVECINT_PLL_LOCK: break; case USBVECINT_PLL_SIGNAL: break; case USBVECINT_PLL_RANGE: if (wUsbEventMask & kUSB_clockFaultEvent) { bWakeUp = USB_handleClockEvent(); } break; case USBVECINT_PWR_VBUSOn: PWRVBUSonHandler(); if (wUsbEventMask & kUSB_VbusOnEvent) { bWakeUp = USB_handleVbusOnEvent(); } break; case USBVECINT_PWR_VBUSOff: PWRVBUSoffHandler(); if (wUsbEventMask & kUSB_VbusOffEvent) { bWakeUp = USB_handleVbusOffEvent(); } break; case USBVECINT_USB_TIMESTAMP: break; case USBVECINT_INPUT_ENDPOINT0: IEP0InterruptHandler(); break; case USBVECINT_OUTPUT_ENDPOINT0: OEP0InterruptHandler(); break; case USBVECINT_RSTR: USB_reset(); if (wUsbEventMask & kUSB_UsbResetEvent) { bWakeUp = USB_handleResetEvent(); } break; case USBVECINT_SUSR: USB_suspend(); if (wUsbEventMask & kUSB_UsbSuspendEvent) { bWakeUp = USB_handleSuspendEvent(); } break; case USBVECINT_RESR: USB_resume(); if (wUsbEventMask & kUSB_UsbResumeEvent) { bWakeUp = USB_handleResumeEvent(); } //-- after resume we will wake up! Independ what event handler says. bWakeUp = TRUE; break; case USBVECINT_SETUP_PACKET_RECEIVED: // NAK both IEP and OEP enpoints tEndPoint0DescriptorBlock.bIEPBCNT = EPBCNT_NAK; tEndPoint0DescriptorBlock.bOEPBCNT = EPBCNT_NAK; SetupPacketInterruptHandler(); break; case USBVECINT_STPOW_PACKET_RECEIVED: break; case USBVECINT_INPUT_ENDPOINT1: //send saved bytes from buffer... bWakeUp = HidToHostFromBuffer(HID0_INTFNUM); break; case USBVECINT_INPUT_ENDPOINT2: break; case USBVECINT_INPUT_ENDPOINT3: break; case USBVECINT_INPUT_ENDPOINT4: break; case USBVECINT_INPUT_ENDPOINT5: break; case USBVECINT_INPUT_ENDPOINT6: break; case USBVECINT_INPUT_ENDPOINT7: break; case USBVECINT_OUTPUT_ENDPOINT1: //call callback function if no receive operation is underway if (!HidIsReceiveInProgress(HID0_INTFNUM)) { if (wUsbEventMask & kUSB_dataReceivedEvent) { bWakeUp = USBHID_handleDataReceived(HID0_INTFNUM); } } else { //complete receive opereation - copy data to user buffer bWakeUp = HidToBufferFromHost(HID0_INTFNUM); } break; case USBVECINT_OUTPUT_ENDPOINT2: break; case USBVECINT_OUTPUT_ENDPOINT3: break; case USBVECINT_OUTPUT_ENDPOINT4: break; case USBVECINT_OUTPUT_ENDPOINT5: break; case USBVECINT_OUTPUT_ENDPOINT6: break; case USBVECINT_OUTPUT_ENDPOINT7: break; default: break; } if (bWakeUp) { __bic_SR_register_on_exit(LPM3_bits); // Exit LPM0-3 __no_operation(); // Required for debugger } }
/* * ======== main ======== */ VOID main (VOID) { WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer Init_Ports(); //Init ports (do first ports because clocks do change ports) SetVCore(3); Init_Clock(); //Init clocks USB_init(); //Init USB Init_TimerA1(); //Enable various USB event handling routines USB_setEnabledEvents( kUSB_VbusOnEvent + kUSB_VbusOffEvent + kUSB_receiveCompletedEvent + kUSB_dataReceivedEvent + kUSB_UsbSuspendEvent + kUSB_UsbResumeEvent + kUSB_UsbResetEvent); //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(); } __enable_interrupt(); //Enable interrupts globally while (1) { BYTE i; //Check the USB state and directly main loop accordingly switch (USB_connectionState()) { case ST_USB_DISCONNECTED: __bis_SR_register(LPM3_bits + GIE); //Enter LPM3 w/ interrupts enabled _NOP(); //For Debugger break; case ST_USB_CONNECTED_NO_ENUM: break; case ST_ENUM_ACTIVE: __bis_SR_register(LPM0_bits + GIE); //Enter LPM0 (can't do LPM3 when active) _NOP(); //For Debugger //Exit LPM on USB receive and perform a receive //operation if (bCDCDataReceived_event){ //Some data is in the buffer; begin receiving a //command char pieceOfString[MAX_STR_LENGTH] = ""; //Holds the new addition to the string char outString[MAX_STR_LENGTH] = ""; //Holds the outgoing string //Add bytes in USB buffer to theCommand cdcReceiveDataInBuffer((BYTE*)pieceOfString, MAX_STR_LENGTH, CDC0_INTFNUM); //Get the next piece of the string strcat(wholeString,pieceOfString); cdcSendDataInBackground((BYTE*)pieceOfString, strlen(pieceOfString),CDC0_INTFNUM,0); //Echoes back the characters received (needed //for Hyperterm) if (retInString(wholeString)){ //Has the user pressed return yet? if (!(strcmp(wholeString, "LED ON"))){ //Compare to string #1, and respond TA1CTL &= ~MC_1; //Turn off Timer P1OUT |= BIT0; //Turn on LED P1.0 strcpy(outString,"\r\nLED is ON\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } else if (!(strcmp(wholeString, "LED OFF"))){ //Compare to string #2, and respond TA1CTL &= ~MC_1; //Turn off Timer P1OUT &= ~BIT0; //Turn off LED P1.0 strcpy(outString,"\r\nLED is OFF\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } else if (!(strcmp(wholeString, "LED TOGGLE - SLOW"))){ //Compare to string #3, and respond TA1CTL &= ~MC_1; //Turn off Timer TA1CCR0 = SlowToggle_Period; //Set Timer Period for slow LED toggle TA1CTL |= MC_1; //Start Timer strcpy(outString, "\r\nLED is toggling slowly\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } else if (!(strcmp(wholeString, "LED TOGGLE - FAST"))){ //Compare to string #4, and respond TA1CTL &= ~MC_1; //Turn off Timer TA1CCR0 = FastToggle_Period; //Set Timer Period for fast LED toggle TA1CTL |= MC_1; strcpy(outString,"\r\nLED is toggling fast\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } else { //Handle other strcpy(outString,"\r\nNo such command!\r\n\r\n"); //Prepare the outgoing string cdcSendDataInBackground((BYTE*)outString, strlen(outString),CDC0_INTFNUM,0); //Send the response over USB } for (i = 0; i < MAX_STR_LENGTH; i++){ //Clear the string in preparation for the next //one wholeString[i] = 0x00; } } bCDCDataReceived_event = FALSE; } break; case ST_ENUM_SUSPENDED: P1OUT &= ~BIT0; //When suspended, turn off LED __bis_SR_register(LPM3_bits + GIE); //Enter LPM3 w/ interrupts _NOP(); break; case ST_ENUM_IN_PROGRESS: break; case ST_NOENUM_SUSPENDED: P1OUT &= ~BIT0; __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_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) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_StartUp(); USB_init(); USB_setEnabledEvents(kUSB_VbusOnEvent + kUSB_VbusOffEvent + kUSB_UsbSuspendEvent + kUSB_UsbResumeEvent + kUSB_dataReceivedEvent); // 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) for(x=0;x<BUF_SIZE;x++) { dataBuf[x] = 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(!bHIDDataReceived_event) // Do this until a key is pressed { strcpy(outString,"Press any key.\r"); // Prepare the outgoing string if(hidSendDataWaitTilDone((BYTE*)outString,strlen(outString),0,0)) // Send it; no timeout { USBHID_abortSend(&x,0); // Operation may still be open; cancel it break; } } else { bHIDDataReceived_event = FALSE; USBHID_rejectData(0); for(rounds=0;rounds<300;rounds++) { if(hidSendDataWaitTilDone(dataBuf,BUF_SIZE,0,0)) // Send all of RAM { USBHID_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(hidSendDataWaitTilDone((BYTE*)outString,strlen(outString),0,0)) // Send it; no timeout { USBHID_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); 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()