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
|
/**************************************************************************
* spi. - SPI functions to connect an Arduidno to the TI CC3000
*
* This code uses the Arduino hardware SPI library (or a bit-banged
* SPI for the Teensy 3.0) to send & receive data between the library
* API calls and the CC3000 hardware. Every
*
* Version 1.0.1b
*
* Copyright (C) 2013 Chris Magagna - cmagagna@yahoo.com
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Don't sue me if my code blows up your board and burns down your house
*
****************************************************************************/
/*****************************************************************************
* Included Files
*****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <debug.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "spi.h"
#include <nuttx/wireless/cc3000.h>
#include <nuttx/wireless/cc3000/cc3000_common.h>
/*****************************************************************************
* Pre-processor Definitions
*****************************************************************************/
#undef SPI_DEBUG /* Define to enable debug */
#undef SPI_VERBOSE /* Define to enable verbose debug */
#ifdef SPI_DEBUG
# define spidbg lldbg
# ifdef SPI_VERBOSE
# define spivdbg lldbg
# else
# define spivdbg(x...)
# endif
#else
# undef SPI_VERBOSE
# define spidbg(x...)
# define spivdbg(x...)
#endif
static struct
{
int cc3000fd;
gcSpiHandleRx pfRxHandler;
pthread_t unsoliced_thread;
bool run;
uint8_t rx_buffer[CC3000_RX_BUFFER_SIZE];
} spiconf;
/*****************************************************************************
* Public Functions
*****************************************************************************/
/*****************************************************************************
* Name: SpiResumeSpi
*
* Description:
* Will re enable the SPI_IRQ'a ability to create interrupts. It is used to
* resume processing after the code passed to SpiOpen is Called
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
*****************************************************************************/
void SpiResumeSpi(void)
{
DEBUGASSERT(spiconf.cc3000fd);
if (ioctl(spiconf.cc3000fd,CC3000IOC_COMPLETE,0))
{
printf("ioctl:CC3000IOC_COMPLETE failed: %s\n", strerror(errno));
}
}
/*****************************************************************************
* Name: SpiWrite
*
* Description:
* This function enter point for write flow
*
* Input Parameters:
* pUserBuffer
* usLength
*
* Returned Value:
*
*****************************************************************************/
long SpiWrite(uint8_t *pUserBuffer, uint16_t usLength)
{
DEBUGASSERT(spiconf.cc3000fd);
return write(spiconf.cc3000fd,pUserBuffer,usLength) == usLength ? 0 : -errno;
}
/*****************************************************************************
* Name: SpiRead
*
* Description:
* This function enter point for read flow. This function will block the
* caller untinlthere is data Available
*
* Input Parameters:
* pUserBuffer
* usLength
*
* Returned Value:
*
*****************************************************************************/
long SpiRead(uint8_t *pUserBuffer, uint16_t usLength)
{
DEBUGASSERT(spiconf.cc3000fd);
return read(spiconf.cc3000fd,pUserBuffer,usLength);
}
/*****************************************************************************
* Name: unsoliced_thread_func
*
* Description:
* This is the thread for unsolicited events. This function will block the
* caller untinlthere is data Available
*
* Input Parameters:
* parameter
*
* Returned Value:
*
*****************************************************************************/
static void *unsoliced_thread_func(void *parameter)
{
char queuename[QUEUE_NAMELEN];
int status = 0;
int nbytes = 0;
int minor = 0;
ioctl(spiconf.cc3000fd, CC3000IOC_GETQUEID, (unsigned long)&minor);
snprintf(queuename, QUEUE_NAMELEN, QUEUE_FORMAT, minor);
mqd_t queue = mq_open(queuename,O_RDONLY);
while(spiconf.run)
{
memset(spiconf.rx_buffer,0,sizeof(spiconf.rx_buffer));
nbytes = mq_receive(queue, spiconf.rx_buffer, CC3000_RX_BUFFER_SIZE, 0);
if (nbytes > 0)
{
spiconf.pfRxHandler(spiconf.rx_buffer);
}
}
mq_close(queue);
pthread_exit((pthread_addr_t)status);
return (pthread_addr_t)status;
}
/*****************************************************************************
* Name: SpiOpen
*
* Description:
* Configure the SPI
*
* Input Parameters:
* pfRxHandler the Rx handler for SPI
*
* Returned Value:
* None
*
*****************************************************************************/
void SpiOpen(gcSpiHandleRx pfRxHandler)
{
pthread_attr_t attr;
int status;
DEBUGASSERT(spiconf.cc3000fd == 0);
int fd = open("/dev/wireless0",O_RDWR|O_BINARY);
if (fd > 0)
{
spiconf.pfRxHandler = pfRxHandler;
spiconf.cc3000fd = fd;
spiconf.run = true;
status = pthread_attr_init(&attr);
DEBUGASSERT(status == 0)
status = pthread_create(&spiconf.unsoliced_thread, &attr,
unsoliced_thread_func, NULL);
DEBUGASSERT(status == 0)
}
}
/*****************************************************************************
* Name: SpiClose
*
* Description:
* Configure the SPI
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
*****************************************************************************/
void SpiClose(void)
{
if (spiconf.cc3000fd)
{
int status;
spiconf.run = false;
pthread_cancel(spiconf.unsoliced_thread);
pthread_join(spiconf.unsoliced_thread, (pthread_addr_t*)&status);
close(spiconf.cc3000fd);
spiconf.cc3000fd = 0;
}
}
|