USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Wed Nov 16 17:17:42 2011 +0000
Revision:
11:a26e7b7a1221
GOOD COMMIT: msd and hid work even on MAC...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 11:a26e7b7a1221 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 11:a26e7b7a1221 2 *
samux 11:a26e7b7a1221 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 11:a26e7b7a1221 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 11:a26e7b7a1221 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 11:a26e7b7a1221 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 11:a26e7b7a1221 7 * Software is furnished to do so, subject to the following conditions:
samux 11:a26e7b7a1221 8 *
samux 11:a26e7b7a1221 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 11:a26e7b7a1221 10 * substantial portions of the Software.
samux 11:a26e7b7a1221 11 *
samux 11:a26e7b7a1221 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 11:a26e7b7a1221 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 11:a26e7b7a1221 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 11:a26e7b7a1221 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 11:a26e7b7a1221 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 11:a26e7b7a1221 17 */
samux 11:a26e7b7a1221 18
samux 11:a26e7b7a1221 19 #include "stdint.h"
samux 11:a26e7b7a1221 20 #include "USBSerial.h"
samux 11:a26e7b7a1221 21 #include "USBBusInterface.h"
samux 11:a26e7b7a1221 22
samux 11:a26e7b7a1221 23
samux 11:a26e7b7a1221 24 int USBSerial::_putc(int c) {
samux 11:a26e7b7a1221 25 send(EPBULK_IN, (uint8_t *)&c, 1);
samux 11:a26e7b7a1221 26 return 1;
samux 11:a26e7b7a1221 27 }
samux 11:a26e7b7a1221 28
samux 11:a26e7b7a1221 29 int USBSerial::_getc() {
samux 11:a26e7b7a1221 30 uint8_t c;
samux 11:a26e7b7a1221 31 while (buf.isEmpty());
samux 11:a26e7b7a1221 32 buf.dequeue(&c);
samux 11:a26e7b7a1221 33 return c;
samux 11:a26e7b7a1221 34 }
samux 11:a26e7b7a1221 35
samux 11:a26e7b7a1221 36 void USBSerial::attach(void (*ptr)(void)) {
samux 11:a26e7b7a1221 37 handler = true;
samux 11:a26e7b7a1221 38 proc = true;
samux 11:a26e7b7a1221 39 fn = ptr;
samux 11:a26e7b7a1221 40 }
samux 11:a26e7b7a1221 41
samux 11:a26e7b7a1221 42
samux 11:a26e7b7a1221 43 bool USBSerial::EP2_OUT_callback() {
samux 11:a26e7b7a1221 44 uint8_t c[65];
samux 11:a26e7b7a1221 45 uint16_t size = 0;
samux 11:a26e7b7a1221 46
samux 11:a26e7b7a1221 47 //we read the packet received and put it on the circular buffer
samux 11:a26e7b7a1221 48 USBCDC::read(EPBULK_OUT, c, &size, MAX_PACKET_SIZE_EPBULK);
samux 11:a26e7b7a1221 49 for (int i = 0; i < size; i++) {
samux 11:a26e7b7a1221 50 buf.queue(c[i]);
samux 11:a26e7b7a1221 51 }
samux 11:a26e7b7a1221 52
samux 11:a26e7b7a1221 53 //call a potential handler
samux 11:a26e7b7a1221 54 if (handler) {
samux 11:a26e7b7a1221 55 if (proc) {
samux 11:a26e7b7a1221 56 (*fn)();
samux 11:a26e7b7a1221 57 } else {
samux 11:a26e7b7a1221 58 rx.call();
samux 11:a26e7b7a1221 59 }
samux 11:a26e7b7a1221 60 }
samux 11:a26e7b7a1221 61
samux 11:a26e7b7a1221 62 // We reactivate the endpoint to receive next characters
samux 11:a26e7b7a1221 63 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
samux 11:a26e7b7a1221 64 return true;
samux 11:a26e7b7a1221 65 }
samux 11:a26e7b7a1221 66
samux 11:a26e7b7a1221 67 uint8_t USBSerial::available() {
samux 11:a26e7b7a1221 68 return buf.available();
samux 11:a26e7b7a1221 69 }