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 BaseUvc.h Source File

BaseUvc.h

00001 // BaseUvc.h
00002 
00003 #pragma once
00004 
00005 // --- UVC --------------------------------------------------
00006 #define _30FPS  333333
00007 #define _25FPS  400000
00008 #define _20FPS  500000
00009 #define _15FPS  666666
00010 #define _10FPS 1000000
00011 #define _5FPS  2000000
00012 #define _1FPS 10000000
00013 
00014 #define SET_CUR  0x01
00015 #define GET_CUR  0x81
00016 #define GET_MIN  0x82
00017 #define GET_MAX  0x83
00018 #define GET_RES  0x84
00019 #define GET_LEN  0x85
00020 #define GET_INFO 0x86
00021 #define GET_DEF  0x87
00022 
00023 #define VS_PROBE_CONTROL  0x01
00024 #define VS_COMMIT_CONTROL 0x02
00025 
00026 class BaseEp;
00027 struct HCITD {    // HostController Isochronous Transfer Descriptor
00028     __IO uint32_t Control;      // +0 Transfer descriptor control
00029     uint8_t*  BufferPage0;      // +4 Buffer Page 0
00030     HCITD* Next;                // +8 Physical pointer to next Isochronous Transfer Descriptor
00031     uint8_t*  BufferEnd;        // +12 buffer End
00032     __IO uint16_t OffsetPSW[8]; // +16 Offset/PSW
00033     BaseEp* ep;                 // +32 endpoint object
00034     __IO uint8_t buf[0];        // +36 buffer
00035                                 // +36
00036     HCITD(BaseEp* obj, uint16_t FrameNumber, int FrameCount, uint16_t PacketSize);
00037     inline void* operator new(size_t size, int buf_size) {
00038         void* p;
00039         if (posix_memalign(&p, 32, size+buf_size) == 0) {
00040             return p;
00041         }
00042         return NULL;
00043     }
00044 
00045     inline void operator delete(void* p) {
00046         free(p);
00047     }
00048 
00049     inline uint16_t StartingFrame() {
00050         return Control & 0xffff;
00051     }
00052 
00053     inline uint8_t FrameCount() {
00054         return ((Control>>24)&7)+1;
00055     }    
00056 
00057     inline uint8_t ConditionCode() {
00058         return Control>>28;
00059     }
00060 };
00061 
00062 struct _HCED {    // HostController EndPoint Descriptor
00063     __IO uint32_t Control; // +0 Endpoint descriptor control
00064     HCTD* TailTd;          // +4 Physical address of tail in Transfer descriptor list
00065     __IO HCTD* HeadTd;     // +8 Physcial address of head in Transfer descriptor list
00066     _HCED* Next;           // +12 Physical address of next Endpoint descriptor
00067                            // +16
00068     _HCED(int addr, uint8_t ep, uint16_t size, int lowSpeed) {
00069         Control =  addr            | /* USB address */
00070         ((ep & 0x7F) << 7)         | /* Endpoint address */
00071         (ep!=0?(((ep&0x80)?2:1) << 11):0)| /* direction : Out = 1, 2 = In */
00072         ((lowSpeed?1:0) << 13)     | /* speed full=0 low=1 */
00073         (size << 16);                /* MaxPkt Size */
00074         Next = NULL;
00075     }
00076 
00077     inline void* operator new(size_t size) {
00078         void* p;
00079         if (posix_memalign(&p, 16, size) == 0) {
00080             return p;
00081         }
00082         return NULL;
00083     }
00084 
00085     inline void operator delete(void* p) {
00086         free(p);
00087     }
00088 
00089     inline uint8_t FunctionAddress() {
00090         return Control & 0x7f;
00091     }
00092 
00093     inline int Speed() {
00094         return (Control>>13)&1;
00095     }
00096 
00097     inline void setFunctionAddress(int addr) {
00098         Control &= ~0x7f;
00099         Control |= addr;
00100     }
00101 
00102     inline void setMaxPacketSize(uint16_t size) {
00103         Control &= ~0xffff0000;
00104         Control |= size<<16;
00105     }
00106 
00107     inline void setSkip() {
00108         Control |= 1<<14;
00109     }
00110 };
00111 
00112 struct _HCCA {    // Host Controller Communication Area
00113     _HCED* InterruptTable[32]; // +0 Interrupt Table
00114     __IO uint16_t FrameNumber;// +128 Frame Number
00115     __IO uint16_t Pad1;       // +130
00116     __IO HCTD* DoneHead;      // +132 Done Head
00117     uint8_t Reserved[116];    // +136 Reserved for future use
00118     uint8_t Unknown[4];       // +252 Unused
00119                               // +256
00120     inline void* operator new(size_t size) {
00121         void* p;
00122         if (posix_memalign(&p, 256, size) == 0) {
00123             return p;
00124         }
00125         return NULL;
00126     }
00127 
00128     inline void operator delete(void* p) {
00129         free(p);
00130     }
00131     
00132     inline void enqueue(_HCED* ed) {
00133         for(int i = 0; i < 32; i++) {
00134             if (InterruptTable[i] == NULL) {
00135                 InterruptTable[i] = ed;
00136             } else {
00137                 _HCED* nextEd = InterruptTable[i];
00138                 while(nextEd->Next && nextEd->Next != ed) {
00139                     nextEd = nextEd->Next;
00140                 }
00141                 nextEd->Next = ed;
00142             }
00143         }
00144     }
00145     
00146     inline void dequeue(_HCED* ed) {
00147          for(int i = 0; i < 32; i++) {
00148             if (InterruptTable[i] == ed) {
00149                 InterruptTable[i] = ed->Next;
00150             } else if (InterruptTable[i]) {
00151                 _HCED* nextEd = InterruptTable[i];
00152                 while(nextEd) {
00153                     if (nextEd->Next == ed) {
00154                         nextEd->Next = ed->Next;
00155                         break;
00156                     }
00157                     nextEd = nextEd->Next;
00158                 }
00159             }
00160          }
00161     }
00162 };
00163 
00164 #define HCTD_QUEUE_SIZE 3
00165 
00166 class BaseEp { // endpoint
00167 public:
00168     BaseEp(int addr, uint8_t ep = 0, uint16_t size = 8, int lowSpeed = 0);
00169     int GetAddr();
00170     int GetLowSpeed();
00171     void update_FunctionAddress(int addr);
00172     void update_MaxPacketSize(uint16_t size);
00173     int transfer(uint8_t* buf, int len);
00174     int status(uint32_t millisec=osWaitForever);
00175     //
00176     virtual void enable() = 0;
00177     virtual void irqWdhHandler(HCTD* td) {m_queue.put(td);} // WDH
00178     int wait_queue_HCTD(HCTD* wait_td, uint32_t millisec=osWaitForever);
00179     // report
00180     uint8_t m_ConditionCode;
00181     int m_report_queue_error;
00182 protected:
00183     int send_receive(uint8_t* buf, int len, int millisec);
00184     HCTD* get_queue_HCTD(uint32_t millisec=osWaitForever);
00185     _HCED* m_pED;
00186     Queue<HCTD, HCTD_QUEUE_SIZE> m_queue; // TD done queue
00187     int m_td_queue_count;
00188 };
00189 
00190 class IsochronousEp : public BaseEp {
00191 public:
00192     IsochronousEp(int addr, uint8_t ep, uint16_t size);
00193     void reset(int delay_ms = 100);
00194     HCITD* isochronousReceive(int millisec=osWaitForever);
00195     int isochronousSend(uint8_t* buf, int len, int millisec=osWaitForever);
00196     HCITD* get_queue_HCITD(int millisec);
00197     uint16_t m_PacketSize;
00198     void disconnect();
00199 private:
00200     HCITD* new_HCITD(BaseEp* obj);
00201     int m_itd_queue_count;
00202     uint16_t m_FrameNumber;
00203     int m_FrameCount; // 1-8
00204     virtual void enable();
00205 };
00206 
00207 class BaseUvc {
00208 public:
00209     void poll(int millisec=osWaitForever);
00210     USB_TYPE Control(int req, int cs, int index, uint8_t* buf, int size);
00211     USB_TYPE setInterfaceAlternate(uint8_t intf, uint8_t alt);
00212     //ControlEp* m_ctlEp;
00213     IsochronousEp* m_isoEp;
00214     uint32_t report_cc_count[16];  // ConditionCode
00215     uint32_t report_ps_cc_count[16]; // Packt Status ConditionCode
00216     // callback
00217     void onResult(uint16_t frame, uint8_t* buf, int len);
00218     void setOnResult( void (*pMethod)(uint16_t, uint8_t*, int) );
00219     class CDummy;
00220     template<class T> 
00221     void setOnResult( T* pItem, void (T::*pMethod)(uint16_t, uint8_t*, int) )
00222     {
00223         m_pCb = NULL;
00224         m_pCbItem = (CDummy*) pItem;
00225         m_pCbMeth = (void (CDummy::*)(uint16_t, uint8_t*, int)) pMethod;
00226     }
00227     void clearOnResult();
00228     CDummy* m_pCbItem;
00229     void (CDummy::*m_pCbMeth)(uint16_t, uint8_t*, int);
00230     void (*m_pCb)(uint16_t, uint8_t*, int);
00231 protected:
00232     USBHost * host;
00233     USBDeviceConnected * dev;
00234 };