<br><br>pigpio is a C library for the Raspberry which allows control of the GPIO.
<h3>Features</h3>o hardware timed PWM on any of GPIO 0-31
<br><br>o hardware timed servo pulses on any of GPIO 0-31
<br><br>o callbacks when any of GPIO 0-31 change state
<br><br>o callbacks at timed intervals
<br><br>o reading/writing all of the GPIO in a bank as one operation
<br><br>o individually setting GPIO modes, reading and writing
<br><br>o notifications when any of GPIO 0-31 change state
<br><br>o the construction of output waveforms with microsecond timing
<br><br>o rudimentary permission control over GPIO
<br><br>o a simple interface to start and stop new threads
<br><br>o I2C, SPI, and serial link wrappers
<br><br>o creating and running scripts
<h3>GPIO</h3>ALL GPIO are identified by their Broadcom number.
<h3>Credits</h3>The PWM and servo pulses are timed using the DMA and PWM peripherals.
<br><br>This use was inspired by Richard Hirst's servoblaster kernel module.
<h3>Usage</h3>Include <pigpio.h> in your source files.
<br><br>Assuming your source is in prog.c use the following command to build and
run the executable.
<br><br><code>gcc -Wall -pthread -o prog prog.c -lpigpio -lrt<br>sudo ./prog<br></code><br><br>For examples of usage see the C programs within the pigpio archive file.
<h3>Notes</h3>All the functions which return an int return < 0 on error.
<br><br><a href="#gpioInitialise">gpioInitialise</a> must be called before all other library functions
with the following exceptions:
<br><br><code><a href="#gpioCfg*">gpioCfg*</a><br><a href="#gpioVersion">gpioVersion</a><br><a href="#gpioHardwareRevision">gpioHardwareRevision</a><br></code><br><br>If the library is not initialised all but the <a href="#gpioCfg*">gpioCfg*</a>,
<a href="#gpioVersion">gpioVersion</a>, and <a href="#gpioHardwareRevision">gpioHardwareRevision</a> functions will
return error PI_NOT_INITIALISED.
<br><br>If the library is initialised the <a href="#gpioCfg*">gpioCfg*</a> functions will return
</b></td><td></td></tr><tr><td></td><td></td></tr><tr><td><a href="#serOpen">serOpen</a></td><td> Opens a serial device
</td></tr><tr><td><a href="#serClose">serClose</a></td><td> Closes a serial device
</td></tr><tr><td></td><td></td></tr><tr><td><a href="#serReadByte">serReadByte</a></td><td> Reads a byte from a serial device
</td></tr><tr><td><a href="#serWriteByte">serWriteByte</a></td><td> Writes a byte to a serial device
</td></tr><tr><td></td><td></td></tr><tr><td><a href="#serRead">serRead</a></td><td> Reads bytes from a serial device
</td></tr><tr><td><a href="#serWrite">serWrite</a></td><td> Writes bytes to a serial device
</td></tr><tr><td></td><td></td></tr><tr><td><a href="#serDataAvailable">serDataAvailable</a></td><td> Returns number of bytes ready to be read
</td></tr><tr><td></td><td></td></tr><tr><td><b>SERIAL BIT BANG (read only)
</b></td><td></td></tr><tr><td></td><td></td></tr><tr><td><a href="#gpioSerialReadOpen">gpioSerialReadOpen</a></td><td> Opens a GPIO for bit bang serial reads
</td></tr><tr><td><a href="#gpioSerialReadClose">gpioSerialReadClose</a></td><td> Closes a GPIO for bit bang serial reads
</td></tr><tr><td></td><td></td></tr><tr><td><a href="#gpioSerialReadInvert">gpioSerialReadInvert</a></td><td> Configures normal/inverted for serial reads
</td></tr><tr><td></td><td></td></tr><tr><td><a href="#gpioSerialRead">gpioSerialRead</a></td><td> Reads bit bang serial data from a GPIO
<br><br><code>gpio: 0-53<br>mode: 0-7<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_GPIO or PI_BAD_MODE.
<br><br>Arduino style: pinMode.
<br><br><b><small>Example</small></b><br><br><code>gpioSetMode(17, PI_INPUT); // Set GPIO17 as input.<br><br>gpioSetMode(18, PI_OUTPUT); // Set GPIO18 as output.<br><br>gpioSetMode(22,PI_ALT0); // Set GPIO22 to alternative mode 0.<br></code><br><br>See <a href="http://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf">http://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf</a> page 102 for an overview of the modes.
Selects the dutycycle range to be used for the GPIO. Subsequent calls
to gpioPWM will use a dutycycle between 0 (off) and range (fully on).
<br><br><code>user_gpio: 0-31<br> range: 25-40000<br></code><br><br>Returns the real range for the given GPIO's frequency if OK,
otherwise PI_BAD_USER_GPIO or PI_BAD_DUTYRANGE.
<br><br>If PWM is currently active on the GPIO its dutycycle will be scaled
to reflect the new range.
<br><br>The real range, the number of steps between fully off and fully
on for each frequency, is given in the following table.
<br><br><code> 25, 50, 100, 125, 200, 250, 400, 500, 625,<br> 800, 1000, 1250, 2000, 2500, 4000, 5000, 10000, 20000<br></code><br><br>The real value set by <a href="#gpioPWM">gpioPWM</a> is (dutycycle * real range) / range.
Starts servo pulses on the GPIO, 0 (off), 500 (most anti-clockwise) to
2500 (most clockwise).
<br><br><code> user_gpio: 0-31<br>pulsewidth: 0, 500-2500<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO or PI_BAD_PULSEWIDTH.
<br><br>The range supported by servos varies and should probably be determined
by experiment. A value of 1500 should always be safe and represents
the mid-point of rotation. You can DAMAGE a servo if you command it
to move beyond its limits.
<br><br>The following causes an on pulse of 1500 microseconds duration to be
transmitted on GPIO 17 at a rate of 50 times per second. This will
command a servo connected to GPIO 17 to rotate to its mid-point.
<br><br>This function updates servos at 50Hz. If you wish to use a different
update frequency you will have to use the PWM functions.
<br><br><code>PWM Hz 50 100 200 400 500<br>1E6/Hz 20000 10000 5000 2500 2000<br></code><br><br>Firstly set the desired PWM frequency using <a href="#gpioSetPWMfrequency">gpioSetPWMfrequency</a>.
<br><br>Then set the PWM range using <a href="#gpioSetPWMrange">gpioSetPWMrange</a> to 1E6/frequency.
Doing this allows you to use units of microseconds when setting
the servo pulsewidth.
<br><br>E.g. If you want to update a servo connected to GPIO25 at 400Hz
<br><br><code>gpioSetPWMfrequency(25, 400);<br><br>gpioSetPWMrange(25, 2500);<br></code><br><br>Thereafter use the PWM command to move the servo,
Registers a function to be called (a callback) when the specified
GPIO changes state.
<br><br><code>user_gpio: 0-31<br> f: the callback function<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO.
<br><br>One callback may be registered per GPIO.
<br><br>The callback is passed the GPIO, the new level, and the tick.
<br><br><code>Parameter Value Meaning<br><br>GPIO 0-31 The GPIO which has changed state<br><br>level 0-2 0 = change to low (a falling edge)<br> 1 = change to high (a rising edge)<br> 2 = no level change (a watchdog timeout)<br><br>tick 32 bit The number of microseconds since boot<br> WARNING: this wraps around from<br> 4294967295 to 0 roughly every 72 minutes<br></code><br><br>The alert may be cancelled by passing NULL as the function.
<br><br>The GPIO are sampled at a rate set when the library is started.
<br><br>If a value isn't specifically set the default of 5 us is used.
<br><br>The number of samples per second is given in the following table.
<br><br><code> samples<br> per sec<br><br> 1 1,000,000<br> 2 500,000<br>sample 4 250,000<br>rate 5 200,000<br>(us) 8 125,000<br> 10 100,000<br></code><br><br>Level changes shorter than the sample rate may be missed.
<br><br>The thread which calls the alert functions is triggered nominally
1000 times per second. The active alert functions will be called
once per level change since the last time the thread was activated.
i.e. The active alert functions will get all level changes but there
will be a latency.
<br><br>If you want to track the level of more than one GPIO do so by
maintaining the state in the callback. Do not use <a href="#gpioRead">gpioRead</a>.
Remember the event that triggered the callback may have
happened several milliseconds before and the GPIO may have
changed level many times since then.
<br><br>The tick value is the time stamp of the sample in microseconds, see
<a href="#gpioTick">gpioTick</a> for more details.
Registers a function to be called (a callback) when the specified
GPIO changes state.
<br><br><code>user_gpio: 0-31<br> f: the callback function<br> userdata: pointer to arbitrary user data<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO.
<br><br>One callback may be registered per GPIO.
<br><br>The callback is passed the GPIO, the new level, the tick, and
the userdata pointer.
<br><br><code>Parameter Value Meaning<br><br>GPIO 0-31 The GPIO which has changed state<br><br>level 0-2 0 = change to low (a falling edge)<br> 1 = change to high (a rising edge)<br> 2 = no level change (a watchdog timeout)<br><br>tick 32 bit The number of microseconds since boot<br> WARNING: this wraps around from<br> 4294967295 to 0 roughly every 72 minutes<br><br>userdata pointer Pointer to an arbitrary object<br></code><br><br>See <a href="#gpioSetAlertFunc">gpioSetAlertFunc</a> for further details.
<br><br>Only one of <a href="#gpioSetAlertFunc">gpioSetAlertFunc</a> or <a href="#gpioSetAlertFuncEx">gpioSetAlertFuncEx</a> can be
Registers a function to be called (a callback) whenever the specified
GPIO interrupt occurs.
<br><br><code> gpio: 0-53<br> edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE<br>timeout: interrupt timeout in milliseconds (<=0 to cancel)<br> f: the callback function<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_GPIO, PI_BAD_EDGE,
or PI_BAD_ISR_INIT.
<br><br>One function may be registered per GPIO.
<br><br>The function is passed the GPIO, the current level, and the
current tick. The level will be PI_TIMEOUT if the optional
interrupt timeout expires.
<br><br><code>Parameter Value Meaning<br><br>GPIO 0-53 The GPIO which has changed state<br><br>level 0-2 0 = change to low (a falling edge)<br> 1 = change to high (a rising edge)<br> 2 = no level change (interrupt timeout)<br><br>tick 32 bit The number of microseconds since boot<br> WARNING: this wraps around from<br> 4294967295 to 0 roughly every 72 minutes<br></code><br><br>The underlying Linux sysfs GPIO interface is used to provide
the interrupt services.
<br><br>The first time the function is called, with a non-NULL f, the
GPIO is exported, set to be an input, and set to interrupt
on the given edge and timeout.
<br><br>Subsequent calls, with a non-NULL f, can vary one or more of the
edge, timeout, or function.
<br><br>The ISR may be cancelled by passing a NULL f, in which case the
GPIO is unexported.
<br><br>The tick is that read at the time the process was informed of
the interrupt. This will be a variable number of microseconds
after the interrupt occurred. Typically the latency will be of
the order of 50 microseconds. The latency is not guaranteed
and will vary with system load.
<br><br>The level is that read at the time the process was informed of
the interrupt, or PI_TIMEOUT if the optional interrupt timeout
expired. It may not be the same as the expected edge as
interrupts happening in rapid succession may be missed by the
kernel (i.e. this mechanism can not be used to capture several
Registers a function to be called (a callback) whenever the specified
GPIO interrupt occurs.
<br><br><code> gpio: 0-53<br> edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE<br> timeout: interrupt timeout in milliseconds (<=0 to cancel)<br> f: the callback function<br>userdata: pointer to arbitrary user data<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_GPIO, PI_BAD_EDGE,
or PI_BAD_ISR_INIT.
<br><br>The function is passed the GPIO, the current level, the
current tick, and the userdata pointer.
<br><br><code>Parameter Value Meaning<br><br>GPIO 0-53 The GPIO which has changed state<br><br>level 0-2 0 = change to low (a falling edge)<br> 1 = change to high (a rising edge)<br> 2 = no level change (interrupt timeout)<br><br>tick 32 bit The number of microseconds since boot<br> WARNING: this wraps around from<br> 4294967295 to 0 roughly every 72 minutes<br><br>userdata pointer Pointer to an arbitrary object<br></code><br><br>Only one of <a href="#gpioSetISRFunc">gpioSetISRFunc</a> or <a href="#gpioSetISRFuncEx">gpioSetISRFuncEx</a> can be
registered per GPIO.
<br><br>See <a href="#gpioSetISRFunc">gpioSetISRFunc</a> for further details.
This function starts notifications on a previously opened handle.
<br><br><code>handle: >=0, as returned by <a href="#gpioNotifyOpen">gpioNotifyOpen</a><br> bits: a bit mask indicating the GPIO of interest<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE.
<br><br>The notification sends state changes for each GPIO whose corresponding
bit in bits is set.
<br><br>Each notification occupies 12 bytes in the fifo and has the
following structure.
<br><br><code>typedef struct<br>{<br> uint16_t seqno;<br> uint16_t flags;<br> uint32_t tick;<br> uint32_t level;<br>} gpioReport_t;<br></code><br><br>seqno: starts at 0 each time the handle is opened and then increments
by one for each report.
<br><br>flags: three flags are defined, PI_NTFY_FLAGS_WDOG,
PI_NTFY_FLAGS_ALIVE, and PI_NTFY_FLAGS_EVENT.
<br><br>If bit 5 is set (PI_NTFY_FLAGS_WDOG) then bits 0-4 of the flags
indicate a GPIO which has had a watchdog timeout.
<br><br>If bit 6 is set (PI_NTFY_FLAGS_ALIVE) this indicates a keep alive
signal on the pipe/socket and is sent once a minute in the absence
of other notification activity.
<br><br>If bit 7 is set (PI_NTFY_FLAGS_EVENT) then bits 0-4 of the flags
indicate an event which has been triggered.
<br><br>tick: the number of microseconds since system boot. It wraps around
after 1h12m.
<br><br>level: indicates the level of each GPIO. If bit 1<<x is set then
This function adds a number of pulses to the current waveform.
<br><br><code>numPulses: the number of pulses<br> pulses: an array of pulses<br></code><br><br>Returns the new total number of pulses in the current waveform if OK,
otherwise PI_TOO_MANY_PULSES.
<br><br>The pulses are interleaved in time order within the existing waveform
(if any).
<br><br>Merging allows the waveform to be built in parts, that is the settings
for GPIO#1 can be added, and then GPIO#2 etc.
<br><br>If the added waveform is intended to start after or within the existing
waveform then the first pulse should consist of a delay.
This function adds a waveform representing serial data to the
existing waveform (if any). The serial data starts offset
microseconds from the start of the waveform.
<br><br><code>user_gpio: 0-31<br> baud: 50-1000000<br>data_bits: 1-32<br>stop_bits: 2-8<br> offset: >=0<br> numBytes: >=1<br> str: an array of chars (which may contain nulls)<br></code><br><br>Returns the new total number of pulses in the current waveform if OK,
Similar to <a href="#gpioWaveCreate">gpioWaveCreate</a>, this function creates a waveform but pads the consumed
resources. Padded waves of equal dimension can be re-cycled efficiently allowing
newly created waves to re-use the resources of deleted waves of the same dimension.
<br><br><code>pctCB: 0-100, the percent of all DMA control blocks to consume.<br>pctBOOL: 0-100, percent On-Off-Level (OOL) buffer to consume for wave output.<br>pctTOOL: 0-100, the percent of OOL buffer to consume for wave input (flags).<br></code><br><br>Upon success a wave id greater than or equal to 0 is returned, otherwise
PI_EMPTY_WAVEFORM, PI_TOO_MANY_CBS, PI_TOO_MANY_OOL, or PI_NO_WAVEFORM_ID.
<br><br>Waveform data provided by <a href="#gpioWaveAdd*">gpioWaveAdd*</a> and <a href="#rawWaveAdd*">rawWaveAdd*</a> functions are
consumed by this function.
<br><br>A usage would be the creation of two waves where one is filled while the other
is being transmitted. Each wave is assigned 50% of the resources.
This buffer structure allows the transmission of infinite wave sequences.
This function deletes the waveform with id wave_id.
<br><br>The wave is flagged for deletion. The resources used by the wave
will only be reused when either of the following apply.
<br><br>- all waves with higher numbered wave ids have been deleted or have
been flagged for deletion.
<br><br>- a new wave is created which uses exactly the same resources as
the current wave (see the C source for gpioWaveCreate for details).
<br><br><code>wave_id: >=0, as returned by <a href="#gpioWaveCreate">gpioWaveCreate</a><br></code><br><br>Wave ids are allocated in order, 0, 1, 2, etc.
<br><br>Returns 0 if OK, otherwise PI_BAD_WAVE_ID.
This function transmits the waveform with id wave_id. The mode
determines whether the waveform is sent once or cycles endlessly.
The SYNC variants wait for the current waveform to reach the
end of a cycle or finish before starting the new waveform.
<br><br>WARNING: bad things may happen if you delete the previous
waveform before it has been synced to the new waveform.
<br><br>NOTE: Any hardware PWM started by <a href="#gpioHardwarePWM">gpioHardwarePWM</a> will be cancelled.
<br><br><code> wave_id: >=0, as returned by <a href="#gpioWaveCreate">gpioWaveCreate</a><br>wave_mode: PI_WAVE_MODE_ONE_SHOT, PI_WAVE_MODE_REPEAT,<br> PI_WAVE_MODE_ONE_SHOT_SYNC, PI_WAVE_MODE_REPEAT_SYNC<br></code><br><br>Returns the number of DMA control blocks in the waveform if OK,
<br><br>NOTE: Any hardware PWM started by <a href="#gpioHardwarePWM">gpioHardwarePWM</a> will be cancelled.
<br><br>The waves to be transmitted are specified by the contents of buf
which contains an ordered list of <a href="#wave_id">wave_id</a>s and optional command
codes and related data.
<br><br><code> buf: pointer to the wave_ids and optional command codes<br>bufSize: the number of bytes in buf<br></code><br><br>Returns 0 if OK, otherwise PI_CHAIN_NESTING, PI_CHAIN_LOOP_CNT, PI_BAD_CHAIN_LOOP, PI_BAD_CHAIN_CMD, PI_CHAIN_COUNTER,
PI_BAD_CHAIN_DELAY, PI_CHAIN_TOO_BIG, or PI_BAD_WAVE_ID.
<br><br>Each wave is transmitted in the order specified. A wave may
occur multiple times per chain.
<br><br>A blocks of waves may be transmitted multiple times by using
the loop commands. The block is bracketed by loop start and
end commands. Loops may be nested.
<br><br>Delays between waves may be added with the delay command.
<br><br>The following command codes are supported:
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td>Name</td><td>Cmd & Data</td><td>Meaning</td></tr><tr><td>Loop Start</td><td>255 0</td><td>Identify start of a wave block</td></tr><tr><td>Loop Repeat</td><td>255 1 x y</td><td>loop x + y*256 times</td></tr><tr><td>Delay</td><td>255 2 x y</td><td>delay x + y*256 microseconds</td></tr><tr><td>Loop Forever</td><td>255 3</td><td>loop forever</td></tr></tbody></table><br><br>If present Loop Forever must be the last entry in the chain.
<br><br>The code is currently dimensioned to support a chain with roughly
This function copies up to bufSize bytes of data read from the
bit bang serial cyclic buffer to the buffer starting at buf.
<br><br><code>user_gpio: 0-31, previously opened with <a href="#gpioSerialReadOpen">gpioSerialReadOpen</a><br> buf: an array to receive the read bytes<br> bufSize: >=0<br></code><br><br>Returns the number of bytes copied if OK, otherwise PI_BAD_USER_GPIO
or PI_NOT_SERIAL_GPIO.
<br><br>The bytes returned for each character depend upon the number of
data bits <a href="#data_bits">data_bits</a> specified in the <a href="#gpioSerialReadOpen">gpioSerialReadOpen</a> command.
<br><br>For <a href="#data_bits">data_bits</a> 1-8 there will be one byte per character.<br>
For <a href="#data_bits">data_bits</a> 9-16 there will be two bytes per character.<br>
For <a href="#data_bits">data_bits</a> 17-32 there will be four bytes per character.
This function closes a GPIO for bit bang reading of serial data.
<br><br><code>user_gpio: 0-31, previously opened with <a href="#gpioSerialReadOpen">gpioSerialReadOpen</a><br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_SERIAL_GPIO.
This returns a handle for the device at the address on the I2C bus.
<br><br><code> i2cBus: >=0<br> i2cAddr: 0-0x7F<br>i2cFlags: 0<br></code><br><br>No flags are currently defined. This parameter should be set to zero.
<br><br>Physically buses 0 and 1 are available on the Pi. Higher numbered buses
will be available if a kernel supported bus multiplexor is being used.
<br><br>The GPIO used are given in the following table.
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td></td><td>SDA</td><td>SCL</td></tr><tr><td>I2C 0</td><td>0</td><td>1</td></tr><tr><td>I2C 1</td><td>2</td><td>3</td></tr></tbody></table><br><br>Returns a handle (>=0) if OK, otherwise PI_BAD_I2C_BUS, PI_BAD_I2C_ADDR,
PI_BAD_FLAGS, PI_NO_HANDLE, or PI_I2C_OPEN_FAILED.
<br><br>For the SMBus commands the low level transactions are shown at the end
of the function description. The following abbreviations are used.
This sends a single bit (in the Rd/Wr bit) to the device associated
with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br> bit: 0-1, the value to write<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This sends a single byte to the device associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br> bVal: 0-0xFF, the value to write<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This reads a single byte from the device associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br></code><br><br>Returns the byte read (>=0) if OK, otherwise PI_BAD_HANDLE,
This writes a single byte to the specified register of the device
associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to write<br> bVal: 0-0xFF, the value to write<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This writes a single 16 bit word to the specified register of the device
associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to write<br> wVal: 0-0xFFFF, the value to write<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This reads a single byte from the specified register of the device
associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to read<br></code><br><br>Returns the byte read (>=0) if OK, otherwise PI_BAD_HANDLE,
This reads a single 16 bit word from the specified register of the device
associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to read<br></code><br><br>Returns the word read (>=0) if OK, otherwise PI_BAD_HANDLE,
This writes 16 bits of data to the specified register of the device
associated with handle and reads 16 bits of data in return.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to write/read<br> wVal: 0-0xFFFF, the value to write<br></code><br><br>Returns the word read (>=0) if OK, otherwise PI_BAD_HANDLE,
This writes up to 32 bytes to the specified register of the device
associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to write<br> buf: an array with the data to send<br> count: 1-32, the number of bytes to write<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This reads a block of up to 32 bytes from the specified register of
the device associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to read<br> buf: an array to receive the read data<br></code><br><br>The amount of returned data is set by the device.
<br><br>Returns the number of bytes read (>=0) if OK, otherwise PI_BAD_HANDLE,
This writes data bytes to the specified register of the device
associated with handle and reads a device specified number
of bytes of data in return.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to write/read<br> buf: an array with the data to send and to receive the read data<br> count: 1-32, the number of bytes to write<br></code><br><br>Returns the number of bytes read (>=0) if OK, otherwise PI_BAD_HANDLE,
PI_BAD_PARAM, or PI_I2C_READ_FAILED.
<br><br>The SMBus 2.0 documentation states that a minimum of 1 byte may be
sent and a minimum of 1 byte may be received. The total number of
This reads count bytes from the specified register of the device
associated with handle . The count may be 1-32.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to read<br> buf: an array to receive the read data<br> count: 1-32, the number of bytes to read<br></code><br><br>Returns the number of bytes read (>0) if OK, otherwise PI_BAD_HANDLE,
This writes 1 to 32 bytes to the specified register of the device
associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br>i2cReg: 0-255, the register to write<br> buf: the data to write<br> count: 1-32, the number of bytes to write<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This reads count bytes from the raw device into buf.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br> buf: an array to receive the read data bytes<br> count: >0, the number of bytes to read<br></code><br><br>Returns count (>0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This writes count bytes from buf to the raw device.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br> buf: an array containing the data bytes to write<br> count: >0, the number of bytes to write<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This sets the I2C (i2c-bcm2708) module "use combined transactions"
parameter on or off.
<br><br><code>setting: 0 to set the parameter off, non-zero to set it on<br></code><br><br>NOTE: when the flag is on a write followed by a read to the same
slave address will use a repeated start (rather than a stop/start).
This function executes multiple I2C segments in one transaction by
calling the I2C_RDWR ioctl.
<br><br><code> handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br> segs: an array of I2C segments<br>numSegs: >0, the number of I2C segments<br></code><br><br>Returns the number of segments if OK, otherwise PI_BAD_I2C_SEG.
This function executes a sequence of I2C operations. The
operations to be performed are specified by the contents of inBuf
which contains the concatenated command codes and associated data.
<br><br><code>handle: >=0, as returned by a call to <a href="#i2cOpen">i2cOpen</a><br> inBuf: pointer to the concatenated I2C commands, see below<br> inLen: size of command buffer<br>outBuf: pointer to buffer to hold returned data<br>outLen: size of output buffer<br></code><br><br>Returns >= 0 if OK (the number of bytes read), otherwise
<br><br>The following command codes are supported:
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td>Name</td><td>Cmd & Data</td><td>Meaning</td></tr><tr><td>End</td><td>0</td><td>No more commands</td></tr><tr><td>Escape</td><td>1</td><td>Next P is two bytes</td></tr><tr><td>On</td><td>2</td><td>Switch combined flag on</td></tr><tr><td>Off</td><td>3</td><td>Switch combined flag off</td></tr><tr><td>Address</td><td>4 P</td><td>Set I2C address to P</td></tr><tr><td>Flags</td><td>5 lsb msb</td><td>Set I2C flags to lsb + (msb << 8)</td></tr><tr><td>Read</td><td>6 P</td><td>Read P bytes of data</td></tr><tr><td>Write</td><td>7 P ...</td><td>Write P bytes of data</td></tr></tbody></table><br><br>The address, read, and write commands take a parameter P.
Normally P is one byte (0-255). If the command is preceded by
the Escape command then P is two bytes (0-65535, least significant
byte first).
<br><br>The address defaults to that associated with the handle.
The flags default to 0. The address and flags maintain their
previous value until updated.
<br><br>The returned I2C data is stored in consecutive locations of outBuf.
This function selects a pair of GPIO for bit banging I2C at a
specified baud rate.
<br><br>Bit banging I2C allows for certain operations which are not possible
with the standard I2C driver.
<br><br>o baud rates as low as 50<br>
o repeated starts<br>
o clock stretching<br>
o I2C on any pair of spare GPIO
<br><br><code> SDA: 0-31<br> SCL: 0-31<br>baud: 50-500000<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_I2C_BAUD, or
PI_GPIO_IN_USE.
<br><br>NOTE:
<br><br>The GPIO used for SDA and SCL must have pull-ups to 3V3 connected. As
a guide the hardware pull-ups on pins 3 and 5 are 1k8 in value.
This function stops bit banging I2C on a pair of GPIO previously
opened with <a href="#bbI2COpen">bbI2COpen</a>.
<br><br><code>SDA: 0-31, the SDA GPIO used in a prior call to <a href="#bbI2COpen">bbI2COpen</a><br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_I2C_GPIO.
This function executes a sequence of bit banged I2C operations. The
operations to be performed are specified by the contents of inBuf
which contains the concatenated command codes and associated data.
<br><br><code> SDA: 0-31 (as used in a prior call to <a href="#bbI2COpen">bbI2COpen</a>)<br> inBuf: pointer to the concatenated I2C commands, see below<br> inLen: size of command buffer<br>outBuf: pointer to buffer to hold returned data<br>outLen: size of output buffer<br></code><br><br>Returns >= 0 if OK (the number of bytes read), otherwise
<br><br>The following command codes are supported:
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td>Name</td><td>Cmd & Data</td><td>Meaning</td></tr><tr><td>End</td><td>0</td><td>No more commands</td></tr><tr><td>Escape</td><td>1</td><td>Next P is two bytes</td></tr><tr><td>Start</td><td>2</td><td>Start condition</td></tr><tr><td>Stop</td><td>3</td><td>Stop condition</td></tr><tr><td>Address</td><td>4 P</td><td>Set I2C address to P</td></tr><tr><td>Flags</td><td>5 lsb msb</td><td>Set I2C flags to lsb + (msb << 8)</td></tr><tr><td>Read</td><td>6 P</td><td>Read P bytes of data</td></tr><tr><td>Write</td><td>7 P ...</td><td>Write P bytes of data</td></tr></tbody></table><br><br>The address, read, and write commands take a parameter P.
Normally P is one byte (0-255). If the command is preceded by
the Escape command then P is two bytes (0-65535, least significant
byte first).
<br><br>The address and flags default to 0. The address and flags maintain
their previous value until updated.
<br><br>No flags are currently defined.
<br><br>The returned I2C data is stored in consecutive locations of outBuf.
This function provides a low-level interface to the SPI/I2C Slave
peripheral on the BCM chip.
<br><br>This peripheral allows the Pi to act as a hardware slave device
on an I2C or SPI bus.
<br><br>This is not a bit bang version and as such is OS timing
independent. The bus timing is handled directly by the chip.
<br><br>The output process is simple. You simply append data to the FIFO
buffer on the chip. This works like a queue, you add data to the
queue and the master removes it.
<br><br>I can't get SPI to work properly. I tried with a
control word of 0x303 and swapped MISO and MOSI.
<br><br>The function sets the BSC mode, writes any data in
the transmit buffer to the BSC transmit FIFO, and
copies any data in the BSC receive FIFO to the
receive buffer.
<br><br><code>bsc_xfer:= a structure defining the transfer<br><br>typedef struct<br>{<br> uint32_t control; // Write<br> int rxCnt; // Read only<br> char rxBuf[BSC_FIFO_SIZE]; // Read only<br> int txCnt; // Write<br> char txBuf[BSC_FIFO_SIZE]; // Write<br>} bsc_xfer_t;<br></code><br><br>To start a transfer set control (see below), copy the bytes to
be added to the transmit FIFO (if any) to txBuf and set txCnt to
the number of copied bytes.
<br><br>Upon return rxCnt will be set to the number of received bytes placed
in rxBuf.
<br><br>Note that the control word sets the BSC mode. The BSC will stay in
that mode until a different control word is sent.
<br><br>GPIO used for models other than those based on the BCM2711.
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td></td><td>SDA</td><td>SCL</td><td>MOSI</td><td>SCLK</td><td>MISO</td><td>CE</td></tr><tr><td>I2C</td><td>18</td><td>19</td><td>-</td><td>-</td><td>-</td><td>-</td></tr><tr><td>SPI</td><td>-</td><td>-</td><td>18</td><td>19</td><td>20</td><td>21</td></tr></tbody></table><br><br>GPIO used for models based on the BCM2711 (e.g. the Pi4B).
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td></td><td>SDA</td><td>SCL</td><td>MOSI</td><td>SCLK</td><td>MISO</td><td>CE</td></tr><tr><td>I2C</td><td>10</td><td>11</td><td>-</td><td>-</td><td>-</td><td>-</td></tr><tr><td>SPI</td><td>-</td><td>-</td><td>10</td><td>11</td><td>9</td><td>8</td></tr></tbody></table><br><br>When a zero control word is received the used GPIO will be reset
to INPUT mode.
<br><br>The returned function value is the status of the transfer (see below).
<br><br>If there was an error the status will be less than zero
(and will contain the error code).
<br><br>The most significant word of the returned status contains the number
of bytes actually copied from txBuf to the BSC transmit FIFO (may be
less than requested if the FIFO already contained untransmitted data).
<br><br>control consists of the following bits.
<br><br><code>22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0<br> a a a a a a a - - IT HC TF IR RE TE BK EC ES PL PH I2 SP EN<br></code><br><br>Bits 0-13 are copied unchanged to the BSC CR register. See
pages 163-165 of the Broadcom peripherals document for full
details.
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td>aaaaaaa</td><td>defines the I2C slave address (only relevant in I2C mode)</td></tr><tr><td>IT</td><td>invert transmit status flags</td></tr><tr><td>HC</td><td>enable host control</td></tr><tr><td>TF</td><td>enable test FIFO</td></tr><tr><td>IR</td><td>invert receive status flags</td></tr><tr><td>RE</td><td>enable receive</td></tr><tr><td>TE</td><td>enable transmit</td></tr><tr><td>BK</td><td>abort operation and clear FIFOs</td></tr><tr><td>EC</td><td>send control register as first I2C byte</td></tr><tr><td>ES</td><td>send status register as first I2C byte</td></tr><tr><td>PL</td><td>set SPI polarity high</td></tr><tr><td>PH</td><td>set SPI phase high</td></tr><tr><td>I2</td><td>enable I2C mode</td></tr><tr><td>SP</td><td>enable SPI mode</td></tr><tr><td>EN</td><td>enable BSC peripheral</td></tr></tbody></table><br><br>The returned status has the following format
<br><br><code>20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0<br> S S S S S R R R R R T T T T T RB TE RF TF RE TB<br></code><br><br>Bits 0-15 are copied unchanged from the BSC FR register. See
pages 165-166 of the Broadcom peripherals document for full
details.
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td>SSSSS</td><td>number of bytes successfully copied to transmit FIFO</td></tr><tr><td>RRRRR</td><td>number of bytes in receieve FIFO</td></tr><tr><td>TTTTT</td><td>number of bytes in transmit FIFO</td></tr><tr><td>RB</td><td>receive busy</td></tr><tr><td>TE</td><td>transmit FIFO empty</td></tr><tr><td>RF</td><td>receive FIFO full</td></tr><tr><td>TF</td><td>transmit FIFO full</td></tr><tr><td>RE</td><td>receive FIFO empty</td></tr><tr><td>TB</td><td>transmit busy</td></tr></tbody></table><br><br>The following example shows how to configure the BSC peripheral as
an I2C slave with address 0x13 and send four bytes.
This function selects a set of GPIO for bit banging SPI with
a specified baud rate and mode.
<br><br><code> CS: 0-31<br> MISO: 0-31<br> MOSI: 0-31<br> SCLK: 0-31<br> baud: 50-250000<br>spiFlags: see below<br></code><br><br>spiFlags consists of the least significant 22 bits.
<br><br><code>21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0<br> 0 0 0 0 0 0 R T 0 0 0 0 0 0 0 0 0 0 0 p m m<br></code><br><br>mm defines the SPI mode, defaults to 0
<br><br><code>Mode CPOL CPHA<br> 0 0 0<br> 1 0 1<br> 2 1 0<br> 3 1 1<br></code><br><br>p is 0 if CS is active low (default) and 1 for active high.
<br><br>T is 1 if the least significant bit is transmitted on MOSI first, the
default (0) shifts the most significant bit out first.
<br><br>R is 1 if the least significant bit is received on MISO first, the
default (0) receives the most significant bit first.
<br><br>The other bits in flags should be set to zero.
<br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_SPI_BAUD, or
PI_GPIO_IN_USE.
<br><br>If more than one device is connected to the SPI bus (defined by
This function stops bit banging SPI on a set of GPIO
opened with <a href="#bbSPIOpen">bbSPIOpen</a>.
<br><br><code>CS: 0-31, the CS GPIO used in a prior call to <a href="#bbSPIOpen">bbSPIOpen</a><br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_SPI_GPIO.
<br><br><code> CS: 0-31 (as used in a prior call to <a href="#bbSPIOpen">bbSPIOpen</a>)<br> inBuf: pointer to buffer to hold data to be sent<br>outBuf: pointer to buffer to hold returned data<br> count: size of data transfer<br></code><br><br>Returns >= 0 if OK (the number of bytes read), otherwise
PI_BAD_USER_GPIO, PI_NOT_SPI_GPIO or PI_BAD_POINTER.
This function returns a handle for the SPI device on the channel.
Data will be transferred at baud bits per second. The flags may
be used to modify the default behaviour of 4-wire operation, mode 0,
active low chip select.
<br><br>The Pi has two SPI peripherals: main and auxiliary.
<br><br>The main SPI has two chip selects (channels), the auxiliary has
three.
<br><br>The auxiliary SPI is available on all models but the A and B.
<br><br>The GPIO used are given in the following table.
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td></td><td>MISO</td><td>MOSI</td><td>SCLK</td><td>CE0</td><td>CE1</td><td>CE2</td></tr><tr><td>Main SPI</td><td>9</td><td>10</td><td>11</td><td>8</td><td>7</td><td>-</td></tr><tr><td>Aux SPI</td><td>19</td><td>20</td><td>21</td><td>18</td><td>17</td><td>16</td></tr></tbody></table><br><br><code> spiChan: 0-1 (0-2 for the auxiliary SPI)<br> baud: 32K-125M (values above 30M are unlikely to work)<br>spiFlags: see below<br></code><br><br>Returns a handle (>=0) if OK, otherwise PI_BAD_SPI_CHANNEL,
PI_BAD_SPI_SPEED, PI_BAD_FLAGS, PI_NO_AUX_SPI, or PI_SPI_OPEN_FAILED.
<br><br>spiFlags consists of the least significant 22 bits.
<br><br><code>21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0<br> b b b b b b R T n n n n W A u2 u1 u0 p2 p1 p0 m m<br></code><br><br>mm defines the SPI mode.
<br><br>Warning: modes 1 and 3 do not appear to work on the auxiliary SPI.
<br><br><code>Mode POL PHA<br> 0 0 0<br> 1 0 1<br> 2 1 0<br> 3 1 1<br></code><br><br>px is 0 if CEx is active low (default) and 1 for active high.
<br><br>ux is 0 if the CEx GPIO is reserved for SPI (default) and 1 otherwise.
<br><br>A is 0 for the main SPI, 1 for the auxiliary SPI.
<br><br>W is 0 if the device is not 3-wire, 1 if the device is 3-wire. Main
SPI only.
<br><br>nnnn defines the number of bytes (0-15) to write before switching
the MOSI line to MISO to read data. This field is ignored
if W is not set. Main SPI only.
<br><br>T is 1 if the least significant bit is transmitted on MOSI first, the
default (0) shifts the most significant bit out first. Auxiliary SPI
only.
<br><br>R is 1 if the least significant bit is received on MISO first, the
default (0) receives the most significant bit first. Auxiliary SPI
only.
<br><br>bbbbbb defines the word size in bits (0-32). The default (0)
sets 8 bits per word. Auxiliary SPI only.
<br><br>The <a href="#spiRead">spiRead</a>, <a href="#spiWrite">spiWrite</a>, and <a href="#spiXfer">spiXfer</a> functions
transfer data packed into 1, 2, or 4 bytes according to
the word size in bits.
<br><br>For bits 1-8 there will be one byte per word.<br>
For bits 9-16 there will be two bytes per word.<br>
For bits 17-32 there will be four bytes per word.
<br><br>Multi-byte transfers are made in least significant byte first order.
<br><br>E.g. to transfer 32 11-bit words buf should contain 64 bytes
and count should be 64.
<br><br>E.g. to transfer the 14 bit value 0x1ABC send the bytes 0xBC followed
by 0x1A.
<br><br>The other bits in flags should be set to zero.
This function reads count bytes of data from the SPI
device associated with the handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#spiOpen">spiOpen</a><br> buf: an array to receive the read data bytes<br> count: the number of bytes to read<br></code><br><br>Returns the number of bytes transferred if OK, otherwise
PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED.
This function writes count bytes of data from buf to the SPI
device associated with the handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#spiOpen">spiOpen</a><br> buf: the data bytes to write<br> count: the number of bytes to write<br></code><br><br>Returns the number of bytes transferred if OK, otherwise
PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED.
This function transfers count bytes of data from txBuf to the SPI
device associated with the handle. Simultaneously count bytes of
data are read from the device and placed in rxBuf.
<br><br><code>handle: >=0, as returned by a call to <a href="#spiOpen">spiOpen</a><br> txBuf: the data bytes to write<br> rxBuf: the received data bytes<br> count: the number of bytes to transfer<br></code><br><br>Returns the number of bytes transferred if OK, otherwise
PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED.
This function opens a serial device at a specified baud rate
and with specified flags. The device name must start with
/dev/tty or /dev/serial.
<br><br><code> sertty: the serial device to open<br> baud: the baud rate in bits per second, see below<br>serFlags: 0<br></code><br><br>Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE, or
PI_SER_OPEN_FAILED.
<br><br>The baud rate must be one of 50, 75, 110, 134, 150,
This function writes bVal to the serial port associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#serOpen">serOpen</a><br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This function reads a byte from the serial port associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#serOpen">serOpen</a><br></code><br><br>Returns the read byte (>=0) if OK, otherwise PI_BAD_HANDLE,
PI_SER_READ_NO_DATA, or PI_SER_READ_FAILED.
<br><br>If no data is ready PI_SER_READ_NO_DATA is returned.
This function writes count bytes from buf to the the serial port
associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#serOpen">serOpen</a><br> buf: the array of bytes to write<br> count: the number of bytes to write<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
This function reads up count bytes from the the serial port
associated with handle and writes them to buf.
<br><br><code>handle: >=0, as returned by a call to <a href="#serOpen">serOpen</a><br> buf: an array to receive the read data<br> count: the maximum number of bytes to read<br></code><br><br>Returns the number of bytes read (>0=) if OK, otherwise PI_BAD_HANDLE,
This function returns the number of bytes available
to be read from the device associated with handle.
<br><br><code>handle: >=0, as returned by a call to <a href="#serOpen">serOpen</a><br></code><br><br>Returns the number of bytes of data available (>=0) if OK,
<br><br><code>user_gpio: 0-31<br> timeout: 0-60000<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO or PI_BAD_WDOG_TIMEOUT.
<br><br>The watchdog is nominally in milliseconds.
<br><br>One watchdog may be registered per GPIO.
<br><br>The watchdog may be cancelled by setting timeout to 0.
<br><br>Until cancelled a timeout will be reported every timeout milliseconds
after the last GPIO activity.
<br><br>In particular:
<br><br>1) any registered alert function for the GPIO will be called with
the level set to PI_TIMEOUT.
<br><br>2) any notification for the GPIO will have a report written to the
fifo with the flags set to indicate a watchdog timeout.
<br><br>Level changes on the GPIO are ignored until a level which has
been stable for <a href="#steady">steady</a> microseconds is detected. Level changes
on the GPIO are then reported for <a href="#active">active</a> microseconds after
which the process repeats.
<br><br><code>user_gpio: 0-31<br> steady: 0-300000<br> active: 0-1000000<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER.
<br><br>This filter affects the GPIO samples returned to callbacks set up
with <a href="#gpioSetAlertFunc">gpioSetAlertFunc</a>, <a href="#gpioSetAlertFuncEx">gpioSetAlertFuncEx</a>, <a href="#gpioSetGetSamplesFunc">gpioSetGetSamplesFunc</a>,
and <a href="#gpioSetGetSamplesFuncEx">gpioSetGetSamplesFuncEx</a>.
<br><br>It does not affect interrupts set up with <a href="#gpioSetISRFunc">gpioSetISRFunc</a>,
<a href="#gpioSetISRFuncEx">gpioSetISRFuncEx</a>, or levels read by <a href="#gpioRead">gpioRead</a>,
<a href="#gpioRead_Bits_0_31">gpioRead_Bits_0_31</a>, or <a href="#gpioRead_Bits_32_53">gpioRead_Bits_32_53</a>.
<br><br>Level changes before and after the active period may
be reported. Your software must be designed to cope with
<br><br>Level changes on the GPIO are not reported unless the level
has been stable for at least <a href="#steady">steady</a> microseconds. The
level is then reported. Level changes of less than <a href="#steady">steady</a>
microseconds are ignored.
<br><br><code>user_gpio: 0-31<br> steady: 0-300000<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER.
<br><br>This filter affects the GPIO samples returned to callbacks set up
with <a href="#gpioSetAlertFunc">gpioSetAlertFunc</a>, <a href="#gpioSetAlertFuncEx">gpioSetAlertFuncEx</a>, <a href="#gpioSetGetSamplesFunc">gpioSetGetSamplesFunc</a>,
and <a href="#gpioSetGetSamplesFuncEx">gpioSetGetSamplesFuncEx</a>.
<br><br>It does not affect interrupts set up with <a href="#gpioSetISRFunc">gpioSetISRFunc</a>,
<a href="#gpioSetISRFuncEx">gpioSetISRFuncEx</a>, or levels read by <a href="#gpioRead">gpioRead</a>,
<a href="#gpioRead_Bits_0_31">gpioRead_Bits_0_31</a>, or <a href="#gpioRead_Bits_32_53">gpioRead_Bits_32_53</a>.
<br><br>Each (stable) edge will be timestamped <a href="#steady">steady</a> microseconds
Registers a function to be called (a callback) every millisecond
with the latest GPIO samples.
<br><br><code> f: the function to call<br>bits: the GPIO of interest<br></code><br><br>Returns 0 if OK.
<br><br>The function is passed a pointer to the samples (an array of
<a href="#gpioSample_t">gpioSample_t</a>), and the number of samples.
<br><br>Only one function can be registered.
<br><br>The callback may be cancelled by passing NULL as the function.
<br><br>The samples returned will be the union of bits, plus any active alerts,
plus any active notifications.
<br><br>e.g. if there are alerts for GPIO 7, 8, and 9, notifications for GPIO
8, 10, 23, 24, and bits is (1<<23)|(1<<17) then samples for GPIO
Registers a function to be called (a callback) every millisecond
with the latest GPIO samples.
<br><br><code> f: the function to call<br> bits: the GPIO of interest<br>userdata: a pointer to arbitrary user data<br></code><br><br>Returns 0 if OK.
<br><br>The function is passed a pointer to the samples (an array of
<a href="#gpioSample_t">gpioSample_t</a>), the number of samples, and the userdata pointer.
<br><br>Only one of <a href="#gpioGetSamplesFunc">gpioGetSamplesFunc</a> or <a href="#gpioGetSamplesFuncEx">gpioGetSamplesFuncEx</a> can be
registered.
<br><br>See <a href="#gpioSetGetSamplesFunc">gpioSetGetSamplesFunc</a> for further details.
Registers a function to be called (a callback) every millis milliseconds.
<br><br><code> timer: 0-9<br>millis: 10-60000<br> f: the function to call<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_TIMER, PI_BAD_MS, or PI_TIMER_FAILED.
<br><br>10 timers are supported numbered 0 to 9.
<br><br>One function may be registered per timer.
<br><br>The timer may be cancelled by passing NULL as the function.
Registers a function to be called (a callback) every millis milliseconds.
<br><br><code> timer: 0-9.<br> millis: 10-60000<br> f: the function to call<br>userdata: a pointer to arbitrary user data<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_TIMER, PI_BAD_MS, or PI_TIMER_FAILED.
<br><br>The function is passed the userdata pointer.
<br><br>Only one of <a href="#gpioSetTimerFunc">gpioSetTimerFunc</a> or <a href="#gpioSetTimerFuncEx">gpioSetTimerFuncEx</a> can be
registered per timer.
<br><br>See <a href="#gpioSetTimerFunc">gpioSetTimerFunc</a> for further details.
Starts a new thread of execution with f as the main routine.
<br><br><code> f: the main function for the new thread<br>userdata: a pointer to arbitrary user data<br></code><br><br>Returns a pointer to pthread_t if OK, otherwise NULL.
<br><br>The function is passed the single argument arg.
<br><br>The thread can be cancelled by passing the pointer to pthread_t to
<br><br><code>pth: a thread pointer returned by <a href="#gpioStartThread">gpioStartThread</a><br></code><br><br>No value is returned.
<br><br>The thread to be stopped should have been started with <a href="#gpioStartThread">gpioStartThread</a>.
<br><br><code>script_id: >=0, as returned by <a href="#gpioStoreScript">gpioStoreScript</a><br> numPar: 0-10, the number of parameters<br> param: an array of parameters<br></code><br><br>The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID, or
PI_TOO_MANY_PARAM.
<br><br>param is an array of up to 10 parameters which may be referenced in
<br><br><code>script_id: >=0, as returned by <a href="#gpioStoreScript">gpioStoreScript</a><br> numPar: 0-10, the number of parameters<br> param: an array of parameters<br></code><br><br>The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID, or
PI_TOO_MANY_PARAM.
<br><br>param is an array of up to 10 parameters which may be referenced in
This function sets the parameters of a script. The script may or
may not be running. The first numPar parameters of the script are
overwritten with the new values.
<br><br><code>script_id: >=0, as returned by <a href="#gpioStoreScript">gpioStoreScript</a><br> numPar: 0-10, the number of parameters<br> param: an array of parameters<br></code><br><br>The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID, or
PI_TOO_MANY_PARAM.
<br><br>param is an array of up to 10 parameters which may be referenced in
This function returns the run status of a stored script as well as
the current values of parameters 0 to 9.
<br><br><code>script_id: >=0, as returned by <a href="#gpioStoreScript">gpioStoreScript</a><br> param: an array to hold the returned 10 parameters<br></code><br><br>The function returns greater than or equal to 0 if OK,
otherwise PI_BAD_SCRIPT_ID.
<br><br>The run status may be
<br><br><code>PI_SCRIPT_INITING<br>PI_SCRIPT_HALTED<br>PI_SCRIPT_RUNNING<br>PI_SCRIPT_WAITING<br>PI_SCRIPT_FAILED<br></code><br><br>The current value of script parameters 0 to 9 are returned in param.
<br><br><code>script_id: >=0, as returned by <a href="#gpioStoreScript">gpioStoreScript</a><br></code><br><br>The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID.
<br><br><code>script_id: >=0, as returned by <a href="#gpioStoreScript">gpioStoreScript</a><br></code><br><br>The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID.
Starts a hardware clock on a GPIO at the specified frequency.
Frequencies above 30MHz are unlikely to work.
<br><br><code> gpio: see description<br>clkfreq: 0 (off) or 4689-250M (13184-375M for the BCM2711)<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_GPIO, PI_NOT_HCLK_GPIO,
PI_BAD_HCLK_FREQ,or PI_BAD_HCLK_PASS.
<br><br>The same clock is available on multiple GPIO. The latest
frequency setting will be used by all GPIO which share a clock.
<br><br>The GPIO must be one of the following.
<br><br><code>4 clock 0 All models<br>5 clock 1 All models but A and B (reserved for system use)<br>6 clock 2 All models but A and B<br>20 clock 0 All models but A and B<br>21 clock 1 All models but A and Rev.2 B (reserved for system use)<br><br>32 clock 0 Compute module only<br>34 clock 0 Compute module only<br>42 clock 1 Compute module only (reserved for system use)<br>43 clock 2 Compute module only<br>44 clock 1 Compute module only (reserved for system use)<br></code><br><br>Access to clock 1 is protected by a password as its use will likely
crash the Pi. The password is given by or'ing 0x5A000000 with the
Starts hardware PWM on a GPIO at the specified frequency and dutycycle.
Frequencies above 30MHz are unlikely to work.
<br><br>NOTE: Any waveform started by <a href="#gpioWaveTxSend">gpioWaveTxSend</a>, or
<a href="#gpioWaveChain">gpioWaveChain</a> will be cancelled.
<br><br>This function is only valid if the pigpio main clock is PCM. The
main clock defaults to PCM but may be overridden by a call to
<a href="#gpioCfgClock">gpioCfgClock</a>.
<br><br><code> gpio: see description<br>PWMfreq: 0 (off) or 1-125M (1-187.5M for the BCM2711)<br>PWMduty: 0 (off) to 1000000 (1M)(fully on)<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_GPIO, PI_NOT_HPWM_GPIO,
PI_BAD_HPWM_DUTY, PI_BAD_HPWM_FREQ, or PI_HPWM_ILLEGAL.
<br><br>The same PWM channel is available on multiple GPIO. The latest
frequency and dutycycle setting will be used by all GPIO which
share a PWM channel.
<br><br>The GPIO must be one of the following.
<br><br><code>12 PWM channel 0 All models but A and B<br>13 PWM channel 1 All models but A and B<br>18 PWM channel 0 All models<br>19 PWM channel 1 All models but A and B<br><br>40 PWM channel 0 Compute module only<br>41 PWM channel 1 Compute module only<br>45 PWM channel 1 Compute module only<br>52 PWM channel 0 Compute module only<br>53 PWM channel 1 Compute module only<br></code><br><br>The actual number of steps beween off and fully on is the
integral part of 250M/PWMfreq (375M/PWMfreq for the BCM2711).
<br><br>The actual frequency set is 250M/steps (375M/steps for the BCM2711).
<br><br>There will only be a million steps for a PWMfreq of 250 (375 for
the BCM2711). Lower frequencies will have more steps and higher
Delays for at least the number of microseconds specified by micros.
<br><br><code>micros: the number of microseconds to sleep<br></code><br><br>Returns the actual length of the delay in microseconds.
<br><br>Delays of 100 microseconds or less use busy waits.
<br><br><code> pad: 0-2, the pad to set<br>padStrength: 1-16 mA<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_PAD, or PI_BAD_STRENGTH.
This function uses the system call to execute a shell script
with the given string as its parameter.
<br><br><code> scriptName: the name of the script, only alphanumeric characters,<br> '-' and '_' are allowed in the name<br>scriptString: the string to pass to the script<br></code><br><br>The exit status of the system call is returned if OK, otherwise
PI_BAD_SHELL_STATUS.
<br><br>scriptName must exist in /opt/pigpio/cgi and must be executable.
<br><br>The returned exit status is normally 256 times that set by the
shell script exit function. If the script can't be found 32512 will
be returned.
<br><br>The following table gives some example returned statuses.
This function returns a handle to a file opened in a specified mode.
<br><br><code>file: the file to open<br>mode: the file open mode<br></code><br><br>Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE, PI_NO_FILE_ACCESS,
PI_BAD_FILE_MODE, PI_FILE_OPEN_FAILED, or PI_FILE_IS_A_DIR.
<br><br>File
<br><br>A file may only be opened if permission is granted by an entry in
/opt/pigpio/access. This is intended to allow remote access to files
in a more or less controlled manner.
<br><br>Each entry in /opt/pigpio/access takes the form of a file path
which may contain wildcards followed by a single letter permission.
The permission may be R for read, W for write, U for read/write,
and N for no access.
<br><br>Where more than one entry matches a file the most specific rule
applies. If no entry matches a file then access is denied.
<br><br>Suppose /opt/pigpio/access contains the following entries
<br><br><code>/home/* n<br>/home/pi/shared/dir_1/* w<br>/home/pi/shared/dir_2/* r<br>/home/pi/shared/dir_3/* u<br>/home/pi/shared/dir_1/file.txt n<br></code><br><br>Files may be written in directory dir_1 with the exception
of file.txt.
<br><br>Files may be read in directory dir_2.
<br><br>Files may be read and written in directory dir_3.
<br><br>If a directory allows read, write, or read/write access then files may
be created in that directory.
<br><br>In an attempt to prevent risky permissions the following paths are
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td>Macro</td><td>Value</td><td>Meaning</td></tr><tr><td>PI_FILE_READ</td><td>1</td><td>open file for reading</td></tr><tr><td>PI_FILE_WRITE</td><td>2</td><td>open file for writing</td></tr><tr><td>PI_FILE_RW</td><td>3</td><td>open file for reading and writing</td></tr></tbody></table><br><br>The following values may be or'd into the mode.
<br><br><table border="1" cellpadding="2" cellspacing="2"><tbody><tr><td>Macro</td><td>Value</td><td>Meaning</td></tr><tr><td>PI_FILE_APPEND</td><td>4</td><td>Writes append data to the end of the file</td></tr><tr><td>PI_FILE_CREATE</td><td>8</td><td>The file is created if it doesn't exist</td></tr><tr><td>PI_FILE_TRUNC</td><td>16</td><td>The file is truncated</td></tr></tbody></table><br><br>Newly created files are owned by root with permissions owner read and write.
This function reads up to count bytes from the the file
associated with handle and writes them to buf.
<br><br><code>handle: >=0, as returned by a call to <a href="#fileOpen">fileOpen</a><br> buf: an array to receive the read data<br> count: the maximum number of bytes to read<br></code><br><br>Returns the number of bytes read (>=0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, PI_FILE_NOT_ROPEN, or PI_BAD_FILE_WRITE.
This function seeks to a position within the file associated
with handle.
<br><br><code> handle: >=0, as returned by a call to <a href="#fileOpen">fileOpen</a><br>seekOffset: the number of bytes to move. Positive offsets<br> move forward, negative offsets backwards.<br> seekFrom: one of PI_FROM_START (0), PI_FROM_CURRENT (1),<br> or PI_FROM_END (2)<br></code><br><br>Returns the new byte position within the file (>=0) if OK, otherwise PI_BAD_HANDLE, or PI_BAD_FILE_SEEK.
This function returns a list of files which match a pattern. The
pattern may contain wildcards.
<br><br><code> fpat: file pattern to match<br> buf: an array to receive the matching file names<br>count: the maximum number of bytes to read<br></code><br><br>Returns the number of returned bytes if OK, otherwise PI_NO_FILE_ACCESS,
or PI_NO_FILE_MATCH.
<br><br>The pattern must match an entry in /opt/pigpio/access. The pattern
may contain wildcards. See <a href="#fileOpen">fileOpen</a>.
<br><br>NOTE
<br><br>The returned value is not the number of files, it is the number
of bytes in the buffer. The file names are separated by newline
Configures pigpio to use a particular sample rate timed by a specified
peripheral.
<br><br>This function is only effective if called before <a href="#gpioInitialise">gpioInitialise</a>.
<br><br><code> cfgMicros: 1, 2, 4, 5, 8, 10<br>cfgPeripheral: 0 (PWM), 1 (PCM)<br> cfgSource: deprecated, value is ignored<br></code><br><br>The timings are provided by the specified peripheral (PWM or PCM).
<br><br>The default setting is 5 microseconds using the PCM peripheral.
<br><br>The approximate CPU percentage used for each sample rate is:
<br><br><code>sample cpu<br> rate %<br><br> 1 25<br> 2 16<br> 4 11<br> 5 10<br> 8 15<br> 10 14<br></code><br><br>A sample rate of 5 microseconds seeems to be the sweet spot.
Configures pigpio to use the specified DMA channels.
<br><br>This function is only effective if called before <a href="#gpioInitialise">gpioInitialise</a>.
<br><br><code> primaryChannel: 0-14<br>secondaryChannel: 0-14<br></code><br><br>The default setting depends on whether the Pi has a BCM2711 chip or
not (currently only the Pi4B has a BCM2711).
<br><br>The default setting for a non-BCM2711 is to use channel 14 for the
primary channel and channel 6 for the secondary channel.
<br><br>The default setting for a BCM2711 is to use channel 7 for the
primary channel and channel 6 for the secondary channel.
<br><br>The secondary channel is only used for the transmission of waves.
<br><br>If possible use one of channels 0 to 6 for the secondary channel
(a full channel).
<br><br>A full channel only requires one DMA control block regardless of the
length of a pulse delay. Channels 7 to 14 (lite channels) require
one DMA control block for each 16383 microseconds of delay. I.e.
a 10 second pulse delay requires one control block on a full channel
Configures pigpio to restrict GPIO updates via the socket or pipe
interfaces to the GPIO specified by the mask. Programs directly
calling the pigpio library (i.e. linked with -lpigpio are not
affected). A GPIO update is a write to a GPIO or a GPIO mode
change or any function which would force such an action.
<br><br>This function is only effective if called before <a href="#gpioInitialise">gpioInitialise</a>.
<br><br><code>updateMask: bit (1<<n) is set for each GPIO n which may be updated<br></code><br><br>The default setting depends upon the Pi model. The user GPIO are
added to the mask.
<br><br>If the board revision is not recognised then GPIO 2-27 are allowed.
This function is available for user customisation.
<br><br>It returns a single integer value.
<br><br><code>arg1: >=0<br>arg2: >=0<br>argx: extra (byte) arguments<br>argc: number of extra arguments<br></code><br><br>Returns >= 0 if OK, less than 0 indicates a user defined error.
This function is available for user customisation.
<br><br>It differs from gpioCustom1 in that it returns an array of bytes
rather than just an integer.
<br><br>The returned value is an integer indicating the number of returned bytes.
<code> arg1: >=0<br> argx: extra (byte) arguments<br> argc: number of extra arguments<br>retBuf: buffer for returned bytes<br>retMax: maximum number of bytes to return<br></code><br><br>Returns >= 0 if OK, less than 0 indicates a user defined error.
<br><br>The number of returned bytes must be retMax or less.
This function adds a waveform representing SPI data to the
existing waveform (if any).
<br><br><code> spi: a pointer to a spi object<br> offset: microseconds from the start of the waveform<br> spiSS: the slave select GPIO<br> buf: the bits to transmit, most significant bit first<br> spiTxBits: the number of bits to write<br>spiBitFirst: the first bit to read<br> spiBitLast: the last bit to read<br> spiBits: the number of bits to transfer<br></code><br><br>Returns the new total number of pulses in the current waveform if OK,
otherwise PI_BAD_USER_GPIO, PI_BAD_SER_OFFSET, or PI_TOO_MANY_PULSES.
This function adds a number of pulses to the current waveform.
<br><br><code>numPulses: the number of pulses<br> pulses: the array containing the pulses<br></code><br><br>Returns the new total number of pulses in the current waveform if OK,
otherwise PI_TOO_MANY_PULSES.
<br><br>The advantage of this function over gpioWaveAddGeneric is that it
allows the setting of the flags field.
<br><br>The pulses are interleaved in time order within the existing waveform
(if any).
<br><br>Merging allows the waveform to be built in parts, that is the settings
for GPIO#1 can be added, and then GPIO#2 etc.
<br><br>If the added waveform is intended to start after or within the existing
waveform then the first pulse should consist of a delay.
<br><br><code> pos: the position of interest<br>lVal: the value to write<br></code><br><br>Not intended for general use.
Sets the wave output parameter stored at pos to value.
<br><br>DEPRECATED: use rawWaveSetOOL instead.
<br><br><code> pos: the position of interest<br>lVal: the value to write<br></code><br><br>Not intended for general use.
<br><br><code> pos: the position of interest<br>lVal: the value to write<br></code><br><br>Not intended for general use.
Used to print a readable version of a script to stderr.
<br><br><code>script_id: >=0, a script_id returned by <a href="#gpioStoreScript">gpioStoreScript</a><br></code><br><br>Not intended for general use.
A flag used to set normal or inverted bit bang serial data level logic.
<h3><a name="level">level</a></h3>
The level of a GPIO. Low or High.
<br><br><code>PI_OFF 0<br>PI_ON 1<br><br>PI_CLEAR 0<br>PI_SET 1<br><br>PI_LOW 0<br>PI_HIGH 1<br></code><br><br>There is one exception. If a watchdog expires on a GPIO the level will be
reported as PI_TIMEOUT. See <a href="#gpioSetWatchdog">gpioSetWatchdog</a>.
The GPIO used for the MISO signal when bit banging SPI.
<h3><a name="mode">mode</a></h3>
1. The operational mode of a GPIO, normally INPUT or OUTPUT.
<br><br><code>PI_INPUT 0<br>PI_OUTPUT 1<br>PI_ALT0 4<br>PI_ALT1 5<br>PI_ALT2 6<br>PI_ALT3 7<br>PI_ALT4 3<br>PI_ALT5 2<br></code><br><br>2. A file open mode.
<br><br><code>PI_FILE_READ 1<br>PI_FILE_WRITE 2<br>PI_FILE_RW 3<br></code><br><br>The following values can be or'd into the mode.
<code>PI_MIN_WDOG_TIMEOUT 0<br>PI_MAX_WDOG_TIMEOUT 60000<br></code><br><br><a href="#gpioSetISRFunc">gpioSetISRFunc</a> and <a href="#gpioSetISRFuncEx">gpioSetISRFuncEx</a>
A 64 bit mask indicating which GPIO may be written to by the user.
<br><br>If GPIO#n may be written then bit (1<<n) is set.
<h3><a name="user_gpio">user_gpio</a></h3>
0-31, a Broadcom numbered GPIO.
<br><br>See <a href="#gpio">gpio</a>.
<h3><a name="*userdata">*userdata</a></h3>
A pointer to arbitrary user data. This may be used to identify the instance.
<br><br>You must ensure that the pointer is in scope at the time it is processed. If
it is a pointer to a global this is automatic. Do not pass the address of a
local variable. If you want to pass a transient object then use the
following technique.
<br><br>In the calling function:
<br><br><code>user_type *userdata;<br><br>user_type my_userdata;<br><br>userdata = malloc(sizeof(user_type));<br><br>*userdata = my_userdata;<br></code><br><br>In the receiving function: