From 0244032d0d547c81ed81c09328e58cda54bb9769 Mon Sep 17 00:00:00 2001 From: Oleg Mazurov Date: Wed, 6 Jul 2011 19:48:43 -0600 Subject: [PATCH] adk final --- Usb.cpp | 4 +- Usb.h | 2 +- adk.cpp | 109 ++++++++++-------------- adk.h | 8 +- examples/adk/demokit_20/demokit_20.pde | 110 +++++++++++++++++++++++++ usbhub.cpp | 8 +- 6 files changed, 165 insertions(+), 76 deletions(-) create mode 100644 examples/adk/demokit_20/demokit_20.pde diff --git a/Usb.cpp b/Usb.cpp index 8d51be40..7a5a6477 100644 --- a/Usb.cpp +++ b/Usb.cpp @@ -225,7 +225,7 @@ uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, ui while( 1 ) // use a 'return' to exit this loop { rcode = dispatchPkt( tokIN, pep->epAddr, nak_limit ); //IN packet to EP-'endpoint'. Function takes care of NAKS. - + if( rcode ) return( rcode ); //should be 0, indicating ACK. Else return error code. @@ -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 )); } diff --git a/Usb.h b/Usb.h index 5a81e80c..8676589f 100644 --- a/Usb.h +++ b/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 diff --git a/adk.cpp b/adk.cpp index 9452d45f..ce49e350 100644 --- a/adk.cpp +++ b/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; iRegisterDeviceClass(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 confDescrParser(this); @@ -144,6 +148,16 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) break; } } // for (uint8_t i=0; isetEpInfoEntry(bAddress, 3, epInfo); + if (rcode) { + goto FailSetDevTblEntry; + } + } + + // Set Configuration Value rcode = pUsb->setConf(bAddress, 0, bConfNum); @@ -151,21 +165,22 @@ uint8_t ADK::Init(uint8_t parent, uint8_t port, bool lowspeed) 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(PSTR("Conf.Val"), conf); - ErrorMessage(PSTR("Iface Num"),iface); - ErrorMessage(PSTR("Alt.Set"), alt); + //ErrorMessage(PSTR("Conf.Val"), conf); + //ErrorMessage(PSTR("Iface Num"),iface); + //ErrorMessage(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 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(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); } diff --git a/adk.h b/adk.h index 210c3d05..2d41f9da 100644 --- a/adk.h +++ b/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); diff --git a/examples/adk/demokit_20/demokit_20.pde b/examples/adk/demokit_20/demokit_20.pde new file mode 100644 index 00000000..b1f47294 --- /dev/null +++ b/examples/adk/demokit_20/demokit_20.pde @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +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 ); +} diff --git a/usbhub.cpp b/usbhub.cpp index 2ec1f754..4fe427be 100644 --- a/usbhub.cpp +++ b/usbhub.cpp @@ -47,9 +47,11 @@ uint8_t USBHub::Init(uint8_t parent, uint8_t port, bool lowspeed) EpInfo *oldep_ptr = NULL; uint8_t len = 0; uint16_t cd_len = 0; + + //USBTRACE("\r\nHub Init Start"); AddressPool &addrPool = pUsb->GetAddressPool(); - + switch (bInitState) { case 0: @@ -113,7 +115,9 @@ uint8_t USBHub::Init(uint8_t parent, uint8_t port, bool lowspeed) bAddress = 0; return rcode; } - + + //USBTRACE2("\r\nHub address: ", bAddress ); + // Restore p->epinfo p->epinfo = oldep_ptr;