60
60
*/
61
61
#if DEVICE_I2C
62
62
#include "i2c.h"
63
+ #include "wait_api.h"
63
64
64
65
/* See i2c.h for details */
65
66
void fI2cInit (i2c_t * obj ,PinName sda ,PinName scl )
@@ -135,15 +136,15 @@ void fI2cFrequency(i2c_t *obj, uint32_t hz)
135
136
int32_t fI2cStart (i2c_t * obj )
136
137
{
137
138
/* Send start bit */
138
- obj -> membase -> CMD_REG = I2C_CMD_START ;
139
+ SEND_COMMAND ( I2C_CMD_START ) ;
139
140
return I2C_API_STATUS_SUCCESS ;
140
141
}
141
142
142
143
/* See i2c.h for details */
143
144
int32_t fI2cStop (i2c_t * obj )
144
145
{
145
146
/* Send stop bit */
146
- obj -> membase -> CMD_REG = I2C_CMD_STOP ;
147
+ SEND_COMMAND ( I2C_CMD_STOP ) ;
147
148
if (obj -> membase -> STATUS .WORD & (I2C_STATUS_CMD_FIFO_FULL_BIT |
148
149
I2C_STATUS_CMD_FIFO_OFL_BIT |
149
150
I2C_STATUS_BUS_ERR_BIT )) {
@@ -154,23 +155,26 @@ int32_t fI2cStop(i2c_t *obj)
154
155
}
155
156
156
157
/* 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 )
158
159
{
159
160
int32_t read = 0 ;
160
161
161
162
while (read < len ) {
162
163
/* Send read command */
163
- d -> membase -> CMD_REG = I2C_CMD_RDAT8 ;
164
+ SEND_COMMAND ( I2C_CMD_RDAT8 ) ;
164
165
while (!RD_DATA_READY ) {
165
166
if (I2C_BUS_ERR_CHECK ) {
166
167
/* Bus error occured */
167
168
return I2C_ERROR_BUS_BUSY ;
168
169
}
169
170
}
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 */
171
172
172
173
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 );
174
178
}
175
179
176
180
/* check for FIFO underflow */
@@ -187,42 +191,49 @@ int32_t fI2cReadB(i2c_t *d, char *buf, int len)
187
191
}
188
192
189
193
/* 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 )
191
195
{
192
196
int32_t write = 0 ;
193
197
194
198
while (write < len ) {
195
199
/* Send write command */
196
- d -> membase -> CMD_REG = I2C_CMD_WDAT8 ;
200
+ SEND_COMMAND (I2C_CMD_WDAT8 );
201
+
197
202
if (buf [write ] == I2C_CMD_RDAT8 ) {
198
203
/* SW work around to counter FSM issue. If the only command in the CMD FIFO is the WDAT8 command (data of 0x13)
199
204
then as the command is read out (i.e. the FIFO goes empty), the WDAT8 command will be misinterpreted as a
200
205
RDAT8 command by the data FSM; resulting in an I2C bus error (NACK instead of an ACK). */
201
206
/* 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 );
211
222
} else {
212
223
/* Send data */
213
- d -> membase -> CMD_REG = buf [write ++ ];
224
+ SEND_COMMAND ( buf [write ++ ]) ;
214
225
}
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? */
218
227
219
228
if (I2C_BUS_ERR_CHECK ) {
220
229
/* Bus error */
221
230
return I2C_ERROR_BUS_BUSY ;
222
231
}
232
+
233
+ while (FIFO_OFL_CHECK ); /* Wait till command overflow ends */
223
234
}
224
235
225
236
return write ;
226
237
}
227
238
228
- #endif /* DEVICE_I2C */
239
+ #endif /* DEVICE_I2C */
0 commit comments