Support Isochronous transfer additionally

Dependents:   USBHostC270_example_GR-PEACH USBHostDac_example USBHostDac_Audio_in_out

Fork of USBHost_custom by Renesas

Committer:
samux
Date:
Tue Mar 12 17:23:37 2013 +0000
Revision:
4:b320d68e98e7
Parent:
0:a554658735bf
Child:
5:e48791a1ef18
bug fixes - support composite device

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:a554658735bf 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
mbed_official 0:a554658735bf 2 *
mbed_official 0:a554658735bf 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mbed_official 0:a554658735bf 4 * and associated documentation files (the "Software"), to deal in the Software without
mbed_official 0:a554658735bf 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mbed_official 0:a554658735bf 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mbed_official 0:a554658735bf 7 * Software is furnished to do so, subject to the following conditions:
mbed_official 0:a554658735bf 8 *
mbed_official 0:a554658735bf 9 * The above copyright notice and this permission notice shall be included in all copies or
mbed_official 0:a554658735bf 10 * substantial portions of the Software.
mbed_official 0:a554658735bf 11 *
mbed_official 0:a554658735bf 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mbed_official 0:a554658735bf 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mbed_official 0:a554658735bf 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mbed_official 0:a554658735bf 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_official 0:a554658735bf 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mbed_official 0:a554658735bf 17 */
mbed_official 0:a554658735bf 18
mbed_official 0:a554658735bf 19 #include "USBHostSerial.h"
mbed_official 0:a554658735bf 20
mbed_official 0:a554658735bf 21 #if USBHOST_SERIAL
mbed_official 0:a554658735bf 22
mbed_official 0:a554658735bf 23 #include "dbg.h"
mbed_official 0:a554658735bf 24
samux 4:b320d68e98e7 25 #define SET_LINE_CODING 0x20
samux 4:b320d68e98e7 26
mbed_official 0:a554658735bf 27 USBHostSerial::USBHostSerial(): circ_buf() {
mbed_official 0:a554658735bf 28 host = USBHost::getHostInst();
mbed_official 0:a554658735bf 29 size_bulk_in = 0;
mbed_official 0:a554658735bf 30 size_bulk_out = 0;
mbed_official 0:a554658735bf 31 init();
mbed_official 0:a554658735bf 32 }
mbed_official 0:a554658735bf 33
mbed_official 0:a554658735bf 34 void USBHostSerial::init() {
mbed_official 0:a554658735bf 35 dev = NULL;
mbed_official 0:a554658735bf 36 bulk_in = NULL;
mbed_official 0:a554658735bf 37 bulk_out = NULL;
mbed_official 0:a554658735bf 38 dev_connected = false;
mbed_official 0:a554658735bf 39 serial_intf = -1;
mbed_official 0:a554658735bf 40 serial_device_found = false;
samux 4:b320d68e98e7 41 memcpy(&line_coding, 0, sizeof(LINE_CODING));
samux 4:b320d68e98e7 42 circ_buf.flush();
mbed_official 0:a554658735bf 43 }
mbed_official 0:a554658735bf 44
mbed_official 0:a554658735bf 45 bool USBHostSerial::connected()
mbed_official 0:a554658735bf 46 {
mbed_official 0:a554658735bf 47 return dev_connected;
mbed_official 0:a554658735bf 48 }
mbed_official 0:a554658735bf 49
mbed_official 0:a554658735bf 50 bool USBHostSerial::connect() {
mbed_official 0:a554658735bf 51
mbed_official 0:a554658735bf 52 if (dev_connected) {
mbed_official 0:a554658735bf 53 return true;
mbed_official 0:a554658735bf 54 }
mbed_official 0:a554658735bf 55 for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
mbed_official 0:a554658735bf 56 if ((dev = host->getDevice(i)) != NULL) {
samux 4:b320d68e98e7 57
samux 4:b320d68e98e7 58 USB_DBG("Trying to connect serial device\r\n");
mbed_official 0:a554658735bf 59
mbed_official 0:a554658735bf 60 if(host->enumerate(dev, this))
mbed_official 0:a554658735bf 61 break;
mbed_official 0:a554658735bf 62
mbed_official 0:a554658735bf 63 if (serial_device_found) {
mbed_official 0:a554658735bf 64 bulk_in = dev->getEndpoint(serial_intf, BULK_ENDPOINT, IN);
mbed_official 0:a554658735bf 65 bulk_out = dev->getEndpoint(serial_intf, BULK_ENDPOINT, OUT);
mbed_official 0:a554658735bf 66
mbed_official 0:a554658735bf 67 if (!bulk_in || !bulk_out)
mbed_official 0:a554658735bf 68 break;
mbed_official 0:a554658735bf 69
samux 4:b320d68e98e7 70 USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, serial_intf);
samux 4:b320d68e98e7 71 dev->setName("Serial", serial_intf);
mbed_official 0:a554658735bf 72 host->registerDriver(dev, serial_intf, this, &USBHostSerial::init);
mbed_official 0:a554658735bf 73
samux 4:b320d68e98e7 74 baud(9600);
samux 4:b320d68e98e7 75
mbed_official 0:a554658735bf 76 size_bulk_in = bulk_in->getSize();
mbed_official 0:a554658735bf 77 size_bulk_out = bulk_out->getSize();
mbed_official 0:a554658735bf 78
mbed_official 0:a554658735bf 79 bulk_in->attach(this, &USBHostSerial::rxHandler);
mbed_official 0:a554658735bf 80 bulk_out->attach(this, &USBHostSerial::txHandler);
mbed_official 0:a554658735bf 81
mbed_official 0:a554658735bf 82 host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
mbed_official 0:a554658735bf 83 dev_connected = true;
mbed_official 0:a554658735bf 84 return true;
mbed_official 0:a554658735bf 85 }
mbed_official 0:a554658735bf 86 }
mbed_official 0:a554658735bf 87 }
mbed_official 0:a554658735bf 88 init();
mbed_official 0:a554658735bf 89 return false;
mbed_official 0:a554658735bf 90 }
mbed_official 0:a554658735bf 91
mbed_official 0:a554658735bf 92 void USBHostSerial::rxHandler() {
mbed_official 0:a554658735bf 93 if (bulk_in) {
mbed_official 0:a554658735bf 94 int len = bulk_in->getLengthTransferred();
mbed_official 0:a554658735bf 95 if (bulk_in->getState() == USB_TYPE_IDLE) {
mbed_official 0:a554658735bf 96 for (int i = 0; i < len; i++) {
mbed_official 0:a554658735bf 97 circ_buf.queue(buf[i]);
mbed_official 0:a554658735bf 98 }
mbed_official 0:a554658735bf 99 rx.call();
samux 4:b320d68e98e7 100 host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
mbed_official 0:a554658735bf 101 }
mbed_official 0:a554658735bf 102 }
mbed_official 0:a554658735bf 103 }
mbed_official 0:a554658735bf 104
mbed_official 0:a554658735bf 105 void USBHostSerial::txHandler() {
mbed_official 0:a554658735bf 106 if (bulk_out) {
mbed_official 0:a554658735bf 107 if (bulk_out->getState() == USB_TYPE_IDLE) {
mbed_official 0:a554658735bf 108 tx.call();
mbed_official 0:a554658735bf 109 }
mbed_official 0:a554658735bf 110 }
mbed_official 0:a554658735bf 111 }
mbed_official 0:a554658735bf 112
mbed_official 0:a554658735bf 113 int USBHostSerial::_putc(int c) {
mbed_official 0:a554658735bf 114 if (bulk_out) {
mbed_official 0:a554658735bf 115 if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) {
mbed_official 0:a554658735bf 116 return 1;
mbed_official 0:a554658735bf 117 }
mbed_official 0:a554658735bf 118 }
mbed_official 0:a554658735bf 119 return -1;
mbed_official 0:a554658735bf 120 }
mbed_official 0:a554658735bf 121
samux 4:b320d68e98e7 122 void USBHostSerial::baud(int baudrate) {
samux 4:b320d68e98e7 123 line_coding.baudrate = baudrate;
samux 4:b320d68e98e7 124 format();
samux 4:b320d68e98e7 125 }
samux 4:b320d68e98e7 126
samux 4:b320d68e98e7 127 void USBHostSerial::format(int bits, Parity parity, int stop_bits) {
samux 4:b320d68e98e7 128 line_coding.data_bits = bits;
samux 4:b320d68e98e7 129 line_coding.parity = parity;
samux 4:b320d68e98e7 130 line_coding.stop_bits = (stop_bits == 1) ? 0 : 2;
samux 4:b320d68e98e7 131
samux 4:b320d68e98e7 132 // set line coding
samux 4:b320d68e98e7 133 int res = host->controlWrite( dev,
samux 4:b320d68e98e7 134 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
samux 4:b320d68e98e7 135 SET_LINE_CODING,
samux 4:b320d68e98e7 136 0, serial_intf, (uint8_t *)&line_coding, 7);
samux 4:b320d68e98e7 137 }
mbed_official 0:a554658735bf 138
mbed_official 0:a554658735bf 139 int USBHostSerial::_getc() {
mbed_official 0:a554658735bf 140 uint8_t c = 0;
mbed_official 0:a554658735bf 141 if (bulk_in == NULL) {
samux 4:b320d68e98e7 142 init();
mbed_official 0:a554658735bf 143 return -1;
mbed_official 0:a554658735bf 144 }
samux 4:b320d68e98e7 145 while (circ_buf.isEmpty());
mbed_official 0:a554658735bf 146 circ_buf.dequeue(&c);
mbed_official 0:a554658735bf 147 return c;
mbed_official 0:a554658735bf 148 }
mbed_official 0:a554658735bf 149
mbed_official 0:a554658735bf 150
mbed_official 0:a554658735bf 151 uint8_t USBHostSerial::available() {
mbed_official 0:a554658735bf 152 return circ_buf.available();
mbed_official 0:a554658735bf 153 }
mbed_official 0:a554658735bf 154
mbed_official 0:a554658735bf 155 /*virtual*/ void USBHostSerial::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 0:a554658735bf 156 {
mbed_official 0:a554658735bf 157 // we don't check VID/PID for MSD driver
mbed_official 0:a554658735bf 158 }
mbed_official 0:a554658735bf 159
mbed_official 0:a554658735bf 160 /*virtual*/ bool USBHostSerial::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
mbed_official 0:a554658735bf 161 {
mbed_official 0:a554658735bf 162 if ((serial_intf == -1) &&
mbed_official 0:a554658735bf 163 (intf_class == SERIAL_CLASS) &&
mbed_official 0:a554658735bf 164 (intf_subclass == 0x00) &&
mbed_official 0:a554658735bf 165 (intf_protocol == 0x00)) {
mbed_official 0:a554658735bf 166 serial_intf = intf_nb;
mbed_official 0:a554658735bf 167 return true;
mbed_official 0:a554658735bf 168 }
mbed_official 0:a554658735bf 169 return false;
mbed_official 0:a554658735bf 170 }
mbed_official 0:a554658735bf 171
mbed_official 0:a554658735bf 172 /*virtual*/ bool USBHostSerial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
mbed_official 0:a554658735bf 173 {
mbed_official 0:a554658735bf 174 if (intf_nb == serial_intf) {
mbed_official 0:a554658735bf 175 if (type == BULK_ENDPOINT) {
mbed_official 0:a554658735bf 176 serial_device_found = true;
mbed_official 0:a554658735bf 177 return true;
mbed_official 0:a554658735bf 178 }
mbed_official 0:a554658735bf 179 }
mbed_official 0:a554658735bf 180 return false;
mbed_official 0:a554658735bf 181 }
mbed_official 0:a554658735bf 182
mbed_official 0:a554658735bf 183 #endif