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