From ceb7bcab70d3bde83afa9af22d01a13b7d4a328a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=8B=D1=87=D0=BA=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9?= Date: Wed, 6 Dec 2017 13:57:10 +0000 Subject: [PATCH] spi git-svn-id: svn://db.shs.com.ru/pip@566 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- main.cpp | 33 +++++++-------- src_main/io/pispi.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++ src_main/io/pispi.h | 31 ++++++++++++++ src_main/piversion.h | 2 +- 4 files changed, 140 insertions(+), 20 deletions(-) create mode 100644 src_main/io/pispi.cpp create mode 100644 src_main/io/pispi.h diff --git a/main.cpp b/main.cpp index 0d006b7f..3c493785 100644 --- a/main.cpp +++ b/main.cpp @@ -1,26 +1,21 @@ #include "pip.h" -#include "picompress.h" +#include "pispi.h" int main(int argc, char *argv[]) { - PIString s = PIString::fromUTF8("hello, привет привет привет привет привет привет"); - PIByteArray ba = s.toUTF8(); - piCout << "original " << ba.toHex(); - ba = piCompress(ba); - piCout << "compress " << ba.toHex(); - ba = piDecompress(ba); - piCout << "decompress" << ba.toHex(); - piCout << PIString::fromUTF8(ba); - ba = PIByteArray(160); - piCout << ba.toHex() << ba.size(); - piCout << piCompress(ba).toHex() << piCompress(ba).size(); - /*PIString s = PIString::fromUTF8("hello, привет"); - piCout << s; - PIByteArray ba = s.toUTF8(); - piCout << ba.toHex(); - PIString s2; - s2 = PIString::fromUTF8(ba); - piCout << s2;*/ + PICLI cli(argc, argv); + cli.setOptionalArgumentsCount(1); + cli.addArgument("dev", true); + cli.addArgument("speed", true); + if (!cli.hasArgument("dev")) { + return 0; + } + PIString path = cli.argumentValue("dev"); + PISPI spi(path); + if (cli.hasArgument("speed")) spi.setSpeed(cli.argumentValue("speed").toInt()); + piCout << "open" << spi.open(); + piCout << "write" << spi.write(PIByteArray::fromHex("0000000000000000000000000000000000000000000000000000000000000000")); + piCout << "read" << spi.readForTime(10).toHex(); return 0; } diff --git a/src_main/io/pispi.cpp b/src_main/io/pispi.cpp new file mode 100644 index 00000000..daffedf4 --- /dev/null +++ b/src_main/io/pispi.cpp @@ -0,0 +1,94 @@ +#include "pispi.h" +#ifndef WINDOWS +# include +# include +# include +#endif + + +PRIVATE_DEFINITION_START(PISPI) +#ifndef WINDOWS +int fd; +spi_ioc_transfer spi_ioc_tr; +#endif +PRIVATE_DEFINITION_END(PISPI) + + +REGISTER_DEVICE(PISPI) + + +PISPI::PISPI(const PIString & path, uint speed, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) { + setPath(path); + setSpeed(speed); + spi_mode = 0; + spi_bits = 8; +#ifndef WINDOWS + fd = 0; +#endif +} + + +void PISPI::setSpeed(uint speed_hz) { + spi_speed = speed_hz; +} + + +bool PISPI::openDevice() { +#ifndef WINDOWS + int ret = 0; + fd = open(path().dataAscii(), O_RDWR); + if (fd < 0) return false; + ret = ioctl(fd, SPI_IOC_WR_MODE, &spi_mode); + if (ret == -1) return false; + ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bits); + if (ret == -1) return false; + ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed); + if (ret == -1) return false; + piCout << "SPI open" << device << "speed:" << spi_speed/1000 << "KHz" << "mode" << spi_mode << "bits" << spi_bits; + spi_ioc_tr.delay_usecs = 0; + spi_ioc_tr.speed_hz = spi_speed; + spi_ioc_tr.bits_per_word = spi_bits; + return true; +#else + piCoutObj << "PISPI not implemented on windows"; + return false; +#endif +} + + +bool PISPI::closeDevice() { +#ifndef WINDOWS + if (fd) close(fd); +#endif + return true; +} + + +int PISPI::readDevice(void * read_to, int max_size) { + int sz = piMini(recv_buf.size_s(), max_size); + memcpy(read_to, recv_buf.data(), sz); + recv_buf.resize(recv_buf.size_s() - sz); + return sz; +} + + +int PISPI::writeDevice(const void * data, int max_size) { +#ifndef WINDOWS + if (max_size > 0) { + if (tx_buf.size_s() != max_size) { + tx_buf.resize(max_size); + rx_buf.resize(max_size); + spi_ioc_tr.tx_buf = (ulong)(tx_buf.data()); + spi_ioc_tr.rx_buf = (ulong)(rx_buf.data()); + spi_ioc_tr.len = max_size; + } + memcpy(tx_buf.data(), data, max_size); + int ret; + ret = ioctl(fd, SPI_IOC_MESSAGE(1), &spi_ioc_tr); + if (ret < 1) {piCout << "init can't send spi message" << ret; return -1;} + recv_buf.append(rx_buf); + return max_size; + } +#endif + return 0; +} diff --git a/src_main/io/pispi.h b/src_main/io/pispi.h new file mode 100644 index 00000000..d6009921 --- /dev/null +++ b/src_main/io/pispi.h @@ -0,0 +1,31 @@ +#ifndef PISPI_H +#define PISPI_H + +#include "piiodevice.h" + + +class PIP_EXPORT PISPI: public PIIODevice +{ + PIIODEVICE(PISPI) +public: + explicit PISPI(const PIString & path = PIString(), uint speed_hz = 100000, PIIODevice::DeviceMode mode = PIIODevice::ReadWrite); + ~PISPI() {} + + void setSpeed(uint speed_hz); + +protected: + bool openDevice(); + bool closeDevice(); + int readDevice(void * read_to, int max_size); + int writeDevice(const void * data, int max_size); + +private: + uint spi_speed; + uchar spi_mode; + uchar spi_bits; + PIByteArray tx_buf, rx_buf; + PIByteArray recv_buf; + PRIVATE_DECLARATION +}; + +#endif // PISPI_H diff --git a/src_main/piversion.h b/src_main/piversion.h index 86cb7985..c29432fe 100644 --- a/src_main/piversion.h +++ b/src_main/piversion.h @@ -3,7 +3,7 @@ #define PIVERSION_H #define PIP_VERSION_MAJOR 1 -#define PIP_VERSION_MINOR 4 +#define PIP_VERSION_MINOR 5 #define PIP_VERSION_REVISION 0 #define PIP_VERSION_SUFFIX ""