Skip to content

Commit

Permalink
Arduino-1.0 compatibility added
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Mazurov committed Jan 10, 2012
1 parent 7972c40 commit 2c728cd
Show file tree
Hide file tree
Showing 39 changed files with 489 additions and 228 deletions.
22 changes: 16 additions & 6 deletions Usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WProgram.h>
#endif

static uint8_t usb_error = 0;
static uint8_t usb_task_state;
Expand Down Expand Up @@ -102,17 +107,22 @@ 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 );

// 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;
}

Expand Down Expand Up @@ -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
{
Expand Down
11 changes: 9 additions & 2 deletions Usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WProgram.h>
#endif

#include "printhex.h"
#include "hexdump.h"
Expand All @@ -48,10 +53,12 @@ typedef MAX3421e<P6, P3> MAX3421E; // Black Widow
typedef MAX3421e<P10, P9> 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'
Expand Down Expand Up @@ -94,6 +101,7 @@ typedef MAX3421e<P10, P9> 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
{
Expand All @@ -105,10 +113,9 @@ class USBDeviceConfig
};

#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
Expand Down
12 changes: 9 additions & 3 deletions address.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ e-mail : support@circuitsathome.com
#include <stddef.h>
#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
{
Expand All @@ -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
};
};
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions adk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WProgram.h>
#endif

#include "printhex.h"
#include "hexdump.h"
Expand Down
6 changes: 4 additions & 2 deletions cdcacm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions cdcacm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WProgram.h>
#endif

#include "printhex.h"
#include "hexdump.h"
Expand Down
2 changes: 1 addition & 1 deletion cdcftdi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions cdcftdi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WProgram.h>
#endif

#include "printhex.h"
#include "hexdump.h"
Expand Down
4 changes: 2 additions & 2 deletions cdcprolific.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 7 additions & 2 deletions cdcprolific.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <WProgram.h>
#endif

#include "printhex.h"
#include "hexdump.h"
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions confdescparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ bool ConfigDescParser<CLASS_ID, SUBCLASS_ID, PROTOCOL_ID, MASK>::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;
Expand Down
2 changes: 1 addition & 1 deletion examples/HID/USBHIDBootKbd/USBHIDBootKbd.pde
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions examples/HID/USBHIDBootMouse/USBHIDBootMouse.pde
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <avr/pgmspace.h>
#include <address.h>
#include <hidboot.h>

#include <printhex.h>
#include <message.h>
#include <hexdump.h>
Expand Down Expand Up @@ -58,12 +57,12 @@ void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
};

USB Usb;
//USBHub Hub(&Usb);
USBHub Hub(&Usb);
HIDBoot<HID_PROTOCOL_MOUSE> Mouse(&Usb);

uint32_t next_time;

MouseRptParser Prs;
MouseRptParser Prs;

void setup()
{
Expand All @@ -77,7 +76,7 @@ void setup()

next_time = millis() + 5000;

Mouse.SetReportParser(0, (HIDReportParser*)&Prs);
Mouse.SetReportParser(0,(HIDReportParser*)&Prs);
}

void loop()
Expand Down
44 changes: 44 additions & 0 deletions examples/HID/USBHID_desc/USBHID_desc.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <avr/pgmspace.h>

#include <avrpins.h>
#include <max3421e.h>
#include <usbhost.h>
#include <usb_ch9.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <address.h>
#include <hid.h>
#include <hiduniversal.h>
#include <hidescriptorparser.h>
#include <printhex.h>
#include <message.h>
#include <hexdump.h>
#include <parsetools.h>

#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<uint8_t>(PSTR("SetReportParser"), 1 );
}

void loop()
{
Usb.Task();
}

52 changes: 52 additions & 0 deletions examples/HID/USBHID_desc/pgmstrings.h
Original file line number Diff line number Diff line change
@@ -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__
Loading

0 comments on commit 2c728cd

Please sign in to comment.