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