forked from JamesP6000/WsprryPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpioclk.cpp
432 lines (378 loc) · 12 KB
/
gpioclk.cpp
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Simple program to route either the cyrstal clock or PLL clock to the
// output GPIO pin.
/*
License:
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 2 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <dirent.h>
#include <math.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <malloc.h>
#include <time.h>
#include <sys/time.h>
#include <getopt.h>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
#define ABORT(a) exit(a)
// Used for debugging
#define MARK std::cout << "Currently in file: " << __FILE__ << " line: " << __LINE__ << std::endl
// Nominal clock frequencies
#define F_XTAL (19200000.0)
#define F_PLLD_CLK (500000000.0)
// Choose proper base address depending on RPI1/RPI2 setting from makefile.
#ifdef RPI2
#define BCM2708_PERI_BASE 0x3f000000
//#pragma message "Raspberry Pi 2/3 detected."
#else
#define BCM2708_PERI_BASE 0x20000000
//#pragma message "Raspberry Pi 1 detected."
#endif
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
// This must be declared global so that it can be called by the atexit
// function.
volatile unsigned *allof7e = NULL;
// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
#define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
#define GPIO_GET *(gpio+13) // sets bits which are 1 ignores bits which are 0
#define ACCESS(base) *(volatile int*)((long int)allof7e+base-0x7e000000)
#define SETBIT(base, bit) ACCESS(base) |= 1<<bit
#define CLRBIT(base, bit) ACCESS(base) &= ~(1<<bit)
#define CM_GP0CTL (0x7e101070)
#define GPFSEL0 (0x7E200000)
#define PADS_GPIO_0_27 (0x7e10002c)
#define CM_GP0DIV (0x7e101074)
#define CLKBASE (0x7E101000)
#define DMABASE (0x7E007000)
#define PWMBASE (0x7e20C000) /* PWM controller */
typedef enum {PLLD,XTAL} source_t;
struct GPCTL {
char SRC : 4;
char ENAB : 1;
char KILL : 1;
char : 1;
char BUSY : 1;
char FLIP : 1;
char MASH : 2;
unsigned int : 13;
char PASSWD : 8;
};
void txon(
const source_t & source,
const double & divisor
) {
SETBIT(GPFSEL0 , 14);
CLRBIT(GPFSEL0 , 13);
CLRBIT(GPFSEL0 , 12);
// Set GPIO drive strength, more info: http://www.scribd.com/doc/101830961/GPIO-Pads-Control2
//ACCESS(PADS_GPIO_0_27) = 0x5a000018 + 0; //2mA -3.4dBm
//ACCESS(PADS_GPIO_0_27) = 0x5a000018 + 1; //4mA +2.1dBm
//ACCESS(PADS_GPIO_0_27) = 0x5a000018 + 2; //6mA +4.9dBm
//ACCESS(PADS_GPIO_0_27) = 0x5a000018 + 3; //8mA +6.6dBm(default)
//ACCESS(PADS_GPIO_0_27) = 0x5a000018 + 4; //10mA +8.2dBm
//ACCESS(PADS_GPIO_0_27) = 0x5a000018 + 5; //12mA +9.2dBm
//ACCESS(PADS_GPIO_0_27) = 0x5a000018 + 6; //14mA +10.0dBm
ACCESS(PADS_GPIO_0_27) = 0x5a000018 + 7; //16mA +10.6dBm
// Set the divider
//cout << divisor << endl;
//cout << divisor*pow(2.0,12) << endl;
int div_val=(0x5a<<24)+((int)(divisor*pow(2.0,12)));
//cout << hex << div_val << dec << endl;
ACCESS(CM_GP0DIV) = div_val;
// Turn on
struct GPCTL setupword6= {6/*SRC*/, 1, 0, 0, 0, 3,0x5a};
struct GPCTL setupword1= {1/*SRC*/, 1, 0, 0, 0, 3,0x5a};
struct GPCTL setupword;
if (source==PLLD) {
setupword=setupword6;
} else {
setupword=setupword1;
}
ACCESS(CM_GP0CTL) = *((int*)&setupword);
}
void txoff()
{
struct GPCTL setupword = {6/*SRC*/, 0, 0, 0, 0, 1,0x5a};
ACCESS(CM_GP0CTL) = *((int*)&setupword);
}
void handSig(const int h) {
exit(0);
}
//
// Set up a memory regions to access GPIO
//
void setup_io(
int & mem_fd,
char * & gpio_mem,
char * & gpio_map,
volatile unsigned * & gpio
) {
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit (-1);
}
/* mmap GPIO */
// Allocate MAP block
if ((gpio_mem = (char *)malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
printf("allocation error \n");
exit (-1);
}
// Make sure pointer is on 4K boundary
if ((unsigned long)gpio_mem % PAGE_SIZE)
gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
// Now map it
gpio_map = (char *)mmap(
gpio_mem,
BLOCK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED,
mem_fd,
GPIO_BASE
);
if ((long)gpio_map < 0) {
printf("mmap error %ld\n", (long int)gpio_map);
exit (-1);
}
// Always use volatile pointer!
gpio = (volatile unsigned *)gpio_map;
}
void setup_gpios(
volatile unsigned * & gpio
){
int g;
// Switch GPIO 7..11 to output mode
/************************************************************************\
* You are about to change the GPIO settings of your computer. *
* Mess this up and it will stop working! *
* It might be a good idea to 'sync' before running this program *
* so at least you still have your code changes written to the SD-card! *
\************************************************************************/
// Set GPIO pins 7-11 to output
for (g=7; g<=11; g++) {
INP_GPIO(g); // must use INP_GPIO before we can use OUT_GPIO
//OUT_GPIO(g);
}
}
void print_usage() {
cout << "Usage:" << endl;
cout << " gpioclk [source options] [frequency options]" << endl;
cout << endl;
cout << "Options:" << endl;
cout << " -s --source PLLD|XTAL" << endl;
cout << " Choose GIO clock source. PLLD=500MHz nominal, XTAL=19.2MHz nominal" << endl;
cout << " Default: PLLD" << endl;
cout << " -f --freq f" << endl;
cout << " Desired output frequency in Hz" << endl;
cout << " -d --divisor d" << endl;
cout << " Set the frequency divider value directly" << endl;
cout << endl;
cout << "Either --freq or --divisor (but not both) must be specified." << endl;
cout << "Divisor is a floating point number where the integer portion is 12 bits wide" << endl;
cout << "and the fractional portion is also 12 bits wide." << endl;
}
void parse_commandline(
// Inputs
const int & argc,
char * const argv[],
// Outputs
source_t & source,
bool & freq_specified,
double & freq,
bool & div_specified,
double & divisor
) {
// Default values
source=PLLD;
freq_specified=false;
freq=0;
div_specified=false;
divisor=0;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"source", required_argument, 0, 's'},
{"freq", required_argument, 0, 'f'},
{"divisor", required_argument, 0, 'd'},
{0, 0, 0, 0}
};
while (1) {
/* getopt_long stores the option index here. */
int option_index = 0;
int c = getopt_long (argc, argv, "hs:f:d:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
char * endp;
case 0:
// Code should only get here if a long option was given a non-null
// flag value.
cout << "Check code!" << endl;
ABORT(-1);
break;
case 'h':
print_usage();
ABORT(-1);
break;
case 's':
if (!strcasecmp(optarg,"PLLD")) {
source=PLLD;
} else if (!strcasecmp(optarg,"XTAL")) {
source=XTAL;
} else {
cerr << "Error: unrecognized frequency source" << endl;
ABORT(-1);
}
break;
case 'f':
freq_specified=true;
freq=strtod(optarg,&endp);
if ((optarg==endp)||(*endp!='\0')) {
cerr << "Error: could not parse frequency value" << endl;
ABORT(-1);
}
break;
case 'd':
div_specified=true;
divisor=strtod(optarg,&endp);
if ((optarg==endp)||(*endp!='\0')) {
cerr << "Error: could not parse frequency value" << endl;
ABORT(-1);
}
break;
case '?':
/* getopt_long already printed an error message. */
ABORT(-1);
default:
ABORT(-1);
}
}
if (optind!=argc) {
cerr << "Error: unrecognized command line options" << endl;
ABORT(-1);
}
// Check consistency among command line options.
if (freq_specified&&div_specified) {
cerr << "Error: cannot specify both --freq and --divisor" << endl;
ABORT(-1);
}
if ((!freq_specified)&&(!div_specified)) {
cerr << "Error: must specify either --freq or --divisor" << endl;
ABORT(-1);
}
if (freq_specified&&(freq<=0)) {
cerr << "Error: frequency must be positive" << endl;
ABORT(-1);
}
if (div_specified&&(divisor<=0)) {
cerr << "Error: divisor must be positive" << endl;
ABORT(-1);
}
// Print a summary of the parsed options
cout << "Clock source: " << ((source==PLLD)?"PLLD":"XTAL") << endl;
if (freq_specified) {
cout << "Requested frequency (nominal): " << setprecision(10) << freq << " Hz" << endl;
} else {
cout << "Requested divisor: " << setprecision(20) << divisor << endl;
}
}
int main(const int argc, char * const argv[]) {
#ifdef RPI1
std::cout << "Detected Raspberry Pi version 1" << std::endl;
#else
std::cout << "Detected Raspberry Pi version 2/3" << std::endl;
#endif
// Parse arguments
source_t source;
bool freq_specified;
double freq;
bool div_specified;
double divisor;
parse_commandline(
argc,
argv,
source,
freq_specified,
freq,
div_specified,
divisor
);
const double source_freq=(source==PLLD)?F_PLLD_CLK:F_XTAL;
const double divisor_max=pow(2.0,13)-2+(1-pow(2.0,-12));
// Calculate the actual divisor
double divisor_actual;
if (freq_specified) {
divisor=source_freq/freq;
}
divisor_actual=divisor;
if (divisor_actual*pow(2.0,12)!=floor(divisor_actual*pow(2.0,12))) {
divisor_actual=floor(divisor_actual*pow(2.0,12)+0.5)/pow(2.0,12);
}
if (divisor_actual>divisor_max) {
divisor_actual=divisor_max;
}
if (divisor_actual<2) {
divisor_actual=2;
}
// Actual frequency
const double freq_actual=source_freq/divisor_actual;
cout << "Actual frequency produced (nominal): " << setprecision(30) << freq_actual << " Hz" << endl;
cout << "Actual divisor used: " << setprecision(30) << divisor_actual << endl;
// Initial configuration
int mem_fd;
char *gpio_mem, *gpio_map;
volatile unsigned *gpio = NULL;
setup_io(mem_fd,gpio_mem,gpio_map,gpio);
setup_gpios(gpio);
allof7e = (unsigned *)mmap(
NULL,
0x002FFFFF, //len
PROT_READ|PROT_WRITE,
MAP_SHARED,
mem_fd,
BCM2708_PERI_BASE //base
);
if ((long int)allof7e==-1) {
cerr << "Error: mmap error!" << endl;
ABORT(-1);
}
cout << "Press CTRL-C to stop / exit" << endl;
atexit(txoff);
signal (SIGINT, handSig);
signal (SIGTERM, handSig);
signal (SIGHUP, handSig);
signal (SIGQUIT, handSig);
txon(source,divisor_actual);
// Wait forever
while (1) {
usleep(1000000);
}
return 0;
}