Skip to content

Commit c55a6b0

Browse files
committed
should fix the SPIBus
1 parent 901f918 commit c55a6b0

File tree

1 file changed

+54
-169
lines changed

1 file changed

+54
-169
lines changed

ext_mod/lcd_bus/esp32_src/spi_bus.c

Lines changed: 54 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -104,172 +104,58 @@ STATIC mp_obj_t mp_lcd_spi_bus_make_new(const mp_obj_type_t *type, size_t n_args
104104
int cs = (int)args[ARG_cs].u_int;
105105
int host = (int)args[ARG_host].u_int;
106106

107+
int temp_host;
108+
107109
uint8_t miso_host = 5;
108-
switch(miso) {
109-
case SPI_IOMUX_PIN_NUM_MISO:
110-
miso_host = SPI1_HOST;
111-
break;
112-
113-
case SPI2_IOMUX_PIN_NUM_MISO:
114-
miso_host = SPI2_HOST;
115-
break;
116-
117-
#ifdef SPI3_IOMUX_PIN_NUM_MISO
118-
case SPI3_IOMUX_PIN_NUM_MISO:
119-
miso_host = SPI3_HOST;
120-
break;
121-
#endif
122-
123-
default:
124-
if (miso == -1) miso_host = 4;
110+
if (
111+
mosi == SPI_IOMUX_PIN_NUM_MOSI &&
112+
clk == SPI_IOMUX_PIN_NUM_CLK &&
113+
(miso == -1 || miso == SPI_IOMUX_PIN_NUM_MISO) &&
114+
(cs == -1 || cs == SPI_IOMUX_PIN_NUM_CS) &&
115+
(hd == -1 || hd == SPI_IOMUX_PIN_NUM_HD) &&
116+
(wp == -1 || wp == SPI_IOMUX_PIN_NUM_WP)
117+
) {
118+
flags |= SPICOMMON_BUSFLAG_IOMUX_PINS;
119+
temp_host = SPI1_HOST;
120+
} else if (
121+
mosi == SPI2_IOMUX_PIN_NUM_MOSI &&
122+
clk == SPI2_IOMUX_PIN_NUM_CLK &&
123+
(miso == -1 || miso == SPI2_IOMUX_PIN_NUM_MISO) &&
124+
(cs == -1 || cs == SPI2_IOMUX_PIN_NUM_CS) &&
125+
(hd == -1 || hd == SPI2_IOMUX_PIN_NUM_HD) &&
126+
(wp == -1 || wp == SPI2_IOMUX_PIN_NUM_WP)
127+
) {
128+
flags |= SPICOMMON_BUSFLAG_IOMUX_PINS;
129+
temp_host = SPI2_HOST;
125130
}
126-
127-
uint8_t mosi_host = 5;
128-
switch(mosi) {
129-
case SPI_IOMUX_PIN_NUM_MOSI:
130-
mosi_host = SPI1_HOST;
131-
break;
132-
133-
case SPI2_IOMUX_PIN_NUM_MOSI:
134-
mosi_host = SPI2_HOST;
135-
break;
136-
137-
#ifdef SPI3_IOMUX_PIN_NUM_MOSI
138-
case SPI3_IOMUX_PIN_NUM_MOSI:
139-
mosi_host = SPI3_HOST;
140-
break;
141-
#endif
142-
143-
default:
144-
if (mosi == -1) mosi_host = 4;
131+
#ifdef SPI3_IOMUX_PIN_NUM_MISO
132+
else if (
133+
mosi == SPI3_IOMUX_PIN_NUM_MOSI &&
134+
clk == SPI3_IOMUX_PIN_NUM_CLK &&
135+
(miso == -1 || miso == SPI3_IOMUX_PIN_NUM_MISO) &&
136+
(cs == -1 || cs == SPI3_IOMUX_PIN_NUM_CS) &&
137+
(hd == -1 || hd == SPI3_IOMUX_PIN_NUM_HD) &&
138+
(wp == -1 || wp == SPI3_IOMUX_PIN_NUM_WP)
139+
) {
140+
flags |= SPICOMMON_BUSFLAG_IOMUX_PINS;
141+
temp_host = SPI3_HOST;
145142
}
146-
147-
uint8_t clk_host = 5;
148-
switch(clk) {
149-
case SPI_IOMUX_PIN_NUM_CLK:
150-
clk_host = SPI1_HOST;
151-
break;
152-
153-
case SPI2_IOMUX_PIN_NUM_CLK:
154-
clk_host = SPI2_HOST;
155-
break;
156-
157-
#ifdef SPI3_IOMUX_PIN_NUM_CLK
158-
case SPI3_IOMUX_PIN_NUM_CLK:
159-
clk_host = SPI3_HOST;
160-
break;
161-
#endif
162-
163-
default:
164-
if (clk == -1) clk_host = 4;
143+
#endif
144+
else {
145+
flags |= SPICOMMON_BUSFLAG_GPIO_PINS;
146+
temp_host = -1;
165147
}
166148

167-
uint8_t cs_host = 5;
168-
switch(cs) {
169-
case SPI_IOMUX_PIN_NUM_CS:
170-
cs_host = SPI1_HOST;
171-
break;
172-
173-
case SPI2_IOMUX_PIN_NUM_CS:
174-
cs_host = SPI2_HOST;
175-
break;
176-
177-
#ifdef SPI3_IOMUX_PIN_NUM_CS
178-
case SPI3_IOMUX_PIN_NUM_CS:
179-
cs_host = SPI3_HOST;
180-
break;
181-
#endif
182-
183-
default:
184-
if (cs == -1) cs_host = 4;
185-
}
186-
187-
uint8_t hd_host = 5;
188-
switch(hd) {
189-
case SPI_IOMUX_PIN_NUM_HD:
190-
hd_host = SPI1_HOST;
191-
break;
192-
193-
case SPI2_IOMUX_PIN_NUM_HD:
194-
hd_host = SPI2_HOST;
195-
break;
196-
197-
#ifdef SPI3_IOMUX_PIN_NUM_HD
198-
case SPI3_IOMUX_PIN_NUM_HD:
199-
hd_host = SPI3_HOST;
200-
break;
201-
#endif
202-
203-
default:
204-
if (hd == -1) hd_host = 4;
149+
if (temp_host == -1 && host == -1) {
150+
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Unable to determine SPI host, please supply a host number"));
151+
} else if (host == -1) {
152+
host = temp_host;
205153
}
206154

207-
uint8_t wp_host = 5;
208-
switch(wp) {
209-
case SPI_IOMUX_PIN_NUM_WP:
210-
wp_host = SPI1_HOST;
211-
break;
212-
213-
case SPI2_IOMUX_PIN_NUM_WP:
214-
wp_host = SPI2_HOST;
215-
break;
216-
217-
#ifdef SPI3_IOMUX_PIN_NUM_WP
218-
case SPI3_IOMUX_PIN_NUM_WP:
219-
wp_host = SPI3_HOST;
220-
break;
221-
#endif
222-
223-
default:
224-
if (wp == -1) wp_host = 4;
225-
}
226-
227-
uint32_t hst = (miso_host << 15) | (mosi_host << 12) | (clk_host << 9) | (cs_host << 6) | (hd_host << 3) | wp_host;
228-
229-
uint32_t io_mux = SPICOMMON_BUSFLAG_IOMUX_PINS;
230-
int found_host = -1;
231-
232-
uint32_t tmp_host;
233-
bool h4;
234-
bool h5;
235-
236-
for (int i=0;i<6;i++) {
237-
tmp_host = hst >> (i * 3);
238-
h4 = (tmp_host & 0x05) == 0x04 ? true : false;
239-
h5 = (tmp_host & 0x05) == 0x05 ? true : false;
240-
tmp_host = tmp_host & 0x03;
241-
242-
if (h4) continue;
243-
244-
if (h5) {
245-
found_host = -1;
246-
io_mux = SPICOMMON_BUSFLAG_GPIO_PINS;
247-
break;
248-
}
249-
250-
if (found_host == -1) {
251-
found_host = (int)tmp_host;
252-
} else if (found_host != (int)tmp_host) {
253-
found_host = -1;
254-
io_mux = SPICOMMON_BUSFLAG_GPIO_PINS;
255-
break;
256-
}
257-
}
258-
259-
if (host == -1 && found_host == -1) {
260-
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("you must supply 0, 1 or 2 for the bus host"));
261-
} else if (host != -1 && found_host != -1 && host != found_host) {
262-
mp_printf(&mp_plat_print, "Setting the bus host to %d will give better performance\n", found_host);
263-
} else if (host == -1 && found_host != -1) {
264-
host = found_host;
265-
}
266-
267-
flags = flags | io_mux;
268-
269155
self->callback = mp_const_none;
270156

271157
self->host = host;
272-
self->bus_handle = (esp_lcd_spi_bus_handle_t)((uint32_t)host);
158+
self->bus_handle = (esp_lcd_spi_bus_handle_t)host;
273159

274160
self->bus_config.sclk_io_num = clk;
275161
self->bus_config.mosi_io_num = mosi;
@@ -300,13 +186,6 @@ STATIC mp_obj_t mp_lcd_spi_bus_make_new(const mp_obj_type_t *type, size_t n_args
300186
self->panel_io_handle.init = &spi_init;
301187
self->panel_io_handle.get_lane_count = &spi_get_lane_count;
302188

303-
mp_printf(
304-
&mp_plat_print,
305-
"mp_lcd_spi_bus_make_new\nsclk_io_num: %d - %d\nmosi_io_num: %d - %d\nmiso_io_num: %d - %d\ncs_gpio_num: %d - %d\ndc_gpio_num: %d\nhost: %d - %d\n",
306-
clk, (int)args[ARG_sclk].u_int, mosi, (int)args[ARG_mosi].u_int, miso, (int)args[ARG_miso].u_int, cs, (int)args[ARG_cs].u_int, (int)args[ARG_dc].u_int, host, (int)args[ARG_host].u_int
307-
);
308-
309-
310189
return MP_OBJ_FROM_PTR(self);
311190
}
312191

@@ -375,13 +254,19 @@ mp_lcd_err_t spi_init(mp_obj_t obj, uint16_t width, uint16_t height, uint8_t bpp
375254
self->panel_io_config.trans_queue_depth = 10;
376255
}
377256

378-
mp_printf(
379-
&mp_plat_print,
380-
"spi_init\ntrans_queue_depth: %d\nmax_transfer_sz: %d\n",
381-
self->panel_io_config.trans_queue_depth, self->bus_config.max_transfer_sz
382-
);
257+
int dma_chan = 0;
258+
259+
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
260+
dma_chan = SPI_DMA_CH_AUTO;
261+
#else
262+
if (self->host == SPI2_HOST) {
263+
dma_chan = 1;
264+
} else {
265+
dma_chan = 2;
266+
}
267+
#endif
383268

384-
mp_lcd_err_t ret = spi_bus_initialize(self->host, &self->bus_config, SPI_DMA_CH_AUTO);
269+
mp_lcd_err_t ret = spi_bus_initialize(self->host, &self->bus_config, dma_chan);
385270
if (ret != 0) {
386271
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(spi_bus_initialize)"), ret);
387272
}

0 commit comments

Comments
 (0)