root/ossiedev/branches/mcarrick/ossiedev-trunk/components/CostasLoop/CostasLoopFPGAInterface.c @ 9296

Revision 9296, 2.8 KB (checked in by mcarrick, 4 years ago)

clarifying debug statements

Line 
1#include <stdio.h>  // using 'printf', 'scanf'
2#include <stdlib.h> // using 'exit'
3#include <string.h> // using 'strcmp'
4#include <fcntl.h>      // using 'open'
5#include <unistd.h> // using 'close'
6#include <sys/mman.h> // using 'mmap', 'munmap'
7#include <math.h>
8
9#define FPGA_BASE_ADDRESS  0xc1a00000
10#define FPGA_SLAVE_REG0OFF 0
11#define FPGA_SLAVE_REG1OFF 12
12#define FPGA_RESET_REG_OFF 64
13
14#define MAP_SIZE 4096UL
15#define MAP_MASK (MAP_SIZE-1)
16
17
18char command[10];
19int memfd;
20void *mapped_base, *mapped_dev_base;
21off_t dev_base = FPGA_BASE_ADDRESS;
22
23void openFPGAInterface()
24{
25    memfd = open("/dev/mem", O_RDWR | O_SYNC);
26    if( memfd == -1 )
27    {
28        printf("CL-Interface: Can't open /dev/mem.\n");
29        exit(EXIT_FAILURE);
30    }
31    printf("CL-Interface: /dev/mem opened.\n");
32
33    mapped_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_base & ~MAP_MASK);
34    if( mapped_base == (void *) -1 )
35    {
36        printf("CL-Interface: Can't map the memory to user space.\n");
37        exit(EXIT_FAILURE);
38    }
39    printf("CL-Interface: Memory mapped at address %p.\n", mapped_base);
40
41    mapped_dev_base = mapped_base + (dev_base & MAP_MASK);
42}
43
44void CostasLoopProcessing()
45{
46    unsigned long samples[512];
47    unsigned long val;
48
49    // push 512 samples to the FPGA
50    unsigned int i;
51    for (i = 0; i < 512; i++)
52    {
53        *((unsigned long *) (mapped_dev_base + FPGA_SLAVE_REG1OFF)) = 0x00000000;
54    }
55
56    // wait for the system to finish processing
57    // (stop if 50000 reads and FPGA circuit still not ready)
58    unsigned int j = 0;
59    unsigned int stop = 0;
60    while(j < 5000 && stop == 0)
61    {
62        val = *((unsigned long *) (mapped_dev_base + FPGA_SLAVE_REG0OFF));
63
64        // exit case to start reading
65        if (val != 0xDEADDEAD)
66        {
67            printf("CL-Interface: Caught exit sequence on the bus, stopping poll loop\n");
68            printf("CL-Interface: Waited for %d cycles\n", j);
69            samples[0] = val;
70            stop = 1; // stop waiting for FPGA circuit, start reading
71        }
72        j++;
73    }
74
75    if (stop == 0)
76    {
77        printf("CL-Interface: Never received ready flag\n");
78    }
79    else
80    {
81        printf("CL-Interface: Reading back 512 samples\n");
82
83         // read back 512 samples
84         unsigned int k;
85         for (k = 1; k < 512; k++)
86         {
87             val = *((unsigned long *) (mapped_dev_base + FPGA_SLAVE_REG0OFF));
88             samples[k] = val;
89         }
90    }
91
92    unsigned int p;
93    for (p = 0; p < 512; p++)
94    {
95        printf("CL-Interface: Sample %d: #%x\n", p, samples[p]);
96    }
97}
98
99void closeFPGAInterface()
100{
101
102    if( munmap(mapped_base, MAP_SIZE) == -1 )
103    {
104        printf("CL-Interface: Can't unmap memory from user space.\n");
105        exit(EXIT_FAILURE);
106    }
107
108    close(memfd);
109}
110
111
Note: See TracBrowser for help on using the browser.