Tuesday, October 13, 2009

How to create a simple 3 button USB mouse using PSoC USB device CY8C24794

Writing code for a usb mouse is such a complicated thing that anybody learning the report structure will go mad. Here comes Cypress's PSoC .PSoC device CY8C24794 is a USB programmable device with a USB interface.Also it can be powered using the USB cable.We can make a simple 3 button mouse using this device within 10 mins,if the CY3214 kit is available.The PSoC makes it all possible.!!!!!The most important thing is to understand the working of basic things and then the technical stuff will become simple.Here a person who doesn't know how to configure the PSoC using the PSoC designer software is advised to go through the Cypress site to get the detailed tutorial. Anybody can download the tutorial by registering into the Cypress site.Also there is a site called PSoC developer where we can find some PSoC related useful posts.This is the code for the 3 button USB mouse
// C main line


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

BYTE abMouseData[4] = {0,0,0,0};

void main()

/*adc initialisation*/
char cResult1, cResult2;
char cResult1_prev, cResult2_prev;
M8C_EnableGInt; // Enable global interrupts
DUALADC8_Start(DUALADC8_HIGHPOWER); // Turn on Analog section
DUALADC8_SetCalcTime(100); // Set CalcTime to 100
DUALADC8_GetSamples(); // Start ADC
/*usb initialisation*/

M8C_EnableGInt; //Enable Global Interrupts
USBFS_Start(0, USB_5V_OPERATION); //Start USBFS Operation using device 0
//and with 5V operation
while(!USBFS_bGetConfiguration()); //Wait for Device to enumerate
//Enumeration is completed load endpoint 1. Do not toggle the first time
USBFS_LoadInEP(1, abMouseData, 3, USB_NO_TOGGLE);

/*lcd initialisation*/
while(!USBFS_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_LoadInEP(1, abMouseData, 3, USB_TOGGLE);
while(DUALADC8_fIsDataAvailable == 0);
cResult1 = DUALADC8_cGetData1();
cResult2 = DUALADC8_cGetData2ClearFlag();
if ((cResult1_prev-cResult1)> 20 ||(cResult1-cResult1_prev) > 20)
abMouseData[1] = 0x01;
else if ((cResult1_prev-cResult1)<> 20 ||(cResult2-cResult2_prev) > 20)
abMouseData[2] = 0x01;
else if ((cResult2_prev-cResult2)< style="text-align: justify;">Here in this code the mouse x and y motions are mimicked by using 2 potentiometers.The PSoC core reads the ADC (8bit)continuously and sends the conditioned value to the PSoC core for processing and sending the required data to the system. The required value is sent through the USB interface.The PRT0DR is the port 0 data register.where the data read from the input ports of the PSoC is stored. The abMouseData is an array which is actually called the report descriptor by software guys who code for this complex USB HID device.The format for the USB HID mouse is given below