6060 */
6161#if DEVICE_I2C
6262#include "i2c.h"
63+ #include "wait_api.h"
6364
6465/* See i2c.h for details */
6566void fI2cInit (i2c_t * obj ,PinName sda ,PinName scl )
@@ -135,15 +136,15 @@ void fI2cFrequency(i2c_t *obj, uint32_t hz)
135136int32_t fI2cStart (i2c_t * obj )
136137{
137138 /* Send start bit */
138- obj -> membase -> CMD_REG = I2C_CMD_START ;
139+ SEND_COMMAND ( I2C_CMD_START ) ;
139140 return I2C_API_STATUS_SUCCESS ;
140141}
141142
142143/* See i2c.h for details */
143144int32_t fI2cStop (i2c_t * obj )
144145{
145146 /* Send stop bit */
146- obj -> membase -> CMD_REG = I2C_CMD_STOP ;
147+ SEND_COMMAND ( I2C_CMD_STOP ) ;
147148 if (obj -> membase -> STATUS .WORD & (I2C_STATUS_CMD_FIFO_FULL_BIT |
148149 I2C_STATUS_CMD_FIFO_OFL_BIT |
149150 I2C_STATUS_BUS_ERR_BIT )) {
@@ -154,23 +155,26 @@ int32_t fI2cStop(i2c_t *obj)
154155}
155156
156157/* See i2c.h for details */
157- int32_t fI2cReadB (i2c_t * d , char * buf , int len )
158+ int32_t fI2cReadB (i2c_t * obj , char * buf , int len )
158159{
159160 int32_t read = 0 ;
160161
161162 while (read < len ) {
162163 /* Send read command */
163- d -> membase -> CMD_REG = I2C_CMD_RDAT8 ;
164+ SEND_COMMAND ( I2C_CMD_RDAT8 ) ;
164165 while (!RD_DATA_READY ) {
165166 if (I2C_BUS_ERR_CHECK ) {
166167 /* Bus error occured */
167168 return I2C_ERROR_BUS_BUSY ;
168169 }
169170 }
170- buf [read ++ ] = d -> membase -> RD_FIFO_REG ; /**< Reading 'read FIFO register' will clear status register */
171+ buf [read ++ ] = obj -> membase -> RD_FIFO_REG ; /**< Reading 'read FIFO register' will clear status register */
171172
172173 if (!(read >=len )) { /* No ACK will be generated for the last read, upper level I2C protocol should generate */
173- d -> membase -> CMD_REG = I2C_CMD_WDAT0 ; /* TODO based on requirement generate ACK or NACK Based on the requirement. */
174+ SEND_COMMAND (I2C_CMD_WDAT0 ); /* TODO based on requirement generate ACK or NACK Based on the requirement. */
175+ } else {
176+ /* No ack */
177+ SEND_COMMAND (I2C_CMD_WDAT1 );
174178 }
175179
176180 /* check for FIFO underflow */
@@ -187,42 +191,49 @@ int32_t fI2cReadB(i2c_t *d, char *buf, int len)
187191}
188192
189193/* See i2c.h for details */
190- int32_t fI2cWriteB (i2c_t * d , const char * buf , int len )
194+ int32_t fI2cWriteB (i2c_t * obj , const char * buf , int len )
191195{
192196 int32_t write = 0 ;
193197
194198 while (write < len ) {
195199 /* Send write command */
196- d -> membase -> CMD_REG = I2C_CMD_WDAT8 ;
200+ SEND_COMMAND (I2C_CMD_WDAT8 );
201+
197202 if (buf [write ] == I2C_CMD_RDAT8 ) {
198203 /* SW work around to counter FSM issue. If the only command in the CMD FIFO is the WDAT8 command (data of 0x13)
199204 then as the command is read out (i.e. the FIFO goes empty), the WDAT8 command will be misinterpreted as a
200205 RDAT8 command by the data FSM; resulting in an I2C bus error (NACK instead of an ACK). */
201206 /* Send 0x13 bit wise */
202- d -> membase -> CMD_REG = I2C_CMD_WDAT0 ;
203- d -> membase -> CMD_REG = I2C_CMD_WDAT0 ;
204- d -> membase -> CMD_REG = I2C_CMD_WDAT0 ;
205- d -> membase -> CMD_REG = I2C_CMD_WDAT1 ;
206-
207- d -> membase -> CMD_REG = I2C_CMD_WDAT0 ;
208- d -> membase -> CMD_REG = I2C_CMD_WDAT0 ;
209- d -> membase -> CMD_REG = I2C_CMD_WDAT1 ;
210- d -> membase -> CMD_REG = I2C_CMD_WDAT1 ;
207+ SEND_COMMAND (I2C_CMD_WDAT0 );
208+
209+ SEND_COMMAND (I2C_CMD_WDAT0 );
210+
211+ SEND_COMMAND (I2C_CMD_WDAT0 );
212+
213+ SEND_COMMAND (I2C_CMD_WDAT1 );
214+
215+ SEND_COMMAND (I2C_CMD_WDAT0 );
216+
217+ SEND_COMMAND (I2C_CMD_WDAT0 );
218+
219+ SEND_COMMAND (I2C_CMD_WDAT1 );
220+
221+ SEND_COMMAND (I2C_CMD_WDAT1 );
211222 } else {
212223 /* Send data */
213- d -> membase -> CMD_REG = buf [write ++ ];
224+ SEND_COMMAND ( buf [write ++ ]) ;
214225 }
215- d -> membase -> CMD_REG = I2C_CMD_VRFY_ACK ; /* TODO Verify ACK based on requirement, Do we need? */
216-
217- while (FIFO_OFL_CHECK ); /* Wait till command overflow ends */
226+ SEND_COMMAND (I2C_CMD_VRFY_ACK ); /* TODO Verify ACK based on requirement, Do we need? */
218227
219228 if (I2C_BUS_ERR_CHECK ) {
220229 /* Bus error */
221230 return I2C_ERROR_BUS_BUSY ;
222231 }
232+
233+ while (FIFO_OFL_CHECK ); /* Wait till command overflow ends */
223234 }
224235
225236 return write ;
226237}
227238
228- #endif /* DEVICE_I2C */
239+ #endif /* DEVICE_I2C */
0 commit comments