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