-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPI.cpp
More file actions
167 lines (131 loc) · 4 KB
/
SPI.cpp
File metadata and controls
167 lines (131 loc) · 4 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
///-------------------------------------------------------------------------------------------------
/// @file POC\SPI.cpp.
///
/// @brief Implements the spi class
#include "SPI.h"
mutex SPI::spiMutex;
///-------------------------------------------------------------------------------------------------
/// @fn SPI::SPI(bool cpha, bool cpol, long fmax)
///
/// @brief Constructor
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param cpha True to cpha.
/// @param cpol True to cpol.
/// @param fmax The fmax.
SPI::SPI(bool cpha, bool cpol, long fmax) : cpha(cpha), cpol(cpol), fmax(fmax) {
}
///-------------------------------------------------------------------------------------------------
/// @fn void SPI::transfere(char* buffer, int n)
///
/// @brief Transferes
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param [in,out] buffer If non-null, the buffer.
/// @param n An int to process.
void SPI::transfere(unsigned char* buffer, int n) {
spiMutex.lock();
int file;
unsigned char mode = 0;
if (cpha) mode |= SPI_CPHA;
if (cpol) mode |= SPI_CPOL;
file = open("/dev/spidev0.0", O_RDWR);
if (file < 0) {
cout << "could not open SPI file" << endl;
}
if (ioctl(file, SPI_IOC_WR_MODE, &mode) < 0) {
cout << "can't set spi mode" << endl;
}
if (ioctl(file, SPI_IOC_RD_MODE, &mode) < 0) {
cout << "SPI rd_mode" << endl;
}
if (ioctl(file, SPI_IOC_WR_MAX_SPEED_HZ, &fmax) < 0) {
cout << "can't set max speed hz" << endl;
}
if (ioctl(file, SPI_IOC_RD_MAX_SPEED_HZ, &fmax) < 0) {
cout << "SPI max_speed_hz" << endl;
}
if (write(file, buffer, n) != n) {
cout << "SPI send failed" << endl;
}
if (read(file, buffer, n) != n) {
cout << "SPI receive failed" << endl;
}
close(file);
spiMutex.unlock();
}
///-------------------------------------------------------------------------------------------------
/// @fn unsigned char SPI::readRegister(unsigned char reg)
///
/// @brief Reads a register
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param reg The register.
///
/// @returns The register.
unsigned char SPI::readRegister(unsigned char reg) {
unsigned char readBuffer[1]{ reg };
transfere(readBuffer, 1);
return readBuffer[0];
}
///-------------------------------------------------------------------------------------------------
/// @fn void SPI::readRegister(unsigned char reg, unsigned char readBuffer[], unsigned int n)
///
/// @brief Reads a register
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param reg The register.
/// @param readBuffer Buffer for read data.
/// @param n An int to process.
void SPI::readRegister(unsigned char reg, unsigned char readBuffer[], unsigned int n) {
unsigned char readBufferTemp[n];
readBufferTemp[0] = reg;
for (unsigned int i = 1; i < n; i++) {
readBufferTemp[i] = 0;
}
transfere(readBufferTemp, n);
for (unsigned int i = 1; i < n; i++) {
readBuffer[i - 1] = readBufferTemp[i];
}
}
///-------------------------------------------------------------------------------------------------
/// @fn void SPI::writeRegister(unsigned char reg, unsigned char data)
///
/// @brief Writes a register
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param reg The register.
/// @param data The data.
void SPI::writeRegister(unsigned char reg, unsigned char data) {
unsigned char writeBuffer[2]{ reg, data };
transfere(writeBuffer, 2);
}
///-------------------------------------------------------------------------------------------------
/// @fn void SPI::writeRegister(unsigned char reg, unsigned char writeBuffer[], unsigned int n)
///
/// @brief Writes a register
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param reg The register.
/// @param writeBuffer Buffer for write data.
/// @param n An int to process.
void SPI::writeRegister(unsigned char reg, unsigned char writeBuffer[], unsigned int n) {
unsigned char writeBufferTemp[n + 1];
writeBufferTemp[0] = reg;
for (unsigned int i = 1; i < n + 1; i++) {
writeBufferTemp[n] = writeBuffer[n - 1];
}
transfere(writeBufferTemp, n + 1);
}