Skip to content

Зависание microWire.cpp при обрыве питания на I2C устройстве #81

Description

@valertron

Как и у оригинальной библиотеки, обрыв питания на управляемом устройстве приводит к зависанию arduino. Происходит это из-за бесконечных циклов в методах библиотеки, например:

void TwoWire::start()
{													// сервисная функция с нее начинается любая работа с шиной
	TWCR = _BV(TWSTA) | _BV(TWEN) | _BV(TWINT); 	// start + TwoWire enable + установка флага "выполнить задачу"
------->	while (!(TWCR & _BV(TWINT)));					// Ожидание завершения 
}

Чтобы избежать такого поведения, можно использовать что-то типа такого:

void TwoWire::start()
{
    unsigned long startingTime = millis();
    TWCR = _BV(TWSTA) | _BV(TWEN) | _BV(TWINT); 
    while (!(TWCR & _BV(TWINT)))
    {
        if((millis() - startingTime) >= timeOutDelay)
        {
            lockUp()
            return;
        }
    }
}

void TwoWire::lockUp()
{
  TWCR = 0; //releases SDA and SCL lines to high impedance
  TWCR = _BV(TWEN) | _BV(TWEA); //reinitialize TWI 
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions