git-svn-id: svn://db.shs.com.ru/pip@567 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2017-12-06 14:06:40 +00:00
parent ceb7bcab70
commit 71a05ed265

View File

@@ -23,7 +23,7 @@ PISPI::PISPI(const PIString & path, uint speed, PIIODevice::DeviceMode mode) : P
spi_mode = 0; spi_mode = 0;
spi_bits = 8; spi_bits = 8;
#ifndef WINDOWS #ifndef WINDOWS
fd = 0; PRIVATE->fd = 0;
#endif #endif
} }
@@ -36,18 +36,18 @@ void PISPI::setSpeed(uint speed_hz) {
bool PISPI::openDevice() { bool PISPI::openDevice() {
#ifndef WINDOWS #ifndef WINDOWS
int ret = 0; int ret = 0;
fd = open(path().dataAscii(), O_RDWR); PRIVATE->fd = open(path().dataAscii(), O_RDWR);
if (fd < 0) return false; if (PRIVATE->fd < 0) return false;
ret = ioctl(fd, SPI_IOC_WR_MODE, &spi_mode); ret = ioctl(PRIVATE->fd, SPI_IOC_WR_MODE, &spi_mode);
if (ret == -1) return false; if (ret == -1) return false;
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bits); ret = ioctl(PRIVATE->fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bits);
if (ret == -1) return false; if (ret == -1) return false;
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed); ret = ioctl(PRIVATE->fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed);
if (ret == -1) return false; if (ret == -1) return false;
piCout << "SPI open" << device << "speed:" << spi_speed/1000 << "KHz" << "mode" << spi_mode << "bits" << spi_bits; piCout << "SPI open" << path() << "speed:" << spi_speed/1000 << "KHz" << "mode" << spi_mode << "bits" << spi_bits;
spi_ioc_tr.delay_usecs = 0; PRIVATE->spi_ioc_tr.delay_usecs = 0;
spi_ioc_tr.speed_hz = spi_speed; PRIVATE->spi_ioc_tr.speed_hz = spi_speed;
spi_ioc_tr.bits_per_word = spi_bits; PRIVATE->spi_ioc_tr.bits_per_word = spi_bits;
return true; return true;
#else #else
piCoutObj << "PISPI not implemented on windows"; piCoutObj << "PISPI not implemented on windows";
@@ -58,7 +58,7 @@ bool PISPI::openDevice() {
bool PISPI::closeDevice() { bool PISPI::closeDevice() {
#ifndef WINDOWS #ifndef WINDOWS
if (fd) close(fd); if (PRIVATE->fd) close(PRIVATE->fd);
#endif #endif
return true; return true;
} }
@@ -78,13 +78,13 @@ int PISPI::writeDevice(const void * data, int max_size) {
if (tx_buf.size_s() != max_size) { if (tx_buf.size_s() != max_size) {
tx_buf.resize(max_size); tx_buf.resize(max_size);
rx_buf.resize(max_size); rx_buf.resize(max_size);
spi_ioc_tr.tx_buf = (ulong)(tx_buf.data()); PRIVATE->spi_ioc_tr.tx_buf = (ulong)(tx_buf.data());
spi_ioc_tr.rx_buf = (ulong)(rx_buf.data()); PRIVATE->spi_ioc_tr.rx_buf = (ulong)(rx_buf.data());
spi_ioc_tr.len = max_size; PRIVATE->spi_ioc_tr.len = max_size;
} }
memcpy(tx_buf.data(), data, max_size); memcpy(tx_buf.data(), data, max_size);
int ret; int ret;
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &spi_ioc_tr); ret = ioctl(PRIVATE->fd, SPI_IOC_MESSAGE(1), &PRIVATE->spi_ioc_tr);
if (ret < 1) {piCout << "init can't send spi message" << ret; return -1;} if (ret < 1) {piCout << "init can't send spi message" << ret; return -1;}
recv_buf.append(rx_buf); recv_buf.append(rx_buf);
return max_size; return max_size;