In order to control peripheral devices from software, software needs to access I/O area. As for accessing I/O area, CPU use either memmory mapped I/O and port mapped I/O.
| Memory Mapped I/O | Port Mapped I/O |
|---|---|
| Memory and Device share same address bus | Different address space |
| Memory Read/Write to access device | Special instruction to access device(IN/OUT) |
| Most widely used I/O method | was popular x86 old system |
Sample of memory mapped I/O.
void output_gpio(int gpio, int value)
{
int offset, shift;
if (value) // value == HIGH
offset = SET_OFFSET + (gpio/32);
else // value == LOW
offset = CLR_OFFSET + (gpio/32);
shift = (gpio%32);
*(gpio_map+offset) = 1 << shift;
}
int input_gpio(int gpio)
{
int offset, value, mask;
offset = PINLEVEL_OFFSET + (gpio/32);
mask = (1 << gpio%32);
value = *(gpio_map+offset) & mask;
return value;
}