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"
23 #include "fsl_clock_manager.h"
24 #include "fsl_i2c_hal.h"
25 #include "fsl_port_hal.h"
26 #include "fsl_sim_hal.h"
27 #include "PeripheralPins.h"
29 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
30 uint32_t i2c_sda = pinmap_peripheral(sda, PinMap_I2C_SDA);
31 uint32_t i2c_scl = pinmap_peripheral(scl, PinMap_I2C_SCL);
32 obj->instance = pinmap_merge(i2c_sda, i2c_scl);
33 MBED_ASSERT((int)obj->instance != NC);
35 CLOCK_SYS_EnableI2cClock(obj->instance);
36 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
37 I2C_HAL_Init(i2c_addrs[obj->instance]);
38 I2C_HAL_Enable(i2c_addrs[obj->instance]);
39 I2C_HAL_SetIntCmd(i2c_addrs[obj->instance], true);
40 i2c_frequency(obj, 100000);
42 pinmap_pinout(sda, PinMap_I2C_SDA);
43 pinmap_pinout(scl, PinMap_I2C_SCL);
45 uint32_t port_addrs[] = PORT_BASE_ADDRS;
46 PORT_HAL_SetOpenDrainCmd(port_addrs[sda >> GPIO_PORT_SHIFT], sda & 0xFF, true);
47 PORT_HAL_SetOpenDrainCmd(port_addrs[scl >> GPIO_PORT_SHIFT], scl & 0xFF, true);
50 int i2c_start(i2c_t *obj) {
51 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
52 I2C_HAL_SendStart(i2c_addrs[obj->instance]);
56 int i2c_stop(i2c_t *obj) {
57 volatile uint32_t n = 0;
58 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
59 if (I2C_HAL_IsMaster(i2c_addrs[obj->instance]))
60 I2C_HAL_SendStop(i2c_addrs[obj->instance]);
62 // It seems that there are timing problems
63 // when there is no waiting time after a STOP.
64 // This wait is also included on the samples
65 // code provided with the freedom board
66 for (n = 0; n < 200; n++) __NOP();
70 static int timeout_status_poll(i2c_t *obj, i2c_status_flag_t flag) {
71 uint32_t i, timeout = 100000;
72 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
74 for (i = 0; i < timeout; i++) {
75 if (I2C_HAL_GetStatusFlag(i2c_addrs[obj->instance], flag))
81 // this function waits the end of a tx transfer and return the status of the transaction:
83 // 1: OK ack not received
85 static int i2c_wait_end_tx_transfer(i2c_t *obj) {
86 // wait for the interrupt flag
87 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
89 if (timeout_status_poll(obj, kI2CInterruptPending)) {
92 I2C_HAL_ClearInt(i2c_addrs[obj->instance]);
94 // wait transfer complete
95 if (timeout_status_poll(obj, kI2CTransferComplete)) {
99 // check if we received the ACK or not
100 return I2C_HAL_GetStatusFlag(i2c_addrs[obj->instance], kI2CReceivedNak) ? 1 : 0;
103 // this function waits the end of a rx transfer and return the status of the transaction:
106 static int i2c_wait_end_rx_transfer(i2c_t *obj) {
107 // wait for the end of the rx transfer
108 if (timeout_status_poll(obj, kI2CInterruptPending)) {
111 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
112 I2C_HAL_ClearInt(i2c_addrs[obj->instance]);
117 static int i2c_do_write(i2c_t *obj, int value) {
118 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
119 I2C_HAL_WriteByte(i2c_addrs[obj->instance], value);
121 // init and wait the end of the transfer
122 return i2c_wait_end_tx_transfer(obj);
125 static int i2c_do_read(i2c_t *obj, char * data, int last) {
126 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
128 I2C_HAL_SendNak(i2c_addrs[obj->instance]);
130 I2C_HAL_SendAck(i2c_addrs[obj->instance]);
133 *data = (I2C_HAL_ReadByte(i2c_addrs[obj->instance]) & 0xFF);
135 // start rx transfer and wait the end of the transfer
136 return i2c_wait_end_rx_transfer(obj);
139 void i2c_frequency(i2c_t *obj, int hz) {
141 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
142 clock_manager_error_code_t error = CLOCK_SYS_GetFreq(kBusClock, &busClock);
143 if (error == kClockManagerSuccess) {
144 I2C_HAL_SetBaudRate(i2c_addrs[obj->instance], busClock, hz / 1000, NULL);
148 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
150 char dummy_read, *ptr;
152 if (i2c_start(obj)) {
154 return I2C_ERROR_BUS_BUSY;
157 if (i2c_do_write(obj, (address | 0x01))) {
159 return I2C_ERROR_NO_SLAVE;
163 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
164 I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CReceive);
167 for (count = 0; count < (length); count++) {
168 ptr = (count == 0) ? &dummy_read : &data[count - 1];
169 uint8_t stop_ = (count == (length - 1)) ? 1 : 0;
170 if (i2c_do_read(obj, ptr, stop_)) {
176 // If not repeated start, send stop.
181 data[count-1] = I2C_HAL_ReadByte(i2c_addrs[obj->instance]);
186 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
189 if (i2c_start(obj)) {
191 return I2C_ERROR_BUS_BUSY;
194 if (i2c_do_write(obj, (address & 0xFE))) {
196 return I2C_ERROR_NO_SLAVE;
199 for (i = 0; i < length; i++) {
200 if(i2c_do_write(obj, data[i])) {
212 void i2c_reset(i2c_t *obj) {
216 int i2c_byte_read(i2c_t *obj, int last) {
218 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
220 I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CReceive);
223 i2c_do_read(obj, &data, last);
226 I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CSend);
227 return I2C_HAL_ReadByte(i2c_addrs[obj->instance]);
230 int i2c_byte_write(i2c_t *obj, int data) {
231 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
233 I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CSend);
235 return !i2c_do_write(obj, (data & 0xFF));
240 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
241 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
244 BW_I2C_C1_MST(i2c_addrs[obj->instance], 0);
245 I2C_HAL_SetIntCmd(i2c_addrs[obj->instance], true);
248 BW_I2C_C1_MST(i2c_addrs[obj->instance], 1);
252 int i2c_slave_receive(i2c_t *obj) {
253 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
254 switch(HW_I2C_S_RD(i2c_addrs[obj->instance])) {
266 int i2c_slave_read(i2c_t *obj, char *data, int length) {
270 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
272 I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CSend);
275 dummy_read = I2C_HAL_ReadByte(i2c_addrs[obj->instance]);
276 if (i2c_wait_end_rx_transfer(obj))
280 dummy_read = I2C_HAL_ReadByte(i2c_addrs[obj->instance]);
281 if (i2c_wait_end_rx_transfer(obj))
284 // read (length - 1) bytes
285 for (count = 0; count < (length - 1); count++) {
286 data[count] = I2C_HAL_ReadByte(i2c_addrs[obj->instance]);
287 if (i2c_wait_end_rx_transfer(obj))
292 ptr = (length == 0) ? &dummy_read : (uint8_t *)&data[count];
293 *ptr = I2C_HAL_ReadByte(i2c_addrs[obj->instance]);
295 return (length) ? (count + 1) : 0;
298 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
300 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
303 I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CSend);
305 for (i = 0; i < length; i++) {
306 if (i2c_do_write(obj, data[count++]) == 2)
311 I2C_HAL_SetDirMode(i2c_addrs[obj->instance], kI2CReceive);
313 // dummy rx transfer needed
314 // otherwise the master cannot generate a stop bit
315 I2C_HAL_ReadByte(i2c_addrs[obj->instance]);
316 if (i2c_wait_end_rx_transfer(obj) == 2)
322 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
323 uint32_t i2c_addrs[] = I2C_BASE_ADDRS;
324 I2C_HAL_SetUpperAddress7bit(i2c_addrs[obj->instance], address & 0xfe);