Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify devmem2.c to work as a function(s) rather than a main function. This function should be able to work with gpio devices from 0 through

Modify devmem2.c to work as a function(s) rather than a main function. This function should be able to work with gpio devices from 0 through 127. The function should be able to manipulate the gpio registers for a given device. The header gpio-info.h is available for your use. Think about how to handle the gpio registers that work with banks rather than devices, for example GPIO_REVISION and GPIO_CLEARDATAOUT. I might use prototypes similar to int read_gpio_register(int bank, int device, int reg, int *value); int write_gpio_register(int bank, int device, int reg, int value); Write a program that will demonstrate that your functions work as desired.

Devmem2.c code is below:

#include #include #include #include #include #include #include #include #include #include #include #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s] ", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w'; if(argc < 2) { fprintf(stderr, " Usage:\t%s { address } [ type [ data ] ] " "\taddress : memory address to act upon " "\ttype : access operation type : [b]yte, [h]alfword, [w]ord " "\tdata : data to be written ", argv[0]); exit(1); } target = strtoul(argv[1], 0, 0); if(argc > 2) access_type = tolower(argv[2][0]); if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL; printf("/dev/mem opened. "); fflush(stdout); /* Map one page */ map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK); if(map_base == (void *) -1) FATAL; printf("Memory mapped at address %p. ", map_base); fflush(stdout); virt_addr = (void *)((char *)map_base + (target & MAP_MASK)); switch(access_type) { case 'b': read_result = *((unsigned char *) virt_addr); break; case 'h': read_result = *((unsigned short *) virt_addr); break; case 'w': read_result = *((unsigned long *) virt_addr); break; default: fprintf(stderr, "Illegal data type '%c'. ", access_type); exit(2); } printf("Value at address 0x%X (%p): 0x%X ", target, virt_addr, read_result); fflush(stdout); if(argc > 3) { writeval = strtoul(argv[3], 0, 0); switch(access_type) { case 'b': *((unsigned char *) virt_addr) = writeval; read_result = *((unsigned char *) virt_addr); break; case 'h': *((unsigned short *) virt_addr) = writeval; read_result = *((unsigned short *) virt_addr); break; case 'w': *((unsigned long *) virt_addr) = writeval; read_result = *((unsigned long *) virt_addr); break; } printf("Written 0x%X; readback 0x%X ", writeval, read_result); fflush(stdout); } if(munmap(map_base, MAP_SIZE) == -1) FATAL; close(fd); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3. Identify challenges to good listening and their remedies

Answered: 1 week ago

Question

4. Identify ethical factors in the listening process

Answered: 1 week ago