Changeset 5237

Show
Ignore:
Timestamp:
05/05/08 14:38:22 (4 years ago)
Author:
khali
Message:

Add support for short writes (SMBus send byte).

Location:
i2c-tools/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • i2c-tools/trunk/CHANGES

    r5209 r5237  
    1515          Restrict the chip address to 0x03-0x77 
    1616          Split the code into several functions for clarity 
     17          Add support for short writes (SMBus send byte) 
    1718 
    18193.0.1 (2008-04-04) 
  • i2c-tools/trunk/tools/i2cset.8

    r5236 r5237  
    1010.I chip-address 
    1111.I data-address 
    12 .I value 
    13 .RI [ "mode " [ mask ]] 
     12.RI [ "value " [ "mode " [ mask ]]] 
    1413.br 
    1514.B i2cset 
     
    3938scripts. 
    4039.PP 
    41 There are four required options to i2cset. \fIi2cbus\fR indicates the number 
     40There are three required options to i2cset. \fIi2cbus\fR indicates the number 
    4241or name of the I2C bus to be scanned.  This number should correspond to one of 
    4342the busses listed by \fIi2cdetect -l\fR. \fIchip-address\fR specifies the 
    4443address of the chip on that bus, and is an integer between 0x03 and 0x77. 
    4544\fIdata-address\fR specifies the address on that chip to write to, and is an 
    46 integer between 0x00 and 0xFF. \fIvalue\fR is the value to write to that 
    47 location on the chip. 
     45integer between 0x00 and 0xFF. 
     46.PP 
     47The \fIvalue\fR parameter, if specified, is the value to write to that 
     48location on the chip. If this parameter is omited, then a short write is 
     49issued. For most chips, it simply sets an internal pointer to the target 
     50location, but doesn't actually write to that location. For a few chips 
     51though, in particular simple ones with a single register, this short write 
     52is an actual write. 
    4853.PP 
    4954The \fImode\fR parameter, if specified, is one of the letters \fBb\fP or 
  • i2c-tools/trunk/tools/i2cset.c

    r5236 r5237  
    3636{ 
    3737        fprintf(stderr, 
    38                 "Usage: i2cset [-f] [-y] I2CBUS CHIP-ADDRESS DATA-ADDRESS VALUE [MODE [MASK]]\n" 
     38                "Usage: i2cset [-f] [-y] I2CBUS CHIP-ADDRESS DATA-ADDRESS [VALUE [MODE [MASK]]]\n" 
    3939                "  I2CBUS is an integer or an I2C bus name\n" 
    4040                "  ADDRESS is an integer (0x03 - 0x77)\n" 
     
    5858 
    5959        switch (size) { 
     60        case I2C_SMBUS_BYTE: 
     61                if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE)) { 
     62                        fprintf(stderr, "Error: Adapter for i2c bus %d does " 
     63                                "not have byte send capability\n", i2cbus); 
     64                        return -1; 
     65                } 
     66                break; 
     67 
    6068        case I2C_SMBUS_BYTE_DATA: 
    6169                if (!(funcs & I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) { 
     
    101109 
    102110        fprintf(stderr, "I will write to device file %s, chip address " 
    103                 "0x%02x, data address\n0x%02x, data 0x%02x%s, mode " 
    104                 "%s.\n", filename, address, daddress, value, 
    105                 vmask ? " (masked)" : "", 
    106                 size == I2C_SMBUS_BYTE_DATA ? "byte" : "word"); 
     111                "0x%02x, data address\n0x%02x, ", filename, address, daddress); 
     112        if (size == I2C_SMBUS_BYTE) 
     113                fprintf(stderr, "no data.\n"); 
     114        else 
     115                fprintf(stderr, "data 0x%02x%s, mode %s.\n", value, 
     116                        vmask ? " (masked)" : "", 
     117                        size == I2C_SMBUS_BYTE_DATA ? "byte" : "word"); 
    107118        if (pec) 
    108119                fprintf(stderr, "PEC checking enabled.\n"); 
     
    148159        } 
    149160 
    150         if (argc < flags + 5) 
     161        if (argc < flags + 4) 
    151162                help(); 
    152163 
     
    165176        } 
    166177 
    167         value = strtol(argv[flags+4], &end, 0); 
    168         if (*end) { 
    169                 fprintf(stderr, "Error: Data value invalid!\n"); 
    170                 help(); 
     178        if (argc > flags + 4) { 
     179                size = I2C_SMBUS_BYTE_DATA; 
     180                value = strtol(argv[flags+4], &end, 0); 
     181                if (*end || value < 0) { 
     182                        fprintf(stderr, "Error: Data value invalid!\n"); 
     183                        help(); 
     184                } 
     185        } else { 
     186                size = I2C_SMBUS_BYTE; 
     187                value = -1; 
    171188        } 
    172189 
     
    180197                } 
    181198                pec = argv[flags+5][1] == 'p'; 
    182         } else { 
    183                 size = I2C_SMBUS_BYTE_DATA; 
    184199        } 
    185200 
     
    192207        } 
    193208 
    194         if (value < 0 
    195          || (size == I2C_SMBUS_BYTE_DATA && value > 0xff) 
     209        if ((size == I2C_SMBUS_BYTE_DATA && value > 0xff) 
    196210         || (size == I2C_SMBUS_WORD_DATA && value > 0xffff)) { 
    197211                fprintf(stderr, "Error: Data value out of range!\n"); 
     
    250264        } 
    251265 
    252         if (size == I2C_SMBUS_WORD_DATA) { 
     266        switch (size) { 
     267        case I2C_SMBUS_BYTE: 
     268                res = i2c_smbus_write_byte(file, daddress); 
     269                break; 
     270        case I2C_SMBUS_WORD_DATA: 
    253271                res = i2c_smbus_write_word_data(file, daddress, value); 
    254         } else { 
     272                break; 
     273        default: /* I2C_SMBUS_BYTE_DATA */ 
    255274                res = i2c_smbus_write_byte_data(file, daddress, value); 
    256275        } 
     
    270289        } 
    271290 
    272         if (size == I2C_SMBUS_WORD_DATA) { 
     291        switch (size) { 
     292        case I2C_SMBUS_BYTE: 
     293                /* No readback */ 
     294                break; 
     295        case I2C_SMBUS_WORD_DATA: 
    273296                res = i2c_smbus_read_word_data(file, daddress); 
    274         } else { 
     297                break; 
     298        default: /* I2C_SMBUS_BYTE_DATA */ 
    275299                res = i2c_smbus_read_byte_data(file, daddress); 
    276300        } 
    277301        close(file); 
     302 
     303        if (size == I2C_SMBUS_BYTE) /* We're done */ 
     304                exit(0); 
    278305 
    279306        if (res < 0) {