]>
git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/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"
21 static uint8_t repeated_start
= 0;
23 #define I2C_STAT(x) ((LPC_I2C0->STAT >> 1) & (0x07))
25 static inline int i2c_status(i2c_t
*obj
) {
29 // Wait until the Serial Interrupt (SI) is set
30 static int i2c_wait_SI(i2c_t
*obj
) {
31 volatile int timeout
= 0;
32 while (!(LPC_I2C0
->STAT
& (1 << 0))) {
34 if (timeout
> 100000) return -1;
39 static inline void i2c_interface_enable(i2c_t
*obj
) {
40 LPC_I2C0
->CFG
|= (1 << 0);
43 void i2c_init(i2c_t
*obj
, PinName sda
, PinName scl
) {
44 MBED_ASSERT((sda
== P0_23
) && (scl
== P0_22
));
46 // Enables clock for I2C0
47 LPC_SYSCON
->SYSAHBCLKCTRL1
|= (1 << 13);
49 LPC_SYSCON
->PRESETCTRL1
|= (1 << 13);
50 LPC_SYSCON
->PRESETCTRL1
&= ~(1 << 13);
53 LPC_SWM
->PINENABLE1
&= ~(0x3 << 3);
55 // set default frequency at 100kHz
56 i2c_frequency(obj
, 100000);
57 i2c_interface_enable(obj
);
60 inline int i2c_start(i2c_t
*obj
) {
63 LPC_I2C0
->MSTCTL
= (1 << 1) | (1 << 0);
66 LPC_I2C0
->MSTCTL
= (1 << 1);
71 inline int i2c_stop(i2c_t
*obj
) {
72 volatile int timeout
= 0;
74 LPC_I2C0
->MSTCTL
= (1 << 2) | (1 << 0);
75 while ((LPC_I2C0
->STAT
& ((1 << 0) | (7 << 1))) != ((1 << 0) | (0 << 1))) {
77 if (timeout
> 100000) return 1;
84 static inline int i2c_do_write(i2c_t
*obj
, int value
, uint8_t addr
) {
86 LPC_I2C0
->MSTDAT
= value
;
89 LPC_I2C0
->MSTCTL
= (1 << 0);
91 // wait and return status
93 return i2c_status(obj
);
96 static inline int i2c_do_read(i2c_t
*obj
, int last
) {
97 // wait for it to arrive
100 LPC_I2C0
->MSTCTL
= (1 << 0);
103 return (LPC_I2C0
->MSTDAT
& 0xFF);
106 void i2c_frequency(i2c_t
*obj
, int hz
) {
107 // No peripheral clock divider on the M0
108 uint32_t PCLK
= SystemCoreClock
;
109 uint32_t clkdiv
= PCLK
/ (hz
* 4) - 1;
111 LPC_I2C0
->DIV
= clkdiv
;
112 LPC_I2C0
->MSTTIME
= 0;
115 int i2c_read(i2c_t
*obj
, int address
, char *data
, int length
, int stop
) {
120 LPC_I2C0
->MSTDAT
= (address
| 0x01);
121 LPC_I2C0
->MSTCTL
|= 0x20;
122 if (i2c_wait_SI(obj
) == -1)
125 status
= ((LPC_I2C0
->STAT
>> 1) & (0x07));
126 if (status
!= 0x01) {
128 return I2C_ERROR_NO_SLAVE
;
131 // Read in all except last byte
132 for (count
= 0; count
< (length
- 1); count
++) {
133 if (i2c_wait_SI(obj
) == -1)
135 LPC_I2C0
->MSTCTL
= (1 << 0);
136 data
[count
] = (LPC_I2C0
->MSTDAT
& 0xFF);
137 status
= ((LPC_I2C0
->STAT
>> 1) & (0x07));
138 if (status
!= 0x01) {
145 if (i2c_wait_SI(obj
) == -1)
148 data
[count
] = (LPC_I2C0
->MSTDAT
& 0xFF);
149 status
= i2c_status(obj
);
150 if (status
!= 0x01) {
154 // If not repeated start, send stop.
164 int i2c_write(i2c_t
*obj
, int address
, const char *data
, int length
, int stop
) {
169 LPC_I2C0
->MSTDAT
= (address
& 0xFE);
170 LPC_I2C0
->MSTCTL
|= 0x20;
171 if (i2c_wait_SI(obj
) == -1)
174 status
= ((LPC_I2C0
->STAT
>> 1) & (0x07));
175 if (status
!= 0x02) {
177 return I2C_ERROR_NO_SLAVE
;
180 for (i
=0; i
<length
; i
++) {
181 LPC_I2C0
->MSTDAT
= data
[i
];
182 LPC_I2C0
->MSTCTL
= (1 << 0);
183 if (i2c_wait_SI(obj
) == -1)
186 status
= ((LPC_I2C0
->STAT
>> 1) & (0x07));
187 if (status
!= 0x02) {
193 // If not repeated start, send stop.
203 void i2c_reset(i2c_t
*obj
) {
207 int i2c_byte_read(i2c_t
*obj
, int last
) {
208 return (i2c_do_read(obj
, last
) & 0xFF);
211 int i2c_byte_write(i2c_t
*obj
, int data
) {
212 if (i2c_do_write(obj
, (data
& 0xFF), 0) == 2) {