tusb_cdc_acm.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2020 Espressif Systems (Shanghai) Co. Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <stdint.h>
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/ringbuf.h"
  21. #include "freertos/semphr.h"
  22. #include "freertos/timers.h"
  23. #include "tusb.h"
  24. #include "tinyusb.h"
  25. /**
  26. * @brief CDC ports available to setup
  27. */
  28. typedef enum{
  29. TINYUSB_CDC_ACM_0 = 0x0
  30. }tinyusb_cdcacm_itf_t;
  31. /* Callbacks and events
  32. ********************************************************************* */
  33. /**
  34. * @brief Data provided to the input of the `callback_rx_wanted_char` callback
  35. */
  36. typedef struct {
  37. char wanted_char; /*!< Wanted character */
  38. } cdcacm_event_rx_wanted_char_data_t;
  39. /**
  40. * @brief Data provided to the input of the `callback_line_state_changed` callback
  41. */
  42. typedef struct {
  43. bool dtr; /*!< Data Terminal Ready (DTR) line state */
  44. bool rts; /*!< Request To Send (RTS) line state */
  45. } cdcacm_event_line_state_changed_data_t;
  46. /**
  47. * @brief Data provided to the input of the `line_coding_changed` callback
  48. */
  49. typedef struct {
  50. cdc_line_coding_t const *p_line_coding; /*!< New line coding value */
  51. } cdcacm_event_line_coding_changed_data_t;
  52. /**
  53. * @brief Types of CDC ACM events
  54. */
  55. typedef enum {
  56. CDC_EVENT_RX,
  57. CDC_EVENT_RX_WANTED_CHAR,
  58. CDC_EVENT_LINE_STATE_CHANGED,
  59. CDC_EVENT_LINE_CODING_CHANGED
  60. } cdcacm_event_type_t;
  61. /**
  62. * @brief Describes an event passing to the input of a callbacks
  63. */
  64. typedef struct {
  65. cdcacm_event_type_t type; /*!< Event type */
  66. union {
  67. cdcacm_event_rx_wanted_char_data_t rx_wanted_char_data; /*!< Data input of the `callback_rx_wanted_char` callback */
  68. cdcacm_event_line_state_changed_data_t line_state_changed_data; /*!< Data input of the `callback_line_state_changed` callback */
  69. cdcacm_event_line_coding_changed_data_t line_coding_changed_data; /*!< Data input of the `line_coding_changed` callback */
  70. };
  71. } cdcacm_event_t;
  72. /**
  73. * @brief CDC-ACM callback type
  74. */
  75. typedef void(*tusb_cdcacm_callback_t)(int itf, cdcacm_event_t *event);
  76. /*********************************************************************** Callbacks and events*/
  77. /* Other structs
  78. ********************************************************************* */
  79. /**
  80. * @brief Configuration structure for CDC-ACM
  81. */
  82. typedef struct {
  83. tinyusb_usbdev_t usb_dev; /*!< Usb device to set up */
  84. tinyusb_cdcacm_itf_t cdc_port; /*!< CDC port */
  85. size_t rx_unread_buf_sz; /*!< Amount of data that can be passed to the AMC at once */
  86. tusb_cdcacm_callback_t callback_rx; /*!< Pointer to the function with the `tusb_cdcacm_callback_t` type that will be handled as a callback */
  87. tusb_cdcacm_callback_t callback_rx_wanted_char; /*!< Pointer to the function with the `tusb_cdcacm_callback_t` type that will be handled as a callback */
  88. tusb_cdcacm_callback_t callback_line_state_changed; /*!< Pointer to the function with the `tusb_cdcacm_callback_t` type that will be handled as a callback */
  89. tusb_cdcacm_callback_t callback_line_coding_changed; /*!< Pointer to the function with the `tusb_cdcacm_callback_t` type that will be handled as a callback */
  90. } tinyusb_config_cdcacm_t;
  91. /*********************************************************************** Other structs*/
  92. /* Public functions
  93. ********************************************************************* */
  94. /**
  95. * @brief Initialize CDC ACM. Initialization will be finished with
  96. * the `tud_cdc_line_state_cb` callback
  97. *
  98. * @param cfg - init configuration structure
  99. * @return esp_err_t
  100. */
  101. esp_err_t tusb_cdc_acm_init(const tinyusb_config_cdcacm_t *cfg);
  102. /**
  103. * @brief Register a callback invoking on CDC event. If the callback had been
  104. * already registered, it will be overwritten
  105. *
  106. * @param itf - number of a CDC object
  107. * @param event_type - type of registered event for a callback
  108. * @param callback - callback function
  109. * @return esp_err_t - ESP_OK or ESP_ERR_INVALID_ARG
  110. */
  111. esp_err_t tinyusb_cdcacm_register_callback(tinyusb_cdcacm_itf_t itf,
  112. cdcacm_event_type_t event_type,
  113. tusb_cdcacm_callback_t callback);
  114. /**
  115. * @brief Unregister a callback invoking on CDC event.
  116. *
  117. * @param itf - number of a CDC object
  118. * @param event_type - type of registered event for a callback
  119. * @return esp_err_t - ESP_OK or ESP_ERR_INVALID_ARG
  120. */
  121. esp_err_t tinyusb_cdcacm_unregister_callback(tinyusb_cdcacm_itf_t itf, cdcacm_event_type_t event_type);
  122. /**
  123. * @brief Sent one character to a write buffer
  124. *
  125. * @param itf - number of a CDC object
  126. * @param ch - character to send
  127. * @return size_t - amount of queued bytes
  128. */
  129. size_t tinyusb_cdcacm_write_queue_char(tinyusb_cdcacm_itf_t itf, char ch);
  130. /**
  131. * @brief Write data to write buffer from a byte array
  132. *
  133. * @param itf - number of a CDC object
  134. * @param in_buf - a source array
  135. * @param in_size - size to write from arr_src
  136. * @return size_t - amount of queued bytes
  137. */
  138. size_t tinyusb_cdcacm_write_queue(tinyusb_cdcacm_itf_t itf, const uint8_t *in_buf, size_t in_size);
  139. /**
  140. * @brief Send all data from a write buffer. Use `tinyusb_cdcacm_write_queue` to add data to the buffer.
  141. *
  142. * WARNING! TinyUSB can block output Endpoint for several RX callbacks, after will do additional flush
  143. * after the each trasfer. That can leads to the situation when you requested a flush, but it will fail until
  144. * ont of the next callbacks ends.
  145. * SO USING OF THE FLUSH WITH TIMEOUTS IN CALLBACKS IS NOT RECOMENDED - YOU CAN GET A LOCK FOR THE TIMEOUT
  146. *
  147. * @param itf - number of a CDC object
  148. * @param timeout_ticks - waiting until flush will be considered as failed
  149. * @return esp_err_t - ESP_OK if (timeout_ticks > 0) and and flush was successful,
  150. * ESP_ERR_TIMEOUT if timeout occurred3 or flush was successful with (timeout_ticks == 0)
  151. * ESP_FAIL if flush was unsuccessful
  152. */
  153. esp_err_t tinyusb_cdcacm_write_flush(tinyusb_cdcacm_itf_t itf, uint32_t timeout_ticks);
  154. /**
  155. * @brief Read a content to the array, and defines it's size to the sz_store
  156. *
  157. * @param itf - number of a CDC object
  158. * @param out_buf - to this array will be stored the object from a CDC buffer
  159. * @param out_buf_sz - size of buffer for results
  160. * @param rx_data_size - to this address will be stored the object's size
  161. * @return esp_err_t ESP_OK, ESP_FAIL or ESP_ERR_INVALID_STATE
  162. */
  163. esp_err_t tinyusb_cdcacm_read(tinyusb_cdcacm_itf_t itf, uint8_t *out_buf, size_t out_buf_sz, size_t *rx_data_size);
  164. /**
  165. * @brief Check if the ACM initialized
  166. *
  167. * @param itf - number of a CDC object
  168. * @return true or false
  169. */
  170. bool tusb_cdc_acm_initialized(tinyusb_cdcacm_itf_t itf);
  171. /*********************************************************************** Public functions*/
  172. #ifdef __cplusplus
  173. }
  174. #endif