EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous Topic #Index #Next Topic

In our previous C code we were reading and writing to physical address directly to 0x10000000. This is because ADS project has MMU programmed in flat segments where virtual address is directly mapping to corresponding physical address. That is why we did not encounter any MMU fault.

Now we cannot do this same if we are running in Linux or Windows. OS runs in protected paging mode of MMU where each process has translation tables named as page directory and child page tables. So physical address is never mapped to virtual unless we do it explicitly. Usermode program can map unmapped physical address my /dev/mem device file. This device gives mmap() operations to map physical pages to user address space. mmap() takes physical offset , size and read/write protection flags and uses remap_pfn_range() kernel call to map pages in the page table in device driver .

Once mapping is done with mmap() this user mode program will look same as ADS project code. Only difference is interrupt from EINT3 will be suppressed/masked as prom user mode there is no way to handle it.

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 
#include <errno.h> 
#include <signal.h> 
#include <fcntl.h> 
#include <ctype.h> 
#include <termios.h> 
10  #include <sys/types.h> 
11  #include <sys/mman.h> 
13  #define LEDBASE 0x10000000
14  #define MAP_SIZE 4096UL 
17  int main(int argc, char **argv) { 
18     int fd; 
19     void *map_base;
20     unsigned char *led_val;
21     off_t offset; 
23      offset = LEDBASE; 
25      if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1) {
27      printf ("/dev/mem not opened.\n");
28      return -1;
29    }
31      map_base = mmap (0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); 
32      if (map_base == (void *) -1) {
33      close(fd);
34      printf ("/dev/mem mapping failed.\n");
35    }; 
36    led_val = map_base;
37    while(1)
38    {
39      printf ("LED is %s \n", (*led_val & 1) ? "On""off");
40      fflush (stdout); 
41      sleep(1);
42      printf ("Changing LED Status\n");
43      fflush (stdout); 
44      *led_val ^= 1;
45      sleep(1);
47    }
49    if (munmap(map_base, MAP_SIZE) == -1) {
50      printf("unmap failed\n");
51    }
52    close (fd); 
53    return 0; 
54  }

You have viewed 1 page out of 14. Your learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

Altera FPGA interfacing ARM, ARM CPU internals, ARM CPU, Cache Memory and MMU, ARM bus hierarchy, ARM data, address and control lines, ARM MMIO read, ARM MMIO write, FPGA interfacing with system bus, VHDL/Verilog program, ADS C Code, Linux Direct MMIO App, Linux Device Driver, Linux Application code, Final Demo,

* #1 webmaster Tue 15 Apr/2014 04:47:58(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.equestionanswers.com. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.