Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <sys/mman.h>
  3. #include <unistd.h>
  4. #include <cstdio>
  5. #include <iostream>
  6.  
  7. #include "Zedboard.h"
  8.  
  9. Zedboard::Zedboard() {
  10.  
  11.    // Length of memory-mapped IO window
  12.     const unsigned gpio_size = 0xff;
  13.    
  14.    // Physical base address of GPIO
  15.     const unsigned gpio_address = 0x400d0000;
  16.                    
  17.     fd = open("/dev/mem", O_RDWR);
  18.     ptr = (char *)mmap(NULL, gpio_size,
  19.         PROT_READ | PROT_WRITE, MAP_SHARED, fd,gpio_address);
  20.     if (ptr == MAP_FAILED) {
  21.         perror("Mapping I/O memory failed - Did you run with 'sudo'?\n");
  22.   }
  23. }
  24.  
  25. Zedboard::~Zedboard() {
  26.   // Length of memory-mapped IO window
  27.     const unsigned gpio_size = 0xff;              
  28.     munmap(ptr, gpio_size);
  29.     close(fd);
  30. }  
  31. void Zedboard::RegisterWrite(int offset, int value){
  32.     *(int *)(ptr + offset) = value;
  33.         }
  34. int Zedboard::RegisterRead(int offset){
  35.     return *(int *)(ptr + offset);
  36.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement