update

Dependencies:   FATFileSystem mbed-rtos

Fork of USBHost by mbed official

Committer:
samux
Date:
Thu Mar 14 11:49:53 2013 +0000
Revision:
8:93da8ea2708b
Parent:
5:e48791a1ef18
Child:
13:b58a2204422f
update doc + license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 8:93da8ea2708b 1 /* mbed USBHost Library
samux 8:93da8ea2708b 2 * Copyright (c) 2006-2013 ARM Limited
samux 8:93da8ea2708b 3 *
samux 8:93da8ea2708b 4 * Licensed under the Apache License, Version 2.0 (the "License");
samux 8:93da8ea2708b 5 * you may not use this file except in compliance with the License.
samux 8:93da8ea2708b 6 * You may obtain a copy of the License at
samux 8:93da8ea2708b 7 *
samux 8:93da8ea2708b 8 * http://www.apache.org/licenses/LICENSE-2.0
samux 8:93da8ea2708b 9 *
samux 8:93da8ea2708b 10 * Unless required by applicable law or agreed to in writing, software
samux 8:93da8ea2708b 11 * distributed under the License is distributed on an "AS IS" BASIS,
samux 8:93da8ea2708b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
samux 8:93da8ea2708b 13 * See the License for the specific language governing permissions and
samux 8:93da8ea2708b 14 * limitations under the License.
samux 8:93da8ea2708b 15 */
mbed_official 0:a554658735bf 16
mbed_official 0:a554658735bf 17 #include "USBHostSerial.h"
mbed_official 0:a554658735bf 18
mbed_official 0:a554658735bf 19 #if USBHOST_SERIAL
mbed_official 0:a554658735bf 20
mbed_official 0:a554658735bf 21 #include "dbg.h"
mbed_official 0:a554658735bf 22
samux 4:b320d68e98e7 23 #define SET_LINE_CODING 0x20
samux 4:b320d68e98e7 24
mbed_official 0:a554658735bf 25 USBHostSerial::USBHostSerial(): circ_buf() {
mbed_official 0:a554658735bf 26 host = USBHost::getHostInst();
mbed_official 0:a554658735bf 27 size_bulk_in = 0;
mbed_official 0:a554658735bf 28 size_bulk_out = 0;
mbed_official 0:a554658735bf 29 init();
mbed_official 0:a554658735bf 30 }
mbed_official 0:a554658735bf 31
mbed_official 0:a554658735bf 32 void USBHostSerial::init() {
mbed_official 0:a554658735bf 33 dev = NULL;
mbed_official 0:a554658735bf 34 bulk_in = NULL;
mbed_official 0:a554658735bf 35 bulk_out = NULL;
mbed_official 0:a554658735bf 36 dev_connected = false;
mbed_official 0:a554658735bf 37 serial_intf = -1;
mbed_official 0:a554658735bf 38 serial_device_found = false;
samux 8:93da8ea2708b 39 line_coding.baudrate = 9600;
samux 8:93da8ea2708b 40 line_coding.data_bits = 8;
samux 8:93da8ea2708b 41 line_coding.parity = None;
samux 8:93da8ea2708b 42 line_coding.stop_bits = 1;
samux 4:b320d68e98e7 43 circ_buf.flush();
mbed_official 0:a554658735bf 44 }
mbed_official 0:a554658735bf 45
mbed_official 0:a554658735bf 46 bool USBHostSerial::connected()
mbed_official 0:a554658735bf 47 {
mbed_official 0:a554658735bf 48 return dev_connected;
mbed_official 0:a554658735bf 49 }
mbed_official 0:a554658735bf 50
mbed_official 0:a554658735bf 51 bool USBHostSerial::connect() {
mbed_official 0:a554658735bf 52
mbed_official 0:a554658735bf 53 if (dev_connected) {
mbed_official 0:a554658735bf 54 return true;
mbed_official 0:a554658735bf 55 }
samux 5:e48791a1ef18 56 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
mbed_official 0:a554658735bf 57 if ((dev = host->getDevice(i)) != NULL) {
samux 4:b320d68e98e7 58
samux 4:b320d68e98e7 59 USB_DBG("Trying to connect serial device\r\n");
mbed_official 0:a554658735bf 60
mbed_official 0:a554658735bf 61 if(host->enumerate(dev, this))
mbed_official 0:a554658735bf 62 break;
mbed_official 0:a554658735bf 63
mbed_official 0:a554658735bf 64 if (serial_device_found) {
mbed_official 0:a554658735bf 65 bulk_in = dev->getEndpoint(serial_intf, BULK_ENDPOINT, IN);
mbed_official 0:a554658735bf 66 bulk_out = dev->getEndpoint(serial_intf, BULK_ENDPOINT, OUT);
mbed_official 0:a554658735bf 67
mbed_official 0:a554658735bf 68 if (!bulk_in || !bulk_out)
mbed_official 0:a554658735bf 69 break;
mbed_official 0:a554658735bf 70
samux 4:b320d68e98e7 71 USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, serial_intf);
samux 4:b320d68e98e7 72 dev->setName("Serial", serial_intf);
mbed_official 0:a554658735bf 73 host->registerDriver(dev, serial_intf, this, &USBHostSerial::init);
mbed_official 0:a554658735bf 74
samux 4:b320d68e98e7 75 baud(9600);
samux 4:b320d68e98e7 76
mbed_official 0:a554658735bf 77 size_bulk_in = bulk_in->getSize();
mbed_official 0:a554658735bf 78 size_bulk_out = bulk_out->getSize();
mbed_official 0:a554658735bf 79
mbed_official 0:a554658735bf 80 bulk_in->attach(this, &USBHostSerial::rxHandler);
mbed_official 0:a554658735bf 81 bulk_out->attach(this, &USBHostSerial::txHandler);
mbed_official 0:a554658735bf 82
mbed_official 0:a554658735bf 83 host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
mbed_official 0:a554658735bf 84 dev_connected = true;
mbed_official 0:a554658735bf 85 return true;
mbed_official 0:a554658735bf 86 }
mbed_official 0:a554658735bf 87 }
mbed_official 0:a554658735bf 88 }
mbed_official 0:a554658735bf 89 init();
mbed_official 0:a554658735bf 90 return false;
mbed_official 0:a554658735bf 91 }
mbed_official 0:a554658735bf 92
mbed_official 0:a554658735bf 93 void USBHostSerial::rxHandler() {
mbed_official 0:a554658735bf 94 if (bulk_in) {
mbed_official 0:a554658735bf 95 int len = bulk_in->getLengthTransferred();
mbed_official 0:a554658735bf 96 if (bulk_in->getState() == USB_TYPE_IDLE) {
mbed_official 0:a554658735bf 97 for (int i = 0; i < len; i++) {
mbed_official 0:a554658735bf 98 circ_buf.queue(buf[i]);
mbed_official 0:a554658735bf 99 }
mbed_official 0:a554658735bf 100 rx.call();
samux 4:b320d68e98e7 101 host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
mbed_official 0:a554658735bf 102 }
mbed_official 0:a554658735bf 103 }
mbed_official 0:a554658735bf 104 }
mbed_official 0:a554658735bf 105
mbed_official 0:a554658735bf 106 void USBHostSerial::txHandler() {
mbed_official 0:a554658735bf 107 if (bulk_out) {
mbed_official 0:a554658735bf 108 if (bulk_out->getState() == USB_TYPE_IDLE) {
mbed_official 0:a554658735bf 109 tx.call();
mbed_official 0:a554658735bf 110 }
mbed_official 0:a554658735bf 111 }
mbed_official 0:a554658735bf 112 }
mbed_official 0:a554658735bf 113
mbed_official 0:a554658735bf 114 int USBHostSerial::_putc(int c) {
mbed_official 0:a554658735bf 115 if (bulk_out) {
mbed_official 0:a554658735bf 116 if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) {
mbed_official 0:a554658735bf 117 return 1;
mbed_official 0:a554658735bf 118 }
mbed_official 0:a554658735bf 119 }
mbed_official 0:a554658735bf 120 return -1;
mbed_official 0:a554658735bf 121 }
mbed_official 0:a554658735bf 122
samux 4:b320d68e98e7 123 void USBHostSerial::baud(int baudrate) {
samux 4:b320d68e98e7 124 line_coding.baudrate = baudrate;
samux 8:93da8ea2708b 125 format(line_coding.data_bits, (Parity)line_coding.parity, line_coding.stop_bits);
samux 4:b320d68e98e7 126 }
samux 4:b320d68e98e7 127
samux 4:b320d68e98e7 128 void USBHostSerial::format(int bits, Parity parity, int stop_bits) {
samux 4:b320d68e98e7 129 line_coding.data_bits = bits;
samux 4:b320d68e98e7 130 line_coding.parity = parity;
samux 4:b320d68e98e7 131 line_coding.stop_bits = (stop_bits == 1) ? 0 : 2;
samux 4:b320d68e98e7 132
samux 4:b320d68e98e7 133 // set line coding
samux 4:b320d68e98e7 134 int res = host->controlWrite( dev,
samux 4:b320d68e98e7 135 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
samux 4:b320d68e98e7 136 SET_LINE_CODING,
samux 4:b320d68e98e7 137 0, serial_intf, (uint8_t *)&line_coding, 7);
samux 4:b320d68e98e7 138 }
mbed_official 0:a554658735bf 139
mbed_official 0:a554658735bf 140 int USBHostSerial::_getc() {
mbed_official 0:a554658735bf 141 uint8_t c = 0;
mbed_official 0:a554658735bf 142 if (bulk_in == NULL) {
samux 4:b320d68e98e7 143 init();
mbed_official 0:a554658735bf 144 return -1;
mbed_official 0:a554658735bf 145 }
samux 4:b320d68e98e7 146 while (circ_buf.isEmpty());
mbed_official 0:a554658735bf 147 circ_buf.dequeue(&c);
mbed_official 0:a554658735bf 148 return c;
mbed_official 0:a554658735bf 149 }
mbed_official 0:a554658735bf 150
mbed_official 0:a554658735bf 151
mbed_official 0:a554658735bf 152 uint8_t USBHostSerial::available() {
mbed_official 0:a554658735bf 153 return circ_buf.available();
mbed_official 0:a554658735bf 154 }
mbed_official 0:a554658735bf 155
mbed_official 0:a554658735bf 156 /*virtual*/ void USBHostSerial::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 0:a554658735bf 157 {
mbed_official 0:a554658735bf 158 // we don't check VID/PID for MSD driver
mbed_official 0:a554658735bf 159 }
mbed_official 0:a554658735bf 160
mbed_official 0:a554658735bf 161 /*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 162 {
mbed_official 0:a554658735bf 163 if ((serial_intf == -1) &&
mbed_official 0:a554658735bf 164 (intf_class == SERIAL_CLASS) &&
mbed_official 0:a554658735bf 165 (intf_subclass == 0x00) &&
mbed_official 0:a554658735bf 166 (intf_protocol == 0x00)) {
mbed_official 0:a554658735bf 167 serial_intf = intf_nb;
mbed_official 0:a554658735bf 168 return true;
mbed_official 0:a554658735bf 169 }
mbed_official 0:a554658735bf 170 return false;
mbed_official 0:a554658735bf 171 }
mbed_official 0:a554658735bf 172
mbed_official 0:a554658735bf 173 /*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 174 {
mbed_official 0:a554658735bf 175 if (intf_nb == serial_intf) {
mbed_official 0:a554658735bf 176 if (type == BULK_ENDPOINT) {
mbed_official 0:a554658735bf 177 serial_device_found = true;
mbed_official 0:a554658735bf 178 return true;
mbed_official 0:a554658735bf 179 }
mbed_official 0:a554658735bf 180 }
mbed_official 0:a554658735bf 181 return false;
mbed_official 0:a554658735bf 182 }
mbed_official 0:a554658735bf 183
mbed_official 0:a554658735bf 184 #endif