From 470ad7537695229e57f2332fdd54a96fed3e556a Mon Sep 17 00:00:00 2001 From: Erki Date: Fri, 16 Feb 2024 16:51:39 +0200 Subject: [PATCH] Peripherals: Begin ESP HAL --- CMakeLists.txt | 2 + Peripherals/Inc/peripherals_hal_concepts.hpp | 36 ++++++++++++++ Peripherals/Inc/peripherals_hal_esp.hpp | 51 ++++++++++++++++++++ Peripherals/Inc/peripherals_hal_st.hpp | 6 +++ 4 files changed, 95 insertions(+) create mode 100644 Peripherals/Inc/peripherals_hal_concepts.hpp create mode 100644 Peripherals/Inc/peripherals_hal_esp.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 21e7acd..a6f4bf5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/) option(SKULLC_WITH_TESTS "Enable unit testing." OFF) option(SKULLC_WITH_HAL "Enable the compiling and deployment of the HAL dependent sections." OFF) +option(SKULLC_USE_HAL_ST "Enable the ST HAl when SKULLC_WITH_HAL is enabled." OFF) +option(SKULLC_USE_HAL_ESP "Enable the ESP HAL when SKULLC_WITH_HAL is enabled." OFF) option(SKULLC_WITH_DOCS "Enable documentation building." OFF) include(skullc-install) diff --git a/Peripherals/Inc/peripherals_hal_concepts.hpp b/Peripherals/Inc/peripherals_hal_concepts.hpp new file mode 100644 index 0000000..8fc5ecc --- /dev/null +++ b/Peripherals/Inc/peripherals_hal_concepts.hpp @@ -0,0 +1,36 @@ +// +// Created by erki on 11/02/24. +// + +#pragma once + +#include +#include + +namespace Peripherals::Hal +{ + +template +concept Gpio = requires (T t, const T ct) { + t.set(true); + t.toggle(); + { ct.read() } -> std::same_as; +}; + +template +concept SerialInterface = requires (T t, std::uint8_t* data, const std::uint32_t len) { + { t.transmit(data, len) } -> std::same_as; + { t.transmit(std::array{}) } -> std::same_as; + { t.receive(data, len) } -> std::same_as; + { t.receive(std::array{}) } -> std::same_as; +}; + +template +concept RegisterInterface = requires (T t, RegName reg, std::uint8_t* data, const std::uint32_t len) { + t.writerRegister(reg, std::uint8_t{}); + t.writeRegisterMultibyte(reg, data, len); + { t.readRegister(reg, std::uint32_t{}) } -> std::same_as; + t.readRegisterMultibyte(reg, data, len, std::uint32_t{}); +}; + +} diff --git a/Peripherals/Inc/peripherals_hal_esp.hpp b/Peripherals/Inc/peripherals_hal_esp.hpp new file mode 100644 index 0000000..53130f2 --- /dev/null +++ b/Peripherals/Inc/peripherals_hal_esp.hpp @@ -0,0 +1,51 @@ +// +// Created by erki on 11/02/24. +// + +#pragma once + +#ifdef SKULLC_USE_HAL_ESP + +#include + +#include + +namespace Peripherals::Hal::Esp +{ + +struct Gpio +{ + gpio_num_t gpio; + const bool inverted = false; + + Gpio() = delete; + explicit Gpio(gpio_num_t gpio, const bool inverted) + : gpio(gpio), inverted(inverted) + { } + + void set(const bool& state) + { + gpio_set_level(gpio, inverted ? !state : state); + } + + void toggle() { gpio_set_level(gpio, !gpio_get_level(gpio)); } + + bool read() const + { + return inverted ? !bool(gpio_get_level(gpio)) : bool(gpio_get_level(gpio)); + } +}; + +static_assert(Peripherals::Hal::Gpio); + +#define CREATE_GPIO(name) \ + Peripherals::Hal::Esp::Gpio{name, false} + +#define CREATE_INV_GPIO(name) \ + Peripherals::Hal::Esp::Gpio{name, true} + +} + +#else +# warning "ESP HAL included without SKULLC_USE_HAL_ESP being defined." +#endif /* SKULLC_USE_HAL_ESP */ diff --git a/Peripherals/Inc/peripherals_hal_st.hpp b/Peripherals/Inc/peripherals_hal_st.hpp index 2e3033b..499fdb0 100644 --- a/Peripherals/Inc/peripherals_hal_st.hpp +++ b/Peripherals/Inc/peripherals_hal_st.hpp @@ -8,6 +8,8 @@ #ifndef SKULLC_PERIPHERALS_HAL_ST_HPP_ #define SKULLC_PERIPHERALS_HAL_ST_HPP_ +#ifdef SKULLC_USE_HAL_ST + #include #include @@ -343,4 +345,8 @@ struct ItmSerialInterface }// namespace Hal }// namespace Peripherals +#else +# warning "ESP HAL included without SKULLC_USE_HAL_ESP being defined." +#endif /* SKULLC_USE_HAL_ST */ + #endif /* SKULLC_PERIPHERALS_HAL_ST_HPP_ */