pigpiod_if(3) | pigpio archive | pigpiod_if(3) |
pigpiod_if - A C library to interface to the pigpio daemon.
#include <pigpiod_if.h>
gcc -Wall -pthread -o prog prog.c -lpigpiod_if -lrt
./prog
THIS LIBRARY IS DEPRECATED. NEW CODE SHOULD BE WRITTEN TO USE THE MORE VERSATILE pigpiod_if2 LIBRARY.
pigpiod_if is a C library for the Raspberry which allows control of the GPIO via the socket interface to the pigpio daemon.
o hardware timed PWM on any of GPIO 0-31
o hardware timed servo pulses on any of GPIO 0-31
o callbacks when any of GPIO 0-31 change state
o callbacks at timed intervals
o reading/writing all of the GPIO in a bank as one operation
o individually setting GPIO modes, reading and writing
o notifications when any of GPIO 0-31 change state
o the construction of output waveforms with microsecond timing
o rudimentary permission control over GPIO
o a simple interface to start and stop new threads
o I2C, SPI, and serial link wrappers
o creating and running scripts on the pigpio daemon
ALL GPIO are identified by their Broadcom number.
The PWM and servo pulses are timed using the DMA and PWM/PCM peripherals.
Include <pigpiod_if.h> in your source files.
Assuming your source is in prog.c use the following command to build
gcc -Wall -pthread -o prog prog.c -lpigpiod_if -lrt
to run make sure the pigpio daemon is running
sudo pigpiod
./prog # sudo is not required to run programs linked to pigpiod_if
For examples see x_pigpiod_if.c within the pigpio archive file.
All the functions which return an int return < 0 on error
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.
Returns a pointer to pthread_t if OK, otherwise NULL.
The function is passed the single argument userdata.
The thread can be cancelled by passing the pointer to pthread_t to stop_thread.
pth: the thread to be stopped.
No value is returned.
The thread to be stopped should have been started with start_thread.
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.
gpio: 0-53.
mode: PI_INPUT, PI_OUTPUT, PI_ALT0, PI_ALT1,
PI_ALT2, PI_ALT3, PI_ALT4, PI_ALT5.
Returns 0 if OK, otherwise PI_BAD_GPIO, PI_BAD_MODE, or PI_NOT_PERMITTED.
gpio: 0-53.
Returns the GPIO mode if OK, otherwise PI_BAD_GPIO.
gpio: 0-53.
pud: PI_PUD_UP, PI_PUD_DOWN, PI_PUD_OFF.
Returns 0 if OK, otherwise PI_BAD_GPIO, PI_BAD_PUD, or PI_NOT_PERMITTED.
gpio:0-53.
Returns the GPIO level if OK, otherwise PI_BAD_GPIO.
gpio: 0-53.
level: 0, 1.
Returns 0 if OK, otherwise PI_BAD_GPIO, PI_BAD_LEVEL, or PI_NOT_PERMITTED.
Notes
If PWM or servo pulses are active on the GPIO they are switched off.
user_gpio: 0-31.
dutycycle: 0-range (range defaults to 255).
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_DUTYCYCLE, or PI_NOT_PERMITTED. Notes
The set_PWM_range function may be used to change the default range of 255.
user_gpio: 0-31.
Returns 0 if OK, otherwise PI_BAD_USER_GPIO or PI_NOT_PWM_GPIO.
For normal PWM the dutycycle will be out of the defined range for the GPIO (see get_PWM_range).
If a hardware clock is active on the GPIO the reported dutycycle will be 500000 (500k) out of 1000000 (1M).
If hardware PWM is active on the GPIO the reported dutycycle will be out of a 1000000 (1M).
user_gpio: 0-31.
range: 25-40000.
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_DUTYRANGE, or PI_NOT_PERMITTED.
Notes
If PWM is currently active on the GPIO its dutycycle will be scaled to reflect the new range.
The real range, the number of steps between fully off and fully on for each of the 18 available GPIO frequencies is
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)
The real value set by set_PWM_range is (dutycycle * real range) / range.
user_gpio: 0-31.
Returns the dutycycle range used for the GPIO if OK, otherwise PI_BAD_USER_GPIO.
If a hardware clock or hardware PWM is active on the GPIO the reported range will be 1000000 (1M).
user_gpio: 0-31.
Returns the real range used for the GPIO if OK, otherwise PI_BAD_USER_GPIO.
If a hardware clock is active on the GPIO the reported real range will be 1000000 (1M).
If hardware PWM is active on the GPIO the reported real range will be approximately 250M divided by the set PWM frequency.
user_gpio: 0-31.
frequency: >=0 (Hz).
Returns the numerically closest frequency if OK, otherwise PI_BAD_USER_GPIO or PI_NOT_PERMITTED.
If PWM is currently active on the GPIO it will be switched off and then back on at the new frequency.
Each GPIO can be independently set to one of 18 different PWM frequencies.
The selectable frequencies depend upon the sample rate which may be 1, 2, 4, 5, 8, or 10 microseconds (default 5). The sample rate is set when the pigpio daemon is started.
The frequencies for each sample rate are:
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
user_gpio: 0-31.
For normal PWM the frequency will be that defined for the GPIO by set_PWM_frequency.
If a hardware clock is active on the GPIO the reported frequency will be that set by hardware_clock.
If hardware PWM is active on the GPIO the reported frequency will be that set by hardware_PWM.
Returns the frequency (in hertz) used for the GPIO if OK, otherwise PI_BAD_USER_GPIO.
user_gpio: 0-31.
pulsewidth: 0 (off), 500 (anti-clockwise) - 2500 (clockwise).
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_PULSEWIDTH or PI_NOT_PERMITTED.
The selected pulsewidth will continue to be transmitted until changed by a subsequent call to set_servo_pulsewidth.
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.
You can DAMAGE a servo if you command it to move beyond its limits.
OTHER UPDATE RATES:
This function updates servos at 50Hz. If you wish to use a different update frequency you will have to use the PWM functions.
Update Rate (Hz) 50 100 200 400 500
1E6/Hz 20000 10000 5000 2500 2000
Firstly set the desired PWM frequency using set_PWM_frequency.
Then set the PWM range using set_PWM_range to 1E6/Hz. Doing this allows you to use units of microseconds when setting the servo pulsewidth.
E.g. If you want to update a servo connected to GPIO 25 at 400Hz
set_PWM_frequency(25, 400);
set_PWM_range(25, 2500);
Thereafter use the set_PWM_dutycycle function to move the servo, e.g. set_PWM_dutycycle(25, 1500) will set a 1500 us pulse.
user_gpio: 0-31.
Returns 0 if OK, otherwise PI_BAD_USER_GPIO or PI_NOT_SERVO_GPIO.
Returns a handle greater than or equal to zero if OK, otherwise PI_NO_HANDLE.
A notification is a method for being notified of GPIO state changes via a pipe.
Pipes are only accessible from the local machine so this function serves no purpose if you are using the library from a remote machine. The in-built (socket) notifications provided by callback should be used instead.
Notifications for handle x will be available at the pipe named /dev/pigpiox (where x is the handle number). E.g. if the function returns 15 then the notifications must be read from /dev/pigpio15.
handle: 0-31 (as returned by notify_open)
bits: a mask indicating the GPIO to be notified.
Returns 0 if OK, otherwise PI_BAD_HANDLE.
The notification sends state changes for each GPIO whose corresponding bit in bits is set.
Each notification occupies 12 bytes in the fifo as follows:
typedef struct
{
uint16_t seqno;
uint16_t flags;
uint32_t tick;
uint32_t level;
} gpioReport_t;
seqno: starts at 0 each time the handle is opened and then increments by one for each report.
flags: two flags are defined, PI_NTFY_FLAGS_WDOG and PI_NTFY_FLAGS_ALIVE.
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.
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.
tick: the number of microseconds since system boot. It wraps around after 1h12m.
level: indicates the level of each GPIO. If bit 1<<x is set then GPIO x is high.
handle: 0-31 (as returned by notify_open)
Returns 0 if OK, otherwise PI_BAD_HANDLE.
Notifications for the handle are suspended until notify_begin is called again.
handle: 0-31 (as returned by notify_open)
Returns 0 if OK, otherwise PI_BAD_HANDLE.
user_gpio: 0-31.
timeout: 0-60000.
Returns 0 if OK, otherwise PI_BAD_USER_GPIO or PI_BAD_WDOG_TIMEOUT.
The watchdog is nominally in milliseconds.
Only one watchdog may be registered per GPIO.
The watchdog may be cancelled by setting timeout to 0.
Once a watchdog has been started callbacks for the GPIO will be triggered every timeout interval after the last GPIO activity.
The callback will receive the special level PI_TIMEOUT.
Level changes on the GPIO are not reported unless the level has been stable for at least steady microseconds. The level is then reported. Level changes of less than steady microseconds are ignored.
user_gpio: 0-31
steady: 0-300000
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER.
This filter affects the GPIO samples returned to callbacks set up with callback, callback_ex and wait_for_edge.
It does not affect levels read by gpio_read, read_bank_1, or read_bank_2. Each (stable) edge will be timestamped steady microseconds after it was first detected.
Level changes on the GPIO are ignored until a level which has been stable for steady microseconds is detected. Level changes on the GPIO are then reported for active microseconds after which the process repeats.
user_gpio: 0-31
steady: 0-300000
active: 0-1000000
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER.
This filter affects the GPIO samples returned to callbacks set up with callback, callback_ex and wait_for_edge.
It does not affect levels read by gpio_read, read_bank_1, or read_bank_2.
Level changes before and after the active period may be reported. Your software must be designed to cope with such reports.
The returned 32 bit integer has a bit set if the corresponding GPIO is logic 1. GPIO n has bit value (1<<n).
The returned 32 bit integer has a bit set if the corresponding GPIO is logic 1. GPIO n has bit value (1<<(n-32)).
bits: a bit mask with 1 set if the corresponding GPIO is
to be cleared.
Returns 0 if OK, otherwise PI_SOME_PERMITTED.
A status of PI_SOME_PERMITTED indicates that the user is not allowed to write to one or more of the GPIO.
bits: a bit mask with 1 set if the corresponding GPIO is
to be cleared.
Returns 0 if OK, otherwise PI_SOME_PERMITTED.
A status of PI_SOME_PERMITTED indicates that the user is not allowed to write to one or more of the GPIO.
bits: a bit mask with 1 set if the corresponding GPIO is
to be set.
Returns 0 if OK, otherwise PI_SOME_PERMITTED.
A status of PI_SOME_PERMITTED indicates that the user is not allowed to write to one or more of the GPIO.
bits: a bit mask with 1 set if the corresponding GPIO is
to be set.
Returns 0 if OK, otherwise PI_SOME_PERMITTED.
A status of PI_SOME_PERMITTED indicates that the user is not allowed to write to one or more of the GPIO.
gpio: see description
frequency: 0 (off) or 4689-250000000 (250M)
Returns 0 if OK, otherwise PI_NOT_PERMITTED, PI_BAD_GPIO, PI_NOT_HCLK_GPIO, PI_BAD_HCLK_FREQ,or PI_BAD_HCLK_PASS.
The same clock is available on multiple GPIO. The latest frequency setting will be used by all GPIO which share a clock.
The GPIO must be one of the following.
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)
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 GPIO number.
NOTE: Any waveform started by wave_send_once, wave_send_repeat, or wave_chain will be cancelled.
This function is only valid if the pigpio main clock is PCM. The main clock defaults to PCM but may be overridden when the pigpio daemon is started (option -t).
gpio: see descripton
PWMfreq: 0 (off) or 1-125000000 (125M)
PWMduty: 0 (off) to 1000000 (1M)(fully on)
Returns 0 if OK, otherwise PI_NOT_PERMITTED, PI_BAD_GPIO, PI_NOT_HPWM_GPIO, PI_BAD_HPWM_DUTY, PI_BAD_HPWM_FREQ, or PI_HPWM_ILLEGAL.
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.
The GPIO must be one of the following.
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
Tick is the number of microseconds since system boot.
As tick is an unsigned 32 bit quantity it wraps around after 2**32 microseconds, which is approximately 1 hour 12 minutes.
The hardware revision is the last few characters on the Revision line of /proc/cpuinfo.
If the hardware revision can not be found or is not a valid hexadecimal number the function returns 0.
The revision number can be used to determine the assignment of GPIO to pins (see gpio).
There are at least three types of board.
Type 1 boards have hardware revision numbers of 2 and 3.
Type 2 boards have hardware revision numbers of 4, 5, 6, and 15.
Type 3 boards have hardware revision numbers of 16 or greater.
Returns 0 if OK.
Returns 0 if OK.
numPulses: the number of pulses.
pulses: an array of pulses.
Returns the new total number of pulses in the current waveform if OK, otherwise PI_TOO_MANY_PULSES.
The pulses are interleaved in time order within the existing waveform (if any).
Merging allows the waveform to be built in parts, that is the settings for GPIO#1 can be added, and then GPIO#2 etc.
If the added waveform is intended to start after or within the existing waveform then the first pulse should consist solely of a delay.
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.
Returns the new total number of pulses in the current waveform if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_WAVE_BAUD, PI_BAD_DATABITS, PI_BAD_STOP_BITS, PI_TOO_MANY_CHARS, PI_BAD_SER_OFFSET, or PI_TOO_MANY_PULSES.
NOTES:
The serial data is formatted as one start bit, data_bits data bits, and stop_bits/2 stop bits.
It is legal to add serial data streams with different baud rates to the same waveform.
numBytes is the number of bytes of data in str.
The bytes required for each character depend upon data_bits.
For data_bits 1-8 there will be one byte per character.
For data_bits 9-16 there will be two bytes per character.
For data_bits 17-32 there will be four bytes per character.
The data provided by the wave_add_* functions is consumed by this function.
As many waveforms may be created as there is space available. The wave id is passed to wave_send_* to specify the waveform to transmit.
Normal usage would be
Step 1. wave_clear to clear all waveforms and added data.
Step 2. wave_add_* calls to supply the waveform data.
Step 3. wave_create to create the waveform and get a unique id
Repeat steps 2 and 3 as needed.
Step 4. wave_send_* with the id of the waveform to transmit.
A waveform comprises one or more pulses. Each pulse consists of a gpioPulse_t structure.
typedef struct
{
uint32_t gpioOn;
uint32_t gpioOff;
uint32_t usDelay;
} gpioPulse_t;
The fields specify
1) the GPIO to be switched on at the start of the pulse.
2) the GPIO to be switched off at the start of the pulse.
3) the delay in microseconds before the next pulse.
Any or all the fields can be zero. It doesn't make any sense to set all the fields to zero (the pulse will be ignored).
When a waveform is started each pulse is executed in order with the specified delay between the pulse and the next.
Returns the new waveform id if OK, otherwise PI_EMPTY_WAVEFORM, PI_NO_WAVEFORM_ID, PI_TOO_MANY_CBS, or PI_TOO_MANY_OOL.
wave_id: >=0, as returned by wave_create.
Wave ids are allocated in order, 0, 1, 2, etc.
The wave is flagged for deletion. The resources used by the wave will only be reused when either of the following apply.
- all waves with higher numbered wave ids have been deleted or have been flagged for deletion.
- a new wave is created which uses exactly the same resources as the current wave (see the C source for gpioWaveCreate for details).
Returns 0 if OK, otherwise PI_BAD_WAVE_ID.
NOTE: Any hardware PWM started by hardware_PWM will be cancelled.
wave_id: >=0, as returned by wave_create.
Returns the number of DMA control blocks in the waveform if OK, otherwise PI_BAD_WAVE_ID, or PI_BAD_WAVE_MODE.
NOTE: Any hardware PWM started by hardware_PWM will be cancelled.
wave_id: >=0, as returned by wave_create.
Returns the number of DMA control blocks in the waveform if OK, otherwise PI_BAD_WAVE_ID, or PI_BAD_WAVE_MODE.
NOTE: Any hardware PWM started by hardware_PWM will be cancelled.
The waves to be transmitted are specified by the contents of buf which contains an ordered list of wave_ids and optional command codes and related data.
buf: pointer to the wave_ids and optional command codes
bufSize: the number of bytes in buf
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.
Each wave is transmitted in the order specified. A wave may occur multiple times per chain.
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.
Delays between waves may be added with the delay command.
The following command codes are supported:
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
If present Loop Forever must be the last entry in the chain.
The code is currently dimensioned to support a chain with roughly 600 entries and 20 loop counters.
Example
#include <stdio.h>
#include <pigpiod_if.h>
#define WAVES 5
#define GPIO 4
int main(int argc, char *argv[])
{
int i, wid[WAVES];
if (pigpio_start(0, 0)<0) return -1;
set_mode(GPIO, PI_OUTPUT);
for (i=0; i<WAVES; i++)
{
wave_add_generic(2, (gpioPulse_t[])
{{1<<GPIO, 0, 20},
{0, 1<<GPIO, (i+1)*200}});
wid[i] = wave_create();
}
wave_chain((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()) time_sleep(0.1);
for (i=0; i<WAVES; i++) wave_delete(wid[i]);
pigpio_stop();
}
Returns 1 if a waveform is currently being transmitted, otherwise 0.
Returns 0 if OK.
This function is intended to stop a waveform started with the repeat mode.
user_gpio: 0-31.
pulseLen: 1-100.
level: 0,1.
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_LEVEL, PI_BAD_PULSELEN, or PI_NOT_PERMITTED.
See http://abyz.me.uk/rpi/pigpio/pigs.html#Scripts for details.
script: the text of the script.
The function returns a script id if the script is valid, otherwise PI_BAD_SCRIPT.
script_id: >=0, as returned by store_script.
numPar: 0-10, the number of parameters.
param: an array of parameters.
The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID, or PI_TOO_MANY_PARAM
param is an array of up to 10 parameters which may be referenced in the script as p0 to p9.
script_id: >=0, as returned by store_script.
param: an array to hold the returned 10 parameters.
The function returns greater than or equal to 0 if OK, otherwise PI_BAD_SCRIPT_ID.
The run status may be
PI_SCRIPT_INITING
PI_SCRIPT_HALTED
PI_SCRIPT_RUNNING
PI_SCRIPT_WAITING
PI_SCRIPT_FAILED
The current value of script parameters 0 to 9 are returned in param.
script_id: >=0, as returned by store_script.
The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID.
script_id: >=0, as returned by store_script.
The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID.
user_gpio: 0-31.
baud: 50-250000
data_bits: 1-32
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_WAVE_BAUD, or PI_GPIO_IN_USE.
The serial data is returned in a cyclic buffer and is read using bb_serial_read.
It is the caller's responsibility to read data from the cyclic buffer in a timely fashion.
user_gpio: 0-31, previously opened with bb_serial_read_open.
buf: an array to receive the read bytes.
bufSize: >=0
Returns the number of bytes copied if OK, otherwise PI_BAD_USER_GPIO or PI_NOT_SERIAL_GPIO.
The bytes returned for each character depend upon the number of data bits data_bits specified in the bb_serial_read_open command.
For data_bits 1-8 there will be one byte per character.
For data_bits 9-16 there will be two bytes per character.
For data_bits 17-32 there will be four bytes per character.
user_gpio: 0-31, previously opened with bb_serial_read_open.
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_SERIAL_GPIO.
user_gpio: 0-31, previously opened with bb_serial_read_open.
invert: 0-1, 1 invert, 0 normal.
Returns 0 if OK, otherwise PI_NOT_SERIAL_GPIO or PI_BAD_SER_INVERT.
i2c_bus: >=0.
i2c_addr: 0-0x7F.
i2c_flags: 0.
No flags are currently defined. This parameter should be set to zero.
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.
The GPIO used are given in the following table.
SDA SCL
I2C 0 0 1
I2C 1 2 3
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.
For the SMBus commands the low level transactions are shown at the end of the function description. The following abbreviations are used.
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.
Comm (8 bits): Command byte, a data byte which often selects a register.
Data (8 bits): A data byte.
Count (8 bits): A data byte containing the length of a block operation.
[..]: Data sent by the device.
handle: >=0, as returned by a call to i2c_open.
Returns 0 if OK, otherwise PI_BAD_HANDLE.
handle: >=0, as returned by a call to i2c_open.
bit: 0-1, the value to write.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_WRITE_FAILED.
Quick command. SMBus 2.0 5.5.1
S Addr Rd/Wr [A] P
handle: >=0, as returned by a call to i2c_open.
bVal: 0-0xFF, the value to write.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_WRITE_FAILED.
Send byte. SMBus 2.0 5.5.2
S Addr Wr [A] Data [A] P
handle: >=0, as returned by a call to i2c_open.
Returns the byte read (>=0) if OK, otherwise PI_BAD_HANDLE, or PI_I2C_READ_FAILED.
Receive byte. SMBus 2.0 5.5.3
S Addr Rd [A] [Data] NA P
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.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_WRITE_FAILED.
Write byte. SMBus 2.0 5.5.4
S Addr Wr [A] Comm [A] Data [A] P
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.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_WRITE_FAILED.
Write word. SMBus 2.0 5.5.4
S Addr Wr [A] Comm [A] DataLow [A] DataHigh [A] P
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to read.
Returns the byte read (>=0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_READ_FAILED.
Read byte. SMBus 2.0 5.5.5
S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P
handle: >=0, as returned by a call to i2c_open.
i2c_reg: 0-255, the register to read.
Returns the word read (>=0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_READ_FAILED.
Read word. SMBus 2.0 5.5.5
S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
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.
Returns the word read (>=0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_READ_FAILED.
Process call. SMBus 2.0 5.5.6
S Addr Wr [A] Comm [A] DataLow [A] DataHigh [A]
S Addr Rd [A] [DataLow] A [DataHigh] NA P
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.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_WRITE_FAILED.
Block write. SMBus 2.0 5.5.7
S Addr Wr [A] Comm [A] Count [A] Data [A] Data [A] ... [A] Data [A] P
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.
The amount of returned data is set by the device.
Returns the number of bytes read (>=0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_READ_FAILED.
Block read. SMBus 2.0 5.5.7
S Addr Wr [A] Comm [A]
S Addr Rd [A] [Count] A [Data] A [Data] A ... A [Data] NA P
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.
Returns the number of bytes read (>=0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_READ_FAILED.
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 bytes sent/received must be 32 or less.
Block write-block read. SMBus 2.0 5.5.8
S Addr Wr [A] Comm [A] Count [A] Data [A] ...
S Addr Rd [A] [Count] A [Data] ... A P
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.
Returns the number of bytes read (>0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_READ_FAILED.
S Addr Wr [A] Comm [A]
S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P
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.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_WRITE_FAILED.
S Addr Wr [A] Comm [A] Data [A] Data [A] ... [A] Data [A] P
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.
Returns count (>0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_READ_FAILED.
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.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_I2C_WRITE_FAILED.
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
Returns >= 0 if OK (the number of bytes read), otherwise PI_BAD_HANDLE, PI_BAD_POINTER, PI_BAD_I2C_CMD, PI_BAD_I2C_RLEN. PI_BAD_I2C_WLEN, or PI_BAD_I2C_SEG.
The following command codes are supported:
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
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).
The address defaults to that associated with the handle. The flags default to 0. The address and flags maintain their previous value until updated.
The returned I2C data is stored in consecutive locations of outBuf.
Example
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
Bit banging I2C allows for certain operations which are not possible with the standard I2C driver.
o baud rates as low as 50
o repeated starts
o clock stretching
o I2C on any pair of spare GPIO
SDA: 0-31
SCL: 0-31
baud: 50-500000
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_I2C_BAUD, or PI_GPIO_IN_USE.
NOTE:
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.
SDA: 0-31, the SDA GPIO used in a prior call to bb_i2c_open
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_I2C_GPIO.
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
Returns >= 0 if OK (the number of bytes read), otherwise PI_BAD_USER_GPIO, PI_NOT_I2C_GPIO, PI_BAD_POINTER, PI_BAD_I2C_CMD, PI_BAD_I2C_RLEN, PI_BAD_I2C_WLEN, PI_I2C_READ_FAILED, or PI_I2C_WRITE_FAILED.
The following command codes are supported:
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
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).
The address and flags default to 0. The address and flags maintain their previous value until updated.
No flags are currently defined.
The returned I2C data is stored in consecutive locations of outBuf.
Example
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
The Pi has two SPI peripherals: main and auxiliary.
The main SPI has two chip selects (channels), the auxiliary has three.
The auxiliary SPI is available on all models but the A and B.
The GPIO used are given in the following table.
MISO MOSI SCLK CE0 CE1 CE2
Main SPI 9 10 11 8 7 -
Aux SPI 19 20 21 18 17 16
spi_channel: 0-1 (0-2 for the auxiliary SPI).
baud: 32K-125M (values above 30M are unlikely to work).
spi_flags: see below.
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.
spi_flags consists of the least significant 22 bits.
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
mm defines the SPI mode.
Warning: modes 1 and 3 do not appear to work on the auxiliary SPI.
Mode POL PHA
0 0 0
1 0 1
2 1 0
3 1 1
px is 0 if CEx is active low (default) and 1 for active high.
ux is 0 if the CEx GPIO is reserved for SPI (default) and 1 otherwise.
A is 0 for the main SPI, 1 for the auxiliary SPI.
W is 0 if the device is not 3-wire, 1 if the device is 3-wire. Main SPI only.
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.
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.
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.
bbbbbb defines the word size in bits (0-32). The default (0) sets 8 bits per word. Auxiliary SPI only.
The spi_read, spi_write, and spi_xfer functions transfer data packed into 1, 2, or 4 bytes according to the word size in bits.
For bits 1-8 there will be one byte per word.
For bits 9-16 there will be two bytes per word.
For bits 17-32 there will be four bytes per word.
Multi-byte transfers are made in least significant byte first order.
E.g. to transfer 32 11-bit words buf should contain 64 bytes and count should be 64.
E.g. to transfer the 14 bit value 0x1ABC send the bytes 0xBC followed by 0x1A.
The other bits in flags should be set to zero.
handle: >=0, as returned by a call to spi_open.
Returns 0 if OK, otherwise PI_BAD_HANDLE.
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.
Returns the number of bytes transferred if OK, otherwise PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED.
handle: >=0, as returned by a call to spi_open.
buf: the data bytes to write.
count: the number of bytes to write.
Returns the number of bytes transferred if OK, otherwise PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED.
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.
Returns the number of bytes transferred if OK, otherwise PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED.
ser_tty: the serial device to open.
baud: the baud rate in bits per second, see below.
ser_flags: 0.
Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE, or PI_SER_OPEN_FAILED.
The baud rate must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, or 230400.
No flags are currently defined. This parameter should be set to zero.
handle: >=0, as returned by a call to serial_open.
Returns 0 if OK, otherwise PI_BAD_HANDLE.
handle: >=0, as returned by a call to serial_open.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_SER_WRITE_FAILED.
handle: >=0, as returned by a call to serial_open.
Returns the read byte (>=0) if OK, otherwise PI_BAD_HANDLE, PI_SER_READ_NO_DATA, or PI_SER_READ_FAILED.
If no data is ready PI_SER_READ_NO_DATA is returned.
handle: >=0, as returned by a call to serial_open.
buf: the array of bytes to write.
count: the number of bytes to write.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or PI_SER_WRITE_FAILED.
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.
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.
If no data is ready zero is returned.
handle: >=0, as returned by a call to serial_open.
Returns the number of bytes of data available (>=0) if OK, otherwise PI_BAD_HANDLE.
It returns a single integer value.
arg1: >=0
arg2: >=0
argx: extra (byte) arguments
argc: number of extra arguments
Returns >= 0 if OK, less than 0 indicates a user defined error.
It differs from custom_1 in that it returns an array of bytes rather than just an integer.
The return value is an integer indicating the number of returned bytes.
arg1: >=0
argc: extra (byte) arguments
count: number of extra arguments
retBuf: buffer for returned data
retMax: maximum number of bytes to return
Returns >= 0 if OK, less than 0 indicates a user defined error.
Note, the number of returned bytes will be retMax or less.
user_gpio: 0-31.
edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE.
f: the callback function.
The function returns a callback id if OK, otherwise pigif_bad_malloc, pigif_duplicate_callback, or pigif_bad_callback.
The callback is called with the GPIO, edge, and tick, whenever the GPIO has the identified edge.
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
user_gpio: 0-31.
edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE.
f: the callback function.
userdata: a pointer to arbitrary user data.
The function returns a callback id if OK, otherwise pigif_bad_malloc, pigif_duplicate_callback, or pigif_bad_callback.
The callback is called with the GPIO, edge, tick, and user, whenever the GPIO has the identified edge.
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.
The function returns 0 if OK, otherwise pigif_callback_not_found.
user_gpio: 0-31.
edge: RISING_EDGE, FALLING_EDGE, or EITHER_EDGE.
timeout: >=0.
The function returns 1 if the edge occurred, otherwise 0.
The function returns when the edge occurs or after the timeout.
The number of microseconds level changes are reported for once a noise filter has been triggered (by steady microseconds of a stable level).
A convenient way to set bit n is to or in (1<<n).
e.g. to select bits 5, 9, 23 you could use (1<<5) | (1<<9) | (1<<23).
typedef void (*CBFunc_t)
(unsigned user_gpio, unsigned level, uint32_t tick);
typedef void (*CBFuncEx_t)
(unsigned user_gpio, unsigned level, uint32_t tick, void * user);
#define PI_MIN_WAVE_DATABITS 1
#define PI_MAX_WAVE_DATABITS 32
The number may vary between 0 and range (default 255) where 0 is off and range is fully on.
RISING_EDGE 0
FALLING_EDGE 1
EITHER_EDGE. 2
There are 54 General Purpose Input Outputs (GPIO) named gpio0 through gpio53.
They are split into two banks. Bank 1 consists of gpio0 through gpio31. Bank 2 consists of gpio32 through gpio53.
All the GPIO which are safe for the user to read and write are in bank 1. Not all GPIO in bank 1 are safe though. Type 1 boards have 17 safe GPIO. Type 2 boards have 21. Type 3 boards have 26.
See get_hardware_revision.
The user GPIO are marked with an X in the following table.
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
There is one exception. If a watchdog expires on a GPIO the level will be reported as PI_TIMEOUT. See set_watchdog.
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_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
PI_MIN_DUTYCYCLE_RANGE 25
PI_MAX_DUTYCYCLE_RANGE 40000
The number of microseconds level changes must be stable for before reporting the level changed (set_glitch_filter) or triggering the active part of a noise filter (set_noise_filter).
#define PI_MIN_WAVE_HALFSTOPBITS 2
#define PI_MAX_WAVE_HALFSTOPBITS 8
PI_MIN_WDOG_TIMEOUT 0
PI_MAX_WDOG_TIMEOUT 60000
See gpio.
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,
} pigifError_t;
joan@abyz.me.uk
2012-2018 | Linux |