[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 | fd = open(device.c_str(), O_RDWR);
|
---|
| 33 | if (fd < 0) {
|
---|
| 34 | Err("open_port: Unable to open %s\n", device.c_str());
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[419] | 37 | if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0) {
|
---|
| 38 | Err("can't set bus mode\n");
|
---|
[415] | 39 | }
|
---|
[419] | 40 |
|
---|
| 41 | if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0) {
|
---|
| 42 | Err("can't set bits per word\n");
|
---|
[415] | 43 | }
|
---|
| 44 |
|
---|
[419] | 45 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
|
---|
| 46 | Err("can't set max speed [Hz]\n");
|
---|
| 47 | }
|
---|
[415] | 48 | }
|
---|
| 49 |
|
---|
[419] | 50 | Unix_SpiPort::~Unix_SpiPort() {
|
---|
| 51 | close(fd);
|
---|
| 52 | }
|
---|
[415] | 53 |
|
---|
| 54 | ssize_t Unix_SpiPort::Write(const void *buf, size_t nbyte) {
|
---|
| 55 | int retv;
|
---|
[419] | 56 | struct spi_ioc_transfer xfer;
|
---|
| 57 | memset(&xfer, 0, sizeof(xfer));
|
---|
[415] | 58 |
|
---|
| 59 | xfer.tx_buf = (__u64) buf;
|
---|
| 60 | xfer.rx_buf = (__u64) 0;
|
---|
| 61 | xfer.len = (__u32) nbyte;
|
---|
| 62 |
|
---|
| 63 | retv = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
|
---|
| 64 | if (retv < 0) {
|
---|
| 65 | Err("ioctl SPI_IOC_MESSAGE\n");
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return retv;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | ssize_t Unix_SpiPort::Read(void *buf, size_t nbyte) {
|
---|
| 72 | int retv;
|
---|
[419] | 73 | struct spi_ioc_transfer xfer;
|
---|
| 74 | memset(&xfer, 0, sizeof(xfer));
|
---|
[415] | 75 |
|
---|
| 76 | xfer.tx_buf = (__u64) 0;
|
---|
| 77 | xfer.rx_buf = (__u64) buf;
|
---|
| 78 | xfer.len = (__u32) nbyte;
|
---|
| 79 |
|
---|
| 80 | retv = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
|
---|
| 81 | if (retv < 0) {
|
---|
| 82 | Err("ioctl SPI_IOC_MESSAGE\n");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | return retv;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | ssize_t Unix_SpiPort::WriteRead(const void *tx_buf, void *rx_buf,size_t nbyte) {
|
---|
| 89 | int retv;
|
---|
[419] | 90 | struct spi_ioc_transfer xfer;
|
---|
| 91 | memset(&xfer, 0, sizeof(xfer));
|
---|
[415] | 92 |
|
---|
| 93 | xfer.tx_buf = (__u64) tx_buf;
|
---|
| 94 | xfer.rx_buf = (__u64) rx_buf;
|
---|
| 95 | xfer.len = (__u32) nbyte;
|
---|
| 96 |
|
---|
| 97 | retv = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
|
---|
| 98 | if (retv < 0){
|
---|
| 99 | Err("ioctl SPI_IOC_MESSAGE\n");
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return retv;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | } // end namespace core
|
---|
| 106 | } // end namespace flair
|
---|