-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcc1101_spi.h
executable file
·407 lines (320 loc) · 7.49 KB
/
cc1101_spi.h
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* 说明:SPI通讯实现
* 方式一: 同时发送与接收实现函数: SPI_Transfer()
* 方式二:发送与接收分开来实现
* SPI_Write() 只发送
* SPI_Read() 只接收
* 两种方式不同之处:方式一,在发的过程中也在接收,第二种方式,收与发单独进行
* Created on: 2013-5-28
* Author: lzy
*/
#ifndef _SPI_H
#define _SPI_H
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
//#include <stddef.h> //NULL
#include <getopt.h>
#include <fcntl.h>
#include <stddef.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
/* #include "Debug.h" */
#define pr_debug printf
#define pr_err printf
#define SPI_DEBUG 0
//extern int spi_write_then_read(struct spi_device *spi,const void *txbuf, unsigned n_tx,void *rxbuf, unsigned n_rx);
static const char *device = "/dev/spidev32766.1";
static uint8_t mode = 0 ; /* SPI通信使用全双工,设置CPOL=0,CPHA=0。 */
static uint8_t bits = 8; /* 8bits读写,MSB first。*/
static uint32_t speed =5*1000*1000;/* min.1M 设置传输速度 */
static uint16_t delay = 0;
static int g_SPI_Fd = 0;
static void pabort(const char *s)
{
perror(s);
abort();
}
/**
* 功 能:同步数据传输
* 入口参数 :
* TxBuf -> 发送数据首地址
* len -> 交换数据的长度
* ns --repeat ns times
* 出口参数:
* RxBuf -> 接收数据缓冲区
* 返回值:0 成功
* 开发人员:Lzy 2013-5-22
*/
/* int SPI_Transfer(const uint8_t *TxBuf, uint8_t *RxBuf, int len) */
int SPI_Transfer( const uint8_t *TxBuf, uint8_t *RxBuf, int len,int ns)
{
int ret;
int fd = g_SPI_Fd;
/*
struct spi_ioc_transfer tr;
memset(&tr,0,sizeof(tr));
tr.tx_buf = (unsigned long) TxBuf;
tr.rx_buf = (unsigned long) RxBuf;
tr.len =len;
tr.delay_usecs = delay;
tr.bits_per_word=bits;
tr.speed_hz=speed;
*/
struct spi_ioc_transfer tr={
.tx_buf = (unsigned long) TxBuf,
.rx_buf = (unsigned long) RxBuf,
.len =len,
.delay_usecs = delay,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(ns), &tr);
if (ret < 1)
pr_err("can't send spi message");
else
{
#if SPI_DEBUG
int i;
pr_debug("\nsend spi message Succeed.");
pr_debug("\nSPI Send [Len:%d]: ", len);
for (i = 0; i < len; i++)
{
if (i % 8 == 0)
printf("\t");
printf("0x%02X ", TxBuf[i]);
}
/* printf("\n"); */
pr_debug("SPI Receive [len:%d]:", len);
for (i = 0; i < len; i++)
{
if (i % 8 == 0)
printf("\t");
printf("0x%02X ", RxBuf[i]);
}
/* printf("\n");*/
#endif
}
return ret;
}
/*---------------------------------------------------------
SPI_Write_then_Read( )
Write then Read SPI device with no interruption
n_tx+n_rx = MAX.32 ????
---------------------------------------------------------*/
int SPI_Write_then_Read(const uint8_t *TxBuf, int n_tx, uint8_t *RxBuf, int n_rx)
{
int ret;
int fd = g_SPI_Fd;
// uint8_t buf[32]; //--MAX 32bytes for one time transfer ?????
struct spi_ioc_transfer xfer[2];
memset(xfer,0,sizeof(xfer));
// memset(buf,0,sizeof(buf));
xfer[0].tx_buf = (unsigned long) TxBuf;
// xfer[0].rx_buf = 0;//NULL;
xfer[0].len = n_tx;
xfer[0].delay_usecs = delay;
// xfer[1].tx_buf = 0;//NULL;
xfer[1].rx_buf = (unsigned long) RxBuf;
xfer[1].len = n_rx;
xfer[1].delay_usecs = delay;
ret = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);
if (ret < 1)
pr_err("********** SPI_Write_then_Read(): Can't send message **********\n");
// else
// pr_err("------SPI_Write_then_Read(): spi IOCTL ret =%d ------\n",ret);
return ret;
}
/*---------------------------------------------------------
SPI_Write_then_Write( )
Write 2 times to SPI device with no interruption
n_tx1+n_tx2 = MAX. 36 ???????
---------------------------------------------------------*/
int SPI_Write_then_Write(const uint8_t *TxBuf1, int n_tx1, uint8_t *TxBuf2, int n_tx2)
{
int ret;
int fd = g_SPI_Fd;
struct spi_ioc_transfer xfer[2];
memset(xfer,0,sizeof(xfer));
xfer[0].tx_buf = (unsigned long) TxBuf1;
xfer[0].len = n_tx1;
xfer[0].delay_usecs = delay;
xfer[1].tx_buf = (unsigned long) TxBuf2;
xfer[1].len = n_tx2;
xfer[1].delay_usecs = delay;
ret = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);
if (ret < 1)
pr_err("********** SPI_Write_then_Write(): Can't send message **********\n");
// else
// pr_err("------SPI_Write_then_Write(): spi IOCTL ret =%d ------\n",ret);
return ret;
}
/**
* 功 能:发送数据
* 入口参数 :
* TxBuf -> 发送数据首地址
*len -> 发送与长度
*返回值:0 成功
* 开发人员:Lzy 2013-5-22
*/
int SPI_Write(uint8_t *TxBuf, int len)
{
int ret;
int fd = g_SPI_Fd;
ret = write(fd, TxBuf, len);
if (ret < 0)
pr_err("SPI Write errorn");
else
{
#if SPI_DEBUG
int i;
pr_debug("\nSPI Write [Len:%d]: ", len);
for (i = 0; i < len; i++)
{
if (i % 8 == 0)
printf("\n\t");
printf("0x%02X ", TxBuf[i]);
}
printf("\n");
#endif
}
return ret;
}
/*
//---- SPI Write then Read ------
int SPI_WRead(uint8_t *TxBuf,int n_tx,uint8_t *RxBuf, int n_rx)
{
int ret;
int fd = g_SPI_Fd;
ret = spi_write_then_read(fd,TxBuf,n_tx,RxBuf,n_rx);
if(ret<0)
printf("spi_write_then_read() error!\n");
return ret;
}
*/
/**
* 功 能:接收数据
* 出口参数:
* RxBuf -> 接收数据缓冲区
* rtn -> 接收到的长度
* 返回值:>=0 成功
* 开发人员:Lzy 2013-5-22
*/
int SPI_Read(uint8_t *RxBuf, int len)
{
int ret;
int fd = g_SPI_Fd;
ret = read(fd, RxBuf, len);
if (ret < 0)
pr_err("SPI Read errorn");
else
{
#if SPI_DEBUG
int i;
pr_debug("SPI Read [len:%d]:", len);
for (i = 0; i < len; i++)
{
if (i % 8 == 0)
printf("\n\t");
printf("0x%02X ", RxBuf[i]);
}
printf("\n");
#endif
}
return ret;
}
/**
* 功 能:打开设备 并初始化设备
* 入口参数 :
* 出口参数:
* 返回值:0 表示已打开 0XF1 表示SPI已打开 其它出错
* 开发人员:Lzy 2013-5-22
*/
int SPI_Open(void)
{
int fd;
int ret = 0;
if (g_SPI_Fd != 0) /* 设备已打开 */
return 0xF1;
fd = open(device, O_RDWR);
/* fd = open(device, O_RDONLY); */
if (fd < 0)
pabort("can't open device");
else
pr_debug("SPI - Open Succeed. Start Init SPI...\n");
g_SPI_Fd = fd;
/*
* spi mode
*/
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1)
pabort("can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1)
pabort("can't get spi mode");
/*
* bits per word
*/
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");
/*
* max speed hz
*/
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't set max speed hz");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't get max speed hz");
pr_debug("spi mode: %d\n", mode);
pr_debug("bits per word: %d\n", bits);
pr_debug("max speed: %d KHz (%d MHz)\n", speed / 1000, speed / 1000 / 1000);
return ret;
}
/**
* 功 能:关闭SPI模块
*/
int SPI_Close(void)
{
int fd = g_SPI_Fd;
if (fd == 0) /* SPI是否已经打开*/
return 0;
close(fd);
g_SPI_Fd = 0;
return 0;
}
/**
* 功 能:自发自收测试程序
* 接收到的数据与发送的数据如果不一样 ,则失败
* 说明:
* 在硬件上需要把输入与输出引脚短跑
* 开发人员:Lzy 2013-5-22
*/
int SPI_LookBackTest(void)
{
int ret, i;
const int BufSize = 16;
uint8_t tx[BufSize], rx[BufSize];
bzero(rx, sizeof(rx));
for (i = 0; i < BufSize; i++)
tx[i] = i;
pr_debug("\nSPI - LookBack Mode Test...n");
ret = SPI_Transfer(tx, rx, BufSize,1);
if (ret > 1)
{
ret = memcmp(tx, rx, BufSize);
if (ret != 0)
{
pr_err("LookBack Mode Test error\n");
//pabort("error");
}
else
pr_debug("SPI - LookBack Mode OKn");
}
return ret;
}
#endif