summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-11-24 07:18:14 +1100
committerjacqueline <me@jacqueline.id.au>2023-11-24 07:41:48 +1100
commit230721cd6271f3239b42e1d2471f8db15bebd712 (patch)
treecc27ec975261742dca8c9acf87b806f5933faae8 /src/drivers
parent2b095948b81cbde28ac84e7a572d472301a50af1 (diff)
downloadtangara-fw-230721cd6271f3239b42e1d2471f8db15bebd712.tar.gz
Periodically check int lines instead of relying on interrupts
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/gpios.cpp33
-rw-r--r--src/drivers/include/gpios.hpp5
-rw-r--r--src/drivers/include/samd.hpp4
-rw-r--r--src/drivers/samd.cpp27
4 files changed, 13 insertions, 56 deletions
diff --git a/src/drivers/gpios.cpp b/src/drivers/gpios.cpp
index dc737710..e5560665 100644
--- a/src/drivers/gpios.cpp
+++ b/src/drivers/gpios.cpp
@@ -6,12 +6,13 @@
#include "gpios.hpp"
-#include <stdint.h>
-
#include <cstdint>
+#include "assert.h"
#include "driver/gpio.h"
#include "esp_attr.h"
+#include "esp_err.h"
+#include "esp_intr_alloc.h"
#include "hal/gpio_types.h"
#include "i2c.hpp"
@@ -60,12 +61,7 @@ constexpr std::pair<uint8_t, uint8_t> unpack(uint16_t ba) {
return std::pair((uint8_t)ba, (uint8_t)(ba >> 8));
}
-SemaphoreHandle_t Gpios::sReadPending;
-
-IRAM_ATTR static void interrupt_isr(void* arg) {
- SemaphoreHandle_t sem = reinterpret_cast<SemaphoreHandle_t>(arg);
- xSemaphoreGive(sem);
-}
+static constexpr gpio_num_t kIntPin = GPIO_NUM_34;
auto Gpios::Create() -> Gpios* {
Gpios* instance = new Gpios();
@@ -78,22 +74,10 @@ auto Gpios::Create() -> Gpios* {
}
Gpios::Gpios() : ports_(pack(kPortADefault, kPortBDefault)), inputs_(0) {
- gpio_config_t config{
- .pin_bit_mask = static_cast<uint64_t>(1) << GPIO_NUM_34,
- .mode = GPIO_MODE_INPUT,
- .pull_up_en = GPIO_PULLUP_ENABLE,
- .pull_down_en = GPIO_PULLDOWN_DISABLE,
- .intr_type = GPIO_INTR_NEGEDGE,
- };
- gpio_config(&config);
- gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1);
- gpio_isr_handler_add(GPIO_NUM_34, &interrupt_isr, sReadPending);
+ gpio_set_direction(kIntPin, GPIO_MODE_INPUT);
}
-Gpios::~Gpios() {
- gpio_isr_handler_remove(GPIO_NUM_34);
- gpio_uninstall_isr_service();
-}
+Gpios::~Gpios() {}
auto Gpios::WriteBuffered(Pin pin, bool value) -> void {
if (value) {
@@ -142,9 +126,4 @@ auto Gpios::Read() -> bool {
return true;
}
-auto Gpios::CreateReadPending() -> SemaphoreHandle_t {
- sReadPending = xSemaphoreCreateBinary();
- return sReadPending;
-}
-
} // namespace drivers
diff --git a/src/drivers/include/gpios.hpp b/src/drivers/include/gpios.hpp
index 18f71551..1755da92 100644
--- a/src/drivers/include/gpios.hpp
+++ b/src/drivers/include/gpios.hpp
@@ -10,6 +10,7 @@
#include <atomic>
#include <functional>
+#include <map>
#include <memory>
#include <mutex>
#include <optional>
@@ -108,8 +109,6 @@ class Gpios : public IGpios {
*/
auto Read(void) -> bool;
- static auto CreateReadPending() -> SemaphoreHandle_t;
-
// Not copyable or movable. There should usually only ever be once instance
// of this class, and that instance will likely have a static lifetime.
Gpios(const Gpios&) = delete;
@@ -120,8 +119,6 @@ class Gpios : public IGpios {
std::atomic<uint16_t> ports_;
std::atomic<uint16_t> inputs_;
-
- static SemaphoreHandle_t sReadPending;
};
} // namespace drivers
diff --git a/src/drivers/include/samd.hpp b/src/drivers/include/samd.hpp
index 2640eb8b..d9f1ca48 100644
--- a/src/drivers/include/samd.hpp
+++ b/src/drivers/include/samd.hpp
@@ -54,8 +54,6 @@ class Samd {
auto ResetToFlashSamd() -> void;
auto PowerDown() -> void;
- static auto CreateReadPending() -> SemaphoreHandle_t;
-
// Not copyable or movable. There should usually only ever be once instance
// of this class, and that instance will likely have a static lifetime.
Samd(const Samd&) = delete;
@@ -64,8 +62,6 @@ class Samd {
private:
std::optional<ChargeStatus> charge_status_;
UsbStatus usb_status_;
-
- static SemaphoreHandle_t sReadPending;
};
} // namespace drivers
diff --git a/src/drivers/samd.cpp b/src/drivers/samd.cpp
index e6014306..f361513e 100644
--- a/src/drivers/samd.cpp
+++ b/src/drivers/samd.cpp
@@ -5,12 +5,15 @@
*/
#include "samd.hpp"
-#include <stdint.h>
+
+#include <cstdint>
#include <optional>
#include "esp_err.h"
#include "esp_log.h"
+#include "hal/gpio_types.h"
#include "hal/i2c_types.h"
+
#include "i2c.hpp"
enum Registers : uint8_t {
@@ -26,23 +29,10 @@ static const uint8_t kAddress = 0x45;
namespace drivers {
-SemaphoreHandle_t Samd::sReadPending;
-
-static void interrupt_isr(void* arg) {
- SemaphoreHandle_t sem = reinterpret_cast<SemaphoreHandle_t>(arg);
- xSemaphoreGive(sem);
-}
+static constexpr gpio_num_t kIntPin = GPIO_NUM_35;
Samd::Samd() {
- gpio_config_t config{
- .pin_bit_mask = static_cast<uint64_t>(1) << GPIO_NUM_35,
- .mode = GPIO_MODE_INPUT,
- .pull_up_en = GPIO_PULLUP_ENABLE,
- .pull_down_en = GPIO_PULLDOWN_DISABLE,
- .intr_type = GPIO_INTR_NEGEDGE,
- };
- gpio_config(&config);
- gpio_isr_handler_add(GPIO_NUM_35, &interrupt_isr, sReadPending);
+ gpio_set_direction(kIntPin, GPIO_MODE_INPUT);
// Being able to interface with the SAMD properly is critical. To ensure we
// will be able to, we begin by checking the I2C protocol version is
@@ -153,9 +143,4 @@ auto Samd::PowerDown() -> void {
ESP_ERROR_CHECK(transaction.Execute(3));
}
-auto Samd::CreateReadPending() -> SemaphoreHandle_t {
- sReadPending = xSemaphoreCreateBinary();
- return sReadPending;
-}
-
} // namespace drivers