BaseUsbHost example program

Dependencies:   BaseUsbHost FATFileSystem mbed mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LogitechC270.cpp Source File

LogitechC270.cpp

00001 // LogitechC270.cpp 2013/1/25
00002 #include "mbed.h"
00003 #include "rtos.h"
00004 #include "BaseUsbHost.h"
00005 //#define DEBUG
00006 #include "BaseUsbHostDebug.h"
00007 #define TEST
00008 #include "BaseUsbHostTest.h"
00009 #include "LogitechC270.h"
00010 
00011 LogitechC270::LogitechC270(int formatIndex, int frameIndex, uint32_t interval, ControlEp* ctlEp)
00012 {
00013     uint8_t buf[26];
00014     
00015     if (ctlEp == NULL) { // root hub
00016         DBG_OHCI(LPC_USB->HcRhPortStatus1);
00017         TEST_ASSERT_FALSE(LPC_USB->HcRhPortStatus1 & 0x200);
00018         ctlEp = new ControlEp();
00019         TEST_ASSERT_TRUE(ctlEp);
00020     }
00021     bool r = check(ctlEp);
00022     TEST_ASSERT(r);
00023     m_ctlEp = ctlEp;
00024     
00025     int addr = m_ctlEp->GetAddr();
00026     m_isoEp = new IsochronousEp(addr, C270_EN, C270_MPS);
00027     TEST_ASSERT_TRUE(m_isoEp);
00028 
00029     int rc = Control(GET_INFO, VS_PROBE_CONTROL, 1, buf, 1);
00030     TEST_ASSERT(rc == USB_OK);
00031     DBG_BYTES("GET_INFO Prob", buf, 1);
00032 
00033     rc = Control(GET_DEF, VS_PROBE_CONTROL, 1, buf, 26);
00034     TEST_ASSERT(rc == USB_OK);
00035     DBG_BYTES("GET_DEF Probe", buf, 26);
00036 
00037     rc = Control(GET_MIN, VS_PROBE_CONTROL, 1, buf, 26);
00038     TEST_ASSERT(rc == USB_OK);
00039     DBG_BYTES("GET_MIN Probe", buf, 26);
00040 
00041     rc = Control(GET_MAX, VS_PROBE_CONTROL, 1, buf, 26);
00042     TEST_ASSERT(rc == USB_OK);
00043     DBG_BYTES("GET_MAX Probe", buf, 26);
00044 
00045     rc = Control(GET_CUR, VS_PROBE_CONTROL, 1, buf, 26);
00046     TEST_ASSERT(rc == USB_OK);
00047     DBG_BYTES("GET_CUR Probe", buf, 26);
00048 
00049     memset(buf, 0, 26);
00050     buf[2] = formatIndex;
00051     buf[3] = frameIndex;
00052     *reinterpret_cast<uint32_t*>(buf+4) = interval;
00053     
00054     DBG_BYTES("SET_CUR Commit", buf, 26);
00055     rc = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, 26);
00056     TEST_ASSERT(rc == USB_OK);
00057 
00058     rc = Control(GET_CUR, VS_COMMIT_CONTROL, 1, buf, 26);
00059     TEST_ASSERT(rc == USB_OK);
00060     TEST_ASSERT_EQUAL(buf[2], formatIndex);
00061     TEST_ASSERT_EQUAL(buf[3], frameIndex);
00062     TEST_ASSERT_EQUAL(*reinterpret_cast<uint32_t*>(buf+4), interval);
00063     DBG_BYTES("GET_CUR Commit", buf, 26);
00064 
00065     int value;
00066     rc = m_ctlEp->GetConfiguration(&value);
00067     TEST_ASSERT_EQUAL(rc, USB_OK);
00068     DBG("config: %d\n", value);
00069 
00070     rc = m_ctlEp->SetConfiguration(1);
00071     TEST_ASSERT_EQUAL(rc, USB_OK);
00072 
00073     rc = m_ctlEp->GetConfiguration(&value);
00074     TEST_ASSERT_EQUAL(rc, USB_OK);
00075     DBG("config: %d\n", value);
00076     TEST_ASSERT_EQUAL(value, 1);
00077 
00078     rc = m_ctlEp->GetInterface(1, &value);
00079     TEST_ASSERT_EQUAL(rc, USB_OK);
00080     DBG("alt: %d\n", value);
00081 
00082     rc = m_ctlEp->SetInterfaceAlternate(1, C270_IF_ALT); // alt=1 packet size = 192
00083     TEST_ASSERT_EQUAL(rc, USB_OK);
00084 
00085     rc = m_ctlEp->GetInterface(1, &value);
00086     TEST_ASSERT_EQUAL(rc, USB_OK);
00087     DBG("alt: %d\n", value);
00088     TEST_ASSERT_EQUAL(value, C270_IF_ALT);
00089 
00090     for(int i = 0; i < 16; i++) {
00091         report_cc_count[i] = 0;
00092         report_ps_cc_count[i] = 0;
00093     }
00094   
00095     LPC_USB->HcControl |= OR_CONTROL_PLE; // PeriodicListEnable
00096     LPC_USB->HcControl |= OR_CONTROL_IE;  // IsochronousEnable
00097 }
00098 
00099 bool LogitechC270::check(ControlEp* ctlEp)
00100 {
00101     if (ctlEp == NULL) {
00102         return false;
00103     }
00104     uint8_t buf[18];
00105     int r = ctlEp->GetDescriptor(1, 0, buf, 8);
00106     if (r != USB_OK) {
00107         return false;
00108     }
00109     DBG_HEX(buf, 8);
00110     const uint8_t desc[] = {0x12,0x01,0x00,0x02,0xef,0x02,0x01,0x40};
00111     if (memcmp(buf, desc, sizeof(desc)) != 0) {
00112         return false;
00113     }
00114     r = ctlEp->GetDescriptor(1, 0, buf, 18);
00115     if (r != USB_OK) {
00116         return false;
00117     }
00118     DBG_HEX(buf, 18);
00119     uint16_t vid = *reinterpret_cast<uint16_t*>(buf+8);
00120     uint16_t pid = *reinterpret_cast<uint16_t*>(buf+10);
00121     DBG("VID PID: %04X %04X\n", vid, pid);
00122     if (vid == C270_VID && pid == C270_PID) {
00123         return true;
00124     }
00125     return false;
00126 }