,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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