1 |
a59a9825
|
Christian Daniel
|
#include <stdio.h>
|
2 |
|
|
#include <unistd.h>
|
3 |
|
|
#include "osmosdr.h"
|
4 |
|
|
#include "sam3u.h"
|
5 |
4cd82c23
|
Christian Daniel
|
#include "lattice/hardware.h"
|
6 |
a59a9825
|
Christian Daniel
|
|
7 |
4cd82c23
|
Christian Daniel
|
int osmoSDRDetect(HANDLE fd)
|
8 |
a59a9825
|
Christian Daniel
|
{
|
9 |
|
|
uint32_t chipID;
|
10 |
|
|
|
11 |
|
|
if(sam3uDetect(fd, &chipID) < 0)
|
12 |
|
|
return -1;
|
13 |
|
|
|
14 |
|
|
if((chipID & 0xffffffe0) == 0x28000960) {
|
15 |
|
|
printf("Chip ID: 0x%08x -- ok\n", chipID);
|
16 |
|
|
} else {
|
17 |
|
|
printf("Chip ID: 0x%08x -- ERROR\n", chipID);
|
18 |
|
|
return -1;
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
return 0;
|
22 |
|
|
}
|
23 |
|
|
|
24 |
4cd82c23
|
Christian Daniel
|
int osmoSDRPrintUID(HANDLE fd, int bank)
|
25 |
a59a9825
|
Christian Daniel
|
{
|
26 |
|
|
uint8_t uniqueID[16];
|
27 |
|
|
int i;
|
28 |
|
|
|
29 |
|
|
if(sam3uReadUniqueID(fd, bank, uniqueID) < 0) {
|
30 |
|
|
fprintf(stderr, "could not read unique ID\n");
|
31 |
|
|
return -1;
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
printf("UID%d : ", bank);
|
35 |
|
|
for(i = 0; i < 16; i++)
|
36 |
|
|
printf("%02x", uniqueID[i]);
|
37 |
|
|
printf("\n");
|
38 |
|
|
|
39 |
|
|
return 0;
|
40 |
|
|
}
|
41 |
|
|
|
42 |
4cd82c23
|
Christian Daniel
|
int osmoSDRBlink(HANDLE fd)
|
43 |
a59a9825
|
Christian Daniel
|
{
|
44 |
|
|
int i;
|
45 |
|
|
|
46 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x44, 1 << 19) < 0)
|
47 |
|
|
return -1;
|
48 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x60, 1 << 19) < 0)
|
49 |
|
|
return -1;
|
50 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x54, 1 << 19) < 0)
|
51 |
|
|
return -1;
|
52 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x10, 1 << 19) < 0)
|
53 |
|
|
return -1;
|
54 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x00, 1 << 19) < 0)
|
55 |
|
|
return -1;
|
56 |
|
|
|
57 |
|
|
for(i = 0; i < 10; i++) {
|
58 |
|
|
printf("on."); fflush(stdout);
|
59 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x34, 1 << 19) < 0)
|
60 |
|
|
return -1;
|
61 |
|
|
usleep(250 * 1000);
|
62 |
|
|
printf("off."); fflush(stdout);
|
63 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x30, 1 << 19) < 0)
|
64 |
|
|
return -1;
|
65 |
|
|
usleep(250 * 1000);
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
printf(" -- ok\n");
|
69 |
|
|
return 0;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
4cd82c23
|
Christian Daniel
|
int osmoSDRRamLoad(HANDLE fd, const void* bin, size_t binSize)
|
73 |
a59a9825
|
Christian Daniel
|
{
|
74 |
|
|
size_t ofs;
|
75 |
|
|
uint32_t tmp;
|
76 |
|
|
|
77 |
4cd82c23
|
Christian Daniel
|
#ifdef WINDOWS
|
78 |
|
|
printf("Uploading %u bytes @ 0x20001000", binSize);
|
79 |
|
|
#else
|
80 |
a59a9825
|
Christian Daniel
|
printf("Uploading %zu bytes @ 0x20001000", binSize);
|
81 |
4cd82c23
|
Christian Daniel
|
#endif
|
82 |
a59a9825
|
Christian Daniel
|
|
83 |
|
|
for(ofs = 0; ofs < binSize; ofs += 4) {
|
84 |
|
|
if(sam3uWrite32(fd, 0x20001000 + ofs, ((uint32_t*)bin)[ofs / 4]) < 0)
|
85 |
|
|
return -1;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
for(ofs = 0; ofs < binSize; ofs += 4) {
|
89 |
|
|
if(sam3uRead32(fd, 0x20001000 + ofs, &tmp) < 0)
|
90 |
|
|
return -1;
|
91 |
|
|
if(tmp != ((uint32_t*)bin)[ofs / 4]) {
|
92 |
|
|
printf(" -- RAM verify failed\n");
|
93 |
|
|
return -1;
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
printf(" -- ok\n");
|
97 |
|
|
|
98 |
|
|
printf("Starting @ 0x%08x\n", ((uint32_t*)bin)[1]);
|
99 |
|
|
|
100 |
|
|
if(sam3uRun(fd, ((uint32_t*)bin)[1]) < 0)
|
101 |
|
|
return -1;
|
102 |
|
|
|
103 |
|
|
return 0;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
4cd82c23
|
Christian Daniel
|
int osmoSDRFlashMCU(HANDLE fd, const void* bin, size_t binSize)
|
107 |
a59a9825
|
Christian Daniel
|
{
|
108 |
|
|
return sam3uFlash(fd, 0, bin, binSize);
|
109 |
|
|
}
|
110 |
4cd82c23
|
Christian Daniel
|
|
111 |
|
|
// HACK
|
112 |
|
|
short int ispEntryPoint();
|
113 |
|
|
|
114 |
|
|
int osmoSDRFlashFPGA(HANDLE fd, const void* algo, size_t algoSize, const void* bin, size_t binSize)
|
115 |
|
|
{
|
116 |
|
|
g_ispFd = fd;
|
117 |
|
|
g_ispAlgo = (uint8_t*)algo;
|
118 |
|
|
g_ispAlgoSize = algoSize;
|
119 |
|
|
g_ispData = (uint8_t*)bin;
|
120 |
|
|
g_ispDataSize = binSize;
|
121 |
|
|
|
122 |
|
|
if(sam3uWrite32(fd, 0x400e0410, (1 << 11)) < 0)
|
123 |
|
|
return -1;
|
124 |
|
|
|
125 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x44, (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8)) < 0)
|
126 |
|
|
return -1;
|
127 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x60, (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8)) < 0)
|
128 |
|
|
return -1;
|
129 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x54, (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8)) < 0)
|
130 |
|
|
return -1;
|
131 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x10, (1 << 5) | (1 << 7) | (1 << 8)) < 0)
|
132 |
|
|
return -1;
|
133 |
|
|
if(sam3uWrite32(fd, 0x400e0e00 + 0x00, (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8)) < 0)
|
134 |
|
|
return -1;
|
135 |
|
|
|
136 |
|
|
ispEntryPoint();
|
137 |
|
|
|
138 |
|
|
return 0;
|
139 |
|
|
}
|