[415] | 1 | // %flair:license{
|
---|
| 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %flair:license}
|
---|
| 5 | // created: 2021/04/13
|
---|
| 6 | // filename: Unix_SpiPort.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: Class for unix spi port
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #include "Unix_SpiPort.h"
|
---|
| 19 | #include <fcntl.h> /* File control definitions */
|
---|
| 20 | #include <unistd.h>
|
---|
| 21 | #include <string.h>
|
---|
| 22 | #include <sys/ioctl.h>
|
---|
| 23 |
|
---|
| 24 | using std::string;
|
---|
| 25 |
|
---|
| 26 | namespace flair {
|
---|
| 27 | namespace core {
|
---|
| 28 |
|
---|
| 29 | Unix_SpiPort::Unix_SpiPort(const Object *parent, string name,
|
---|
| 30 | string device,int mode, int bits, int speed) : SpiPort(parent, name) {
|
---|
| 31 |
|
---|
| 32 | memset(&xfer, 0, sizeof(xfer));
|
---|
| 33 |
|
---|
| 34 | // open SPIdev
|
---|
| 35 | fd = open(device.c_str(), O_RDWR);
|
---|
| 36 | if (fd < 0) {
|
---|
| 37 | Err("open_port: Unable to open %s\n", device.c_str());
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | if (mode) {
|
---|
| 41 | if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0) {
|
---|
| 42 | Err("can't set bus mode\n");
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | /*
|
---|
| 46 | if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0) {
|
---|
| 47 | Err("can't get bus mode\n");
|
---|
| 48 | }
|
---|
| 49 | */
|
---|
| 50 | // get "LSB first"
|
---|
| 51 | uint8_t lsb;
|
---|
| 52 | if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0) {
|
---|
| 53 | Err("can't get lsb first\n");
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | if (bits) {
|
---|
| 57 | if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0) {
|
---|
| 58 | Err("can't set bits per word\n");
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | /*
|
---|
| 62 | // get bits per word
|
---|
| 63 | if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) {
|
---|
| 64 | Err("can't get bits per word\n");
|
---|
| 65 | }
|
---|
| 66 | */
|
---|
| 67 | // set max speed [Hz]
|
---|
| 68 | if (speed) {
|
---|
| 69 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
|
---|
| 70 | Err("can't set max speed [Hz]\n");
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | /*
|
---|
| 74 | // get max speed [Hz]
|
---|
| 75 | if (ioctl(self->fd, SPI_IOC_RD_MAX_SPEED_HZ, &self->speed) < 0)
|
---|
| 76 | {
|
---|
| 77 | SPI_DBG("error in spi_init(): can't get max speed [Hz]");
|
---|
| 78 | return SPI_ERR_SET_SPEED;
|
---|
| 79 | }
|
---|
| 80 | */
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | Unix_SpiPort::~Unix_SpiPort() { close(fd); }
|
---|
| 84 |
|
---|
| 85 | ssize_t Unix_SpiPort::Write(const void *buf, size_t nbyte) {
|
---|
| 86 | int retv;
|
---|
| 87 |
|
---|
| 88 | xfer.tx_buf = (__u64) buf;
|
---|
| 89 | xfer.rx_buf = (__u64) 0;
|
---|
| 90 | xfer.len = (__u32) nbyte;
|
---|
| 91 |
|
---|
| 92 | retv = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
|
---|
| 93 | if (retv < 0) {
|
---|
| 94 | Err("ioctl SPI_IOC_MESSAGE\n");
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return retv;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | ssize_t Unix_SpiPort::Read(void *buf, size_t nbyte) {
|
---|
| 101 | int retv;
|
---|
| 102 |
|
---|
| 103 | xfer.tx_buf = (__u64) 0;
|
---|
| 104 | xfer.rx_buf = (__u64) buf;
|
---|
| 105 | xfer.len = (__u32) nbyte;
|
---|
| 106 |
|
---|
| 107 | retv = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
|
---|
| 108 | if (retv < 0) {
|
---|
| 109 | Err("ioctl SPI_IOC_MESSAGE\n");
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | return retv;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | ssize_t Unix_SpiPort::WriteRead(const void *tx_buf, void *rx_buf,size_t nbyte) {
|
---|
| 116 | int retv;
|
---|
| 117 |
|
---|
| 118 | xfer.tx_buf = (__u64) tx_buf;
|
---|
| 119 | xfer.rx_buf = (__u64) rx_buf;
|
---|
| 120 | xfer.len = (__u32) nbyte;
|
---|
| 121 |
|
---|
| 122 | retv = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
|
---|
| 123 | if (retv < 0){
|
---|
| 124 | Err("ioctl SPI_IOC_MESSAGE\n");
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | return retv;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | } // end namespace core
|
---|
| 131 | } // end namespace flair
|
---|