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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

CDC.cpp 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 c = USB_Recv(CDC_RX);
  121. int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
  122. // if we should be storing the received character into the location
  123. // just before the tail (meaning that the head would advance to the
  124. // current location of the tail), we're about to overflow the buffer
  125. // and so we don't write the character or advance the head.
  126. if (i != buffer->tail) {
  127. buffer->buffer[buffer->head] = c;
  128. buffer->head = i;
  129. }
  130. }
  131. int Serial_::available(void)
  132. {
  133. ring_buffer *buffer = &cdc_rx_buffer;
  134. return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
  135. }
  136. int Serial_::peek(void)
  137. {
  138. ring_buffer *buffer = &cdc_rx_buffer;
  139. if (buffer->head == buffer->tail) {
  140. return -1;
  141. } else {
  142. return buffer->buffer[buffer->tail];
  143. }
  144. }
  145. int Serial_::read(void)
  146. {
  147. ring_buffer *buffer = &cdc_rx_buffer;
  148. // if the head isn't ahead of the tail, we don't have any characters
  149. if (buffer->head == buffer->tail) {
  150. return -1;
  151. } else {
  152. unsigned char c = buffer->buffer[buffer->tail];
  153. buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
  154. return c;
  155. }
  156. }
  157. void Serial_::flush(void)
  158. {
  159. USB_Flush(CDC_TX);
  160. }
  161. size_t Serial_::write(uint8_t c)
  162. {
  163. /* only try to send bytes if the high-level CDC connection itself
  164. is open (not just the pipe) - the OS should set lineState when the port
  165. is opened and clear lineState when the port is closed.
  166. bytes sent before the user opens the connection or after
  167. the connection is closed are lost - just like with a UART. */
  168. // TODO - ZE - check behavior on different OSes and test what happens if an
  169. // open connection isn't broken cleanly (cable is yanked out, host dies
  170. // or locks up, or host virtual serial port hangs)
  171. if (_usbLineInfo.lineState > 0) {
  172. int r = USB_Send(CDC_TX,&c,1);
  173. if (r > 0) {
  174. return r;
  175. } else {
  176. setWriteError();
  177. return 0;
  178. }
  179. }
  180. setWriteError();
  181. return 0;
  182. }
  183. // This operator is a convenient way for a sketch to check whether the
  184. // port has actually been configured and opened by the host (as opposed
  185. // to just being connected to the host). It can be used, for example, in
  186. // setup() before printing to ensure that an application on the host is
  187. // actually ready to receive and display the data.
  188. // We add a short delay before returning to fix a bug observed by Federico
  189. // where the port is configured (lineState != 0) but not quite opened.
  190. Serial_::operator bool() {
  191. bool result = false;
  192. if (_usbLineInfo.lineState > 0)
  193. result = true;
  194. delay(10);
  195. return result;
  196. }
  197. Serial_ Serial;
  198. #endif
  199. #endif /* if defined(USBCON) */