tusb_cdc_acm.h 6.7 KB

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