1.1 --- a/pkg/landfall-examples/ci20_i2c/ci20_i2c.c Sun Jul 21 22:23:06 2019 +0200
1.2 +++ b/pkg/landfall-examples/ci20_i2c/ci20_i2c.c Mon Jul 22 00:17:30 2019 +0200
1.3 @@ -28,6 +28,7 @@
1.4 #include <stdio.h>
1.5 #include <unistd.h>
1.6 #include <stdint.h>
1.7 +#include <string.h>
1.8
1.9
1.10
1.11 @@ -77,8 +78,8 @@
1.12 printf("No reply from bus.\n");
1.13 }
1.14
1.15 -static unsigned int i2c_read(void *i2c_channel, uint8_t *buf, unsigned length,
1.16 - l4_cap_idx_t irqcap)
1.17 +static long i2c_read(void *i2c_channel, uint8_t *buf, unsigned length,
1.18 + l4_cap_idx_t irqcap)
1.19 {
1.20 l4_msgtag_t tag;
1.21 long err;
1.22 @@ -87,30 +88,46 @@
1.23
1.24 while (!jz4780_i2c_read_done(i2c_channel))
1.25 {
1.26 - if (jz4780_i2c_read_incomplete(i2c_channel))
1.27 - {
1.28 - printf("Failed\n");
1.29 - break;
1.30 - }
1.31 -
1.32 tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_rel(1, 20)));
1.33
1.34 if ((err = l4_ipc_error(tag, l4_utcb())))
1.35 - {
1.36 - printf("Error on IRQ receive: %ld\n", err);
1.37 - break;
1.38 - }
1.39 + return err;
1.40 +
1.41 + if (jz4780_i2c_failed(i2c_channel))
1.42 + return 0;
1.43
1.44 jz4780_i2c_read(i2c_channel);
1.45 }
1.46
1.47 - jz4780_i2c_stop(i2c_channel);
1.48 return jz4780_i2c_have_read(i2c_channel);
1.49 }
1.50
1.51 +static long i2c_write(void *i2c_channel, uint8_t *buf, unsigned length,
1.52 + l4_cap_idx_t irqcap)
1.53 +{
1.54 + l4_msgtag_t tag;
1.55 + long err;
1.56 +
1.57 + jz4780_i2c_start_write(i2c_channel, buf, length);
1.58 +
1.59 + while (!jz4780_i2c_write_done(i2c_channel))
1.60 + {
1.61 + tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_rel(1, 20)));
1.62 +
1.63 + if ((err = l4_ipc_error(tag, l4_utcb())))
1.64 + return err;
1.65 +
1.66 + if (jz4780_i2c_failed(i2c_channel))
1.67 + return 0;
1.68 +
1.69 + jz4780_i2c_write(i2c_channel);
1.70 + }
1.71 +
1.72 + return jz4780_i2c_have_written(i2c_channel);
1.73 +}
1.74 +
1.75 static void i2c_scan(void *i2c_channel, l4_cap_idx_t irqcap)
1.76 {
1.77 - l4_msgtag_t tag;
1.78 uint8_t buf[1];
1.79 unsigned int address;
1.80
1.81 @@ -128,25 +145,11 @@
1.82
1.83 jz4780_i2c_set_target(i2c_channel, address);
1.84 buf[0] = 0;
1.85 - jz4780_i2c_write(i2c_channel, buf, 1);
1.86 - jz4780_i2c_start_read(i2c_channel, buf, 1);
1.87 -
1.88 - while (!jz4780_i2c_read_done(i2c_channel))
1.89 - {
1.90 - if (jz4780_i2c_read_incomplete(i2c_channel))
1.91 - break;
1.92 -
1.93 - tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_rel(2, 10)));
1.94 -
1.95 - if (l4_ipc_error(tag, l4_utcb()))
1.96 - break;
1.97 -
1.98 - jz4780_i2c_read(i2c_channel);
1.99 - }
1.100 -
1.101 + i2c_write(i2c_channel, buf, 1, irqcap);
1.102 + i2c_read(i2c_channel, buf, 1, irqcap);
1.103 jz4780_i2c_stop(i2c_channel);
1.104
1.105 - if (jz4780_i2c_have_read(i2c_channel))
1.106 + if (!jz4780_i2c_failed(i2c_channel))
1.107 printf("%02x ", address);
1.108 else
1.109 printf("?? ");
1.110 @@ -163,7 +166,7 @@
1.111 /* Buffer for reading. */
1.112
1.113 uint8_t buf[128];
1.114 - unsigned int pos;
1.115 + long pos;
1.116
1.117 /* DDC EDID details. */
1.118
1.119 @@ -176,11 +179,17 @@
1.120
1.121 jz4780_i2c_set_target(i2c_channel, 0x50);
1.122 buf[0] = 0;
1.123 - jz4780_i2c_write(i2c_channel, buf, 1);
1.124 + i2c_write(i2c_channel, buf, 1, irqcap);
1.125 + memset(buf, 0, 128);
1.126 + pos = i2c_read(i2c_channel, buf, 128, irqcap);
1.127 + jz4780_i2c_stop(i2c_channel);
1.128
1.129 - printf("Waiting...\n");
1.130 + if (pos <= 0)
1.131 + {
1.132 + printf("Read failed.\n");
1.133 + return;
1.134 + }
1.135
1.136 - pos = i2c_read(i2c_channel, buf, 128, irqcap);
1.137 show_data(buf, pos);
1.138
1.139 /* Attempt to decode EDID information. */
1.140 @@ -201,25 +210,37 @@
1.141
1.142 jz4780_i2c_set_target(i2c_channel, 0x5a);
1.143 buf[0] = 0x32;
1.144 - jz4780_i2c_write(i2c_channel, buf, 1);
1.145 + i2c_write(i2c_channel, buf, 1, irqcap);
1.146 i2c_read(i2c_channel, buf, 1, irqcap);
1.147 - printf("Read %02x\n", buf[0]);
1.148 + jz4780_i2c_stop(i2c_channel);
1.149 +
1.150 + if (!jz4780_i2c_failed(i2c_channel))
1.151 + printf("Read: %02x\n", buf[0]);
1.152 + else
1.153 + printf("Read failed.\n");
1.154
1.155 #if 0
1.156 /* Disable the regulator. */
1.157
1.158 printf("Updating...\n");
1.159 +
1.160 buf[1] = buf[0] & ~0x80;
1.161 buf[0] = 0x32;
1.162 - jz4780_i2c_write(i2c_channel, buf, 2);
1.163 + i2c_write(i2c_channel, buf, 2, irqcap);
1.164 + jz4780_i2c_stop(i2c_channel);
1.165
1.166 /* Read back from the register. Seemed to give 0xff, which makes no real
1.167 sense, although the regulator did bring the voltage level low. */
1.168
1.169 buf[0] = 0x32;
1.170 - jz4780_i2c_write(i2c_channel, buf, 1);
1.171 + i2c_write(i2c_channel, buf, 1, irqcap);
1.172 i2c_read(i2c_channel, buf, 1, irqcap);
1.173 - printf("Read %02x\n", buf[0]);
1.174 + jz4780_i2c_stop(i2c_channel);
1.175 +
1.176 + if (!jz4780_i2c_failed(i2c_channel))
1.177 + printf("Read: %02x\n", buf[0]);
1.178 + else
1.179 + printf("Read failed.\n");
1.180 #endif
1.181 }
1.182