source: flair-src/trunk/lib/FlairCore/src/Unix_SpiPort.cpp@ 419

Last change on this file since 419 was 419, checked in by Sanahuja Guillaume, 9 months ago

spi changes

File size: 2.3 KB
Line 
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
24using std::string;
25
26namespace flair {
27namespace core {
28
29Unix_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
37 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0) {
38 Err("can't set bus mode\n");
39 }
40
41 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) < 0) {
42 Err("can't set bits per word\n");
43 }
44
45 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
46 Err("can't set max speed [Hz]\n");
47 }
48}
49
50Unix_SpiPort::~Unix_SpiPort() {
51 close(fd);
52}
53
54ssize_t Unix_SpiPort::Write(const void *buf, size_t nbyte) {
55 int retv;
56 struct spi_ioc_transfer xfer;
57 memset(&xfer, 0, sizeof(xfer));
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
71ssize_t Unix_SpiPort::Read(void *buf, size_t nbyte) {
72 int retv;
73 struct spi_ioc_transfer xfer;
74 memset(&xfer, 0, sizeof(xfer));
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
88ssize_t Unix_SpiPort::WriteRead(const void *tx_buf, void *rx_buf,size_t nbyte) {
89 int retv;
90 struct spi_ioc_transfer xfer;
91 memset(&xfer, 0, sizeof(xfer));
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
Note: See TracBrowser for help on using the repository browser.