usb_console.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include "esp_err.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * @file usb_console.h
  16. * This file contains definitions of low-level USB console functions.
  17. * These functions are not considered to be a public interface and
  18. * should not be called by applications directly.
  19. * Application interface to the USB console is provided either by
  20. * "cdcacm" VFS driver, or by the USB CDC driver in TinyUSB.
  21. */
  22. /**
  23. * RX/TX callback function type
  24. * @param arg callback-specific context pointer
  25. */
  26. typedef void (*esp_usb_console_cb_t)(void* arg);
  27. /**
  28. * Initialize USB console output using ROM USB CDC driver.
  29. * This function is called by the early startup code if USB CDC is
  30. * selected as the console output option.
  31. * @return
  32. * - ESP_OK on success
  33. * - ESP_ERR_NO_MEM
  34. * - other error codes from the interrupt allocator
  35. */
  36. esp_err_t esp_usb_console_init(void);
  37. /**
  38. * Write a buffer to USB CDC
  39. * @param buf data to write
  40. * @param size size of the data, in bytes
  41. * @return -1 on error, otherwise the number of bytes
  42. */
  43. ssize_t esp_usb_console_write_buf(const char* buf, size_t size);
  44. /**
  45. * @brief Wait until all buffered USB CDC output is written
  46. *
  47. * @return ssize_t Number of bytes written, or -1 if the driver is not initialized
  48. */
  49. ssize_t esp_usb_console_flush(void);
  50. /**
  51. * @brief Read data from USB CDC
  52. *
  53. * May read less data than requested.
  54. *
  55. * @param buf Buffer to read data into
  56. * @param buf_size Size of the buffer
  57. * @return ssize_t Number of bytes written into the buffer, or -1 if the driver is not initialized
  58. */
  59. ssize_t esp_usb_console_read_buf(char* buf, size_t buf_size);
  60. /**
  61. * @brief Get the number of bytes available for reading from USB CDC
  62. *
  63. * @return ssize_t Number of bytes available, or -1 if the driver is not initialized
  64. */
  65. ssize_t esp_usb_console_available_for_read(void);
  66. /**
  67. * @brief Check if data can be written into USB CDC
  68. *
  69. * @return true if data can be written now without blocking
  70. */
  71. bool esp_usb_console_write_available(void);
  72. /**
  73. * @brief Set RX/TX callback functions to be called from ISR
  74. *
  75. * @param rx_cb RX callback function
  76. * @param tx_cb TX callback function
  77. * @param arg callback-specific context pointer
  78. * @return ESP_OK if the callbacks were set, ESP_ERR_INVALID_STATE if the driver is not initialized
  79. */
  80. esp_err_t esp_usb_console_set_cb(esp_usb_console_cb_t rx_cb, esp_usb_console_cb_t tx_cb, void* arg);
  81. #ifdef __cplusplus
  82. }
  83. #endif