Updated I2C for ESP32 and added mutex protection for printable serial

This commit is contained in:
dario 2024-04-13 17:45:53 +02:00
parent 56de6ac3e2
commit c843aaf33f
5 changed files with 36 additions and 11 deletions

View File

@ -6,13 +6,18 @@
#include <sta/bus/i2c/device.hpp> #include <sta/bus/i2c/device.hpp>
#include <sta/bus/i2c/i2c.hpp> #include <sta/bus/i2c/i2c.hpp>
#include <sta/mutex.hpp>
namespace sta namespace sta
{ {
class ArduinoI2C : public I2C class ArduinoI2C : public I2C
{ {
public: public:
#ifdef STA_PLATFORM_ARDUINO_DEVICE_ESP32
ArduinoI2C(Mutex * mutex = nullptr, uint8_t sda = STA_ESP32_SDA_DEFAULT_PIN, uint8_t scl = STA_ESP32_SCL_DEFAULT_PIN, uint32_t frequency = 0U);
#else
ArduinoI2C(Mutex * mutex = nullptr); ArduinoI2C(Mutex * mutex = nullptr);
#endif // STA_PLATFORM_ARDUINO_DEVICE_ESP32
void transfer(uint8_t value) override; void transfer(uint8_t value) override;
void transfer16(uint16_t value) override; void transfer16(uint16_t value) override;

View File

@ -3,8 +3,13 @@
#include <sta/devices/arduino/mcu/common.hpp> #include <sta/devices/arduino/mcu/common.hpp>
#define STA_MCU_LITTLE_ENDIAN
#define STA_PLATFORM_ARDUINO_DEVICE_ESP32 #define STA_PLATFORM_ARDUINO_DEVICE_ESP32
#define STA_MCU_ESP32_S3 #define STA_MCU_ESP32_S3
#define STA_ESP32_SCL_DEFAULT_PIN 22
#define STA_ESP32_SDA_DEFAULT_PIN 21
#endif // STA_CORE_ARDUINO_MCU_ESP32_S3_HPP #endif // STA_CORE_ARDUINO_MCU_ESP32_S3_HPP

View File

@ -4,15 +4,20 @@
#include <sta/debug/printing/printable.hpp> #include <sta/debug/printing/printable.hpp>
#ifdef STA_PLATFORM_ARDUINO #ifdef STA_PLATFORM_ARDUINO
#include <sta/mutex.hpp>
namespace sta namespace sta
{ {
class PrintableSerial: public Printable class PrintableSerial: public Printable
{ {
public: public:
/** /**
* @brief Construct a new Printable Serial object * @brief Printable implementation for Arduino Serial printing.
*
* @param baud The baud rate for serial printing.
* @param mutex An optional mutex to protect the serial bus.
*/ */
PrintableSerial(unsigned long baud); PrintableSerial(unsigned long baud, Mutex * mutex = nullptr);
/** /**
* @brief Print string. * @brief Print string.
@ -21,6 +26,8 @@ namespace sta
* @param length String length * @param length String length
*/ */
void print(const char * str, size_t length) override; void print(const char * str, size_t length) override;
private:
Mutex * mutex_;
}; };
} // namespace sta } // namespace sta

View File

@ -7,11 +7,19 @@
namespace sta namespace sta
{ {
#ifdef STA_PLATFORM_ARDUINO_DEVICE_ESP32
ArduinoI2C::ArduinoI2C(Mutex * mutex /* = nullptr */, uint8_t sda /* = STA_ESP32_SDA_DEFAULT_PIN */, uint8_t scl /*= STA_ESP32_SCL_DEFAULT_PIN */, uint32_t frequency /* = 0U */)
: I2C{mutex}
{
Wire.begin(sda, scl, frequency);
}
#else
ArduinoI2C::ArduinoI2C(Mutex * mutex /* = nullptr */) ArduinoI2C::ArduinoI2C(Mutex * mutex /* = nullptr */)
: I2C{mutex} : I2C{mutex}
{ {
Wire.begin(); Wire.begin();
} }
#endif // STA_PLATFORM_ARDUINO_DEVICE_ESP32
void ArduinoI2C::transfer(uint8_t value) void ArduinoI2C::transfer(uint8_t value)
{ {
@ -36,15 +44,11 @@ namespace sta
void ArduinoI2C::receive(uint8_t * buffer, size_t size) void ArduinoI2C::receive(uint8_t * buffer, size_t size)
{ {
Wire.requestFrom((uint8_t)address_, (uint8_t)size); size_t count = Wire.requestFrom((uint8_t)address_, (uint8_t)size, true);
size_t index = 0;
while (index < size) for (size_t i = 0; i < min(size, count); ++i)
{ {
while (Wire.available()) buffer[i] = Wire.read();
{
buffer[index++] = Wire.read();
}
} }
} }

View File

@ -3,18 +3,22 @@
#ifdef STA_PLATFORM_ARDUINO #ifdef STA_PLATFORM_ARDUINO
#include <sta/devices/arduino/hal.hpp> #include <sta/devices/arduino/hal.hpp>
#include <sta/mutex.hpp>
namespace sta namespace sta
{ {
PrintableSerial::PrintableSerial(unsigned long baud) PrintableSerial::PrintableSerial(unsigned long baud, Mutex * mutex /* = nullptr */)
: Printable{} : Printable{},
mutex_{mutex}
{ {
Serial.begin(baud); Serial.begin(baud);
} }
void PrintableSerial::print(const char * str, size_t length) void PrintableSerial::print(const char * str, size_t length)
{ {
mutex_->acquire();
Serial.write(str); Serial.write(str);
mutex_->release();
} }
} // namespace sta } // namespace sta