final

Dependencies:   mbed FATFileSystem

Fork of KL46Z-USBHostMSD_HelloWorld by Norimasa Okamoto

Committer:
homzovam
Date:
Sat Apr 04 20:16:39 2015 +0000
Revision:
4:77d6450f34d7
prijimac-funkcni final

Who changed what in which revision?

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