1
\$\begingroup\$

I have a question about how to use a basic GPIO function from the TIVA driver library to set an output pin high. See the below image for more detail.

The red underline on the code on the left hand uses GPIOPinWrite. On the right is the datasheet for the driver library which shows a description of the function.

Can someone explain how this sets pin 2 high? I understand the first two values passed into the function but not how the last Ui8Val sets the pin high?

EDIT

I see in the GPIO header file that GPIO_PIN_2 is defined as 0x04.

Thanks!

enter image description here

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

https://github.com/yuvadm/tiva-c/blob/master/driverlib/gpio.c

//The pin(s) are specified using a bit-packed byte, where each bit that is
//! set identifies the pin to be accessed, and where bit 0 of the byte
//! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.

The first parameter points to which port you will be writing. The second one ui8Pins is like a mask, such that the function only affects the specified pins (bits of ui8Pins that are 1), for those 1 bits ui8Pins the bits in the same position of ui8Val (0 or 1) will be written to the the port.

This masking of ui8Pins is actually part of the architecture of the Tiva, the function you are using will write to an address like this

(*((volatile ulong *)(ui32Port + (GPIO_O_DATA + (ui8Pins << 2))))) = ui8Val;

and the (ui8Pins << 2) will not really address a "memory" but bitmask ui8Val so that not all of its bits change the values in ui32Port + GPIO_O_DATA which would be the GPIO data (each pin being high or low).

GPIO DataThis register is virtually mapped to 256 locations in the address space.
To facilitate the reading and writing of data to these registers byindependent 
drivers, the data read from and written to the registers aremasked by the 
eight address lines [9:2]. Reads from this register return its current state. 
Writes to this register only affect bits that are not masked by ADDR[9:2] and are 
configured as outputs. See “Data RegisterOperation” on page 654 for examples 
of reads and writes.

In most other microcontroller the process a masking and writing can be done as such

(*((volatile ulong *)(ui32Port + (GPIO_O_DATA)))) = (~ui8Pins & *(ui32Port + (GPIO_O_DATA))) +(ui8Pins & ui8Val);
\\for all the bits that are zero in ui8Pins, the values in the memory to which you are writing should remain the same
(~ui8Pins & *(ui32Port + (GPIO_O_DATA)));
\\for the bits that are one in ui8Pins use the same bits in ui8Val
(GPIO_O_DATA))) +(ui8Pins & ui8Val);
\$\endgroup\$
2
  • \$\begingroup\$ Ah ok, but in the example code to clear the pin they write a hex value of 0 to Ui8Val. So when they set a pin high by writing GPIO_PIN_2 to Ui8Val, how does this write a 1? \$\endgroup\$
    – David777
    May 21, 2020 at 20:45
  • 1
    \$\begingroup\$ in both cases you should have ui8Pins as GPIO_PIN_2 for it to work, to set it you also have ui8Val equal to GPIO_PIN_2 (or even 0xFF), because you will have the bit that represent pin 2 in ui8Pins and you will have a 1 bit at the same bit position in ui8Val. When clearing the pin (setting it to low) you use ui8Pins as GPIO_PIN_2 and ui8Val as 0x00 (but is could be any hex as long as the bit representing pin 2 in ui8Val is 0). \$\endgroup\$
    – jDAQ
    May 21, 2020 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.