mirror of
https://github.com/felis/USB_Host_Shield_2.0.git
synced 2024-03-22 11:31:26 +01:00
adk final
This commit is contained in:
parent
55b60c59bc
commit
0244032d0d
6 changed files with 165 additions and 76 deletions
2
Usb.cpp
2
Usb.cpp
|
@ -605,7 +605,7 @@ uint8_t USB::getConfDescr( uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser
|
|||
|
||||
uint16_t total = ((USB_CONFIGURATION_DESCRIPTOR*)buf)->wTotalLength;
|
||||
|
||||
USBTRACE2("total:", total);
|
||||
//USBTRACE2("\r\ntotal conf.size:", total);
|
||||
|
||||
return( ctrlReq( addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p ));
|
||||
}
|
||||
|
|
2
Usb.h
2
Usb.h
|
@ -100,7 +100,7 @@ public:
|
|||
#define USB_NAK_LIMIT 32000 //NAK limit for a transfer. o meand 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 //used in Richard's PS2/Wiimote code
|
||||
#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
|
||||
|
|
109
adk.cpp
109
adk.cpp
|
@ -15,6 +15,7 @@ ADK::ADK(USB *p, const char* manufacturer,
|
|||
pUsb(p), //pointer to USB class instance - mandatory
|
||||
bAddress(0), //device address - mandatory
|
||||
bNumEP(1), //if config descriptor needs to be parsed
|
||||
ready(false),
|
||||
|
||||
/* ADK ID Strings */
|
||||
|
||||
|
@ -33,6 +34,9 @@ ADK::ADK(USB *p, const char* manufacturer,
|
|||
epInfo[i].epAttribs = ( 0xfc & ( USB_NAK_MAX_POWER<<2 ));
|
||||
}//for(uint8_t i=0; i<ADK_MAX_ENDPOINTS; i++...
|
||||
|
||||
//set bulk-IN EP naklimit to 1
|
||||
epInfo[epDataInIndex].epAttribs = ( 0xfc & ( USB_NAK_NOWAIT<<2 ));
|
||||
|
||||
// register in USB subsystem
|
||||
if (pUsb) {
|
||||
pUsb->RegisterDeviceClass(this); //set devConfig[] entry
|
||||
|
@ -103,11 +107,11 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
|
|||
p->lowspeed = false;
|
||||
addrPool.FreeAddress(bAddress);
|
||||
bAddress = 0;
|
||||
USBTRACE2("setAddr:",rcode);
|
||||
//USBTRACE2("setAddr:",rcode);
|
||||
return rcode;
|
||||
}//if (rcode...
|
||||
|
||||
USBTRACE2("\r\nAddr:", bAddress);
|
||||
//USBTRACE2("\r\nAddr:", bAddress);
|
||||
|
||||
p->lowspeed = false;
|
||||
|
||||
|
@ -119,7 +123,7 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
|
|||
|
||||
p->lowspeed = lowspeed;
|
||||
|
||||
// Assign epInfo to epinfo pointer
|
||||
// Assign epInfo to epinfo pointer - only EP0 is known
|
||||
rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
|
||||
if (rcode) {
|
||||
goto FailSetDevTblEntry;
|
||||
|
@ -132,7 +136,7 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
|
|||
/* go through configurations, find first bulk-IN, bulk-OUT EP, fill epInfo and quit */
|
||||
num_of_conf = ((USB_DEVICE_DESCRIPTOR*)buf)->bNumConfigurations;
|
||||
|
||||
USBTRACE2("\r\nNC:",num_of_conf);
|
||||
//USBTRACE2("\r\nNC:",num_of_conf);
|
||||
|
||||
for (uint8_t i=0; i<num_of_conf; i++) {
|
||||
ConfigDescParser<0, 0, 0, 0> confDescrParser(this);
|
||||
|
@ -145,27 +149,38 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed)
|
|||
}
|
||||
} // for (uint8_t i=0; i<num_of_conf; i++...
|
||||
|
||||
if( bNumEP == 3 ) {
|
||||
// Assign epInfo to epinfo pointer - this time all 3 endpoins
|
||||
rcode = pUsb->setEpInfoEntry(bAddress, 3, epInfo);
|
||||
if (rcode) {
|
||||
goto FailSetDevTblEntry;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Set Configuration Value
|
||||
rcode = pUsb->setConf(bAddress, 0, bConfNum);
|
||||
if( rcode ){
|
||||
goto FailSetConf;
|
||||
}
|
||||
/* print endpoint structure */
|
||||
USBTRACE("\r\nEndpoint Structure:");
|
||||
USBTRACE("\r\nEP0:");
|
||||
USBTRACE2("\r\nAddr: ", epInfo[0].epAddr );
|
||||
USBTRACE2("\r\nMax.pkt.size: ", epInfo[0].maxPktSize );
|
||||
USBTRACE2("\r\nAttr: ", epInfo[0].epAttribs );
|
||||
USBTRACE("\r\nEpout:");
|
||||
USBTRACE2("\r\nAddr: ", epInfo[epDataOutIndex].epAddr );
|
||||
USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataOutIndex].maxPktSize );
|
||||
USBTRACE2("\r\nAttr: ", epInfo[epDataOutIndex].epAttribs );
|
||||
USBTRACE("\r\nEpin:");
|
||||
USBTRACE2("\r\nAddr: ", epInfo[epDataInIndex].epAddr );
|
||||
USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataInIndex].maxPktSize );
|
||||
USBTRACE2("\r\nAttr: ", epInfo[epDataInIndex].epAttribs );
|
||||
// USBTRACE("\r\nEndpoint Structure:");
|
||||
// USBTRACE("\r\nEP0:");
|
||||
// USBTRACE2("\r\nAddr: ", epInfo[0].epAddr );
|
||||
// USBTRACE2("\r\nMax.pkt.size: ", epInfo[0].maxPktSize );
|
||||
// USBTRACE2("\r\nAttr: ", epInfo[0].epAttribs );
|
||||
// USBTRACE("\r\nEpout:");
|
||||
// USBTRACE2("\r\nAddr: ", epInfo[epDataOutIndex].epAddr );
|
||||
// USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataOutIndex].maxPktSize );
|
||||
// USBTRACE2("\r\nAttr: ", epInfo[epDataOutIndex].epAttribs );
|
||||
// USBTRACE("\r\nEpin:");
|
||||
// USBTRACE2("\r\nAddr: ", epInfo[epDataInIndex].epAddr );
|
||||
// USBTRACE2("\r\nMax.pkt.size: ", epInfo[epDataInIndex].maxPktSize );
|
||||
// USBTRACE2("\r\nAttr: ", epInfo[epDataInIndex].epAttribs );
|
||||
|
||||
USBTRACE("\r\nConfiguration successful");
|
||||
ready = true;
|
||||
return 0; //successful configuration
|
||||
}//if( buf->idVendor == ADK_VID...
|
||||
|
||||
|
@ -234,17 +249,17 @@ FailSetConf:
|
|||
// goto Fail;
|
||||
//
|
||||
Fail:
|
||||
USBTRACE2("\r\nADK Init Failed, error code: ", rcode);
|
||||
//Release();
|
||||
//USBTRACE2("\r\nADK Init Failed, error code: ", rcode);
|
||||
Release();
|
||||
return rcode;
|
||||
}
|
||||
|
||||
/* Extracts bulk-IN and bulk-OUT endpoint information from config descriptor */
|
||||
void ADK::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *pep)
|
||||
{
|
||||
ErrorMessage<uint8_t>(PSTR("Conf.Val"), conf);
|
||||
ErrorMessage<uint8_t>(PSTR("Iface Num"),iface);
|
||||
ErrorMessage<uint8_t>(PSTR("Alt.Set"), alt);
|
||||
//ErrorMessage<uint8_t>(PSTR("Conf.Val"), conf);
|
||||
//ErrorMessage<uint8_t>(PSTR("Iface Num"),iface);
|
||||
//ErrorMessage<uint8_t>(PSTR("Alt.Set"), alt);
|
||||
|
||||
bConfNum = conf;
|
||||
|
||||
|
@ -260,63 +275,25 @@ void ADK::EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto
|
|||
|
||||
bNumEP ++;
|
||||
|
||||
PrintEndpointDescriptor(pep);
|
||||
//PrintEndpointDescriptor(pep);
|
||||
}
|
||||
|
||||
/* Performs a cleanup after failed Init() attempt */
|
||||
uint8_t ADK::Release()
|
||||
{
|
||||
pUsb->GetAddressPool().FreeAddress(bAddress);
|
||||
//
|
||||
// bControlIface = 0;
|
||||
// bDataIface = 0;
|
||||
|
||||
bNumEP = 1; //must have to be reset to 1
|
||||
//
|
||||
|
||||
bAddress = 0;
|
||||
// qNextPollTime = 0;
|
||||
// bPollEnable = false;
|
||||
ready = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//uint8_t ADK::Poll()
|
||||
//{
|
||||
// uint8_t rcode = 0;
|
||||
//
|
||||
// if (!bPollEnable)
|
||||
// return 0;
|
||||
//
|
||||
// uint32_t time_now = millis();
|
||||
//
|
||||
// if (qNextPollTime <= time_now)
|
||||
// {
|
||||
// qNextPollTime = time_now + 100;
|
||||
//
|
||||
// uint8_t rcode;
|
||||
// const uint8_t constBufSize = 16;
|
||||
// uint8_t buf[constBufSize];
|
||||
//
|
||||
// for (uint8_t i=0; i<constBufSize; i++)
|
||||
// buf[i] = 0;
|
||||
//
|
||||
//// uint16_t read = (constBufSize > epInfo[epInterruptInIndex].maxPktSize)
|
||||
//// ? epInfo[epInterruptInIndex].maxPktSize : constBufSize;
|
||||
//// rcode = pUsb->inTransfer(bAddress, epInfo[epInterruptInIndex].epAddr, &read, buf);
|
||||
//
|
||||
// if (rcode)
|
||||
// return rcode;
|
||||
//
|
||||
//// for (uint8_t i=0; i<read; i++)
|
||||
//// {
|
||||
//// PrintHex<uint8_t>(buf[i]);
|
||||
//// Serial.print(" ");
|
||||
//// }
|
||||
//// USBTRACE("\r\n");
|
||||
// }
|
||||
// return rcode;
|
||||
//}
|
||||
|
||||
uint8_t ADK::RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr)
|
||||
{
|
||||
//USBTRACE2("\r\nAddr: ", bAddress );
|
||||
//USBTRACE2("\r\nEP: ",epInfo[epDataInIndex].epAddr);
|
||||
return pUsb->inTransfer(bAddress, epInfo[epDataInIndex].epAddr, bytes_rcvd, dataptr);
|
||||
}
|
||||
|
||||
|
|
8
adk.h
8
adk.h
|
@ -66,12 +66,9 @@ protected:
|
|||
USB *pUsb;
|
||||
uint8_t bAddress;
|
||||
uint8_t bConfNum; // configuration number
|
||||
// uint8_t bControlIface; // Control interface value
|
||||
// uint8_t bDataIface; // Data interface value
|
||||
|
||||
uint8_t bNumEP; // total number of EP in the configuration
|
||||
// uint32_t qNextPollTime; // next poll time
|
||||
// bool bPollEnable; // poll enable flag
|
||||
//uint8_t bInitState; //initialization state machine state
|
||||
bool ready;
|
||||
|
||||
/* Endpoint data structure */
|
||||
EpInfo epInfo[ADK_MAX_ENDPOINTS];
|
||||
|
@ -96,6 +93,7 @@ public:
|
|||
virtual uint8_t Release();
|
||||
virtual uint8_t Poll(){}; //not implemented
|
||||
virtual uint8_t GetAddress() { return bAddress; };
|
||||
virtual bool isReady() { return ready; };
|
||||
|
||||
//UsbConfigXtracter implementation
|
||||
virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
|
||||
|
|
110
examples/adk/demokit_20/demokit_20.pde
Normal file
110
examples/adk/demokit_20/demokit_20.pde
Normal file
|
@ -0,0 +1,110 @@
|
|||
#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 <adk.h>
|
||||
|
||||
#include <printhex.h>
|
||||
#include <message.h>
|
||||
#include <hexdump.h>
|
||||
#include <parsetools.h>
|
||||
|
||||
USB Usb;
|
||||
USBHub hub0(&Usb);
|
||||
USBHub hub1(&Usb);
|
||||
ADK adk(&Usb,"Google, Inc.",
|
||||
"DemoKit",
|
||||
"DemoKit Arduino Board",
|
||||
"1.0",
|
||||
"http://www.android.com",
|
||||
"0000000012345678");
|
||||
uint8_t b, b1;
|
||||
|
||||
|
||||
#define LED1_RED 3
|
||||
#define BUTTON1 2
|
||||
|
||||
void setup();
|
||||
void loop();
|
||||
|
||||
void init_buttons()
|
||||
{
|
||||
pinMode(BUTTON1, INPUT);
|
||||
|
||||
// enable the internal pullups
|
||||
digitalWrite(BUTTON1, HIGH);
|
||||
}
|
||||
|
||||
void init_leds()
|
||||
{
|
||||
digitalWrite(LED1_RED, 0);
|
||||
|
||||
pinMode(LED1_RED, OUTPUT);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("\r\nADK demo start");
|
||||
|
||||
if (Usb.Init() == -1) {
|
||||
Serial.println("OSCOKIRQ failed to assert");
|
||||
while(1); //halt
|
||||
}//if (Usb.Init() == -1...
|
||||
|
||||
|
||||
init_leds();
|
||||
init_buttons();
|
||||
b1 = digitalRead(BUTTON1);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint8_t rcode;
|
||||
uint8_t msg[3] = { 0x00 };
|
||||
Usb.Task();
|
||||
|
||||
if( adk.isReady() == false ) {
|
||||
analogWrite(LED1_RED, 255);
|
||||
return;
|
||||
}
|
||||
uint16_t len = sizeof(msg);
|
||||
|
||||
rcode = adk.RcvData(&len, msg);
|
||||
if( rcode ) {
|
||||
USBTRACE2("Data rcv. :", rcode );
|
||||
}
|
||||
if(len > 0) {
|
||||
USBTRACE("\r\nData Packet.");
|
||||
// assumes only one command per packet
|
||||
if (msg[0] == 0x2) {
|
||||
switch( msg[1] ) {
|
||||
case 0:
|
||||
analogWrite(LED1_RED, 255 - msg[2]);
|
||||
break;
|
||||
}//switch( msg[1]...
|
||||
}//if (msg[0] == 0x2...
|
||||
}//if( len > 0...
|
||||
|
||||
msg[0] = 0x1;
|
||||
|
||||
b = digitalRead(BUTTON1);
|
||||
if (b != b1) {
|
||||
USBTRACE("\r\nButton state changed");
|
||||
msg[1] = 0;
|
||||
msg[2] = b ? 0 : 1;
|
||||
rcode = adk.SndData( 3, msg );
|
||||
if( rcode ) {
|
||||
USBTRACE2("Button send: ", rcode );
|
||||
}
|
||||
b1 = b;
|
||||
}//if (b != b1...
|
||||
|
||||
|
||||
delay( 10 );
|
||||
}
|
|
@ -48,6 +48,8 @@ uint8_t USBHub::Init(uint8_t parent, uint8_t port, bool lowspeed)
|
|||
uint8_t len = 0;
|
||||
uint16_t cd_len = 0;
|
||||
|
||||
//USBTRACE("\r\nHub Init Start");
|
||||
|
||||
AddressPool &addrPool = pUsb->GetAddressPool();
|
||||
|
||||
switch (bInitState)
|
||||
|
@ -114,6 +116,8 @@ uint8_t USBHub::Init(uint8_t parent, uint8_t port, bool lowspeed)
|
|||
return rcode;
|
||||
}
|
||||
|
||||
//USBTRACE2("\r\nHub address: ", bAddress );
|
||||
|
||||
// Restore p->epinfo
|
||||
p->epinfo = oldep_ptr;
|
||||
|
||||
|
|
Loading…
Reference in a new issue