mbed library sources for GR-PEACH rev.B.

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Apr 09 07:00:08 2015 +0100
Revision:
509:53fc1beb5664
Parent:
367:d0878bd5474b
Synchronized with git revision c3cd171fc3a484f24e3c7fa3c25928ad3024a341

Full URL: https://github.com/mbedmicro/mbed/commit/c3cd171fc3a484f24e3c7fa3c25928ad3024a341/

LPC82X - Fixed hardcoded MRT_Clock_MHz

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 337:6ed01c00b962 1 /* mbed Microcontroller Library
mbed_official 337:6ed01c00b962 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 337:6ed01c00b962 3 *
mbed_official 337:6ed01c00b962 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 337:6ed01c00b962 5 * you may not use this file except in compliance with the License.
mbed_official 337:6ed01c00b962 6 * You may obtain a copy of the License at
mbed_official 337:6ed01c00b962 7 *
mbed_official 337:6ed01c00b962 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 337:6ed01c00b962 9 *
mbed_official 337:6ed01c00b962 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 337:6ed01c00b962 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 337:6ed01c00b962 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 337:6ed01c00b962 13 * See the License for the specific language governing permissions and
mbed_official 337:6ed01c00b962 14 * limitations under the License.
mbed_official 337:6ed01c00b962 15 */
mbed_official 337:6ed01c00b962 16 #include <stdlib.h>
mbed_official 337:6ed01c00b962 17 #include <string.h>
mbed_official 337:6ed01c00b962 18
mbed_official 337:6ed01c00b962 19 #include "i2c_api.h"
mbed_official 337:6ed01c00b962 20 #include "cmsis.h"
mbed_official 337:6ed01c00b962 21 #include "pinmap.h"
mbed_official 337:6ed01c00b962 22
mbed_official 509:53fc1beb5664 23 #define LPC824_I2C0_FMPLUS 1
mbed_official 337:6ed01c00b962 24
mbed_official 337:6ed01c00b962 25 #if DEVICE_I2C
mbed_official 337:6ed01c00b962 26
mbed_official 337:6ed01c00b962 27 static const SWM_Map SWM_I2C_SDA[] = {
mbed_official 509:53fc1beb5664 28 //PINASSIGN Register ID, Pinselect bitfield position
mbed_official 509:53fc1beb5664 29 { 9, 8},
mbed_official 337:6ed01c00b962 30 { 9, 24},
mbed_official 337:6ed01c00b962 31 {10, 8},
mbed_official 337:6ed01c00b962 32 };
mbed_official 337:6ed01c00b962 33
mbed_official 337:6ed01c00b962 34 static const SWM_Map SWM_I2C_SCL[] = {
mbed_official 509:53fc1beb5664 35 //PINASSIGN Register ID, Pinselect bitfield position
mbed_official 337:6ed01c00b962 36 { 9, 16},
mbed_official 337:6ed01c00b962 37 {10, 0},
mbed_official 337:6ed01c00b962 38 {10, 16},
mbed_official 337:6ed01c00b962 39 };
mbed_official 337:6ed01c00b962 40
mbed_official 337:6ed01c00b962 41
mbed_official 337:6ed01c00b962 42 static int i2c_used = 0;
mbed_official 337:6ed01c00b962 43 static uint8_t repeated_start = 0;
mbed_official 337:6ed01c00b962 44
mbed_official 337:6ed01c00b962 45 #define I2C_DAT(x) (x->i2c->MSTDAT)
mbed_official 337:6ed01c00b962 46 #define I2C_STAT(x) ((x->i2c->STAT >> 1) & (0x07))
mbed_official 337:6ed01c00b962 47
mbed_official 337:6ed01c00b962 48 static inline void i2c_power_enable(int ch)
mbed_official 337:6ed01c00b962 49 {
mbed_official 337:6ed01c00b962 50 switch(ch) {
mbed_official 337:6ed01c00b962 51 case 0:
mbed_official 509:53fc1beb5664 52 // I2C0, Same as for LPC812
mbed_official 337:6ed01c00b962 53 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 5);
mbed_official 337:6ed01c00b962 54 LPC_SYSCON->PRESETCTRL &= ~(1 << 6);
mbed_official 337:6ed01c00b962 55 LPC_SYSCON->PRESETCTRL |= (1 << 6);
mbed_official 337:6ed01c00b962 56 break;
mbed_official 337:6ed01c00b962 57 case 1:
mbed_official 337:6ed01c00b962 58 case 2:
mbed_official 337:6ed01c00b962 59 case 3:
mbed_official 509:53fc1beb5664 60 // I2C1,I2C2 or I2C3. Not available for LPC812
mbed_official 337:6ed01c00b962 61 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << (20 + ch));
mbed_official 337:6ed01c00b962 62 LPC_SYSCON->PRESETCTRL &= ~(1 << (13 + ch));
mbed_official 337:6ed01c00b962 63 LPC_SYSCON->PRESETCTRL |= (1 << (13 + ch));
mbed_official 337:6ed01c00b962 64 break;
mbed_official 337:6ed01c00b962 65 default:
mbed_official 337:6ed01c00b962 66 break;
mbed_official 337:6ed01c00b962 67 }
mbed_official 337:6ed01c00b962 68 }
mbed_official 337:6ed01c00b962 69
mbed_official 337:6ed01c00b962 70
mbed_official 509:53fc1beb5664 71 static inline void i2c_interface_enable(i2c_t *obj) {
mbed_official 509:53fc1beb5664 72 obj->i2c->CFG |= (1 << 0); // Enable Master mode
mbed_official 509:53fc1beb5664 73 // obj->i2c->CFG &= ~(1 << 1); // Disable Slave mode
mbed_official 509:53fc1beb5664 74 }
mbed_official 509:53fc1beb5664 75
mbed_official 509:53fc1beb5664 76
mbed_official 337:6ed01c00b962 77 static int get_available_i2c(void) {
mbed_official 337:6ed01c00b962 78 int i;
mbed_official 337:6ed01c00b962 79 for (i=0; i<3; i++) {
mbed_official 337:6ed01c00b962 80 if ((i2c_used & (1 << i)) == 0)
mbed_official 337:6ed01c00b962 81 return i+1;
mbed_official 337:6ed01c00b962 82 }
mbed_official 337:6ed01c00b962 83 return -1;
mbed_official 337:6ed01c00b962 84 }
mbed_official 337:6ed01c00b962 85
mbed_official 337:6ed01c00b962 86 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
mbed_official 337:6ed01c00b962 87 {
mbed_official 337:6ed01c00b962 88 const SWM_Map *swm;
mbed_official 337:6ed01c00b962 89 uint32_t regVal;
mbed_official 337:6ed01c00b962 90 int i2c_ch = 0;
mbed_official 509:53fc1beb5664 91
mbed_official 509:53fc1beb5664 92 //LPC824
mbed_official 509:53fc1beb5664 93 //I2C0 can support FM+ but only on P0_11 and P0_10
mbed_official 337:6ed01c00b962 94 if (sda == I2C_SDA && scl == I2C_SCL) {
mbed_official 509:53fc1beb5664 95 //Select I2C mode for P0_11 and P0_10
mbed_official 509:53fc1beb5664 96 LPC_SWM->PINENABLE0 &= ~(0x3 << 11);
mbed_official 509:53fc1beb5664 97
mbed_official 509:53fc1beb5664 98 #if(LPC824_I2C0_FMPLUS == 1)
mbed_official 509:53fc1beb5664 99 // Enable FM+ mode on P0_11, P0_10
mbed_official 509:53fc1beb5664 100 LPC_IOCON->PIO0_10 &= ~(0x3 << 8);
mbed_official 509:53fc1beb5664 101 LPC_IOCON->PIO0_10 |= (0x2 << 8); //FM+ mode
mbed_official 509:53fc1beb5664 102 LPC_IOCON->PIO0_11 &= ~(0x3 << 8);
mbed_official 509:53fc1beb5664 103 LPC_IOCON->PIO0_11 |= (0x2 << 8); //FM+ mode
mbed_official 509:53fc1beb5664 104 #endif
mbed_official 337:6ed01c00b962 105 }
mbed_official 337:6ed01c00b962 106 else {
mbed_official 509:53fc1beb5664 107 //Select any other pin for I2C1, I2C2 or I2C3
mbed_official 337:6ed01c00b962 108 i2c_ch = get_available_i2c();
mbed_official 337:6ed01c00b962 109 if (i2c_ch == -1)
mbed_official 337:6ed01c00b962 110 return;
mbed_official 366:2c37f9c21d44 111 i2c_used |= (1 << (i2c_ch - 1));
mbed_official 337:6ed01c00b962 112
mbed_official 337:6ed01c00b962 113 swm = &SWM_I2C_SDA[i2c_ch - 1];
mbed_official 337:6ed01c00b962 114 regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
mbed_official 337:6ed01c00b962 115 LPC_SWM->PINASSIGN[swm->n] = regVal | ((sda >> PIN_SHIFT) << swm->offset);
mbed_official 337:6ed01c00b962 116
mbed_official 337:6ed01c00b962 117 swm = &SWM_I2C_SCL[i2c_ch - 1];
mbed_official 337:6ed01c00b962 118 regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset);
mbed_official 337:6ed01c00b962 119 LPC_SWM->PINASSIGN[swm->n] = regVal | ((scl >> PIN_SHIFT) << swm->offset);
mbed_official 337:6ed01c00b962 120 }
mbed_official 337:6ed01c00b962 121
mbed_official 337:6ed01c00b962 122 switch(i2c_ch) {
mbed_official 337:6ed01c00b962 123 case 0:
mbed_official 337:6ed01c00b962 124 obj->i2c = (LPC_I2C0_Type *)LPC_I2C0;
mbed_official 337:6ed01c00b962 125 break;
mbed_official 337:6ed01c00b962 126 case 1:
mbed_official 337:6ed01c00b962 127 obj->i2c = (LPC_I2C0_Type *)LPC_I2C1;
mbed_official 337:6ed01c00b962 128 break;
mbed_official 337:6ed01c00b962 129 case 2:
mbed_official 337:6ed01c00b962 130 obj->i2c = (LPC_I2C0_Type *)LPC_I2C2;
mbed_official 337:6ed01c00b962 131 break;
mbed_official 337:6ed01c00b962 132 case 3:
mbed_official 337:6ed01c00b962 133 obj->i2c = (LPC_I2C0_Type *)LPC_I2C3;
mbed_official 337:6ed01c00b962 134 break;
mbed_official 337:6ed01c00b962 135 default:
mbed_official 337:6ed01c00b962 136 break;
mbed_official 337:6ed01c00b962 137 }
mbed_official 337:6ed01c00b962 138
mbed_official 337:6ed01c00b962 139 // enable power
mbed_official 337:6ed01c00b962 140 i2c_power_enable(i2c_ch);
mbed_official 509:53fc1beb5664 141 // set default frequency at 100k
mbed_official 509:53fc1beb5664 142 i2c_frequency(obj, 100000);
mbed_official 367:d0878bd5474b 143 i2c_interface_enable(obj);
mbed_official 337:6ed01c00b962 144 }
mbed_official 337:6ed01c00b962 145
mbed_official 509:53fc1beb5664 146
mbed_official 509:53fc1beb5664 147 static inline int i2c_status(i2c_t *obj) {
mbed_official 509:53fc1beb5664 148 return I2C_STAT(obj);
mbed_official 509:53fc1beb5664 149 }
mbed_official 509:53fc1beb5664 150
mbed_official 509:53fc1beb5664 151 // Wait until the Master Serial Interrupt (SI) is set
mbed_official 509:53fc1beb5664 152 // Timeout when it takes too long.
mbed_official 509:53fc1beb5664 153 static int i2c_wait_SI(i2c_t *obj) {
mbed_official 509:53fc1beb5664 154 int timeout = 0;
mbed_official 509:53fc1beb5664 155 while (!(obj->i2c->STAT & (1 << 0))) {
mbed_official 509:53fc1beb5664 156 timeout++;
mbed_official 509:53fc1beb5664 157 if (timeout > 100000) return -1;
mbed_official 509:53fc1beb5664 158 }
mbed_official 509:53fc1beb5664 159 return 0;
mbed_official 509:53fc1beb5664 160 }
mbed_official 509:53fc1beb5664 161
mbed_official 509:53fc1beb5664 162
mbed_official 509:53fc1beb5664 163 //Attention. Spec says: First store Address in DAT before setting STA !
mbed_official 509:53fc1beb5664 164 //Undefined state when using single byte I2C operations and too much delay
mbed_official 509:53fc1beb5664 165 //between i2c_start and do_i2c_write(Address).
mbed_official 509:53fc1beb5664 166 //Also note that lpc812/824 will immediately continue reading a byte when Address b0 == 1
mbed_official 509:53fc1beb5664 167 inline int i2c_start(i2c_t *obj) {
mbed_official 337:6ed01c00b962 168 int status = 0;
mbed_official 337:6ed01c00b962 169 if (repeated_start) {
mbed_official 509:53fc1beb5664 170 obj->i2c->MSTCTL = (1 << 1) | (1 << 0); // STA bit and Continue bit to complete previous RD or WR
mbed_official 337:6ed01c00b962 171 repeated_start = 0;
mbed_official 337:6ed01c00b962 172 } else {
mbed_official 509:53fc1beb5664 173 obj->i2c->MSTCTL = (1 << 1); // STA bit
mbed_official 337:6ed01c00b962 174 }
mbed_official 337:6ed01c00b962 175 return status;
mbed_official 337:6ed01c00b962 176 }
mbed_official 337:6ed01c00b962 177
mbed_official 509:53fc1beb5664 178 //Generate Stop condition and wait until bus is Idle
mbed_official 509:53fc1beb5664 179 //Will also send NAK for previous RD
mbed_official 509:53fc1beb5664 180 inline int i2c_stop(i2c_t *obj) {
mbed_official 509:53fc1beb5664 181 int timeout = 0;
mbed_official 337:6ed01c00b962 182
mbed_official 509:53fc1beb5664 183 // STP bit and Continue bit. Sends NAK to complete previous RD
mbed_official 337:6ed01c00b962 184 obj->i2c->MSTCTL = (1 << 2) | (1 << 0);
mbed_official 509:53fc1beb5664 185
mbed_official 509:53fc1beb5664 186 //Spin until Ready (b0 == 1)and Status is Idle (b3..b1 == 000)
mbed_official 509:53fc1beb5664 187 while ((obj->i2c->STAT & ((7 << 1) | (1 << 0))) != ((0 << 1) | (1 << 0))) {
mbed_official 337:6ed01c00b962 188 timeout ++;
mbed_official 337:6ed01c00b962 189 if (timeout > 100000) return 1;
mbed_official 337:6ed01c00b962 190 }
mbed_official 337:6ed01c00b962 191
mbed_official 509:53fc1beb5664 192 // repeated_start = 0; // bus free
mbed_official 337:6ed01c00b962 193 return 0;
mbed_official 337:6ed01c00b962 194 }
mbed_official 337:6ed01c00b962 195
mbed_official 509:53fc1beb5664 196 //Spec says: first check Idle and status is Ok
mbed_official 509:53fc1beb5664 197 static inline int i2c_do_write(i2c_t *obj, int value, uint8_t addr) {
mbed_official 337:6ed01c00b962 198 // write the data
mbed_official 337:6ed01c00b962 199 I2C_DAT(obj) = value;
mbed_official 509:53fc1beb5664 200
mbed_official 337:6ed01c00b962 201 if (!addr)
mbed_official 509:53fc1beb5664 202 obj->i2c->MSTCTL = (1 << 0); //Set continue for data. Should not be set for addr since that uses STA
mbed_official 509:53fc1beb5664 203
mbed_official 337:6ed01c00b962 204 // wait and return status
mbed_official 337:6ed01c00b962 205 i2c_wait_SI(obj);
mbed_official 337:6ed01c00b962 206 return i2c_status(obj);
mbed_official 337:6ed01c00b962 207 }
mbed_official 337:6ed01c00b962 208
mbed_official 509:53fc1beb5664 209
mbed_official 509:53fc1beb5664 210 //Attention, correct Order: wait for data ready, read data, read status, continue, return
mbed_official 509:53fc1beb5664 211 //Dont read DAT or STAT when not ready, so dont read after setting continue.
mbed_official 509:53fc1beb5664 212 //Results may be invalid when next read is underway.
mbed_official 509:53fc1beb5664 213 static inline int i2c_do_read(i2c_t *obj, int last) {
mbed_official 337:6ed01c00b962 214 // wait for it to arrive
mbed_official 337:6ed01c00b962 215 i2c_wait_SI(obj);
mbed_official 337:6ed01c00b962 216 if (!last)
mbed_official 509:53fc1beb5664 217 obj->i2c->MSTCTL = (1 << 0); //ACK and Continue
mbed_official 509:53fc1beb5664 218
mbed_official 337:6ed01c00b962 219 // return the data
mbed_official 337:6ed01c00b962 220 return (I2C_DAT(obj) & 0xFF);
mbed_official 337:6ed01c00b962 221 }
mbed_official 337:6ed01c00b962 222
mbed_official 509:53fc1beb5664 223
mbed_official 509:53fc1beb5664 224 void i2c_frequency(i2c_t *obj, int hz) {
mbed_official 509:53fc1beb5664 225 // No peripheral clock divider on the M0
mbed_official 509:53fc1beb5664 226 uint32_t PCLK = SystemCoreClock;
mbed_official 509:53fc1beb5664 227
mbed_official 509:53fc1beb5664 228 uint32_t clkdiv = PCLK / (hz * 4) - 1;
mbed_official 509:53fc1beb5664 229
mbed_official 509:53fc1beb5664 230 obj->i2c->CLKDIV = clkdiv;
mbed_official 509:53fc1beb5664 231 obj->i2c->MSTTIME = 0;
mbed_official 337:6ed01c00b962 232 }
mbed_official 337:6ed01c00b962 233
mbed_official 509:53fc1beb5664 234 // The I2C does a read or a write as a whole operation
mbed_official 509:53fc1beb5664 235 // There are two types of error conditions it can encounter
mbed_official 509:53fc1beb5664 236 // 1) it can not obtain the bus
mbed_official 509:53fc1beb5664 237 // 2) it gets error responses at part of the transmission
mbed_official 509:53fc1beb5664 238 //
mbed_official 509:53fc1beb5664 239 // We tackle them as follows:
mbed_official 509:53fc1beb5664 240 // 1) we retry until we get the bus. we could have a "timeout" if we can not get it
mbed_official 509:53fc1beb5664 241 // which basically turns it in to a 2)
mbed_official 509:53fc1beb5664 242 // 2) on error, we use the standard error mechanisms to report/debug
mbed_official 509:53fc1beb5664 243 //
mbed_official 509:53fc1beb5664 244 // Therefore an I2C transaction should always complete. If it doesn't it is usually
mbed_official 509:53fc1beb5664 245 // because something is setup wrong (e.g. wiring), and we don't need to programatically
mbed_official 509:53fc1beb5664 246 // check for that
mbed_official 509:53fc1beb5664 247 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
mbed_official 509:53fc1beb5664 248 int count, status;
mbed_official 509:53fc1beb5664 249
mbed_official 509:53fc1beb5664 250 //Store the address+RD and then generate STA
mbed_official 509:53fc1beb5664 251 I2C_DAT(obj) = address | 0x01;
mbed_official 509:53fc1beb5664 252 i2c_start(obj);
mbed_official 509:53fc1beb5664 253
mbed_official 509:53fc1beb5664 254 // Wait for completion of STA and Sending of SlaveAddress+RD and first Read byte
mbed_official 509:53fc1beb5664 255 i2c_wait_SI(obj);
mbed_official 509:53fc1beb5664 256 status = i2c_status(obj);
mbed_official 509:53fc1beb5664 257 if (status == 0x03) { // NAK on SlaveAddress
mbed_official 509:53fc1beb5664 258 i2c_stop(obj);
mbed_official 509:53fc1beb5664 259 return I2C_ERROR_NO_SLAVE;
mbed_official 509:53fc1beb5664 260 }
mbed_official 337:6ed01c00b962 261
mbed_official 509:53fc1beb5664 262 // Read in all except last byte
mbed_official 509:53fc1beb5664 263 for (count = 0; count < (length-1); count++) {
mbed_official 509:53fc1beb5664 264
mbed_official 509:53fc1beb5664 265 // Wait for it to arrive, note that first byte read after address+RD is already waiting
mbed_official 509:53fc1beb5664 266 i2c_wait_SI(obj);
mbed_official 509:53fc1beb5664 267 status = i2c_status(obj);
mbed_official 509:53fc1beb5664 268 if (status != 0x01) { // RX RDY
mbed_official 509:53fc1beb5664 269 i2c_stop(obj);
mbed_official 509:53fc1beb5664 270 return count;
mbed_official 509:53fc1beb5664 271 }
mbed_official 509:53fc1beb5664 272 data[count] = I2C_DAT(obj) & 0xFF; // Store read byte
mbed_official 509:53fc1beb5664 273
mbed_official 509:53fc1beb5664 274 obj->i2c->MSTCTL = (1 << 0); // Send ACK and Continue to read
mbed_official 509:53fc1beb5664 275 }
mbed_official 509:53fc1beb5664 276
mbed_official 509:53fc1beb5664 277 // Read final byte
mbed_official 509:53fc1beb5664 278 // Wait for it to arrive
mbed_official 509:53fc1beb5664 279 i2c_wait_SI(obj);
mbed_official 509:53fc1beb5664 280
mbed_official 509:53fc1beb5664 281 status = i2c_status(obj);
mbed_official 509:53fc1beb5664 282 if (status != 0x01) { // RX RDY
mbed_official 509:53fc1beb5664 283 i2c_stop(obj);
mbed_official 509:53fc1beb5664 284 return count;
mbed_official 509:53fc1beb5664 285 }
mbed_official 509:53fc1beb5664 286 data[count] = I2C_DAT(obj) & 0xFF; // Store final read byte
mbed_official 509:53fc1beb5664 287
mbed_official 509:53fc1beb5664 288 // If not repeated start, send stop.
mbed_official 509:53fc1beb5664 289 if (stop) {
mbed_official 509:53fc1beb5664 290 i2c_stop(obj); // Also sends NAK for last read byte
mbed_official 509:53fc1beb5664 291 } else {
mbed_official 509:53fc1beb5664 292 repeated_start = 1;
mbed_official 509:53fc1beb5664 293 }
mbed_official 509:53fc1beb5664 294
mbed_official 509:53fc1beb5664 295 return length;
mbed_official 337:6ed01c00b962 296 }
mbed_official 337:6ed01c00b962 297
mbed_official 509:53fc1beb5664 298
mbed_official 509:53fc1beb5664 299 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
mbed_official 509:53fc1beb5664 300 int i, status;
mbed_official 337:6ed01c00b962 301
mbed_official 509:53fc1beb5664 302 //Store the address+/WR and then generate STA
mbed_official 509:53fc1beb5664 303 I2C_DAT(obj) = address & 0xFE;
mbed_official 509:53fc1beb5664 304 i2c_start(obj);
mbed_official 509:53fc1beb5664 305
mbed_official 509:53fc1beb5664 306 // Wait for completion of STA and Sending of SlaveAddress+/WR
mbed_official 509:53fc1beb5664 307 i2c_wait_SI(obj);
mbed_official 509:53fc1beb5664 308 status = i2c_status(obj);
mbed_official 509:53fc1beb5664 309 if (status == 0x03) { // NAK SlaveAddress
mbed_official 509:53fc1beb5664 310 i2c_stop(obj);
mbed_official 509:53fc1beb5664 311 return I2C_ERROR_NO_SLAVE;
mbed_official 509:53fc1beb5664 312 }
mbed_official 509:53fc1beb5664 313
mbed_official 509:53fc1beb5664 314 //Write all bytes
mbed_official 509:53fc1beb5664 315 for (i=0; i<length; i++) {
mbed_official 509:53fc1beb5664 316 status = i2c_do_write(obj, data[i], 0);
mbed_official 509:53fc1beb5664 317 if (status != 0x02) { // TX RDY. Handles a Slave NAK on datawrite
mbed_official 509:53fc1beb5664 318 i2c_stop(obj);
mbed_official 509:53fc1beb5664 319 return i;
mbed_official 509:53fc1beb5664 320 }
mbed_official 509:53fc1beb5664 321 }
mbed_official 509:53fc1beb5664 322
mbed_official 509:53fc1beb5664 323 // If not repeated start, send stop.
mbed_official 509:53fc1beb5664 324 if (stop) {
mbed_official 509:53fc1beb5664 325 i2c_stop(obj);
mbed_official 509:53fc1beb5664 326 } else {
mbed_official 509:53fc1beb5664 327 repeated_start = 1;
mbed_official 509:53fc1beb5664 328 }
mbed_official 509:53fc1beb5664 329
mbed_official 509:53fc1beb5664 330 return length;
mbed_official 337:6ed01c00b962 331 }
mbed_official 337:6ed01c00b962 332
mbed_official 509:53fc1beb5664 333 void i2c_reset(i2c_t *obj) {
mbed_official 337:6ed01c00b962 334 i2c_stop(obj);
mbed_official 337:6ed01c00b962 335 }
mbed_official 337:6ed01c00b962 336
mbed_official 509:53fc1beb5664 337 int i2c_byte_read(i2c_t *obj, int last) {
mbed_official 337:6ed01c00b962 338 return (i2c_do_read(obj, last) & 0xFF);
mbed_official 509:53fc1beb5664 339 // return (i2c_do_read(obj, last, 0) & 0xFF);
mbed_official 337:6ed01c00b962 340 }
mbed_official 337:6ed01c00b962 341
mbed_official 509:53fc1beb5664 342 int i2c_byte_write(i2c_t *obj, int data) {
mbed_official 337:6ed01c00b962 343 int ack;
mbed_official 337:6ed01c00b962 344 int status = i2c_do_write(obj, (data & 0xFF), 0);
mbed_official 509:53fc1beb5664 345
mbed_official 337:6ed01c00b962 346 switch(status) {
mbed_official 509:53fc1beb5664 347 case 2: // TX RDY. Handles a Slave NAK on datawrite
mbed_official 337:6ed01c00b962 348 ack = 1;
mbed_official 337:6ed01c00b962 349 break;
mbed_official 337:6ed01c00b962 350 default:
mbed_official 337:6ed01c00b962 351 ack = 0;
mbed_official 337:6ed01c00b962 352 break;
mbed_official 337:6ed01c00b962 353 }
mbed_official 337:6ed01c00b962 354
mbed_official 337:6ed01c00b962 355 return ack;
mbed_official 337:6ed01c00b962 356 }
mbed_official 337:6ed01c00b962 357
mbed_official 509:53fc1beb5664 358
mbed_official 337:6ed01c00b962 359 #if DEVICE_I2CSLAVE
mbed_official 337:6ed01c00b962 360
mbed_official 509:53fc1beb5664 361 #define I2C_SLVDAT(x) (x->i2c->SLVDAT)
mbed_official 509:53fc1beb5664 362 #define I2C_SLVSTAT(x) ((x->i2c->STAT >> 9) & (0x03))
mbed_official 509:53fc1beb5664 363 #define I2C_SLVSI(x) ((x->i2c->STAT >> 8) & (0x01))
mbed_official 509:53fc1beb5664 364 //#define I2C_SLVCNT(x) (x->i2c->SLVCTL = (1 << 0))
mbed_official 509:53fc1beb5664 365 //#define I2C_SLVNAK(x) (x->i2c->SLVCTL = (1 << 1))
mbed_official 509:53fc1beb5664 366
mbed_official 509:53fc1beb5664 367 #if(0)
mbed_official 509:53fc1beb5664 368 // Wait until the Slave Serial Interrupt (SI) is set
mbed_official 509:53fc1beb5664 369 // Timeout when it takes too long.
mbed_official 509:53fc1beb5664 370 static int i2c_wait_slave_SI(i2c_t *obj) {
mbed_official 509:53fc1beb5664 371 int timeout = 0;
mbed_official 509:53fc1beb5664 372 while (!(obj->i2c->STAT & (1 << 8))) {
mbed_official 509:53fc1beb5664 373 timeout++;
mbed_official 509:53fc1beb5664 374 if (timeout > 100000) return -1;
mbed_official 337:6ed01c00b962 375 }
mbed_official 509:53fc1beb5664 376 return 0;
mbed_official 509:53fc1beb5664 377 }
mbed_official 509:53fc1beb5664 378 #endif
mbed_official 509:53fc1beb5664 379
mbed_official 509:53fc1beb5664 380 void i2c_slave_mode(i2c_t *obj, int enable_slave) {
mbed_official 509:53fc1beb5664 381
mbed_official 509:53fc1beb5664 382 if (enable_slave) {
mbed_official 509:53fc1beb5664 383 // obj->i2c->CFG &= ~(1 << 0); //Disable Master mode
mbed_official 509:53fc1beb5664 384 obj->i2c->CFG |= (1 << 1); //Enable Slave mode
mbed_official 509:53fc1beb5664 385 }
mbed_official 509:53fc1beb5664 386 else {
mbed_official 509:53fc1beb5664 387 // obj->i2c->CFG |= (1 << 0); //Enable Master mode
mbed_official 509:53fc1beb5664 388 obj->i2c->CFG &= ~(1 << 1); //Disable Slave mode
mbed_official 509:53fc1beb5664 389 }
mbed_official 509:53fc1beb5664 390 }
mbed_official 337:6ed01c00b962 391
mbed_official 509:53fc1beb5664 392 // Wait for next I2C event and find out what is going on
mbed_official 509:53fc1beb5664 393 //
mbed_official 509:53fc1beb5664 394 int i2c_slave_receive(i2c_t *obj) {
mbed_official 509:53fc1beb5664 395 int addr;
mbed_official 509:53fc1beb5664 396
mbed_official 509:53fc1beb5664 397 // Check if there is any data pending
mbed_official 509:53fc1beb5664 398 if (! I2C_SLVSI(obj)) {
mbed_official 509:53fc1beb5664 399 return 0; //NoData
mbed_official 509:53fc1beb5664 400 };
mbed_official 509:53fc1beb5664 401
mbed_official 509:53fc1beb5664 402 // Check State
mbed_official 509:53fc1beb5664 403 switch(I2C_SLVSTAT(obj)) {
mbed_official 509:53fc1beb5664 404 case 0x0: // Slave address plus R/W received
mbed_official 509:53fc1beb5664 405 // At least one of the four slave addresses has been matched by hardware.
mbed_official 509:53fc1beb5664 406 // You can figure out which address by checking Slave address match Index in STAT register.
mbed_official 509:53fc1beb5664 407
mbed_official 509:53fc1beb5664 408 // Get the received address
mbed_official 509:53fc1beb5664 409 addr = I2C_SLVDAT(obj) & 0xFF;
mbed_official 509:53fc1beb5664 410 // Send ACK on address and Continue
mbed_official 509:53fc1beb5664 411 obj->i2c->SLVCTL = (1 << 0);
mbed_official 509:53fc1beb5664 412
mbed_official 509:53fc1beb5664 413 if (addr == 0x00) {
mbed_official 509:53fc1beb5664 414 return 2; //WriteGeneral
mbed_official 509:53fc1beb5664 415 }
mbed_official 509:53fc1beb5664 416 //check the RW bit
mbed_official 509:53fc1beb5664 417 if ((addr & 0x01) == 0x01) {
mbed_official 509:53fc1beb5664 418 return 1; //ReadAddressed
mbed_official 509:53fc1beb5664 419 }
mbed_official 509:53fc1beb5664 420 else {
mbed_official 509:53fc1beb5664 421 return 3; //WriteAddressed
mbed_official 509:53fc1beb5664 422 }
mbed_official 509:53fc1beb5664 423 //break;
mbed_official 509:53fc1beb5664 424
mbed_official 509:53fc1beb5664 425 case 0x1: // Slave receive. Received data is available (Slave Receiver mode).
mbed_official 509:53fc1beb5664 426 // Oops, should never get here...
mbed_official 509:53fc1beb5664 427 obj->i2c->SLVCTL = (1 << 1); // Send NACK on received data, try to recover...
mbed_official 509:53fc1beb5664 428 return 0; //NoData
mbed_official 509:53fc1beb5664 429
mbed_official 509:53fc1beb5664 430 case 0x2: // Slave transmit. Data can be transmitted (Slave Transmitter mode).
mbed_official 509:53fc1beb5664 431 // Oops, should never get here...
mbed_official 509:53fc1beb5664 432 I2C_SLVDAT(obj) = 0xFF; // Send dummy data for transmission
mbed_official 509:53fc1beb5664 433 obj->i2c->SLVCTL = (1 << 0); // Continue and try to recover...
mbed_official 509:53fc1beb5664 434 return 0; //NoData
mbed_official 509:53fc1beb5664 435
mbed_official 509:53fc1beb5664 436 case 0x3: // Reserved.
mbed_official 509:53fc1beb5664 437 default: // Oops, should never get here...
mbed_official 509:53fc1beb5664 438 obj->i2c->SLVCTL = (1 << 0); // Continue and try to recover...
mbed_official 509:53fc1beb5664 439 return 0; //NoData
mbed_official 509:53fc1beb5664 440 //break;
mbed_official 509:53fc1beb5664 441 } //switch status
mbed_official 509:53fc1beb5664 442 }
mbed_official 509:53fc1beb5664 443
mbed_official 509:53fc1beb5664 444 // The dedicated I2C Slave byte read and byte write functions need to be called
mbed_official 509:53fc1beb5664 445 // from 'common' mbed I2CSlave API for devices that have separate Master and
mbed_official 509:53fc1beb5664 446 // Slave engines such as the lpc812 and lpc1549.
mbed_official 509:53fc1beb5664 447
mbed_official 509:53fc1beb5664 448 //Called when Slave is addressed for Write, Slave will receive Data in polling mode
mbed_official 509:53fc1beb5664 449 //Parameter last=1 means received byte will be NACKed.
mbed_official 509:53fc1beb5664 450 int i2c_slave_byte_read(i2c_t *obj, int last) {
mbed_official 509:53fc1beb5664 451 int data;
mbed_official 509:53fc1beb5664 452
mbed_official 509:53fc1beb5664 453 // Wait for data
mbed_official 509:53fc1beb5664 454 while (!I2C_SLVSI(obj)); // Wait forever
mbed_official 509:53fc1beb5664 455 //if (i2c_wait_slave_SI(obj) != 0) {return -2;} // Wait with timeout
mbed_official 509:53fc1beb5664 456
mbed_official 509:53fc1beb5664 457 // Dont bother to check State, were not returning it anyhow..
mbed_official 509:53fc1beb5664 458 //if (I2C_SLVSTAT(obj)) == 0x01) {
mbed_official 509:53fc1beb5664 459 // Slave receive. Received data is available (Slave Receiver mode).
mbed_official 509:53fc1beb5664 460 //};
mbed_official 509:53fc1beb5664 461
mbed_official 509:53fc1beb5664 462 data = I2C_SLVDAT(obj) & 0xFF; // Get and store the received data
mbed_official 509:53fc1beb5664 463 if (last) {
mbed_official 509:53fc1beb5664 464 obj->i2c->SLVCTL = (1 << 1); // Send NACK on received data and Continue
mbed_official 509:53fc1beb5664 465 }
mbed_official 509:53fc1beb5664 466 else {
mbed_official 509:53fc1beb5664 467 obj->i2c->SLVCTL = (1 << 0); // Send ACK on data and Continue to read
mbed_official 509:53fc1beb5664 468 }
mbed_official 509:53fc1beb5664 469
mbed_official 509:53fc1beb5664 470 return data;
mbed_official 337:6ed01c00b962 471 }
mbed_official 337:6ed01c00b962 472
mbed_official 509:53fc1beb5664 473
mbed_official 509:53fc1beb5664 474 //Called when Slave is addressed for Read, Slave will send Data in polling mode
mbed_official 509:53fc1beb5664 475 //
mbed_official 509:53fc1beb5664 476 int i2c_slave_byte_write(i2c_t *obj, int data) {
mbed_official 509:53fc1beb5664 477
mbed_official 509:53fc1beb5664 478 // Wait until Ready
mbed_official 509:53fc1beb5664 479 while (!I2C_SLVSI(obj)); // Wait forever
mbed_official 509:53fc1beb5664 480 // if (i2c_wait_slave_SI(obj) != 0) {return -2;} // Wait with timeout
mbed_official 509:53fc1beb5664 481
mbed_official 509:53fc1beb5664 482 // Check State
mbed_official 509:53fc1beb5664 483 switch(I2C_SLVSTAT(obj)) {
mbed_official 509:53fc1beb5664 484 case 0x0: // Slave address plus R/W received
mbed_official 509:53fc1beb5664 485 // At least one of the four slave addresses has been matched by hardware.
mbed_official 509:53fc1beb5664 486 // You can figure out which address by checking Slave address match Index in STAT register.
mbed_official 509:53fc1beb5664 487 // I2C Restart occurred
mbed_official 509:53fc1beb5664 488 return -1;
mbed_official 509:53fc1beb5664 489 //break;
mbed_official 509:53fc1beb5664 490 case 0x1: // Slave receive. Received data is available (Slave Receiver mode).
mbed_official 509:53fc1beb5664 491 // Should not get here...
mbed_official 509:53fc1beb5664 492 return -2;
mbed_official 509:53fc1beb5664 493 //break;
mbed_official 509:53fc1beb5664 494 case 0x2: // Slave transmit. Data can be transmitted (Slave Transmitter mode).
mbed_official 509:53fc1beb5664 495 I2C_SLVDAT(obj) = data & 0xFF; // Store the data for transmission
mbed_official 509:53fc1beb5664 496 obj->i2c->SLVCTL = (1 << 0); // Continue to send
mbed_official 509:53fc1beb5664 497
mbed_official 509:53fc1beb5664 498 return 1;
mbed_official 509:53fc1beb5664 499 //break;
mbed_official 509:53fc1beb5664 500 case 0x3: // Reserved.
mbed_official 509:53fc1beb5664 501 default:
mbed_official 509:53fc1beb5664 502 // Should not get here...
mbed_official 509:53fc1beb5664 503 return -3;
mbed_official 509:53fc1beb5664 504 //break;
mbed_official 509:53fc1beb5664 505 } // switch status
mbed_official 337:6ed01c00b962 506 }
mbed_official 337:6ed01c00b962 507
mbed_official 509:53fc1beb5664 508
mbed_official 509:53fc1beb5664 509 //Called when Slave is addressed for Write, Slave will receive Data in polling mode
mbed_official 509:53fc1beb5664 510 //Parameter length (>=1) is the maximum allowable number of bytes. All bytes will be ACKed.
mbed_official 509:53fc1beb5664 511 int i2c_slave_read(i2c_t *obj, char *data, int length) {
mbed_official 509:53fc1beb5664 512 int count=0;
mbed_official 509:53fc1beb5664 513
mbed_official 509:53fc1beb5664 514 // Read and ACK all expected bytes
mbed_official 509:53fc1beb5664 515 while (count < length) {
mbed_official 509:53fc1beb5664 516 // Wait for data
mbed_official 509:53fc1beb5664 517 while (!I2C_SLVSI(obj)); // Wait forever
mbed_official 509:53fc1beb5664 518 // if (i2c_wait_slave_SI(obj) != 0) {return -2;} // Wait with timeout
mbed_official 337:6ed01c00b962 519
mbed_official 509:53fc1beb5664 520 // Check State
mbed_official 509:53fc1beb5664 521 switch(I2C_SLVSTAT(obj)) {
mbed_official 509:53fc1beb5664 522 case 0x0: // Slave address plus R/W received
mbed_official 509:53fc1beb5664 523 // At least one of the four slave addresses has been matched by hardware.
mbed_official 509:53fc1beb5664 524 // You can figure out which address by checking Slave address match Index in STAT register.
mbed_official 509:53fc1beb5664 525 // I2C Restart occurred
mbed_official 509:53fc1beb5664 526 return -1;
mbed_official 509:53fc1beb5664 527 //break;
mbed_official 509:53fc1beb5664 528
mbed_official 509:53fc1beb5664 529 case 0x1: // Slave receive. Received data is available (Slave Receiver mode).
mbed_official 509:53fc1beb5664 530 data[count] = I2C_SLVDAT(obj) & 0xFF; // Get and store the received data
mbed_official 509:53fc1beb5664 531 obj->i2c->SLVCTL = (1 << 0); // Send ACK on data and Continue to read
mbed_official 509:53fc1beb5664 532 break;
mbed_official 509:53fc1beb5664 533
mbed_official 509:53fc1beb5664 534 case 0x2: // Slave transmit. Data can be transmitted (Slave Transmitter mode).
mbed_official 509:53fc1beb5664 535 case 0x3: // Reserved.
mbed_official 509:53fc1beb5664 536 default: // Should never get here...
mbed_official 509:53fc1beb5664 537 return -2;
mbed_official 509:53fc1beb5664 538 //break;
mbed_official 509:53fc1beb5664 539 } // switch status
mbed_official 509:53fc1beb5664 540
mbed_official 509:53fc1beb5664 541 count++;
mbed_official 509:53fc1beb5664 542 } // for all bytes
mbed_official 509:53fc1beb5664 543
mbed_official 509:53fc1beb5664 544 return count; // Received the expected number of bytes
mbed_official 337:6ed01c00b962 545 }
mbed_official 337:6ed01c00b962 546
mbed_official 509:53fc1beb5664 547
mbed_official 509:53fc1beb5664 548 //Called when Slave is addressed for Read, Slave will send Data in polling mode
mbed_official 509:53fc1beb5664 549 //Parameter length (>=1) is the maximum number of bytes. Exit when Slave byte is NACKed.
mbed_official 509:53fc1beb5664 550 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
mbed_official 509:53fc1beb5664 551 int count;
mbed_official 509:53fc1beb5664 552
mbed_official 509:53fc1beb5664 553 // Send and all bytes or Exit on NAK
mbed_official 509:53fc1beb5664 554 for (count=0; count < length; count++) {
mbed_official 509:53fc1beb5664 555 // Wait until Ready for data
mbed_official 509:53fc1beb5664 556 while (!I2C_SLVSI(obj)); // Wait forever
mbed_official 509:53fc1beb5664 557 // if (i2c_wait_slave_SI(obj) != 0) {return -2;} // Wait with timeout
mbed_official 337:6ed01c00b962 558
mbed_official 509:53fc1beb5664 559 // Check State
mbed_official 509:53fc1beb5664 560 switch(I2C_SLVSTAT(obj)) {
mbed_official 509:53fc1beb5664 561 case 0x0: // Slave address plus R/W received
mbed_official 509:53fc1beb5664 562 // At least one of the four slave addresses has been matched by hardware.
mbed_official 509:53fc1beb5664 563 // You can figure out which address by checking Slave address match Index in STAT register.
mbed_official 509:53fc1beb5664 564 // I2C Restart occurred
mbed_official 509:53fc1beb5664 565 return -1;
mbed_official 509:53fc1beb5664 566 //break;
mbed_official 509:53fc1beb5664 567 case 0x1: // Slave receive. Received data is available (Slave Receiver mode).
mbed_official 509:53fc1beb5664 568 // Should not get here...
mbed_official 509:53fc1beb5664 569 return -2;
mbed_official 509:53fc1beb5664 570 //break;
mbed_official 509:53fc1beb5664 571 case 0x2: // Slave transmit. Data can be transmitted (Slave Transmitter mode).
mbed_official 509:53fc1beb5664 572 I2C_SLVDAT(obj) = data[count] & 0xFF; // Store the data for transmission
mbed_official 509:53fc1beb5664 573 obj->i2c->SLVCTL = (1 << 0); // Continue to send
mbed_official 509:53fc1beb5664 574 break;
mbed_official 509:53fc1beb5664 575 case 0x3: // Reserved.
mbed_official 509:53fc1beb5664 576 default:
mbed_official 509:53fc1beb5664 577 // Should not get here...
mbed_official 509:53fc1beb5664 578 return -3;
mbed_official 509:53fc1beb5664 579 //break;
mbed_official 509:53fc1beb5664 580 } // switch status
mbed_official 509:53fc1beb5664 581 } // for all bytes
mbed_official 509:53fc1beb5664 582
mbed_official 509:53fc1beb5664 583 return length; // Transmitted the max number of bytes
mbed_official 337:6ed01c00b962 584 }
mbed_official 337:6ed01c00b962 585
mbed_official 509:53fc1beb5664 586
mbed_official 509:53fc1beb5664 587 // Set the four slave addresses.
mbed_official 509:53fc1beb5664 588 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
mbed_official 509:53fc1beb5664 589 obj->i2c->SLVADR0 = (address & 0xFE); // Store address in address 0 register
mbed_official 509:53fc1beb5664 590 obj->i2c->SLVADR1 = (0x00 & 0xFE); // Store general call write address in address 1 register
mbed_official 509:53fc1beb5664 591 obj->i2c->SLVADR2 = (0x01); // Disable address 2 register
mbed_official 509:53fc1beb5664 592 obj->i2c->SLVADR3 = (0x01); // Disable address 3 register
mbed_official 509:53fc1beb5664 593 obj->i2c->SLVQUAL0 = (mask & 0xFE); // Qualifier mask for address 0 register. Any maskbit that is 1 will always be a match
mbed_official 337:6ed01c00b962 594 }
mbed_official 337:6ed01c00b962 595
mbed_official 337:6ed01c00b962 596 #endif
mbed_official 337:6ed01c00b962 597
mbed_official 337:6ed01c00b962 598 #endif