@@ -286,19 +286,33 @@ namespace m5gfx
286286 } while (lgfx::millis () - time < 10 );
287287 }
288288
289+ // / TF card dummy clock送信 ;
290+ static void _send_sd_dummy_clock (int_fast16_t pin_cs)
291+ {
292+ static constexpr uint32_t dummy_clock[] = { ~0u , ~0u , ~0u , ~0u , ~0u };
293+ _pin_level (pin_cs, true );
294+ m5gfx::spi::writeBytes (VSPI_HOST, (const uint8_t *)dummy_clock, sizeof (dummy_clock));
295+ _pin_level (pin_cs, false );
296+ }
297+
289298 // / TF card をSPIモードに移行する ;
290299 static void _set_sd_spimode (int_fast16_t pin_cs)
291300 {
292- _pin_level (pin_cs, true );
293301 m5gfx::spi::beginTransaction (VSPI_HOST, 400000 , 0 );
294- static constexpr uint32_t dummy_clock[] = { ~0u , ~0u , ~0u , ~0u , ~0u };
295- m5gfx::spi::writeBytes (VSPI_HOST, (const uint8_t *)dummy_clock, sizeof (dummy_clock));
302+ _send_sd_dummy_clock (pin_cs);
296303
297- _pin_level (pin_cs, false );
298- static constexpr uint8_t sd_cmd0[] = { 0x40 , 0 , 0 , 0 , 0 , 0x95 , 0xFF , 0xFF };
299- m5gfx::spi::writeBytes (VSPI_HOST, sd_cmd0, sizeof (sd_cmd0));
300- m5gfx::spi::endTransaction (VSPI_HOST);
304+ uint8_t sd_cmd58[] = { 0x7A , 0 , 0 , 0 , 0 , 0xFD , 0xFF , 0xFF }; // READ_OCR command.
305+ m5gfx::spi::readBytes (VSPI_HOST, sd_cmd58, sizeof (sd_cmd58));
306+
307+ if (sd_cmd58[6 ] == sd_cmd58[7 ]) // not SPI mode
308+ {
309+ _send_sd_dummy_clock (pin_cs);
310+
311+ static constexpr uint8_t sd_cmd0[] = { 0x40 , 0 , 0 , 0 , 0 , 0x95 , 0xFF , 0xFF }; // GO_IDLE_STATE command.
312+ m5gfx::spi::writeBytes (VSPI_HOST, sd_cmd0, sizeof (sd_cmd0));
313+ }
301314 _pin_level (pin_cs, true );
315+ m5gfx::spi::endTransaction (VSPI_HOST);
302316 }
303317
304318 static std::uint32_t _read_panel_id (lgfx::Bus_SPI* bus, std::int32_t pin_cs, std::uint32_t cmd = 0x04 , std::uint8_t dummy_read_bit = 1 ) // 0x04 = RDDID command
0 commit comments