- Posts: 17
- Thank you received: 10
#define TRANSPORT_SPI5
#ifdef TRANSPORT_SPI5
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>
#endif
#ifdef TRANSPORT_SPI5
int spifd;
static uint8_t mode = SPI_MODE_0;
static uint8_t bits = 8;
static uint32_t speed = 1500000;
#endif
#ifdef TRANSPORT_SPI5
rtapi_print("Info: Initialize SPI5 connection\n");
spifd = open("/dev/spidev0.0", O_RDWR);
if (spifd < 0) {
rtapi_print_msg(RTAPI_MSG_ERR,"Failed to open SPI device\n");
return -1;
}
// Set SPI mode
if (ioctl(spifd, SPI_IOC_WR_MODE, &mode) == -1) {
rtapi_print_msg(RTAPI_MSG_ERR,"Failed to set SPI mode\n");
close(spifd);
return -1;
}
// Set bits per word
if (ioctl(spifd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
rtapi_print_msg(RTAPI_MSG_ERR,"Failed to set bits per word\n");
close(spifd);
return -1;
}
// Set max speed
if (ioctl(spifd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
rtapi_print_msg(RTAPI_MSG_ERR,"Failed to set max speed\n");
close(spifd);
return -1;
}
#endif
//Edit rtapi_app_exit function, add close(spifd)
void rtapi_app_exit(void)
{
close(spifd);
hal_exit(comp_id);
}
#ifdef TRANSPORT_SPI5
struct spi_ioc_transfer tr = {
//.tx_buf = (uint64_t)tx_buf,
//.rx_buf = (uint64_t)rx_buf,
.tx_buf = (uint64_t)txData.txBuffer,
.rx_buf = (uint64_t)rxData.rxBuffer,
.len = SPIBUFSIZE,
.speed_hz = speed,
.delay_usecs = 0,
.bits_per_word = bits,
};
// Perform SPI transfer
if (ioctl(spifd, SPI_IOC_MESSAGE(1), &tr) == -1) {
rtapi_print_msg(RTAPI_MSG_ERR,"Failed to perform SPI transfer\n");
//close(spifd);
//return -1;
}
#endif
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.