BaseUsbHost example program

Dependencies:   BaseUsbHost FATFileSystem mbed mbed-rtos

Revision:
1:80205a2de336
Parent:
0:2a9734a95d55
Child:
2:c10029b87439
--- a/LogitechC270/LogitechC270.cpp	Tue Dec 04 13:39:57 2012 +0000
+++ b/LogitechC270/LogitechC270.cpp	Wed Dec 05 13:25:18 2012 +0000
@@ -1,4 +1,4 @@
-// LogitechC270.cpp 2012/12/4
+// LogitechC270.cpp 2012/12/5
 #include "mbed.h"
 #include "rtos.h"
 #include "BaseUsbHost.h"
@@ -8,30 +8,22 @@
 #include "BaseUsbHostTest.h"
 #include "LogitechC270.h"
 
-LogitechC270::LogitechC270(int frame, uint32_t interval, ControlEp* ctlEp)
+LogitechC270::LogitechC270(int frameIndex, uint32_t interval, ControlEp* ctlEp)
 {
     uint8_t buf[26];
     
     if (ctlEp == NULL) { // root hub
         DBG_OHCI(LPC_USB->HcRhPortStatus1);
         TEST_ASSERT_FALSE(LPC_USB->HcRhPortStatus1 & 0x200);
-        m_ctlEp = new ControlEp();
-        TEST_ASSERT_TRUE(m_ctlEp);
-    } else {
-        m_ctlEp = ctlEp;
+        ctlEp = new ControlEp();
+        TEST_ASSERT_TRUE(ctlEp);
     }
-
-    int r = m_ctlEp->GetDescriptor(1, 0, buf, 18);
-    TEST_ASSERT(r == USB_OK);
-    DBG_HEX(buf, 18);
-    vid = *reinterpret_cast<uint16_t*>(buf+8);
-    pid = *reinterpret_cast<uint16_t*>(buf+10);
-    DBG("VID PID: %04X %04X\n", vid, pid);
-    TEST_ASSERT(vid == 0x046d && pid == 0x0825);
+    bool r = check(ctlEp);
+    TEST_ASSERT(r);
+    m_ctlEp = ctlEp;
+    
     int addr = m_ctlEp->GetAddr();
-    DBG("addr: %d\n", addr);
-
-    m_isoEp = new IsochronousEp(addr, 0x81, 192);
+    m_isoEp = new IsochronousEp(addr, C270_EN, C270_MPS);
     TEST_ASSERT_TRUE(m_isoEp);
 
     int rc = Control(GET_INFO, VS_PROBE_CONTROL, 1, buf, 1);
@@ -56,7 +48,7 @@
 
     memset(buf, 0, 26);
     buf[2] = C270_MJPEG;
-    buf[3] = frame;
+    buf[3] = frameIndex;
     *reinterpret_cast<uint32_t*>(buf+4) = interval;
     
     DBG_BYTES("SET_CUR Commit", buf, 26);
@@ -66,7 +58,7 @@
     rc = Control(GET_CUR, VS_COMMIT_CONTROL, 1, buf, 26);
     TEST_ASSERT(rc == USB_OK);
     TEST_ASSERT_EQUAL(buf[2], C270_MJPEG);
-    TEST_ASSERT_EQUAL(buf[3], frame);
+    TEST_ASSERT_EQUAL(buf[3], frameIndex);
     TEST_ASSERT_EQUAL(*reinterpret_cast<uint32_t*>(buf+4), interval);
     DBG_BYTES("GET_CUR Commit", buf, 26);
 
@@ -88,7 +80,7 @@
     TEST_ASSERT_EQUAL(rc, USB_OK);
     DBG("alt: %d\n", alt);
 
-    rc = m_ctlEp->SetInterfaceAlternate(1, 1); // alt=1 packet size = 192
+    rc = m_ctlEp->SetInterfaceAlternate(1, C270_IF_ALT); // alt=1 packet size = 192
     TEST_ASSERT_EQUAL(rc, USB_OK);
 
     rc = m_ctlEp->GetInterface(1, &alt);
@@ -105,53 +97,32 @@
     LPC_USB->HcControl |= OR_CONTROL_IE;  // IsochronousEnable
 }
 
-void LogitechC270::poll()
+bool LogitechC270::check(ControlEp* ctlEp)
 {
-    HCITD* itd = m_isoEp->read();
-    if (itd) {
-        uint8_t cc = itd->Control>>28;
-        report_cc_count[cc]++;
-        if (cc == 0) { // ConditionCode
-            //DBG_ITD(itd);
-            uint16_t frame = itd->Control & 0xffff;
-            uint8_t* buf = const_cast<uint8_t*>(itd->buf); 
-            int mps = m_isoEp->m_PacketSize;
-            for(int i = 0; i < m_isoEp->m_FrameCount; i++) {
-                uint16_t pswn = itd->OffsetPSW[i];
-                cc = pswn>>12;
-                if (cc == 0 || cc == 9) {
-                    int len = pswn & 0x7ff;
-                    onResult(frame, buf, len);
-               }
-               report_ps_cc_count[cc]++;
-               buf += mps;
-                frame++;
-            }
-        }
-        m_isoEp->delete_HCTD(reinterpret_cast<HCTD*>(itd));
+    if (ctlEp == NULL) {
+        return false;
+    }
+    uint8_t buf[18];
+    int r = ctlEp->GetDescriptor(1, 0, buf, 8);
+    if (r != USB_OK) {
+        return false;
+    }
+    DBG_HEX(buf, 8);
+    const uint8_t desc[] = {0x12,0x01,0x00,0x02,0xef,0x02,0x01,0x40};
+    if (memcmp(buf, desc, sizeof(desc)) != 0) {
+        return false;
     }
+    r = ctlEp->GetDescriptor(1, 0, buf, 18);
+    if (r != USB_OK) {
+        return false;
+    }
+    DBG_HEX(buf, 18);
+    uint16_t vid = *reinterpret_cast<uint16_t*>(buf+8);
+    uint16_t pid = *reinterpret_cast<uint16_t*>(buf+10);
+    DBG("VID PID: %04X %04X\n", vid, pid);
+    if (vid == C270_VID && pid == C270_PID) {
+        return true;
+    }
+    return false;
 }
 
-int LogitechC270::Control(int req, int cs, int index, uint8_t* buf, int size)
-{
-    TEST_ASSERT(m_ctlEp);
-    int rc;
-    if (req == SET_CUR) {    
-        rc = m_ctlEp->controlSend(
-                    USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE, 
-                    req, cs<<8, index, buf, size);
-        return rc;
-    }
-    rc = m_ctlEp->controlReceive(
-                USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE, 
-                req, cs<<8, index, buf, size);
-    return rc;
-}
-
-void LogitechC270::onResult(uint16_t frame, uint8_t* buf, int len)
-{
-  if(m_pCbItem && m_pCbMeth)
-    (m_pCbItem->*m_pCbMeth)(frame, buf, len);
-  else if(m_pCb)
-    m_pCb(frame, buf, len);
-}