65
65
/* See i2c.h for details */
66
66
void fI2cInit (i2c_t * obj ,PinName sda ,PinName scl )
67
67
{
68
- uint32_t clockDivisor ;
69
68
/* determine the I2C to use */
70
69
I2CName i2c_sda = (I2CName )pinmap_peripheral (sda , PinMap_I2C_SDA );
71
70
I2CName i2c_scl = (I2CName )pinmap_peripheral (scl , PinMap_I2C_SCL );
@@ -93,9 +92,7 @@ void fI2cInit(i2c_t *obj,PinName sda,PinName scl)
93
92
obj -> membase -> CR .BITS .I2C_APB_CD_EN = True ;
94
93
95
94
/* set default baud rate at 100k */
96
- clockDivisor = ((fClockGetPeriphClockfrequency () / 100000 ) >> 2 ) - 2 ;
97
- obj -> membase -> CR .BITS .CD_VAL = (clockDivisor & I2C_CLOCKDIVEDER_VAL_MASK );
98
- obj -> membase -> PRE_SCALE_REG = (clockDivisor & I2C_APB_CLK_DIVIDER_VAL_MASK ) >> 5 ; /**< Zero pre-scale value not allowed */
95
+ fI2cFrequency (obj , 100000 );
99
96
100
97
/* Cross bar setting */
101
98
pinmap_pinout (sda , PinMap_I2C_SDA );
@@ -110,8 +107,8 @@ void fI2cInit(i2c_t *obj,PinName sda,PinName scl)
110
107
PadReg_t * padRegScl = (PadReg_t * )(PADREG_BASE + (scl * PAD_REG_ADRS_BYTE_SIZE ));
111
108
112
109
CLOCK_ENABLE (CLOCK_PAD );
113
- padRegSda -> PADIO0 .BITS .POWER = 1 ; /* sda: Drive strength */
114
- padRegScl -> PADIO0 .BITS .POWER = 1 ; /* scl: Drive strength */
110
+ padRegSda -> PADIO0 .BITS .POWER = 3 ; /* sda: Drive strength */
111
+ padRegScl -> PADIO0 .BITS .POWER = 3 ; /* scl: Drive strength */
115
112
CLOCK_DISABLE (CLOCK_PAD );
116
113
117
114
CLOCK_ENABLE (CLOCK_GPIO );
@@ -160,7 +157,10 @@ int32_t fI2cReadB(i2c_t *obj, char *buf, int len)
160
157
int32_t read = 0 ;
161
158
162
159
while (read < len ) {
163
- /* Send read command */
160
+
161
+ while (FIFO_OFL_CHECK ); /* Wait till command overflow ends */
162
+
163
+ /* Send read command */
164
164
SEND_COMMAND (I2C_CMD_RDAT8 );
165
165
while (!RD_DATA_READY ) {
166
166
if (I2C_BUS_ERR_CHECK ) {
@@ -170,16 +170,16 @@ int32_t fI2cReadB(i2c_t *obj, char *buf, int len)
170
170
}
171
171
buf [read ++ ] = obj -> membase -> RD_FIFO_REG ; /**< Reading 'read FIFO register' will clear status register */
172
172
173
- if (!(read >=len )) { /* No ACK will be generated for the last read, upper level I2C protocol should generate */
174
- SEND_COMMAND (I2C_CMD_WDAT0 ); /* TODO based on requirement generate ACK or NACK Based on the requirement. */
173
+ if (!(read >=len )) {
174
+ SEND_COMMAND (I2C_CMD_WDAT0 );
175
175
} else {
176
176
/* No ack */
177
177
SEND_COMMAND (I2C_CMD_WDAT1 );
178
178
}
179
179
180
180
/* check for FIFO underflow */
181
181
if (I2C_UFL_CHECK ) {
182
- return I2C_ERROR_NO_SLAVE ; /* TODO No error available for this in i2c_api.h */
182
+ return I2C_EVENT_ERROR ;
183
183
}
184
184
if (I2C_BUS_ERR_CHECK ) {
185
185
/* Bus error */
@@ -196,44 +196,36 @@ int32_t fI2cWriteB(i2c_t *obj, const char *buf, int len)
196
196
int32_t write = 0 ;
197
197
198
198
while (write < len ) {
199
- /* Send write command */
200
- SEND_COMMAND ( I2C_CMD_WDAT8 );
199
+
200
+ while ( FIFO_OFL_CHECK ); /* Wait till command overflow ends */
201
201
202
202
if (buf [write ] == I2C_CMD_RDAT8 ) {
203
203
/* SW work around to counter FSM issue. If the only command in the CMD FIFO is the WDAT8 command (data of 0x13)
204
204
then as the command is read out (i.e. the FIFO goes empty), the WDAT8 command will be misinterpreted as a
205
205
RDAT8 command by the data FSM; resulting in an I2C bus error (NACK instead of an ACK). */
206
206
/* Send 0x13 bit wise */
207
207
SEND_COMMAND (I2C_CMD_WDAT0 );
208
-
209
208
SEND_COMMAND (I2C_CMD_WDAT0 );
210
-
211
209
SEND_COMMAND (I2C_CMD_WDAT0 );
212
-
213
210
SEND_COMMAND (I2C_CMD_WDAT1 );
214
-
215
211
SEND_COMMAND (I2C_CMD_WDAT0 );
216
-
217
212
SEND_COMMAND (I2C_CMD_WDAT0 );
218
-
219
213
SEND_COMMAND (I2C_CMD_WDAT1 );
220
-
221
214
SEND_COMMAND (I2C_CMD_WDAT1 );
215
+ write ++ ;
222
216
} else {
223
217
/* Send data */
218
+ SEND_COMMAND (I2C_CMD_WDAT8 );
224
219
SEND_COMMAND (buf [write ++ ]);
225
220
}
226
- SEND_COMMAND (I2C_CMD_VRFY_ACK ); /* TODO Verify ACK based on requirement, Do we need? */
221
+ SEND_COMMAND (I2C_CMD_VRFY_ACK );
227
222
228
223
if (I2C_BUS_ERR_CHECK ) {
229
224
/* Bus error */
230
225
return I2C_ERROR_BUS_BUSY ;
231
226
}
232
-
233
- while (FIFO_OFL_CHECK ); /* Wait till command overflow ends */
234
227
}
235
-
236
228
return write ;
237
229
}
238
230
239
- #endif /* DEVICE_I2C */
231
+ #endif /* DEVICE_I2C */
0 commit comments