Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
SPIController.cpp
Go to the documentation of this file.
1
25
26#include "SPIController.hpp"
27#include <cstring>
28#include <fcntl.h>
29#include <linux/spi/spidev.h>
30#include <stdexcept>
31#include <sys/ioctl.h>
32#include <unistd.h>
33
44 CloseFunc closeFunc)
46 speed(DefaultSpeedHz), m_ioctlFunc(ioctlFunc), m_openFunc(openFunc),
47 m_closeFunc(closeFunc) {}
48
55
64bool SPIController::openDevice(const std::string &device) {
65 spi_fd = m_openFunc(device.c_str(), O_RDWR);
66 if (spi_fd < 0) {
67 throw std::runtime_error("Failed to open SPI device");
68 }
69 return true;
70}
71
85void SPIController::configure(uint8_t mode, uint8_t bits, uint32_t speed) {
86 if (spi_fd < 0) {
87 throw std::runtime_error("SPI device not open");
88 }
89
90 this->mode = mode;
91 this->bits = bits;
92 this->speed = speed;
93
94 if (m_ioctlFunc(spi_fd, SPI_IOC_WR_MODE, &mode) < 0) {
95 throw std::runtime_error("Failed to set SPI mode");
96 }
97
98 if (m_ioctlFunc(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0) {
99 throw std::runtime_error("Failed to set SPI bits per word");
100 }
101
102 if (m_ioctlFunc(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
103 throw std::runtime_error("Failed to set SPI speed");
104 }
105}
106
115void SPIController::writeByte(uint8_t address, uint8_t data) {
116 uint8_t tx[] = {static_cast<uint8_t>(Opcode::Write), address, data};
117 spiTransfer(tx, nullptr, sizeof(tx));
118}
119
128uint8_t SPIController::readByte(uint8_t address) {
129 uint8_t tx[] = {static_cast<uint8_t>(Opcode::Read), address, 0x00};
130 uint8_t rx[sizeof(tx)] = {0};
131 spiTransfer(tx, rx, sizeof(tx));
132 return rx[2];
133}
134
145void SPIController::spiTransfer(const uint8_t *tx, uint8_t *rx, size_t length) {
146 if (spi_fd < 0) {
147 throw std::runtime_error("SPI device not open");
148 }
149
150 struct spi_ioc_transfer transfer = {};
151 transfer.tx_buf = reinterpret_cast<unsigned long>(tx);
152 transfer.rx_buf = reinterpret_cast<unsigned long>(rx);
153 transfer.len = length;
154 transfer.speed_hz = speed;
155 transfer.bits_per_word = bits;
156
157 if (m_ioctlFunc(spi_fd, SPI_IOC_MESSAGE(1), &transfer) < 0) {
158 throw std::runtime_error("SPI transfer error");
159 }
160}
161
167 if (spi_fd >= 0) {
169 spi_fd = -1;
170 }
171}
Definition of the SPIController class.
int(*)(int) CloseFunc
int(*)(const char *, int,...) OpenFunc
int(*)(int, unsigned long,...) IoctlFunc
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.