Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Pre-Lab Assignment: Before this lab session, please prepare the following material: a) Using the UNIX manual pages (man), as well any necessary online documentation, explain

Pre-Lab Assignment: Before this lab session, please prepare the following material: a) Using the UNIX manual pages (man), as well any necessary online documentation, explain the code in functions Initialize(), Finalize(), RegisterRead(), and RegisterWrite().

image text in transcribed

The code is listed Below:

image text in transcribedimage text in transcribed

Pre-Lab Assignment Before this lab session, please prepare the following material: a) Using the UNIX manual pages (man), as well any necessary online documentation, explain the code in functions Initialize(), Finalize(), Register Read(), and Registerwrite(). b) Write a function for controlling each LED individually. The function should have the interface below: The function calls Registerlirite (--) to write the state to the specified LED. /** Changes the state of an LED (ON or OFF) @param pBase base address of I/O * @param ledNum LED number (0 to 7) * @param state State to change to (ON or OFF) void Writel Led (char *pBase, int ledNum, int state); In this function ledNum selects LED to control (0 ... 7) and state controls if LED is off (O), or on (1). For example: WritelLed (pBase, 0, 1); Should turn on LED O. Hints: See the definition of each LEDs offset address (#define at top of the file). What is the address distance between LED offsets, are they constant? Can you compute an offset based on an LED number (0... 7)? Compute first the offset for the desired LED and store it in variable int ledOffset. Write a function analogous to the one above but this time the function should read a switch instead. The function should have the interface below. In this function switchNum selects the switch to read from (0... 7). The function calls RegisterRead (...) to read the specified switch and returns the value read. /** Reads the value of a switch * - Uses base address of I/O * @param pBase base address of I/O * @param switchNum Switch number (0 to 7) * @return Switch value read */ int ReadiSwitch(char *pBase, int switchNum); Submit a single PDF file with your responses and pre-lab code on Blackboard before coming to the lab. Controlling the LEDs The following program is our first attempt to control the devices present in the ZedBoard, starting with the LEDs. We will write a program that controls the LEDs, switches, and push buttons using memory mapped I/O. Study the program below and complete the pre-lab. #include #include #include #include #include using namespace std; 1/ Physical base address of GPIO const unsigned gpio_address = Ox40000000; 1/ Length of memory-mapped IO window const unsigned gpio_size = Oxff: const int gpio_ledi_offset = 0x120; const int gpio_led_offset = 0x130; const int gpio_led_offset = 0x134; const int gpio_led_offset = 0x138; const int gpio_led5_offset = Ox130; const int gpio_led_offset = 0x140; const int gpio_led 7_offset = 0x144; const int gpio_led_offset = 0x148; // Offset for LED1 // Offset for LED2 // Offset for LED3 // Offset for LED4 // Offset for LED5 // Offset for LED // Offset for LED7 // Offset for LEDS const int gpio_swi_offset = 0x14C; const int gpio_sw2_offset = 0x150; const int gpio_sw3_offset - Ox154; const int gpio_sw4_offset = 0x158; const int gpio_sw5_offset = 6x15C; const int gpio_sw6_offset - Ox160; const int gpio_sw7_offset = 8x164; const int gpio_sw8_offset = 8x168; // Offset for Switch 1 // Offset for Switch 2 // Offset for Switch 3 // Offset for Switch 4 // Offset for Switch 5 // Offset for Switch 6 // Offset for Switch 7 // Offset for Switch 8 const int gpio_pbtnl_offset = 0x16C; const int gpio_pbtnr_offset = Ox170; const int gpio_pbtnu_offset = 0x174; const int gpio_pbtnd_offset = 0x178; const int gpio_pbtnc_offset = 0x17C; // Offset for left push button // Offset for right push button // Offset for up push button // Offset for down push button // Offset for center push button * Write a 4-byte value at the specified general-purpose I/0 location. * @param pBase * @parem offset * @param value Base address returned by 'mmap'. Offset where device is mapped. Value to be written. void RegisterWrite(char *pBase, int offset, int value) * (int *) (pBase + offset) = value; * Read a 4-byte value from the specified general-purpose 1/0 location. * @param pBase * @param offset * @return Base address returned by 'mmap'. Offset where device is mapped. Value read. int Register Read(char *pBase, int offset) return * (int *) (pBase + offset); * Initialize general-purpose I/O - Opens access to physical memory /dev/mem * . Maps memory at offset 'gpio_address' into virtual address space * @param fd * @return File descriptor passed by reference, where the result of function 'open' will be stored. Address to virtual memory which is mapped to physical, or MAP_FAILED on error. char *Initialize(int *fd) *fd = open( "/dev/mem", O_RDWR); return (char*)mmap(NULL, gpio_size, PROT_READ *fd, gpio_address); PROT_WRITE, MAP_SHARED, * Close general-purpose I/0. * @param pBase Virtual address where I/O was mapped. * @param fd File descriptor previously returned by 'open'. void Finalize(char *pBase, int fd) munmap (Base, gpio_size); close(fd); int main() // Initialize int fd; char *pBase - Initialize(&fd); // Check error if (pBase == MAP_FAILED) cerr #include #include #include #include using namespace std; 1/ Physical base address of GPIO const unsigned gpio_address = Ox40000000; 1/ Length of memory-mapped IO window const unsigned gpio_size = Oxff: const int gpio_ledi_offset = 0x120; const int gpio_led_offset = 0x130; const int gpio_led_offset = 0x134; const int gpio_led_offset = 0x138; const int gpio_led5_offset = Ox130; const int gpio_led_offset = 0x140; const int gpio_led 7_offset = 0x144; const int gpio_led_offset = 0x148; // Offset for LED1 // Offset for LED2 // Offset for LED3 // Offset for LED4 // Offset for LED5 // Offset for LED // Offset for LED7 // Offset for LEDS const int gpio_swi_offset = 0x14C; const int gpio_sw2_offset = 0x150; const int gpio_sw3_offset - Ox154; const int gpio_sw4_offset = 0x158; const int gpio_sw5_offset = 6x15C; const int gpio_sw6_offset - Ox160; const int gpio_sw7_offset = 8x164; const int gpio_sw8_offset = 8x168; // Offset for Switch 1 // Offset for Switch 2 // Offset for Switch 3 // Offset for Switch 4 // Offset for Switch 5 // Offset for Switch 6 // Offset for Switch 7 // Offset for Switch 8 const int gpio_pbtnl_offset = 0x16C; const int gpio_pbtnr_offset = Ox170; const int gpio_pbtnu_offset = 0x174; const int gpio_pbtnd_offset = 0x178; const int gpio_pbtnc_offset = 0x17C; // Offset for left push button // Offset for right push button // Offset for up push button // Offset for down push button // Offset for center push button * Write a 4-byte value at the specified general-purpose I/0 location. * @param pBase * @parem offset * @param value Base address returned by 'mmap'. Offset where device is mapped. Value to be written. void RegisterWrite(char *pBase, int offset, int value) * (int *) (pBase + offset) = value; * Read a 4-byte value from the specified general-purpose 1/0 location. * @param pBase * @param offset * @return Base address returned by 'mmap'. Offset where device is mapped. Value read. int Register Read(char *pBase, int offset) return * (int *) (pBase + offset); * Initialize general-purpose I/O - Opens access to physical memory /dev/mem * . Maps memory at offset 'gpio_address' into virtual address space * @param fd * @return File descriptor passed by reference, where the result of function 'open' will be stored. Address to virtual memory which is mapped to physical, or MAP_FAILED on error. char *Initialize(int *fd) *fd = open( "/dev/mem", O_RDWR); return (char*)mmap(NULL, gpio_size, PROT_READ *fd, gpio_address); PROT_WRITE, MAP_SHARED, * Close general-purpose I/0. * @param pBase Virtual address where I/O was mapped. * @param fd File descriptor previously returned by 'open'. void Finalize(char *pBase, int fd) munmap (Base, gpio_size); close(fd); int main() // Initialize int fd; char *pBase - Initialize(&fd); // Check error if (pBase == MAP_FAILED) cerr

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions