Skip to content

Commit 541fe12

Browse files
committed
feat: Add the ability for ESP ports to not initialize peripherals
This adds the ability of the ESP serial and SPI ports to not initialize the peripheral. This enables using the peripherals for multiple devices or multiple uses for one device (e.g. flashing and monitoring). The boolean flag added to config structures will be false by default so no changes are required for existing code.
1 parent c4161d4 commit 541fe12

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

port/esp32_port.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static int64_t s_time_end;
4141
static int32_t s_uart_port;
4242
static int32_t s_reset_trigger_pin;
4343
static int32_t s_gpio0_trigger_pin;
44+
static bool s_peripheral_needs_deinit;
4445

4546
esp_loader_error_t loader_port_esp32_init(const loader_esp32_config_t *config)
4647
{
@@ -49,30 +50,34 @@ esp_loader_error_t loader_port_esp32_init(const loader_esp32_config_t *config)
4950
s_gpio0_trigger_pin = config->gpio0_trigger_pin;
5051

5152
// Initialize UART
52-
uart_config_t uart_config = {
53-
.baud_rate = config->baud_rate,
54-
.data_bits = UART_DATA_8_BITS,
55-
.parity = UART_PARITY_DISABLE,
56-
.stop_bits = UART_STOP_BITS_1,
57-
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
53+
if (!config->dont_initialize_peripheral) {
54+
uart_config_t uart_config = {
55+
.baud_rate = config->baud_rate,
56+
.data_bits = UART_DATA_8_BITS,
57+
.parity = UART_PARITY_DISABLE,
58+
.stop_bits = UART_STOP_BITS_1,
59+
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
5860
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
59-
.source_clk = UART_SCLK_DEFAULT,
61+
.source_clk = UART_SCLK_DEFAULT,
6062
#endif
61-
};
62-
63-
int rx_buffer_size = config->rx_buffer_size ? config->rx_buffer_size : 400;
64-
int tx_buffer_size = config->tx_buffer_size ? config->tx_buffer_size : 400;
65-
QueueHandle_t *uart_queue = config->uart_queue ? config->uart_queue : NULL;
66-
int queue_size = config->queue_size ? config->queue_size : 0;
67-
68-
if ( uart_param_config(s_uart_port, &uart_config) != ESP_OK ) {
69-
return ESP_LOADER_ERROR_FAIL;
70-
}
71-
if ( uart_set_pin(s_uart_port, config->uart_tx_pin, config->uart_rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE) != ESP_OK ) {
72-
return ESP_LOADER_ERROR_FAIL;
73-
}
74-
if ( uart_driver_install(s_uart_port, rx_buffer_size, tx_buffer_size, queue_size, uart_queue, 0) != ESP_OK ) {
75-
return ESP_LOADER_ERROR_FAIL;
63+
};
64+
65+
int rx_buffer_size = config->rx_buffer_size ? config->rx_buffer_size : 400;
66+
int tx_buffer_size = config->tx_buffer_size ? config->tx_buffer_size : 400;
67+
QueueHandle_t *uart_queue = config->uart_queue ? config->uart_queue : NULL;
68+
int queue_size = config->queue_size ? config->queue_size : 0;
69+
70+
if ( uart_param_config(s_uart_port, &uart_config) != ESP_OK ) {
71+
return ESP_LOADER_ERROR_FAIL;
72+
}
73+
if ( uart_set_pin(s_uart_port, config->uart_tx_pin, config->uart_rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE) != ESP_OK ) {
74+
return ESP_LOADER_ERROR_FAIL;
75+
}
76+
if ( uart_driver_install(s_uart_port, rx_buffer_size, tx_buffer_size, queue_size, uart_queue, 0) != ESP_OK ) {
77+
return ESP_LOADER_ERROR_FAIL;
78+
}
79+
80+
s_peripheral_needs_deinit = true;
7681
}
7782

7883
// Initialize boot pin selection pins
@@ -89,7 +94,9 @@ esp_loader_error_t loader_port_esp32_init(const loader_esp32_config_t *config)
8994

9095
void loader_port_esp32_deinit(void)
9196
{
92-
uart_driver_delete(s_uart_port);
97+
if (s_peripheral_needs_deinit) {
98+
uart_driver_delete(s_uart_port);
99+
}
93100
}
94101

95102

port/esp32_port.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ typedef struct {
3535
uint32_t queue_size; /*!< Set to zero for default UART queue size */
3636
QueueHandle_t *uart_queue; /*!< Set to NULL, if UART queue handle is not
3737
necessary. Otherwise, it will be assigned here */
38+
bool dont_initialize_peripheral; /* Use if the peripheral has already been initialized,
39+
useful when using the peripheral for multiple
40+
purposes (e.g. monitoring) */
3841
} loader_esp32_config_t;
3942

4043
/**

port/esp32_spi_port.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static uint32_t s_strap_bit1_pin;
7070
static uint32_t s_strap_bit2_pin;
7171
static uint32_t s_strap_bit3_pin;
7272
static uint32_t s_spi_cs_pin;
73+
static bool s_bus_needs_deinit;
7374

7475
esp_loader_error_t loader_port_esp32_spi_init(const loader_esp32_spi_config_t *config)
7576
{
@@ -83,15 +84,19 @@ esp_loader_error_t loader_port_esp32_spi_init(const loader_esp32_spi_config_t *c
8384
s_spi_cs_pin = config->spi_cs_pin;
8485

8586
/* Configure and initialize the SPI bus*/
86-
s_spi_config.mosi_io_num = config->spi_mosi_pin;
87-
s_spi_config.miso_io_num = config->spi_miso_pin;
88-
s_spi_config.sclk_io_num = config->spi_clk_pin;
89-
s_spi_config.quadwp_io_num = config->spi_quadwp_pin;
90-
s_spi_config.quadhd_io_num = config->spi_quadhd_pin;
91-
s_spi_config.max_transfer_sz = 4096 * 4;
92-
93-
if (spi_bus_initialize(s_spi_bus, &s_spi_config, DMA_CHAN) != ESP_OK) {
94-
return ESP_LOADER_ERROR_FAIL;
87+
if (!config->dont_initialize_bus) {
88+
s_spi_config.mosi_io_num = config->spi_mosi_pin;
89+
s_spi_config.miso_io_num = config->spi_miso_pin;
90+
s_spi_config.sclk_io_num = config->spi_clk_pin;
91+
s_spi_config.quadwp_io_num = config->spi_quadwp_pin;
92+
s_spi_config.quadhd_io_num = config->spi_quadhd_pin;
93+
s_spi_config.max_transfer_sz = 4096 * 4;
94+
95+
if (spi_bus_initialize(s_spi_bus, &s_spi_config, DMA_CHAN) != ESP_OK) {
96+
return ESP_LOADER_ERROR_FAIL;
97+
}
98+
99+
s_bus_needs_deinit = true;
95100
}
96101

97102
/* Configure and add the device */
@@ -125,7 +130,9 @@ void loader_port_esp32_spi_deinit(void)
125130
gpio_reset_pin(s_reset_trigger_pin);
126131
gpio_reset_pin(s_spi_cs_pin);
127132
spi_bus_remove_device(s_device_h);
128-
spi_bus_free(s_spi_bus);
133+
if (s_bus_needs_deinit) {
134+
spi_bus_free(s_spi_bus);
135+
}
129136
}
130137

131138

port/esp32_spi_port.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ typedef struct {
3636
uint32_t strap_bit1_pin;
3737
uint32_t strap_bit2_pin;
3838
uint32_t strap_bit3_pin;
39+
bool dont_initialize_bus; /* Use if the bus has already been initialized,
40+
useful when sharing the bus with other devices. */
3941
} loader_esp32_spi_config_t;
4042

4143
/**

0 commit comments

Comments
 (0)