Project

General

Profile

RE: uPP/DMA registers ยป main.c

Scott Whitney, 09/21/2011 05:09 PM

 
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdint.h>
4
#include <sys/types.h>
5
#include <sys/stat.h>
6
#include <fcntl.h>
7
#include <unistd.h>
8
#include <sys/mman.h>
9
#include "fpga.h"
10
#include "pin_mux.h"
11
#include "upp_reg.h"
12
#include <string.h>
13

    
14
#define DEBUG_UPP 1
15

    
16
/* definitions for upp tx and rx buffers */
17
#define UPP_BUF_SIZE (4096*10)
18
void *rx_buf, *tx_buf;
19

    
20

    
21
static int upp_mmap_size, psc1_mmap_size, pinmux_size;
22
static uintptr_t upp_base, psc1_base, pinmux_base;
23

    
24
/* enbale the upp for digital loop back */
25
int upp_enable(void) 
26
{
27
	int mux_val;
28
	int reg_stat;
29

    
30

    
31
	reg_stat = 	( *(uint32_t *)(upp_base+UPPCR) ) = 0;
32
	printf("loopback register: %#010x\n",reg_stat); 
33

    
34

    
35
	return 1;
36
	/*	( *(uint32_t *)(upp_base+UPDBL) )= 0x01;
37
	reg_stat = ( *(uint32_t *)(upp_base+UPDBL) );
38
	printf("loopback register: %#010x\n",reg_stat);  */
39

    
40
	/*	mux_val = ( *(uint32_t *)(upp_base+UPDBL));
41
		printf("DBL: %#010x\n",mux_val);  */
42

    
43

    
44
}
45

    
46
int upp_reset(void)
47
{
48
	int reg_stat;
49

    
50
	reg_stat = ( *(uint32_t *)(upp_base+UPPCR) );
51
	printf("upp Control before reset: %#010x\n",reg_stat); 	
52

    
53

    
54
	/* lets clear out EN first */
55
	( *(uint32_t *)(upp_base+UPPCR) ) = 0;
56
	/* check the dma active bit */
57
	while (( *(uint32_t *)(upp_base+UPPCR) ) & 0x00 ){
58
	  usleep(5);
59
	}
60
	reg_stat = UPPRESET;
61
	( *(uint32_t *)(upp_base+UPPCR) ) = reg_stat;
62
	usleep(100000);
63

    
64
	( *(uint32_t *)(upp_base+UPPCR) ) = 0;
65
	reg_stat = ( *(uint32_t *)(upp_base+UPPCR) );
66
	printf("upp Control UPPCR: %#010x\n",reg_stat); 	
67

    
68
	return 1;
69
}
70

    
71
int upp_config(void)
72
{
73
  int reg_stat;
74
  
75
  reg_stat = 0x02020007;
76
  ( *(uint32_t *)(upp_base+UPCTL) ) = reg_stat;
77
  reg_stat = ( *(uint32_t *)(upp_base+UPCTL) );
78
  printf("UPCTL: %#010x\n",reg_stat); 	
79

    
80
  reg_stat = 0x180020;
81
  ( *(uint32_t *)(upp_base+UPICR) ) = reg_stat;
82
  reg_stat = ( *(uint32_t *)(upp_base+UPICR) );
83
  printf("UPICR: %#010x\n",reg_stat); 	
84
 
85
  /*  reg_stat = 0xDEAD; */
86
  ( *(uint32_t *)(upp_base+UPIVR) ) = 0x00;
87
  reg_stat = ( *(uint32_t *)(upp_base+UPIVR) );
88
  printf("UPIVR: %#010x\n",reg_stat); 	
89

    
90
  reg_stat = 0x010100;
91
  ( *(uint32_t *)(upp_base+UPTCR) ) = reg_stat;
92
  reg_stat = ( *(uint32_t *)(upp_base+UPTCR) );
93
  printf("UPICR: %#010x\n",reg_stat); 	
94

    
95

    
96
  reg_stat = 0x01000;
97
  ( *(uint32_t *)(upp_base+UPDBL) ) = reg_stat;
98
  reg_stat = ( *(uint32_t *)(upp_base+UPDBL) );
99
#if DEBUG_UPP
100
  printf("UPDBL: %#010x\n",reg_stat); 	
101
#endif
102

    
103

    
104
  ( *(uint32_t *)(upp_base+UPIEC) ) = 0xffffffff;
105
  reg_stat = ( *(uint32_t *)(upp_base+UPIEC) );
106
#if DEBUG_UPP
107
  printf("UPIEC: %#010x\n",reg_stat); 	
108
#endif
109

    
110

    
111
  ( *(uint32_t *)(upp_base+UPIES) ) = 0x0f0f;
112
  reg_stat = ( *(uint32_t *)(upp_base+UPIES) );
113
#if DEBUG_UPP
114
  printf("UPIES: %#010x\n",reg_stat); 	
115
#endif
116

    
117
  reg_stat = ( *(uint32_t *)(upp_base+UPISR) );
118
#if DEBUG_UPP
119
  printf("UPISR: %#010x\n",reg_stat); 	
120
#endif
121

    
122
  printf("Wait for interrupt\n\n");
123
  /*  while ( !(*(uint32_t *)(upp_base+UPIER))&& !(*(uint32_t *)(upp_base+UPISR)) )
124
      usleep(2); */
125

    
126
  /*  printf("Interrupt done\n");
127
  printf("Rx buf\n");
128
  printf("0x%08x ", *(int *)(rx_buf));
129
  printf("0x%08x ", *(int *)(rx_buf + 4));
130
  printf("0x%08x ", *(int *)(rx_buf + 8));
131
  printf("0x%08x ", *(int *)(rx_buf + 12));
132

    
133
  printf("Tx buf\n");
134
  printf("0x%08x ", *(int *)(tx_buf));
135
  printf("0x%08x ", *(int *)(tx_buf + 4));
136
  printf("0x%08x ", *(int *)(tx_buf + 8));
137
  printf("0x%08x ", *(int *)(tx_buf + 12)); */
138

    
139
  return 1;
140
  
141
}
142
 
143

    
144
/* lets map the pinmux registers */
145
int upp_pinmux(void) 
146
{
147
	int mux_val;
148
	/*unlock the sysconfig registers */
149
	mux_val=( *(uint32_t *)(pinmux_base+KICK0R));
150
	printf("Original KICK0: %#010x\n",mux_val); 
151

    
152
	mux_val=( *(uint32_t *)(pinmux_base+KICK1R));
153
	printf("Original KICK1: %#010x\n",mux_val); 
154
	( *(uint32_t *)(pinmux_base+KICK0R)) = KICK0UL;
155
	( *(uint32_t *)(pinmux_base+KICK1R)) = KICK1UL;
156
	mux_val=( *(uint32_t *)(pinmux_base+KICK0R));
157
	printf("Changed KICK0: %#010x\n",mux_val); 
158

    
159
	mux_val=( *(uint32_t *)(pinmux_base+KICK1R));
160
	printf("Changed KICK1: %#010x\n",mux_val); 
161

    
162

    
163
	mux_val = ( *(uint32_t *)(pinmux_base+PINMUX13));
164
	printf("Original PINMUX13: %#010x\n",mux_val); 
165
	mux_val |= REG13;
166
	(*(uint32_t *)(pinmux_base+PINMUX13))=0x00;
167
	//	(*(uint32_t *)(pinmux_base+PINMUX13))=mux_val;
168
	mux_val = ( *(uint32_t *)(pinmux_base+PINMUX13));
169
	printf("PINMUX13: %#010x\n",mux_val); 
170
	(*(uint32_t *)(pinmux_base+PINMUX14))=REG14;
171
	(*(uint32_t *)(pinmux_base+PINMUX15))=REG15;
172
	mux_val = ( *(uint32_t *)(pinmux_base+PINMUX15));
173
	printf("PINMUX15: %#010x\n",mux_val); 
174
	printf("We want to set 15 to: %#010x\n",REG15); 
175

    
176
	(*(uint32_t *)(pinmux_base+PINMUX16))=REG16;
177
	(*(uint32_t *)(pinmux_base+PINMUX17))=REG17;
178
	(*(uint32_t *)(pinmux_base+PINMUX18))=REG18;
179
	mux_val = ( *(uint32_t *)(pinmux_base+PINMUX15));
180
	printf("PINMUX15: %#010x\n",mux_val); 
181

    
182
	return 1;
183

    
184
}
185

    
186
static int upp_buff_init(void)
187
{
188
    int ret;
189
    int ps = getpagesize();
190

    
191
    if ( (ret = posix_memalign( &rx_buf, ps, UPP_BUF_SIZE) ) ) {
192
        perror("Memalign failed");
193
        return -1;
194
    }
195

    
196
    if ( (ret = posix_memalign( &tx_buf, ps, UPP_BUF_SIZE) ) ) {
197
        perror("Memalign failed");
198
        return -1;
199
    }
200

    
201
    memset( (void *)tx_buf, 0xab, UPP_BUF_SIZE );
202
    memset( (void *)rx_buf, 0xDE, UPP_BUF_SIZE );
203

    
204
#if DEBUG_UPP
205
    printf("Tx buf addr = 0x%08x (0x%08x)\n", (void *)tx_buf, *(int *)tx_buf);
206
    printf("Rx buf addr = 0x%08x (0x%08x)\n", (void *)rx_buf, *(int *)rx_buf);
207
#endif
208

    
209
    return 0;
210
}
211

    
212
static int set_dma(void)
213
{
214

    
215
  int reg_stat;
216

    
217
  /*--------------------------------------------------------------------------
218
   *  UPID0 - Channel A (xmit)
219
   *--------------------------------------------------------------------------*/
220
  
221
  *(uint32_t *)(upp_base + UPID0) = *(uint32_t *)(tx_buf);
222
  reg_stat = *(uint32_t *)(upp_base + UPID0);
223
#if DEBUG_UPP
224
  printf("UPID0 = 0x%08x\n", reg_stat);
225
#endif
226
  /*--------------------------------------------------------------------------
227
   * UPID1 - Channel A (xmit)
228
   *--------------------------------------------------------------------------*/
229
     *(uint32_t *)(upp_base + UPID1) = (0x4 << 16 | 0x400);
230
    reg_stat = *(uint32_t *)(upp_base + UPID1);
231
#if DEBUG_UPP
232
    printf("UPID1 = 0x%08x\n", reg_stat);
233
#endif
234
    /*--------------------------------------------------------------------------
235
     * UPID2 - Channel A (xmit)
236
     *--------------------------------------------------------------------------*/
237
    *(uint32_t *)(upp_base + UPID2) = 0x00000400;
238
    reg_stat = *(uint32_t *)(upp_base + UPID2);
239
#if DEBUG_UPP
240
    printf("UPID2 = 0x%08x\n", reg_stat);
241
#endif
242
    /*--------------------------------------------------------------------------
243
     * UPQD0 - Channel B (Rcv)
244
     *--------------------------------------------------------------------------*/
245
    *(uint32_t *)(upp_base + UPQD0) = *(uint32_t *)(tx_buf);
246
    reg_stat = *(uint32_t *)(upp_base + UPQD0);
247
#if DEBUG_UPP 
248
    printf("UPQD0 = 0x%08x\n", reg_stat);
249
#endif
250

    
251
    /*--------------------------------------------------------------------------
252
     * UPQD1 - Channel B (Rcv)
253
     *--------------------------------------------------------------------------*/
254
     *(uint32_t *)(upp_base + UPQD1) = (0x4 << 16 | 0x400);
255
    reg_stat = *(uint32_t *)(upp_base + UPQD1);
256
#if DEBUG_UPP 
257
    printf("UPQD1 = 0x%08x\n", reg_stat);
258
#endif
259

    
260
    /*--------------------------------------------------------------------------
261
     * UPQD2 - Channel B (Rcv)
262
     *-------------------------------------------------------------------------*/
263
    *(uint32_t *)(upp_base + UPQD2) = 0x00000400;
264
    reg_stat = *(uint32_t *)(upp_base + UPQD2);
265
#if DEBUG_UPP
266
    printf("UPQD2 = 0x%08x\n", reg_stat);
267
#endif
268

    
269
}
270

    
271

    
272

    
273
/* lets map the pinmux registers */
274
static int upp_mmap_init(void)
275
{
276
  int mem_fd;
277
  int reg_stat;
278

    
279
  mem_fd = open("/dev/mem", O_RDWR);
280
  if ( mem_fd < 0){
281
    printf("failed to open /dev/mem\n");
282
    return mem_fd;
283
  }
284
  /* map the pin mux */
285
  pinmux_size = getpagesize();
286
  pinmux_base = (uintptr_t)mmap(0,pinmux_size, PROT_READ|PROT_WRITE,
287
			     MAP_SHARED, mem_fd, SYSBASE);
288
  if ( pinmux_base == (uintptr_t)MAP_FAILED){
289
    printf("\n\nupp_base mmap fail!\n\n");
290
    return -1;
291
} 
292

    
293
  /* map the uPP */
294
  upp_mmap_size = getpagesize();
295
  upp_base = (uintptr_t)mmap(0,upp_mmap_size, PROT_READ|PROT_WRITE,
296
			     MAP_SHARED, mem_fd, UPPBASE);
297
  if ( upp_base == (uintptr_t)MAP_FAILED){
298
    printf("\n\nupp_base mmap fail!\n\n");
299
    return -1;
300
  }
301

    
302
  /* map the Power control module */
303
  psc1_mmap_size = getpagesize();
304
  psc1_base = (uintptr_t)mmap(0,psc1_mmap_size, PROT_READ|PROT_WRITE,
305
			     MAP_SHARED, mem_fd, PSC1);
306
  if ( psc1_base == (uintptr_t)MAP_FAILED){
307
    printf("\n\nupp_base mmap fail!\n\n");
308
    return -1;
309
  }
310

    
311
  close(mem_fd);
312
}
313

    
314

    
315
/* lets unmap the pinmux registers */
316
static int upp_mmap_deinit(void){
317

    
318
  if (munmap( (void *)pinmux_base, pinmux_size) == -1){
319
    printf("\n\n munmap fail\n");
320
    return -1;
321
  }
322

    
323

    
324

    
325
  if (munmap( (void *)upp_base, upp_mmap_size) == -1){
326
    printf("\n\n munmap fail\n");
327
    return -1;
328
  }
329

    
330
  if (munmap( (void *)psc1_base, psc1_mmap_size) == -1){
331
    printf("\n\n munmap fail\n");
332
    return -1;
333
  }
334

    
335
}
336

    
337
int main(void)
338
{
339
	int fd;
340
	char *mem;
341
	unsigned short *reg;
342
	int i;
343
	int id;
344
	int reg_stat;
345
	unsigned short val;
346

    
347

    
348

    
349

    
350
	/* map the pinmux registers */
351
	upp_mmap_init();
352
	
353
	reg_stat = ( *(uint32_t *)(psc1_base)); 
354
	printf("Peripheral Revision is: %#010x\n",reg_stat);
355

    
356
	reg_stat = ( *(uint32_t *)(psc1_base+PSTAT)); 
357
	printf("PSTAT is: %#010x\n",reg_stat);
358

    
359
	reg_stat = 0x03;
360
	( *(uint32_t *)(psc1_base+UPPCONTROL))= reg_stat;
361
	printf("uPP PSC status: %#010x\n",reg_stat); 
362

    
363
	reg_stat = ( *(uint32_t *)(psc1_base+PSCMD)); 
364
	reg_stat &= 0xfffffffc;
365
	reg_stat |= 0x03;
366
	( *(uint32_t *)(psc1_base+PSCMD))= reg_stat;
367
	
368
	while ( *(uint32_t *)(psc1_base + PSTAT) & 0x01)
369
	  usleep(2);
370

    
371
	reg_stat = ( *(uint32_t *)(psc1_base+UPPSTATUS));
372
	printf("uPP PSC status: %#010x\n",reg_stat); 
373
	
374
	
375
	reg_stat = ( *(uint32_t *)(upp_base) );
376
	printf("uPP Peripheral ID register: %#010x\n",reg_stat); 
377

    
378

    
379

    
380
	/* mux the upp */
381
	upp_pinmux();
382

    
383
	/* now enbale upp loopback */
384
	/*upp_enable();*/
385
	upp_reset();
386

    
387
	/* configure the upp for loopback */
388
	upp_config();
389

    
390
	/* setup the upp xmit and rcv buffers */
391
	upp_buff_init();
392

    
393

    
394

    
395
	/* enable the device */
396
	*(uint32_t *)(upp_base + UPPCR) = (0x08);
397
	reg_stat = ( *(uint32_t *)(upp_base+UPPCR) );
398
	printf("upp Control now: %#010x\n",reg_stat); 	
399

    
400
	reg_stat = ( *(uint32_t *)(psc1_base+UPIS0)); 
401
	printf("Chan I status0: %#010x\n",reg_stat);
402
	reg_stat = ( *(uint32_t *)(psc1_base+UPIS1)); 
403
	printf("Chan I status1: %#010x\n",reg_stat);
404
	reg_stat = ( *(uint32_t *)(psc1_base+UPIS2)); 
405
	printf("Chan I status2: %#010x\n",reg_stat);
406
	reg_stat = ( *(uint32_t *)(psc1_base+UPQS0)); 
407
	printf("Chan Q status0: %#010x\n",reg_stat);
408
	reg_stat = ( *(uint32_t *)(psc1_base+UPQS1)); 
409
	printf("Chan Q status1: %#010x\n",reg_stat);
410
	reg_stat = ( *(uint32_t *)(psc1_base+UPQS2)); 
411
	printf("Chan Q status2: %#010x\n",reg_stat);
412

    
413
 	/* set the dma transfers */
414
	set_dma();
415

    
416
	reg_stat = ( *(uint32_t *)(psc1_base+UPIES)); 
417
	printf("UPIES: %#010x\n",reg_stat);
418

    
419
	/*	for (i=0;i<2048;i++){
420
	  *(tx_buf+i)=i;
421
	  }*/
422

    
423
	reg_stat = ( *(uint32_t *)(psc1_base+UPIES)); 
424
	printf("UPIES: %#010x\n",reg_stat);
425

    
426

    
427
	reg_stat = ( *(uint32_t *)(psc1_base+UPIS0)); 
428
	printf("Chan I status0: %#010x\n",reg_stat);
429
	reg_stat = ( *(uint32_t *)(psc1_base+UPIS1)); 
430
	printf("Chan I status1: %#010x\n",reg_stat);
431
	reg_stat = ( *(uint32_t *)(psc1_base+UPIS2)); 
432
	printf("Chan I status2: %#010x\n",reg_stat);
433
	reg_stat = ( *(uint32_t *)(psc1_base+UPQS0)); 
434
	printf("Chan Q status0: %#010x\n",reg_stat);
435
	reg_stat = ( *(uint32_t *)(psc1_base+UPQS1)); 
436
	printf("Chan Q status1: %#010x\n",reg_stat);
437
	reg_stat = ( *(uint32_t *)(psc1_base+UPQS2)); 
438
	printf("Chan Q status2: %#010x\n",reg_stat);
439

    
440
	reg_stat = ( *(uint32_t *)(psc1_base+UPIES)); 
441
	printf("UPIES: %#010x\n",reg_stat);
442

    
443

    
444

    
445

    
446
	/* unmap the pinmux registers */
447
	upp_mmap_deinit();
448

    
449
	return EXIT_SUCCESS;
450
}
451

    
    (1-1/1)
    Go to top
    Add picture from clipboard (Maximum size: 1 GB)