My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

cdcuser.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*----------------------------------------------------------------------------
  2. * U S B - K e r n e l
  3. *----------------------------------------------------------------------------
  4. * Name: cdcuser.c
  5. * Purpose: USB Communication Device Class User module
  6. * Version: V1.10
  7. *----------------------------------------------------------------------------
  8. * This software is supplied "AS IS" without any warranties, express,
  9. * implied or statutory, including but not limited to the implied
  10. * warranties of fitness for purpose, satisfactory quality and
  11. * noninfringement. Keil extends you a royalty-free right to reproduce
  12. * and distribute executable files created using this software for use
  13. * on NXP Semiconductors LPC microcontroller devices only. Nothing else
  14. * gives you the right to use this software.
  15. *
  16. * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
  17. *---------------------------------------------------------------------------*/
  18. extern "C" {
  19. #include <lpc_types.h>
  20. #include <debug_frmwrk.h>
  21. }
  22. #include "usb.h"
  23. #include "usbhw.h"
  24. #include "usbcfg.h"
  25. #include "usbcore.h"
  26. #include "cdc.h"
  27. #include "cdcuser.h"
  28. #include <serial.h>
  29. unsigned char BulkBufIn[USB_CDC_BUFSIZE]; // Buffer to store USB IN packet
  30. unsigned char BulkBufOut[USB_CDC_BUFSIZE]; // Buffer to store USB OUT packet
  31. unsigned char NotificationBuf[10];
  32. CDC_LINE_CODING CDC_LineCoding = { 921600, 0, 0, 8 };
  33. unsigned short CDC_DepInEmpty = 1; // Data IN EP is empty
  34. unsigned short CDC_LineState = 0;
  35. unsigned short CDC_SerialState = 0;
  36. extern HalSerial usb_serial;
  37. /*----------------------------------------------------------------------------
  38. write data to CDC_OutBuf
  39. *---------------------------------------------------------------------------*/
  40. uint32_t CDC_WrOutBuf(const char *buffer, uint32_t *length) {
  41. uint32_t bytesToWrite, bytesWritten;
  42. // Write *length bytes
  43. bytesToWrite = *length;
  44. bytesWritten = bytesToWrite;
  45. while (bytesToWrite) {
  46. #if ENABLED(EMERGENCY_PARSER)
  47. emergency_parser.update(usb_serial.emergency_state, *buffer);
  48. #endif
  49. usb_serial.receive_buffer.write(*buffer++); // Copy Data to buffer
  50. bytesToWrite--;
  51. }
  52. return (bytesWritten);
  53. }
  54. /*----------------------------------------------------------------------------
  55. check if character(s) are available at CDC_OutBuf
  56. *---------------------------------------------------------------------------*/
  57. uint32_t CDC_OutBufAvailChar(uint32_t *availChar) {
  58. *availChar = usb_serial.transmit_buffer.available();
  59. return (0);
  60. }
  61. /* end Buffer handling */
  62. /*----------------------------------------------------------------------------
  63. CDC Initialisation
  64. Initializes the data structures and serial port
  65. Parameters: None
  66. Return Value: None
  67. *---------------------------------------------------------------------------*/
  68. void CDC_Init() {
  69. CDC_DepInEmpty = 1;
  70. }
  71. /*----------------------------------------------------------------------------
  72. CDC SendEncapsulatedCommand Request Callback
  73. Called automatically on CDC SEND_ENCAPSULATED_COMMAND Request
  74. Parameters: None (global SetupPacket and EP0Buf)
  75. Return Value: TRUE - Success, FALSE - Error
  76. *---------------------------------------------------------------------------*/
  77. uint32_t CDC_SendEncapsulatedCommand(void) {
  78. return (TRUE);
  79. }
  80. /*----------------------------------------------------------------------------
  81. CDC GetEncapsulatedResponse Request Callback
  82. Called automatically on CDC Get_ENCAPSULATED_RESPONSE Request
  83. Parameters: None (global SetupPacket and EP0Buf)
  84. Return Value: TRUE - Success, FALSE - Error
  85. *---------------------------------------------------------------------------*/
  86. uint32_t CDC_GetEncapsulatedResponse(void) {
  87. /* ... add code to handle request */
  88. return (TRUE);
  89. }
  90. /*----------------------------------------------------------------------------
  91. CDC SetCommFeature Request Callback
  92. Called automatically on CDC Set_COMM_FATURE Request
  93. Parameters: FeatureSelector
  94. Return Value: TRUE - Success, FALSE - Error
  95. *---------------------------------------------------------------------------*/
  96. uint32_t CDC_SetCommFeature(unsigned short wFeatureSelector) {
  97. /* ... add code to handle request */
  98. return (TRUE);
  99. }
  100. /*----------------------------------------------------------------------------
  101. CDC GetCommFeature Request Callback
  102. Called automatically on CDC Get_COMM_FATURE Request
  103. Parameters: FeatureSelector
  104. Return Value: TRUE - Success, FALSE - Error
  105. *---------------------------------------------------------------------------*/
  106. uint32_t CDC_GetCommFeature(unsigned short wFeatureSelector) {
  107. /* ... add code to handle request */
  108. return (TRUE);
  109. }
  110. /*----------------------------------------------------------------------------
  111. CDC ClearCommFeature Request Callback
  112. Called automatically on CDC CLEAR_COMM_FATURE Request
  113. Parameters: FeatureSelector
  114. Return Value: TRUE - Success, FALSE - Error
  115. *---------------------------------------------------------------------------*/
  116. uint32_t CDC_ClearCommFeature(unsigned short wFeatureSelector) {
  117. /* ... add code to handle request */
  118. return (TRUE);
  119. }
  120. /*----------------------------------------------------------------------------
  121. CDC SetLineCoding Request Callback
  122. Called automatically on CDC SET_LINE_CODING Request
  123. Parameters: none (global SetupPacket and EP0Buf)
  124. Return Value: TRUE - Success, FALSE - Error
  125. *---------------------------------------------------------------------------*/
  126. uint32_t CDC_SetLineCoding(void) {
  127. CDC_LineCoding.dwDTERate = (EP0Buf[0] << 0) | (EP0Buf[1] << 8) | (EP0Buf[2] << 16) | (EP0Buf[3] << 24);
  128. CDC_LineCoding.bCharFormat = EP0Buf[4];
  129. CDC_LineCoding.bParityType = EP0Buf[5];
  130. CDC_LineCoding.bDataBits = EP0Buf[6];
  131. return (TRUE);
  132. }
  133. /*----------------------------------------------------------------------------
  134. CDC GetLineCoding Request Callback
  135. Called automatically on CDC GET_LINE_CODING Request
  136. Parameters: None (global SetupPacket and EP0Buf)
  137. Return Value: TRUE - Success, FALSE - Error
  138. *---------------------------------------------------------------------------*/
  139. uint32_t CDC_GetLineCoding(void) {
  140. EP0Buf[0] = (CDC_LineCoding.dwDTERate >> 0) & 0xFF;
  141. EP0Buf[1] = (CDC_LineCoding.dwDTERate >> 8) & 0xFF;
  142. EP0Buf[2] = (CDC_LineCoding.dwDTERate >> 16) & 0xFF;
  143. EP0Buf[3] = (CDC_LineCoding.dwDTERate >> 24) & 0xFF;
  144. EP0Buf[4] = CDC_LineCoding.bCharFormat;
  145. EP0Buf[5] = CDC_LineCoding.bParityType;
  146. EP0Buf[6] = CDC_LineCoding.bDataBits;
  147. return (TRUE);
  148. }
  149. /*----------------------------------------------------------------------------
  150. CDC SetControlLineState Request Callback
  151. Called automatically on CDC SET_CONTROL_LINE_STATE Request
  152. Parameters: ControlSignalBitmap
  153. Return Value: TRUE - Success, FALSE - Error
  154. *---------------------------------------------------------------------------*/
  155. uint32_t CDC_SetControlLineState(unsigned short wControlSignalBitmap) {
  156. CDC_LineState = wControlSignalBitmap;
  157. usb_serial.host_connected = wControlSignalBitmap > 0 ? true : false;
  158. return true;
  159. }
  160. /*----------------------------------------------------------------------------
  161. CDC SendBreak Request Callback
  162. Called automatically on CDC Set_COMM_FATURE Request
  163. Parameters: 0xFFFF start of Break
  164. 0x0000 stop of Break
  165. 0x#### Duration of Break
  166. Return Value: TRUE - Success, FALSE - Error
  167. *---------------------------------------------------------------------------*/
  168. uint32_t CDC_SendBreak(unsigned short wDurationOfBreak) {
  169. /* ... add code to handle request */
  170. return (TRUE);
  171. }
  172. /*----------------------------------------------------------------------------
  173. CDC_BulkIn call on DataIn Request
  174. Parameters: none
  175. Return Value: none
  176. *---------------------------------------------------------------------------*/
  177. void CDC_BulkIn(void) {
  178. uint32_t numBytesAvail = usb_serial.transmit_buffer.available();
  179. if (numBytesAvail > 0) {
  180. numBytesAvail = numBytesAvail > (USB_CDC_BUFSIZE - 1) ? (USB_CDC_BUFSIZE - 1) : numBytesAvail;
  181. for(uint32_t i = 0; i < numBytesAvail; ++i) {
  182. BulkBufIn[i] = usb_serial.transmit_buffer.read(); //todo: optimise
  183. }
  184. USB_WriteEP(CDC_DEP_IN, &BulkBufIn[0], numBytesAvail);
  185. } else {
  186. CDC_DepInEmpty = 1;
  187. }
  188. }
  189. /*----------------------------------------------------------------------------
  190. CDC_BulkOut call on DataOut Request
  191. Parameters: none
  192. Return Value: none
  193. *---------------------------------------------------------------------------*/
  194. void CDC_BulkOut(void) {
  195. uint32_t numBytesRead = USB_ReadEP(CDC_DEP_OUT, &BulkBufOut[0]);
  196. CDC_WrOutBuf((char *) &BulkBufOut[0], &numBytesRead);
  197. }
  198. /*----------------------------------------------------------------------------
  199. Get the SERIAL_STATE as defined in usbcdc11.pdf, 6.3.5, Table 69.
  200. Parameters: none
  201. Return Value: SerialState as defined in usbcdc11.pdf
  202. *---------------------------------------------------------------------------*/
  203. unsigned short CDC_GetSerialState(void) {
  204. CDC_SerialState = CDC_LineState;
  205. //todo: detect buffer overrun
  206. return (CDC_SerialState);
  207. }
  208. /*----------------------------------------------------------------------------
  209. Send the SERIAL_STATE notification as defined in usbcdc11.pdf, 6.3.5.
  210. *---------------------------------------------------------------------------*/
  211. void CDC_NotificationIn(void) {
  212. NotificationBuf[0] = 0xA1; // bmRequestType
  213. NotificationBuf[1] = CDC_NOTIFICATION_SERIAL_STATE; // bNotification (SERIAL_STATE)
  214. NotificationBuf[2] = 0x00; // wValue
  215. NotificationBuf[3] = 0x00;
  216. NotificationBuf[4] = 0x00; // wIndex (Interface #, LSB first)
  217. NotificationBuf[5] = 0x00;
  218. NotificationBuf[6] = 0x02; // wLength (Data length = 2 bytes, LSB first)
  219. NotificationBuf[7] = 0x00;
  220. NotificationBuf[8] = (CDC_SerialState >> 0) & 0xFF; // UART State Bitmap (16bits, LSB first)
  221. NotificationBuf[9] = (CDC_SerialState >> 8) & 0xFF;
  222. USB_WriteEP(CDC_CEP_IN, &NotificationBuf[0], 10); // send notification
  223. }