Skip to content

Commit

Permalink
[CH32] more of infra for micro-SD card
Browse files Browse the repository at this point in the history
  • Loading branch information
lyusupov committed Oct 31, 2024
1 parent 29f00b8 commit 8d2ebb3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
63 changes: 63 additions & 0 deletions software/firmware/source/SoftRF/src/platform/CH32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ static void CH32_msc_flush_cb (void)
#if defined(ENABLE_RECORDER)
#include <SdFat.h>

#if defined(USE_SOFTSPI)
SoftSPI uSD_SPI(SOC_GPIO_YD_SD_CMD, SOC_GPIO_YD_SD_D0, SOC_GPIO_YD_SD_CLK);
#else
SPIClass uSD_SPI;
#endif /* USE_SOFTSPI */

#define SD_CONFIG SdSpiConfig(uSD_SS_pin, SHARED_SPI, SD_SCK_MHZ(8), &uSD_SPI)

Expand Down Expand Up @@ -242,6 +246,23 @@ static void CH32_setup()
#endif /* USE_SOFTSPI */

ft = new Adafruit_FlashTransport_SPI(SOC_GPIO_YD_FL_SS, &FlashSPI);

#if defined(ENABLE_RECORDER)
{
int uSD_SS_pin = SOC_GPIO_YD_SD_D3;

#if !defined(USE_SOFTSPI)
uSD_SPI.setMISO(SOC_GPIO_YD_SD_D0);
uSD_SPI.setMOSI(SOC_GPIO_YD_SD_CMD);
uSD_SPI.setSCLK(SOC_GPIO_YD_SD_CLK);
#endif /* USE_SOFTSPI */

pinMode(uSD_SS_pin, OUTPUT);
digitalWrite(uSD_SS_pin, HIGH);

uSD_is_attached = uSD.cardBegin(SD_CONFIG);
}
#endif /* ENABLE_RECORDER */
break;

case CH32_WCH_V307V_R1:
Expand All @@ -257,6 +278,13 @@ static void CH32_setup()

hw_info.storage = CH32_has_spiflash ? STORAGE_FLASH : STORAGE_NONE;

#if defined(ENABLE_RECORDER)
if (uSD_is_attached && uSD.card()->cardSize() > 0) {
hw_info.storage = (hw_info.storage == STORAGE_FLASH) ?
STORAGE_FLASH_AND_CARD : STORAGE_CARD;
}
#endif /* ENABLE_RECORDER */

if (CH32_has_spiflash) {
spiflash_id = SPIFlash->getJEDECID();

Expand Down Expand Up @@ -333,6 +361,41 @@ static void CH32_post_init()
Serial.flush();
}

#if defined(ENABLE_RECORDER)
if (CH32_board == CH32_YD_V307VCT6)
{
if (!uSD_is_attached) {
Serial.println(F("WARNING: unable to attach micro-SD card."));
} else {
// The number of 512 byte sectors in the card
// or zero if an error occurs.
size_t cardSize = uSD.card()->cardSize();

if (cardSize == 0) {
Serial.println(F("WARNING: invalid micro-SD card size."));
} else {
uint8_t cardType = uSD.card()->type();

Serial.print(F("SD Card Type: "));
if(cardType == SD_CARD_TYPE_SD1){
Serial.println(F("V1"));
} else if(cardType == SD_CARD_TYPE_SD2){
Serial.println(F("V2"));
} else if(cardType == SD_CARD_TYPE_SDHC){
Serial.println(F("SDHC"));
} else {
Serial.println(F("UNKNOWN"));
}

Serial.print("SD Card Size: ");
Serial.print(cardSize / (2 * 1024));
Serial.println(" MB");
}
}
Serial.println();
}
#endif /* ENABLE_RECORDER */

Serial.println(F("Data output device(s):"));

Serial.print(F("NMEA - "));
Expand Down
22 changes: 19 additions & 3 deletions software/firmware/source/SoftRF/src/platform/CH32.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,28 @@ struct rst_info {
#define SOC_GPIO_YD_BUTTON PB_3

// uSD
#define SOC_GPIO_YD_SD_D0 PC_8
#if defined(PC8)
#define SOC_GPIO_YD_SD_D0 PC8
#else
#define SOC_GPIO_YD_SD_D0 PC_8 /* MISO */
#endif
#define SOC_GPIO_YD_SD_D1 PC_9
#define SOC_GPIO_YD_SD_D2 PC_10
#define SOC_GPIO_YD_SD_D3 PC_11
#if defined(PC11)
#define SOC_GPIO_YD_SD_D3 PC11
#else
#define SOC_GPIO_YD_SD_D3 PC_11 /* SS */
#endif
#if defined(PC12)
#define SOC_GPIO_YD_SD_CLK PC12
#else
#define SOC_GPIO_YD_SD_CLK PC_12
#define SOC_GPIO_YD_SD_CMD PD_2
#endif
#if defined(PD2)
#define SOC_GPIO_YD_SD_CMD PD2
#else
#define SOC_GPIO_YD_SD_CMD PD_2 /* MOSI */
#endif
#define SOC_GPIO_YD_SD_SW PD_7

// W25Q32
Expand Down

0 comments on commit 8d2ebb3

Please sign in to comment.