testing n-Bed with a Logitech C270 camera

Dependencies:   USBHost mbed

Fork of USBHostC270_example by Norimasa Okamoto

Revision:
9:fecabade834a
Child:
10:387c49b2fc7e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHostC270/USBHostC270.cpp	Sat Mar 16 13:07:55 2013 +0000
@@ -0,0 +1,129 @@
+#include "USBHostC270.h"
+#include "dbg.h"
+
+// ------------------ HcControl Register ---------------------
+#define  OR_CONTROL_IE                  0x00000008
+
+USBHostC270::USBHostC270(int formatIndex, int frameIndex, uint32_t interval)
+{
+    _formatIndex = formatIndex;
+    _frameIndex = frameIndex;
+    _interval = interval;
+    host = USBHost::getHostInst();
+    init();
+}
+
+void USBHostC270::init()
+{
+    C270_DBG("");
+    dev_connected = false;
+    dev = NULL;
+    c270_intf = -1;
+    c270_device_found = false;
+    c270_vid_pid_found = false;
+    clearOnResult();
+}
+
+bool USBHostC270::connected()
+{
+    C270_DBG("");
+    return dev_connected;
+}
+
+bool USBHostC270::connect()
+{
+    C270_DBG("");
+    if (dev_connected) {
+        return true;
+    }
+
+    for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+        if ((dev = host->getDevice(i)) != NULL) {
+            
+            C270_DBG("Trying to connect C270 device\r\n");
+            
+            if(host->enumerate(dev, this))
+                break;
+
+            if (c270_device_found) {
+                USB_INFO("New C270 device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, c270_intf);
+                dev->setName("C270", c270_intf);
+                host->registerDriver(dev, c270_intf, this, &USBHostC270::init);
+                int addr = dev->getAddress();
+                m_isoEp = new IsochronousEp(addr, C270_EN, C270_MPS);
+                uint8_t buf[26];
+                memset(buf, 0, sizeof(buf));
+                buf[2] = _formatIndex;
+                buf[3] = _frameIndex;
+                *reinterpret_cast<uint32_t*>(buf+4) = _interval;
+                USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf));
+                if (res != USB_TYPE_OK) {
+                    C270_DBG("SET_CUR VS_COMMIT_CONTROL FAILED");
+                }
+                res = setInterfaceAlternate(1, C270_IF_ALT); // alt=1 packet size = 192
+                if (res != USB_TYPE_OK) {
+                    C270_DBG("SET_INTERFACE FAILED");
+                }
+                for(int i = 0; i < 16; i++) {
+                    report_cc_count[i] = 0;
+                    report_ps_cc_count[i] = 0;
+                }
+                LPC_USB->HcControl |= OR_CONTROL_PLE; // PeriodicListEnable
+                LPC_USB->HcControl |= OR_CONTROL_IE;  // IsochronousEnable
+
+                dev_connected = true;
+                return true;
+            }
+        }
+    }
+    init();
+    return false;
+}
+
+/*virtual*/ void USBHostC270::setVidPid(uint16_t vid, uint16_t pid)
+{
+    C270_DBG("vid:%04x,pid:%04x", vid, pid);
+    if (vid == C270_VID && pid == C270_PID) { 
+        c270_vid_pid_found = true;
+    } else {
+        c270_vid_pid_found = false;
+    }
+}
+
+/*virtual*/ bool USBHostC270::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
+{
+    C270_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol);
+    if ((c270_intf == -1) && c270_vid_pid_found) {
+        c270_intf = intf_nb;
+        c270_device_found = true;
+        return true;
+    }
+    return false;
+}
+
+/*virtual*/ bool USBHostC270::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
+{
+    C270_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir);
+    return false;
+}
+
+USB_TYPE USBHostC270::setInterfaceAlternate(uint8_t intf, uint8_t alt)
+{
+    C270_DBG("intf:%d, alt:%d", intf, alt);
+    return host->controlWrite(dev, USB_HOST_TO_DEVICE | USB_RECIPIENT_INTERFACE,
+                                   SET_INTERFACE, alt, intf, NULL, 0);
+}
+
+USB_TYPE USBHostC270::Control(int req, int cs, int index, uint8_t* buf, int size)
+{
+    C270_DBG("req:%d,cs:%d,index:%d", req, cs,index);
+    if (req == SET_CUR) {    
+        return host->controlWrite(dev,
+                    USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE, 
+                    req, cs<<8, index, buf, size);
+    }
+    return host->controlRead(dev,
+                USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE, 
+                req, cs<<8, index, buf, size);
+}
+