//##############################################################################
//
// FILE: usb_ex2_dev_mouse.c
//
// TITLE: Main routines for the USB HID mouse example.
//
//! \addtogroup driver_example_list
//! <h1> USB HID Mouse Device </h1>
//!
//! This example application turns the evaluation board into a USB mouse
//! supporting the Human Interface Device class. After loading and running the
//! example simply connect the PC to the controlCARDs microUSB port using a USB
//! cable, and the mouse pointer will move in a square pattern for the duration
//! of the time it is plugged in.
//!
//! SCIA, connected to the FTDI virtual COM port and running at 115200,
//! 8-N-1, is used to display messages from this application.
//!
//
#include "driverlib.h"
#include "device.h"
#include "usb_hal.h"
#include <usblib.h>
#include <usbhid.h>
#include <device/usbdevice.h>
#include <device/usbdhid.h>
#include <device/usbdhidmouse.h>
#include <stdint.h>
#include <stdbool.h>
#include <inc/hw_types.h>
#include <usb_ids.h>
#include <wchar.h>
static uint32_t MouseHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgData, void *pvMsgData);
struct StringDescs{
uint8_t * data[6];
uint8_t buffer[256];
StringDescs() { // Statischer Konstruktor
const wchar_t strings[]=
L"\u0109\0"
L"Texas Instruments\0"
L"Mouse Example\0"
L"12345678\0"
L"HID Mouse Interface\0"
L"HID Mouse Configuration\0";
//Kette nullterminierter Strings in Kette aus String-Deskriptoren übersetzen
const wchar_t*s=strings;
uint8_t*d=buffer;
for(size_t i=0; i<6; i++) {
data[i]=d; // Anfangsadresse setzen
int l=wcslen(s);
*d++=2+l*2; // Deskriptor-Länge
*d++=3; // Deskriptortyp: String
for(;*s;s++) {
*d++=*s&0xFF; // lobyte
*d++=*s>>8; // hibyte
}
s++; // "\0" übergehen für nächste String-Konstante
}
}
}g_pStringDescriptors;
//******************************************************************************
//
// The HID mouse device initialization and customization structures.
//
//******************************************************************************
tHIDMouseInstance g_sMouseInstance;
tUSBDHIDMouseDevice g_sMouseDevice = {
USB_VID_TI_1CBE,
USB_PID_MOUSE,
500,
USB_CONF_ATTR_SELF_PWR,
MouseHandler,
&g_sMouseDevice,
g_pStringDescriptors.data,
6,
};
//******************************************************************************
//
// Defines
//
//******************************************************************************
#define MOUSE_MOVE_INC 1 // The incremental update for the mouse.
#define MOUSE_MOVE_DEC -1
#define TICKS_PER_SECOND 100
#define MS_PER_SYSTICK (1000 / TICKS_PER_SECOND)
#define TICK_EVENT 0
#define DEBUG_PRINT(x)
#define MAX_SEND_DELAY 50 // 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.
//******************************************************************************
//
// Globals
//
//******************************************************************************
volatile uint32_t g_ui32Commands; // Holds command bits used to signal
// the main loop to perform
// various tasks.
volatile bool g_bConnected; // A flag used to indicate whether or
// not we are currently connected to
// the USB host.
volatile uint32_t g_ui32SysTickCount; // Global system tick counter holds
// elapsed time since the application
// started expressed in 100ths of a
// second.
//******************************************************************************
//
// This enumeration holds the various states that the mouse can be in during
// normal operation.
//
//******************************************************************************
volatile enum {
MOUSE_STATE_UNCONFIGURED, // Unconfigured.
MOUSE_STATE_IDLE, // No keys to send and not waiting on data.
MOUSE_STATE_SENDING // Waiting on data to be sent out.
}g_eMouseState = MOUSE_STATE_UNCONFIGURED;
//******************************************************************************
//
// MouseHandler - This function handles notification messages from the
// mouse device driver.
//
//******************************************************************************
uint32_t MouseHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgData, void *pvMsgData) {
switch(ui32Event)
{
//
// 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;
}
//
// Ignore the other events.
//
default:
break;
}
return(0);
}
//******************************************************************************
//
// WaitForSendIdle - Wait for a period of time for the state to become idle.
//
// \param ui32TimeoutTick is the number of system ticks to wait before declaring
// a timeout and returning \b false.
//
// This function polls the current keyboard state for ui32TimeoutTicks system
// ticks waiting for it to become idle. If the state becomes idle, the
// function returns true. If it ui32TimeoutTicks 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.
//
//******************************************************************************
bool WaitForSendIdle(uint32_t ui32TimeoutTicks) {
uint32_t ui32Start;
uint32_t ui32Now;
uint32_t ui32Elapsed;
ui32Start = g_ui32SysTickCount;
ui32Elapsed = 0;
while(ui32Elapsed < ui32TimeoutTicks)
{
//
// 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_ui32SysTickCount.
//
ui32Now = g_ui32SysTickCount;
ui32Elapsed = ((ui32Start < ui32Now) ? (ui32Now - ui32Start) :
(((uint32_t)0xFFFFFFFF - ui32Start) + ui32Now + 1));
}
//
// If we get here, we timed out so return a bad return code to let the
// caller know.
//
return(false);
}
//******************************************************************************
//
// MoveHandler - This function provides simulated movements of the mouse.
//
//******************************************************************************
void MoveHandler() {
uint32_t ui32Retcode;
char cDeltaX, cDeltaY;
//
// Determine the direction to move the mouse.
//
ui32Retcode = g_ui32SysTickCount % (4 * TICKS_PER_SECOND);
if(ui32Retcode < TICKS_PER_SECOND)
{
cDeltaX = MOUSE_MOVE_INC;
cDeltaY = 0;
}
else if(ui32Retcode < (2 * TICKS_PER_SECOND))
{
cDeltaX = 0;
cDeltaY = MOUSE_MOVE_INC;
}
else if(ui32Retcode < (3 * TICKS_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;
ui32Retcode = USBDHIDMouseStateChange(&g_sMouseDevice, cDeltaX,
cDeltaY, 0);
//
// Did we schedule the report for transmission?
//
if(ui32Retcode == 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;
}
}
}
//******************************************************************************
//
// CPUTimerIntHandler - This is the interrupt handler for the CPU Timer
// interrupt. It is called periodically and updates a global tick counter then
// sets a flag to tell the main loop to move the mouse.
//
//******************************************************************************
__interrupt void
CPUTimerIntHandler(void)
{
GPIO_writePin(0,1);
g_ui32SysTickCount++;
HWREG(&g_ui32Commands) |= 1;
GPIO_writePin(0,0);
Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP1);
}
#pragma DATA_SECTION("CLK_CFG_REGS")
volatile CLK_CFG_REGS ClkCfgRegs;
//******************************************************************************
//
// This is the main application entry function.
//
//******************************************************************************
int main() {
g_bConnected = false;
//
// Initialize device clock and peripherals
//
Device_init();
//
// Initialize GPIO and configure GPIO pins for USB.
//
Device_initGPIO();
ClkCfgRegs.CLKSRCCTL2.all |= 1; // Taktquelle für PLL: Quarz 12 MHz
ClkCfgRegs.AUXPLLCTL1.bit.PLLEN = 0;
ClkCfgRegs.AUXPLLMULT.all = 10; // 12 MHz × 10 = 120 MHz; Ausgabeteiler steht bereits auf 2, aktiviert PLLEN
while (!(ClkCfgRegs.AUXPLLSTS.bit.LOCKS)); // USB will 60 MHz
ClkCfgRegs.AUXPLLCTL1.bit.PLLCLKEN = 1;
Interrupt_initModule();
Interrupt_initVectorTable();
EINT;
ERTM;
USBGPIOEnable();
Interrupt_register(INT_USBA, f28x_USB0DeviceIntHandler);
USBStackModeSet(0, eUSBModeForceDevice, 0);
GPIO_setDirectionMode(0, GPIO_DIR_MODE_OUT);
Interrupt_register(INT_TIMER0, &CPUTimerIntHandler);
CPUTimerInit();
CPUTimer_setPeriod(CPUTIMER0_BASE,
(SysCtl_getClock(F_XTAL) / TICKS_PER_SECOND));
//
// Enable the CPU Timer interrupt.
//
CPUTimer_enableInterrupt(CPUTIMER0_BASE);
Interrupt_enable(INT_TIMER0);
CPUTimer_startTimer(CPUTIMER0_BASE);
//
// Show the application name on the display and SCI output.
//
DEBUG_PRINT("\nC2000 F2838x Series USB HID Mouse device example\n");
DEBUG_PRINT("---------------------------------\n\n");
//
// Pass the USB library our device information, initialize the USB
// controller and connect the device to the bus.
//
USBDHIDMouseInit(0,&g_sMouseDevice);
Interrupt_enableMaster();
//
// Drop into the main loop.
//
for(;;) {
//
// Tell the user what we are doing.
//
//SCIprintf("Waiting for host...\n");
//
// Wait for USB configuration to complete.
//
while (!g_bConnected);
//
// Update the status.
//
//SCIprintf("Host connected...\n");
//
// 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(HWREG(&g_ui32Commands) & 1)
{
HWREG(&g_ui32Commands) &= ~1;
MoveHandler();
}
}
}
}
//
// End of file
//
Detected encoding: UTF-8 | 0
|