Files
esp-idf/components/esp_system/include/esp_private/usb_console.h
Guillaume Souchere ae7c8691d9 fix(esp_vfs_console): USB CDC read when non blocking
In non blocking mode, the read function is expected
to return weather data is available for reading or not.

In case data are available but the size does not match
the expected size, the function read should return whatever
data is available.

Previously, the function was returning -1 with errno set
to EWOULDBLOCK even if the size of data in the buffer was
less than the requested size. It would only return the
available data if the size in the buffer was greater or equal
to the requested size.

The implementation of cdcacm_read is modified to return the avilable
data from the buffer even is the size is lesser than the requested
size.
2025-01-30 08:51:54 +01:00

115 lines
3.0 KiB
C

/*
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @file usb_console.h
* This file contains definitions of low-level USB console functions.
* These functions are not considered to be a public interface and
* should not be called by applications directly.
* Application interface to the USB console is provided either by
* "cdcacm" VFS driver, or by the USB CDC driver in TinyUSB.
*/
/**
* RX/TX callback function type
* @param arg callback-specific context pointer
*/
typedef void (*esp_usb_console_cb_t)(void* arg);
/**
* Initialize USB console output using ROM USB CDC driver.
* This function is called by the early startup code if USB CDC is
* selected as the console output option.
* @return
* - ESP_OK on success
* - ESP_ERR_NO_MEM
* - other error codes from the interrupt allocator
*/
esp_err_t esp_usb_console_init(void);
/**
* Write a buffer to USB CDC
* @param buf data to write
* @param size size of the data, in bytes
* @return -1 on error, otherwise the number of bytes
*/
ssize_t esp_usb_console_write_buf(const char* buf, size_t size);
/**
* @brief Wait until all buffered USB CDC output is written
*
* @return ssize_t Number of bytes written, or -1 if the driver is not initialized
*/
ssize_t esp_usb_console_flush(void);
/**
* @brief Read data from USB CDC
*
* May read less data than requested.
*
* @param buf Buffer to read data into
* @param buf_size Size of the buffer
* @return ssize_t Number of bytes written into the buffer, or -1 if the driver is not initialized
*/
ssize_t esp_usb_console_read_buf(char* buf, size_t buf_size);
/**
* @brief Get the number of bytes available for reading from USB CDC
*
* @return ssize_t Number of bytes available, or -1 if the driver is not initialized
*/
ssize_t esp_usb_console_available_for_read(void);
/**
* @brief Check if data can be written into USB CDC
*
* @return true if data can be written now without blocking
*/
bool esp_usb_console_write_available(void);
/**
* @brief Set RX/TX callback functions to be called from ISR
*
* @param rx_cb RX callback function
* @param tx_cb TX callback function
* @param arg callback-specific context pointer
* @return ESP_OK if the callbacks were set, ESP_ERR_INVALID_STATE if the driver is not initialized
*/
esp_err_t esp_usb_console_set_cb(esp_usb_console_cb_t rx_cb, esp_usb_console_cb_t tx_cb, void* arg);
/**
* @brief Checks whether the USB console is installed or not
*
* @return
* - true USB console is installed
* - false USB console is not installed
*/
bool esp_usb_console_is_installed(void);
/**
* @brief Call the USB interrupt handler while any interrupts
* are pending, but not more than a few times. Non-static to
* allow placement into IRAM by ldgen.
*
*/
void esp_usb_console_poll_interrupts(void); // [refactor-todo] Remove when implementing IDF-12175
#ifdef __cplusplus
}
#endif