-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcm_spi_io.c
274 lines (234 loc) · 5.39 KB
/
bcm_spi_io.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* bcm_spi.c
*
* Created on: 22.11.2015
* Author: alexs
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "glob_vars.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "bcm2835.h"
#include "crc8.h"
static int bcm_spi_clk=0;
void spi_set_clock(int clk) {
bcm_spi_clk=clk;
}
int spi_init() {
#ifdef POINT
point_add(spi_init_s);
#endif
if (!bcm2835_init()) return 1;
bcm2835_gpio_fsel(17, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_fsel(18, BCM2835_GPIO_FSEL_INPT);
/*
bcm2835_gpio_set_eds(17);
bcm2835_gpio_set_eds(18);
bcm2835_gpio_ren(17);
bcm2835_gpio_ren(18);
*/
bcm2835_gpio_fsel(9, BCM2835_GPIO_FSEL_ALT0); /* MISO */
bcm2835_gpio_fsel(10, BCM2835_GPIO_FSEL_ALT0); /* MOSI */
bcm2835_gpio_fsel(11, BCM2835_GPIO_FSEL_ALT0); /* CLK */
if (dev_pins.main.pin) {
bcm2835_gpio_fsel(dev_pins.main.pin,BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_set(dev_pins.main.pin);
}
if (dev_pins.powe.pin) {
bcm2835_gpio_fsel(dev_pins.powe.pin,BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_set(dev_pins.powe.pin);
}
if (dev_pins.rfid.pin) {
bcm2835_gpio_fsel(dev_pins.rfid.pin,BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_set(dev_pins.rfid.pin);
}
if (dev_pins.sens.pin) {
bcm2835_gpio_fsel(dev_pins.sens.pin,BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_set(dev_pins.sens.pin);
}
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); /*The default*/
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); /*The default*/
bcm2835_spi_setClockDivider(bcm_spi_clk); /*The default*/
bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE); /*The default*/
#ifdef POINT
point_add(spi_init_e1);
#endif
return 0;
}
void spi_release() {
#ifdef POINT
point_add(spi_release_s);
#endif
if (!bcm2835_init()) return;
bcm2835_gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_INPT); /* MISO */
bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_INPT); /* MOSI */
bcm2835_gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_INPT); /* CLK */
if (dev_pins.main.pin) {
bcm2835_gpio_fsel(dev_pins.main.pin,BCM2835_GPIO_FSEL_INPT);
}
if (dev_pins.powe.pin) {
bcm2835_gpio_fsel(dev_pins.powe.pin,BCM2835_GPIO_FSEL_INPT);
}
if (dev_pins.rfid.pin) {
bcm2835_gpio_fsel(dev_pins.rfid.pin,BCM2835_GPIO_FSEL_INPT);
}
if (dev_pins.sens.pin) {
bcm2835_gpio_fsel(dev_pins.sens.pin,BCM2835_GPIO_FSEL_INPT);
}
bcm2835_close();
#ifdef POINT
point_add(spi_release_e1);
#endif
}
void spi_xmit (pin_t * dev, __uint8_t * buff, int len){
#ifdef POINT
point_add(spi_xmit_s);
#endif
bcm2835_gpio_clr(dev->pin);
bcm2835_spi_transfern((char *)buff,len);
// usleep(100);
bcm2835_gpio_set(dev->pin);
usleep(100);
#ifdef POINT
point_add(spi_xmit_e1);
#endif
}
void spi_5V(int on) {
#ifdef POINT
point_add(spi_5V_s);
#endif
if (on) {
bcm2835_gpio_set(dev_pins.powe.pin);
bcm2835_gpio_set(dev_pins.sens.pin);
}else{
bcm2835_gpio_clr(dev_pins.powe.pin);
bcm2835_gpio_clr(dev_pins.sens.pin);
}
#ifdef POINT
point_add(spi_5V_e1);
#endif
}
void spi_3V(int on) {
#ifdef POINT
point_add(spi_3V_s);
#endif
if (on) {
bcm2835_gpio_set(dev_pins.rfid.pin);
}else{
bcm2835_gpio_clr(dev_pins.rfid.pin);
}
#ifdef POINT
point_add(spi_3V_e1);
#endif
}
int spi_read(pin_t * dev, __uint8_t * buff, int len) {
__uint8_t crc=0;
int i;
__uint8_t tmp[64];
int err=20;
#ifdef POINT
point_add(spi_read_s);
#endif
if (len>=sizeof(tmp)) {
#ifdef POINT
point_add(spi_read_e1);
#endif
if (loglevel >= DEBUG) syslog(LOG_ERR|LOG_DAEMON,"spi_read error length");
return 1;
}
while (err) {
crc=0;
for (i=0;i<len;i++) tmp[i]=buff[i];
spi_xmit(dev,tmp,len);
for (i=0;i<len;i++) {
crc=_crc_ibutton_update(crc,tmp[i]);
// printf("%d %d %d\n",i,tmp[i],crc);
}
if (tmp[0]!=0) if (!crc) break;
if (loglevel >= DEBUG) syslog(LOG_ERR|LOG_DAEMON,"spi_read error CRC. Dev %d",dev->pin);
err--;
usleep(9000);
}
if (!err) {
#ifdef POINT
point_add(spi_read_e2);
#endif
if (loglevel >= DEBUG) syslog(LOG_ERR|LOG_DAEMON,"spi_read error count");
return 1;
}
for (i=0;i<len;i++) {
buff[i]=tmp[i];
}
#ifdef POINT
point_add(spi_read_e3);
#endif
return 0;
}
int spi_write(pin_t * dev, __uint8_t * const buff, int len) {
__uint8_t crc=0;
__uint8_t crc_save;
int i;
__uint8_t tmp[64];
int err=20;
#ifdef POINT
point_add(spi_write_s);
#endif
if (len>sizeof(tmp)-3) {
#ifdef POINT
point_add(spi_write_e1);
#endif
return 1;
}
while (err) {
crc=0;
for (i=0;i<len;i++) {
crc=_crc_ibutton_update(crc,buff[i]);
tmp[i]=buff[i];
}
tmp[len]=crc;
crc_save=crc;
// printf("%d %d %d\n",tmp[0],tmp[1],tmp[2]);
spi_xmit(dev,tmp,len+2);
// printf("%d %d %d %d\n",tmp[0],tmp[1],tmp[2],tmp[3]);
crc=_crc_ibutton_update(0,tmp[0]);
for (i=0;i<len;i++) {
crc=_crc_ibutton_update(crc,buff[i]);
}
crc=_crc_ibutton_update(crc,crc_save);
crc=_crc_ibutton_update(crc,tmp[len+1]);
// printf("%d\n",crc);
if (!crc) break;
if (loglevel >= DEBUG1) syslog(LOG_ERR|LOG_DAEMON,"spi_write error CRC. Dev %d",dev->pin);
err--;
usleep(9000);
}
if (!err) {
#ifdef POINT
point_add(spi_write_e2);
#endif
return 1;
}
#ifdef POINT
point_add(spi_write_e3);
#endif
return 0;
}
int get_irq(int num) {
#ifdef POINT
point_add(get_irq_s);
#endif
int r=0;
if ((r=bcm2835_gpio_lev(num))==1) {
if (loglevel >= DEBUG1) syslog(LOG_DEBUG|LOG_DAEMON,"DEBUG> IRQ_%s",((num==17)?"A":"B"));
}
#ifdef POINT
point_add(get_irq_e1);
#endif
return r;
}