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

@ -22,7 +22,6 @@ namespace sta
protected: protected:
void select() override; void select() override;
void deselect() override; void deselect() override;
private: private:

View File

@ -1,7 +1,8 @@
#ifndef STA_CONFIG_HPP #ifndef STA_CONFIG_HPP
#define STA_CONFIG_HPP #define STA_CONFIG_HPP
#include <sta/devices/stm32/mcu/STM32F411xE.hpp> // #include <sta/devices/stm32/mcu/STM32F411xE.hpp>
#include <sta/devices/raspi/mcu/common.hpp>
#define STA_DEBUGGING_ENABLED #define STA_DEBUGGING_ENABLED
#define STA_PRINTF_USE_STDLIB #define STA_PRINTF_USE_STDLIB

View File

@ -29,6 +29,7 @@ namespace sta
void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override;
void receive(uint8_t * buffer, size_t size) override; void receive(uint8_t * buffer, size_t size) override;
void selectAddress();
void acquire() override; void acquire() override;
void release() override; void release() override;

View File

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