Exemplo n.º 1
0
bool eep_read(eep_address_t eeprom_address, uint8_t *data_buffer_ptr, eep_length_t length) {
    /* Perform sanity checks. */
    if (EEP_SOURCE_UNSUPPORTED == eep_source) { return false; }
    if (NULL == data_buffer_ptr) { return false; }
    if (0 == length) { return false; }
    
    if (EEP_SOURCE_EXTERNAL == eep_source) {
        return at24cxx_read_continuous(eeprom_address, length, data_buffer_ptr);
    } else {
        do {
            EEGET(*data_buffer_ptr, eeprom_address);
            data_buffer_ptr++;
            eeprom_address++;
            length--;
        } while (0 != length);
        
        return true;
    }
}
/**
 * \brief Application entry point for AT24CXX Component Example.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint32_t i;
	twi_options_t opt;

	/* Initialize the SAM system */
	sysclk_init();

	/* Initialize the board */
	board_init();

	/* Turn off LEDs */
	ioport_set_pin_level(LED0_GPIO, LED0_INACTIVE_LEVEL);
	ioport_set_pin_level(LED1_GPIO, LED1_INACTIVE_LEVEL);

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

	/* Output example information */
	puts(STRING_HEADER);

	/* Configure the options of TWI driver */
	opt.master_clk = sysclk_get_cpu_hz();
	opt.speed = AT24C_TWI_CLK;

	if (twi_master_setup(BOARD_AT24C_TWI_INSTANCE, &opt) != TWI_SUCCESS) {
		puts("AT24CXX initialization is failed.\r");
		ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
		ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
		while (1) {
			/* Capture error */
		}
	}

	/* Fill EEPROM with memory pattern */
	if (at24cxx_fill_pattern(AT24C_MEM_ADDR,
			AT24C_MEM_ADDR + TEST_DATA_LENGTH - 1,
			MEMORY_PATTERN) != AT24C_WRITE_SUCCESS) {
		puts("AT24CXX pattern fill is failed.\r");
		ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
		ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
		while (1) {
			/* Capture error */
		}
	}

	if (at24cxx_read_continuous(AT24C_MEM_ADDR, TEST_DATA_LENGTH,
			test_data_rx) != AT24C_READ_SUCCESS) {
		puts("AT24CXX read packet is failed.\r");
		ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
		ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
		while (1) {
			/* Capture error */
		}
	}

	/* Compare the sent and the received */
	for (i = 0; i < TEST_DATA_LENGTH; i++) {
		if (MEMORY_PATTERN != test_data_rx[i]) {
			/* No match */
			puts("Pattern comparison: Unmatched!\r");
			ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
			ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
			while (1) {
				/* Capture error */
			}
		}
	}
	puts("Pattern comparison: Matched!\r");

	/* Send test pattern to EEPROM */
	if (at24cxx_write_continuous(AT24C_MEM_ADDR, TEST_DATA_LENGTH,
			test_data_tx) != AT24C_WRITE_SUCCESS) {
		puts("AT24CXX write packet is failed.\r");
		ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
		ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
		while (1) {
			/* Capture error */
		}
	}

	/* Get memory from EEPROM */
	if (at24cxx_read_continuous(AT24C_MEM_ADDR, TEST_DATA_LENGTH,
			test_data_rx) != AT24C_READ_SUCCESS) {
		puts("AT24CXX read packet is failed.\r");
		ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
		ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
		while (1) {
			/* Capture error */
		}
	}

	/* Compare the sent and the received */
	for (i = 0; i < TEST_DATA_LENGTH; i++) {
		if (test_data_tx[i] != test_data_rx[i]) {
			/* No match */
			puts("Data comparison: Unmatched!\r");
			ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
			ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
			while (1) {
				/* Capture error */
			}
		}
	}
	/* Match */
	puts("Data comparison: Matched!\r");

	/* Page Operation */
	for (i = 0; i < PAGE_SIZE; i++) {
		page_read_buf[i] = 0;
		page_write_buf[i] = i;
	}

	if (at24cxx_write_page(PAGE_ADDR, PAGE_SIZE, page_write_buf) !=
			AT24C_WRITE_SUCCESS) {
		puts("AT24CXX page write is failed.\r");
		ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
		ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
		while (1) {
			/* Capture error */
		}
	}

	if (at24cxx_read_page(PAGE_ADDR, PAGE_SIZE, page_read_buf) !=
			AT24C_READ_SUCCESS) {
		puts("AT24CXX page read is failed.\r");
		ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
		ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
		while (1) {
			/* Capture error */
		}
	}

	for (i = 0; i < PAGE_SIZE; i++) {
		if (page_read_buf[i] != page_write_buf[i]) {
			/* No match */
			puts("Page comparison: Unmatched!\r");
			ioport_set_pin_level(LED0_GPIO, LED0_ACTIVE_LEVEL);
			ioport_set_pin_level(LED1_GPIO, LED1_ACTIVE_LEVEL);
			while (1) {
				/* Capture error */
			}
		}
	}
	/* Match */
	puts("Page comparison: Matched!\r");
	while (1) {
	}
}