12 #include <netinet/in.h>
16 uint32_t gram_read(struct gramCtx
*ctx
, void *addr
) {
17 uint8_t commands
[6] = { 0x02, 0x01 };
19 int received
, sent
, fd
;
21 fd
= *(int*)(ctx
->user_data
);
23 *(uint32_t*)(&commands
[2]) = htonl((uint32_t)addr
>> 2);
25 sent
= write(fd
, commands
, sizeof(commands
));
26 if (sent
!= sizeof(commands
)) {
27 fprintf(stderr
, "gram_read error (sent bytes length mismatch)\n");
29 received
= read(fd
, &reply
, sizeof(reply
));
30 if (received
!= sizeof(reply
)) {
31 fprintf(stderr
, "gram_read error (read bytes length mismatch: %d != %d)\n", received
, sizeof(reply
));
34 //printf("gram_read: 0x%08x: 0x%08x\n", addr, ntohl(reply));
39 int gram_write(struct gramCtx
*ctx
, void *addr
, uint32_t value
) {
40 uint8_t commands
[10] = { 0x01, 0x01 };
43 *(uint32_t*)(commands
+2) = htonl((uint32_t)addr
>> 2);
44 *(uint32_t*)(commands
+6) = htonl(value
);
46 //printf("gram_write: 0x%08x: 0x%08x\n", addr, value);
48 sent
= write(*(int*)(ctx
->user_data
), commands
, sizeof(commands
));
49 if (sent
!= sizeof(commands
)) {
50 fprintf(stderr
, "gram_write error (sent bytes length mismatch)\n");
57 int serial_setup(const char *devname
, int baudrate
) {
61 serialfd
= open("/dev/ttyUSB1", O_RDWR
|O_NOCTTY
);
63 fprintf(stderr
, "Error %i from open: %s\n", errno
, strerror(errno
));
66 memset(&tty
, 0, sizeof(tty
));
67 if (tcgetattr(serialfd
, &tty
) != 0) {
68 fprintf(stderr
, "Error %i from tcgetattr: %s\n", errno
, strerror(errno
));
71 /* Parameters from flterm */
72 tcgetattr(serialfd
, &tty
);
73 tty
.c_cflag
= B115200
;
76 tty
.c_iflag
= IGNPAR
| IGNBRK
;
77 tty
.c_cflag
|= CLOCAL
;
82 tcsetattr(serialfd
, TCSANOW
, &tty
);
83 tcflush(serialfd
, TCOFLUSH
);
84 tcflush(serialfd
, TCIFLUSH
);
86 cfsetispeed(&tty
, B115200
);
87 cfsetospeed(&tty
, B115200
);
91 tcflush(serialfd
, TCIFLUSH
);
92 if (tcsetattr(serialfd
, TCSANOW
, &tty
) != 0) {
93 fprintf(stderr
, "Error %i from tcsetattr: %s\n", errno
, strerror(errno
));
99 int main(int argc
, char *argv
[]) {
101 int serial_port
, baudrate
= 0;
102 uint32_t read_value
, expected_value
;
103 const size_t kPatternSize
= 512;
104 uint32_t pattern
[kPatternSize
];
105 const int kDumpWidth
= 8;
111 uint32_t ddr_base
= 0x10000000;
114 struct gramProfile profile
= {
116 0x2708, 0x2054, 0x0512, 0x0000
123 struct gramProfile profile
= {
125 0x320, 0x6, 0x200, 0x0
131 struct gramProfile profile2
;
134 fprintf(stderr
, "Usage: %s port baudrate\n", argv
[0]);
138 sscanf(argv
[2], "%d", &baudrate
);
140 fprintf(stderr
, "%d is not a valid baudrate\n", baudrate
);
143 printf("Port: %s, baudrate: %d\n", argv
[1], baudrate
);
145 serial_port
= serial_setup(argv
[1], baudrate
);
146 ctx
.user_data
= &serial_port
;
148 printf("gram init... ");
149 gram_init(&ctx
, &profile
, (void*)ddr_base
, (void*)0x00009000, (void*)0x00008000);
152 printf("Rdly\np0: ");
153 for (size_t i
= 0; i
< 8; i
++) {
154 profile2
.rdly_p0
= i
;
155 gram_load_calibration(&ctx
, &profile2
);
156 gram_reset_burstdet(&ctx
);
157 for (size_t j
= 0; j
< 128; j
++) {
158 tmp
= gram_read(&ctx
, ddr_base
+4*j
);
160 if (gram_read_burstdet(&ctx
, 0)) {
169 printf("Rdly\np1: ");
170 for (size_t i
= 0; i
< 8; i
++) {
171 profile2
.rdly_p1
= i
;
172 gram_load_calibration(&ctx
, &profile2
);
173 gram_reset_burstdet(&ctx
);
174 for (size_t j
= 0; j
< 128; j
++) {
175 tmp
= gram_read(&ctx
, ddr_base
+4*j
);
177 if (gram_read_burstdet(&ctx
, 1)) {
186 printf("Auto calibrating... ");
187 res
= gram_generate_calibration(&ctx
, &profile2
);
188 if (res
!= GRAM_ERR_NONE
) {
190 gram_load_calibration(&ctx
, &profile
);
192 gram_load_calibration(&ctx
, &profile2
);
196 printf("Auto calibration profile:\n");
197 printf("\tp0 rdly: %d\n", profile2
.rdly_p0
);
198 printf("\tp1 rdly: %d\n", profile2
.rdly_p1
);
200 gram_reset_burstdet(&ctx
);
203 for (i
= 0; i
< kPatternSize
; i
++) {
207 printf("memtest... \n");
209 printf("Writing data sequence...");
210 for (i
= 0; i
< kPatternSize
; i
++) {
211 gram_write(&ctx
, ddr_base
+4*i
, pattern
[i
]);
216 sscanf(argv
[3], "%d", &delay
);
217 printf("waiting for %d second(s)...", delay
);
223 printf("Reading data sequence...\n");
224 for (i
= 0; i
< kPatternSize
; i
++) {
225 if ((i
% kDumpWidth
) == 0) {
226 printf("%08x | ", ddr_base
+4*i
);
229 read_value
= gram_read(&ctx
, ddr_base
+4*i
);
230 expected_value
= pattern
[i
];
232 for (int j
= 3; j
>= 0; j
--) {
233 if (((uint8_t*)(&read_value
))[j
] != ((uint8_t*)(&expected_value
))[j
]) {
234 printf("\033[0;31m%02x\033[0m", ((uint8_t*)(&read_value
))[j
]);
237 printf("\033[0;32m%02x\033[0m", ((uint8_t*)(&read_value
))[j
]);
241 if ((i
% kDumpWidth
) == kDumpWidth
-1) {
248 printf("Burstdet %d-%d\n", gram_read_burstdet(&ctx
, 0), gram_read_burstdet(&ctx
, 1));
250 printf("Memtest miss score (lowest is better): %d/100\n", (miss
/4)*100/kPatternSize
);