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@12:b91fdea8c0a7, 2014-06-09 (annotated)
- Committer:
- va009039
- Date:
- Mon Jun 09 09:01:10 2014 +0000
- Revision:
- 12:b91fdea8c0a7
- Child:
- 16:981c3104f6c0
Simple USBHost for Nucleo F401RE
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 | 12:b91fdea8c0a7 | 35 | } |
va009039 | 12:b91fdea8c0a7 | 36 | |
va009039 | 12:b91fdea8c0a7 | 37 | /** |
va009039 | 12:b91fdea8c0a7 | 38 | * Initialize an endpoint |
va009039 | 12:b91fdea8c0a7 | 39 | * |
va009039 | 12:b91fdea8c0a7 | 40 | * @param type endpoint type |
va009039 | 12:b91fdea8c0a7 | 41 | * @param dir endpoint direction |
va009039 | 12:b91fdea8c0a7 | 42 | * @param size endpoint size |
va009039 | 12:b91fdea8c0a7 | 43 | * @param ep_number endpoint number |
va009039 | 12:b91fdea8c0a7 | 44 | */ |
va009039 | 12:b91fdea8c0a7 | 45 | void init(ENDPOINT_TYPE _type, ENDPOINT_DIRECTION _dir, uint32_t size, uint8_t ep_number) { |
va009039 | 12:b91fdea8c0a7 | 46 | type = _type; |
va009039 | 12:b91fdea8c0a7 | 47 | dir = _dir; |
va009039 | 12:b91fdea8c0a7 | 48 | MaxPacketSize = size; |
va009039 | 12:b91fdea8c0a7 | 49 | address = ep_number; |
va009039 | 12:b91fdea8c0a7 | 50 | data01_toggle = DATA0; |
va009039 | 12:b91fdea8c0a7 | 51 | } |
va009039 | 12:b91fdea8c0a7 | 52 | |
va009039 | 12:b91fdea8c0a7 | 53 | /** |
va009039 | 12:b91fdea8c0a7 | 54 | * Attach a member function to call when a transfer is finished |
va009039 | 12:b91fdea8c0a7 | 55 | * |
va009039 | 12:b91fdea8c0a7 | 56 | * @param tptr pointer to the object to call the member function on |
va009039 | 12:b91fdea8c0a7 | 57 | * @param mptr pointer to the member function to be called |
va009039 | 12:b91fdea8c0a7 | 58 | */ |
va009039 | 12:b91fdea8c0a7 | 59 | template<typename T> |
va009039 | 12:b91fdea8c0a7 | 60 | inline void attach(T* tptr, void (T::*mptr)(void)) { |
va009039 | 12:b91fdea8c0a7 | 61 | if((mptr != NULL) && (tptr != NULL)) { |
va009039 | 12:b91fdea8c0a7 | 62 | rx.attach(tptr, mptr); |
va009039 | 12:b91fdea8c0a7 | 63 | } |
va009039 | 12:b91fdea8c0a7 | 64 | } |
va009039 | 12:b91fdea8c0a7 | 65 | |
va009039 | 12:b91fdea8c0a7 | 66 | /** |
va009039 | 12:b91fdea8c0a7 | 67 | * Attach a callback called when a transfer is finished |
va009039 | 12:b91fdea8c0a7 | 68 | * |
va009039 | 12:b91fdea8c0a7 | 69 | * @param fptr function pointer |
va009039 | 12:b91fdea8c0a7 | 70 | */ |
va009039 | 12:b91fdea8c0a7 | 71 | inline void attach(void (*fptr)(void)) { |
va009039 | 12:b91fdea8c0a7 | 72 | if(fptr != NULL) { |
va009039 | 12:b91fdea8c0a7 | 73 | rx.attach(fptr); |
va009039 | 12:b91fdea8c0a7 | 74 | } |
va009039 | 12:b91fdea8c0a7 | 75 | } |
va009039 | 12:b91fdea8c0a7 | 76 | |
va009039 | 12:b91fdea8c0a7 | 77 | /** |
va009039 | 12:b91fdea8c0a7 | 78 | * Call the handler associted to the end of a transfer |
va009039 | 12:b91fdea8c0a7 | 79 | */ |
va009039 | 12:b91fdea8c0a7 | 80 | inline void call() { |
va009039 | 12:b91fdea8c0a7 | 81 | rx.call(); |
va009039 | 12:b91fdea8c0a7 | 82 | }; |
va009039 | 12:b91fdea8c0a7 | 83 | |
va009039 | 12:b91fdea8c0a7 | 84 | void setDevice(USBDeviceConnected* _dev) { dev = _dev; } |
va009039 | 12:b91fdea8c0a7 | 85 | void setState(uint8_t st){}; // dummy |
va009039 | 12:b91fdea8c0a7 | 86 | void setBuffer(uint8_t* buf, int size) { buf_start = buf, buf_size = size; } |
va009039 | 12:b91fdea8c0a7 | 87 | void setLengthTransferred(int len) { transferred = len; }; |
va009039 | 12:b91fdea8c0a7 | 88 | void setAddress(int addr) { address = addr; } |
va009039 | 12:b91fdea8c0a7 | 89 | void setSize(int size) { MaxPacketSize = size; } |
va009039 | 12:b91fdea8c0a7 | 90 | void setData01(uint8_t data01) { data01_toggle = data01; } |
va009039 | 12:b91fdea8c0a7 | 91 | void setNextEndpoint(USBEndpoint* ep) { nextEp = ep; }; |
va009039 | 12:b91fdea8c0a7 | 92 | |
va009039 | 12:b91fdea8c0a7 | 93 | USBDeviceConnected* getDevice() { return dev; } |
va009039 | 12:b91fdea8c0a7 | 94 | ENDPOINT_TYPE getType() { return type; }; |
va009039 | 12:b91fdea8c0a7 | 95 | int getLengthTransferred() { return transferred; } |
va009039 | 12:b91fdea8c0a7 | 96 | uint8_t *getBufStart() { return buf_start; } |
va009039 | 12:b91fdea8c0a7 | 97 | int getBufSize() { return buf_size; } |
va009039 | 12:b91fdea8c0a7 | 98 | uint8_t getAddress(){ return address; }; |
va009039 | 12:b91fdea8c0a7 | 99 | int getSize() { return MaxPacketSize; } |
va009039 | 12:b91fdea8c0a7 | 100 | ENDPOINT_DIRECTION getDir() { return dir; } |
va009039 | 12:b91fdea8c0a7 | 101 | uint8_t getData01() { return data01_toggle; } |
va009039 | 12:b91fdea8c0a7 | 102 | void toggleData01() { |
va009039 | 12:b91fdea8c0a7 | 103 | data01_toggle = (data01_toggle == DATA0) ? DATA1 : DATA0; |
va009039 | 12:b91fdea8c0a7 | 104 | } |
va009039 | 12:b91fdea8c0a7 | 105 | USBEndpoint* nextEndpoint() { return nextEp; }; |
va009039 | 12:b91fdea8c0a7 | 106 | |
va009039 | 12:b91fdea8c0a7 | 107 | private: |
va009039 | 12:b91fdea8c0a7 | 108 | ENDPOINT_TYPE type; |
va009039 | 12:b91fdea8c0a7 | 109 | ENDPOINT_DIRECTION dir; |
va009039 | 12:b91fdea8c0a7 | 110 | USBDeviceConnected* dev; |
va009039 | 12:b91fdea8c0a7 | 111 | uint8_t data01_toggle; // DATA0,DATA1 |
va009039 | 12:b91fdea8c0a7 | 112 | uint8_t address; |
va009039 | 12:b91fdea8c0a7 | 113 | int transferred; |
va009039 | 12:b91fdea8c0a7 | 114 | uint8_t * buf_start; |
va009039 | 12:b91fdea8c0a7 | 115 | int buf_size; |
va009039 | 12:b91fdea8c0a7 | 116 | FunctionPointer rx; |
va009039 | 12:b91fdea8c0a7 | 117 | int MaxPacketSize; |
va009039 | 12:b91fdea8c0a7 | 118 | USBEndpoint* nextEp; |
va009039 | 12:b91fdea8c0a7 | 119 | }; |
va009039 | 12:b91fdea8c0a7 | 120 | |
va009039 | 12:b91fdea8c0a7 | 121 | class EndpointQueue { |
va009039 | 12:b91fdea8c0a7 | 122 | public: |
va009039 | 12:b91fdea8c0a7 | 123 | EndpointQueue():head(NULL),tail(NULL) {} |
va009039 | 12:b91fdea8c0a7 | 124 | void push(USBEndpoint* ep) { |
va009039 | 12:b91fdea8c0a7 | 125 | if (head) { |
va009039 | 12:b91fdea8c0a7 | 126 | tail->setNextEndpoint(ep); |
va009039 | 12:b91fdea8c0a7 | 127 | } else { |
va009039 | 12:b91fdea8c0a7 | 128 | head = ep; |
va009039 | 12:b91fdea8c0a7 | 129 | } |
va009039 | 12:b91fdea8c0a7 | 130 | tail = ep; |
va009039 | 12:b91fdea8c0a7 | 131 | ep->setNextEndpoint(NULL); |
va009039 | 12:b91fdea8c0a7 | 132 | } |
va009039 | 12:b91fdea8c0a7 | 133 | USBEndpoint* pop() { |
va009039 | 12:b91fdea8c0a7 | 134 | USBEndpoint* ep = head; |
va009039 | 12:b91fdea8c0a7 | 135 | if (ep) { |
va009039 | 12:b91fdea8c0a7 | 136 | head = ep->nextEndpoint(); |
va009039 | 12:b91fdea8c0a7 | 137 | } |
va009039 | 12:b91fdea8c0a7 | 138 | return ep; |
va009039 | 12:b91fdea8c0a7 | 139 | } |
va009039 | 12:b91fdea8c0a7 | 140 | bool empty() { return head == NULL; } |
va009039 | 12:b91fdea8c0a7 | 141 | |
va009039 | 12:b91fdea8c0a7 | 142 | private: |
va009039 | 12:b91fdea8c0a7 | 143 | USBEndpoint* head; |
va009039 | 12:b91fdea8c0a7 | 144 | USBEndpoint* tail; |
va009039 | 12:b91fdea8c0a7 | 145 | }; |