mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Oct 28 11:17:30 2016 +0100
Revision:
149:156823d33999
This updates the lib to the mbed lib v128

NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /**
<> 149:156823d33999 2 ******************************************************************************
<> 149:156823d33999 3 * @file i2c.c
<> 149:156823d33999 4 * @brief I2C driver
<> 149:156823d33999 5 * @internal
<> 149:156823d33999 6 * @author ON Semiconductor
<> 149:156823d33999 7 * $Rev: $
<> 149:156823d33999 8 * $Date: 2016-04-12 $
<> 149:156823d33999 9 ******************************************************************************
<> 149:156823d33999 10 * Copyright 2016 Semiconductor Components Industries LLC (d/b/a “ON Semiconductor”).
<> 149:156823d33999 11 * All rights reserved. This software and/or documentation is licensed by ON Semiconductor
<> 149:156823d33999 12 * under limited terms and conditions. The terms and conditions pertaining to the software
<> 149:156823d33999 13 * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf
<> 149:156823d33999 14 * (“ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software”) and
<> 149:156823d33999 15 * if applicable the software license agreement. Do not use this software and/or
<> 149:156823d33999 16 * documentation unless you have carefully read and you agree to the limited terms and
<> 149:156823d33999 17 * conditions. By using this software and/or documentation, you agree to the limited
<> 149:156823d33999 18 * terms and conditions.
<> 149:156823d33999 19 *
<> 149:156823d33999 20 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
<> 149:156823d33999 21 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
<> 149:156823d33999 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
<> 149:156823d33999 23 * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
<> 149:156823d33999 24 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
<> 149:156823d33999 25 * @endinternal
<> 149:156823d33999 26 *
<> 149:156823d33999 27 * @ingroup i2c
<> 149:156823d33999 28 *
<> 149:156823d33999 29 * @details
<> 149:156823d33999 30 *
<> 149:156823d33999 31 * <h1> Reference document(s) </h1>
<> 149:156823d33999 32 * <p>
<> 149:156823d33999 33 * IPC7208 APB I2C Master Design Specification v1.3
<> 149:156823d33999 34 * </p>
<> 149:156823d33999 35 * The I2C bus is an industry-standard two-wire (clock and data) serial communication bus between master(initiator) and slave device.
<> 149:156823d33999 36 * Within the procedure of the I2C-bus, unique situations arise which are defined as START and STOP conditions .A HIGH to LOW transition on
<> 149:156823d33999 37 * the SDA line while SCL is HIGH is one such unique case. This situation indicates a START condition.A LOW to HIGH transition on the
<> 149:156823d33999 38 * SDA line while SCL is HIGH defines a STOP condition.START and STOP conditions are always generated by the master. The bus is considered
<> 149:156823d33999 39 * to be busy after the START condition. The bus is considered to be free again a certain time after the STOP condition.
<> 149:156823d33999 40 * A master may start a transfer only if the bus is free. Two or more masters may generate a START condition.
<> 149:156823d33999 41 * Every byte put on the SDA line must be 8-bits long.Each byte has to be followed by an acknowledge bit.
<> 149:156823d33999 42 * This APB(Advanced peripheral bus) I2C Master is an APB Slave peripheral that can also serves as an I2C bus Master. The Command register
<> 149:156823d33999 43 * is the programming interface to the I2C Engine. The commands arrive at the I2C Engine via the Command FIFO,so the first valid command
<> 149:156823d33999 44 * that is written to the Command register is the first I2C instruction implemented on the I2C bus.Because the command interface provides
<> 149:156823d33999 45 * the basic building blocks for any I2C transaction, access to a wide range of I2C slave devices is supported.
<> 149:156823d33999 46 * I2C can be enabled by setting bit 7 of the control register .
<> 149:156823d33999 47 * There is a generated clock (a divided version of the APB clock) in this module that may be used as the I2C System Clock.
<> 149:156823d33999 48 * There are two FIFO in the I2C; Command FIFO and Read data FIFO
<> 149:156823d33999 49 * The commands(I2C instructions) and data arrive at the I2C Engine via the Command FIFO.
<> 149:156823d33999 50 * if the command FIFO is empty , up to 32 commands can be written to the command interface , it is programmer's responsibility to keep
<> 149:156823d33999 51 * the track of command FIFO's status either by interrupt or by polling method by reading status register, which represents Operational
<> 149:156823d33999 52 * Status of the I2C Module and its sub-modules.The action from the processor may be necessary after reading the status register.Reading
<> 149:156823d33999 53 * the Status register clears the blkInt Interrupt signal.Read data FIFO is where data read by the processor from I2C slave is placed .
<> 149:156823d33999 54 *
<> 149:156823d33999 55 *
<> 149:156823d33999 56 * <h1> Functional description (internal) </h1>
<> 149:156823d33999 57 * <p>
<> 149:156823d33999 58 *
<> 149:156823d33999 59 * </p>
<> 149:156823d33999 60 */
<> 149:156823d33999 61 #if DEVICE_I2C
<> 149:156823d33999 62 #include "i2c.h"
<> 149:156823d33999 63
<> 149:156823d33999 64 /* See i2c.h for details */
<> 149:156823d33999 65 void fI2cInit(i2c_t *obj,PinName sda,PinName scl)
<> 149:156823d33999 66 {
<> 149:156823d33999 67 uint32_t clockDivisor;
<> 149:156823d33999 68 /* determine the I2C to use */
<> 149:156823d33999 69 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
<> 149:156823d33999 70 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
<> 149:156823d33999 71 obj->membase = (I2cIpc7208Reg_pt)pinmap_merge(i2c_sda, i2c_scl);
<> 149:156823d33999 72 MBED_ASSERT((int)obj->membase != NC);
<> 149:156823d33999 73
<> 149:156823d33999 74 /* By default disbale interrupts */
<> 149:156823d33999 75 obj->membase->IER.WORD = False;
<> 149:156823d33999 76
<> 149:156823d33999 77 /* enable interrupt associated with the device */
<> 149:156823d33999 78 if(obj->membase == I2C1REG) {
<> 149:156823d33999 79 CLOCK_ENABLE(CLOCK_I2C); /* enable i2c peripheral */
<> 149:156823d33999 80 NVIC_ClearPendingIRQ(I2C_IRQn);
<> 149:156823d33999 81 NVIC_EnableIRQ(I2C_IRQn);
<> 149:156823d33999 82 } else {
<> 149:156823d33999 83 CLOCK_ENABLE(CLOCK_I2C2); /* enable i2c peripheral */
<> 149:156823d33999 84 NVIC_ClearPendingIRQ(I2C2_IRQn);
<> 149:156823d33999 85 NVIC_EnableIRQ(I2C2_IRQn);
<> 149:156823d33999 86 }
<> 149:156823d33999 87
<> 149:156823d33999 88 /*select I2C clock source */
<> 149:156823d33999 89 obj->membase->CR.BITS.I2C_CLK_SRC = True;
<> 149:156823d33999 90
<> 149:156823d33999 91 /* enable I2C clock divider */
<> 149:156823d33999 92 obj->membase->CR.BITS.I2C_APB_CD_EN = True;
<> 149:156823d33999 93
<> 149:156823d33999 94 /* set default baud rate at 100k */
<> 149:156823d33999 95 clockDivisor = ((fClockGetPeriphClockfrequency() / 100000) >> 2) - 2;
<> 149:156823d33999 96 obj->membase->CR.BITS.CD_VAL = (clockDivisor & I2C_CLOCKDIVEDER_VAL_MASK);
<> 149:156823d33999 97 obj->membase->PRE_SCALE_REG = (clockDivisor & I2C_APB_CLK_DIVIDER_VAL_MASK) >> 5; /**< Zero pre-scale value not allowed */
<> 149:156823d33999 98
<> 149:156823d33999 99 /* Cross bar setting */
<> 149:156823d33999 100 pinmap_pinout(sda, PinMap_I2C_SDA);
<> 149:156823d33999 101 pinmap_pinout(scl, PinMap_I2C_SCL);
<> 149:156823d33999 102
<> 149:156823d33999 103 /*Enable open drain & pull up for sda & scl pin */
<> 149:156823d33999 104 pin_mode(sda, OpenDrainPullUp);
<> 149:156823d33999 105 pin_mode(scl, OpenDrainPullUp);
<> 149:156823d33999 106
<> 149:156823d33999 107 /* PAD drive strength */
<> 149:156823d33999 108 PadReg_t *padRegSda = (PadReg_t*)(PADREG_BASE + (sda * PAD_REG_ADRS_BYTE_SIZE));
<> 149:156823d33999 109 PadReg_t *padRegScl = (PadReg_t*)(PADREG_BASE + (scl * PAD_REG_ADRS_BYTE_SIZE));
<> 149:156823d33999 110
<> 149:156823d33999 111 CLOCK_ENABLE(CLOCK_PAD);
<> 149:156823d33999 112 padRegSda->PADIO0.BITS.POWER = 1; /* sda: Drive strength */
<> 149:156823d33999 113 padRegScl->PADIO0.BITS.POWER = 1; /* scl: Drive strength */
<> 149:156823d33999 114 CLOCK_DISABLE(CLOCK_PAD);
<> 149:156823d33999 115
<> 149:156823d33999 116 CLOCK_ENABLE(CLOCK_GPIO);
<> 149:156823d33999 117 GPIOREG->W_OUT |= ((True << sda) | (True << scl));
<> 149:156823d33999 118 CLOCK_DISABLE(CLOCK_GPIO);
<> 149:156823d33999 119
<> 149:156823d33999 120 /* Enable i2c module */
<> 149:156823d33999 121 obj->membase->CR.BITS.I2C_MODULE_EN = True;
<> 149:156823d33999 122 }
<> 149:156823d33999 123
<> 149:156823d33999 124 /* See i2c.h for details */
<> 149:156823d33999 125 void fI2cFrequency(i2c_t *obj, uint32_t hz)
<> 149:156823d33999 126 {
<> 149:156823d33999 127 /* Set user baud rate */
<> 149:156823d33999 128 uint32_t clockDivisor;
<> 149:156823d33999 129 clockDivisor = ((fClockGetPeriphClockfrequency() / hz) >> 2) - 2;
<> 149:156823d33999 130 obj->membase->CR.BITS.CD_VAL = (clockDivisor & I2C_CLOCKDIVEDER_VAL_MASK);
<> 149:156823d33999 131 obj->membase->PRE_SCALE_REG = (clockDivisor & I2C_APB_CLK_DIVIDER_VAL_MASK) >> 5; /**< Zero pre-scale value not allowed */
<> 149:156823d33999 132 }
<> 149:156823d33999 133
<> 149:156823d33999 134 /* See i2c.h for details */
<> 149:156823d33999 135 int32_t fI2cStart(i2c_t *obj)
<> 149:156823d33999 136 {
<> 149:156823d33999 137 /* Send start bit */
<> 149:156823d33999 138 obj->membase->CMD_REG = I2C_CMD_START;
<> 149:156823d33999 139 return I2C_API_STATUS_SUCCESS;
<> 149:156823d33999 140 }
<> 149:156823d33999 141
<> 149:156823d33999 142 /* See i2c.h for details */
<> 149:156823d33999 143 int32_t fI2cStop(i2c_t *obj)
<> 149:156823d33999 144 {
<> 149:156823d33999 145 /* Send stop bit */
<> 149:156823d33999 146 obj->membase->CMD_REG = I2C_CMD_STOP;
<> 149:156823d33999 147 if (obj->membase->STATUS.WORD & (I2C_STATUS_CMD_FIFO_FULL_BIT |
<> 149:156823d33999 148 I2C_STATUS_CMD_FIFO_OFL_BIT |
<> 149:156823d33999 149 I2C_STATUS_BUS_ERR_BIT)) {
<> 149:156823d33999 150 /* I2c error occured */
<> 149:156823d33999 151 return I2C_ERROR_BUS_BUSY;
<> 149:156823d33999 152 }
<> 149:156823d33999 153 return I2C_API_STATUS_SUCCESS;
<> 149:156823d33999 154 }
<> 149:156823d33999 155
<> 149:156823d33999 156 /* See i2c.h for details */
<> 149:156823d33999 157 int32_t fI2cReadB(i2c_t *d, char *buf, int len)
<> 149:156823d33999 158 {
<> 149:156823d33999 159 int32_t read = 0;
<> 149:156823d33999 160
<> 149:156823d33999 161 while (read < len) {
<> 149:156823d33999 162 /* Send read command */
<> 149:156823d33999 163 d->membase->CMD_REG = I2C_CMD_RDAT8;
<> 149:156823d33999 164 while(!RD_DATA_READY) {
<> 149:156823d33999 165 if (I2C_BUS_ERR_CHECK) {
<> 149:156823d33999 166 /* Bus error occured */
<> 149:156823d33999 167 return I2C_ERROR_BUS_BUSY;
<> 149:156823d33999 168 }
<> 149:156823d33999 169 }
<> 149:156823d33999 170 buf[read++] = d->membase->RD_FIFO_REG; /**< Reading 'read FIFO register' will clear status register */
<> 149:156823d33999 171
<> 149:156823d33999 172 if(!(read>=len)) { /* No ACK will be generated for the last read, upper level I2C protocol should generate */
<> 149:156823d33999 173 d->membase->CMD_REG=I2C_CMD_WDAT0; /* TODO based on requirement generate ACK or NACK Based on the requirement. */
<> 149:156823d33999 174 }
<> 149:156823d33999 175
<> 149:156823d33999 176 /* check for FIFO underflow */
<> 149:156823d33999 177 if(I2C_UFL_CHECK) {
<> 149:156823d33999 178 return I2C_ERROR_NO_SLAVE; /* TODO No error available for this in i2c_api.h */
<> 149:156823d33999 179 }
<> 149:156823d33999 180 if(I2C_BUS_ERR_CHECK) {
<> 149:156823d33999 181 /* Bus error */
<> 149:156823d33999 182 return I2C_ERROR_BUS_BUSY;
<> 149:156823d33999 183 }
<> 149:156823d33999 184 }
<> 149:156823d33999 185
<> 149:156823d33999 186 return read;
<> 149:156823d33999 187 }
<> 149:156823d33999 188
<> 149:156823d33999 189 /* See i2c.h for details */
<> 149:156823d33999 190 int32_t fI2cWriteB(i2c_t *d, const char *buf, int len)
<> 149:156823d33999 191 {
<> 149:156823d33999 192 int32_t write = 0;
<> 149:156823d33999 193
<> 149:156823d33999 194 while (write < len) {
<> 149:156823d33999 195 /* Send write command */
<> 149:156823d33999 196 d->membase->CMD_REG = I2C_CMD_WDAT8;
<> 149:156823d33999 197 if(buf[write] == I2C_CMD_RDAT8) {
<> 149:156823d33999 198 /* SW work around to counter FSM issue. If the only command in the CMD FIFO is the WDAT8 command (data of 0x13)
<> 149:156823d33999 199 then as the command is read out (i.e. the FIFO goes empty), the WDAT8 command will be misinterpreted as a
<> 149:156823d33999 200 RDAT8 command by the data FSM; resulting in an I2C bus error (NACK instead of an ACK). */
<> 149:156823d33999 201 /* Send 0x13 bit wise */
<> 149:156823d33999 202 d->membase->CMD_REG = I2C_CMD_WDAT0;
<> 149:156823d33999 203 d->membase->CMD_REG = I2C_CMD_WDAT0;
<> 149:156823d33999 204 d->membase->CMD_REG = I2C_CMD_WDAT0;
<> 149:156823d33999 205 d->membase->CMD_REG = I2C_CMD_WDAT1;
<> 149:156823d33999 206
<> 149:156823d33999 207 d->membase->CMD_REG = I2C_CMD_WDAT0;
<> 149:156823d33999 208 d->membase->CMD_REG = I2C_CMD_WDAT0;
<> 149:156823d33999 209 d->membase->CMD_REG = I2C_CMD_WDAT1;
<> 149:156823d33999 210 d->membase->CMD_REG = I2C_CMD_WDAT1;
<> 149:156823d33999 211 } else {
<> 149:156823d33999 212 /* Send data */
<> 149:156823d33999 213 d->membase->CMD_REG = buf[write++];
<> 149:156823d33999 214 }
<> 149:156823d33999 215 d->membase->CMD_REG = I2C_CMD_VRFY_ACK; /* TODO Verify ACK based on requirement, Do we need? */
<> 149:156823d33999 216
<> 149:156823d33999 217 while(FIFO_OFL_CHECK); /* Wait till command overflow ends */
<> 149:156823d33999 218
<> 149:156823d33999 219 if (I2C_BUS_ERR_CHECK) {
<> 149:156823d33999 220 /* Bus error */
<> 149:156823d33999 221 return I2C_ERROR_BUS_BUSY;
<> 149:156823d33999 222 }
<> 149:156823d33999 223 }
<> 149:156823d33999 224
<> 149:156823d33999 225 return write;
<> 149:156823d33999 226 }
<> 149:156823d33999 227
<> 149:156823d33999 228 #endif /* DEVICE_I2C */