I found the code below from some forum to use my board as a PC mouse.
I loaded the program in the microcontroller. But it doesn't function as a mouse. The program doesn't get USB_EVENT_CONNECTED. What am I missing? Please give detail information as I am a beginner. Also, I don't have proper information about which pins to enable.
///////////////THIS IS mouse_description.h file/////////////////////////////////
extern unsigned long MouseHandler(void *pvCBData,
unsigned long ulEvent,
unsigned long ulMsgData,
void *pvMsgData);
extern const tUSBDHIDMouseDevice g_sMouseDevice;
//*****************************************************************************
//
// The languages supported by this device.
//
//*****************************************************************************
const unsigned char g_pLangDescriptor[] =
{
4,
USB_DTYPE_STRING,
USBShort(USB_LANG_EN_US)
};
//*****************************************************************************
//
// The manufacturer string.
//
//*****************************************************************************
const unsigned char g_pManufacturerString[] =
{
(17 + 1) * 2,
USB_DTYPE_STRING,
'T', 0, 'e', 0, 'x', 0, 'a', 0, 's', 0, ' ', 0, 'I', 0, 'n', 0, 's', 0,
't', 0, 'r', 0, 'u', 0, 'm', 0, 'e', 0, 'n', 0, 't', 0, 's', 0,
};
//*****************************************************************************
//
// The product string.
//
//*****************************************************************************
const unsigned char g_pProductString[] =
{
(13 + 1) * 2,
USB_DTYPE_STRING,
'M', 0, 'o', 0, 'u', 0, 's', 0, 'e', 0, ' ', 0, 'E', 0, 'x', 0, 'a', 0,
'm', 0, 'p', 0, 'l', 0, 'e', 0
};
//*****************************************************************************
//
// The serial number string.
//
//*****************************************************************************
const unsigned char g_pSerialNumberString[] =
{
(8 + 1) * 2,
USB_DTYPE_STRING,
'1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0, '8', 0
};
//*****************************************************************************
//
// The interface description string.
//
//*****************************************************************************
const unsigned char g_pHIDInterfaceString[] =
{
(19 + 1) * 2,
USB_DTYPE_STRING,
'H', 0, 'I', 0, 'D', 0, ' ', 0, 'M', 0, 'o', 0, 'u', 0, 's', 0,
'e', 0, ' ', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0,
'a', 0, 'c', 0, 'e', 0
};
//*****************************************************************************
//
// The configuration description string.
//
//*****************************************************************************
const unsigned char g_pConfigString[] =
{
(23 + 1) * 2,
USB_DTYPE_STRING,
'H', 0, 'I', 0, 'D', 0, ' ', 0, 'M', 0, 'o', 0, 'u', 0, 's', 0,
'e', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, 'f', 0, 'i', 0, 'g', 0,
'u', 0, 'r', 0, 'a', 0, 't', 0, 'i', 0, 'o', 0, 'n', 0
};
//*****************************************************************************
//
// The descriptor string table.
//
//*****************************************************************************
const unsigned char * const g_pStringDescriptors[] =
{
g_pLangDescriptor,
g_pManufacturerString,
g_pProductString,
g_pSerialNumberString,
g_pHIDInterfaceString,
g_pConfigString
};
#define NUM_STRING_DESCRIPTORS (sizeof(g_pStringDescriptors) / \
sizeof(unsigned char *))
//*****************************************************************************
//
// The HID mouse device initialization and customization structures.
//
//*****************************************************************************
tHIDMouseInstance g_sMouseInstance;
const tUSBDHIDMouseDevice g_sMouseDevice =
{
USB_VID_STELLARIS,
USB_PID_MOUSE,
500,
USB_CONF_ATTR_SELF_PWR | USB_CONF_ATTR_RWAKE,
MouseHandler,
(void *)&g_sMouseDevice,
g_pStringDescriptors,
NUM_STRING_DESCRIPTORS,
&g_sMouseInstance
};
//////////////////////////////////////////////////////////////////////
/////////////////this is main.c file////////////////////////////////////////////
#include "mouse_description.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>USB HID Mouse Device (usb_dev_mouse)</h1>
//!
//! This example application turns the evaluation board into a USB mouse
//! supporting the Human Interface Device class. The mouse pointer will move
//! in a square pattern for the duration of the time it is plugged in.
//!
//! UART0, connected to the FTDI virtual COM port and running at 115,200,
//! 8-N-1, is used to display messages from this application.
//
//*****************************************************************************
//*****************************************************************************
//
// The incremental update for the mouse.
//
//*****************************************************************************
#define MOUSE_MOVE_INC 1
#define MOUSE_MOVE_DEC -1
//*****************************************************************************
//
// The system tick timer rate.
//
//*****************************************************************************
#define SYSTICKS_PER_SECOND 100
#define MS_PER_SYSTICK (1000 / SYSTICKS_PER_SECOND)
//*****************************************************************************
//
// Holds command bits used to signal the main loop to perform various tasks.
//
//*****************************************************************************
volatile unsigned long g_ulCommands;
#define TICK_EVENT 0
//*****************************************************************************
//
// A flag used to indicate whether or not we are currently connected to the USB
// host.
//
//*****************************************************************************
volatile tBoolean g_bConnected;
volatile tBoolean g_bSuspended;
//*****************************************************************************
//
// Global system tick counter holds elapsed time since the application started
// expressed in 100ths of a second.
//
//*****************************************************************************
volatile unsigned long g_ulSysTickCount;
//*****************************************************************************
//
// The number of system ticks to wait for each USB packet to be sent before
// we assume the host has disconnected. The value 50 equates to half a second.
//
//*****************************************************************************
#define MAX_SEND_DELAY 50
//*****************************************************************************
//
// This enumeration holds the various states that the mouse can be in during
// normal operation.
//
//*****************************************************************************
volatile enum
{
//
// Unconfigured.
//
MOUSE_STATE_UNCONFIGURED,
//
// No keys to send and not waiting on data.
//
MOUSE_STATE_IDLE,
//
// Waiting on data to be sent out.
//
MOUSE_STATE_SENDING
}
g_eMouseState = MOUSE_STATE_UNCONFIGURED;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
//
// This function handles notification messages from the mouse device driver.
//
//*****************************************************************************
unsigned long
MouseHandler(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgData, void *pvMsgData)
{
switch(ulEvent)
{
//
// The USB host has connected to and configured the device.
//
case USB_EVENT_CONNECTED:
{
g_eMouseState = MOUSE_STATE_IDLE;
g_bConnected = true;
break;
}
//
// The USB host has disconnected from the device.
//
case USB_EVENT_DISCONNECTED:
{
g_bConnected = false;
g_eMouseState = MOUSE_STATE_UNCONFIGURED;
break;
}
//
// A report was sent to the host. We are not free to send another.
//
case USB_EVENT_TX_COMPLETE:
{
g_eMouseState = MOUSE_STATE_IDLE;
break;
}
case USB_EVENT_SUSPEND:
{
g_bSuspended = true;
break;
}
}
return(0);
}
//***************************************************************************
//
// Wait for a period of time for the state to become idle.
//
// \param ulTimeoutTick is the number of system ticks to wait before
// declaring a timeout and returning \b false.
//
// This function polls the current keyboard state for ulTimeoutTicks system
// ticks waiting for it to become idle. If the state becomes idle, the
// function returns true. If it ulTimeoutTicks occur prior to the state
// becoming idle, false is returned to indicate a timeout.
//
// \return Returns \b true on success or \b false on timeout.
//
//***************************************************************************
tBoolean
WaitForSendIdle(unsigned long ulTimeoutTicks)
{
unsigned long ulStart;
unsigned long ulNow;
unsigned long ulElapsed;
ulStart = g_ulSysTickCount;
ulElapsed = 0;
g_bConnected=false;
g_bSuspended=false;
while(ulElapsed < ulTimeoutTicks)
{
//
// Is the mouse is idle, return immediately.
//
if(g_eMouseState == MOUSE_STATE_IDLE)
{
return(true);
}
//
// Determine how much time has elapsed since we started waiting. This
// should be safe across a wrap of g_ulSysTickCount.
//
ulNow = g_ulSysTickCount;
ulElapsed = ((ulStart < ulNow) ? (ulNow - ulStart) :
(((unsigned long)0xFFFFFFFF - ulStart) + ulNow + 1));
}
//
// If we get here, we timed out so return a bad return code to let the
// caller know.
//
return(false);
}
//*****************************************************************************
//
// This function provides simulated movements of the mouse.
//
//*****************************************************************************
void
MoveHandler(void)
{
unsigned long ulRetcode;
char cDeltaX, cDeltaY;
//
// Determine the direction to move the mouse.
//
ulRetcode = g_ulSysTickCount % (4 * SYSTICKS_PER_SECOND);
if(ulRetcode < SYSTICKS_PER_SECOND)
{
cDeltaX = MOUSE_MOVE_INC;
cDeltaY = 0;
}
else if(ulRetcode < (2 * SYSTICKS_PER_SECOND))
{
cDeltaX = 0;
cDeltaY = MOUSE_MOVE_INC;
}
else if(ulRetcode < (3 * SYSTICKS_PER_SECOND))
{
cDeltaX = (char)MOUSE_MOVE_DEC;
cDeltaY = 0;
}
else
{
cDeltaX = 0;
cDeltaY = (char)MOUSE_MOVE_DEC;
}
//
// Tell the HID driver to send this new report.
//
g_eMouseState = MOUSE_STATE_SENDING;
ulRetcode = USBDHIDMouseStateChange((void *)&g_sMouseDevice, cDeltaX,
cDeltaY, 0);
//
// Did we schedule the report for transmission?
//
if(ulRetcode == MOUSE_SUCCESS)
{
//
// Wait for the host to acknowledge the transmission if all went well.
//
if(!WaitForSendIdle(MAX_SEND_DELAY))
{
//
// The transmission failed, so assume the host disconnected and go
// back to waiting for a new connection.
//
g_bConnected = false;
}
}
}
//*****************************************************************************
//
// This is the interrupt handler for the SysTick interrupt. It is called
// periodically and updates a global tick counter then sets a flag to tell the
// main loop to move the mouse.
//
//*****************************************************************************
void
SysTickIntHandler(void)
{
g_ulSysTickCount++;
HWREGBITW(&g_ulCommands, TICK_EVENT) = 1;
}
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
int
main(void)
{
//
// Set the clocking to run from the PLL at 50MHz.
//
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
/*
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
GPIOPinConfigure(GPIO_PG4_USB0EPEN);
GPIOPinTypeUSBDigital(GPIO_PORTG_BASE, GPIO_PIN_4);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL);
GPIOPinTypeUSBAnalog(GPIO_PORTL_BASE, GPIO_PIN_6 | GPIO_PIN_7);
GPIOPinTypeUSBAnalog(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
*/
//
//
// Set the system tick to fire 100 times per second.
//
SysTickPeriodSet(SysCtlClockGet() / SYSTICKS_PER_SECOND);
SysTickIntEnable();
SysTickEnable();
//
// Pass the USB library our device information, initialize the USB
// controller and connect the device to the bus.
//
USBDHIDMouseInit(0, (tUSBDHIDMouseDevice *)&g_sMouseDevice);
USBDHIDMouseRemoteWakeupRequest((void *)&g_sMouseDevice);
//
// Drop into the main loop.
//
while(1)
{
//
// Wait for USB configuration to complete.
//
while(!g_bConnected)
{
}
//
// Now keep processing the mouse as long as the host is connected.
//
while(g_bConnected)
{
//
// If it is time to move the mouse then do so.
//
if(HWREGBITW(&g_ulCommands, TICK_EVENT) == 1)
{
HWREGBITW(&g_ulCommands, TICK_EVENT) = 0;
MoveHandler();
}
}
}
}
//////////////////////////////