// (c)2009 by Texas Instruments Incorporated, All Rights Reserved. /*----------------------------------------------------------------------------+ | | | Texas Instruments | | | | MSP430 USB-Example (CDC/HID Driver) | | | +-----------------------------------------------------------------------------+ | Source: main.c, v1.19 2009/19/11 | ------------------------------------------------------------------------------+ LED Control Demo: This USB demo example is to be used with a PC application (e.g. HyperTerminal) This demo application is used to control the operation of the LED at P1.0 Typing the following pharses in the HyperTerminal Window does the following 1. "LED ON" Turns on the LED and returns "LED is ON" phrase to PC 2. "LED OFF" Turns off the LED and returns "LED is OFF" back to HyperTerminal 3. "LED TOGGLE - SLOW" Turns on the timer used to toggle LED with a large period and returns "LED is toggling slowly" phrase back to HyperTerminal 4. "LED TOGGLE - FAST" Turns on the timer used to toggle LED with a smaller period and returns "LED is toggling fast" phrase back to HyperTerminal +----------------------------------------------------------------------------+ Please refer to "USB CDC API Demo Examples Guide.pdf" for more details //----------------------------------------------------------------------------*/ #include "Common\device.h" #include "Common\types.h" // Basic Type declarations #include ".\USB_Common\descriptors.h" #include "USB_Common\usb.h" // USB-specific functions #include "main.h" #include "Common\hal_UCS.h" #include "Common\hal_pmm.h" #ifdef _CDC_ #include "USB_CDC_API\UsbCdc.h" #endif #ifdef _HID_ #include "USB_HID_API\UsbHid.h" #endif #include #include #include "USBCDC_constructs.h" #include "main.h" volatile BYTE bDataReceived_event = FALSE; // Indicates data has been received without an open rcv operation volatile BYTE bDataReceiveCompleted_event = FALSE; // data receive completed event volatile BYTE bDataSendCompleted_event = FALSE; // data send completed event #define MAX_STR_LENGTH 64 char wholeString[MAX_STR_LENGTH] = ""; // The entire input string from the last 'return' unsigned int SlowToggle_Period = 20000-1; unsigned int FastToggle_Period = 1000-1; /*----------------------------------------------------------------------------+ | Main Routine | +----------------------------------------------------------------------------*/ VOID main(VOID) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer Init_StartUp(); //initialize device Init_TimerA1(); 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_handleVbusOnEvent(); 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(bDataReceived_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 receiveDataInBuffer((BYTE*)pieceOfString,MAX_STR_LENGTH,1); // Get the next piece of the string strcat(wholeString,pieceOfString); // Add it to the whole sendData_inBackground((BYTE*)pieceOfString,strlen(pieceOfString),1,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 sendData_inBackground((BYTE*)outString,strlen(outString),1,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 sendData_inBackground((BYTE*)outString,strlen(outString),1,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 sendData_inBackground((BYTE*)outString,strlen(outString),1,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 sendData_inBackground((BYTE*)outString,strlen(outString),1,0); // Send the response over USB } else // Handle other { strcpy(outString,"\r\nNo such command!\r\n\r\n"); // Prepare the outgoing string sendData_inBackground((BYTE*)outString,strlen(outString),1,0); // Send the response over USB } for(i=0;i