usbd_cdc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @file usbd_cdc.c
  3. * @brief
  4. *
  5. * Copyright (c) 2021 Bouffalolab team
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #include "usbd_core.h"
  24. #include "usbd_cdc.h"
  25. const char *stop_name[] = { "1", "1.5", "2" };
  26. const char *parity_name[] = { "N", "O", "E", "M", "S" };
  27. /* Device data structure */
  28. struct cdc_acm_cfg_private {
  29. /* CDC ACM line coding properties. LE order */
  30. struct cdc_line_coding line_coding;
  31. /* CDC ACM line state bitmap, DTE side */
  32. uint8_t line_state;
  33. /* CDC ACM serial state bitmap, DCE side */
  34. uint8_t serial_state;
  35. /* CDC ACM notification sent status */
  36. uint8_t notification_sent;
  37. /* CDC ACM configured flag */
  38. bool configured;
  39. /* CDC ACM suspended flag */
  40. bool suspended;
  41. uint32_t uart_first_init_flag;
  42. } usbd_cdc_acm_cfg;
  43. static void usbd_cdc_acm_reset(void)
  44. {
  45. usbd_cdc_acm_cfg.line_coding.dwDTERate = 2000000;
  46. usbd_cdc_acm_cfg.line_coding.bDataBits = 8;
  47. usbd_cdc_acm_cfg.line_coding.bParityType = 0;
  48. usbd_cdc_acm_cfg.line_coding.bCharFormat = 0;
  49. usbd_cdc_acm_cfg.configured = false;
  50. usbd_cdc_acm_cfg.uart_first_init_flag = 0;
  51. }
  52. /**
  53. * @brief Handler called for Class requests not handled by the USB stack.
  54. *
  55. * @param pSetup Information about the request to execute.
  56. * @param len Size of the buffer.
  57. * @param data Buffer containing the request result.
  58. *
  59. * @return 0 on success, negative errno code on fail.
  60. */
  61. static int cdc_acm_class_request_handler(struct usb_setup_packet *pSetup, uint8_t **data, uint32_t *len)
  62. {
  63. switch (pSetup->bRequest) {
  64. case CDC_REQUEST_SET_LINE_CODING:
  65. /*******************************************************************************/
  66. /* Line Coding Structure */
  67. /*-----------------------------------------------------------------------------*/
  68. /* Offset | Field | Size | Value | Description */
  69. /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
  70. /* 4 | bCharFormat | 1 | Number | Stop bits */
  71. /* 0 - 1 Stop bit */
  72. /* 1 - 1.5 Stop bits */
  73. /* 2 - 2 Stop bits */
  74. /* 5 | bParityType | 1 | Number | Parity */
  75. /* 0 - None */
  76. /* 1 - Odd */
  77. /* 2 - Even */
  78. /* 3 - Mark */
  79. /* 4 - Space */
  80. /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
  81. /*******************************************************************************/
  82. if (usbd_cdc_acm_cfg.uart_first_init_flag == 0) {
  83. usbd_cdc_acm_cfg.uart_first_init_flag = 1;
  84. return 0;
  85. }
  86. memcpy(&usbd_cdc_acm_cfg.line_coding, *data, sizeof(usbd_cdc_acm_cfg.line_coding));
  87. USBD_LOG_DBG("CDC_SET_LINE_CODING <%d %d %s %s>\r\n",
  88. usbd_cdc_acm_cfg.line_coding.dwDTERate,
  89. usbd_cdc_acm_cfg.line_coding.bDataBits,
  90. parity_name[usbd_cdc_acm_cfg.line_coding.bParityType],
  91. stop_name[usbd_cdc_acm_cfg.line_coding.bCharFormat]);
  92. usbd_cdc_acm_set_line_coding(usbd_cdc_acm_cfg.line_coding.dwDTERate, usbd_cdc_acm_cfg.line_coding.bDataBits,
  93. usbd_cdc_acm_cfg.line_coding.bParityType, usbd_cdc_acm_cfg.line_coding.bCharFormat);
  94. break;
  95. case CDC_REQUEST_SET_CONTROL_LINE_STATE:
  96. usbd_cdc_acm_cfg.line_state = (uint8_t)pSetup->wValue;
  97. bool dtr = (pSetup->wValue & 0x01);
  98. bool rts = (pSetup->wValue & 0x02);
  99. USBD_LOG_DBG("DTR 0x%x,RTS 0x%x\r\n",
  100. dtr, rts);
  101. usbd_cdc_acm_set_dtr(dtr);
  102. usbd_cdc_acm_set_rts(rts);
  103. break;
  104. case CDC_REQUEST_GET_LINE_CODING:
  105. *data = (uint8_t *)(&usbd_cdc_acm_cfg.line_coding);
  106. *len = sizeof(usbd_cdc_acm_cfg.line_coding);
  107. USBD_LOG_DBG("CDC_GET_LINE_CODING %d %d %d %d\r\n",
  108. usbd_cdc_acm_cfg.line_coding.dwDTERate,
  109. usbd_cdc_acm_cfg.line_coding.bCharFormat,
  110. usbd_cdc_acm_cfg.line_coding.bParityType,
  111. usbd_cdc_acm_cfg.line_coding.bDataBits);
  112. break;
  113. default:
  114. USBD_LOG_DBG("CDC ACM request 0x%x, value 0x%x\r\n",
  115. pSetup->bRequest, pSetup->wValue);
  116. return -1;
  117. }
  118. return 0;
  119. }
  120. static void cdc_notify_handler(uint8_t event, void *arg)
  121. {
  122. switch (event) {
  123. case USB_EVENT_RESET:
  124. usbd_cdc_acm_reset();
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. __weak void usbd_cdc_acm_set_line_coding(uint32_t baudrate, uint8_t databits, uint8_t parity, uint8_t stopbits)
  131. {
  132. }
  133. __weak void usbd_cdc_acm_set_dtr(bool dtr)
  134. {
  135. }
  136. __weak void usbd_cdc_acm_set_rts(bool rts)
  137. {
  138. }
  139. void usbd_cdc_add_acm_interface(usbd_class_t *class, usbd_interface_t *intf)
  140. {
  141. static usbd_class_t *last_class = NULL;
  142. if (last_class != class) {
  143. last_class = class;
  144. usbd_class_register(class);
  145. }
  146. intf->class_handler = cdc_acm_class_request_handler;
  147. intf->custom_handler = NULL;
  148. intf->vendor_handler = NULL;
  149. intf->notify_handler = cdc_notify_handler;
  150. usbd_class_add_interface(class, intf);
  151. }