Example #1
0
/**
 * \brief  Initialize a nand_flash_ecc instance.
 *
 * \param ecc  Pointer to an nand_flash_ecc instance.
 * \param model  Pointer to the underlying NAND chip model. Can be 0.
 * \param command_address  Address at which commands are sent.
 * \param address_address  Address at which addresses are sent.
 * \param data_address  Address at which data is sent.
 *
 * \return 0 if the initialization is done; or return the error code.
 */
uint32_t nand_flash_ecc_initialize(struct nand_flash_ecc *ecc,
		const struct nand_flash_model *model,
		uint32_t command_address,
		uint32_t address_address, uint32_t data_address)
{
	uint32_t rc;
	rc = nand_flash_raw_initialize(RAW(ecc),
			model, command_address, address_address, data_address);
	return rc;
}
int main(void)
{
	uint16_t block, page;
	uint32_t i;

	uint32_t error = 0;

	/* Initialize the SAM3 system */
	sysclk_init();
	board_init();

	/* Initialize the console uart */
	configure_console();

	/* Output example information */
	printf("-- NAND Flash Raw Example --\n\r");
	printf("-- %s\n\r", BOARD_NAME);
	printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);

	memset(&nf_raw, 0, sizeof(nf_raw));

	if (nand_flash_raw_initialize(&nf_raw, 0, cmd_address,
			addr_address, data_address)) {

		printf("-E- Device Unknown\n\r");
		return 0;
	}

	printf("-I- NAND Flash driver initialized\n\r");

	/* Get device parameters */
	mem_size = nand_flash_model_get_device_size_in_bytes(&nf_raw.model);
	block_size = nand_flash_model_get_block_size_in_bytes(&nf_raw.model);
	num_block = nand_flash_model_get_device_size_in_blocks(&nf_raw.model);
	page_size = nand_flash_model_get_page_data_size(&nf_raw.model);
	pages_per_block =
			nand_flash_model_get_block_size_in_pages(&nf_raw.model);

	printf("-I- Size of the whole device in bytes : 0x%x \n\r", mem_size);
	printf("-I- Size in bytes of one single block of a device : 0x%x \n\r",
			block_size);
	printf("-I- Number of blocks in the entire device : 0x%x \n\r",
			num_block);
	printf("-I- Size of the data area of a page in bytes : 0x%x \n\r",
			page_size);
	printf("-I- Number of pages in the entire device : 0x%x \n\r",
			pages_per_block);

	/* Erase all block and use the last good block for read/write test */
	for (i = 0; i < num_block; i++) {
		error = nand_flash_raw_erase_block(&nf_raw, i);

		if (error == NAND_COMMON_ERROR_BADBLOCK) {
			printf("-E- Block %u is BAD block. \n\r", i);
		} else {
			block = i;
		}
	}
	/* Prepare a page size buffer in SRAM. */
	printf("-I- Preparing a buffer in SRAM ...\n\r");
	for (i = 0; i < page_size; i++) {
		write_buffer[i] = i & 0xFF;
	}

	/* Reset read buffer. */
	memset(read_buffer, 0, sizeof(read_buffer));

	/* Select a page. */
	page = 0;
	/* Write a page to the NAND Flash. */
	printf("-I- Write the buffer in page %d of block %d without ECC.\n\r",
			page, block);
	error = nand_flash_raw_write_page(&nf_raw, block, page, write_buffer,
			0);
	if (error) {
		printf("-E- Cannot write page %d of block %d.\n\r", page,
				block);
		return error;
	}

	/* Read the page from the NAND Flash. */
	printf("-I- Read page %d of block %d without ECC.\n\r", page, block);
	nand_flash_raw_read_page(&nf_raw, block, page, read_buffer, 0);

	/* Test if the read buffer is the same as SRAM buffer */
	error = memcmp(read_buffer, write_buffer, sizeof(write_buffer));
	if (error) {
		printf("-I- Read data is different from write data.\n\r");
		printf("-I- Need ECC module when handle this page.\n\r");

		return 0;
	}
	printf("-I- Read data matches write data.\n\r");

	printf("-I- Test passed.\n\r");

	while (1) {
		/* Do nothing */
	}
}