-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspi-test.c
177 lines (141 loc) · 4.19 KB
/
spi-test.c
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
/*
* NXCTRL BeagleBone Black Control Library
*
* SPI EEPROM(Microchip 25LC512) Test Program
*
* Copyright (C) 2014 Sungjin Chun <chunsj@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <NXCTRL.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#define SPI_CS0 17
#define SPI_D1 18
#define SPI_D0 21
#define SPI_CLK 22
const uint8_t INSTREAD = 0b00000011;
const uint8_t INSTWRITE = 0b00000010;
const uint8_t INSTWREN = 0b00000110;
const uint8_t INSTWRDI = 0b00000100;
const uint8_t INSTRDSR = 0b00000101;
static int
__SPI_read (int nFD, uint16_t nAddr, uint8_t *pnD) {
int nStatus;
struct spi_ioc_transfer xfer[2];
char rchTXData[4];
char rchRXData[4];
memset(xfer, 0, sizeof(xfer));
memcpy(rchTXData, &INSTREAD, sizeof(uint8_t));
memcpy(rchTXData + 1, &nAddr, sizeof(uint16_t));
memcpy(rchTXData + 1 + 2, pnD, sizeof(uint8_t));
xfer[0].tx_buf = (unsigned int)rchTXData;
xfer[0].rx_buf = (unsigned int)rchRXData;
xfer[0].len = 4;
xfer[0].delay_usecs = 200;
nStatus = ioctl(nFD, SPI_IOC_MESSAGE(1), xfer);
if (nStatus < 0) {
perror("SPI_IOC_MESSAGE");
return -1;
}
*pnD = rchRXData[3];
return 0;
}
static int
__SPI_write (int nFD, uint16_t nAddr, uint8_t nD) {
int nStatus;
struct spi_ioc_transfer xfer[2];
char rchTXData[4];
char rchRXData[4];
memset(xfer, 0, sizeof(xfer));
memcpy(rchTXData, &INSTWREN, sizeof(uint8_t));
xfer[0].tx_buf = (unsigned int)rchTXData;
xfer[0].rx_buf = (unsigned int)rchRXData;
xfer[0].len = 1;
xfer[0].delay_usecs = 200;
nStatus = ioctl(nFD, SPI_IOC_MESSAGE(1), xfer);
if (nStatus < 0) {
perror("SPI_IOC_MESSAGE");
return -1;
}
memset(xfer, 0, sizeof(xfer));
memcpy(rchTXData, &INSTWRITE, sizeof(uint8_t));
memcpy(rchTXData + 1, &nAddr, sizeof(uint16_t));
memcpy(rchTXData + 1 + 2, &nD, sizeof(uint8_t));
xfer[0].tx_buf = (unsigned int)rchTXData;
xfer[0].rx_buf = (unsigned int)rchRXData;
xfer[0].len = 4;
xfer[0].delay_usecs = 200;
nStatus = ioctl(nFD, SPI_IOC_MESSAGE(1), xfer);
if (nStatus < 0) {
perror("SPI_IOC_MESSAGE");
return -1;
}
memset(xfer, 0, sizeof(xfer));
memcpy(rchTXData, &INSTWRDI, sizeof(uint8_t));
xfer[0].tx_buf = (unsigned int)rchTXData;
xfer[0].rx_buf = (unsigned int)rchRXData;
xfer[0].len = 1;
xfer[0].delay_usecs = 200;
nStatus = ioctl(nFD, SPI_IOC_MESSAGE(1), xfer);
if (nStatus < 0) {
perror("SPI_IOC_MESSAGE");
return -1;
}
return 0;
}
NXCTRL_VOID
NXCTRLSetup (NXCTRL_VOID) {
int nFD, i;
uint8_t nLSB, nD;
uint32_t nSpeed, nSPIMode;
uint16_t nAddr;
NXCTRLPinMux(NXCTRL_P9, SPI_CS0, NXCTRL_MODE0, NXCTRL_PULLUP, NXCTRL_LOW);
NXCTRLPinMux(NXCTRL_P9, SPI_D1, NXCTRL_MODE0, NXCTRL_PULLUP, NXCTRL_LOW);
NXCTRLPinMux(NXCTRL_P9, SPI_D0, NXCTRL_MODE0, NXCTRL_PULLUP, NXCTRL_HIGH);
NXCTRLPinMux(NXCTRL_P9, SPI_CLK, NXCTRL_MODE0, NXCTRL_PULLUP, NXCTRL_HIGH);
nFD = open("/dev/spidev1.0", O_RDWR);
nLSB = 0;
ioctl(nFD, SPI_IOC_WR_LSB_FIRST, &nLSB);
nSpeed = 500000;
ioctl(nFD, SPI_IOC_WR_MAX_SPEED_HZ, &nSpeed);
nSPIMode = SPI_MODE_0;
ioctl(nFD, SPI_IOC_WR_MODE, &nSPIMode);
nAddr = 305;
nD = -1;
for (i = 0; i < 10; i++) {
__SPI_read(nFD, nAddr, &nD);
printf("%d: %d\n", i, nD);
nD = (uint8_t)i;
__SPI_write(nFD, nAddr, nD);
NXCTRLSleep(5, 0);
}
close(nFD);
}
NXCTRL_VOID
NXCTRLLoop (NXCTRL_VOID) {
NXCTRLExitLoop();
}
int
main (void) {
return NXCTRLMain();
}