USBDevice stack

Dependents:   erg USBSerial_HelloWorld Slingshot SuperScanner ... more

Committer:
samux
Date:
Wed May 30 18:28:11 2012 +0000
Revision:
0:140cdf8e2d60

        

Who changed what in which revision?

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