-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmcp3008Spi.cpp
142 lines (117 loc) · 4.25 KB
/
mcp3008Spi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "mcp3008Spi.h"
using namespace std;
/**********************************************************
* spiOpen() :function is called by the constructor.
* It is responsible for opening the spidev device
* "devspi" and then setting up the spidev interface.
* private member variables are used to configure spidev.
* They must be set appropriately by constructor before calling
* this function.
* *********************************************************/
int mcp3008Spi::spiOpen(std::string devspi){
int statusVal = -1;
this->spifd = open(devspi.c_str(), O_RDWR);
if(this->spifd < 0){
perror("could not open SPI device");
exit(1);
}
statusVal = ioctl (this->spifd, SPI_IOC_WR_MODE, &(this->mode));
if(statusVal < 0){
perror("Could not set SPIMode (WR)...ioctl fail");
exit(1);
}
statusVal = ioctl (this->spifd, SPI_IOC_RD_MODE, &(this->mode));
if(statusVal < 0) {
perror("Could not set SPIMode (RD)...ioctl fail");
exit(1);
}
statusVal = ioctl (this->spifd, SPI_IOC_WR_BITS_PER_WORD, &(this->bitsPerWord));
if(statusVal < 0) {
perror("Could not set SPI bitsPerWord (WR)...ioctl fail");
exit(1);
}
statusVal = ioctl (this->spifd, SPI_IOC_RD_BITS_PER_WORD, &(this->bitsPerWord));
if(statusVal < 0) {
perror("Could not set SPI bitsPerWord(RD)...ioctl fail");
exit(1);
}
statusVal = ioctl (this->spifd, SPI_IOC_WR_MAX_SPEED_HZ, &(this->speed));
if(statusVal < 0) {
perror("Could not set SPI speed (WR)...ioctl fail");
exit(1);
}
statusVal = ioctl (this->spifd, SPI_IOC_RD_MAX_SPEED_HZ, &(this->speed));
if(statusVal < 0) {
perror("Could not set SPI speed (RD)...ioctl fail");
exit(1);
}
return statusVal;
}
/***********************************************************
* spiClose(): Responsible for closing the spidev interface.
* Called in destructor
* *********************************************************/
int mcp3008Spi::spiClose(){
int statusVal = -1;
statusVal = close(this->spifd);
if(statusVal < 0) {
perror("Could not close SPI device");
exit(1);
}
return statusVal;
}
/********************************************************************
* This function writes data "data" of length "length" to the spidev
* device. Data shifted in from the spidev device is saved back into
* "data".
* ******************************************************************/
int mcp3008Spi::spiWriteRead( unsigned char *data, int length){
struct spi_ioc_transfer spi[length];
int i = 0;
int retVal = -1;
bzero(spi, sizeof spi); // ioctl struct must be zeroed
// one spi transfer for each byte
for (i = 0 ; i < length ; i++){
spi[i].tx_buf = (unsigned long)(data + i); // transmit from "data"
spi[i].rx_buf = (unsigned long)(data + i) ; // receive into "data"
spi[i].len = sizeof(*(data + i)) ;
spi[i].delay_usecs = 0 ;
spi[i].speed_hz = this->speed ;
spi[i].bits_per_word = this->bitsPerWord ;
spi[i].cs_change = 0;
}
retVal = ioctl (this->spifd, SPI_IOC_MESSAGE(length), &spi) ;
if(retVal < 0){
perror("Problem transmitting spi data..ioctl");
exit(1);
}
return retVal;
}
/*************************************************
* Default constructor. Set member variables to
* default values and then call spiOpen()
* ***********************************************/
mcp3008Spi::mcp3008Spi(){
this->mode = SPI_MODE_0 ;
this->bitsPerWord = 8;
this->speed = 1000000;
this->spifd = -1;
this->spiOpen(std::string("/dev/spidev0.0"));
}
/*************************************************
* overloaded constructor. let user set member variables to
* and then call spiOpen()
* ***********************************************/
mcp3008Spi::mcp3008Spi(std::string devspi, unsigned char spiMode, unsigned int spiSpeed, unsigned char spibitsPerWord){
this->mode = spiMode ;
this->bitsPerWord = spibitsPerWord;
this->speed = spiSpeed;
this->spifd = -1;
this->spiOpen(devspi);
}
/**********************************************
* Destructor: calls spiClose()
* ********************************************/
mcp3008Spi::~mcp3008Spi(){
this->spiClose();
}