Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
SPIController.hpp
Go to the documentation of this file.
1
16
17#ifndef SPICONTROLLER_HPP
18#define SPICONTROLLER_HPP
19
20#include "ISPIController.hpp"
21#include <cstdint>
22#include <fcntl.h>
23#include <string>
24#include <sys/ioctl.h>
25#include <unistd.h>
26
27using IoctlFunc = int (*)(int, unsigned long, ...);
28using OpenFunc = int (*)(const char *, int, ...);
29using CloseFunc = int (*)(int);
30
36public:
37 enum class Opcode : uint8_t { Write = 0x02, Read = 0x03 };
38
39 SPIController(IoctlFunc ioctlFunc = ::ioctl, OpenFunc openFunc = ::open,
40 CloseFunc closeFunc = ::close);
41 ~SPIController() override;
42
43 bool openDevice(const std::string &device) override;
44 void configure(uint8_t mode, uint8_t bits, uint32_t speed) override;
45 void writeByte(uint8_t address, uint8_t data) override;
46 uint8_t readByte(uint8_t address) override;
47 void spiTransfer(const uint8_t *tx, uint8_t *rx, size_t length) override;
48 void closeDevice() override;
49
50private:
52 int spi_fd;
54 uint8_t mode;
56 uint8_t bits;
58 uint32_t speed;
59
66
67 static constexpr uint8_t DefaultBitsPerWord = 8;
68 static constexpr uint32_t DefaultSpeedHz = 1'000'000;
69 static constexpr uint8_t DefaultMode = 0;
70};
71
72#endif // SPICONTROLLER_HPP
Definition of the ISPIController interface.
int(*)(int) CloseFunc
int(*)(const char *, int,...) OpenFunc
int(*)(int, unsigned long,...) IoctlFunc
Interface for the SPI controller.
static constexpr uint32_t DefaultSpeedHz
void configure(uint8_t mode, uint8_t bits, uint32_t speed) override
Configure the SPI device.
IoctlFunc m_ioctlFunc
Function pointer to the ioctl function.
SPIController(IoctlFunc ioctlFunc=::ioctl, OpenFunc openFunc=::open, CloseFunc closeFunc=::close)
Construct a new SPIController::SPIController object.
uint8_t mode
Mode of the SPI communication.
CloseFunc m_closeFunc
Function pointer to the close function.
static constexpr uint8_t DefaultMode
uint8_t readByte(uint8_t address) override
Read a byte from the SPI device.
void closeDevice() override
Close the SPI device.
int spi_fd
File descriptor of the SPI device.
void spiTransfer(const uint8_t *tx, uint8_t *rx, size_t length) override
Transfer data over SPI.
void writeByte(uint8_t address, uint8_t data) override
Write a byte to the SPI device.
bool openDevice(const std::string &device) override
Open the SPI device.
uint8_t bits
Number of bits per word.
static constexpr uint8_t DefaultBitsPerWord
uint32_t speed
Speed of the SPI communication.
OpenFunc m_openFunc
Function pointer to the open function.
~SPIController() override
Destroy the SPIController::SPIController object.