tusb_cdc_acm.h 6.7 KB

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