I've bought a Development tool from Microchip which allows me to program my own USB interfaces. The Development board (PICDEM FS USB) from microchip has a potentiometer on board and comes with a number of Firmwares
I'm trying to integrate two pieces of code together so that i can get a mouse pointer to move from right to left using a potentiometer.
The first piece of code contains a program which orders the mouse pointer to move in circles (Although i have already been integrating the Potentionmeter components from the other code) The second is a program which determines the potentiometer position / resistance where feedback is provided in the form of an on screen graphical user interface.
I think i need to do something like do an A/D convert, then change the A/D result to a value between -127~127,for 10bit A/D using this formula to convert itA/D result)*256/1024-127.Then send this value to buffer[1] (The X Vector) in file user_mouse.c, and set buffer[2]=0.
But i am not sure how to write this into my code, and the structure which it requires...
This is the Mouse Code:-
Code:
/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
#include <usart.h>
#include "system\typedefs.h"
#include "system\usb\usb.h"
#include "io_cfg.h" // I/O pin mapping
#include "user\user_mouse.h"
/** V A R I A B L E S ********************************************************/
#pragma udata // not sure what udata does???
byte old_sw2,old_sw3;
BOOL emulate_mode;
rom signed char dir_table[]={-8,-8,-8, 0, 8, 8, 8, 0};
byte movement_length;
byte vector = 0;
char buffer[3]; // buffer is a char buffer and is 3 in size
/** P R I V A T E P R O T O T Y P E S ***************************************/
void BlinkUSBStatus(void);
BOOL Switch2IsPressed(void);
BOOL Switch3IsPressed(void); //function returns a Boolean value (1 or 0)
void ReadPOT(void);
void Emulate_Mouse(void);
/** D E C L A R A T I O N S **************************************************/
#pragma code
void UserInit(void)
{
mInitAllLEDs();
mInitAllSwitches();
old_sw2 = sw2;
old_sw3 = sw3;
buffer[0]=0;
buffer[1]=0;
buffer[2]=0;
buffer[3]=0;
emulate_mode = TRUE;
}//end UserInit
/******************************************************************************
* Function: void ProcessIO(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This function is a place holder for other user routines.
* It is a mixture of both USB and non-USB tasks.
*
* Note: None
*****************************************************************************/
void ProcessIO(void)
{
BlinkUSBStatus();
// User Application USB tasks
if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1)) return;
if(Switch3IsPressed())
emulate_mode = !emulate_mode; //emulate_mode = Not emulate_mode
Emulate_Mouse(); // if switch 3 is pressed on demo board it calls Emulate_Mouse Function prototyped below
}//end ProcessIO
void Emulate_Mouse(void) // void at beginning shows that function does not return a value
{ // void in brackets shows that function does not receive a value
// Function Prototype
if(emulate_mode == TRUE) // if emulate_mode has returned a value == to TRUE
{ // then do inside if statement
if(movement_length > 14) // if movement_length > 14 do this statement
{
buffer[0] = 0; // 0 is put into buffer position 0
//vector is a byte and is initiated to 0
buffer[1] = dir_table[vector & 0x07]; // X-Vector
// takes value from a direction table, the position in the table is dictated by
// the vector ANDED with hexadecimal 7 (This loops between positions 0 to 7)
buffer[2] = dir_table[(vector+2) & 0x07]; // Y-Vector
// for the Y direction 2 is added to the vector which means that when buffer pos 1 (X direction)is
// at dir_table value 7 buffer pos 2 is at dir_table value 1 (Y direction)
vector++; // vector keeps incrementing (Isn't that dangerous ? when will it stop ?)
movement_length = 0;
}//end if(movement_length < 14)
} // otherwise do this else statement
else
buffer[0] = buffer[1] = buffer[2] = 0; // set all buffers to zero
if(!mHIDTxIsBusy()) // if transmit channel is not busy then do rest of if statement
{
HIDTxReport(buffer,3); // HIDTxReport takes two input arguments,
// The values in the buffer array and the byte length
movement_length++; // increment movement_length (What is movement length?) is this a delay?
}//end if(mHIDIsPutReportReady())
}//end Emulate_Mouse
void ReadPOT(void)
{
ADCON0bits.GO = 1; // Start AD conversion
while(ADCON0bits.NOT_DONE); // Wait for conversion
return;
}//end ReadPOT
/******************************************************************************
* Function: void BlinkUSBStatus(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: BlinkUSBStatus turns on and off LEDs corresponding to
* the USB device state.
*
* Note: mLED macros can be found in io_cfg.h
* usb_device_state is declared in usbmmap.c and is modified
* in usbdrv.c, usbctrltrf.c, and usb9.c
*****************************************************************************/
void BlinkUSBStatus(void)
{
static word led_count=0;
if(led_count == 0)led_count = 10000U;
led_count--;
#define mLED_Both_Off() {mLED_1_Off();mLED_2_Off();}
#define mLED_Both_On() {mLED_1_On();mLED_2_On();}
#define mLED_Only_1_On() {mLED_1_On();mLED_2_Off();}
#define mLED_Only_2_On() {mLED_1_Off();mLED_2_On();}
if(UCONbits.SUSPND == 1)
{
if(led_count==0)
{
mLED_1_Toggle();
mLED_2 = mLED_1; // Both blink at the same time
}//end if
}
else
{
if(usb_device_state == DETACHED_STATE)
{
mLED_Both_Off();
}
else if(usb_device_state == ATTACHED_STATE)
{
mLED_Both_On();
}
else if(usb_device_state == POWERED_STATE)
{
mLED_Only_1_On();
}
else if(usb_device_state == DEFAULT_STATE)
{
mLED_Only_2_On();
}
else if(usb_device_state == ADDRESS_STATE)
{
if(led_count == 0)
{
mLED_1_Toggle();
mLED_2_Off();
}//end if
}
else if(usb_device_state == CONFIGURED_STATE)
{
if(led_count==0)
{
mLED_1_Toggle();
mLED_2 = !mLED_1; // Alternate blink
}//end if
}//end if(...)
}//end if(UCONbits.SUSPND...)
}//end BlinkUSBStatus
BOOL Switch2IsPressed(void) // Function Returns a Boolean
{
if(sw2 != old_sw2) // if sw2 is not equal to old sw then do statement, ie if pressed.
{
old_sw2 = sw2; // Save new value
if(sw2 == 0) // If pressed
return TRUE; // Was pressed
}//end if
return FALSE; // Was not pressed
}//end Switch2IsPressed
BOOL Switch3IsPressed(void) // Function Returns a Boolean
{
if(sw3 != old_sw3)
{
old_sw3 = sw3; // Save new value
if(sw3 == 0) // If pressed
return TRUE; // Was pressed
}//end if
return FALSE; // Was not pressed
}//end Switch3IsPressed
/** EOF user_mouse.c *********************************************************/
this is the Potentiometer Code etc...
Code:
/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
#include <usart.h>
#include "system\typedefs.h"
#include "system\usb\usb.h"
#include "io_cfg.h" // I/O pin mapping
#include "user\user.h"
#include "user\temperature.h"
/** V A R I A B L E S ********************************************************/
#pragma udata
byte old_sw2,old_sw3;
byte counter;
byte trf_state;
byte temp_mode;
DATA_PACKET dataPacket;
byte pTemp; // Pointer to current logging position, will
// loop to zero once the max index is reached
byte valid_temp; // Keeps count of the valid data points
word temp_data[30]; // 30 points of data
// Timer0 - 1 second interval setup.
// Fosc/4 = 12MHz
// Use /256 prescalar, this brings counter freq down to 46,875 Hz
// Timer0 should = 65536 - 46875 = 18661 or 0x48E5
#define TIMER0L_VAL 0xE5
#define TIMER0H_VAL 0x48
/** P R I V A T E P R O T O T Y P E S ***************************************/
void BlinkUSBStatus(void);
BOOL Switch2IsPressed(void);
BOOL Switch3IsPressed(void);
void ResetTempLog(void);
void ReadPOT(void);
void ServiceRequests(void);
// For board testing purpose only
void PICDEMFSUSBDemoBoardTest(void);
/** D E C L A R A T I O N S **************************************************/
#pragma code
void UserInit(void)
{
mInitAllLEDs();
mInitAllSwitches();
old_sw2 = sw2;
old_sw3 = sw3;
InitTempSensor();
mInitPOT();
ADCON2bits.ADFM = 1; // ADC result right justified
ResetTempLog();
temp_mode = TEMP_REAL_TIME;
/* Init Timer0 for data logging interval (every 1 second) */
T0CON = 0b10010111;
//T0CONbits.T08BIT = 0; // 16-bit mode
//T0CONbits.T0CS = 0; // Select Fosc/4
//T0CONbits.PSA = 0; // Assign prescalar (default is /256)
/* Timer0 is already enabled by default */
}//end UserInit
/******************************************************************************
* Function: void ProcessIO(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This function is a place holder for other user routines.
* It is a mixture of both USB and non-USB tasks.
*
* Note: None
*****************************************************************************/
void ProcessIO(void)
{
BlinkUSBStatus();
// User Application USB tasks
if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1)) return;
ServiceRequests();
if(temp_mode == TEMP_LOGGING)
{
if(INTCONbits.TMR0IF == 1)
{
INTCONbits.TMR0IF = 0; // Clear flag
TMR0H = TIMER0H_VAL;
TMR0L = TIMER0L_VAL; // Reinit timer value;
if(AcquireTemperature())
{
temp_data[pTemp] = temperature._word;
// First update valid_temp
if(valid_temp < 30) // 30 data points max
valid_temp++;
// Next update pTemp
if(pTemp == 29)
pTemp = 0;
else
pTemp++;
}//end if
}//end if
}//end if
}//end ProcessIO
void ResetTempLog(void)
{
pTemp = 0;
valid_temp = 0;
}//end ResetLog
void ReadPOT(void)
{
ADCON0bits.GO = 1; // Start AD conversion
while(ADCON0bits.NOT_DONE); // Wait for conversion
return;
}//end ReadPOT
void ServiceRequests(void)
{
byte index;
if(USBGenRead((byte*)&dataPacket,sizeof(dataPacket)))
{
counter = 0;
switch(dataPacket.CMD)
{
case READ_VERSION:
//dataPacket._byte[1] is len
dataPacket._byte[2] = MINOR_VERSION;
dataPacket._byte[3] = MAJOR_VERSION;
counter=0x04;
break;
case ID_BOARD:
counter = 0x01;
if(dataPacket.ID == 0)
{
mLED_3_Off();mLED_4_Off();
}
else if(dataPacket.ID == 1)
{
mLED_3_Off();mLED_4_On();
}
else if(dataPacket.ID == 2)
{
mLED_3_On();mLED_4_Off();
}
else if(dataPacket.ID == 3)
{
mLED_3_On();mLED_4_On();
}
else
counter = 0x00;
break;
case UPDATE_LED:
// LED1 & LED2 are used as USB event indicators.
if(dataPacket.led_num == 3)
{
mLED_3 = dataPacket.led_status;
counter = 0x01;
}//end if
else if(dataPacket.led_num == 4)
{
mLED_4 = dataPacket.led_status;
counter = 0x01;
}//end if else
break;
case SET_TEMP_REAL:
temp_mode = TEMP_REAL_TIME;
ResetTempLog();
counter = 0x01;
break;
case RD_TEMP:
if(AcquireTemperature())
{
dataPacket.word_data = temperature._word;
counter=0x03;
}//end if
break;
case SET_TEMP_LOGGING:
temp_mode = TEMP_LOGGING;
ResetTempLog();
counter=0x01;
break;
case RD_TEMP_LOGGING:
counter = (valid_temp<<1)+2; // Update count in byte
dataPacket.len = (valid_temp<<1);
for(index = valid_temp; index > 0; index--)
{
if(pTemp == 0)
pTemp = 29;
else
pTemp--;
dataPacket._word[index] = temp_data[pTemp];
}//end for
ResetTempLog(); // Once read, log will restart
break;
case RD_POT: // Comes from PC Software
ReadPOT();
dataPacket._byte[1] = ADRESL;
dataPacket._byte[2] = ADRESH;
counter=0x03;
break;
case RESET:
Reset();
break;
default:
break;
}//end switch()
if(counter != 0)
{
if(!mUSBGenTxIsBusy())
USBGenWrite((byte*)&dataPacket,counter);
}//end if
}//end if
}//end ServiceRequests
/******************************************************************************
* Function: void BlinkUSBStatus(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: BlinkUSBStatus turns on and off LEDs corresponding to
* the USB device state.
*
* Note: mLED macros can be found in io_cfg.h
* usb_device_state is declared in usbmmap.c and is modified
* in usbdrv.c, usbctrltrf.c, and usb9.c
*****************************************************************************/
void BlinkUSBStatus(void)
{
static word led_count=0;
if(led_count == 0)led_count = 10000U;
led_count--;
#define mLED_Both_Off() {mLED_1_Off();mLED_2_Off();}
#define mLED_Both_On() {mLED_1_On();mLED_2_On();}
#define mLED_Only_1_On() {mLED_1_On();mLED_2_Off();}
#define mLED_Only_2_On() {mLED_1_Off();mLED_2_On();}
if(UCONbits.SUSPND == 1)
{
if(led_count==0)
{
mLED_1_Toggle();
mLED_2 = mLED_1; // Both blink at the same time
}//end if
}
else
{
if(usb_device_state == DETACHED_STATE)
{
mLED_Both_Off();
PICDEMFSUSBDemoBoardTest();
}
else if(usb_device_state == ATTACHED_STATE)
{
mLED_Both_On();
}
else if(usb_device_state == POWERED_STATE)
{
mLED_Only_1_On();
}
else if(usb_device_state == DEFAULT_STATE)
{
mLED_Only_2_On();
}
else if(usb_device_state == ADDRESS_STATE)
{
if(led_count == 0)
{
mLED_1_Toggle();
mLED_2_Off();
}//end if
}
else if(usb_device_state == CONFIGURED_STATE)
{
if(led_count==0)
{
mLED_1_Toggle();
mLED_2 = !mLED_1; // Alternate blink
}//end if
}//end if(...)
}//end if(UCONbits.SUSPND...)
}//end BlinkUSBStatus
BOOL Switch2IsPressed(void)
{
if(sw2 != old_sw2)
{
old_sw2 = sw2; // Save new value
if(sw2 == 0) // If pressed
return TRUE; // Was pressed
}//end if
return FALSE; // Was not pressed
}//end Switch2IsPressed
BOOL Switch3IsPressed(void)
{
if(sw3 != old_sw3)
{
old_sw3 = sw3; // Save new value
if(sw3 == 0) // If pressed
return TRUE; // Was pressed
}//end if
return FALSE; // Was not pressed
}//end Switch3IsPressed
void TXbyte(byte data)
{
while(TXSTAbits.TRMT==0);
TXREG = data;
}//end TXbyte
void PICDEMFSUSBDemoBoardTest(void)
{
byte temp;
//PICDEM FS USB Demo Board Test Procedure:
if(Switch2IsPressed())
{
//LEDs and push buttons testing
mLED_1_On();
while(!Switch2IsPressed());
mLED_1_Off();
mLED_2_On();
while(!Switch3IsPressed());
mLED_2_Off();
mLED_3_On();
while(!Switch3IsPressed());
mLED_3_Off();
mLED_4_On();
while(!Switch3IsPressed());
mLED_4_Off();
//RS-232 Setup
SSPCON1 = 0; // Make sure SPI is disabled
TRISCbits.TRISC7=1; // RX
TRISCbits.TRISC6=0; // TX
SPBRG = 0x71;
SPBRGH = 0x02; // 0x0271 for 48MHz -> 19200 baud
TXSTA = 0x24; // TX enable BRGH=1
RCSTA = 0x90; // continuous RX
BAUDCON = 0x08; // BRG16 = 1
temp = RCREG; // Empty buffer
temp = RCREG; // Empty buffer
//RS-232 Tx & Rx Tests
while(!Switch3IsPressed());
TXbyte('R');
TXbyte('S');
TXbyte('-');
TXbyte('2');
TXbyte('3');
TXbyte('2');
TXbyte(' ');
TXbyte('T');
TXbyte('X');
TXbyte(' ');
TXbyte('T');
TXbyte('e');
TXbyte('s');
TXbyte('t');
TXbyte(' ');
TXbyte('O');
TXbyte('K');
TXbyte(',');
TXbyte(' ');
TXbyte('P');
TXbyte('r');
TXbyte('e');
TXbyte('s');
TXbyte('s');
TXbyte(' ');
TXbyte('"');
TXbyte('r');
TXbyte('"');
TXbyte(',');
while(PIR1bits.RCIF==0); //Wait for data from RS232
if(RCREG == 'r')
{
TXbyte(' ');
TXbyte('R');
TXbyte('X');
TXbyte(' ');
TXbyte('T');
TXbyte('e');
TXbyte('s');
TXbyte('t');
TXbyte(' ');
TXbyte('O');
TXbyte('K');
}//end if
UserInit(); //Re-initialize default user fw
//Test phase 1 done
}//end if
}//end PICDEMFSUSBDemoBoardTest()
/** EOF user.c ***************************************************************/
This is the Human Interface Device C File... which is in the mouse workspace
Code:
/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
#include "system\typedefs.h"
#include "system\usb\usb.h"
#ifdef USB_USE_HID
/** V A R I A B L E S ********************************************************/
#pragma udata
byte idle_rate;
byte active_protocol; // [0] Boot Protocol [1] Report Protocol
byte hid_rpt_rx_len;
/** P R I V A T E P R O T O T Y P E S ***************************************/
void HIDGetReportHandler(void);
void HIDSetReportHandler(void);
/** D E C L A R A T I O N S **************************************************/
#pragma code
/** C L A S S S P E C I F I C R E Q ****************************************/
/******************************************************************************
* Function: void USBCheckHIDRequest(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine checks the setup data packet to see if it
* knows how to handle it
*
* Note: None
*****************************************************************************/
void USBCheckHIDRequest(void)
{
if(SetupPkt.Recipient != RCPT_INTF) return;
if(SetupPkt.bIntfID != HID_INTF_ID) return;
/*
* There are two standard requests that hid.c may support.
* 1. GET_DSC(DSC_HID,DSC_RPT,DSC_PHY);
* 2. SET_DSC(DSC_HID,DSC_RPT,DSC_PHY);
*/
if(SetupPkt.bRequest == GET_DSC)
{
switch(SetupPkt.bDscType)
{
case DSC_HID:
ctrl_trf_session_owner = MUID_HID;
mUSBGetHIDDscAdr(pSrc.bRom); // See usbcfg.h
wCount._word = sizeof(USB_HID_DSC);
break;
case DSC_RPT:
ctrl_trf_session_owner = MUID_HID;
mUSBGetHIDRptDscAdr(pSrc.bRom); // See usbcfg.h
mUSBGetHIDRptDscSize(wCount._word); // See usbcfg.h
break;
case DSC_PHY:
// ctrl_trf_session_owner = MUID_HID;
break;
}//end switch(SetupPkt.bDscType)
usb_stat.ctrl_trf_mem = _ROM;
}//end if(SetupPkt.bRequest == GET_DSC)
if(SetupPkt.RequestType != CLASS) return;
switch(SetupPkt.bRequest)
{
case GET_REPORT:
HIDGetReportHandler();
break;
case SET_REPORT:
HIDSetReportHandler();
break;
case GET_IDLE:
ctrl_trf_session_owner = MUID_HID;
pSrc.bRam = (byte*)&idle_rate; // Set source
usb_stat.ctrl_trf_mem = _RAM; // Set memory type
LSB(wCount) = 1; // Set data count
break;
case SET_IDLE:
ctrl_trf_session_owner = MUID_HID;
idle_rate = MSB(SetupPkt.W_Value);
break;
case GET_PROTOCOL:
ctrl_trf_session_owner = MUID_HID;
pSrc.bRam = (byte*)&active_protocol;// Set source
usb_stat.ctrl_trf_mem = _RAM; // Set memory type
LSB(wCount) = 1; // Set data count
break;
case SET_PROTOCOL:
ctrl_trf_session_owner = MUID_HID;
active_protocol = LSB(SetupPkt.W_Value);
break;
}//end switch(SetupPkt.bRequest)
}//end USBCheckHIDRequest
void HIDGetReportHandler(void)
{
// ctrl_trf_session_owner = MUID_HID;
}//end HIDGetReportHandler
void HIDSetReportHandler(void)
{
// ctrl_trf_session_owner = MUID_HID;
// pDst.bRam = (byte*)&hid_report_out;
}//end HIDSetReportHandler
/** U S E R A P I ***********************************************************/
/******************************************************************************
* Function: void HIDInitEP(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: HIDInitEP initializes HID endpoints, buffer descriptors,
* internal state-machine, and variables.
* It should be called after the USB host has sent out a
* SET_CONFIGURATION request.
* See USBStdSetCfgHandler() in usb9.c for examples.
*
* Note: None
*****************************************************************************/
void HIDInitEP(void)
{
hid_rpt_rx_len =0;
HID_UEP = EP_OUT_IN|HSHK_EN; // Enable 2 data pipes
HID_BD_OUT.Cnt = sizeof(hid_report_out); // Set buffer size
HID_BD_OUT.ADR = (byte*)&hid_report_out; // Set buffer address
HID_BD_OUT.Stat._byte = _USIE|_DAT0|_DTSEN; // Set status
/*
* Do not have to init Cnt of IN pipes here.
* Reason: Number of bytes to send to the host
* varies from one transaction to
* another. Cnt should equal the exact
* number of bytes to transmit for
* a given IN transaction.
* This number of bytes will only
* be known right before the data is
* sent.
*/
HID_BD_IN.ADR = (byte*)&hid_report_in; // Set buffer address
HID_BD_IN.Stat._byte = _UCPU|_DAT1; // Set status
}//end HIDInitEP
/******************************************************************************
* Function: void HIDTxReport(char *buffer, byte len)
*
* PreCondition: mHIDTxIsBusy() must return false.
*
* Value of 'len' must be equal to or smaller than
* HID_INT_IN_EP_SIZE
* For an interrupt endpoint, the largest buffer size is
* 64 bytes.
*
* Input: buffer : Pointer to the starting location of data bytes
* len : Number of bytes to be transferred
*
* Output: None
*
* Side Effects: None
*
* Overview: Use this macro to transfer data located in data memory.
*
* Remember: mHIDTxIsBusy() must return false before user
* can call this function.
* Unexpected behavior will occur if this function is called
* when mHIDTxIsBusy() == 0
*
* Typical Usage:
* if(!mHIDTxIsBusy())
* HIDTxReport(buffer, 3);
*
* Note: None
*****************************************************************************/
void HIDTxReport(char *buffer, byte len)
{
byte i;
/*
* Value of len should be equal to or smaller than HID_INT_IN_EP_SIZE.
* This check forces the value of len to meet the precondition.
*/
if(len > HID_INT_IN_EP_SIZE)
len = HID_INT_IN_EP_SIZE;
/*
* Copy data from user's buffer to dual-ram buffer
*/
for (i = 0; i < len; i++)
hid_report_in[i] = buffer[i];
HID_BD_IN.Cnt = len;
mUSBBufferReady(HID_BD_IN);
}//end HIDTxReport
/******************************************************************************
* Function: byte HIDRxReport(char *buffer, byte len)
*
* PreCondition: Value of input argument 'len' should be smaller than the
* maximum endpoint size responsible for receiving report
* data from USB host for HID class.
* Input argument 'buffer' should point to a buffer area that
* is bigger or equal to the size specified by 'len'.
*
* Input: buffer : Pointer to where received bytes are to be stored
* len : The number of bytes expected.
*
* Output: The number of bytes copied to buffer.
*
* Side Effects: Publicly accessible variable hid_rpt_rx_len is updated
* with the number of bytes copied to buffer.
* Once HIDRxReport is called, subsequent retrieval of
* hid_rpt_rx_len can be done by calling macro
* mHIDGetRptRxLength().
*
* Overview: HIDRxReport copies a string of bytes received through
* USB HID OUT endpoint to a user's specified location.
* It is a non-blocking function. It does not wait
* for data if there is no data available. Instead it returns
* '0' to notify the caller that there is no data available.
*
* Note: If the actual number of bytes received is larger than the
* number of bytes expected (len), only the expected number
* of bytes specified will be copied to buffer.
* If the actual number of bytes received is smaller than the
* number of bytes expected (len), only the actual number
* of bytes received will be copied to buffer.
*****************************************************************************/
byte HIDRxReport(char *buffer, byte len)
{
hid_rpt_rx_len = 0;
if(!mHIDRxIsBusy())
{
/*
* Adjust the expected number of bytes to equal
* the actual number of bytes received.
*/
if(len > HID_BD_OUT.Cnt)
len = HID_BD_OUT.Cnt;
/*
* Copy data from dual-ram buffer to user's buffer
*/
for(hid_rpt_rx_len = 0; hid_rpt_rx_len < len; hid_rpt_rx_len++)
buffer[hid_rpt_rx_len] = hid_report_out[hid_rpt_rx_len];
/*
* Prepare dual-ram buffer for next OUT transaction
*/
HID_BD_OUT.Cnt = sizeof(hid_report_out);
mUSBBufferReady(HID_BD_OUT);
}//end if
return hid_rpt_rx_len;
}//end HIDRxReport
#endif //def USB_USE_HID
/** EOF hid.c ***************************************************************/
and this is the generic.c file
Code:
/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
#include "system\typedefs.h"
#include "system\usb\usb.h"
#ifdef USB_USE_GEN
/** V A R I A B L E S ********************************************************/
#pragma udata
byte usbgen_rx_len;
/** P R I V A T E P R O T O T Y P E S ***************************************/
/** D E C L A R A T I O N S **************************************************/
#pragma code
/** U S E R A P I ***********************************************************/
/******************************************************************************
* Function: void USBGenInitEP(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: USBGenInitEP initializes generic endpoints, buffer
* descriptors, internal state-machine, and variables.
* It should be called after the USB host has sent out a
* SET_CONFIGURATION request.
* See USBStdSetCfgHandler() in usb9.c for examples.
*
* Note: None
*****************************************************************************/
void USBGenInitEP(void)
{
usbgen_rx_len = 0;
USBGEN_UEP = EP_OUT_IN|HSHK_EN; // Enable 2 data pipes
/*
* Do not have to init Cnt of IN pipes here.
* Reason: Number of bytes to send to the host
* varies from one transaction to
* another. Cnt should equal the exact
* number of bytes to transmit for
* a given IN transaction.
* This number of bytes will only
* be known right before the data is
* sent.
*/
USBGEN_BD_OUT.Cnt = sizeof(usbgen_out); // Set buffer size
USBGEN_BD_OUT.ADR = (byte*)&usbgen_out; // Set buffer address
USBGEN_BD_OUT.Stat._byte = _USIE|_DAT0|_DTSEN;// Set status
USBGEN_BD_IN.ADR = (byte*)&usbgen_in; // Set buffer address
USBGEN_BD_IN.Stat._byte = _UCPU|_DAT1; // Set buffer status
}//end USBGenInitEP
/******************************************************************************
* Function: void USBGenWrite(byte *buffer, byte len)
*
* PreCondition: mUSBGenTxIsBusy() must return false.
*
* Value of 'len' must be equal to or smaller than
* USBGEN_EP_SIZE
* For an interrupt/bulk endpoint, the largest buffer size is
* 64 bytes.
*
* Input: buffer : Pointer to the starting location of data bytes
* len : Number of bytes to be transferred
*
* Output: None
*
* Side Effects: None
*
* Overview: Use this macro to transfer data located in data memory.
*
* Remember: mUSBGenTxIsBusy() must return false before user
* can call this function.
* Unexpected behavior will occur if this function is called
* when mUSBGenTxIsBusy() != 0
*
* Typical Usage:
* if(!mUSBGenTxIsBusy())
* USBGenWrite(buffer, 3);
*
* Note: None
*****************************************************************************/
void USBGenWrite(byte *buffer, byte len)
{
byte i;
/*
* Value of len should be equal to or smaller than USBGEN_EP_SIZE.
* This check forces the value of len to meet the precondition.
*/
if(len > USBGEN_EP_SIZE)
len = USBGEN_EP_SIZE;
/*
* Copy data from user's buffer to dual-ram buffer
*/
for (i = 0; i < len; i++)
usbgen_in[i] = buffer[i];
USBGEN_BD_IN.Cnt = len;
mUSBBufferReady(USBGEN_BD_IN);
}//end USBGenWrite
/******************************************************************************
* Function: byte USBGenRead(byte *buffer, byte len)
*
* PreCondition: Value of input argument 'len' should be smaller than the
* maximum endpoint size responsible for receiving report
* data from USB host for HID class.
* Input argument 'buffer' should point to a buffer area that
* is bigger or equal to the size specified by 'len'.
*
* Input: buffer : Pointer to where received bytes are to be stored
* len : The number of bytes expected.
*
* Output: The number of bytes copied to buffer.
*
* Side Effects: Publicly accessible variable usbgen_rx_len is updated
* with the number of bytes copied to buffer.
* Once USBGenRead is called, subsequent retrieval of
* usbgen_rx_len can be done by calling macro
* mUSBGenGetRxLength().
*
* Overview: USBGenRead copies a string of bytes received through
* the OUT endpoint to a user's specified location.
* It is a non-blocking function. It does not wait
* for data if there is no data available. Instead it returns
* '0' to notify the caller that there is no data available.
*
* Note: If the actual number of bytes received is larger than the
* number of bytes expected (len), only the expected number
* of bytes specified will be copied to buffer.
* If the actual number of bytes received is smaller than the
* number of bytes expected (len), only the actual number
* of bytes received will be copied to buffer.
*****************************************************************************/
byte USBGenRead(byte *buffer, byte len)
{
usbgen_rx_len = 0;
if(!mUSBGenRxIsBusy())
{
/*
* Adjust the expected number of bytes to equal
* the actual number of bytes received.
*/
if(len > USBGEN_BD_OUT.Cnt)
len = USBGEN_BD_OUT.Cnt;
/*
* Copy data from dual-ram buffer to user's buffer
*/
for(usbgen_rx_len = 0; usbgen_rx_len < len; usbgen_rx_len++)
buffer[usbgen_rx_len] = usbgen_out[usbgen_rx_len];
/*
* Prepare dual-ram buffer for next OUT transaction
*/
USBGEN_BD_OUT.Cnt = sizeof(usbgen_out);
mUSBBufferReady(USBGEN_BD_OUT);
}//end if
return usbgen_rx_len;
}//end USBGenRead
#endif //def USB_USE_GEN
/** EOF usbgen.c *************************************************************/
Any Help Much appreciated...