CDC.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* Copyright (c) 2011, Peter Barrett
  2. **
  3. ** Permission to use, copy, modify, and/or distribute this software for
  4. ** any purpose with or without fee is hereby granted, provided that the
  5. ** above copyright notice and this permission notice appear in all copies.
  6. **
  7. ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
  10. ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
  11. ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  12. ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  13. ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  14. ** SOFTWARE.
  15. */
  16. #include "Platform.h"
  17. #include "USBAPI.h"
  18. #include <avr/wdt.h>
  19. #if defined(USBCON)
  20. #ifdef CDC_ENABLED
  21. #if (RAMEND < 1000)
  22. #define SERIAL_BUFFER_SIZE 16
  23. #else
  24. #define SERIAL_BUFFER_SIZE 64
  25. #endif
  26. struct ring_buffer
  27. {
  28. unsigned char buffer[SERIAL_BUFFER_SIZE];
  29. volatile int head;
  30. volatile int tail;
  31. };
  32. ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};
  33. typedef struct
  34. {
  35. u32 dwDTERate;
  36. u8 bCharFormat;
  37. u8 bParityType;
  38. u8 bDataBits;
  39. u8 lineState;
  40. } LineInfo;
  41. static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
  42. #define WEAK __attribute__ ((weak))
  43. extern const CDCDescriptor _cdcInterface PROGMEM;
  44. const CDCDescriptor _cdcInterface =
  45. {
  46. D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
  47. // CDC communication interface
  48. D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
  49. D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd)
  50. D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management (not)
  51. D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
  52. D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
  53. D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
  54. // CDC data interface
  55. D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
  56. D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
  57. D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
  58. };
  59. int WEAK CDC_GetInterface(u8* interfaceNum)
  60. {
  61. interfaceNum[0] += 2; // uses 2
  62. return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
  63. }
  64. bool WEAK CDC_Setup(Setup& setup)
  65. {
  66. u8 r = setup.bRequest;
  67. u8 requestType = setup.bmRequestType;
  68. if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
  69. {
  70. if (CDC_GET_LINE_CODING == r)
  71. {
  72. USB_SendControl(0,(void*)&_usbLineInfo,7);
  73. return true;
  74. }
  75. }
  76. if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
  77. {
  78. if (CDC_SET_LINE_CODING == r)
  79. {
  80. USB_RecvControl((void*)&_usbLineInfo,7);
  81. return true;
  82. }
  83. if (CDC_SET_CONTROL_LINE_STATE == r)
  84. {
  85. _usbLineInfo.lineState = setup.wValueL;
  86. // auto-reset into the bootloader is triggered when the port, already
  87. // open at 1200 bps, is closed. this is the signal to start the watchdog
  88. // with a relatively long period so it can finish housekeeping tasks
  89. // like servicing endpoints before the sketch ends
  90. if (1200 == _usbLineInfo.dwDTERate) {
  91. // We check DTR state to determine if host port is open (bit 0 of lineState).
  92. if ((_usbLineInfo.lineState & 0x01) == 0) {
  93. *(uint16_t *)0x0800 = 0x7777;
  94. wdt_enable(WDTO_120MS);
  95. } else {
  96. // Most OSs do some intermediate steps when configuring ports and DTR can
  97. // twiggle more than once before stabilizing.
  98. // To avoid spurious resets we set the watchdog to 250ms and eventually
  99. // cancel if DTR goes back high.
  100. wdt_disable();
  101. wdt_reset();
  102. *(uint16_t *)0x0800 = 0x0;
  103. }
  104. }
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. int _serialPeek = -1;
  111. void Serial_::begin(uint16_t baud_count)
  112. {
  113. }
  114. void Serial_::end(void)
  115. {
  116. }
  117. void Serial_::accept(void)
  118. {
  119. ring_buffer *buffer = &cdc_rx_buffer;
  120. int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
  121. // if we should be storing the received character into the location
  122. // just before the tail (meaning that the head would advance to the
  123. // current location of the tail), we're about to overflow the buffer
  124. // and so we don't write the character or advance the head.
  125. // while we have room to store a byte
  126. while (i != buffer->tail) {
  127. int c = USB_Recv(CDC_RX);
  128. if (c == -1)
  129. break; // no more data
  130. buffer->buffer[buffer->head] = c;
  131. buffer->head = i;
  132. i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
  133. }
  134. }
  135. int Serial_::available(void)
  136. {
  137. ring_buffer *buffer = &cdc_rx_buffer;
  138. return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
  139. }
  140. int Serial_::peek(void)
  141. {
  142. ring_buffer *buffer = &cdc_rx_buffer;
  143. if (buffer->head == buffer->tail) {
  144. return -1;
  145. } else {
  146. return buffer->buffer[buffer->tail];
  147. }
  148. }
  149. int Serial_::read(void)
  150. {
  151. ring_buffer *buffer = &cdc_rx_buffer;
  152. // if the head isn't ahead of the tail, we don't have any characters
  153. if (buffer->head == buffer->tail) {
  154. return -1;
  155. } else {
  156. unsigned char c = buffer->buffer[buffer->tail];
  157. buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
  158. return c;
  159. }
  160. }
  161. void Serial_::flush(void)
  162. {
  163. USB_Flush(CDC_TX);
  164. }
  165. size_t Serial_::write(uint8_t c)
  166. {
  167. /* only try to send bytes if the high-level CDC connection itself
  168. is open (not just the pipe) - the OS should set lineState when the port
  169. is opened and clear lineState when the port is closed.
  170. bytes sent before the user opens the connection or after
  171. the connection is closed are lost - just like with a UART. */
  172. // TODO - ZE - check behavior on different OSes and test what happens if an
  173. // open connection isn't broken cleanly (cable is yanked out, host dies
  174. // or locks up, or host virtual serial port hangs)
  175. if (_usbLineInfo.lineState > 0) {
  176. int r = USB_Send(CDC_TX,&c,1);
  177. if (r > 0) {
  178. return r;
  179. } else {
  180. setWriteError();
  181. return 0;
  182. }
  183. }
  184. setWriteError();
  185. return 0;
  186. }
  187. // This operator is a convenient way for a sketch to check whether the
  188. // port has actually been configured and opened by the host (as opposed
  189. // to just being connected to the host). It can be used, for example, in
  190. // setup() before printing to ensure that an application on the host is
  191. // actually ready to receive and display the data.
  192. // We add a short delay before returning to fix a bug observed by Federico
  193. // where the port is configured (lineState != 0) but not quite opened.
  194. Serial_::operator bool() {
  195. bool result = false;
  196. if (_usbLineInfo.lineState > 0)
  197. result = true;
  198. delay(10);
  199. return result;
  200. }
  201. Serial_ Serial;
  202. #endif
  203. #endif /* if defined(USBCON) */