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 Norimasa Okamoto

Committer:
va009039
Date:
Tue Jul 01 18:33:31 2014 +0900
Revision:
18:61554f238584
Parent:
16:981c3104f6c0
Child:
24:5396b6a93262
add lpc4088/lpc1768

Who changed what in which revision?

UserRevisionLine numberNew 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 18:61554f238584 55 void ohci_init(uint8_t frameCount, uint8_t queueLimit) {
va009039 18:61554f238584 56 ohci.frameCount = frameCount;
va009039 18:61554f238584 57 ohci.queueLimit = queueLimit;
va009039 18:61554f238584 58 }
va009039 18:61554f238584 59
va009039 12:b91fdea8c0a7 60 /**
va009039 12:b91fdea8c0a7 61 * Attach a member function to call when a transfer is finished
va009039 12:b91fdea8c0a7 62 *
va009039 12:b91fdea8c0a7 63 * @param tptr pointer to the object to call the member function on
va009039 12:b91fdea8c0a7 64 * @param mptr pointer to the member function to be called
va009039 12:b91fdea8c0a7 65 */
va009039 12:b91fdea8c0a7 66 template<typename T>
va009039 12:b91fdea8c0a7 67 inline void attach(T* tptr, void (T::*mptr)(void)) {
va009039 12:b91fdea8c0a7 68 if((mptr != NULL) && (tptr != NULL)) {
va009039 12:b91fdea8c0a7 69 rx.attach(tptr, mptr);
va009039 12:b91fdea8c0a7 70 }
va009039 12:b91fdea8c0a7 71 }
va009039 12:b91fdea8c0a7 72
va009039 12:b91fdea8c0a7 73 /**
va009039 12:b91fdea8c0a7 74 * Attach a callback called when a transfer is finished
va009039 12:b91fdea8c0a7 75 *
va009039 12:b91fdea8c0a7 76 * @param fptr function pointer
va009039 12:b91fdea8c0a7 77 */
va009039 12:b91fdea8c0a7 78 inline void attach(void (*fptr)(void)) {
va009039 12:b91fdea8c0a7 79 if(fptr != NULL) {
va009039 12:b91fdea8c0a7 80 rx.attach(fptr);
va009039 12:b91fdea8c0a7 81 }
va009039 12:b91fdea8c0a7 82 }
va009039 12:b91fdea8c0a7 83
va009039 12:b91fdea8c0a7 84 /**
va009039 12:b91fdea8c0a7 85 * Call the handler associted to the end of a transfer
va009039 12:b91fdea8c0a7 86 */
va009039 12:b91fdea8c0a7 87 inline void call() {
va009039 12:b91fdea8c0a7 88 rx.call();
va009039 12:b91fdea8c0a7 89 };
va009039 12:b91fdea8c0a7 90
va009039 16:981c3104f6c0 91 void setType(ENDPOINT_TYPE _type) { type = _type; }
va009039 16:981c3104f6c0 92 void setState(USB_TYPE st) { state = st; }
va009039 12:b91fdea8c0a7 93 void setDevice(USBDeviceConnected* _dev) { dev = _dev; }
va009039 12:b91fdea8c0a7 94 void setBuffer(uint8_t* buf, int size) { buf_start = buf, buf_size = size; }
va009039 12:b91fdea8c0a7 95 void setLengthTransferred(int len) { transferred = len; };
va009039 12:b91fdea8c0a7 96 void setAddress(int addr) { address = addr; }
va009039 12:b91fdea8c0a7 97 void setSize(int size) { MaxPacketSize = size; }
va009039 12:b91fdea8c0a7 98 void setData01(uint8_t data01) { data01_toggle = data01; }
va009039 12:b91fdea8c0a7 99 void setNextEndpoint(USBEndpoint* ep) { nextEp = ep; };
va009039 16:981c3104f6c0 100 template<class T>
va009039 16:981c3104f6c0 101 void setHALData(T data) { pData = data; }
va009039 12:b91fdea8c0a7 102
va009039 12:b91fdea8c0a7 103 USBDeviceConnected* getDevice() { return dev; }
va009039 12:b91fdea8c0a7 104 ENDPOINT_TYPE getType() { return type; };
va009039 16:981c3104f6c0 105 USB_TYPE getState() { return state; }
va009039 12:b91fdea8c0a7 106 int getLengthTransferred() { return transferred; }
va009039 12:b91fdea8c0a7 107 uint8_t *getBufStart() { return buf_start; }
va009039 12:b91fdea8c0a7 108 int getBufSize() { return buf_size; }
va009039 12:b91fdea8c0a7 109 uint8_t getAddress(){ return address; };
va009039 12:b91fdea8c0a7 110 int getSize() { return MaxPacketSize; }
va009039 12:b91fdea8c0a7 111 ENDPOINT_DIRECTION getDir() { return dir; }
va009039 12:b91fdea8c0a7 112 uint8_t getData01() { return data01_toggle; }
va009039 12:b91fdea8c0a7 113 void toggleData01() {
va009039 12:b91fdea8c0a7 114 data01_toggle = (data01_toggle == DATA0) ? DATA1 : DATA0;
va009039 12:b91fdea8c0a7 115 }
va009039 12:b91fdea8c0a7 116 USBEndpoint* nextEndpoint() { return nextEp; };
va009039 16:981c3104f6c0 117 template<class T>
va009039 16:981c3104f6c0 118 T getHALData() { return reinterpret_cast<T>(pData); }
va009039 12:b91fdea8c0a7 119
va009039 18:61554f238584 120 struct {
va009039 18:61554f238584 121 uint8_t queueLimit;
va009039 18:61554f238584 122 uint8_t frameCount; // 1-8
va009039 18:61554f238584 123 } ohci;
va009039 12:b91fdea8c0a7 124 private:
va009039 16:981c3104f6c0 125 USBEndpoint(){}
va009039 12:b91fdea8c0a7 126 ENDPOINT_TYPE type;
va009039 16:981c3104f6c0 127 USB_TYPE state;
va009039 12:b91fdea8c0a7 128 ENDPOINT_DIRECTION dir;
va009039 12:b91fdea8c0a7 129 USBDeviceConnected* dev;
va009039 12:b91fdea8c0a7 130 uint8_t data01_toggle; // DATA0,DATA1
va009039 12:b91fdea8c0a7 131 uint8_t address;
va009039 12:b91fdea8c0a7 132 int transferred;
va009039 12:b91fdea8c0a7 133 uint8_t * buf_start;
va009039 12:b91fdea8c0a7 134 int buf_size;
va009039 12:b91fdea8c0a7 135 FunctionPointer rx;
va009039 12:b91fdea8c0a7 136 int MaxPacketSize;
va009039 12:b91fdea8c0a7 137 USBEndpoint* nextEp;
va009039 16:981c3104f6c0 138 void* pData;
va009039 12:b91fdea8c0a7 139 };
va009039 12:b91fdea8c0a7 140
va009039 12:b91fdea8c0a7 141 class EndpointQueue {
va009039 12:b91fdea8c0a7 142 public:
va009039 12:b91fdea8c0a7 143 EndpointQueue():head(NULL),tail(NULL) {}
va009039 12:b91fdea8c0a7 144 void push(USBEndpoint* ep) {
va009039 12:b91fdea8c0a7 145 if (head) {
va009039 12:b91fdea8c0a7 146 tail->setNextEndpoint(ep);
va009039 12:b91fdea8c0a7 147 } else {
va009039 12:b91fdea8c0a7 148 head = ep;
va009039 12:b91fdea8c0a7 149 }
va009039 12:b91fdea8c0a7 150 tail = ep;
va009039 12:b91fdea8c0a7 151 ep->setNextEndpoint(NULL);
va009039 12:b91fdea8c0a7 152 }
va009039 12:b91fdea8c0a7 153 USBEndpoint* pop() {
va009039 12:b91fdea8c0a7 154 USBEndpoint* ep = head;
va009039 12:b91fdea8c0a7 155 if (ep) {
va009039 12:b91fdea8c0a7 156 head = ep->nextEndpoint();
va009039 12:b91fdea8c0a7 157 }
va009039 12:b91fdea8c0a7 158 return ep;
va009039 12:b91fdea8c0a7 159 }
va009039 12:b91fdea8c0a7 160 bool empty() { return head == NULL; }
va009039 12:b91fdea8c0a7 161
va009039 12:b91fdea8c0a7 162 private:
va009039 12:b91fdea8c0a7 163 USBEndpoint* head;
va009039 12:b91fdea8c0a7 164 USBEndpoint* tail;
va009039 12:b91fdea8c0a7 165 };
va009039 16:981c3104f6c0 166
va009039 16:981c3104f6c0 167