From 2c728cd7ddd04daddec7521e734d58764ecfde4d Mon Sep 17 00:00:00 2001 From: Oleg Mazurov Date: Tue, 10 Jan 2012 12:49:42 -0700 Subject: [PATCH] Arduino-1.0 compatibility added --- Usb.cpp | 22 +- Usb.h | 11 +- address.h | 12 +- adk.h | 5 + cdcacm.cpp | 6 +- cdcacm.h | 5 + cdcftdi.cpp | 2 +- cdcftdi.h | 5 + cdcprolific.cpp | 4 +- cdcprolific.h | 9 +- confdescparser.h | 10 +- examples/HID/USBHIDBootKbd/USBHIDBootKbd.pde | 2 +- .../HID/USBHIDBootMouse/USBHIDBootMouse.pde | 7 +- examples/HID/USBHID_desc/USBHID_desc.pde | 44 ++++ examples/HID/USBHID_desc/pgmstrings.h | 52 +++++ examples/USB_desc/USB_desc.pde | 16 +- examples/acm/acm_terminal/acm_terminal.pde | 2 +- examples/adk/adk_barcode/adk_barcode.pde | 2 +- examples/adk/term_test/term_test.pde | 3 +- .../ftdi/USBFTDILoopback/USBFTDILoopback.pde | 10 +- examples/hub_demo/hub_demo.pde | 2 +- .../pl2303_gprs_terminal.pde | 4 +- examples/pl2303/pl2303_gps/pl2303_gps.pde | 21 +- .../pl2303/pl2303_tinygps/pl2303_tinygps.pde | 2 +- .../pl2303_xbee_terminal.pde | 4 +- hid.cpp | 31 +-- hid.h | 52 +---- hidboot.h | 40 ++-- hidescriptorparser.cpp | 199 +++++++++++++++--- hidescriptorparser.h | 5 + hiduniversal.cpp | 87 ++++---- hiduniversal.h | 7 + max_LCD.cpp | 7 +- message.cpp | 4 + parsetools.h | 6 +- printhex.h | 4 + usbhost.h | 1 - usbhub.cpp | 7 +- usbhub.h | 5 + 39 files changed, 489 insertions(+), 228 deletions(-) create mode 100644 examples/HID/USBHID_desc/USBHID_desc.pde create mode 100644 examples/HID/USBHID_desc/pgmstrings.h diff --git a/Usb.cpp b/Usb.cpp index 676a77bc..a94ff8bb 100644 --- a/Usb.cpp +++ b/Usb.cpp @@ -20,7 +20,12 @@ e-mail : support@circuitsathome.com #include "max3421e.h" #include "usbhost.h" #include "Usb.h" -#include "WProgram.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif static uint8_t usb_error = 0; static uint8_t usb_task_state; @@ -102,8 +107,15 @@ uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t &nak_l if (!*ppep) return USB_ERROR_EP_NOT_FOUND_IN_TBL; - nak_limit = (0x0001UL << ( ( (*ppep)->bmNakPower > USB_NAK_MAX_POWER ) ? USB_NAK_MAX_POWER : (*ppep)->bmNakPower) ); - + nak_limit = (0x0001UL << ( ( (*ppep)->bmNakPower > USB_NAK_MAX_POWER ) ? USB_NAK_MAX_POWER : (*ppep)->bmNakPower) ); + nak_limit--; +/* + USBTRACE2("\r\nAddress: ", addr); + USBTRACE2(" EP: ", ep); + USBTRACE2(" NAK Power: ",(*ppep)->bmNakPower); + USBTRACE2(" NAK Limit: ", nak_limit); + USBTRACE("\r\n"); +*/ regWr( rPERADDR, addr ); //set peripheral address uint8_t mode = regRd( rMODE ); @@ -111,8 +123,6 @@ uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t &nak_l // Set bmLOWSPEED and bmHUBPRE in case of low-speed device, reset them otherwise regWr( rMODE, (p->lowspeed) ? mode | bmLOWSPEED | bmHubPre : mode & ~(bmHUBPRE | bmLOWSPEED)); - //delay(20); - return 0; } @@ -354,7 +364,7 @@ uint8_t USB::dispatchPkt( uint8_t token, uint8_t ep, uint16_t nak_limit ) while( timeout > millis() ) { regWr( rHXFR, ( token|ep )); //launch the transfer - rcode = 0xff; + rcode = USB_ERROR_TRANSFER_TIMEOUT; while( millis() < timeout ) //wait for transfer completion { diff --git a/Usb.h b/Usb.h index 665ac578..341c1368 100644 --- a/Usb.h +++ b/Usb.h @@ -33,7 +33,12 @@ e-mail : support@circuitsathome.com #include "usbhost.h" #include "usb_ch9.h" #include "address.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif #include "printhex.h" #include "hexdump.h" @@ -48,10 +53,12 @@ typedef MAX3421e MAX3421E; // Black Widow typedef MAX3421e MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560 #endif +//Debug macros. In 1.0 it is possible to move strings to PROGMEM by defining USBTRACE (Serial.print(F(s))) #define USBTRACE(s) (Serial.print((s))) #define USBTRACE2(s,r) (Serial.print((s)), Serial.println((r),HEX)) + /* Common setup data constant combinations */ #define bmREQ_GET_DESCR USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_DEVICE //get descriptor request type #define bmREQ_SET USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_DEVICE //set request type for all but 'set feature' and 'set interface' @@ -94,6 +101,7 @@ typedef MAX3421e MAX3421E; // Official Arduinos (UNO, Duemilanove, Me #define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE 0xD9 #define USB_ERROR_INVALID_MAX_PKT_SIZE 0xDA #define USB_ERROR_EP_NOT_FOUND_IN_TBL 0xDB +#define USB_ERROR_TRANSFER_TIMEOUT 0xFF class USBDeviceConfig { @@ -105,10 +113,9 @@ public: }; #define USB_XFER_TIMEOUT 5000 //USB transfer timeout in milliseconds, per section 9.2.6.1 of USB 2.0 spec -#define USB_NAK_LIMIT 32000 //NAK limit for a transfer. o meand NAKs are not counted +//#define USB_NAK_LIMIT 32000 //NAK limit for a transfer. 0 means NAKs are not counted #define USB_RETRY_LIMIT 3 //retry limit for a transfer #define USB_SETTLE_DELAY 200 //settle delay in milliseconds -#define USB_NAK_NOWAIT 1 //quit after receiving a single NAK #define USB_NUMDEVICES 16 //number of USB devices //#define HUB_MAX_HUBS 7 // maximum number of hubs that can be attached to the host controller diff --git a/address.h b/address.h index 234a433e..5d3e31fe 100644 --- a/address.h +++ b/address.h @@ -21,7 +21,13 @@ e-mail : support@circuitsathome.com #include #include "max3421e.h" + +/* NAK powers. To save space in endpoint data structure, amount of retries before giving up and returning 0x4 is stored in */ +/* bmNakPower as a power of 2. The actual nak_limit is then calculated as nak_limit = ( 2^bmNakPower - 1) */ #define USB_NAK_MAX_POWER 15 //NAK binary order maximum value +#define USB_NAK_DEFAULT 14 //default 32K-1 NAKs before giving up +#define USB_NAK_NOWAIT 1 //Single NAK stops transfer +#define USB_NAK_NONAK 0 //Do not count NAKs, stop retrying after USB Timeout struct EpInfo { @@ -34,8 +40,8 @@ struct EpInfo struct { - uint8_t bmSndToggle : 1; // Send toggle, when zerro bmSNDTOG0, bmSNDTOG1 otherwise - uint8_t bmRcvToggle : 1; // Send toggle, when zerro bmRCVTOG0, bmRCVTOG1 otherwise + uint8_t bmSndToggle : 1; // Send toggle, when zero bmSNDTOG0, bmSNDTOG1 otherwise + uint8_t bmRcvToggle : 1; // Send toggle, when zero bmRCVTOG0, bmRCVTOG1 otherwise uint8_t bmNakPower : 6; // Binary order for NAK_LIMIT value }; }; @@ -160,7 +166,7 @@ class AddressPoolImpl : public AddressPool public: AddressPoolImpl() : hubCounter(0) { - // Zerro address is reserved + // Zero address is reserved InitEntry(0); thePool[0].address = 0; diff --git a/adk.h b/adk.h index f14c82d5..024b013b 100644 --- a/adk.h +++ b/adk.h @@ -27,7 +27,12 @@ e-mail : support@circuitsathome.com #include "usbhost.h" #include "usb_ch9.h" #include "Usb.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif #include "printhex.h" #include "hexdump.h" diff --git a/cdcacm.cpp b/cdcacm.cpp index 17706388..df013fbc 100644 --- a/cdcacm.cpp +++ b/cdcacm.cpp @@ -35,8 +35,10 @@ ACM::ACM(USB *p, CDCAsyncOper *pasync) : epInfo[i].epAddr = 0; epInfo[i].maxPktSize = (i) ? 0 : 8; epInfo[i].epAttribs = 0; - - if (!i) + //epInfo[i].bmNakPower = USB_NAK_NOWAIT; + epInfo[i].bmNakPower = USB_NAK_MAX_POWER; + + //if (!i) epInfo[i].bmNakPower = USB_NAK_MAX_POWER; } if (pUsb) diff --git a/cdcacm.h b/cdcacm.h index 25d82e53..b315787d 100644 --- a/cdcacm.h +++ b/cdcacm.h @@ -24,7 +24,12 @@ e-mail : support@circuitsathome.com #include "usbhost.h" #include "usb_ch9.h" #include "Usb.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif #include "printhex.h" #include "hexdump.h" diff --git a/cdcftdi.cpp b/cdcftdi.cpp index c5b9b0c8..04fdca9a 100644 --- a/cdcftdi.cpp +++ b/cdcftdi.cpp @@ -33,7 +33,7 @@ FTDI::FTDI(USB *p, FTDIAsyncOper *pasync) : epInfo[i].maxPktSize = (i) ? 0 : 8; epInfo[i].epAttribs = 0; - if (!i) + //if (!i) epInfo[i].bmNakPower = USB_NAK_MAX_POWER; } if (pUsb) diff --git a/cdcftdi.h b/cdcftdi.h index 270492a1..41d0650c 100644 --- a/cdcftdi.h +++ b/cdcftdi.h @@ -24,7 +24,12 @@ e-mail : support@circuitsathome.com #include "usbhost.h" #include "usb_ch9.h" #include "Usb.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif #include "printhex.h" #include "hexdump.h" diff --git a/cdcprolific.cpp b/cdcprolific.cpp index fd39ed45..30694d43 100644 --- a/cdcprolific.cpp +++ b/cdcprolific.cpp @@ -16,13 +16,13 @@ e-mail : support@circuitsathome.com */ #include "cdcprolific.h" -PL::PL(USB *p, CDCAsyncOper *pasync) : +PL2303::PL2303(USB *p, CDCAsyncOper *pasync) : ACM(p, pasync), wPLType(0) { } -uint8_t PL::Init(uint8_t parent, uint8_t port, bool lowspeed) +uint8_t PL2303::Init(uint8_t parent, uint8_t port, bool lowspeed) { const uint8_t constBufSize = sizeof(USB_DEVICE_DESCRIPTOR); diff --git a/cdcprolific.h b/cdcprolific.h index 095f39d3..b008ddd0 100644 --- a/cdcprolific.h +++ b/cdcprolific.h @@ -24,7 +24,12 @@ e-mail : support@circuitsathome.com #include "usbhost.h" #include "usb_ch9.h" #include "Usb.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif #include "printhex.h" #include "hexdump.h" @@ -132,12 +137,12 @@ enum pl2303_type #define PL_MAX_ENDPOINTS 4 -class PL : public ACM +class PL2303 : public ACM { uint16_t wPLType; // Type of chip public: - PL(USB *pusb, CDCAsyncOper *pasync); + PL2303(USB *pusb, CDCAsyncOper *pasync); // USBDeviceConfig implementation virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed); diff --git a/confdescparser.h b/confdescparser.h index 295a1203..0c215227 100644 --- a/confdescparser.h +++ b/confdescparser.h @@ -164,11 +164,11 @@ bool ConfigDescParser::ParseDescriptor if (theXtractor) theXtractor->EndpointXtract(confValue, ifaceNumber, ifaceAltSet, protoValue, (USB_ENDPOINT_DESCRIPTOR*)varBuffer); break; - case HID_DESCRIPTOR_HID: - if (!valParser.Parse(pp, pcntdn)) - return false; - PrintHidDescriptor((const USB_HID_DESCRIPTOR*)varBuffer); - break; + //case HID_DESCRIPTOR_HID: + // if (!valParser.Parse(pp, pcntdn)) + // return false; + // PrintHidDescriptor((const USB_HID_DESCRIPTOR*)varBuffer); + // break; default: if (!theSkipper.Skip(pp, pcntdn, dscrLen-2)) return false; diff --git a/examples/HID/USBHIDBootKbd/USBHIDBootKbd.pde b/examples/HID/USBHIDBootKbd/USBHIDBootKbd.pde index 1b1d7ebf..24fe65be 100644 --- a/examples/HID/USBHIDBootKbd/USBHIDBootKbd.pde +++ b/examples/HID/USBHIDBootKbd/USBHIDBootKbd.pde @@ -63,7 +63,7 @@ void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) void KbdRptParser::OnKeyPressed(uint8_t key) { Serial.print("ASCII: "); - Serial.println(key); + Serial.println((char)key); }; USB Usb; diff --git a/examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde b/examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde index 10e97eb6..eb3500f4 100644 --- a/examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde +++ b/examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde @@ -8,7 +8,6 @@ #include #include #include - #include #include #include @@ -58,12 +57,12 @@ void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi) }; USB Usb; -//USBHub Hub(&Usb); +USBHub Hub(&Usb); HIDBoot Mouse(&Usb); uint32_t next_time; -MouseRptParser Prs; +MouseRptParser Prs; void setup() { @@ -77,7 +76,7 @@ void setup() next_time = millis() + 5000; - Mouse.SetReportParser(0, (HIDReportParser*)&Prs); + Mouse.SetReportParser(0,(HIDReportParser*)&Prs); } void loop() diff --git a/examples/HID/USBHID_desc/USBHID_desc.pde b/examples/HID/USBHID_desc/USBHID_desc.pde new file mode 100644 index 00000000..2ba4ce3b --- /dev/null +++ b/examples/HID/USBHID_desc/USBHID_desc.pde @@ -0,0 +1,44 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pgmstrings.h" + +USB Usb; +USBHub Hub(&Usb); +HIDUniversal Hid(&Usb); +UniversalReportParser Uni; + +void setup() +{ + Serial.begin( 115200 ); + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + if (!Hid.SetReportParser(0, &Uni)) + ErrorMessage(PSTR("SetReportParser"), 1 ); +} + +void loop() +{ + Usb.Task(); +} + diff --git a/examples/HID/USBHID_desc/pgmstrings.h b/examples/HID/USBHID_desc/pgmstrings.h new file mode 100644 index 00000000..bdb0077e --- /dev/null +++ b/examples/HID/USBHID_desc/pgmstrings.h @@ -0,0 +1,52 @@ +#if !defined(__PGMSTRINGS_H__) +#define __PGMSTRINGS_H__ + +#define LOBYTE(x) ((char*)(&(x)))[0] +#define HIBYTE(x) ((char*)(&(x)))[1] +#define BUFSIZE 256 //buffer size + + +/* Print strings in Program Memory */ +const char Gen_Error_str[] PROGMEM = "\r\nRequest error. Error code:\t"; +const char Dev_Header_str[] PROGMEM ="\r\nDevice descriptor: "; +const char Dev_Length_str[] PROGMEM ="\r\nDescriptor Length:\t"; +const char Dev_Type_str[] PROGMEM ="\r\nDescriptor type:\t"; +const char Dev_Version_str[] PROGMEM ="\r\nUSB version:\t\t"; +const char Dev_Class_str[] PROGMEM ="\r\nDevice class:\t\t"; +const char Dev_Subclass_str[] PROGMEM ="\r\nDevice Subclass:\t"; +const char Dev_Protocol_str[] PROGMEM ="\r\nDevice Protocol:\t"; +const char Dev_Pktsize_str[] PROGMEM ="\r\nMax.packet size:\t"; +const char Dev_Vendor_str[] PROGMEM ="\r\nVendor ID:\t\t"; +const char Dev_Product_str[] PROGMEM ="\r\nProduct ID:\t\t"; +const char Dev_Revision_str[] PROGMEM ="\r\nRevision ID:\t\t"; +const char Dev_Mfg_str[] PROGMEM ="\r\nMfg.string index:\t"; +const char Dev_Prod_str[] PROGMEM ="\r\nProd.string index:\t"; +const char Dev_Serial_str[] PROGMEM ="\r\nSerial number index:\t"; +const char Dev_Nconf_str[] PROGMEM ="\r\nNumber of conf.:\t"; +const char Conf_Trunc_str[] PROGMEM ="Total length truncated to 256 bytes"; +const char Conf_Header_str[] PROGMEM ="\r\nConfiguration descriptor:"; +const char Conf_Totlen_str[] PROGMEM ="\r\nTotal length:\t\t"; +const char Conf_Nint_str[] PROGMEM ="\r\nNum.intf:\t\t"; +const char Conf_Value_str[] PROGMEM ="\r\nConf.value:\t\t"; +const char Conf_String_str[] PROGMEM ="\r\nConf.string:\t\t"; +const char Conf_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char Conf_Pwr_str[] PROGMEM ="\r\nMax.pwr:\t\t"; +const char Int_Header_str[] PROGMEM ="\r\n\r\nInterface descriptor:"; +const char Int_Number_str[] PROGMEM ="\r\nIntf.number:\t\t"; +const char Int_Alt_str[] PROGMEM ="\r\nAlt.:\t\t\t"; +const char Int_Endpoints_str[] PROGMEM ="\r\nEndpoints:\t\t"; +const char Int_Class_str[] PROGMEM ="\r\nIntf. Class:\t\t"; +const char Int_Subclass_str[] PROGMEM ="\r\nIntf. Subclass:\t\t"; +const char Int_Protocol_str[] PROGMEM ="\r\nIntf. Protocol:\t\t"; +const char Int_String_str[] PROGMEM ="\r\nIntf.string:\t\t"; +const char End_Header_str[] PROGMEM ="\r\n\r\nEndpoint descriptor:"; +const char End_Address_str[] PROGMEM ="\r\nEndpoint address:\t"; +const char End_Attr_str[] PROGMEM ="\r\nAttr.:\t\t\t"; +const char End_Pktsize_str[] PROGMEM ="\r\nMax.pkt size:\t\t"; +const char End_Interval_str[] PROGMEM ="\r\nPolling interval:\t"; +const char Unk_Header_str[] PROGMEM = "\r\nUnknown descriptor:"; +const char Unk_Length_str[] PROGMEM ="\r\nLength:\t\t"; +const char Unk_Type_str[] PROGMEM ="\r\nType:\t\t"; +const char Unk_Contents_str[] PROGMEM ="\r\nContents:\t"; + +#endif // __PGMSTRINGS_H__ \ No newline at end of file diff --git a/examples/USB_desc/USB_desc.pde b/examples/USB_desc/USB_desc.pde index 1a7b0f78..770e421c 100644 --- a/examples/USB_desc/USB_desc.pde +++ b/examples/USB_desc/USB_desc.pde @@ -10,13 +10,13 @@ #include "pgmstrings.h" USB Usb; -USBHub Hub1(&Usb); -USBHub Hub2(&Usb); -USBHub Hub3(&Usb); -USBHub Hub4(&Usb); -USBHub Hub5(&Usb); -USBHub Hub6(&Usb); -USBHub Hub7(&Usb); +//USBHub Hub1(&Usb); +//USBHub Hub2(&Usb); +//USBHub Hub3(&Usb); +//USBHub Hub4(&Usb); +//USBHub Hub5(&Usb); +//USBHub Hub6(&Usb); +//USBHub Hub7(&Usb); uint32_t next_time; @@ -343,5 +343,5 @@ void printProgStr(const prog_char str[]) char c; if(!str) return; while((c = pgm_read_byte(str++))) - Serial.print(c,BYTE); + Serial.print(c); } diff --git a/examples/acm/acm_terminal/acm_terminal.pde b/examples/acm/acm_terminal/acm_terminal.pde index 92141c78..ae2756d0 100644 --- a/examples/acm/acm_terminal/acm_terminal.pde +++ b/examples/acm/acm_terminal/acm_terminal.pde @@ -95,7 +95,7 @@ void loop() if( rcvd ) { //more than zero bytes received for(uint16_t i=0; i < rcvd; i++ ) { - Serial.print(buf[i]); //printing on the screen + Serial.print((char)buf[i]); //printing on the screen } } delay(10); diff --git a/examples/adk/adk_barcode/adk_barcode.pde b/examples/adk/adk_barcode/adk_barcode.pde index e0b763dc..02d68728 100644 --- a/examples/adk/adk_barcode/adk_barcode.pde +++ b/examples/adk/adk_barcode/adk_barcode.pde @@ -63,7 +63,7 @@ uint8_t keylcl; rcode = adk.SndData( 1, &keylcl ); } - Serial.print( keylcl ); + Serial.print((char) keylcl ); Serial.print(" : "); Serial.println( keylcl, HEX ); }; diff --git a/examples/adk/term_test/term_test.pde b/examples/adk/term_test/term_test.pde index baacfc7d..fa863337 100644 --- a/examples/adk/term_test/term_test.pde +++ b/examples/adk/term_test/term_test.pde @@ -10,6 +10,7 @@ #include USB Usb; +//USBHub Hub(&Usb); ADK adk(&Usb,"Circuits@Home, ltd.", "USB Host Shield", @@ -50,7 +51,7 @@ void loop() USBTRACE("\r\nData Packet."); for( uint8_t i = 0; i < len; i++ ) { - Serial.print(msg[i]); + Serial.print((char)msg[i]); } /* sending back what was received */ rcode = adk.SndData( strlen( recv ), (uint8_t *)recv ); diff --git a/examples/ftdi/USBFTDILoopback/USBFTDILoopback.pde b/examples/ftdi/USBFTDILoopback/USBFTDILoopback.pde index 645ca346..8e305754 100644 --- a/examples/ftdi/USBFTDILoopback/USBFTDILoopback.pde +++ b/examples/ftdi/USBFTDILoopback/USBFTDILoopback.pde @@ -69,21 +69,23 @@ void loop() { uint8_t rcode; char strbuf[] = "DEADBEEF"; + //char strbuf[] = "The quick brown fox jumps over the lazy dog"; + //char strbuf[] = "This string contains 61 character to demonstrate FTDI buffers"; //add one symbol to it to see some garbage Serial.print("."); - rcode = Ftdi.SndData(8, (uint8_t*)strbuf); + rcode = Ftdi.SndData(strlen(strbuf), (uint8_t*)strbuf); if (rcode) ErrorMessage(PSTR("SndData"), rcode); delay(50); - uint8_t buf[16]; + uint8_t buf[64]; - for (uint8_t i=0; i<16; i++) + for (uint8_t i=0; i<64; i++) buf[i] = 0; - uint16_t rcvd = 15; + uint16_t rcvd = 64; rcode = Ftdi.RcvData(&rcvd, buf); if (rcode && rcode != hrNAK) diff --git a/examples/hub_demo/hub_demo.pde b/examples/hub_demo/hub_demo.pde index a0b801e4..0a8665ba 100644 --- a/examples/hub_demo/hub_demo.pde +++ b/examples/hub_demo/hub_demo.pde @@ -339,5 +339,5 @@ void printProgStr(const prog_char str[]) char c; if(!str) return; while((c = pgm_read_byte(str++))) - Serial.print(c,BYTE); + Serial.print(c); } diff --git a/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.pde b/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.pde index 5d1fecda..d6ce279a 100644 --- a/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.pde +++ b/examples/pl2303/pl2303_gprs_terminal/pl2303_gprs_terminal.pde @@ -53,7 +53,7 @@ uint8_t PLAsyncOper::OnInit(ACM *pacm) USB Usb; //USBHub Hub(&Usb); PLAsyncOper AsyncOper; -PL Pl(&Usb, &AsyncOper); +PL2303 Pl(&Usb, &AsyncOper); void setup() { @@ -96,7 +96,7 @@ void loop() if( rcvd ) { //more than zero bytes received for(uint16_t i=0; i < rcvd; i++ ) { - Serial.print(buf[i]); //printing on the screen + Serial.print((char)buf[i]); //printing on the screen } }//if( rcvd ... }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING.. diff --git a/examples/pl2303/pl2303_gps/pl2303_gps.pde b/examples/pl2303/pl2303_gps/pl2303_gps.pde index aefc2823..24585e0a 100644 --- a/examples/pl2303/pl2303_gps/pl2303_gps.pde +++ b/examples/pl2303/pl2303_gps/pl2303_gps.pde @@ -52,9 +52,11 @@ uint8_t PLAsyncOper::OnInit(ACM *pacm) } USB Usb; -//USBHub Hub(&Usb); +USBHub Hub(&Usb); PLAsyncOper AsyncOper; -PL Pl(&Usb, &AsyncOper); +PL2303 Pl(&Usb, &AsyncOper); +uint32_t read_delay; +#define READ_DELAY 100 void setup() { @@ -69,23 +71,26 @@ void setup() void loop() { - Usb.Task(); +uint8_t rcode; +uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint +uint16_t rcvd = 64; + + Usb.Task(); if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) { - uint8_t rcode; - uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint - uint16_t rcvd = 64; /* reading the GPS */ + if( read_delay < millis() ){ + read_delay += READ_DELAY; rcode = Pl.RcvData(&rcvd, buf); if ( rcode && rcode != hrNAK ) ErrorMessage(PSTR("Ret"), rcode); if( rcvd ) { //more than zero bytes received for( uint16_t i=0; i < rcvd; i++ ) { - Serial.print(buf[i]); //printing on the screen + Serial.print((char)buf[i]); //printing on the screen }//for( uint16_t i=0; i < rcvd; i++... }//if( rcvd - //delay(10); + }//if( read_delay > millis()... }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING.. } diff --git a/examples/pl2303/pl2303_tinygps/pl2303_tinygps.pde b/examples/pl2303/pl2303_tinygps/pl2303_tinygps.pde index 77e30510..985f9d3d 100644 --- a/examples/pl2303/pl2303_tinygps/pl2303_tinygps.pde +++ b/examples/pl2303/pl2303_tinygps/pl2303_tinygps.pde @@ -66,7 +66,7 @@ uint8_t PLAsyncOper::OnInit(ACM *pacm) USB Usb; //USBHub Hub(&Usb); PLAsyncOper AsyncOper; -PL Pl(&Usb, &AsyncOper); +PL2303 Pl(&Usb, &AsyncOper); TinyGPS gps; void gpsdump(TinyGPS &gps); diff --git a/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.pde b/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.pde index 89c15420..1cf3ac57 100644 --- a/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.pde +++ b/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.pde @@ -53,7 +53,7 @@ uint8_t PLAsyncOper::OnInit(ACM *pacm) USB Usb; //USBHub Hub(&Usb); PLAsyncOper AsyncOper; -PL Pl(&Usb, &AsyncOper); +PL2303 Pl(&Usb, &AsyncOper); void setup() { @@ -109,7 +109,7 @@ void loop() Serial.print("\r\n"); //insert linefeed } else { - Serial.print(buf[i]); //printing on the screen + Serial.print((char)buf[i]); //printing on the screen } } } diff --git a/hid.cpp b/hid.cpp index 1e5449e7..4796e782 100644 --- a/hid.cpp +++ b/hid.cpp @@ -1,40 +1,13 @@ -/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved. - -This software may be distributed and modified under the terms of the GNU -General Public License version 2 (GPL2) as published by the Free Software -Foundation and appearing in the file GPL2.TXT included in the packaging of -this file. Please note that GPL2 Section 2[b] requires that all works based -on this software must also be made publicly available under the terms of -the GPL2 ("Copyleft"). - -Contact information -------------------- - -Circuits At Home, LTD -Web : http://www.circuitsathome.com -e-mail : support@circuitsathome.com -*/ #include "hid.h" -//const uint16_t HID::maxHidInterfaces = 3; -//const uint16_t HID::maxEpPerInterface = 2; -const uint8_t HID::epInterruptInIndex = 1; -const uint8_t HID::epInterruptOutIndex = 2; - - //get HID report descriptor uint8_t HID::GetReportDescr( uint8_t ep, USBReadParser *parser ) { const uint8_t constBufLen = 64; uint8_t buf[constBufLen]; - //HexDumper HexDump; - - //return( pUsb->ctrlReq( bAddress, ep, /*bmREQ_HIDREPORT*/0x81, USB_REQUEST_GET_DESCRIPTOR, 0x00, - // HID_DESCRIPTOR_REPORT, 0x0000, constBufLen, constBufLen, buf, NULL )); - - uint8_t rcode = pUsb->ctrlReq( bAddress, ep, /*bmREQ_HIDREPORT*/0x81, USB_REQUEST_GET_DESCRIPTOR, 0x00, - HID_DESCRIPTOR_REPORT, 0x0000, 0x4C1, constBufLen, buf, (USBReadParser*)parser ); + uint8_t rcode = pUsb->ctrlReq( bAddress, ep, bmREQ_HIDREPORT, USB_REQUEST_GET_DESCRIPTOR, 0x00, + HID_DESCRIPTOR_REPORT, 0x0000, 128, constBufLen, buf, (USBReadParser*)parser ); //return ((rcode != hrSTALL) ? rcode : 0); return rcode; diff --git a/hid.h b/hid.h index f32a13c7..513feb2c 100644 --- a/hid.h +++ b/hid.h @@ -1,19 +1,3 @@ -/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved. - -This software may be distributed and modified under the terms of the GNU -General Public License version 2 (GPL2) as published by the Free Software -Foundation and appearing in the file GPL2.TXT included in the packaging of -this file. Please note that GPL2 Section 2[b] requires that all works based -on this software must also be made publicly available under the terms of -the GPL2 ("Copyleft"). - -Contact information -------------------- - -Circuits At Home, LTD -Web : http://www.circuitsathome.com -e-mail : support@circuitsathome.com -*/ #if !defined(__HID_H__) #define __HID_H__ @@ -24,7 +8,12 @@ e-mail : support@circuitsathome.com #include "usbhost.h" #include "usb_ch9.h" #include "Usb.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif #include "printhex.h" #include "hexdump.h" @@ -103,24 +92,6 @@ e-mail : support@circuitsathome.com #define HID_PROTOCOL_KEYBOARD 0x01 #define HID_PROTOCOL_MOUSE 0x02 -///* HID descriptor */ -//typedef struct -//{ -// uint8_t bLength; -// uint8_t bDescriptorType; -// uint16_t bcdHID; // HID class specification release -// uint8_t bCountryCode; -// uint8_t bNumDescriptors; // Number of additional class specific descriptors -// uint8_t bDescrType; // Type of class descriptor -// uint16_t wDescriptorLength; // Total size of the Report descriptor -//} USB_HID_DESCRIPTOR; -// -//typedef struct -//{ -// uint8_t bDescrType; // Type of class descriptor -// uint16_t wDescriptorLength; // Total size of the Report descriptor -//} HID_CLASS_DESCRIPTOR_LEN_AND_TYPE; - struct HidItemPrefix { uint8_t bSize : 2; @@ -172,11 +143,6 @@ public: }; #define MAX_REPORT_PARSERS 2 - -#define maxHidInterfaces 3 -#define maxEpPerInterface 2 -#define totalEndpoints (maxHidInterfaces * maxEpPerInterface + 1) - #define HID_MAX_HID_CLASS_DESCRIPTORS 5 class HID : public USBDeviceConfig, public UsbConfigXtracter @@ -186,9 +152,13 @@ protected: uint8_t bAddress; // address protected: - static const uint8_t epInterruptInIndex; // InterruptIN endpoint index - static const uint8_t epInterruptOutIndex; // InterruptOUT endpoint index + static const uint8_t epInterruptInIndex = 1; // InterruptIN endpoint index + static const uint8_t epInterruptOutIndex = 2; // InterruptOUT endpoint index + static const uint8_t maxHidInterfaces = 3; + static const uint8_t maxEpPerInterface = 2; + static const uint8_t totalEndpoints = (maxHidInterfaces * maxEpPerInterface + 1); + void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr); void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc); diff --git a/hidboot.h b/hidboot.h index 3350f552..26ac451b 100644 --- a/hidboot.h +++ b/hidboot.h @@ -25,7 +25,12 @@ e-mail : support@circuitsathome.com #include "usb_ch9.h" #include "Usb.h" #include "hid.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif #include "printhex.h" #include "hexdump.h" @@ -208,9 +213,7 @@ void HIDBoot::Initialize() epInfo[i].epAddr = 0; epInfo[i].maxPktSize = (i) ? 0 : 8; epInfo[i].epAttribs = 0; - - if (!i) - epInfo[i].bmNakPower = USB_NAK_MAX_POWER; + epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER; } bNumEP = 1; bNumIface = 0; @@ -322,18 +325,18 @@ uint8_t HIDBoot::Init(uint8_t parent, uint8_t port, bool lowspeed if (rcode) goto FailSetDevTblEntry; - USBTRACE2("NC:", num_of_conf); + //USBTRACE2("NC:", num_of_conf); for (uint8_t i=0; i HexDump; + //HexDumper HexDump; ConfigDescParser< USB_CLASS_HID, HID_BOOT_INTF_SUBCLASS, BOOT_PROTOCOL, CP_MASK_COMPARE_ALL> confDescrParser(this); - rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump); + //rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump); rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser); if (bNumEP > 1) @@ -343,13 +346,13 @@ uint8_t HIDBoot::Init(uint8_t parent, uint8_t port, bool lowspeed if (bNumEP < 2) return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED; - USBTRACE2("\r\nbAddr:", bAddress); - USBTRACE2("\r\nbNumEP:", bNumEP); + //USBTRACE2("\r\nbAddr:", bAddress); + //USBTRACE2("\r\nbNumEP:", bNumEP); // Assign epInfo to epinfo pointer rcode = pUsb->setEpInfoEntry(bAddress, bNumEP, epInfo); - USBTRACE2("\r\nCnf:", bConfNum); + //USBTRACE2("\r\nCnf:", bConfNum); // Set Configuration Value rcode = pUsb->setConf(bAddress, 0, bConfNum); @@ -357,7 +360,7 @@ uint8_t HIDBoot::Init(uint8_t parent, uint8_t port, bool lowspeed if (rcode) goto FailSetConfDescr; - USBTRACE2("\r\nIf:", bIfaceNum); + //USBTRACE2("\r\nIf:", bIfaceNum); rcode = SetProtocol(bIfaceNum, HID_BOOT_PROTOCOL); @@ -413,9 +416,9 @@ void HIDBoot::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t if (bNumEP > 1 && conf != bConfNum) return; - ErrorMessage(PSTR("\r\nConf.Val"), conf); - ErrorMessage(PSTR("Iface Num"), iface); - ErrorMessage(PSTR("Alt.Set"), alt); + //ErrorMessage(PSTR("\r\nConf.Val"), conf); + //ErrorMessage(PSTR("Iface Num"), iface); + //ErrorMessage(PSTR("Alt.Set"), alt); bConfNum = conf; bIfaceNum = iface; @@ -424,7 +427,6 @@ void HIDBoot::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t if ((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80) { - USBTRACE("I8\r\n"); index = epInterruptInIndex; // Fill in the endpoint info structure @@ -434,7 +436,7 @@ void HIDBoot::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t bNumEP ++; - PrintEndpointDescriptor(pep); + //PrintEndpointDescriptor(pep); } } @@ -478,10 +480,10 @@ uint8_t HIDBoot::Poll() USBTRACE2("Poll:", rcode); return rcode; } - for (uint8_t i=0; i(buf[i]); - if (read) - Serial.println(""); + //for (uint8_t i=0; i(buf[i]); + //if (read) + // Serial.println(""); if (pRptParser) pRptParser->Parse((HID*)this, 0, (uint8_t)read, buf); diff --git a/hidescriptorparser.cpp b/hidescriptorparser.cpp index 92355fbe..426cbc89 100644 --- a/hidescriptorparser.cpp +++ b/hidescriptorparser.cpp @@ -1,21 +1,4 @@ -/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved. - -This software may be distributed and modified under the terms of the GNU -General Public License version 2 (GPL2) as published by the Free Software -Foundation and appearing in the file GPL2.TXT included in the packaging of -this file. Please note that GPL2 Section 2[b] requires that all works based -on this software must also be made publicly available under the terms of -the GPL2 ("Copyleft"). - -Contact information -------------------- - -Circuits At Home, LTD -Web : http://www.circuitsathome.com -e-mail : support@circuitsathome.com -*/ #include "hidescriptorparser.h" -//#include "hidusagetitlearrays.cpp" const char *ReportDescParserBase::usagePageTitles0[] PROGMEM = { @@ -1060,7 +1043,7 @@ void ReportDescParserBase::Parse(const uint16_t len, const uint8_t *pbuf, const //if (ParseItem(&p, &cntdn)) // return; } - USBTRACE2("Total:", totalSize); + //USBTRACE2("Total:", totalSize); } void ReportDescParserBase::PrintValue(uint8_t *p, uint8_t len) @@ -1348,7 +1331,7 @@ void ReportDescParserBase::PrintButtonPageUsage(uint16_t usage) { Notify(pstrSpace); Notify(PSTR("Btn")); - Serial.print(usage, DEC); + Serial.print(usage, HEX); } void ReportDescParserBase::PrintOrdinalPageUsage(uint16_t usage) @@ -1645,6 +1628,7 @@ uint8_t ReportDescParser2::ParseItem(uint8_t **pp, uint16_t *pcntdn) totalSize += (uint16_t)rptSize * (uint16_t)rptCount; + // Кажется это надо делать в начале, а не в конце... rptSize = 0; rptCount = 0; useMin = 0; @@ -1661,8 +1645,13 @@ void ReportDescParser2::OnInputItem(uint8_t itm) { uint8_t byte_offset = (totalSize >> 3); // calculate offset to the next unhandled byte i = (int)(totalCount / 8); uint32_t tmp = (byte_offset << 3); - uint8_t bit_offset = totalSize - tmp; // number of bits in the current byte already handled - uint8_t *p = pBuf + byte_offset; // current byte pointer + uint8_t bit_offset = totalSize - tmp; // number of bits in the current byte already handled + uint8_t *p = pBuf + byte_offset; // current byte pointer + + + //Serial.print("Itm:"); + //PrintHex(itm); + //Serial.println(""); //Serial.print(" tS:"); //PrintHex(totalSize); @@ -1684,11 +1673,35 @@ void ReportDescParser2::OnInputItem(uint8_t itm) uint8_t usage = useMin; + //Serial.print("\r\nUseMin:"); + //PrintHex(useMin); + //Serial.println(""); + + //Serial.print("UseMax:"); + //PrintHex(useMax); + //Serial.println(""); + + //Serial.print("pF:"); + //PrintHex(useMin); + //Serial.println(""); + bool print_usemin_usemax = ( (useMin < useMax) && ((itm & 3) == 2) && pfUsage) ? true : false; uint8_t bits_of_byte = 8; - for (uint8_t i=0; i 8) ? 8 : (bits_left > bits_of_byte) ? bits_of_byte : bits_left; + bits_to_copy = (bits_left > bits_of_byte) ? bits_of_byte : bits_left; + //bits_to_copy = (bits_left > 8) ? 8 : (bits_left > bits_of_byte) ? bits_of_byte : bits_left; result.dwResult <<= bits_to_copy; // Result buffer is shifted by the number of bits to be copied into it - if (bits_to_copy == 8) - { - result.bResult[0] = *p; - bits_of_byte = 8; - p ++; - continue; - } + //if (bits_to_copy == 8) + //{ + // result.dwResult = (uint32_t)*p; + // bits_of_byte = 8; + // p ++; + // continue; + //} uint8_t val = *p; val >>= (8 - bits_of_byte); // Shift by the number of bits already processed + //Serial.print(" bl:"); + //PrintHex(bits_left); + //Serial.print(" sh:"); //PrintHex(8 - bits_of_byte); @@ -1741,7 +1762,8 @@ void ReportDescParser2::OnInputItem(uint8_t itm) result.bResult[0] = (result.bResult[0] | (val & mask)); //Serial.print(" res:"); - //PrintHex(result.bResult[0]); + //Serial.print(": "); + //PrintHex(result.dwResult); //Serial.print(" b2c:"); //PrintHex(bits_to_copy); @@ -1750,13 +1772,120 @@ void ReportDescParser2::OnInputItem(uint8_t itm) //Serial.print(" bob:"); //PrintHex(bits_of_byte); + + if (bits_of_byte < 1) + { + bits_of_byte = 8; + p ++; + } + //Serial.println(""); } - PrintByteValue(result.bResult[0]); + PrintByteValue(result.dwResult); } Serial.println(""); } +//void ReportDescParser2::OnInputItem(uint8_t itm) +//{ +// uint8_t byte_offset = (totalSize >> 3); // calculate offset to the next unhandled byte i = (int)(totalCount / 8); +// uint32_t tmp = (byte_offset << 3); +// uint8_t bit_offset = totalSize - tmp; // number of bits in the current byte already handled +// uint8_t *p = pBuf + byte_offset; // current byte pointer +// +// //Serial.print(" tS:"); +// //PrintHex(totalSize); +// +// //Serial.print(" byO:"); +// //PrintHex(byte_offset); +// +// //Serial.print(" biO:"); +// //PrintHex(bit_offset); +// +// //Serial.print(" rSz:"); +// //PrintHex(rptSize); +// +// //Serial.print(" rCn:"); +// //PrintHex(rptCount); +// +// if (bit_offset) +// *p >>= bit_offset; +// +// uint8_t usage = useMin; +// +// bool print_usemin_usemax = ( (useMin < useMax) && ((itm & 3) == 2) && pfUsage) ? true : false; +// +// uint8_t bits_of_byte = 8; +// +// for (uint8_t i=0; i 8) ? 8 : (bits_left > bits_of_byte) ? bits_of_byte : bits_left; +// +// result.dwResult <<= bits_to_copy; // Result buffer is shifted by the number of bits to be copied into it +// +// if (bits_to_copy == 8) +// { +// result.bResult[0] = *p; +// bits_of_byte = 8; +// p ++; +// continue; +// } +// +// uint8_t val = *p; +// +// val >>= (8 - bits_of_byte); // Shift by the number of bits already processed +// +// //Serial.print(" sh:"); +// //PrintHex(8 - bits_of_byte); +// +// mask = 0; +// +// for (uint8_t j=bits_to_copy; j; j--) +// { +// mask <<= 1; +// mask |= 1; +// } +// +// //Serial.print(" msk:"); +// //PrintHex(mask); +// +// result.bResult[0] = (result.bResult[0] | (val & mask)); +// +// //Serial.print(" res:"); +// //PrintHex(result.bResult[0]); +// +// //Serial.print(" b2c:"); +// //PrintHex(bits_to_copy); +// +// bits_of_byte -= bits_to_copy; +// +// //Serial.print(" bob:"); +// //PrintHex(bits_of_byte); +// } +// +// PrintByteValue(result.bResult[0]); +// } +// Serial.println(""); +//} + void UniversalReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) { ReportDescParser2 prs(len, buf); diff --git a/hidescriptorparser.h b/hidescriptorparser.h index 8dbc4519..48e232ca 100644 --- a/hidescriptorparser.h +++ b/hidescriptorparser.h @@ -24,7 +24,12 @@ e-mail : support@circuitsathome.com #include "usbhost.h" #include "usb_ch9.h" #include "Usb.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif #include "printhex.h" #include "hexdump.h" diff --git a/hiduniversal.cpp b/hiduniversal.cpp index e3dae0eb..231b0955 100644 --- a/hiduniversal.cpp +++ b/hiduniversal.cpp @@ -51,13 +51,13 @@ void HIDUniversal::Initialize() epInfo[i].epAddr = 0; epInfo[i].maxPktSize = (i) ? 0 : 8; epInfo[i].epAttribs = 0; - - if (!i) - epInfo[i].bmNakPower = USB_NAK_MAX_POWER; + epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER; } bNumEP = 1; bNumIface = 0; bConfNum = 0; + + ZeroMemory(constBuffLen, prevBuf); } bool HIDUniversal::SetReportParser(uint8_t id, HIDReportParser *prs) @@ -96,7 +96,6 @@ uint8_t HIDUniversal::Init(uint8_t parent, uint8_t port, bool lowspeed) UsbDevice *p = NULL; EpInfo *oldep_ptr = NULL; uint8_t len = 0; - uint16_t cd_len = 0; uint8_t num_of_conf; // number of configurations uint8_t num_of_intf; // number of interfaces @@ -195,11 +194,11 @@ uint8_t HIDUniversal::Init(uint8_t parent, uint8_t port, bool lowspeed) for (uint8_t i=0; i HexDump; + //HexDumper HexDump; ConfigDescParser confDescrParser(this); - rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump); + //rcode = pUsb->getConfDescr(bAddress, 0, i, &HexDump); rcode = pUsb->getConfDescr(bAddress, 0, i, &confDescrParser); if (bNumEP > 1) @@ -215,7 +214,6 @@ uint8_t HIDUniversal::Init(uint8_t parent, uint8_t port, bool lowspeed) USBTRACE2("\r\nCnf:", bConfNum); // Set Configuration Value - //rcode = pUsb->setConf(bAddress, 0, 0); rcode = pUsb->setConf(bAddress, 0, bConfNum); if (rcode) @@ -226,16 +224,9 @@ uint8_t HIDUniversal::Init(uint8_t parent, uint8_t port, bool lowspeed) if (hidInterfaces[i].epIndex[epInterruptInIndex] == 0) continue; - USBTRACE("Proto\r\n"); - - //rcode = SetProtocol(hidInterfaces[i].bmInterface, HID_RPT_PROTOCOL); - - //if (rcode) - // goto FailSetProtocol; - rcode = SetIdle(hidInterfaces[i].bmInterface, 0, 0); - if (rcode) + if (rcode && rcode != hrSTALL) goto FailSetIdle; } @@ -303,9 +294,9 @@ void HIDUniversal::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint if (bNumEP > 1 && conf != bConfNum) return; - ErrorMessage(PSTR("\r\nConf.Val"), conf); - ErrorMessage(PSTR("Iface Num"), iface); - ErrorMessage(PSTR("Alt.Set"), alt); + //ErrorMessage(PSTR("\r\nConf.Val"), conf); + //ErrorMessage(PSTR("Iface Num"), iface); + //ErrorMessage(PSTR("Alt.Set"), alt); bConfNum = conf; @@ -323,31 +314,24 @@ void HIDUniversal::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint } if ((pep->bmAttributes & 0x03) == 3 && (pep->bEndpointAddress & 0x80) == 0x80) - { - USBTRACE("I8\r\n"); index = epInterruptInIndex; - } else - { - USBTRACE("I0\r\n"); index = epInterruptOutIndex; - } if (index) { - USBTRACE2("Ind:", index); - // Fill in the endpoint info structure epInfo[bNumEP].epAddr = (pep->bEndpointAddress & 0x0F); epInfo[bNumEP].maxPktSize = (uint8_t)pep->wMaxPacketSize; epInfo[bNumEP].epAttribs = 0; + epInfo[bNumEP].bmNakPower = USB_NAK_NOWAIT; // Fill in the endpoint index list piface->epIndex[index] = bNumEP; //(pep->bEndpointAddress & 0x0F); bNumEP ++; } - PrintEndpointDescriptor(pep); + //PrintEndpointDescriptor(pep); } @@ -362,6 +346,25 @@ uint8_t HIDUniversal::Release() return 0; } +bool HIDUniversal::BuffersIdentical(uint8_t len, uint8_t *buf1, uint8_t *buf2) +{ + for (uint8_t i=0; iinTransfer(bAddress, epInfo[index].epAddr, &read, buf); @@ -392,25 +391,31 @@ uint8_t HIDUniversal::Poll() { if (rcode != hrNAK) USBTRACE2("Poll:", rcode); - else - USBTRACE2("poll:", rcode); return rcode; } - Serial.print("Read:"); - PrintHex(read); - Serial.println(""); + + if (read > constBuffLen) + read = constBuffLen; + + bool identical = BuffersIdentical(read, buf, prevBuf); + + SaveBuffer(read, buf, prevBuf); + + if (identical) + return 0; + + Serial.print("\r\nBuf: "); for (uint8_t i=0; i(buf[i]); - if (read) - Serial.println(""); + + Serial.println(""); HIDReportParser *prs = GetReportParser( ((bHasReportId) ? *buf : 0) ); if (prs) prs->Parse(this, bHasReportId, (uint8_t)read, buf); } - } return rcode; } diff --git a/hiduniversal.h b/hiduniversal.h index ba56ddf6..edc9ff9a 100644 --- a/hiduniversal.h +++ b/hiduniversal.h @@ -39,9 +39,16 @@ class HIDUniversal : public HID uint32_t qNextPollTime; // next poll time bool bPollEnable; // poll enable flag + static const uint16_t constBuffLen = 64; // event buffer length + uint8_t prevBuf[constBuffLen]; // previous event buffer + void Initialize(); HIDInterface* FindInterface(uint8_t iface, uint8_t alt, uint8_t proto); + void ZeroMemory(uint8_t len, uint8_t *buf); + bool BuffersIdentical(uint8_t len, uint8_t *buf1, uint8_t *buf2); + void SaveBuffer(uint8_t len, uint8_t *src, uint8_t *dest); + protected: bool bHasReportId; diff --git a/max_LCD.cpp b/max_LCD.cpp index 84f1bec3..307f6409 100644 --- a/max_LCD.cpp +++ b/max_LCD.cpp @@ -20,7 +20,12 @@ e-mail : support@circuitsathome.com #include #include #include -#include "WProgram.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif // pin definition and set/clear diff --git a/message.cpp b/message.cpp index 82ee97de..edb1b5b4 100644 --- a/message.cpp +++ b/message.cpp @@ -23,5 +23,9 @@ void Notify(char const * msg) char c; while((c = pgm_read_byte(msg++))) +#if defined(ARDUINO) && ARDUINO >=100 + Serial.print(c); +#else Serial.print(c,BYTE); +#endif } diff --git a/parsetools.h b/parsetools.h index ba42038d..d19b8dac 100644 --- a/parsetools.h +++ b/parsetools.h @@ -22,8 +22,12 @@ e-mail : support@circuitsathome.com #include "printhex.h" #include "hexdump.h" #include "message.h" -#include +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else +#include +#endif struct MultiValueBuffer { diff --git a/printhex.h b/printhex.h index 9f09adda..7a3b3d9b 100644 --- a/printhex.h +++ b/printhex.h @@ -17,7 +17,11 @@ e-mail : support@circuitsathome.com #if !defined(__PRINTHEX_H__) #define __PRINTHEX_H__ +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif template void PrintHex(T val) diff --git a/usbhost.h b/usbhost.h index 36aa6674..264b317a 100644 --- a/usbhost.h +++ b/usbhost.h @@ -18,7 +18,6 @@ e-mail : support@circuitsathome.com #ifndef _USBHOST_H_ #define _USBHOST_H_ -//#include #include "avrpins.h" #include "max3421e.h" #include "usb_ch9.h" diff --git a/usbhub.cpp b/usbhub.cpp index 4fe427be..761b3a37 100644 --- a/usbhub.cpp +++ b/usbhub.cpp @@ -32,8 +32,9 @@ USBHub::USBHub(USB *p) : epInfo[0].bmNakPower = USB_NAK_MAX_POWER; epInfo[1].epAddr = 1; - epInfo[1].maxPktSize = 1; - epInfo[1].epAttribs = 0; + epInfo[1].maxPktSize = 8; //kludge + epInfo[1].epAttribs = 0; + epInfo[1].bmNakPower = USB_NAK_NOWAIT; if (pUsb) pUsb->RegisterDeviceClass(this); @@ -368,7 +369,7 @@ void PrintHubPortStatus(USBHub *hubptr, uint8_t addr, uint8_t port, bool print_c if (rcode) { - Serial.println("ERROR!!!"); + Serial.println("ERROR!"); return; } Serial.print("\r\nPort "); diff --git a/usbhub.h b/usbhub.h index 8e878821..d34afa43 100644 --- a/usbhub.h +++ b/usbhub.h @@ -24,7 +24,12 @@ e-mail : support@circuitsathome.com #include "usbhost.h" #include "usb_ch9.h" #include "Usb.h" + +#if defined(ARDUINO) && ARDUINO >=100 +#include "Arduino.h" +#else #include +#endif #define USB_DESCRIPTOR_HUB 0x09 // Hub descriptor type