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

Update functions.c

parent af555662
......@@ -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){
......
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