Amazon

Monday 29 December 2008

HID Mouse in PSoC

Recently, I had to simulate a 3-button mouse with a PSoC connected to a CPU via the USB link.  The PSoC is a CY8C24894 and the CPU is a ICH-7 from Intel.

Here's the code I wrote to simulate the left, right, down, and up movements as well as the right and left click buttons. You need to had your own routine on how to determine the mouse actions.

First, you need to configure the USB module using the USB Setup Wizard in PSoC designer. You need to get the Vendor ID and Product ID of the ICH-7 CPU. In this case, Vendor ID is 8086h and the Product ID is 27CAh (this is in relation with which UHCI controller you are using). For the rest of the attributes, refer to the USBFS datasheet provided in PSoC designer.

//----------------------------------------------------------------------------
// HID Mouse
// with USBFS module
//----------------------------------------------------------------------------

#include        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "psocgpioint.h"
#include "globalparams.h"
#include

int move_left;
int move_right;
int move_up;
int move_down;
int right_click;
int left_click;

BYTE Mouse_data[3] = {0,0,0}; 

void main()   
{   
M8C_EnableGInt;                       // Enable Global Interrupts

USBFS_1_Start(0, USB_3V_OPERATION); //Start USBFS Operation using device 0  
                                 //and with 3.3V operation  
 while (!USBFS_1_bGetConfiguration()); //Wait for Device to enumerate
//Enumeration is completed load endpoint 1. Do not toggle the first time
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_NO_TOGGLE);

while(1) 
{

if (move_left) 
Mouse_data[1] = 0xFB; //Move cursor left 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[1] = 0x00; //Stop cursor 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE);

if (move_right) 
Mouse_data[1] = 0x05; //Move cursor right 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[1] = 0x00; //Stop cursor 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 

if (move_down) 
Mouse_data[2] = 0x05; //Move cursor down 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[2] = 0x00; //Stop cursor 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
}

if (move_up) 
Mouse_data[2] = 0xFB; //Move cursor up 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[2] = 0x00; //Stop cursor 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 

if (left_click) 
Mouse_data[0] |= 0x01; //Left click 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[0] = 0x00;
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data Mouse_data, 3, USB_TOGGLE); 

if (right_click) 
Mouse_data[0] |= 0x02; //Right click 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[0] = 0x00;
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
}
}
}

No comments: