@@ -57,27 +57,28 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
57
57
memcpy (transmitBuf , json , jsonLen );
58
58
transmitBuf [jsonLen ++ ] = '\n' ;
59
59
60
+ // Lock over the entire transaction
61
+ _LockI2C ();
62
+
60
63
// Transmit the request in chunks, but also in segments so as not to overwhelm the notecard's interrupt buffers
61
64
const char * estr ;
62
65
uint8_t * chunk = transmitBuf ;
63
66
uint32_t sentInSegment = 0 ;
64
67
while (jsonLen > 0 ) {
65
68
int chunklen = (uint8_t ) (jsonLen > (int )_I2CMax () ? (int )_I2CMax () : jsonLen );
66
- _LockI2C ();
67
69
_DelayIO ();
68
70
estr = _I2CTransmit (_I2CAddress (), chunk , chunklen );
69
71
if (estr != NULL ) {
70
72
_Free (transmitBuf );
71
73
_I2CReset (_I2CAddress ());
72
- _UnlockI2C ();
73
74
#ifdef ERRDBG
74
75
_Debug ("i2c transmit: " );
75
76
_Debug (estr );
76
77
_Debug ("\n" );
77
78
#endif
79
+ _UnlockI2C ();
78
80
return estr ;
79
81
}
80
- _UnlockI2C ();
81
82
chunk += chunklen ;
82
83
jsonLen -= chunklen ;
83
84
sentInSegment += chunklen ;
@@ -97,6 +98,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
97
98
98
99
// If no reply expected, we're done
99
100
if (jsonResponse == NULL ) {
101
+ _UnlockI2C ();
100
102
return NULL ;
101
103
}
102
104
@@ -110,6 +112,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
110
112
#ifdef ERRDBG
111
113
_Debug ("transaction: jsonbuf malloc failed\n" );
112
114
#endif
115
+ _UnlockI2C ();
113
116
return ERRSTR ("insufficient memory" ,c_mem );
114
117
}
115
118
@@ -134,6 +137,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
134
137
_Debug ("transaction: jsonbuf grow malloc failed\n" );
135
138
#endif
136
139
_Free (jsonbuf );
140
+ _UnlockI2C ();
137
141
return ERRSTR ("insufficient memory" ,c_mem );
138
142
}
139
143
memcpy (jsonbufNew , jsonbuf , jsonbufLen );
@@ -143,15 +147,14 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
143
147
144
148
// Read the chunk
145
149
uint32_t available ;
146
- _LockI2C ();
147
150
_DelayIO ();
148
151
const char * err = _I2CReceive (_I2CAddress (), (uint8_t * ) & jsonbuf [jsonbufLen ], chunklen , & available );
149
- _UnlockI2C ();
150
152
if (err != NULL ) {
151
153
_Free (jsonbuf );
152
154
#ifdef ERRDBG
153
155
_Debug ("i2c receive error\n" );
154
156
#endif
157
+ _UnlockI2C ();
155
158
return err ;
156
159
}
157
160
@@ -184,6 +187,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
184
187
#ifdef ERRDBG
185
188
_Debug ("reply to request didn't arrive from module in time\n" );
186
189
#endif
190
+ _UnlockI2C ();
187
191
return ERRSTR ("request or response was lost {io}" ,c_iotimeout );
188
192
}
189
193
@@ -194,6 +198,9 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
194
198
195
199
}
196
200
201
+ // Done with the bus
202
+ _UnlockI2C ();
203
+
197
204
// Null-terminate it, using the +1 space that we'd allocated in the buffer
198
205
jsonbuf [jsonbufLen ] = '\0' ;
199
206
@@ -215,21 +222,19 @@ bool i2cNoteReset()
215
222
// Reset the I2C subsystem and exit if failure
216
223
_LockI2C ();
217
224
bool success = _I2CReset (_I2CAddress ());
218
- _UnlockI2C ();
219
225
if (!success ) {
226
+ _UnlockI2C ();
220
227
return false;
221
228
}
222
229
223
230
// Synchronize by guaranteeing not only that I2C works, but that after we send \n that we drain
224
231
// the remainder of any pending partial reply from a previously-aborted session.
225
232
// If we get a failure on transmitting the \n, it means that the notecard isn't even present.
226
- _LockI2C ();
227
233
_DelayIO ();
228
234
const char * transmitErr = _I2CTransmit (_I2CAddress (), (uint8_t * )"\n" , 1 );
229
235
if (!cardTurboIO ) {
230
236
_DelayMs (CARD_REQUEST_I2C_SEGMENT_DELAY_MS );
231
237
}
232
- _UnlockI2C ();
233
238
234
239
// This outer loop does retries on I2C error, and is simply here for robustness.
235
240
bool notecardReady = false;
@@ -245,10 +250,8 @@ bool i2cNoteReset()
245
250
uint8_t buffer [128 ];
246
251
chunklen = (chunklen > (int )sizeof (buffer )) ? (int )sizeof (buffer ) : chunklen ;
247
252
chunklen = (chunklen > (int )_I2CMax ()) ? (int )_I2CMax () : chunklen ;
248
- _LockI2C ();
249
253
_DelayIO ();
250
254
const char * err = _I2CReceive (_I2CAddress (), buffer , chunklen , & available );
251
- _UnlockI2C ();
252
255
if (err ) {
253
256
break ;
254
257
}
@@ -273,12 +276,13 @@ bool i2cNoteReset()
273
276
274
277
// Reinitialize i2c if there's no response
275
278
if (!notecardReady ) {
276
- _LockI2C ();
277
279
_I2CReset (_I2CAddress ());
278
- _UnlockI2C ();
279
280
_Debug (ERRSTR ("notecard not responding\n" , "no notecard\n" ));
280
281
}
281
282
283
+ // Done with the bus
284
+ _UnlockI2C ();
285
+
282
286
// Done
283
287
return notecardReady ;
284
288
}
0 commit comments