Program Listing for File spi.hh

Return to documentation for file (include/spi_bus/spi.hh)

#ifndef IPLO_SPI_HH
#define IPLO_SPI_HH

#include <cstdint>

namespace iplo {

struct SpiConfig {
    std::uint8_t mode = 0;
    std::uint8_t bitsPerWord = 8;
    std::uint32_t speedHz = 1000000;
    std::uint16_t delayUs = 0;
};

class SPI {
    const char* _fileName;
    int _fd;
    SpiConfig _spiConfig;
    void open();

    void close() const;

public:
    explicit SPI(const char* fileName);

    SPI(const char* fileName, const SpiConfig& config);

    [[nodiscard]] std::int32_t read(std::uint8_t* rxBuffer, std::uint8_t rxBufferLength);

    [[nodiscard]] std::int32_t write(std::uint8_t* txBuffer, std::uint8_t txBufferLength);

    [[nodiscard]] std::int32_t xfer(std::uint8_t* txBuffer, std::uint8_t txBufferLength, std::uint8_t* rxBuffer,
                                    std::uint8_t rxBufferLength);

    bool setSpeed(std::uint32_t speedHz);

    bool setMode(std::uint8_t mode);

    bool setBitsPerWord(std::uint8_t bits);

    bool setConfig(const SpiConfig& spiConfig);

    ~SPI();
};

} /* namespace iplo */

#endif /* IPLO_SPI_HH */