1 /******************************************************************************
5 * Instantiation of eeprom routines
7 * Revision information:
9 * 28AUG2004 kb_admin initial creation - adapted from Atmel sources
10 * 12JAN2005 kb_admin fixed clock generation, write polling, init
13 * No warranty, expressed or implied, is included with this software. It is
14 * provided "AS IS" and no warranty of any kind including statutory or aspects
15 * relating to merchantability or fitness for any purpose is provided. All
16 * intellectual property rights of others is maintained with the respective
17 * owners. This software is not copyrighted and is intended for reference
22 *****************************************************************************/
24 #include "at91rm9200_lowlevel.h"
25 #include "at91rm9200.h"
28 /******************************* GLOBALS *************************************/
31 /*********************** PRIVATE FUNCTIONS/DATA ******************************/
34 /* Use a macro to calculate the TWI clock generator value to save code space. */
35 #define AT91C_TWSI_CLOCK 100000
36 #define TWSI_EEPROM_ADDRESS 0x50
38 #define TWI_CLK_BASE_DIV ((AT91C_MASTER_CLOCK/(4*AT91C_TWSI_CLOCK)) - 2)
39 #define SET_TWI_CLOCK ((0x00010000) | (TWI_CLK_BASE_DIV) | (TWI_CLK_BASE_DIV << 8))
42 /*************************** GLOBAL FUNCTIONS ********************************/
46 * .KB_C_FN_DEFINITION_START
47 * void InitEEPROM(void)
48 * This global function initializes the EEPROM interface (TWI). Intended
49 * to be called a single time.
50 * .KB_C_FN_DEFINITION_END
56 AT91PS_TWI twiPtr = (AT91PS_TWI)AT91C_BASE_TWI;
58 AT91PS_PIO pPio = (AT91PS_PIO)AT91C_BASE_PIOA;
59 AT91PS_PMC pPMC = (AT91PS_PMC)AT91C_BASE_PMC;
61 pPio->PIO_ASR = AT91C_PIO_PA25 | AT91C_PIO_PA26;
62 pPio->PIO_PDR = AT91C_PIO_PA25 | AT91C_PIO_PA26;
64 pPio->PIO_MDDR = ~AT91C_PIO_PA25;
65 pPio->PIO_MDER = AT91C_PIO_PA25;
67 pPMC->PMC_PCER = 1u << AT91C_ID_TWI;
69 twiPtr->TWI_IDR = 0xffffffffu;
70 twiPtr->TWI_CR = AT91C_TWI_SWRST;
71 twiPtr->TWI_CR = AT91C_TWI_MSEN | AT91C_TWI_SVDIS;
73 twiPtr->TWI_CWGR = SET_TWI_CLOCK;
78 * .KB_C_FN_DEFINITION_START
79 * void ReadEEPROM(unsigned ee_addr, char *data_addr, unsigned size)
80 * This global function reads data from the eeprom at ee_addr storing data
81 * to data_addr for size bytes. Assume the TWI has been initialized.
82 * This function does not utilize the page read mode to simplify the code.
83 * .KB_C_FN_DEFINITION_END
86 ReadEEPROM(unsigned ee_off, unsigned char *data_addr, unsigned size)
88 const AT91PS_TWI twiPtr = AT91C_BASE_TWI;
92 status = twiPtr->TWI_SR;
93 status = twiPtr->TWI_RHR;
95 // Set the TWI Master Mode Register
96 twiPtr->TWI_MMR = (TWSI_EEPROM_ADDRESS << 16) |
97 AT91C_TWI_IADRSZ_2_BYTE | AT91C_TWI_MREAD;
99 // Set TWI Internal Address Register
100 twiPtr->TWI_IADR = ee_off;
103 twiPtr->TWI_CR = AT91C_TWI_START;
105 status = twiPtr->TWI_SR;
108 // Wait RHR Holding register is full
110 while (!(twiPtr->TWI_SR & AT91C_TWI_RXRDY) && --count > 0)
116 *(data_addr++) = twiPtr->TWI_RHR;
119 twiPtr->TWI_CR = AT91C_TWI_STOP;
121 status = twiPtr->TWI_SR;
123 // Wait transfer is finished
124 while (!(twiPtr->TWI_SR & AT91C_TWI_TXCOMP))
128 *data_addr = twiPtr->TWI_RHR;
134 * .KB_C_FN_DEFINITION_START
135 * void WriteEEPROM(unsigned ee_off, char *data_addr, unsigned size)
136 * This global function writes data to the eeprom at ee_off using data
137 * from data_addr for size bytes. Assume the TWI has been initialized.
138 * This function does not utilize the page write mode as the write time is
139 * much greater than the time required to access the device for byte-write
140 * functionality. This allows the function to be much simpler.
141 * .KB_C_FN_DEFINITION_END
144 WriteEEPROM(unsigned ee_off, char *data_addr, unsigned size)
146 const AT91PS_TWI twiPtr = AT91C_BASE_TWI;
148 unsigned char test_data;
151 if (!(ee_off & 0x3f))
154 // Set the TWI Master Mode Register
155 twiPtr->TWI_MMR = ((TWSI_EEPROM_ADDRESS << 16) |
156 AT91C_TWI_IADRSZ_2_BYTE) & ~AT91C_TWI_MREAD;
158 // Set TWI Internal Address Register
159 twiPtr->TWI_IADR = ee_off++;
161 status = twiPtr->TWI_SR;
163 twiPtr->TWI_THR = *(data_addr++);
165 twiPtr->TWI_CR = AT91C_TWI_START;
167 // Wait transfer is finished
168 while (!(twiPtr->TWI_SR & AT91C_TWI_TXRDY))
171 twiPtr->TWI_CR = AT91C_TWI_STOP;
173 status = twiPtr->TWI_SR;
175 // Wait transfer is finished
176 while (!(twiPtr->TWI_SR & AT91C_TWI_TXCOMP))
179 // wait for write operation to complete
180 ReadEEPROM(ee_off, &test_data, 1);