mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 1:9db0e321a9f4 1 /*
kenjiArai 1:9db0e321a9f4 2 * Copyright (c) 2018-2019, Arm Limited and affiliates.
kenjiArai 1:9db0e321a9f4 3 * SPDX-License-Identifier: Apache-2.0
kenjiArai 1:9db0e321a9f4 4 *
kenjiArai 1:9db0e321a9f4 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:9db0e321a9f4 6 * you may not use this file except in compliance with the License.
kenjiArai 1:9db0e321a9f4 7 * You may obtain a copy of the License at
kenjiArai 1:9db0e321a9f4 8 *
kenjiArai 1:9db0e321a9f4 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:9db0e321a9f4 10 *
kenjiArai 1:9db0e321a9f4 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:9db0e321a9f4 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:9db0e321a9f4 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:9db0e321a9f4 14 * See the License for the specific language governing permissions and
kenjiArai 1:9db0e321a9f4 15 * limitations under the License.
kenjiArai 1:9db0e321a9f4 16 */
kenjiArai 1:9db0e321a9f4 17
kenjiArai 1:9db0e321a9f4 18 #ifndef USBDEVICE_H
kenjiArai 1:9db0e321a9f4 19 #define USBDEVICE_H
kenjiArai 1:9db0e321a9f4 20
kenjiArai 1:9db0e321a9f4 21 #include <stddef.h>
kenjiArai 1:9db0e321a9f4 22 #include "USBDevice_Types.h"
kenjiArai 1:9db0e321a9f4 23 #include "USBPhy.h"
kenjiArai 1:9db0e321a9f4 24 #include "mbed_critical.h"
kenjiArai 1:9db0e321a9f4 25 #include "Callback.h"
kenjiArai 1:9db0e321a9f4 26
kenjiArai 1:9db0e321a9f4 27 /**
kenjiArai 1:9db0e321a9f4 28 * \defgroup drivers_USBDevice USBDevice class
kenjiArai 1:9db0e321a9f4 29 * \ingroup drivers-internal-api-usb
kenjiArai 1:9db0e321a9f4 30 * @{
kenjiArai 1:9db0e321a9f4 31 */
kenjiArai 1:9db0e321a9f4 32
kenjiArai 1:9db0e321a9f4 33 /**
kenjiArai 1:9db0e321a9f4 34 * Core USB Device driver
kenjiArai 1:9db0e321a9f4 35 *
kenjiArai 1:9db0e321a9f4 36 * USB driver which wraps and provides synchronization for a USBPhy object.
kenjiArai 1:9db0e321a9f4 37 */
kenjiArai 1:9db0e321a9f4 38 class USBDevice: public USBPhyEvents {
kenjiArai 1:9db0e321a9f4 39 public:
kenjiArai 1:9db0e321a9f4 40 typedef void (USBDevice::*ep_cb_t)();
kenjiArai 1:9db0e321a9f4 41
kenjiArai 1:9db0e321a9f4 42 enum RequestResult {
kenjiArai 1:9db0e321a9f4 43 Receive = 0,
kenjiArai 1:9db0e321a9f4 44 Send = 1,
kenjiArai 1:9db0e321a9f4 45 Success = 2,
kenjiArai 1:9db0e321a9f4 46 Failure = 3,
kenjiArai 1:9db0e321a9f4 47 PassThrough = 4,
kenjiArai 1:9db0e321a9f4 48 };
kenjiArai 1:9db0e321a9f4 49
kenjiArai 1:9db0e321a9f4 50 enum DeviceState {
kenjiArai 1:9db0e321a9f4 51 Attached,
kenjiArai 1:9db0e321a9f4 52 Powered,
kenjiArai 1:9db0e321a9f4 53 Default,
kenjiArai 1:9db0e321a9f4 54 Address,
kenjiArai 1:9db0e321a9f4 55 Configured
kenjiArai 1:9db0e321a9f4 56 };
kenjiArai 1:9db0e321a9f4 57
kenjiArai 1:9db0e321a9f4 58 struct setup_packet_t {
kenjiArai 1:9db0e321a9f4 59 struct {
kenjiArai 1:9db0e321a9f4 60 uint8_t dataTransferDirection;
kenjiArai 1:9db0e321a9f4 61 uint8_t Type;
kenjiArai 1:9db0e321a9f4 62 uint8_t Recipient;
kenjiArai 1:9db0e321a9f4 63 } bmRequestType;
kenjiArai 1:9db0e321a9f4 64 uint8_t bRequest;
kenjiArai 1:9db0e321a9f4 65 uint16_t wValue;
kenjiArai 1:9db0e321a9f4 66 uint16_t wIndex;
kenjiArai 1:9db0e321a9f4 67 uint16_t wLength;
kenjiArai 1:9db0e321a9f4 68 };
kenjiArai 1:9db0e321a9f4 69
kenjiArai 1:9db0e321a9f4 70 /**
kenjiArai 1:9db0e321a9f4 71 * Instantiate a new USBDevice with the given parameters
kenjiArai 1:9db0e321a9f4 72 *
kenjiArai 1:9db0e321a9f4 73 * @param phy The USBPhy providing physical USB access
kenjiArai 1:9db0e321a9f4 74 * @param vendor_id The USB vendor ID
kenjiArai 1:9db0e321a9f4 75 * @param product_id The USB product ID
kenjiArai 1:9db0e321a9f4 76 * @param product_release The device release number
kenjiArai 1:9db0e321a9f4 77 */
kenjiArai 1:9db0e321a9f4 78 USBDevice(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
kenjiArai 1:9db0e321a9f4 79
kenjiArai 1:9db0e321a9f4 80 /**
kenjiArai 1:9db0e321a9f4 81 * Cleanup this USBDevice
kenjiArai 1:9db0e321a9f4 82 *
kenjiArai 1:9db0e321a9f4 83 * This USBDevice must be uninitialized when the destructor is
kenjiArai 1:9db0e321a9f4 84 * called or the behavior is undefined.
kenjiArai 1:9db0e321a9f4 85 */
kenjiArai 1:9db0e321a9f4 86 virtual ~USBDevice();
kenjiArai 1:9db0e321a9f4 87
kenjiArai 1:9db0e321a9f4 88 /**
kenjiArai 1:9db0e321a9f4 89 * Initialize this instance
kenjiArai 1:9db0e321a9f4 90 *
kenjiArai 1:9db0e321a9f4 91 * This function must be called before calling
kenjiArai 1:9db0e321a9f4 92 * any other functions of this class, unless specifically
kenjiArai 1:9db0e321a9f4 93 */
kenjiArai 1:9db0e321a9f4 94 void init();
kenjiArai 1:9db0e321a9f4 95
kenjiArai 1:9db0e321a9f4 96 /**
kenjiArai 1:9db0e321a9f4 97 * Power down this instance
kenjiArai 1:9db0e321a9f4 98 *
kenjiArai 1:9db0e321a9f4 99 * Disable interrupts and stop sending events.
kenjiArai 1:9db0e321a9f4 100 * This method can be used for temporary power-saving; This call can allow
kenjiArai 1:9db0e321a9f4 101 * USB to be temporarily disabled to permit power saving.
kenjiArai 1:9db0e321a9f4 102 * However, it is up to the user to make sure all the
kenjiArai 1:9db0e321a9f4 103 * transfers have concluded (for example when USB power is lost).
kenjiArai 1:9db0e321a9f4 104 * USBDevice::connect can be used to resume USB operation.
kenjiArai 1:9db0e321a9f4 105 */
kenjiArai 1:9db0e321a9f4 106 void deinit();
kenjiArai 1:9db0e321a9f4 107
kenjiArai 1:9db0e321a9f4 108 /**
kenjiArai 1:9db0e321a9f4 109 * Check if the device is configured
kenjiArai 1:9db0e321a9f4 110 *
kenjiArai 1:9db0e321a9f4 111 * @returns true if configured, false otherwise
kenjiArai 1:9db0e321a9f4 112 */
kenjiArai 1:9db0e321a9f4 113 bool configured();
kenjiArai 1:9db0e321a9f4 114
kenjiArai 1:9db0e321a9f4 115 /**
kenjiArai 1:9db0e321a9f4 116 * Connect a device
kenjiArai 1:9db0e321a9f4 117 * This method can also be used to resume USB operation when USB power is
kenjiArai 1:9db0e321a9f4 118 * detected after it was suspended via USBDevice::deinit.
kenjiArai 1:9db0e321a9f4 119 */
kenjiArai 1:9db0e321a9f4 120 void connect();
kenjiArai 1:9db0e321a9f4 121
kenjiArai 1:9db0e321a9f4 122 /**
kenjiArai 1:9db0e321a9f4 123 * Disconnect a device
kenjiArai 1:9db0e321a9f4 124 */
kenjiArai 1:9db0e321a9f4 125 void disconnect();
kenjiArai 1:9db0e321a9f4 126
kenjiArai 1:9db0e321a9f4 127 /**
kenjiArai 1:9db0e321a9f4 128 * Enable the start of frame interrupt
kenjiArai 1:9db0e321a9f4 129 *
kenjiArai 1:9db0e321a9f4 130 * Call USBDevice::callback_sof on every frame.
kenjiArai 1:9db0e321a9f4 131 */
kenjiArai 1:9db0e321a9f4 132 void sof_enable();
kenjiArai 1:9db0e321a9f4 133
kenjiArai 1:9db0e321a9f4 134 /**
kenjiArai 1:9db0e321a9f4 135 * Disable the start of frame interrupt
kenjiArai 1:9db0e321a9f4 136 *
kenjiArai 1:9db0e321a9f4 137 * Stop calling USBDevice::callback_sof.
kenjiArai 1:9db0e321a9f4 138 */
kenjiArai 1:9db0e321a9f4 139 void sof_disable();
kenjiArai 1:9db0e321a9f4 140
kenjiArai 1:9db0e321a9f4 141 /**
kenjiArai 1:9db0e321a9f4 142 * Add an endpoint
kenjiArai 1:9db0e321a9f4 143 *
kenjiArai 1:9db0e321a9f4 144 * @param endpoint Endpoint to enable
kenjiArai 1:9db0e321a9f4 145 * @param max_packet Maximum size of a packet which can be sent or received on this endpoint
kenjiArai 1:9db0e321a9f4 146 * @param type Endpoint type - USB_EP_TYPE_BULK, USB_EP_TYPE_INT or USB_EP_TYPE_ISO
kenjiArai 1:9db0e321a9f4 147 * @param callback Method pointer to be called when a packet is transferred
kenjiArai 1:9db0e321a9f4 148 * @returns true if successful, false otherwise
kenjiArai 1:9db0e321a9f4 149 */
kenjiArai 1:9db0e321a9f4 150 bool endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, mbed::Callback<void()> callback = NULL);
kenjiArai 1:9db0e321a9f4 151
kenjiArai 1:9db0e321a9f4 152 /**
kenjiArai 1:9db0e321a9f4 153 * Add an endpoint
kenjiArai 1:9db0e321a9f4 154 *
kenjiArai 1:9db0e321a9f4 155 * @param endpoint Endpoint to enable
kenjiArai 1:9db0e321a9f4 156 * @param max_packet Maximum size of a packet which can be sent or received on this endpoint
kenjiArai 1:9db0e321a9f4 157 * @param type Endpoint type - USB_EP_TYPE_BULK, USB_EP_TYPE_INT or USB_EP_TYPE_ISO
kenjiArai 1:9db0e321a9f4 158 * @param callback Method pointer to be called when a packet is transferred
kenjiArai 1:9db0e321a9f4 159 * @returns true if successful, false otherwise
kenjiArai 1:9db0e321a9f4 160 */
kenjiArai 1:9db0e321a9f4 161 template<typename T>
kenjiArai 1:9db0e321a9f4 162 bool endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, void (T::*callback)())
kenjiArai 1:9db0e321a9f4 163 {
kenjiArai 1:9db0e321a9f4 164 return endpoint_add(endpoint, max_packet, type, mbed::callback(this, static_cast<ep_cb_t>(callback)));
kenjiArai 1:9db0e321a9f4 165 }
kenjiArai 1:9db0e321a9f4 166
kenjiArai 1:9db0e321a9f4 167 /**
kenjiArai 1:9db0e321a9f4 168 * Remove an endpoint
kenjiArai 1:9db0e321a9f4 169 *
kenjiArai 1:9db0e321a9f4 170 * @param endpoint Endpoint to disable
kenjiArai 1:9db0e321a9f4 171 * @note This endpoint must already have been setup with endpoint_add
kenjiArai 1:9db0e321a9f4 172 */
kenjiArai 1:9db0e321a9f4 173 void endpoint_remove(usb_ep_t endpoint);
kenjiArai 1:9db0e321a9f4 174
kenjiArai 1:9db0e321a9f4 175 /**
kenjiArai 1:9db0e321a9f4 176 * Remove all non-zero endpoints
kenjiArai 1:9db0e321a9f4 177 */
kenjiArai 1:9db0e321a9f4 178 void endpoint_remove_all();
kenjiArai 1:9db0e321a9f4 179
kenjiArai 1:9db0e321a9f4 180 /**
kenjiArai 1:9db0e321a9f4 181 * Stall an endpoint
kenjiArai 1:9db0e321a9f4 182 *
kenjiArai 1:9db0e321a9f4 183 * If there is an ongoing transfer on this endpoint then it will
kenjiArai 1:9db0e321a9f4 184 * be aborted.
kenjiArai 1:9db0e321a9f4 185 *
kenjiArai 1:9db0e321a9f4 186 * @param endpoint Endpoint to stall
kenjiArai 1:9db0e321a9f4 187 * @note You cannot stall endpoint 0 with this function
kenjiArai 1:9db0e321a9f4 188 * @note This endpoint must already have been setup with endpoint_add
kenjiArai 1:9db0e321a9f4 189 */
kenjiArai 1:9db0e321a9f4 190 void endpoint_stall(usb_ep_t endpoint);
kenjiArai 1:9db0e321a9f4 191
kenjiArai 1:9db0e321a9f4 192 /**
kenjiArai 1:9db0e321a9f4 193 * Un-stall an endpoint
kenjiArai 1:9db0e321a9f4 194 *
kenjiArai 1:9db0e321a9f4 195 * Un-stalling an endpoint resets data toggle back to DATA0.
kenjiArai 1:9db0e321a9f4 196 * Additionally, if there is an ongoing transfer on this endpoint
kenjiArai 1:9db0e321a9f4 197 * it will be aborted.
kenjiArai 1:9db0e321a9f4 198 *
kenjiArai 1:9db0e321a9f4 199 * @param endpoint Endpoint to un-stall
kenjiArai 1:9db0e321a9f4 200 * @note This endpoint must already have been setup with endpoint_add
kenjiArai 1:9db0e321a9f4 201 */
kenjiArai 1:9db0e321a9f4 202 void endpoint_unstall(usb_ep_t endpoint);
kenjiArai 1:9db0e321a9f4 203
kenjiArai 1:9db0e321a9f4 204 /**
kenjiArai 1:9db0e321a9f4 205 * Get the current maximum size for this endpoint
kenjiArai 1:9db0e321a9f4 206 *
kenjiArai 1:9db0e321a9f4 207 * Return the currently configured maximum packet size, wMaxPacketSize,
kenjiArai 1:9db0e321a9f4 208 * for this endpoint.
kenjiArai 1:9db0e321a9f4 209 * @note This endpoint must already have been setup with endpoint_add
kenjiArai 1:9db0e321a9f4 210 */
kenjiArai 1:9db0e321a9f4 211 uint32_t endpoint_max_packet_size(usb_ep_t endpoint);
kenjiArai 1:9db0e321a9f4 212
kenjiArai 1:9db0e321a9f4 213 /**
kenjiArai 1:9db0e321a9f4 214 * Abort the current transfer on this endpoint
kenjiArai 1:9db0e321a9f4 215 *
kenjiArai 1:9db0e321a9f4 216 * @param endpoint endpoint with transfer to abort
kenjiArai 1:9db0e321a9f4 217 * @note This endpoint must already have been setup with endpoint_add
kenjiArai 1:9db0e321a9f4 218 */
kenjiArai 1:9db0e321a9f4 219 void endpoint_abort(usb_ep_t endpoint);
kenjiArai 1:9db0e321a9f4 220
kenjiArai 1:9db0e321a9f4 221 /**
kenjiArai 1:9db0e321a9f4 222 * start a read on the given endpoint
kenjiArai 1:9db0e321a9f4 223 *
kenjiArai 1:9db0e321a9f4 224 * Start a read on the given endpoint. The data buffer must remain
kenjiArai 1:9db0e321a9f4 225 * unchanged until the transfer either completes or is aborted.
kenjiArai 1:9db0e321a9f4 226 *
kenjiArai 1:9db0e321a9f4 227 * @param endpoint endpoint to read data from
kenjiArai 1:9db0e321a9f4 228 * @param buffer buffer to fill with read data
kenjiArai 1:9db0e321a9f4 229 * @param size The size of data to read. This must be greater than or equal
kenjiArai 1:9db0e321a9f4 230 * to the max packet size for this endpoint
kenjiArai 1:9db0e321a9f4 231 * @return true if the read was completed, otherwise false
kenjiArai 1:9db0e321a9f4 232 * @note This endpoint must already have been setup with endpoint_add
kenjiArai 1:9db0e321a9f4 233 */
kenjiArai 1:9db0e321a9f4 234 bool read_start(usb_ep_t endpoint, uint8_t *buffer, uint32_t size);
kenjiArai 1:9db0e321a9f4 235
kenjiArai 1:9db0e321a9f4 236 /**
kenjiArai 1:9db0e321a9f4 237 * Get the status of a read
kenjiArai 1:9db0e321a9f4 238 *
kenjiArai 1:9db0e321a9f4 239 * @param endpoint endpoint to get the status of
kenjiArai 1:9db0e321a9f4 240 * @return number of bytes read by this endpoint
kenjiArai 1:9db0e321a9f4 241 */
kenjiArai 1:9db0e321a9f4 242 uint32_t read_finish(usb_ep_t endpoint);
kenjiArai 1:9db0e321a9f4 243
kenjiArai 1:9db0e321a9f4 244 /**
kenjiArai 1:9db0e321a9f4 245 * Write a data to the given endpoint
kenjiArai 1:9db0e321a9f4 246 *
kenjiArai 1:9db0e321a9f4 247 * Write data to an endpoint. The data sent must remain unchanged until
kenjiArai 1:9db0e321a9f4 248 * the transfer either completes or is aborted.
kenjiArai 1:9db0e321a9f4 249 *
kenjiArai 1:9db0e321a9f4 250 * @param endpoint endpoint to write data to
kenjiArai 1:9db0e321a9f4 251 * @param buffer data to write
kenjiArai 1:9db0e321a9f4 252 * @param size the size of data to send. This must be less than or equal to the
kenjiArai 1:9db0e321a9f4 253 * max packet size of this endpoint
kenjiArai 1:9db0e321a9f4 254 * @note This endpoint must already have been setup with endpoint_add
kenjiArai 1:9db0e321a9f4 255 */
kenjiArai 1:9db0e321a9f4 256 bool write_start(usb_ep_t endpoint, uint8_t *buffer, uint32_t size);
kenjiArai 1:9db0e321a9f4 257
kenjiArai 1:9db0e321a9f4 258 /**
kenjiArai 1:9db0e321a9f4 259 * Get the status of a write
kenjiArai 1:9db0e321a9f4 260 *
kenjiArai 1:9db0e321a9f4 261 * @param endpoint endpoint to get the status of
kenjiArai 1:9db0e321a9f4 262 * @return number of bytes sent by this endpoint
kenjiArai 1:9db0e321a9f4 263 */
kenjiArai 1:9db0e321a9f4 264 uint32_t write_finish(usb_ep_t endpoint);
kenjiArai 1:9db0e321a9f4 265
kenjiArai 1:9db0e321a9f4 266 /*
kenjiArai 1:9db0e321a9f4 267 * Get device descriptor.
kenjiArai 1:9db0e321a9f4 268 *
kenjiArai 1:9db0e321a9f4 269 * @returns pointer to the device descriptor
kenjiArai 1:9db0e321a9f4 270 */
kenjiArai 1:9db0e321a9f4 271 virtual const uint8_t *device_desc();
kenjiArai 1:9db0e321a9f4 272
kenjiArai 1:9db0e321a9f4 273 /*
kenjiArai 1:9db0e321a9f4 274 * Get configuration descriptor
kenjiArai 1:9db0e321a9f4 275 *
kenjiArai 1:9db0e321a9f4 276 * @param index descriptor index
kenjiArai 1:9db0e321a9f4 277 * @returns pointer to the configuration descriptor
kenjiArai 1:9db0e321a9f4 278 */
kenjiArai 1:9db0e321a9f4 279 virtual const uint8_t *configuration_desc(uint8_t index) = 0;
kenjiArai 1:9db0e321a9f4 280
kenjiArai 1:9db0e321a9f4 281 /*
kenjiArai 1:9db0e321a9f4 282 * Get string lang id descriptor
kenjiArai 1:9db0e321a9f4 283 *
kenjiArai 1:9db0e321a9f4 284 * @return pointer to the string lang id descriptor
kenjiArai 1:9db0e321a9f4 285 */
kenjiArai 1:9db0e321a9f4 286 virtual const uint8_t *string_langid_desc();
kenjiArai 1:9db0e321a9f4 287
kenjiArai 1:9db0e321a9f4 288 /*
kenjiArai 1:9db0e321a9f4 289 * Get string manufacturer descriptor
kenjiArai 1:9db0e321a9f4 290 *
kenjiArai 1:9db0e321a9f4 291 * @returns pointer to the string manufacturer descriptor
kenjiArai 1:9db0e321a9f4 292 */
kenjiArai 1:9db0e321a9f4 293 virtual const uint8_t *string_imanufacturer_desc();
kenjiArai 1:9db0e321a9f4 294
kenjiArai 1:9db0e321a9f4 295 /*
kenjiArai 1:9db0e321a9f4 296 * Get string product descriptor
kenjiArai 1:9db0e321a9f4 297 *
kenjiArai 1:9db0e321a9f4 298 * @returns pointer to the string product descriptor
kenjiArai 1:9db0e321a9f4 299 */
kenjiArai 1:9db0e321a9f4 300 virtual const uint8_t *string_iproduct_desc();
kenjiArai 1:9db0e321a9f4 301
kenjiArai 1:9db0e321a9f4 302 /*
kenjiArai 1:9db0e321a9f4 303 * Get string serial descriptor
kenjiArai 1:9db0e321a9f4 304 *
kenjiArai 1:9db0e321a9f4 305 * @returns pointer to the string serial descriptor
kenjiArai 1:9db0e321a9f4 306 */
kenjiArai 1:9db0e321a9f4 307 virtual const uint8_t *string_iserial_desc();
kenjiArai 1:9db0e321a9f4 308
kenjiArai 1:9db0e321a9f4 309 /*
kenjiArai 1:9db0e321a9f4 310 * Get string configuration descriptor
kenjiArai 1:9db0e321a9f4 311 *
kenjiArai 1:9db0e321a9f4 312 * @returns pointer to the string configuration descriptor
kenjiArai 1:9db0e321a9f4 313 */
kenjiArai 1:9db0e321a9f4 314 virtual const uint8_t *string_iconfiguration_desc();
kenjiArai 1:9db0e321a9f4 315
kenjiArai 1:9db0e321a9f4 316 /*
kenjiArai 1:9db0e321a9f4 317 * Get string interface descriptor
kenjiArai 1:9db0e321a9f4 318 *
kenjiArai 1:9db0e321a9f4 319 * @returns pointer to the string interface descriptor
kenjiArai 1:9db0e321a9f4 320 */
kenjiArai 1:9db0e321a9f4 321 virtual const uint8_t *string_iinterface_desc();
kenjiArai 1:9db0e321a9f4 322
kenjiArai 1:9db0e321a9f4 323 /*
kenjiArai 1:9db0e321a9f4 324 * Get the length of the report descriptor
kenjiArai 1:9db0e321a9f4 325 *
kenjiArai 1:9db0e321a9f4 326 * @returns length of the report descriptor
kenjiArai 1:9db0e321a9f4 327 */
kenjiArai 1:9db0e321a9f4 328 virtual uint16_t report_desc_dength()
kenjiArai 1:9db0e321a9f4 329 {
kenjiArai 1:9db0e321a9f4 330 return 0;
kenjiArai 1:9db0e321a9f4 331 };
kenjiArai 1:9db0e321a9f4 332
kenjiArai 1:9db0e321a9f4 333 protected:
kenjiArai 1:9db0e321a9f4 334
kenjiArai 1:9db0e321a9f4 335 /**
kenjiArai 1:9db0e321a9f4 336 * Called by USBDevice layer on power state change.
kenjiArai 1:9db0e321a9f4 337 *
kenjiArai 1:9db0e321a9f4 338 * @param powered true if device is powered, false otherwise
kenjiArai 1:9db0e321a9f4 339 *
kenjiArai 1:9db0e321a9f4 340 * Warning: Called in ISR context
kenjiArai 1:9db0e321a9f4 341 */
kenjiArai 1:9db0e321a9f4 342 virtual void callback_power(bool powered)
kenjiArai 1:9db0e321a9f4 343 {
kenjiArai 1:9db0e321a9f4 344
kenjiArai 1:9db0e321a9f4 345 }
kenjiArai 1:9db0e321a9f4 346
kenjiArai 1:9db0e321a9f4 347 /**
kenjiArai 1:9db0e321a9f4 348 * Called by USBDevice layer on each new USB frame.
kenjiArai 1:9db0e321a9f4 349 *
kenjiArai 1:9db0e321a9f4 350 * Callbacks are enabled and disabled by calling sof_enable
kenjiArai 1:9db0e321a9f4 351 * and sof_disable.
kenjiArai 1:9db0e321a9f4 352 *
kenjiArai 1:9db0e321a9f4 353 * @param frame_number The current frame number
kenjiArai 1:9db0e321a9f4 354 *
kenjiArai 1:9db0e321a9f4 355 * Warning: Called in ISR context
kenjiArai 1:9db0e321a9f4 356 */
kenjiArai 1:9db0e321a9f4 357 virtual void callback_sof(int frame_number)
kenjiArai 1:9db0e321a9f4 358 {
kenjiArai 1:9db0e321a9f4 359
kenjiArai 1:9db0e321a9f4 360 }
kenjiArai 1:9db0e321a9f4 361
kenjiArai 1:9db0e321a9f4 362 /**
kenjiArai 1:9db0e321a9f4 363 * Called by USBDevice layer on bus reset.
kenjiArai 1:9db0e321a9f4 364 *
kenjiArai 1:9db0e321a9f4 365 * complete_reset must be called after
kenjiArai 1:9db0e321a9f4 366 * the device is fully reset.
kenjiArai 1:9db0e321a9f4 367 *
kenjiArai 1:9db0e321a9f4 368 * Warning: Called in ISR context
kenjiArai 1:9db0e321a9f4 369 */
kenjiArai 1:9db0e321a9f4 370 virtual void callback_reset()
kenjiArai 1:9db0e321a9f4 371 {
kenjiArai 1:9db0e321a9f4 372
kenjiArai 1:9db0e321a9f4 373 }
kenjiArai 1:9db0e321a9f4 374
kenjiArai 1:9db0e321a9f4 375 /**
kenjiArai 1:9db0e321a9f4 376 * Called when USB changes state
kenjiArai 1:9db0e321a9f4 377 *
kenjiArai 1:9db0e321a9f4 378 * @param new_state The new state of the USBDevice
kenjiArai 1:9db0e321a9f4 379 *
kenjiArai 1:9db0e321a9f4 380 * Warning: Called in ISR context
kenjiArai 1:9db0e321a9f4 381 */
kenjiArai 1:9db0e321a9f4 382 virtual void callback_state_change(DeviceState new_state) = 0;
kenjiArai 1:9db0e321a9f4 383
kenjiArai 1:9db0e321a9f4 384 /**
kenjiArai 1:9db0e321a9f4 385 * Called by USBDevice on Endpoint0 request.
kenjiArai 1:9db0e321a9f4 386 *
kenjiArai 1:9db0e321a9f4 387 * This is used to handle extensions to standard requests
kenjiArai 1:9db0e321a9f4 388 * and class specific requests. The function complete_request
kenjiArai 1:9db0e321a9f4 389 * must be always be called in response to this callback.
kenjiArai 1:9db0e321a9f4 390 *
kenjiArai 1:9db0e321a9f4 391 * Warning: Called in ISR context
kenjiArai 1:9db0e321a9f4 392 */
kenjiArai 1:9db0e321a9f4 393 virtual void callback_request(const setup_packet_t *setup) = 0;
kenjiArai 1:9db0e321a9f4 394
kenjiArai 1:9db0e321a9f4 395 /**
kenjiArai 1:9db0e321a9f4 396 * Called to complete the setup stage of a callback request
kenjiArai 1:9db0e321a9f4 397 *
kenjiArai 1:9db0e321a9f4 398 * Possible options that can be passed as a result are:
kenjiArai 1:9db0e321a9f4 399 * - Receive - Start the data OUT phase of this control transfer
kenjiArai 1:9db0e321a9f4 400 * - Send - Start the data IN phase of this control transfer
kenjiArai 1:9db0e321a9f4 401 * - Success - Operation was a success so start the status phase
kenjiArai 1:9db0e321a9f4 402 * - Failure - Operation failed or is unsupported so send a stall
kenjiArai 1:9db0e321a9f4 403 * - PassThrough - Pass on the request for standard processing
kenjiArai 1:9db0e321a9f4 404 *
kenjiArai 1:9db0e321a9f4 405 * @param result The result of the setup phase.
kenjiArai 1:9db0e321a9f4 406 * @param data Buffer to send or receive if the result is Send or Receive
kenjiArai 1:9db0e321a9f4 407 * @param size Size to transfer if the result is Send or Receive
kenjiArai 1:9db0e321a9f4 408 */
kenjiArai 1:9db0e321a9f4 409 void complete_request(RequestResult result, uint8_t *data = NULL, uint32_t size = 0);
kenjiArai 1:9db0e321a9f4 410
kenjiArai 1:9db0e321a9f4 411 /**
kenjiArai 1:9db0e321a9f4 412 * Called by USBDevice on data stage completion
kenjiArai 1:9db0e321a9f4 413 *
kenjiArai 1:9db0e321a9f4 414 * The function complete_request_xfer_done must be always be called
kenjiArai 1:9db0e321a9f4 415 * in response to this callback.
kenjiArai 1:9db0e321a9f4 416 *
kenjiArai 1:9db0e321a9f4 417 * @param setup Setup packet of the current request
kenjiArai 1:9db0e321a9f4 418 * @param aborted false if the operation was aborted, true otherwise
kenjiArai 1:9db0e321a9f4 419 *
kenjiArai 1:9db0e321a9f4 420 * Warning: Called in ISR context
kenjiArai 1:9db0e321a9f4 421 */
kenjiArai 1:9db0e321a9f4 422 virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted) = 0;
kenjiArai 1:9db0e321a9f4 423
kenjiArai 1:9db0e321a9f4 424 /**
kenjiArai 1:9db0e321a9f4 425 * Called to complete the data stage of a callback request
kenjiArai 1:9db0e321a9f4 426 *
kenjiArai 1:9db0e321a9f4 427 * @param success true if the operation was successful, false otherwise
kenjiArai 1:9db0e321a9f4 428 */
kenjiArai 1:9db0e321a9f4 429 void complete_request_xfer_done(bool success);
kenjiArai 1:9db0e321a9f4 430
kenjiArai 1:9db0e321a9f4 431 /*
kenjiArai 1:9db0e321a9f4 432 * Called by USBDevice layer in response to set_configuration.
kenjiArai 1:9db0e321a9f4 433 *
kenjiArai 1:9db0e321a9f4 434 * Upon reception of this command endpoints of the previous configuration
kenjiArai 1:9db0e321a9f4 435 * if any must be removed with endpoint_remove and new endpoint added with
kenjiArai 1:9db0e321a9f4 436 * endpoint_add.
kenjiArai 1:9db0e321a9f4 437 *
kenjiArai 1:9db0e321a9f4 438 * @param configuration Number of the configuration
kenjiArai 1:9db0e321a9f4 439 *
kenjiArai 1:9db0e321a9f4 440 * Warning: Called in ISR context
kenjiArai 1:9db0e321a9f4 441 */
kenjiArai 1:9db0e321a9f4 442 virtual void callback_set_configuration(uint8_t configuration) = 0;
kenjiArai 1:9db0e321a9f4 443
kenjiArai 1:9db0e321a9f4 444 /**
kenjiArai 1:9db0e321a9f4 445 * Called to complete a set configuration command
kenjiArai 1:9db0e321a9f4 446 *
kenjiArai 1:9db0e321a9f4 447 * @param success true if the configuration was set, false otherwise
kenjiArai 1:9db0e321a9f4 448 */
kenjiArai 1:9db0e321a9f4 449 void complete_set_configuration(bool success);
kenjiArai 1:9db0e321a9f4 450
kenjiArai 1:9db0e321a9f4 451 /*
kenjiArai 1:9db0e321a9f4 452 * Called by USBDevice layer in response to set_interface.
kenjiArai 1:9db0e321a9f4 453 *
kenjiArai 1:9db0e321a9f4 454 * Upon reception of this command endpoints of any previous interface
kenjiArai 1:9db0e321a9f4 455 * if any must be removed with endpoint_remove and new endpoint added with
kenjiArai 1:9db0e321a9f4 456 * endpoint_add.
kenjiArai 1:9db0e321a9f4 457 *
kenjiArai 1:9db0e321a9f4 458 * @param configuration Number of the configuration
kenjiArai 1:9db0e321a9f4 459 *
kenjiArai 1:9db0e321a9f4 460 * Warning: Called in ISR context
kenjiArai 1:9db0e321a9f4 461 */
kenjiArai 1:9db0e321a9f4 462 virtual void callback_set_interface(uint16_t interface, uint8_t alternate) = 0;
kenjiArai 1:9db0e321a9f4 463
kenjiArai 1:9db0e321a9f4 464 /**
kenjiArai 1:9db0e321a9f4 465 * Called to complete a set interface command
kenjiArai 1:9db0e321a9f4 466 *
kenjiArai 1:9db0e321a9f4 467 * @param success true if the interface was set, false otherwise
kenjiArai 1:9db0e321a9f4 468 */
kenjiArai 1:9db0e321a9f4 469 void complete_set_interface(bool success);
kenjiArai 1:9db0e321a9f4 470
kenjiArai 1:9db0e321a9f4 471 /**
kenjiArai 1:9db0e321a9f4 472 * Find a descriptor type inside the configuration descriptor
kenjiArai 1:9db0e321a9f4 473 *
kenjiArai 1:9db0e321a9f4 474 * @param descriptor_type Type of descriptor to find
kenjiArai 1:9db0e321a9f4 475 * @param index Configuration descriptor index ( 0 if only one configuration present )
kenjiArai 1:9db0e321a9f4 476 * @return A descriptor of the given type or NULL if none were found
kenjiArai 1:9db0e321a9f4 477 */
kenjiArai 1:9db0e321a9f4 478 uint8_t *find_descriptor(uint8_t descriptor_type, uint8_t index = 0);
kenjiArai 1:9db0e321a9f4 479
kenjiArai 1:9db0e321a9f4 480 /**
kenjiArai 1:9db0e321a9f4 481 * Get the endpoint table of this device
kenjiArai 1:9db0e321a9f4 482 *
kenjiArai 1:9db0e321a9f4 483 * @return Endpoint table of the USBPhy attached to this USBDevice
kenjiArai 1:9db0e321a9f4 484 */
kenjiArai 1:9db0e321a9f4 485 const usb_ep_table_t *endpoint_table();
kenjiArai 1:9db0e321a9f4 486
kenjiArai 1:9db0e321a9f4 487 /**
kenjiArai 1:9db0e321a9f4 488 * Callback called to indicate the USB processing needs to be done
kenjiArai 1:9db0e321a9f4 489 */
kenjiArai 1:9db0e321a9f4 490 virtual void start_process();
kenjiArai 1:9db0e321a9f4 491
kenjiArai 1:9db0e321a9f4 492 /**
kenjiArai 1:9db0e321a9f4 493 * Acquire exclusive access to this instance USBDevice
kenjiArai 1:9db0e321a9f4 494 */
kenjiArai 1:9db0e321a9f4 495 virtual void lock();
kenjiArai 1:9db0e321a9f4 496
kenjiArai 1:9db0e321a9f4 497 /**
kenjiArai 1:9db0e321a9f4 498 * Release exclusive access to this instance USBDevice
kenjiArai 1:9db0e321a9f4 499 */
kenjiArai 1:9db0e321a9f4 500 virtual void unlock();
kenjiArai 1:9db0e321a9f4 501
kenjiArai 1:9db0e321a9f4 502 /**
kenjiArai 1:9db0e321a9f4 503 * Assert that the current thread of execution holds the lock
kenjiArai 1:9db0e321a9f4 504 *
kenjiArai 1:9db0e321a9f4 505 */
kenjiArai 1:9db0e321a9f4 506 virtual void assert_locked();
kenjiArai 1:9db0e321a9f4 507
kenjiArai 1:9db0e321a9f4 508 uint16_t vendor_id;
kenjiArai 1:9db0e321a9f4 509 uint16_t product_id;
kenjiArai 1:9db0e321a9f4 510 uint16_t product_release;
kenjiArai 1:9db0e321a9f4 511 uint8_t device_descriptor[18];
kenjiArai 1:9db0e321a9f4 512
kenjiArai 1:9db0e321a9f4 513 private:
kenjiArai 1:9db0e321a9f4 514 // USBPhyEvents
kenjiArai 1:9db0e321a9f4 515 virtual void power(bool powered);
kenjiArai 1:9db0e321a9f4 516 virtual void suspend(bool suspended);
kenjiArai 1:9db0e321a9f4 517 virtual void sof(int frame_number);
kenjiArai 1:9db0e321a9f4 518 virtual void reset();
kenjiArai 1:9db0e321a9f4 519 virtual void ep0_setup();
kenjiArai 1:9db0e321a9f4 520 virtual void ep0_out();
kenjiArai 1:9db0e321a9f4 521 virtual void ep0_in();
kenjiArai 1:9db0e321a9f4 522 virtual void out(usb_ep_t endpoint);
kenjiArai 1:9db0e321a9f4 523 virtual void in(usb_ep_t endpoint);
kenjiArai 1:9db0e321a9f4 524
kenjiArai 1:9db0e321a9f4 525 bool _request_get_descriptor();
kenjiArai 1:9db0e321a9f4 526 bool _control_out();
kenjiArai 1:9db0e321a9f4 527 bool _control_in();
kenjiArai 1:9db0e321a9f4 528 bool _request_set_address();
kenjiArai 1:9db0e321a9f4 529 bool _request_set_configuration();
kenjiArai 1:9db0e321a9f4 530 bool _request_set_feature();
kenjiArai 1:9db0e321a9f4 531 bool _request_clear_feature();
kenjiArai 1:9db0e321a9f4 532 bool _request_get_status();
kenjiArai 1:9db0e321a9f4 533 bool _request_setup();
kenjiArai 1:9db0e321a9f4 534 void _control_setup();
kenjiArai 1:9db0e321a9f4 535 void _control_abort();
kenjiArai 1:9db0e321a9f4 536 void _control_abort_start();
kenjiArai 1:9db0e321a9f4 537 void _control_setup_continue();
kenjiArai 1:9db0e321a9f4 538 void _decode_setup_packet(uint8_t *data, setup_packet_t *packet);
kenjiArai 1:9db0e321a9f4 539 bool _request_get_configuration();
kenjiArai 1:9db0e321a9f4 540 bool _request_get_interface();
kenjiArai 1:9db0e321a9f4 541 bool _request_set_interface();
kenjiArai 1:9db0e321a9f4 542 void _change_state(DeviceState state);
kenjiArai 1:9db0e321a9f4 543 void _run_later(void (USBDevice::*function)());
kenjiArai 1:9db0e321a9f4 544
kenjiArai 1:9db0e321a9f4 545 void _complete_request();
kenjiArai 1:9db0e321a9f4 546 void _complete_request_xfer_done();
kenjiArai 1:9db0e321a9f4 547 void _complete_set_configuration();
kenjiArai 1:9db0e321a9f4 548 void _complete_set_interface();
kenjiArai 1:9db0e321a9f4 549
kenjiArai 1:9db0e321a9f4 550 struct endpoint_info_t {
kenjiArai 1:9db0e321a9f4 551 mbed::Callback<void()> callback;
kenjiArai 1:9db0e321a9f4 552 uint16_t max_packet_size;
kenjiArai 1:9db0e321a9f4 553 uint16_t transfer_size;
kenjiArai 1:9db0e321a9f4 554 uint8_t flags;
kenjiArai 1:9db0e321a9f4 555 uint8_t pending;
kenjiArai 1:9db0e321a9f4 556 };
kenjiArai 1:9db0e321a9f4 557
kenjiArai 1:9db0e321a9f4 558 struct usb_device_t {
kenjiArai 1:9db0e321a9f4 559 volatile DeviceState state;
kenjiArai 1:9db0e321a9f4 560 uint8_t configuration;
kenjiArai 1:9db0e321a9f4 561 bool suspended;
kenjiArai 1:9db0e321a9f4 562 };
kenjiArai 1:9db0e321a9f4 563
kenjiArai 1:9db0e321a9f4 564 enum ControlState {
kenjiArai 1:9db0e321a9f4 565 Setup,
kenjiArai 1:9db0e321a9f4 566 DataOut,
kenjiArai 1:9db0e321a9f4 567 DataIn,
kenjiArai 1:9db0e321a9f4 568 Status
kenjiArai 1:9db0e321a9f4 569 };
kenjiArai 1:9db0e321a9f4 570
kenjiArai 1:9db0e321a9f4 571 enum UserCallback {
kenjiArai 1:9db0e321a9f4 572 None,
kenjiArai 1:9db0e321a9f4 573 Request,
kenjiArai 1:9db0e321a9f4 574 RequestXferDone,
kenjiArai 1:9db0e321a9f4 575 SetConfiguration,
kenjiArai 1:9db0e321a9f4 576 SetInterface
kenjiArai 1:9db0e321a9f4 577 };
kenjiArai 1:9db0e321a9f4 578
kenjiArai 1:9db0e321a9f4 579 struct complete_request_t {
kenjiArai 1:9db0e321a9f4 580 RequestResult result;
kenjiArai 1:9db0e321a9f4 581 uint8_t *data;
kenjiArai 1:9db0e321a9f4 582 uint32_t size;
kenjiArai 1:9db0e321a9f4 583 };
kenjiArai 1:9db0e321a9f4 584
kenjiArai 1:9db0e321a9f4 585 union complete_args_t {
kenjiArai 1:9db0e321a9f4 586 complete_request_t request;
kenjiArai 1:9db0e321a9f4 587 bool status;
kenjiArai 1:9db0e321a9f4 588 };
kenjiArai 1:9db0e321a9f4 589
kenjiArai 1:9db0e321a9f4 590 struct control_transfer_t {
kenjiArai 1:9db0e321a9f4 591 setup_packet_t setup;
kenjiArai 1:9db0e321a9f4 592 uint8_t *ptr;
kenjiArai 1:9db0e321a9f4 593 uint32_t remaining;
kenjiArai 1:9db0e321a9f4 594 uint8_t direction;
kenjiArai 1:9db0e321a9f4 595 bool zlp;
kenjiArai 1:9db0e321a9f4 596 bool notify;
kenjiArai 1:9db0e321a9f4 597 ControlState stage;
kenjiArai 1:9db0e321a9f4 598 UserCallback user_callback;
kenjiArai 1:9db0e321a9f4 599 complete_args_t args;
kenjiArai 1:9db0e321a9f4 600 };
kenjiArai 1:9db0e321a9f4 601
kenjiArai 1:9db0e321a9f4 602 endpoint_info_t _endpoint_info[32 - 2];
kenjiArai 1:9db0e321a9f4 603
kenjiArai 1:9db0e321a9f4 604 USBPhy *_phy;
kenjiArai 1:9db0e321a9f4 605 bool _initialized;
kenjiArai 1:9db0e321a9f4 606 bool _connected;
kenjiArai 1:9db0e321a9f4 607 bool _endpoint_add_remove_allowed;
kenjiArai 1:9db0e321a9f4 608 control_transfer_t _transfer;
kenjiArai 1:9db0e321a9f4 609 usb_device_t _device;
kenjiArai 1:9db0e321a9f4 610 uint32_t _max_packet_size_ep0;
kenjiArai 1:9db0e321a9f4 611 void (USBDevice::*_post_process)();
kenjiArai 1:9db0e321a9f4 612
kenjiArai 1:9db0e321a9f4 613 bool _setup_ready;
kenjiArai 1:9db0e321a9f4 614 bool _abort_control;
kenjiArai 1:9db0e321a9f4 615
kenjiArai 1:9db0e321a9f4 616 uint16_t _current_interface;
kenjiArai 1:9db0e321a9f4 617 uint8_t _current_alternate;
kenjiArai 1:9db0e321a9f4 618 uint32_t _locked;
kenjiArai 1:9db0e321a9f4 619 };
kenjiArai 1:9db0e321a9f4 620
kenjiArai 1:9db0e321a9f4 621 /** @}*/
kenjiArai 1:9db0e321a9f4 622
kenjiArai 1:9db0e321a9f4 623 #endif