USBDevice for STM support

Dependents:   Nucleo_Usb_JoyMouse Nucleo_usbmouse ELEC350_1-referral-2018-usb-hid USBJoystick_HelloWorld2_wip ... more

This library contains all mbed usb device library (mbed-os\features\unsupported\USBDevice).

Committer:
frq08711@LMECWL0871.LME.ST.COM
Date:
Tue Mar 28 11:00:57 2017 +0200
Branch:
master
Revision:
4:50ec00aa4515
Parent:
1:2a3ae13b45ef
update for 5.4.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 2 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 4 * and associated documentation files (the "Software"), to deal in the Software without
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 7 * Software is furnished to do so, subject to the following conditions:
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 8 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 9 * The above copyright notice and this permission notice shall be included in all copies or
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 10 * substantial portions of the Software.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 11 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 17 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 18
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 19 #ifndef USBSERIAL_H
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 20 #define USBSERIAL_H
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 21
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 22 #include "USBCDC.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 23 #include "Stream.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 24 #include "CircBuffer.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 25 #include "Callback.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 26
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 27 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 28 * USBSerial example
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 29 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 30 * @code
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 31 * #include "mbed.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 32 * #include "USBSerial.h"
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 33 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 34 * //Virtual serial port over USB
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 35 * USBSerial serial;
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 36 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 37 * int main(void) {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 38 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 39 * while(1)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 40 * {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 41 * serial.printf("I am a virtual serial port\n");
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 42 * wait(1);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 43 * }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 44 * }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 45 * @endcode
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 46 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 47 class USBSerial: public USBCDC, public Stream {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 48 public:
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 49
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 50 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 51 * Constructor
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 52 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 53 * @param vendor_id Your vendor_id (default: 0x1f00)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 54 * @param product_id Your product_id (default: 0x2012)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 55 * @param product_release Your preoduct_release (default: 0x0001)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 56 * @param connect_blocking define if the connection must be blocked if USB not plugged in
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 57 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 58 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 59 USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001, bool connect_blocking = true): USBCDC(vendor_id, product_id, product_release, connect_blocking){
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 60 settingsChangedCallback = 0;
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 61 };
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 62
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 63
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 64 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 65 * Send a character. You can use puts, printf.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 66 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 67 * @param c character to be sent
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 68 * @returns true if there is no error, false otherwise
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 69 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 70 virtual int _putc(int c);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 71
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 72 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 73 * Read a character: blocking
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 74 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 75 * @returns character read
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 76 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 77 virtual int _getc();
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 78
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 79 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 80 * Check the number of bytes available.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 81 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 82 * @returns the number of bytes available
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 83 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 84 uint8_t available();
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 85
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 86 /** Determine if there is a character available to read
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 87 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 88 * @returns
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 89 * 1 if there is a character available to read,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 90 * 0 otherwise
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 91 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 92 int readable() { return available() ? 1 : 0; }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 93
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 94 /** Determine if there is space available to write a character
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 95 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 96 * @returns
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 97 * 1 if there is space to write a character,
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 98 * 0 otherwise
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 99 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 100 int writeable() { return 1; } // always return 1, for write operation is blocking
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 101
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 102 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 103 * Write a block of data.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 104 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 105 * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 106 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 107 * @param buf pointer on data which will be written
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 108 * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes)
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 109 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 110 * @returns true if successfull
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 111 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 112 bool writeBlock(uint8_t * buf, uint16_t size);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 113
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 114 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 115 * Attach a member function to call when a packet is received.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 116 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 117 * @param tptr pointer to the object to call the member function on
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 118 * @param mptr pointer to the member function to be called
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 119 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 120 template<typename T>
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 121 void attach(T* tptr, void (T::*mptr)(void)) {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 122 if((mptr != NULL) && (tptr != NULL)) {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 123 rx.attach(tptr, mptr);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 124 }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 125 }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 126
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 127 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 128 * Attach a callback called when a packet is received
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 129 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 130 * @param fptr function pointer
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 131 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 132 void attach(void (*fptr)(void)) {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 133 if(fptr != NULL) {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 134 rx.attach(fptr);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 135 }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 136 }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 137
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 138 /**
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 139 * Attach a callback to call when serial's settings are changed.
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 140 *
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 141 * @param fptr function pointer
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 142 */
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 143 void attach(void (*fptr)(int baud, int bits, int parity, int stop)) {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 144 settingsChangedCallback = fptr;
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 145 }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 146
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 147 protected:
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 148 virtual bool EPBULK_OUT_callback();
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 149 virtual void lineCodingChanged(int baud, int bits, int parity, int stop){
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 150 if (settingsChangedCallback) {
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 151 settingsChangedCallback(baud, bits, parity, stop);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 152 }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 153 }
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 154
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 155 private:
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 156 Callback<void()> rx;
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 157 CircBuffer<uint8_t,128> buf;
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 158 void (*settingsChangedCallback)(int baud, int bits, int parity, int stop);
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 159 };
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 160
frq08711@LMECWL0871.LME.ST.COM 1:2a3ae13b45ef 161 #endif