diff options
-rw-r--r-- | firmware/drivers/i2c-h100.c | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/firmware/drivers/i2c-h100.c b/firmware/drivers/i2c-h100.c index e2fbea2573..c50cd7ed0a 100644 --- a/firmware/drivers/i2c-h100.c +++ b/firmware/drivers/i2c-h100.c | |||
@@ -70,19 +70,22 @@ void i2c_close(void) | |||
70 | int i2c_write(int device, unsigned char *buf, int count) | 70 | int i2c_write(int device, unsigned char *buf, int count) |
71 | { | 71 | { |
72 | int i; | 72 | int i; |
73 | int rc; | ||
73 | 74 | ||
74 | if (i2c_gen_start(device) == -1) | 75 | rc = i2c_gen_start(device); |
76 | if (rc < 0) | ||
75 | { | 77 | { |
76 | DEBUGF("i2c: gen_start failed (d=%d)", device); | 78 | DEBUGF("i2c: gen_start failed (d=%d)", device); |
77 | return -1; | 79 | return rc*10 - 1; |
78 | } | 80 | } |
79 | 81 | ||
80 | for (i=0; i<count; i++) | 82 | for (i=0; i<count; i++) |
81 | { | 83 | { |
82 | if (i2c_write_byte(device, buf[i]) == -1) | 84 | rc = i2c_write_byte(device, buf[i]); |
85 | if (rc < 0) | ||
83 | { | 86 | { |
84 | DEBUGF("i2c: write failed at (d=%d,i=%d)", device, i); | 87 | DEBUGF("i2c: write failed at (d=%d,i=%d)", device, i); |
85 | return i-1; | 88 | return rc*10 - 2; |
86 | } | 89 | } |
87 | } | 90 | } |
88 | 91 | ||
@@ -110,23 +113,25 @@ int i2c_write_byte(int device, unsigned char data) | |||
110 | if (count >= MAX_LOOP) | 113 | if (count >= MAX_LOOP) |
111 | return -1; | 114 | return -1; |
112 | 115 | ||
116 | count = 0; | ||
117 | |||
113 | /* Wait for interrupt flag */ | 118 | /* Wait for interrupt flag */ |
114 | while (!(regs[O_MBSR] & IFF) && count < MAX_LOOP) | 119 | while (!(regs[O_MBSR] & IFF) && count < MAX_LOOP) |
115 | { | 120 | { |
116 | yield(); | 121 | yield(); |
117 | count++; | 122 | count++; |
118 | } | 123 | } |
119 | 124 | ||
120 | if (count >= MAX_LOOP) | 125 | if (count >= MAX_LOOP) |
121 | return -1; | 126 | return -2; |
122 | 127 | ||
123 | regs[O_MBSR] &= ~IFF; /* Clear interrupt flag */ | 128 | regs[O_MBSR] &= ~IFF; /* Clear interrupt flag */ |
124 | 129 | ||
125 | if (!(regs[O_MBSR] & ICF)) /* Check that transfer is complete */ | 130 | if (!(regs[O_MBSR] & ICF)) /* Check that transfer is complete */ |
126 | return -1; | 131 | return -3; |
127 | 132 | ||
128 | if (regs[O_MBSR] & RXAK) /* Check that the byte has been ACKed */ | 133 | if (regs[O_MBSR] & RXAK) /* Check that the byte has been ACKed */ |
129 | return -1; | 134 | return -4; |
130 | 135 | ||
131 | return 0; | 136 | return 0; |
132 | } | 137 | } |
@@ -140,14 +145,17 @@ int i2c_gen_start(int device) | |||
140 | 145 | ||
141 | /* Wait for bus to become free */ | 146 | /* Wait for bus to become free */ |
142 | while ((regs[O_MBSR] & IBB) && (count < MAX_LOOP)) | 147 | while ((regs[O_MBSR] & IBB) && (count < MAX_LOOP)) |
148 | { | ||
149 | yield(); | ||
143 | count++; | 150 | count++; |
151 | } | ||
144 | 152 | ||
145 | if (count >= MAX_LOOP) | 153 | if (count >= MAX_LOOP) |
146 | return -1; | 154 | return -1; |
147 | 155 | ||
148 | regs[O_MBCR] |= MSTA | MTX; /* Generate START */ | 156 | regs[O_MBCR] |= MSTA | MTX; /* Generate START */ |
149 | 157 | ||
150 | return 0; | 158 | return 0; |
151 | } | 159 | } |
152 | 160 | ||
153 | void i2c_gen_stop(int device) | 161 | void i2c_gen_stop(int device) |