![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
LogitechC270 webcam class driver alpha version
Fork of USBHostMSD_HelloWorld by
USBHostC270/USBisochronous/USBIsochronous.h@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 | // USBIsochronous.h |
va009039 | 12:ea4badc78215 | 2 | #pragma once |
va009039 | 12:ea4badc78215 | 3 | |
va009039 | 12:ea4badc78215 | 4 | class IsochronousEp; |
va009039 | 12:ea4badc78215 | 5 | struct HCITD { // HostController Isochronous Transfer Descriptor |
va009039 | 12:ea4badc78215 | 6 | __IO uint32_t Control; // +0 Transfer descriptor control |
va009039 | 12:ea4badc78215 | 7 | uint8_t* BufferPage0; // +4 Buffer Page 0 |
va009039 | 12:ea4badc78215 | 8 | HCITD* Next; // +8 Physical pointer to next Isochronous Transfer Descriptor |
va009039 | 12:ea4badc78215 | 9 | uint8_t* BufferEnd; // +12 buffer End |
va009039 | 12:ea4badc78215 | 10 | __IO uint16_t OffsetPSW[8]; // +16 Offset/PSW |
va009039 | 12:ea4badc78215 | 11 | IsochronousEp* ep; // +32 endpoint object |
va009039 | 12:ea4badc78215 | 12 | __IO uint8_t buf[0]; // +36 buffer |
va009039 | 12:ea4badc78215 | 13 | // +36 |
va009039 | 12:ea4badc78215 | 14 | HCITD(IsochronousEp* obj, uint16_t FrameNumber, int FrameCount, uint16_t PacketSize); |
va009039 | 12:ea4badc78215 | 15 | inline void* operator new(size_t size, int buf_size) { |
va009039 | 12:ea4badc78215 | 16 | void* p; |
va009039 | 12:ea4badc78215 | 17 | if (posix_memalign(&p, 32, size+buf_size) == 0) { |
va009039 | 12:ea4badc78215 | 18 | return p; |
va009039 | 12:ea4badc78215 | 19 | } |
va009039 | 12:ea4badc78215 | 20 | return NULL; |
va009039 | 12:ea4badc78215 | 21 | } |
va009039 | 12:ea4badc78215 | 22 | |
va009039 | 12:ea4badc78215 | 23 | inline void operator delete(void* p) { |
va009039 | 12:ea4badc78215 | 24 | free(p); |
va009039 | 12:ea4badc78215 | 25 | } |
va009039 | 12:ea4badc78215 | 26 | |
va009039 | 12:ea4badc78215 | 27 | inline uint16_t StartingFrame() { |
va009039 | 12:ea4badc78215 | 28 | return Control & 0xffff; |
va009039 | 12:ea4badc78215 | 29 | } |
va009039 | 12:ea4badc78215 | 30 | |
va009039 | 12:ea4badc78215 | 31 | inline uint8_t FrameCount() { |
va009039 | 12:ea4badc78215 | 32 | return ((Control>>24)&7)+1; |
va009039 | 12:ea4badc78215 | 33 | } |
va009039 | 12:ea4badc78215 | 34 | |
va009039 | 12:ea4badc78215 | 35 | inline uint8_t ConditionCode() { |
va009039 | 12:ea4badc78215 | 36 | return Control>>28; |
va009039 | 12:ea4badc78215 | 37 | } |
va009039 | 12:ea4badc78215 | 38 | }; |
va009039 | 12:ea4badc78215 | 39 | |
va009039 | 12:ea4badc78215 | 40 | struct _HCED { // HostController EndPoint Descriptor |
va009039 | 12:ea4badc78215 | 41 | __IO uint32_t Control; // +0 Endpoint descriptor control |
va009039 | 12:ea4badc78215 | 42 | HCTD* TailTd; // +4 Physical address of tail in Transfer descriptor list |
va009039 | 12:ea4badc78215 | 43 | __IO HCTD* HeadTd; // +8 Physcial address of head in Transfer descriptor list |
va009039 | 12:ea4badc78215 | 44 | _HCED* Next; // +12 Physical address of next Endpoint descriptor |
va009039 | 12:ea4badc78215 | 45 | // +16 |
va009039 | 12:ea4badc78215 | 46 | _HCED(int addr, uint8_t ep, uint16_t size, int lowSpeed = 0) { |
va009039 | 12:ea4badc78215 | 47 | Control = addr | /* USB address */ |
va009039 | 12:ea4badc78215 | 48 | ((ep & 0x7F) << 7) | /* Endpoint address */ |
va009039 | 12:ea4badc78215 | 49 | (ep!=0?(((ep&0x80)?2:1) << 11):0)| /* direction : Out = 1, 2 = In */ |
va009039 | 12:ea4badc78215 | 50 | ((lowSpeed?1:0) << 13) | /* speed full=0 low=1 */ |
va009039 | 12:ea4badc78215 | 51 | (size << 16); /* MaxPkt Size */ |
va009039 | 12:ea4badc78215 | 52 | Next = NULL; |
va009039 | 12:ea4badc78215 | 53 | } |
va009039 | 12:ea4badc78215 | 54 | |
va009039 | 12:ea4badc78215 | 55 | inline void* operator new(size_t size) { |
va009039 | 12:ea4badc78215 | 56 | void* p; |
va009039 | 12:ea4badc78215 | 57 | if (posix_memalign(&p, 16, size) == 0) { |
va009039 | 12:ea4badc78215 | 58 | return p; |
va009039 | 12:ea4badc78215 | 59 | } |
va009039 | 12:ea4badc78215 | 60 | return NULL; |
va009039 | 12:ea4badc78215 | 61 | } |
va009039 | 12:ea4badc78215 | 62 | |
va009039 | 12:ea4badc78215 | 63 | inline void operator delete(void* p) { |
va009039 | 12:ea4badc78215 | 64 | free(p); |
va009039 | 12:ea4badc78215 | 65 | } |
va009039 | 12:ea4badc78215 | 66 | |
va009039 | 12:ea4badc78215 | 67 | inline uint8_t FunctionAddress() { |
va009039 | 12:ea4badc78215 | 68 | return Control & 0x7f; |
va009039 | 12:ea4badc78215 | 69 | } |
va009039 | 12:ea4badc78215 | 70 | |
va009039 | 12:ea4badc78215 | 71 | inline int Speed() { |
va009039 | 12:ea4badc78215 | 72 | return (Control>>13)&1; |
va009039 | 12:ea4badc78215 | 73 | } |
va009039 | 12:ea4badc78215 | 74 | |
va009039 | 12:ea4badc78215 | 75 | inline void setFunctionAddress(int addr) { |
va009039 | 12:ea4badc78215 | 76 | Control &= ~0x7f; |
va009039 | 12:ea4badc78215 | 77 | Control |= addr; |
va009039 | 12:ea4badc78215 | 78 | } |
va009039 | 12:ea4badc78215 | 79 | |
va009039 | 12:ea4badc78215 | 80 | inline void setMaxPacketSize(uint16_t size) { |
va009039 | 12:ea4badc78215 | 81 | Control &= ~0xffff0000; |
va009039 | 12:ea4badc78215 | 82 | Control |= size<<16; |
va009039 | 12:ea4badc78215 | 83 | } |
va009039 | 12:ea4badc78215 | 84 | |
va009039 | 12:ea4badc78215 | 85 | int Skip() { |
va009039 | 12:ea4badc78215 | 86 | return (Control>>14) & 1; |
va009039 | 12:ea4badc78215 | 87 | } |
va009039 | 12:ea4badc78215 | 88 | |
va009039 | 12:ea4badc78215 | 89 | void setSkip() { |
va009039 | 12:ea4badc78215 | 90 | Control |= (1<<14); |
va009039 | 12:ea4badc78215 | 91 | } |
va009039 | 12:ea4badc78215 | 92 | |
va009039 | 12:ea4badc78215 | 93 | void setFormat() { |
va009039 | 12:ea4badc78215 | 94 | Control |= (1<<15); |
va009039 | 12:ea4badc78215 | 95 | } |
va009039 | 12:ea4badc78215 | 96 | |
va009039 | 12:ea4badc78215 | 97 | template<typename T> |
va009039 | 12:ea4badc78215 | 98 | inline bool enqueue(T* td) { |
va009039 | 12:ea4badc78215 | 99 | if (td) { |
va009039 | 12:ea4badc78215 | 100 | T* tail = reinterpret_cast<T*>(TailTd); |
va009039 | 12:ea4badc78215 | 101 | if (tail) { |
va009039 | 12:ea4badc78215 | 102 | tail->Next = td; |
va009039 | 12:ea4badc78215 | 103 | TailTd = reinterpret_cast<HCTD*>(td); |
va009039 | 12:ea4badc78215 | 104 | return true; |
va009039 | 12:ea4badc78215 | 105 | } |
va009039 | 12:ea4badc78215 | 106 | } |
va009039 | 12:ea4badc78215 | 107 | return false; |
va009039 | 12:ea4badc78215 | 108 | } |
va009039 | 12:ea4badc78215 | 109 | |
va009039 | 12:ea4badc78215 | 110 | template<typename T> |
va009039 | 12:ea4badc78215 | 111 | inline T* dequeue() { |
va009039 | 12:ea4badc78215 | 112 | T* head = reinterpret_cast<T*>(reinterpret_cast<uint32_t>(HeadTd)&~3); // delete Halted and Toggle Carry bit |
va009039 | 12:ea4badc78215 | 113 | T* tail = reinterpret_cast<T*>(TailTd); |
va009039 | 12:ea4badc78215 | 114 | if (head == NULL || tail == NULL || head == tail) { |
va009039 | 12:ea4badc78215 | 115 | return NULL; |
va009039 | 12:ea4badc78215 | 116 | } |
va009039 | 12:ea4badc78215 | 117 | HeadTd = reinterpret_cast<HCTD*>(head->Next); |
va009039 | 12:ea4badc78215 | 118 | return head; |
va009039 | 12:ea4badc78215 | 119 | } |
va009039 | 12:ea4badc78215 | 120 | template<typename T> |
va009039 | 12:ea4badc78215 | 121 | void init_queue(T* td) { |
va009039 | 12:ea4badc78215 | 122 | TailTd = reinterpret_cast<HCTD*>(td); |
va009039 | 12:ea4badc78215 | 123 | HeadTd = reinterpret_cast<HCTD*>(td); |
va009039 | 12:ea4badc78215 | 124 | } |
va009039 | 12:ea4badc78215 | 125 | }; |
va009039 | 12:ea4badc78215 | 126 | |
va009039 | 12:ea4badc78215 | 127 | struct _HCCA { // Host Controller Communication Area |
va009039 | 12:ea4badc78215 | 128 | _HCED* InterruptTable[32]; // +0 Interrupt Table |
va009039 | 12:ea4badc78215 | 129 | __IO uint16_t FrameNumber;// +128 Frame Number |
va009039 | 12:ea4badc78215 | 130 | __IO uint16_t Pad1; // +130 |
va009039 | 12:ea4badc78215 | 131 | __IO HCTD* DoneHead; // +132 Done Head |
va009039 | 12:ea4badc78215 | 132 | uint8_t Reserved[116]; // +136 Reserved for future use |
va009039 | 12:ea4badc78215 | 133 | uint8_t Unknown[4]; // +252 Unused |
va009039 | 12:ea4badc78215 | 134 | // +256 |
va009039 | 12:ea4badc78215 | 135 | inline void* operator new(size_t size) { |
va009039 | 12:ea4badc78215 | 136 | void* p; |
va009039 | 12:ea4badc78215 | 137 | if (posix_memalign(&p, 256, size) == 0) { |
va009039 | 12:ea4badc78215 | 138 | return p; |
va009039 | 12:ea4badc78215 | 139 | } |
va009039 | 12:ea4badc78215 | 140 | return NULL; |
va009039 | 12:ea4badc78215 | 141 | } |
va009039 | 12:ea4badc78215 | 142 | |
va009039 | 12:ea4badc78215 | 143 | inline void operator delete(void* p) { |
va009039 | 12:ea4badc78215 | 144 | free(p); |
va009039 | 12:ea4badc78215 | 145 | } |
va009039 | 12:ea4badc78215 | 146 | |
va009039 | 12:ea4badc78215 | 147 | inline void enqueue(_HCED* ed) { |
va009039 | 12:ea4badc78215 | 148 | for(int i = 0; i < 32; i++) { |
va009039 | 12:ea4badc78215 | 149 | if (InterruptTable[i] == NULL) { |
va009039 | 12:ea4badc78215 | 150 | InterruptTable[i] = ed; |
va009039 | 12:ea4badc78215 | 151 | } else { |
va009039 | 12:ea4badc78215 | 152 | _HCED* nextEd = InterruptTable[i]; |
va009039 | 12:ea4badc78215 | 153 | while(nextEd->Next && nextEd->Next != ed) { |
va009039 | 12:ea4badc78215 | 154 | nextEd = nextEd->Next; |
va009039 | 12:ea4badc78215 | 155 | } |
va009039 | 12:ea4badc78215 | 156 | nextEd->Next = ed; |
va009039 | 12:ea4badc78215 | 157 | } |
va009039 | 12:ea4badc78215 | 158 | } |
va009039 | 12:ea4badc78215 | 159 | } |
va009039 | 12:ea4badc78215 | 160 | |
va009039 | 12:ea4badc78215 | 161 | inline void dequeue(_HCED* ed) { |
va009039 | 12:ea4badc78215 | 162 | for(int i = 0; i < 32; i++) { |
va009039 | 12:ea4badc78215 | 163 | if (InterruptTable[i] == ed) { |
va009039 | 12:ea4badc78215 | 164 | InterruptTable[i] = ed->Next; |
va009039 | 12:ea4badc78215 | 165 | } else if (InterruptTable[i]) { |
va009039 | 12:ea4badc78215 | 166 | _HCED* nextEd = InterruptTable[i]; |
va009039 | 12:ea4badc78215 | 167 | while(nextEd) { |
va009039 | 12:ea4badc78215 | 168 | if (nextEd->Next == ed) { |
va009039 | 12:ea4badc78215 | 169 | nextEd->Next = ed->Next; |
va009039 | 12:ea4badc78215 | 170 | break; |
va009039 | 12:ea4badc78215 | 171 | } |
va009039 | 12:ea4badc78215 | 172 | nextEd = nextEd->Next; |
va009039 | 12:ea4badc78215 | 173 | } |
va009039 | 12:ea4badc78215 | 174 | } |
va009039 | 12:ea4badc78215 | 175 | } |
va009039 | 12:ea4badc78215 | 176 | } |
va009039 | 12:ea4badc78215 | 177 | }; |
va009039 | 12:ea4badc78215 | 178 | |
va009039 | 12:ea4badc78215 | 179 | #define HCITD_QUEUE_SIZE 3 |
va009039 | 12:ea4badc78215 | 180 | |
va009039 | 12:ea4badc78215 | 181 | class IsochronousEp { |
va009039 | 12:ea4badc78215 | 182 | public: |
va009039 | 12:ea4badc78215 | 183 | void init(int addr, uint8_t ep, uint16_t size, uint8_t frameCount = 4, uint8_t queueLimit = HCITD_QUEUE_SIZE); |
va009039 | 12:ea4badc78215 | 184 | void reset(int delay_ms = 100); |
va009039 | 12:ea4badc78215 | 185 | HCITD* isochronousReceive(int timeout_ms); |
va009039 | 12:ea4badc78215 | 186 | int isochronousSend(uint8_t* buf, int len, int timeout_ms); |
va009039 | 12:ea4badc78215 | 187 | HCITD* get_queue_HCITD(int timeout_ms); |
va009039 | 12:ea4badc78215 | 188 | uint16_t m_PacketSize; |
va009039 | 12:ea4badc78215 | 189 | void disconnect(); |
va009039 | 12:ea4badc78215 | 190 | void irqWdhHandler(HCITD* itd) {m_queue.put(itd);} // WDH |
va009039 | 12:ea4badc78215 | 191 | private: |
va009039 | 12:ea4badc78215 | 192 | HCITD* new_HCITD(IsochronousEp* obj); |
va009039 | 12:ea4badc78215 | 193 | Queue<HCITD, HCITD_QUEUE_SIZE> m_queue; // ITD done queue |
va009039 | 12:ea4badc78215 | 194 | int m_itd_queue_count; |
va009039 | 12:ea4badc78215 | 195 | int m_itd_queue_limit; |
va009039 | 12:ea4badc78215 | 196 | uint16_t m_FrameNumber; |
va009039 | 12:ea4badc78215 | 197 | int m_FrameCount; // 1-8 |
va009039 | 12:ea4badc78215 | 198 | void enable(); |
va009039 | 12:ea4badc78215 | 199 | _HCED* m_pED; |
va009039 | 12:ea4badc78215 | 200 | }; |