66 * Change Logs:
77 * Date Author Notes
88 * 2024-10-08 wumingzi first implementation
9+ * 2024-10-08 wumingzi add custom configuration and support muti spi obj
910 */
1011
1112#include <rtthread.h>
1213#include <rtdevice.h>
14+ #include <time.h>
1315
1416#include "rtdef.h"
1517#include "rttypes.h"
2022#include "driver/spi_master.h"
2123
2224#include "drv_spi.h"
25+ #include "drivers/dev_spi.h"
2326
2427#ifdef RT_USING_SPI
2528#ifdef BSP_USING_SPI2
2932static struct rt_spi_bus spi_bus2 ;
3033
3134static spi_device_handle_t spi ;
32-
3335static spi_bus_config_t buscfg ;
3436
3537static struct esp32_spi spi_bus_obj [] = {
@@ -62,20 +64,9 @@ static void esp32_spi_init(struct esp32_spi *esp32_spi)
6264 spi_configure (NULL ,NULL );
6365}
6466
65- static void spi_pin_mode (rt_base_t pin )
66- {
67- gpio_config_t io_conf ;
68- io_conf .intr_type = GPIO_INTR_DISABLE ;
69- io_conf .mode = GPIO_MODE_OUTPUT ;
70- io_conf .pin_bit_mask = (1ULL << pin );
71- io_conf .pull_down_en = 0 ;
72- io_conf .pull_up_en = 1 ;
73- }
74-
7567static rt_err_t spi_configure (struct rt_spi_device * device ,
7668 struct rt_spi_configuration * configuration )
7769{
78- /* spi_pin_mode(RT_BSP_SPI_CS_PIN);*/
7970 static spi_bus_config_t buscfg =
8071 {
8172 .miso_io_num = SPI2_IOMUX_PIN_NUM_MISO , /*MISO*/
@@ -89,19 +80,87 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
8980 esp_err_t err = spi_bus_initialize (SPI2_HOST , & buscfg , SPI_DMA_CH_AUTO );
9081 ESP_ERROR_CHECK (err );
9182
92- static spi_device_interface_config_t devcfg = {
93- .clock_speed_hz = SPI_MASTER_FREQ_8M ,
94- .mode = 0 ,
95- .spics_io_num = RT_BSP_SPI_CS_PIN ,
96- .queue_size = 7 ,
97- };
83+ static spi_device_interface_config_t devcfg ;
84+ if (configuration -> data_width == 8 )
85+ {
86+ size_t length ; /*/< Total data length, in bits*/
87+ size_t rxlength ; /*/< Total data length received, should be not greater than ``length`` in full-duplex mode (0 defaults this to the value of ``length``)*/
88+ }
89+
90+ LOG_W ("configuration->max_hz = %d \n" ,configuration -> max_hz );
91+ if (configuration -> max_hz >= SPI_MASTER_FREQ_80M )
92+ {
93+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_80M ; /*/< 80MHz*/
94+ }
95+ else if (configuration -> max_hz >= SPI_MASTER_FREQ_40M )
96+ {
97+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_40M ; /*/< 40MHz*/
98+ }
99+ else if (configuration -> max_hz >= SPI_MASTER_FREQ_26M )
100+ {
101+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_26M ; /*/< 26.67MHz*/
102+ }
103+ else if (configuration -> max_hz >= SPI_MASTER_FREQ_20M )
104+ {
105+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_20M ; /*/< 20MHz*/
106+ }
107+ else if (configuration -> max_hz >= SPI_MASTER_FREQ_16M )
108+ {
109+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_16M ; /*/< 16MHz*/
110+ }
111+ else if (configuration -> max_hz >= SPI_MASTER_FREQ_13M )
112+ {
113+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_13M ; /*/< 13.33MHz*/
114+ }
115+ else if (configuration -> max_hz >= SPI_MASTER_FREQ_11M )
116+ {
117+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_11M ; /*/< 11.43MHz*/
118+ }
119+ else if (configuration -> max_hz >= SPI_MASTER_FREQ_10M )
120+ {
121+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_10M ; /*/< 10MHz*/
122+ }
123+ else if (configuration -> max_hz >= SPI_MASTER_FREQ_9M )
124+ {
125+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_9M ; /*/< 8.89MHz*/
126+ }
127+ else
128+ {
129+ devcfg .clock_speed_hz = SPI_MASTER_FREQ_8M ;
130+ }
131+
132+ switch (configuration -> mode )
133+ {
134+ case RT_SPI_MODE_0 : /*!< CPOL = 0, CPHA = 0 */
135+ devcfg .mode = 0 ;
136+ case RT_SPI_MODE_1 : /*!< CPOL = 0, CPHA = 1 */
137+ devcfg .mode = 1 ;
138+ case RT_SPI_MODE_2 : /*!< CPOL = 1, CPHA = 0 */
139+ devcfg .mode = 2 ;
140+ case RT_SPI_MODE_3 : /*!< CPOL = 1, CPHA = 1 */
141+ devcfg .mode = 3 ;
142+ default :
143+ devcfg .mode = 0 ;
144+ }
145+
146+ /* todo: support changing cs_pin,queue_size or specifing spi_device_interface_config_t and
147+ * spi_transaction_t by resever data.Meanwhile finish the initialization of interrupt
148+ * callback function and dma.
149+ */
150+
151+ devcfg .spics_io_num = RT_BSP_SPI_CS_PIN ;
152+ devcfg .queue_size = 7 ;
98153
99154 err = spi_bus_add_device (SPI2_HOST , & devcfg , & spi );
100155 ESP_ERROR_CHECK (err );
101156
102- spi_bus_obj [0 ].bus_name = "spi2" ;
103- spi_bus_obj [0 ].spi_bus = & spi_bus2 ;
104- spi_bus_obj [0 ].esp32_spi_bus_cfg = & buscfg ;
157+ /* Although there is only one spi bus object, it will be a template for other bsps of ESP32 series */
158+ for (int i = 0 ; i < sizeof (spi_bus_obj )/sizeof (spi_bus_obj [0 ]); i ++ )
159+ {
160+ spi_bus_obj [i ].bus_name = "spi2" ;
161+ spi_bus_obj [i ].spi_bus = & spi_bus2 ;
162+ spi_bus_obj [i ].esp32_spi_bus_cfg = & buscfg ;
163+ }
105164
106165 return RT_EOK ;
107166};
@@ -116,8 +175,8 @@ static rt_ssize_t spixfer(struct rt_spi_device* device, struct rt_spi_message* m
116175
117176 trans .tx_buffer = message -> send_buf ;
118177 trans .rx_buffer = message -> recv_buf ;
119- trans .length = message -> length ;
120- trans .rxlength = message -> length ;
178+ trans .length = ( message -> length ) * 8 ;
179+ trans .rxlength = ( message -> length ) * 8 ;
121180
122181 spi_device_acquire_bus (spi , portMAX_DELAY );
123182 esp_err_t err = spi_device_polling_transmit (spi , & trans );
@@ -161,17 +220,20 @@ int rt_hw_spi_init(void)
161220{
162221 int result = 0 ;
163222
164- spi_bus_obj [0 ].spi_bus -> parent .user_data = (void * )& spi_bus_obj [0 ];
165- result = rt_spi_bus_register (spi_bus_obj [0 ].spi_bus , spi_bus_obj [0 ].bus_name , & esp32_spi_ops );
223+ for (int i = 0 ; i < sizeof (spi_bus_obj )/sizeof (spi_bus_obj [0 ]); i ++ )
224+ {
225+ spi_bus_obj [i ].spi_bus -> parent .user_data = (void * )& spi_bus_obj [i ];
226+ result = rt_spi_bus_register (spi_bus_obj [i ].spi_bus , spi_bus_obj [i ].bus_name , & esp32_spi_ops );
166227
167- RT_ASSERT (result == RT_EOK );
228+ RT_ASSERT (result == RT_EOK );
168229
169- LOG_D ("%s bus init done" , spi_bus_obj [i ].bus_name );
230+ LOG_D ("%s bus init done" , spi_bus_obj [i ].bus_name );
231+ }
170232
171233 return result ;
172234}
173235
174236INIT_BOARD_EXPORT (rt_hw_spi_init );
175237
176238#endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4*/
177- #endif /* RT_USING_SPI */
239+ #endif /* RT_USING_SPI */
0 commit comments