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
11 int sm750_hw_i2c_init(
12 unsigned char bus_speed_mode
17 /* Enable GPIO 30 & 31 as IIC clock & data */
18 value
= PEEK32(GPIO_MUX
);
20 value
= FIELD_SET(value
, GPIO_MUX
, 30, I2C
) |
21 FIELD_SET(0, GPIO_MUX
, 31, I2C
);
22 POKE32(GPIO_MUX
, value
);
24 /* Enable Hardware I2C power.
25 TODO: Check if we need to enable GPIO power?
29 /* Enable the I2C Controller and set the bus speed mode */
30 value
= PEEK32(I2C_CTRL
);
31 if (bus_speed_mode
== 0)
32 value
= FIELD_SET(value
, I2C_CTRL
, MODE
, STANDARD
);
34 value
= FIELD_SET(value
, I2C_CTRL
, MODE
, FAST
);
35 value
= FIELD_SET(value
, I2C_CTRL
, EN
, ENABLE
);
36 POKE32(I2C_CTRL
, value
);
41 void sm750_hw_i2c_close(void)
45 /* Disable I2C controller */
46 value
= PEEK32(I2C_CTRL
);
47 value
= FIELD_SET(value
, I2C_CTRL
, EN
, DISABLE
);
48 POKE32(I2C_CTRL
, value
);
50 /* Disable I2C Power */
53 /* Set GPIO 30 & 31 back as GPIO pins */
54 value
= PEEK32(GPIO_MUX
);
55 value
= FIELD_SET(value
, GPIO_MUX
, 30, GPIO
);
56 value
= FIELD_SET(value
, GPIO_MUX
, 31, GPIO
);
57 POKE32(GPIO_MUX
, value
);
60 static long hw_i2c_wait_tx_done(void)
64 /* Wait until the transfer is completed. */
65 timeout
= HWI2C_WAIT_TIMEOUT
;
66 while ((FIELD_GET(PEEK32(I2C_STATUS
), I2C_STATUS
, TX
) != I2C_STATUS_TX_COMPLETED
) &&
77 * This function writes data to the i2c slave device registers.
80 * addr - i2c Slave device address
81 * length - Total number of bytes to be written to the device
82 * buf - The buffer that contains the data to be written to the
86 * Total number of bytes those are actually written.
88 static unsigned int hw_i2c_write_data(
94 unsigned char count
, i
;
95 unsigned int total_bytes
= 0;
97 /* Set the Device Address */
98 POKE32(I2C_SLAVE_ADDRESS
, addr
& ~0x01);
102 * Only 16 byte can be accessed per i2c start instruction.
105 /* Reset I2C by writing 0 to I2C_RESET register to clear the previous status. */
106 POKE32(I2C_RESET
, 0);
108 /* Set the number of bytes to be written */
109 if (length
< MAX_HWI2C_FIFO
)
112 count
= MAX_HWI2C_FIFO
- 1;
113 POKE32(I2C_BYTE_COUNT
, count
);
115 /* Move the data to the I2C data register */
116 for (i
= 0; i
<= count
; i
++)
117 POKE32(I2C_DATA0
+ i
, *buf
++);
120 POKE32(I2C_CTRL
, FIELD_SET(PEEK32(I2C_CTRL
), I2C_CTRL
, CTRL
, START
));
122 /* Wait until the transfer is completed. */
123 if (hw_i2c_wait_tx_done() != 0)
126 /* Substract length */
127 length
-= (count
+ 1);
129 /* Total byte written */
130 total_bytes
+= (count
+ 1);
132 } while (length
> 0);
138 * This function reads data from the slave device and stores them
139 * in the given buffer
142 * addr - i2c Slave device address
143 * length - Total number of bytes to be read
144 * buf - Pointer to a buffer to be filled with the data read
145 * from the slave device. It has to be the same size as the
146 * length to make sure that it can keep all the data read.
149 * Total number of actual bytes read from the slave device
151 static unsigned int hw_i2c_read_data(
157 unsigned char count
, i
;
158 unsigned int total_bytes
= 0;
160 /* Set the Device Address */
161 POKE32(I2C_SLAVE_ADDRESS
, addr
| 0x01);
163 /* Read data and save them to the buffer.
165 * Only 16 byte can be accessed per i2c start instruction.
168 /* Reset I2C by writing 0 to I2C_RESET register to clear all the status. */
169 POKE32(I2C_RESET
, 0);
171 /* Set the number of bytes to be read */
172 if (length
<= MAX_HWI2C_FIFO
)
175 count
= MAX_HWI2C_FIFO
- 1;
176 POKE32(I2C_BYTE_COUNT
, count
);
179 POKE32(I2C_CTRL
, FIELD_SET(PEEK32(I2C_CTRL
), I2C_CTRL
, CTRL
, START
));
181 /* Wait until transaction done. */
182 if (hw_i2c_wait_tx_done() != 0)
185 /* Save the data to the given buffer */
186 for (i
= 0; i
<= count
; i
++)
187 *buf
++ = PEEK32(I2C_DATA0
+ i
);
189 /* Substract length by 16 */
190 length
-= (count
+ 1);
192 /* Number of bytes read. */
193 total_bytes
+= (count
+ 1);
195 } while (length
> 0);
201 * This function reads the slave device's register
204 * deviceAddress - i2c Slave device address which register
206 * registerIndex - Slave device's register to be read
211 unsigned char sm750_hw_i2c_read_reg(
216 unsigned char value
= (0xFF);
218 if (hw_i2c_write_data(addr
, 1, ®
) == 1)
219 hw_i2c_read_data(addr
, 1, &value
);
225 * This function writes a value to the slave device's register
228 * deviceAddress - i2c Slave device address which register
230 * registerIndex - Slave device's register to be written
231 * data - Data to be written to the register
237 int sm750_hw_i2c_write_reg(
243 unsigned char value
[2];
247 if (hw_i2c_write_data(addr
, 2, value
) == 2)
This page took 0.038241 seconds and 6 git commands to generate.