-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterface.c
321 lines (246 loc) · 5.24 KB
/
interface.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
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
/*
* MIT License
*
* Copyright (c) 2021 Mark Retalack
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
typedef unsigned char BYTE; /* 8-bit unsigned */
typedef unsigned short int WORD; /* 16-bit unsigned */
extern void _INT1Interrupt();
int RFIF;
int RFIE;
int PHY_CS;
int PHY_RESETn;
int RF_INT_PIN;
#define CHANNEL 0
// see https://pinout.xyz/pinout/pin31_gpio6#
#define RADIO_INT 22 //5
#define RADIO_RESET 21 //6
#define USR_ISR
#ifdef USR_ISR
/**
* The event semaphore
*/
static sem_t sem;
void radio_interrupt(void)
{
sem_post(&sem);
}
/**
* Wait for the interrupt or a timeout
* \param timeout timeout to wait before exiting
* \return 0=timeout, 1=int
*/
int wait_for_interupt(int timeout)
{
int res=0;
int s;
struct timespec ts;
// get the current time
clock_gettime(CLOCK_REALTIME, &ts);
// add in the timeout
ts.tv_sec+=timeout;
// ok, wait for the semaphore event
s = sem_timedwait(&sem, &ts);
if (s==0)
{
_INT1Interrupt();
res=1;
}
else
{
printf("Timeout waiting for interrupt\n");
}
return(res);
}
#elif defined( USE_POLL)
int wait_for_interupt(int timeout)
{
int ret=0;
int loop=1;
printf("wait_for_interupt\n");
time_t start = time(NULL);
while(loop==1)
{
usleep(1*100);
// check it is low
int res = digitalRead(RADIO_INT);
if (res==0)
{
printf("Calling _INT1Interrupt\n");
// and run the ISR
_INT1Interrupt();
loop=0;
ret=1;
}
time_t current = time(NULL);
if ((current - start)> timeout)
{
loop=0;
}
}
return(ret);
}
#else
int wait_for_interupt(int timeout)
{
usleep(1*100);
return(0);
}
#endif
/**
* Function to setup SPI
*/
void setup_spi()
{
// setup the wiring lib
wiringPiSetup();
// and the SPI channel
int fd = wiringPiSPISetup(CHANNEL, 500000);
// set GPIO 5 to output (Reset line)
pinMode(RADIO_RESET, OUTPUT);
// set GPIO 76 as input for INT line
pinMode(RADIO_INT, INPUT);
#ifdef USR_ISR
// setup the semaphore
sem_init(&sem, 0, 0);
// and add an ISR to the RADIO_INT
wiringPiISR(RADIO_INT, INT_EDGE_FALLING, radio_interrupt);
#endif
}
/**
* Function to set the reset to the Radio Low
*/
void PHY_RESETn_LOW()
{
//printf("PHY_RESETn_LOW\n");
digitalWrite (RADIO_RESET, LOW);
}
/**
* Function to set the reset to the Radio high
*/
void PHY_RESETn_HIGH()
{
//printf("PHY_RESETn_HIGH\n");
digitalWrite (RADIO_RESET, HIGH);
}
/**
* Funtion to action any interrupt that has happened
*/
int RFIF_PIN()
{
wait_for_interupt(30);
return(0);
}
/**
* Function to set A long address value
*/
void PHYSetLongRAMAddr(WORD address, BYTE value)
{
unsigned char buffer[4];
buffer[0]=((((BYTE)(address>>3))&0x7F)|0x80);
buffer[1]=((((BYTE)(address<<5))&0xE0)|0x10);
buffer[2]=(value);
wiringPiSPIDataRW(CHANNEL, buffer, 3);
}
/**
* Function to set A short address value
*/
void PHYSetShortRAMAddr(BYTE address, BYTE value)
{
unsigned char buffer[4];
buffer[0]=(address);
buffer[1]=(value);
wiringPiSPIDataRW(CHANNEL, buffer, 2);
}
/**
* Function to get A short address value
*/
BYTE PHYGetShortRAMAddr(BYTE address)
{
unsigned char buffer[4];
buffer[0]=(address);
buffer[1]=0;
wiringPiSPIDataRW(CHANNEL, buffer, 2);
return(buffer[1]);
}
/**
* Function to get A long address value
*/
BYTE PHYGetLongRAMAddr(WORD address)
{
unsigned char buffer[4];
buffer[0]=(((address>>3)&0x7F)|0x80);
buffer[1]=(((address<<5)&0xE0));
buffer[2]=0;
wiringPiSPIDataRW(CHANNEL, buffer, 3);
return(buffer[2]);
}
/**
* Stub function to setup console
*/
void ConsoleInit(void)
{
printf("setup\n");
}
/**
* wrapper to print to console
*/
void ConsolePutROMString(char* str)
{
printf("%s\n",str);
}
/**
* wrapper to print to console
*/
void ConsolePut(unsigned char c)
{
printf("%c",c);
}
/**
* wrapper to get from the console
*/
unsigned char ConsoleGet()
{
return(0x0);
}
/**
* wrapper to put a char to the console
*/
void PrintChar(unsigned char c)
{
printf("%c",c);
}
/**
* wrapper to put a value to the console
*/
void PrintDec(unsigned char c)
{
printf("%d",c);
}