@@ -52,7 +52,7 @@ TwoWire::TwoWire(uint8_t bus_num)
52
52
,_timeOutMillis(50 )
53
53
,nonStop(false )
54
54
#if !CONFIG_DISABLE_HAL_LOCKS
55
- ,nonStopTask (NULL )
55
+ ,currentTaskHandle (NULL )
56
56
,lock(NULL )
57
57
#endif
58
58
#if SOC_I2C_SUPPORT_SLAVE
@@ -433,15 +433,15 @@ void TwoWire::beginTransmission(uint16_t address)
433
433
}
434
434
#endif /* SOC_I2C_SUPPORT_SLAVE */
435
435
#if !CONFIG_DISABLE_HAL_LOCKS
436
- if (nonStop && nonStopTask == xTaskGetCurrentTaskHandle ()){
437
- log_e ( " Unfinished Repeated Start transaction! Expected requestFrom, not beginTransmission! Clearing... " );
438
- // release lock
439
- xSemaphoreGive ( lock);
440
- }
441
- // acquire lock
442
- if (lock == NULL || xSemaphoreTake (lock, portMAX_DELAY) != pdTRUE){
443
- log_e ( " could not acquire lock " );
444
- return ;
436
+ TaskHandle_t task = xTaskGetCurrentTaskHandle ();
437
+ if (currentTaskHandle != task)
438
+ {
439
+ // acquire lock
440
+ if (lock == NULL || xSemaphoreTake (lock, portMAX_DELAY) != pdTRUE){
441
+ log_e ( " could not acquire lock" );
442
+ return ;
443
+ }
444
+ currentTaskHandle = task ;
445
445
}
446
446
#endif
447
447
nonStop = false ;
@@ -475,15 +475,13 @@ uint8_t TwoWire::endTransmission(bool sendStop)
475
475
if (sendStop){
476
476
err = i2cWrite (num, txAddress, txBuffer, txLength, _timeOutMillis);
477
477
#if !CONFIG_DISABLE_HAL_LOCKS
478
+ currentTaskHandle = NULL ;
478
479
// release lock
479
480
xSemaphoreGive (lock);
480
481
#endif
481
482
} else {
482
483
// mark as non-stop
483
484
nonStop = true ;
484
- #if !CONFIG_DISABLE_HAL_LOCKS
485
- nonStopTask = xTaskGetCurrentTaskHandle ();
486
- #endif
487
485
}
488
486
switch (err){
489
487
case ESP_OK: return 0 ;
@@ -507,13 +505,26 @@ size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
507
505
return 0 ;
508
506
}
509
507
esp_err_t err = ESP_OK;
510
- if (nonStop
511
508
#if !CONFIG_DISABLE_HAL_LOCKS
512
- && nonStopTask == xTaskGetCurrentTaskHandle ()
513
- #endif
514
- ){
509
+ TaskHandle_t task = xTaskGetCurrentTaskHandle ();
510
+ if (currentTaskHandle != task)
511
+ {
512
+ // acquire lock
513
+ if (lock == NULL || xSemaphoreTake (lock, portMAX_DELAY) != pdTRUE){
514
+ log_e (" could not acquire lock" );
515
+ return 0 ;
516
+ }
517
+ currentTaskHandle = task;
518
+ }
519
+ #endif
520
+ if (nonStop){
515
521
if (address != txAddress){
516
522
log_e (" Unfinished Repeated Start transaction! Expected address do not match! %u != %u" , address, txAddress);
523
+ #if !CONFIG_DISABLE_HAL_LOCKS
524
+ currentTaskHandle = NULL ;
525
+ // release lock
526
+ xSemaphoreGive (lock);
527
+ #endif
517
528
return 0 ;
518
529
}
519
530
nonStop = false ;
@@ -524,13 +535,6 @@ size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
524
535
log_e (" i2cWriteReadNonStop returned Error %d" , err);
525
536
}
526
537
} else {
527
- #if !CONFIG_DISABLE_HAL_LOCKS
528
- // acquire lock
529
- if (lock == NULL || xSemaphoreTake (lock, portMAX_DELAY) != pdTRUE){
530
- log_e (" could not acquire lock" );
531
- return 0 ;
532
- }
533
- #endif
534
538
rxIndex = 0 ;
535
539
rxLength = 0 ;
536
540
err = i2cRead (num, address, rxBuffer, size, _timeOutMillis, &rxLength);
@@ -539,6 +543,7 @@ size_t TwoWire::requestFrom(uint16_t address, size_t size, bool sendStop)
539
543
}
540
544
}
541
545
#if !CONFIG_DISABLE_HAL_LOCKS
546
+ currentTaskHandle = NULL ;
542
547
// release lock
543
548
xSemaphoreGive (lock);
544
549
#endif
0 commit comments