@@ -57,27 +57,28 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
5757 memcpy (transmitBuf , json , jsonLen );
5858 transmitBuf [jsonLen ++ ] = '\n' ;
5959
60+ // Lock over the entire transaction
61+ _LockI2C ();
62+
6063 // Transmit the request in chunks, but also in segments so as not to overwhelm the notecard's interrupt buffers
6164 const char * estr ;
6265 uint8_t * chunk = transmitBuf ;
6366 uint32_t sentInSegment = 0 ;
6467 while (jsonLen > 0 ) {
6568 int chunklen = (uint8_t ) (jsonLen > (int )_I2CMax () ? (int )_I2CMax () : jsonLen );
66- _LockI2C ();
6769 _DelayIO ();
6870 estr = _I2CTransmit (_I2CAddress (), chunk , chunklen );
6971 if (estr != NULL ) {
7072 _Free (transmitBuf );
7173 _I2CReset (_I2CAddress ());
72- _UnlockI2C ();
7374#ifdef ERRDBG
7475 _Debug ("i2c transmit: " );
7576 _Debug (estr );
7677 _Debug ("\n" );
7778#endif
79+ _UnlockI2C ();
7880 return estr ;
7981 }
80- _UnlockI2C ();
8182 chunk += chunklen ;
8283 jsonLen -= chunklen ;
8384 sentInSegment += chunklen ;
@@ -97,6 +98,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
9798
9899 // If no reply expected, we're done
99100 if (jsonResponse == NULL ) {
101+ _UnlockI2C ();
100102 return NULL ;
101103 }
102104
@@ -110,6 +112,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
110112#ifdef ERRDBG
111113 _Debug ("transaction: jsonbuf malloc failed\n" );
112114#endif
115+ _UnlockI2C ();
113116 return ERRSTR ("insufficient memory" ,c_mem );
114117 }
115118
@@ -134,6 +137,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
134137 _Debug ("transaction: jsonbuf grow malloc failed\n" );
135138#endif
136139 _Free (jsonbuf );
140+ _UnlockI2C ();
137141 return ERRSTR ("insufficient memory" ,c_mem );
138142 }
139143 memcpy (jsonbufNew , jsonbuf , jsonbufLen );
@@ -143,15 +147,14 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
143147
144148 // Read the chunk
145149 uint32_t available ;
146- _LockI2C ();
147150 _DelayIO ();
148151 const char * err = _I2CReceive (_I2CAddress (), (uint8_t * ) & jsonbuf [jsonbufLen ], chunklen , & available );
149- _UnlockI2C ();
150152 if (err != NULL ) {
151153 _Free (jsonbuf );
152154#ifdef ERRDBG
153155 _Debug ("i2c receive error\n" );
154156#endif
157+ _UnlockI2C ();
155158 return err ;
156159 }
157160
@@ -184,6 +187,7 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
184187#ifdef ERRDBG
185188 _Debug ("reply to request didn't arrive from module in time\n" );
186189#endif
190+ _UnlockI2C ();
187191 return ERRSTR ("request or response was lost {io}" ,c_iotimeout );
188192 }
189193
@@ -194,6 +198,9 @@ const char *i2cNoteTransaction(char *json, char **jsonResponse)
194198
195199 }
196200
201+ // Done with the bus
202+ _UnlockI2C ();
203+
197204 // Null-terminate it, using the +1 space that we'd allocated in the buffer
198205 jsonbuf [jsonbufLen ] = '\0' ;
199206
@@ -215,21 +222,19 @@ bool i2cNoteReset()
215222 // Reset the I2C subsystem and exit if failure
216223 _LockI2C ();
217224 bool success = _I2CReset (_I2CAddress ());
218- _UnlockI2C ();
219225 if (!success ) {
226+ _UnlockI2C ();
220227 return false;
221228 }
222229
223230 // Synchronize by guaranteeing not only that I2C works, but that after we send \n that we drain
224231 // the remainder of any pending partial reply from a previously-aborted session.
225232 // If we get a failure on transmitting the \n, it means that the notecard isn't even present.
226- _LockI2C ();
227233 _DelayIO ();
228234 const char * transmitErr = _I2CTransmit (_I2CAddress (), (uint8_t * )"\n" , 1 );
229235 if (!cardTurboIO ) {
230236 _DelayMs (CARD_REQUEST_I2C_SEGMENT_DELAY_MS );
231237 }
232- _UnlockI2C ();
233238
234239 // This outer loop does retries on I2C error, and is simply here for robustness.
235240 bool notecardReady = false;
@@ -245,10 +250,8 @@ bool i2cNoteReset()
245250 uint8_t buffer [128 ];
246251 chunklen = (chunklen > (int )sizeof (buffer )) ? (int )sizeof (buffer ) : chunklen ;
247252 chunklen = (chunklen > (int )_I2CMax ()) ? (int )_I2CMax () : chunklen ;
248- _LockI2C ();
249253 _DelayIO ();
250254 const char * err = _I2CReceive (_I2CAddress (), buffer , chunklen , & available );
251- _UnlockI2C ();
252255 if (err ) {
253256 break ;
254257 }
@@ -273,12 +276,13 @@ bool i2cNoteReset()
273276
274277 // Reinitialize i2c if there's no response
275278 if (!notecardReady ) {
276- _LockI2C ();
277279 _I2CReset (_I2CAddress ());
278- _UnlockI2C ();
279280 _Debug (ERRSTR ("notecard not responding\n" , "no notecard\n" ));
280281 }
281282
283+ // Done with the bus
284+ _UnlockI2C ();
285+
282286 // Done
283287 return notecardReady ;
284288}
0 commit comments