USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Dec 11 15:22:50 2011 +0000
Revision:
18:08b207d10056
Parent:
14:757226626acb
all is working: rename...

Who changed what in which revision?

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