source: drsdaq/VME/struck/sis1100/V2.02/test/sdram_rw_1.c@ 23

Last change on this file since 23 was 22, checked in by ogrimm, 16 years ago
First commit of drsdaq program
File size: 2.9 KB
Line 
1#define _GNU_SOURCE
2#define _LARGEFILE_SOURCE
3#define _LARGEFILE64_SOURCE
4#define _FILE_OFFSET_BITS 64
5#include <stdio.h>
6#include <errno.h>
7#include <string.h>
8#include <sys/types.h>
9#include <time.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <fcntl.h>
13#include <sys/ioctl.h>
14
15#include "dev/pci/sis1100_var.h"
16
17static int do_write(int p, int start, int size, int* data)
18{
19 off_t pos;
20 int res;
21
22 pos=lseek(p, sizeof(int)*start, SEEK_SET);
23 if (pos==(off_t)-1) {
24 perror("do_write::lseek");
25 return 1;
26 }
27 res=write(p, data, size*sizeof(int));
28 if (res!=size*sizeof(int)) {
29 u_int32_t error;
30 ioctl(p, SIS1100_LAST_ERROR, &error);
31 if (res<0) {
32 fprintf(stderr, "write: %s; error=0x%x\n", strerror(errno), error);
33 } else {
34 fprintf(stderr, "write: res=%d; error=0x%x\n", res, error);
35 }
36 return 1;
37 }
38 return 0;
39}
40
41static int do_read(int p, int start, int size, int* data)
42{
43 off_t pos;
44 int res;
45
46 pos=lseek(p, sizeof(int)*start, SEEK_SET);
47 if (pos==(off_t)-1) {
48 perror("do_read::lseek");
49 return 1;
50 }
51 res=read(p, data, size*sizeof(int));
52 if (res!=size*sizeof(int)) {
53 u_int32_t error;
54 ioctl(p, SIS1100_LAST_ERROR, &error);
55 if (res<0) {
56 fprintf(stderr, "read: %s; error=0x%x\n", strerror(errno), error);
57 } else {
58 fprintf(stderr, "read: res=%d; error=0x%x\n", res, error);
59 }
60 return 1;
61 }
62 return 0;
63}
64
65static void fill_buf(int size, int* buf, int num)
66{
67 int i;
68 for (i=0; i<size; i++) {
69 buf[i]=num<<20|i;
70 }
71}
72
73static int test_buf(int size, int* obuf, int* ibuf)
74{
75 int i, n=0;
76
77 for (i=0; i<size; i++) {
78 if (obuf[i]!=ibuf[i]) {
79 if (n++<20) {
80 printf("[%3d] 0x%08X --> 0x%08X\n", i, obuf[i], ibuf[i]);
81 }
82 }
83 }
84 if (n) printf(" %d errors\n", n);
85 return n;
86}
87
88int main(int argc, char* argv[])
89{
90 int *ibuf, *obuf;
91 off_t max;
92 int p, size;
93
94 if (argc!=2) {
95 fprintf(stderr, "usage: %s path\n", argv[0]);
96 return 1;
97 }
98
99 if ((p=open(argv[1], O_RDWR, 0))<0) {
100 perror("open");
101 return 1;
102 }
103
104 max=lseek(p, 0, SEEK_END);
105 if (max==(off_t)-1) {
106 perror("lseek(0, SEEK_END)");
107 return 1;
108 }
109 fprintf(stderr, "size of sdram is %08Lx (%Ld MByte)\n", max, max/(1<<20));
110
111 max/=sizeof(int);
112 ibuf=calloc(max, sizeof(int));
113 obuf=calloc(max, sizeof(int));
114 if (!ibuf || !obuf) {
115 perror("calloc");
116 return 1;
117 }
118
119 printf("calloc ok\n");
120
121 for (size=256; size<=16384; size+=256) {
122 printf("size=%d\n", size);
123 fill_buf(size, obuf, size);
124 do_write(p, 0, size, obuf);
125 do_read(p, 0, size, ibuf);
126 test_buf(size, obuf, ibuf);
127 }
128
129 free(obuf);
130 free(ibuf);
131 close(p);
132
133 return 0;
134}
Note: See TracBrowser for help on using the repository browser.