FreeBSD/sys/boot/arm/at91/libat91/eeprom.c
/*
 * .KB_C_FN_DEFINITION_START
 * void ReadEEPROM(unsigned ee_addr, char *data_addr, unsigned size)
 *  This global function reads data from the eeprom at ee_addr storing data
 * to data_addr for size bytes.  Assume the TWI has been initialized.
 * This function does not utilize the page read mode to simplify the code.
 * .KB_C_FN_DEFINITION_END
 */
.KB_C_FN_DEFINITION_START
void ReadEEPROM(unsigned ee_addr, char *data_addr, unsigned size)
This global function reads data from the eeprom at ee_addr storing data
to data_addr for size bytes.  Assume the TWI has been initialized.
This function does not utilize the page read mode to simplify the code.
. KB_C_FN_DEFINITION_START void ReadEEPROM ( unsigned ee_addr char * data_addr unsigned size ) This global function reads data from the eeprom at ee_addr storing data to data_addr for size bytes . Assume the TWI has been initialized . This function does not utilize the page read mode to simplify the code .
int
ReadEEPROM(unsigned ee_off, unsigned char *data_addr, unsigned size)
{
	const AT91PS_TWI 	twiPtr = AT91C_BASE_TWI;
	unsigned int status;
	unsigned int count;
	status = twiPtr->TWI_SR;
	status = twiPtr->TWI_RHR;
	twiPtr->TWI_MMR = (TWSI_EEPROM_ADDRESS << 16) |
	    AT91C_TWI_IADRSZ_2_BYTE | AT91C_TWI_MREAD;
	twiPtr->TWI_IADR = ee_off;
	twiPtr->TWI_CR = AT91C_TWI_START;
	status = twiPtr->TWI_SR;
	while (size-- > 1){
		count = 1000000;
		while (!(twiPtr->TWI_SR & AT91C_TWI_RXRDY) && --count > 0)
			continue;
		if (count <= 0)
			return -1;
		*(data_addr++) = twiPtr->TWI_RHR;
	}
	twiPtr->TWI_CR = AT91C_TWI_STOP;
	status = twiPtr->TWI_SR;
	while (!(twiPtr->TWI_SR & AT91C_TWI_TXCOMP))
		continue;
	*data_addr = twiPtr->TWI_RHR;
	return 0;
}
int ReadEEPROM ( unsigned ee_off , unsigned char * data_addr , unsigned size ) { const AT91PS_TWI twiPtr = AT91C_BASE_TWI ; unsigned int status ; unsigned int count ; status = twiPtr -> TWI_SR ; status = twiPtr -> TWI_RHR ; twiPtr -> TWI_MMR = ( TWSI_EEPROM_ADDRESS << 16 ) | AT91C_TWI_IADRSZ_2_BYTE | AT91C_TWI_MREAD ; twiPtr -> TWI_IADR = ee_off ; twiPtr -> TWI_CR = AT91C_TWI_START ; status = twiPtr -> TWI_SR ; while ( size -- > 1 ) { count = 1000000 ; while ( ! ( twiPtr -> TWI_SR & AT91C_TWI_RXRDY ) && -- count > 0 ) continue ; if ( count <= 0 ) return -1 ; * ( data_addr ++ ) = twiPtr -> TWI_RHR ; } twiPtr -> TWI_CR = AT91C_TWI_STOP ; status = twiPtr -> TWI_SR ; while ( ! ( twiPtr -> TWI_SR & AT91C_TWI_TXCOMP ) ) continue ; * data_addr = twiPtr -> TWI_RHR ; return 0 ; }
.KB_C_FN_DEFINITION_START
 void ReadEEPROM(unsigned ee_addr, char *data_addr, unsigned size)
  This global function reads data from the eeprom at ee_addr storing data
 to data_addr for size bytes.
. KB_C_FN_DEFINITION_START void ReadEEPROM ( unsigned ee_addr char * data_addr unsigned size ) This global function reads data from the eeprom at ee_addr storing data to data_addr for size bytes .
// Set the TWI Master Mode Register // Set TWI Internal Address Register // Start transfer // Wait RHR Holding register is full // Read byte // Wait transfer is finished // Read last byte