Rework SPI device interface

This commit is contained in:
Henrik Stickann 2022-05-09 21:12:59 +02:00
parent 99eb7e79fd
commit 7c0c05d296
2 changed files with 21 additions and 6 deletions

View File

@ -4,6 +4,7 @@
#ifndef STA_SPI_DEVICE_HPP #ifndef STA_SPI_DEVICE_HPP
#define STA_SPI_DEVICE_HPP #define STA_SPI_DEVICE_HPP
#include <sta/intf/gpio_pin.hpp>
#include <sta/intf/spi_interface.hpp> #include <sta/intf/spi_interface.hpp>
#include <cstddef> #include <cstddef>
@ -20,8 +21,9 @@ namespace sta
public: public:
/** /**
* @param intf SPI hardware interface * @param intf SPI hardware interface
* @param pin Chip select pin
*/ */
SpiDevice(SpiInterface * intf); SpiDevice(SpiInterface * intf, GpioPin * csPin);
/** /**
@ -94,14 +96,15 @@ namespace sta
/** /**
* @brief Activate device via CS pin. * @brief Activate device via CS pin.
*/ */
virtual void select() = 0; void select();
/** /**
* @brief Deactivate device via CS pin. * @brief Deactivate device via CS pin.
*/ */
virtual void deselect() = 0; void deselect();
private: private:
SpiInterface * intf_; /**< SPI hardware interface */ SpiInterface * intf_; /**< SPI hardware interface */
GpioPin * csPin_; /**< Chip select pin */
}; };
} // namespace sta } // namespace sta

View File

@ -1,14 +1,15 @@
#include <sta/intf/spi_device.hpp> #include <sta/spi_device.hpp>
#include <sta/assert.hpp> #include <sta/assert.hpp>
namespace sta namespace sta
{ {
SpiDevice::SpiDevice(SpiInterface * intf) SpiDevice::SpiDevice(SpiInterface * intf, GpioPin * csPin)
: intf_{intf} : intf_{intf}, csPin_{csPin}
{ {
STA_ASSERT(intf != nullptr); STA_ASSERT(intf != nullptr);
STA_ASSERT(csPin != nullptr);
} }
void SpiDevice::beginTransmission() void SpiDevice::beginTransmission()
@ -73,4 +74,15 @@ namespace sta
{ {
return intf_->settings(); return intf_->settings();
} }
void SpiDevice::select()
{
csPin_->setState(GpioPinState::LOW);
}
void SpiDevice::deselect()
{
csPin_->setState(GpioPinState::HIGH);
}
} // namespace sta } // namespace sta