# HG changeset patch # User Paul Boddie # Date 1714856008 -7200 # Node ID 135649724e29583bcdfe0b6d61c06aea34c01e9e # Parent 6f492d79dbc0d9e61af3bf7c06256371e20c7f65 Added an I2C set operation. diff -r 6f492d79dbc0 -r 135649724e29 pkg/landfall-examples/hw_info/hw_info.c --- a/pkg/landfall-examples/hw_info/hw_info.c Sat May 04 22:52:55 2024 +0200 +++ b/pkg/landfall-examples/hw_info/hw_info.c Sat May 04 22:53:28 2024 +0200 @@ -1009,6 +1009,42 @@ printf("\n"); } +static void i2c_set(void) +{ + void *channel; + int num; + uint8_t buffer[32]; + unsigned int address, reg, value; + + channel = get_channel(num_i2c_channels, i2c_channels, &num); + + if (channel == NULL) + return; + + if (!read_encoded_number("Address", "%2x", &address)) + return; + + if (address >= 0x80) + { + printf("Address must be less than 80.\n"); + return; + } + + if (!read_encoded_number("Register", "%2x", ®)) + return; + + if (!read_encoded_number("Value", "%2x", &value)) + return; + + buffer[0] = (uint8_t) (reg & 0xff); + buffer[1] = (uint8_t) (value & 0xff); + + i2c_set_target(channel, address); + + if (!_i2c_write(channel, buffer, 2, 1, i2c_irqs[num], 1000000)) + printf("Register write failed.\n"); +} + static void i2c_scan(void) { void *channel; @@ -1820,10 +1856,12 @@ new_i2c_channel(i2c); else if (!strcmp(token, "g") || !strcmp(token, "get")) i2c_get(); - else if (!strcmp(token, "s") || !strcmp(token, "scan")) + else if (!strcmp(token, "S") || !strcmp(token, "scan")) i2c_scan(); + else if (!strcmp(token, "s") || !strcmp(token, "set")) + i2c_set(); else - printf("i2c channel | get | list | scan\n"); + printf("i2c channel | get | list | scan | set\n"); } else list_i2c_channels();