-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPI.cpp
233 lines (193 loc) · 4.95 KB
/
SPI.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
* Copyright (c) 2013 Intel Corporation
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/spi/spidev.h>
#include <Stdint.h>
#include "Arduino.h"
#include "trace.h"
#include "SPI.h"
#define MY_TRACE_PREFIX "SPI"
/* For Arduino, this is assumed to be 4MHz, using SPI_CLOCK_DEV4
* Sticking to this for backward compatibility */
#define SPI_CLK_DEFAULT_HZ 4000000
#define SPI_SS_GPIO_PIN 10
SPIClass SPI;
int SPIClass::fd = 0;
/* Constructor - establish defaults */
SPIClass::SPIClass()
{
/* reflect Arduino's default behaviour where possible */
this->mode = SPI_MODE0;
this->bitOrder = MSBFIRST;
this->clkDiv = SPI_CLOCK_DIV4;
}
void SPIClass::begin()
{
/* Set the pin mux, for the SCK, MOSI and MISO pins ONLY
*
* Leave the SS pin in GPIO mode (the application will control it)
* but set it's direction to output and initially high
*/
system("echo on > /sys/devices/pci0000:00/0000:00:07.1/power/control"); //disables SPI power management
pinMode(SPI_SS_GPIO_PIN, OUTPUT);
digitalWrite(SPI_SS_GPIO_PIN, HIGH);
muxSelectSpi(1);
if(fd <= 0)
{
this->fd = open(LINUX_SPIDEV, O_RDWR);
if (this->fd < 0) {
trace_error("Failed to open SPI device\n");
return;
}
}
/* Load default/last configuration */
this->setClockDivider(this->clkDiv);
this->setBitOrder(this->bitOrder);
this->setDataMode(this->mode);
}
void SPIClass::end()
{
if (this->fd >= 0)
close(this->fd);
this->fd = 0;
}
void SPIClass::setBitOrder(uint8_t bitOrder)
{
uint8_t lsbFirst = 0;
if (bitOrder == LSBFIRST) {
lsbFirst = 1;
}
if (ioctl (this->fd, SPI_IOC_WR_LSB_FIRST, &lsbFirst) < 0) {
trace_error("Failed to set SPI bit justification\n");
return;
}
this->bitOrder = bitOrder;
}
void SPIClass::setDataMode(uint8_t mode)
{
uint8_t linuxSpiMode = 0;
switch(mode) {
case SPI_MODE0:
linuxSpiMode = SPI_MODE_0;
break;
case SPI_MODE1:
linuxSpiMode = SPI_MODE_1;
break;
case SPI_MODE2:
linuxSpiMode = SPI_MODE_2;
break;
case SPI_MODE3:
linuxSpiMode = SPI_MODE_3;
break;
default:
trace_error("Invalid SPI mode specified\n");
return;
}
if (ioctl (this->fd, SPI_IOC_WR_MODE, &linuxSpiMode) < 0) {
trace_error("Failed to set SPI mode\n");
return;
}
this->mode = mode;
}
void SPIClass::setClockDivider(uint8_t clkDiv)
{
uint32_t maxSpeedHz = SPI_CLK_DEFAULT_HZ;
/* Adjust the clock speed relative to the default divider of 4 */
switch(clkDiv)
{
case SPI_CLOCK_DIV1:
maxSpeedHz = SPI_CLK_DEFAULT_HZ << 2; //25 MHz
break;
case SPI_CLOCK_DIV2:
maxSpeedHz = SPI_CLK_DEFAULT_HZ << 1; //8.4 MHz
break;
case SPI_CLOCK_DIV4:
/* Do nothing */ //4.17 MHz
break;
case SPI_CLOCK_DIV8:
maxSpeedHz = SPI_CLK_DEFAULT_HZ >> 1; //2.08 MHz
break;
case SPI_CLOCK_DIV16:
maxSpeedHz = SPI_CLK_DEFAULT_HZ >> 2; //1 MHz
break;
case SPI_CLOCK_DIV32:
maxSpeedHz = SPI_CLK_DEFAULT_HZ >> 3; //500 KHz
break;
case SPI_CLOCK_DIV64:
maxSpeedHz = SPI_CLK_DEFAULT_HZ >> 4; //250 KHz
break;
case SPI_CLOCK_DIV128:
maxSpeedHz = SPI_CLK_DEFAULT_HZ >> 5; //125 KHz
break;
default:
trace_error("Invalid SPI mode specified\n");
return;
}
if (ioctl (this->fd, SPI_IOC_WR_MAX_SPEED_HZ, &maxSpeedHz) < 0) {
trace_error("Failed to set SPI clock speed\n");
return;
}
this->clkDiv = clkDiv;
}
void SPIClass::setClockSpeed(uint32_t clkSpeed)
{
if(clkSpeed > 25000000)
{
clkSpeed = 25000000;
}
uint32_t maxSpeedHz = clkSpeed;
if (ioctl (this->fd, SPI_IOC_WR_MAX_SPEED_HZ, &maxSpeedHz) < 0) {
trace_error("Failed to set SPI clock speed\n");
return;
}
this->clkDiv = 0x0;
}
uint8_t SPIClass::transfer(uint8_t txData)
{
uint8_t rxData = 0xFF;
struct spi_ioc_transfer msg;
memset(&msg, 0, sizeof(msg));
msg.tx_buf = (__u64) &txData;
msg.rx_buf = (__u64) &rxData;
msg.len = sizeof(uint8_t);
if (ioctl (this->fd, SPI_IOC_MESSAGE(1), &msg) < 0) {
trace_error("Failed to execute SPI transfer\n");
}
return rxData;
}
int16_t SPIClass::transfer16(int16_t _txdata){
int rxHighbyte = 0xFF;
int rxLowbyte = 0xFF;
rxLowbyte = SPI.transfer(lowByte(_txdata));
rxHighbyte = SPI.transfer(highByte(_txdata));
int16_t output = rxHighbyte * 256 + rxLowbyte;
return output;
}
void SPIClass::transferBuffer(const uint8_t *txData,
uint8_t *rxData,
uint32_t len)
{
struct spi_ioc_transfer msg;
memset(&msg, 0, sizeof(msg));
msg.tx_buf = (__u64) txData;
msg.rx_buf = (__u64) rxData;
msg.len = len;
if (ioctl (this->fd, SPI_IOC_MESSAGE(1), &msg) < 0)
trace_error("Failed to execute SPI transfer\n");
}
void SPIClass::attachInterrupt() {
trace_error("SPI slave mode is not currently supported\n");
}
void SPIClass::detachInterrupt() {
/* Do nothing */
}