Project

General

Profile

spidevice.h

Jonathan Cormier, 02/28/2019 04:44 PM

Download (1.79 KB)

 
1
/**
2
 * \file spidevice.h
3
 *
4
 * \brief Wrapper class to communicate to device via spidev
5
 *
6
 * Websites:
7
 * Critical Link - http://www.criticallink.com
8
 *
9
 * \copyright Critical Link LLC 2015
10
 */
11

    
12
#ifndef SPIDEVICE_H_
13
#define SPIDEVICE_H_
14

    
15
#include <fcntl.h>
16
#include <unistd.h>
17
#include <sys/ioctl.h>
18
#include <linux/types.h>
19
#include <linux/spi/spidev.h>
20

    
21
#include <string>
22

    
23
struct tsSPIConfiguration
24
{
25
        int mnMode;        // SPI transfer mode including polarity and clock phase.
26
        unsigned char mnLsbType;      // LSB first/last.
27
        unsigned char mnBits;                        // The number of bits per word.
28
        long mnSpeed;                // The speed in Hz.
29
};
30

    
31
class tcSPIDevice
32
{
33
public:
34
        /**
35
         * Creates a spi device on the given bus and chip select.
36
         */
37
        tcSPIDevice(std::string asDevice);
38
        tcSPIDevice(const char* asDevice);
39
        virtual ~tcSPIDevice();
40

    
41
        /**
42
         * Open the device if it is not already open.
43
         */
44
        void openDevice();
45

    
46
        /**
47
         * Close the SPI device.
48
         */
49
        void closeDevice();
50

    
51
        bool isOpen();
52

    
53
        tsSPIConfiguration getConfiguration();
54

    
55
        bool setConfiguration(struct tsSPIConfiguration &arConfiguration);
56

    
57
        /**
58
         * Write the data out.
59
         *
60
         * @param apTxData - the data to send.
61
         * @param anLen - the number of bytes in apTxData
62
         */
63
        void write(char *apTxData, long anLen);
64

    
65
        /**
66
         * Read data.
67
         *
68
         * @param apRxData - the location to store data to
69
         * @param anLen - the number of bytes in apRxData
70
         */
71
        void read(char *apRxData, long anLen);
72

    
73
        /**
74
         * Perform a full duplex operation; Tx/Rx of length anLen.
75
         *
76
         * Note: The arrays must be of the same length.
77
         *
78
         * @param apRxData - the location to store received data in.
79
         * @param apTxData - the data to send
80
         * @param anLen the length of the arrays.
81
         */
82
        void duplex(char *apTxData, char *apRxData, long anLen);
83

    
84
private:
85
        std::string msDevice;
86
        int mnFileDescriptor;
87
};
88

    
89
#endif /* SPIDEVICE_H_ */
Add picture from clipboard (Maximum size: 600 MB)