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.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #ifndef USBHOSTSERIAL_H
  17. #define USBHOSTSERIAL_H
  18. #include "USBHostConf.h"
  19. #if USBHOST_SERIAL
  20. #include "USBHost.h"
  21. #include "Stream.h"
  22. #include "MtxCircBuffer.h"
  23. /**
  24. * A class to communicate a USB virtual serial port
  25. */
  26. class USBHostSerialPort : public Stream {
  27. public:
  28. /**
  29. * Constructor
  30. */
  31. USBHostSerialPort();
  32. enum IrqType {
  33. RxIrq,
  34. TxIrq
  35. };
  36. enum Parity {
  37. None = 0,
  38. Odd,
  39. Even,
  40. Mark,
  41. Space
  42. };
  43. void connect(USBHost* _host, USBDeviceConnected * _dev,
  44. uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out);
  45. /**
  46. * Check the number of bytes available.
  47. *
  48. * @returns the number of bytes available
  49. */
  50. uint8_t available();
  51. /**
  52. * Attach a member function to call when a packet is received.
  53. *
  54. * @param tptr pointer to the object to call the member function on
  55. * @param mptr pointer to the member function to be called
  56. * @param irq irq type
  57. */
  58. template<typename T>
  59. inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) {
  60. if ((mptr != NULL) && (tptr != NULL)) {
  61. if (irq == RxIrq) {
  62. rx.attach(tptr, mptr);
  63. } else {
  64. tx.attach(tptr, mptr);
  65. }
  66. }
  67. }
  68. /**
  69. * Attach a callback called when a packet is received
  70. *
  71. * @param ptr function pointer
  72. */
  73. inline void attach(void (*fn)(void), IrqType irq = RxIrq) {
  74. if (fn != NULL) {
  75. if (irq == RxIrq) {
  76. rx.attach(fn);
  77. } else {
  78. tx.attach(fn);
  79. }
  80. }
  81. }
  82. /** Set the baud rate of the serial port
  83. *
  84. * @param baudrate The baudrate of the serial port (default = 9600).
  85. */
  86. void baud(int baudrate = 9600);
  87. /** Set the transmission format used by the Serial port
  88. *
  89. * @param bits The number of bits in a word (default = 8)
  90. * @param parity The parity used (USBHostSerialPort::None, USBHostSerialPort::Odd, USBHostSerialPort::Even, USBHostSerialPort::Mark, USBHostSerialPort::Space; default = USBHostSerialPort::None)
  91. * @param stop The number of stop bits (1 or 2; default = 1)
  92. */
  93. void format(int bits = 8, Parity parity = USBHostSerialPort::None, int stop_bits = 1);
  94. virtual int writeBuf(const char* b, int s);
  95. virtual int readBuf(char* b, int s);
  96. protected:
  97. virtual int _getc();
  98. virtual int _putc(int c);
  99. private:
  100. USBHost * host;
  101. USBDeviceConnected * dev;
  102. USBEndpoint * bulk_in;
  103. USBEndpoint * bulk_out;
  104. uint32_t size_bulk_in;
  105. uint32_t size_bulk_out;
  106. void init();
  107. MtxCircBuffer<uint8_t, 128> circ_buf;
  108. uint8_t buf[64];
  109. typedef struct {
  110. uint32_t baudrate;
  111. uint8_t stop_bits;
  112. uint8_t parity;
  113. uint8_t data_bits;
  114. } PACKED LINE_CODING;
  115. LINE_CODING line_coding;
  116. void rxHandler();
  117. void txHandler();
  118. FunctionPointer rx;
  119. FunctionPointer tx;
  120. uint8_t serial_intf;
  121. };
  122. #if (USBHOST_SERIAL <= 1)
  123. class USBHostSerial : public IUSBEnumerator, public USBHostSerialPort
  124. {
  125. public:
  126. USBHostSerial();
  127. /**
  128. * Try to connect a serial device
  129. *
  130. * @return true if connection was successful
  131. */
  132. bool connect();
  133. void disconnect();
  134. /**
  135. * Check if a any serial port is connected
  136. *
  137. * @returns true if a serial device is connected
  138. */
  139. bool connected();
  140. protected:
  141. USBHost* host;
  142. USBDeviceConnected* dev;
  143. uint8_t port_intf;
  144. int ports_found;
  145. //From IUSBEnumerator
  146. virtual void setVidPid(uint16_t vid, uint16_t pid);
  147. virtual bool 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
  148. virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
  149. private:
  150. bool dev_connected;
  151. };
  152. #else // (USBHOST_SERIAL > 1)
  153. class USBHostMultiSerial : public IUSBEnumerator {
  154. public:
  155. USBHostMultiSerial();
  156. virtual ~USBHostMultiSerial();
  157. USBHostSerialPort* getPort(int port)
  158. {
  159. return port < USBHOST_SERIAL ? ports[port] : NULL;
  160. }
  161. /**
  162. * Try to connect a serial device
  163. *
  164. * @return true if connection was successful
  165. */
  166. bool connect();
  167. void disconnect();
  168. /**
  169. * Check if a any serial port is connected
  170. *
  171. * @returns true if a serial device is connected
  172. */
  173. bool connected();
  174. protected:
  175. USBHost* host;
  176. USBDeviceConnected* dev;
  177. USBHostSerialPort* ports[USBHOST_SERIAL];
  178. uint8_t port_intf[USBHOST_SERIAL];
  179. int ports_found;
  180. //From IUSBEnumerator
  181. virtual void setVidPid(uint16_t vid, uint16_t pid);
  182. virtual bool 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
  183. virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
  184. private:
  185. bool dev_connected;
  186. };
  187. #endif // (USBHOST_SERIAL <= 1)
  188. #endif
  189. #endif