mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-08-02 17:31:53 +00:00
Add SPI interface
This commit is contained in:
parent
2e9f64a045
commit
4140a07307
87
include/sta/spi_interface.hpp
Normal file
87
include/sta/spi_interface.hpp
Normal file
@ -0,0 +1,87 @@
|
||||
#ifndef STA_SPI_INTERFACE_HPP
|
||||
#define STA_SPI_INTERFACE_HPP
|
||||
|
||||
#include <sta/mutex.hpp>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
/**
|
||||
* @brief Interface for SPI hardware.
|
||||
*/
|
||||
class SpiInterface
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @param mutex Mutex object for managing shared access. Pass nullptr for no access control
|
||||
*/
|
||||
SpiInterface(Mutex * mutex = nullptr);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Send single byte of data.
|
||||
*
|
||||
* @param value 8-bit value
|
||||
*/
|
||||
virtual void transfer(uint8_t value) = 0;
|
||||
/**
|
||||
* @brief Send data from buffer.
|
||||
*
|
||||
* @param buffer Source buffer
|
||||
* @param size Number of bytes in buffer
|
||||
*/
|
||||
virtual void transfer(const uint8_t * buffer, size_t size) = 0;
|
||||
|
||||
/**
|
||||
* @brief Send two bytes of data.
|
||||
*
|
||||
* @param value 16-bit value
|
||||
*/
|
||||
virtual void transfer16(uint16_t value) = 0;
|
||||
|
||||
/**
|
||||
* @brief Send byte value repeatedly.
|
||||
*
|
||||
* @param value 8-bit value to repeat
|
||||
* @param count Number of repetitions
|
||||
*/
|
||||
virtual void fill(uint8_t value, size_t count) = 0;
|
||||
/**
|
||||
* @brief Send 32-bit value repeatedly.
|
||||
*
|
||||
* @param value 32-bit value to repeat
|
||||
* @param count Number of repetitions
|
||||
*/
|
||||
virtual void fill32(uint32_t value, size_t count) = 0;
|
||||
|
||||
/**
|
||||
* @brief Read incoming data to buffer.
|
||||
*
|
||||
* @param buffer Destination buffer
|
||||
* @param size Number of bytes to read
|
||||
*/
|
||||
virtual void receive(uint8_t * buffer, size_t size) = 0;
|
||||
|
||||
/**
|
||||
* @brief Acquire usage rights to use the interface.
|
||||
*
|
||||
* Must be called before any I/O operations are executed.
|
||||
*/
|
||||
virtual void acquire();
|
||||
/**
|
||||
* @brief Release usage rights for interface.
|
||||
*
|
||||
* Must be called after last I/O operation.
|
||||
*/
|
||||
virtual void release();
|
||||
|
||||
private:
|
||||
Mutex * mutex_; /**< Mutex object */
|
||||
};
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_SPI_INTERFACE_HPP
|
21
src/spi_interface.cpp
Normal file
21
src/spi_interface.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include <sta/spi_interface.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
SpiInterface::SpiInterface(Mutex * mutex /* = nullptr */)
|
||||
: mutex_{mutex}
|
||||
{}
|
||||
|
||||
void SpiInterface::acquire()
|
||||
{
|
||||
if (mutex_ != nullptr)
|
||||
mutex_->acquire();
|
||||
}
|
||||
|
||||
void SpiInterface::release()
|
||||
{
|
||||
if (mutex_ != nullptr)
|
||||
mutex_->release();
|
||||
}
|
||||
} // namespace sta
|
Loading…
x
Reference in New Issue
Block a user