diff --git a/src/blockdevice/sd.c b/src/blockdevice/sd.c index e779f04..e9bf62e 100644 --- a/src/blockdevice/sd.c +++ b/src/blockdevice/sd.c @@ -19,7 +19,7 @@ #include "blockdevice/sd.h" typedef struct { - spi_inst_t *spi_inst; + spi_inst_t* spi_inst; uint8_t mosi; // PICO_DEFAULT_SPI_TX_PIN pin 19 uint8_t miso; // PICO_DEFAULT_SPI_RX_PIN pin 16 uint8_t sclk; // PICO_DEFAULT_SPI_SCK_PIN pin 18 @@ -98,11 +98,11 @@ typedef struct { enum sdcard_type { - SDCARD_NONE = 0, - SDCARD_V1 = 1, - SDCARD_V2 = 2, - SDCARD_V2HC = 3, - CARD_UNKNOWN = 4, + SDCARD_NONE = 0, + SDCARD_V1 = 1, + SDCARD_V2 = 2, + SDCARD_V2HC = 3, + CARD_UNKNOWN = 4, }; enum cmd_supported { @@ -183,8 +183,8 @@ static const uint8_t CRC7_TABLE[256] = { 0x46, 0x4F, 0x54, 0x5D, 0x62, 0x6B, 0x70, 0x79 }; -static uint8_t _crc7(const void *buffer, size_t length) { - const uint8_t *b = buffer; +static uint8_t _crc7(const void* buffer, size_t length) { + const uint8_t* b = buffer; uint8_t crc = 0; for (size_t i = 0; i < length; i++) crc = CRC7_TABLE[(crc << 1) ^ b[i]]; @@ -226,30 +226,30 @@ static const uint16_t CRC16_TABLE[256] = { 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 }; -static uint16_t _crc16(const void *buffer, size_t length) { - const uint8_t *data = buffer; +static uint16_t _crc16(const void* buffer, size_t length) { + const uint8_t* data = buffer; uint16_t crc = 0; for (size_t i = 0; i < length; i++) crc = (crc << 8) ^ CRC16_TABLE[((crc >> 8) ^ data[i]) & 0x00FF]; return crc; } -static uint8_t _spi_write(void *_config, uint8_t data) { - blockdevice_sd_config_t *config = _config; - uint8_t *buffer = (uint8_t *)&data; +static uint8_t _spi_write(void* _config, uint8_t data) { + blockdevice_sd_config_t* config = _config; + uint8_t* buffer = (uint8_t*)&data; spi_write_read_blocking(config->spi_inst, buffer, buffer, 1); return (uint8_t)*buffer; } -static void _spi_wait(void *_config, size_t count) { - blockdevice_sd_config_t *config = _config; +static void _spi_wait(void* _config, size_t count) { + blockdevice_sd_config_t* config = _config; for (size_t i = 0; i < count; i++) { spi_write_read_blocking(config->spi_inst, &SPI_FILL_CHAR, NULL, sizeof(SPI_FILL_CHAR)); } } -static void _spi_init(void *_config) { - blockdevice_sd_config_t *config = _config; +static void _spi_init(void* _config) { + blockdevice_sd_config_t* config = _config; gpio_set_function(config->mosi, GPIO_FUNC_SPI); gpio_set_function(config->miso, GPIO_FUNC_SPI); @@ -268,19 +268,19 @@ static void _spi_init(void *_config) { gpio_put(config->cs, old_cs); } -static void _preclock_then_select(void *_config) { - blockdevice_sd_config_t *config = _config; +static void _preclock_then_select(void* _config) { + blockdevice_sd_config_t* config = _config; spi_write_read_blocking(config->spi_inst, &SPI_FILL_CHAR, NULL, sizeof(SPI_FILL_CHAR)); gpio_put(config->cs, 0); } -static void _postclock_then_deselect(void *_config) { - blockdevice_sd_config_t *config = _config; +static void _postclock_then_deselect(void* _config) { + blockdevice_sd_config_t* config = _config; spi_write_read_blocking(config->spi_inst, &SPI_FILL_CHAR, NULL, sizeof(SPI_FILL_CHAR)); gpio_put(config->cs, 1); } -static inline void debug_if(int condition, const char *format, ...) { +static inline void debug_if(int condition, const char* format, ...) { if (condition) { va_list args; va_start(args, format); @@ -289,8 +289,8 @@ static inline void debug_if(int condition, const char *format, ...) { } } -static bool _wait_token(void *_config, uint8_t token) { - blockdevice_sd_config_t *config = _config; +static bool _wait_token(void* _config, uint8_t token) { + blockdevice_sd_config_t* config = _config; uint32_t t = to_ms_since_boot(get_absolute_time()); do { @@ -302,8 +302,8 @@ static bool _wait_token(void *_config, uint8_t token) { return false; } -static bool _wait_ready(void *_config, uint64_t timeout) { - blockdevice_sd_config_t *config = _config; +static bool _wait_ready(void* _config, uint64_t timeout) { + blockdevice_sd_config_t* config = _config; uint8_t response; uint32_t t = to_ms_since_boot(get_absolute_time()); do { @@ -316,10 +316,10 @@ static bool _wait_ready(void *_config, uint64_t timeout) { } -static uint8_t _cmd_spi(void *_config, int cmd, uint32_t arg) { - blockdevice_sd_config_t *config = _config; +static uint8_t _cmd_spi(void* _config, int cmd, uint32_t arg) { + blockdevice_sd_config_t* config = _config; uint8_t response; - uint8_t cmd_packet[PACKET_SIZE] = {0}; + uint8_t cmd_packet[PACKET_SIZE] = { 0 }; // Prepare the command packet cmd_packet[0] = SPI_CMD(cmd); @@ -331,7 +331,8 @@ static uint8_t _cmd_spi(void *_config, int cmd, uint32_t arg) { if (config->enable_crc) { uint8_t crc = _crc7(cmd_packet, 5); cmd_packet[5] = (crc << 1) | 0x01; - } else { + } + else { switch (cmd) { case CMD0_GO_IDLE_STATE: cmd_packet[5] = 0x95; @@ -347,7 +348,7 @@ static uint8_t _cmd_spi(void *_config, int cmd, uint32_t arg) { // send a command for (int i = 0; i < PACKET_SIZE; i++) { - spi_write_read_blocking(config->spi_inst, (const uint8_t *)&cmd_packet[i], NULL, 1); + spi_write_read_blocking(config->spi_inst, (const uint8_t*)&cmd_packet[i], NULL, 1); } // The received byte immediataly following CMD12 is a stuff byte, @@ -366,8 +367,8 @@ static uint8_t _cmd_spi(void *_config, int cmd, uint32_t arg) { return response; } -static int _cmd(void *_config, int cmd, uint32_t arg, bool is_acmd, uint32_t *resp) { - blockdevice_sd_config_t *config = _config; +static int _cmd(void* _config, int cmd, uint32_t arg, bool is_acmd, uint32_t* resp) { + blockdevice_sd_config_t* config = _config; int32_t status = BD_ERROR_OK; uint32_t response; @@ -428,37 +429,38 @@ static int _cmd(void *_config, int cmd, uint32_t arg, bool is_acmd, uint32_t *re // Set status for other errors if ((response & R1_ERASE_RESET) || (response & R1_ERASE_SEQUENCE_ERROR)) { status = SD_BLOCK_DEVICE_ERROR_ERASE; // Erase error - } else if ((response & R1_ADDRESS_ERROR) || (response & R1_PARAMETER_ERROR)) { + } + else if ((response & R1_ADDRESS_ERROR) || (response & R1_PARAMETER_ERROR)) { // Misaligned address / invalid address block length status = SD_BLOCK_DEVICE_ERROR_PARAMETER; } // Get rest of the response part for other commands switch (cmd) { - case CMD8_SEND_IF_COND: // Response R7 - debug_if(SD_DBG, "V2-Version Card\n"); - config->card_type = SDCARD_V2; // fallthrough + case CMD8_SEND_IF_COND: // Response R7 + debug_if(SD_DBG, "V2-Version Card\n"); + config->card_type = SDCARD_V2; // fallthrough // Note: No break here, need to read rest of the response - case CMD58_READ_OCR: // Response R3 - response = (_spi_write(config, SPI_FILL_CHAR) << 24); - response |= (_spi_write(config, SPI_FILL_CHAR) << 16); - response |= (_spi_write(config, SPI_FILL_CHAR) << 8); - response |= _spi_write(config, SPI_FILL_CHAR); - debug_if(SD_DBG, "R3/R7: 0x%" PRIx32 "\n", response); - break; + case CMD58_READ_OCR: // Response R3 + response = (_spi_write(config, SPI_FILL_CHAR) << 24); + response |= (_spi_write(config, SPI_FILL_CHAR) << 16); + response |= (_spi_write(config, SPI_FILL_CHAR) << 8); + response |= _spi_write(config, SPI_FILL_CHAR); + debug_if(SD_DBG, "R3/R7: 0x%" PRIx32 "\n", response); + break; - case CMD12_STOP_TRANSMISSION: // Response R1b - case CMD38_ERASE: - _wait_ready(config, SD_COMMAND_TIMEOUT); - break; + case CMD12_STOP_TRANSMISSION: // Response R1b + case CMD38_ERASE: + _wait_ready(config, SD_COMMAND_TIMEOUT); + break; - case ACMD13_SD_STATUS: // Response R2 - response = _spi_write(config, SPI_FILL_CHAR); - debug_if(SD_DBG, "R2: 0x%" PRIx32 "\n", response); - break; + case ACMD13_SD_STATUS: // Response R2 + response = _spi_write(config, SPI_FILL_CHAR); + debug_if(SD_DBG, "R2: 0x%" PRIx32 "\n", response); + break; - default: // Response R1 - break; + default: // Response R1 + break; } // Pass the updated response to the command @@ -468,9 +470,9 @@ static int _cmd(void *_config, int cmd, uint32_t arg, bool is_acmd, uint32_t *re // Do not deselect card if read is in progress. if (((CMD9_SEND_CSD == cmd) || (ACMD22_SEND_NUM_WR_BLOCKS == cmd) || - (CMD24_WRITE_BLOCK == cmd) || (CMD25_WRITE_MULTIPLE_BLOCK == cmd) || - (CMD17_READ_SINGLE_BLOCK == cmd) || (CMD18_READ_MULTIPLE_BLOCK == cmd)) - && (BD_ERROR_OK == status)) { + (CMD24_WRITE_BLOCK == cmd) || (CMD25_WRITE_MULTIPLE_BLOCK == cmd) || + (CMD17_READ_SINGLE_BLOCK == cmd) || (CMD18_READ_MULTIPLE_BLOCK == cmd)) + && (BD_ERROR_OK == status)) { return BD_ERROR_OK; } // Deselect card @@ -478,8 +480,8 @@ static int _cmd(void *_config, int cmd, uint32_t arg, bool is_acmd, uint32_t *re return status; } -static int _cmd8(void *_config) { - blockdevice_sd_config_t *config = _config; +static int _cmd8(void* _config) { + blockdevice_sd_config_t* config = _config; uint32_t arg = (CMD8_PATTERN << 0); // [7:0]check pattern uint32_t response = 0; int32_t status = BD_ERROR_OK; @@ -499,8 +501,8 @@ static int _cmd8(void *_config) { return status; } -static uint32_t _go_idle_state(void *_config) { - blockdevice_sd_config_t *config = _config; +static uint32_t _go_idle_state(void* _config) { + blockdevice_sd_config_t* config = _config; uint32_t response; for (size_t i = 0; i < SD_CMD0_GO_IDLE_STATE_RETRIES; i++) { @@ -513,8 +515,8 @@ static uint32_t _go_idle_state(void *_config) { return response; } -static int init_card(void *_config) { - blockdevice_sd_config_t *config = _config; +static int init_card(void* _config) { + blockdevice_sd_config_t* config = _config; int32_t status = BD_ERROR_OK; uint32_t response, arg; @@ -577,11 +579,13 @@ static int init_card(void *_config) { if (response & OCR_HCS_CCS) { config->card_type = SDCARD_V2HC; debug_if(SD_DBG, "Card Initialized: High Capacity Card \n"); - } else { + } + else { debug_if(SD_DBG, "Card Initialized: Standard Capacity Card: Version 2.x \n"); } } - } else { + } + else { config->card_type = SDCARD_V1; debug_if(SD_DBG, "Card Initialized: Version 1.x Card\n"); } @@ -589,13 +593,14 @@ static int init_card(void *_config) { if (!config->enable_crc) { // Disable CRC status = _cmd(config, CMD59_CRC_ON_OFF, config->enable_crc, 0x0, &response); - } else { + } + else { status = _cmd(config, CMD59_CRC_ON_OFF, 0, 0x0, &response); } return status; } -static uint32_t ext_bits(unsigned char *data, int msb, int lsb) +static uint32_t ext_bits(unsigned char* data, int msb, int lsb) { uint32_t bits = 0; uint32_t size = 1 + msb - lsb; @@ -609,8 +614,8 @@ static uint32_t ext_bits(unsigned char *data, int msb, int lsb) return bits; } -static int _read_bytes(void *_config, uint8_t *buffer, uint32_t length) { - blockdevice_sd_config_t *config = _config; +static int _read_bytes(void* _config, uint8_t* buffer, uint32_t length) { + blockdevice_sd_config_t* config = _config; uint16_t crc; // read until start byte (0xFE) @@ -634,7 +639,7 @@ static int _read_bytes(void *_config, uint8_t *buffer, uint32_t length) { // Compute and verify checksum if (crc_result != crc) { debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx32 "\n", - crc, crc_result); + crc, crc_result); _postclock_then_deselect(config); return SD_BLOCK_DEVICE_ERROR_CRC; } @@ -643,8 +648,8 @@ static int _read_bytes(void *_config, uint8_t *buffer, uint32_t length) { return 0; } -static uint64_t _sd_sectors(void *_config) { - blockdevice_sd_config_t *config = _config; +static uint64_t _sd_sectors(void* _config) { + blockdevice_sd_config_t* config = _config; uint32_t c_size, c_size_mult, read_bl_len; uint32_t block_len, mult, blocknr; uint32_t hc_c_size; @@ -664,57 +669,59 @@ static uint64_t _sd_sectors(void *_config) { // csd_structure : csd[127:126] int csd_structure = ext_bits(csd, 127, 126); switch (csd_structure) { - case 0: - c_size = ext_bits(csd, 73, 62); // c_size : csd[73:62] - c_size_mult = ext_bits(csd, 49, 47); // c_size_mult : csd[49:47] - read_bl_len = ext_bits(csd, 83, 80); // read_bl_len : csd[83:80] - the *maximum* read block length - block_len = 1 << read_bl_len; // BLOCK_LEN = 2^READ_BL_LEN - mult = 1 << (c_size_mult + 2); // MULT = 2^C_SIZE_MULT+2 (C_SIZE_MULT < 8) - blocknr = (c_size + 1) * mult; // BLOCKNR = (C_SIZE+1) * MULT - capacity = (uint64_t)blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN - blocks = capacity / config->block_size; - debug_if(SD_DBG, "Standard Capacity: c_size: %" PRIu32 " \n", c_size); - - // ERASE_BLK_EN = 1: Erase in multiple of 512 bytes supported - if (ext_bits(csd, 46, 46)) { - config->erase_size = BLOCK_SIZE_HC; - } else { - // ERASE_BLK_EN = 1: Erase in multiple of SECTOR_SIZE supported - config->erase_size = BLOCK_SIZE_HC * (ext_bits(csd, 45, 39) + 1); - } - break; - - case 1: - hc_c_size = ext_bits(csd, 69, 48); // device size : C_SIZE : [69:48] - blocks = (hc_c_size + 1) << 10; // block count = C_SIZE+1) * 1K byte (512B is block size) - debug_if(SD_DBG, "SDHC/SDXC Card: hc_c_size: %" PRIu32 " \n", hc_c_size); - // ERASE_BLK_EN is fixed to 1, which means host can erase one or multiple of 512 bytes. + case 0: + c_size = ext_bits(csd, 73, 62); // c_size : csd[73:62] + c_size_mult = ext_bits(csd, 49, 47); // c_size_mult : csd[49:47] + read_bl_len = ext_bits(csd, 83, 80); // read_bl_len : csd[83:80] - the *maximum* read block length + block_len = 1 << read_bl_len; // BLOCK_LEN = 2^READ_BL_LEN + mult = 1 << (c_size_mult + 2); // MULT = 2^C_SIZE_MULT+2 (C_SIZE_MULT < 8) + blocknr = (c_size + 1) * mult; // BLOCKNR = (C_SIZE+1) * MULT + capacity = (uint64_t)blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN + blocks = capacity / config->block_size; + debug_if(SD_DBG, "Standard Capacity: c_size: %" PRIu32 " \n", c_size); + + // ERASE_BLK_EN = 1: Erase in multiple of 512 bytes supported + if (ext_bits(csd, 46, 46)) { config->erase_size = BLOCK_SIZE_HC; - break; + } + else { + // ERASE_BLK_EN = 1: Erase in multiple of SECTOR_SIZE supported + config->erase_size = BLOCK_SIZE_HC * (ext_bits(csd, 45, 39) + 1); + } + break; - default: - debug_if(SD_DBG, "CSD struct unsupported\r\n"); - return 0; + case 1: + hc_c_size = ext_bits(csd, 69, 48); // device size : C_SIZE : [69:48] + blocks = (hc_c_size + 1) << 10; // block count = C_SIZE+1) * 1K byte (512B is block size) + debug_if(SD_DBG, "SDHC/SDXC Card: hc_c_size: %" PRIu32 " \n", hc_c_size); + // ERASE_BLK_EN is fixed to 1, which means host can erase one or multiple of 512 bytes. + config->erase_size = BLOCK_SIZE_HC; + break; + + default: + debug_if(SD_DBG, "CSD struct unsupported\r\n"); + return 0; }; return blocks; } -static int _freq(void *_config) { - blockdevice_sd_config_t *config = _config; +static int _freq(void* _config) { + blockdevice_sd_config_t* config = _config; // Max frequency supported is 25MHZ if (config->hz <= 25000000) { spi_set_baudrate(config->spi_inst, config->hz); return 0; - } else { // TODO: Switch function to be implemented for higher frequency + } + else { // TODO: Switch function to be implemented for higher frequency config->hz = 25000000; spi_set_baudrate(config->spi_inst, config->hz); return -EINVAL; } } -static int init(blockdevice_t *device) { - blockdevice_sd_config_t *config = device->config; +static int init(blockdevice_t* device) { + blockdevice_sd_config_t* config = device->config; mutex_enter_blocking(&config->_mutex); int err = init_card(config); @@ -751,30 +758,30 @@ static int init(blockdevice_t *device) { return BD_ERROR_OK; } -static bool is_valid_read(blockdevice_t *device, bd_size_t addr, bd_size_t size) { +static bool is_valid_read(blockdevice_t* device, bd_size_t addr, bd_size_t size) { return (addr % device->read_size == 0 && - size % device->read_size == 0 && - addr + size <= device->size(device)); + size % device->read_size == 0 && + addr + size <= device->size(device)); } -static bool is_valid_program(blockdevice_t *device, bd_size_t addr, bd_size_t size) { +static bool is_valid_program(blockdevice_t* device, bd_size_t addr, bd_size_t size) { return (addr % device->program_size == 0 && - size % device->program_size == 0 && - addr + size <= device->size(device)); + size % device->program_size == 0 && + addr + size <= device->size(device)); } -static int deinit(blockdevice_t *device) { +static int deinit(blockdevice_t* device) { device->is_initialized = false; return 0; } -static int sync(blockdevice_t *device) { +static int sync(blockdevice_t* device) { (void)device; return 0; } -static int _read(void *_config, uint8_t *buffer, uint32_t length) { - blockdevice_sd_config_t *config = _config; +static int _read(void* _config, uint8_t* buffer, uint32_t length) { + blockdevice_sd_config_t* config = _config; uint16_t crc; @@ -795,7 +802,7 @@ static int _read(void *_config, uint8_t *buffer, uint32_t length) { uint16_t crc_result = _crc16(buffer, length); if (crc_result != crc) { debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 "\n", - crc, crc_result); + crc, crc_result); return SD_BLOCK_DEVICE_ERROR_CRC; } } @@ -803,8 +810,8 @@ static int _read(void *_config, uint8_t *buffer, uint32_t length) { return 0; } -static int read(blockdevice_t *device, const void *_buffer, bd_size_t addr, bd_size_t size) { - blockdevice_sd_config_t *config = device->config; +static int read(blockdevice_t* device, const void* _buffer, bd_size_t addr, bd_size_t size) { + blockdevice_sd_config_t* config = device->config; mutex_enter_blocking(&config->_mutex); if (!is_valid_read(device, addr, size)) { @@ -817,9 +824,9 @@ static int read(blockdevice_t *device, const void *_buffer, bd_size_t addr, bd_s return SD_BLOCK_DEVICE_ERROR_PARAMETER; } - uint8_t *buffer = (uint8_t *)_buffer; + uint8_t* buffer = (uint8_t*)_buffer; int status = BD_ERROR_OK; - size_t block_count = size / config->block_size; + size_t block_count = size / config->block_size; // SDSC Card (CCS=0) uses byte unit address // SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit) @@ -830,7 +837,8 @@ static int read(blockdevice_t *device, const void *_buffer, bd_size_t addr, bd_s // Write command ro receive data if (block_count > 1) { status = _cmd(config, CMD18_READ_MULTIPLE_BLOCK, addr, 0, NULL); - } else { + } + else { status = _cmd(config, CMD17_READ_SINGLE_BLOCK, addr, 0, NULL); } if (BD_ERROR_OK != status) { @@ -858,8 +866,8 @@ static int read(blockdevice_t *device, const void *_buffer, bd_size_t addr, bd_s return status; } -static uint8_t _write(void *_config, const uint8_t *buffer, uint8_t token, uint32_t length) { - blockdevice_sd_config_t *config = _config; +static uint8_t _write(void* _config, const uint8_t* buffer, uint8_t token, uint32_t length) { + blockdevice_sd_config_t* config = _config; uint32_t crc = (~0); uint8_t response = 0xFF; @@ -891,8 +899,8 @@ static uint8_t _write(void *_config, const uint8_t *buffer, uint8_t token, uint3 } -static int program(blockdevice_t *device, const void *_buffer, bd_size_t addr, bd_size_t size) { - blockdevice_sd_config_t *config = device->config; +static int program(blockdevice_t* device, const void* _buffer, bd_size_t addr, bd_size_t size) { + blockdevice_sd_config_t* config = device->config; mutex_enter_blocking(&config->_mutex); if (!is_valid_program(device, addr, size)) { @@ -905,7 +913,7 @@ static int program(blockdevice_t *device, const void *_buffer, bd_size_t addr, b return SD_BLOCK_DEVICE_ERROR_NO_INIT; } - const uint8_t *buffer = (const uint8_t *)_buffer; + const uint8_t* buffer = (const uint8_t*)_buffer; int status = BD_ERROR_OK; uint8_t response; @@ -934,7 +942,8 @@ static int program(blockdevice_t *device, const void *_buffer, bd_size_t addr, b debug_if(SD_DBG, "Single Block Write failed: 0x%x \n", response); status = SD_BLOCK_DEVICE_ERROR_WRITE; } - } else { + } + else { // Pre-erase setting prior to multiple block write operation _cmd(config, ACMD23_SET_WR_BLK_ERASE_COUNT, block_count, 1, NULL); @@ -966,23 +975,23 @@ static int program(blockdevice_t *device, const void *_buffer, bd_size_t addr, b return status; } -static int erase(blockdevice_t *device, bd_size_t addr, bd_size_t size) { +static int erase(blockdevice_t* device, bd_size_t addr, bd_size_t size) { (void)device; (void)addr; (void)size; return 0; } -static bool _is_valid_trim(blockdevice_t *device, bd_size_t addr, bd_size_t size) { - blockdevice_sd_config_t *config = device->config; +static bool _is_valid_trim(blockdevice_t* device, bd_size_t addr, bd_size_t size) { + blockdevice_sd_config_t* config = device->config; return (addr % config->erase_size == 0 && - size % config->erase_size == 0 && - addr + size <= device->size(device)); + size % config->erase_size == 0 && + addr + size <= device->size(device)); } -static int trim(blockdevice_t *device, bd_size_t addr, bd_size_t size) { - blockdevice_sd_config_t *config = device->config; +static int trim(blockdevice_t* device, bd_size_t addr, bd_size_t size) { + blockdevice_sd_config_t* config = device->config; mutex_enter_blocking(&config->_mutex); if (!_is_valid_trim(device, addr, size)) { @@ -1021,39 +1030,43 @@ static int trim(blockdevice_t *device, bd_size_t addr, bd_size_t size) { return status; } -static bd_size_t size(blockdevice_t *device) { - blockdevice_sd_config_t *config = device->config; +static bd_size_t size(blockdevice_t* device) { + blockdevice_sd_config_t* config = device->config; return config->block_size * config->total_sectors; } -blockdevice_t *blockdevice_sd_create(spi_inst_t *spi_inst, - uint8_t mosi, - uint8_t miso, - uint8_t sclk, - uint8_t cs, - uint32_t hz, - bool enable_crc) +blockdevice_t* blockdevice_sd_create(spi_inst_t* spi_inst, + uint8_t mosi, + uint8_t miso, + uint8_t sclk, + uint8_t cs, + uint32_t hz, + bool enable_crc) { - if (spi_inst == spi0) { - assert(mosi == 3 || mosi == 7 || mosi == 19 || mosi == 23); - assert(miso == 0 || miso == 4 || miso == 16 || miso == 20); - assert(sclk == 2 || sclk == 6 || sclk == 18 || sclk == 22); + if (spi_inst == spi0) + { + assert(mosi == 3 || mosi == 7 || mosi == 19 || mosi == 23 || mosi == 35 || mosi == 39); + assert(miso == 0 || miso == 4 || miso == 16 || miso == 20 || miso == 32 || miso == 36); + assert(sclk == 2 || sclk == 6 || sclk == 18 || sclk == 22 || sclk == 34 || sclk == 38); // assert(cs == 1 || cs == 5 || cs == 17 || cs == 21); - } else if (spi_inst == spi1) { - assert(mosi == 11 || mosi == 15 || mosi == 27); - assert(miso == 8 || miso == 12 || mosi == 24 || mosi == 28); - assert(sclk == 10 || sclk == 14 || sclk == 26); + } + else if (spi_inst == spi1) + { + assert(mosi == 11 || mosi == 15 || mosi == 27 || mosi == 31 || mosi == 43 || mosi == 47); + assert(miso == 8 || miso == 12 || miso == 24 || miso == 28 || miso == 40 || miso == 44); + assert(sclk == 10 || sclk == 14 || sclk == 26 || sclk == 30 || sclk == 42 || sclk == 46); // assert(cs == 9 || cs == 13 || cs == 25 || cs == 29); - } else { + } + else { assert(spi_inst == spi0 || spi_inst == spi1); } - blockdevice_t *device = calloc(1, sizeof(blockdevice_t)); + blockdevice_t* device = calloc(1, sizeof(blockdevice_t)); if (device == NULL) { fprintf(stderr, "blockdevice_sd_create: Out of memory\n"); return NULL; } - blockdevice_sd_config_t *config = calloc(1, sizeof(blockdevice_sd_config_t)); + blockdevice_sd_config_t* config = calloc(1, sizeof(blockdevice_sd_config_t)); if (config == NULL) { fprintf(stderr, "blockdevice_sd_create: Out of memory\n"); free(device); @@ -1078,8 +1091,8 @@ blockdevice_t *blockdevice_sd_create(spi_inst_t *spi_inst, config->mosi = mosi; config->miso = miso; config->sclk = sclk; - config->cs = cs; - config->hz = hz; + config->cs = cs; + config->hz = hz; config->enable_crc = enable_crc; config->card_type = SDCARD_NONE; config->block_size = 512; @@ -1090,7 +1103,7 @@ blockdevice_t *blockdevice_sd_create(spi_inst_t *spi_inst, return device; } -void blockdevice_sd_free(blockdevice_t *device) { +void blockdevice_sd_free(blockdevice_t* device) { free(device->config); device->config = NULL; free(device);