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.
-
1That's useful to know. It means that if you are using one bit of I/O as a clock, and want to clock on the falling edge, then you probably want to use this code as it, but if you want to clock on the rising edge you probably want a version of this function which performs the clear before the set. Commented Jul 25, 2012 at 10:24
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.
-
-
Not very accurate. The vast majority of GPIO blocks ever made have byte- or word- wide access. Bitwide access is either an optional hardware feature, or something created by driver software (ie, read-modify-write) such the linux GPIO driver. Typically GPIO devices are configured by operations quite comparable to how the outputs are set or inputs are read, so if you can do raw I/O, you can also customize the configuration within the constraints of the external circuit. Commented Oct 7, 2012 at 18:12