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 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <HAL_LPC1768/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. usb_serial.receive_buffer.write(*buffer++); // Copy Data to buffer
  47. bytesToWrite--;
  48. }
  49. return (bytesWritten);
  50. }
  51. /*----------------------------------------------------------------------------
  52. check if character(s) are available at CDC_OutBuf
  53. *---------------------------------------------------------------------------*/
  54. uint32_t CDC_OutBufAvailChar(uint32_t *availChar) {
  55. *availChar = usb_serial.transmit_buffer.available();
  56. return (0);
  57. }
  58. /* end Buffer handling */
  59. /*----------------------------------------------------------------------------
  60. CDC Initialisation
  61. Initializes the data structures and serial port
  62. Parameters: None
  63. Return Value: None
  64. *---------------------------------------------------------------------------*/
  65. void CDC_Init() {
  66. CDC_DepInEmpty = 1;
  67. }
  68. /*----------------------------------------------------------------------------
  69. CDC SendEncapsulatedCommand Request Callback
  70. Called automatically on CDC SEND_ENCAPSULATED_COMMAND Request
  71. Parameters: None (global SetupPacket and EP0Buf)
  72. Return Value: TRUE - Success, FALSE - Error
  73. *---------------------------------------------------------------------------*/
  74. uint32_t CDC_SendEncapsulatedCommand(void) {
  75. return (TRUE);
  76. }
  77. /*----------------------------------------------------------------------------
  78. CDC GetEncapsulatedResponse Request Callback
  79. Called automatically on CDC Get_ENCAPSULATED_RESPONSE Request
  80. Parameters: None (global SetupPacket and EP0Buf)
  81. Return Value: TRUE - Success, FALSE - Error
  82. *---------------------------------------------------------------------------*/
  83. uint32_t CDC_GetEncapsulatedResponse(void) {
  84. /* ... add code to handle request */
  85. return (TRUE);
  86. }
  87. /*----------------------------------------------------------------------------
  88. CDC SetCommFeature Request Callback
  89. Called automatically on CDC Set_COMM_FATURE Request
  90. Parameters: FeatureSelector
  91. Return Value: TRUE - Success, FALSE - Error
  92. *---------------------------------------------------------------------------*/
  93. uint32_t CDC_SetCommFeature(unsigned short wFeatureSelector) {
  94. /* ... add code to handle request */
  95. return (TRUE);
  96. }
  97. /*----------------------------------------------------------------------------
  98. CDC GetCommFeature Request Callback
  99. Called automatically on CDC Get_COMM_FATURE Request
  100. Parameters: FeatureSelector
  101. Return Value: TRUE - Success, FALSE - Error
  102. *---------------------------------------------------------------------------*/
  103. uint32_t CDC_GetCommFeature(unsigned short wFeatureSelector) {
  104. /* ... add code to handle request */
  105. return (TRUE);
  106. }
  107. /*----------------------------------------------------------------------------
  108. CDC ClearCommFeature Request Callback
  109. Called automatically on CDC CLEAR_COMM_FATURE Request
  110. Parameters: FeatureSelector
  111. Return Value: TRUE - Success, FALSE - Error
  112. *---------------------------------------------------------------------------*/
  113. uint32_t CDC_ClearCommFeature(unsigned short wFeatureSelector) {
  114. /* ... add code to handle request */
  115. return (TRUE);
  116. }
  117. /*----------------------------------------------------------------------------
  118. CDC SetLineCoding Request Callback
  119. Called automatically on CDC SET_LINE_CODING Request
  120. Parameters: none (global SetupPacket and EP0Buf)
  121. Return Value: TRUE - Success, FALSE - Error
  122. *---------------------------------------------------------------------------*/
  123. uint32_t CDC_SetLineCoding(void) {
  124. CDC_LineCoding.dwDTERate = (EP0Buf[0] << 0) | (EP0Buf[1] << 8) | (EP0Buf[2] << 16) | (EP0Buf[3] << 24);
  125. CDC_LineCoding.bCharFormat = EP0Buf[4];
  126. CDC_LineCoding.bParityType = EP0Buf[5];
  127. CDC_LineCoding.bDataBits = EP0Buf[6];
  128. return (TRUE);
  129. }
  130. /*----------------------------------------------------------------------------
  131. CDC GetLineCoding Request Callback
  132. Called automatically on CDC GET_LINE_CODING Request
  133. Parameters: None (global SetupPacket and EP0Buf)
  134. Return Value: TRUE - Success, FALSE - Error
  135. *---------------------------------------------------------------------------*/
  136. uint32_t CDC_GetLineCoding(void) {
  137. EP0Buf[0] = (CDC_LineCoding.dwDTERate >> 0) & 0xFF;
  138. EP0Buf[1] = (CDC_LineCoding.dwDTERate >> 8) & 0xFF;
  139. EP0Buf[2] = (CDC_LineCoding.dwDTERate >> 16) & 0xFF;
  140. EP0Buf[3] = (CDC_LineCoding.dwDTERate >> 24) & 0xFF;
  141. EP0Buf[4] = CDC_LineCoding.bCharFormat;
  142. EP0Buf[5] = CDC_LineCoding.bParityType;
  143. EP0Buf[6] = CDC_LineCoding.bDataBits;
  144. return (TRUE);
  145. }
  146. /*----------------------------------------------------------------------------
  147. CDC SetControlLineState Request Callback
  148. Called automatically on CDC SET_CONTROL_LINE_STATE Request
  149. Parameters: ControlSignalBitmap
  150. Return Value: TRUE - Success, FALSE - Error
  151. *---------------------------------------------------------------------------*/
  152. uint32_t CDC_SetControlLineState(unsigned short wControlSignalBitmap) {
  153. CDC_LineState = wControlSignalBitmap;
  154. usb_serial.host_connected = wControlSignalBitmap > 0 ? true : false;
  155. return true;
  156. }
  157. /*----------------------------------------------------------------------------
  158. CDC SendBreak Request Callback
  159. Called automatically on CDC Set_COMM_FATURE Request
  160. Parameters: 0xFFFF start of Break
  161. 0x0000 stop of Break
  162. 0x#### Duration of Break
  163. Return Value: TRUE - Success, FALSE - Error
  164. *---------------------------------------------------------------------------*/
  165. uint32_t CDC_SendBreak(unsigned short wDurationOfBreak) {
  166. /* ... add code to handle request */
  167. return (TRUE);
  168. }
  169. /*----------------------------------------------------------------------------
  170. CDC_BulkIn call on DataIn Request
  171. Parameters: none
  172. Return Value: none
  173. *---------------------------------------------------------------------------*/
  174. void CDC_BulkIn(void) {
  175. uint32_t numBytesAvail = usb_serial.transmit_buffer.available();
  176. if (numBytesAvail > 0) {
  177. numBytesAvail = numBytesAvail > (USB_CDC_BUFSIZE - 1) ? (USB_CDC_BUFSIZE - 1) : numBytesAvail;
  178. for(uint32_t i = 0; i < numBytesAvail; ++i) {
  179. BulkBufIn[i] = usb_serial.transmit_buffer.read(); //todo: optimise
  180. }
  181. USB_WriteEP(CDC_DEP_IN, &BulkBufIn[0], numBytesAvail);
  182. } else {
  183. CDC_DepInEmpty = 1;
  184. }
  185. }
  186. /*----------------------------------------------------------------------------
  187. CDC_BulkOut call on DataOut Request
  188. Parameters: none
  189. Return Value: none
  190. *---------------------------------------------------------------------------*/
  191. void CDC_BulkOut(void) {
  192. uint32_t numBytesRead = USB_ReadEP(CDC_DEP_OUT, &BulkBufOut[0]);
  193. CDC_WrOutBuf((char *) &BulkBufOut[0], &numBytesRead);
  194. }
  195. /*----------------------------------------------------------------------------
  196. Get the SERIAL_STATE as defined in usbcdc11.pdf, 6.3.5, Table 69.
  197. Parameters: none
  198. Return Value: SerialState as defined in usbcdc11.pdf
  199. *---------------------------------------------------------------------------*/
  200. unsigned short CDC_GetSerialState(void) {
  201. CDC_SerialState = CDC_LineState;
  202. //todo: detect buffer overrun
  203. return (CDC_SerialState);
  204. }
  205. /*----------------------------------------------------------------------------
  206. Send the SERIAL_STATE notification as defined in usbcdc11.pdf, 6.3.5.
  207. *---------------------------------------------------------------------------*/
  208. void CDC_NotificationIn(void) {
  209. NotificationBuf[0] = 0xA1; // bmRequestType
  210. NotificationBuf[1] = CDC_NOTIFICATION_SERIAL_STATE; // bNotification (SERIAL_STATE)
  211. NotificationBuf[2] = 0x00; // wValue
  212. NotificationBuf[3] = 0x00;
  213. NotificationBuf[4] = 0x00; // wIndex (Interface #, LSB first)
  214. NotificationBuf[5] = 0x00;
  215. NotificationBuf[6] = 0x02; // wLength (Data length = 2 bytes, LSB first)
  216. NotificationBuf[7] = 0x00;
  217. NotificationBuf[8] = (CDC_SerialState >> 0) & 0xFF; // UART State Bitmap (16bits, LSB first)
  218. NotificationBuf[9] = (CDC_SerialState >> 8) & 0xFF;
  219. USB_WriteEP(CDC_CEP_IN, &NotificationBuf[0], 10); // send notification
  220. }