Masato YAMANISHI / Mbed 2 deprecated USBHostC210_example

Dependencies:   TextLCD USBHost mbed

Fork of USBHostC270_example by Norimasa Okamoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostC270.cpp Source File

USBHostC270.cpp

00001 #include "USBHostC270.h"
00002 #include "dbg.h"
00003 
00004 //#define C270_DEBUG 1
00005 #ifdef C270_DEBUG
00006 #define C270_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
00007 #else
00008 #define C270_DBG(...)  while(0);
00009 #endif
00010 
00011 // ------------------ HcControl Register ---------------------
00012 #define  OR_CONTROL_IE                  0x00000008
00013 
00014 USBHostC270::USBHostC270(int formatIndex, int frameIndex, uint32_t interval)
00015 {
00016     C270_DBG("formatIndex: %d, frameIndex: %d, interval: %d", formatIndex, frameIndex, interval);
00017     _formatIndex = formatIndex;
00018     _frameIndex = frameIndex;
00019     _interval = interval;
00020     m_isoEp = NULL;
00021     clearOnResult();
00022     host = USBHost::getHostInst();
00023     init();
00024 }
00025 
00026 void USBHostC270::init()
00027 {
00028     C270_DBG("");
00029     dev_connected = false;
00030     dev = NULL;
00031     c270_intf = -1;
00032     c270_device_found = false;
00033     c270_vid_pid_found = false;
00034 }
00035 
00036 bool USBHostC270::connected()
00037 {
00038     C270_DBG("dev_connected: %d", dev_connected);
00039     return dev_connected;
00040 }
00041 
00042 bool USBHostC270::connect()
00043 {
00044     C270_DBG("dev_connected: %d", dev_connected);
00045     if (dev_connected) {
00046         return true;
00047     }
00048 
00049     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00050         if ((dev = host->getDevice(i)) != NULL) {
00051             
00052             C270_DBG("Trying to connect C270 device\r\n");
00053             
00054             if(host->enumerate(dev, this))
00055                 break;
00056 
00057             if (c270_device_found) {
00058                 USB_INFO("New C270 device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, c270_intf);
00059                 dev->setName("C270", c270_intf);
00060                 host->registerDriver(dev, c270_intf, this, &USBHostC270::onDisconnect);
00061                 int addr = dev->getAddress();
00062                 m_isoEp = new IsochronousEp(addr, C270_EN, C270_MPS);
00063                 uint8_t buf[26];
00064                 memset(buf, 0, sizeof(buf));
00065                 buf[2] = _formatIndex;
00066                 buf[3] = _frameIndex;
00067                 *reinterpret_cast<uint32_t*>(buf+4) = _interval;
00068                 USB_TYPE res = Control(SET_CUR, VS_COMMIT_CONTROL, 1, buf, sizeof(buf));
00069                 if (res != USB_TYPE_OK) {
00070                     C270_DBG("SET_CUR VS_COMMIT_CONTROL FAILED");
00071                 }
00072                 res = setInterfaceAlternate(1, C270_IF_ALT); // alt=1 packet size = 192
00073                 if (res != USB_TYPE_OK) {
00074                     C270_DBG("SET_INTERFACE FAILED");
00075                 }
00076                 for(int i = 0; i < 16; i++) {
00077                     report_cc_count[i] = 0;
00078                     report_ps_cc_count[i] = 0;
00079                 }
00080                 LPC_USB->HcControl |= OR_CONTROL_PLE; // PeriodicListEnable
00081                 LPC_USB->HcControl |= OR_CONTROL_IE;  // IsochronousEnable
00082 
00083                 dev_connected = true;
00084                 return true;
00085             }
00086         }
00087     }
00088     init();
00089     return false;
00090 }
00091 
00092 void USBHostC270::onDisconnect()
00093 {
00094     C270_DBG("dev_connected: %d", dev_connected);
00095     // TODO
00096     if (m_isoEp) {
00097        m_isoEp->disconnect();
00098     }
00099     init();
00100 }
00101 
00102 /*virtual*/ void USBHostC270::setVidPid(uint16_t vid, uint16_t pid)
00103 {
00104     C270_DBG("vid:%04x,pid:%04x", vid, pid);
00105     if (vid == C270_VID && pid == C270_PID) { 
00106         c270_vid_pid_found = true;
00107     } else {
00108         c270_vid_pid_found = false;
00109     }
00110 }
00111 
00112 /*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
00113 {
00114     C270_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol);
00115     if ((c270_intf == -1) && c270_vid_pid_found) {
00116         c270_intf = intf_nb;
00117         c270_vid_pid_found = false;
00118         c270_device_found = true;
00119         return true;
00120     }
00121     return false;
00122 }
00123 
00124 /*virtual*/ bool USBHostC270::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00125 {
00126     C270_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir);
00127     return false;
00128 }
00129 
00130 #define SEQ_READ_IDOL 0
00131 #define SEQ_READ_EXEC 1
00132 #define SEQ_READ_DONE 2
00133 
00134 int USBHostC270::readJPEG(uint8_t* buf, int size, int timeout_ms) {
00135     _buf = buf;
00136     _pos = 0;
00137     _size = size;
00138     _seq = SEQ_READ_IDOL;
00139     setOnResult(this, &USBHostC270::callback_motion_jpeg);
00140     Timer timeout_t;
00141     timeout_t.reset();
00142     timeout_t.start();
00143     while(timeout_t.read_ms() < timeout_ms && _seq != SEQ_READ_DONE) {
00144         poll(timeout_ms);
00145     } 
00146     return _pos;
00147 }
00148 
00149 /* virtual */ void USBHostC270::outputJPEG(uint8_t c, int status) { // from decodeMJPEG
00150     if (_seq == SEQ_READ_IDOL) {
00151         if (status == JPEG_START) {
00152             _pos = 0;
00153             _seq = SEQ_READ_EXEC;
00154         }
00155     }
00156     if (_seq == SEQ_READ_EXEC) {
00157         if (_pos < _size) {
00158             _buf[_pos++] = c;  
00159         }  
00160         if (status == JPEG_END) {
00161             _seq = SEQ_READ_DONE;
00162         }
00163     }
00164 }
00165 
00166 void USBHostC270::callback_motion_jpeg(uint16_t frame, uint8_t* buf, int len) {
00167         inputPacket(buf, len);
00168 }