supported GR-PEACH original: http://developer.mbed.org/users/va009039/code/USBHostC270_example/ The function of Isochronous has moved to USBHost_AddIso library.
Dependencies: USBHost_custom_Addiso
Fork of USBHostC270_example_GR-PEACH by
USBHostCam/USBHostCam.cpp@13:fa85d3614acf, 2015-04-01 (annotated)
- Committer:
- dkato
- Date:
- Wed Apr 01 05:46:06 2015 +0000
- Revision:
- 13:fa85d3614acf
- Parent:
- 12:ea4badc78215
The function of Isochronous has moved to USBHost_AddIso library.
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 | |
va009039 | 12:ea4badc78215 | 86 | dev_connected = true; |
va009039 | 12:ea4badc78215 | 87 | return true; |
va009039 | 12:ea4badc78215 | 88 | } |
va009039 | 12:ea4badc78215 | 89 | } |
va009039 | 12:ea4badc78215 | 90 | } |
va009039 | 12:ea4badc78215 | 91 | init(); |
va009039 | 12:ea4badc78215 | 92 | return false; |
va009039 | 12:ea4badc78215 | 93 | } |
va009039 | 12:ea4badc78215 | 94 | |
va009039 | 12:ea4badc78215 | 95 | void USBHostCam::onDisconnect() |
va009039 | 12:ea4badc78215 | 96 | { |
va009039 | 12:ea4badc78215 | 97 | CAM_DBG("dev_connected: %d", dev_connected); |
va009039 | 12:ea4badc78215 | 98 | if (dev_connected) { |
va009039 | 12:ea4badc78215 | 99 | m_isoEp->disconnect(); |
va009039 | 12:ea4badc78215 | 100 | init(); |
va009039 | 12:ea4badc78215 | 101 | } |
va009039 | 12:ea4badc78215 | 102 | } |
va009039 | 12:ea4badc78215 | 103 | |
va009039 | 12:ea4badc78215 | 104 | /*virtual*/ void USBHostCam::setVidPid(uint16_t vid, uint16_t pid) |
va009039 | 12:ea4badc78215 | 105 | { |
va009039 | 12:ea4badc78215 | 106 | CAM_DBG("vid:%04x,pid:%04x", vid, pid); |
va009039 | 12:ea4badc78215 | 107 | caminfo = CamInfoList; |
va009039 | 12:ea4badc78215 | 108 | while(caminfo->vid != 0) { |
va009039 | 12:ea4badc78215 | 109 | if (caminfo->vid == vid && caminfo->pid == pid && |
va009039 | 12:ea4badc78215 | 110 | caminfo->size == _caminfo_size && caminfo->option == _caminfo_option) { |
va009039 | 12:ea4badc78215 | 111 | caminfo_found = true; |
va009039 | 12:ea4badc78215 | 112 | break; |
va009039 | 12:ea4badc78215 | 113 | } |
va009039 | 12:ea4badc78215 | 114 | caminfo++; |
va009039 | 12:ea4badc78215 | 115 | } |
va009039 | 12:ea4badc78215 | 116 | } |
va009039 | 12:ea4badc78215 | 117 | |
va009039 | 12:ea4badc78215 | 118 | /*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 | 119 | { |
va009039 | 12:ea4badc78215 | 120 | 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 | 121 | if ((cam_intf == -1) && caminfo_found) { |
va009039 | 12:ea4badc78215 | 122 | cam_intf = intf_nb; |
va009039 | 12:ea4badc78215 | 123 | device_found = true; |
va009039 | 12:ea4badc78215 | 124 | return true; |
va009039 | 12:ea4badc78215 | 125 | } |
va009039 | 12:ea4badc78215 | 126 | return false; |
va009039 | 12:ea4badc78215 | 127 | } |
va009039 | 12:ea4badc78215 | 128 | |
va009039 | 12:ea4badc78215 | 129 | /*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 | 130 | { |
va009039 | 12:ea4badc78215 | 131 | CAM_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir); |
va009039 | 12:ea4badc78215 | 132 | return false; |
va009039 | 12:ea4badc78215 | 133 | } |
va009039 | 12:ea4badc78215 | 134 | |
va009039 | 12:ea4badc78215 | 135 | #define SEQ_READ_IDOL 0 |
va009039 | 12:ea4badc78215 | 136 | #define SEQ_READ_EXEC 1 |
va009039 | 12:ea4badc78215 | 137 | #define SEQ_READ_DONE 2 |
va009039 | 12:ea4badc78215 | 138 | |
va009039 | 12:ea4badc78215 | 139 | int USBHostCam::readJPEG(uint8_t* buf, int size, int timeout_ms) { |
va009039 | 12:ea4badc78215 | 140 | _buf = buf; |
va009039 | 12:ea4badc78215 | 141 | _pos = 0; |
va009039 | 12:ea4badc78215 | 142 | _size = size; |
va009039 | 12:ea4badc78215 | 143 | _seq = SEQ_READ_IDOL; |
va009039 | 12:ea4badc78215 | 144 | setOnResult(this, &USBHostCam::callback_motion_jpeg); |
va009039 | 12:ea4badc78215 | 145 | Timer timeout_t; |
va009039 | 12:ea4badc78215 | 146 | timeout_t.reset(); |
va009039 | 12:ea4badc78215 | 147 | timeout_t.start(); |
va009039 | 12:ea4badc78215 | 148 | while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE && connected()) { |
va009039 | 12:ea4badc78215 | 149 | poll(); |
va009039 | 12:ea4badc78215 | 150 | Thread::wait(1); |
va009039 | 12:ea4badc78215 | 151 | } |
va009039 | 12:ea4badc78215 | 152 | return _pos; |
va009039 | 12:ea4badc78215 | 153 | } |
va009039 | 12:ea4badc78215 | 154 | |
va009039 | 12:ea4badc78215 | 155 | /* virtual */ void USBHostCam::outputJPEG(uint8_t c, int status) { // from decodeMJPEG |
va009039 | 12:ea4badc78215 | 156 | if (_seq == SEQ_READ_IDOL) { |
va009039 | 12:ea4badc78215 | 157 | if (status == JPEG_START) { |
va009039 | 12:ea4badc78215 | 158 | _pos = 0; |
va009039 | 12:ea4badc78215 | 159 | _seq = SEQ_READ_EXEC; |
va009039 | 12:ea4badc78215 | 160 | } |
va009039 | 12:ea4badc78215 | 161 | } |
va009039 | 12:ea4badc78215 | 162 | if (_seq == SEQ_READ_EXEC) { |
va009039 | 12:ea4badc78215 | 163 | if (_pos < _size) { |
va009039 | 12:ea4badc78215 | 164 | _buf[_pos++] = c; |
va009039 | 12:ea4badc78215 | 165 | } |
va009039 | 12:ea4badc78215 | 166 | if (status == JPEG_END) { |
va009039 | 12:ea4badc78215 | 167 | _seq = SEQ_READ_DONE; |
va009039 | 12:ea4badc78215 | 168 | } |
va009039 | 12:ea4badc78215 | 169 | } |
va009039 | 12:ea4badc78215 | 170 | } |
va009039 | 12:ea4badc78215 | 171 | |
va009039 | 12:ea4badc78215 | 172 | void USBHostCam::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) { |
va009039 | 12:ea4badc78215 | 173 | inputPacket(buf, len); |
va009039 | 12:ea4badc78215 | 174 | } |