2

Can any one explains me clearly, what is mapping? Port mapping? Memory mapping? In firmware development.

I gone through many other sites, still the question is not clear.

Asked in an interview for Firmware Developer.

The actual question is "How does you access data holding in the register/memory of a controller using C?" (and he given clue do you know memory mapped I/O, port mapped I/O? like that.

I understood the question may be like this,

If there is data in SPI/I2C/ADC such memory buffers of a microcontroller, how do you access that data? The question asked by Graphene Semiconductors.

thank you

  • You should give more context about where you've encountered this term. Mapping just means looking a value up in some sort of table or directory in order to get another value. What sort of microcontroller are your talking about, and what are you trying to accomplish with it? – Ross Ridge May 27 '15 at 05:19
  • Jus i am a learner in this topic, some wherein the interview i asked by this question. I am asking in general terms. You may consider Atmega128 controller – Lokesh V Gowda May 27 '15 at 05:31
  • What was the actual question you were asked? – Ross Ridge May 27 '15 at 05:32
  • the actual question is "How does you access data holding in the register/memory using C?" (and he given clue do you know memory mapped I/O, port mapped I/O? like that. The question asked by Graphene Semiconductors. – Lokesh V Gowda May 27 '15 at 05:42

3 Answers3

3

Memory mapped I/O allows writing/reading to I/O devices the same as reading/writing to normal memory (using the same machine code/asm). You use up physical memory address space for your memory mapped I/O devices.

Usually there is some address decoding logic between CPU and RAM so when you hit a memory location which belongs to an I/O device the address decoding logic kicks in and connect the CPU address lines to that I/O device (instead of RAM).

It's a neat way to access I/O but it consumes memory space.

Port mapping I/O allows writing/reading to I/O devices using special asm instructions (in and out in x86 assembly). You don't consume memory address space.

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
  • thank you for your answer, can you please brief me, how can we achieve this on microcontroller using C program? – Lokesh V Gowda May 27 '15 at 11:18
  • That latter would be a special IO-address space (somewhat an extension of Harvard-Architecture for another special address space), not port-mapped IO. Port mapped IO is like using a GPIO to e.g. access a DAC, simulating a bus (with software-defined address space), or through SPI/i2C or a QSPI-Flash which is not traqnparently integrated in the normal address spaces and requires an intermediate driver for the interface. – too honest for this site May 27 '15 at 20:48
  • @G_G "achieve what?" How to map memory using C? – Lokesh V Gowda May 28 '15 at 05:50
  • 1
    @Lokesh: you don't "map memory" in C. Memory mapping is something you define in your electrical wiring. You design the address decoding logic to let the CPU access RAM and I/O devices. In C you just access memory locations and if you access some memory space belonging to an I/O device you end up reading/writing to that I/O device instead of RAM. – Gianluca Ghettini May 28 '15 at 08:59
  • I'm uncertain about `You use up physical memory address space for your memory mapped I/O devices.`. Maybe I'm wrong, but I thought one may map some address space interval to I/O device and that address space may be even not a physical memory, e.g. MCU has 10kB of physical memory space by maps addresses of 10kB-20kB interval to its peripheral devices. Am I wrong? – Sergey Kanaev May 28 '15 at 15:02
  • You're right! Physical memory address space is actually related to the number of physical address lines of the CPU. So if the CPU has got 16 address lines you can address 64K memory location (I'm not talking about RAM memory locations, just abstract locations). We can either use a 64K RAM (using all the 16 lines) or use a 32K RAM and an I/O device which has 32K registers. Basically we are using 1 CPU address line out of the 16 to CS'd (chip select) either the RAM or the I/O device. That 1 line is your (very simple) address decoding logic. – Gianluca Ghettini May 28 '15 at 15:47
  • comment cont. - So as you can see we can implement memory mapped I/O without "stealing" RAM space. Obviously we always "steal" CPU address space. – Gianluca Ghettini May 28 '15 at 15:50
1

Port mapping and memory mapping are totally different. Port mapping is to assign which port to use. For example, which port you want to use for serial communication, UART1 or UART2 or USB?

Memory mapped IO shares same memory address space for IO(same space but unique address). But, Memory isolated IO has separated space for memory & IO.

*gpio_for_led = 1; // mem mapped io

gpio_for_led = 0x1234; //isolated io(assembly code has in, out instruction)

outportb(gpio_for_led, 1);

Does this make you clear?

Bobby Ahn
  • 11
  • 4
0

For port mapped IO you need an extra driver for the interface. That would define completely how to address the external device. It can be as simple as a GPIO just driving an external register with a strobe, or SPI. All this is mostly software-defined or given by the interface. However, it has no relation to the CPU address and data bus.

Memory mapped IO is just as if the register was a variable in the normal address space. However, you need to ensure the compiler does neither optimize away accesses, nor reorder them (either to the same variable or to other such registers). As the address for such a register is fixed by the hardware, one might either set that by the linker(-script) or as follows (for a 16 bit read/write register at address 0x1234):

#define REGX (*(volatile uint16_t *)0x1234)
too honest for this site
  • 12,050
  • 4
  • 30
  • 52