From 558a574793f78088b5ff41f5177b980f938cd230 Mon Sep 17 00:00:00 2001 From: Dario Date: Tue, 15 Aug 2023 17:01:32 +0100 Subject: [PATCH] fixes for raspi i2c support --- include/sta/bus/i2c/device.hpp | 1 - include/sta/config.hpp | 3 ++- include/sta/devices/raspi/bus/i2c.hpp | 1 + src/devices/raspi/bus/i2c.cpp | 32 +++++++++++++++++---------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/include/sta/bus/i2c/device.hpp b/include/sta/bus/i2c/device.hpp index 71552fc..5894b5b 100644 --- a/include/sta/bus/i2c/device.hpp +++ b/include/sta/bus/i2c/device.hpp @@ -22,7 +22,6 @@ namespace sta protected: void select() override; - void deselect() override; private: diff --git a/include/sta/config.hpp b/include/sta/config.hpp index f798ba8..725478f 100644 --- a/include/sta/config.hpp +++ b/include/sta/config.hpp @@ -1,7 +1,8 @@ #ifndef STA_CONFIG_HPP #define STA_CONFIG_HPP -#include +// #include +#include #define STA_DEBUGGING_ENABLED #define STA_PRINTF_USE_STDLIB diff --git a/include/sta/devices/raspi/bus/i2c.hpp b/include/sta/devices/raspi/bus/i2c.hpp index b16449c..d2ba9ef 100644 --- a/include/sta/devices/raspi/bus/i2c.hpp +++ b/include/sta/devices/raspi/bus/i2c.hpp @@ -29,6 +29,7 @@ namespace sta void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; void receive(uint8_t * buffer, size_t size) override; + void selectAddress(); void acquire() override; void release() override; diff --git a/src/devices/raspi/bus/i2c.cpp b/src/devices/raspi/bus/i2c.cpp index 689d7df..393a0e4 100644 --- a/src/devices/raspi/bus/i2c.cpp +++ b/src/devices/raspi/bus/i2c.cpp @@ -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 \ No newline at end of file