lilzz
lilzz

Reputation: 5413

In Embedded Programming, are we dealing with Virtual address and Physical address?

From Datasheet of a hardware chip,

Peripherals (at physical address 0x20000000 on) are mapped into the kernel virtual 
address

 space starting at address 0xF2000000. Thus a peripheral advertised here at bus address 
0x7Ennnnnn is available in the ARM kenel at virtual address 0xF2nnnnnn.

Then from the Sample code,

  #define BCM2835_PERI_BASE          0x20000000

I thought Programmer are supposed to dealing with Virtual address, the Physical address is hiding behind. The MMU hides the smaller side of physical address and makes the virtual address bigger. The end user/Programmer would dealing with virtual address.

But from above, the sample code clearly used the physical address instead, why?

Sample code

     #define BCM2835_PERI_BASE           0x20000000
    #define GPIO_PADS       (BCM2708_PERI_BASE + 0x00100000)
    #define CLOCK_BASE      (BCM2708_PERI_BASE + 0x00101000)
    #define GPIO_BASE       (BCM2708_PERI_BASE + 0x00200000)
    #define GPIO_TIMER      (BCM2708_PERI_BASE + 0x0000B000)
    #define GPIO_PWM        (BCM2708_PERI_BASE + 0x0020C000)

   static volatile uint32_t *pads ;



 if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem: %s\n", strerror (errno)) ;

       pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;

Upvotes: 3

Views: 395

Answers (1)

doron
doron

Reputation: 28892

Although it is true that a kernel developer works in virtual memory, the developer still needs to worry about physical memory.

The device that you have has a GPIO controller at physical address GPIO_PADS. You want to program the GPIO for which you need to get access to its memory.

The code that you have show calls mmap to take the physical address in GPIO_PADS and gets the MMU to map this into your virtual address space. You now can read and write to the memory directly and do the GPIO programming that you need.

Upvotes: 3

Related Questions