Page 1 of 1

GPIO

Posted: Mon Feb 22, 2010 3:33 pm
by blahartinger
So i've been trying to output a high or a low on some output pins and m code below doesn't seem to work...I have them as functions that are called from the web server. I traced the code and this code definitely runs and then when i hook up a multi meter to J2[39] it seems always high at 3.3V. I am confused.


void relayEnable(int sock, PCSTR url) {

J2[39].function(PINJ2_39_GPIO);

char buffer[80];

J2[39].set();

if (J2[39].read()) {
siprintf(buffer, "Success!");

} else {
siprintf(buffer, "Failed!");
}

writestring(sock, buffer);
}

void relayDisable(int sock, PCSTR url) {


char buffer[80];

J2[39].clr();

if (!J2[39].read()) {
siprintf(buffer, "Success!");

} else {
siprintf(buffer, "Failed!");
}

writestring(sock, buffer);
}

Re: GPIO

Posted: Mon Feb 22, 2010 5:18 pm
by rnixon
I would init the gpio function just once, not every time you make the call.
When you read a pin it has to change itself to input mode, so it is no longer an output.

Re: GPIO

Posted: Mon Feb 22, 2010 6:15 pm
by tod
First, thanks for the question. I was completely unaware of the PinIO class until I saw this question. Anyone else as ignorant as I am can find a technical note on the class here
http://www.netburner.com/downloads/mod5 ... O-R1p1.pdf

First I assume you are using one of the boards required by the PinIO class.

Second (keep in mind I have no idea how experienced you are), is this what your hardware does? That is, when you throw this particular pin high or low, does the hardware then feed back to you a high or low voltage on that pin? I ask just in case you aren't a HW guy but a SW guy and you are thinking of GPIO as if it was a latched state. In other words setting a GPIO pin can have nothing to do with what you read in on that pin.

Re: GPIO

Posted: Tue Feb 23, 2010 11:49 am
by lgitlitz
The main registers for the GPIO peripheral are as follows:
PAR_xxx - Pin Assignment - These registers will control which function is assigned to a pin. A pin must be assigned to GPIO if you want to control it as a GPIO pin.
PDDR_xxx - Pin Data Direction - This group of registers will control whether a pin is configured to be an input or output. They only affect a pin if it is set as a GPIO with the PAR registers.
PODR_xxx - Pin Output - This group of registers hold the value to be driven on a pin if it is set as an output.
PPD_xxx - Port Pin Data - This group of pins shows the actual value of the pins. If the pin is configured as an output then this should be the same value as the corresponding PODR registers, if the pin is an input then the value read here should be whatever is being driven externally onto the pin.

The pins class is meant to simplify using GPIO. It allows simple GPIO without having to figure out all the mappings of these registers. The downside of the simplicity is a slight loss of functionality and a small hit to performance. There are really only 3 core functions of the pins class:
function - this will change the PAR registers to configure the pin to a specific functioon
read - this will set the PDDR to be an input and then read the PPD value of the pin
set - This will set the pin to desired value in the PODR register and then configure the PDDR register to be an output

One of the functions lost in the pins class is the ability to read the current value being driven on an output. The pins class assumes that any read means the user wants the pin now configured as an input and will modify the PDDR register before reading the PPD register. If the current value of the output needs to be known then you must either create another variable to hold this value or read the PODR register. Another loss is functionality is the ability to read or write to multiple pins on the same port in a single instruction. For example the CS pins are all on the same port. This means that a single write to the PODR_CS register can change all the pins values at the same time. The Pins class must right to each of the pins separately. The performance hit is probably close to 10 clocks for pins class vs 1 clock for a GPIO register write, keep in mind this is an estimate and can vary depending on things such as cache. This should not be a problem for simple GPIO such as reading the value of a switch or controlling an LED. It may be a problem if you are doing high speed communication via GPIO bit-banging.

I also wanted to add that I find it easier to simply read or write to the pins in the pin class without using the set or read functions. For example, from the code you posted:
if ( !J2[39].read() ) {
This can be:
if ( !J2[39] ) {
or
if ( J2[39] != 0 ) {
or
int x = J2[39];
if ( !x )

Same goes with writing to a pin:
J2[39].set();
J2[39].clr();
This can be:
J2[39] = 1;
J2[39] = 0;
or
J2[39] = TRUE;
J2[39] = FALSE;

-Larry