Skip to content

Commit 94617c7

Browse files
committed
fix/machine: Special handling for SPI and SDCard
1 parent 06c0418 commit 94617c7

File tree

3 files changed

+501
-43
lines changed

3 files changed

+501
-43
lines changed

m5stack/CMakeListsDefault.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ set(MICROPY_SOURCE_PORT
8585
# ${PROJECT_DIR}/../micropython/ports/esp32/machine_wdt.c
8686
${PROJECT_DIR}/../micropython/ports/esp32/mpthreadport.c
8787
${PROJECT_DIR}/machine_rtc.c
88-
${PROJECT_DIR}/../micropython/ports/esp32/machine_sdcard.c
8988
${PROJECT_DIR}/../micropython/ports/esp32/modespnow.c
9089
)
9190

9291
if (BOARD_TYPE STREQUAL "cores3" OR BOARD_TYPE STREQUAL "core2")
92+
LIST(APPEND MICROPY_SOURCE_PORT ${PROJECT_DIR}/machine_sdcard.c)
9393
LIST(APPEND MICROPY_SOURCE_PORT ${PROJECT_DIR}/machine_hw_spi.c)
9494
else()
95+
LIST(APPEND MICROPY_SOURCE_PORT ${PROJECT_DIR}/../micropython/ports/esp32/machine_sdcard.c)
9596
LIST(APPEND MICROPY_SOURCE_PORT ${PROJECT_DIR}/../micropython/ports/esp32/machine_hw_spi.c)
9697
endif()
9798

m5stack/machine_hw_spi.c

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@
6464
#ifndef MICROPY_HW_SPI2_SCK
6565
#if CONFIG_IDF_TARGET_ESP32
6666
// ESP32 has IO_MUX pins for VSPI/SPI3 lines, use them as defaults
67-
#define MICROPY_HW_SPI2_SCK VSPI_IOMUX_PIN_NUM_CLK // pin 18
68-
#define MICROPY_HW_SPI2_MOSI VSPI_IOMUX_PIN_NUM_MOSI // pin 23
69-
#define MICROPY_HW_SPI2_MISO VSPI_IOMUX_PIN_NUM_MISO // pin 19
67+
#define MICROPY_HW_SPI2_SCK SPI3_IOMUX_PIN_NUM_CLK // pin 18
68+
#define MICROPY_HW_SPI2_MOSI SPI3_IOMUX_PIN_NUM_MOSI // pin 23
69+
#define MICROPY_HW_SPI2_MISO SPI3_IOMUX_PIN_NUM_MISO // pin 19
7070
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
7171
// ESP32S2 and S3 uses GPIO matrix for SPI3 pins, no IO_MUX possible
7272
// Set defaults to the pins used by SPI2 in Octal mode
@@ -79,13 +79,6 @@
7979
#define MP_HW_SPI_MAX_XFER_BYTES (4092)
8080
#define MP_HW_SPI_MAX_XFER_BITS (MP_HW_SPI_MAX_XFER_BYTES * 8) // Has to be an even multiple of 8
8181

82-
#if CONFIG_IDF_TARGET_ESP32C3
83-
#define HSPI_HOST SPI2_HOST
84-
#elif CONFIG_IDF_TARGET_ESP32S3
85-
#define HSPI_HOST SPI3_HOST
86-
#define FSPI_HOST SPI2_HOST
87-
#endif
88-
8982
typedef struct _machine_hw_spi_default_pins_t {
9083
int8_t sck;
9184
int8_t mosi;
@@ -112,15 +105,15 @@ typedef struct _machine_hw_spi_obj_t {
112105
} machine_hw_spi_obj_t;
113106

114107
// Default pin mappings for the hardware SPI instances
115-
STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[2] = {
108+
STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[MICROPY_HW_SPI_MAX] = {
116109
{ .sck = MICROPY_HW_SPI1_SCK, .mosi = MICROPY_HW_SPI1_MOSI, .miso = MICROPY_HW_SPI1_MISO },
117110
#ifdef MICROPY_HW_SPI2_SCK
118111
{ .sck = MICROPY_HW_SPI2_SCK, .mosi = MICROPY_HW_SPI2_MOSI, .miso = MICROPY_HW_SPI2_MISO },
119112
#endif
120113
};
121114

122-
// Static objects mapping to HSPI and VSPI hardware peripherals
123-
STATIC machine_hw_spi_obj_t machine_hw_spi_obj[2];
115+
// Static objects mapping to SPI2 (and SPI3 if available) hardware peripherals.
116+
STATIC machine_hw_spi_obj_t machine_hw_spi_obj[MICROPY_HW_SPI_MAX];
124117

125118
STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
126119
switch (spi_bus_remove_device(self->spi)) {
@@ -129,18 +122,28 @@ STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) {
129122
return;
130123

131124
case ESP_ERR_INVALID_STATE:
132-
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI device already freed"));
133-
return;
125+
// NOTE:
126+
// core2和cores3的屏幕和sd卡复用一个spi,
127+
// 所以这里不需要对VSPI_HOST和SPI3_HOST进行初始化。
128+
if (self->host != SPI2_HOST) {
129+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI device already freed"));
130+
return;
131+
}
134132
}
135133

136-
switch (spi_bus_free(self->host)) {
137-
case ESP_ERR_INVALID_ARG:
138-
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
139-
return;
140-
141-
case ESP_ERR_INVALID_STATE:
142-
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI bus already freed"));
143-
return;
134+
// NOTE:
135+
// core2和cores3的屏幕和sd卡复用一个spi,
136+
// 所以这里不需要对VSPI_HOST和SPI3_HOST进行初始化。
137+
if (self->host != SPI2_HOST) {
138+
switch (spi_bus_free(self->host)) {
139+
case ESP_ERR_INVALID_ARG:
140+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("invalid configuration"));
141+
return;
142+
143+
case ESP_ERR_INVALID_STATE:
144+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI bus already freed"));
145+
return;
146+
}
144147
}
145148

146149
int8_t pins[3] = {self->miso, self->mosi, self->sck};
@@ -223,17 +226,6 @@ STATIC void machine_hw_spi_init_internal(
223226
changed = true;
224227
}
225228

226-
if (self->host != HSPI_HOST
227-
#ifdef FSPI_HOST
228-
&& self->host != FSPI_HOST
229-
#endif
230-
#ifdef VSPI_HOST
231-
&& self->host != VSPI_HOST
232-
#endif
233-
) {
234-
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), self->host);
235-
}
236-
237229
if (changed) {
238230
if (self->state == MACHINE_HW_SPI_STATE_INIT) {
239231
self->state = MACHINE_HW_SPI_STATE_DEINIT;
@@ -267,7 +259,7 @@ STATIC void machine_hw_spi_init_internal(
267259
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
268260
dma_chan = SPI_DMA_CH_AUTO;
269261
#else
270-
if (self->host == HSPI_HOST) {
262+
if (self->host == SPI2_HOST) {
271263
dma_chan = 1;
272264
} else {
273265
dma_chan = 2;
@@ -281,10 +273,13 @@ STATIC void machine_hw_spi_init_internal(
281273
return;
282274

283275
case ESP_ERR_INVALID_STATE:
284-
if (self->host != HSPI_HOST) {
276+
// NOTE:
277+
// core2和cores3的屏幕和sd卡复用一个spi,
278+
// 所以这里不需要对VSPI_HOST和SPI3_HOST进行初始化。
279+
if (self->host != SPI2_HOST) {
285280
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("SPI host already in use"));
281+
return;
286282
}
287-
return;
288283
}
289284

290285
ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
@@ -482,13 +477,14 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
482477

483478
machine_hw_spi_obj_t *self;
484479
const machine_hw_spi_default_pins_t *default_pins;
485-
if (args[ARG_id].u_int == 1) { // SPI2_HOST which is FSPI_HOST on ESP32Sx, HSPI_HOST on others
486-
self = &machine_hw_spi_obj[0];
487-
default_pins = &machine_hw_spi_default_pins[0];
480+
mp_int_t spi_id = args[ARG_id].u_int;
481+
if (1 <= spi_id && spi_id <= MICROPY_HW_SPI_MAX) {
482+
self = &machine_hw_spi_obj[spi_id - 1];
483+
default_pins = &machine_hw_spi_default_pins[spi_id - 1];
488484
} else {
489-
self = &machine_hw_spi_obj[1];
490-
default_pins = &machine_hw_spi_default_pins[1];
485+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id);
491486
}
487+
492488
self->base.type = &machine_spi_type;
493489

494490
int8_t sck, mosi, miso;

0 commit comments

Comments
 (0)