Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

USBHostSerial.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* mbed USBHost Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "USBHostSerial.h"
  17. #if USBHOST_SERIAL
  18. #include "dbg.h"
  19. #define CHECK_INTERFACE(cls,subcls,proto) \
  20. (((cls == 0xFF) && (subcls == 0xFF) && (proto == 0xFF)) /* QUALCOM CDC */ || \
  21. ((cls == SERIAL_CLASS) && (subcls == 0x00) && (proto == 0x00)) /* STANDARD CDC */ )
  22. #if (USBHOST_SERIAL <= 1)
  23. USBHostSerial::USBHostSerial()
  24. {
  25. host = USBHost::getHostInst();
  26. ports_found = 0;
  27. dev_connected = false;
  28. }
  29. bool USBHostSerial::connected()
  30. {
  31. return dev_connected;
  32. }
  33. void USBHostSerial::disconnect(void)
  34. {
  35. ports_found = 0;
  36. dev = NULL;
  37. }
  38. bool USBHostSerial::connect() {
  39. if (dev)
  40. {
  41. for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++)
  42. {
  43. USBDeviceConnected* d = host->getDevice(i);
  44. if (dev == d)
  45. return true;
  46. }
  47. disconnect();
  48. }
  49. for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++)
  50. {
  51. USBDeviceConnected* d = host->getDevice(i);
  52. if (d != NULL) {
  53. USB_DBG("Trying to connect serial device \r\n");
  54. if(host->enumerate(d, this))
  55. break;
  56. USBEndpoint* bulk_in = d->getEndpoint(port_intf, BULK_ENDPOINT, IN);
  57. USBEndpoint* bulk_out = d->getEndpoint(port_intf, BULK_ENDPOINT, OUT);
  58. if (bulk_in && bulk_out)
  59. {
  60. USBHostSerialPort::connect(host,d,port_intf,bulk_in, bulk_out);
  61. dev = d;
  62. dev_connected = true;
  63. }
  64. }
  65. }
  66. return dev != NULL;
  67. }
  68. /*virtual*/ void USBHostSerial::setVidPid(uint16_t vid, uint16_t pid)
  69. {
  70. // we don't check VID/PID for MSD driver
  71. }
  72. /*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
  73. {
  74. if (!ports_found &&
  75. CHECK_INTERFACE(intf_class, intf_subclass, intf_protocol)) {
  76. port_intf = intf_nb;
  77. ports_found = true;
  78. return true;
  79. }
  80. return false;
  81. }
  82. /*virtual*/ bool USBHostSerial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
  83. {
  84. if (ports_found && (intf_nb == port_intf)) {
  85. if (type == BULK_ENDPOINT)
  86. return true;
  87. }
  88. return false;
  89. }
  90. #else // (USBHOST_SERIAL > 1)
  91. //------------------------------------------------------------------------------
  92. USBHostMultiSerial::USBHostMultiSerial()
  93. {
  94. host = USBHost::getHostInst();
  95. dev = NULL;
  96. memset(ports, NULL, sizeof(ports));
  97. ports_found = 0;
  98. dev_connected = false;
  99. }
  100. USBHostMultiSerial::~USBHostMultiSerial()
  101. {
  102. disconnect();
  103. }
  104. bool USBHostMultiSerial::connected()
  105. {
  106. return dev_connected;
  107. }
  108. void USBHostMultiSerial::disconnect(void)
  109. {
  110. for (int port = 0; port < USBHOST_SERIAL; port ++)
  111. {
  112. if (ports[port])
  113. {
  114. delete ports[port];
  115. ports[port] = NULL;
  116. }
  117. }
  118. ports_found = 0;
  119. dev = NULL;
  120. }
  121. bool USBHostMultiSerial::connect() {
  122. if (dev)
  123. {
  124. for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++)
  125. {
  126. USBDeviceConnected* d = host->getDevice(i);
  127. if (dev == d)
  128. return true;
  129. }
  130. disconnect();
  131. }
  132. for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++)
  133. {
  134. USBDeviceConnected* d = host->getDevice(i);
  135. if (d != NULL) {
  136. USB_DBG("Trying to connect serial device \r\n");
  137. if(host->enumerate(d, this))
  138. break;
  139. for (int port = 0; port < ports_found; port ++)
  140. {
  141. USBEndpoint* bulk_in = d->getEndpoint(port_intf[port], BULK_ENDPOINT, IN);
  142. USBEndpoint* bulk_out = d->getEndpoint(port_intf[port], BULK_ENDPOINT, OUT);
  143. if (bulk_in && bulk_out)
  144. {
  145. ports[port] = new USBHostSerialPort();
  146. if (ports[port])
  147. {
  148. ports[port]->connect(host,d,port_intf[port],bulk_in, bulk_out);
  149. dev = d;
  150. dev_connected = true;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. return dev != NULL;
  157. }
  158. /*virtual*/ void USBHostMultiSerial::setVidPid(uint16_t vid, uint16_t pid)
  159. {
  160. // we don't check VID/PID for MSD driver
  161. }
  162. /*virtual*/ bool USBHostMultiSerial::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
  163. {
  164. if ((ports_found < USBHOST_SERIAL) &&
  165. CHECK_INTERFACE(intf_class, intf_subclass, intf_protocol)) {
  166. port_intf[ports_found++] = intf_nb;
  167. return true;
  168. }
  169. return false;
  170. }
  171. /*virtual*/ bool USBHostMultiSerial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
  172. {
  173. if ((ports_found > 0) && (intf_nb == port_intf[ports_found-1])) {
  174. if (type == BULK_ENDPOINT)
  175. return true;
  176. }
  177. return false;
  178. }
  179. #endif
  180. //------------------------------------------------------------------------------
  181. #define SET_LINE_CODING 0x20
  182. USBHostSerialPort::USBHostSerialPort(): circ_buf()
  183. {
  184. init();
  185. }
  186. void USBHostSerialPort::init(void)
  187. {
  188. host = NULL;
  189. dev = NULL;
  190. serial_intf = NULL;
  191. size_bulk_in = 0;
  192. size_bulk_out = 0;
  193. bulk_in = NULL;
  194. bulk_out = NULL;
  195. line_coding.baudrate = 9600;
  196. line_coding.data_bits = 8;
  197. line_coding.parity = None;
  198. line_coding.stop_bits = 1;
  199. circ_buf.flush();
  200. }
  201. void USBHostSerialPort::connect(USBHost* _host, USBDeviceConnected * _dev,
  202. uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out)
  203. {
  204. host = _host;
  205. dev = _dev;
  206. serial_intf = _serial_intf;
  207. bulk_in = _bulk_in;
  208. bulk_out = _bulk_out;
  209. USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, serial_intf);
  210. dev->setName("Serial", serial_intf);
  211. host->registerDriver(dev, serial_intf, this, &USBHostSerialPort::init);
  212. baud(9600);
  213. size_bulk_in = bulk_in->getSize();
  214. size_bulk_out = bulk_out->getSize();
  215. bulk_in->attach(this, &USBHostSerialPort::rxHandler);
  216. bulk_out->attach(this, &USBHostSerialPort::txHandler);
  217. host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
  218. }
  219. void USBHostSerialPort::rxHandler() {
  220. if (bulk_in) {
  221. int len = bulk_in->getLengthTransferred();
  222. if (bulk_in->getState() == USB_TYPE_IDLE) {
  223. for (int i = 0; i < len; i++) {
  224. circ_buf.queue(buf[i]);
  225. }
  226. rx.call();
  227. host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
  228. }
  229. }
  230. }
  231. void USBHostSerialPort::txHandler() {
  232. if (bulk_out) {
  233. if (bulk_out->getState() == USB_TYPE_IDLE) {
  234. tx.call();
  235. }
  236. }
  237. }
  238. int USBHostSerialPort::_putc(int c) {
  239. if (bulk_out) {
  240. if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) {
  241. return 1;
  242. }
  243. }
  244. return -1;
  245. }
  246. void USBHostSerialPort::baud(int baudrate) {
  247. line_coding.baudrate = baudrate;
  248. format(line_coding.data_bits, (Parity)line_coding.parity, line_coding.stop_bits);
  249. }
  250. void USBHostSerialPort::format(int bits, Parity parity, int stop_bits) {
  251. line_coding.data_bits = bits;
  252. line_coding.parity = parity;
  253. line_coding.stop_bits = (stop_bits == 1) ? 0 : 2;
  254. // set line coding
  255. host->controlWrite( dev,
  256. USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
  257. SET_LINE_CODING,
  258. 0, serial_intf, (uint8_t *)&line_coding, 7);
  259. }
  260. int USBHostSerialPort::_getc() {
  261. uint8_t c = 0;
  262. if (bulk_in == NULL) {
  263. init();
  264. return -1;
  265. }
  266. while (circ_buf.isEmpty());
  267. circ_buf.dequeue(&c);
  268. return c;
  269. }
  270. int USBHostSerialPort::writeBuf(const char* b, int s)
  271. {
  272. int c = 0;
  273. if (bulk_out)
  274. {
  275. while (c < s)
  276. {
  277. int i = (s < size_bulk_out) ? s : size_bulk_out;
  278. if (host->bulkWrite(dev, bulk_out, (uint8_t *)(b+c), i) == USB_TYPE_OK)
  279. c += i;
  280. }
  281. }
  282. return s;
  283. }
  284. int USBHostSerialPort::readBuf(char* b, int s)
  285. {
  286. int i = 0;
  287. if (bulk_in)
  288. {
  289. for (i = 0; i < s; )
  290. b[i++] = getc();
  291. }
  292. return i;
  293. }
  294. uint8_t USBHostSerialPort::available() {
  295. return circ_buf.available();
  296. }
  297. #endif