Ejemplo n.º 1
0
/**
 * \brief Translates address/size access of a nand_flash_model to block, page
 * and offset values.
 *
 * \param model  nand_flash_model instance.
 * \param address  Access address.
 * \param size  Access size in bytes.
 * \param block  Stores the first accessed block number.
 * \param page  Stores the first accessed page number inside the first block.
 * \param offset  Stores the byte offset inside the first accessed page.
 *
 * \return 0 if the access is correct; otherwise returns
 * NAND_COMMON_ERROR_OUTOFBOUNDS.
 *
 * \note The values are stored in the provided variables if their pointer is
 * not 0.
 */
uint32_t nand_flash_model_translate_access(const struct nand_flash_model
		*model, uint32_t address, uint32_t size, uint16_t *block,
		uint16_t *page, uint16_t *offset)
{
	/* Check that access is not too big */
	if ((address + size) > nand_flash_model_get_device_size_in_bytes(model)) {
		return NAND_COMMON_ERROR_OUTOFBOUNDS;
	}

	/* Get NAND info */
	uint32_t block_size = nand_flash_model_get_block_size_in_bytes(model);
	uint32_t page_size = nand_flash_model_get_page_data_size(model);

	/* Translate address */
	uint16_t temp_block = address / block_size;
	address -= (temp_block * block_size);
	uint16_t temp_page = address / page_size;
	address -= (temp_page * page_size);
	uint16_t temp_Offset = address;

	/* Save results */
	if (block) {
		*block = temp_block;
	}
	if (page) {
		*page = temp_page;
	}
	if (offset) {
		*offset = temp_Offset;
	}

	return 0;
}
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 */
	}
}