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