Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e78247b

Browse files
committedNov 21, 2023
astyle
1 parent 288af41 commit e78247b

File tree

1 file changed

+275
-275
lines changed

1 file changed

+275
-275
lines changed
 

‎libraries/Wire/src/Wire.cpp

Lines changed: 275 additions & 275 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ static const uint8_t MASTER_ADDRESS = 0x01;
3535

3636
TwoWire::TwoWire()
3737
{
38-
memset((void*)&_i2c, 0, sizeof(_i2c));
39-
_i2c.sda = digitalPinToPinName(SDA);
40-
_i2c.scl = digitalPinToPinName(SCL);
38+
memset((void*)&_i2c, 0, sizeof(_i2c));
39+
_i2c.sda = digitalPinToPinName(SDA);
40+
_i2c.scl = digitalPinToPinName(SCL);
4141
}
4242

4343
TwoWire::TwoWire(uint32_t sda, uint32_t scl)
4444
{
45-
memset((void*)&_i2c, 0, sizeof(_i2c));
46-
_i2c.sda = digitalPinToPinName(sda);
47-
_i2c.scl = digitalPinToPinName(scl);
45+
memset((void*)&_i2c, 0, sizeof(_i2c));
46+
_i2c.sda = digitalPinToPinName(sda);
47+
_i2c.scl = digitalPinToPinName(scl);
4848
}
4949

5050
/**
@@ -53,179 +53,179 @@ TwoWire::TwoWire(uint32_t sda, uint32_t scl)
5353
*/
5454
TwoWire::~TwoWire()
5555
{
56-
end();
56+
end();
5757
}
5858

5959
// Public Methods //////////////////////////////////////////////////////////////
6060

6161
void TwoWire::begin(uint32_t sda, uint32_t scl)
6262
{
63-
_i2c.sda = digitalPinToPinName(sda);
64-
_i2c.scl = digitalPinToPinName(scl);
65-
begin();
63+
_i2c.sda = digitalPinToPinName(sda);
64+
_i2c.scl = digitalPinToPinName(scl);
65+
begin();
6666
}
6767

6868
void TwoWire::begin(bool generalCall)
6969
{
70-
begin(MASTER_ADDRESS, generalCall);
70+
begin(MASTER_ADDRESS, generalCall);
7171
}
7272

7373
void TwoWire::begin(uint8_t address, bool generalCall, bool NoStretchMode)
7474
{
75-
rxBufferIndex = 0;
76-
rxBufferLength = 0;
77-
rxBuffer = nullptr;
78-
rxBufferAllocated = 0;
79-
resetRxBuffer();
75+
rxBufferIndex = 0;
76+
rxBufferLength = 0;
77+
rxBuffer = nullptr;
78+
rxBufferAllocated = 0;
79+
resetRxBuffer();
8080

81-
txDataSize = 0;
82-
txAddress = 0;
83-
txBuffer = nullptr;
84-
txBufferAllocated = 0;
85-
resetTxBuffer();
81+
txDataSize = 0;
82+
txAddress = 0;
83+
txBuffer = nullptr;
84+
txBufferAllocated = 0;
85+
resetTxBuffer();
8686

87-
_i2c.__this = (void *)this;
88-
user_onRequest = NULL;
89-
transmitting = 0;
87+
_i2c.__this = (void *)this;
88+
user_onRequest = NULL;
89+
transmitting = 0;
9090

91-
ownAddress = address << 1;
91+
ownAddress = address << 1;
9292

93-
_i2c.isMaster = (address == MASTER_ADDRESS) ? 1 : 0;
93+
_i2c.isMaster = (address == MASTER_ADDRESS) ? 1 : 0;
9494

95-
_i2c.generalCall = (generalCall == true) ? 1 : 0;
95+
_i2c.generalCall = (generalCall == true) ? 1 : 0;
9696

97-
_i2c.NoStretchMode = (NoStretchMode == true) ? 1 : 0;
97+
_i2c.NoStretchMode = (NoStretchMode == true) ? 1 : 0;
9898

99-
recoverBus(); // in case I2C bus (device) is stuck after a reset for example
99+
recoverBus(); // in case I2C bus (device) is stuck after a reset for example
100100

101-
i2c_custom_init(&_i2c, 100000, I2C_ADDRESSINGMODE_7BIT, ownAddress);
101+
i2c_custom_init(&_i2c, 100000, I2C_ADDRESSINGMODE_7BIT, ownAddress);
102102

103-
if (_i2c.isMaster == 0) {
104-
// i2c_attachSlaveTxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*)>(&TwoWire::onRequestService));
105-
// i2c_attachSlaveRxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*, uint8_t*, int)>(&TwoWire::onReceiveService));
103+
if (_i2c.isMaster == 0) {
104+
// i2c_attachSlaveTxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*)>(&TwoWire::onRequestService));
105+
// i2c_attachSlaveRxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*, uint8_t*, int)>(&TwoWire::onReceiveService));
106106

107-
i2c_attachSlaveTxEvent(&_i2c, onRequestService);
108-
i2c_attachSlaveRxEvent(&_i2c, onReceiveService);
109-
}
107+
i2c_attachSlaveTxEvent(&_i2c, onRequestService);
108+
i2c_attachSlaveRxEvent(&_i2c, onReceiveService);
109+
}
110110
}
111111

112112
void TwoWire::begin(int address, bool generalCall, bool NoStretchMode)
113113
{
114-
begin((uint8_t)address, generalCall, NoStretchMode);
114+
begin((uint8_t)address, generalCall, NoStretchMode);
115115
}
116116

117117
void TwoWire::end(void)
118118
{
119-
i2c_deinit(&_i2c);
120-
if (txBuffer != nullptr) {
121-
free(txBuffer);
122-
txBuffer = nullptr;
123-
}
124-
txBufferAllocated = 0;
125-
if (rxBuffer != nullptr) {
126-
free(rxBuffer);
127-
rxBuffer = nullptr;
128-
}
129-
rxBufferAllocated = 0;
119+
i2c_deinit(&_i2c);
120+
if (txBuffer != nullptr) {
121+
free(txBuffer);
122+
txBuffer = nullptr;
123+
}
124+
txBufferAllocated = 0;
125+
if (rxBuffer != nullptr) {
126+
free(rxBuffer);
127+
rxBuffer = nullptr;
128+
}
129+
rxBufferAllocated = 0;
130130
}
131131

132132
void TwoWire::setClock(uint32_t frequency)
133133
{
134-
i2c_setTiming(&_i2c, frequency);
135-
if (_i2c.isMaster == 0) {
136-
i2c_attachSlaveTxEvent(&_i2c, onRequestService);
137-
i2c_attachSlaveRxEvent(&_i2c, onReceiveService);
138-
}
134+
i2c_setTiming(&_i2c, frequency);
135+
if (_i2c.isMaster == 0) {
136+
i2c_attachSlaveTxEvent(&_i2c, onRequestService);
137+
i2c_attachSlaveRxEvent(&_i2c, onReceiveService);
138+
}
139139
}
140140

141141
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
142142
{
143143
#if !defined(I2C_OTHER_FRAME)
144-
UNUSED(sendStop);
144+
UNUSED(sendStop);
145145
#endif
146-
uint8_t read = 0;
146+
uint8_t read = 0;
147147

148-
if (_i2c.isMaster == 1) {
149-
allocateRxBuffer(quantity);
148+
if (_i2c.isMaster == 1) {
149+
allocateRxBuffer(quantity);
150150

151-
if (isize > 0) {
152-
// send internal address; this mode allows sending a repeated start to access
153-
// some devices' internal registers. This function is executed by the hardware
154-
// TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)
151+
if (isize > 0) {
152+
// send internal address; this mode allows sending a repeated start to access
153+
// some devices' internal registers. This function is executed by the hardware
154+
// TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)
155155

156-
beginTransmission(address);
156+
beginTransmission(address);
157157

158-
// the maximum size of internal address is 3 bytes
159-
if (isize > 3) {
160-
isize = 3;
161-
}
158+
// the maximum size of internal address is 3 bytes
159+
if (isize > 3) {
160+
isize = 3;
161+
}
162162

163-
// write internal register address - most significant byte first
164-
while (isize-- > 0) {
165-
write((uint8_t)(iaddress >> (isize * 8)));
166-
}
167-
endTransmission(false);
168-
}
163+
// write internal register address - most significant byte first
164+
while (isize-- > 0) {
165+
write((uint8_t)(iaddress >> (isize * 8)));
166+
}
167+
endTransmission(false);
168+
}
169169

170-
// perform blocking read into buffer
170+
// perform blocking read into buffer
171171
#if defined(I2C_OTHER_FRAME)
172-
if (sendStop == 0) {
173-
_i2c.handle.XferOptions = I2C_OTHER_FRAME ;
174-
} else {
175-
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
176-
}
172+
if (sendStop == 0) {
173+
_i2c.handle.XferOptions = I2C_OTHER_FRAME ;
174+
} else {
175+
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
176+
}
177177
#endif
178178

179-
if (I2C_OK == i2c_master_read(&_i2c, address << 1, rxBuffer, quantity)) {
180-
read = quantity;
181-
}
179+
if (I2C_OK == i2c_master_read(&_i2c, address << 1, rxBuffer, quantity)) {
180+
read = quantity;
181+
}
182182

183-
// set rx buffer iterator vars
184-
rxBufferIndex = 0;
185-
rxBufferLength = read;
183+
// set rx buffer iterator vars
184+
rxBufferIndex = 0;
185+
rxBufferLength = read;
186186

187-
}
188-
return read;
187+
}
188+
return read;
189189
}
190190

191191
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
192192
{
193-
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint32_t)0, (uint8_t)0, (uint8_t)sendStop);
193+
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint32_t)0, (uint8_t)0, (uint8_t)sendStop);
194194
}
195195

196196
uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool sendStop)
197197
{
198-
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
198+
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
199199
}
200200

201201
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
202202
{
203-
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
203+
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
204204
}
205205

206206
uint8_t TwoWire::requestFrom(int address, int quantity)
207207
{
208-
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
208+
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
209209
}
210210

211211
uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
212212
{
213-
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
213+
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
214214
}
215215

216216
void TwoWire::beginTransmission(uint8_t address)
217217
{
218-
// indicate that we are transmitting
219-
transmitting = 1;
220-
// set address of targeted slave
221-
txAddress = address << 1;
222-
// reset tx data size
223-
txDataSize = 0;
218+
// indicate that we are transmitting
219+
transmitting = 1;
220+
// set address of targeted slave
221+
txAddress = address << 1;
222+
// reset tx data size
223+
txDataSize = 0;
224224
}
225225

226226
void TwoWire::beginTransmission(int address)
227227
{
228-
beginTransmission((uint8_t)address);
228+
beginTransmission((uint8_t)address);
229229
}
230230

231231
//
@@ -244,85 +244,85 @@ void TwoWire::beginTransmission(int address)
244244
uint8_t TwoWire::endTransmission(uint8_t sendStop)
245245
{
246246
#if !defined(I2C_OTHER_FRAME)
247-
UNUSED(sendStop);
247+
UNUSED(sendStop);
248248
#endif
249-
int8_t ret = 4;
250-
// check transfer options and store it in the I2C handle
249+
int8_t ret = 4;
250+
// check transfer options and store it in the I2C handle
251251
#if defined(I2C_OTHER_FRAME)
252-
if (sendStop == 0) {
253-
_i2c.handle.XferOptions = I2C_OTHER_FRAME ;
254-
} else {
255-
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
256-
}
252+
if (sendStop == 0) {
253+
_i2c.handle.XferOptions = I2C_OTHER_FRAME ;
254+
} else {
255+
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
256+
}
257257
#endif
258258

259-
if (_i2c.isMaster == 1) {
260-
// transmit buffer (blocking)
261-
switch (i2c_master_write(&_i2c, txAddress, txBuffer, txDataSize)) {
262-
case I2C_OK :
263-
ret = 0; // Success
264-
break;
265-
case I2C_DATA_TOO_LONG :
266-
ret = 1;
267-
break;
268-
case I2C_NACK_ADDR:
269-
ret = 2;
270-
break;
271-
case I2C_NACK_DATA:
272-
ret = 3;
273-
break;
274-
case I2C_TIMEOUT:
275-
case I2C_BUSY:
276-
case I2C_ERROR:
277-
default:
278-
ret = 4;
279-
break;
259+
if (_i2c.isMaster == 1) {
260+
// transmit buffer (blocking)
261+
switch (i2c_master_write(&_i2c, txAddress, txBuffer, txDataSize)) {
262+
case I2C_OK :
263+
ret = 0; // Success
264+
break;
265+
case I2C_DATA_TOO_LONG :
266+
ret = 1;
267+
break;
268+
case I2C_NACK_ADDR:
269+
ret = 2;
270+
break;
271+
case I2C_NACK_DATA:
272+
ret = 3;
273+
break;
274+
case I2C_TIMEOUT:
275+
case I2C_BUSY:
276+
case I2C_ERROR:
277+
default:
278+
ret = 4;
279+
break;
280+
}
281+
282+
// reset Tx buffer
283+
resetTxBuffer();
284+
285+
// reset tx buffer data size
286+
txDataSize = 0;
287+
288+
// indicate that we are done transmitting
289+
transmitting = 0;
280290
}
281-
282-
// reset Tx buffer
283-
resetTxBuffer();
284-
285-
// reset tx buffer data size
286-
txDataSize = 0;
287-
288-
// indicate that we are done transmitting
289-
transmitting = 0;
290-
}
291-
return ret;
291+
return ret;
292292
}
293293

294294
// This provides backwards compatibility with the original
295295
// definition, and expected behaviour, of endTransmission
296296
//
297297
uint8_t TwoWire::endTransmission(void)
298298
{
299-
return endTransmission((uint8_t)true);
299+
return endTransmission((uint8_t)true);
300300
}
301301

302302
// must be called in:
303303
// slave tx event callback
304304
// or after beginTransmission(address)
305305
size_t TwoWire::write(uint8_t data)
306306
{
307-
size_t ret = 1;
308-
if (transmitting) {
309-
// in master transmitter mode
310-
if (allocateTxBuffer(txDataSize + 1) == 0) {
311-
ret = 0;
307+
size_t ret = 1;
308+
if (transmitting) {
309+
// in master transmitter mode
310+
if (allocateTxBuffer(txDataSize + 1) == 0) {
311+
ret = 0;
312+
} else {
313+
// put byte in tx buffer
314+
txBuffer[txDataSize] = data;
315+
// update amount in buffer
316+
txDataSize++;
317+
}
312318
} else {
313-
// put byte in tx buffer
314-
txBuffer[txDataSize] = data;
315-
// update amount in buffer
316-
txDataSize++;
319+
// in slave send mode
320+
// reply to master
321+
if (i2c_slave_write_IT(&_i2c, &data, 1) != I2C_OK) {
322+
ret = 0;
323+
}
317324
}
318-
} else {
319-
// in slave send mode
320-
// reply to master
321-
if (i2c_slave_write_IT(&_i2c, &data, 1) != I2C_OK) {
322-
ret = 0;
323-
}
324-
}
325-
return ret;
325+
return ret;
326326
}
327327

328328
/**
@@ -334,134 +334,134 @@ size_t TwoWire::write(uint8_t data)
334334
*/
335335
size_t TwoWire::write(const uint8_t *data, size_t quantity)
336336
{
337-
size_t ret = quantity;
337+
size_t ret = quantity;
338338

339-
if (transmitting) {
340-
// in master transmitter mode
341-
if (allocateTxBuffer(txDataSize + quantity) == 0) {
342-
ret = 0;
343-
} else {
344-
// put bytes in tx buffer
345-
memcpy(&(txBuffer[txDataSize]), data, quantity);
339+
if (transmitting) {
340+
// in master transmitter mode
341+
if (allocateTxBuffer(txDataSize + quantity) == 0) {
342+
ret = 0;
343+
} else {
344+
// put bytes in tx buffer
345+
memcpy(&(txBuffer[txDataSize]), data, quantity);
346346

347-
// update amount in buffer
348-
txDataSize += quantity;
349-
}
350-
} else {
351-
// in slave send mode
352-
// reply to master
353-
if (i2c_slave_write_IT(&_i2c, (uint8_t *)data, quantity) != I2C_OK) {
354-
ret = 0;
347+
// update amount in buffer
348+
txDataSize += quantity;
349+
}
350+
} else {
351+
// in slave send mode
352+
// reply to master
353+
if (i2c_slave_write_IT(&_i2c, (uint8_t *)data, quantity) != I2C_OK) {
354+
ret = 0;
355+
}
355356
}
356-
}
357-
return ret;
357+
return ret;
358358
}
359359

360360
// must be called in:
361361
// slave rx event callback
362362
// or after requestFrom(address, numBytes)
363363
int TwoWire::available(void)
364364
{
365-
return rxBufferLength - rxBufferIndex;
365+
return rxBufferLength - rxBufferIndex;
366366
}
367367

368368
// must be called in:
369369
// slave rx event callback
370370
// or after requestFrom(address, numBytes)
371371
int TwoWire::read(void)
372372
{
373-
int value = -1;
373+
int value = -1;
374374

375-
// get each successive byte on each call
376-
if (rxBufferIndex < rxBufferLength) {
377-
value = rxBuffer[rxBufferIndex];
378-
++rxBufferIndex;
375+
// get each successive byte on each call
376+
if (rxBufferIndex < rxBufferLength) {
377+
value = rxBuffer[rxBufferIndex];
378+
++rxBufferIndex;
379379

380-
/* Commented as not I think it is not useful
381-
* but kept to show that it is possible to
382-
* reset rx buffer when no more data available */
383-
/*if(rxBufferIndex == rxBufferLength) {
384-
resetRxBuffer();
385-
}*/
386-
}
387-
return value;
380+
/* Commented as not I think it is not useful
381+
* but kept to show that it is possible to
382+
* reset rx buffer when no more data available */
383+
/*if(rxBufferIndex == rxBufferLength) {
384+
resetRxBuffer();
385+
}*/
386+
}
387+
return value;
388388
}
389389

390390
// must be called in:
391391
// slave rx event callback
392392
// or after requestFrom(address, numBytes)
393393
int TwoWire::peek(void)
394394
{
395-
int value = -1;
395+
int value = -1;
396396

397-
if (rxBufferIndex < rxBufferLength) {
398-
value = rxBuffer[rxBufferIndex];
399-
}
400-
return value;
397+
if (rxBufferIndex < rxBufferLength) {
398+
value = rxBuffer[rxBufferIndex];
399+
}
400+
return value;
401401
}
402402

403403
void TwoWire::flush(void)
404404
{
405-
rxBufferIndex = 0;
406-
rxBufferLength = 0;
407-
resetRxBuffer();
408-
txDataSize = 0;
409-
resetTxBuffer();
405+
rxBufferIndex = 0;
406+
rxBufferLength = 0;
407+
resetRxBuffer();
408+
txDataSize = 0;
409+
resetTxBuffer();
410410
}
411411

412412
// behind the scenes function that is called when data is received
413413
void TwoWire::onReceiveService(i2c_t *obj)
414414
{
415-
uint8_t *inBytes = (uint8_t *) obj->i2cTxRxBuffer;
416-
int numBytes = obj->slaveRxNbData;
417-
418-
TwoWire *TW = (TwoWire *)(obj->__this);
419-
420-
// don't bother if user hasn't registered a callback
421-
if (TW->user_onReceive) {
422-
// don't bother if rx buffer is in use by a master requestFrom() op
423-
// i know this drops data, but it allows for slight stupidity
424-
// meaning, they may not have read all the master requestFrom() data yet
425-
if (TW->rxBufferIndex >= TW->rxBufferLength) {
426-
TW->allocateRxBuffer(numBytes);
427-
428-
// copy twi rx buffer into local read buffer
429-
// this enables new reads to happen in parallel
430-
memcpy(TW->rxBuffer, inBytes, numBytes);
431-
// set rx iterator vars
432-
TW->rxBufferIndex = 0;
433-
TW->rxBufferLength = numBytes;
434-
// alert user program
435-
TW->user_onReceive(numBytes);
415+
uint8_t *inBytes = (uint8_t *) obj->i2cTxRxBuffer;
416+
int numBytes = obj->slaveRxNbData;
417+
418+
TwoWire *TW = (TwoWire *)(obj->__this);
419+
420+
// don't bother if user hasn't registered a callback
421+
if (TW->user_onReceive) {
422+
// don't bother if rx buffer is in use by a master requestFrom() op
423+
// i know this drops data, but it allows for slight stupidity
424+
// meaning, they may not have read all the master requestFrom() data yet
425+
if (TW->rxBufferIndex >= TW->rxBufferLength) {
426+
TW->allocateRxBuffer(numBytes);
427+
428+
// copy twi rx buffer into local read buffer
429+
// this enables new reads to happen in parallel
430+
memcpy(TW->rxBuffer, inBytes, numBytes);
431+
// set rx iterator vars
432+
TW->rxBufferIndex = 0;
433+
TW->rxBufferLength = numBytes;
434+
// alert user program
435+
TW->user_onReceive(numBytes);
436+
}
436437
}
437-
}
438438
}
439439

440440
// behind the scenes function that is called when data is requested
441441
void TwoWire::onRequestService(i2c_t *obj)
442442
{
443-
TwoWire *TW = (TwoWire *)(obj->__this);
443+
TwoWire *TW = (TwoWire *)(obj->__this);
444444

445-
// don't bother if user hasn't registered a callback
446-
if (TW->user_onRequest) {
447-
// reset tx data size
448-
// !!! this will kill any pending pre-master sendTo() activity
449-
TW->txDataSize = 0;
450-
// alert user program
451-
TW->user_onRequest();
452-
}
445+
// don't bother if user hasn't registered a callback
446+
if (TW->user_onRequest) {
447+
// reset tx data size
448+
// !!! this will kill any pending pre-master sendTo() activity
449+
TW->txDataSize = 0;
450+
// alert user program
451+
TW->user_onRequest();
452+
}
453453
}
454454

455455
// sets function called on slave write
456456
void TwoWire::onReceive(cb_function_receive_t function)
457457
{
458-
user_onReceive = function;
458+
user_onReceive = function;
459459
}
460460

461461
// sets function called on slave read
462462
void TwoWire::onRequest(cb_function_request_t function)
463463
{
464-
user_onRequest = function;
464+
user_onRequest = function;
465465
}
466466

467467
/**
@@ -471,57 +471,57 @@ void TwoWire::onRequest(cb_function_request_t function)
471471
*/
472472
void TwoWire::allocateRxBuffer(size_t length)
473473
{
474-
if (rxBufferAllocated < length) {
475-
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
476-
if (length < BUFFER_LENGTH) {
477-
length = BUFFER_LENGTH;
474+
if (rxBufferAllocated < length) {
475+
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
476+
if (length < BUFFER_LENGTH) {
477+
length = BUFFER_LENGTH;
478+
}
479+
uint8_t *tmp = (uint8_t *)realloc(rxBuffer, length * sizeof(uint8_t));
480+
if (tmp != nullptr) {
481+
rxBuffer = tmp;
482+
rxBufferAllocated = length;
483+
} else {
484+
_Error_Handler("No enough memory! (%i)\n", length);
485+
}
478486
}
479-
uint8_t *tmp = (uint8_t *)realloc(rxBuffer, length * sizeof(uint8_t));
480-
if (tmp != nullptr) {
481-
rxBuffer = tmp;
482-
rxBufferAllocated = length;
483-
} else {
484-
_Error_Handler("No enough memory! (%i)\n", length);
485-
}
486-
}
487487
}
488488

489489
inline size_t TwoWire::allocateTxBuffer(size_t length)
490490
{
491-
size_t ret = length;
492-
if (length > WIRE_MAX_TX_BUFF_LENGTH) {
493-
ret = 0;
494-
} else if (txBufferAllocated < length) {
495-
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
496-
if (length < BUFFER_LENGTH) {
497-
length = BUFFER_LENGTH;
498-
}
499-
uint8_t *tmp = (uint8_t *)realloc(txBuffer, length * sizeof(uint8_t));
500-
if (tmp != nullptr) {
501-
txBuffer = tmp;
502-
txBufferAllocated = length;
503-
} else {
504-
_Error_Handler("No enough memory! (%i)\n", length);
491+
size_t ret = length;
492+
if (length > WIRE_MAX_TX_BUFF_LENGTH) {
493+
ret = 0;
494+
} else if (txBufferAllocated < length) {
495+
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
496+
if (length < BUFFER_LENGTH) {
497+
length = BUFFER_LENGTH;
498+
}
499+
uint8_t *tmp = (uint8_t *)realloc(txBuffer, length * sizeof(uint8_t));
500+
if (tmp != nullptr) {
501+
txBuffer = tmp;
502+
txBufferAllocated = length;
503+
} else {
504+
_Error_Handler("No enough memory! (%i)\n", length);
505+
}
505506
}
506-
}
507-
return ret;
507+
return ret;
508508
}
509509

510510
/**
511511
* @brief Reset Rx/Tx buffer content to 0
512512
*/
513513
inline void TwoWire::resetRxBuffer(void)
514514
{
515-
if (rxBuffer != nullptr) {
516-
memset(rxBuffer, 0, rxBufferAllocated);
517-
}
515+
if (rxBuffer != nullptr) {
516+
memset(rxBuffer, 0, rxBufferAllocated);
517+
}
518518
}
519519

520520
inline void TwoWire::resetTxBuffer(void)
521521
{
522-
if (txBuffer != nullptr) {
523-
memset(txBuffer, 0, txBufferAllocated);
524-
}
522+
if (txBuffer != nullptr) {
523+
memset(txBuffer, 0, txBufferAllocated);
524+
}
525525
}
526526

527527
// Send clear bus (clock pulse) sequence to recover bus.
@@ -531,19 +531,19 @@ inline void TwoWire::resetTxBuffer(void)
531531
// https://bits4device.wordpress.com/2017/07/28/i2c-bus-recovery/
532532
void TwoWire::recoverBus(void)
533533
{
534-
pinMode(pinNametoDigitalPin(_i2c.sda), INPUT);
534+
pinMode(pinNametoDigitalPin(_i2c.sda), INPUT);
535535

536-
if (digitalReadFast(_i2c.sda) == LOW) {
537-
pinMode(pinNametoDigitalPin(_i2c.scl), OUTPUT);
536+
if (digitalReadFast(_i2c.sda) == LOW) {
537+
pinMode(pinNametoDigitalPin(_i2c.scl), OUTPUT);
538538

539-
for (int i = 0; i < 20; i++) {
540-
digitalWriteFast(_i2c.scl, LOW);
541-
delayMicroseconds(10);
542-
digitalWriteFast(_i2c.scl, HIGH);
543-
delayMicroseconds(10);
539+
for (int i = 0; i < 20; i++) {
540+
digitalWriteFast(_i2c.scl, LOW);
541+
delayMicroseconds(10);
542+
digitalWriteFast(_i2c.scl, HIGH);
543+
delayMicroseconds(10);
544+
}
545+
pinMode(pinNametoDigitalPin(_i2c.scl), INPUT);
544546
}
545-
pinMode(pinNametoDigitalPin(_i2c.scl), INPUT);
546-
}
547547
}
548548

549549
// Preinstantiate Objects //////////////////////////////////////////////////////

0 commit comments

Comments
 (0)
Please sign in to comment.