mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16 #include "mbed_assert.h"
elessair 0:f269e3021894 17 #include "i2c_api.h"
elessair 0:f269e3021894 18 #include "cmsis.h"
elessair 0:f269e3021894 19 #include "pinmap.h"
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 static uint8_t repeated_start = 0;
elessair 0:f269e3021894 22
elessair 0:f269e3021894 23 #define I2C_STAT(x) ((LPC_I2C0->STAT >> 1) & (0x07))
elessair 0:f269e3021894 24
elessair 0:f269e3021894 25 static inline int i2c_status(i2c_t *obj) {
elessair 0:f269e3021894 26 return I2C_STAT(obj);
elessair 0:f269e3021894 27 }
elessair 0:f269e3021894 28
elessair 0:f269e3021894 29 // Wait until the Serial Interrupt (SI) is set
elessair 0:f269e3021894 30 static int i2c_wait_SI(i2c_t *obj) {
elessair 0:f269e3021894 31 volatile int timeout = 0;
elessair 0:f269e3021894 32 while (!(LPC_I2C0->STAT & (1 << 0))) {
elessair 0:f269e3021894 33 timeout++;
elessair 0:f269e3021894 34 if (timeout > 100000) return -1;
elessair 0:f269e3021894 35 }
elessair 0:f269e3021894 36 return 0;
elessair 0:f269e3021894 37 }
elessair 0:f269e3021894 38
elessair 0:f269e3021894 39 static inline void i2c_interface_enable(i2c_t *obj) {
elessair 0:f269e3021894 40 LPC_I2C0->CFG |= (1 << 0);
elessair 0:f269e3021894 41 }
elessair 0:f269e3021894 42
elessair 0:f269e3021894 43 void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
elessair 0:f269e3021894 44 MBED_ASSERT((sda == P0_23) && (scl == P0_22));
elessair 0:f269e3021894 45
elessair 0:f269e3021894 46 // Enables clock for I2C0
elessair 0:f269e3021894 47 LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << 13);
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 LPC_SYSCON->PRESETCTRL1 |= (1 << 13);
elessair 0:f269e3021894 50 LPC_SYSCON->PRESETCTRL1 &= ~(1 << 13);
elessair 0:f269e3021894 51
elessair 0:f269e3021894 52 // pin enable
elessair 0:f269e3021894 53 LPC_SWM->PINENABLE1 &= ~(0x3 << 3);
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 // set default frequency at 100kHz
elessair 0:f269e3021894 56 i2c_frequency(obj, 100000);
elessair 0:f269e3021894 57 i2c_interface_enable(obj);
elessair 0:f269e3021894 58 }
elessair 0:f269e3021894 59
elessair 0:f269e3021894 60 inline int i2c_start(i2c_t *obj) {
elessair 0:f269e3021894 61 int status = 0;
elessair 0:f269e3021894 62 if (repeated_start) {
elessair 0:f269e3021894 63 LPC_I2C0->MSTCTL = (1 << 1) | (1 << 0);
elessair 0:f269e3021894 64 repeated_start = 0;
elessair 0:f269e3021894 65 } else {
elessair 0:f269e3021894 66 LPC_I2C0->MSTCTL = (1 << 1);
elessair 0:f269e3021894 67 }
elessair 0:f269e3021894 68 return status;
elessair 0:f269e3021894 69 }
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 inline int i2c_stop(i2c_t *obj) {
elessair 0:f269e3021894 72 volatile int timeout = 0;
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 LPC_I2C0->MSTCTL = (1 << 2) | (1 << 0);
elessair 0:f269e3021894 75 while ((LPC_I2C0->STAT & ((1 << 0) | (7 << 1))) != ((1 << 0) | (0 << 1))) {
elessair 0:f269e3021894 76 timeout ++;
elessair 0:f269e3021894 77 if (timeout > 100000) return 1;
elessair 0:f269e3021894 78 }
elessair 0:f269e3021894 79
elessair 0:f269e3021894 80 return 0;
elessair 0:f269e3021894 81 }
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83
elessair 0:f269e3021894 84 static inline int i2c_do_write(i2c_t *obj, int value, uint8_t addr) {
elessair 0:f269e3021894 85 // write the data
elessair 0:f269e3021894 86 LPC_I2C0->MSTDAT = value;
elessair 0:f269e3021894 87
elessair 0:f269e3021894 88 if (!addr)
elessair 0:f269e3021894 89 LPC_I2C0->MSTCTL = (1 << 0);
elessair 0:f269e3021894 90
elessair 0:f269e3021894 91 // wait and return status
elessair 0:f269e3021894 92 i2c_wait_SI(obj);
elessair 0:f269e3021894 93 return i2c_status(obj);
elessair 0:f269e3021894 94 }
elessair 0:f269e3021894 95
elessair 0:f269e3021894 96 static inline int i2c_do_read(i2c_t *obj, int last) {
elessair 0:f269e3021894 97 // wait for it to arrive
elessair 0:f269e3021894 98 i2c_wait_SI(obj);
elessair 0:f269e3021894 99 if (!last)
elessair 0:f269e3021894 100 LPC_I2C0->MSTCTL = (1 << 0);
elessair 0:f269e3021894 101
elessair 0:f269e3021894 102 // return the data
elessair 0:f269e3021894 103 return (LPC_I2C0->MSTDAT & 0xFF);
elessair 0:f269e3021894 104 }
elessair 0:f269e3021894 105
elessair 0:f269e3021894 106 void i2c_frequency(i2c_t *obj, int hz) {
elessair 0:f269e3021894 107 // No peripheral clock divider on the M0
elessair 0:f269e3021894 108 uint32_t PCLK = SystemCoreClock;
elessair 0:f269e3021894 109 uint32_t clkdiv = PCLK / (hz * 4) - 1;
elessair 0:f269e3021894 110
elessair 0:f269e3021894 111 LPC_I2C0->DIV = clkdiv;
elessair 0:f269e3021894 112 LPC_I2C0->MSTTIME = 0;
elessair 0:f269e3021894 113 }
elessair 0:f269e3021894 114
elessair 0:f269e3021894 115 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
elessair 0:f269e3021894 116 int count, status;
elessair 0:f269e3021894 117
elessair 0:f269e3021894 118 i2c_start(obj);
elessair 0:f269e3021894 119
elessair 0:f269e3021894 120 LPC_I2C0->MSTDAT = (address | 0x01);
elessair 0:f269e3021894 121 LPC_I2C0->MSTCTL |= 0x20;
elessair 0:f269e3021894 122 if (i2c_wait_SI(obj) == -1)
elessair 0:f269e3021894 123 return -1;
elessair 0:f269e3021894 124
elessair 0:f269e3021894 125 status = ((LPC_I2C0->STAT >> 1) & (0x07));
elessair 0:f269e3021894 126 if (status != 0x01) {
elessair 0:f269e3021894 127 i2c_stop(obj);
elessair 0:f269e3021894 128 return I2C_ERROR_NO_SLAVE;
elessair 0:f269e3021894 129 }
elessair 0:f269e3021894 130
elessair 0:f269e3021894 131 // Read in all except last byte
elessair 0:f269e3021894 132 for (count = 0; count < (length - 1); count++) {
elessair 0:f269e3021894 133 if (i2c_wait_SI(obj) == -1)
elessair 0:f269e3021894 134 return -1;
elessair 0:f269e3021894 135 LPC_I2C0->MSTCTL = (1 << 0);
elessair 0:f269e3021894 136 data[count] = (LPC_I2C0->MSTDAT & 0xFF);
elessair 0:f269e3021894 137 status = ((LPC_I2C0->STAT >> 1) & (0x07));
elessair 0:f269e3021894 138 if (status != 0x01) {
elessair 0:f269e3021894 139 i2c_stop(obj);
elessair 0:f269e3021894 140 return count;
elessair 0:f269e3021894 141 }
elessair 0:f269e3021894 142 }
elessair 0:f269e3021894 143
elessair 0:f269e3021894 144 // read in last byte
elessair 0:f269e3021894 145 if (i2c_wait_SI(obj) == -1)
elessair 0:f269e3021894 146 return -1;
elessair 0:f269e3021894 147
elessair 0:f269e3021894 148 data[count] = (LPC_I2C0->MSTDAT & 0xFF);
elessair 0:f269e3021894 149 status = i2c_status(obj);
elessair 0:f269e3021894 150 if (status != 0x01) {
elessair 0:f269e3021894 151 i2c_stop(obj);
elessair 0:f269e3021894 152 return length - 1;
elessair 0:f269e3021894 153 }
elessair 0:f269e3021894 154 // If not repeated start, send stop.
elessair 0:f269e3021894 155 if (stop) {
elessair 0:f269e3021894 156 i2c_stop(obj);
elessair 0:f269e3021894 157 } else {
elessair 0:f269e3021894 158 repeated_start = 1;
elessair 0:f269e3021894 159 }
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 return length;
elessair 0:f269e3021894 162 }
elessair 0:f269e3021894 163
elessair 0:f269e3021894 164 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
elessair 0:f269e3021894 165 int i, status;
elessair 0:f269e3021894 166
elessair 0:f269e3021894 167 i2c_start(obj);
elessair 0:f269e3021894 168
elessair 0:f269e3021894 169 LPC_I2C0->MSTDAT = (address & 0xFE);
elessair 0:f269e3021894 170 LPC_I2C0->MSTCTL |= 0x20;
elessair 0:f269e3021894 171 if (i2c_wait_SI(obj) == -1)
elessair 0:f269e3021894 172 return -1;
elessair 0:f269e3021894 173
elessair 0:f269e3021894 174 status = ((LPC_I2C0->STAT >> 1) & (0x07));
elessair 0:f269e3021894 175 if (status != 0x02) {
elessair 0:f269e3021894 176 i2c_stop(obj);
elessair 0:f269e3021894 177 return I2C_ERROR_NO_SLAVE;
elessair 0:f269e3021894 178 }
elessair 0:f269e3021894 179
elessair 0:f269e3021894 180 for (i=0; i<length; i++) {
elessair 0:f269e3021894 181 LPC_I2C0->MSTDAT = data[i];
elessair 0:f269e3021894 182 LPC_I2C0->MSTCTL = (1 << 0);
elessair 0:f269e3021894 183 if (i2c_wait_SI(obj) == -1)
elessair 0:f269e3021894 184 return -1;
elessair 0:f269e3021894 185
elessair 0:f269e3021894 186 status = ((LPC_I2C0->STAT >> 1) & (0x07));
elessair 0:f269e3021894 187 if (status != 0x02) {
elessair 0:f269e3021894 188 i2c_stop(obj);
elessair 0:f269e3021894 189 return i;
elessair 0:f269e3021894 190 }
elessair 0:f269e3021894 191 }
elessair 0:f269e3021894 192
elessair 0:f269e3021894 193 // If not repeated start, send stop.
elessair 0:f269e3021894 194 if (stop) {
elessair 0:f269e3021894 195 i2c_stop(obj);
elessair 0:f269e3021894 196 } else {
elessair 0:f269e3021894 197 repeated_start = 1;
elessair 0:f269e3021894 198 }
elessair 0:f269e3021894 199
elessair 0:f269e3021894 200 return length;
elessair 0:f269e3021894 201 }
elessair 0:f269e3021894 202
elessair 0:f269e3021894 203 void i2c_reset(i2c_t *obj) {
elessair 0:f269e3021894 204 i2c_stop(obj);
elessair 0:f269e3021894 205 }
elessair 0:f269e3021894 206
elessair 0:f269e3021894 207 int i2c_byte_read(i2c_t *obj, int last) {
elessair 0:f269e3021894 208 return (i2c_do_read(obj, last) & 0xFF);
elessair 0:f269e3021894 209 }
elessair 0:f269e3021894 210
elessair 0:f269e3021894 211 int i2c_byte_write(i2c_t *obj, int data) {
elessair 0:f269e3021894 212 if (i2c_do_write(obj, (data & 0xFF), 0) == 2) {
elessair 0:f269e3021894 213 return 1;
elessair 0:f269e3021894 214 } else {
elessair 0:f269e3021894 215 return 0;
elessair 0:f269e3021894 216 }
elessair 0:f269e3021894 217 }