I have a basic understanding of the GPIO on the RPi. I want to adapt some of my previous software which interfaced my project board via the Parallel port via direct output to the PP address (0x378) using Outb in C. Can I directly access the GPIO using Outb in C (ofc with a different port address).
2 Answers
outb writes a byte to an x86 IO port, whereas most other architectures don't have this concept. With the Pi, the GPIOs can be accessed as a memory location, and there is some sample code for doing this in C.
You would need to take this code and write your own implementation of the outb() function, which takes the data and writes it to the GPIO pins you want to use. This could be very simple. If you just write the byte directly to the GPIO memory location, you will have eight GPIOs update immediately with the same state you would have gotten on the parallel port. Something like this ought to work:
void outb_pi(unsigned char val)
{
// Set all the bits in val that are 1
GPIO_SET = val;
// Clear all the bits in val that are 0
GPIO_CLR = ~val;
}
The only problem here is that this may not update the GPIO pins you want (it may even update some GPIO pins that aren't connected - I'm not sure of the mapping) so you may need to alter the function so the pins you want are the ones used.
You will also get an intermediate value appearing momentarily on the GPIO pins (when the number is half set) which may or may not be an issue. Looking at the Broadcom datasheet it doesn't look like there's a way to both set and clear GPIO pins at the same time.
- 3,372
- 5
- 21
- 33
- 2,109
- 15
- 25
outb has a hardware-specific implementation, which is traditionally implemented as a single x86 instruction. Furthermore, it is a byte-wide instruction where GPIO access is normally bit-wide on the RPi.
Having said all that, you could write your own implementation of outb for the Pi, which would allow you port your code, but it would have to assume a certain configuration of the GPIO.
- 15,638
- 15
- 69
- 113