From 871b245d4d603944b9a340dea478e9c5e501c4e8 Mon Sep 17 00:00:00 2001 From: Ludwig Maier Date: Sun, 16 Feb 2014 02:05:53 +0100 Subject: [PATCH] trb_i2c rc mode added, i.e. read number of registers in a row --- trbrich/trb_i2c.c | 116 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 110 insertions(+), 6 deletions(-) diff --git a/trbrich/trb_i2c.c b/trbrich/trb_i2c.c index ed45386..fc66485 100644 --- a/trbrich/trb_i2c.c +++ b/trbrich/trb_i2c.c @@ -37,11 +37,15 @@ void usage(const char *progName) printf(" -d turn on Debugging Information\n"); printf(" -V Version number\n"); printf("\nCommands:\n"); - printf(" w -> " + printf(" w -> " "write to \n"); - printf(" r -> " - "read from \n"); - printf(" c -> " + printf(" r -> " + "read from \n"); + printf(" rc -> " + "read \n" + " " + "starting at \n"); + printf(" c -> " "clear I2C-Bus on \n"); } @@ -258,8 +262,8 @@ int main(int argc, char** argv) } free(buffer); + } else if (strcmp(argv[optind], "r") == 0) { - /*************************************************/ /* I2C read */ /*************************************************/ @@ -338,7 +342,107 @@ int main(int argc, char** argv) } } - free(buffer); + free(buffer); + + } else if (strcmp(argv[optind], "rc") == 0) { + + /*************************************************/ + /* I2C read continuous */ + /*************************************************/ + + uint32_t *buffer = NULL; + unsigned int reg_counter = 0; + int status = 0; + uint16_t trb_address = 0; + uint32_t value = 0; + uint8_t i2c_chip = 0; + uint16_t i2c_register = 0; + uint16_t i2c_num_regs = 1; + + int i; + + if (argc - optind != 5) { + usage(argv[0]); + exit(EXIT_FAILURE); + } + + trb_address = (uint16_t)strtoul(argv[optind + 1], NULL, 0); + i2c_chip = spi_mode == 0 ? (uint8_t)strtoul(argv[optind + 2], NULL, 0) : 0; + i2c_register = spi_mode == 0 + ? (uint8_t)strtoul(argv[optind + 3], NULL, 0) + : (uint16_t)strtoul(argv[optind + 3], NULL, 0) & 0x1fff; + i2c_num_regs = (uint16_t)strtoul(argv[optind + 4], NULL, 0); + + + + for (reg_counter = 0; reg_counter < i2c_num_regs; reg_counter++) { + + buffer = (uint32_t*)malloc(sizeof(uint32_t) * BUFFER_SIZE); + if (buffer == NULL) abort(); + + /* Check whether I2C is ready */ + if (readI2CRegister(trb_address, 0, buffer, BUFFER_SIZE) == -1) { + trb_error("I2C not ready"); + free(buffer); + exit(EXIT_FAILURE); + } + + /* Read Value */ + if (spi_mode == 0) { + value = multBytes == 0 + ? (0xff000000 | (i2c_chip << 16) | + ((i2c_register + reg_counter) << 8)) + : (0xc0000000 | (num_bytes << 24) | + (i2c_chip << 16) | ((i2c_register + reg_counter) << 8)); + } else { + value = (0xff000000 | (i2c_register << 8)); + } + + if (trb_register_write(trb_address, trb_i2c_register, value) == -1) { + trb_error("Error reading value"); + clearI2C(trb_address); + free(buffer); + exit(EXIT_FAILURE); + } + + /* Wait for ACK */ + if ((status = readI2CRegister(trb_address, 1, buffer, BUFFER_SIZE)) + == -1) { + clearI2C(trb_address); + free(buffer); + exit(EXIT_FAILURE); + } + + /* Clear IC2 bus */ + if (clearI2C(trb_address) == -1) { + free(buffer); + exit(EXIT_FAILURE); + } + + /* Print results */ + for (i = 0; i < status; i += 2) { + if (multBytes == 0) { + fprintf(stdout, + (decMode == 0 ? + "0x%04x 0x%02x 0x%02x\n" : + "0x%04x %u %u\n"), + buffer[i], + (i2c_register + reg_counter), + buffer[i + 1] & 0xff); + } else { + fprintf(stdout, + (decMode == 0 ? + "0x%04x 0x%02x 0x%08x\n" : + "0x%04x %u %u\n"), + buffer[i], + (i2c_register + reg_counter), + buffer[i + 1]); + } + } + + free(buffer); + } + } else if (strcmp(argv[optind], "c") == 0) { /*************************************************/ -- 2.43.0