static int _copy(int argc, char **argv) { int src_block; int dst_block; char tmp_copy[SD_HC_BLOCK_SIZE]; if (argc != 3) { printf("usage: %s src_block dst_block\n", argv[0]); return -1; } src_block = atoi(argv[1]); dst_block = atoi(argv[2]); sd_rw_response_t rd_state; sdcard_spi_read_blocks(card, src_block, tmp_copy, SD_HC_BLOCK_SIZE, 1, &rd_state); if (rd_state != SD_RW_OK) { printf("read error %d (block %d)\n", rd_state, src_block); return -1; } sd_rw_response_t wr_state; sdcard_spi_write_blocks(card, dst_block, tmp_copy, SD_HC_BLOCK_SIZE, 1, &wr_state); if (wr_state != SD_RW_OK) { printf("write error %d (block %d)\n", wr_state, dst_block); return -2; } printf("copy block %d to %d [OK]\n", src_block, dst_block); return 0; }
static int _write(int argc, char **argv) { int bladdr; char *data; int size; bool repeat_data = false; if (argc == 3 || argc == 4) { bladdr = atoi(argv[1]); data = argv[2]; size = strlen(argv[2]); printf("will write '%s' (%d chars) at start of block %d\n", data, size, bladdr); if (argc == 4 && (strcmp("-r", argv[3]) == 0)) { repeat_data = true; puts("the rest of the block will be filled with copies of that string"); } else { puts("the rest of the block will be filled with zeros"); } } else { printf("usage: %s blockaddr string [-r]\n", argv[0]); return -1; } if (size > SD_HC_BLOCK_SIZE) { printf("maximum stringsize to write at once is %d ...aborting\n", SD_HC_BLOCK_SIZE); return -1; } /* copy data to a full-block-sized buffer an fill remaining block space according to -r param*/ char buffer[SD_HC_BLOCK_SIZE]; for (unsigned i = 0; i < sizeof(buffer); i++) { if (repeat_data || ((int)i < size)) { buffer[i] = data[i % size]; } else { buffer[i] = 0; } } sd_rw_response_t state; int chunks_written = sdcard_spi_write_blocks(card, bladdr, buffer, SD_HC_BLOCK_SIZE, 1, &state); if (state != SD_RW_OK) { printf("write error %d (wrote %d/%d blocks)\n", state, chunks_written, 1); return -1; } printf("write block %d [OK]\n", bladdr); return 0; }
/** * @brief writes sectors to disk * * @param[in] pdrv Physical drive nmuber to identify the drive * @param[in] buff Data to be written * @param[in] sector Start sector in LBA * @param[in] count Number of sectors to write * * @return RES_OK if no error occurred * @return RES_NOTRDY if data wasn't written completely */ DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count) { sdcard_spi_t *card = get_sd_card(pdrv); if ((card != NULL) && card->init_done) { sd_rw_response_t state; if (count != sdcard_spi_write_blocks(card, sector, (char *)buff, SD_HC_BLOCK_SIZE, count, &state)) { printf("[ERROR] disk_write: sdcard_spi_write_blocks: %d\n", state); return RES_NOTRDY; } return RES_OK; } return RES_NOTRDY; }