rasProject_01 / weSweetHome
R. 240
process control / RasPi software by weinert-automation
|
IO functions for Raspberry Pis. More...
Macros | |
#define | PI_PUD_DEFAULT |
Leave pull resistor setting as defaulted. More... | |
#define | PI_PUD_KEEP |
Leave pull resistor setting unchanged. More... | |
#define | PINig |
Defines a disabled GPIO. More... | |
Functions | |
uint8_t | gpio4pin (int const pin) |
Pin number to GPIO number lookup. More... | |
void | initAsDrive (int thePi, unsigned gpio, unsigned init) |
Initialise a GPIO pin as output. More... | |
void | initAsHiDrive (int thePi, unsigned gpio, unsigned init) |
Initialise a GPIO pin as high drive. More... | |
void | initAsHiInput (int thePi, unsigned gpio) |
Initialise a GPIO pin as input with pull down. More... | |
void | initAsInput (int thePi, unsigned gpio) |
Initialise a GPIO pin as input. More... | |
void | initAsInputs (unsigned const lesGPIOs[]) |
Initialise one or more GPIO pin as input. More... | |
void | initAsLoInput (int thePi, unsigned gpio) |
Initialise a GPIO pin as input with pull up. More... | |
uint32_t | initAsOutput (int thePi, unsigned gpio) |
Initialise a GPIO pin as output. More... | |
uint32_t | initAsOutputs (unsigned const lesGPIOs[]) |
Make one or more GPIO pins output. More... | |
uint8_t | pin4GPIO (int const gpio) |
GPIO number to pin number lookup. More... | |
uint32_t | releaseOutputs (int const thePi) |
Release all GPIO pins set as output. More... | |
uint32_t | releaseOutputsReport (int const thePi) |
Release all GPIO pins set as output with report. More... | |
void | reportPinOp (char const *op, unsigned const lesGpio[]) |
Report an arbitrary operation on a list of GPIOs. More... | |
void | setOutput (unsigned const gpio, unsigned const level) |
Set a GPIO output pin. More... | |
void | setOutputs (uint32_t const lesOuts, unsigned const level) |
Set a list/mask of GPIO output pins. More... | |
void | setPadStrength (int thePi, unsigned mA) |
Set the output drive capacity of GPIO ports 0..27. More... | |
Variables | |
uint32_t const | gpio2bit [36] |
GPIO number to bank pin number lookup. More... | |
uint8_t const | gpio2pin [] |
GPIO number to pin number lookup. More... | |
uint8_t const | pin2gpio [44] |
Pin number to GPIO number lookup. More... | |
char const | pudTxt [5][6] |
Names for input's pull resistor settings. | |
int | thePi |
The standard Pi for gpio(d) IO of the program. More... | |
IO functions for Raspberry Pis.
Revision history
The IO functions will work with the gpio/gpiod library – daemon running – as defined in pigpiod_if2.h.
This is a supplementary basic library to be used in conjunction with the pigpio library (linked by: -lpigpiod_if2 -lrt).
#define PI_PUD_KEEP |
Leave pull resistor setting unchanged.
This is an illegal value (4) just one above the legal ones (0..2). Its purpose is to state the current whatever pull setting shall be left untouched.
#define PI_PUD_DEFAULT |
Leave pull resistor setting as defaulted.
This is an illegal value (3) just one above the legal ones (0..2). Its purpose is to state that an otherwise defined default value shall be used. If no such default value is known, the current pull setting shall be left untouched.
#define PINig |
Defines a disabled GPIO.
This is an illegal GPIO value (33) which some IO functions recognise as "disabled". Hence they can be just called in the normal program flow doing no I/O on a disabled port.
uint8_t pin4GPIO | ( | int const | gpio | ) |
GPIO number to pin number lookup.
gpio | a GPIO number (>= 0) available on the Pi's 40 respectively 26 pins IO connector |
uint8_t gpio4pin | ( | int const | pin | ) |
Pin number to GPIO number lookup.
pin | 1..40 (26) is the legal IO connector pin number |
void initAsInputs | ( | unsigned const | lesGPIOs[] | ) |
Initialise one or more GPIO pin as input.
This functions sets the pins listed as GPIOs to input mode. This is also used to release them from any output modes as input means hi impedandance.
The Pi for the IO operation is thePi.
lesGPIOs | array of GPIO numbers (0..53); use 0x7F as end marker |
void reportPinOp | ( | char const * | op, |
unsigned const | lesGpio[] | ||
) |
uint32_t initAsOutputs | ( | unsigned const | lesGPIOs[] | ) |
Make one or more GPIO pins output.
This functions sets the pins listed as GPIOs to output mode.
Normally, at program end, the same list of outputs should be released to high impedance by initAsInputs().
The Pi for the IO operations is thePi.
lesGPIOs | array of GPIO numbers (0..53); use 0x7F as end marker |
void setOutputs | ( | uint32_t const | lesOuts, |
unsigned const | level | ||
) |
Set a list/mask of GPIO output pins.
This functions sets the (output) pins set in the bank mask ON or OFF.
The Pi for the IO operations is thePi.
lesOuts | bank mask of outputs to be set |
level | OFF or ON (0 or 1) |
void setOutput | ( | unsigned const | gpio, |
unsigned const | level | ||
) |
Set a GPIO output pin.
This functions sets an output pin gpio ON or OFF.
If gpio is > 31 nothing is done. That handles the meaning of gpio 33 (within a bank) as unused.
The Pi for the IO operations is thePi.
gpio | an output pin (that should have been set as such!) |
level | OFF or ON (0 or 1) |
uint32_t initAsOutput | ( | int | thePi, |
unsigned | gpio | ||
) |
Initialise a GPIO pin as output.
This sets a GPIO as output and puts it in the list of GPIOs used as outputs by the program if in the range of 0..31 (resp. 2..27).
All functions setting setting as output should use this function.
thePi | the Raspberry's identifier as got from initialising gpio(d) |
gpio | the GPIO number (0..31); PINig means no action |
uint32_t releaseOutputs | ( | int const | thePi | ) |
Release all GPIO pins set as output.
This releases all GPIO in the range 0..27 that this program has set as outputs (by setting those as inputs).
The order of this operation is by increasing GPIO number. If another order or extra actions are required this must be done before or afterwards.
thePi | the Raspberry's identifier as got from initialising gpio(d) |
uint32_t releaseOutputsReport | ( | int const | thePi | ) |
Release all GPIO pins set as output with report.
Same as releaseOutputs() plus a "releaseToIn" for every output released.
thePi | the Raspberry's identifier as got from initialising gpio(d) |
void initAsInput | ( | int | thePi, |
unsigned | gpio | ||
) |
Initialise a GPIO pin as input.
This sets a GPIO as input and removes it form the list of GPIOs set as output if in the range of 0..31 (resp. 2..27).
All functions setting as input should use this function.
thePi | the Raspberry's identifier as got from initialising gpio(d) |
gpio | the GPIO number (0..53) |
void initAsLoInput | ( | int | thePi, |
unsigned | gpio | ||
) |
Initialise a GPIO pin as input with pull up.
This initialisation is for an input sensing a switch (button) or transistor (optocoupler) connected to ground (gnd, 0V). This is the normal configuration instead of switching to Hi (3.3V).
In most of the cases the Pi's internal pull up resistor (about 50 kOhm) is sufficient for Lo-switches and should then be used.
thePi | the Raspberry's identifier as got from initialising gpio(d) |
gpio | the GPIO number (0..53) |
void initAsHiInput | ( | int | thePi, |
unsigned | gpio | ||
) |
Initialise a GPIO pin as input with pull down.
This initialisation is for an input sensing an electronic device delivering a voltage about 3 V when active respectively ON. Some of those devices require a pull down to deliver clean signals.
The Pi's internal pull down resistor (about 50 kOhm) may be sufficient for Hi-switches and should then be used.
thePi | the Raspberry's identifier as got from initialising gpio(d) |
gpio | the GPIO number (0..53) |
void initAsHiDrive | ( | int | thePi, |
unsigned | gpio, | ||
unsigned | init | ||
) |
Initialise a GPIO pin as high drive.
Of course, Raspberry's (BCM2837's) GPIO pins are high and low drivers as output. Hi-drive is provided by turning on pull-up as to allow broken wire diagnosis when shortly switching to input.
thePi | the Raspberry's identifier as got from initialising gpio(d) |
gpio | the GPIO number (0..53) |
init | 0 or 1: the initial output value; else: leave unchanged |
void initAsDrive | ( | int | thePi, |
unsigned | gpio, | ||
unsigned | init | ||
) |
Initialise a GPIO pin as output.
This function sets the GPIO pi as output, optionally sets the drive capacity and leaves a pull resistor setting unchanged.
thePi | the Raspberry's identifier as got from initialising gpio(d) |
gpio | the GPIO number (0..53) |
init | 0 or 1: the initial output value; else: leave unchanged |
void setPadStrength | ( | int | thePi, |
unsigned | mA | ||
) |
Set the output drive capacity of GPIO ports 0..27.
For pins set as output (by initAsHiDrive() or initAsDrive() e.g.) this function sets the drive capacity in the range of 2..16 mA.
Note 1: All 27 pins get the same common value. Hence, one has to set the maximum needed for any pin.
Note 2: This value is no current limit nor pin overload protection. It is the maximum load current, which a valid 0 or 1 output voltage can be guaranteed under. Note 3: The BCM processor can set the strength in 2mA steps (2, 4.. 14, 16). Nevertheless, this function accepts all values 2..16 incrementing odd values.
thePi | the Raspberry's identifier as got from initialising gpio(d) |
mA | 2..16: output drive capacity for providing legal low or high; else: leave drive capacity unchanged |
|
extern |
|
extern |
GPIO number to bank pin number lookup.
Index [0..31] is a GPIO number partly (2..27) available on the Pi's 40 (26) pins connector. Result 0x00000001..0x80000000: a 32 bit with exactly one bit set corresponding to place in a 32 bit bank mask.
Outside [0..31] index out of bound.
See also gpio2pin, pin2gpio
|
extern |
|
extern |
The standard Pi for gpio(d) IO of the program.
This global variable is provided to hold the main pi used by a program doing process IO via piGpIO[d]. In most local use cases
it will be 0 = this local Pi. After usage don't forget to terminate the connection to the pigpio daemon by