gcc -Wall -pthread -o prog prog.c -lpigpiod_if2 -lrt
sudo pigpiod
./prog # sudo is not required to run programs linked to pigpiod_if2
ESSENTIAL | |
pigpio_start | Connects to a pigpio daemon |
pigpio_stop | Disconnects from a pigpio daemon |
BASIC | |
set_mode | Set a GPIO mode |
get_mode | Get a GPIO mode |
set_pull_up_down | Set/clear GPIO pull up/down resistor |
gpio_read | Read a GPIO |
gpio_write | Write a GPIO |
PWM (overrides servo commands on same GPIO) | |
set_PWM_dutycycle | Start/stop PWM pulses on a GPIO |
set_PWM_frequency | Configure PWM frequency for a GPIO |
set_PWM_range | Configure PWM range for a GPIO |
get_PWM_dutycycle | Get the PWM dutycycle in use on a GPIO |
get_PWM_frequency | Get configured PWM frequency for a GPIO |
get_PWM_range | Get configured PWM range for a GPIO |
get_PWM_real_range | Get underlying PWM range for a GPIO |
Servo (overrides PWM commands on same GPIO) | |
set_servo_pulsewidth | Start/stop servo pulses on a GPIO |
get_servo_pulsewidth | Get the servo pulsewidth in use on a GPIO |
INTERMEDIATE | |
gpio_trigger | Send a trigger pulse to a GPIO. |
set_watchdog | Set a watchdog on a GPIO. |
read_bank_1 | Read all GPIO in bank 1 |
read_bank_2 | Read all GPIO in bank 2 |
clear_bank_1 | Clear selected GPIO in bank 1 |
clear_bank_2 | Clear selected GPIO in bank 2 |
set_bank_1 | Set selected GPIO in bank 1 |
set_bank_2 | Set selected GPIO in bank 2 |
callback | Create GPIO level change callback |
callback_ex | Create GPIO level change callback, extended |
callback_cancel | Cancel a callback |
wait_for_edge | Wait for GPIO level change |
start_thread | Start a new thread |
stop_thread | Stop a previously started thread |
ADVANCED | |
notify_open | Request a notification handle |
notify_begin | Start notifications for selected GPIO |
notify_pause | Pause notifications |
notify_close | Close a notification |
hardware_clock | Start hardware clock on supported GPIO |
hardware_PWM | Start hardware PWM on supported GPIO |
set_glitch_filter | Set a glitch filter on a GPIO |
set_noise_filter | Set a noise filter on a GPIO |
set_pad_strength | Sets a pads drive strength |
get_pad_strength | Gets a pads drive strength |
shell_ | Executes a shell command |
Custom | |
custom_1 | User custom function 1 |
custom_2 | User custom function 2 |
Events | |
event_callback | Sets a callback for an event |
event_callback_ex | Sets a callback for an event, extended |
event_callback_cancel | Cancel an event callback |
event_trigger | Triggers an event |
wait_for_event | Wait for an event |
Scripts | |
store_script | Store a script |
run_script | Run a stored script |
update_script | Set a scripts parameters |
script_status | Get script status and parameters |
stop_script | Stop a running script |
delete_script | Delete a stored script |
I2C | |
i2c_open | Opens an I2C device |
i2c_close | Closes an I2C device |
i2c_write_quick | smbus write quick |
i2c_read_byte | smbus read byte |
i2c_write_byte | smbus write byte |
i2c_read_byte_data | smbus read byte data |
i2c_write_byte_data | smbus write byte data |
i2c_read_word_data | smbus read word data |
i2c_write_word_data | smbus write word data |
i2c_read_block_data | smbus read block data |
i2c_write_block_data | smbus write block data |
i2c_read_i2c_block_data | smbus read I2C block data |
i2c_write_i2c_block_data | smbus write I2C block data |
i2c_read_device | Reads the raw I2C device |
i2c_write_device | Writes the raw I2C device |
i2c_process_call | smbus process call |
i2c_block_process_call | smbus block process call |
i2c_zip | Performs multiple I2C transactions |
I2C BIT BANG | |
bb_i2c_open | Opens GPIO for bit banging I2C |
bb_i2c_close | Closes GPIO for bit banging I2C |
bb_i2c_zip | Performs bit banged I2C transactions |
I2C/SPI SLAVE | |
bsc_xfer | I2C/SPI as slave transfer |
bsc_i2c | I2C as slave transfer |
SERIAL | |
serial_open | Opens a serial device |
serial_close | Closes a serial device |
serial_read_byte | Reads a byte from a serial device |
serial_write_byte | Writes a byte to a serial device |
serial_read | Reads bytes from a serial device |
serial_write | Writes bytes to a serial device |
serial_data_available | Returns number of bytes ready to be read |
SERIAL BIT BANG (read only) | |
bb_serial_read_open | Opens a GPIO for bit bang serial reads |
bb_serial_read_close | Closes a GPIO for bit bang serial reads |
bb_serial_invert | Invert serial logic (1 invert, 0 normal) |
bb_serial_read | Reads bit bang serial data from a GPIO |
SPI | |
spi_open | Opens a SPI device |
spi_close | Closes a SPI device |
spi_read | Reads bytes from a SPI device |
spi_write | Writes bytes to a SPI device |
spi_xfer | Transfers bytes with a SPI device |
SPI BIT BANG | |
bb_spi_open | Opens GPIO for bit banging SPI |
bb_spi_close | Closes GPIO for bit banging SPI |
bb_spi_xfer | Transfers bytes with bit banging SPI |
FILES | |
file_open | Opens a file |
file_close | Closes a file |
file_read | Reads bytes from a file |
file_write | Writes bytes to a file |
file_seek | Seeks to a position within a file |
file_list | List files which match a pattern |
WAVES | |
wave_clear | Deletes all waveforms |
wave_add_new | Starts a new waveform |
wave_add_generic | Adds a series of pulses to the waveform |
wave_add_serial | Adds serial data to the waveform |
wave_create | Creates a waveform from added data |
wave_create_and_pad | Creates a waveform of fixed size from added data |
wave_delete | Deletes one or more waveforms |
wave_send_once | Transmits a waveform once |
wave_send_repeat | Transmits a waveform repeatedly |
wave_send_using_mode | Transmits a waveform in the chosen mode |
wave_chain | Transmits a chain of waveforms |
wave_tx_at | Returns the current transmitting waveform |
wave_tx_busy | Checks to see if the waveform has ended |
wave_tx_stop | Aborts the current waveform |
wave_get_cbs | Length in cbs of the current waveform |
wave_get_high_cbs | Length of longest waveform so far |
wave_get_max_cbs | Absolute maximum allowed cbs |
wave_get_micros | Length in micros of the current waveform |
wave_get_high_micros | Length of longest waveform so far |
wave_get_max_micros | Absolute maximum allowed micros |
wave_get_pulses | Length in pulses of the current waveform |
wave_get_high_pulses | Length of longest waveform so far |
wave_get_max_pulses | Absolute maximum allowed pulses |
UTILITIES | |
get_current_tick | Get current tick (microseconds) |
get_hardware_revision | Get hardware revision |
get_pigpio_version | Get the pigpio version |
pigpiod_if_version | Get the pigpiod_if2 version |
pigpio_error | Get a text description of an error code. |
time_sleep | Sleeps for a float number of seconds |
time_time | Float number of seconds since the epoch |
seconds: the number of seconds to delay.
errnum: the error code.
thread_func: the main function for the new thread.
userdata: a pointer to an arbitrary argument.
pth: the thread to be stopped.
addrStr: specifies the host or IP address of the Pi running the
pigpio daemon. It may be NULL in which case localhost
is used unless overridden by the PIGPIO_ADDR environment
variable.
portStr: specifies the port address used by the Pi running the
pigpio daemon. It may be NULL in which case "8888"
is used unless overridden by the PIGPIO_PORT environment
variable.
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
gpio: 0-53.
mode: PI_INPUT, PI_OUTPUT, PI_ALT0, PI_ALT1,
PI_ALT2, PI_ALT3, PI_ALT4, PI_ALT5.
pi: >=0 (as returned by pigpio_start).
gpio: 0-53.
pi: >=0 (as returned by pigpio_start).
gpio: 0-53.
pud: PI_PUD_UP, PI_PUD_DOWN, PI_PUD_OFF.
pi: >=0 (as returned by pigpio_start).
gpio:0-53.
pi: >=0 (as returned by pigpio_start).
gpio: 0-53.
level: 0, 1.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
dutycycle: 0-range (range defaults to 255).
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
range: 25-40000.
25(#1), 50(#2), 100(#3), 125(#4), 200(#5), 250(#6),
400(#7), 500(#8), 625(#9), 800(#10), 1000(#11), 1250(#12),
2000(#13), 2500(#14), 4000(#15), 5000(#16), 10000(#17), 20000(#18)
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
frequency: >=0 (Hz).
Hertz
1: 40000 20000 10000 8000 5000 4000 2500 2000 1600
1250 1000 800 500 400 250 200 100 50
2: 20000 10000 5000 4000 2500 2000 1250 1000 800
625 500 400 250 200 125 100 50 25
4: 10000 5000 2500 2000 1250 1000 625 500 400
313 250 200 125 100 63 50 25 13
sample
rate
(us) 5: 8000 4000 2000 1600 1000 800 500 400 320
250 200 160 100 80 50 40 20 10
8: 5000 2500 1250 1000 625 500 313 250 200
156 125 100 63 50 31 25 13 6
10: 4000 2000 1000 800 500 400 250 200 160
125 100 80 50 40 25 20 10 5
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
pulsewidth: 0 (off), 500 (anti-clockwise) - 2500 (clockwise).
Update Rate (Hz) 50 100 200 400 500
1E6/Hz 20000 10000 5000 2500 2000
set_PWM_frequency(25, 400);
set_PWM_range(25, 2500);
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
handle: 0-31 (as returned by notify_open)
bits: a mask indicating the GPIO to be notified.
typedef struct
{
uint16_t seqno;
uint16_t flags;
uint32_t tick;
uint32_t level;
} gpioReport_t;
pi: >=0 (as returned by pigpio_start).
handle: 0-31 (as returned by notify_open)
pi: >=0 (as returned by pigpio_start).
handle: 0-31 (as returned by notify_open)
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
timeout: 0-60000.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31
steady: 0-300000
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31
steady: 0-300000
active: 0-1000000
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
bits: a bit mask with 1 set if the corresponding GPIO is
to be cleared.
pi: >=0 (as returned by pigpio_start).
bits: a bit mask with 1 set if the corresponding GPIO is
to be cleared.
pi: >=0 (as returned by pigpio_start).
bits: a bit mask with 1 set if the corresponding GPIO is
to be set.
pi: >=0 (as returned by pigpio_start).
bits: a bit mask with 1 set if the corresponding GPIO is
to be set.
pi: >=0 (as returned by pigpio_start).
gpio: see description
frequency: 0 (off) or 4689-250M (13184-375M for the BCM2711)
4 clock 0 All models
5 clock 1 All models but A and B (reserved for system use)
6 clock 2 All models but A and B
20 clock 0 All models but A and B
21 clock 1 All models but A and Rev.2 B (reserved for system use)
32 clock 0 Compute module only
34 clock 0 Compute module only
42 clock 1 Compute module only (reserved for system use)
43 clock 2 Compute module only
44 clock 1 Compute module only (reserved for system use)
pi: >=0 (as returned by pigpio_start).
gpio: see descripton
PWMfreq: 0 (off) or 1-125M (1-187.5M for the BCM2711)
PWMduty: 0 (off) to 1000000 (1M)(fully on)
12 PWM channel 0 All models but A and B
13 PWM channel 1 All models but A and B
18 PWM channel 0 All models
19 PWM channel 1 All models but A and B
40 PWM channel 0 Compute module only
41 PWM channel 1 Compute module only
45 PWM channel 1 Compute module only
52 PWM channel 0 Compute module only
53 PWM channel 1 Compute module only
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
numPulses: the number of pulses.
pulses: an array of pulses.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
baud: 50-1000000
data_bits: number of data bits (1-32)
stop_bits: number of stop half bits (2-8)
offset: >=0
numBytes: >=1
str: an array of chars.
pi: >=0 (as returned by pigpio_start).
typedef struct
{
uint32_t gpioOn;
uint32_t gpioOff;
uint32_t usDelay;
} gpioPulse_t;
pi: >=0 (as returned by pigpio_start).
percent: 0-100, size of waveform as percentage of maximum available.
pi: >=0 (as returned by pigpio_start).
wave_id: >=0, as returned by wave_create.
pi: >=0 (as returned by pigpio_start).
wave_id: >=0, as returned by wave_create.
pi: >=0 (as returned by pigpio_start).
wave_id: >=0, as returned by wave_create.
pi: >=0 (as returned by pigpio_start).
wave_id: >=0, as returned by wave_create.
mode: PI_WAVE_MODE_ONE_SHOT, PI_WAVE_MODE_REPEAT,
PI_WAVE_MODE_ONE_SHOT_SYNC, or PI_WAVE_MODE_REPEAT_SYNC.
pi: >=0 (as returned by pigpio_start).
buf: pointer to the wave_ids and optional command codes
bufSize: the number of bytes in buf
Name | Cmd & Data | Meaning |
Loop Start | 255 0 | Identify start of a wave block |
Loop Repeat | 255 1 x y | loop x + y*256 times |
Delay | 255 2 x y | delay x + y*256 microseconds |
Loop Forever | 255 3 | loop forever |
#include <stdio.h>
#include <pigpiod_if2.h>
#define WAVES 5
#define GPIO 4
int main(int argc, char *argv[])
{
int i, pi, wid[WAVES];
pi = pigpio_start(0, 0);
if (pi<0) return -1;
set_mode(pi, GPIO, PI_OUTPUT);
for (i=0; i<WAVES; i++)
{
wave_add_generic(pi, 2, (gpioPulse_t[])
{{1<<GPIO, 0, 20},
{0, 1<<GPIO, (i+1)*200}});
wid[i] = wave_create(pi);
}
wave_chain(pi, (char []) {
wid[4], wid[3], wid[2], // transmit waves 4+3+2
255, 0, // loop start
wid[0], wid[0], wid[0], // transmit waves 0+0+0
255, 0, // loop start
wid[0], wid[1], // transmit waves 0+1
255, 2, 0x88, 0x13, // delay 5000us
255, 1, 30, 0, // loop end (repeat 30 times)
255, 0, // loop start
wid[2], wid[3], wid[0], // transmit waves 2+3+0
wid[3], wid[1], wid[2], // transmit waves 3+1+2
255, 1, 10, 0, // loop end (repeat 10 times)
255, 1, 5, 0, // loop end (repeat 5 times)
wid[4], wid[4], wid[4], // transmit waves 4+4+4
255, 2, 0x20, 0x4E, // delay 20000us
wid[0], wid[0], wid[0], // transmit waves 0+0+0
}, 46);
while (wave_tx_busy(pi)) time_sleep(0.1);
for (i=0; i<WAVES; i++) wave_delete(pi, wid[i]);
pigpio_stop(pi);
}
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
pulseLen: 1-100.
level: 0,1.
pi: >=0 (as returned by pigpio_start).
script: the text of the script.
pi: >=0 (as returned by pigpio_start).
script_id: >=0, as returned by store_script.
numPar: 0-10, the number of parameters.
param: an array of parameters.
pi: >=0 (as returned by pigpio_start).
script_id: >=0, as returned by store_script.
numPar: 0-10, the number of parameters.
param: an array of parameters.
pi: >=0 (as returned by pigpio_start).
script_id: >=0, as returned by store_script.
param: an array to hold the returned 10 parameters.
PI_SCRIPT_INITING
PI_SCRIPT_HALTED
PI_SCRIPT_RUNNING
PI_SCRIPT_WAITING
PI_SCRIPT_FAILED
pi: >=0 (as returned by pigpio_start).
script_id: >=0, as returned by store_script.
pi: >=0 (as returned by pigpio_start).
script_id: >=0, as returned by store_script.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
baud: 50-250000
data_bits: 1-32
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31, previously opened with bb_serial_read_open.
buf: an array to receive the read bytes.
bufSize: >=0
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31, previously opened with bb_serial_read_open.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31, previously opened with bb_serial_read_open.
invert: 0-1, 1 invert, 0 normal.
pi: >=0 (as returned by pigpio_start).
i2c_bus: >=0.
i2c_addr: 0-0x7F.
i2c_flags: 0.
SDA | SCL | |
I2C 0 | 0 | 1 |
I2C 1 | 2 | 3 |
S (1 bit) : Start bit
P (1 bit) : Stop bit
Rd/Wr (1 bit) : Read/Write bit. Rd equals 1, Wr equals 0.
A, NA (1 bit) : Accept and not accept bit.
Addr (7 bits): I2C 7 bit address.
i2c_reg (8 bits): A byte which often selects a register.
Data (8 bits): A data byte.
Count (8 bits): A byte defining the length of a block operation.
[..]: Data sent by the device.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
bit: 0-1, the value to write.
S Addr bit [A] P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
bVal: 0-0xFF, the value to write.
S Addr Wr [A] bVal [A] P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
S Addr Rd [A] [Data] NA P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to write.
bVal: 0-0xFF, the value to write.
S Addr Wr [A] i2c_reg [A] bVal [A] P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to write.
wVal: 0-0xFFFF, the value to write.
S Addr Wr [A] i2c_reg [A] wval_Low [A] wVal_High [A] P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to read.
S Addr Wr [A] i2c_reg [A] S Addr Rd [A] [Data] NA P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to read.
S Addr Wr [A] i2c_reg [A]
S Addr Rd [A] [DataLow] A [DataHigh] NA P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to write/read.
wVal: 0-0xFFFF, the value to write.
S Addr Wr [A] i2c_reg [A] wVal_Low [A] wVal_High [A]
S Addr Rd [A] [DataLow] A [DataHigh] NA P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to write.
buf: an array with the data to send.
count: 1-32, the number of bytes to write.
S Addr Wr [A] i2c_reg [A] count [A] buf0 [A] buf1 [A] ...
[A] bufn [A] P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to read.
buf: an array to receive the read data.
S Addr Wr [A] i2c_reg [A]
S Addr Rd [A] [Count] A [buf0] A [buf1] A ... A [bufn] NA P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to write/read.
buf: an array with the data to send and to receive the read data.
count: 1-32, the number of bytes to write.
S Addr Wr [A] i2c_reg [A] count [A] buf0 [A] ...
S Addr Rd [A] [Count] A [Data] ... A P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to read.
buf: an array to receive the read data.
count: 1-32, the number of bytes to read.
S Addr Wr [A] i2c_reg [A]
S Addr Rd [A] [buf0] A [buf1] A ... A [bufn] NA P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to write.
buf: the data to write.
count: 1-32, the number of bytes to write.
S Addr Wr [A] i2c_reg [A] buf0 [A] buf1 [A] ... [A] bufn [A] P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
buf: an array to receive the read data bytes.
count: >0, the number of bytes to read.
S Addr Rd [A] [buf0] A [buf1] A ... A [bufn] NA P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2c_open.
buf: an array containing the data bytes to write.
count: >0, the number of bytes to write.
S Addr Wr [A] buf0 [A] buf1 [A] ... [A] bufn [A] P
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to i2cOpen
inBuf: pointer to the concatenated I2C commands, see below
inLen: size of command buffer
outBuf: pointer to buffer to hold returned data
outLen: size of output buffer
Name | Cmd & Data | Meaning |
End | 0 | No more commands |
Escape | 1 | Next P is two bytes |
On | 2 | Switch combined flag on |
Off | 3 | Switch combined flag off |
Address | 4 P | Set I2C address to P |
Flags | 5 lsb msb | Set I2C flags to lsb + (msb << 8) |
Read | 6 P | Read P bytes of data |
Write | 7 P ... | Write P bytes of data |
Set address 0x53, write 0x32, read 6 bytes
Set address 0x1E, write 0x03, read 6 bytes
Set address 0x68, write 0x1B, read 8 bytes
End
0x04 0x53 0x07 0x01 0x32 0x06 0x06
0x04 0x1E 0x07 0x01 0x03 0x06 0x06
0x04 0x68 0x07 0x01 0x1B 0x06 0x08
0x00
pi: >=0 (as returned by pigpio_start).
SDA: 0-31
SCL: 0-31
baud: 50-500000
pi: >=0 (as returned by pigpio_start).
SDA: 0-31, the SDA GPIO used in a prior call to bb_i2c_open
pi: >=0 (as returned by pigpio_start).
SDA: 0-31 (as used in a prior call to bb_i2c_open)
inBuf: pointer to the concatenated I2C commands, see below
inLen: size of command buffer
outBuf: pointer to buffer to hold returned data
outLen: size of output buffer
Name | Cmd & Data | Meaning |
End | 0 | No more commands |
Escape | 1 | Next P is two bytes |
Start | 2 | Start condition |
Stop | 3 | Stop condition |
Address | 4 P | Set I2C address to P |
Flags | 5 lsb msb | Set I2C flags to lsb + (msb << 8) |
Read | 6 P | Read P bytes of data |
Write | 7 P ... | Write P bytes of data |
Set address 0x53
start, write 0x32, (re)start, read 6 bytes, stop
Set address 0x1E
start, write 0x03, (re)start, read 6 bytes, stop
Set address 0x68
start, write 0x1B, (re)start, read 8 bytes, stop
End
0x04 0x53
0x02 0x07 0x01 0x32 0x02 0x06 0x06 0x03
0x04 0x1E
0x02 0x07 0x01 0x03 0x02 0x06 0x06 0x03
0x04 0x68
0x02 0x07 0x01 0x1B 0x02 0x06 0x08 0x03
0x00
pi: >=0 (as returned by pigpio_start).
CS: 0-31
MISO: 0-31
MOSI: 0-31
SCLK: 0-31
baud: 50-250000
spi_flags: see below
21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 R T 0 0 0 0 0 0 0 0 0 0 0 p m m
Mode CPOL CPHA
0 0 0
1 0 1
2 1 0
3 1 1
bb_spi_open(pi,10, MISO, MOSI, SCLK, 10000, 0); // device 1
bb_spi_open(pi,11, MISO, MOSI, SCLK, 20000, 3); // device 2
pi: >=0 (as returned by pigpio_start).
CS: 0-31, the CS GPIO used in a prior call to bb_spi_open
pi: >=0 (as returned by pigpio_start).
CS: 0-31 (as used in a prior call to bb_spi_open)
txBuf: pointer to buffer to hold data to be sent
rxBuf: pointer to buffer to hold returned data
count: size of data transfer
// gcc -Wall -pthread -o bb_spi_x_test bb_spi_x_test.c -lpigpiod_if2
// ./bb_spi_x_test
#include <stdio.h>
#include "pigpiod_if2.h"
#define CE0 5
#define CE1 6
#define MISO 13
#define MOSI 19
#define SCLK 12
int main(int argc, char *argv[])
{
int i, pi, count, set_val, read_val;
unsigned char inBuf[3];
char cmd1[] = {0, 0};
char cmd2[] = {12, 0};
char cmd3[] = {1, 128, 0};
if ((pi = pigpio_start(0, 0)) < 0)
{
fprintf(stderr, "pigpio initialisation failed (%d).\n", pi);
return 1;
}
bb_spi_open(pi, CE0, MISO, MOSI, SCLK, 10000, 0); // MCP4251 DAC
bb_spi_open(pi, CE1, MISO, MOSI, SCLK, 20000, 3); // MCP3008 ADC
for (i=0; i<256; i++)
{
cmd1[1] = i;
count = bb_spi_xfer(pi, CE0, cmd1, (char *)inBuf, 2); // > DAC
if (count == 2)
{
count = bb_spi_xfer(pi, CE0, cmd2, (char *)inBuf, 2); // < DAC
if (count == 2)
{
set_val = inBuf[1];
count = bb_spi_xfer(pi, CE1, cmd3, (char *)inBuf, 3); // < ADC
if (count == 3)
{
read_val = ((inBuf[1]&3)<<8) | inBuf[2];
printf("%d %d\n", set_val, read_val);
}
}
}
}
bb_spi_close(pi, CE0);
bb_spi_close(pi, CE1);
pigpio_stop(pi);
}
MISO | MOSI | SCLK | CE0 | CE1 | CE2 | |
Main SPI | 9 | 10 | 11 | 8 | 7 | - |
Aux SPI | 19 | 20 | 21 | 18 | 17 | 16 |
pi: >=0 (as returned by pigpio_start).
spi_channel: 0-1 (0-2 for the auxiliary SPI).
baud: 32K-125M (values above 30M are unlikely to work).
spi_flags: see below.
21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
b b b b b b R T n n n n W A u2 u1 u0 p2 p1 p0 m m
Mode POL PHA
0 0 0
1 0 1
2 1 0
3 1 1
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to spi_open.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to spi_open.
buf: an array to receive the read data bytes.
count: the number of bytes to read.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to spi_open.
buf: the data bytes to write.
count: the number of bytes to write.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to spi_open.
txBuf: the data bytes to write.
rxBuf: the received data bytes.
count: the number of bytes to transfer.
pi: >=0 (as returned by pigpio_start).
ser_tty: the serial device to open.
baud: the baud rate in bits per second, see below.
ser_flags: 0.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to serial_open.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to serial_open.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to serial_open.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to serial_open.
buf: the array of bytes to write.
count: the number of bytes to write.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to serial_open.
buf: an array to receive the read data.
count: the maximum number of bytes to read.
pi: >=0 (as returned by pigpio_start).
handle: >=0, as returned by a call to serial_open.
pi: >=0 (as returned by pigpio_start).
arg1: >=0
arg2: >=0
argx: extra (byte) arguments
argc: number of extra arguments
pi: >=0 (as returned by pigpio_start).
arg1: >=0
argc: extra (byte) arguments
count: number of extra arguments
retBuf: buffer for returned data
retMax: maximum number of bytes to return
pi: >=0 (as returned by pigpio_start).
pad: 0-2, the pad to get.
Pad | GPIO |
0 | 0-27 |
1 | 28-45 |
2 | 46-53 |
strength = get_pad_strength(pi, 0); // get pad 0 strength
pi: >=0 (as returned by pigpio_start).
pad: 0-2, the pad to set.
padStrength: 1-16 mA.
Pad | GPIO |
0 | 0-27 |
1 | 28-45 |
2 | 46-53 |
set_pad_strength(pi, 0, 10); // set pad 0 strength to 10 mA
pi: >=0 (as returned by pigpio_start).
scriptName: the name of the script, only alphanumeric characters,
'-' and '_' are allowed in the name.
scriptString: the string to pass to the script.
Script exit status | Returned system call status |
1 | 256 |
5 | 1280 |
10 | 2560 |
200 | 51200 |
script not found | 32512 |
// pass two parameters, hello and world
status = shell_(pi, "scr1", "hello world");
// pass three parameters, hello, string with spaces, and world
status = shell_(pi, "scr1", "hello 'string with spaces' world");
// pass one parameter, hello string with spaces world
status = shell_(pi, "scr1", "\"hello string with spaces world\"");
pi: >=0 (as returned by pigpio_start).
file: the file to open.
mode: the file open mode.
/home/* n
/home/pi/shared/dir_1/* w
/home/pi/shared/dir_2/* r
/home/pi/shared/dir_3/* u
/home/pi/shared/dir_1/file.txt n
a path containing ..
a path containing only wildcards (*?)
a path containing less than two non-wildcard parts
Macro | Value | Meaning |
PI_FILE_READ | 1 | open file for reading |
PI_FILE_WRITE | 2 | open file for writing |
PI_FILE_RW | 3 | open file for reading and writing |
Macro | Value | Meaning |
PI_FILE_APPEND | 4 | Writes append data to the end of the file |
PI_FILE_CREATE | 8 | The file is created if it doesn't exist |
PI_FILE_TRUNC | 16 | The file is truncated |
#include <stdio.h>
#include <pigpiod_if2.h>
int main(int argc, char *argv[])
{
int pi, handle, c;
char buf[60000];
pi = pigpio_start(NULL, NULL);
if (pi < 0) return 1;
// assumes /opt/pigpio/access contains the following line
// /ram/*.c r
handle = file_open(pi, "/ram/pigpio.c", PI_FILE_READ);
if (handle >= 0)
{
while ((c=file_read(pi, handle, buf, sizeof(buf)-1)))
{
buf[c] = 0;
printf("%s", buf);
}
file_close(pi, handle);
}
pigpio_stop(pi);
}
pi: >=0 (as returned by pigpio_start).
handle: >=0 (as returned by file_open).
file_close(pi, handle);
pi: >=0 (as returned by pigpio_start).
handle: >=0 (as returned by file_open).
buf: the array of bytes to write.
count: the number of bytes to write.
if (file_write(pi, handle, buf, 100) == 0)
{
// file written okay
}
else
{
// error
}
pi: >=0 (as returned by pigpio_start).
handle: >=0 (as returned by file_open).
buf: an array to receive the read data.
count: the maximum number of bytes to read.
bytes = file_read(pi, handle, buf, sizeof(buf));
if (bytes >= 0)
{
// process read data
}
pi: >=0 (as returned by pigpio_start).
handle: >=0 (as returned by file_open).
seekOffset: the number of bytes to move. Positive offsets
move forward, negative offsets backwards.
seekFrom: one of PI_FROM_START (0), PI_FROM_CURRENT (1),
or PI_FROM_END (2).
file_seek(pi, handle, 123, PI_FROM_START); // Start plus 123
size = file_seek(pi, handle, 0, PI_FROM_END); // End, return size
pos = file_seek(pi, handle, 0, PI_FROM_CURRENT); // Current position
pi: >=0 (as returned by pigpio_start).
fpat: file pattern to match.
buf: an array to receive the matching file names.
count: the maximum number of bytes to read.
#include <stdio.h>
#include <pigpiod_if2.h>
int main(int argc, char *argv[])
{
int pi, handle, c;
char buf[60000];
pi = pigpio_start(NULL, NULL);
if (pi < 0) return 1;
// assumes /opt/pigpio/access contains the following line
// /ram/*.c r
c = file_list(pi, "/ram/p*.c", buf, sizeof(buf));
if (c >= 0)
{
buf[c] = 0;
printf("%s", buf);
}
pigpio_stop(pi);
}
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE.
f: the callback function.
Parameter Value Meaning
GPIO 0-31 The GPIO which has changed state
edge 0-2 0 = change to low (a falling edge)
1 = change to high (a rising edge)
2 = no level change (a watchdog timeout)
tick 32 bit The number of microseconds since boot
WARNING: this wraps around from
4294967295 to 0 roughly every 72 minutes
samples
per sec
1 1,000,000
2 500,000
sample 4 250,000
rate 5 200,000
(us) 8 125,000
10 100,000
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE.
f: the callback function.
userdata: a pointer to arbitrary user data.
Parameter Value Meaning
GPIO 0-31 The GPIO which has changed state
edge 0-2 0 = change to low (a falling edge)
1 = change to high (a rising edge)
2 = no level change (a watchdog timeout)
tick 32 bit The number of microseconds since boot
WARNING: this wraps around from
4294967295 to 0 roughly every 72 minutes
userdata pointer Pointer to an arbitrary object
callback_id: >=0, as returned by a call to callback or callback_ex.
pi: >=0 (as returned by pigpio_start).
user_gpio: 0-31.
edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE.
timeout: >=0.
pi: >=0 (as returned by pigpio_start).
bscxfer: a structure defining the transfer.
typedef struct
{
uint32_t control; // Write
int rxCnt; // Read only
char rxBuf[BSC_FIFO_SIZE]; // Read only
int txCnt; // Write
char txBuf[BSC_FIFO_SIZE]; // Write
} bsc_xfer_t;
SDA | SCL | MOSI | SCLK | MISO | CE | |
I2C | 18 | 19 | - | - | - | - |
SPI | - | - | 18 | 19 | 20 | 21 |
SDA | SCL | MOSI | SCLK | MISO | CE | |
I2C | 10 | 11 | - | - | - | - |
SPI | - | - | 10 | 11 | 9 | 8 |
22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
a a a a a a a - - IT HC TF IR RE TE BK EC ES PL PH I2 SP EN
aaaaaaa | defines the I2C slave address (only relevant in I2C mode) |
IT | invert transmit status flags |
HC | enable host control |
TF | enable test FIFO |
IR | invert receive status flags |
RE | enable receive |
TE | enable transmit |
BK | abort operation and clear FIFOs |
EC | send control register as first I2C byte |
ES | send status register as first I2C byte |
PL | set SPI polarity high |
PH | set SPI phase high |
I2 | enable I2C mode |
SP | enable SPI mode |
EN | enable BSC peripheral |
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
S S S S S R R R R R T T T T T RB TE RF TF RE TB
SSSSS | number of bytes successfully copied to transmit FIFO |
RRRRR | number of bytes in receieve FIFO |
TTTTT | number of bytes in transmit FIFO |
RB | receive busy |
TE | transmit FIFO empty |
RF | receive FIFO full |
TF | transmit FIFO full |
RE | receive FIFO empty |
TB | transmit busy |
bsc_xfer_t xfer;
xfer.control = (0x13<<16) | 0x305;
memcpy(xfer.txBuf, "ABCD", 4);
xfer.txCnt = 4;
status = bsc_xfer(pi, &xfer);
if (status >= 0)
{
// process transfer
}
pi: >=0 (as returned by pigpio_start).
i2c_addr: 0-0x7F.
bscxfer: a structure defining the transfer.
typedef struct
{
uint32_t control; // N/A
int rxCnt; // Read only
char rxBuf[BSC_FIFO_SIZE]; // Read only
int txCnt; // Write
char txBuf[BSC_FIFO_SIZE]; // Write
} bsc_xfer_t;
pi: >=0 (as returned by pigpio_start).
event: 0-31.
f: the callback function.
pi: >=0 (as returned by pigpio_start).
event: 0-31.
f: the callback function.
userdata: a pointer to arbitrary user data.
callback_id: >=0, as returned by a call to event_callback or
event_callback_ex.
pi: >=0 (as returned by pigpio_start).
event: 0-31.
timeout: >=0.
pi: >=0 (as returned by pigpio_start).
event: 0-31.
typedef struct
{
uint32_t control; // Write
int rxCnt; // Read only
char rxBuf[BSC_FIFO_SIZE]; // Read only
int txCnt; // Write
char txBuf[BSC_FIFO_SIZE]; // Write
} bsc_xfer_t;
typedef void (*CBFunc_t)
(int pi, unsigned user_gpio, unsigned level, uint32_t tick);
typedef void (*CBFuncEx_t)
(int pi, unsigned user_gpio, unsigned level, uint32_t tick, void * userdata);
#define PI_MIN_WAVE_DATABITS 1
#define PI_MAX_WAVE_DATABITS 32
RISING_EDGE 0
FALLING_EDGE 1
EITHER_EDGE. 2
typedef void (*evtCBFunc_t)
(int pi, unsigned event, uint32_t tick);
typedef void (*evtCBFuncEx_t)
(int pi, unsigned event, uint32_t tick, void *userdata);
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Type 1 X X - - X - - X X X X X - - X X
Type 2 - - X X X - - X X X X X - - X X
Type 3 X X X X X X X X X X X X X X
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Type 1 - X X - - X X X X X - - - - - -
Type 2 - X X - - - X X X X - X X X X X
Type 3 X X X X X X X X X X X X - - - -
typedef struct
{
uint32_t gpioOn;
uint32_t gpioOff;
uint32_t usDelay;
} gpioPulse_t;
typedef void *(gpioThreadFunc_t) (void *);
PI_OFF 0
PI_ON 1
PI_CLEAR 0
PI_SET 1
PI_LOW 0
PI_HIGH 1
PI_TIMEOUT 2
PI_INPUT 0
PI_OUTPUT 1
PI_ALT0 4
PI_ALT1 5
PI_ALT2 6
PI_ALT3 7
PI_ALT4 3
PI_ALT5 2
PI_WAVE_MODE_ONE_SHOT 0
PI_WAVE_MODE_REPEAT 1
PI_WAVE_MODE_ONE_SHOT_SYNC 2
PI_WAVE_MODE_REPEAT_SYNC 3
PI_FILE_READ 1
PI_FILE_WRITE 2
PI_FILE_RW 3
PI_FILE_APPEND 4
PI_FILE_CREATE 8
PI_FILE_TRUNC 16
Pad | GPIO |
0 | 0-27 |
1 | 28-45 |
2 | 46-53 |
PI_PUD_OFF 0
PI_PUD_DOWN 1
PI_PUD_UP 2
PI_SERVO_OFF 0
PI_MIN_SERVO_PULSEWIDTH 500
PI_MAX_SERVO_PULSEWIDTH 2500
#define PI_HW_PWM_RANGE 1000000
#define PI_HW_PWM_MIN_FREQ 1
#define PI_HW_PWM_MAX_FREQ 125000000
#define PI_HW_PWM_MAX_FREQ_2711 187500000
PI_MIN_DUTYCYCLE_RANGE 25
PI_MAX_DUTYCYCLE_RANGE 40000
PI_FROM_START 0
PI_FROM_CURRENT 1
PI_FROM_END 2
#define PI_MIN_WAVE_HALFSTOPBITS 2
#define PI_MAX_WAVE_HALFSTOPBITS 8
PI_MIN_WDOG_TIMEOUT 0
PI_MAX_WDOG_TIMEOUT 60000
user_type *userdata;
user_type my_userdata;
userdata = malloc(sizeof(user_type));
*userdata = my_userdata;
user_type my_userdata = *(user_type*)userdata;
free(userdata);
typedef enum
{
pigif_bad_send = -2000,
pigif_bad_recv = -2001,
pigif_bad_getaddrinfo = -2002,
pigif_bad_connect = -2003,
pigif_bad_socket = -2004,
pigif_bad_noib = -2005,
pigif_duplicate_callback = -2006,
pigif_bad_malloc = -2007,
pigif_bad_callback = -2008,
pigif_notify_failed = -2009,
pigif_callback_not_found = -2010,
pigif_unconnected_pi = -2011,
pigif_too_many_pis = -2012,
} pigifError_t;