Usb read

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Committer:
va009039
Date:
Mon Feb 03 13:00:16 2014 +0000
Revision:
9:7f9f64cf5ded
Parent:
8:6463cd1964c0
Child:
10:40c7f6788902
add interrupt write transfer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 7:9a20482c9a7a 1 // Simple USBHost for FRDM-KL46Z
va009039 7:9a20482c9a7a 2 #pragma once
va009039 7:9a20482c9a7a 3 #include "mbed.h"
va009039 7:9a20482c9a7a 4 #include "USBHALHost.h"
va009039 7:9a20482c9a7a 5 #include "USBDeviceConnected.h"
va009039 8:6463cd1964c0 6 #include "IUSBEnumerator.h"
va009039 8:6463cd1964c0 7 #include "USBHostConf.h"
va009039 9:7f9f64cf5ded 8 #include "dbg.h"
va009039 9:7f9f64cf5ded 9 #include "myqueue.h"
va009039 7:9a20482c9a7a 10
va009039 7:9a20482c9a7a 11 class USBHost : public USBHALHost {
va009039 7:9a20482c9a7a 12 public:
va009039 7:9a20482c9a7a 13 /**
va009039 7:9a20482c9a7a 14 * Static method to create or retrieve the single USBHost instance
va009039 7:9a20482c9a7a 15 */
va009039 7:9a20482c9a7a 16 static USBHost* getHostInst();
va009039 7:9a20482c9a7a 17
va009039 7:9a20482c9a7a 18 /**
va009039 7:9a20482c9a7a 19 * Control read: setup stage, data stage and status stage
va009039 7:9a20482c9a7a 20 *
va009039 7:9a20482c9a7a 21 * @param dev the control read will be done for this device
va009039 7:9a20482c9a7a 22 * @param requestType request type
va009039 7:9a20482c9a7a 23 * @param request request
va009039 7:9a20482c9a7a 24 * @param value value
va009039 7:9a20482c9a7a 25 * @param index index
va009039 7:9a20482c9a7a 26 * @param buf pointer on a buffer where will be store the data received
va009039 7:9a20482c9a7a 27 * @param len length of the transfer
va009039 7:9a20482c9a7a 28 *
va009039 7:9a20482c9a7a 29 * @returns status of the control read
va009039 7:9a20482c9a7a 30 */
va009039 7:9a20482c9a7a 31 USB_TYPE controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len);
va009039 7:9a20482c9a7a 32
va009039 7:9a20482c9a7a 33 /**
va009039 7:9a20482c9a7a 34 * Control write: setup stage, data stage and status stage
va009039 7:9a20482c9a7a 35 *
va009039 7:9a20482c9a7a 36 * @param dev the control write will be done for this device
va009039 7:9a20482c9a7a 37 * @param requestType request type
va009039 7:9a20482c9a7a 38 * @param request request
va009039 7:9a20482c9a7a 39 * @param value value
va009039 7:9a20482c9a7a 40 * @param index index
va009039 7:9a20482c9a7a 41 * @param buf pointer on a buffer which will be written
va009039 7:9a20482c9a7a 42 * @param len length of the transfer
va009039 7:9a20482c9a7a 43 *
va009039 7:9a20482c9a7a 44 * @returns status of the control write
va009039 7:9a20482c9a7a 45 */
va009039 7:9a20482c9a7a 46 USB_TYPE controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len);
va009039 9:7f9f64cf5ded 47
va009039 7:9a20482c9a7a 48 /**
va009039 7:9a20482c9a7a 49 * Bulk read
va009039 7:9a20482c9a7a 50 *
va009039 7:9a20482c9a7a 51 * @param dev the bulk transfer will be done for this device
va009039 7:9a20482c9a7a 52 * @param ep USBEndpoint which will be used to read a packet
va009039 7:9a20482c9a7a 53 * @param buf pointer on a buffer where will be store the data received
va009039 7:9a20482c9a7a 54 * @param len length of the transfer
va009039 7:9a20482c9a7a 55 * @param blocking if true, the read is blocking (wait for completion)
va009039 7:9a20482c9a7a 56 *
va009039 7:9a20482c9a7a 57 * @returns status of the bulk read
va009039 7:9a20482c9a7a 58 */
va009039 7:9a20482c9a7a 59 USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 7:9a20482c9a7a 60
va009039 7:9a20482c9a7a 61 /**
va009039 7:9a20482c9a7a 62 * Bulk write
va009039 7:9a20482c9a7a 63 *
va009039 7:9a20482c9a7a 64 * @param dev the bulk transfer will be done for this device
va009039 7:9a20482c9a7a 65 * @param ep USBEndpoint which will be used to write a packet
va009039 7:9a20482c9a7a 66 * @param buf pointer on a buffer which will be written
va009039 7:9a20482c9a7a 67 * @param len length of the transfer
va009039 7:9a20482c9a7a 68 * @param blocking if true, the write is blocking (wait for completion)
va009039 7:9a20482c9a7a 69 *
va009039 7:9a20482c9a7a 70 * @returns status of the bulk write
va009039 7:9a20482c9a7a 71 */
va009039 7:9a20482c9a7a 72 USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 7:9a20482c9a7a 73
va009039 7:9a20482c9a7a 74 /**
va009039 7:9a20482c9a7a 75 * Interrupt read
va009039 7:9a20482c9a7a 76 *
va009039 7:9a20482c9a7a 77 * @param dev the interrupt transfer will be done for this device
va009039 7:9a20482c9a7a 78 * @param ep USBEndpoint which will be used to write a packet
va009039 7:9a20482c9a7a 79 * @param buf pointer on a buffer which will be written
va009039 7:9a20482c9a7a 80 * @param len length of the transfer
va009039 7:9a20482c9a7a 81 * @param blocking if true, the read is blocking (wait for completion)
va009039 7:9a20482c9a7a 82 *
va009039 7:9a20482c9a7a 83 * @returns status of the interrupt read
va009039 7:9a20482c9a7a 84 */
va009039 7:9a20482c9a7a 85 USB_TYPE interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 7:9a20482c9a7a 86
va009039 7:9a20482c9a7a 87 /**
va009039 7:9a20482c9a7a 88 * Interrupt write
va009039 7:9a20482c9a7a 89 *
va009039 7:9a20482c9a7a 90 * @param dev the interrupt transfer will be done for this device
va009039 7:9a20482c9a7a 91 * @param ep USBEndpoint which will be used to write a packet
va009039 7:9a20482c9a7a 92 * @param buf pointer on a buffer which will be written
va009039 7:9a20482c9a7a 93 * @param len length of the transfer
va009039 7:9a20482c9a7a 94 * @param blocking if true, the write is blocking (wait for completion)
va009039 7:9a20482c9a7a 95 *
va009039 7:9a20482c9a7a 96 * @returns status of the interrupt write
va009039 7:9a20482c9a7a 97 */
va009039 7:9a20482c9a7a 98 USB_TYPE interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true);
va009039 7:9a20482c9a7a 99
va009039 8:6463cd1964c0 100 /**
va009039 9:7f9f64cf5ded 101 * Isochronous read
va009039 9:7f9f64cf5ded 102 *
va009039 9:7f9f64cf5ded 103 * @param dev the isochronous transfer will be done for this device
va009039 9:7f9f64cf5ded 104 * @param ep USBEndpoint which will be used to write a packet
va009039 9:7f9f64cf5ded 105 * @param buf pointer on a buffer which will be written
va009039 9:7f9f64cf5ded 106 * @param len length of the transfer
va009039 9:7f9f64cf5ded 107 * @param blocking if true, the read is blocking (wait for completion)
va009039 9:7f9f64cf5ded 108 *
va009039 9:7f9f64cf5ded 109 * @returns status of the interrupt read
va009039 9:7f9f64cf5ded 110 */
va009039 9:7f9f64cf5ded 111 USB_TYPE isochronousRead(USBDeviceConnected* dev, USBEndpoint* ep, uint8_t* buf, uint32_t len, bool blocking = true);
va009039 9:7f9f64cf5ded 112
va009039 9:7f9f64cf5ded 113 /**
va009039 8:6463cd1964c0 114 * Enumerate a device.
va009039 8:6463cd1964c0 115 *
va009039 8:6463cd1964c0 116 * @param dev device which will be enumerated
va009039 8:6463cd1964c0 117 *
va009039 8:6463cd1964c0 118 * @returns status of the enumeration
va009039 8:6463cd1964c0 119 */
va009039 8:6463cd1964c0 120 USB_TYPE enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator);
va009039 8:6463cd1964c0 121
va009039 8:6463cd1964c0 122 /**
va009039 8:6463cd1964c0 123 * Get a device
va009039 8:6463cd1964c0 124 *
va009039 8:6463cd1964c0 125 * @param index index of the device which will be returned
va009039 8:6463cd1964c0 126 *
va009039 8:6463cd1964c0 127 * @returns pointer on the "index" device
va009039 8:6463cd1964c0 128 */
va009039 8:6463cd1964c0 129 USBDeviceConnected * getDevice(uint8_t index) {
va009039 9:7f9f64cf5ded 130 return DeviceLists.empty() ? NULL : DeviceLists.at(index);
va009039 8:6463cd1964c0 131 }
va009039 8:6463cd1964c0 132
va009039 8:6463cd1964c0 133 /**
va009039 8:6463cd1964c0 134 * register a driver into the host associated with a callback function called when the device is disconnected
va009039 8:6463cd1964c0 135 *
va009039 8:6463cd1964c0 136 * @param dev device
va009039 8:6463cd1964c0 137 * @param intf interface number
va009039 8:6463cd1964c0 138 * @param tptr pointer to the object to call the member function on
va009039 8:6463cd1964c0 139 * @param mptr pointer to the member function to be called
va009039 8:6463cd1964c0 140 */
va009039 8:6463cd1964c0 141 template<typename T>
va009039 8:6463cd1964c0 142 void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) {
va009039 8:6463cd1964c0 143 }
va009039 8:6463cd1964c0 144
va009039 9:7f9f64cf5ded 145 // KL46Z-USBHost extensions
va009039 9:7f9f64cf5ded 146 int interruptReadNB(USBEndpoint* ep, uint8_t* data, int size);
va009039 9:7f9f64cf5ded 147 int interruptWriteNB(USBEndpoint* ep, const uint8_t* data, int size);
va009039 9:7f9f64cf5ded 148 int bulkReadNB(USBEndpoint*ep, uint8_t* data, int size);
va009039 9:7f9f64cf5ded 149 int bulkWriteNB(USBEndpoint*ep, const uint8_t* data, int size);
va009039 9:7f9f64cf5ded 150 int isochronousReadNB(USBEndpoint*ep, uint8_t* data, int size);
va009039 9:7f9f64cf5ded 151 static void poll();
va009039 7:9a20482c9a7a 152
va009039 7:9a20482c9a7a 153 private:
va009039 7:9a20482c9a7a 154 USBHost();
va009039 7:9a20482c9a7a 155 static USBHost* inst;
va009039 8:6463cd1964c0 156 virtual bool addDevice(int hub, int port, bool lowSpeed);
va009039 8:6463cd1964c0 157 void root_enumeration(USBDeviceConnected* dev);
va009039 8:6463cd1964c0 158 void parseConfDescr(USBDeviceConnected* dev, uint8_t* conf_descr, uint32_t len, IUSBEnumerator* pEnumerator);
va009039 9:7f9f64cf5ded 159 myqueue<USBDeviceConnected*>DeviceLists;
va009039 8:6463cd1964c0 160
va009039 8:6463cd1964c0 161 int ControlRead(USBDeviceConnected* dev, SETUP_PACKET* setup, uint8_t* data, int size);
va009039 8:6463cd1964c0 162 int ControlWrite(USBDeviceConnected* dev, SETUP_PACKET* setup, uint8_t* data = NULL, int size = 0);
va009039 9:7f9f64cf5ded 163 int bulkReadBLOCK(USBEndpoint*ep, uint8_t* data, int size, int timeout_ms);
va009039 9:7f9f64cf5ded 164 void task();
va009039 9:7f9f64cf5ded 165 myqueue<USBEndpoint*>ep_queue;
va009039 8:6463cd1964c0 166
va009039 8:6463cd1964c0 167 // USB HUB
va009039 8:6463cd1964c0 168 bool Hub(USBDeviceConnected* dev);
va009039 8:6463cd1964c0 169 int SetPortPower(USBDeviceConnected* dev, int port);
va009039 8:6463cd1964c0 170 int ClearPortPower(USBDeviceConnected* dev, int port);
va009039 8:6463cd1964c0 171 int PortReset(USBDeviceConnected* dev, int port);
va009039 8:6463cd1964c0 172 int SetPortFeature(USBDeviceConnected* dev, int feature, int index);
va009039 8:6463cd1964c0 173 int ClearPortFeature(USBDeviceConnected* dev, int feature, int index);
va009039 8:6463cd1964c0 174 int SetPortReset(USBDeviceConnected* dev, int port);
va009039 8:6463cd1964c0 175 int GetPortStatus(USBDeviceConnected* dev, int port, uint32_t* status);
va009039 7:9a20482c9a7a 176 };
va009039 7:9a20482c9a7a 177