Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LPC4088-USBHost mbed
USBHostCam.cpp
00001 // USBHostCam.cpp 00002 #include "USBHostCam.h" 00003 #include "dbg.h" 00004 00005 //#define CAM_DEBUG 1 00006 #ifdef CAM_DEBUG 00007 #define CAM_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); 00008 #else 00009 #define CAM_DBG(...) while(0); 00010 #endif 00011 00012 // ------------------ HcControl Register --------------------- 00013 #define OR_CONTROL_IE 0x00000008 00014 00015 CamInfo* getCamInfoList(); // CamInfo.cpp 00016 00017 USBHostCam::USBHostCam(uint8_t size, uint8_t option, CamInfo* user_caminfo) 00018 { 00019 CAM_DBG("size: %d, option: %d", size, option); 00020 _caminfo_size = size; 00021 _caminfo_option = option; 00022 if (user_caminfo) { 00023 CamInfoList = user_caminfo; 00024 } else { 00025 CamInfoList = getCamInfoList(); 00026 } 00027 clearOnResult(); 00028 host = USBHost::getHostInst(); 00029 init(); 00030 } 00031 00032 void USBHostCam::init() 00033 { 00034 CAM_DBG(""); 00035 dev_connected = false; 00036 dev = NULL; 00037 cam_intf = -1; 00038 device_found = false; 00039 caminfo_found = false; 00040 } 00041 00042 bool USBHostCam::connected() 00043 { 00044 return dev_connected; 00045 } 00046 00047 bool USBHostCam::connect() 00048 { 00049 if (dev_connected) { 00050 return true; 00051 } 00052 00053 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00054 if ((dev = host->getDevice(i)) != NULL) { 00055 00056 CAM_DBG("Trying to connect Cam device\r\n"); 00057 00058 if(host->enumerate(dev, this)) { 00059 break; 00060 } 00061 if (device_found) { 00062 USB_INFO("New Cam: %s device: VID:%04x PID:%04x [dev: %p - intf: %d]", caminfo->name, dev->getVid(), dev->getPid(), dev, cam_intf); 00063 dev->setName(caminfo->name, cam_intf); 00064 //host->registerDriver(dev, cam_intf, this, &USBHostCam::onDisconnect); 00065 m_isoEp = new IsochronousEp(dev); 00066 m_isoEp->init(ISOCHRONOUS_ENDPOINT, IN, caminfo->mps, caminfo->en); 00067 m_isoEp->init2(caminfo->frameCount, caminfo->queueLimit); 00068 uint8_t buf[26]; 00069 memset(buf, 0, sizeof(buf)); 00070 buf[2] = caminfo->formatIndex; 00071 buf[3] = caminfo->frameIndex; 00072 *reinterpret_cast<uint32_t*>(buf+4) = caminfo->interval; 00073 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf)); 00074 if (res != USB_TYPE_OK) { 00075 CAM_DBG("SET_CUR VS_COMMIT_CONTROL FAILED"); 00076 } 00077 res = setInterfaceAlternate(1, caminfo->if_alt); 00078 if (res != USB_TYPE_OK) { 00079 CAM_DBG("SET_INTERFACE FAILED"); 00080 } 00081 for(int i = 0; i < 16; i++) { 00082 report_cc_count[i] = 0; 00083 report_ps_cc_count[i] = 0; 00084 } 00085 LPC_USB->HcControl |= OR_CONTROL_PLE; // PeriodicListEnable 00086 LPC_USB->HcControl |= OR_CONTROL_IE; // IsochronousEnable 00087 00088 dev_connected = true; 00089 return true; 00090 } 00091 } 00092 } 00093 init(); 00094 return false; 00095 } 00096 00097 void USBHostCam::onDisconnect() 00098 { 00099 } 00100 00101 /*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid) 00102 { 00103 CAM_DBG("vid:%04x,pid:%04x", vid, pid); 00104 caminfo = CamInfoList; 00105 while(caminfo->vid != 0) { 00106 if (caminfo->vid == vid && caminfo->pid == pid && 00107 caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) { 00108 caminfo_found = true; 00109 break; 00110 } 00111 caminfo++; 00112 } 00113 } 00114 00115 /*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 00116 { 00117 CAM_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol); 00118 if ((cam_intf == -1) && caminfo_found) { 00119 cam_intf = intf_nb; 00120 device_found = true; 00121 return true; 00122 } 00123 return false; 00124 } 00125 00126 /*virtual*/ bool USBHostCam::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00127 { 00128 CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir); 00129 return false; 00130 } 00131 00132 #define SEQ_READ_IDLE 0 00133 #define SEQ_READ_EXEC 1 00134 #define SEQ_READ_DONE 2 00135 00136 int USBHostCam::readJPEG(uint8_t* buf, int size, int timeout_ms) { 00137 _buf = buf; 00138 _pos = 0; 00139 _size = size; 00140 _seq = SEQ_READ_IDLE; 00141 setOnResult(this, &USBHostCam::callback_motion_jpeg); 00142 Timer timeout_t; 00143 timeout_t.reset(); 00144 timeout_t.start(); 00145 while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE && connected()) { 00146 poll(); 00147 Thread::wait(1); 00148 } 00149 return _pos; 00150 } 00151 00152 /* virtual */ void USBHostCam::outputJPEG(uint8_t c, int status) { // from decodeMJPEG 00153 if (_seq == SEQ_READ_IDLE) { 00154 if (status == JPEG_START) { 00155 _pos = 0; 00156 _seq = SEQ_READ_EXEC; 00157 } 00158 } 00159 if (_seq == SEQ_READ_EXEC) { 00160 if (_pos < _size) { 00161 _buf[_pos++] = c; 00162 } 00163 if (status == JPEG_END) { 00164 _seq = SEQ_READ_DONE; 00165 } 00166 } 00167 } 00168 00169 void USBHostCam::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) { 00170 inputPacket(buf, len); 00171 } 00172
Generated on Fri Jul 15 2022 09:03:16 by
1.7.2