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