dari pinpin usb langsung
Dependencies: mbed
Fork of Nucleo_Ex04_USBPAD by
USBHost/USBEndpoint.h@0:b5f79b4f741d, 2016-03-15 (annotated)
- Committer:
- beaglescout007
- Date:
- Tue Mar 15 11:39:04 2016 +0000
- Revision:
- 0:b5f79b4f741d
Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
beaglescout007 | 0:b5f79b4f741d | 1 | /* mbed USBHost Library |
beaglescout007 | 0:b5f79b4f741d | 2 | * Copyright (c) 2006-2013 ARM Limited |
beaglescout007 | 0:b5f79b4f741d | 3 | * |
beaglescout007 | 0:b5f79b4f741d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
beaglescout007 | 0:b5f79b4f741d | 5 | * you may not use this file except in compliance with the License. |
beaglescout007 | 0:b5f79b4f741d | 6 | * You may obtain a copy of the License at |
beaglescout007 | 0:b5f79b4f741d | 7 | * |
beaglescout007 | 0:b5f79b4f741d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
beaglescout007 | 0:b5f79b4f741d | 9 | * |
beaglescout007 | 0:b5f79b4f741d | 10 | * Unless required by applicable law or agreed to in writing, software |
beaglescout007 | 0:b5f79b4f741d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
beaglescout007 | 0:b5f79b4f741d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
beaglescout007 | 0:b5f79b4f741d | 13 | * See the License for the specific language governing permissions and |
beaglescout007 | 0:b5f79b4f741d | 14 | * limitations under the License. |
beaglescout007 | 0:b5f79b4f741d | 15 | */ |
beaglescout007 | 0:b5f79b4f741d | 16 | |
beaglescout007 | 0:b5f79b4f741d | 17 | #pragma once |
beaglescout007 | 0:b5f79b4f741d | 18 | #include "FunctionPointer.h" |
beaglescout007 | 0:b5f79b4f741d | 19 | #include "USBHostTypes.h" |
beaglescout007 | 0:b5f79b4f741d | 20 | #include "USBDeviceConnected.h" |
beaglescout007 | 0:b5f79b4f741d | 21 | |
beaglescout007 | 0:b5f79b4f741d | 22 | class USBDeviceConnected; |
beaglescout007 | 0:b5f79b4f741d | 23 | |
beaglescout007 | 0:b5f79b4f741d | 24 | /** |
beaglescout007 | 0:b5f79b4f741d | 25 | * USBEndpoint class |
beaglescout007 | 0:b5f79b4f741d | 26 | */ |
beaglescout007 | 0:b5f79b4f741d | 27 | class USBEndpoint { |
beaglescout007 | 0:b5f79b4f741d | 28 | public: |
beaglescout007 | 0:b5f79b4f741d | 29 | /** |
beaglescout007 | 0:b5f79b4f741d | 30 | * Constructor |
beaglescout007 | 0:b5f79b4f741d | 31 | */ |
beaglescout007 | 0:b5f79b4f741d | 32 | USBEndpoint(USBDeviceConnected* _dev) { |
beaglescout007 | 0:b5f79b4f741d | 33 | init(CONTROL_ENDPOINT, IN, 8, 0); |
beaglescout007 | 0:b5f79b4f741d | 34 | dev = _dev; |
beaglescout007 | 0:b5f79b4f741d | 35 | pData = NULL; |
beaglescout007 | 0:b5f79b4f741d | 36 | } |
beaglescout007 | 0:b5f79b4f741d | 37 | |
beaglescout007 | 0:b5f79b4f741d | 38 | /** |
beaglescout007 | 0:b5f79b4f741d | 39 | * Initialize an endpoint |
beaglescout007 | 0:b5f79b4f741d | 40 | * |
beaglescout007 | 0:b5f79b4f741d | 41 | * @param type endpoint type |
beaglescout007 | 0:b5f79b4f741d | 42 | * @param dir endpoint direction |
beaglescout007 | 0:b5f79b4f741d | 43 | * @param size endpoint size |
beaglescout007 | 0:b5f79b4f741d | 44 | * @param ep_number endpoint number |
beaglescout007 | 0:b5f79b4f741d | 45 | */ |
beaglescout007 | 0:b5f79b4f741d | 46 | void init(ENDPOINT_TYPE _type, ENDPOINT_DIRECTION _dir, uint32_t size, uint8_t ep_number) { |
beaglescout007 | 0:b5f79b4f741d | 47 | setState(USB_TYPE_FREE); |
beaglescout007 | 0:b5f79b4f741d | 48 | setType(_type); |
beaglescout007 | 0:b5f79b4f741d | 49 | dir = _dir; |
beaglescout007 | 0:b5f79b4f741d | 50 | MaxPacketSize = size; |
beaglescout007 | 0:b5f79b4f741d | 51 | address = ep_number; |
beaglescout007 | 0:b5f79b4f741d | 52 | data01_toggle = DATA0; |
beaglescout007 | 0:b5f79b4f741d | 53 | } |
beaglescout007 | 0:b5f79b4f741d | 54 | |
beaglescout007 | 0:b5f79b4f741d | 55 | void ohci_init(uint8_t frameCount, uint8_t queueLimit) { |
beaglescout007 | 0:b5f79b4f741d | 56 | ohci.frameCount = frameCount; |
beaglescout007 | 0:b5f79b4f741d | 57 | ohci.queueLimit = queueLimit; |
beaglescout007 | 0:b5f79b4f741d | 58 | } |
beaglescout007 | 0:b5f79b4f741d | 59 | |
beaglescout007 | 0:b5f79b4f741d | 60 | /** |
beaglescout007 | 0:b5f79b4f741d | 61 | * Attach a member function to call when a transfer is finished |
beaglescout007 | 0:b5f79b4f741d | 62 | * |
beaglescout007 | 0:b5f79b4f741d | 63 | * @param tptr pointer to the object to call the member function on |
beaglescout007 | 0:b5f79b4f741d | 64 | * @param mptr pointer to the member function to be called |
beaglescout007 | 0:b5f79b4f741d | 65 | */ |
beaglescout007 | 0:b5f79b4f741d | 66 | template<typename T> |
beaglescout007 | 0:b5f79b4f741d | 67 | inline void attach(T* tptr, void (T::*mptr)(void)) { |
beaglescout007 | 0:b5f79b4f741d | 68 | if((mptr != NULL) && (tptr != NULL)) { |
beaglescout007 | 0:b5f79b4f741d | 69 | rx.attach(tptr, mptr); |
beaglescout007 | 0:b5f79b4f741d | 70 | } |
beaglescout007 | 0:b5f79b4f741d | 71 | } |
beaglescout007 | 0:b5f79b4f741d | 72 | |
beaglescout007 | 0:b5f79b4f741d | 73 | /** |
beaglescout007 | 0:b5f79b4f741d | 74 | * Attach a callback called when a transfer is finished |
beaglescout007 | 0:b5f79b4f741d | 75 | * |
beaglescout007 | 0:b5f79b4f741d | 76 | * @param fptr function pointer |
beaglescout007 | 0:b5f79b4f741d | 77 | */ |
beaglescout007 | 0:b5f79b4f741d | 78 | inline void attach(void (*fptr)(void)) { |
beaglescout007 | 0:b5f79b4f741d | 79 | if(fptr != NULL) { |
beaglescout007 | 0:b5f79b4f741d | 80 | rx.attach(fptr); |
beaglescout007 | 0:b5f79b4f741d | 81 | } |
beaglescout007 | 0:b5f79b4f741d | 82 | } |
beaglescout007 | 0:b5f79b4f741d | 83 | |
beaglescout007 | 0:b5f79b4f741d | 84 | /** |
beaglescout007 | 0:b5f79b4f741d | 85 | * Call the handler associted to the end of a transfer |
beaglescout007 | 0:b5f79b4f741d | 86 | */ |
beaglescout007 | 0:b5f79b4f741d | 87 | inline void call() { |
beaglescout007 | 0:b5f79b4f741d | 88 | rx.call(); |
beaglescout007 | 0:b5f79b4f741d | 89 | }; |
beaglescout007 | 0:b5f79b4f741d | 90 | |
beaglescout007 | 0:b5f79b4f741d | 91 | void setType(ENDPOINT_TYPE _type) { type = _type; } |
beaglescout007 | 0:b5f79b4f741d | 92 | void setState(USB_TYPE st) { state = st; } |
beaglescout007 | 0:b5f79b4f741d | 93 | void setDevice(USBDeviceConnected* _dev) { dev = _dev; } |
beaglescout007 | 0:b5f79b4f741d | 94 | void setBuffer(uint8_t* buf, int size) { buf_start = buf, buf_size = size; } |
beaglescout007 | 0:b5f79b4f741d | 95 | void setLengthTransferred(int len) { transferred = len; }; |
beaglescout007 | 0:b5f79b4f741d | 96 | void setAddress(int addr) { address = addr; } |
beaglescout007 | 0:b5f79b4f741d | 97 | void setSize(int size) { MaxPacketSize = size; } |
beaglescout007 | 0:b5f79b4f741d | 98 | void setData01(uint8_t data01) { data01_toggle = data01; } |
beaglescout007 | 0:b5f79b4f741d | 99 | void setNextEndpoint(USBEndpoint* ep) { nextEp = ep; }; |
beaglescout007 | 0:b5f79b4f741d | 100 | template<class T> |
beaglescout007 | 0:b5f79b4f741d | 101 | void setHALData(T data) { pData = data; } |
beaglescout007 | 0:b5f79b4f741d | 102 | |
beaglescout007 | 0:b5f79b4f741d | 103 | USBDeviceConnected* getDevice() { return dev; } |
beaglescout007 | 0:b5f79b4f741d | 104 | ENDPOINT_TYPE getType() { return type; }; |
beaglescout007 | 0:b5f79b4f741d | 105 | USB_TYPE getState() { return state; } |
beaglescout007 | 0:b5f79b4f741d | 106 | int getLengthTransferred() { return transferred; } |
beaglescout007 | 0:b5f79b4f741d | 107 | uint8_t *getBufStart() { return buf_start; } |
beaglescout007 | 0:b5f79b4f741d | 108 | int getBufSize() { return buf_size; } |
beaglescout007 | 0:b5f79b4f741d | 109 | uint8_t getAddress(){ return address; }; |
beaglescout007 | 0:b5f79b4f741d | 110 | int getSize() { return MaxPacketSize; } |
beaglescout007 | 0:b5f79b4f741d | 111 | ENDPOINT_DIRECTION getDir() { return dir; } |
beaglescout007 | 0:b5f79b4f741d | 112 | uint8_t getData01() { return data01_toggle; } |
beaglescout007 | 0:b5f79b4f741d | 113 | void toggleData01() { |
beaglescout007 | 0:b5f79b4f741d | 114 | data01_toggle = (data01_toggle == DATA0) ? DATA1 : DATA0; |
beaglescout007 | 0:b5f79b4f741d | 115 | } |
beaglescout007 | 0:b5f79b4f741d | 116 | USBEndpoint* nextEndpoint() { return nextEp; }; |
beaglescout007 | 0:b5f79b4f741d | 117 | template<class T> |
beaglescout007 | 0:b5f79b4f741d | 118 | T getHALData() { return reinterpret_cast<T>(pData); } |
beaglescout007 | 0:b5f79b4f741d | 119 | |
beaglescout007 | 0:b5f79b4f741d | 120 | struct { |
beaglescout007 | 0:b5f79b4f741d | 121 | uint8_t queueLimit; |
beaglescout007 | 0:b5f79b4f741d | 122 | uint8_t frameCount; // 1-8 |
beaglescout007 | 0:b5f79b4f741d | 123 | } ohci; |
beaglescout007 | 0:b5f79b4f741d | 124 | private: |
beaglescout007 | 0:b5f79b4f741d | 125 | USBEndpoint(){} |
beaglescout007 | 0:b5f79b4f741d | 126 | ENDPOINT_TYPE type; |
beaglescout007 | 0:b5f79b4f741d | 127 | USB_TYPE state; |
beaglescout007 | 0:b5f79b4f741d | 128 | ENDPOINT_DIRECTION dir; |
beaglescout007 | 0:b5f79b4f741d | 129 | USBDeviceConnected* dev; |
beaglescout007 | 0:b5f79b4f741d | 130 | uint8_t data01_toggle; // DATA0,DATA1 |
beaglescout007 | 0:b5f79b4f741d | 131 | uint8_t address; |
beaglescout007 | 0:b5f79b4f741d | 132 | int transferred; |
beaglescout007 | 0:b5f79b4f741d | 133 | uint8_t * buf_start; |
beaglescout007 | 0:b5f79b4f741d | 134 | int buf_size; |
beaglescout007 | 0:b5f79b4f741d | 135 | FunctionPointer rx; |
beaglescout007 | 0:b5f79b4f741d | 136 | int MaxPacketSize; |
beaglescout007 | 0:b5f79b4f741d | 137 | USBEndpoint* nextEp; |
beaglescout007 | 0:b5f79b4f741d | 138 | void* pData; |
beaglescout007 | 0:b5f79b4f741d | 139 | }; |
beaglescout007 | 0:b5f79b4f741d | 140 | |
beaglescout007 | 0:b5f79b4f741d | 141 | class EndpointQueue { |
beaglescout007 | 0:b5f79b4f741d | 142 | public: |
beaglescout007 | 0:b5f79b4f741d | 143 | EndpointQueue():head(NULL),tail(NULL) {} |
beaglescout007 | 0:b5f79b4f741d | 144 | void push(USBEndpoint* ep) { |
beaglescout007 | 0:b5f79b4f741d | 145 | if (head) { |
beaglescout007 | 0:b5f79b4f741d | 146 | tail->setNextEndpoint(ep); |
beaglescout007 | 0:b5f79b4f741d | 147 | } else { |
beaglescout007 | 0:b5f79b4f741d | 148 | head = ep; |
beaglescout007 | 0:b5f79b4f741d | 149 | } |
beaglescout007 | 0:b5f79b4f741d | 150 | tail = ep; |
beaglescout007 | 0:b5f79b4f741d | 151 | ep->setNextEndpoint(NULL); |
beaglescout007 | 0:b5f79b4f741d | 152 | } |
beaglescout007 | 0:b5f79b4f741d | 153 | USBEndpoint* pop() { |
beaglescout007 | 0:b5f79b4f741d | 154 | USBEndpoint* ep = head; |
beaglescout007 | 0:b5f79b4f741d | 155 | if (ep) { |
beaglescout007 | 0:b5f79b4f741d | 156 | head = ep->nextEndpoint(); |
beaglescout007 | 0:b5f79b4f741d | 157 | } |
beaglescout007 | 0:b5f79b4f741d | 158 | return ep; |
beaglescout007 | 0:b5f79b4f741d | 159 | } |
beaglescout007 | 0:b5f79b4f741d | 160 | bool empty() { return head == NULL; } |
beaglescout007 | 0:b5f79b4f741d | 161 | |
beaglescout007 | 0:b5f79b4f741d | 162 | private: |
beaglescout007 | 0:b5f79b4f741d | 163 | USBEndpoint* head; |
beaglescout007 | 0:b5f79b4f741d | 164 | USBEndpoint* tail; |
beaglescout007 | 0:b5f79b4f741d | 165 | }; |
beaglescout007 | 0:b5f79b4f741d | 166 | |
beaglescout007 | 0:b5f79b4f741d | 167 | |
beaglescout007 | 0:b5f79b4f741d | 168 |