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
b44dcd03
Commit
b44dcd03
authored
Dec 09, 2020
by
Gaia Grosso
Browse files
Update functions.c
parent
af555662
Changes
1
Hide whitespace changes
Inline
Side-by-side
functions.c
View file @
b44dcd03
...
...
@@ -27,12 +27,12 @@
#define htols(x) __bswap_16(x)
#endif
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
exit(1);
} while(0)
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno)); } while(0)
#define MAP_SIZE (32*1024UL)
#define MAP_MASK (MAP_SIZE - 1)
void
write_bits
(
char
*
device_loc
,
char
*
address
,
unsigned
int
writeval
)
int
write_bits
(
char
*
device_loc
,
char
*
address
,
unsigned
int
writeval
)
{
char
*
device
=
strdup
(
device_loc
);
off_t
target
=
strtoul
(
address
,
0
,
0
);
...
...
@@ -41,15 +41,19 @@ void write_bits(char *device_loc, char *address, unsigned int writeval)
/* open device */
int
fd
=
open
(
device_loc
,
O_RDWR
|
O_SYNC
));
if
((
fd
==
-
1
)
if
((
fd
==
-
1
)
{
FATAL
;
return
-
1
;
}
printf
(
"character device %s opened.
\n
"
,
device_loc
);
fflush
(
stdout
);
/* map one page */
void
*
map_base
=
mmap
(
0
,
MAP_SIZE
,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
,
fd
,
0
);
if
(
map_base
==
(
void
*
)
-
1
)
if
(
map_base
==
(
void
*
)
-
1
)
{
FATAL
;
return
-
1
;
}
printf
(
"Memory mapped at address %p.
\n
"
,
map_base
);
fflush
(
stdout
);
...
...
@@ -65,9 +69,10 @@ void write_bits(char *device_loc, char *address, unsigned int writeval)
writeval
=
htoll
(
writeval
);
*
((
uint32_t
*
)
virt_addr
)
=
writeval
;
fflush
(
stdout
);
return
0
;
};
unsigned
int
read_bits
(
char
*
device_loc
,
char
*
address
)
int
read_bits
(
char
*
device_loc
,
char
*
address
,
uint32_t
*
read_result
)
{
char
*
device
=
strdup
(
device_loc
);
off_t
target
=
strtoul
(
address
,
0
,
0
);
...
...
@@ -76,15 +81,19 @@ unsigned int read_bits(char *device_loc, char *address)
/* open device */
int
fd
=
open
(
device_loc
,
O_RDWR
|
O_SYNC
));
if
((
fd
==
-
1
)
if
((
fd
==
-
1
)
{
FATAL
;
return
-
1
;
}
printf
(
"character device %s opened.
\n
"
,
device_loc
);
fflush
(
stdout
);
/* map one page */
void
*
map_base
=
mmap
(
0
,
MAP_SIZE
,
PROT_READ
|
PROT_WRITE
,
MAP_SHARED
,
fd
,
0
);
if
(
map_base
==
(
void
*
)
-
1
)
if
(
map_base
==
(
void
*
)
-
1
)
{
FATAL
;
return
-
1
;
}
printf
(
"Memory mapped at address %p.
\n
"
,
map_base
);
fflush
(
stdout
);
...
...
@@ -92,17 +101,18 @@ unsigned int read_bits(char *device_loc, char *address)
void
*
virt_addr
=
map_base
+
target
;
/* Read */
uint32_t
read_result
=
*
((
uint32_t
*
)
virt_addr
);
uint32_t
read_result
_tmp
=
*
((
uint32_t
*
)
virt_addr
);
/* swap 32-bit endianess if host is not little-endian */
read_result
=
ltohl
(
read_result
);
read_result
_tmp
=
ltohl
(
read_result
_tmp
);
printf
(
"Read 32-bit value at address 0x%08x (%p): 0x%08x
\n
"
,
(
unsigned
int
)
target
,
virt_addr
,
(
unsigned
int
)
read_result
);
(
unsigned
int
)
read_result
_tmp
);
fflush
(
stdout
);
return
(
unsigned
int
)
read_result
;
*
read_result
=
read_result_tmp
;
return
0
;
};
void
printBits
(
unsigned
int
num
){
...
...
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