]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088/i2c_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC408X / TARGET_LPC4088 / i2c_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "mbed_assert.h"
17 #include "i2c_api.h"
18 #include "cmsis.h"
19 #include "pinmap.h"
20
21 static const PinMap PinMap_I2C_SDA[] = {
22 {P0_0 , I2C_1, 3},
23 {P0_10, I2C_2, 2},
24 {P0_19, I2C_1, 3},
25 {P0_27, I2C_0, 1},
26 {P1_15, I2C_2, 3},
27 {P1_30, I2C_0, 4},
28 {P2_14, I2C_1, 2},
29 {P2_30, I2C_2, 2},
30 {P4_20, I2C_2, 4},
31 {P5_2, I2C_0, 5},
32 {NC , NC , 0}
33 };
34
35 static const PinMap PinMap_I2C_SCL[] = {
36 {P0_1 , I2C_1, 3},
37 {P0_11, I2C_2, 2},
38 {P0_20, I2C_1, 3},
39 {P0_28, I2C_0, 1},
40 {P1_31, I2C_0, 4},
41 {P2_15, I2C_1, 2},
42 {P2_31, I2C_2, 2},
43 {P4_21, I2C_2, 2},
44 {P4_29, I2C_2, 4},
45 {P5_3, I2C_0, 5},
46 {NC , NC, 0}
47 };
48
49 #define I2C_CONSET(x) (x->i2c->CONSET)
50 #define I2C_CONCLR(x) (x->i2c->CONCLR)
51 #define I2C_STAT(x) (x->i2c->STAT)
52 #define I2C_DAT(x) (x->i2c->DAT)
53 #define I2C_SCLL(x, val) (x->i2c->SCLL = val)
54 #define I2C_SCLH(x, val) (x->i2c->SCLH = val)
55
56 static const uint32_t I2C_addr_offset[2][4] = {
57 {0x0C, 0x20, 0x24, 0x28},
58 {0x30, 0x34, 0x38, 0x3C}
59 };
60
61 static inline void i2c_conclr(i2c_t *obj, int start, int stop, int interrupt, int acknowledge) {
62 I2C_CONCLR(obj) = (start << 5)
63 | (stop << 4)
64 | (interrupt << 3)
65 | (acknowledge << 2);
66 }
67
68 static inline void i2c_conset(i2c_t *obj, int start, int stop, int interrupt, int acknowledge) {
69 I2C_CONSET(obj) = (start << 5)
70 | (stop << 4)
71 | (interrupt << 3)
72 | (acknowledge << 2);
73 }
74
75 // Clear the Serial Interrupt (SI)
76 static inline void i2c_clear_SI(i2c_t *obj) {
77 i2c_conclr(obj, 0, 0, 1, 0);
78 }
79
80 static inline int i2c_status(i2c_t *obj) {
81 return I2C_STAT(obj);
82 }
83
84 // Wait until the Serial Interrupt (SI) is set
85 static int i2c_wait_SI(i2c_t *obj) {
86 int timeout = 0;
87 while (!(I2C_CONSET(obj) & (1 << 3))) {
88 timeout++;
89 if (timeout > 100000) return -1;
90 }
91 return 0;
92 }
93
94 static inline void i2c_interface_enable(i2c_t *obj) {
95 I2C_CONSET(obj) = 0x40;
96 }
97
98 static inline void i2c_power_enable(i2c_t *obj) {
99 switch ((int)obj->i2c) {
100 case I2C_0: LPC_SC->PCONP |= 1 << 7; break;
101 case I2C_1: LPC_SC->PCONP |= 1 << 19; break;
102 case I2C_2: LPC_SC->PCONP |= 1 << 26; break;
103 }
104 }
105
106 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
107 // determine the SPI to use
108 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
109 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
110 obj->i2c = (LPC_I2C_TypeDef *)pinmap_merge(i2c_sda, i2c_scl);
111 MBED_ASSERT((int)obj->i2c != NC);
112
113 // enable power
114 i2c_power_enable(obj);
115
116 // set default frequency at 100k
117 i2c_frequency(obj, 100000);
118 i2c_conclr(obj, 1, 1, 1, 1);
119 i2c_interface_enable(obj);
120
121 pinmap_pinout(sda, PinMap_I2C_SDA);
122 pinmap_pinout(scl, PinMap_I2C_SCL);
123
124 // OpenDrain must explicitly be enabled for p0.0 and p0.1
125 if (sda == P0_0) {
126 pin_mode(sda, OpenDrain);
127 }
128 if (scl == P0_1) {
129 pin_mode(scl, OpenDrain);
130 }
131
132 }
133
134 inline int i2c_start(i2c_t *obj) {
135 int status = 0;
136 // 8.1 Before master mode can be entered, I2CON must be initialised to:
137 // - I2EN STA STO SI AA - -
138 // - 1 0 0 0 x - -
139 // if AA = 0, it can't enter slave mode
140 i2c_conclr(obj, 1, 1, 1, 1);
141
142 // The master mode may now be entered by setting the STA bit
143 // this will generate a start condition when the bus becomes free
144 i2c_conset(obj, 1, 0, 0, 1);
145
146 i2c_wait_SI(obj);
147 status = i2c_status(obj);
148
149 // Clear start bit now transmitted, and interrupt bit
150 i2c_conclr(obj, 1, 0, 0, 0);
151 return status;
152 }
153
154 inline int i2c_stop(i2c_t *obj) {
155 int timeout = 0;
156
157 // write the stop bit
158 i2c_conset(obj, 0, 1, 0, 0);
159 i2c_clear_SI(obj);
160
161 // wait for STO bit to reset
162 while(I2C_CONSET(obj) & (1 << 4)) {
163 timeout ++;
164 if (timeout > 100000) return 1;
165 }
166
167 return 0;
168 }
169
170
171 static inline int i2c_do_write(i2c_t *obj, int value, uint8_t addr) {
172 // write the data
173 I2C_DAT(obj) = value;
174
175 // clear SI to init a send
176 i2c_clear_SI(obj);
177
178 // wait and return status
179 i2c_wait_SI(obj);
180 return i2c_status(obj);
181 }
182
183 static inline int i2c_do_read(i2c_t *obj, int last) {
184 // we are in state 0x40 (SLA+R tx'd) or 0x50 (data rx'd and ack)
185 if(last) {
186 i2c_conclr(obj, 0, 0, 0, 1); // send a NOT ACK
187 } else {
188 i2c_conset(obj, 0, 0, 0, 1); // send a ACK
189 }
190
191 // accept byte
192 i2c_clear_SI(obj);
193
194 // wait for it to arrive
195 i2c_wait_SI(obj);
196
197 // return the data
198 return (I2C_DAT(obj) & 0xFF);
199 }
200
201 void i2c_frequency(i2c_t *obj, int hz) {
202 uint32_t PCLK = PeripheralClock;
203 uint32_t pulse = PCLK / (hz * 2);
204
205 // I2C Rate
206 I2C_SCLL(obj, pulse);
207 I2C_SCLH(obj, pulse);
208 }
209
210 // The I2C does a read or a write as a whole operation
211 // There are two types of error conditions it can encounter
212 // 1) it can not obtain the bus
213 // 2) it gets error responses at part of the transmission
214 //
215 // We tackle them as follows:
216 // 1) we retry until we get the bus. we could have a "timeout" if we can not get it
217 // which basically turns it in to a 2)
218 // 2) on error, we use the standard error mechanisms to report/debug
219 //
220 // Therefore an I2C transaction should always complete. If it doesn't it is usually
221 // because something is setup wrong (e.g. wiring), and we don't need to programatically
222 // check for that
223 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
224 int count, status;
225
226 status = i2c_start(obj);
227
228 if ((status != 0x10) && (status != 0x08)) {
229 i2c_stop(obj);
230 return I2C_ERROR_BUS_BUSY;
231 }
232
233 status = i2c_do_write(obj, (address | 0x01), 1);
234 if (status != 0x40) {
235 i2c_stop(obj);
236 return I2C_ERROR_NO_SLAVE;
237 }
238
239 // Read in all except last byte
240 for (count = 0; count < (length - 1); count++) {
241 int value = i2c_do_read(obj, 0);
242 status = i2c_status(obj);
243 if (status != 0x50) {
244 i2c_stop(obj);
245 return count;
246 }
247 data[count] = (char) value;
248 }
249
250 // read in last byte
251 int value = i2c_do_read(obj, 1);
252 status = i2c_status(obj);
253 if (status != 0x58) {
254 i2c_stop(obj);
255 return length - 1;
256 }
257
258 data[count] = (char) value;
259
260 // If not repeated start, send stop.
261 if (stop) {
262 i2c_stop(obj);
263 }
264
265 return length;
266 }
267
268 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
269 int i, status;
270
271 status = i2c_start(obj);
272
273 if ((status != 0x10) && (status != 0x08)) {
274 i2c_stop(obj);
275 return I2C_ERROR_BUS_BUSY;
276 }
277
278 status = i2c_do_write(obj, (address & 0xFE), 1);
279 if (status != 0x18) {
280 i2c_stop(obj);
281 return I2C_ERROR_NO_SLAVE;
282 }
283
284 for (i=0; i<length; i++) {
285 status = i2c_do_write(obj, data[i], 0);
286 if (status != 0x28) {
287 i2c_stop(obj);
288 return i;
289 }
290 }
291
292 // clearing the serial interrupt here might cause an unintended rewrite of the last byte
293 // see also issue report https://mbed.org/users/mbed_official/code/mbed/issues/1
294 // i2c_clear_SI(obj);
295
296 // If not repeated start, send stop.
297 if (stop) {
298 i2c_stop(obj);
299 }
300
301 return length;
302 }
303
304 void i2c_reset(i2c_t *obj) {
305 i2c_stop(obj);
306 }
307
308 int i2c_byte_read(i2c_t *obj, int last) {
309 return (i2c_do_read(obj, last) & 0xFF);
310 }
311
312 int i2c_byte_write(i2c_t *obj, int data) {
313 int ack;
314 int status = i2c_do_write(obj, (data & 0xFF), 0);
315
316 switch(status) {
317 case 0x18: case 0x28: // Master transmit ACKs
318 ack = 1;
319 break;
320
321 case 0x40: // Master receive address transmitted ACK
322 ack = 1;
323 break;
324
325 case 0xB8: // Slave transmit ACK
326 ack = 1;
327 break;
328
329 default:
330 ack = 0;
331 break;
332 }
333
334 return ack;
335 }
336
337 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
338 if (enable_slave != 0) {
339 i2c_conclr(obj, 1, 1, 1, 0);
340 i2c_conset(obj, 0, 0, 0, 1);
341 } else {
342 i2c_conclr(obj, 1, 1, 1, 1);
343 }
344 }
345
346 int i2c_slave_receive(i2c_t *obj) {
347 int status;
348 int retval;
349
350 status = i2c_status(obj);
351 switch(status) {
352 case 0x60: retval = 3; break;
353 case 0x70: retval = 2; break;
354 case 0xA8: retval = 1; break;
355 default : retval = 0; break;
356 }
357
358 return(retval);
359 }
360
361 int i2c_slave_read(i2c_t *obj, char *data, int length) {
362 int count = 0;
363 int status;
364
365 do {
366 i2c_clear_SI(obj);
367 i2c_wait_SI(obj);
368 status = i2c_status(obj);
369 if((status == 0x80) || (status == 0x90)) {
370 data[count] = I2C_DAT(obj) & 0xFF;
371 }
372 count++;
373 } while (((status == 0x80) || (status == 0x90) ||
374 (status == 0x060) || (status == 0x70)) && (count < length));
375
376 if(status != 0xA0) {
377 i2c_stop(obj);
378 }
379
380 i2c_clear_SI(obj);
381
382 return count;
383 }
384
385 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
386 int count = 0;
387 int status;
388
389 if(length <= 0) {
390 return(0);
391 }
392
393 do {
394 status = i2c_do_write(obj, data[count], 0);
395 count++;
396 } while ((count < length) && (status == 0xB8));
397
398 if((status != 0xC0) && (status != 0xC8)) {
399 i2c_stop(obj);
400 }
401
402 i2c_clear_SI(obj);
403
404 return(count);
405 }
406
407 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
408 uint32_t addr;
409
410 if ((idx >= 0) && (idx <= 3)) {
411 addr = ((uint32_t)obj->i2c) + I2C_addr_offset[0][idx];
412 *((uint32_t *) addr) = address & 0xFF;
413 addr = ((uint32_t)obj->i2c) + I2C_addr_offset[1][idx];
414 *((uint32_t *) addr) = mask & 0xFE;
415 }
416 }
Imprint / Impressum