32 #if defined(PIOS_INCLUDE_SPI)
36 #include <sys/ioctl.h>
37 #include <linux/types.h>
38 #include <linux/spi/spidev.h>
53 static bool PIOS_SPI_validate(
struct pios_spi_dev *com_dev)
71 if (!spi_dev)
goto out_fail;
77 spi_dev->slave_count = 0;
80 char path[PATH_MAX + 2];
84 int fd = open(path, O_RDWR);
90 spi_dev->slave_count =
i+1;
94 for (
int i = 0;
i < spi_dev->slave_count;
i++) {
102 spi_dev->slave_count);
104 if (spi_dev->slave_count < 1) {
114 printf(
"PIOS_SPI: fail.\n");
120 bool valid = PIOS_SPI_validate(spi_dev);
123 spi_dev->speed_hz = spi_speed;
130 bool valid = PIOS_SPI_validate(spi_dev);
141 bool valid = PIOS_SPI_validate(spi_dev);
151 bool valid = PIOS_SPI_validate(spi_dev);
155 struct spi_ioc_transfer xfer = {
160 spi_dev->selected = slave_id;
166 spi_dev->selected = -1;
171 int status = ioctl(spi_dev->fd[slave_id], SPI_IOC_MESSAGE(1), &xfer);
174 perror(
"ioctl-SPI_IOC_MESSAGE");
192 bool valid = PIOS_SPI_validate(spi_dev);
194 int slave_id = spi_dev->selected;
200 struct spi_ioc_transfer xfer = {
201 .rx_buf = (uintptr_t) receive_buffer,
202 .tx_buf = (uintptr_t) send_buffer,
204 .speed_hz = spi_dev->speed_hz,
208 int status = ioctl(spi_dev->fd[slave_id], SPI_IOC_MESSAGE(1), &xfer);
211 perror(
"ioctl-SPI_IOC_MESSAGE");
Main PiOS header to include all the compiled in PiOS options.
int32_t PIOS_SPI_RC_PinSet(pios_spi_t spi_dev, uint32_t slave_id, bool pin_value)
int32_t PIOS_SPI_ClaimBus(pios_spi_t spi_dev)
void * PIOS_malloc(size_t size)
static struct flyingpicmd_cfg_fa cfg
struct pios_semaphore * PIOS_Semaphore_Create(void)
Creates a binary semaphore.
uint8_t PIOS_SPI_TransferByte(pios_spi_t spi_dev, uint8_t b)
int32_t PIOS_SPI_SetClockSpeed(pios_spi_t spi_dev, uint32_t speed)
int32_t PIOS_SPI_Init(pios_spi_t *spi_dev, const struct pios_spi_cfg *cfg)
int32_t PIOS_SPI_TransferBlock(pios_spi_t spi_dev, const uint8_t *send_buffer, uint8_t *receive_buffer, uint16_t len)
bool PIOS_Semaphore_Give(struct pios_semaphore *sema)
Gives binary semaphore.
const struct pios_spi_cfg * cfg
bool PIOS_Semaphore_Take(struct pios_semaphore *sema, uint32_t timeout_ms)
Takes binary semaphore.
int printf(const char *format,...)
int snprintf(char *buf, size_t count, const char *format,...)
int32_t PIOS_SPI_ReleaseBus(pios_spi_t spi_dev)
#define PIOS_Assert(test)
struct pios_semaphore * busy