Skip to content

Commit

Permalink
HID works( sort of, data received don't make sense). Also, changed co…
Browse files Browse the repository at this point in the history
…ntrol

transfer to work off of function pointer array instead of switch statement.
  • Loading branch information
Oleg Mazurov committed May 8, 2009
1 parent 81ed9d6 commit bb9b73b
Show file tree
Hide file tree
Showing 14 changed files with 1,016 additions and 644 deletions.
109 changes: 109 additions & 0 deletions HID.c
@@ -0,0 +1,109 @@
/* HID class support functions */

#include "project_config.h"

extern char bigbuf[]; //256 bytes
extern DEV_RECORD devtable[];

HID_DEVICE hid_device = {{ 0 }};
EP_RECORD hid_ep[ 2 ] = {{ 0 }}; //HID class endpoints, 1 control, 1 interrupt-IN
//the third endpoint is not implemented

/* HID Mouse probe. Called from USB state machine. */
/* assumes configuration length is less than 256 bytes */
/* looks for Class:03, Subclass: 01, Protocol: 02 in interface descriptor */
/* sets mouse in boot protocol */
/* assumes single configuration and interface configuration 0 */
BOOL HIDMProbe( BYTE addr, DWORD flags )
{
BYTE tmpbyte;
BYTE rcode;
BYTE confvalue;
WORD total_length;
USB_DESCR* data_ptr = ( USB_DESCR * )&bigbuf;
char* byte_ptr = bigbuf;
rcode = XferGetConfDescr( addr, 0, CONF_DESCR_LEN, 0, bigbuf ); //get configuration descriptor
if( rcode ) { //error handling
return( FALSE );
}
if( data_ptr->descr.config.wTotalLength > 256 ) {
total_length = 256;
}
else {
total_length = data_ptr->descr.config.wTotalLength;
}
rcode = XferGetConfDescr( addr, 0, total_length, 0, bigbuf ); //get the whole configuration
if( rcode ) { //error handling
return( FALSE );
}
confvalue = data_ptr->descr.config.bConfigurationValue;
while( byte_ptr < bigbuf + total_length ) {
if( data_ptr->descr.config.bDescriptorType != USB_DESCRIPTOR_INTERFACE ) {
byte_ptr = byte_ptr + data_ptr->descr.config.bLength;
data_ptr = ( USB_DESCR* )byte_ptr;
}// if( data_ptr->descr.config.bDescriptorType != USB_DESCRIPTOR_INTERFACE
else { //interface descriptor
if( data_ptr->descr.interface.bInterfaceClass == 03 &&
data_ptr->descr.interface.bInterfaceSubClass == 01 &&
data_ptr->descr.interface.bInterfaceProtocol == 02 ) {
devtable[ addr ].devclass = HID_M; //device class
tmpbyte = devtable[ addr ].epinfo->MaxPktSize;
HIDM_init(); //initialize data structures
devtable[ addr ].epinfo = hid_ep; //switch endpoint information structure
devtable[ addr ].epinfo[ 0 ].MaxPktSize = tmpbyte;
hid_device.interface = data_ptr->descr.interface.bInterfaceNumber;
hid_device.addr = addr;
byte_ptr = byte_ptr + data_ptr->descr.config.bLength;
data_ptr = ( USB_DESCR* )byte_ptr;
while( byte_ptr < bigbuf + total_length ) {
if( data_ptr->descr.config.bDescriptorType != USB_DESCRIPTOR_ENDPOINT ) { //skip to endpoint descriptor
byte_ptr = byte_ptr + data_ptr->descr.config.bLength;
data_ptr = ( USB_DESCR* )byte_ptr;
}
else {
/* fill endpoint information structure */
devtable[ addr ].epinfo[ 1 ].epAddr = data_ptr->descr.endpoint.bEndpointAddress;
devtable[ addr ].epinfo[ 1 ].Attr = data_ptr->descr.endpoint.bmAttributes;
devtable[ addr ].epinfo[ 1 ].MaxPktSize = data_ptr->descr.endpoint.wMaxPacketSize;
devtable[ addr ].epinfo[ 1 ].Interval = data_ptr->descr.endpoint.bInterval;
// devtable[ addr ].epinfo[ 1 ].rcvToggle = bmRCVTOG0;
/* configure device */
rcode = XferSetConf( addr, 0, confvalue ); //set configuration
if( rcode ) { //error handling
return( FALSE );
}
rcode = XferSetProto( addr, 0, hid_device.interface, BOOT_PROTOCOL );
Nop();
if( rcode ) { //error handling
return( FALSE );
}
else {
return( TRUE );
}
}
}//while( byte_ptr....
}//if (Class matches
else { //if class don't match; die on first interface. Not really correct
return( FALSE );
}
}//else if( data_ptr->
}// while( byte_ptr < &buf + total_length
return( FALSE );
}
void HIDM_init( void )
{
hid_ep[ 1 ].sndToggle = bmSNDTOG0;
hid_ep[ 1 ].rcvToggle = bmRCVTOG0;
}
/* poll boot mouse */
BYTE mousePoll( BOOT_MOUSE_REPORT* buf )
{
BYTE rcode;
MAXreg_wr( rPERADDR, hid_device.addr ); //set peripheral address
rcode = XferInTransfer( hid_device.addr, 1, 8, ( char* )&buf, devtable[ hid_device.addr ].epinfo[ 1 ].MaxPktSize );
return( rcode );
}
BOOL HIDMEventHandler( BYTE address, BYTE event, void *data, DWORD size )
{
return( FALSE );
}
50 changes: 50 additions & 0 deletions HID.h
@@ -0,0 +1,50 @@
/* HID support header */

#ifndef _HID_h_
#define _HID_h

/* HID device structure */
typedef struct {
BYTE addr;
BYTE interface;
} HID_DEVICE;
/* Boot mouse report 8 bytes */
typedef struct {
// struct {
// unsigned one:1;
// unsigned two:1;
// unsigned three:1;
// unsigned :5;
// } button;
BYTE button;
BYTE Xdispl;
BYTE Ydispl;
BYTE bytes3to7[ 5 ] ; //optional bytes
} BOOT_MOUSE_REPORT;
/* boot keyboard report 8 bytes */
typedef struct {
struct {
unsigned numlock:1;
unsigned capslock:1;
unsigned scrolllock:1;
unsigned compose:1;
unsigned kana:1;
unsigned constant:3;
} mod;
BYTE reserved;
BYTE keycode1;
BYTE keycode2;
BYTE keycode3;
BYTE keycode4;
BYTE keycode5;
BYTE keycode6;
} BOOT_KBD_REPORT;


/* Function prototypes */
BOOL HIDMProbe( BYTE address, DWORD flags );
void HIDM_init( void );
BYTE mousePoll( BOOT_MOUSE_REPORT* buf );
BOOL HIDMEventHandler( BYTE addr, BYTE event, void *data, DWORD size );

#endif // _HID_h_
2 changes: 1 addition & 1 deletion LwUSBhost.c
Expand Up @@ -8,7 +8,7 @@

#include "project_config.h"

/* Global vars */
/* Global variables */
volatile DWORD uptime = 0; //system uptime. Gets updated every millisecond

volatile BYTE USART_Tx_buf [ USART_RX_BUFSIZE ];
Expand Down
6 changes: 5 additions & 1 deletion LwUSBhost.mcp
Expand Up @@ -30,27 +30,31 @@ file_002=.
file_003=.
file_004=.
file_005=.
file_006=.
[GENERATED_FILES]
file_000=no
file_001=no
file_002=no
file_003=no
file_004=no
file_005=no
file_006=no
[OTHER_FILES]
file_000=no
file_001=no
file_002=no
file_003=no
file_004=no
file_005=no
file_006=no
[FILE_INFO]
file_000=LwUSBhost.c
file_001=USARTio.c
file_002=cli.c
file_003=MAX3421E.c
file_004=transfer.c
file_005=C:\MCC18\lkr\18f26k20i.lkr
file_005=HID.c
file_006=C:\MCC18\lkr\18f26k20i.lkr
[SUITE_INFO]
suite_guid={5B7D72DD-9861-47BD-9F60-2BE967BF8416}
suite_state=
Expand Down
1 change: 1 addition & 0 deletions LwUSBhost.tagsrc
Expand Up @@ -3,3 +3,4 @@ C:\work\projects\Lightweight_USB\PIC18F26K20\USARTio.c
C:\work\projects\Lightweight_USB\PIC18F26K20\cli.c
C:\work\projects\Lightweight_USB\PIC18F26K20\MAX3421E.c
C:\work\projects\Lightweight_USB\PIC18F26K20\transfer.c
C:\work\projects\Lightweight_USB\PIC18F26K20\HID.c
5 changes: 2 additions & 3 deletions MAX3421E.c
Expand Up @@ -98,7 +98,7 @@ void MAXreg_wr(BYTE reg, BYTE val)
}
/* multiple-byte write */
/* returns a pointer to a memory position after last written */
char* MAXbytes_wr( BYTE reg, BYTE nbytes, BYTE * data )
char* MAXbytes_wr( BYTE reg, BYTE nbytes, char* data )
{
Select_MAX3421E; //assert SS
SPI_wr ( reg + 2 ); //set W/R bit and select register
Expand All @@ -122,7 +122,7 @@ BYTE MAXreg_rd( BYTE reg )
}
/* multiple-bytes register read */
/* returns a pointer to a memory position after last read */
char* MAXbytes_rd ( BYTE reg, BYTE nbytes, BYTE *data )
char* MAXbytes_rd ( BYTE reg, BYTE nbytes, char* data )
{
Select_MAX3421E; //assert SS
SPI_wr ( reg ); //send register number
Expand Down Expand Up @@ -261,7 +261,6 @@ void MaxIntHandler( void )
//}
/* End HIRQ interrupts handling, clear serviced IRQs */
MAXreg_wr( rHIRQ, HIRQ_sendback );
Nop();
}
void MaxGpxHandler( void )
{
Expand Down
2 changes: 2 additions & 0 deletions MAX3421E.h
Expand Up @@ -188,6 +188,8 @@
#define bmSNDTOG0 0x40
#define bmSNDTOG1 0x80



#define rHXFR 0xf0 //30<<3
/* Host transfer token values for writing the HXFR register (R30) */
/* OR this bit field with the endpoint number in bits 3:0 */
Expand Down

0 comments on commit bb9b73b

Please sign in to comment.