Ejemplo n.º 1
0
/**
 * \brief Returns the number of pages in the entire device.
 *
 * \param model  Pointer to a nand_flash_model instance.
 */
uint32_t nand_flash_model_get_device_size_in_pages(const struct nand_flash_model
		*model)
{
	return (uint32_t) nand_flash_model_get_device_size_in_blocks(model) *
			nand_flash_model_get_block_size_in_pages(model);
}
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 */
	}
}