]>
git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/i2c_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "mbed_assert.h"
20 #include "PeripheralPins.h" // For the Peripheral to Pin Definitions found in the individual Target's Platform
22 #define I2C_CONSET(x) (x->i2c->CONSET)
23 #define I2C_CONCLR(x) (x->i2c->CONCLR)
24 #define I2C_STAT(x) (x->i2c->STAT)
25 #define I2C_DAT(x) (x->i2c->DAT)
26 #define I2C_SCLL(x, val) (x->i2c->SCLL = val)
27 #define I2C_SCLH(x, val) (x->i2c->SCLH = val)
29 static const uint32_t I2C_addr_offset
[2][4] = {
30 {0x0C, 0x20, 0x24, 0x28},
31 {0x30, 0x34, 0x38, 0x3C}
34 static inline void i2c_conclr(i2c_t
*obj
, int start
, int stop
, int interrupt
, int acknowledge
) {
35 I2C_CONCLR(obj
) = (start
<< 5)
41 static inline void i2c_conset(i2c_t
*obj
, int start
, int stop
, int interrupt
, int acknowledge
) {
42 I2C_CONSET(obj
) = (start
<< 5)
48 // Clear the Serial Interrupt (SI)
49 static inline void i2c_clear_SI(i2c_t
*obj
) {
50 i2c_conclr(obj
, 0, 0, 1, 0);
53 static inline int i2c_status(i2c_t
*obj
) {
57 // Wait until the Serial Interrupt (SI) is set
58 static int i2c_wait_SI(i2c_t
*obj
) {
60 while (!(I2C_CONSET(obj
) & (1 << 3))) {
62 if (timeout
> 100000) return -1;
67 static inline void i2c_interface_enable(i2c_t
*obj
) {
68 I2C_CONSET(obj
) = 0x40;
71 static inline void i2c_power_enable(i2c_t
*obj
) {
72 LPC_SYSCON
->SYSAHBCLKCTRL
|= (1 << 5);
73 LPC_SYSCON
->PRESETCTRL
|= 1 << 1;
76 void i2c_init(i2c_t
*obj
, PinName sda
, PinName scl
) {
77 // determine the SPI to use
78 I2CName i2c_sda
= (I2CName
)pinmap_peripheral(sda
, PinMap_I2C_SDA
);
79 I2CName i2c_scl
= (I2CName
)pinmap_peripheral(scl
, PinMap_I2C_SCL
);
80 obj
->i2c
= (LPC_I2C_Type
*)pinmap_merge(i2c_sda
, i2c_scl
);
81 MBED_ASSERT((int)obj
->i2c
!= NC
);
84 i2c_power_enable(obj
);
86 // set default frequency at 100k
87 i2c_frequency(obj
, 100000);
88 i2c_conclr(obj
, 1, 1, 1, 1);
89 i2c_interface_enable(obj
);
91 pinmap_pinout(sda
, PinMap_I2C_SDA
);
92 pinmap_pinout(scl
, PinMap_I2C_SCL
);
95 inline int i2c_start(i2c_t
*obj
) {
97 // 8.1 Before master mode can be entered, I2CON must be initialised to:
98 // - I2EN STA STO SI AA - -
100 // if AA = 0, it can't enter slave mode
101 i2c_conclr(obj
, 1, 1, 1, 1);
103 // The master mode may now be entered by setting the STA bit
104 // this will generate a start condition when the bus becomes free
105 i2c_conset(obj
, 1, 0, 0, 1);
108 status
= i2c_status(obj
);
110 // Clear start bit now transmitted, and interrupt bit
111 i2c_conclr(obj
, 1, 0, 0, 0);
115 inline int i2c_stop(i2c_t
*obj
) {
118 // write the stop bit
119 i2c_conset(obj
, 0, 1, 0, 0);
122 // wait for STO bit to reset
123 while(I2C_CONSET(obj
) & (1 << 4)) {
125 if (timeout
> 100000) return 1;
132 static inline int i2c_do_write(i2c_t
*obj
, int value
, uint8_t addr
) {
134 I2C_DAT(obj
) = value
;
136 // clear SI to init a send
139 // wait and return status
141 return i2c_status(obj
);
144 static inline int i2c_do_read(i2c_t
*obj
, int last
) {
145 // we are in state 0x40 (SLA+R tx'd) or 0x50 (data rx'd and ack)
147 i2c_conclr(obj
, 0, 0, 0, 1); // send a NOT ACK
149 i2c_conset(obj
, 0, 0, 0, 1); // send a ACK
155 // wait for it to arrive
159 return (I2C_DAT(obj
) & 0xFF);
162 void i2c_frequency(i2c_t
*obj
, int hz
) {
163 // No peripheral clock divider on the M0
164 uint32_t PCLK
= SystemCoreClock
;
166 uint32_t pulse
= PCLK
/ (hz
* 2);
169 I2C_SCLL(obj
, pulse
);
170 I2C_SCLH(obj
, pulse
);
173 // The I2C does a read or a write as a whole operation
174 // There are two types of error conditions it can encounter
175 // 1) it can not obtain the bus
176 // 2) it gets error responses at part of the transmission
178 // We tackle them as follows:
179 // 1) we retry until we get the bus. we could have a "timeout" if we can not get it
180 // which basically turns it in to a 2)
181 // 2) on error, we use the standard error mechanisms to report/debug
183 // Therefore an I2C transaction should always complete. If it doesn't it is usually
184 // because something is setup wrong (e.g. wiring), and we don't need to programatically
187 int i2c_read(i2c_t
*obj
, int address
, char *data
, int length
, int stop
) {
190 status
= i2c_start(obj
);
192 if ((status
!= 0x10) && (status
!= 0x08)) {
194 return I2C_ERROR_BUS_BUSY
;
197 status
= i2c_do_write(obj
, (address
| 0x01), 1);
198 if (status
!= 0x40) {
200 return I2C_ERROR_NO_SLAVE
;
203 // Read in all except last byte
204 for (count
= 0; count
< (length
- 1); count
++) {
205 int value
= i2c_do_read(obj
, 0);
206 status
= i2c_status(obj
);
207 if (status
!= 0x50) {
211 data
[count
] = (char) value
;
215 int value
= i2c_do_read(obj
, 1);
216 status
= i2c_status(obj
);
217 if (status
!= 0x58) {
222 data
[count
] = (char) value
;
224 // If not repeated start, send stop.
232 int i2c_write(i2c_t
*obj
, int address
, const char *data
, int length
, int stop
) {
235 status
= i2c_start(obj
);
237 if ((status
!= 0x10) && (status
!= 0x08)) {
239 return I2C_ERROR_BUS_BUSY
;
242 status
= i2c_do_write(obj
, (address
& 0xFE), 1);
243 if (status
!= 0x18) {
245 return I2C_ERROR_NO_SLAVE
;
248 for (i
=0; i
<length
; i
++) {
249 status
= i2c_do_write(obj
, data
[i
], 0);
256 // clearing the serial interrupt here might cause an unintended rewrite of the last byte
257 // see also issue report https://mbed.org/users/mbed_official/code/mbed/issues/1
258 // i2c_clear_SI(obj);
260 // If not repeated start, send stop.
268 void i2c_reset(i2c_t
*obj
) {
272 int i2c_byte_read(i2c_t
*obj
, int last
) {
273 return (i2c_do_read(obj
, last
) & 0xFF);
276 int i2c_byte_write(i2c_t
*obj
, int data
) {
278 int status
= i2c_do_write(obj
, (data
& 0xFF), 0);
281 case 0x18: case 0x28: // Master transmit ACKs
284 case 0x40: // Master receive address transmitted ACK
287 case 0xB8: // Slave transmit ACK
298 void i2c_slave_mode(i2c_t
*obj
, int enable_slave
) {
299 if (enable_slave
!= 0) {
300 i2c_conclr(obj
, 1, 1, 1, 0);
301 i2c_conset(obj
, 0, 0, 0, 1);
303 i2c_conclr(obj
, 1, 1, 1, 1);
307 int i2c_slave_receive(i2c_t
*obj
) {
311 status
= i2c_status(obj
);
313 case 0x60: retval
= 3; break;
314 case 0x70: retval
= 2; break;
315 case 0xA8: retval
= 1; break;
316 default : retval
= 0; break;
322 int i2c_slave_read(i2c_t
*obj
, char *data
, int length
) {
329 status
= i2c_status(obj
);
330 if((status
== 0x80) || (status
== 0x90)) {
331 data
[count
] = I2C_DAT(obj
) & 0xFF;
334 } while (((status
== 0x80) || (status
== 0x90) ||
335 (status
== 0x060) || (status
== 0x70)) && (count
< length
));
346 int i2c_slave_write(i2c_t
*obj
, const char *data
, int length
) {
355 status
= i2c_do_write(obj
, data
[count
], 0);
357 } while ((count
< length
) && (status
== 0xB8));
359 if((status
!= 0xC0) && (status
!= 0xC8)) {
368 void i2c_slave_address(i2c_t
*obj
, int idx
, uint32_t address
, uint32_t mask
) {
371 if ((idx
>= 0) && (idx
<= 3)) {
372 addr
= ((uint32_t)obj
->i2c
) + I2C_addr_offset
[0][idx
];
373 *((uint32_t *) addr
) = address
& 0xFF;