partly working USB Device lib for STM32F746NG Discovery both Interface are working

Dependents:   DISCO-F746NG-USB_Device McLighTT project_Keyboard_to_the_Keyboard MIDIInstrumentPADProject ... more

Committer:
DieterGraef
Date:
Sun Jul 31 17:47:35 2016 +0000
Revision:
0:0a2eaa300982
partly working USB Device library - serial and MIDI is working

Who changed what in which revision?

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