mirror of
				https://github.com/espressif/esp-idf.git
				synced 2025-10-31 04:59:55 +00:00 
			
		
		
		
	bootloader: combine console code for ESP32 and S2, add USB support
This commit is contained in:
		| @@ -0,0 +1,89 @@ | ||||
| // Copyright 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. | ||||
|  | ||||
| /** | ||||
|  * This file is contains console-related functions which should be located in iram_loader_seg, | ||||
|  * to be available in the "loader" phase, when iram_seg may be overwritten. | ||||
|  */ | ||||
| #include <stdint.h> | ||||
| #include <stddef.h> | ||||
| #include "sdkconfig.h" | ||||
| #include "bootloader_console.h" | ||||
| #ifdef CONFIG_IDF_TARGET_ESP32 | ||||
| #include "esp32/rom/ets_sys.h" | ||||
| #include "esp32/rom/uart.h" | ||||
| #elif CONFIG_IDF_TARGET_ESP32S2 | ||||
| #include "esp32s2/rom/ets_sys.h" | ||||
| #include "esp32s2/rom/uart.h" | ||||
| #include "esp32s2/rom/usb/chip_usb_dw_wrapper.h" | ||||
| #include "esp32s2/rom/usb/usb_dc.h" | ||||
| #include "esp32s2/rom/usb/cdc_acm.h" | ||||
| #include "esp32s2/rom/usb/usb_persist.h" | ||||
| #endif | ||||
|  | ||||
| #ifdef CONFIG_ESP_CONSOLE_USB_CDC | ||||
| /* The following functions replace ets_write_char_uart, uart_tx_one_char, | ||||
|  * and uart_tx_one_char_uart ROM functions. The main difference is that | ||||
|  * uart_tx_one_char_uart calls cdc_acm_fifo_fill for each byte passed to it, | ||||
|  * which results in very slow console output. The version here uses a TX buffer. | ||||
|  * It also doesn't handle UART output, only works with USB. | ||||
|  */ | ||||
| static char cdc_txbuf[ACM_BYTES_PER_TX]; | ||||
| static size_t cdc_txpos; | ||||
|  | ||||
| static void bootloader_console_flush_usb(void) | ||||
| { | ||||
|     cdc_acm_fifo_fill(uart_acm_dev, (const uint8_t *) cdc_txbuf, cdc_txpos); | ||||
|     /* return value ignored — if bootloader fails to log something, proceed anyway */ | ||||
|     cdc_txpos = 0; | ||||
| } | ||||
|  | ||||
| static void bootloader_console_write_one_char_usb(char ch) | ||||
| { | ||||
|     cdc_txbuf[cdc_txpos++] = ch; | ||||
|     if (ch == '\n' || cdc_txpos == sizeof(cdc_txbuf)) { | ||||
|         bootloader_console_flush_usb(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| void bootloader_console_write_char_usb(char c) | ||||
| { | ||||
|     if (c == '\n') { | ||||
|         bootloader_console_write_one_char_usb('\r'); | ||||
|         bootloader_console_write_one_char_usb('\n'); | ||||
|     } else if (c == '\r') { | ||||
|     } else { | ||||
|         bootloader_console_write_one_char_usb(c); | ||||
|     } | ||||
| } | ||||
| #endif //CONFIG_ESP_CONSOLE_USB_CDC | ||||
|  | ||||
| void bootloader_console_deinit(void) | ||||
| { | ||||
| #ifdef CONFIG_ESP_CONSOLE_UART | ||||
|     /* Ensure any buffered log output is displayed */ | ||||
|     uart_tx_flush(CONFIG_ESP_CONSOLE_UART_NUM); | ||||
| #endif // CONFIG_ESP_CONSOLE_UART | ||||
|  | ||||
| #ifdef CONFIG_ESP_CONSOLE_USB_CDC | ||||
|     bootloader_console_flush_usb(); | ||||
|     usb_dc_prepare_persist(); | ||||
|     chip_usb_set_persist_flags(USBDC_PERSIST_ENA); | ||||
|     ets_delay_us(100); | ||||
|     for (int i = 0; i < 10; i++) { | ||||
|         usb_dc_check_poll_for_interrupts(); | ||||
|     } | ||||
|     ets_install_putc1(NULL); | ||||
| #endif | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Ivan Grokhotkov
					Ivan Grokhotkov