2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 04:20:29 2018 +0000
Revision:
44:ef52682c423e
Parent:
43:1675750cca08
2018.07.26;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sayzyas 43:1675750cca08 1 /* mbed USBHost Library
sayzyas 43:1675750cca08 2 * Copyright (c) 2006-2013 ARM Limited
sayzyas 43:1675750cca08 3 *
sayzyas 43:1675750cca08 4 * Licensed under the Apache License, Version 2.0 (the "License");
sayzyas 43:1675750cca08 5 * you may not use this file except in compliance with the License.
sayzyas 43:1675750cca08 6 * You may obtain a copy of the License at
sayzyas 43:1675750cca08 7 *
sayzyas 43:1675750cca08 8 * http://www.apache.org/licenses/LICENSE-2.0
sayzyas 43:1675750cca08 9 *
sayzyas 43:1675750cca08 10 * Unless required by applicable law or agreed to in writing, software
sayzyas 43:1675750cca08 11 * distributed under the License is distributed on an "AS IS" BASIS,
sayzyas 43:1675750cca08 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sayzyas 43:1675750cca08 13 * See the License for the specific language governing permissions and
sayzyas 43:1675750cca08 14 * limitations under the License.
sayzyas 43:1675750cca08 15 */
sayzyas 43:1675750cca08 16
sayzyas 43:1675750cca08 17 #ifndef USBDEVICECONNECTED_H
sayzyas 43:1675750cca08 18 #define USBDEVICECONNECTED_H
sayzyas 43:1675750cca08 19
sayzyas 43:1675750cca08 20 #include "stdint.h"
sayzyas 43:1675750cca08 21 #include "USBEndpoint.h"
sayzyas 43:1675750cca08 22 #include "USBHostConf.h"
sayzyas 43:1675750cca08 23 #include "rtos.h"
sayzyas 43:1675750cca08 24
sayzyas 43:1675750cca08 25 class USBHostHub;
sayzyas 43:1675750cca08 26
sayzyas 43:1675750cca08 27 typedef struct {
sayzyas 43:1675750cca08 28 bool in_use;
sayzyas 43:1675750cca08 29 uint8_t nb_endpoint;
sayzyas 43:1675750cca08 30 uint8_t intf_class;
sayzyas 43:1675750cca08 31 uint8_t intf_subclass;
sayzyas 43:1675750cca08 32 uint8_t intf_protocol;
sayzyas 43:1675750cca08 33 USBEndpoint * ep[MAX_ENDPOINT_PER_INTERFACE];
sayzyas 43:1675750cca08 34 FunctionPointer detach;
sayzyas 43:1675750cca08 35 char name[10];
sayzyas 43:1675750cca08 36 } INTERFACE;
sayzyas 43:1675750cca08 37
sayzyas 43:1675750cca08 38 /**
sayzyas 43:1675750cca08 39 * USBDeviceConnected class
sayzyas 43:1675750cca08 40 */
sayzyas 43:1675750cca08 41 class USBDeviceConnected
sayzyas 43:1675750cca08 42 {
sayzyas 43:1675750cca08 43 public:
sayzyas 43:1675750cca08 44
sayzyas 43:1675750cca08 45 /**
sayzyas 43:1675750cca08 46 * Constructor
sayzyas 43:1675750cca08 47 */
sayzyas 43:1675750cca08 48 USBDeviceConnected();
sayzyas 43:1675750cca08 49
sayzyas 43:1675750cca08 50 /**
sayzyas 43:1675750cca08 51 * Attach an USBEndpoint to this device
sayzyas 43:1675750cca08 52 *
sayzyas 43:1675750cca08 53 * @param intf_nb interface number
sayzyas 43:1675750cca08 54 * @param ep pointeur on the USBEndpoint which will be attached
sayzyas 43:1675750cca08 55 * @returns true if successful, false otherwise
sayzyas 43:1675750cca08 56 */
sayzyas 43:1675750cca08 57 bool addEndpoint(uint8_t intf_nb, USBEndpoint * ep);
sayzyas 43:1675750cca08 58
sayzyas 43:1675750cca08 59 /**
sayzyas 43:1675750cca08 60 * Retrieve an USBEndpoint by its TYPE and DIRECTION
sayzyas 43:1675750cca08 61 *
sayzyas 43:1675750cca08 62 * @param intf_nb the interface on which to lookup the USBEndpoint
sayzyas 43:1675750cca08 63 * @param type type of the USBEndpoint looked for
sayzyas 43:1675750cca08 64 * @param dir direction of the USBEndpoint looked for
sayzyas 43:1675750cca08 65 * @param index the index of the USBEndpoint whitin the interface
sayzyas 43:1675750cca08 66 * @returns pointer on the USBEndpoint if found, NULL otherwise
sayzyas 43:1675750cca08 67 */
sayzyas 43:1675750cca08 68 USBEndpoint * getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index = 0);
sayzyas 43:1675750cca08 69
sayzyas 43:1675750cca08 70 /**
sayzyas 43:1675750cca08 71 * Retrieve an USBEndpoint by its index
sayzyas 43:1675750cca08 72 *
sayzyas 43:1675750cca08 73 * @param intf_nb interface number
sayzyas 43:1675750cca08 74 * @param index index of the USBEndpoint
sayzyas 43:1675750cca08 75 * @returns pointer on the USBEndpoint if found, NULL otherwise
sayzyas 43:1675750cca08 76 */
sayzyas 43:1675750cca08 77 USBEndpoint * getEndpoint(uint8_t intf_nb, uint8_t index);
sayzyas 43:1675750cca08 78
sayzyas 43:1675750cca08 79 /**
sayzyas 43:1675750cca08 80 * Add a new interface to this device
sayzyas 43:1675750cca08 81 *
sayzyas 43:1675750cca08 82 * @param intf_nb interface number
sayzyas 43:1675750cca08 83 * @param intf_class interface class
sayzyas 43:1675750cca08 84 * @param intf_subclass interface subclass
sayzyas 43:1675750cca08 85 * @param intf_protocol interface protocol
sayzyas 43:1675750cca08 86 * @returns true if successful, false otherwise
sayzyas 43:1675750cca08 87 */
sayzyas 43:1675750cca08 88 bool addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol);
sayzyas 43:1675750cca08 89
sayzyas 43:1675750cca08 90 /**
sayzyas 43:1675750cca08 91 * Get a specific interface
sayzyas 43:1675750cca08 92 *
sayzyas 43:1675750cca08 93 * @param index index of the interface to be fetched
sayzyas 43:1675750cca08 94 * @returns interface
sayzyas 43:1675750cca08 95 */
sayzyas 43:1675750cca08 96 INTERFACE * getInterface(uint8_t index);
sayzyas 43:1675750cca08 97
sayzyas 43:1675750cca08 98 /**
sayzyas 43:1675750cca08 99 * Attach a member function to call when a the device has been disconnected
sayzyas 43:1675750cca08 100 *
sayzyas 43:1675750cca08 101 * @param intf_nb interface number
sayzyas 43:1675750cca08 102 * @param tptr pointer to the object to call the member function on
sayzyas 43:1675750cca08 103 * @param mptr pointer to the member function to be called
sayzyas 43:1675750cca08 104 */
sayzyas 43:1675750cca08 105 template<typename T>
sayzyas 43:1675750cca08 106 inline void onDisconnect(uint8_t intf_nb, T* tptr, void (T::*mptr)(void)) {
sayzyas 43:1675750cca08 107 if ((mptr != NULL) && (tptr != NULL)) {
sayzyas 43:1675750cca08 108 intf[intf_nb].detach.attach(tptr, mptr);
sayzyas 43:1675750cca08 109 }
sayzyas 43:1675750cca08 110 }
sayzyas 43:1675750cca08 111
sayzyas 43:1675750cca08 112 /**
sayzyas 43:1675750cca08 113 * Attach a callback called when the device has been disconnected
sayzyas 43:1675750cca08 114 *
sayzyas 43:1675750cca08 115 * @param intf_nb interface number
sayzyas 43:1675750cca08 116 * @param fn function pointer
sayzyas 43:1675750cca08 117 */
sayzyas 43:1675750cca08 118 inline void onDisconnect(uint8_t intf_nb, void (*fn)(void)) {
sayzyas 43:1675750cca08 119 if (fn != NULL) {
sayzyas 43:1675750cca08 120 intf[intf_nb].detach.attach(fn);
sayzyas 43:1675750cca08 121 }
sayzyas 43:1675750cca08 122 }
sayzyas 43:1675750cca08 123
sayzyas 43:1675750cca08 124 /**
sayzyas 43:1675750cca08 125 * Disconnect the device by calling a callback function registered by a driver
sayzyas 43:1675750cca08 126 */
sayzyas 43:1675750cca08 127 void disconnect();
sayzyas 43:1675750cca08 128
sayzyas 43:1675750cca08 129 // setters
sayzyas 43:1675750cca08 130 void init(uint8_t hub, uint8_t port, bool lowSpeed);
sayzyas 43:1675750cca08 131 inline void setAddress(uint8_t addr_) { addr = addr_; };
sayzyas 43:1675750cca08 132 inline void setVid(uint16_t vid_) { vid = vid_; };
sayzyas 43:1675750cca08 133 inline void setPid(uint16_t pid_) { pid = pid_; };
sayzyas 43:1675750cca08 134 inline void setClass(uint8_t device_class_) { device_class = device_class_; };
sayzyas 43:1675750cca08 135 inline void setSubClass(uint8_t device_subclass_) { device_subclass = device_subclass_; };
sayzyas 43:1675750cca08 136 inline void setProtocol(uint8_t pr) { proto = pr; };
sayzyas 43:1675750cca08 137 inline void setSizeControlEndpoint(uint32_t size) { sizeControlEndpoint = size; };
sayzyas 43:1675750cca08 138 inline void activeAddress(bool active) { activeAddr = active; };
sayzyas 43:1675750cca08 139 inline void setEnumerated() { enumerated = true; };
sayzyas 43:1675750cca08 140 inline void setNbIntf(uint8_t nb_intf) {nb_interf = nb_intf; };
sayzyas 43:1675750cca08 141 inline void setHubParent(USBHostHub * hub) { hub_parent = hub; };
sayzyas 43:1675750cca08 142 inline void setName(const char * name_, uint8_t intf_nb) { strcpy(intf[intf_nb].name, name_); };
sayzyas 43:1675750cca08 143
sayzyas 43:1675750cca08 144 //getters
sayzyas 43:1675750cca08 145 inline uint8_t getPort() { return port; };
sayzyas 43:1675750cca08 146 inline uint8_t getHub() { return hub_nb; };
sayzyas 43:1675750cca08 147 inline uint8_t getAddress() { return addr; };
sayzyas 43:1675750cca08 148 inline uint16_t getVid() { return vid; };
sayzyas 43:1675750cca08 149 inline uint16_t getPid() { return pid; };
sayzyas 43:1675750cca08 150 inline uint8_t getClass() { return device_class; };
sayzyas 43:1675750cca08 151 inline uint8_t getSubClass() { return device_subclass; };
sayzyas 43:1675750cca08 152 inline uint8_t getProtocol() { return proto; };
sayzyas 43:1675750cca08 153 inline bool getSpeed() { return speed; };
sayzyas 43:1675750cca08 154 inline uint32_t getSizeControlEndpoint() { return sizeControlEndpoint; };
sayzyas 43:1675750cca08 155 inline bool isActiveAddress() { return activeAddr; };
sayzyas 43:1675750cca08 156 inline bool isEnumerated() { return enumerated; };
sayzyas 43:1675750cca08 157 inline USBHostHub * getHubParent() { return hub_parent; };
sayzyas 43:1675750cca08 158 inline uint8_t getNbIntf() { return nb_interf; };
sayzyas 43:1675750cca08 159 inline const char * getName(uint8_t intf_nb) { return intf[intf_nb].name; };
sayzyas 43:1675750cca08 160
sayzyas 43:1675750cca08 161 // in case this device is a hub
sayzyas 43:1675750cca08 162 USBHostHub * hub;
sayzyas 43:1675750cca08 163
sayzyas 43:1675750cca08 164 private:
sayzyas 43:1675750cca08 165 USBHostHub * hub_parent;
sayzyas 43:1675750cca08 166
sayzyas 43:1675750cca08 167 INTERFACE intf[MAX_INTF];
sayzyas 43:1675750cca08 168 uint32_t sizeControlEndpoint;
sayzyas 43:1675750cca08 169 uint8_t hub_nb;
sayzyas 43:1675750cca08 170 uint8_t port;
sayzyas 43:1675750cca08 171 uint16_t vid;
sayzyas 43:1675750cca08 172 uint16_t pid;
sayzyas 43:1675750cca08 173 uint8_t addr;
sayzyas 43:1675750cca08 174 uint8_t device_class;
sayzyas 43:1675750cca08 175 uint8_t device_subclass;
sayzyas 43:1675750cca08 176 uint8_t proto;
sayzyas 43:1675750cca08 177 bool speed;
sayzyas 43:1675750cca08 178 volatile bool activeAddr;
sayzyas 43:1675750cca08 179 volatile bool enumerated;
sayzyas 43:1675750cca08 180 uint8_t nb_interf;
sayzyas 43:1675750cca08 181
sayzyas 43:1675750cca08 182 void init();
sayzyas 43:1675750cca08 183 };
sayzyas 43:1675750cca08 184
sayzyas 43:1675750cca08 185 #endif