Naoya Hasegawa / USBDevice

Fork of USBDevice by Samuel Mokrani

Committer:
hase
Date:
Wed Nov 07 09:31:53 2012 +0000
Revision:
2:9fdd29bb0d95
Parent:
1:f6a91be12476
test NGprogram

Who changed what in which revision?

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