diff --git a/command.c b/command.c index baa41eb..c33f438 100644 --- a/command.c +++ b/command.c @@ -26,7 +26,7 @@ For more information, please refer to */ /* -This version is for pigpio version 32+ +This version is for pigpio version 33+ */ #include @@ -47,7 +47,7 @@ cmdInfo_t cmdInfo[]= {PI_CMD_BI2CC, "BI2CC", 112, 0}, // bbI2CClose {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_BR2, "BR2", 101, 3}, // gpioRead_Bits_32_53 @@ -90,7 +90,7 @@ cmdInfo_t cmdInfo[]= {PI_CMD_I2CWS, "I2CWS", 121, 0}, // i2cWriteByte {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_MILS, "MILS", 112, 0}, // gpioDelay @@ -225,115 +225,98 @@ cmdInfo_t cmdInfo[]= }; -char * cmdUsage = "\ -BC1 bits Clear specified gpios in bank 1\n\ -BC2 bits Clear specified gpios in bank 2\n\ +char * cmdUsage = "\n\ +BC1 bits Clear gpios in bank 1\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\ BR2 Read bank 2 gpios\n\ -BS1 bits Set specified gpios in bank 2\n\ -BS2 bits Set specified gpios in bank 2\n\ +BS1 bits Set gpios in bank 2\n\ +BS2 bits Set gpios in bank 2\n\ \n\ -CF1 uvs Custom function 1\n\ -CF2 uvs Custom function 2\n\ +CF1 ... Custom function 1\n\ +CF2 ... Custom function 2\n\ \n\ -GDC u Get PWM dutycycle for gpio\n\ -GPW u Get servo pulsewidth for gpio\n\ +GDC g Get PWM dutycycle for gpio\n\ +GPW g Get servo pulsewidth for gpio\n\ \n\ H/HELP Display command help\n\ -\n\ -HC g cf Set hardware clock frequency\n\ -HP g pf pdc Set hardware PWM frequency and dutycycle\n\ -\n\ +HC g f Set hardware clock frequency\n\ +HP g f dc Set hardware PWM frequency and dutycycle\n\ HWVER Get hardware version\n\ \n\ I2CC h Close I2C handle\n\ -I2CO ib id if Open I2C bus and device with flags\n\ -\n\ -I2CPC h r wv SMBus Process Call: exchange register with word\n\ -I2CPK h r bvs SMBus Block Process Call: exchange data bytes with register\n\ -\n\ +I2CO bus device flags | Open I2C bus and device with flags\n\ +I2CPC h r word SMBus Process Call: exchange register with word\n\ +I2CPK h r ... SMBus Block Process Call: exchange data bytes with register\n\ I2CRB h r SMBus Read Byte Data: read byte from register\n\ -I2CRD h num i2c Read bytes\n\ -I2CRI h r num SMBus Read I2C Block Data: read bytes from register\n\ +I2CRD h n I2C Read bytes\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\ I2CRS h SMBus Read Byte: read byte\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\ -I2CWB h r bv SMBus Write Byte Data: write byte to register\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\ +M/MODES g mode Set gpio mode\n\ MG/MODEG g Get gpio mode\n\ -\n\ -MICS v Delay for microseconds\n\ -MILS v Delay for milliseconds\n\ +MICS n Delay for microseconds\n\ +MILS n Delay for milliseconds\n\ \n\ NB h bits Start notification\n\ NC h Close notification\n\ NO Request a notification\n\ NP h Pause notification\n\ \n\ -P/PWM u v Set gpio PWM value\n\ -\n\ -PARSE t Validate script\n\ -\n\ -PFG u Get gpio PWM frequency\n\ -PFS u v Set gpio PWM frequency\n\ -\n\ +P/PWM g v Set gpio PWM value\n\ +PARSE text Validate script\n\ +PFG g Get gpio PWM frequency\n\ +PFS g v Set gpio PWM frequency\n\ PIGPV Get pigpio library version\n\ -\n\ -PRG u Get gpio PWM range\n\ -\n\ -PROC t Store script\n\ +PRG g Get gpio PWM range\n\ +PROC text Store script\n\ PROCD sid Delete script\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\ -\n\ -PRRG u Get gpio PWM real range\n\ -PRS u v Set gpio PWM range\n\ -\n\ -PUD g p Set gpio pull up/down\n\ +PRRG g Get gpio PWM real range\n\ +PRS g v Set gpio PWM range\n\ +PUD g pud Set gpio pull up/down\n\ \n\ R/READ g Read gpio level\n\ \n\ -S/SERVO u v Set gpio servo pulsewidth\n\ -\n\ +S/SERVO g v Set gpio servo pulsewidth\n\ SERC h Close serial handle\n\ SERDA h Check for serial data ready to read\n\ -SERO srd srb srf Open serial device at baud with flags\n\ -\n\ -SERR h num Read bytes from serial handle\n\ +SERO text baud flags | Open serial device at baud with flags\n\ +SERR h n Read bytes from serial handle\n\ SERRB h Read byte from serial handle\n\ -SERW h bvs Write bytes to serial handle\n\ -SERWB h bv Write byte to serial handle\n\ -\n\ -SLR u num Read bit bang serial data from gpio\n\ -SLRC u Close gpio for bit bang serial data\n\ -SLRO u b db Open gpio for bit bang serial data\n\ -\n\ +SERW h ... Write bytes to serial handle\n\ +SERWB h byte Write byte to serial handle\n\ +SLR g v Read bit bang serial data from gpio\n\ +SLRC g Close gpio for bit bang serial data\n\ +SLRO g baud bitlen | Open gpio for bit bang serial data\n\ SPIC h SPI close handle\n\ -SPIO sc sb sf SPI open channel at baud with flags\n\ -SPIR h num SPI read bytes from handle\n\ -SPIW h bvs SPI write bytes to handle\n\ -SPIX h bvs SPI transfer bytes to handle\n\ +SPIO channel baud flags | SPI open channel at baud with flags\n\ +SPIR h v SPI read bytes from handle\n\ +SPIW h ... SPI write bytes to handle\n\ +SPIX h ... SPI transfer bytes to handle\n\ \n\ T/TICK Get current tick\n\ +TRIG g micros l Trigger level for micros on gpio\n\ \n\ -TRIG u pl L Trigger level for micros on gpio\n\ -\n\ -W/WRITE g L Write level to gpio\n\ -\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\ +W/WRITE g l Write level to gpio\n\ +WDOG g millis Set millisecond watchdog on gpio\n\ +WVAG triplets Wave add generic pulses\n\ +WVAS g baud bitlen stopbits offset ... | Wave add serial data\n\ WVBSY Check if wave busy\n\ WVCLR Wave clear\n\ WVCRE Create wave from added pulses\n\ @@ -342,52 +325,17 @@ WVGO Wave transmit (DEPRECATED)\n\ WVGOR Wave transmit repeatedly (DEPRECATED)\n\ WVHLT Wave stop\n\ WVNEW Start a new empty wave\n\ -WVSC ws Wave get DMA control block stats\n\ -WVSM ws Wave get micros stats\n\ -WVSP ws Wave get pulses stats\n\ +WVSC 0,1,2 Wave get DMA control block stats\n\ +WVSM 0,1,2 Wave get micros stats\n\ +WVSP 0,1,2 Wave get pulses stats\n\ WVTX wid Transmit wave as one-shot\n\ WVTXR wid Transmit wave repeatedly\n\ \n\ -bits = a mask where (1<=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\ 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 { @@ -500,11 +448,14 @@ static errInfo_t errInfo[]= {PI_BAD_DATABITS , "serial data bits not 1-32"}, {PI_MSG_TOOBIG , "socket/pipe message too big"}, {PI_BAD_MALLOC_MODE , "bad memory allocation mode"}, - {PI_TOO_MANY_PARTS , "too many I2C transaction parts"}, - {PI_BAD_I2C_PART , "a combined I2C transaction failed"}, + {PI_TOO_MANY_SEGS , "too many I2C transaction segments"}, + {PI_BAD_I2C_SEG , "an I2C transaction segment failed"}, {PI_BAD_SMBUS_CMD , "SMBus command not supported by driver"}, {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"; @@ -893,7 +844,7 @@ int cmdParse( 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. */ diff --git a/pigpio.3 b/pigpio.3 index 38180c5..bc5f550 100644 --- a/pigpio.3 +++ b/pigpio.3 @@ -1570,7 +1570,7 @@ else .EE -.IP "\fBint gpioWaveAddSerial(unsigned user_gpio, unsigned bbBaud, unsigned bbBits, unsigned bbStop, unsigned offset, unsigned numBytes, char *str)\fP" +.IP "\fBint gpioWaveAddSerial(unsigned user_gpio, unsigned baud, unsigned data_bits, unsigned stop_bits, unsigned offset, unsigned numBytes, char *str)\fP" .IP "" 4 This function adds a waveform representing serial data to the existing waveform (if any). The serial data starts offset @@ -1583,11 +1583,11 @@ microseconds from the start of the waveform. .EX user_gpio: 0-31 .br - bbBaud: 100-1000000 + baud: 50-1000000 .br - bbBits: 1-32 +data_bits: 1-32 .br - bbStop: 2-8 +stop_bits: 2-8 .br offset: 0- .br @@ -1614,8 +1614,8 @@ NOTES: .br .br -The serial data is formatted as one start bit, bbBits data bits, and -bbStop/2 stop bits. +The serial data is formatted as one start bit, data_bits data bits, and +stop_bits/2 stop bits. .br @@ -1631,16 +1631,16 @@ numBytes is the number of bytes of data in str. .br .br -The bytes required for each character depend upon bbBits. +The bytes required for each character depend upon data_bits. .br .br -For bbBits 1-8 there will be one byte per character. +For data_bits 1-8 there will be one byte per character. .br -For bbBits 9-16 there will be two bytes per character. +For data_bits 9-16 there will be two bytes per character. .br -For bbBits 17-32 there will be four bytes per character. +For data_bits 17-32 there will be four bytes per character. .br @@ -1938,7 +1938,7 @@ waveform created since \fBgpioInitialise\fP was called. This function returns the maximum possible size of a waveform in DMA control blocks. -.IP "\fBint gpioSerialReadOpen(unsigned user_gpio, unsigned bbBaud, unsigned bbBits)\fP" +.IP "\fBint gpioSerialReadOpen(unsigned user_gpio, unsigned baud, unsigned data_bits)\fP" .IP "" 4 This function opens a gpio for bit bang reading of serial data. @@ -1949,9 +1949,9 @@ This function opens a gpio for bit bang reading of serial data. .EX user_gpio: 0-31 .br - bbBaud: 100-250000 + baud: 50-250000 .br - bbBits: 1-32 +data_bits: 1-32 .br .EE @@ -2003,16 +2003,16 @@ or PI_NOT_SERIAL_GPIO. .br The bytes returned for each character depend upon the number of -data bits \fBbbBits\fP specified in the \fBgpioSerialReadOpen\fP command. +data bits \fBdata_bits\fP specified in the \fBgpioSerialReadOpen\fP command. .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 -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 -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 gpioSerialReadClose(unsigned user_gpio)\fP" .IP "" 4 @@ -2044,7 +2044,7 @@ This returns a handle for the device at the address on the I2C bus. .EX i2cBus: 0-1 .br - i2cAddr: 0x08-0x77 + i2cAddr: 0x00-0x7F .br i2cFlags: 0 .br @@ -2117,54 +2117,6 @@ handle: >=0, as returned by a call to \fBi2cOpen\fP .br Returns 0 if OK, otherwise PI_BAD_HANDLE. -.IP "\fBint i2cReadDevice(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 \fBi2cOpen\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 i2cWriteDevice(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 \fBi2cOpen\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 i2cWriteQuick(unsigned handle, unsigned bit)\fP" .IP "" 4 This sends a single bit (in the Rd/Wr bit) to the device associated @@ -2646,7 +2598,430 @@ S Addr Wr [A] Comm [A] Data [A] Data [A] ... [A] Data [A] P .EE -.IP "\fBint spiOpen(unsigned spiChan, unsigned spiBaud, unsigned spiFlags)\fP" +.IP "\fBint i2cReadDevice(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 \fBi2cOpen\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 i2cWriteDevice(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 \fBi2cOpen\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 "\fBvoid i2cSwitchCombined(int setting)\fP" +.IP "" 4 +This sets the I2C (i2c-bcm2708) module "use combined transactions" +parameter on or off. + +.br + +.br + +.EX +setting: 0 to set the parameter off, non-zero to set it on +.br + +.EE + +.br + +.br + +.br + +.br +NOTE: when the flag is on a write followed by a read to the same +slave address will use a repeated start (rather than a stop/start). + +.IP "\fBint i2cSegments(unsigned handle, pi_i2c_msg_t *segs, unsigned numSegs)\fP" +.IP "" 4 +This function executes multiple I2C segments in one transaction by +calling the I2C_RDWR ioctl. + +.br + +.br + +.EX + handle: >=0, as returned by a call to \fBi2cOpen\fP +.br + segs: an array of I2C segments +.br +numSegs: >0, the number of I2C segments +.br + +.EE + +.br + +.br +Returns the number of segments if OK, otherwise PI_BAD_I2C_SEG. + +.IP "\fBint i2cZip(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 + +.IP "\fBint bbI2COpen(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 bbI2CClose(unsigned SDA)\fP" +.IP "" 4 +This function stops bit banging I2C on a pair of gpios previously +opened with \fBbbI2COpen\fP. + +.br + +.br + +.EX +SDA: 0-31, the SDA gpio used in a prior call to \fBbbI2COpen\fP +.br + +.EE + +.br + +.br +Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_I2C_GPIO. + +.IP "\fBint bbI2CZip(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 \fBbbI2COpen\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 spiOpen(unsigned spiChan, unsigned baud, unsigned spiFlags)\fP" .IP "" 4 This function returns a handle for the SPI device on the channel. Data will be transferred at baud bits per second. The flags may @@ -2667,7 +3042,7 @@ device has 3 chip selects and a selectable word size in bits. .EX spiChan: 0-1 (0-2 for A+/B+/Pi2 auxiliary device) .br - spiBaud: 32K-125M (values above 30M are unlikely to work) + baud: 32K-125M (values above 30M are unlikely to work) .br spiFlags: see below .br @@ -2876,7 +3251,7 @@ handle: >=0, as returned by a call to \fBspiOpen\fP Returns the number of bytes transferred if OK, otherwise PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED. -.IP "\fBint serOpen(char *sertty, unsigned serBaud, unsigned serFlags)\fP" +.IP "\fBint serOpen(char *sertty, unsigned baud, unsigned serFlags)\fP" .IP "" 4 This function opens a serial device at a specified baud rate with specified flags. @@ -2888,7 +3263,7 @@ with specified flags. .EX sertty: the serial device to open, /dev/tty* .br - serBaud: the baud rate to use + baud: the baud rate in bits per second, see below .br serFlags: 0 .br @@ -2903,6 +3278,13 @@ PI_SER_OPEN_FAILED. .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 No flags are currently defined. This parameter should be set to zero. @@ -4916,10 +5298,6 @@ meaning is defined by the customiser. .br .IP "\fBargc\fP" 0 - -.br - -.br The count of bytes passed to a user customised function. .br @@ -4927,10 +5305,6 @@ The count of bytes passed to a user customised function. .br .IP "\fB*argx\fP" 0 - -.br - -.br A pointer to an array of bytes passed to a user customised function. Its meaning and content is defined by the customiser. @@ -4938,85 +5312,15 @@ Its meaning and content is defined by the customiser. .br -.IP "\fBbbBaud\fP" 0 - -.br - -.br -The baud rate used for the transmission and reception of bit banged -serial data. - -.br - -.br - -.EX -PI_BB_MIN_BAUD 50 -.br -PI_BB_RX_MAX_BAUD 250000 -.br -PI_BB_TX_MAX_BAUD 1000000 -.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 +.IP "\fBbaud\fP" 0 +The speed of serial communication (I2C, SPI, serial link, waves) in +bits per second. .br .br .IP "\fBbit\fP" 0 - -.br - -.br A value of 0 or 1. .br @@ -5024,10 +5328,6 @@ A value of 0 or 1. .br .IP "\fBbitPos\fP" 0 - -.br - -.br A bit position within a byte or word. The least significant bit is position 0. @@ -5036,10 +5336,6 @@ position 0. .br .IP "\fBbits\fP" 0 - -.br - -.br A value used to select gpios. If bit n of bits is set then gpio n is selected. @@ -5224,6 +5520,30 @@ command. .br +.IP "\fBdata_bits\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 "\fBDMAchannel\fP: 0-14" 0 .EX @@ -5587,12 +5907,8 @@ A number referencing an object opened by one of .br -.IP "\fBi2cAddr\fP: 0x08-0x77" 0 - -.br - -.br -The address of a device on the I2C bus (0x08 - 0x77) +.IP "\fBi2cAddr\fP" 0 +The address of a device on the I2C bus. .br @@ -5645,11 +5961,21 @@ PI_DISABLE_SOCK_IF 2 .br +.IP "\fB*inBuf\fP" 0 +A buffer used to pass data to a function. + +.br + +.br + +.IP "\fBinLen\fP" 0 +The number of bytes of data in a buffer. + +.br + +.br + .IP "\fBint\fP" 0 - -.br - -.br A whole number, negative or positive. .br @@ -5657,10 +5983,6 @@ A whole number, negative or positive. .br .IP "\fBlevel\fP" 0 - -.br - -.br The level of a gpio. Low or High. .br @@ -5842,13 +6164,6 @@ The number of parameters passed to a script. .br -.IP "\fBnumParts\fP" 0 -The number of parts in a combined I2C transaction. - -.br - -.br - .IP "\fBnumPulses\fP" 0 The number of pulses to be added to a waveform. @@ -5856,6 +6171,13 @@ The number of pulses to be added to a waveform. .br +.IP "\fBnumSegs\fP" 0 +The number of segments in a combined I2C transaction. + +.br + +.br + .IP "\fBoffset\fP" 0 The associated data starts this number of microseconds from the start of tghe waveform. @@ -5864,6 +6186,20 @@ tghe waveform. .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 An array of script parameters. @@ -5871,13 +6207,6 @@ An array of script parameters. .br -.IP "\fB*parts\fP" 0 -An array of the part transactions which make up a combined I2C transaction. - -.br - -.br - .IP "\fBpi_i2c_msg_t\fP" 0 .EX @@ -6199,6 +6528,17 @@ A pointer to a buffer to receive data. .br +.IP "\fBSCL\fP" 0 + +.br + +.br +The user gpio to use for the clock when bit banging I2C. + +.br + +.br + .IP "\fB*script\fP" 0 .br @@ -6221,6 +6561,17 @@ An id of a stored script as returned by \fBgpioStoreScript\fP. .br +.IP "\fBSDA\fP" 0 + +.br + +.br +The user gpio to use for data when bit banging I2C. + +.br + +.br + .IP "\fBsecondaryChannel\fP: 0-6" 0 .br @@ -6255,28 +6606,18 @@ The number of seconds. .br -.IP "\fBserBaud\fP" 0 +.IP "\fB*segs\fP" 0 .br .br -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. +An array of segments which make up a combined I2C transaction. .br .br .IP "\fBserFlags\fP" 0 - -.br - -.br Flags which modify a serial open command. None are currently defined. .br @@ -6284,16 +6625,19 @@ Flags which modify a serial open command. None are currently defined. .br .IP "\fB*sertty\fP" 0 - -.br - -.br The name of a serial tty device, e.g. /dev/ttyAMA0, /dev/ttyUSB0, /dev/tty1. .br .br +.IP "\fBsetting\fP" 0 +A value used to set a flag, 0 for false, non-zero for true. + +.br + +.br + .IP "\fBsignum\fP: 0-63" 0 .EX @@ -6320,32 +6664,13 @@ A standard type used to indicate the size of an object in bytes. .br .IP "\fB*spi\fP" 0 - -.br - -.br A pointer to a \fBrawSPI_t\fP structure. .br .br -.IP "\fBspiBaud\fP" 0 - -.br - -.br -The speed in bits per second to use for the SPI device. - -.br - -.br - .IP "\fBspiBitFirst\fP" 0 - -.br - -.br Gpio reads are made from spiBitFirst to spiBitLast. .br @@ -6364,10 +6689,6 @@ Gpio reads are made from spiBitFirst to spiBitLast. .br .IP "\fBspiBits\fP" 0 - -.br - -.br The number of bits to transfer in a raw SPI transaction. .br @@ -6375,10 +6696,6 @@ The number of bits to transfer in a raw SPI transaction. .br .IP "\fBspiChan\fP" 0 - -.br - -.br A SPI channel, 0-2. .br @@ -6386,10 +6703,6 @@ A SPI channel, 0-2. .br .IP "\fBspiFlags\fP" 0 - -.br - -.br See \fBspiOpen\fP. .br @@ -6397,10 +6710,6 @@ See \fBspiOpen\fP. .br .IP "\fBspiSS\fP" 0 - -.br - -.br The SPI slave select gpio in a raw SPI transaction. .br @@ -6408,21 +6717,33 @@ The SPI slave select gpio in a raw SPI transaction. .br .IP "\fBspiTxBits\fP" 0 - -.br - -.br The number of bits to transfer dring a raw SPI transaction .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 - -.br - -.br An array of characters. .br @@ -6430,10 +6751,6 @@ An array of characters. .br .IP "\fBtimeout\fP" 0 - -.br - -.br A gpio watchdog timeout in milliseconds. .EX @@ -6820,11 +7137,11 @@ A 16-bit word value. .br #define PI_CMD_BI2CO 90 .br -#define PI_CMD_BI2CX 91 +#define PI_CMD_BI2CZ 91 .br .br -#define PI_CMD_I2CX 92 +#define PI_CMD_I2CZ 92 .br .br @@ -7051,19 +7368,21 @@ A 16-bit word value. .br #define PI_BAD_MALLOC_MODE -104 // bad memory allocation mode .br -#define PI_TOO_MANY_PARTS -105 // too many I2C transaction parts +#define PI_TOO_MANY_SEGS -105 // too many I2C transaction segments .br -#define PI_BAD_I2C_PART -106 // a combined I2C transaction failed +#define PI_BAD_I2C_SEG -106 // an I2C transaction segment failed .br #define PI_BAD_SMBUS_CMD -107 // SMBus command not supported by driver .br #define PI_NOT_I2C_GPIO -108 // no bit bang I2C in progress on gpio .br -#define PI_BAD_BB_WLEN -109 // bad BB write length +#define PI_BAD_I2C_WLEN -109 // bad I2C write length .br -#define PI_BAD_BB_RLEN -110 // bad BB read length +#define PI_BAD_I2C_RLEN -110 // bad I2C read length .br -#define PI_BAD_BB_CMD -111 // bad BB command +#define PI_BAD_I2C_CMD -111 // bad I2C command +.br +#define PI_BAD_I2C_BAUD -112 // bad I2C baud rate, not 50-500k .br .br diff --git a/pigpio.c b/pigpio.c index b0fcaae..5b127e5 100644 --- a/pigpio.c +++ b/pigpio.c @@ -25,7 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to */ -/* pigpio version 32 */ +/* pigpio version 33 */ /* include ------------------------------------------------------- */ @@ -908,6 +908,7 @@ typedef struct { uint16_t state; int16_t fd; + uint32_t addr; uint32_t flags; uint32_t funcs; } i2cInfo_t; @@ -1000,7 +1001,7 @@ typedef struct { int mode; int gpio; - uint32_t baud; /* 50-250000 */ + uint32_t baud; union { 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) { 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]); break; - case PI_CMD_BI2CX: + case PI_CMD_BI2CZ: /* use half buffer for write, half buffer for read */ 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) { 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]); 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: @@ -3470,8 +3506,8 @@ int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags) if (i2cBus >= PI_NUM_I2C_BUS) SOFT_ERROR(PI_BAD_I2C_BUS, "bad I2C bus (%d)", i2cBus); -// if ((i2cAddr < 0x08) || (i2cAddr > 0x77)) -// SOFT_ERROR(PI_BAD_I2C_ADDR, "bad I2C address (0x%X)", i2cAddr); + if (i2cAddr > PI_MAX_I2C_ADDR) + SOFT_ERROR(PI_BAD_I2C_ADDR, "bad I2C address (%d)", i2cAddr); if (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].addr = i2cAddr; i2cInfo[slot].flags = i2cFlags; i2cInfo[slot].funcs = funcs; @@ -3548,7 +3585,23 @@ int i2cClose(unsigned handle) 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; 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) SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle); - if (parts == NULL) - SOFT_ERROR(PI_BAD_POINTER, "null parts"); + if (segs == NULL) + SOFT_ERROR(PI_BAD_POINTER, "null segments"); - if (numParts > PI_I2C_RDRW_IOCTL_MAX_MSGS) - SOFT_ERROR(PI_TOO_MANY_PARTS, "too many parts (%d)", numParts); + if (numSegs > PI_I2C_RDRW_IOCTL_MAX_MSGS) + SOFT_ERROR(PI_TOO_MANY_SEGS, "too many segments (%d)", numSegs); - rdwr.msgs = parts; - rdwr.nmsgs = numParts; + rdwr.msgs = segs; + rdwr.nmsgs = numSegs; retval = ioctl(i2cInfo[handle].fd, PI_I2C_RDWR, &rdwr); - DBG(0, "i2cTransaction retval=%d", retval); - if (retval >= 0) return retval; - else return PI_BAD_I2C_PART; + else return PI_BAD_I2C_SEG; } -#ifdef ZZZZ - -int i2cXfer( +int i2cZip( unsigned handle, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen) { - int i, parts, inPos, outPos, status, bytes, len_bytes,flags, addr; - pi_i2c_msg_t part[PI_I2C_RDRW_IOCTL_MAX_MSGS]; + int numSegs, inPos, outPos, status, bytes, flags, addr; + 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", handle, myBuf2Str(inLen, (char *)inBuf), (int)outBuf, outLen); @@ -3606,19 +3656,21 @@ int i2cXfer( if (!outBuf && outLen) SOFT_ERROR(PI_BAD_POINTER, "output buffer can't be NULL"); - parts = 0; + numSegs = 0; + inPos = 0; outPos = 0; status = 0; + + addr = i2cInfo[handle].addr; 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", - status, inPos, inLen, inBuf[inPos]); - - len_bytes = 1; + DBG(DBG_INTERNAL, "status=%d inpos=%d inlen=%d cmd=%d addr=%d flags=%x", + status, inPos, inLen, inBuf[inPos], addr, flags); switch (inBuf[inPos++]) { @@ -3626,83 +3678,114 @@ int i2cXfer( status = 1; break; - case PI_I2C_READ16: - len_bytes = 2; - /* fall through */ - - case PI_I2C_READ: - if (inPos <= (inLen - len_bytes)) + case PI_I2C_COMBINED_ON: + /* Run prior transactions before setting combined flag */ + if (numSegs) { - if (len_bytes != 1) - { - bytes=inBuf[inPos] + (inBuf[inPos+1]<<8); - 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; + status = i2cSegments(handle, segs, numSegs); + if (status >= 0) status = 0; /* continue */ + numSegs = 0; } - else status = PI_BAD_BB_WLEN; + i2cSwitchCombined(1); break; - case PI_I2C_WRITE16: - len_bytes = 2; - /* fall through */ + case PI_I2C_COMBINED_OFF: + /* Run prior transactions before clearing combined flag */ + 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: - 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++]); - if (ack) status = PI_I2C_WRITE_FAILED; + status = i2cSegments(handle, segs, numSegs); + 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; 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; 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; - DBG(DBG_USER, "spiChan=%d spiBaud=%d spiFlags=0x%X", - spiChan, spiBaud, spiFlags); + DBG(DBG_USER, "spiChan=%d baud=%d spiFlags=0x%X", + spiChan, baud, spiFlags); CHECK_INITED; @@ -4125,8 +4208,8 @@ int spiOpen(unsigned spiChan, unsigned spiBaud, unsigned spiFlags) if (spiChan >= i) SOFT_ERROR(PI_BAD_SPI_CHANNEL, "bad spiChan (%d)", spiChan); - if ((spiBaud < MIN_SPI_SPEED) || (spiBaud > MAX_SPI_SPEED)) - SOFT_ERROR(PI_BAD_SPI_SPEED, "bad spiBaud (%d)", spiBaud); + if ((baud < PI_SPI_MIN_BAUD) || (baud > PI_SPI_MAX_BAUD)) + SOFT_ERROR(PI_BAD_SPI_SPEED, "bad baud (%d)", baud); if (spiFlags > (1<<22)) 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 */ { spiInit(spiFlags); - spiGo(spiBaud, spiFlags, NULL, NULL, 0); + spiGo(baud, spiFlags, NULL, NULL, 0); } slot = -1; @@ -4152,7 +4235,7 @@ int spiOpen(unsigned spiChan, unsigned spiBaud, unsigned spiFlags) if (slot < 0) 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); return slot; @@ -4890,6 +4973,7 @@ static void sigHandler(int signum) default: DBG(DBG_ALWAYS, "Unhandled signal %d, terminating\n", signum); + gpioTerminate(); exit(-1); } } @@ -4899,7 +4983,7 @@ static void sigHandler(int signum) /* exit */ DBG(DBG_ALWAYS, "Unhandled signal %d, terminating\n", signum); - + gpioTerminate(); exit(-1); } } @@ -5050,7 +5134,7 @@ static void * pthAlertThread(void *x) { /* shouldn't happen */ - gpioCfg.showStats = 1; + //gpioCfg.showStats = 1; gpioStats.diffTick[0]++; } @@ -5059,7 +5143,7 @@ static void * pthAlertThread(void *x) { /* shouldn't happen */ - gpioCfg.showStats = 1; + //gpioCfg.showStats = 1; gpioStats.diffTick[TICKSLOTS-1]++; } @@ -5303,10 +5387,10 @@ static void * pthAlertThread(void *x) { /* 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); - DBG(0, "%s", strerror(errno)); + DBG(DBG_ALWAYS, "%s", strerror(errno)); gpioNotify[n].bits = 0; gpioNotify[n].state = PI_NOTIFY_CLOSING; @@ -5331,10 +5415,10 @@ static void * pthAlertThread(void *x) { 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); - DBG(0, "%s", strerror(errno)); + DBG(DBG_ALWAYS, "%s", strerror(errno)); /* serious error, no point continuing */ gpioNotify[n].bits = 0; @@ -5897,7 +5981,7 @@ static void *pthSocketThreadHandler(void *fdC) if (recv(sock, buf, p[3], MSG_WAITALL) != p[3]) { /* 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); @@ -5907,7 +5991,7 @@ static void *pthSocketThreadHandler(void *fdC) else { /* 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); @@ -5949,12 +6033,13 @@ static void *pthSocketThreadHandler(void *fdC) { /* extensions */ - case PI_CMD_BI2CX: + case PI_CMD_BI2CZ: case PI_CMD_CF2: case PI_CMD_I2CPK: case PI_CMD_I2CRD: case PI_CMD_I2CRI: case PI_CMD_I2CRK: + case PI_CMD_I2CZ: case PI_CMD_PROCP: case PI_CMD_SERR: 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) { - int fd; - int lockResult; + int fd; + int lockResult; char pidStr[20]; + initCheckLockFile(); + /* try to grab the lock file */ fd = open(PI_LOCKFILE, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0644); @@ -6910,6 +7030,9 @@ static void initReleaseResources(void) close(fdMbox); fdMbox = -1; } + + gpioStats.DMARestarts = 0; + gpioStats.dmaInitCbsCount = 0; } int initInitialise(void) @@ -7233,6 +7356,8 @@ int gpioInitialise(void) if (libInitialised) return PIGPIO_VERSION; + DBG(DBG_STARTUP, "not initialised, initialising"); + runState = PI_STARTING; status = initInitialise(); @@ -7262,6 +7387,8 @@ void gpioTerminate(void) if (!libInitialised) return; + DBG(DBG_STARTUP, "initialised, terminating"); + runState = PI_ENDING; gpioMaskSet = 0; @@ -7863,9 +7990,9 @@ int gpioWaveAddGeneric(unsigned numPulses, gpioPulse_t *pulses) int gpioWaveAddSerial (unsigned gpio, - unsigned bbBaud, - unsigned bbBits, - unsigned bbStops, + unsigned baud, + unsigned data_bits, + unsigned stop_bits, unsigned offset, unsigned numBytes, char *bstr) @@ -7879,7 +8006,7 @@ int gpioWaveAddSerial DBG(DBG_USER, "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)); CHECK_INITED; @@ -7887,16 +8014,16 @@ int gpioWaveAddSerial if (gpio > PI_MAX_USER_GPIO) SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio); - if ((bbBaud < PI_BB_MIN_BAUD) || (bbBaud > PI_BB_TX_MAX_BAUD)) - SOFT_ERROR(PI_BAD_WAVE_BAUD, "bad baud rate (%d)", bbBaud); + if ((baud < PI_WAVE_MIN_BAUD) || (baud > PI_WAVE_MAX_BAUD)) + SOFT_ERROR(PI_BAD_WAVE_BAUD, "bad baud rate (%d)", baud); - if ((bbBits < PI_MIN_WAVE_DATABITS) || (bbBits > PI_MAX_WAVE_DATABITS)) - SOFT_ERROR(PI_BAD_DATABITS, "bad number of databits (%d)", bbBits); + if ((data_bits < PI_MIN_WAVE_DATABITS) || (data_bits > PI_MAX_WAVE_DATABITS)) + SOFT_ERROR(PI_BAD_DATABITS, "bad number of databits (%d)", data_bits); - if ((bbStops < PI_MIN_WAVE_HALFSTOPBITS) || - (bbStops > PI_MAX_WAVE_HALFSTOPBITS)) + if ((stop_bits < PI_MIN_WAVE_HALFSTOPBITS) || + (stop_bits > PI_MAX_WAVE_HALFSTOPBITS)) 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) SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio); @@ -7907,12 +8034,12 @@ int gpioWaveAddSerial if (offset > PI_WAVE_MAX_MICROS) SOFT_ERROR(PI_BAD_SER_OFFSET, "offset too large (%d)", offset); - if (bbBits > 8) numBytes /= 2; - if (bbBits > 16) numBytes /= 2; + if (data_bits > 8) numBytes /= 2; + if (data_bits > 16) numBytes /= 2; if (!numBytes) return 0; - waveBitDelay(bbBaud, bbBits, bbStops, bitDelay); + waveBitDelay(baud, data_bits, stop_bits, bitDelay); p = 0; @@ -7936,11 +8063,11 @@ int gpioWaveAddSerial lev = 0; - if (bbBits < 9) c = bstr[i]; - else if (bbBits < 17) c = wstr[i]; + if (data_bits < 9) c = bstr[i]; + else if (data_bits < 17) c = wstr[i]; else c = lstr[i]; - for (b=0; b PI_MAX_USER_GPIO) SOFT_ERROR(PI_BAD_USER_GPIO, "bad SCL (%d)", SCL); - if ((bbBaud < PI_BB_MIN_BAUD) || (bbBaud > PI_BB_RX_MAX_BAUD)) - SOFT_ERROR(PI_BAD_WAVE_BAUD, - "SDA %d, bad baud rate (%d)", SDA, bbBaud); + if ((baud < PI_BB_I2C_MIN_BAUD) || (baud > PI_BB_I2C_MAX_BAUD)) + SOFT_ERROR(PI_BAD_I2C_BAUD, + "SDA %d, bad baud rate (%d)", SDA, baud); if (wfRx[SDA].mode != PI_WFRX_NONE) 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); wfRx[SDA].gpio = SDA; wfRx[SDA].mode = PI_WFRX_I2C; - wfRx[SDA].baud = bbBaud; + wfRx[SDA].baud = baud; wfRx[SDA].I.started = 0; wfRx[SDA].I.SDA = SDA; 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.SCLMode = gpioGetMode(SCL); @@ -8597,14 +8724,17 @@ int bbI2CClose(unsigned SDA) return 0; } - /*-------------------------------------------------------------------------*/ -int bbI2CXfer( +int bbI2CZip( 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; DBG(DBG_USER, "gpio=%d inBuf=%s outBuf=%08X len=%d", @@ -8630,12 +8760,15 @@ int bbI2CXfer( outPos = 0; status = 0; + addr = 0; + flags = 0; + esc = 0; + setesc = 0; + while (!status && (inPos < inLen)) { - DBG(DBG_INTERNAL, "status=%d inpos=%d inlen=%d cmd=%d", - status, inPos, inLen, inBuf[inPos]); - - len_bytes = 1; + DBG(DBG_INTERNAL, "status=%d inpos=%d inlen=%d cmd=%d addr=%d flags=%x", + status, inPos, inLen, inBuf[inPos], addr, flags); switch (inBuf[inPos++]) { @@ -8651,71 +8784,79 @@ int bbI2CXfer( I2CStop(w); break; - case PI_I2C_READ16: - len_bytes = 2; - /* fall through */ - - 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; + case PI_I2C_ADDR: + addr = myI2CGetPar(inBuf, &inPos, inLen, &esc); + if (addr < 0) status = PI_BAD_I2C_CMD; break; - case PI_I2C_WRITE16: - len_bytes = 2; - /* fall through */ + 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) 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: - 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++]); - if (ack) status = PI_I2C_WRITE_FAILED; } - ack = I2CPutByte(w, inBuf[inPos++]); - //if (ack) status = PI_I2C_WRITE_FAILED; - } - else status = PI_BAD_BB_RLEN; + else status = PI_BAD_I2C_RLEN; + } else status = PI_I2C_WRITE_FAILED; } - else status = PI_BAD_BB_RLEN; + else status = PI_BAD_I2C_CMD; break; 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; @@ -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; - 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; if (gpio > PI_MAX_USER_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, - "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, - "gpio %d, bad data bits (%d)", gpio, bbBits); + "gpio %d, bad data bits (%d)", gpio, data_bits); if (wfRx[gpio].mode != PI_WFRX_NONE) 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; wfRx[gpio].gpio = gpio; 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.bufSize = SRX_BUF_SIZE; @@ -8766,10 +8908,10 @@ int gpioSerialReadOpen(unsigned gpio, unsigned bbBaud, unsigned bbBits) wfRx[gpio].s.readPos = 0; wfRx[gpio].s.writePos = 0; wfRx[gpio].s.bit = -1; - wfRx[gpio].s.dataBits = bbBits; + wfRx[gpio].s.dataBits = data_bits; - if (bbBits < 9) wfRx[gpio].s.bytes = 1; - else if (bbBits < 17) wfRx[gpio].s.bytes = 2; + if (data_bits < 9) wfRx[gpio].s.bytes = 1; + else if (data_bits < 17) wfRx[gpio].s.bytes = 2; else wfRx[gpio].s.bytes = 4; gpioSetAlertFunc(gpio, waveRxBit); diff --git a/pigpio.h b/pigpio.h index 3ee663b..2ee5684 100644 --- a/pigpio.h +++ b/pigpio.h @@ -31,7 +31,7 @@ For more information, please refer to #include #include -#define PIGPIO_VERSION 32 +#define PIGPIO_VERSION 33 /*TEXT @@ -222,9 +222,6 @@ I2C i2cOpen Opens an I2C device i2cClose Closes an I2C device -i2cReadDevice Reads the raw I2C device -i2cWriteDevice Writes the raw I2C device - i2cWriteQuick SMBus write quick i2cWriteByte SMBus write byte i2cReadByte SMBus read byte @@ -240,6 +237,19 @@ i2cBlockProcessCall SMBus block process call i2cWriteI2CBlockData SMBus write 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 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_CHARS (PI_WAVE_BLOCKS * 300) -#define PI_BB_MIN_BAUD 50 -#define PI_BB_RX_MAX_BAUD 250000 -#define PI_BB_TX_MAX_BAUD 1000000 +#define PI_BB_I2C_MIN_BAUD 50 +#define PI_BB_I2C_MAX_BAUD 500000 + +#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_MAX_WAVE_DATABITS 32 @@ -535,14 +556,12 @@ typedef void *(gpioThreadFunc_t) (void *); /* I2C, SPI, SER */ -#define MIN_SPI_SPEED 32000 -#define MAX_SPI_SPEED 125000000 - #define PI_I2C_SLOTS 32 #define PI_SPI_SLOTS 16 #define PI_SER_SLOTS 8 #define PI_NUM_I2C_BUS 2 +#define PI_MAX_I2C_ADDR 0x7F #define PI_NUM_AUX_SPI_CHANNEL 3 #define PI_NUM_STD_SPI_CHANNEL 2 @@ -554,7 +573,7 @@ typedef void *(gpioThreadFunc_t) (void *); #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_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_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */ -/* bit bang I2C commands */ +/* bbI2CZip and i2cZip commands */ -#define PI_I2C_END 0 -#define PI_I2C_START 1 -#define PI_I2C_STOP 2 -#define PI_I2C_READ 3 -#define PI_I2C_WRITE 4 -#define PI_I2C_READ16 5 -#define PI_I2C_WRITE16 6 - -/* combined transaction I2C flag commands */ - -#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 */ +#define PI_I2C_END 0 +#define PI_I2C_ESC 1 +#define PI_I2C_START 2 +#define PI_I2C_COMBINED_ON 2 +#define PI_I2C_STOP 3 +#define PI_I2C_COMBINED_OFF 3 +#define PI_I2C_ADDR 4 +#define PI_I2C_FLAGS 5 +#define PI_I2C_READ 6 +#define PI_I2C_WRITE 7 /* SPI */ @@ -1429,9 +1441,9 @@ D*/ /*F*/ int gpioWaveAddSerial (unsigned user_gpio, - unsigned bbBaud, - unsigned bbBits, - unsigned bbStop, + unsigned baud, + unsigned data_bits, + unsigned stop_bits, unsigned offset, unsigned numBytes, char *str); @@ -1442,9 +1454,9 @@ microseconds from the start of the waveform. . . user_gpio: 0-31 - bbBaud: 100-1000000 - bbBits: 1-32 - bbStop: 2-8 + baud: 50-1000000 +data_bits: 1-32 +stop_bits: 2-8 offset: 0- numBytes: 1- str: an array of chars (which may contain nulls) @@ -1457,19 +1469,19 @@ or PI_TOO_MANY_PULSES. NOTES: -The serial data is formatted as one start bit, bbBits data bits, and -bbStop/2 stop bits. +The serial data is formatted as one start bit, data_bits data bits, and +stop_bits/2 stop bits. It is legal to add serial data streams with different baud rates to the same waveform. numBytes is the number of bytes of data in str. -The bytes required for each character depend upon bbBits. +The bytes required for each character depend upon data_bits. -For bbBits 1-8 there will be one byte per character. -For bbBits 9-16 there will be two bytes per character. -For bbBits 17-32 there will be four bytes per character. +For data_bits 1-8 there will be one byte per character. +For data_bits 9-16 there will be two bytes per character. +For data_bits 17-32 there will be four bytes per character. ... #define MSG_LEN 8 @@ -1690,14 +1702,14 @@ D*/ /*F*/ -int gpioSerialReadOpen(unsigned user_gpio, unsigned bbBaud, unsigned bbBits); +int gpioSerialReadOpen(unsigned user_gpio, unsigned baud, unsigned data_bits); /*D This function opens a gpio for bit bang reading of serial data. . . user_gpio: 0-31 - bbBaud: 100-250000 - bbBits: 1-32 + baud: 50-250000 +data_bits: 1-32 . . 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. 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 [*bbBits*] 9-16 there will be two bytes per character. -For [*bbBits*] 17-32 there will be four bytes per character. +For [*data_bits*] 1-8 there will be one byte per character. +For [*data_bits*] 9-16 there will be two bytes per character. +For [*data_bits*] 17-32 there will be four bytes per character. D*/ @@ -1754,7 +1766,7 @@ This returns a handle for the device at the address on the I2C bus. . . i2cBus: 0-1 - i2cAddr: 0x08-0x77 + i2cAddr: 0x00-0x7F i2cFlags: 0 . . @@ -1794,42 +1806,6 @@ Returns 0 if OK, otherwise PI_BAD_HANDLE. 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*/ int i2cWriteQuick(unsigned handle, unsigned bit); /*D @@ -2124,17 +2100,242 @@ S Addr Wr [A] Comm [A] Data [A] Data [A] ... [A] Data [A] P . . 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( - unsigned SDA, - char *inBuf, unsigned inLen, char *outBuf, unsigned outLen); +Returns count (>0) if OK, otherwise PI_BAD_HANDLE, PI_BAD_PARAM, or +PI_I2C_READ_FAILED. +D*/ /*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 This function returns a handle for the SPI device on the channel. 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) - spiBaud: 32K-125M (values above 30M are unlikely to work) + baud: 32K-125M (values above 30M are unlikely to work) spiFlags: see below . . @@ -2267,20 +2468,24 @@ D*/ /*F*/ -int serOpen(char *sertty, unsigned serBaud, unsigned serFlags); +int serOpen(char *sertty, unsigned baud, unsigned serFlags); /*D This function opens a serial device at a specified baud rate with specified flags. . . 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 . . Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE, or PI_SER_OPEN_FAILED. +The baud rate must be one of 50, 75, 110, 134, 150, +200, 300, 600, 1200, 1800, 2400, 4800, 9500, 19200, +38400, 57600, 115200, or 230400. + No flags are currently defined. This parameter should be set to zero. D*/ @@ -3489,56 +3694,24 @@ An unsigned argument passed to a user customised function. Its meaning is defined by the customiser. argc:: - The count of bytes passed to a user customised function. *argx:: - A pointer to an array of bytes passed to a user customised function. Its meaning and content is defined by the customiser. -bbBaud:: - -The baud rate used for the transmission and reception of bit banged -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 -. . +baud:: +The speed of serial communication (I2C, SPI, serial link, waves) in +bits per second. bit:: - A value of 0 or 1. bitPos:: - A bit position within a byte or word. The least significant bit is position 0. bits:: - A value used to select gpios. If bit n of bits is set then gpio n is selected. @@ -3611,6 +3784,16 @@ count:: The number of bytes to be transferred in an I2C, SPI, or Serial 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 . . PI_MIN_DMA_CHANNEL 0 @@ -3767,9 +3950,8 @@ A number referencing an object opened by one of [*serOpen*] [*spiOpen*] -i2cAddr::0x08-0x77 - -The address of a device on the I2C bus (0x08 - 0x77) +i2cAddr:: +The address of a device on the I2C bus. i2cBus::0-1 @@ -3789,12 +3971,16 @@ PI_DISABLE_FIFO_IF 1 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. level:: - The level of a gpio. Low or High. . . @@ -3869,22 +4055,25 @@ per character. numPar:: 0-10 The number of parameters passed to a script. -numParts:: -The number of parts in a combined I2C transaction. - numPulses:: The number of pulses to be added to a waveform. +numSegs:: +The number of segments in a combined I2C transaction. + offset:: The associated data starts this number of microseconds from the start of tghe waveform. +*outBuf:: +A buffer used to return data from a function. + +outLen:: +The size in bytes of an output buffer. + *param:: An array of script parameters. -*parts:: -An array of the part transactions which make up a combined I2C transaction. - pi_i2c_msg_t:: . . 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. +SCL:: + +The user gpio to use for the clock when bit banging I2C. + *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*]. +SDA:: + +The user gpio to use for data when bit banging I2C. + secondaryChannel:: 0-6 The DMA channel used to time output waveforms. @@ -4044,21 +4241,19 @@ seconds:: The number of seconds. -serBaud:: +*segs:: -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. +An array of segments which make up a combined I2C transaction. serFlags:: - Flags which modify a serial open command. None are currently defined. *sertty:: - 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 . . PI_MIN_SIGNUM 0 @@ -4070,15 +4265,9 @@ size_t:: A standard type used to indicate the size of an object in bytes. *spi:: - A pointer to a [*rawSPI_t*] structure. -spiBaud:: - -The speed in bits per second to use for the SPI device. - spiBitFirst:: - Gpio reads are made from spiBitFirst to spiBitLast. spiBitLast:: @@ -4086,31 +4275,33 @@ spiBitLast:: Gpio reads are made from spiBitFirst to spiBitLast. spiBits:: - The number of bits to transfer in a raw SPI transaction. spiChan:: - A SPI channel, 0-2. spiFlags:: - See [*spiOpen*]. spiSS:: - The SPI slave select gpio in a raw SPI transaction. spiTxBits:: - 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. timeout:: - A gpio watchdog timeout in milliseconds. . . PI_MIN_WDOG_TIMEOUT 0 @@ -4285,9 +4476,9 @@ PARAMS*/ #define PI_CMD_BI2CC 89 #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 @@ -4458,13 +4649,14 @@ after this command is issued. #define PI_BAD_STOPBITS -102 // serial (half) stop bits not 2-8 #define PI_MSG_TOOBIG -103 // socket/pipe message too big #define PI_BAD_MALLOC_MODE -104 // bad memory allocation mode -#define PI_TOO_MANY_PARTS -105 // too many I2C transaction parts -#define PI_BAD_I2C_PART -106 // a combined I2C transaction failed +#define PI_TOO_MANY_SEGS -105 // too many I2C transaction segments +#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_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_BB_RLEN -110 // bad BB read length -#define PI_BAD_BB_CMD -111 // bad BB command +#define PI_BAD_I2C_WLEN -109 // bad I2C write length +#define PI_BAD_I2C_RLEN -110 // bad I2C read length +#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_99 -2099 diff --git a/pigpio.py b/pigpio.py index cc43e58..3974272 100644 --- a/pigpio.py +++ b/pigpio.py @@ -194,9 +194,6 @@ I2C i2c_open Opens 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_byte SMBus write 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_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_open Opens a SPI device @@ -257,7 +263,7 @@ import threading import os import atexit -VERSION = "1.17" +VERSION = "1.18" exceptions = True @@ -416,7 +422,13 @@ _PI_CMD_HP =86 _PI_CMD_CF1 =87 _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 @@ -525,10 +537,14 @@ PI_BAD_DATABITS =-101 PI_BAD_STOPBITS =-102 PI_MSG_TOOBIG =-103 PI_BAD_MALLOC_MODE =-104 -_PI_TOO_MANY_PARTS =-105 -_PI_BAD_I2C_PART =-106 +_PI_TOO_MANY_SEGS =-105 +_PI_BAD_I2C_SEG =-106 PI_BAD_SMBUS_CMD =-107 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 @@ -635,10 +651,14 @@ _errors=[ [PI_BAD_STOPBITS , "serial (half) stop bits not 2-8"], [PI_MSG_TOOBIG , "socket/pipe message too big"], [PI_BAD_MALLOC_MODE , "bad memory allocation mode"], - [_PI_TOO_MANY_PARTS , "too many I2C transaction parts"], - [_PI_BAD_I2C_PART , "a combined I2C transaction failed"], + [_PI_TOO_MANY_SEGS , "too many I2C transaction segments"], + [_PI_BAD_I2C_SEG , "an I2C transaction segment failed"], [PI_BAD_SMBUS_CMD , "SMBus command not supported"], [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 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 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 to output. - bb_baud:= baud rate to use. + baud:= 50-1000000 bits per second. data:= the bytes to write. offset:= number of microseconds from the start of the waveform, default 0. @@ -1767,7 +1787,7 @@ class pi(): # pigpio message format # I p1 gpio - # I p2 bb_baud + # I p2 baud # I p3 len+12 ## extension ## # I bb_bits @@ -1777,7 +1797,7 @@ class pi(): if len(data): extents = [struct.pack("III", bb_bits, bb_stop, offset), data] 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: return 0 @@ -2003,7 +2023,7 @@ class pi(): Returns a handle (>=0) for the device at the I2C bus address. i2c_bus:= 0-1. - i2c_address:= 0x08-0x77. + i2c_address:= 0x00-0x7F. i2c_flags:= 0, no flags are currently defined. 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)) - 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): """ Sends a single bit to the device associated with handle. @@ -2441,7 +2406,269 @@ class pi(): self.sl.l.release() 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 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_baud:= 32K-125M (values above 30M are unlikely to work). + baud:= 32K-125M (values above 30M are unlikely to work). spi_flags:= see below. 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. ... - # 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 p2 spi_baud + # I p2 baud # I p3 4 ## extension ## # I spi_flags extents = [struct.pack("I", spi_flags)] 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): """ @@ -2631,13 +2858,13 @@ class pi(): self.sl.l.release() 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 - at ser_baud bits per second. + at baud bits per second. 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. 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 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) h2 = pi.serial_open("/dev/ttyUSB1", 19200, 0) ... """ - # I p1 ser_baud + # I p1 baud # I p2 ser_flags # I p3 len ## extension ## # s len data bytes 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): """ @@ -2912,12 +3143,12 @@ class pi(): """ 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. 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. The serial data is held in a cyclic buffer and is read using @@ -2934,13 +3165,13 @@ class pi(): # pigpio message format # I p1 user_gpio - # I p2 bb_baud + # I p2 baud # I p3 4 ## extension ## # I bb_bits extents = [struct.pack("I", bb_bits)] 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): """ @@ -3222,16 +3453,15 @@ def xref(): An array of bytes passed to a user customised function. Its meaning and content is defined by the customiser. - bb_baud: 100 - 250000 - The baud rate used for the transmission of bit bang serial data. + baud: + The speed of serial communication (I2C, SPI, serial link, waves) + in bits per second. bb_bits: 1-32 - The number of data bits to be used when adding serial data to a waveform. bb_stop: 2-8 - The number of (half) stop bits to be used when adding serial data to a waveform. @@ -3424,7 +3654,7 @@ def xref(): One of the i2c_ functions. 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 An I2C bus number. @@ -3496,18 +3726,17 @@ def xref(): The maximum number of bytes a user customised function should return, default 8192. + SCL: + The user gpio to use for the clock when bit banging I2C. + script: The text of a script to store on the pigpio daemon. script_id: 0- A number referencing a script created by [*store_script*]. - ser_baud: - The transmission rate in bits per second. - - The allowable values are 50, 75, 110, 134, 150, 200, 300, - 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, - 57600, 115200, or 230400. + SDA: + The user gpio to use for data when bit banging I2C. ser_flags: 32 bit No serial flags are currently defined. @@ -3518,9 +3747,6 @@ def xref(): spi_*: One of the spi_ functions. - spi_baud: 32K-125M - The transmission rate in bits per second. - spi_channel: 0-2 A SPI channel. diff --git a/pigpiod_if.3 b/pigpiod_if.3 index 4b639c3..dfa99b3 100644 --- a/pigpiod_if.3 +++ b/pigpiod_if.3 @@ -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 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 This function adds a waveform representing serial data to the existing waveform (if any). The serial data starts offset @@ -1430,11 +1430,11 @@ microseconds from the start of the waveform. .EX user_gpio: 0-31. .br - bbBaud: 100-250000 + baud: 50-1000000 .br - bbBits: number of data bits (1-32) +data_bits: number of data bits (1-32) .br - bbStop: number of stop half bits (2-8) +stop_bits: number of stop half bits (2-8) .br offset: 0- .br @@ -1461,8 +1461,8 @@ NOTES: .br .br -The serial data is formatted as one start bit, \fBbbBits\fP data bits, -and \fBbbStop\fP/2 stop bits. +The serial data is formatted as one start bit, \fBdata_bits\fP data bits, +and \fBstop_bits\fP/2 stop bits. .br @@ -1478,16 +1478,16 @@ the same waveform. .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 -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 -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 -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 "" 4 @@ -1934,7 +1934,7 @@ script_id: >=0, as returned by \fBstore_script\fP. .br 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 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 user_gpio: 0-31. .br - bbBaud: 100-250000 + baud: 50-250000 .br - bbBits: 1-32 +data_bits: 1-32 .br .EE @@ -1999,16 +1999,16 @@ or PI_NOT_SERIAL_GPIO. .br 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 -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 -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 -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 "" 4 @@ -2040,7 +2040,7 @@ This returns a handle for the device at address i2c_addr on bus i2c_bus. .EX i2c_bus: 0-1. .br - i2c_addr: 0x08-0x77. + i2c_addr: 0x00-0x7F. .br i2c_flags: 0. .br @@ -2077,54 +2077,6 @@ handle: >=0, as returned by a call to \fBi2c_open\fP. .br 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 "" 4 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 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 This function returns a handle for the SPI device on channel. 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 spi_channel: 0-1 (0-2 for A+/B+/Pi2 auxiliary device). .br - spi_baud: 32K-125M (values above 30M are unlikely to work). + baud: 32K-125M (values above 30M are unlikely to work). .br spi_flags: see below. .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 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 This function opens a serial device at a specified baud rate with specified flags. @@ -2758,7 +3088,7 @@ with specified flags. .EX ser_tty: the serial device to open, /dev/tty*. .br - ser_baud: the baud rate to use. + baud: the baud rate in bits per second, see below. .br ser_flags: 0. .br @@ -2773,6 +3103,13 @@ PI_SER_OPEN_FAILED. .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 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 "\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 +.IP "\fBbaud\fP" 0 +The speed of serial communication (I2C, SPI, serial link, waves) in +bits per second. .br @@ -3316,6 +3589,25 @@ command. .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 A floating point number. @@ -3491,8 +3783,8 @@ A number referencing an object opened by one of \fBi2c_open\fP, \fBnotify_open\f .br -.IP "\fBi2c_addr\fP: 0x08-0x77" 0 -The address of a device on the I2C bus (0x08 - 0x77) +.IP "\fBi2c_addr\fP" 0 +The address of a device on the I2C bus. .br @@ -3519,10 +3811,16 @@ A register of an I2C device. .br +.IP "\fB*inBuf\fP" 0 +A buffer used to pass data to a function. + .br .br +.IP "\fBinLen\fP" 0 +The number of bytes of data in a buffer. + .br .br @@ -3643,6 +3941,20 @@ the waveform. .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 An array of script parameters. @@ -3650,10 +3962,6 @@ An array of script parameters. .br -.br - -.br - .IP "\fB*portStr\fP" 0 A string specifying the port address used by the Pi running the pigpio daemon. It may be NULL in which case "8888" @@ -3806,6 +4114,13 @@ A pointer to a buffer to receive data. .br +.IP "\fBSCL\fP" 0 +The user gpio to use for the clock when bit banging I2C. + +.br + +.br + .IP "\fB*script\fP" 0 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 +.IP "\fBSDA\fP" 0 +The user gpio to use for data when bit banging I2C. + .br .br @@ -3831,19 +4149,6 @@ The number of seconds. .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 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 -.IP "\fBspi_baud\fP" 0 -The speed in bits per second to use for the SPI device. - -.br - -.br - .IP "\fBspi_channel\fP" 0 A SPI channel, 0-2. @@ -3886,6 +4184,26 @@ See \fBspi_open\fP. .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 An array of characters. diff --git a/pigpiod_if.c b/pigpiod_if.c index 0f5020c..d5d2baf 100644 --- a/pigpiod_if.c +++ b/pigpiod_if.c @@ -25,7 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to */ -/* PIGPIOD_IF_VERSION 14 */ +/* PIGPIOD_IF_VERSION 15 */ #include #include @@ -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); } +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 status; @@ -846,9 +868,7 @@ int script_status(unsigned script_id, uint32_t *param) if (status > 0) { - /* get the data */ - recv(gPigCommand, p, status, MSG_WAITALL); - + recvMax(p, sizeof(p), status); status = p[0]; 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) { - /* get the data */ - recv(gPigCommand, buf, bytes, MSG_WAITALL); + bytes = recvMax(buf, bufSize, bytes); } 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) {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) {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) { - /* get the data */ - recv(gPigCommand, buf, bytes, MSG_WAITALL); + bytes = recvMax(buf, 32, bytes); } pthread_mutex_unlock(&command_mutex); @@ -1092,8 +1074,7 @@ int i2c_block_process_call( if (bytes > 0) { - /* get the data */ - recv(gPigCommand, buf, bytes, MSG_WAITALL); + bytes = recvMax(buf, 32, bytes); } pthread_mutex_unlock(&command_mutex); @@ -1123,8 +1104,7 @@ int i2c_read_i2c_block_data( if (bytes > 0) { - /* get the data */ - recv(gPigCommand, buf, bytes, MSG_WAITALL); + bytes = recvMax(buf, count, bytes); } 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); } +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) { gpioExtent_t ext[1]; @@ -1184,8 +1289,7 @@ int spi_read(unsigned handle, char *buf, unsigned count) if (bytes > 0) { - /* get the data */ - recv(gPigCommand, buf, bytes, MSG_WAITALL); + bytes = recvMax(buf, count, bytes); } pthread_mutex_unlock(&command_mutex); @@ -1233,8 +1337,7 @@ int spi_xfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count) if (bytes > 0) { - /* get the data */ - recv(gPigCommand, rxBuf, bytes, MSG_WAITALL); + bytes = recvMax(rxBuf, count, bytes); } pthread_mutex_unlock(&command_mutex); @@ -1301,8 +1404,7 @@ int serial_read(unsigned handle, char *buf, unsigned count) if (bytes > 0) { - /* get the data */ - recv(gPigCommand, buf, bytes, MSG_WAITALL); + bytes = recvMax(buf, count, bytes); } pthread_mutex_unlock(&command_mutex); @@ -1355,8 +1457,7 @@ int custom_2(unsigned arg1, char *argx, unsigned count, if (bytes > 0) { - /* get the data */ - recv(gPigCommand, retBuf, bytes, MSG_WAITALL); + bytes = recvMax(retBuf, retMax, bytes); } pthread_mutex_unlock(&command_mutex); diff --git a/pigpiod_if.h b/pigpiod_if.h index 4db7071..7a7ede0 100644 --- a/pigpiod_if.h +++ b/pigpiod_if.h @@ -30,7 +30,7 @@ For more information, please refer to #include "pigpio.h" -#define PIGPIOD_IF_VERSION 14 +#define PIGPIOD_IF_VERSION 15 /*TEXT @@ -209,9 +209,6 @@ I2C i2c_open Opens 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_byte smbus write 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_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_open Opens a SPI device @@ -1028,8 +1034,8 @@ D*/ /*F*/ int wave_add_serial - (unsigned user_gpio, unsigned bbBaud, unsigned bbBits, - unsigned bbStop, unsigned offset, unsigned numBytes, char *str); + (unsigned user_gpio, unsigned baud, unsigned data_bits, + unsigned stop_bits, unsigned offset, unsigned numBytes, char *str); /*D This function adds a waveform representing serial data to the existing waveform (if any). The serial data starts offset @@ -1037,9 +1043,9 @@ microseconds from the start of the waveform. . . user_gpio: 0-31. - bbBaud: 100-250000 - bbBits: number of data bits (1-32) - bbStop: number of stop half bits (2-8) + baud: 50-1000000 +data_bits: number of data bits (1-32) +stop_bits: number of stop half bits (2-8) offset: 0- numBytes: 1- str: an array of chars. @@ -1052,19 +1058,19 @@ or PI_TOO_MANY_PULSES. NOTES: -The serial data is formatted as one start bit, [*bbBits*] data bits, -and [*bbStop*]/2 stop bits. +The serial data is formatted as one start bit, [*data_bits*] data bits, +and [*stop_bits*]/2 stop bits. It is legal to add serial data streams with different baud rates to the same waveform. [*numBytes*] is the number of bytes of data in str. -The bytes required for each character depend upon [*bbBits*]. +The bytes required for each character depend upon [*data_bits*]. -For [*bbBits*] 1-8 there will be one byte per character. -For [*bbBits*] 9-16 there will be two bytes per character. -For [*bbBits*] 17-32 there will be four bytes per character. +For [*data_bits*] 1-8 there will be one byte per character. +For [*data_bits*] 9-16 there will be two bytes per character. +For [*data_bits*] 17-32 there will be four bytes per character. D*/ /*F*/ @@ -1369,14 +1375,14 @@ The function returns 0 if OK, otherwise PI_BAD_SCRIPT_ID. D*/ /*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 This function opens a gpio for bit bang reading of serial data. . . user_gpio: 0-31. - bbBaud: 100-250000 - bbBits: 1-32 + baud: 50-250000 +data_bits: 1-32 . . 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. 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 [*bbBits*] 9-16 there will be two bytes per character. -For [*bbBits*] 17-32 there will be four bytes per character. +For [*data_bits*] 1-8 there will be one byte per character. +For [*data_bits*] 9-16 there will be two bytes per character. +For [*data_bits*] 17-32 there will be four bytes per character. D*/ /*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_addr: 0x08-0x77. + i2c_addr: 0x00-0x7F. 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. 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*/ int i2c_write_quick(unsigned handle, unsigned bit); /*D @@ -1723,7 +1699,211 @@ PI_I2C_WRITE_FAILED. D*/ /*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 This function returns a handle for the SPI device on channel. 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_baud: 32K-125M (values above 30M are unlikely to work). + baud: 32K-125M (values above 30M are unlikely to work). spi_flags: see below. . . @@ -1853,20 +2033,24 @@ PI_BAD_HANDLE, PI_BAD_SPI_COUNT, or PI_SPI_XFER_FAILED. D*/ /*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 This function opens a serial device at a specified baud rate with specified flags. . . 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. . . Returns a handle (>=0) if OK, otherwise PI_NO_HANDLE, or PI_SER_OPEN_FAILED. +The baud rate must be one of 50, 75, 110, 134, 150, +200, 300, 600, 1200, 1800, 2400, 4800, 9500, 19200, +38400, 57600, 115200, or 230400. + No flags are currently defined. This parameter should be set to zero. 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. Its meaning and content is defined by the customiser. - -bbBaud:: -The baud rate used for the transmission and reception of bit banged -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 -. . +baud:: +The speed of serial communication (I2C, SPI, serial link, waves) in +bits per second. bit:: A value of 0 or 1. @@ -2165,6 +2323,14 @@ count:: The number of bytes to be transferred in an I2C, SPI, or Serial 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:: A floating point number. @@ -2246,8 +2412,8 @@ handle::0- A number referencing an object opened by one of [*i2c_open*], [*notify_open*], [*serial_open*], and [*spi_open*]. -i2c_addr::0x08-0x77 -The address of a device on the I2C bus (0x08 - 0x77) +i2c_addr:: +The address of a device on the I2C bus. i2c_bus::0-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 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:: A whole number, negative or positive. @@ -2313,10 +2483,15 @@ offset:: The associated data starts this number of microseconds from the start of the waveform. +*outBuf:: +A buffer used to return data from a function. + +outLen:: +The size in bytes of an output buffer. + *param:: An array of script parameters. - *portStr:: A string specifying the port address used by the Pi running 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:: A pointer to a buffer to receive data. +SCL:: +The user gpio to use for the clock when bit banging I2C. + *script:: A pointer to the text of a script. script_id:: An id of a stored script as returned by [*store_script*]. +SDA:: +The user gpio to use for data when bit banging I2C. 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:: 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:: 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:: A SPI channel, 0-2. spi_flags:: 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:: An array of characters. diff --git a/pigs.1 b/pigs.1 index 0afa9c5..08d9a17 100644 --- a/pigs.1 +++ b/pigs.1 @@ -251,6 +251,135 @@ ERROR: no permission to update one or more gpios .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 "" 4 This command read gpios 0-31 (bank 1) and returns the levels as a @@ -1139,6 +1268,72 @@ $ pigs i2cww 0 0 0xffff .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 "" 4 @@ -2149,7 +2344,7 @@ $ pigs serda 0 .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 .br @@ -2166,6 +2361,11 @@ will be returned. The UART is /dev/ttyAMA0. The consoles are /dev/ttyx. The USB 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 \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 will be returned. +.br +The baud rate may be between 50 and 250000 bits per second. + .br The received data is held in a cyclic buffer. @@ -2422,7 +2625,26 @@ will be returned. .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 .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, mode 0, active low chip select. +.br +Speeds between 32kbps and 125Mbps are allowed. Speeds above 30Mbps +are unlikely to work. + .br 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 @@ -2524,6 +2750,12 @@ $ pigs spio 0 100000 3 # Open channel 0 at 100kbps in mode 3. 0 .br +.br +$ pigs spio 0 32000 256 # Open channel 0 of auxiliary spi at 32kbps. +.br +1 +.br + .EE .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 \fBsb\fP/2 stop bits. +.br +The baud rate may be between 50 and 1000000 bits per second. + .br It is legal to add serial data streams with different baud rates to the same waveform. @@ -3285,8 +3520,9 @@ ERROR: non existent wave id .br -.IP "\fBb\fP - baud (100-250000)" 0 -The command expects a baud rate for serial data. +.IP "\fBb\fP - baud" 0 +The command expects the baud rate in bits per second for +the transmission of serial data (I2C/SPI/serial link, waves). .br @@ -3519,6 +3755,18 @@ The command expects the number of stop (half) bits per serial character. .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 The command expects a flag value. No serial flags are currently defined. diff --git a/pigs.c b/pigs.c index 1869164..cc6876c 100644 --- a/pigs.c +++ b/pigs.c @@ -26,7 +26,7 @@ For more information, please refer to */ /* -This version is for pigpio version 32+ +This version is for pigpio version 33+ */ #include @@ -141,7 +141,7 @@ void print_result(int sock, int rv, cmdCmd_t cmd) printf(cmdUsage); 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); if (r < 0) fatal("ERROR: %s", cmdErrStr(r)); if (r > 0) @@ -178,12 +178,13 @@ void get_extensions(int sock, int command, int res) { switch (command) { - case PI_CMD_BI2CX: + case PI_CMD_BI2CZ: case PI_CMD_CF2: case PI_CMD_I2CPK: case PI_CMD_I2CRD: case PI_CMD_I2CRI: case PI_CMD_I2CRK: + case PI_CMD_I2CZ: case PI_CMD_PROCP: case PI_CMD_SERR: case PI_CMD_SLR: diff --git a/setup.py b/setup.py index b027567..0f3a7cc 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from distutils.core import setup setup(name='pigpio', - version='1.17', + version='1.18', author='joan', author_email='joan@abyz.co.uk', maintainer='joan', diff --git a/x_pigs b/x_pigs index e791e0b..fc12cf9 100755 --- a/x_pigs +++ b/x_pigs @@ -49,7 +49,7 @@ s=$(pigs bs2 0) if [[ $s = "" ]]; then echo "BS2 ok"; else echo "BS2 fail ($s)"; fi 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) if [[ $s -ne 0 ]]; then echo "HWVER ok"; else echo "HWVER fail ($s)"; fi diff --git a/x_pipe b/x_pipe index 0697b24..9f34589 100755 --- a/x_pipe +++ b/x_pipe @@ -56,14 +56,16 @@ if [[ $s = 0 ]]; then echo "BS2 ok"; else echo "BS2 fail ($s)"; fi echo "h" >/dev/pigpio read -t 1 s /dev/pigpio read -t 1 s