Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
scouting-demonstrator
SCONE
Commits
feb50c57
Commit
feb50c57
authored
Nov 30, 2020
by
Gaia Grosso
Browse files
modifications for merge request
parent
2197823c
Changes
1
Hide whitespace changes
Inline
Side-by-side
functions.c
View file @
feb50c57
...
...
@@ -32,73 +32,35 @@
#define MAP_SIZE (32*1024UL)
#define MAP_MASK (MAP_SIZE - 1)
void
write_bits
(
char
*
device_loc
,
char
*
address
,
char
*
width
,
unsigned
int
writeval
);
void
write_bits
(
char
*
device_loc
,
char
*
address
,
char
*
width
,
unsigned
int
writeval
)
void
write_bits
(
char
*
device_loc
,
char
*
address
,
unsigned
int
writeval
)
{
int
fd
;
void
*
map_base
,
*
virt_addr
;
off_t
target
;
int
access_width
;
access_width
=
tolower
(
width
[
0
]);
char
*
device
;
device
=
strdup
(
device_loc
);
char
*
device
=
strdup
(
device_loc
);
off_t
target
=
strtoul
(
address
,
0
,
0
);
printf
(
"device: %s
\n
"
,
device
);
target
=
strtoul
(
address
,
0
,
0
);
printf
(
"address: 0x%08x
\n
"
,
(
unsigned
int
)
target
);
printf
(
"access width: "
);
if
(
access_width
==
'b'
)
printf
(
"byte (8-bits)
\n
"
);
else
if
(
access_width
==
'h'
)
printf
(
"half word (16-bits)
\n
"
);
else
if
(
access_width
==
'w'
)
printf
(
"word (32-bits)
\n
"
);
else
{
printf
(
"word (32-bits)
\n
"
);
access_width
=
'w'
;
}
if
((
fd
=
open
(
device_loc
,
O_RDWR
|
O_SYNC
))
==
-
1
)
/* open device */
int
fd
=
open
(
device_loc
,
O_RDWR
|
O_SYNC
));
if
((
fd
==
-
1
)
FATAL
;
printf
(
"character device %s opened.
\n
"
,
device_loc
);
fflush
(
stdout
);
/* map one page */
map_base
=
mmap
(
0
,
MAP_SIZE
,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
,
fd
,
0
);
void
*
map_base
=
mmap
(
0
,
MAP_SIZE
,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
,
fd
,
0
);
if
(
map_base
==
(
void
*
)
-
1
)
FATAL
;
printf
(
"Memory mapped at address %p.
\n
"
,
map_base
);
fflush
(
stdout
);
/* calculate the virtual address to be accessed */
virt_addr
=
map_base
+
target
;
void
*
virt_addr
=
map_base
+
target
;
/* Write */
switch
(
access_width
)
{
case
'b'
:
printf
(
"Write 8-bits value 0x%02x to 0x%08x (0x%p)
\n
"
,
(
unsigned
int
)
writeval
,
(
unsigned
int
)
target
,
virt_addr
);
*
((
uint8_t
*
)
virt_addr
)
=
writeval
;
#if 0
read_result = *((uint8_t *) virt_addr);
printf("Written 0x%02x; readback 0x%02x\n", writeval, read_result);
#endif
break
;
case
'h'
:
printf
(
"Write 16-bits value 0x%04x to 0x%08x (0x%p)
\n
"
,
(
unsigned
int
)
writeval
,
(
unsigned
int
)
target
,
virt_addr
);
/* swap 16-bit endianess if host is not little-endian */
writeval
=
htols
(
writeval
);
*
((
uint16_t
*
)
virt_addr
)
=
writeval
;
#if 0
read_result = *((uint16_t *) virt_addr);
printf("Written 0x%04x; readback 0x%04x\n", writeval, read_result);
#endif
break
;
case
'w'
:
printf
(
"Write 32-bits value 0x%08x to 0x%08x (0x%p)
\n
"
,
(
unsigned
int
)
writeval
,
(
unsigned
int
)
target
,
virt_addr
);
printf
(
"Write 32-bits value 0x%08x to 0x%08x (0x%p)
\n
"
,
(
unsigned
int
)
writeval
,
(
unsigned
int
)
target
,
virt_addr
);
/* swap 32-bit endianess if host is not little-endian */
writeval
=
htoll
(
writeval
);
*
((
uint32_t
*
)
virt_addr
)
=
writeval
;
...
...
@@ -106,89 +68,48 @@ void write_bits(char *device_loc, char *address, char *width, unsigned int write
read_result = *((uint32_t *) virt_addr);
printf("Written 0x%08x; readback 0x%08x\n", writeval, read_result);
#endif
break
;
}
fflush
(
stdout
);
};
unsigned
int
read_bits
(
char
*
device_loc
,
char
*
address
,
char
*
width
);
unsigned
int
read_bits
(
char
*
device_loc
,
char
*
address
,
char
*
width
)
unsigned
int
read_bits
(
char
*
device_loc
,
char
*
address
)
{
uint32_t
read_result
;
int
fd
;
void
*
map_base
,
*
virt_addr
;
off_t
target
;
int
access_width
;
access_width
=
tolower
(
width
[
0
]);
char
*
device
;
device
=
strdup
(
device_loc
);
char
*
device
=
strdup
(
device_loc
);
off_t
target
=
strtoul
(
address
,
0
,
0
);
printf
(
"device: %s
\n
"
,
device
);
target
=
strtoul
(
address
,
0
,
0
);
printf
(
"address: 0x%08x
\n
"
,
(
unsigned
int
)
target
);
printf
(
"access width: "
);
if
(
access_width
==
'b'
)
printf
(
"byte (8-bits)
\n
"
);
else
if
(
access_width
==
'h'
)
printf
(
"half word (16-bits)
\n
"
);
else
if
(
access_width
==
'w'
)
printf
(
"word (32-bits)
\n
"
);
else
{
printf
(
"word (32-bits)
\n
"
);
access_width
=
'w'
;
}
if
((
fd
=
open
(
device_loc
,
O_RDWR
|
O_SYNC
))
==
-
1
)
/* open device */
int
fd
=
open
(
device_loc
,
O_RDWR
|
O_SYNC
));
if
((
fd
==
-
1
)
FATAL
;
printf
(
"character device %s opened.
\n
"
,
device_loc
);
fflush
(
stdout
);
/* map one page */
map_base
=
mmap
(
0
,
MAP_SIZE
,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
,
fd
,
0
);
void
*
map_base
=
mmap
(
0
,
MAP_SIZE
,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
,
fd
,
0
);
if
(
map_base
==
(
void
*
)
-
1
)
FATAL
;
printf
(
"Memory mapped at address %p.
\n
"
,
map_base
);
fflush
(
stdout
);
/* calculate the virtual address to be accessed */
virt_addr
=
map_base
+
target
;
void
*
virt_addr
=
map_base
+
target
;
/* Read */
switch
(
access_width
)
{
case
'b'
:
read_result
=
*
((
uint8_t
*
)
virt_addr
);
printf
(
"Read 8-bits value at address 0x%08x (%p): 0x%02x
\n
"
,
(
unsigned
int
)
target
,
virt_addr
,
(
unsigned
int
)
read_result
);
break
;
case
'h'
:
read_result
=
*
((
uint16_t
*
)
virt_addr
);
/* swap 16-bit endianess if host is not little-endian */
read_result
=
ltohs
(
read_result
);
printf
(
"Read 16-bit value at address 0x%08x (%p): 0x%04x
\n
"
,
(
unsigned
int
)
target
,
virt_addr
,
(
unsigned
int
)
read_result
);
break
;
case
'w'
:
read_result
=
*
((
uint32_t
*
)
virt_addr
);
/* swap 32-bit endianess if host is not little-endian */
read_result
=
ltohl
(
read_result
);
printf
(
"Read 32-bit value at address 0x%08x (%p): 0x%08x
\n
"
,
(
unsigned
int
)
target
,
virt_addr
,
(
unsigned
int
)
read_result
);
break
;
default:
fprintf
(
stderr
,
"Illegal data type '%c'.
\n
"
,
access_width
);
exit
(
2
);
}
uint32_t
read_result
=
*
((
uint32_t
*
)
virt_addr
);
/* swap 32-bit endianess if host is not little-endian */
read_result
=
ltohl
(
read_result
);
printf
(
"Read 32-bit value at address 0x%08x (%p): 0x%08x
\n
"
,
(
unsigned
int
)
target
,
virt_addr
,
(
unsigned
int
)
read_result
);
fflush
(
stdout
);
return
(
unsigned
int
)
read_result
;
};
void
printBits
(
unsigned
int
num
);
void
printBits
(
unsigned
int
num
){
unsigned
int
size
=
sizeof
(
unsigned
int
);
unsigned
int
maxPow
=
1
<<
(
size
*
8
-
1
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment