USB BARCODE READER

Fork of USBHOST by ST

Committer:
shivanandgowdakr
Date:
Mon Jul 02 12:10:45 2018 +0000
Revision:
7:ccd0ac9ae1c9
Parent:
1:ab240722d7ef
USB BARCODE READER ;

Who changed what in which revision?

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