This commit is contained in:
joan 2015-05-17 12:36:40 +01:00
parent ff0e468b0b
commit b68a696597
13 changed files with 2852 additions and 1172 deletions

195
command.c
View File

@ -26,7 +26,7 @@ For more information, please refer to <http://unlicense.org/>
*/ */
/* /*
This version is for pigpio version 32+ This version is for pigpio version 33+
*/ */
#include <stdio.h> #include <stdio.h>
@ -47,7 +47,7 @@ cmdInfo_t cmdInfo[]=
{PI_CMD_BI2CC, "BI2CC", 112, 0}, // bbI2CClose {PI_CMD_BI2CC, "BI2CC", 112, 0}, // bbI2CClose
{PI_CMD_BI2CO, "BI2CO", 131, 0}, // bbI2COpen {PI_CMD_BI2CO, "BI2CO", 131, 0}, // bbI2COpen
{PI_CMD_BI2CX, "BI2CX", 193, 6}, // bbI2CXfer {PI_CMD_BI2CZ, "BI2CZ", 193, 6}, // bbI2CZip
{PI_CMD_BR1, "BR1", 101, 3}, // gpioRead_Bits_0_31 {PI_CMD_BR1, "BR1", 101, 3}, // gpioRead_Bits_0_31
{PI_CMD_BR2, "BR2", 101, 3}, // gpioRead_Bits_32_53 {PI_CMD_BR2, "BR2", 101, 3}, // gpioRead_Bits_32_53
@ -90,7 +90,7 @@ cmdInfo_t cmdInfo[]=
{PI_CMD_I2CWS, "I2CWS", 121, 0}, // i2cWriteByte {PI_CMD_I2CWS, "I2CWS", 121, 0}, // i2cWriteByte
{PI_CMD_I2CWW, "I2CWW", 131, 0}, // i2cWriteWordData {PI_CMD_I2CWW, "I2CWW", 131, 0}, // i2cWriteWordData
{PI_CMD_I2CX, "I2CX", 131, 0}, // i2cXfer {PI_CMD_I2CZ, "I2CZ", 193, 6}, // i2cZip
{PI_CMD_MICS, "MICS", 112, 0}, // gpioDelay {PI_CMD_MICS, "MICS", 112, 0}, // gpioDelay
{PI_CMD_MILS, "MILS", 112, 0}, // gpioDelay {PI_CMD_MILS, "MILS", 112, 0}, // gpioDelay
@ -225,115 +225,98 @@ cmdInfo_t cmdInfo[]=
}; };
char * cmdUsage = "\ char * cmdUsage = "\n\
BC1 bits Clear specified gpios in bank 1\n\ BC1 bits Clear gpios in bank 1\n\
BC2 bits Clear specified gpios in bank 2\n\ BC2 bits Clear gpios in bank 2\n\
BI2CC sda Close bit bang I2C\n\
BI2CO sda scl baud | Open bit bang I2C\n\
BI2CZ sda ... I2C bit bang multiple transactions\n\
BR1 Read bank 1 gpios\n\ BR1 Read bank 1 gpios\n\
BR2 Read bank 2 gpios\n\ BR2 Read bank 2 gpios\n\
BS1 bits Set specified gpios in bank 2\n\ BS1 bits Set gpios in bank 2\n\
BS2 bits Set specified gpios in bank 2\n\ BS2 bits Set gpios in bank 2\n\
\n\ \n\
CF1 uvs Custom function 1\n\ CF1 ... Custom function 1\n\
CF2 uvs Custom function 2\n\ CF2 ... Custom function 2\n\
\n\ \n\
GDC u Get PWM dutycycle for gpio\n\ GDC g Get PWM dutycycle for gpio\n\
GPW u Get servo pulsewidth for gpio\n\ GPW g Get servo pulsewidth for gpio\n\
\n\ \n\
H/HELP Display command help\n\ H/HELP Display command help\n\
\n\ HC g f Set hardware clock frequency\n\
HC g cf Set hardware clock frequency\n\ HP g f dc Set hardware PWM frequency and dutycycle\n\
HP g pf pdc Set hardware PWM frequency and dutycycle\n\
\n\
HWVER Get hardware version\n\ HWVER Get hardware version\n\
\n\ \n\
I2CC h Close I2C handle\n\ I2CC h Close I2C handle\n\
I2CO ib id if Open I2C bus and device with flags\n\ I2CO bus device flags | Open I2C bus and device with flags\n\
\n\ I2CPC h r word SMBus Process Call: exchange register with word\n\
I2CPC h r wv SMBus Process Call: exchange register with word\n\ I2CPK h r ... SMBus Block Process Call: exchange data bytes with register\n\
I2CPK h r bvs SMBus Block Process Call: exchange data bytes with register\n\
\n\
I2CRB h r SMBus Read Byte Data: read byte from register\n\ I2CRB h r SMBus Read Byte Data: read byte from register\n\
I2CRD h num i2c Read bytes\n\ I2CRD h n I2C Read bytes\n\
I2CRI h r num SMBus Read I2C Block Data: read bytes from register\n\ I2CRI h r n SMBus Read I2C Block Data: read bytes from register\n\
I2CRK h r SMBus Read Block Data: read data from register\n\ I2CRK h r SMBus Read Block Data: read data from register\n\
I2CRS h SMBus Read Byte: read byte\n\ I2CRS h SMBus Read Byte: read byte\n\
I2CRW h r SMBus Read Word Data: read word from register\n\ I2CRW h r SMBus Read Word Data: read word from register\n\
I2CWB h r byte SMBus Write Byte Data: write byte to register\n\
I2CWD h ... I2C Write data\n\
I2CWI h r ... SMBus Write I2C Block Data\n\
I2CWK h r ... SMBus Write Block Data: write data to register\n\
I2CWQ h b SMBus Write Quick: write bit\n\
I2CWS h b SMBus Write Byte: write byte\n\
I2CWW h r word SMBus Write Word Data: write word to register\n\
I2CZ h ... I2C multiple transactions\n\
\n\ \n\
I2CWB h r bv SMBus Write Byte Data: write byte to register\n\ M/MODES g mode Set gpio mode\n\
I2CWD h bvs i2c Write data\n\
I2CWI h r bvs SMBus Write I2C Block Data\n\
I2CWK h r bvs SMBus Write Block Data: write data to register\n\
I2CWQ h bit SMBus Write Quick: write bit\n\
I2CWS h bv SMBus Write Byte: write byte\n\
I2CWW h r wv SMBus Write Word Data: write word to register\n\
\n\
M/MODES g m Set gpio mode\n\
MG/MODEG g Get gpio mode\n\ MG/MODEG g Get gpio mode\n\
\n\ MICS n Delay for microseconds\n\
MICS v Delay for microseconds\n\ MILS n Delay for milliseconds\n\
MILS v Delay for milliseconds\n\
\n\ \n\
NB h bits Start notification\n\ NB h bits Start notification\n\
NC h Close notification\n\ NC h Close notification\n\
NO Request a notification\n\ NO Request a notification\n\
NP h Pause notification\n\ NP h Pause notification\n\
\n\ \n\
P/PWM u v Set gpio PWM value\n\ P/PWM g v Set gpio PWM value\n\
\n\ PARSE text Validate script\n\
PARSE t Validate script\n\ PFG g Get gpio PWM frequency\n\
\n\ PFS g v Set gpio PWM frequency\n\
PFG u Get gpio PWM frequency\n\
PFS u v Set gpio PWM frequency\n\
\n\
PIGPV Get pigpio library version\n\ PIGPV Get pigpio library version\n\
\n\ PRG g Get gpio PWM range\n\
PRG u Get gpio PWM range\n\ PROC text Store script\n\
\n\
PROC t Store script\n\
PROCD sid Delete script\n\ PROCD sid Delete script\n\
PROCP sid Get script status and parameters\n\ PROCP sid Get script status and parameters\n\
PROCR sid pars Run script\n\ PROCR sid ... Run script\n\
PROCS sid Stop script\n\ PROCS sid Stop script\n\
\n\ PRRG g Get gpio PWM real range\n\
PRRG u Get gpio PWM real range\n\ PRS g v Set gpio PWM range\n\
PRS u v Set gpio PWM range\n\ PUD g pud Set gpio pull up/down\n\
\n\
PUD g p Set gpio pull up/down\n\
\n\ \n\
R/READ g Read gpio level\n\ R/READ g Read gpio level\n\
\n\ \n\
S/SERVO u v Set gpio servo pulsewidth\n\ S/SERVO g v Set gpio servo pulsewidth\n\
\n\
SERC h Close serial handle\n\ SERC h Close serial handle\n\
SERDA h Check for serial data ready to read\n\ SERDA h Check for serial data ready to read\n\
SERO srd srb srf Open serial device at baud with flags\n\ SERO text baud flags | Open serial device at baud with flags\n\
\n\ SERR h n Read bytes from serial handle\n\
SERR h num Read bytes from serial handle\n\
SERRB h Read byte from serial handle\n\ SERRB h Read byte from serial handle\n\
SERW h bvs Write bytes to serial handle\n\ SERW h ... Write bytes to serial handle\n\
SERWB h bv Write byte to serial handle\n\ SERWB h byte Write byte to serial handle\n\
\n\ SLR g v Read bit bang serial data from gpio\n\
SLR u num Read bit bang serial data from gpio\n\ SLRC g Close gpio for bit bang serial data\n\
SLRC u Close gpio for bit bang serial data\n\ SLRO g baud bitlen | Open gpio for bit bang serial data\n\
SLRO u b db Open gpio for bit bang serial data\n\
\n\
SPIC h SPI close handle\n\ SPIC h SPI close handle\n\
SPIO sc sb sf SPI open channel at baud with flags\n\ SPIO channel baud flags | SPI open channel at baud with flags\n\
SPIR h num SPI read bytes from handle\n\ SPIR h v SPI read bytes from handle\n\
SPIW h bvs SPI write bytes to handle\n\ SPIW h ... SPI write bytes to handle\n\
SPIX h bvs SPI transfer bytes to handle\n\ SPIX h ... SPI transfer bytes to handle\n\
\n\ \n\
T/TICK Get current tick\n\ T/TICK Get current tick\n\
TRIG g micros l Trigger level for micros on gpio\n\
\n\ \n\
TRIG u pl L Trigger level for micros on gpio\n\ W/WRITE g l Write level to gpio\n\
\n\ WDOG g millis Set millisecond watchdog on gpio\n\
W/WRITE g L Write level to gpio\n\ WVAG triplets Wave add generic pulses\n\
\n\ WVAS g baud bitlen stopbits offset ... | Wave add serial data\n\
WDOG u v Set millisecond watchdog on gpio\n\
\n\
WVAG trips Wave add generic pulses\n\
WVAS u b db hb Wave add serial data for gpio u at b baud, db databits,\n\
o bvs hb (half)stopbits, offset o micros from wave start\n\
WVBSY Check if wave busy\n\ WVBSY Check if wave busy\n\
WVCLR Wave clear\n\ WVCLR Wave clear\n\
WVCRE Create wave from added pulses\n\ WVCRE Create wave from added pulses\n\
@ -342,52 +325,17 @@ WVGO Wave transmit (DEPRECATED)\n\
WVGOR Wave transmit repeatedly (DEPRECATED)\n\ WVGOR Wave transmit repeatedly (DEPRECATED)\n\
WVHLT Wave stop\n\ WVHLT Wave stop\n\
WVNEW Start a new empty wave\n\ WVNEW Start a new empty wave\n\
WVSC ws Wave get DMA control block stats\n\ WVSC 0,1,2 Wave get DMA control block stats\n\
WVSM ws Wave get micros stats\n\ WVSM 0,1,2 Wave get micros stats\n\
WVSP ws Wave get pulses stats\n\ WVSP 0,1,2 Wave get pulses stats\n\
WVTX wid Transmit wave as one-shot\n\ WVTX wid Transmit wave as one-shot\n\
WVTXR wid Transmit wave repeatedly\n\ WVTXR wid Transmit wave repeatedly\n\
\n\ \n\
bits = a mask where (1<<g) is set for each gpio g of interest\n\
bv = byte value (0-255)\n\
bvs = one or more byte values (0-255)\n\
cf = hardware clock frequency (4689-250M)\n\
db = data bits (1-32)\n\
g = any gpio (0-53)\n\
h = handle (>=0)\n\
hb = (half) stop bits (2-8)\n\
ib = I2C bus (0-1)\n\
id = I2C device (0-127)\n\
if = I2C flags (0)\n\
L = level (0-1)\n\
m = mode (RW540123)\n\
num = number of bytes to read\n\
o = offset (>=0)\n\
p = pud (ODU)\n\
pars = 0 to 10 parameters for script\n\
pdc = hardware PWM dutycycle (0-1M)\n\
pf = hardware PWM frequency (1-125M)\n\
pl = pulse length (1-100)\n\
r = register\n\
sid = script id (>=0)\n\
sb = SPI baud\n\
sc = SPI channel (0-1)\n\
sf = SPI flags (0-3)\n\
srd = serial device (/dev/tty*)\n\
srb = serial baud rate\n\
srf = serial flags (0)\n\
t = text\n\
trips = 1 or more triplets of gpios on, gpios off, delay\n\
u = user gpio (0-31)\n\
uvs = zero or more values >= 0, any after the first two must <= 255\n\
v = value\n\
w = wave id (>=0)\n\
ws = 0=now, 1=high, 2=max\n\
wv = word value (0-65535)\n\
\n\ \n\
Numbers may be entered as hex (prefix 0x), octal (prefix 0),\n\ Numbers may be entered as hex (prefix 0x), octal (prefix 0),\n\
otherwise they are assumed to be decimal\n\ otherwise they are assumed to be decimal.\n\
"; \n\
man pigs for full details.\n\n";
typedef struct typedef struct
{ {
@ -500,11 +448,14 @@ static errInfo_t errInfo[]=
{PI_BAD_DATABITS , "serial data bits not 1-32"}, {PI_BAD_DATABITS , "serial data bits not 1-32"},
{PI_MSG_TOOBIG , "socket/pipe message too big"}, {PI_MSG_TOOBIG , "socket/pipe message too big"},
{PI_BAD_MALLOC_MODE , "bad memory allocation mode"}, {PI_BAD_MALLOC_MODE , "bad memory allocation mode"},
{PI_TOO_MANY_PARTS , "too many I2C transaction parts"}, {PI_TOO_MANY_SEGS , "too many I2C transaction segments"},
{PI_BAD_I2C_PART , "a combined I2C transaction failed"}, {PI_BAD_I2C_SEG , "an I2C transaction segment failed"},
{PI_BAD_SMBUS_CMD , "SMBus command not supported by driver"}, {PI_BAD_SMBUS_CMD , "SMBus command not supported by driver"},
{PI_NOT_I2C_GPIO , "no bit bang I2C in progress on gpio"}, {PI_NOT_I2C_GPIO , "no bit bang I2C in progress on gpio"},
{PI_BAD_I2C_WLEN , "bad I2C write length"},
{PI_BAD_I2C_RLEN , "bad I2C read length"},
{PI_BAD_I2C_CMD , "bad I2C command"},
{PI_BAD_I2C_BAUD , "bad I2C baud rate, not 50-500k"},
}; };
static char * fmtMdeStr="RW540123"; static char * fmtMdeStr="RW540123";
@ -893,7 +844,7 @@ int cmdParse(
break; break;
case 193: /* BI2CX I2CWD SERW SPIW SPIX case 193: /* BI2CZ I2CWD I2CZ SERW SPIW SPIX
Two or more parameters, first >=0, rest 0-255. Two or more parameters, first >=0, rest 0-255.
*/ */

833
pigpio.3

File diff suppressed because it is too large Load Diff

542
pigpio.c
View File

@ -25,7 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/> For more information, please refer to <http://unlicense.org/>
*/ */
/* pigpio version 32 */ /* pigpio version 33 */
/* include ------------------------------------------------------- */ /* include ------------------------------------------------------- */
@ -908,6 +908,7 @@ typedef struct
{ {
uint16_t state; uint16_t state;
int16_t fd; int16_t fd;
uint32_t addr;
uint32_t flags; uint32_t flags;
uint32_t funcs; uint32_t funcs;
} i2cInfo_t; } i2cInfo_t;
@ -1000,7 +1001,7 @@ typedef struct
{ {
int mode; int mode;
int gpio; int gpio;
uint32_t baud; /* 50-250000 */ uint32_t baud;
union union
{ {
wfRxSerial_t s; wfRxSerial_t s;
@ -1483,6 +1484,31 @@ static uint32_t myGetLevel(int pos)
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
static int myI2CGetPar(char *inBuf, int *inPos, int inLen, int *esc)
{
int bytes;
if (*esc) bytes = 2; else bytes = 1;
*esc = 0;
if (*inPos <= (inLen - bytes))
{
if (bytes == 1)
{
return inBuf[(*inPos)++];
}
else
{
(*inPos) += 2;
return inBuf[*inPos-2] + (inBuf[*inPos-1]<<8);
}
}
return -1;
}
/* ----------------------------------------------------------------------- */
static uint32_t myGetTick(int pos) static uint32_t myGetTick(int pos)
{ {
uint32_t tick; uint32_t tick;
@ -1588,10 +1614,10 @@ static int myDoCommand(uint32_t *p, unsigned bufSize, char *buf)
res = bbI2COpen(p[1], p[2], p[4]); res = bbI2COpen(p[1], p[2], p[4]);
break; break;
case PI_CMD_BI2CX: case PI_CMD_BI2CZ:
/* use half buffer for write, half buffer for read */ /* use half buffer for write, half buffer for read */
if (p[3] > (bufSize/2)) p[3] = bufSize/2; if (p[3] > (bufSize/2)) p[3] = bufSize/2;
res = bbI2CXfer(p[1], buf, p[3], buf+(bufSize/2), bufSize/2); res = bbI2CZip(p[1], buf, p[3], buf+(bufSize/2), bufSize/2);
if (res > 0) if (res > 0)
{ {
memcpy(buf, buf+(bufSize/2), res); memcpy(buf, buf+(bufSize/2), res);
@ -1740,6 +1766,16 @@ static int myDoCommand(uint32_t *p, unsigned bufSize, char *buf)
res = i2cWriteWordData(p[1], p[2], p[4]); res = i2cWriteWordData(p[1], p[2], p[4]);
break; break;
case PI_CMD_I2CZ:
/* use half buffer for write, half buffer for read */
if (p[3] > (bufSize/2)) p[3] = bufSize/2;
res = i2cZip(p[1], buf, p[3], buf+(bufSize/2), bufSize/2);
if (res > 0)
{
memcpy(buf, buf+(bufSize/2), res);
}
break;
case PI_CMD_MICS: case PI_CMD_MICS:
@ -3470,8 +3506,8 @@ int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags)
if (i2cBus >= PI_NUM_I2C_BUS) if (i2cBus >= PI_NUM_I2C_BUS)
SOFT_ERROR(PI_BAD_I2C_BUS, "bad I2C bus (%d)", i2cBus); SOFT_ERROR(PI_BAD_I2C_BUS, "bad I2C bus (%d)", i2cBus);
// if ((i2cAddr < 0x08) || (i2cAddr > 0x77)) if (i2cAddr > PI_MAX_I2C_ADDR)
// SOFT_ERROR(PI_BAD_I2C_ADDR, "bad I2C address (0x%X)", i2cAddr); SOFT_ERROR(PI_BAD_I2C_ADDR, "bad I2C address (%d)", i2cAddr);
if (i2cFlags) if (i2cFlags)
SOFT_ERROR(PI_BAD_FLAGS, "bad flags (0x%X)", i2cFlags); SOFT_ERROR(PI_BAD_FLAGS, "bad flags (0x%X)", i2cFlags);
@ -3522,6 +3558,7 @@ int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags)
} }
i2cInfo[slot].fd = fd; i2cInfo[slot].fd = fd;
i2cInfo[slot].addr = i2cAddr;
i2cInfo[slot].flags = i2cFlags; i2cInfo[slot].flags = i2cFlags;
i2cInfo[slot].funcs = funcs; i2cInfo[slot].funcs = funcs;
@ -3548,7 +3585,23 @@ int i2cClose(unsigned handle)
return 0; return 0;
} }
int i2cTransaction(unsigned handle, pi_i2c_msg_t *parts, unsigned numParts) void i2cSwitchCombined(int setting)
{
int fd;
DBG(DBG_USER, "setting=%d", setting);
fd = open(PI_I2C_COMBINED, O_WRONLY);
if (fd >= 0)
{
if (setting) write(fd, "1\n", 2); else write(fd, "0\n", 2);
close(fd);
}
}
int i2cSegments(unsigned handle, pi_i2c_msg_t *segs, unsigned numSegs)
{ {
int retval; int retval;
my_i2c_rdwr_ioctl_data_t rdwr; my_i2c_rdwr_ioctl_data_t rdwr;
@ -3563,31 +3616,28 @@ int i2cTransaction(unsigned handle, pi_i2c_msg_t *parts, unsigned numParts)
if (i2cInfo[handle].state != PI_I2C_OPENED) if (i2cInfo[handle].state != PI_I2C_OPENED)
SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle); SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
if (parts == NULL) if (segs == NULL)
SOFT_ERROR(PI_BAD_POINTER, "null parts"); SOFT_ERROR(PI_BAD_POINTER, "null segments");
if (numParts > PI_I2C_RDRW_IOCTL_MAX_MSGS) if (numSegs > PI_I2C_RDRW_IOCTL_MAX_MSGS)
SOFT_ERROR(PI_TOO_MANY_PARTS, "too many parts (%d)", numParts); SOFT_ERROR(PI_TOO_MANY_SEGS, "too many segments (%d)", numSegs);
rdwr.msgs = parts; rdwr.msgs = segs;
rdwr.nmsgs = numParts; rdwr.nmsgs = numSegs;
retval = ioctl(i2cInfo[handle].fd, PI_I2C_RDWR, &rdwr); retval = ioctl(i2cInfo[handle].fd, PI_I2C_RDWR, &rdwr);
DBG(0, "i2cTransaction retval=%d", retval);
if (retval >= 0) return retval; if (retval >= 0) return retval;
else return PI_BAD_I2C_PART; else return PI_BAD_I2C_SEG;
} }
#ifdef ZZZZ int i2cZip(
int i2cXfer(
unsigned handle, unsigned handle,
char *inBuf, unsigned inLen, char *outBuf, unsigned outLen) char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)
{ {
int i, parts, inPos, outPos, status, bytes, len_bytes,flags, addr; int numSegs, inPos, outPos, status, bytes, flags, addr;
pi_i2c_msg_t part[PI_I2C_RDRW_IOCTL_MAX_MSGS]; int esc, setesc;
pi_i2c_msg_t segs[PI_I2C_RDRW_IOCTL_MAX_MSGS];
DBG(DBG_USER, "handle=%d inBuf=%s outBuf=%08X len=%d", DBG(DBG_USER, "handle=%d inBuf=%s outBuf=%08X len=%d",
handle, myBuf2Str(inLen, (char *)inBuf), (int)outBuf, outLen); handle, myBuf2Str(inLen, (char *)inBuf), (int)outBuf, outLen);
@ -3606,19 +3656,21 @@ int i2cXfer(
if (!outBuf && outLen) if (!outBuf && outLen)
SOFT_ERROR(PI_BAD_POINTER, "output buffer can't be NULL"); SOFT_ERROR(PI_BAD_POINTER, "output buffer can't be NULL");
parts = 0; numSegs = 0;
inPos = 0; inPos = 0;
outPos = 0; outPos = 0;
status = 0; status = 0;
addr = i2cInfo[handle].addr;
flags = 0; flags = 0;
addr = 0; esc = 0;
setesc = 0;
while (!status && (inPos < inLen) && (parts < PI_I2C_RDRW_IOCTL_MAX_MSGS)) while (!status && (inPos < inLen))
{ {
DBG(DBG_INTERNAL, "status=%d inpos=%d inlen=%d cmd=%d", DBG(DBG_INTERNAL, "status=%d inpos=%d inlen=%d cmd=%d addr=%d flags=%x",
status, inPos, inLen, inBuf[inPos]); status, inPos, inLen, inBuf[inPos], addr, flags);
len_bytes = 1;
switch (inBuf[inPos++]) switch (inBuf[inPos++])
{ {
@ -3626,83 +3678,114 @@ int i2cXfer(
status = 1; status = 1;
break; break;
case PI_I2C_READ16: case PI_I2C_COMBINED_ON:
len_bytes = 2; /* Run prior transactions before setting combined flag */
/* fall through */ if (numSegs)
case PI_I2C_READ:
if (inPos <= (inLen - len_bytes))
{ {
if (len_bytes != 1) status = i2cSegments(handle, segs, numSegs);
{ if (status >= 0) status = 0; /* continue */
bytes=inBuf[inPos] + (inBuf[inPos+1]<<8); numSegs = 0;
inPos += 2;
}
else
{
bytes=inBuf[inPos++];
}
if (bytes && ((bytes + outPos) < outLen))
{
part[parts].addr = addr;
part[parts].flags = flags;
part[parts].len =
part[parts].buf =
for (i=0; i<(bytes-1); i++)
{
outBuf[outPos++] = I2CGetByte(w, 0);
}
outBuf[outPos++] = I2CGetByte(w, 1);
}
else status = PI_BAD_BB_WLEN;
} }
else status = PI_BAD_BB_WLEN; i2cSwitchCombined(1);
break; break;
case PI_I2C_WRITE16: case PI_I2C_COMBINED_OFF:
len_bytes = 2; /* Run prior transactions before clearing combined flag */
/* fall through */ if (numSegs)
{
status = i2cSegments(handle, segs, numSegs);
if (status >= 0) status = 0; /* continue */
numSegs = 0;
}
i2cSwitchCombined(0);
break;
case PI_I2C_ADDR:
addr = myI2CGetPar(inBuf, &inPos, inLen, &esc);
if (addr < 0) status = PI_BAD_I2C_CMD;
break;
case PI_I2C_FLAGS:
/* cheat to force two byte flags */
esc = 1;
flags = myI2CGetPar(inBuf, &inPos, inLen, &esc);
if (flags < 0) status = PI_BAD_I2C_CMD;
break;
case PI_I2C_ESC:
setesc = 1;
break;
case PI_I2C_READ:
bytes = myI2CGetPar(inBuf, &inPos, inLen, &esc);
if (bytes >= 0)
{
if ((bytes + outPos) < outLen)
{
segs[numSegs].addr = addr;
segs[numSegs].flags = (flags|1);
segs[numSegs].len = bytes;
segs[numSegs].buf = (uint8_t *)(outBuf + outPos);
outPos += bytes;
numSegs++;
if (numSegs >= PI_I2C_RDRW_IOCTL_MAX_MSGS)
{
status = i2cSegments(handle, segs, numSegs);
if (status >= 0) status = 0; /* continue */
numSegs = 0;
}
}
else status = PI_BAD_I2C_RLEN;
}
else status = PI_BAD_I2C_RLEN;
break;
case PI_I2C_WRITE: case PI_I2C_WRITE:
if (inPos <= (inLen - len_bytes))
{
if (len_bytes != 1)
{
bytes=inBuf[inPos] + (inBuf[inPos+1]<<8);
inPos += 2;
}
else
{
bytes=inBuf[inPos++];
}
if (bytes && ((bytes + inPos) < inLen)) bytes = myI2CGetPar(inBuf, &inPos, inLen, &esc);
if (bytes >= 0)
{
if ((bytes + inPos) < inLen)
{ {
for (i=0; i<(bytes-1); i++) segs[numSegs].addr = addr;
segs[numSegs].flags = flags;
segs[numSegs].len = bytes;
segs[numSegs].buf = (uint8_t *)(inBuf + inPos);
inPos += bytes;
numSegs++;
if (numSegs >= PI_I2C_RDRW_IOCTL_MAX_MSGS)
{ {
ack = I2CPutByte(w, inBuf[inPos++]); status = i2cSegments(handle, segs, numSegs);
if (ack) status = PI_I2C_WRITE_FAILED; if (status >= 0) status = 0; /* continue */
numSegs = 0;
} }
ack = I2CPutByte(w, inBuf[inPos++]);
//if (ack) status = PI_I2C_WRITE_FAILED;
} }
else status = PI_BAD_BB_RLEN; else status = PI_BAD_I2C_WLEN;
} }
else status = PI_BAD_BB_RLEN; else status = PI_BAD_I2C_WLEN;
break; break;
default: default:
status = PI_BAD_BB_CMD; status = PI_BAD_I2C_CMD;
} }
if (setesc) esc = 1; else esc = 0;
setesc = 0;
}
if (status >= 0)
{
if (numSegs) status = i2cSegments(handle, segs, numSegs);
} }
if (status >= 0) status = outPos; if (status >= 0) status = outPos;
return status; return status;
} }
#endif
/* ======================================================================= */ /* ======================================================================= */
@ -4103,12 +4186,12 @@ static void spiTerm(uint32_t flags)
} }
} }
int spiOpen(unsigned spiChan, unsigned spiBaud, unsigned spiFlags) int spiOpen(unsigned spiChan, unsigned baud, unsigned spiFlags)
{ {
int i, slot; int i, slot;
DBG(DBG_USER, "spiChan=%d spiBaud=%d spiFlags=0x%X", DBG(DBG_USER, "spiChan=%d baud=%d spiFlags=0x%X",
spiChan, spiBaud, spiFlags); spiChan, baud, spiFlags);
CHECK_INITED; CHECK_INITED;
@ -4125,8 +4208,8 @@ int spiOpen(unsigned spiChan, unsigned spiBaud, unsigned spiFlags)
if (spiChan >= i) if (spiChan >= i)
SOFT_ERROR(PI_BAD_SPI_CHANNEL, "bad spiChan (%d)", spiChan); SOFT_ERROR(PI_BAD_SPI_CHANNEL, "bad spiChan (%d)", spiChan);
if ((spiBaud < MIN_SPI_SPEED) || (spiBaud > MAX_SPI_SPEED)) if ((baud < PI_SPI_MIN_BAUD) || (baud > PI_SPI_MAX_BAUD))
SOFT_ERROR(PI_BAD_SPI_SPEED, "bad spiBaud (%d)", spiBaud); SOFT_ERROR(PI_BAD_SPI_SPEED, "bad baud (%d)", baud);
if (spiFlags > (1<<22)) if (spiFlags > (1<<22))
SOFT_ERROR(PI_BAD_FLAGS, "bad spiFlags (0x%X)", spiFlags); SOFT_ERROR(PI_BAD_FLAGS, "bad spiFlags (0x%X)", spiFlags);
@ -4134,7 +4217,7 @@ int spiOpen(unsigned spiChan, unsigned spiBaud, unsigned spiFlags)
if (!spiAnyOpen(spiFlags)) /* initialise on first open */ if (!spiAnyOpen(spiFlags)) /* initialise on first open */
{ {
spiInit(spiFlags); spiInit(spiFlags);
spiGo(spiBaud, spiFlags, NULL, NULL, 0); spiGo(baud, spiFlags, NULL, NULL, 0);
} }
slot = -1; slot = -1;
@ -4152,7 +4235,7 @@ int spiOpen(unsigned spiChan, unsigned spiBaud, unsigned spiFlags)
if (slot < 0) if (slot < 0)
SOFT_ERROR(PI_NO_HANDLE, "no SPI handles"); SOFT_ERROR(PI_NO_HANDLE, "no SPI handles");
spiInfo[slot].speed = spiBaud; spiInfo[slot].speed = baud;
spiInfo[slot].flags = spiFlags | PI_SPI_FLAGS_CHANNEL(spiChan); spiInfo[slot].flags = spiFlags | PI_SPI_FLAGS_CHANNEL(spiChan);
return slot; return slot;
@ -4890,6 +4973,7 @@ static void sigHandler(int signum)
default: default:
DBG(DBG_ALWAYS, "Unhandled signal %d, terminating\n", signum); DBG(DBG_ALWAYS, "Unhandled signal %d, terminating\n", signum);
gpioTerminate();
exit(-1); exit(-1);
} }
} }
@ -4899,7 +4983,7 @@ static void sigHandler(int signum)
/* exit */ /* exit */
DBG(DBG_ALWAYS, "Unhandled signal %d, terminating\n", signum); DBG(DBG_ALWAYS, "Unhandled signal %d, terminating\n", signum);
gpioTerminate();
exit(-1); exit(-1);
} }
} }
@ -5050,7 +5134,7 @@ static void * pthAlertThread(void *x)
{ {
/* shouldn't happen */ /* shouldn't happen */
gpioCfg.showStats = 1; //gpioCfg.showStats = 1;
gpioStats.diffTick[0]++; gpioStats.diffTick[0]++;
} }
@ -5059,7 +5143,7 @@ static void * pthAlertThread(void *x)
{ {
/* shouldn't happen */ /* shouldn't happen */
gpioCfg.showStats = 1; //gpioCfg.showStats = 1;
gpioStats.diffTick[TICKSLOTS-1]++; gpioStats.diffTick[TICKSLOTS-1]++;
} }
@ -5303,10 +5387,10 @@ static void * pthAlertThread(void *x)
{ {
/* serious error, no point continuing */ /* serious error, no point continuing */
DBG(0, "fd=%d err=%d errno=%d", DBG(DBG_ALWAYS, "fd=%d err=%d errno=%d",
gpioNotify[n].fd, err, errno); gpioNotify[n].fd, err, errno);
DBG(0, "%s", strerror(errno)); DBG(DBG_ALWAYS, "%s", strerror(errno));
gpioNotify[n].bits = 0; gpioNotify[n].bits = 0;
gpioNotify[n].state = PI_NOTIFY_CLOSING; gpioNotify[n].state = PI_NOTIFY_CLOSING;
@ -5331,10 +5415,10 @@ static void * pthAlertThread(void *x)
{ {
if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
{ {
DBG(0, "fd=%d err=%d errno=%d", DBG(DBG_ALWAYS, "fd=%d err=%d errno=%d",
gpioNotify[n].fd, err, errno); gpioNotify[n].fd, err, errno);
DBG(0, "%s", strerror(errno)); DBG(DBG_ALWAYS, "%s", strerror(errno));
/* serious error, no point continuing */ /* serious error, no point continuing */
gpioNotify[n].bits = 0; gpioNotify[n].bits = 0;
@ -5897,7 +5981,7 @@ static void *pthSocketThreadHandler(void *fdC)
if (recv(sock, buf, p[3], MSG_WAITALL) != p[3]) if (recv(sock, buf, p[3], MSG_WAITALL) != p[3])
{ {
/* Serious error. No point continuing. */ /* Serious error. No point continuing. */
DBG(0, "recv failed for %d bytes", p[3]); DBG(DBG_ALWAYS, "recv failed for %d bytes", p[3]);
close(sock); close(sock);
@ -5907,7 +5991,7 @@ static void *pthSocketThreadHandler(void *fdC)
else else
{ {
/* Serious error. No point continuing. */ /* Serious error. No point continuing. */
DBG(0, "ext too large %d(%d)", p[3], sizeof(buf)); DBG(DBG_ALWAYS, "ext too large %d(%d)", p[3], sizeof(buf));
close(sock); close(sock);
@ -5949,12 +6033,13 @@ static void *pthSocketThreadHandler(void *fdC)
{ {
/* extensions */ /* extensions */
case PI_CMD_BI2CX: case PI_CMD_BI2CZ:
case PI_CMD_CF2: case PI_CMD_CF2:
case PI_CMD_I2CPK: case PI_CMD_I2CPK:
case PI_CMD_I2CRD: case PI_CMD_I2CRD:
case PI_CMD_I2CRI: case PI_CMD_I2CRI:
case PI_CMD_I2CRK: case PI_CMD_I2CRK:
case PI_CMD_I2CZ:
case PI_CMD_PROCP: case PI_CMD_PROCP:
case PI_CMD_SERR: case PI_CMD_SERR:
case PI_CMD_SLR: case PI_CMD_SLR:
@ -6031,12 +6116,47 @@ static void * pthSocketThread(void *x)
/* ======================================================================= */ /* ======================================================================= */
static void initCheckLockFile(void)
{
int fd;
int count;
int pid;
int err;
int delete;
char str[20];
fd = open(PI_LOCKFILE, O_RDONLY);
if (fd != -1)
{
DBG(DBG_STARTUP, "lock file exists");
delete = 1;
count = read(fd, str, sizeof(str)-1);
if (count)
{
pid = atoi(str);
err = kill(pid, 0);
if (!err) delete = 0; /* process still exists */
DBG(DBG_STARTUP, "lock file pid=%d err=%d", pid, err);
}
close(fd);
DBG(DBG_STARTUP, "lock file delete=%d", delete);
if (delete) unlink(PI_LOCKFILE);
}
}
static int initGrabLockFile(void) static int initGrabLockFile(void)
{ {
int fd; int fd;
int lockResult; int lockResult;
char pidStr[20]; char pidStr[20];
initCheckLockFile();
/* try to grab the lock file */ /* try to grab the lock file */
fd = open(PI_LOCKFILE, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0644); fd = open(PI_LOCKFILE, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0644);
@ -6910,6 +7030,9 @@ static void initReleaseResources(void)
close(fdMbox); close(fdMbox);
fdMbox = -1; fdMbox = -1;
} }
gpioStats.DMARestarts = 0;
gpioStats.dmaInitCbsCount = 0;
} }
int initInitialise(void) int initInitialise(void)
@ -7233,6 +7356,8 @@ int gpioInitialise(void)
if (libInitialised) return PIGPIO_VERSION; if (libInitialised) return PIGPIO_VERSION;
DBG(DBG_STARTUP, "not initialised, initialising");
runState = PI_STARTING; runState = PI_STARTING;
status = initInitialise(); status = initInitialise();
@ -7262,6 +7387,8 @@ void gpioTerminate(void)
if (!libInitialised) return; if (!libInitialised) return;
DBG(DBG_STARTUP, "initialised, terminating");
runState = PI_ENDING; runState = PI_ENDING;
gpioMaskSet = 0; gpioMaskSet = 0;
@ -7863,9 +7990,9 @@ int gpioWaveAddGeneric(unsigned numPulses, gpioPulse_t *pulses)
int gpioWaveAddSerial int gpioWaveAddSerial
(unsigned gpio, (unsigned gpio,
unsigned bbBaud, unsigned baud,
unsigned bbBits, unsigned data_bits,
unsigned bbStops, unsigned stop_bits,
unsigned offset, unsigned offset,
unsigned numBytes, unsigned numBytes,
char *bstr) char *bstr)
@ -7879,7 +8006,7 @@ int gpioWaveAddSerial
DBG(DBG_USER, DBG(DBG_USER,
"gpio=%d baud=%d bits=%d stops=%d offset=%d numBytes=%d str=[%s]", "gpio=%d baud=%d bits=%d stops=%d offset=%d numBytes=%d str=[%s]",
gpio, bbBaud, bbBits, bbStops, offset, gpio, baud, data_bits, stop_bits, offset,
numBytes, myBuf2Str(numBytes, (char *)bstr)); numBytes, myBuf2Str(numBytes, (char *)bstr));
CHECK_INITED; CHECK_INITED;
@ -7887,16 +8014,16 @@ int gpioWaveAddSerial
if (gpio > PI_MAX_USER_GPIO) if (gpio > PI_MAX_USER_GPIO)
SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio); SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
if ((bbBaud < PI_BB_MIN_BAUD) || (bbBaud > PI_BB_TX_MAX_BAUD)) if ((baud < PI_WAVE_MIN_BAUD) || (baud > PI_WAVE_MAX_BAUD))
SOFT_ERROR(PI_BAD_WAVE_BAUD, "bad baud rate (%d)", bbBaud); SOFT_ERROR(PI_BAD_WAVE_BAUD, "bad baud rate (%d)", baud);
if ((bbBits < PI_MIN_WAVE_DATABITS) || (bbBits > PI_MAX_WAVE_DATABITS)) if ((data_bits < PI_MIN_WAVE_DATABITS) || (data_bits > PI_MAX_WAVE_DATABITS))
SOFT_ERROR(PI_BAD_DATABITS, "bad number of databits (%d)", bbBits); SOFT_ERROR(PI_BAD_DATABITS, "bad number of databits (%d)", data_bits);
if ((bbStops < PI_MIN_WAVE_HALFSTOPBITS) || if ((stop_bits < PI_MIN_WAVE_HALFSTOPBITS) ||
(bbStops > PI_MAX_WAVE_HALFSTOPBITS)) (stop_bits > PI_MAX_WAVE_HALFSTOPBITS))
SOFT_ERROR(PI_BAD_STOPBITS, SOFT_ERROR(PI_BAD_STOPBITS,
"bad number of (half) stop bits (%d)", bbStops); "bad number of (half) stop bits (%d)", stop_bits);
if (gpio > PI_MAX_USER_GPIO) if (gpio > PI_MAX_USER_GPIO)
SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio); SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
@ -7907,12 +8034,12 @@ int gpioWaveAddSerial
if (offset > PI_WAVE_MAX_MICROS) if (offset > PI_WAVE_MAX_MICROS)
SOFT_ERROR(PI_BAD_SER_OFFSET, "offset too large (%d)", offset); SOFT_ERROR(PI_BAD_SER_OFFSET, "offset too large (%d)", offset);
if (bbBits > 8) numBytes /= 2; if (data_bits > 8) numBytes /= 2;
if (bbBits > 16) numBytes /= 2; if (data_bits > 16) numBytes /= 2;
if (!numBytes) return 0; if (!numBytes) return 0;
waveBitDelay(bbBaud, bbBits, bbStops, bitDelay); waveBitDelay(baud, data_bits, stop_bits, bitDelay);
p = 0; p = 0;
@ -7936,11 +8063,11 @@ int gpioWaveAddSerial
lev = 0; lev = 0;
if (bbBits < 9) c = bstr[i]; if (data_bits < 9) c = bstr[i];
else if (bbBits < 17) c = wstr[i]; else if (data_bits < 17) c = wstr[i];
else c = lstr[i]; else c = lstr[i];
for (b=0; b<bbBits; b++) for (b=0; b<data_bits; b++)
{ {
if (c & (1<<b)) v=1; else v=0; if (c & (1<<b)) v=1; else v=0;
@ -7970,14 +8097,14 @@ int gpioWaveAddSerial
/* stop bit */ /* stop bit */
if (lev) wf[2][p].usDelay += bitDelay[bbBits+1]; if (lev) wf[2][p].usDelay += bitDelay[data_bits+1];
else else
{ {
p++; p++;
wf[2][p].gpioOn = (1<<gpio); wf[2][p].gpioOn = (1<<gpio);
wf[2][p].gpioOff = 0; wf[2][p].gpioOff = 0;
wf[2][p].usDelay = bitDelay[bbBits+1]; wf[2][p].usDelay = bitDelay[data_bits+1];
wf[2][p].flags = 0; wf[2][p].flags = 0;
} }
} }
@ -8519,9 +8646,9 @@ static uint8_t I2CGetByte(wfRx_t *w, int nack)
return byte; return byte;
} }
int bbI2COpen(unsigned SDA, unsigned SCL, unsigned bbBaud) int bbI2COpen(unsigned SDA, unsigned SCL, unsigned baud)
{ {
DBG(DBG_USER, "SDA=%d SCL=%d bbBaud=%d", SDA, SCL, bbBaud); DBG(DBG_USER, "SDA=%d SCL=%d baud=%d", SDA, SCL, baud);
CHECK_INITED; CHECK_INITED;
@ -8531,24 +8658,24 @@ int bbI2COpen(unsigned SDA, unsigned SCL, unsigned bbBaud)
if (SCL > PI_MAX_USER_GPIO) if (SCL > PI_MAX_USER_GPIO)
SOFT_ERROR(PI_BAD_USER_GPIO, "bad SCL (%d)", SCL); SOFT_ERROR(PI_BAD_USER_GPIO, "bad SCL (%d)", SCL);
if ((bbBaud < PI_BB_MIN_BAUD) || (bbBaud > PI_BB_RX_MAX_BAUD)) if ((baud < PI_BB_I2C_MIN_BAUD) || (baud > PI_BB_I2C_MAX_BAUD))
SOFT_ERROR(PI_BAD_WAVE_BAUD, SOFT_ERROR(PI_BAD_I2C_BAUD,
"SDA %d, bad baud rate (%d)", SDA, bbBaud); "SDA %d, bad baud rate (%d)", SDA, baud);
if (wfRx[SDA].mode != PI_WFRX_NONE) if (wfRx[SDA].mode != PI_WFRX_NONE)
SOFT_ERROR(PI_GPIO_IN_USE, "gpio %d is already being used", SDA); SOFT_ERROR(PI_GPIO_IN_USE, "gpio %d is already being used", SDA);
if (wfRx[SCL].mode != PI_WFRX_NONE) if ((wfRx[SCL].mode != PI_WFRX_NONE) || (SCL == SDA))
SOFT_ERROR(PI_GPIO_IN_USE, "gpio %d is already being used", SCL); SOFT_ERROR(PI_GPIO_IN_USE, "gpio %d is already being used", SCL);
wfRx[SDA].gpio = SDA; wfRx[SDA].gpio = SDA;
wfRx[SDA].mode = PI_WFRX_I2C; wfRx[SDA].mode = PI_WFRX_I2C;
wfRx[SDA].baud = bbBaud; wfRx[SDA].baud = baud;
wfRx[SDA].I.started = 0; wfRx[SDA].I.started = 0;
wfRx[SDA].I.SDA = SDA; wfRx[SDA].I.SDA = SDA;
wfRx[SDA].I.SCL = SCL; wfRx[SDA].I.SCL = SCL;
wfRx[SDA].I.delay = 500000 / bbBaud; wfRx[SDA].I.delay = 500000 / baud;
wfRx[SDA].I.SDAMode = gpioGetMode(SDA); wfRx[SDA].I.SDAMode = gpioGetMode(SDA);
wfRx[SDA].I.SCLMode = gpioGetMode(SCL); wfRx[SDA].I.SCLMode = gpioGetMode(SCL);
@ -8597,14 +8724,17 @@ int bbI2CClose(unsigned SDA)
return 0; return 0;
} }
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
int bbI2CXfer( int bbI2CZip(
unsigned SDA, unsigned SDA,
char *inBuf, unsigned inLen, char *outBuf, unsigned outLen) char *inBuf,
unsigned inLen,
char *outBuf,
unsigned outLen)
{ {
int i, ack, inPos, outPos, status, bytes, len_bytes; int i, ack, inPos, outPos, status, bytes;
int addr, flags, esc, setesc;
wfRx_t *w; wfRx_t *w;
DBG(DBG_USER, "gpio=%d inBuf=%s outBuf=%08X len=%d", DBG(DBG_USER, "gpio=%d inBuf=%s outBuf=%08X len=%d",
@ -8630,12 +8760,15 @@ int bbI2CXfer(
outPos = 0; outPos = 0;
status = 0; status = 0;
addr = 0;
flags = 0;
esc = 0;
setesc = 0;
while (!status && (inPos < inLen)) while (!status && (inPos < inLen))
{ {
DBG(DBG_INTERNAL, "status=%d inpos=%d inlen=%d cmd=%d", DBG(DBG_INTERNAL, "status=%d inpos=%d inlen=%d cmd=%d addr=%d flags=%x",
status, inPos, inLen, inBuf[inPos]); status, inPos, inLen, inBuf[inPos], addr, flags);
len_bytes = 1;
switch (inBuf[inPos++]) switch (inBuf[inPos++])
{ {
@ -8651,71 +8784,79 @@ int bbI2CXfer(
I2CStop(w); I2CStop(w);
break; break;
case PI_I2C_READ16: case PI_I2C_ADDR:
len_bytes = 2; addr = myI2CGetPar(inBuf, &inPos, inLen, &esc);
/* fall through */ if (addr < 0) status = PI_BAD_I2C_CMD;
case PI_I2C_READ:
if (inPos <= (inLen - len_bytes))
{
if (len_bytes != 1)
{
bytes=inBuf[inPos] + (inBuf[inPos+1]<<8);
inPos += 2;
}
else
{
bytes=inBuf[inPos++];
}
if (bytes && ((bytes + outPos) < outLen))
{
for (i=0; i<(bytes-1); i++)
{
outBuf[outPos++] = I2CGetByte(w, 0);
}
outBuf[outPos++] = I2CGetByte(w, 1);
}
else status = PI_BAD_BB_WLEN;
}
else status = PI_BAD_BB_WLEN;
break; break;
case PI_I2C_WRITE16: case PI_I2C_FLAGS:
len_bytes = 2; /* cheat to force two byte flags */
/* fall through */ esc = 1;
flags = myI2CGetPar(inBuf, &inPos, inLen, &esc);
if (flags < 0) status = PI_BAD_I2C_CMD;
break;
case PI_I2C_ESC:
setesc = 1;
break;
case PI_I2C_READ:
bytes = myI2CGetPar(inBuf, &inPos, inLen, &esc);
if (bytes >= 0) ack = I2CPutByte(w, (addr<<1)|1);
if (bytes > 0)
{
if (!ack)
{
if ((bytes + outPos) < outLen)
{
for (i=0; i<(bytes-1); i++)
{
outBuf[outPos++] = I2CGetByte(w, 0);
}
outBuf[outPos++] = I2CGetByte(w, 1);
}
else status = PI_BAD_I2C_RLEN;
}
else status = PI_I2C_READ_FAILED;
}
else status = PI_BAD_I2C_CMD;
break;
case PI_I2C_WRITE: case PI_I2C_WRITE:
if (inPos <= (inLen - len_bytes))
{
if (len_bytes != 1)
{
bytes=inBuf[inPos] + (inBuf[inPos+1]<<8);
inPos += 2;
}
else
{
bytes=inBuf[inPos++];
}
if (bytes && ((bytes + inPos) < inLen)) bytes = myI2CGetPar(inBuf, &inPos, inLen, &esc);
if (bytes >= 0) ack = I2CPutByte(w, addr<<1);
if (bytes > 0)
{
if (!ack)
{ {
for (i=0; i<(bytes-1); i++) if ((bytes + inPos) < inLen)
{ {
for (i=0; i<(bytes-1); i++)
{
ack = I2CPutByte(w, inBuf[inPos++]);
if (ack) status = PI_I2C_WRITE_FAILED;
}
ack = I2CPutByte(w, inBuf[inPos++]); ack = I2CPutByte(w, inBuf[inPos++]);
if (ack) status = PI_I2C_WRITE_FAILED;
} }
ack = I2CPutByte(w, inBuf[inPos++]); else status = PI_BAD_I2C_RLEN;
//if (ack) status = PI_I2C_WRITE_FAILED; } else status = PI_I2C_WRITE_FAILED;
}
else status = PI_BAD_BB_RLEN;
} }
else status = PI_BAD_BB_RLEN; else status = PI_BAD_I2C_CMD;
break; break;
default: default:
status = PI_BAD_BB_CMD; status = PI_BAD_I2C_CMD;
} }
if (setesc) esc = 1; else esc = 0;
setesc = 0;
} }
if (status >= 0) status = outPos; if (status >= 0) status = outPos;
@ -8726,37 +8867,38 @@ int bbI2CXfer(
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
int gpioSerialReadOpen(unsigned gpio, unsigned bbBaud, unsigned bbBits) int gpioSerialReadOpen(unsigned gpio, unsigned baud, unsigned data_bits)
{ {
int bitTime, timeout; int bitTime, timeout;
DBG(DBG_USER, "gpio=%d bbBaud=%d bbBits=%d", gpio, bbBaud, bbBits); DBG(DBG_USER, "gpio=%d baud=%d data_bits=%d", gpio, baud, data_bits);
CHECK_INITED; CHECK_INITED;
if (gpio > PI_MAX_USER_GPIO) if (gpio > PI_MAX_USER_GPIO)
SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio); SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
if ((bbBaud < PI_BB_MIN_BAUD) || (bbBaud > PI_BB_RX_MAX_BAUD)) if ((baud < PI_BB_SER_MIN_BAUD) || (baud > PI_BB_SER_MAX_BAUD))
SOFT_ERROR(PI_BAD_WAVE_BAUD, SOFT_ERROR(PI_BAD_WAVE_BAUD,
"gpio %d, bad baud rate (%d)", gpio, bbBaud); "gpio %d, bad baud rate (%d)", gpio, baud);
if ((bbBits < PI_MIN_WAVE_DATABITS) || (bbBits > PI_MAX_WAVE_DATABITS)) if ((data_bits < PI_MIN_WAVE_DATABITS) ||
(data_bits > PI_MAX_WAVE_DATABITS))
SOFT_ERROR(PI_BAD_DATABITS, SOFT_ERROR(PI_BAD_DATABITS,
"gpio %d, bad data bits (%d)", gpio, bbBits); "gpio %d, bad data bits (%d)", gpio, data_bits);
if (wfRx[gpio].mode != PI_WFRX_NONE) if (wfRx[gpio].mode != PI_WFRX_NONE)
SOFT_ERROR(PI_GPIO_IN_USE, "gpio %d is already being used", gpio); SOFT_ERROR(PI_GPIO_IN_USE, "gpio %d is already being used", gpio);
bitTime = (1000 * MILLION) / bbBaud; /* nanos */ bitTime = (1000 * MILLION) / baud; /* nanos */
timeout = ((bbBits+2) * bitTime)/MILLION; /* millis */ timeout = ((data_bits+2) * bitTime)/MILLION; /* millis */
if (timeout < 1) timeout = 1; if (timeout < 1) timeout = 1;
wfRx[gpio].gpio = gpio; wfRx[gpio].gpio = gpio;
wfRx[gpio].mode = PI_WFRX_SERIAL; wfRx[gpio].mode = PI_WFRX_SERIAL;
wfRx[gpio].baud = bbBaud; wfRx[gpio].baud = baud;
wfRx[gpio].s.buf = malloc(SRX_BUF_SIZE); wfRx[gpio].s.buf = malloc(SRX_BUF_SIZE);
wfRx[gpio].s.bufSize = SRX_BUF_SIZE; wfRx[gpio].s.bufSize = SRX_BUF_SIZE;
@ -8766,10 +8908,10 @@ int gpioSerialReadOpen(unsigned gpio, unsigned bbBaud, unsigned bbBits)
wfRx[gpio].s.readPos = 0; wfRx[gpio].s.readPos = 0;
wfRx[gpio].s.writePos = 0; wfRx[gpio].s.writePos = 0;
wfRx[gpio].s.bit = -1; wfRx[gpio].s.bit = -1;
wfRx[gpio].s.dataBits = bbBits; wfRx[gpio].s.dataBits = data_bits;
if (bbBits < 9) wfRx[gpio].s.bytes = 1; if (data_bits < 9) wfRx[gpio].s.bytes = 1;
else if (bbBits < 17) wfRx[gpio].s.bytes = 2; else if (data_bits < 17) wfRx[gpio].s.bytes = 2;
else wfRx[gpio].s.bytes = 4; else wfRx[gpio].s.bytes = 4;
gpioSetAlertFunc(gpio, waveRxBit); gpioSetAlertFunc(gpio, waveRxBit);

526
pigpio.h
View File

@ -31,7 +31,7 @@ For more information, please refer to <http://unlicense.org/>
#include <stdint.h> #include <stdint.h>
#include <pthread.h> #include <pthread.h>
#define PIGPIO_VERSION 32 #define PIGPIO_VERSION 33
/*TEXT /*TEXT
@ -222,9 +222,6 @@ I2C
i2cOpen Opens an I2C device i2cOpen Opens an I2C device
i2cClose Closes an I2C device i2cClose Closes an I2C device
i2cReadDevice Reads the raw I2C device
i2cWriteDevice Writes the raw I2C device
i2cWriteQuick SMBus write quick i2cWriteQuick SMBus write quick
i2cWriteByte SMBus write byte i2cWriteByte SMBus write byte
i2cReadByte SMBus read byte i2cReadByte SMBus read byte
@ -240,6 +237,19 @@ i2cBlockProcessCall SMBus block process call
i2cWriteI2CBlockData SMBus write I2C block data i2cWriteI2CBlockData SMBus write I2C block data
i2cReadI2CBlockData SMBus read I2C block data i2cReadI2CBlockData SMBus read I2C block data
i2cReadDevice Reads the raw I2C device
i2cWriteDevice Writes the raw I2C device
i2cSwitchCombined Sets or clears the combined flag
i2cSegments Performs multiple I2C transactions
i2cZip Performs multiple I2C transactions
bbI2COpen Opens gpios for bit banging I2C
bbI2CClose Closes gpios for bit banging I2C
bbI2CZip Performs multiple bit banged I2C transactions
SPI SPI
spiOpen Opens a SPI device spiOpen Opens a SPI device
@ -514,9 +524,20 @@ typedef void *(gpioThreadFunc_t) (void *);
#define PI_WAVE_MAX_PULSES (PI_WAVE_BLOCKS * 3000) #define PI_WAVE_MAX_PULSES (PI_WAVE_BLOCKS * 3000)
#define PI_WAVE_MAX_CHARS (PI_WAVE_BLOCKS * 300) #define PI_WAVE_MAX_CHARS (PI_WAVE_BLOCKS * 300)
#define PI_BB_MIN_BAUD 50 #define PI_BB_I2C_MIN_BAUD 50
#define PI_BB_RX_MAX_BAUD 250000 #define PI_BB_I2C_MAX_BAUD 500000
#define PI_BB_TX_MAX_BAUD 1000000
#define PI_BB_SER_MIN_BAUD 50
#define PI_BB_SER_MAX_BAUD 250000
#define PI_WAVE_MIN_BAUD 50
#define PI_WAVE_MAX_BAUD 1000000
//#define PI_SPI_MIN_BAUD 32000
//#define PI_SPI_MAX_BAUD 125000000
#define PI_SPI_MIN_BAUD 1
#define PI_SPI_MAX_BAUD 500000000
#define PI_MIN_WAVE_DATABITS 1 #define PI_MIN_WAVE_DATABITS 1
#define PI_MAX_WAVE_DATABITS 32 #define PI_MAX_WAVE_DATABITS 32
@ -535,14 +556,12 @@ typedef void *(gpioThreadFunc_t) (void *);
/* I2C, SPI, SER */ /* I2C, SPI, SER */
#define MIN_SPI_SPEED 32000
#define MAX_SPI_SPEED 125000000
#define PI_I2C_SLOTS 32 #define PI_I2C_SLOTS 32
#define PI_SPI_SLOTS 16 #define PI_SPI_SLOTS 16
#define PI_SER_SLOTS 8 #define PI_SER_SLOTS 8
#define PI_NUM_I2C_BUS 2 #define PI_NUM_I2C_BUS 2
#define PI_MAX_I2C_ADDR 0x7F
#define PI_NUM_AUX_SPI_CHANNEL 3 #define PI_NUM_AUX_SPI_CHANNEL 3
#define PI_NUM_STD_SPI_CHANNEL 2 #define PI_NUM_STD_SPI_CHANNEL 2
@ -554,7 +573,7 @@ typedef void *(gpioThreadFunc_t) (void *);
#define PI_I2C_RDRW_IOCTL_MAX_MSGS 42 #define PI_I2C_RDRW_IOCTL_MAX_MSGS 42
/* flags for pi_i2c_msg_t */ /* flags for i2cTransaction, pi_i2c_msg_t */
#define PI_I2C_M_WR 0x0000 /* write data */ #define PI_I2C_M_WR 0x0000 /* write data */
#define PI_I2C_M_RD 0x0001 /* read data */ #define PI_I2C_M_RD 0x0001 /* read data */
@ -565,25 +584,18 @@ typedef void *(gpioThreadFunc_t) (void *);
#define PI_I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */ #define PI_I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define PI_I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */ #define PI_I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
/* bit bang I2C commands */ /* bbI2CZip and i2cZip commands */
#define PI_I2C_END 0 #define PI_I2C_END 0
#define PI_I2C_START 1 #define PI_I2C_ESC 1
#define PI_I2C_STOP 2 #define PI_I2C_START 2
#define PI_I2C_READ 3 #define PI_I2C_COMBINED_ON 2
#define PI_I2C_WRITE 4 #define PI_I2C_STOP 3
#define PI_I2C_READ16 5 #define PI_I2C_COMBINED_OFF 3
#define PI_I2C_WRITE16 6 #define PI_I2C_ADDR 4
#define PI_I2C_FLAGS 5
/* combined transaction I2C flag commands */ #define PI_I2C_READ 6
#define PI_I2C_WRITE 7
#define PI_I2C_F_CLEAR 40 /* clear flags */
#define PI_I2C_F_TEN 41 /* ten bit chip address */
#define PI_I2C_F_RECV_LEN 42 /* length will be first received byte */
#define PI_I2C_F_NO_RD_ACK 43 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define PI_I2C_F_IGNORE_NAK 44 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define PI_I2C_F_REV_DIR_ADDR 45 /* if I2C_FUNC_PROTOCOL_MANGLING */
#define PI_I2C_F_NOSTART 46 /* if I2C_FUNC_PROTOCOL_MANGLING */
/* SPI */ /* SPI */
@ -1429,9 +1441,9 @@ D*/
/*F*/ /*F*/
int gpioWaveAddSerial int gpioWaveAddSerial
(unsigned user_gpio, (unsigned user_gpio,
unsigned bbBaud, unsigned baud,
unsigned bbBits, unsigned data_bits,
unsigned bbStop, unsigned stop_bits,
unsigned offset, unsigned offset,
unsigned numBytes, unsigned numBytes,
char *str); char *str);
@ -1442,9 +1454,9 @@ microseconds from the start of the waveform.
. . . .
user_gpio: 0-31 user_gpio: 0-31
bbBaud: 100-1000000 baud: 50-1000000
bbBits: 1-32 data_bits: 1-32
bbStop: 2-8 stop_bits: 2-8
offset: 0- offset: 0-
numBytes: 1- numBytes: 1-
str: an array of chars (which may contain nulls) str: an array of chars (which may contain nulls)
@ -1457,19 +1469,19 @@ or PI_TOO_MANY_PULSES.
NOTES: NOTES:
The serial data is formatted as one start bit, bbBits data bits, and The serial data is formatted as one start bit, data_bits data bits, and
bbStop/2 stop bits. stop_bits/2 stop bits.
It is legal to add serial data streams with different baud rates to It is legal to add serial data streams with different baud rates to
the same waveform. the same waveform.
numBytes is the number of bytes of data in str. numBytes is the number of bytes of data in str.
The bytes required for each character depend upon bbBits. The bytes required for each character depend upon data_bits.
For bbBits 1-8 there will be one byte per character. For data_bits 1-8 there will be one byte per character.
For bbBits 9-16 there will be two bytes per character. For data_bits 9-16 there will be two bytes per character.
For bbBits 17-32 there will be four bytes per character. For data_bits 17-32 there will be four bytes per character.
... ...
#define MSG_LEN 8 #define MSG_LEN 8
@ -1690,14 +1702,14 @@ D*/
/*F*/ /*F*/
int gpioSerialReadOpen(unsigned user_gpio, unsigned bbBaud, unsigned bbBits); int gpioSerialReadOpen(unsigned user_gpio, unsigned baud, unsigned data_bits);
/*D /*D
This function opens a gpio for bit bang reading of serial data. This function opens a gpio for bit bang reading of serial data.
. . . .
user_gpio: 0-31 user_gpio: 0-31
bbBaud: 100-250000 baud: 50-250000
bbBits: 1-32 data_bits: 1-32
. . . .
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_WAVE_BAUD, Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_WAVE_BAUD,
@ -1727,11 +1739,11 @@ Returns the number of bytes copied if OK, otherwise PI_BAD_USER_GPIO
or PI_NOT_SERIAL_GPIO. or PI_NOT_SERIAL_GPIO.
The bytes returned for each character depend upon the number of The bytes returned for each character depend upon the number of
data bits [*bbBits*] specified in the [*gpioSerialReadOpen*] command. data bits [*data_bits*] specified in the [*gpioSerialReadOpen*] command.
For [*bbBits*] 1-8 there will be one byte per character. For [*data_bits*] 1-8 there will be one byte per character.
For [*bbBits*] 9-16 there will be two bytes per character. For [*data_bits*] 9-16 there will be two bytes per character.
For [*bbBits*] 17-32 there will be four bytes per character. For [*data_bits*] 17-32 there will be four bytes per character.
D*/ D*/
@ -1754,7 +1766,7 @@ This returns a handle for the device at the address on the I2C bus.
. . . .
i2cBus: 0-1 i2cBus: 0-1
i2cAddr: 0x08-0x77 i2cAddr: 0x00-0x7F
i2cFlags: 0 i2cFlags: 0
. . . .
@ -1794,42 +1806,6 @@ Returns 0 if OK, otherwise PI_BAD_HANDLE.
D*/ D*/
/*F*/
int i2cReadDevice(unsigned handle, char *buf, unsigned count);
/*D
This reads count bytes from the raw device into buf.
. .
handle: >=0, as returned by a call to [*i2cOpen*]
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.
D*/
/*F*/
int i2cWriteDevice(unsigned handle, char *buf, unsigned count);
/*D
This writes count bytes from buf to the raw device.
. .
handle: >=0, as returned by a call to [*i2cOpen*]
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.
D*/
int i2cTransaction(unsigned handle, pi_i2c_msg_t *parts, unsigned numParts);
/*
This performs a combined I2C transction made up of count parts.
*/
/*F*/ /*F*/
int i2cWriteQuick(unsigned handle, unsigned bit); int i2cWriteQuick(unsigned handle, unsigned bit);
/*D /*D
@ -2124,17 +2100,242 @@ S Addr Wr [A] Comm [A] Data [A] Data [A] ... [A] Data [A] P
. . . .
D*/ D*/
int bbI2COpen(unsigned SDA, unsigned SCL, unsigned bbBaud); /*F*/
int i2cReadDevice(unsigned handle, char *buf, unsigned count);
/*D
This reads count bytes from the raw device into buf.
int bbI2CClose(unsigned SDA); . .
handle: >=0, as returned by a call to [*i2cOpen*]
buf: an array to receive the read data bytes
count: >0, the number of bytes to read
. .
int bbI2CXfer( Returns count (>0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
unsigned SDA, PI_I2C_READ_FAILED.
char *inBuf, unsigned inLen, char *outBuf, unsigned outLen); D*/
/*F*/ /*F*/
int spiOpen(unsigned spiChan, unsigned spiBaud, unsigned spiFlags); int i2cWriteDevice(unsigned handle, char *buf, unsigned count);
/*D
This writes count bytes from buf to the raw device.
. .
handle: >=0, as returned by a call to [*i2cOpen*]
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.
D*/
/*F*/
void i2cSwitchCombined(int setting);
/*D
This sets the I2C (i2c-bcm2708) module "use combined transactions"
parameter on or off.
. .
setting: 0 to set the parameter off, non-zero to set it on
. .
NOTE: when the flag is on a write followed by a read to the same
slave address will use a repeated start (rather than a stop/start).
D*/
/*F*/
int i2cSegments(unsigned handle, pi_i2c_msg_t *segs, unsigned numSegs);
/*D
This function executes multiple I2C segments in one transaction by
calling the I2C_RDWR ioctl.
. .
handle: >=0, as returned by a call to [*i2cOpen*]
segs: an array of I2C segments
numSegs: >0, the number of I2C segments
. .
Returns the number of segments if OK, otherwise PI_BAD_I2C_SEG.
D*/
/*F*/
int i2cZip(
unsigned handle,
char *inBuf,
unsigned inLen,
char *outBuf,
unsigned outLen);
/*D
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.
. .
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.
...
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
...
D*/
/*F*/
int bbI2COpen(unsigned SDA, unsigned SCL, unsigned baud);
/*D
This function selects a pair of gpios for bit banging I2C at a
specified baud rate.
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 gpios
. .
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 gpios 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.
D*/
/*F*/
int bbI2CClose(unsigned SDA);
/*D
This function stops bit banging I2C on a pair of gpios previously
opened with [*bbI2COpen*].
. .
SDA: 0-31, the SDA gpio used in a prior call to [*bbI2COpen*]
. .
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_I2C_GPIO.
D*/
/*F*/
int bbI2CZip(
unsigned SDA,
char *inBuf,
unsigned inLen,
char *outBuf,
unsigned outLen);
/*D
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.
. .
SDA: 0-31 (as used in a prior call to [*bbI2COpen*])
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.
...
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
...
D*/
/*F*/
int spiOpen(unsigned spiChan, unsigned baud, unsigned spiFlags);
/*D /*D
This function returns a handle for the SPI device on the channel. This function returns a handle for the SPI device on the channel.
Data will be transferred at baud bits per second. The flags may Data will be transferred at baud bits per second. The flags may
@ -2147,7 +2348,7 @@ device has 3 chip selects and a selectable word size in bits.
. . . .
spiChan: 0-1 (0-2 for A+/B+/Pi2 auxiliary device) spiChan: 0-1 (0-2 for A+/B+/Pi2 auxiliary device)
spiBaud: 32K-125M (values above 30M are unlikely to work) baud: 32K-125M (values above 30M are unlikely to work)
spiFlags: see below spiFlags: see below
. . . .
@ -2267,20 +2468,24 @@ D*/
/*F*/ /*F*/
int serOpen(char *sertty, unsigned serBaud, unsigned serFlags); int serOpen(char *sertty, unsigned baud, unsigned serFlags);
/*D /*D
This function opens a serial device at a specified baud rate This function opens a serial device at a specified baud rate
with specified flags. with specified flags.
. . . .
sertty: the serial device to open, /dev/tty* sertty: the serial device to open, /dev/tty*
serBaud: the baud rate to use baud: the baud rate in bits per second, see below
serFlags: 0 serFlags: 0
. . . .
Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE, or Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE, or
PI_SER_OPEN_FAILED. PI_SER_OPEN_FAILED.
The baud rate must be one of 50, 75, 110, 134, 150,
200, 300, 600, 1200, 1800, 2400, 4800, 9500, 19200,
38400, 57600, 115200, or 230400.
No flags are currently defined. This parameter should be set to zero. No flags are currently defined. This parameter should be set to zero.
D*/ D*/
@ -3489,56 +3694,24 @@ An unsigned argument passed to a user customised function. Its
meaning is defined by the customiser. meaning is defined by the customiser.
argc:: argc::
The count of bytes passed to a user customised function. The count of bytes passed to a user customised function.
*argx:: *argx::
A pointer to an array of bytes passed to a user customised function. A pointer to an array of bytes passed to a user customised function.
Its meaning and content is defined by the customiser. Its meaning and content is defined by the customiser.
bbBaud:: baud::
The speed of serial communication (I2C, SPI, serial link, waves) in
The baud rate used for the transmission and reception of bit banged bits per second.
serial data.
. .
PI_BB_MIN_BAUD 50
PI_BB_RX_MAX_BAUD 250000
PI_BB_TX_MAX_BAUD 1000000
. .
bbBits::1-32
The number of data bits to be used when adding serial data to a
waveform.
. .
#define PI_MIN_WAVE_DATABITS 1
#define PI_MAX_WAVE_DATABITS 32
. .
bbStop::2-8
The number of (half) stop bits to be used when adding serial data
to a waveform.
. .
#define PI_MIN_WAVE_HALFSTOPBITS 2
#define PI_MAX_WAVE_HALFSTOPBITS 8
. .
bit:: bit::
A value of 0 or 1. A value of 0 or 1.
bitPos:: bitPos::
A bit position within a byte or word. The least significant bit is A bit position within a byte or word. The least significant bit is
position 0. position 0.
bits:: bits::
A value used to select gpios. If bit n of bits is set then gpio n is A value used to select gpios. If bit n of bits is set then gpio n is
selected. selected.
@ -3611,6 +3784,16 @@ count::
The number of bytes to be transferred in an I2C, SPI, or Serial The number of bytes to be transferred in an I2C, SPI, or Serial
command. command.
data_bits::1-32
The number of data bits to be used when adding serial data to a
waveform.
. .
#define PI_MIN_WAVE_DATABITS 1
#define PI_MAX_WAVE_DATABITS 32
. .
DMAchannel::0-14 DMAchannel::0-14
. . . .
PI_MIN_DMA_CHANNEL 0 PI_MIN_DMA_CHANNEL 0
@ -3767,9 +3950,8 @@ A number referencing an object opened by one of
[*serOpen*] [*serOpen*]
[*spiOpen*] [*spiOpen*]
i2cAddr::0x08-0x77 i2cAddr::
The address of a device on the I2C bus.
The address of a device on the I2C bus (0x08 - 0x77)
i2cBus::0-1 i2cBus::0-1
@ -3789,12 +3971,16 @@ PI_DISABLE_FIFO_IF 1
PI_DISABLE_SOCK_IF 2 PI_DISABLE_SOCK_IF 2
. . . .
int:: *inBuf::
A buffer used to pass data to a function.
inLen::
The number of bytes of data in a buffer.
int::
A whole number, negative or positive. A whole number, negative or positive.
level:: level::
The level of a gpio. Low or High. The level of a gpio. Low or High.
. . . .
@ -3869,22 +4055,25 @@ per character.
numPar:: 0-10 numPar:: 0-10
The number of parameters passed to a script. The number of parameters passed to a script.
numParts::
The number of parts in a combined I2C transaction.
numPulses:: numPulses::
The number of pulses to be added to a waveform. The number of pulses to be added to a waveform.
numSegs::
The number of segments in a combined I2C transaction.
offset:: offset::
The associated data starts this number of microseconds from the start of The associated data starts this number of microseconds from the start of
tghe waveform. tghe waveform.
*outBuf::
A buffer used to return data from a function.
outLen::
The size in bytes of an output buffer.
*param:: *param::
An array of script parameters. An array of script parameters.
*parts::
An array of the part transactions which make up a combined I2C transaction.
pi_i2c_msg_t:: pi_i2c_msg_t::
. . . .
typedef struct typedef struct
@ -4023,6 +4212,10 @@ The maximum number of bytes a user customised function should return.
A pointer to a buffer to receive data. A pointer to a buffer to receive data.
SCL::
The user gpio to use for the clock when bit banging I2C.
*script:: *script::
A pointer to the text of a script. A pointer to the text of a script.
@ -4031,6 +4224,10 @@ script_id::
An id of a stored script as returned by [*gpioStoreScript*]. An id of a stored script as returned by [*gpioStoreScript*].
SDA::
The user gpio to use for data when bit banging I2C.
secondaryChannel:: 0-6 secondaryChannel:: 0-6
The DMA channel used to time output waveforms. The DMA channel used to time output waveforms.
@ -4044,21 +4241,19 @@ seconds::
The number of seconds. The number of seconds.
serBaud:: *segs::
The baud rate to use on the serial link. An array of segments which make up a combined I2C transaction.
It must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
4800, 9600, 19200, 38400, 57600, 115200, 230400.
serFlags:: serFlags::
Flags which modify a serial open command. None are currently defined. Flags which modify a serial open command. None are currently defined.
*sertty:: *sertty::
The name of a serial tty device, e.g. /dev/ttyAMA0, /dev/ttyUSB0, /dev/tty1. The name of a serial tty device, e.g. /dev/ttyAMA0, /dev/ttyUSB0, /dev/tty1.
setting::
A value used to set a flag, 0 for false, non-zero for true.
signum::0-63 signum::0-63
. . . .
PI_MIN_SIGNUM 0 PI_MIN_SIGNUM 0
@ -4070,15 +4265,9 @@ size_t::
A standard type used to indicate the size of an object in bytes. A standard type used to indicate the size of an object in bytes.
*spi:: *spi::
A pointer to a [*rawSPI_t*] structure. A pointer to a [*rawSPI_t*] structure.
spiBaud::
The speed in bits per second to use for the SPI device.
spiBitFirst:: spiBitFirst::
Gpio reads are made from spiBitFirst to spiBitLast. Gpio reads are made from spiBitFirst to spiBitLast.
spiBitLast:: spiBitLast::
@ -4086,31 +4275,33 @@ spiBitLast::
Gpio reads are made from spiBitFirst to spiBitLast. Gpio reads are made from spiBitFirst to spiBitLast.
spiBits:: spiBits::
The number of bits to transfer in a raw SPI transaction. The number of bits to transfer in a raw SPI transaction.
spiChan:: spiChan::
A SPI channel, 0-2. A SPI channel, 0-2.
spiFlags:: spiFlags::
See [*spiOpen*]. See [*spiOpen*].
spiSS:: spiSS::
The SPI slave select gpio in a raw SPI transaction. The SPI slave select gpio in a raw SPI transaction.
spiTxBits:: spiTxBits::
The number of bits to transfer dring a raw SPI transaction The number of bits to transfer dring a raw SPI transaction
*str:: stop_bits::2-8
The number of (half) stop bits to be used when adding serial data
to a waveform.
. .
#define PI_MIN_WAVE_HALFSTOPBITS 2
#define PI_MAX_WAVE_HALFSTOPBITS 8
. .
*str::
An array of characters. An array of characters.
timeout:: timeout::
A gpio watchdog timeout in milliseconds. A gpio watchdog timeout in milliseconds.
. . . .
PI_MIN_WDOG_TIMEOUT 0 PI_MIN_WDOG_TIMEOUT 0
@ -4285,9 +4476,9 @@ PARAMS*/
#define PI_CMD_BI2CC 89 #define PI_CMD_BI2CC 89
#define PI_CMD_BI2CO 90 #define PI_CMD_BI2CO 90
#define PI_CMD_BI2CX 91 #define PI_CMD_BI2CZ 91
#define PI_CMD_I2CX 92 #define PI_CMD_I2CZ 92
#define PI_CMD_NOIB 99 #define PI_CMD_NOIB 99
@ -4458,13 +4649,14 @@ after this command is issued.
#define PI_BAD_STOPBITS -102 // serial (half) stop bits not 2-8 #define PI_BAD_STOPBITS -102 // serial (half) stop bits not 2-8
#define PI_MSG_TOOBIG -103 // socket/pipe message too big #define PI_MSG_TOOBIG -103 // socket/pipe message too big
#define PI_BAD_MALLOC_MODE -104 // bad memory allocation mode #define PI_BAD_MALLOC_MODE -104 // bad memory allocation mode
#define PI_TOO_MANY_PARTS -105 // too many I2C transaction parts #define PI_TOO_MANY_SEGS -105 // too many I2C transaction segments
#define PI_BAD_I2C_PART -106 // a combined I2C transaction failed #define PI_BAD_I2C_SEG -106 // an I2C transaction segment failed
#define PI_BAD_SMBUS_CMD -107 // SMBus command not supported by driver #define PI_BAD_SMBUS_CMD -107 // SMBus command not supported by driver
#define PI_NOT_I2C_GPIO -108 // no bit bang I2C in progress on gpio #define PI_NOT_I2C_GPIO -108 // no bit bang I2C in progress on gpio
#define PI_BAD_BB_WLEN -109 // bad BB write length #define PI_BAD_I2C_WLEN -109 // bad I2C write length
#define PI_BAD_BB_RLEN -110 // bad BB read length #define PI_BAD_I2C_RLEN -110 // bad I2C read length
#define PI_BAD_BB_CMD -111 // bad BB command #define PI_BAD_I2C_CMD -111 // bad I2C command
#define PI_BAD_I2C_BAUD -112 // bad I2C baud rate, not 50-500k
#define PI_PIGIF_ERR_0 -2000 #define PI_PIGIF_ERR_0 -2000
#define PI_PIGIF_ERR_99 -2099 #define PI_PIGIF_ERR_99 -2099

422
pigpio.py
View File

@ -194,9 +194,6 @@ I2C
i2c_open Opens an I2C device i2c_open Opens an I2C device
i2c_close Closes an I2C device i2c_close Closes an I2C device
i2c_read_device Reads the raw I2C device
i2c_write_device Writes the raw I2C device
i2c_write_quick SMBus write quick i2c_write_quick SMBus write quick
i2c_write_byte SMBus write byte i2c_write_byte SMBus write byte
i2c_read_byte SMBus read byte i2c_read_byte SMBus read byte
@ -212,6 +209,15 @@ i2c_block_process_call SMBus block process call
i2c_read_i2c_block_data SMBus read I2C block data i2c_read_i2c_block_data SMBus read I2C block data
i2c_write_i2c_block_data SMBus write 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_zip Performs multiple I2C transactions
bb_i2c_open Opens gpios for bit banging I2C
bb_i2c_close Closes gpios for bit banging I2C
bb_i2c_zip Performs multiple bit banged I2C transactions
SPI SPI
spi_open Opens a SPI device spi_open Opens a SPI device
@ -257,7 +263,7 @@ import threading
import os import os
import atexit import atexit
VERSION = "1.17" VERSION = "1.18"
exceptions = True exceptions = True
@ -416,7 +422,13 @@ _PI_CMD_HP =86
_PI_CMD_CF1 =87 _PI_CMD_CF1 =87
_PI_CMD_CF2 =88 _PI_CMD_CF2 =88
_PI_CMD_NOIB= 99 _PI_CMD_NOIB =99
_PI_CMD_BI2CC=89
_PI_CMD_BI2CO=90
_PI_CMD_BI2CZ=91
_PI_CMD_I2CZ =92
# pigpio error numbers # pigpio error numbers
@ -525,10 +537,14 @@ PI_BAD_DATABITS =-101
PI_BAD_STOPBITS =-102 PI_BAD_STOPBITS =-102
PI_MSG_TOOBIG =-103 PI_MSG_TOOBIG =-103
PI_BAD_MALLOC_MODE =-104 PI_BAD_MALLOC_MODE =-104
_PI_TOO_MANY_PARTS =-105 _PI_TOO_MANY_SEGS =-105
_PI_BAD_I2C_PART =-106 _PI_BAD_I2C_SEG =-106
PI_BAD_SMBUS_CMD =-107 PI_BAD_SMBUS_CMD =-107
PI_NOT_I2C_GPIO =-108 PI_NOT_I2C_GPIO =-108
PI_BAD_I2C_WLEN =-109
PI_BAD_I2C_RLEN =-110
PI_BAD_I2C_CMD =-111
PI_BAD_I2C_BAUD =-112
# pigpio error text # pigpio error text
@ -635,10 +651,14 @@ _errors=[
[PI_BAD_STOPBITS , "serial (half) stop bits not 2-8"], [PI_BAD_STOPBITS , "serial (half) stop bits not 2-8"],
[PI_MSG_TOOBIG , "socket/pipe message too big"], [PI_MSG_TOOBIG , "socket/pipe message too big"],
[PI_BAD_MALLOC_MODE , "bad memory allocation mode"], [PI_BAD_MALLOC_MODE , "bad memory allocation mode"],
[_PI_TOO_MANY_PARTS , "too many I2C transaction parts"], [_PI_TOO_MANY_SEGS , "too many I2C transaction segments"],
[_PI_BAD_I2C_PART , "a combined I2C transaction failed"], [_PI_BAD_I2C_SEG , "an I2C transaction segment failed"],
[PI_BAD_SMBUS_CMD , "SMBus command not supported"], [PI_BAD_SMBUS_CMD , "SMBus command not supported"],
[PI_NOT_I2C_GPIO , "no bit bang I2C in progress on gpio"], [PI_NOT_I2C_GPIO , "no bit bang I2C in progress on gpio"],
[PI_BAD_I2C_WLEN , "bad I2C write length"],
[PI_BAD_I2C_RLEN , "bad I2C read length"],
[PI_BAD_I2C_CMD , "bad I2C command"],
[PI_BAD_I2C_BAUD , "bad I2C baud rate, not 50-500k"],
] ]
@ -1725,7 +1745,7 @@ class pi():
return 0 return 0
def wave_add_serial( def wave_add_serial(
self, user_gpio, bb_baud, data, offset=0, bb_bits=8, bb_stop=2): self, user_gpio, baud, data, offset=0, bb_bits=8, bb_stop=2):
""" """
Adds a waveform representing serial data to the existing Adds a waveform representing serial data to the existing
waveform (if any). The serial data starts [*offset*] waveform (if any). The serial data starts [*offset*]
@ -1733,7 +1753,7 @@ class pi():
user_gpio:= gpio to transmit data. You must set the gpio mode user_gpio:= gpio to transmit data. You must set the gpio mode
to output. to output.
bb_baud:= baud rate to use. baud:= 50-1000000 bits per second.
data:= the bytes to write. data:= the bytes to write.
offset:= number of microseconds from the start of the offset:= number of microseconds from the start of the
waveform, default 0. waveform, default 0.
@ -1767,7 +1787,7 @@ class pi():
# pigpio message format # pigpio message format
# I p1 gpio # I p1 gpio
# I p2 bb_baud # I p2 baud
# I p3 len+12 # I p3 len+12
## extension ## ## extension ##
# I bb_bits # I bb_bits
@ -1777,7 +1797,7 @@ class pi():
if len(data): if len(data):
extents = [struct.pack("III", bb_bits, bb_stop, offset), data] extents = [struct.pack("III", bb_bits, bb_stop, offset), data]
return _u2i(_pigpio_command_ext( return _u2i(_pigpio_command_ext(
self.sl, _PI_CMD_WVAS, user_gpio, bb_baud, len(data)+12, extents)) self.sl, _PI_CMD_WVAS, user_gpio, baud, len(data)+12, extents))
else: else:
return 0 return 0
@ -2003,7 +2023,7 @@ class pi():
Returns a handle (>=0) for the device at the I2C bus address. Returns a handle (>=0) for the device at the I2C bus address.
i2c_bus:= 0-1. i2c_bus:= 0-1.
i2c_address:= 0x08-0x77. i2c_address:= 0x00-0x7F.
i2c_flags:= 0, no flags are currently defined. i2c_flags:= 0, no flags are currently defined.
Normally you would only use the [*i2c_**] functions if Normally you would only use the [*i2c_**] functions if
@ -2036,61 +2056,6 @@ class pi():
""" """
return _u2i(_pigpio_command(self.sl, _PI_CMD_I2CC, handle, 0)) return _u2i(_pigpio_command(self.sl, _PI_CMD_I2CC, handle, 0))
def i2c_read_device(self, handle, count):
"""
Returns count bytes read from the raw device associated
with handle.
handle:= >=0 (as returned by a prior call to [*i2c_open*]).
count:= >0, the number of bytes to read.
The returned value is a tuple of the number of bytes read and a
bytearray containing the bytes. If there was an error the
number of bytes read will be less than zero (and will contain
the error code).
...
(count, data) = pi.i2c_read_device(h, 12)
...
"""
# Don't raise exception. Must release lock.
bytes = u2i(
_pigpio_command(self.sl, _PI_CMD_I2CRD, handle, count, False))
if bytes > 0:
data = self._rxbuf(bytes)
else:
data = ""
self.sl.l.release()
return bytes, data
def i2c_write_device(self, handle, data):
"""
Writes the data bytes to the raw device associated with handle.
handle:= >=0 (as returned by a prior call to [*i2c_open*]).
data:= the bytes to write.
...
pi.i2c_write_device(h, b"\\x12\\x34\\xA8")
pi.i2c_write_device(h, b"help")
pi.i2c_write_device(h, 'help')
pi.i2c_write_device(h, [23, 56, 231])
...
"""
# I p1 handle
# I p2 0
# I p3 len
## extension ##
# s len data bytes
if len(data):
return _u2i(_pigpio_command_ext(
self.sl, _PI_CMD_I2CWD, handle, 0, len(data), [data]))
else:
return 0
def i2c_write_quick(self, handle, bit): def i2c_write_quick(self, handle, bit):
""" """
Sends a single bit to the device associated with handle. Sends a single bit to the device associated with handle.
@ -2441,7 +2406,269 @@ class pi():
self.sl.l.release() self.sl.l.release()
return bytes, data return bytes, data
def spi_open(self, spi_channel, spi_baud, spi_flags=0): def i2c_read_device(self, handle, count):
"""
Returns count bytes read from the raw device associated
with handle.
handle:= >=0 (as returned by a prior call to [*i2c_open*]).
count:= >0, the number of bytes to read.
The returned value is a tuple of the number of bytes read and a
bytearray containing the bytes. If there was an error the
number of bytes read will be less than zero (and will contain
the error code).
...
(count, data) = pi.i2c_read_device(h, 12)
...
"""
# Don't raise exception. Must release lock.
bytes = u2i(
_pigpio_command(self.sl, _PI_CMD_I2CRD, handle, count, False))
if bytes > 0:
data = self._rxbuf(bytes)
else:
data = ""
self.sl.l.release()
return bytes, data
def i2c_write_device(self, handle, data):
"""
Writes the data bytes to the raw device associated with handle.
handle:= >=0 (as returned by a prior call to [*i2c_open*]).
data:= the bytes to write.
...
pi.i2c_write_device(h, b"\\x12\\x34\\xA8")
pi.i2c_write_device(h, b"help")
pi.i2c_write_device(h, 'help')
pi.i2c_write_device(h, [23, 56, 231])
...
"""
# I p1 handle
# I p2 0
# I p3 len
## extension ##
# s len data bytes
if len(data):
return _u2i(_pigpio_command_ext(
self.sl, _PI_CMD_I2CWD, handle, 0, len(data), [data]))
else:
return 0
def i2c_zip(self, handle, data):
"""
This function executes a sequence of I2C operations. The
operations to be performed are specified by the contents of data
which contains the concatenated command codes and associated data.
handle:= >=0 (as returned by a prior call to [*i2c_open*]).
data:= the concatenated I2C commands, see below
The returned value is a tuple of the number of bytes read and a
bytearray containing the bytes. If there was an error the
number of bytes read will be less than zero (and will contain
the error code).
...
(count, data) = pi.i2c_zip(h, [4, 0x53, 7, 1, 0x32, 6, 6, 0])
...
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.
Any read I2C data is concatenated in the returned bytearray.
...
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
...
"""
# I p1 handle
# I p2 0
# I p3 len
## extension ##
# s len data bytes
# Don't raise exception. Must release lock.
bytes = u2i(_pigpio_command_ext(
self.sl, _PI_CMD_I2CZ, handle, 0, len(data), [data], False))
if bytes > 0:
data = self._rxbuf(bytes)
else:
data = ""
self.sl.l.release()
return bytes, data
def bb_i2c_open(self, SDA, SCL, baud=100000):
"""
This function selects a pair of gpios for bit banging I2C at a
specified baud rate.
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 gpios
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 gpios 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.
...
h = pi.bb_i2c_open(4, 5, 50000) # bit bang on gpio 4/5 at 50kbps
...
"""
# I p1 SDA
# I p2 SCL
# I p3 4
## extension ##
# I baud
extents = [struct.pack("I", baud)]
return _u2i(_pigpio_command_ext(
self.sl, _PI_CMD_BI2CO, SDA, SCL, 4, extents))
def bb_i2c_close(self, SDA):
"""
This function stops bit banging I2C on a pair of gpios
previously opened with [*bb_i2c_open*].
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.
...
pi.bb_i2c_close(SDA)
...
"""
return _u2i(_pigpio_command(self.sl, _PI_CMD_BI2CC, SDA, 0))
def bb_i2c_zip(self, SDA, data):
"""
This function executes a sequence of bit banged I2C operations.
The operations to be performed are specified by the contents
of data which contains the concatenated command codes and
associated data.
SDA:= 0-31 (as used in a prior call to [*bb_i2c_open*])
data:= the concatenated I2C commands, see below
The returned value is a tuple of the number of bytes read and a
bytearray containing the bytes. If there was an error the
number of bytes read will be less than zero (and will contain
the error code).
...
(count, data) = pi.bb_i2c_zip(
h, [4, 0x53, 2, 7, 1, 0x32, 2, 6, 6, 3, 0])
...
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.
Any read I2C data is concatenated in the returned bytearray.
...
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
...
"""
# I p1 SDA
# I p2 0
# I p3 len
## extension ##
# s len data bytes
# Don't raise exception. Must release lock.
bytes = u2i(_pigpio_command_ext(
self.sl, _PI_CMD_BI2CZ, SDA, 0, len(data), [data], False))
if bytes > 0:
data = self._rxbuf(bytes)
else:
data = ""
self.sl.l.release()
return bytes, data
def spi_open(self, spi_channel, baud, spi_flags=0):
""" """
Returns a handle for the SPI device on channel. Data will be Returns a handle for the SPI device on channel. Data will be
transferred at baud bits per second. The flags may be used to transferred at baud bits per second. The flags may be used to
@ -2454,7 +2681,7 @@ class pi():
spi_channel:= 0-1 (0-2 for A+/B+/Pi2 auxiliary device). spi_channel:= 0-1 (0-2 for A+/B+/Pi2 auxiliary device).
spi_baud:= 32K-125M (values above 30M are unlikely to work). baud:= 32K-125M (values above 30M are unlikely to work).
spi_flags:= see below. spi_flags:= see below.
Normally you would only use the [*spi_**] functions if Normally you would only use the [*spi_**] functions if
@ -2511,19 +2738,19 @@ class pi():
The other bits in flags should be set to zero. The other bits in flags should be set to zero.
... ...
# open SPI device on channel 1 in mode 3 at 20000 bits per second # open SPI device on channel 1 in mode 3 at 50000 bits per second
h = pi.spi_open(1, 20000, 3) h = pi.spi_open(1, 50000, 3)
... ...
""" """
# I p1 spi_channel # I p1 spi_channel
# I p2 spi_baud # I p2 baud
# I p3 4 # I p3 4
## extension ## ## extension ##
# I spi_flags # I spi_flags
extents = [struct.pack("I", spi_flags)] extents = [struct.pack("I", spi_flags)]
return _u2i(_pigpio_command_ext( return _u2i(_pigpio_command_ext(
self.sl, _PI_CMD_SPIO, spi_channel, spi_baud, 4, extents)) self.sl, _PI_CMD_SPIO, spi_channel, baud, 4, extents))
def spi_close(self, handle): def spi_close(self, handle):
""" """
@ -2631,13 +2858,13 @@ class pi():
self.sl.l.release() self.sl.l.release()
return bytes, data return bytes, data
def serial_open(self, tty, ser_baud, ser_flags=0): def serial_open(self, tty, baud, ser_flags=0):
""" """
Returns a handle for the serial tty device opened Returns a handle for the serial tty device opened
at ser_baud bits per second. at baud bits per second.
tty:= the serial device to open. tty:= the serial device to open.
ser_baud:= baud rate baud:= baud rate in bits per second, see below.
ser_flags:= 0, no flags are currently defined. ser_flags:= 0, no flags are currently defined.
Normally you would only use the [*serial_**] functions if Normally you would only use the [*serial_**] functions if
@ -2645,19 +2872,23 @@ class pi():
you will always run on the local Pi use the standard serial you will always run on the local Pi use the standard serial
module instead. module instead.
The baud rate must be one of 50, 75, 110, 134, 150,
200, 300, 600, 1200, 1800, 2400, 4800, 9500, 19200,
38400, 57600, 115200, or 230400.
... ...
h1 = pi.serial_open("/dev/ttyAMA0", 300) h1 = pi.serial_open("/dev/ttyAMA0", 300)
h2 = pi.serial_open("/dev/ttyUSB1", 19200, 0) h2 = pi.serial_open("/dev/ttyUSB1", 19200, 0)
... ...
""" """
# I p1 ser_baud # I p1 baud
# I p2 ser_flags # I p2 ser_flags
# I p3 len # I p3 len
## extension ## ## extension ##
# s len data bytes # s len data bytes
return _u2i(_pigpio_command_ext( return _u2i(_pigpio_command_ext(
self.sl, _PI_CMD_SERO, ser_baud, ser_flags, len(tty), [tty])) self.sl, _PI_CMD_SERO, baud, ser_flags, len(tty), [tty]))
def serial_close(self, handle): def serial_close(self, handle):
""" """
@ -2912,12 +3143,12 @@ class pi():
""" """
return _u2i(_pigpio_command(self.sl, _PI_CMD_PROCD, script_id, 0)) return _u2i(_pigpio_command(self.sl, _PI_CMD_PROCD, script_id, 0))
def bb_serial_read_open(self, user_gpio, bb_baud, bb_bits=8): def bb_serial_read_open(self, user_gpio, baud, bb_bits=8):
""" """
Opens a gpio for bit bang reading of serial data. Opens a gpio for bit bang reading of serial data.
user_gpio:= 0-31, the gpio to use. user_gpio:= 0-31, the gpio to use.
bb_baud:= 300-250000, the baud rate. baud:= 50-250000, the baud rate.
bb_bits:= 1-32, the number of bits per word, default 8. bb_bits:= 1-32, the number of bits per word, default 8.
The serial data is held in a cyclic buffer and is read using The serial data is held in a cyclic buffer and is read using
@ -2934,13 +3165,13 @@ class pi():
# pigpio message format # pigpio message format
# I p1 user_gpio # I p1 user_gpio
# I p2 bb_baud # I p2 baud
# I p3 4 # I p3 4
## extension ## ## extension ##
# I bb_bits # I bb_bits
extents = [struct.pack("I", bb_bits)] extents = [struct.pack("I", bb_bits)]
return _u2i(_pigpio_command_ext( return _u2i(_pigpio_command_ext(
self.sl, _PI_CMD_SLRO, user_gpio, bb_baud, 4, extents)) self.sl, _PI_CMD_SLRO, user_gpio, baud, 4, extents))
def bb_serial_read(self, user_gpio): def bb_serial_read(self, user_gpio):
""" """
@ -3222,16 +3453,15 @@ def xref():
An array of bytes passed to a user customised function. An array of bytes passed to a user customised function.
Its meaning and content is defined by the customiser. Its meaning and content is defined by the customiser.
bb_baud: 100 - 250000 baud:
The baud rate used for the transmission of bit bang serial data. The speed of serial communication (I2C, SPI, serial link, waves)
in bits per second.
bb_bits: 1-32 bb_bits: 1-32
The number of data bits to be used when adding serial data to a The number of data bits to be used when adding serial data to a
waveform. waveform.
bb_stop: 2-8 bb_stop: 2-8
The number of (half) stop bits to be used when adding serial data The number of (half) stop bits to be used when adding serial data
to a waveform. to a waveform.
@ -3424,7 +3654,7 @@ def xref():
One of the i2c_ functions. One of the i2c_ functions.
i2c_address: i2c_address:
The address of a device on the I2C bus (0x08 - 0x77) The address of a device on the I2C bus.
i2c_bus: 0-1 i2c_bus: 0-1
An I2C bus number. An I2C bus number.
@ -3496,18 +3726,17 @@ def xref():
The maximum number of bytes a user customised function The maximum number of bytes a user customised function
should return, default 8192. should return, default 8192.
SCL:
The user gpio to use for the clock when bit banging I2C.
script: script:
The text of a script to store on the pigpio daemon. The text of a script to store on the pigpio daemon.
script_id: 0- script_id: 0-
A number referencing a script created by [*store_script*]. A number referencing a script created by [*store_script*].
ser_baud: SDA:
The transmission rate in bits per second. The user gpio to use for data when bit banging I2C.
The allowable values are 50, 75, 110, 134, 150, 200, 300,
600, 1200, 1800, 2400, 4800, 9600, 19200, 38400,
57600, 115200, or 230400.
ser_flags: 32 bit ser_flags: 32 bit
No serial flags are currently defined. No serial flags are currently defined.
@ -3518,9 +3747,6 @@ def xref():
spi_*: spi_*:
One of the spi_ functions. One of the spi_ functions.
spi_baud: 32K-125M
The transmission rate in bits per second.
spi_channel: 0-2 spi_channel: 0-2
A SPI channel. A SPI channel.

View File

@ -1417,7 +1417,7 @@ for gpio#1 can be added, and then gpio#2 etc.
If the added waveform is intended to start after or within the existing If the added waveform is intended to start after or within the existing
waveform then the first pulse should consist solely of a delay. waveform then the first pulse should consist solely of a delay.
.IP "\fBint wave_add_serial(unsigned user_gpio, unsigned bbBaud, unsigned bbBits, unsigned bbStop, unsigned offset, unsigned numBytes, char *str)\fP" .IP "\fBint wave_add_serial(unsigned user_gpio, unsigned baud, unsigned data_bits, unsigned stop_bits, unsigned offset, unsigned numBytes, char *str)\fP"
.IP "" 4 .IP "" 4
This function adds a waveform representing serial data to the This function adds a waveform representing serial data to the
existing waveform (if any). The serial data starts offset existing waveform (if any). The serial data starts offset
@ -1430,11 +1430,11 @@ microseconds from the start of the waveform.
.EX .EX
user_gpio: 0-31. user_gpio: 0-31.
.br .br
bbBaud: 100-250000 baud: 50-1000000
.br .br
bbBits: number of data bits (1-32) data_bits: number of data bits (1-32)
.br .br
bbStop: number of stop half bits (2-8) stop_bits: number of stop half bits (2-8)
.br .br
offset: 0- offset: 0-
.br .br
@ -1461,8 +1461,8 @@ NOTES:
.br .br
.br .br
The serial data is formatted as one start bit, \fBbbBits\fP data bits, The serial data is formatted as one start bit, \fBdata_bits\fP data bits,
and \fBbbStop\fP/2 stop bits. and \fBstop_bits\fP/2 stop bits.
.br .br
@ -1478,16 +1478,16 @@ the same waveform.
.br .br
.br .br
The bytes required for each character depend upon \fBbbBits\fP. The bytes required for each character depend upon \fBdata_bits\fP.
.br .br
.br .br
For \fBbbBits\fP 1-8 there will be one byte per character. For \fBdata_bits\fP 1-8 there will be one byte per character.
.br .br
For \fBbbBits\fP 9-16 there will be two bytes per character. For \fBdata_bits\fP 9-16 there will be two bytes per character.
.br .br
For \fBbbBits\fP 17-32 there will be four bytes per character. For \fBdata_bits\fP 17-32 there will be four bytes per character.
.IP "\fBint wave_create(void)\fP" .IP "\fBint wave_create(void)\fP"
.IP "" 4 .IP "" 4
@ -1934,7 +1934,7 @@ script_id: >=0, as returned by \fBstore_script\fP.
.br .br
The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID. The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID.
.IP "\fBint bb_serial_read_open(unsigned user_gpio, unsigned bbBaud, unsigned bbBits)\fP" .IP "\fBint bb_serial_read_open(unsigned user_gpio, unsigned baud, unsigned data_bits)\fP"
.IP "" 4 .IP "" 4
This function opens a gpio for bit bang reading of serial data. This function opens a gpio for bit bang reading of serial data.
@ -1945,9 +1945,9 @@ This function opens a gpio for bit bang reading of serial data.
.EX .EX
user_gpio: 0-31. user_gpio: 0-31.
.br .br
bbBaud: 100-250000 baud: 50-250000
.br .br
bbBits: 1-32 data_bits: 1-32
.br .br
.EE .EE
@ -1999,16 +1999,16 @@ or PI_NOT_SERIAL_GPIO.
.br .br
The bytes returned for each character depend upon the number of The bytes returned for each character depend upon the number of
data bits \fBbbBits\fP specified in the \fBbb_serial_read_open\fP command. data bits \fBdata_bits\fP specified in the \fBbb_serial_read_open\fP command.
.br .br
.br .br
For \fBbbBits\fP 1-8 there will be one byte per character. For \fBdata_bits\fP 1-8 there will be one byte per character.
.br .br
For \fBbbBits\fP 9-16 there will be two bytes per character. For \fBdata_bits\fP 9-16 there will be two bytes per character.
.br .br
For \fBbbBits\fP 17-32 there will be four bytes per character. For \fBdata_bits\fP 17-32 there will be four bytes per character.
.IP "\fBint bb_serial_read_close(unsigned user_gpio)\fP" .IP "\fBint bb_serial_read_close(unsigned user_gpio)\fP"
.IP "" 4 .IP "" 4
@ -2040,7 +2040,7 @@ This returns a handle for the device at address i2c_addr on bus i2c_bus.
.EX .EX
i2c_bus: 0-1. i2c_bus: 0-1.
.br .br
i2c_addr: 0x08-0x77. i2c_addr: 0x00-0x7F.
.br .br
i2c_flags: 0. i2c_flags: 0.
.br .br
@ -2077,54 +2077,6 @@ handle: >=0, as returned by a call to \fBi2c_open\fP.
.br .br
Returns 0 if OK, otherwise PI_BAD_HANDLE. Returns 0 if OK, otherwise PI_BAD_HANDLE.
.IP "\fBint i2c_read_device(unsigned handle, char *buf, unsigned count)\fP"
.IP "" 4
This reads count bytes from the raw device into buf.
.br
.br
.EX
handle: >=0, as returned by a call to \fBi2c_open\fP.
.br
buf: an array to receive the read data bytes.
.br
count: >0, the number of bytes to read.
.br
.EE
.br
.br
Returns count (>0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
PI_I2C_READ_FAILED.
.IP "\fBint i2c_write_device(unsigned handle, char *buf, unsigned count)\fP"
.IP "" 4
This writes count bytes from buf to the raw device.
.br
.br
.EX
handle: >=0, as returned by a call to \fBi2c_open\fP.
.br
buf: an array containing the data bytes to write.
.br
count: >0, the number of bytes to write.
.br
.EE
.br
.br
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
PI_I2C_WRITE_FAILED.
.IP "\fBint i2c_write_quick(unsigned handle, unsigned bit)\fP" .IP "\fBint i2c_write_quick(unsigned handle, unsigned bit)\fP"
.IP "" 4 .IP "" 4
This sends a single bit (in the Rd/Wr bit) to the device associated This sends a single bit (in the Rd/Wr bit) to the device associated
@ -2516,7 +2468,385 @@ i2c_reg: 0-255, the register to write.
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
PI_I2C_WRITE_FAILED. PI_I2C_WRITE_FAILED.
.IP "\fBint spi_open(unsigned spi_channel, unsigned spi_baud, unsigned spi_flags)\fP" .IP "\fBint i2c_read_device(unsigned handle, char *buf, unsigned count)\fP"
.IP "" 4
This reads count bytes from the raw device into buf.
.br
.br
.EX
handle: >=0, as returned by a call to \fBi2c_open\fP.
.br
buf: an array to receive the read data bytes.
.br
count: >0, the number of bytes to read.
.br
.EE
.br
.br
Returns count (>0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
PI_I2C_READ_FAILED.
.IP "\fBint i2c_write_device(unsigned handle, char *buf, unsigned count)\fP"
.IP "" 4
This writes count bytes from buf to the raw device.
.br
.br
.EX
handle: >=0, as returned by a call to \fBi2c_open\fP.
.br
buf: an array containing the data bytes to write.
.br
count: >0, the number of bytes to write.
.br
.EE
.br
.br
Returns 0 if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or
PI_I2C_WRITE_FAILED.
.IP "\fBint i2c_zip(unsigned handle, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)\fP"
.IP "" 4
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
.EX
handle: >=0, as returned by a call to \fBi2cOpen\fP
.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
.EE
.br
.br
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.
.br
.br
The following command codes are supported:
.br
.br
Name Cmd & Data Meaning
.br
End 0 No more commands
.br
Escape 1 Next P is two bytes
.br
On 2 Switch combined flag on
.br
Off 3 Switch combined flag off
.br
Address 4 P Set I2C address to P
.br
Flags 5 lsb msb Set I2C flags to lsb + (msb << 8)
.br
Read 6 P Read P bytes of data
.br
Write 7 P ... Write P bytes of data
.br
.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.
.br
.br
\fBExample\fP
.br
.EX
Set address 0x53, write 0x32, read 6 bytes
.br
Set address 0x1E, write 0x03, read 6 bytes
.br
Set address 0x68, write 0x1B, read 8 bytes
.br
End
.br
.br
0x04 0x53 0x07 0x01 0x32 0x06 0x06
.br
0x04 0x1E 0x07 0x01 0x03 0x06 0x06
.br
0x04 0x68 0x07 0x01 0x1B 0x06 0x08
.br
0x00
.br
.EE
.br
.br
.IP "\fBint bb_i2c_open(unsigned SDA, unsigned SCL, unsigned baud)\fP"
.IP "" 4
This function selects a pair of gpios 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 gpios
.br
.br
.EX
SDA: 0-31
.br
SCL: 0-31
.br
baud: 50-500000
.br
.EE
.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 gpios 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.
.IP "\fBint bb_i2c_close(unsigned SDA)\fP"
.IP "" 4
This function stops bit banging I2C on a pair of gpios previously
opened with \fBbb_i2c_open\fP.
.br
.br
.EX
SDA: 0-31, the SDA gpio used in a prior call to \fBbb_i2c_open\fP
.br
.EE
.br
.br
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_I2C_GPIO.
.IP "\fBint bb_i2c_zip(unsigned SDA, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)\fP"
.IP "" 4
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
.EX
SDA: 0-31 (as used in a prior call to \fBbb_i2c_open\fP)
.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
.EE
.br
.br
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.
.br
.br
The following command codes are supported:
.br
.br
Name Cmd & Data Meaning
.br
End 0 No more commands
.br
Escape 1 Next P is two bytes
.br
Start 2 Start condition
.br
Stop 3 Stop condition
.br
Address 4 P Set I2C address to P
.br
Flags 5 lsb msb Set I2C flags to lsb + (msb << 8)
.br
Read 6 P Read P bytes of data
.br
Write 7 P ... Write P bytes of data
.br
.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.
.br
.br
\fBExample\fP
.br
.EX
Set address 0x53
.br
start, write 0x32, (re)start, read 6 bytes, stop
.br
Set address 0x1E
.br
start, write 0x03, (re)start, read 6 bytes, stop
.br
Set address 0x68
.br
start, write 0x1B, (re)start, read 8 bytes, stop
.br
End
.br
.br
0x04 0x53
.br
0x02 0x07 0x01 0x32 0x02 0x06 0x06 0x03
.br
.br
0x04 0x1E
.br
0x02 0x07 0x01 0x03 0x02 0x06 0x06 0x03
.br
.br
0x04 0x68
.br
0x02 0x07 0x01 0x1B 0x02 0x06 0x08 0x03
.br
.br
0x00
.br
.EE
.IP "\fBint spi_open(unsigned spi_channel, unsigned baud, unsigned spi_flags)\fP"
.IP "" 4 .IP "" 4
This function returns a handle for the SPI device on channel. This function returns a handle for the SPI device on channel.
Data will be transferred at baud bits per second. The flags may Data will be transferred at baud bits per second. The flags may
@ -2537,7 +2867,7 @@ device has 3 chip selects and a selectable word size in bits.
.EX .EX
spi_channel: 0-1 (0-2 for A+/B+/Pi2 auxiliary device). spi_channel: 0-1 (0-2 for A+/B+/Pi2 auxiliary device).
.br .br
spi_baud: 32K-125M (values above 30M are unlikely to work). baud: 32K-125M (values above 30M are unlikely to work).
.br .br
spi_flags: see below. spi_flags: see below.
.br .br
@ -2746,7 +3076,7 @@ handle: >=0, as returned by a call to \fBspi_open\fP.
Returns the number of bytes transferred if OK, otherwise Returns the number of bytes transferred if OK, otherwise
PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED. PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED.
.IP "\fBint serial_open(char *ser_tty, unsigned ser_baud, unsigned ser_flags)\fP" .IP "\fBint serial_open(char *ser_tty, unsigned baud, unsigned ser_flags)\fP"
.IP "" 4 .IP "" 4
This function opens a serial device at a specified baud rate This function opens a serial device at a specified baud rate
with specified flags. with specified flags.
@ -2758,7 +3088,7 @@ with specified flags.
.EX .EX
ser_tty: the serial device to open, /dev/tty*. ser_tty: the serial device to open, /dev/tty*.
.br .br
ser_baud: the baud rate to use. baud: the baud rate in bits per second, see below.
.br .br
ser_flags: 0. ser_flags: 0.
.br .br
@ -2773,6 +3103,13 @@ PI_SER_OPEN_FAILED.
.br .br
.br
The baud rate must be one of 50, 75, 110, 134, 150,
200, 300, 600, 1200, 1800, 2400, 4800, 9500, 19200,
38400, 57600, 115200, or 230400.
.br
.br .br
No flags are currently defined. This parameter should be set to zero. No flags are currently defined. This parameter should be set to zero.
@ -3136,73 +3473,9 @@ Its meaning and content is defined by the customiser.
.br .br
.br .IP "\fBbaud\fP" 0
The speed of serial communication (I2C, SPI, serial link, waves) in
.br bits per second.
.IP "\fBbbBaud\fP" 0
The baud rate used for the transmission and reception of bit banged
serial data.
.br
.br
.EX
PI_WAVE_MIN_BAUD 100
.br
PI_WAVE_MAX_BAUD 250000
.br
.EE
.br
.br
.IP "\fBbbBits\fP: 1-32" 0
.br
.br
The number of data bits to be used when adding serial data to a
waveform.
.br
.br
.EX
#define PI_MIN_WAVE_DATABITS 1
.br
#define PI_MAX_WAVE_DATABITS 32
.br
.EE
.br
.br
.IP "\fBbbStop\fP: 2-8" 0
.br
.br
The number of (half) stop bits to be used when adding serial data
to a waveform.
.br
.br
.EX
#define PI_MIN_WAVE_HALFSTOPBITS 2
.br
#define PI_MAX_WAVE_HALFSTOPBITS 8
.br
.EE
.br .br
@ -3316,6 +3589,25 @@ command.
.br .br
.IP "\fBdata_bits\fP: 1-32" 0
The number of data bits in each character of serial data.
.br
.br
.EX
#define PI_MIN_WAVE_DATABITS 1
.br
#define PI_MAX_WAVE_DATABITS 32
.br
.EE
.br
.br
.IP "\fBdouble\fP" 0 .IP "\fBdouble\fP" 0
A floating point number. A floating point number.
@ -3491,8 +3783,8 @@ A number referencing an object opened by one of \fBi2c_open\fP, \fBnotify_open\f
.br .br
.IP "\fBi2c_addr\fP: 0x08-0x77" 0 .IP "\fBi2c_addr\fP" 0
The address of a device on the I2C bus (0x08 - 0x77) The address of a device on the I2C bus.
.br .br
@ -3519,10 +3811,16 @@ A register of an I2C device.
.br .br
.IP "\fB*inBuf\fP" 0
A buffer used to pass data to a function.
.br .br
.br .br
.IP "\fBinLen\fP" 0
The number of bytes of data in a buffer.
.br .br
.br .br
@ -3643,6 +3941,20 @@ the waveform.
.br .br
.IP "\fB*outBuf\fP" 0
A buffer used to return data from a function.
.br
.br
.IP "\fBoutLen\fP" 0
The size in bytes of an output buffer.
.br
.br
.IP "\fB*param\fP" 0 .IP "\fB*param\fP" 0
An array of script parameters. An array of script parameters.
@ -3650,10 +3962,6 @@ An array of script parameters.
.br .br
.br
.br
.IP "\fB*portStr\fP" 0 .IP "\fB*portStr\fP" 0
A string specifying the port address used by the Pi running A string specifying the port address used by the Pi running
the pigpio daemon. It may be NULL in which case "8888" the pigpio daemon. It may be NULL in which case "8888"
@ -3806,6 +4114,13 @@ A pointer to a buffer to receive data.
.br .br
.IP "\fBSCL\fP" 0
The user gpio to use for the clock when bit banging I2C.
.br
.br
.IP "\fB*script\fP" 0 .IP "\fB*script\fP" 0
A pointer to the text of a script. A pointer to the text of a script.
@ -3820,6 +4135,9 @@ An id of a stored script as returned by \fBstore_script\fP.
.br .br
.IP "\fBSDA\fP" 0
The user gpio to use for data when bit banging I2C.
.br .br
.br .br
@ -3831,19 +4149,6 @@ The number of seconds.
.br .br
.IP "\fBser_baud\fP" 0
The baud rate to use on the serial link.
.br
.br
It must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
4800, 9600, 19200, 38400, 57600, 115200, 230400.
.br
.br
.IP "\fBser_flags\fP" 0 .IP "\fBser_flags\fP" 0
Flags which modify a serial open command. None are currently defined. Flags which modify a serial open command. None are currently defined.
@ -3865,13 +4170,6 @@ A standard type used to indicate the size of an object in bytes.
.br .br
.IP "\fBspi_baud\fP" 0
The speed in bits per second to use for the SPI device.
.br
.br
.IP "\fBspi_channel\fP" 0 .IP "\fBspi_channel\fP" 0
A SPI channel, 0-2. A SPI channel, 0-2.
@ -3886,6 +4184,26 @@ See \fBspi_open\fP.
.br .br
.IP "\fBstop_bits\fP: 2-8" 0
The number of (half) stop bits to be used when adding serial data
to a waveform.
.br
.br
.EX
#define PI_MIN_WAVE_HALFSTOPBITS 2
.br
#define PI_MAX_WAVE_HALFSTOPBITS 8
.br
.EE
.br
.br
.IP "\fB*str\fP" 0 .IP "\fB*str\fP" 0
An array of characters. An array of characters.

View File

@ -25,7 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/> For more information, please refer to <http://unlicense.org/>
*/ */
/* PIGPIOD_IF_VERSION 14 */ /* PIGPIOD_IF_VERSION 15 */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -837,6 +837,28 @@ int run_script(unsigned script_id, unsigned numPar, uint32_t *param)
(gPigCommand, PI_CMD_PROCR, script_id, 0, numPar*4, 1, ext, 1); (gPigCommand, PI_CMD_PROCR, script_id, 0, numPar*4, 1, ext, 1);
} }
int recvMax(void *buf, int bufsize, int sent)
{
uint8_t scratch[4096];
int remaining, fetch, count;
if (sent < bufsize) count = sent; else count = bufsize;
if (count) recv(gPigCommand, buf, count, MSG_WAITALL);
remaining = sent - count;
while (remaining)
{
fetch = remaining;
if (fetch > sizeof(scratch)) fetch = sizeof(scratch);
recv(gPigCommand, scratch, fetch, MSG_WAITALL);
remaining -= fetch;
}
return count;
}
int script_status(unsigned script_id, uint32_t *param) int script_status(unsigned script_id, uint32_t *param)
{ {
int status; int status;
@ -846,9 +868,7 @@ int script_status(unsigned script_id, uint32_t *param)
if (status > 0) if (status > 0)
{ {
/* get the data */ recvMax(p, sizeof(p), status);
recv(gPigCommand, p, status, MSG_WAITALL);
status = p[0]; status = p[0];
if (param) memcpy(param, p+1, sizeof(p)-4); if (param) memcpy(param, p+1, sizeof(p)-4);
} }
@ -891,8 +911,7 @@ int bb_serial_read(unsigned user_gpio, void *buf, size_t bufSize)
if (bytes > 0) if (bytes > 0)
{ {
/* get the data */ bytes = recvMax(buf, bufSize, bytes);
recv(gPigCommand, buf, bytes, MSG_WAITALL);
} }
pthread_mutex_unlock(&command_mutex); pthread_mutex_unlock(&command_mutex);
@ -925,42 +944,6 @@ int i2c_open(unsigned i2c_bus, unsigned i2c_addr, uint32_t i2c_flags)
int i2c_close(unsigned handle) int i2c_close(unsigned handle)
{return pigpio_command(gPigCommand, PI_CMD_I2CC, handle, 0, 1);} {return pigpio_command(gPigCommand, PI_CMD_I2CC, handle, 0, 1);}
int i2c_read_device(unsigned handle, char *buf, unsigned count)
{
int bytes;
bytes = pigpio_command(gPigCommand, PI_CMD_I2CRD, handle, count, 0);
if (bytes > 0)
{
/* get the data */
recv(gPigCommand, buf, bytes, MSG_WAITALL);
}
pthread_mutex_unlock(&command_mutex);
return bytes;
}
int i2c_write_device(unsigned handle, char *buf, unsigned count)
{
gpioExtent_t ext[1];
/*
p1=handle
p2=0
p3=count
## extension ##
char buf[count]
*/
ext[0].size = count;
ext[0].ptr = buf;
return pigpio_command_ext
(gPigCommand, PI_CMD_I2CWD, handle, 0, count, 1, ext, 1);
}
int i2c_write_quick(unsigned handle, unsigned bit) int i2c_write_quick(unsigned handle, unsigned bit)
{return pigpio_command(gPigCommand, PI_CMD_I2CWQ, handle, bit, 1);} {return pigpio_command(gPigCommand, PI_CMD_I2CWQ, handle, bit, 1);}
@ -1061,8 +1044,7 @@ int i2c_read_block_data(unsigned handle, unsigned reg, char *buf)
if (bytes > 0) if (bytes > 0)
{ {
/* get the data */ bytes = recvMax(buf, 32, bytes);
recv(gPigCommand, buf, bytes, MSG_WAITALL);
} }
pthread_mutex_unlock(&command_mutex); pthread_mutex_unlock(&command_mutex);
@ -1092,8 +1074,7 @@ int i2c_block_process_call(
if (bytes > 0) if (bytes > 0)
{ {
/* get the data */ bytes = recvMax(buf, 32, bytes);
recv(gPigCommand, buf, bytes, MSG_WAITALL);
} }
pthread_mutex_unlock(&command_mutex); pthread_mutex_unlock(&command_mutex);
@ -1123,8 +1104,7 @@ int i2c_read_i2c_block_data(
if (bytes > 0) if (bytes > 0)
{ {
/* get the data */ bytes = recvMax(buf, count, bytes);
recv(gPigCommand, buf, bytes, MSG_WAITALL);
} }
pthread_mutex_unlock(&command_mutex); pthread_mutex_unlock(&command_mutex);
@ -1153,6 +1133,131 @@ int i2c_write_i2c_block_data(
(gPigCommand, PI_CMD_I2CWI, handle, reg, count, 1, ext, 1); (gPigCommand, PI_CMD_I2CWI, handle, reg, count, 1, ext, 1);
} }
int i2c_read_device(unsigned handle, char *buf, unsigned count)
{
int bytes;
bytes = pigpio_command(gPigCommand, PI_CMD_I2CRD, handle, count, 0);
if (bytes > 0)
{
bytes = recvMax(buf, count, bytes);
}
pthread_mutex_unlock(&command_mutex);
return bytes;
}
int i2c_write_device(unsigned handle, char *buf, unsigned count)
{
gpioExtent_t ext[1];
/*
p1=handle
p2=0
p3=count
## extension ##
char buf[count]
*/
ext[0].size = count;
ext[0].ptr = buf;
return pigpio_command_ext
(gPigCommand, PI_CMD_I2CWD, handle, 0, count, 1, ext, 1);
}
int i2c_zip(
unsigned handle,
char *inBuf,
unsigned inLen,
char *outBuf,
unsigned outLen)
{
int bytes;
gpioExtent_t ext[1];
/*
p1=handle
p2=0
p3=inLen
## extension ##
char inBuf[inLen]
*/
ext[0].size = inLen;
ext[0].ptr = inBuf;
bytes = pigpio_command_ext
(gPigCommand, PI_CMD_I2CZ, handle, 0, inLen, 1, ext, 0);
if (bytes > 0)
{
bytes = recvMax(outBuf, outLen, bytes);
}
pthread_mutex_unlock(&command_mutex);
return bytes;
}
int bb_i2c_open(unsigned SDA, unsigned SCL, unsigned baud)
{
gpioExtent_t ext[1];
/*
p1=SDA
p2=SCL
p3=4
## extension ##
uint32_t baud
*/
ext[0].size = sizeof(uint32_t);
ext[0].ptr = &baud;
return pigpio_command_ext
(gPigCommand, PI_CMD_BI2CO, SDA, SCL, 4, 1, ext, 1);
}
int bb_i2c_close(unsigned SDA)
{return pigpio_command(gPigCommand, PI_CMD_BI2CC, SDA, 0, 1);}
int bb_i2c_zip(
unsigned SDA,
char *inBuf,
unsigned inLen,
char *outBuf,
unsigned outLen)
{
int bytes;
gpioExtent_t ext[1];
/*
p1=SDA
p2=0
p3=inLen
## extension ##
char inBuf[inLen]
*/
ext[0].size = inLen;
ext[0].ptr = inBuf;
bytes = pigpio_command_ext
(gPigCommand, PI_CMD_BI2CZ, SDA, 0, inLen, 1, ext, 0);
if (bytes > 0)
{
bytes = recvMax(outBuf, outLen, bytes);
}
pthread_mutex_unlock(&command_mutex);
return bytes;
}
int spi_open(unsigned channel, unsigned speed, uint32_t flags) int spi_open(unsigned channel, unsigned speed, uint32_t flags)
{ {
gpioExtent_t ext[1]; gpioExtent_t ext[1];
@ -1184,8 +1289,7 @@ int spi_read(unsigned handle, char *buf, unsigned count)
if (bytes > 0) if (bytes > 0)
{ {
/* get the data */ bytes = recvMax(buf, count, bytes);
recv(gPigCommand, buf, bytes, MSG_WAITALL);
} }
pthread_mutex_unlock(&command_mutex); pthread_mutex_unlock(&command_mutex);
@ -1233,8 +1337,7 @@ int spi_xfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count)
if (bytes > 0) if (bytes > 0)
{ {
/* get the data */ bytes = recvMax(rxBuf, count, bytes);
recv(gPigCommand, rxBuf, bytes, MSG_WAITALL);
} }
pthread_mutex_unlock(&command_mutex); pthread_mutex_unlock(&command_mutex);
@ -1301,8 +1404,7 @@ int serial_read(unsigned handle, char *buf, unsigned count)
if (bytes > 0) if (bytes > 0)
{ {
/* get the data */ bytes = recvMax(buf, count, bytes);
recv(gPigCommand, buf, bytes, MSG_WAITALL);
} }
pthread_mutex_unlock(&command_mutex); pthread_mutex_unlock(&command_mutex);
@ -1355,8 +1457,7 @@ int custom_2(unsigned arg1, char *argx, unsigned count,
if (bytes > 0) if (bytes > 0)
{ {
/* get the data */ bytes = recvMax(retBuf, retMax, bytes);
recv(gPigCommand, retBuf, bytes, MSG_WAITALL);
} }
pthread_mutex_unlock(&command_mutex); pthread_mutex_unlock(&command_mutex);

View File

@ -30,7 +30,7 @@ For more information, please refer to <http://unlicense.org/>
#include "pigpio.h" #include "pigpio.h"
#define PIGPIOD_IF_VERSION 14 #define PIGPIOD_IF_VERSION 15
/*TEXT /*TEXT
@ -209,9 +209,6 @@ I2C
i2c_open Opens an I2C device i2c_open Opens an I2C device
i2c_close Closes an I2C device i2c_close Closes an I2C device
i2c_read_device Reads the raw I2C device
i2c_write_device Writes the raw I2C device
i2c_write_quick smbus write quick i2c_write_quick smbus write quick
i2c_write_byte smbus write byte i2c_write_byte smbus write byte
i2c_read_byte smbus read byte i2c_read_byte smbus read byte
@ -227,6 +224,15 @@ i2c_block_process_call smbus block process call
i2c_write_i2c_block_data smbus write I2C block data i2c_write_i2c_block_data smbus write I2C block data
i2c_read_i2c_block_data smbus read I2C block data i2c_read_i2c_block_data smbus read I2C block data
i2c_read_device Reads the raw I2C device
i2c_write_device Writes the raw I2C device
i2c_zip Performs multiple I2C transactions
bb_i2c_open Opens gpios for bit banging I2C
bb_i2c_close Closes gpios for bit banging I2C
bb_i2c_zip Performs multiple bit banged I2C transactions
SPI SPI
spi_open Opens a SPI device spi_open Opens a SPI device
@ -1028,8 +1034,8 @@ D*/
/*F*/ /*F*/
int wave_add_serial int wave_add_serial
(unsigned user_gpio, unsigned bbBaud, unsigned bbBits, (unsigned user_gpio, unsigned baud, unsigned data_bits,
unsigned bbStop, unsigned offset, unsigned numBytes, char *str); unsigned stop_bits, unsigned offset, unsigned numBytes, char *str);
/*D /*D
This function adds a waveform representing serial data to the This function adds a waveform representing serial data to the
existing waveform (if any). The serial data starts offset existing waveform (if any). The serial data starts offset
@ -1037,9 +1043,9 @@ microseconds from the start of the waveform.
. . . .
user_gpio: 0-31. user_gpio: 0-31.
bbBaud: 100-250000 baud: 50-1000000
bbBits: number of data bits (1-32) data_bits: number of data bits (1-32)
bbStop: number of stop half bits (2-8) stop_bits: number of stop half bits (2-8)
offset: 0- offset: 0-
numBytes: 1- numBytes: 1-
str: an array of chars. str: an array of chars.
@ -1052,19 +1058,19 @@ or PI_TOO_MANY_PULSES.
NOTES: NOTES:
The serial data is formatted as one start bit, [*bbBits*] data bits, The serial data is formatted as one start bit, [*data_bits*] data bits,
and [*bbStop*]/2 stop bits. and [*stop_bits*]/2 stop bits.
It is legal to add serial data streams with different baud rates to It is legal to add serial data streams with different baud rates to
the same waveform. the same waveform.
[*numBytes*] is the number of bytes of data in str. [*numBytes*] is the number of bytes of data in str.
The bytes required for each character depend upon [*bbBits*]. The bytes required for each character depend upon [*data_bits*].
For [*bbBits*] 1-8 there will be one byte per character. For [*data_bits*] 1-8 there will be one byte per character.
For [*bbBits*] 9-16 there will be two bytes per character. For [*data_bits*] 9-16 there will be two bytes per character.
For [*bbBits*] 17-32 there will be four bytes per character. For [*data_bits*] 17-32 there will be four bytes per character.
D*/ D*/
/*F*/ /*F*/
@ -1369,14 +1375,14 @@ The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID.
D*/ D*/
/*F*/ /*F*/
int bb_serial_read_open(unsigned user_gpio, unsigned bbBaud, unsigned bbBits); int bb_serial_read_open(unsigned user_gpio, unsigned baud, unsigned data_bits);
/*D /*D
This function opens a gpio for bit bang reading of serial data. This function opens a gpio for bit bang reading of serial data.
. . . .
user_gpio: 0-31. user_gpio: 0-31.
bbBaud: 100-250000 baud: 50-250000
bbBits: 1-32 data_bits: 1-32
. . . .
Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_WAVE_BAUD, Returns 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_WAVE_BAUD,
@ -1405,11 +1411,11 @@ Returns the number of bytes copied if OK, otherwise PI_BAD_USER_GPIO
or PI_NOT_SERIAL_GPIO. or PI_NOT_SERIAL_GPIO.
The bytes returned for each character depend upon the number of The bytes returned for each character depend upon the number of
data bits [*bbBits*] specified in the [*bb_serial_read_open*] command. data bits [*data_bits*] specified in the [*bb_serial_read_open*] command.
For [*bbBits*] 1-8 there will be one byte per character. For [*data_bits*] 1-8 there will be one byte per character.
For [*bbBits*] 9-16 there will be two bytes per character. For [*data_bits*] 9-16 there will be two bytes per character.
For [*bbBits*] 17-32 there will be four bytes per character. For [*data_bits*] 17-32 there will be four bytes per character.
D*/ D*/
/*F*/ /*F*/
@ -1431,7 +1437,7 @@ This returns a handle for the device at address i2c_addr on bus i2c_bus.
. . . .
i2c_bus: 0-1. i2c_bus: 0-1.
i2c_addr: 0x08-0x77. i2c_addr: 0x00-0x7F.
i2c_flags: 0. i2c_flags: 0.
. . . .
@ -1453,36 +1459,6 @@ handle: >=0, as returned by a call to [*i2c_open*].
Returns 0 if OK, otherwise PI_BAD_HANDLE. Returns 0 if OK, otherwise PI_BAD_HANDLE.
D*/ D*/
/*F*/
int i2c_read_device(unsigned handle, char *buf, unsigned count);
/*D
This reads count bytes from the raw device into buf.
. .
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.
D*/
/*F*/
int i2c_write_device(unsigned handle, char *buf, unsigned count);
/*D
This writes count bytes from buf to the raw device.
. .
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.
D*/
/*F*/ /*F*/
int i2c_write_quick(unsigned handle, unsigned bit); int i2c_write_quick(unsigned handle, unsigned bit);
/*D /*D
@ -1723,7 +1699,211 @@ PI_I2C_WRITE_FAILED.
D*/ D*/
/*F*/ /*F*/
int spi_open(unsigned spi_channel, unsigned spi_baud, unsigned spi_flags); int i2c_read_device(unsigned handle, char *buf, unsigned count);
/*D
This reads count bytes from the raw device into buf.
. .
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.
D*/
/*F*/
int i2c_write_device(unsigned handle, char *buf, unsigned count);
/*D
This writes count bytes from buf to the raw device.
. .
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.
D*/
/*F*/
int i2c_zip(
unsigned handle,
char *inBuf,
unsigned inLen,
char *outBuf,
unsigned outLen);
/*D
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.
. .
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.
...
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
...
D*/
/*F*/
int bb_i2c_open(unsigned SDA, unsigned SCL, unsigned baud);
/*D
This function selects a pair of gpios for bit banging I2C at a
specified baud rate.
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 gpios
. .
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 gpios 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.
D*/
/*F*/
int bb_i2c_close(unsigned SDA);
/*D
This function stops bit banging I2C on a pair of gpios previously
opened with [*bb_i2c_open*].
. .
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.
D*/
/*F*/
int bb_i2c_zip(
unsigned SDA,
char *inBuf,
unsigned inLen,
char *outBuf,
unsigned outLen);
/*D
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.
. .
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.
...
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
...
D*/
/*F*/
int spi_open(unsigned spi_channel, unsigned baud, unsigned spi_flags);
/*D /*D
This function returns a handle for the SPI device on channel. This function returns a handle for the SPI device on channel.
Data will be transferred at baud bits per second. The flags may Data will be transferred at baud bits per second. The flags may
@ -1736,7 +1916,7 @@ device has 3 chip selects and a selectable word size in bits.
. . . .
spi_channel: 0-1 (0-2 for A+/B+/Pi2 auxiliary device). spi_channel: 0-1 (0-2 for A+/B+/Pi2 auxiliary device).
spi_baud: 32K-125M (values above 30M are unlikely to work). baud: 32K-125M (values above 30M are unlikely to work).
spi_flags: see below. spi_flags: see below.
. . . .
@ -1853,20 +2033,24 @@ PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED.
D*/ D*/
/*F*/ /*F*/
int serial_open(char *ser_tty, unsigned ser_baud, unsigned ser_flags); int serial_open(char *ser_tty, unsigned baud, unsigned ser_flags);
/*D /*D
This function opens a serial device at a specified baud rate This function opens a serial device at a specified baud rate
with specified flags. with specified flags.
. . . .
ser_tty: the serial device to open, /dev/tty*. ser_tty: the serial device to open, /dev/tty*.
ser_baud: the baud rate to use. baud: the baud rate in bits per second, see below.
ser_flags: 0. ser_flags: 0.
. . . .
Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE, or Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE, or
PI_SER_OPEN_FAILED. PI_SER_OPEN_FAILED.
The baud rate must be one of 50, 75, 110, 134, 150,
200, 300, 600, 1200, 1800, 2400, 4800, 9500, 19200,
38400, 57600, 115200, or 230400.
No flags are currently defined. This parameter should be set to zero. No flags are currently defined. This parameter should be set to zero.
D*/ D*/
@ -2088,35 +2272,9 @@ The count of bytes passed to a user customised function.
A pointer to an array of bytes passed to a user customised function. A pointer to an array of bytes passed to a user customised function.
Its meaning and content is defined by the customiser. Its meaning and content is defined by the customiser.
baud::
bbBaud:: The speed of serial communication (I2C, SPI, serial link, waves) in
The baud rate used for the transmission and reception of bit banged bits per second.
serial data.
. .
PI_WAVE_MIN_BAUD 100
PI_WAVE_MAX_BAUD 250000
. .
bbBits::1-32
The number of data bits to be used when adding serial data to a
waveform.
. .
#define PI_MIN_WAVE_DATABITS 1
#define PI_MAX_WAVE_DATABITS 32
. .
bbStop::2-8
The number of (half) stop bits to be used when adding serial data
to a waveform.
. .
#define PI_MIN_WAVE_HALFSTOPBITS 2
#define PI_MAX_WAVE_HALFSTOPBITS 8
. .
bit:: bit::
A value of 0 or 1. A value of 0 or 1.
@ -2165,6 +2323,14 @@ count::
The number of bytes to be transferred in an I2C, SPI, or Serial The number of bytes to be transferred in an I2C, SPI, or Serial
command. command.
data_bits::1-32
The number of data bits in each character of serial data.
. .
#define PI_MIN_WAVE_DATABITS 1
#define PI_MAX_WAVE_DATABITS 32
. .
double:: double::
A floating point number. A floating point number.
@ -2246,8 +2412,8 @@ handle::0-
A number referencing an object opened by one of [*i2c_open*], [*notify_open*], A number referencing an object opened by one of [*i2c_open*], [*notify_open*],
[*serial_open*], and [*spi_open*]. [*serial_open*], and [*spi_open*].
i2c_addr::0x08-0x77 i2c_addr::
The address of a device on the I2C bus (0x08 - 0x77) The address of a device on the I2C bus.
i2c_bus::0-1 i2c_bus::0-1
An I2C bus, 0 or 1. An I2C bus, 0 or 1.
@ -2258,7 +2424,11 @@ Flags which modify an I2C open command. None are currently defined.
i2c_reg:: 0-255 i2c_reg:: 0-255
A register of an I2C device. A register of an I2C device.
*inBuf::
A buffer used to pass data to a function.
inLen::
The number of bytes of data in a buffer.
int:: int::
A whole number, negative or positive. A whole number, negative or positive.
@ -2313,10 +2483,15 @@ offset::
The associated data starts this number of microseconds from the start of The associated data starts this number of microseconds from the start of
the waveform. the waveform.
*outBuf::
A buffer used to return data from a function.
outLen::
The size in bytes of an output buffer.
*param:: *param::
An array of script parameters. An array of script parameters.
*portStr:: *portStr::
A string specifying the port address used by the Pi running A string specifying the port address used by the Pi running
the pigpio daemon. It may be NULL in which case "8888" the pigpio daemon. It may be NULL in which case "8888"
@ -2384,22 +2559,21 @@ The maximum number of bytes a user customised function should return.
*rxBuf:: *rxBuf::
A pointer to a buffer to receive data. A pointer to a buffer to receive data.
SCL::
The user gpio to use for the clock when bit banging I2C.
*script:: *script::
A pointer to the text of a script. A pointer to the text of a script.
script_id:: script_id::
An id of a stored script as returned by [*store_script*]. An id of a stored script as returned by [*store_script*].
SDA::
The user gpio to use for data when bit banging I2C.
seconds:: seconds::
The number of seconds. The number of seconds.
ser_baud::
The baud rate to use on the serial link.
It must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
4800, 9600, 19200, 38400, 57600, 115200, 230400.
ser_flags:: ser_flags::
Flags which modify a serial open command. None are currently defined. Flags which modify a serial open command. None are currently defined.
@ -2409,15 +2583,21 @@ The name of a serial tty device, e.g. /dev/ttyAMA0, /dev/ttyUSB0, /dev/tty1.
size_t:: size_t::
A standard type used to indicate the size of an object in bytes. A standard type used to indicate the size of an object in bytes.
spi_baud::
The speed in bits per second to use for the SPI device.
spi_channel:: spi_channel::
A SPI channel, 0-2. A SPI channel, 0-2.
spi_flags:: spi_flags::
See [*spi_open*]. See [*spi_open*].
stop_bits::2-8
The number of (half) stop bits to be used when adding serial data
to a waveform.
. .
#define PI_MIN_WAVE_HALFSTOPBITS 2
#define PI_MAX_WAVE_HALFSTOPBITS 8
. .
*str:: *str::
An array of characters. An array of characters.

256
pigs.1
View File

@ -251,6 +251,135 @@ ERROR: no permission to update one or more gpios
.br .br
.IP "\fBBI2CC sda\fP - Close bit bang I2C"
.IP "" 4
This command signals that bit banging I2C on \fBsda\fP (and \fBscl\fP) is no
longer required.
.br
\fBExample\fP
.br
.EX
$ pigs bi2cc 5
.br
.EE
.br
.IP "\fBBI2CO sda scl b\fP - Open bit bang I2C"
.IP "" 4
This command signals that gpios \fBsda\fP and \fBscl\fP are to be used
for bit banging I2C at \fBb\fP baud.
.br
Bit banging I2C allows for certain operations which are not possible
with the standard I2C driver.
.br
o baud rates as low as 50
.br
o repeated starts
.br
o clock stretching
.br
o I2C on any pair of spare gpios
.br
The baud rate may be between 50 and 500000 bits per second.
.br
The gpios 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.
.br
.IP "\fBBI2CZ sda bvs\fP - I2C bit bang multiple transactions"
.IP "" 4
This function executes a sequence of bit banged I2C operations. The
operations to be performed are specified by the contents of \fBbvs\fP
which contains the concatenated command codes and associated data.
.br
The following command codes are supported:
.br
.EX
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
.EE
.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
The address and flags default to 0. The address and flags maintain
their previous value until updated.
.br
No flags are currently defined.
.br
\fBExample\fP
.br
.EX
Set address 0x53
.br
start, write 0x32, (re)start, read 6 bytes, stop
.br
Set address 0x1E
.br
start, write 0x03, (re)start, read 6 bytes, stop
.br
Set address 0x68
.br
start, write 0x1B, (re)start, read 8 bytes, stop
.br
End
.br
.br
0x04 0x53
.br
0x02 0x07 0x01 0x32 0x02 0x06 0x06 0x03
.br
.br
0x04 0x1E
.br
0x02 0x07 0x01 0x03 0x02 0x06 0x06 0x03
.br
.br
0x04 0x68
.br
0x02 0x07 0x01 0x1B 0x02 0x06 0x08 0x03
.br
.br
0x00
.br
.EE
.br
.IP "\fBBR1 \fP - Read bank 1 gpios" .IP "\fBBR1 \fP - Read bank 1 gpios"
.IP "" 4 .IP "" 4
This command read gpios 0-31 (bank 1) and returns the levels as a This command read gpios 0-31 (bank 1) and returns the levels as a
@ -1139,6 +1268,72 @@ $ pigs i2cww 0 0 0xffff
.br .br
.IP "\fBI2CZ h bvs\fP - Performs multiple I2C transactions"
.IP "" 4
This command executes a sequence of I2C operations. The
operations to be performed are specified by the contents of \fBbvs\fP
which contains the concatenated command codes and associated data.
.br
The following command codes are supported:
.br
.EX
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
.EE
.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
The address defaults to that associated with the handle \fBh\fP.
The flags default to 0. The address and flags maintain their
previous value until updated.
.br
\fBExample\fP
.br
.EX
Set address 0x53, write 0x32, read 6 bytes
.br
Set address 0x1E, write 0x03, read 6 bytes
.br
Set address 0x68, write 0x1B, read 8 bytes
.br
End
.br
.br
0x04 0x53 0x07 0x01 0x32 0x06 0x06
.br
0x04 0x1E 0x07 0x01 0x03 0x06 0x06
.br
0x04 0x68 0x07 0x01 0x1B 0x06 0x08
.br
0x00
.br
.EE
.br
.br
.IP "\fBM/MODES g m\fP - Set gpio mode" .IP "\fBM/MODES g m\fP - Set gpio mode"
.IP "" 4 .IP "" 4
@ -2149,7 +2344,7 @@ $ pigs serda 0
.br .br
.IP "\fBSERO dev b sef\fP - Open serial device dev at baud with flags" .IP "\fBSERO dev b sef\fP - Open serial device dev at baud b with flags"
.IP "" 4 .IP "" 4
.br .br
@ -2166,6 +2361,11 @@ will be returned.
The UART is /dev/ttyAMA0. The consoles are /dev/ttyx. The USB The UART is /dev/ttyAMA0. The consoles are /dev/ttyx. The USB
devices are /dev/ttyUSBx. devices are /dev/ttyUSBx.
.br
The baud rate must be one of 50, 75, 110, 134, 150,
200, 300, 600, 1200, 1800, 2400, 4800, 9500, 19200,
38400, 57600, 115200, or 230400.
.br .br
\fBExample\fP \fBExample\fP
@ -2381,6 +2581,9 @@ at \fBb\fP baud and \fBdb\fP data bits.
Upon success nothing is returned. On error a negative status code Upon success nothing is returned. On error a negative status code
will be returned. will be returned.
.br
The baud rate may be between 50 and 250000 bits per second.
.br .br
The received data is held in a cyclic buffer. The received data is held in a cyclic buffer.
@ -2422,7 +2625,26 @@ will be returned.
.br .br
.IP "\fBSPIO c b spf\fP - SPI open channel at baud with flags" \fBExample\fP
.br
.EX
$ pigs spic 1
.br
.br
$ pigs spic 1
.br
-25
.br
ERROR: unknown handle
.br
.EE
.br
.IP "\fBSPIO c b spf\fP - SPI open channel at baud b with flags"
.IP "" 4 .IP "" 4
.br .br
@ -2433,6 +2655,10 @@ Data will be transferred at \fBb\fP bits per second. The flags \fBspf\fP
may be used to modify the default behaviour of 4-wire operation, may be used to modify the default behaviour of 4-wire operation,
mode 0, active low chip select. mode 0, active low chip select.
.br
Speeds between 32kbps and 125Mbps are allowed. Speeds above 30Mbps
are unlikely to work.
.br .br
An auxiliary SPI device is available on the A+/B+/Pi2 and may be An auxiliary SPI device is available on the A+/B+/Pi2 and may be
selected by setting the A bit in the flags. The auxiliary selected by setting the A bit in the flags. The auxiliary
@ -2524,6 +2750,12 @@ $ pigs spio 0 100000 3 # Open channel 0 at 100kbps in mode 3.
0 0
.br .br
.br
$ pigs spio 0 32000 256 # Open channel 0 of auxiliary spi at 32kbps.
.br
1
.br
.EE .EE
.br .br
@ -2810,6 +3042,9 @@ returned. On error a negative status code will be returned.
The serial data is formatted as one start bit, \fBdb\fP data bits, and The serial data is formatted as one start bit, \fBdb\fP data bits, and
\fBsb\fP/2 stop bits. \fBsb\fP/2 stop bits.
.br
The baud rate may be between 50 and 1000000 bits per second.
.br .br
It is legal to add serial data streams with different baud rates to It is legal to add serial data streams with different baud rates to
the same waveform. the same waveform.
@ -3285,8 +3520,9 @@ ERROR: non existent wave id
.br .br
.IP "\fBb\fP - baud (100-250000)" 0 .IP "\fBb\fP - baud" 0
The command expects a baud rate for serial data. The command expects the baud rate in bits per second for
the transmission of serial data (I2C/SPI/serial link, waves).
.br .br
@ -3519,6 +3755,18 @@ The command expects the number of stop (half) bits per serial character.
.br .br
.IP "\fBscl\fP - user gpio (0-31)" 0
The command expects the number of the gpio to be used for SCL
when bit banging I2C.
.br
.IP "\fBsda\fP - user gpio (0-31)" 0
The command expects the number of the gpio to be used for SDA
when bit banging I2C.
.br
.IP "\fBsef\fP - serial flags (32 bits)" 0 .IP "\fBsef\fP - serial flags (32 bits)" 0
The command expects a flag value. No serial flags are currently defined. The command expects a flag value. No serial flags are currently defined.

7
pigs.c
View File

@ -26,7 +26,7 @@ For more information, please refer to <http://unlicense.org/>
*/ */
/* /*
This version is for pigpio version 32+ This version is for pigpio version 33+
*/ */
#include <stdio.h> #include <stdio.h>
@ -141,7 +141,7 @@ void print_result(int sock, int rv, cmdCmd_t cmd)
printf(cmdUsage); printf(cmdUsage);
break; break;
case 6: /* BI2CX CF2 I2CPK I2CRD I2CRI I2CRK SERR SLR SPIX SPIR */ case 6: /* BI2CZ CF2 I2CPK I2CRD I2CRI I2CRK I2CZ SERR SLR SPIX SPIR */
printf("%d", r); printf("%d", r);
if (r < 0) fatal("ERROR: %s", cmdErrStr(r)); if (r < 0) fatal("ERROR: %s", cmdErrStr(r));
if (r > 0) if (r > 0)
@ -178,12 +178,13 @@ void get_extensions(int sock, int command, int res)
{ {
switch (command) switch (command)
{ {
case PI_CMD_BI2CX: case PI_CMD_BI2CZ:
case PI_CMD_CF2: case PI_CMD_CF2:
case PI_CMD_I2CPK: case PI_CMD_I2CPK:
case PI_CMD_I2CRD: case PI_CMD_I2CRD:
case PI_CMD_I2CRI: case PI_CMD_I2CRI:
case PI_CMD_I2CRK: case PI_CMD_I2CRK:
case PI_CMD_I2CZ:
case PI_CMD_PROCP: case PI_CMD_PROCP:
case PI_CMD_SERR: case PI_CMD_SERR:
case PI_CMD_SLR: case PI_CMD_SLR:

View File

@ -3,7 +3,7 @@
from distutils.core import setup from distutils.core import setup
setup(name='pigpio', setup(name='pigpio',
version='1.17', version='1.18',
author='joan', author='joan',
author_email='joan@abyz.co.uk', author_email='joan@abyz.co.uk',
maintainer='joan', maintainer='joan',

2
x_pigs
View File

@ -49,7 +49,7 @@ s=$(pigs bs2 0)
if [[ $s = "" ]]; then echo "BS2 ok"; else echo "BS2 fail ($s)"; fi if [[ $s = "" ]]; then echo "BS2 ok"; else echo "BS2 fail ($s)"; fi
s=$(pigs h) s=$(pigs h)
if [[ ${#s} = 5193 ]]; then echo "HELP ok"; else echo "HELP fail (${#s})"; fi if [[ ${#s} = 4220 ]]; then echo "HELP ok"; else echo "HELP fail (${#s})"; fi
s=$(pigs hwver) s=$(pigs hwver)
if [[ $s -ne 0 ]]; then echo "HWVER ok"; else echo "HWVER fail ($s)"; fi if [[ $s -ne 0 ]]; then echo "HWVER ok"; else echo "HWVER fail ($s)"; fi

6
x_pipe
View File

@ -56,14 +56,16 @@ if [[ $s = 0 ]]; then echo "BS2 ok"; else echo "BS2 fail ($s)"; fi
echo "h" >/dev/pigpio echo "h" >/dev/pigpio
read -t 1 s </dev/pigout read -t 1 s </dev/pigout
if [[ $s = "BC1 bits Clear specified gpios in bank 1" ]] read -t 1 s </dev/pigout
if [[ $s = "BC1 bits Clear gpios in bank 1" ]]
then echo "HELP-a ok" then echo "HELP-a ok"
else echo "HELP-a fail ($s)" else echo "HELP-a fail ($s)"
fi fi
read -t 1 -N 9000 </dev/pigout # dump rest of help read -t 1 -N 9000 </dev/pigout # dump rest of help
echo "help" >/dev/pigpio echo "help" >/dev/pigpio
read -t 1 s </dev/pigout read -t 1 s </dev/pigout
if [[ $s = "BC1 bits Clear specified gpios in bank 1" ]] read -t 1 s </dev/pigout
if [[ $s = "BC1 bits Clear gpios in bank 1" ]]
then echo "HELP-b ok" then echo "HELP-b ok"
else echo "HELP-b fail ($s)" else echo "HELP-b fail ($s)"
fi fi