Lab 7d – Creating a CDC Push Button App
8. Add global variables.
char pbStr[5] = ""; // Stores the string to send
volatile unsigned short usiButton1 = 0; // Stores the button state
9. Add additional setup code.
GPIO_setAsOutputPin( GPIO_PORT_P4, GPIO_PIN7 );
GPIO_setAsInputPinWithPullUpresistor( GPIO_PORT_P2, GPIO_PIN1 );
initTimers();
10. Add code to ST_ENUM_ACTIVE state.
// If USB is present, sent the button state to host. Flag set every sec
if (bSend)
{
bSend = FALSE;
usiButton1 = GPIO_getInputPinValue ( GPIO_PORT_P2, GPIO_PIN1 );
if ( usiButton1 == GPIO_INPUT_PIN_LOW ) {
// If button is down, turn on LED
GPIO_setOutputHighOnPin( GPIO_PORT_P4, GPIO_PIN7 );
pbStr[0] = 'D';
pbStr[1] = 'O';
pbStr[2] = 'W';
pbStr[3] = 'N';
pbStr[4] = '\n';
}
else {
// If button is up, turn off LED
GPIO_setOutputLowOnPin( GPIO_PORT_P4, GPIO_PIN7 );
pbStr[0] = 'U';
pbStr[1] = 'P';
pbStr[2] = ' ';
pbStr[3] = ' ';
pbStr[4] = '\n';
}
// This function begins the USB send operation, and immediately
// returns, while the sending happens in the background.
// Send pbStr, 5 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 (cdcSendDataInBackground((BYTE*)pbStr, 5, 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
}
11. Add #include "USB_app/usbConstructs.h".
We need to use this header file since it supports the cdcSendDataInBackground() function
we are using to send data via USB.
12. Build and launch debugger. Then run the program.
MSP430 Workshop - USB Devices 7 - 45