You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.9 KiB
C++
88 lines
1.9 KiB
C++
#include "SpiLib.h"
|
|
|
|
//typedef unsigned char u8;
|
|
|
|
#define RE_SUC 0x01
|
|
#define RE_ERROR 0x00
|
|
|
|
#define TAG_SPI "SPI"
|
|
|
|
int SpiLIb::spi_transfer(int fd, unsigned char *txbuf, unsigned char *rxbuf, int bytes)
|
|
{
|
|
struct spi_ioc_transfer xfer[2];
|
|
|
|
int status;
|
|
|
|
memset(xfer, 0, sizeof(xfer));
|
|
|
|
xfer[0].tx_buf = (__u64)txbuf;
|
|
xfer[0].rx_buf = (__u64)rxbuf;
|
|
xfer[0].len = bytes;
|
|
xfer[0].delay_usecs = 2;
|
|
|
|
status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);
|
|
if (status < 0)
|
|
{
|
|
perror("SPI_IOC_MESSAGE");
|
|
return -1;
|
|
}
|
|
|
|
return status;
|
|
}
|
|
|
|
void SpiLIb::spi_master_init(const char *name, int fd)
|
|
{
|
|
__u8 mode = 3;
|
|
__u8 lsb = 0;
|
|
__u8 bits = 8;
|
|
//__u32 speed = 30000000;
|
|
__u32 speed = 2000000;
|
|
//__u32 speed = 2000000;
|
|
// __u32 speed = 33000000;
|
|
|
|
// SPI_IOC_WR_MODE
|
|
int res = 0;
|
|
|
|
res = ioctl(fd, SPI_IOC_WR_MODE, &mode);
|
|
res = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
|
|
res = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &lsb);
|
|
|
|
if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0)
|
|
{
|
|
perror("SPI rd_mode");
|
|
return;
|
|
}
|
|
|
|
if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0)
|
|
{
|
|
perror("SPI rd_lsb_fist");
|
|
return;
|
|
}
|
|
|
|
if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0)
|
|
{
|
|
perror("SPI rd bits_per_word");
|
|
return;
|
|
}
|
|
|
|
if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0)
|
|
{
|
|
perror("SPI rd max_speed_hz");
|
|
return;
|
|
}
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "%s: spi mode %d, %d bits %sper word, %d Hz max\n", name, mode, bits, lsb ? "(lsb first) " : "", speed);
|
|
//printf("%s: spi mode %d, %d bits %sper word, %d Hz max\n",
|
|
// name, mode, bits, lsb ? "(lsb first) " : "", speed);
|
|
}
|
|
|
|
int SpiLIb::delay(int x)
|
|
{
|
|
#ifdef _WIN32
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(x));
|
|
#else
|
|
usleep(x);
|
|
#endif
|
|
return 0;
|
|
}
|