Recently, I have started working on embedded system (and I don't have any prior knowledge about it), trying to gain some knowledge in this field. I am working with Raspberry-pi. I am doing bare-metal programming, without any OS. I was reading the data-sheet of bcm2835 and got to know that

Peripherals (at physical address 0x20000000 on) are mapped into the kernel virtual address space starting at address 0xF2000000.

Now, I would like to know that if I can assign any kernel virtual address to a pointer and perform standard pointer operation on it. Like

uint32_t *p = (uint32_t*)0xF20203000; //PCM Base address
p[1] = 1;   // This should be (0xF20203000 + 0x0004) = 0xF20203004
uint32_t value = p[1];

I have seen many code samples for raspberry-pi but the problem is they all are using mmap() function of Linux. Till now, I am doing it in assembly: (Code taken from http://wiki.osdev.org/ARM_RaspberryPi_Tutorial_C#Writing_a_kernel_in_C)

static inline void mmio_write(uint32_t reg, uint32_t data) {
    uint32_t *ptr = (uint32_t*)reg;
    asm volatile("str %[data], [%[reg]]" : : [reg]"r"(ptr), [data]"r"(data));

static inline uint32_t mmio_read(uint32_t reg) {
    uint32_t *ptr = (uint32_t*)reg;
    uint32_t data;
    asm volatile("ldr %[data], [%[reg]]" : [data]"=r"(data) : [reg]"r"(ptr));
    return data;

And if the above operation using pointers can be performed then will it actually write to physical address? What will be the trade-off?


In bare metal programming you are in control of the hardware. You decide how you want memory to work and you decide if you want to program the memory management unit(s) to use virtual addresses.

Terms like kernel space and user space are meaningless for bare metal programming.

The mmap function is only relevant if your program is running in a Linux (or similar) environment.

  • Thanks. Need to clarify one more thing. In bare metal, there is no dynamic memory allocation, right? For this, I have to allocate a large chunk of memory at the time of system startup, like a pool of memory, and use it at runtime. – Arpit Dec 28 '14 at 16:01
  • You should be able to find a bare metal library which provides memory allocation. I'm not a bare metal person so have no personal experience. valvers.com/open-software/raspberry-pi/… looks interesting. – joan Dec 28 '14 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.