-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.c
executable file
·213 lines (155 loc) · 5.1 KB
/
adc.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
/*
* adc.c
*/
/******************************************************************************
* Include Files *
******************************************************************************/
// Standard header files
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
// Driver header file
#include "prussdrv.h"
#include <pruss_intc_mapping.h>
#include "adc.h"
/******************************************************************************
* Local Macro Declarations *
******************************************************************************/
#define PRU_NUM 1
#define DDR_BASEADDR 0x80000000
#define OFFSET_DDR 0x00001008
#define OFFSET_SHAREDRAM 0
#define PRUSS1_SHARED_DATARAM 4
/******************************************************************************
* Local Function Declarations *
******************************************************************************/
static int LOCAL_exampleInit ( );
/******************************************************************************
* Global Variable Definitions *
******************************************************************************/
static int mem_fd;
static void *ddrMem, *sharedMem;
static int chunk;
static unsigned int *sharedMem_int;
FILE* outfile;
/******************************************************************************
* Global Function Definitions *
******************************************************************************/
void dumpdata(void)
{
unsigned short int *DDR_regaddr;
unsigned char* test;
int ln;
int x;
unsigned char tv;
unsigned short int* valp;
unsigned short int val;
unsigned char rgb24[4];
unsigned char v1, v2;
rgb24[3]=0;
DDR_regaddr = ddrMem + OFFSET_DDR;
valp=(unsigned short int*)&sharedMem_int[OFFSET_SHAREDRAM+1];
for (x=0; x<2000; x++)
{
val=*valp;
val=val & 0xff; // we're just interested in 8 bits
fprintf(outfile, "%d\n", val);
valp++;
valp++;
}
printf("\n");
}
int main (void)
{
unsigned int ret;
int i;
unsigned int count;
void *DDR_paramaddr;
void *DDR_ackaddr;
int fin;
char fname_new[255];
tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
printf("\nINFO: Starting %s example.\r\n", "ADC");
/* Initialize the PRU */
prussdrv_init ();
/* Open PRU Interrupt */
ret = prussdrv_open(PRU_EVTOUT_1);
if (ret)
{
printf("prussdrv_open open failed\n");
return (ret);
}
/* Get the interrupt initialized */
prussdrv_pruintc_init(&pruss_intc_initdata);
// Open file
outfile=fopen("data.csv", "w"); // change back
/* Initialize example */
printf("\tINFO: Initializing example.\r\n");
LOCAL_exampleInit(PRU_NUM);
/* Execute example on PRU */
printf("\tINFO: Executing example.\r\n");
DDR_paramaddr = ddrMem + OFFSET_DDR - 8;
DDR_ackaddr = ddrMem + OFFSET_DDR - 4;
sharedMem_int[OFFSET_SHAREDRAM]=0; // set to zero means no command
// Execute program
prussdrv_exec_program (PRU_NUM, "./prucode_adc.bin");
printf("Initializing. \n");
sleep(1);
sharedMem_int[OFFSET_SHAREDRAM]=(unsigned int)2; // set to 2 means perform capture
// give some time for the PRU code to execute
sleep(1);
printf("Executed: Waiting for ack (curr=%d). \n", sharedMem_int[OFFSET_SHAREDRAM]);
fin=0;
do
{
if ( sharedMem_int[OFFSET_SHAREDRAM] == 1 )
{
// we have received the ack!
dumpdata(); // Store to file // change back
sharedMem_int[OFFSET_SHAREDRAM] = 0;
fin=1;
printf("Ack\n");
}
count++;
if (count > 500000000){
printf("Exeeded allowed limit\n" );
break;
}
} while(!fin);
//prussdrv_pru_wait_event (PRU_EVTOUT_1);
printf("Done\n");
//prussdrv_pru_clear_event (PRU1_ARM_INTERRUPT);
fclose(outfile); // change back
/* Disable PRU and close memory mapping*/
prussdrv_pru_disable(PRU_NUM);
prussdrv_exit ();
munmap(ddrMem, 0x0FFFFFFF);
close(mem_fd);
return(0);
}
/*****************************************************************************
* Local Function Definitions *
*****************************************************************************/
static int LOCAL_exampleInit ( )
{
void *DDR_regaddr1, *DDR_regaddr2, *DDR_regaddr3;
prussdrv_map_prumem(PRUSS1_SHARED_DATARAM, &sharedMem);
sharedMem_int = (unsigned int*) sharedMem;
/* open the device */
mem_fd = open("/dev/mem", O_RDWR);
if (mem_fd < 0) {
printf("Failed to open /dev/mem (%s)\n", strerror(errno));
return -1;
}
/* map the DDR memory */
ddrMem = mmap(0, 0x0FFFFFFF, PROT_WRITE | PROT_READ, MAP_SHARED, mem_fd, DDR_BASEADDR);
if (ddrMem == NULL) {
printf("Failed to map the device (%s)\n", strerror(errno));
close(mem_fd);
return -1;
}
return(0);
}