-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryMappedDevice.h
351 lines (295 loc) · 12.7 KB
/
MemoryMappedDevice.h
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#ifndef MEMORY_MAPPED_DEVICE_H
#define MEMORY_MAPPED_DEVICE_H
#include <sys/mman.h>
#include <fcntl.h>
#include <string>
#include "TraceLog.h"
#include <BitfieldMacros.h>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
//
// This module has the generic API for access to memory mapped devices for
// 8, 16, and 32 bit memory mapped HW devices, create derived classes for a
// specific memory mapped HW device and add any functionality as needed, see
// the examples My8BitDevice.h, My16BitDevice.h, and My32BitDevice.h for
// examples of derived classes.
//
// All the basic bit/register banging is provided by these base classes, the
// derived classes should implement any higher level functionality as required
// by the specific device.
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// class definition for 8-bit devices
//
////////////////////////////////////////////////////////////////////////////////
class MemoryMappedDevice8
{
public:
// constructor for a RAM based buffer address pointer
MemoryMappedDevice8(const char *name_, void *address_, unsigned size_) : _address((uint8_t *)address_), _memFd(0), _size(size_), _name(name_), _isMapped(true) {};
// constructor for a mapped HW address via a hardcoded address value, if device == NULL, it will just assume the
// address passed in is already mapped and will be used as-is, if device != NULL, it will do an mmap to map the
// device and set the address accordingly
MemoryMappedDevice8(const char *name_, unsigned long address_, unsigned size_, const char *device_ = NULL);
~MemoryMappedDevice8();
// get/set whole register values
void setRegister(unsigned register_, uint8_t value_){SET_REGISTER_VALUE(register_, value_);};
uint8_t getRegister(unsigned register_){GET_REGISTER_VALUE(register_);};
// get/set bitfield values
void setBitfield(unsigned register_, unsigned lowOrderBit_, unsigned highOrderBit_, uint8_t value_){SET_REGISTER_BITFIELD8(register_, lowOrderBit_, highOrderBit_, value_);};
uint8_t getBitfield(unsigned register_, unsigned lowOrderBit_, unsigned highOrderBit_){GET_REGISTER_BITFIELD8(register_, lowOrderBit_, highOrderBit_);};
// set an address that is already memory mapped via another method
void setAddress(void *address_){_address = (uint8_t *)address_;};
// return memory mapped device, name, and size
const char *getName(void){return (_name.data());};
const char *getDevice(void){return (_device.data());};
unsigned getSize(void){return (_size);};
// return if memory has been successfully mapped via mmap
bool isMemoryMapped(void){return (_isMapped);};
protected:
// return the memory mapped address at the specified 8-bit offset
volatile uint8_t *getAddress(unsigned offset_ = 0){return (&_address[offset_]);};
private:
volatile uint8_t *_address;
int _memFd;
unsigned _size;
string _name;
string _device;
bool _isMapped;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
inline MemoryMappedDevice8::MemoryMappedDevice8(const char *name_, unsigned long address_, unsigned size_, const char *device_)
{
_memFd = 0;
_size = size_;
_name = name_;
_device = device_;
if (device_ != NULL)
{
// setup our base memory mapped address
_memFd = open(device_, O_RDWR | O_SYNC);
_address = (uint8_t *) mmap(NULL,
size_*sizeof(uint8_t),
PROT_READ | PROT_WRITE,
MAP_SHARED,
_memFd,
address_);
if (_address == MAP_FAILED)
{
printf("ERROR: %s failed to map address: 0x%x, size: %d, on device: %s\n", getName(), (uint32_t)address_, size_, device_);
_isMapped = false;
}
else
{
printf("INFO: %s successfully mapped address: 0x%x, size: %d, on device: %s\n", getName(), (uint32_t)address_, size_, device_);
_isMapped = true;
}
}
else
{
_address = (uint8_t *)address_;
_isMapped = true;
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
inline MemoryMappedDevice8::~MemoryMappedDevice8()
{
if ((_memFd != 0) && (_address != MAP_FAILED))
{
if (munmap((void *)_address, _size) == 0)
{
printf("INFO: %s successfully unmapped memory on device: %s\n", getName(), getDevice());
}
else
{
printf("ERROR: %s failed to unmap memory on device: %s\n", getName(), getDevice());
}
}
}
////////////////////////////////////////////////////////////////////////////////
//
// class definition for 16-bit devices
//
////////////////////////////////////////////////////////////////////////////////
class MemoryMappedDevice16
{
public:
// constructor for a RAM based buffer address pointer
MemoryMappedDevice16(const char *name_, void *address_, unsigned size_) : _address((uint16_t *)address_), _memFd(0), _size(size_), _name(name_), _isMapped(true) {};
// constructor for a mapped HW address via a hardcoded address value, if device == NULL, it will just assume the
// address passed in is already mapped and will be used as-is, if device != NULL, it will do an mmap to map the
// device and set the address accordingly
MemoryMappedDevice16(const char *name_, unsigned long address_, unsigned size_, const char *device_ = NULL);
~MemoryMappedDevice16();
// get/set whole register values
void setRegister(unsigned register_, uint16_t value_){SET_REGISTER_VALUE(register_, value_);};
uint16_t getRegister(unsigned register_){GET_REGISTER_VALUE(register_);};
// get/set bitfield values
void setBitfield(unsigned register_, unsigned lowOrderBit_, unsigned highOrderBit_, uint16_t value_){SET_REGISTER_BITFIELD16(register_, lowOrderBit_, highOrderBit_, value_);};
uint16_t getBitfield(unsigned register_, unsigned lowOrderBit_, unsigned highOrderBit_){GET_REGISTER_BITFIELD16(register_, lowOrderBit_, highOrderBit_);};
// set an address that is already memory mapped via another method
void setAddress(void *address_){_address = (uint16_t *)address_;};
// return memory mapped device, name, and size
const char *getName(void){return (_name.data());};
const char *getDevice(void){return (_device.data());};
unsigned getSize(void){return (_size);};
// return if memory has been successfully mapped via mmap
bool isMemoryMapped(void){return (_isMapped);};
protected:
// return the memory mapped address at the specified 16-bit offset
volatile uint16_t *getAddress(unsigned offset_ = 0){return (&_address[offset_]);};
private:
volatile uint16_t *_address;
int _memFd;
unsigned _size;
string _name;
string _device;
bool _isMapped;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
inline MemoryMappedDevice16::MemoryMappedDevice16(const char *name_, unsigned long address_, unsigned size_, const char *device_)
{
_memFd = 0;
_size = size_;
_name = name_;
_device = device_;
if (device_ != NULL)
{
// setup our base memory mapped address
_memFd = open(device_, O_RDWR | O_SYNC);
_address = (uint16_t *) mmap(NULL,
size_*sizeof(uint16_t),
PROT_READ | PROT_WRITE,
MAP_SHARED,
_memFd,
address_);
if (_address == MAP_FAILED)
{
printf("ERROR: %s failed to map address: 0x%x, size: %d, on device: %s\n", getName(), (uint32_t)address_, size_, device_);
_isMapped = false;
}
else
{
printf("INFO: %s successfully mapped address: 0x%x, size: %d, on device: %s\n", getName(), (uint32_t)address_, size_, device_);
_isMapped = true;
}
}
else
{
_address = (uint16_t *)address_;
_isMapped = true;
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
inline MemoryMappedDevice16::~MemoryMappedDevice16()
{
if ((_memFd != 0) && (_address != MAP_FAILED))
{
if (munmap((void *)_address, _size) == 0)
{
printf("INFO: %s successfully unmapped memory on device: %s\n", getName(), getDevice());
}
else
{
printf("ERROR: %s failed to unmap memory on device: %s\n", getName(), getDevice());
}
}
}
////////////////////////////////////////////////////////////////////////////////
//
// class definition for 32-bit devices
//
////////////////////////////////////////////////////////////////////////////////
class MemoryMappedDevice32
{
public:
// constructor for a RAM based buffer address pointer
MemoryMappedDevice32(const char *name_, void *address_, unsigned size_) : _address((uint32_t *)address_), _memFd(0), _size(size_), _name(name_), _isMapped(true) {};
// constructor for a mapped HW address via a hardcoded address value, if device == NULL, it will just assume the
// address passed in is already mapped and will be used as-is, if device != NULL, it will do an mmap to map the
// device and set the address accordingly
MemoryMappedDevice32(const char *name_, unsigned long address_, unsigned size_, const char *device_ = NULL);
~MemoryMappedDevice32();
// get/set whole register values
void setRegister(unsigned register_, uint32_t value_){SET_REGISTER_VALUE(register_, value_);};
uint32_t getRegister(unsigned register_){GET_REGISTER_VALUE(register_);};
// get/set bitfield values
void setBitfield(unsigned register_, unsigned lowOrderBit_, unsigned highOrderBit_, uint32_t value_){SET_REGISTER_BITFIELD32(register_, lowOrderBit_, highOrderBit_, value_);};
uint32_t getBitfield(unsigned register_, unsigned lowOrderBit_, unsigned highOrderBit_){GET_REGISTER_BITFIELD32(register_, lowOrderBit_, highOrderBit_);};
// set an address that is already memory mapped via another method
void setAddress(void *address_){_address = (uint32_t *)address_;};
// return memory mapped device, name, and size
const char *getName(void){return (_name.data());};
const char *getDevice(void){return (_device.data());};
unsigned getSize(void){return (_size);};
// return if memory has been successfully mapped via mmap
bool isMemoryMapped(void){return (_isMapped);};
protected:
// return the memory mapped address at the specified 32-bit offset
volatile uint32_t *getAddress(unsigned offset_ = 0){return (&_address[offset_]);};
private:
volatile uint32_t *_address;
int _memFd;
unsigned _size;
string _name;
string _device;
bool _isMapped;
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
inline MemoryMappedDevice32::MemoryMappedDevice32(const char *name_, unsigned long address_, unsigned size_, const char *device_)
{
_memFd = 0;
_size = size_;
_name = name_;
_device = device_;
if (device_ != NULL)
{
// setup our base memory mapped address
_memFd = open(device_, O_RDWR | O_SYNC);
_address = (uint32_t *) mmap(NULL,
size_*sizeof(uint32_t),
PROT_READ | PROT_WRITE,
MAP_SHARED,
_memFd,
address_);
if (_address == MAP_FAILED)
{
printf("ERROR: %s failed to map address: 0x%x, size: %d, on device: %s\n", getName(), (uint32_t)address_, size_, device_);
_isMapped = false;
}
else
{
printf("INFO: %s successfully mapped address: 0x%x, size: %d, on device: %s\n", getName(), (uint32_t)address_, size_, device_);
_isMapped = true;
}
}
else
{
_address = (uint32_t *)address_;
_isMapped = true;
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
inline MemoryMappedDevice32::~MemoryMappedDevice32()
{
if ((_memFd != 0) && (_address != MAP_FAILED))
{
if (munmap((void *)_address, _size) == 0)
{
printf("INFO: %s successfully unmapped memory on device: %s\n", getName(), getDevice());
}
else
{
printf("ERROR: %s failed to unmap memory on device: %s\n", getName(), getDevice());
}
}
}
#endif