22 #include <sys/ioctl.h>
23 #include <linux/types.h>
24 #include <linux/spi/spidev.h>
28 #define DEBUG_PRINT(...)
36 void LinuxSpiDriverComponentImpl::SpiReadWrite_handler(
40 if (this->m_fd == -1) {
48 memset(&tr, 0,
sizeof(tr));
49 tr.tx_buf =
reinterpret_cast<__u64
>(writeBuffer.
getData());
50 tr.rx_buf =
reinterpret_cast<__u64
>(readBuffer.
getData());
67 this->m_bytes += readBuffer.
getSize();
76 this->m_device = device;
77 this->m_select = select;
83 snprintf(devName,
sizeof(devName),
"/dev/spidev%d.%d",device,select);
85 devName[
sizeof(devName)-1] = 0;
88 fd =
::open(devName, O_RDWR);
90 DEBUG_PRINT(
"open SPI device %d.%d failed. %d\n",device,select,errno);
94 DEBUG_PRINT(
"Successfully opened SPI device %s fd %d\n",devName,fd);
124 ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
126 DEBUG_PRINT(
"ioctl SPI_IOC_WR_MODE fd %d failed. %d\n",fd,errno);
130 DEBUG_PRINT(
"SPI fd %d WR mode successfully configured to %d\n",fd,mode);
133 ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
135 DEBUG_PRINT(
"ioctl SPI_IOC_RD_MODE fd %d failed. %d\n",fd,errno);
139 DEBUG_PRINT(
"SPI fd %d RD mode successfully configured to %d\n",fd,mode);
146 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
148 DEBUG_PRINT(
"ioctl SPI_IOC_WR_BITS_PER_WORD fd %d failed. %d\n",fd,errno);
152 DEBUG_PRINT(
"SPI fd %d WR bits per word successfully configured to %d\n",fd,bits);
155 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
157 DEBUG_PRINT(
"ioctl SPI_IOC_RD_BITS_PER_WORD fd %d failed. %d\n",fd,errno);
161 DEBUG_PRINT(
"SPI fd %d RD bits per word successfully configured to %d\n",fd,bits);
167 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &clock);
169 DEBUG_PRINT(
"ioctl SPI_IOC_WR_MAX_SPEED_HZ fd %d failed. %d\n",fd,errno);
173 DEBUG_PRINT(
"SPI fd %d WR freq successfully configured to %d\n",fd,clock);
176 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &clock);
178 DEBUG_PRINT(
"ioctl SPI_IOC_RD_MAX_SPEED_HZ fd %d failed. %d\n",fd,errno);
182 DEBUG_PRINT(
"SPI fd %d RD freq successfully configured to %d\n",fd,clock);
191 (void) close(this->m_fd);
PlatformIntType NATIVE_INT_TYPE
uint8_t U8
8-bit unsigned integer
C++-compatible configuration header for fprime configuration.
void log_WARNING_HI_SPI_WriteError(I32 device, I32 select, I32 error)
void tlmWrite_SPI_Bytes(U32 arg, Fw::Time _tlmTime=Fw::Time())
void log_WARNING_HI_SPI_ConfigError(I32 device, I32 select, I32 error)
void log_WARNING_HI_SPI_OpenError(I32 device, I32 select, I32 error)
~LinuxSpiDriverComponentImpl()
bool open(NATIVE_INT_TYPE device, NATIVE_INT_TYPE select, SpiFrequency clock, SpiMode spiMode=SpiMode::SPI_MODE_CPOL_LOW_CPHA_LOW)
Open device.
@ SPI_MODE_CPOL_HIGH_CPHA_LOW
(CPOL = 1, CPHA = 0)
@ SPI_MODE_CPOL_LOW_CPHA_HIGH
(CPOL = 0, CPHA = 1)
@ SPI_MODE_CPOL_HIGH_CPHA_HIGH
(CPOL = 1, CPHA = 1)
@ SPI_MODE_CPOL_LOW_CPHA_LOW
(CPOL = 0, CPHA = 0)