forked from crystalfontz/at91sam9g45_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.cpp
236 lines (208 loc) · 6.3 KB
/
mem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Description:
*
* An memory read/write application for accesing hardware
* memory addresses
*
* License:
*
* Copyright 2012 Crystalfontz America, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
const char * MEM_FILE = "/dev/mem";
const int MAP_PROT = PROT_READ | PROT_WRITE;
const size_t SYS_MAP_SIZE = 0xFFFF+1;
const int SYS_MAP_MASK = 0xFFFF;
class SysMem
{
private:
int fSysFd;
unsigned long int fSysMappedMem;
public:
SysMem()
: fSysFd(-1) {}
~SysMem() { close_mem(); }
public:
bool open_mem( unsigned long int address ) {
bool ret = false;
// Set up memory interface
fSysFd = open( MEM_FILE, O_RDWR | O_SYNC );
if (fSysFd >= 0) {
off_t pgoff;
pgoff = address & ~SYS_MAP_MASK;
fSysMappedMem = (unsigned long int)mmap( 0, SYS_MAP_SIZE, MAP_PROT, MAP_SHARED, fSysFd, pgoff );
ret = (fSysMappedMem != (unsigned long int)MAP_FAILED);
}
return ret;
}
void close_mem( void ) {
if (fSysFd >= 0)
close(fSysFd);
}
void write_mem32(unsigned long int addr, unsigned long value) {
(*(volatile unsigned long *)(fSysMappedMem+(addr&SYS_MAP_MASK))) = value;
}
void write_mem16(unsigned long int addr, unsigned short value) {
(*(volatile unsigned short *)(fSysMappedMem+(addr&SYS_MAP_MASK))) = value;
}
unsigned long read_mem32(unsigned long int addr) {
return (*(volatile unsigned long *)(fSysMappedMem+(addr&SYS_MAP_MASK)));
}
unsigned short read_mem16(unsigned long int addr) {
return (*(volatile unsigned short *)(fSysMappedMem+(addr&SYS_MAP_MASK)));
}
};
void show_help(const char* prog_name)
{
fprintf(stderr,"%s --read=address --bits=16|32\n", prog_name);
fprintf(stderr,"%s --write=address --bits=16|32 --value=value\n", prog_name);
}
int main(int argc, char** argv, char** env)
{
int ret = 0;
int c;
bool iread = false;
bool iwrite = false;
bool ivalue = false;
unsigned long int value = 0;
unsigned long int bitcount = 0;
unsigned long int iocount = 1;
unsigned long int address = 0;
while (1) {
static struct option long_options[] = {
{"write", required_argument, 0, 'w'},
{"value", required_argument, 0, 'v'},
{"read", required_argument, 0, 'r'},
{"bits", required_argument, 0, 'b'},
{"count", required_argument, 0, 'c'},
{"help", no_argument, 0, '?'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "w:v:r::cb?",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c) {
case 'w':
if (iread) {
fprintf(stderr, "--read and --write are mutually exclusive\n");
exit(1);
}
iwrite = true;
address = strtoll(optarg,NULL,0);
break;
case 'r':
if (iwrite) {
fprintf(stderr, "--read and --write are mutually exclusive\n");
exit(1);
}
iread = true;
address = strtoll(optarg,NULL,0);
break;
case 'v':
ivalue = true;
value = strtoll(optarg,NULL,0);
break;
case 'b':
bitcount = strtoll(optarg,NULL,0);
break;
case 'c':
iocount = strtoll(optarg,NULL,0);
break;
case '?':
show_help(argv[0]);
exit(1);
break;
default:
show_help(argv[0]);
exit(1);
}
}
if (!(iread || iwrite)) {
fprintf(stderr,"Need to specify either --read or --write\n");
show_help(argv[0]);
exit(1);
}
if (bitcount != 16 && bitcount != 32) {
fprintf(stderr,"Need to specify --bits as either 16 or 32\n");
show_help(argv[0]);
exit(1);
}
if ((!ivalue && iwrite) || (ivalue && iread)) {
fprintf(stderr,"Need to specify a value with --value when doing a write only\n");
show_help(argv[0]);
exit(1);
}
if (iocount * (bitcount/8) > SYS_MAP_SIZE) {
fprintf(stderr,"--count cannot cause i/o to span more than %u bytes\n", SYS_MAP_SIZE);
exit(1);
}
SysMem mem;
if (mem.open_mem(address))
{
unsigned int i = 0;
while (i < iocount)
{
bool newline = ((i*(bitcount/8))%16)==0;
if (newline && i > 0)
printf("\n");
if (newline)
printf("0x%08lx:", address);
if (iread)
{
switch (bitcount) {
case 16:
printf(" 0x%04x", mem.read_mem16(address));
break;
case 32:
printf(" 0x%08lx", mem.read_mem32(address));
break;
}
}
else
{
switch (bitcount) {
case 16:
mem.write_mem16(address, value);
printf(" 0x%04lx", value);
break;
case 32:
mem.write_mem32(address, value);
printf(" 0x%08lx", value);
break;
}
}
i++;
address+=(bitcount/8);
}
printf("\n");
}
else
{
fprintf(stderr, "Opening memory block for 0x%08lx Failed!\n", address);
ret = 2;
}
return ret;
}