2626 * @}
2727 */
2828
29+ /* -Wsign-compare has to be deactivated in this file due to the comparison
30+ * of int and size_t in spi_ll.h of the ESP-IDF */
31+ #pragma GCC diagnostic ignored "-Wsign-compare"
32+
2933#include <assert.h>
3034#include <string.h>
3135
3842#include "periph/spi.h"
3943#include "syscalls.h"
4044
41- #include "driver/periph_ctrl.h"
4245#include "esp_attr.h"
46+ #include "esp_cpu.h"
47+ #include "esp_private/periph_ctrl.h"
4348#include "esp_rom_gpio.h"
4449#include "hal/spi_hal.h"
4550#include "hal/spi_types.h"
@@ -97,6 +102,15 @@ _Static_assert(SPI_NUMOF == ARRAY_SIZE(_spi),
97102_Static_assert (SPI_NUMOF <= SPI_NUMOF_MAX ,
98103 "Number of defined SPI devices is greater than the number of supported devices" );
99104
105+ #define PERIPH_SPI1_MODULE PERIPH_SPI_MODULE
106+ #if defined(CPU_FAM_ESP32 )
107+ # define PERIPH_SPI2_MODULE PERIPH_HSPI_MODULE
108+ # define PERIPH_SPI3_MODULE PERIPH_VSPI_MODULE
109+ #elif defined(CPU_FAM_ESP32S2 )
110+ # define PERIPH_SPI2_MODULE PERIPH_FSPI_MODULE
111+ # define PERIPH_SPI3_MODULE PERIPH_HSPI_MODULE
112+ #endif
113+
100114void IRAM_ATTR spi_init (spi_t bus )
101115{
102116 DEBUG ("%s bus=%u\n" , __func__ , bus );
@@ -117,10 +131,27 @@ void IRAM_ATTR spi_init(spi_t bus)
117131 }
118132
119133 /* enable (power on) the according SPI module */
120- periph_module_enable (_spi [bus ].periph -> module );
134+ if (spi_config [bus ].ctrl == SPI1_HOST ) {
135+ periph_module_enable (PERIPH_SPI1_MODULE );
136+ }
137+ #if SOC_SPI_PERIPH_NUM > 1
138+ else if (spi_config [bus ].ctrl == SPI2_HOST ) {
139+ periph_module_enable (PERIPH_SPI2_MODULE );
140+ }
141+ #endif
142+ #if SOC_SPI_PERIPH_NUM > 2
143+ else if (spi_config [bus ].ctrl == SPI3_HOST ) {
144+ periph_module_enable (PERIPH_SPI3_MODULE );
145+ }
146+ #endif
147+ else {
148+ assert (false);
149+ }
121150
122151 /* initialize SPI peripheral */
123152 spi_ll_master_init (_spi [bus ].periph -> hw );
153+ spi_ll_set_mosi_delay (_spi [bus ].periph -> hw , 0 , 0 );
154+ spi_ll_apply_config (_spi [bus ].periph -> hw );
124155
125156 /* bring the bus into a defined state (one-line mode) */
126157 spi_ll_master_set_line_mode (_spi [bus ].periph -> hw , (spi_line_mode_t ){ 1 , 1 , 1 });
@@ -332,6 +363,7 @@ void IRAM_ATTR spi_acquire(spi_t bus, spi_cs_t cs, spi_mode_t mode, spi_clk_t cl
332363 }
333364 spi_ll_master_set_clock_by_reg (_spi [bus ].periph -> hw ,
334365 & _spi [bus ].timing .clock_reg );
366+ spi_ll_apply_config (_spi [bus ].periph -> hw );
335367
336368#if defined(CPU_FAM_ESP32C3 ) || defined(CPU_FAM_ESP32S3 )
337369 /*
@@ -417,7 +449,8 @@ static void IRAM_ATTR _spi_transfer(uint8_t bus,
417449 spi_ll_write_buffer (_spi [bus ].periph -> hw , out ? out : _spi_empty_out , len << 3 );
418450
419451 /* start the transfer */
420- spi_ll_master_user_start (_spi [bus ].periph -> hw );
452+ spi_ll_apply_config (_spi [bus ].periph -> hw );
453+ spi_ll_user_start (_spi [bus ].periph -> hw );
421454
422455 /* wait until the transfer is finished */
423456 while (spi_ll_get_running_cmd (_spi [bus ].periph -> hw )) {}
0 commit comments