code format
This commit is contained in:
@@ -1,39 +1,40 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
SPI
|
||||
Andrey Bychkov work.a.b@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
SPI
|
||||
Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "pispi.h"
|
||||
#include "pipropertystorage.h"
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "pipropertystorage.h"
|
||||
#ifdef LINUX
|
||||
# define PIP_SPI
|
||||
#endif
|
||||
#ifdef PIP_SPI
|
||||
# include <fcntl.h>
|
||||
# include <sys/ioctl.h>
|
||||
# include <linux/spi/spidev.h>
|
||||
# include <fcntl.h>
|
||||
# include <linux/spi/spidev.h>
|
||||
# include <sys/ioctl.h>
|
||||
#endif
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PISPI)
|
||||
#ifdef PIP_SPI
|
||||
int fd;
|
||||
spi_ioc_transfer spi_ioc_tr;
|
||||
int fd;
|
||||
spi_ioc_transfer spi_ioc_tr;
|
||||
#endif
|
||||
PRIVATE_DEFINITION_END(PISPI)
|
||||
|
||||
@@ -41,7 +42,7 @@ PRIVATE_DEFINITION_END(PISPI)
|
||||
REGISTER_DEVICE(PISPI)
|
||||
|
||||
|
||||
PISPI::PISPI(const PIString & path, uint speed, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) {
|
||||
PISPI::PISPI(const PIString & path, uint speed, PIIODevice::DeviceMode mode): PIIODevice(path, mode) {
|
||||
#ifdef MICRO_PIP
|
||||
setThreadedReadBufferSize(512);
|
||||
#else
|
||||
@@ -51,8 +52,7 @@ PISPI::PISPI(const PIString & path, uint speed, PIIODevice::DeviceMode mode) : P
|
||||
setSpeed(speed);
|
||||
setBits(8);
|
||||
spi_mode = 0;
|
||||
if (mode == ReadOnly)
|
||||
piCoutObj << "error, SPI can't work in ReadOnly mode";
|
||||
if (mode == ReadOnly) piCoutObj << "error, SPI can't work in ReadOnly mode";
|
||||
#ifdef PIP_SPI
|
||||
PRIVATE->fd = 0;
|
||||
#endif
|
||||
@@ -95,22 +95,35 @@ ssize_t PISPI::bytesAvailable() const {
|
||||
|
||||
bool PISPI::openDevice() {
|
||||
#ifdef PIP_SPI
|
||||
int ret = 0;
|
||||
//piCoutObj << "open device" << path();
|
||||
int ret = 0;
|
||||
// piCoutObj << "open device" << path();
|
||||
PRIVATE->fd = ::open(path().dataAscii(), O_RDWR);
|
||||
if (PRIVATE->fd < 0) {piCoutObj << "can't open device";return false;}
|
||||
//piCoutObj << "set mode" << spi_mode;
|
||||
if (PRIVATE->fd < 0) {
|
||||
piCoutObj << "can't open device";
|
||||
return false;
|
||||
}
|
||||
// piCoutObj << "set mode" << spi_mode;
|
||||
ret = ioctl(PRIVATE->fd, SPI_IOC_WR_MODE, &spi_mode);
|
||||
if (ret == -1) {piCoutObj << "can't set spi write mode";return false;}
|
||||
//piCoutObj << "set bits" << spi_bits;
|
||||
if (ret == -1) {
|
||||
piCoutObj << "can't set spi write mode";
|
||||
return false;
|
||||
}
|
||||
// piCoutObj << "set bits" << spi_bits;
|
||||
ret = ioctl(PRIVATE->fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bits);
|
||||
if (ret == -1) {piCoutObj << "can't set bits per word";return false;}
|
||||
//piCoutObj << "set speed" << spi_speed;
|
||||
if (ret == -1) {
|
||||
piCoutObj << "can't set bits per word";
|
||||
return false;
|
||||
}
|
||||
// piCoutObj << "set speed" << spi_speed;
|
||||
ret = ioctl(PRIVATE->fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed);
|
||||
if (ret == -1) {piCoutObj << "can't set max write speed hz";return false;}
|
||||
piCoutObj << "SPI open" << path() << "speed:" << spi_speed/1000 << "KHz" << "mode" << spi_mode << "bits" << spi_bits;
|
||||
PRIVATE->spi_ioc_tr.delay_usecs = 0;
|
||||
PRIVATE->spi_ioc_tr.speed_hz = 0;
|
||||
if (ret == -1) {
|
||||
piCoutObj << "can't set max write speed hz";
|
||||
return false;
|
||||
}
|
||||
piCoutObj << "SPI open" << path() << "speed:" << spi_speed / 1000 << "KHz"
|
||||
<< "mode" << spi_mode << "bits" << spi_bits;
|
||||
PRIVATE->spi_ioc_tr.delay_usecs = 0;
|
||||
PRIVATE->spi_ioc_tr.speed_hz = 0;
|
||||
PRIVATE->spi_ioc_tr.bits_per_word = spi_bits;
|
||||
return true;
|
||||
#else
|
||||
@@ -144,13 +157,16 @@ ssize_t PISPI::writeDevice(const void * data, ssize_t max_size) {
|
||||
rx_buf.resize(max_size);
|
||||
PRIVATE->spi_ioc_tr.tx_buf = (ulong)(tx_buf.data());
|
||||
PRIVATE->spi_ioc_tr.rx_buf = (ulong)(rx_buf.data());
|
||||
PRIVATE->spi_ioc_tr.len = max_size;
|
||||
PRIVATE->spi_ioc_tr.len = max_size;
|
||||
}
|
||||
memcpy(tx_buf.data(), data, max_size);
|
||||
int ret;
|
||||
//piCoutObj << "write" << max_size << tx_buf.size();
|
||||
// piCoutObj << "write" << max_size << tx_buf.size();
|
||||
ret = ioctl(PRIVATE->fd, SPI_IOC_MESSAGE(1), &PRIVATE->spi_ioc_tr);
|
||||
if (ret < 1) {piCoutObj << "can't send spi message" << ret; return -1;}
|
||||
if (ret < 1) {
|
||||
piCoutObj << "can't send spi message" << ret;
|
||||
return -1;
|
||||
}
|
||||
if (canRead()) recv_buf.append(rx_buf);
|
||||
if (recv_buf.size_s() > threadedReadBufferSize()) recv_buf.resize(threadedReadBufferSize());
|
||||
return max_size;
|
||||
@@ -162,7 +178,8 @@ ssize_t PISPI::writeDevice(const void * data, ssize_t max_size) {
|
||||
|
||||
PIString PISPI::constructFullPathDevice() const {
|
||||
PIString ret;
|
||||
ret += path() + ":" + PIString::fromNumber((int)speed()) + ":" + PIString::fromNumber((int)bits()) + ":" + PIString::fromNumber((int)parameters());
|
||||
ret += path() + ":" + PIString::fromNumber((int)speed()) + ":" + PIString::fromNumber((int)bits()) + ":" +
|
||||
PIString::fromNumber((int)parameters());
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user