From 03ca528d91948e10cc05617122cd786026c4681e Mon Sep 17 00:00:00 2001 From: Henrik Stickann <4376396-Mithradir@users.noreply.gitlab.com> Date: Sat, 9 Apr 2022 21:49:04 +0200 Subject: [PATCH] Add HAL SPI implementation --- include/sta/hal/spi.hpp | 70 +++++++++++++++++++++++++++++++++++++++++ src/hal/spi.cpp | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 include/sta/hal/spi.hpp create mode 100644 src/hal/spi.cpp diff --git a/include/sta/hal/spi.hpp b/include/sta/hal/spi.hpp new file mode 100644 index 0000000..a8b2afe --- /dev/null +++ b/include/sta/hal/spi.hpp @@ -0,0 +1,70 @@ +/** + * @brief Implementations for SpiInterface and SpiDevice using HAL. + * + * Define STA_HAL_SPI_ENABLE in to enable. + * Requires STA_HAL_GPIO_ENABLE. + */ +#ifndef STA_HAL_SPI_HPP +#define STA_HAL_SPI_HPP + +#include +#ifdef STA_HAL_SPI_ENABLE + +#include +#include + +#include + + +namespace sta +{ + /** + * @brief Implementation of SpiInterface using HAL. + */ + class HalSpiInterface : public SpiInterface + { + public: + /** + * @param handle SPI handle + * @param mutex Mutex object for managing access. Pass nullptr for no access control + */ + HalSpiInterface(SPI_HandleTypeDef * handle, Mutex * mutex = nullptr); + + void transfer(uint8_t value) override; + void transfer(const uint8_t * buffer, size_t size) override; + void transfer16(uint16_t value) override; + + void fill(uint8_t value, size_t count) override; + void fill32(uint32_t value, size_t count) override; + + void receive(uint8_t * buffer, size_t size) override; + + private: + SPI_HandleTypeDef * handle_; /**< SPI handle */ + }; + + + /** + * @brief Implementation of SpiDevice using HAL. + */ + class HalSpiDevice : public SpiDevice + { + public: + /** + * @param intf SPI interface + * @param csPin Device CS pin + */ + HalSpiDevice(SpiInterface * intf, HalGpioPin csPin); + + void select() override; + void deselect() override; + + private: + HalGpioPin csPin_; /**< Device CS pin */ + }; +} // namespace sta + + +#endif // STA_HAL_SPI_ENABLE + +#endif // STA_HAL_SPI_HPP diff --git a/src/hal/spi.cpp b/src/hal/spi.cpp new file mode 100644 index 0000000..48f25f1 --- /dev/null +++ b/src/hal/spi.cpp @@ -0,0 +1,70 @@ +#include + +#ifdef STA_HAL_SPI_ENABLE + + +namespace sta +{ + HalSpiInterface::HalSpiInterface(SPI_HandleTypeDef * handle, Mutex * mutex /* = nullptr */) + : SpiInterface(mutex), handle_{handle} + {} + + + void HalSpiInterface::transfer(uint8_t data) + { + HAL_SPI_Transmit(handle_, &data, 1, HAL_MAX_DELAY); + } + + void HalSpiInterface::transfer(const uint8_t * data, size_t size) + { + HAL_SPI_Transmit(handle_, const_cast(data), size, HAL_MAX_DELAY); + } + + + void HalSpiInterface::transfer16(uint16_t value) + { + HAL_SPI_Transmit(handle_, reinterpret_cast(&value), 2, HAL_MAX_DELAY); + } + + + void HalSpiInterface::fill(uint8_t value, size_t count) + { + for (size_t i = 0; i < count; ++i) + { + transfer(value); + } + } + + void HalSpiInterface::fill32(uint32_t value, size_t count) + { + for (size_t i = 0; i < count; ++i) + { + HAL_SPI_Transmit(handle_, reinterpret_cast(&value), 4, HAL_MAX_DELAY); + } + } + + + void HalSpiInterface::receive(uint8_t * data, size_t size) + { + HAL_SPI_Receive(handle_, data, size, HAL_MAX_DELAY); + } + + + + HalSpiDevice::HalSpiDevice(SpiInterface * intf, HalGpioPin csPin) + : SpiDevice(intf), csPin_{csPin} + {} + + void HalSpiDevice::select() + { + STA_HAL_GPIO_PIN_WRITE(csPin_, GPIO_PIN_RESET); + } + + void HalSpiDevice::deselect() + { + STA_HAL_GPIO_PIN_WRITE(csPin_, GPIO_PIN_SET); + } +} // namespace sta + + +#endif // STA_HAL_SPI_ENABLE