summaryrefslogtreecommitdiff
path: root/src/drivers/include
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2025-08-08 16:14:41 +1000
committerjacqueline <me@jacqueline.id.au>2025-08-08 16:14:41 +1000
commit73baf2f88f61e307afd6cd17f6727da4e446b64c (patch)
treed13241937786b3d422423cc2b804e0ec42b21335 /src/drivers/include
parent2b2e595a8fb28e34ec33a98035c31bd4cba76808 (diff)
downloadtangara-fw-73baf2f88f61e307afd6cd17f6727da4e446b64c.tar.gz
Migrate to the new esp-idf I2C driver
It's a better, less verbose driver, and also this fixes an occasional crash on boot.
Diffstat (limited to 'src/drivers/include')
-rw-r--r--src/drivers/include/drivers/gpios.hpp3
-rw-r--r--src/drivers/include/drivers/haptics.hpp3
-rw-r--r--src/drivers/include/drivers/i2c.hpp80
-rw-r--r--src/drivers/include/drivers/samd.hpp2
-rw-r--r--src/drivers/include/drivers/touchwheel.hpp1
-rw-r--r--src/drivers/include/drivers/wm8523.hpp2
6 files changed, 13 insertions, 78 deletions
diff --git a/src/drivers/include/drivers/gpios.hpp b/src/drivers/include/drivers/gpios.hpp
index 5010e45f..61d458a5 100644
--- a/src/drivers/include/drivers/gpios.hpp
+++ b/src/drivers/include/drivers/gpios.hpp
@@ -18,6 +18,7 @@
#include <utility>
#include "driver/i2c.h"
+#include "driver/i2c_types.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_log.h"
@@ -138,6 +139,8 @@ class Gpios : public IGpios {
private:
Gpios(bool invert_lock);
+ i2c_master_dev_handle_t i2c_;
+
std::atomic<uint16_t> ports_;
std::atomic<uint16_t> inputs_;
const bool invert_lock_switch_;
diff --git a/src/drivers/include/drivers/haptics.hpp b/src/drivers/include/drivers/haptics.hpp
index e4666fad..60c54851 100644
--- a/src/drivers/include/drivers/haptics.hpp
+++ b/src/drivers/include/drivers/haptics.hpp
@@ -14,6 +14,7 @@
#include <string>
#include <variant>
+#include "driver/i2c_types.h"
#include "drivers/nvs.hpp"
namespace drivers {
@@ -197,6 +198,7 @@ class Haptics {
auto TourLibraries(Effect from, Effect to) -> void;
private:
+ i2c_master_dev_handle_t i2c_;
std::optional<Effect> current_effect_;
std::mutex playing_effect_;
@@ -313,7 +315,6 @@ class Haptics {
kLraResonancePeriod = 0,
};
- auto PowerUp() -> void;
auto WriteRegister(Register reg, uint8_t val) -> void;
auto ReadRegister(Register reg) -> uint8_t;
diff --git a/src/drivers/include/drivers/i2c.hpp b/src/drivers/include/drivers/i2c.hpp
index 0dc1e7c0..55ac49b2 100644
--- a/src/drivers/include/drivers/i2c.hpp
+++ b/src/drivers/include/drivers/i2c.hpp
@@ -8,86 +8,12 @@
#include <cstdint>
-#include "driver/i2c.h"
-#include "hal/i2c_types.h"
+#include "driver/i2c_master.h"
+#include "driver/i2c_types.h"
namespace drivers {
esp_err_t init_i2c(void);
-esp_err_t deinit_i2c(void);
-
-/*
- * Convenience wrapper for performing an I2C transaction with a reasonable
- * preconfigured timeout, automatic management of a heap-based command buffer,
- * and a terser API for enqueuing bytes.
- *
- * Any error codes from the underlying ESP IDF are treated as fatal, since they
- * typically represent invalid arguments or OOMs.
- */
-class I2CTransaction {
- public:
- static const uint8_t kI2CTimeout = pdMS_TO_TICKS(100);
-
- I2CTransaction();
- ~I2CTransaction();
-
- /*
- * Executes all enqueued commands, returning the result code. Possible error
- * codes, per the ESP-IDF docs:
- *
- * ESP_OK Success
- * ESP_ERR_INVALID_ARG Parameter error
- * ESP_FAIL Sending command error, slave doesn’t ACK the transfer.
- * ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
- * ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
- */
- esp_err_t Execute(int num_retries = 0);
-
- /*
- * Enqueues a start condition. May also be used for repeated start
- * conditions.
- */
- I2CTransaction& start();
- /* Enqueues a stop condition. */
- I2CTransaction& stop();
-
- /*
- * Enqueues writing the given 7 bit address, followed by one bit indicating
- * whether this is a read or write request.
- *
- * This command will expect an ACK before continuing.
- */
- I2CTransaction& write_addr(uint8_t addr, uint8_t op);
-
- /*
- * Enqueues one or more bytes to be written. The transaction will wait for
- * an ACK to be returned before writing the next byte.
- */
- I2CTransaction& write_ack(uint8_t data);
- template <typename... More>
- I2CTransaction& write_ack(uint8_t data, More... more) {
- write_ack(data);
- write_ack(more...);
- return *this;
- }
-
- /*
- * Enqueues a read of one byte into the given uint8. Responds with the given
- * ACK/NACK type.
- */
- I2CTransaction& read(uint8_t* dest, i2c_ack_type_t ack);
-
- /* Returns the underlying command buffer. */
- i2c_cmd_handle_t handle() { return handle_; }
-
- // Cannot be moved or copied, since doing so is probably an error. Pass a
- // reference instead.
- I2CTransaction(const I2CTransaction&) = delete;
- I2CTransaction& operator=(const I2CTransaction&) = delete;
-
- private:
- i2c_cmd_handle_t handle_;
- uint8_t* buffer_;
-};
+i2c_master_bus_handle_t i2c_handle();
} // namespace drivers
diff --git a/src/drivers/include/drivers/samd.hpp b/src/drivers/include/drivers/samd.hpp
index 2bdfa5c6..ea692cda 100644
--- a/src/drivers/include/drivers/samd.hpp
+++ b/src/drivers/include/drivers/samd.hpp
@@ -10,6 +10,7 @@
#include <optional>
#include <string>
+#include "driver/i2c_types.h"
#include "drivers/nvs.hpp"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
@@ -73,6 +74,7 @@ class Samd {
Samd& operator=(const Samd&) = delete;
private:
+ i2c_master_dev_handle_t i2c_;
NvsStorage& nvs_;
enum class RegisterName {
diff --git a/src/drivers/include/drivers/touchwheel.hpp b/src/drivers/include/drivers/touchwheel.hpp
index 9cd925a6..d14b3919 100644
--- a/src/drivers/include/drivers/touchwheel.hpp
+++ b/src/drivers/include/drivers/touchwheel.hpp
@@ -43,6 +43,7 @@ class TouchWheel {
auto LowPowerMode(bool en) -> void;
private:
+ i2c_master_dev_handle_t i2c_;
TouchWheelData data_;
enum Register {
diff --git a/src/drivers/include/drivers/wm8523.hpp b/src/drivers/include/drivers/wm8523.hpp
index a64f6bac..ac29ed81 100644
--- a/src/drivers/include/drivers/wm8523.hpp
+++ b/src/drivers/include/drivers/wm8523.hpp
@@ -8,6 +8,7 @@
#include <stdint.h>
#include <cstdint>
#include <optional>
+#include "esp_err.h"
namespace drivers {
namespace wm8523 {
@@ -45,6 +46,7 @@ enum class Register : uint8_t {
kZeroDetect = 8,
};
+auto Init() -> esp_err_t;
auto ReadRegister(Register reg) -> std::optional<uint16_t>;
auto WriteRegister(Register reg, uint16_t data) -> bool;
auto WriteRegister(Register reg, uint8_t msb, uint8_t lsb) -> bool;