<metaname="description"content="Raspberry Pi Reg. C GPIO library and Python GPIO module and shell command utilities to control the GPIO, including SPI, I2C, and serial links."/>
<td><center><h2>pigpiod_if</h2></center><br><br>THIS LIBRARY IS DEPRECATED. NEW CODE SHOULD BE WRITTEN TO
USE THE MORE VERSATILE pigpiod_if2 LIBRARY.
<br><br>pigpiod_if is a C library for the Raspberry which allows control
of the GPIO via the socket interface to the pigpio daemon.<br>
<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 on the pigpio daemon
<h3>GPIO</h3>ALL GPIO are identified by their Broadcom number.
<h3>Notes</h3>The PWM and servo pulses are timed using the DMA and PWM/PCM peripherals.
<h3>Usage</h3>Include <pigpiod_if.h> in your source files.
<br><br>Assuming your source is in prog.c use the following command to build
<br><br><code>gcc -Wall -pthread -o prog prog.c -lpigpiod_if -lrt<br></code><br><br>to run make sure the pigpio daemon is running
<br><br><code>sudo pigpiod<br><br> ./prog # sudo is not required to run programs linked to pigpiod_if<br></code><br><br>For examples see x_pigpiod_if.c within the pigpio archive file.
<h3>Notes</h3>All the functions which return an int return < 0 on error
</b></td><td></td></tr><tr><td></td><td></td></tr><tr><td><ahref="#get_PWM_real_range">get_PWM_real_range</a></td><td> Get underlying PWM range for a GPIO
</td></tr><tr><td></td><td></td></tr><tr><td><ahref="#notify_open">notify_open</a></td><td> Request a notification handle
</td></tr><tr><td><ahref="#notify_begin">notify_begin</a></td><td> Start notifications for selected GPIO
</b></td><td></td></tr><tr><td></td><td></td></tr><tr><td><ahref="#serial_open">serial_open</a></td><td> Opens a serial device
</td></tr><tr><td><ahref="#serial_close">serial_close</a></td><td> Closes a serial device
</td></tr><tr><td></td><td></td></tr><tr><td><ahref="#serial_write_byte">serial_write_byte</a></td><td> Writes a byte to a serial device
</td></tr><tr><td><ahref="#serial_read_byte">serial_read_byte</a></td><td> Reads a byte from a serial device
</td></tr><tr><td><ahref="#serial_write">serial_write</a></td><td> Writes bytes to a serial device
</td></tr><tr><td><ahref="#serial_read">serial_read</a></td><td> Reads bytes from a serial device
</td></tr><tr><td></td><td></td></tr><tr><td><ahref="#serial_data_available">serial_data_available</a></td><td> Returns number of bytes ready to be read
Starts a new thread of execution with thread_func as the main routine.
<br><br><code>thread_func: the main function for the new thread.<br> userdata: a pointer to an arbitrary argument.<br></code><br><br>Returns a pointer to pthread_t if OK, otherwise NULL.
<br><br>The function is passed the single argument userdata.
<br><br>The thread can be cancelled by passing the pointer to pthread_t to
Set the range of PWM values to be used on the GPIO.
<br><br><code>user_gpio: 0-31.<br> range: 25-40000.<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_DUTYRANGE,
or PI_NOT_PERMITTED.
<br><br>Notes
<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 of the 18 available GPIO frequencies is
<br><br><code> 25(#1), 50(#2), 100(#3), 125(#4), 200(#5), 250(#6),<br> 400(#7), 500(#8), 625(#9), 800(#10), 1000(#11), 1250(#12),<br>2000(#13), 2500(#14), 4000(#15), 5000(#16), 10000(#17), 20000(#18)<br></code><br><br>The real value set by set_PWM_range is (dutycycle * real range) / range.
Start (500-2500) or stop (0) servo pulses on the GPIO.
<br><br><code> user_gpio: 0-31.<br>pulsewidth: 0 (off), 500 (anti-clockwise) - 2500 (clockwise).<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_PULSEWIDTH or
PI_NOT_PERMITTED.
<br><br>The selected pulsewidth will continue to be transmitted until
changed by a subsequent call to set_servo_pulsewidth.
<br><br>The pulsewidths 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.
<br><br>You can DAMAGE a servo if you command it to move beyond its limits.
<br><br>OTHER UPDATE RATES:
<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>Update Rate (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 <ahref="#set_PWM_frequency">set_PWM_frequency</a>.
<br><br>Then set the PWM range using <ahref="#set_PWM_range">set_PWM_range</a> to 1E6/Hz.
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 GPIO 25 at 400Hz
<br><br><code>set_PWM_frequency(25, 400);<br>set_PWM_range(25, 2500);<br></code><br><br>Thereafter use the <ahref="#set_PWM_dutycycle">set_PWM_dutycycle</a> function to move the servo,
e.g. set_PWM_dutycycle(25, 1500) will set a 1500 us pulse.<br>
Start notifications on a previously opened handle.
<br><br><code>handle: 0-31 (as returned by <ahref="#notify_open">notify_open</a>)<br> bits: a mask indicating the GPIO to be notified.<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 as follows:
<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: two flags are defined, PI_NTFY_FLAGS_WDOG and PI_NTFY_FLAGS_ALIVE.
<br><br>PI_NTFY_FLAGS_WDOG, if bit 5 is set then bits 0-4 of the flags
indicate a GPIO which has had a watchdog timeout.
<br><br>PI_NTFY_FLAGS_ALIVE, if bit 6 is set 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>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
<br><br>Level changes on the GPIO are not reported unless the level
has been stable for at least <ahref="#steady">steady</a> microseconds. The
level is then reported. Level changes of less than <ahref="#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 <ahref="#callback">callback</a>, <ahref="#callback_ex">callback_ex</a> and <ahref="#wait_for_edge">wait_for_edge</a>.
<br><br>It does not affect levels read by <ahref="#gpio_read">gpio_read</a>,
<ahref="#read_bank_1">read_bank_1</a>, or <ahref="#read_bank_2">read_bank_2</a>.
Each (stable) edge will be timestamped <ahref="#steady">steady</a> microseconds
<br><br>Level changes on the GPIO are ignored until a level which has
been stable for <ahref="#steady">steady</a> microseconds is detected. Level changes
on the GPIO are then reported for <ahref="#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 <ahref="#callback">callback</a>, <ahref="#callback_ex">callback_ex</a> and <ahref="#wait_for_edge">wait_for_edge</a>.
<br><br>It does not affect levels read by <ahref="#gpio_read">gpio_read</a>,
<ahref="#read_bank_1">read_bank_1</a>, or <ahref="#read_bank_2">read_bank_2</a>.
<br><br>Level changes before and after the active period may
be reported. Your software must be designed to cope with
<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
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 solely 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: number of data bits (1-32)<br>stop_bits: number of stop half bits (2-8)<br> offset: >=0<br> numBytes: >=1<br> str: an array of chars.<br></code><br><br>Returns the new total number of pulses in the current waveform if OK,
This function deletes the waveform with id wave_id.
<br><br><code>wave_id: >=0, as returned by <ahref="#wave_create">wave_create</a>.<br></code><br><br>Wave ids are allocated in order, 0, 1, 2, etc.
<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>Returns 0 if OK, otherwise PI_BAD_WAVE_ID.
This function transmits the waveform with id wave_id. The waveform
is sent once.
<br><br>NOTE: Any hardware PWM started by <ahref="#hardware_PWM">hardware_PWM</a> will be cancelled.
<br><br><code>wave_id: >=0, as returned by <ahref="#wave_create">wave_create</a>.<br></code><br><br>Returns the number of DMA control blocks in the waveform if OK,
This function transmits the waveform with id wave_id. The waveform
cycles until cancelled (either by the sending of a new waveform or
by <ahref="#wave_tx_stop">wave_tx_stop</a>).
<br><br>NOTE: Any hardware PWM started by <ahref="#hardware_PWM">hardware_PWM</a> will be cancelled.
<br><br><code>wave_id: >=0, as returned by <ahref="#wave_create">wave_create</a>.<br></code><br><br>Returns the number of DMA control blocks in the waveform if OK,
<br><br>NOTE: Any hardware PWM started by <ahref="#hardware_PWM">hardware_PWM</a> will be cancelled.
<br><br>The waves to be transmitted are specified by the contents of buf
which contains an ordered list of <ahref="#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><tableborder="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
<br><br><code>script_id: >=0, as returned by <ahref="#store_script">store_script</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 <ahref="#store_script">store_script</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 <ahref="#store_script">store_script</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 <ahref="#store_script">store_script</a>.<br></code><br><br>The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID.
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 <ahref="#bb_serial_read_open">bb_serial_read_open</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 <ahref="#data_bits">data_bits</a> specified in the <ahref="#bb_serial_read_open">bb_serial_read_open</a> command.
<br><br>For <ahref="#data_bits">data_bits</a> 1-8 there will be one byte per character.<br>
For <ahref="#data_bits">data_bits</a> 9-16 there will be two bytes per character.<br>
For <ahref="#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 <ahref="#bb_serial_read_open">bb_serial_read_open</a>.<br></code><br><br>Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_SERIAL_GPIO.
This function inverts serial logic for big bang serial reads.
<br><br><code>user_gpio: 0-31, previously opened with <ahref="#bb_serial_read_open">bb_serial_read_open</a>.<br> invert: 0-1, 1 invert, 0 normal.<br></code><br><br>Returns 0 if OK, otherwise PI_NOT_SERIAL_GPIO or PI_BAD_SER_INVERT.
This returns a handle for the device at address i2c_addr on bus i2c_bus.
<br><br><code> i2c_bus: >=0.<br> i2c_addr: 0-0x7F.<br>i2c_flags: 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><tableborder="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 <ahref="#i2c_open">i2c_open</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 <ahref="#i2c_open">i2c_open</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 <ahref="#i2c_open">i2c_open</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 <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 and reads 16 bits of data in return.
<br><br><code> handle: >=0, as returned by a call to <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 <ahref="#i2c_open">i2c_open</a>.<br>i2c_reg: 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 <ahref="#i2c_open">i2c_open</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 <ahref="#i2c_open">i2c_open</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 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 <ahref="#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><tableborder="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 <ahref="#bb_i2c_open">bb_i2c_open</a>.
<br><br><code>SDA: 0-31, the SDA GPIO used in a prior call to <ahref="#bb_i2c_open">bb_i2c_open</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 <ahref="#bb_i2c_open">bb_i2c_open</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><tableborder="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 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><tableborder="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>spi_channel: 0-1 (0-2 for the auxiliary SPI).<br> baud: 32K-125M (values above 30M are unlikely to work).<br> spi_flags: 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>spi_flags 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 <ahref="#spi_read">spi_read</a>, <ahref="#spi_write">spi_write</a>, and <ahref="#spi_xfer">spi_xfer</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 <ahref="#spi_open">spi_open</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 <ahref="#spi_open">spi_open</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 <ahref="#spi_open">spi_open</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
with specified flags. The device name must start with
/dev/tty or /dev/serial.
<br><br><code> ser_tty: the serial device to open.<br> baud: the baud rate in bits per second, see below.<br>ser_flags: 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 <ahref="#serial_open">serial_open</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 <ahref="#serial_open">serial_open</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 <ahref="#serial_open">serial_open</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 to 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 <ahref="#serial_open">serial_open</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_SER_READ_NO_DATA, or PI_SER_WRITE_FAILED.
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 <ahref="#serial_open">serial_open</a>.<br></code><br><br>Returns the number of bytes of data available (>=0) if OK,
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 custom_1 in that it returns an array of bytes
rather than just an integer.
<br><br>The return value is an integer indicating the number of returned bytes.
<code> arg1: >=0<br> argc: extra (byte) arguments<br> count: number of extra arguments<br>retBuf: buffer for returned data<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>Note, the number of returned bytes will be retMax or less.
<br><br><code>user_gpio: 0-31.<br> edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE.<br> f: the callback function.<br></code><br><br>The function returns a callback id if OK, otherwise pigif_bad_malloc,
pigif_duplicate_callback, or pigif_bad_callback.
<br><br>The callback is called with the GPIO, edge, and tick, whenever the
<br><br><code>user_gpio: 0-31.<br> edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE.<br> f: the callback function.<br> userdata: a pointer to arbitrary user data.<br></code><br><br>The function returns a callback id if OK, otherwise pigif_bad_malloc,
pigif_duplicate_callback, or pigif_bad_callback.
<br><br>The callback is called with the GPIO, edge, tick, and user, whenever
This function cancels a callback identified by its id.
<br><br><code>callback_id: >=0, as returned by a call to <ahref="#callback">callback</a> or <ahref="#callback_ex">callback_ex</a>.<br></code><br><br>The function returns 0 if OK, otherwise pigif_callback_not_found.
This function waits for edge on the GPIO for up to timeout
seconds.
<br><br><code>user_gpio: 0-31.<br> edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE.<br> timeout: >=0.<br></code><br><br>The function returns 1 if the edge occurred, otherwise 0.
<br><br>The function returns when the edge occurs or after the timeout.
A number referencing an object opened by one of <ahref="#i2c_open">i2c_open</a>, <ahref="#notify_open">notify_open</a>,
<ahref="#serial_open">serial_open</a>, and <ahref="#spi_open">spi_open</a>.
<h3><aname="i2c_addr">i2c_addr</a>: 0-0x7F</h3>
The address of a device on the I2C bus.
<h3><aname="i2c_bus">i2c_bus</a>: >=0</h3>
An I2C bus number.
<h3><aname="i2c_flags">i2c_flags</a>: 0</h3>
Flags which modify an I2C open command. None are currently defined.
<h3><aname="i2c_reg">i2c_reg</a>: 0-255</h3>
A register of an I2C device.
<h3><aname="*inBuf">*inBuf</a></h3>
A buffer used to pass data to a function.
<h3><aname="inLen">inLen</a></h3>
The number of bytes of data in a buffer.
<h3><aname="int">int</a></h3>
A whole number, negative or positive.
<h3><aname="invert">invert</a></h3>
A flag used to set normal or inverted bit bang serial data level logic.
<h3><aname="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 <ahref="#set_watchdog">set_watchdog</a>.