Skip to content

Commit e2e895d

Browse files
Potential fix for code scanning alert no. 271: Poorly documented large function
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent c2a63a1 commit e2e895d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

libraries/SD/src/sd_diskio.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,18 +462,34 @@ struct AcquireSPI {
462462
* FATFS API
463463
* */
464464

465+
/**
466+
* @brief Initialize the SD card and prepare it for use with the FATFS filesystem.
467+
*
468+
* This function performs the low-level initialization sequence for an SD card
469+
* over SPI, following the SD card protocol. It sends the required commands to
470+
* bring the card out of idle state, checks for card type (SDv1, SDv2, SDHC, MMC),
471+
* and configures the card for block access. The function also handles error
472+
* conditions and sets the card status accordingly.
473+
*
474+
* @param pdrv Physical drive number (index into s_cards array).
475+
* @return DSTATUS Status of the disk after initialization.
476+
*/
465477
DSTATUS ff_sd_initialize(uint8_t pdrv) {
466478
char token;
467479
unsigned int resp;
468480
unsigned int start;
481+
// Get the card structure for the given drive number
469482
ardu_sdcard_t *card = s_cards[pdrv];
470483

484+
// If the card is already initialized, return its status
471485
if (!(card->status & STA_NOINIT)) {
472486
return card->status;
473487
}
474488

489+
// Lock the SPI bus and set it to a low frequency (400kHz) for initialization
475490
AcquireSPI card_locked(card, 400000);
476491

492+
// Send at least 74 clock cycles with CS high and MOSI high (send 0xFF)
477493
digitalWrite(card->ssPin, HIGH);
478494
for (uint8_t i = 0; i < 20; i++) {
479495
card->spi->transfer(0XFF);

0 commit comments

Comments
 (0)