3 #include "ddk750_help.h"
4 #include "ddk750_reg.h"
5 #include "ddk750_hwi2c.h"
6 #include "ddk750_power.h"
8 #define MAX_HWI2C_FIFO 16
9 #define HWI2C_WAIT_TIMEOUT 0xF0000
13 unsigned char busSpeedMode
18 /* Enable GPIO 30 & 31 as IIC clock & data */
19 value = PEEK32(GPIO_MUX);
21 value = FIELD_SET(value, GPIO_MUX, 30, I2C) |
22 FIELD_SET(0, GPIO_MUX, 31, I2C);
23 POKE32(GPIO_MUX, value);
25 /* Enable Hardware I2C power.
26 TODO: Check if we need to enable GPIO power?
30 /* Enable the I2C Controller and set the bus speed mode */
31 value = PEEK32(I2C_CTRL);
32 if (busSpeedMode == 0)
33 value = FIELD_SET(value, I2C_CTRL, MODE, STANDARD);
35 value = FIELD_SET(value, I2C_CTRL, MODE, FAST);
36 value = FIELD_SET(value, I2C_CTRL, EN, ENABLE);
37 POKE32(I2C_CTRL, value);
47 /* Disable I2C controller */
48 value = PEEK32(I2C_CTRL);
49 value = FIELD_SET(value, I2C_CTRL, EN, DISABLE);
50 POKE32(I2C_CTRL, value);
52 /* Disable I2C Power */
55 /* Set GPIO 30 & 31 back as GPIO pins */
56 value = PEEK32(GPIO_MUX);
57 value = FIELD_SET(value, GPIO_MUX, 30, GPIO);
58 value = FIELD_SET(value, GPIO_MUX, 31, GPIO);
59 POKE32(GPIO_MUX, value);
63 static long hwI2CWaitTXDone(void)
67 /* Wait until the transfer is completed. */
68 timeout = HWI2C_WAIT_TIMEOUT;
69 while ((FIELD_GET(PEEK32(I2C_STATUS), I2C_STATUS, TX) != I2C_STATUS_TX_COMPLETED) &&
82 * This function writes data to the i2c slave device registers.
85 * deviceAddress - i2c Slave device address
86 * length - Total number of bytes to be written to the device
87 * pBuffer - The buffer that contains the data to be written to the
91 * Total number of bytes those are actually written.
93 static unsigned int hwI2CWriteData(
94 unsigned char deviceAddress,
96 unsigned char *pBuffer
99 unsigned char count, i;
100 unsigned int totalBytes = 0;
102 /* Set the Device Address */
103 POKE32(I2C_SLAVE_ADDRESS, deviceAddress & ~0x01);
107 * Only 16 byte can be accessed per i2c start instruction.
111 /* Reset I2C by writing 0 to I2C_RESET register to clear the previous status. */
112 POKE32(I2C_RESET, 0);
114 /* Set the number of bytes to be written */
115 if (length < MAX_HWI2C_FIFO)
118 count = MAX_HWI2C_FIFO - 1;
119 POKE32(I2C_BYTE_COUNT, count);
121 /* Move the data to the I2C data register */
122 for (i = 0; i <= count; i++)
123 POKE32(I2C_DATA0 + i, *pBuffer++);
126 POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
128 /* Wait until the transfer is completed. */
129 if (hwI2CWaitTXDone() != 0)
132 /* Substract length */
133 length -= (count + 1);
135 /* Total byte written */
136 totalBytes += (count + 1);
138 } while (length > 0);
147 * This function reads data from the slave device and stores them
148 * in the given buffer
151 * deviceAddress - i2c Slave device address
152 * length - Total number of bytes to be read
153 * pBuffer - Pointer to a buffer to be filled with the data read
154 * from the slave device. It has to be the same size as the
155 * length to make sure that it can keep all the data read.
158 * Total number of actual bytes read from the slave device
160 static unsigned int hwI2CReadData(
161 unsigned char deviceAddress,
163 unsigned char *pBuffer
166 unsigned char count, i;
167 unsigned int totalBytes = 0;
169 /* Set the Device Address */
170 POKE32(I2C_SLAVE_ADDRESS, deviceAddress | 0x01);
172 /* Read data and save them to the buffer.
174 * Only 16 byte can be accessed per i2c start instruction.
178 /* Reset I2C by writing 0 to I2C_RESET register to clear all the status. */
179 POKE32(I2C_RESET, 0);
181 /* Set the number of bytes to be read */
182 if (length <= MAX_HWI2C_FIFO)
185 count = MAX_HWI2C_FIFO - 1;
186 POKE32(I2C_BYTE_COUNT, count);
189 POKE32(I2C_CTRL, FIELD_SET(PEEK32(I2C_CTRL), I2C_CTRL, CTRL, START));
191 /* Wait until transaction done. */
192 if (hwI2CWaitTXDone() != 0)
195 /* Save the data to the given buffer */
196 for (i = 0; i <= count; i++)
197 *pBuffer++ = PEEK32(I2C_DATA0 + i);
199 /* Substract length by 16 */
200 length -= (count + 1);
202 /* Number of bytes read. */
203 totalBytes += (count + 1);
205 } while (length > 0);
214 * This function reads the slave device's register
217 * deviceAddress - i2c Slave device address which register
219 * registerIndex - Slave device's register to be read
224 unsigned char hwI2CReadReg(
225 unsigned char deviceAddress,
226 unsigned char registerIndex
229 unsigned char value = (0xFF);
231 if (hwI2CWriteData(deviceAddress, 1, ®isterIndex) == 1)
232 hwI2CReadData(deviceAddress, 1, &value);
242 * This function writes a value to the slave device's register
245 * deviceAddress - i2c Slave device address which register
247 * registerIndex - Slave device's register to be written
248 * data - Data to be written to the register
255 unsigned char deviceAddress,
256 unsigned char registerIndex,
260 unsigned char value[2];
262 value[0] = registerIndex;
264 if (hwI2CWriteData(deviceAddress, 2, value) == 2)