Skip to content

Commit

Permalink
Merge branch 'bugfix/esp32s3_usb_otg_console_v5.1' into 'release/v5.1'
Browse files Browse the repository at this point in the history
system: support USB_OTG CDC console on ESP32-S3 (v5.1)

See merge request espressif/esp-idf!24337
  • Loading branch information
jack0c committed Nov 24, 2023
2 parents 9e86352 + 6481b01 commit 3ba5771
Show file tree
Hide file tree
Showing 45 changed files with 300 additions and 898 deletions.
2 changes: 1 addition & 1 deletion components/bootloader_support/src/bootloader_console.c
Expand Up @@ -98,7 +98,7 @@ void bootloader_console_init(void)
#endif

esp_rom_uart_usb_acm_init(s_usb_cdc_buf, sizeof(s_usb_cdc_buf));
esp_rom_uart_set_as_console(ESP_ROM_UART_USB);
esp_rom_uart_set_as_console(ESP_ROM_USB_OTG_NUM);
esp_rom_install_channel_putc(1, bootloader_console_write_char_usb);
#if SOC_USB_SERIAL_JTAG_SUPPORTED
usb_phy_ll_usb_wrap_pad_enable(&USB_WRAP, true);
Expand Down
7 changes: 2 additions & 5 deletions components/console/esp_console_repl.c
Expand Up @@ -81,7 +81,7 @@ esp_err_t esp_console_new_repl_usb_cdc(const esp_console_dev_usb_cdc_config_t *d
/* Move the caret to the beginning of the next line on '\n' */
esp_vfs_dev_cdcacm_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);

/* Enable non-blocking mode on stdin and stdout */
/* Enable blocking mode on stdin and stdout */
fcntl(fileno(stdout), F_SETFL, 0);
fcntl(fileno(stdin), F_SETFL, 0);

Expand Down Expand Up @@ -142,15 +142,12 @@ esp_err_t esp_console_new_repl_usb_serial_jtag(const esp_console_dev_usb_serial_
goto _exit;
}

/* Disable buffering on stdin */
setvbuf(stdin, NULL, _IONBF, 0);

/* 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);
/* 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);

/* Enable non-blocking mode on stdin and stdout */
/* Enable blocking mode on stdin and stdout */
fcntl(fileno(stdout), F_SETFL, 0);
fcntl(fileno(stdin), F_SETFL, 0);

Expand Down
2 changes: 1 addition & 1 deletion components/esp_rom/CMakeLists.txt
Expand Up @@ -288,5 +288,5 @@ else() # Regular app build
endif()

if(target STREQUAL "esp32s2")
target_sources(${COMPONENT_LIB} PRIVATE "esp32s2/usb_descriptors.c")
target_sources(${COMPONENT_LIB} PRIVATE "esp32s2/usb_patches.c")
endif()
4 changes: 4 additions & 0 deletions components/esp_rom/esp32s2/Kconfig.soc_caps.in
Expand Up @@ -30,3 +30,7 @@ config ESP_ROM_HAS_NEWLIB_NANO_FORMAT
config ESP_ROM_HAS_FLASH_COUNT_PAGES_BUG
bool
default y

config ESP_ROM_USB_OTG_NUM
int
default 3
1 change: 1 addition & 0 deletions components/esp_rom/esp32s2/esp_rom_caps.h
Expand Up @@ -13,3 +13,4 @@
#define ESP_ROM_HAS_REGI2C_BUG (1) // ROM has the regi2c bug
#define ESP_ROM_HAS_NEWLIB_NANO_FORMAT (1) // ROM has the newlib nano version of formatting functions
#define ESP_ROM_HAS_FLASH_COUNT_PAGES_BUG (1) // ROM api Cache_Count_Flash_Pages will return unexpected value
#define ESP_ROM_USB_OTG_NUM (3) // The serial port ID (UART, USB, ...) of USB_OTG CDC in the ROM.
Expand Up @@ -65,3 +65,19 @@ void rom_usb_cdc_set_descriptor_patch(void)
/* Override the pointer to descriptors structure */
rom_usb_curr_desc = &s_acm_usb_descriptors_override;
}

/* On ESP32-S2, ROM doesn't provide interfaces to clear usb_dev and usb_dw_ctrl structures.
* Starting from ESP32-S3, usb_dev_deinit and usb_dw_ctrl_deinit ROM functions are available.
* Here we implement the missing functionality for the ESP32-S2.
*/
void usb_dev_deinit(void)
{
extern char rom_usb_dev, rom_usb_dev_end;
memset((void *) &rom_usb_dev, 0, &rom_usb_dev_end - &rom_usb_dev);
}

void usb_dw_ctrl_deinit(void)
{
extern char rom_usb_dw_ctrl, rom_usb_dw_ctrl_end;
memset((void *) &rom_usb_dw_ctrl, 0, &rom_usb_dw_ctrl_end - &rom_usb_dw_ctrl);
}
4 changes: 4 additions & 0 deletions components/esp_rom/esp32s3/Kconfig.soc_caps.in
Expand Up @@ -27,6 +27,10 @@ config ESP_ROM_HAS_RETARGETABLE_LOCKING
bool
default y

config ESP_ROM_USB_OTG_NUM
int
default 3

config ESP_ROM_USB_SERIAL_DEVICE_NUM
int
default 4
Expand Down
1 change: 1 addition & 0 deletions components/esp_rom/esp32s3/esp_rom_caps.h
Expand Up @@ -12,6 +12,7 @@
#define ESP_ROM_HAS_JPEG_DECODE (1) // ROM has JPEG decode library
#define ESP_ROM_UART_CLK_IS_XTAL (1) // UART clock source is selected to XTAL in ROM
#define ESP_ROM_HAS_RETARGETABLE_LOCKING (1) // ROM was built with retargetable locking
#define ESP_ROM_USB_OTG_NUM (3) // The serial port ID (UART, USB, ...) of USB_OTG CDC in the ROM.
#define ESP_ROM_USB_SERIAL_DEVICE_NUM (4) // The serial port ID (UART, USB, ...) of USB_SERIAL_JTAG in the ROM.
#define ESP_ROM_HAS_ERASE_0_REGION_BUG (1) // ROM has esp_flash_erase_region(size=0) bug
#define ESP_ROM_GET_CLK_FREQ (1) // Get clk frequency with rom function `ets_get_cpu_frequency`
Expand Down
2 changes: 1 addition & 1 deletion components/esp_rom/esp32s3/ld/esp32s3.rom.ld
Expand Up @@ -792,7 +792,7 @@ usb_dfu_force_detach = 0x40002a54;
usb_dev_deinit = 0x40002a60;
usb_dw_ctrl_deinit = 0x40002a6c;
/* Data (.data, .bss, .rodata) */
s_usb_osglue = 0x3fceffac;
rom_usb_osglue = 0x3fceffac;


/***************************************
Expand Down
36 changes: 5 additions & 31 deletions components/esp_rom/include/esp32s2/rom/usb/cdc_acm.h
@@ -1,34 +1,8 @@
/*******************************************************************************
*
* Copyright(c) 2015,2016 Intel Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
/*
* SPDX-FileCopyrightText: 2015, 2016 Intel Corporation.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#pragma once
#include <stdint.h>
Expand Down
18 changes: 5 additions & 13 deletions components/esp_rom/include/esp32s2/rom/usb/chip_usb_dw_wrapper.h
@@ -1,16 +1,8 @@
// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once
#include <stdint.h>
Expand Down
18 changes: 5 additions & 13 deletions components/esp_rom/include/esp32s2/rom/usb/cpio.h
@@ -1,16 +1,8 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/


/**
Expand Down
4 changes: 1 addition & 3 deletions components/esp_rom/include/esp32s2/rom/usb/usb_cdc.h
@@ -1,7 +1,5 @@
/* usb_cdc.h - USB CDC-ACM and CDC-ECM public header */

/*
* Copyright (c) 2017 PHYTEC Messtechnik GmbH
* SPDX-FileCopyrightText: 2017 PHYTEC Messtechnik GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down
37 changes: 5 additions & 32 deletions components/esp_rom/include/esp32s2/rom/usb/usb_common.h
@@ -1,36 +1,9 @@
/***************************************************************************
/*
* SPDX-FileCopyrightText: 2015,2016 Intel Corporation
* SPDX-FileContributor: 2017 PHYTEC Messtechnik GmbH
*
*
* Copyright(c) 2015,2016 Intel Corporation.
* Copyright(c) 2017 PHYTEC Messtechnik GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
***************************************************************************/
* SPDX-License-Identifier: BSD-3-Clause
*/

/**
* @file
Expand Down
38 changes: 30 additions & 8 deletions components/esp_rom/include/esp32s2/rom/usb/usb_dc.h
@@ -1,7 +1,5 @@
/* usb_dc.h - USB device controller driver interface */

/*
* Copyright (c) 2016 Intel Corporation.
* SPDX-FileCopyrightText: 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -372,19 +370,43 @@ int usb_dc_ep_read_continue(uint8_t ep);
int usb_dc_ep_mps(uint8_t ep);



//Hack - fake interrupts by pollinfg
/**
* @brief Poll for interrupts that need to be handled
*
* When the USB interrupt is not hooked up to an actual CPU interrupt, you
* can call this periodically to handle the USB events that need handling.
*/
void usb_dc_check_poll_for_interrupts(void);


//Prepare for USB persist. You should reboot after this.
/*
* @brief Prepare for USB persist
*
* This takes the USB peripheral offline in such a way that it seems 'just busy' to the
* host. This way, the chip can reboot (e.g. into bootloader mode) and pick up the USB
* configuration again, without the conenction to the host being interrupted.
*
* @note Actual persistence is depending on USBDC_PERSIST_ENA being set in flags, as this
* is also used to e.g. reboot into DFU mode.
*
* @note Please reboot soon after calling this.
*/
int usb_dc_prepare_persist(void);


/*
* @brief USB interrupt handler
*
* This can be hooked up by the OS to the USB peripheral interrupt.
*/
void usb_dw_isr_handler(void);

/**
* @brief Provide IDF with an interface to clear the static variable usb_dw_ctrl
*
*
*/
void usb_dw_ctrl_deinit(void);

int usb_dc_ep_write_would_block(const uint8_t ep);


#ifdef __cplusplus
Expand Down
18 changes: 5 additions & 13 deletions components/esp_rom/include/esp32s2/rom/usb/usb_descriptor.h
@@ -1,16 +1,8 @@
// Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

Expand Down
37 changes: 12 additions & 25 deletions components/esp_rom/include/esp32s2/rom/usb/usb_device.h
@@ -1,29 +1,10 @@
/*
* LPCUSB, an USB device driver for LPC microcontrollers
* Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl)
* Copyright (c) 2016 Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* SPDX-FileCopyrightText: 2006 Bertrik Sikken (bertrik@sikken.nl)
* SPDX-FileContributor: 2016 Intel Corporation
*
* SPDX-License-Identifier: BSD-3-Clause
*
* LPCUSB, an USB device driver for LPC microcontrollers
*/

/**
Expand Down Expand Up @@ -392,6 +373,12 @@ int usb_transfer_sync(uint8_t ep, uint8_t *data, size_t dlen, unsigned int flags
*/
void usb_cancel_transfer(uint8_t ep);

/**
* @brief Provide IDF with an interface to clear the static variable usb_dev
*
*
*/
void usb_dev_deinit(void);

void usb_dev_resume(int configuration);
int usb_dev_get_configuration(void);
Expand Down

0 comments on commit 3ba5771

Please sign in to comment.