47 #include <sys/ioctl.h> 48 #include <linux/types.h> 49 #include <linux/spi/spidev.h> 51 #include <px4_platform_common/px4_config.h> 56 SPI::SPI(
const char *
name,
const char *devname,
int bus, uint32_t
device,
enum spi_mode_e mode, uint32_t frequency) :
62 DEVICE_DEBUG(
"SPI::SPI name = %s devname = %s", name, devname);
65 _device_id.devid_s.bus_type = DeviceBusType_SPI;
66 _device_id.devid_s.bus = bus;
67 _device_id.devid_s.address = (uint8_t)device;
69 _device_id.devid_s.devtype = 0;
85 snprintf(dev_path,
sizeof(dev_path),
"/dev/spidev%i.%i", get_device_bus(), PX4_SPI_DEV_ID(
_device));
87 _fd = ::open(dev_path, O_RDWR);
90 PX4_ERR(
"could not open %s", dev_path);
119 if ((send ==
nullptr) && (recv ==
nullptr)) {
124 int result = ::ioctl(
_fd, SPI_IOC_WR_MODE, &
_mode);
127 PX4_ERR(
"can’t set spi mode");
131 spi_ioc_transfer spi_transfer[1] {};
133 spi_transfer[0].tx_buf = (uint64_t)send;
134 spi_transfer[0].rx_buf = (uint64_t)recv;
135 spi_transfer[0].len = len;
137 spi_transfer[0].bits_per_word = 8;
139 spi_transfer[0].cs_change =
true;
141 result = ::ioctl(
_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
143 if (result != (
int)len) {
144 PX4_ERR(
"write failed. Reported %d bytes written (%s)", result, strerror(errno));
154 if ((send ==
nullptr) && (recv ==
nullptr)) {
159 int result = ::ioctl(
_fd, SPI_IOC_WR_MODE, &
_mode);
162 PX4_ERR(
"can’t set spi mode");
167 result = ::ioctl(
_fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
170 PX4_ERR(
"can’t set 16 bit spi mode");
174 spi_ioc_transfer spi_transfer[1] {};
176 spi_transfer[0].tx_buf = (uint64_t)send;
177 spi_transfer[0].rx_buf = (uint64_t)recv;
178 spi_transfer[0].len = len * 2;
182 spi_transfer[0].cs_change =
true;
184 result = ::ioctl(
_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
186 if (result != (
int)(len * 2)) {
187 PX4_ERR(
"write failed. Reported %d bytes written (%s)", result, strerror(errno));
196 #endif // __PX4_LINUX int transfer(uint8_t *send, uint8_t *recv, unsigned len)
Perform a SPI transfer.
Namespace encapsulating all device framework classes, functions and data.
virtual int init() override
virtual int probe()
Check for the presence of the device on the bus.
void init()
Activates/configures the hardware registers.
#define DEVICE_LOG(FMT,...)
int transferhword(uint16_t *send, uint16_t *recv, unsigned len)
Perform a SPI 16 bit transfer.
#define DEVICE_DEBUG(FMT,...)