Beispiel #1
0
/**
 * \brief SD/MMC card read and write test.
 *
 * \param test Current test case.
 */
static void run_sd_mmc_rw_test(const struct test_case *test)
{
	uint32_t i;
	uint32_t last_blocks_addr;
	uint16_t nb_block, nb_trans;
	bool split_tansfer = false;

	/* Compute the last address */
	last_blocks_addr = sd_mmc_get_capacity(0) * (1024/SD_MMC_BLOCK_SIZE) - 50;
	test_assert_true(test, last_blocks_addr > NB_MULTI_BLOCKS,
			"Error: SD/MMC capacity.");

	last_blocks_addr -= NB_MULTI_BLOCKS;
	nb_block = 1;

run_sd_mmc_rw_test_next:

	/* Read (save blocks) the last blocks */
	test_assert_true(test, SD_MMC_OK ==
			sd_mmc_init_read_blocks(0, last_blocks_addr, nb_block),
			"Error: SD/MMC initialize read sector(s).");

	for (nb_trans = 0; nb_trans < (split_tansfer? nb_block : 1); nb_trans++) {
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_start_read_blocks(
				&buf_save[nb_trans * SD_MMC_BLOCK_SIZE],
				split_tansfer? 1 : nb_block),
				"Error: SD/MMC start read sector(s).");
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_wait_end_of_read_blocks(false),
				"Error: SD/MMC wait end of read sector(s).");
	}

	test_assert_true(test, !sd_mmc_is_write_protected(0),
			"Error: SD/MMC is write protected.");

	/* Fill buffer */
	for (i = 0; i < (SD_MMC_BLOCK_SIZE * nb_block / sizeof(uint32_t)); i++) {
		((uint32_t*)buf_test)[i] = TEST_FILL_VALUE_U32;
	}

	/* Write the last blocks */
	test_assert_true(test, SD_MMC_OK ==
			sd_mmc_init_write_blocks(0, last_blocks_addr, nb_block),
			"Error: SD/MMC initialize write sector(s).");

	for (nb_trans = 0; nb_trans < (split_tansfer? nb_block : 1); nb_trans++) {
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_start_write_blocks(
				&buf_test[nb_trans * SD_MMC_BLOCK_SIZE],
				split_tansfer? 1 : nb_block),
				"Error: SD/MMC start write sector(s).");
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_wait_end_of_write_blocks(false),
				"Error: SD/MMC wait end of write sector(s).");
	}

	/* Clean buffer */
	for (i = 0; i < (SD_MMC_BLOCK_SIZE * nb_block / sizeof(uint32_t)); i++) {
		((uint32_t*)buf_test)[i] = 0xFFFFFFFF;
	}

	/* Read the last block */
	test_assert_true(test, SD_MMC_OK ==
			sd_mmc_init_read_blocks(0, last_blocks_addr, nb_block),
			"Error: SD/MMC initialize read sector(s).");

	for (nb_trans = 0; nb_trans < (split_tansfer? nb_block : 1); nb_trans++) {
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_start_read_blocks(
				&buf_test[nb_trans * SD_MMC_BLOCK_SIZE],
				split_tansfer? 1 : nb_block),
				"Error: SD/MMC start read sector(s).");
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_wait_end_of_read_blocks(false),
				"Error: SD/MMC wait end of read sector(s).");
	}

	/* Check buffer */
	for (i = 0; i < (SD_MMC_BLOCK_SIZE * nb_block / sizeof(uint32_t)); i++) {
		test_assert_true(test,
				((uint32_t*)buf_test)[i] == TEST_FILL_VALUE_U32,
				"Error: SD/MMC verify write operation.");
	}

	/* Write (restore) the last block */
	test_assert_true(test, SD_MMC_OK ==
			sd_mmc_init_write_blocks(0, last_blocks_addr, nb_block),
			"Error: SD/MMC initialize write restore sector(s).");

	for (nb_trans = 0; nb_trans < (split_tansfer? nb_block : 1); nb_trans++) {
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_start_write_blocks(
				&buf_save[nb_trans * SD_MMC_BLOCK_SIZE],
				split_tansfer? 1 : nb_block),
				"Error: SD/MMC start write restore sector(s).");
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_wait_end_of_write_blocks(false),
				"Error: SD/MMC wait end of write restore sector(s).");
	}

	/* Read (check restore) the last block */
	test_assert_true(test, SD_MMC_OK ==
			sd_mmc_init_read_blocks(0, last_blocks_addr, nb_block),
			"Error: SD/MMC initialize read sector(s).");

	for (nb_trans = 0; nb_trans < (split_tansfer? nb_block : 1); nb_trans++) {
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_start_read_blocks(
				&buf_test[nb_trans * SD_MMC_BLOCK_SIZE],
				split_tansfer? 1 : nb_block),
				"Error: SD/MMC start read sector(s).");
		test_assert_true(test, SD_MMC_OK ==
				sd_mmc_wait_end_of_read_blocks(false),
				"Error: SD/MMC wait end of read sector(s).");
	}

	/* Check buffer restored */
	for (i = 0; i < (SD_MMC_BLOCK_SIZE * nb_block / sizeof(uint32_t)); i++) {
		test_assert_true(test,
				((uint32_t*)buf_test)[i] == ((uint32_t*)buf_save)[i],
				"Error: SD/MMC verify restore operation.");
	}

	if (nb_block == 1) {
		/* Launch second test */
		nb_block = NB_MULTI_BLOCKS;
		goto run_sd_mmc_rw_test_next;
	}
	if (!split_tansfer) {
		/* Launch third test */
		split_tansfer = true;
		goto run_sd_mmc_rw_test_next;
	}
}
Beispiel #2
0
/**
 * \brief Card R/W tests
 *
 * \param slot   SD/MMC slot to test
 */
static void main_test_memory(uint8_t slot)
{
    uint32_t last_blocks_addr, i, nb_trans;
    REDTIMESTAMP tick_start;
    uint32_t time_ms;

    // Compute the last address
    last_blocks_addr = sd_mmc_get_capacity(slot) *
            (1024 / SD_MMC_BLOCK_SIZE);
    if (last_blocks_addr < (TEST_MEM_START_OFFSET / 512lu)) {
        printf("[Memory is too small.]\n\r");
        return;
    }
    last_blocks_addr -= (TEST_MEM_START_OFFSET / SD_MMC_BLOCK_SIZE);

    printf("Card R/W test:\n\r");

    // Read the last block
    printf("    Read... ");
    tick_start = RedOsTimestamp();
    if (SD_MMC_OK != sd_mmc_init_read_blocks(slot,
            last_blocks_addr,
            TEST_MEM_AREA_SIZE / SD_MMC_BLOCK_SIZE)) {
        printf("[FAIL]\n\r");
        return;
    }
    for (nb_trans = 0; nb_trans < (TEST_MEM_AREA_SIZE / TEST_MEM_ACCESS_SIZE);
            nb_trans++) {
        if (SD_MMC_OK != sd_mmc_start_read_blocks(buf_test,
                    TEST_MEM_ACCESS_SIZE / SD_MMC_BLOCK_SIZE)) {
            printf("[FAIL]\n\r");
            return;
        }
        if (SD_MMC_OK != sd_mmc_wait_end_of_read_blocks(false)) {
            printf("[FAIL]\n\r");
            return;
        }
    }
    time_ms = RedOsTimePassed(tick_start) / 1000U;
    if (time_ms) { // Valid time_ms
        printf(" %d KBps ", (int)(((TEST_MEM_AREA_SIZE
                * 1000lu) / 1024lu) / time_ms));
    }
    printf("[OK]\n\r");

    if (sd_mmc_is_write_protected(slot)) {
        printf("Card is write protected [WRITE TEST SKIPPED]\n\r");
        return;
    }

    // Fill buffer with a pattern
    for (i = 0; i < (TEST_MEM_ACCESS_SIZE / sizeof(uint32_t)); i++) {
        ((uint32_t*)buf_test)[i] = TEST_FILL_VALUE_U32;
    }

    printf("    Write pattern... ");
    if (SD_MMC_OK != sd_mmc_init_write_blocks(slot,
            last_blocks_addr,
            TEST_MEM_AREA_SIZE / SD_MMC_BLOCK_SIZE)) {
        printf("[FAIL]\n\r");
        return;
    }
    tick_start = RedOsTimestamp();
    for (nb_trans = 0; nb_trans < (TEST_MEM_AREA_SIZE / TEST_MEM_ACCESS_SIZE);
            nb_trans++) {
        ((uint32_t*)buf_test)[0] = nb_trans; // Unique value for each area
        if (SD_MMC_OK != sd_mmc_start_write_blocks(buf_test,
                TEST_MEM_ACCESS_SIZE / SD_MMC_BLOCK_SIZE)) {
            printf("[FAIL]\n\r");
            return;
        }
        if (SD_MMC_OK != sd_mmc_wait_end_of_write_blocks(false)) {
            printf("[FAIL]\n\r");
            return;
        }
    }
    time_ms = RedOsTimePassed(tick_start) / 1000U;
    if (time_ms) { // Valid time_ms
        printf(" %d KBps ", (int)(((TEST_MEM_AREA_SIZE
                * 1000lu) / 1024lu) / time_ms));
    }
    printf("[OK]\n\r");

    printf("    Read and check pattern... ");
    if (SD_MMC_OK != sd_mmc_init_read_blocks(slot,
            last_blocks_addr,
            TEST_MEM_AREA_SIZE / SD_MMC_BLOCK_SIZE)) {
        printf("Read [FAIL]\n\r");
        return;
    }
    for (nb_trans = 0; nb_trans < (TEST_MEM_AREA_SIZE / TEST_MEM_ACCESS_SIZE);
            nb_trans++) {
        // Clear buffer
        for (i = 0; i < (TEST_MEM_ACCESS_SIZE / sizeof(uint32_t)); i++) {
            ((uint32_t*)buf_test)[i] = 0xFFFFFFFF;
        }
        // Fill buffer
        if (SD_MMC_OK != sd_mmc_start_read_blocks(buf_test,
                    TEST_MEM_ACCESS_SIZE / SD_MMC_BLOCK_SIZE)) {
            printf("Read [FAIL]\n\r");
            return;
        }
        if (SD_MMC_OK != sd_mmc_wait_end_of_read_blocks(false)) {
            printf("Read [FAIL]\n\r");
            return;
        }
        // Check the unique value of the area
        if (((uint32_t*)buf_test)[0] != nb_trans) {
            printf("Check [FAIL]\n\r");
            return;
        }
        // Check buffer
        for (i = 1; i < (TEST_MEM_ACCESS_SIZE / sizeof(uint32_t)); i++) {
            if (((uint32_t*)buf_test)[i] != TEST_FILL_VALUE_U32) {
                printf("Check [FAIL]\n\r");
                return;
            }
        }
    }
    printf("[OK]\n\r");
}
bool sd_mmc_wr_protect(uint8_t slot)
{
	return sd_mmc_is_write_protected(slot);
}