![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
LogitechC270 webcam class driver alpha version
Fork of USBHostMSD_HelloWorld by
USBHostCam/USBHostCam.cpp@12:ea4badc78215, 2013-03-20 (annotated)
- Committer:
- va009039
- Date:
- Wed Mar 20 14:28:39 2013 +0000
- Revision:
- 12:ea4badc78215
support detach
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 12:ea4badc78215 | 1 | // USBHostCam.cpp |
va009039 | 12:ea4badc78215 | 2 | #include "USBHostCam.h" |
va009039 | 12:ea4badc78215 | 3 | #include "dbg.h" |
va009039 | 12:ea4badc78215 | 4 | |
va009039 | 12:ea4badc78215 | 5 | //#define CAM_DEBUG 1 |
va009039 | 12:ea4badc78215 | 6 | #ifdef CAM_DEBUG |
va009039 | 12:ea4badc78215 | 7 | #define CAM_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); |
va009039 | 12:ea4badc78215 | 8 | #else |
va009039 | 12:ea4badc78215 | 9 | #define CAM_DBG(...) while(0); |
va009039 | 12:ea4badc78215 | 10 | #endif |
va009039 | 12:ea4badc78215 | 11 | |
va009039 | 12:ea4badc78215 | 12 | // ------------------ HcControl Register --------------------- |
va009039 | 12:ea4badc78215 | 13 | #define OR_CONTROL_IE 0x00000008 |
va009039 | 12:ea4badc78215 | 14 | |
va009039 | 12:ea4badc78215 | 15 | CamInfo* getCamInfoList(); // CamInfo.cpp |
va009039 | 12:ea4badc78215 | 16 | |
va009039 | 12:ea4badc78215 | 17 | USBHostCam::USBHostCam(uint8_t size, uint8_t option, CamInfo* user_caminfo) |
va009039 | 12:ea4badc78215 | 18 | { |
va009039 | 12:ea4badc78215 | 19 | CAM_DBG("size: %d, option: %d", size, option); |
va009039 | 12:ea4badc78215 | 20 | _caminfo_size = size; |
va009039 | 12:ea4badc78215 | 21 | _caminfo_option = option; |
va009039 | 12:ea4badc78215 | 22 | if (user_caminfo) { |
va009039 | 12:ea4badc78215 | 23 | CamInfoList = user_caminfo; |
va009039 | 12:ea4badc78215 | 24 | } else { |
va009039 | 12:ea4badc78215 | 25 | CamInfoList = getCamInfoList(); |
va009039 | 12:ea4badc78215 | 26 | } |
va009039 | 12:ea4badc78215 | 27 | clearOnResult(); |
va009039 | 12:ea4badc78215 | 28 | host = USBHost::getHostInst(); |
va009039 | 12:ea4badc78215 | 29 | m_isoEp = new IsochronousEp; |
va009039 | 12:ea4badc78215 | 30 | init(); |
va009039 | 12:ea4badc78215 | 31 | } |
va009039 | 12:ea4badc78215 | 32 | |
va009039 | 12:ea4badc78215 | 33 | void USBHostCam::init() |
va009039 | 12:ea4badc78215 | 34 | { |
va009039 | 12:ea4badc78215 | 35 | CAM_DBG(""); |
va009039 | 12:ea4badc78215 | 36 | dev_connected = false; |
va009039 | 12:ea4badc78215 | 37 | dev = NULL; |
va009039 | 12:ea4badc78215 | 38 | cam_intf = -1; |
va009039 | 12:ea4badc78215 | 39 | device_found = false; |
va009039 | 12:ea4badc78215 | 40 | caminfo_found = false; |
va009039 | 12:ea4badc78215 | 41 | } |
va009039 | 12:ea4badc78215 | 42 | |
va009039 | 12:ea4badc78215 | 43 | bool USBHostCam::connected() |
va009039 | 12:ea4badc78215 | 44 | { |
va009039 | 12:ea4badc78215 | 45 | return dev_connected; |
va009039 | 12:ea4badc78215 | 46 | } |
va009039 | 12:ea4badc78215 | 47 | |
va009039 | 12:ea4badc78215 | 48 | bool USBHostCam::connect() |
va009039 | 12:ea4badc78215 | 49 | { |
va009039 | 12:ea4badc78215 | 50 | if (dev_connected) { |
va009039 | 12:ea4badc78215 | 51 | return true; |
va009039 | 12:ea4badc78215 | 52 | } |
va009039 | 12:ea4badc78215 | 53 | |
va009039 | 12:ea4badc78215 | 54 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
va009039 | 12:ea4badc78215 | 55 | if ((dev = host->getDevice(i)) != NULL) { |
va009039 | 12:ea4badc78215 | 56 | |
va009039 | 12:ea4badc78215 | 57 | CAM_DBG("Trying to connect Cam device\r\n"); |
va009039 | 12:ea4badc78215 | 58 | |
va009039 | 12:ea4badc78215 | 59 | if(host->enumerate(dev, this)) { |
va009039 | 12:ea4badc78215 | 60 | break; |
va009039 | 12:ea4badc78215 | 61 | } |
va009039 | 12:ea4badc78215 | 62 | if (device_found) { |
va009039 | 12:ea4badc78215 | 63 | USB_INFO("New Cam: %s device: VID:%04x PID:%04x [dev: %p - intf: %d]", caminfo->name, dev->getVid(), dev->getPid(), dev, cam_intf); |
va009039 | 12:ea4badc78215 | 64 | dev->setName(caminfo->name, cam_intf); |
va009039 | 12:ea4badc78215 | 65 | host->registerDriver(dev, cam_intf, this, &USBHostCam::onDisconnect); |
va009039 | 12:ea4badc78215 | 66 | int addr = dev->getAddress(); |
va009039 | 12:ea4badc78215 | 67 | m_isoEp->init(addr, caminfo->en, caminfo->mps, caminfo->frameCount, caminfo->queueLimit); |
va009039 | 12:ea4badc78215 | 68 | uint8_t buf[26]; |
va009039 | 12:ea4badc78215 | 69 | memset(buf, 0, sizeof(buf)); |
va009039 | 12:ea4badc78215 | 70 | buf[2] = caminfo->formatIndex; |
va009039 | 12:ea4badc78215 | 71 | buf[3] = caminfo->frameIndex; |
va009039 | 12:ea4badc78215 | 72 | *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval; |
va009039 | 12:ea4badc78215 | 73 | USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf)); |
va009039 | 12:ea4badc78215 | 74 | if (res != USB_TYPE_OK) { |
va009039 | 12:ea4badc78215 | 75 | CAM_DBG("SET_CUR VS_COMMIT_CONTROL FAILED"); |
va009039 | 12:ea4badc78215 | 76 | } |
va009039 | 12:ea4badc78215 | 77 | res = setInterfaceAlternate(1, caminfo->if_alt); |
va009039 | 12:ea4badc78215 | 78 | if (res != USB_TYPE_OK) { |
va009039 | 12:ea4badc78215 | 79 | CAM_DBG("SET_INTERFACE FAILED"); |
va009039 | 12:ea4badc78215 | 80 | } |
va009039 | 12:ea4badc78215 | 81 | for(int i = 0; i < 16; i++) { |
va009039 | 12:ea4badc78215 | 82 | report_cc_count[i] = 0; |
va009039 | 12:ea4badc78215 | 83 | report_ps_cc_count[i] = 0; |
va009039 | 12:ea4badc78215 | 84 | } |
va009039 | 12:ea4badc78215 | 85 | LPC_USB->HcControl |= OR_CONTROL_PLE; // PeriodicListEnable |
va009039 | 12:ea4badc78215 | 86 | LPC_USB->HcControl |= OR_CONTROL_IE; // IsochronousEnable |
va009039 | 12:ea4badc78215 | 87 | |
va009039 | 12:ea4badc78215 | 88 | dev_connected = true; |
va009039 | 12:ea4badc78215 | 89 | return true; |
va009039 | 12:ea4badc78215 | 90 | } |
va009039 | 12:ea4badc78215 | 91 | } |
va009039 | 12:ea4badc78215 | 92 | } |
va009039 | 12:ea4badc78215 | 93 | init(); |
va009039 | 12:ea4badc78215 | 94 | return false; |
va009039 | 12:ea4badc78215 | 95 | } |
va009039 | 12:ea4badc78215 | 96 | |
va009039 | 12:ea4badc78215 | 97 | void USBHostCam::onDisconnect() |
va009039 | 12:ea4badc78215 | 98 | { |
va009039 | 12:ea4badc78215 | 99 | CAM_DBG("dev_connected: %d", dev_connected); |
va009039 | 12:ea4badc78215 | 100 | if (dev_connected) { |
va009039 | 12:ea4badc78215 | 101 | m_isoEp->disconnect(); |
va009039 | 12:ea4badc78215 | 102 | init(); |
va009039 | 12:ea4badc78215 | 103 | } |
va009039 | 12:ea4badc78215 | 104 | } |
va009039 | 12:ea4badc78215 | 105 | |
va009039 | 12:ea4badc78215 | 106 | /*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid) |
va009039 | 12:ea4badc78215 | 107 | { |
va009039 | 12:ea4badc78215 | 108 | CAM_DBG("vid:%04x,pid:%04x", vid, pid); |
va009039 | 12:ea4badc78215 | 109 | caminfo = CamInfoList; |
va009039 | 12:ea4badc78215 | 110 | while(caminfo->vid != 0) { |
va009039 | 12:ea4badc78215 | 111 | if (caminfo->vid == vid && caminfo->pid == pid && |
va009039 | 12:ea4badc78215 | 112 | caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) { |
va009039 | 12:ea4badc78215 | 113 | caminfo_found = true; |
va009039 | 12:ea4badc78215 | 114 | break; |
va009039 | 12:ea4badc78215 | 115 | } |
va009039 | 12:ea4badc78215 | 116 | caminfo++; |
va009039 | 12:ea4badc78215 | 117 | } |
va009039 | 12:ea4badc78215 | 118 | } |
va009039 | 12:ea4badc78215 | 119 | |
va009039 | 12:ea4badc78215 | 120 | /*virtual*/ bool USBHostCam::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 |
va009039 | 12:ea4badc78215 | 121 | { |
va009039 | 12:ea4badc78215 | 122 | CAM_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol); |
va009039 | 12:ea4badc78215 | 123 | if ((cam_intf == -1) && caminfo_found) { |
va009039 | 12:ea4badc78215 | 124 | cam_intf = intf_nb; |
va009039 | 12:ea4badc78215 | 125 | device_found = true; |
va009039 | 12:ea4badc78215 | 126 | return true; |
va009039 | 12:ea4badc78215 | 127 | } |
va009039 | 12:ea4badc78215 | 128 | return false; |
va009039 | 12:ea4badc78215 | 129 | } |
va009039 | 12:ea4badc78215 | 130 | |
va009039 | 12:ea4badc78215 | 131 | /*virtual*/ bool USBHostCam::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
va009039 | 12:ea4badc78215 | 132 | { |
va009039 | 12:ea4badc78215 | 133 | CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir); |
va009039 | 12:ea4badc78215 | 134 | return false; |
va009039 | 12:ea4badc78215 | 135 | } |
va009039 | 12:ea4badc78215 | 136 | |
va009039 | 12:ea4badc78215 | 137 | #define SEQ_READ_IDOL 0 |
va009039 | 12:ea4badc78215 | 138 | #define SEQ_READ_EXEC 1 |
va009039 | 12:ea4badc78215 | 139 | #define SEQ_READ_DONE 2 |
va009039 | 12:ea4badc78215 | 140 | |
va009039 | 12:ea4badc78215 | 141 | int USBHostCam::readJPEG(uint8_t* buf, int size, int timeout_ms) { |
va009039 | 12:ea4badc78215 | 142 | _buf = buf; |
va009039 | 12:ea4badc78215 | 143 | _pos = 0; |
va009039 | 12:ea4badc78215 | 144 | _size = size; |
va009039 | 12:ea4badc78215 | 145 | _seq = SEQ_READ_IDOL; |
va009039 | 12:ea4badc78215 | 146 | setOnResult(this, &USBHostCam::callback_motion_jpeg); |
va009039 | 12:ea4badc78215 | 147 | Timer timeout_t; |
va009039 | 12:ea4badc78215 | 148 | timeout_t.reset(); |
va009039 | 12:ea4badc78215 | 149 | timeout_t.start(); |
va009039 | 12:ea4badc78215 | 150 | while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE && connected()) { |
va009039 | 12:ea4badc78215 | 151 | poll(); |
va009039 | 12:ea4badc78215 | 152 | Thread::wait(1); |
va009039 | 12:ea4badc78215 | 153 | } |
va009039 | 12:ea4badc78215 | 154 | return _pos; |
va009039 | 12:ea4badc78215 | 155 | } |
va009039 | 12:ea4badc78215 | 156 | |
va009039 | 12:ea4badc78215 | 157 | /* virtual */ void USBHostCam::outputJPEG(uint8_t c, int status) { // from decodeMJPEG |
va009039 | 12:ea4badc78215 | 158 | if (_seq == SEQ_READ_IDOL) { |
va009039 | 12:ea4badc78215 | 159 | if (status == JPEG_START) { |
va009039 | 12:ea4badc78215 | 160 | _pos = 0; |
va009039 | 12:ea4badc78215 | 161 | _seq = SEQ_READ_EXEC; |
va009039 | 12:ea4badc78215 | 162 | } |
va009039 | 12:ea4badc78215 | 163 | } |
va009039 | 12:ea4badc78215 | 164 | if (_seq == SEQ_READ_EXEC) { |
va009039 | 12:ea4badc78215 | 165 | if (_pos < _size) { |
va009039 | 12:ea4badc78215 | 166 | _buf[_pos++] = c; |
va009039 | 12:ea4badc78215 | 167 | } |
va009039 | 12:ea4badc78215 | 168 | if (status == JPEG_END) { |
va009039 | 12:ea4badc78215 | 169 | _seq = SEQ_READ_DONE; |
va009039 | 12:ea4badc78215 | 170 | } |
va009039 | 12:ea4badc78215 | 171 | } |
va009039 | 12:ea4badc78215 | 172 | } |
va009039 | 12:ea4badc78215 | 173 | |
va009039 | 12:ea4badc78215 | 174 | void USBHostCam::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) { |
va009039 | 12:ea4badc78215 | 175 | inputPacket(buf, len); |
va009039 | 12:ea4badc78215 | 176 | } |