Skip to content

Commit 4680b97

Browse files
committed
Merge branch 'feat/spi_p4_eco5_support' into 'master'
feat(driver_spi): update p4 eco5 spi and twai support Closes IDF-13509, IDF-13434, and IDF-13726 See merge request espressif/esp-idf!41676
2 parents b789198 + 7cb69a8 commit 4680b97

File tree

9 files changed

+39
-2446
lines changed

9 files changed

+39
-2446
lines changed

components/esp_driver_spi/src/gpspi/spi_common.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,8 @@ esp_err_t spicommon_bus_initialize_io(spi_host_device_t host, const spi_bus_conf
614614
}
615615
//set flags for DUAL mode according to output-capability of MOSI and MISO pins.
616616
if ((bus_config->mosi_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->mosi_io_num)) &&
617-
(bus_config->miso_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->miso_io_num))) {
617+
(bus_config->miso_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(bus_config->miso_io_num)) &&
618+
(bus_config->miso_io_num != bus_config->mosi_io_num)) {
618619
temp_flag |= SPICOMMON_BUSFLAG_DUAL;
619620
}
620621

components/esp_driver_spi/src/gpspi/spi_master.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,7 @@ esp_err_t spi_bus_remove_device(spi_device_handle_t handle)
633633

634634
esp_err_t spi_device_get_actual_freq(spi_device_handle_t handle, int* freq_khz)
635635
{
636-
if ((spi_device_t *)handle == NULL || freq_khz == NULL) {
637-
return ESP_ERR_INVALID_ARG;
638-
}
636+
SPI_CHECK(handle && freq_khz, "invalid arg", ESP_ERR_INVALID_ARG);
639637

640638
*freq_khz = handle->hal_dev.timing_conf.real_freq / 1000;
641639
return ESP_OK;

components/esp_driver_spi/src/gpspi/spi_slave.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,26 @@ static inline bool SPI_SLAVE_ISR_ATTR bus_is_iomux(spi_slave_t *host)
104104
return host->flags & SPICOMMON_BUSFLAG_IOMUX_PINS;
105105
}
106106

107-
static void SPI_SLAVE_ISR_ATTR freeze_cs(spi_slave_t *host)
107+
static inline void SPI_SLAVE_ISR_ATTR freeze_cs(spi_slave_t *host)
108108
{
109+
#if SPI_LL_SLAVE_NEEDS_CS_WORKAROUND
110+
// This workaround only for ESP32 due to old hardware design, see MR !3207
109111
esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ONE_INPUT, host->cs_in_signal, false);
112+
#endif
110113
}
111114

112115
// Use this function instead of cs_initial to avoid overwrite the output config
113116
// This is used in test by internal gpio matrix connections
114117
static inline void SPI_SLAVE_ISR_ATTR restore_cs(spi_slave_t *host)
115118
{
119+
#if SPI_LL_SLAVE_NEEDS_CS_WORKAROUND
120+
// This workaround only for ESP32 due to old hardware design, see MR !3207
116121
if (host->cs_iomux) {
117122
gpio_ll_set_input_signal_from(GPIO_HAL_GET_HW(GPIO_PORT_0), host->cs_in_signal, false);
118123
} else {
119124
esp_rom_gpio_connect_in_signal(host->cfg.spics_io_num, host->cs_in_signal, false);
120125
}
126+
#endif
121127
}
122128

123129
#if (SOC_CPU_CORES_NUM > 1) && (!CONFIG_FREERTOS_UNICORE)
@@ -640,21 +646,13 @@ static void SPI_SLAVE_ISR_ATTR s_spi_slave_dma_prepare_data(spi_dma_ctx_t *dma_c
640646

641647
spi_dma_reset(dma_ctx->rx_dma_chan);
642648
spi_slave_hal_hw_prepare_rx(hal->hw);
649+
spi_dma_start(dma_ctx->rx_dma_chan, dma_ctx->dmadesc_rx);
643650
}
644651
if (hal->tx_buffer) {
645652
spicommon_dma_desc_setup_link(dma_ctx->dmadesc_tx, hal->tx_buffer, (hal->bitlen + 7) / 8, false);
646653

647654
spi_dma_reset(dma_ctx->tx_dma_chan);
648655
spi_slave_hal_hw_prepare_tx(hal->hw);
649-
}
650-
}
651-
652-
static void SPI_SLAVE_ISR_ATTR s_spi_slave_start_dma(spi_dma_ctx_t *dma_ctx, spi_slave_hal_context_t *hal)
653-
{
654-
if (hal->rx_buffer) {
655-
spi_dma_start(dma_ctx->rx_dma_chan, dma_ctx->dmadesc_rx);
656-
}
657-
if (hal->tx_buffer) {
658656
spi_dma_start(dma_ctx->tx_dma_chan, dma_ctx->dmadesc_tx);
659657
}
660658
}
@@ -784,10 +782,7 @@ static void SPI_SLAVE_ISR_ATTR spi_intr(void *arg)
784782

785783
//The slave rx dma get disturbed by unexpected transaction. Only connect the CS and start DMA when slave is ready.
786784
if (use_dma) {
787-
// Note: order of restore_cs and s_spi_slave_start_dma is important
788-
// restore_cs also bring potential glitch, should happen before start DMA
789785
restore_cs(host);
790-
s_spi_slave_start_dma(host->dma_ctx, hal);
791786
}
792787

793788
//Kick off transfer

components/esp_driver_spi/test_apps/master/main/test_spi_master.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,7 @@ TEST_CASE("test_spi_master_sleep_retention", "[spi]")
18551855
spi_device_interface_config_t devcfg = SPI_DEVICE_TEST_DEFAULT_CONFIG();
18561856
buscfg.flags |= SPICOMMON_BUSFLAG_GPIO_PINS;
18571857
buscfg.flags |= SPICOMMON_BUSFLAG_SLP_ALLOW_PD;
1858+
buscfg.miso_io_num = buscfg.mosi_io_num; // set spi "self-loop"
18581859
uint8_t send[16] = "hello spi x\n";
18591860
uint8_t recv[16];
18601861
spi_transaction_t trans_cfg = {
@@ -1871,8 +1872,6 @@ TEST_CASE("test_spi_master_sleep_retention", "[spi]")
18711872
#endif
18721873
printf("Retention on GPSPI%d with dma: %d\n", periph + 1, use_dma);
18731874
TEST_ESP_OK(spi_bus_initialize(periph, &buscfg, use_dma));
1874-
// set spi "self-loop" after bus initialized
1875-
spitest_gpio_output_sel(buscfg.miso_io_num, FUNC_GPIO, spi_periph_signal[periph].spid_out);
18761875
TEST_ESP_OK(spi_bus_add_device(periph, &devcfg, &dev_handle));
18771876

18781877
for (uint8_t cnt = 0; cnt < 3; cnt ++) {
@@ -1927,9 +1926,8 @@ TEST_CASE("test_spi_master_auto_sleep_retention", "[spi]")
19271926
spi_bus_config_t buscfg = SPI_BUS_TEST_DEFAULT_CONFIG();
19281927
buscfg.flags = (allow_pd) ? SPICOMMON_BUSFLAG_SLP_ALLOW_PD : 0;
19291928
buscfg.flags |= SPICOMMON_BUSFLAG_GPIO_PINS;
1929+
buscfg.miso_io_num = buscfg.mosi_io_num; // set spi "self-loop"
19301930
TEST_ESP_OK(spi_bus_initialize(TEST_SPI_HOST, &buscfg, SPI_DMA_DISABLED));
1931-
// set spi "self-loop" after bus initialized
1932-
spitest_gpio_output_sel(buscfg.miso_io_num, FUNC_GPIO, spi_periph_signal[TEST_SPI_HOST].spid_out);
19331931

19341932
spi_device_handle_t dev_handle;
19351933
spi_device_interface_config_t devcfg = SPI_DEVICE_TEST_DEFAULT_CONFIG();

components/hal/esp32/include/hal/spi_ll.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ extern "C" {
4444
#define SPI_LL_CPU_MAX_BIT_LEN (16 * 32) //Fifo len: 16 words
4545
#define SPI_LL_MOSI_FREE_LEVEL 0 //Default level after bus initialized
4646

47+
// CS_WORKAROUND: SPI slave with using DMA, the rx dma suffers from unexpected transactions
48+
// before slave is ready, need disconnect CS before and after each transaction
49+
#define SPI_LL_SLAVE_NEEDS_CS_WORKAROUND 1
4750
#define SPI_LL_SLAVE_NEEDS_RESET_WORKAROUND 1
4851
#define SPI_LL_SUPPORT_TIME_TUNING 1
4952

components/hal/esp32p4/include/hal/spi_ll.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <stdlib.h> //for abs()
1818
#include <string.h>
19+
#include "hal/config.h"
1920
#include "esp_types.h"
2021
#include "soc/spi_periph.h"
2122
#include "soc/spi_struct.h"
@@ -262,6 +263,10 @@ static inline void spi_ll_master_init(spi_dev_t *hw)
262263
hw->slave.val = 0;
263264
hw->user.val = 0;
264265

266+
//Disable unused error_end condition
267+
hw->user1.mst_wfull_err_end_en = 0;
268+
hw->user2.mst_rempty_err_end_en = 0;
269+
265270
hw->dma_conf.val = 0;
266271
hw->dma_conf.slv_tx_seg_trans_clr_en = 1;
267272
hw->dma_conf.slv_rx_seg_trans_clr_en = 1;
@@ -763,21 +768,28 @@ static inline void spi_ll_master_keep_cs(spi_dev_t *hw, int keep_active)
763768
*----------------------------------------------------------------------------*/
764769
/**
765770
* Set the standard clock mode for master.
771+
* This config take effect only when SPI_CLK (pre-div before periph) div >=2
766772
*
767773
* @param hw Beginning address of the peripheral registers.
768774
* @param enable_std True for std timing, False for half cycle delay sampling.
769775
*/
770776
static inline void spi_ll_master_set_rx_timing_mode(spi_dev_t *hw, spi_sampling_point_t sample_point)
771777
{
772-
//This is not supported
778+
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
779+
hw->clock.clk_edge_sel = (sample_point == SPI_SAMPLING_POINT_PHASE_1);
780+
#endif
773781
}
774782

775783
/**
776784
* Get if standard clock mode is supported.
777785
*/
778786
static inline bool spi_ll_master_is_rx_std_sample_supported(void)
779787
{
788+
#if HAL_CONFIG(CHIP_SUPPORT_MIN_REV) >= 300
789+
return true;
790+
#else
780791
return false;
792+
#endif
781793
}
782794

783795
/**

0 commit comments

Comments
 (0)