# HG changeset patch # User Paul Boddie # Date 1563922440 -7200 # Node ID 6a5b49213ca2737f6676ec342d31de841fb158a8 # Parent e60d54c4797f4d91ab148f625cc0e27a9c4ad82b Added some convenience functions and some tentative RTC device inspection. diff -r e60d54c4797f -r 6a5b49213ca2 pkg/landfall-examples/ci20_i2c/ci20_i2c.c --- a/pkg/landfall-examples/ci20_i2c/ci20_i2c.c Wed Jul 24 00:51:30 2019 +0200 +++ b/pkg/landfall-examples/ci20_i2c/ci20_i2c.c Wed Jul 24 00:54:00 2019 +0200 @@ -126,6 +126,34 @@ return jz4780_i2c_have_written(i2c_channel); } +static long i2c_get(void *i2c_channel, uint8_t reg, uint8_t *buffer, long length, l4_cap_idx_t irqcap) +{ + long pos; + + buffer[0] = reg; + i2c_write(i2c_channel, buffer, 1, irqcap); + + memset(buffer, 0, length); + pos = i2c_read(i2c_channel, buffer, length, irqcap); + + jz4780_i2c_stop(i2c_channel); + + return pos; +} + +static void i2c_report(void *i2c_channel, uint8_t *buffer, long length) +{ + if (!jz4780_i2c_failed(i2c_channel)) + { + printf("Read: "); + while (length--) + printf("%02x", *(buffer++)); + printf("\n"); + } + else + printf("Read failed.\n"); +} + static void i2c_scan(void *i2c_channel, l4_cap_idx_t irqcap) { uint8_t buf[1]; @@ -144,10 +172,7 @@ printf("\n"); jz4780_i2c_set_target(i2c_channel, address); - buf[0] = 0; - i2c_write(i2c_channel, buf, 1, irqcap); - i2c_read(i2c_channel, buf, 1, irqcap); - jz4780_i2c_stop(i2c_channel); + i2c_get(i2c_channel, 0, buf, 1, irqcap); if (!jz4780_i2c_failed(i2c_channel)) printf("%02x ", address); @@ -178,11 +203,7 @@ See: drivers/video/fbdev/core/fb_ddc.c */ jz4780_i2c_set_target(i2c_channel, 0x50); - buf[0] = 0; - i2c_write(i2c_channel, buf, 1, irqcap); - memset(buf, 0, 128); - pos = i2c_read(i2c_channel, buf, 128, irqcap); - jz4780_i2c_stop(i2c_channel); + pos = i2c_get(i2c_channel, 0, buf, 128, irqcap); if (pos <= 0) { @@ -209,15 +230,8 @@ /* Attempt to read from address 0x5a for PMIC OUT3. */ jz4780_i2c_set_target(i2c_channel, 0x5a); - buf[0] = 0x32; - i2c_write(i2c_channel, buf, 1, irqcap); - i2c_read(i2c_channel, buf, 1, irqcap); - jz4780_i2c_stop(i2c_channel); - - if (!jz4780_i2c_failed(i2c_channel)) - printf("Read: %02x\n", buf[0]); - else - printf("Read failed.\n"); + i2c_get(i2c_channel, 0x32, buf, 1, irqcap); + i2c_report(i2c_channel, buf, 1); #if 0 /* Disable the regulator. */ @@ -232,16 +246,22 @@ /* Read back from the register. Seemed to give 0xff, which makes no real sense, although the regulator did bring the voltage level low. */ - buf[0] = 0x32; - i2c_write(i2c_channel, buf, 1, irqcap); - i2c_read(i2c_channel, buf, 1, irqcap); - jz4780_i2c_stop(i2c_channel); + i2c_get(i2c_channel, 0x32, buf, 1, irqcap); + i2c_report(i2c_channel, buf, 1); +#endif +} - if (!jz4780_i2c_failed(i2c_channel)) - printf("Read: %02x\n", buf[0]); - else - printf("Read failed.\n"); -#endif +static void rtc_control(void *i2c_channel, l4_cap_idx_t irqcap) +{ + /* Buffer for communicating. */ + + uint8_t buf[1]; + + /* Attempt to read from address 0x51 for RTC. */ + + jz4780_i2c_set_target(i2c_channel, 0x51); + i2c_get(i2c_channel, 0x0f, buf, 1, irqcap); + i2c_report(i2c_channel, buf, 1); } int main(void) @@ -377,8 +397,8 @@ jz4780_gpio_config_pad(gpio_port_d, PMSCL, Function_alt, 0); jz4780_gpio_config_pad(gpio_port_d, PMSDA, Function_alt, 0); - jz4780_gpio_config_pad(gpio_port_e, RTCSCL, Function_alt, 1); - jz4780_gpio_config_pad(gpio_port_e, RTCSDA, Function_alt, 1); + //jz4780_gpio_config_pad(gpio_port_e, RTCSCL, Function_alt, 1); + //jz4780_gpio_config_pad(gpio_port_e, RTCSDA, Function_alt, 1); jz4780_gpio_config_pad(gpio_port_f, DDCSCL, Function_alt, 1); jz4780_gpio_config_pad(gpio_port_f, DDCSDA, Function_alt, 1); @@ -408,6 +428,8 @@ ddc_read(i2c4, irq4cap); + //rtc_control(i2c4, irq4cap); + jz4780_i2c_disable(i2c0); jz4780_i2c_disable(i2c4);