[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_2_eng / packages / ti / board / src / flash / platform_flash / evmc66x_i2c_eeprom.c
1 /*
2 * Copyright (c) 2010-2015, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
34 /**
35 *
36 * \file evmc66x_i2c_eeprom.c
37 *
38 * \brief Provides a polling i2c driver.
39 *
40 ******************************************************************************/
42 /************************
43 * Include Files
44 ************************/
45 #include "platform_internal.h"
47 #if (PLATFORM_I2C_EEPROM_IN)
49 /************************
50 * Globals
51 ************************/
52 uint8_t gI2cBlock[68];
54 /******************************************************************************
55 *
56 * Function: i2cEepromWriteBlock
57 *
58 * Description: Writes a specified number of bytes to EEPROM. 2 bytes of write
59 * offset is expected in the first location of 'puiData'.
60 * Data will be written to EEPROM with MSB first. 'uiNumBytes' is
61 * expected to be actual number of data bytes plus write offset
62 * length (2bytes)
63 *
64 * Parameters: uint8_t uchEepromI2cAddress - i2c address of EEPROM
65 * uint32_t *puiData - Pointer to buffer base address
66 * uint32_t uiNumBytes - Number of bytes of buffer
67 * uint32_t uiEndBusState - State on which bus should be left
68 * uint8_t i2cportnumber - i2c port number
69 *
70 * Return Value: I2C_RET - status
71 *
72 ******************************************************************************/
73 I2C_RET i2cEepromWriteBlock( uint8_t uchEepromI2cAddress, uint32_t *puiData,
74 uint32_t uiNumBytes, uint32_t uiEndBusState,
75 uint8_t i2cportnumber)
76 {
77 I2C_RET uiReturnValue;
78 uint32_t writeOffset;
80 writeOffset = *puiData++;
82 uiReturnValue = i2cWriteLong(i2cportnumber, uchEepromI2cAddress,
83 puiData, writeOffset, 2, (uiNumBytes-2),
84 uiEndBusState);
86 return (uiReturnValue);
88 } // i2cEepromWriteBlock
90 /******************************************************************************
91 *
92 * Function: i2cEepromRead
93 *
94 * Description: Reads a fixed number of bytes from an I2C EEPROM. The read
95 * consists of a master write of 2 bytes (forming a 16 bit address,
96 * msb transmitted first), followed by a master read of the
97 * input number of bytes.
98 *
99 * Parameters: uint8_t uchEepromI2cAddress - i2c address of EEPROM
100 * uint8_t *puiData - Pointer to buffer base address
101 * uint32_t uiNumBytes - Number of bytes of buffer
102 * uint32_t uiEndBusState - State on which bus should be left
103 * uint8_t i2cportnumber - i2c port number
104 *
105 * Return Value: I2C_RET - status
106 *
107 ******************************************************************************/
108 I2C_RET i2cEepromRead ( uint32_t byte_addr, uint32_t uiNumBytes,
109 uint8_t *puiData, uint8_t uchEepromI2cAddress,
110 uint8_t i2cportnumber)
111 {
112 I2C_RET uiReturnValue;
114 uiReturnValue = i2cRead (i2cportnumber, uchEepromI2cAddress, puiData,
115 byte_addr, 2, uiNumBytes);
117 return (uiReturnValue);
119 } // i2cEepromRead
121 /******************************************************************************
122 *
123 * Function: i2cEepromWrite
124 *
125 * Description: Writes specified number of bytes to the I2C EEPROM. 2 bytes of
126 * write offset is expected in the first location of 'puiData'.
127 * 'uiNumBytes' is expected to be number of data bytes in 'puiData'
128 * excluding the first location.
129 * Data will be written to EEPROM with MSB first.
130 *
131 * Parameters: uint8_t uchEepromI2cAddress - i2c address of EEPROM
132 * uint32_t *puiData - Pointer to buffer base address
133 * uint32_t uiNumBytes - Number of bytes of buffer
134 * uint32_t uiEndBusState - State on which bus should be left
135 * uint8_t i2cportnumber - i2c port number
136 *
137 * Return Value: I2C_RET - status
138 *
139 ******************************************************************************/
140 I2C_RET i2cEepromWrite( uint16_t uchEepromI2cAddress, uint32_t *puiData,
141 uint32_t uiNumBytes, uint32_t uiEndBusState,
142 uint8_t i2cportnumber)
143 {
144 uint32_t uiBytesWritten;
145 uint32_t uiBlockCount;
146 uint32_t uiSizeBytes;
147 uint32_t uiAddress;
148 I2C_RET iret;
150 uiSizeBytes = uiNumBytes;
151 uiAddress = *puiData;
152 uiBytesWritten = 0;
154 puiData++;
155 /* Block the data into 64 byte blocks, perform a write */
156 do{
157 /* Get the number of bytes in the current block */
158 uiBlockCount = uiSizeBytes - uiBytesWritten;
159 if (uiBlockCount > 64)
160 {
161 uiBlockCount = 64;
162 }
164 if (uiBlockCount == 0)
165 {
166 break;
167 }
169 iret = i2cWriteLong(i2cportnumber, uchEepromI2cAddress,
170 (puiData + uiBytesWritten/4), uiAddress,
171 2, uiBlockCount, uiEndBusState);
172 if (iret != I2C_RET_OK)
173 {
174 return (iret);
175 }
177 i2cDelay(DELAY_CONST);
179 uiBytesWritten = uiBytesWritten + uiBlockCount;
180 uiAddress = uiAddress + uiBlockCount;
182 } while (uiBlockCount);
184 return (I2C_RET_OK);
185 } // i2cEepromWrite
187 /******************************************************************************
188 *
189 * Function: i2cEepromWriteByteAddr
190 *(Same as above function except it takes byte address as a parameter)
191 *
192 * Description: Writes specified number of bytes from the I2C EEPROM
193 *
194 * Parameters: uint8_t uchEepromI2cAddress - i2c address of EEPROM
195 * uint8_t *puiData - Pointer to buffer base address
196 * uint32_t uiNumBytes - Number of bytes of buffer
197 * uint32_t uiEndBusState - State on which bus should be left
198 * uint8_t i2cportnumber - i2c port number
199 *
200 * Return Value: I2C_RET - status
201 *
202 ******************************************************************************/
203 I2C_RET i2cEepromWriteByteAddr( uint32_t byte_addr, uint16_t uchEepromI2cAddress,
204 uint8_t *puiData, uint32_t uiNumBytes,
205 uint32_t uiEndBusState, uint8_t i2cportnumber)
206 {
207 uint32_t uiBytesWritten;
208 uint32_t uiBlockCount;
209 uint32_t uiCount;
210 uint32_t uiSizeBytes;
211 uint32_t uiAddress;
212 I2C_RET iret;
214 uiSizeBytes = uiNumBytes;
215 uiAddress = byte_addr;
216 uiBytesWritten = 0;
218 /* Block the data into 64 byte blocks, perform a write */
219 do{
220 /* Get the number of bytes in the current block */
221 uiBlockCount = uiSizeBytes - uiBytesWritten;
222 if (uiBlockCount > 64)
223 {
224 uiBlockCount = 64;
225 }
227 if (uiBlockCount == 0)
228 {
229 break;
230 }
232 for (uiCount = 0; uiCount < uiBlockCount; uiCount++)
233 {
234 gI2cBlock[uiCount + 2] = puiData[uiBytesWritten + uiCount];
235 }
237 gI2cBlock[0] = (uiAddress >> 8) & 0xff;
238 gI2cBlock[1] = uiAddress & 0xff;
240 iret = i2cWrite(i2cportnumber, uchEepromI2cAddress, gI2cBlock,
241 (uiBlockCount+2), uiEndBusState);
242 if (iret != I2C_RET_OK)
243 {
244 return (iret);
245 }
247 i2cDelay(0x100);
249 uiBytesWritten = uiBytesWritten + uiBlockCount;
250 uiAddress = uiAddress + uiBlockCount;
252 } while (uiBlockCount);
254 return (I2C_RET_OK);
255 } //i2cEepromWriteByteAddr
257 /******************************************************************************
258 *
259 * Function: i2cEepromMacWrite
260 *
261 * Description: Writing MAC addresses to EEPROM.
262 *
263 * Parameters: uint8_t *puchSrc - pointer to mac address
264 * uint8_t i2cportnumber - The i2c port number
265 * uint16_t macI2cAddress - i2c address of EEPROM block
266 * containing MAC address details
267 * uint16_t macOffset - Offset of MAC address in EEPROM
268 * uint8_t macSize - Size of MAC address
269 *
270 * Return Value: I2C_RET - status
271 *
272 ******************************************************************************/
273 I2C_RET i2cEepromMacWrite( uint8_t *puchSrc, uint8_t i2cportnumber,
274 uint16_t macI2cAddress, uint16_t macOffset,
275 uint8_t macSize)
276 {
277 uint32_t uiIndex;
278 I2C_RET returnValue;
280 for(uiIndex = 0; uiIndex < macSize; uiIndex++)
281 {
282 gI2cBlock[uiIndex + 2] = puchSrc[uiIndex];
283 }
285 gI2cBlock[0] = ((macOffset & 0xFF00) >> 8);
286 gI2cBlock[1] = (macOffset & 0xFF);
288 returnValue = i2cWrite(i2cportnumber, macI2cAddress, gI2cBlock,
289 (macSize + 2), I2C_RELEASE_BUS);
291 return (returnValue);
292 }
294 /******************************************************************************
295 *
296 * Function: i2cEepromMacRead
297 *
298 * Description: API to read MAC addresses from I2C EEPROM
299 *
300 * Parameters: uint8_t *puchDst - pointer to buffer for reading mac address
301 * uint8_t i2cportnumber - The i2c port number
302 * uint16_t macI2cAddress - i2c address of EEPROM block
303 * containing MAC address details
304 * uint16_t macOffset - Offset of MAC address in EEPROM
305 * uint8_t macSize - Size of MAC address
306 *
307 * Return Value: I2C_RET - status
308 *
309 ******************************************************************************/
310 I2C_RET i2cEepromMacRead(uint8_t *puchDst, uint8_t i2cportnumber,
311 uint16_t macI2cAddress, uint16_t macOffset,
312 uint8_t macSize)
313 {
314 I2C_RET returnValue;
316 returnValue = i2cRead (i2cportnumber, macI2cAddress, puchDst,
317 macOffset, 2, macSize);
319 return (returnValue);
320 }
322 #endif