Commit acbe93ae authored by Gaia Grosso's avatar Gaia Grosso
Browse files

Update functions.c

parent 0aeea688
......@@ -32,71 +32,83 @@
#define MAP_SIZE (32*1024UL)
#define MAP_MASK (MAP_SIZE - 1)
int write_bits(char *device_loc, char *address, unsigned int writeval)
int write_bits(char *device_loc, char *address, unsigned int writeval, int verbose)
{
char *device = strdup(device_loc);
off_t target = strtoul(address, 0, 0);
if(verbose>0){
printf("device: %s\n", device);
printf("address: 0x%08x\n", (unsigned int)target);
fflush(stdout);
}
/* open device */
int fd = open(device_loc, O_RDWR | O_SYNC);
if (fd == -1) {
FATAL;
return -1;
}
if(verbose>0){
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) {
FATAL;
close(fd);
return -1;
}
if(verbose>0){
printf("Memory mapped at address %p.\n", map_base);
fflush(stdout);
}
/* calculate the virtual address to be accessed */
void *virt_addr = map_base + target;
/* Write */
if(verbose>0){
printf("Write 32-bits value 0x%08x to 0x%08x (0x%p)\n",
(unsigned int)writeval, (unsigned int)target,
virt_addr);
fflush(stdout);
}
/* swap 32-bit endianess if host is not little-endian */
writeval = htoll(writeval);
*((uint32_t *) virt_addr) = writeval;
fflush(stdout);
close(fd);
return 0;
};
int read_bits(char *device_loc, char *address, uint32_t *read_result)
int read_bits(char *device_loc, char *address, uint32_t *read_result, int verbose)
{
char *device = strdup(device_loc);
off_t target = strtoul(address, 0, 0);
if(verbose>0){
printf("device: %s\n", device);
printf("address: 0x%08x\n", (unsigned int)target);
fflush(stdout);
}
/* open device */
int fd = open(device_loc, O_RDWR | O_SYNC);
if ( fd == -1){
FATAL;
return -1;
}
if(verbose>0){
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){
FATAL;
close(fd);
return -1;
}
if(verbose>0){
printf("Memory mapped at address %p.\n", map_base);
fflush(stdout);
}
/* calculate the virtual address to be accessed */
void *virt_addr = map_base + target;
......@@ -105,13 +117,15 @@ int read_bits(char *device_loc, char *address, uint32_t *read_result)
/* swap 32-bit endianess if host is not little-endian */
read_result_tmp = ltohl(read_result_tmp);
if(verbose>0){
printf
("Read 32-bit value at address 0x%08x (%p): 0x%08x\n",
(unsigned int)target, virt_addr,
(unsigned int)read_result_tmp);
fflush(stdout);
}
*read_result = read_result_tmp;
close(fd);
return 0;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment