fixes for raspi i2c support

This commit is contained in:
Dario
2023-08-15 17:01:32 +01:00
parent d1d43870f4
commit 558a574793
4 changed files with 23 additions and 14 deletions

View File

@@ -43,14 +43,9 @@ namespace sta
{
STA_ASSERT(open_);
selectAddress();
ssize_t n_out = write(i2cfd_, &value, 1);
if (n_out < 0)
{
STA_DEBUG_PRINT("Transfer of single byte failed: ");
STA_DEBUG_PRINTLN(strerror(errno));
}
STA_ASSERT(n_out == 1);
}
@@ -58,6 +53,7 @@ namespace sta
{
STA_ASSERT(open_);
selectAddress();
ssize_t n_out = write(i2cfd_, &value, 2);
STA_ASSERT(n_out == 2);
@@ -67,6 +63,7 @@ namespace sta
{
STA_ASSERT(open_);
selectAddress();
ssize_t n_out = write(i2cfd_, buffer, size);
STA_ASSERT(n_out == size);
@@ -83,16 +80,18 @@ namespace sta
{
STA_ASSERT(open_);
if (read(i2cfd_, buffer, size) <= 0)
{
printf("Error while reading i2c bus.");
}
selectAddress();
ssize_t n_in = read(i2cfd_, buffer, size);
STA_ASSERT(n_in >= 0);
}
void RaspiI2C::fill(uint8_t value, size_t count)
{
STA_ASSERT(open_);
selectAddress();
// Initialize a buffer of size count and fill it with the value.
uint8_t *buffer = new uint8_t[count];
memset(buffer, value, count);
@@ -104,6 +103,14 @@ namespace sta
delete [] buffer;
}
void RaspiI2C::selectAddress()
{
// Select the slave with the given address.
int rslt = ioctl(i2cfd_, I2C_SLAVE, address_);
STA_ASSERT(rslt != -1);
}
void RaspiI2C::acquire()
{
I2C::acquire();
@@ -116,14 +123,13 @@ namespace sta
open_ = true;
STA_ASSERT(i2cfd_ >= 0);
STA_DEBUG_PRINTLN("Successfully opened file");
}
void RaspiI2C::release()
{
if (!persistent_open_ && open_) {
close(i2cfd_);
open_ = false;
}
I2C::release();
@@ -134,6 +140,8 @@ namespace sta
{
}
} // namespace sta
#endif // STA_PLATFORM_RASPI