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