Skip to content

Commit

Permalink
refactor(usb_serial_jtag): make usb_serial_jtag as component
Browse files Browse the repository at this point in the history
  • Loading branch information
mythbuster5 committed Jan 5, 2024
1 parent 32e2101 commit 3dc76e9
Show file tree
Hide file tree
Showing 33 changed files with 244 additions and 168 deletions.
2 changes: 1 addition & 1 deletion components/console/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ idf_component_register(SRCS "commands.c"
INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
REQUIRES vfs
PRIV_REQUIRES esp_driver_uart
driver # to be replaced by esp_driver_usj
esp_driver_usb_serial_jtag
)
11 changes: 5 additions & 6 deletions components/console/esp_console_repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
#include "esp_err.h"
#include "esp_log.h"
#include "esp_console.h"
#include "esp_vfs_dev.h"
#include "esp_vfs_cdcacm.h"
#include "esp_vfs_usb_serial_jtag.h"
#include "driver/usb_serial_jtag_vfs.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
Expand Down Expand Up @@ -144,9 +143,9 @@ esp_err_t esp_console_new_repl_usb_serial_jtag(const esp_console_dev_usb_serial_
}

/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
usb_serial_jtag_vfs_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
/* Move the caret to the beginning of the next line on '\n' */
esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
usb_serial_jtag_vfs_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);

/* Enable blocking mode on stdin and stdout */
fcntl(fileno(stdout), F_SETFL, 0);
Expand All @@ -167,7 +166,7 @@ esp_err_t esp_console_new_repl_usb_serial_jtag(const esp_console_dev_usb_serial_
}

/* Tell vfs to use usb-serial-jtag driver */
esp_vfs_usb_serial_jtag_use_driver();
usb_serial_jtag_vfs_use_driver();

// setup history
ret = esp_console_setup_history(repl_config->history_save_path, repl_config->max_history_len, &usb_serial_jtag_repl->repl_com);
Expand Down Expand Up @@ -466,7 +465,7 @@ static esp_err_t esp_console_repl_usb_serial_jtag_delete(esp_console_repl_t *rep
}
repl_com->state = CONSOLE_REPL_STATE_DEINIT;
esp_console_deinit();
esp_vfs_usb_serial_jtag_use_nonblocking();
usb_serial_jtag_vfs_use_nonblocking();
usb_serial_jtag_driver_uninstall();
free(usb_serial_jtag_repl);
_exit:
Expand Down
18 changes: 2 additions & 16 deletions components/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ set(srcs)
set(includes "deprecated"
"i2c/include"
"touch_sensor/include"
"twai/include"
"usb_serial_jtag/include")
"twai/include")

# Always included linker fragments
set(ldfragments "")
Expand Down Expand Up @@ -86,12 +85,6 @@ if(CONFIG_SOC_TWAI_SUPPORTED)
list(APPEND ldfragments "twai/linker.lf")
endif()

# USB Serial JTAG related source files
if(CONFIG_SOC_USB_SERIAL_JTAG_SUPPORTED)
list(APPEND srcs "usb_serial_jtag/usb_serial_jtag.c"
"usb_serial_jtag/usb_serial_jtag_connection_monitor.c")
endif()

# Other source files
if(${target} STREQUAL "esp32")
list(APPEND srcs "deprecated/adc_i2s_deprecated.c")
Expand All @@ -113,14 +106,7 @@ else()
esp_driver_gpio esp_driver_pcnt esp_driver_gptimer esp_driver_spi esp_driver_mcpwm
esp_driver_ana_cmpr esp_driver_i2s esp_driver_sdmmc esp_driver_sdspi esp_driver_sdio
esp_driver_dac esp_driver_rmt esp_driver_tsens esp_driver_sdm esp_driver_i2c
esp_driver_uart esp_driver_ledc esp_driver_parlio
esp_driver_uart esp_driver_ledc esp_driver_parlio esp_driver_usb_serial_jtag
LDFRAGMENTS ${ldfragments}
)
endif()

# If system needs to monitor USJ connection status, then usb_serial_jtag_connection_monitor object file has to be linked
# to the binary, to allow tick hook to be registered
if(CONFIG_USJ_NO_AUTO_LS_ON_CONNECTION OR
(CONFIG_SOC_USB_SERIAL_JTAG_SUPPORT_LIGHT_SLEEP AND CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED))
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usb_serial_jtag_connection_monitor_include")
endif()
17 changes: 0 additions & 17 deletions components/driver/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,4 @@ menu "Driver Configurations"

orsource "./twai/Kconfig.twai"

menu "USB Serial/JTAG Configuration"
depends on SOC_USB_SERIAL_JTAG_SUPPORTED
config USJ_NO_AUTO_LS_ON_CONNECTION
bool "Don't enter the automatic light sleep when USB Serial/JTAG port is connected"
depends on PM_ENABLE && ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED && !SOC_USB_SERIAL_JTAG_SUPPORT_LIGHT_SLEEP
default n
help
If enabled, the chip will constantly monitor the connection status of the USB Serial/JTAG port. As long
as the USB Serial/JTAG is connected, a ESP_PM_NO_LIGHT_SLEEP power management lock will be acquired to
prevent the system from entering light sleep.
This option can be useful if serial monitoring is needed via USB Serial/JTAG while power management is
enabled, as the USB Serial/JTAG cannot work under light sleep and after waking up from light sleep.
Note. This option can only control the automatic Light-Sleep behavior. If esp_light_sleep_start() is
called manually from the program, enabling this option will not prevent light sleep entry even if the
USB Serial/JTAG is in use.
endmenu # USB Serial/JTAG Configuration

endmenu # Driver configurations
11 changes: 0 additions & 11 deletions components/driver/test_apps/.build-test-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,3 @@ components/driver/test_apps/twai:
- components/driver/twai/**/*
depends_components:
- esp_driver_gpio

components/driver/test_apps/usb_serial_jtag:
disable:
- if: SOC_USB_SERIAL_JTAG_SUPPORTED != 1
depends_filepatterns:
- components/driver/usb_serial_jtag/**/*
depends_components:
- hal
- esp_hw_support # for clock
- vfs
- esp_driver_gpio
2 changes: 1 addition & 1 deletion components/esp_driver_uart/src/uart_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ void uart_vfs_dev_use_driver(int uart_num)
_lock_release_recursive(&s_ctx[uart_num]->read_lock);
}

#if CONFIG_VFS_SUPPORT_IO && CONFIG_ESP_CONSOLE_UART
#if CONFIG_ESP_CONSOLE_UART
ESP_SYSTEM_INIT_FN(init_vfs_uart, CORE, BIT(0), 110)
{
esp_vfs_set_primary_dev_vfs_def_struct(&uart_vfs);
Expand Down
25 changes: 25 additions & 0 deletions components/esp_driver_usb_serial_jtag/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
set(srcs)
set(include "include")

# USB Serial JTAG related source files
if(CONFIG_SOC_USB_SERIAL_JTAG_SUPPORTED)
list(APPEND srcs "src/usb_serial_jtag.c"
"src/usb_serial_jtag_connection_monitor.c")
endif()

idf_component_register(SRCS ${srcs}
INCLUDE_DIRS ${include}
PRIV_REQUIRES esp_driver_gpio esp_ringbuf esp_pm esp_timer
)

if(CONFIG_VFS_SUPPORT_IO AND CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED)
target_link_libraries(${COMPONENT_LIB} PUBLIC idf::vfs)
target_sources(${COMPONENT_LIB} PRIVATE "src/usb_serial_jtag_vfs.c")
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usb_serial_jtag_vfs_include_dev_init")
endif()

# If system needs to monitor USJ connection status, then usb_serial_jtag_connection_monitor object file has to be linked
# to the binary, to allow tick hook to be registered
if(CONFIG_USJ_NO_AUTO_LS_ON_CONNECTION OR CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED)
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u usb_serial_jtag_connection_monitor_include")
endif()
16 changes: 16 additions & 0 deletions components/esp_driver_usb_serial_jtag/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
menu "ESP-Driver:USB Serial/JTAG Configuration"
depends on SOC_USB_SERIAL_JTAG_SUPPORTED
config USJ_NO_AUTO_LS_ON_CONNECTION
bool "Don't enter the automatic light sleep when USB Serial/JTAG port is connected"
depends on PM_ENABLE && ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED && !SOC_USB_SERIAL_JTAG_SUPPORT_LIGHT_SLEEP
default n
help
If enabled, the chip will constantly monitor the connection status of the USB Serial/JTAG port. As long
as the USB Serial/JTAG is connected, a ESP_PM_NO_LIGHT_SLEEP power management lock will be acquired to
prevent the system from entering light sleep.
This option can be useful if serial monitoring is needed via USB Serial/JTAG while power management is
enabled, as the USB Serial/JTAG cannot work under light sleep and after waking up from light sleep.
Note. This option can only control the automatic Light-Sleep behavior. If esp_light_sleep_start() is
called manually from the program, enabling this option will not prevent light sleep entry even if the
USB Serial/JTAG is in use.
endmenu # USB Serial/JTAG Configuration
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "esp_err.h"
#include "esp_vfs.h"
#include "esp_vfs_common.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief add /dev/usbserjtag virtual filesystem driver
*
* This function is called from startup code to enable console output
*/
esp_err_t usb_serial_jtag_vfs_register(void);

/**
* @brief Set the line endings expected to be received
*
* This specifies the conversion between line endings received and
* newlines ('\n', LF) passed into stdin:
*
* - ESP_LINE_ENDINGS_CRLF: convert CRLF to LF
* - ESP_LINE_ENDINGS_CR: convert CR to LF
* - ESP_LINE_ENDINGS_LF: no modification
*
* @note this function is not thread safe w.r.t. reading
*
* @param mode line endings expected
*/
void usb_serial_jtag_vfs_set_rx_line_endings(esp_line_endings_t mode);

/**
* @brief Set the line endings to sent
*
* This specifies the conversion between newlines ('\n', LF) on stdout and line
* endings sent:
*
* - ESP_LINE_ENDINGS_CRLF: convert LF to CRLF
* - ESP_LINE_ENDINGS_CR: convert LF to CR
* - ESP_LINE_ENDINGS_LF: no modification
*
* @note this function is not thread safe w.r.t. writing
*
* @param mode line endings to send
*/
void usb_serial_jtag_vfs_set_tx_line_endings(esp_line_endings_t mode);

/**
* @brief set VFS to use USB-SERIAL-JTAG driver for reading and writing
* @note application must configure USB-SERIAL-JTAG driver before calling these functions
* With these functions, read and write are blocking and interrupt-driven.
*/
void usb_serial_jtag_vfs_use_driver(void);

/**
* @brief set VFS to use simple functions for reading and writing UART
* Read is non-blocking, write is busy waiting until TX FIFO has enough space.
* These functions are used by default.
*/
void usb_serial_jtag_vfs_use_nonblocking(void);

#ifdef __cplusplus
}
#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -35,7 +35,7 @@ typedef enum {
#define USB_SER_JTAG_ENDP_SIZE (64)
#define USB_SER_JTAG_RX_MAX_SIZE (64)

typedef struct{
typedef struct {
intr_handle_t intr_handle; /*!< USB-SERIAL-JTAG interrupt handler */
portMUX_TYPE spinlock; /*!< Spinlock for usb_serial_jtag */
_Atomic fifo_status_t fifo_status; /*!< Record the status of fifo */
Expand Down Expand Up @@ -63,7 +63,8 @@ static size_t usb_serial_jtag_write_and_flush(const uint8_t *buf, uint32_t wr_le
return size;
}

static void usb_serial_jtag_isr_handler_default(void *arg) {
static void usb_serial_jtag_isr_handler_default(void *arg)
{
BaseType_t xTaskWoken = 0;
uint32_t usbjtag_intr_status = 0;
usbjtag_intr_status = usb_serial_jtag_ll_get_intsts_mask();
Expand Down Expand Up @@ -154,7 +155,7 @@ esp_err_t usb_serial_jtag_driver_install(usb_serial_jtag_driver_config_t *usb_se
ESP_RETURN_ON_FALSE((usb_serial_jtag_config->rx_buffer_size > 0), ESP_ERR_INVALID_ARG, USB_SERIAL_JTAG_TAG, "RX buffer is not prepared");
ESP_RETURN_ON_FALSE((usb_serial_jtag_config->rx_buffer_size > USB_SER_JTAG_RX_MAX_SIZE), ESP_ERR_INVALID_ARG, USB_SERIAL_JTAG_TAG, "RX buffer prepared is so small, should larger than 64");
ESP_RETURN_ON_FALSE((usb_serial_jtag_config->tx_buffer_size > 0), ESP_ERR_INVALID_ARG, USB_SERIAL_JTAG_TAG, "TX buffer is not prepared");
p_usb_serial_jtag_obj = (usb_serial_jtag_obj_t*) heap_caps_calloc(1, sizeof(usb_serial_jtag_obj_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
p_usb_serial_jtag_obj = (usb_serial_jtag_obj_t*) heap_caps_calloc(1, sizeof(usb_serial_jtag_obj_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
p_usb_serial_jtag_obj->rx_buf_size = usb_serial_jtag_config->rx_buffer_size;
p_usb_serial_jtag_obj->tx_buf_size = usb_serial_jtag_config->tx_buffer_size;
p_usb_serial_jtag_obj->tx_stash_cnt = 0;
Expand Down Expand Up @@ -188,9 +189,9 @@ esp_err_t usb_serial_jtag_driver_install(usb_serial_jtag_driver_config_t *usb_se
// Configure PHY
usb_fsls_phy_ll_int_jtag_enable(&USB_SERIAL_JTAG);

usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY|
USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY|
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY |
USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY |
USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);

err = esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0, usb_serial_jtag_isr_handler_default, NULL, &p_usb_serial_jtag_obj->intr_handle);
Expand Down Expand Up @@ -245,7 +246,7 @@ int usb_serial_jtag_write_bytes(const void* src, size_t size, TickType_t ticks_t

// Blocking method, Sending data to ringbuffer, and handle the data in ISR.
if (size - sent_data > 0) {
result = xRingbufferSend(p_usb_serial_jtag_obj->tx_ring_buf, (void*) (buff+sent_data), size-sent_data, ticks_to_wait);
result = xRingbufferSend(p_usb_serial_jtag_obj->tx_ring_buf, (void*)(buff + sent_data), size - sent_data, ticks_to_wait);
} else {
atomic_store(&p_usb_serial_jtag_obj->fifo_status, FIFO_IDLE);
}
Expand All @@ -255,7 +256,7 @@ int usb_serial_jtag_write_bytes(const void* src, size_t size, TickType_t ticks_t

esp_err_t usb_serial_jtag_driver_uninstall(void)
{
if(p_usb_serial_jtag_obj == NULL) {
if (p_usb_serial_jtag_obj == NULL) {
ESP_LOGI(USB_SERIAL_JTAG_TAG, "ALREADY NULL");
return ESP_OK;
}
Expand All @@ -265,11 +266,11 @@ esp_err_t usb_serial_jtag_driver_uninstall(void)
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
esp_intr_free(p_usb_serial_jtag_obj->intr_handle);

if(p_usb_serial_jtag_obj->rx_ring_buf) {
if (p_usb_serial_jtag_obj->rx_ring_buf) {
vRingbufferDelete(p_usb_serial_jtag_obj->rx_ring_buf);
p_usb_serial_jtag_obj->rx_ring_buf = NULL;
}
if(p_usb_serial_jtag_obj->tx_ring_buf) {
if (p_usb_serial_jtag_obj->tx_ring_buf) {
vRingbufferDelete(p_usb_serial_jtag_obj->tx_ring_buf);
p_usb_serial_jtag_obj->tx_ring_buf = NULL;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -29,7 +29,6 @@ static uint32_t remaining_allowed_no_sof_ticks;

static __attribute__((unused)) const char *USB_SERIAL_JTAG_CONN_MONITOR_TAG = "usb_serial_jtag";


bool usb_serial_jtag_is_connected(void)
{
return s_usb_serial_jtag_conn_status;
Expand Down Expand Up @@ -76,7 +75,7 @@ ESP_SYSTEM_INIT_FN(usb_serial_jtag_conn_status_init, SECONDARY, BIT(0), 230)
{
#if CONFIG_USJ_NO_AUTO_LS_ON_CONNECTION
ESP_RETURN_ON_ERROR(esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "usb_serial_jtag", &s_usb_serial_jtag_pm_lock),
USB_SERIAL_JTAG_CONN_MONITOR_TAG, "create NO_LIGHT_SLEEP lock failed");
USB_SERIAL_JTAG_CONN_MONITOR_TAG, "create NO_LIGHT_SLEEP lock failed");
// We always assume it is connected at first, so acquires the lock to avoid auto light sleep
esp_pm_lock_acquire(s_usb_serial_jtag_pm_lock);
#endif
Expand Down
Loading

0 comments on commit 3dc76e9

Please sign in to comment.