test

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Tue Nov 08 17:45:16 2016 +0000
Revision:
150:02e0a0aed4ec
This updates the lib to the mbed lib v129

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 150:02e0a0aed4ec 1 /*******************************************************************************
<> 150:02e0a0aed4ec 2 * Copyright (c) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 150:02e0a0aed4ec 3 *
<> 150:02e0a0aed4ec 4 * Permission is hereby granted, free of charge, to any person obtaining a
<> 150:02e0a0aed4ec 5 * copy of this software and associated documentation files (the "Software"),
<> 150:02e0a0aed4ec 6 * to deal in the Software without restriction, including without limitation
<> 150:02e0a0aed4ec 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 150:02e0a0aed4ec 8 * and/or sell copies of the Software, and to permit persons to whom the
<> 150:02e0a0aed4ec 9 * Software is furnished to do so, subject to the following conditions:
<> 150:02e0a0aed4ec 10 *
<> 150:02e0a0aed4ec 11 * The above copyright notice and this permission notice shall be included
<> 150:02e0a0aed4ec 12 * in all copies or substantial portions of the Software.
<> 150:02e0a0aed4ec 13 *
<> 150:02e0a0aed4ec 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 150:02e0a0aed4ec 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 150:02e0a0aed4ec 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 150:02e0a0aed4ec 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 150:02e0a0aed4ec 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 150:02e0a0aed4ec 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 150:02e0a0aed4ec 20 * OTHER DEALINGS IN THE SOFTWARE.
<> 150:02e0a0aed4ec 21 *
<> 150:02e0a0aed4ec 22 * Except as contained in this notice, the name of Maxim Integrated
<> 150:02e0a0aed4ec 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 150:02e0a0aed4ec 24 * Products, Inc. Branding Policy.
<> 150:02e0a0aed4ec 25 *
<> 150:02e0a0aed4ec 26 * The mere transfer of this software does not imply any licenses
<> 150:02e0a0aed4ec 27 * of trade secrets, proprietary technology, copyrights, patents,
<> 150:02e0a0aed4ec 28 * trademarks, maskwork rights, or any other form of intellectual
<> 150:02e0a0aed4ec 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 150:02e0a0aed4ec 30 * ownership rights.
<> 150:02e0a0aed4ec 31 *******************************************************************************
<> 150:02e0a0aed4ec 32 */
<> 150:02e0a0aed4ec 33
<> 150:02e0a0aed4ec 34 #include "mbed_assert.h"
<> 150:02e0a0aed4ec 35 #include "i2c_api.h"
<> 150:02e0a0aed4ec 36 #include "i2cm_regs.h"
<> 150:02e0a0aed4ec 37 #include "i2cm.h"
<> 150:02e0a0aed4ec 38 #include "pinmap.h"
<> 150:02e0a0aed4ec 39 #include "PeripheralPins.h"
<> 150:02e0a0aed4ec 40
<> 150:02e0a0aed4ec 41 #ifndef MXC_I2CM_RX_TIMEOUT
<> 150:02e0a0aed4ec 42 #define MXC_I2CM_RX_TIMEOUT 0x5000
<> 150:02e0a0aed4ec 43 #endif
<> 150:02e0a0aed4ec 44
<> 150:02e0a0aed4ec 45 #define MBED_NAK 0
<> 150:02e0a0aed4ec 46 #define MBED_ACK 1
<> 150:02e0a0aed4ec 47 #define MBED_TIMEOUT 2
<> 150:02e0a0aed4ec 48
<> 150:02e0a0aed4ec 49 //******************************************************************************
<> 150:02e0a0aed4ec 50 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
<> 150:02e0a0aed4ec 51 {
<> 150:02e0a0aed4ec 52 // SDA and SCL must map to same peripheral instance
<> 150:02e0a0aed4ec 53 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
<> 150:02e0a0aed4ec 54 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
<> 150:02e0a0aed4ec 55 mxc_i2cm_regs_t *i2c = (mxc_i2cm_regs_t*)pinmap_merge(i2c_sda, i2c_scl);
<> 150:02e0a0aed4ec 56 MBED_ASSERT((int)i2c != NC);
<> 150:02e0a0aed4ec 57
<> 150:02e0a0aed4ec 58 obj->i2c = i2c;
<> 150:02e0a0aed4ec 59 obj->fifo = MXC_I2CM_GET_FIFO(MXC_I2CM_GET_IDX(i2c));
<> 150:02e0a0aed4ec 60 obj->start_pending = 0;
<> 150:02e0a0aed4ec 61
<> 150:02e0a0aed4ec 62 // Merge pin function requests for use with CMSIS init func
<> 150:02e0a0aed4ec 63 ioman_req_t io_req;
<> 150:02e0a0aed4ec 64 pin_function_t *pin_func;
<> 150:02e0a0aed4ec 65 pin_func = (pin_function_t *)pinmap_find_function(sda, PinMap_I2C_SDA);
<> 150:02e0a0aed4ec 66 io_req.value = pin_func->req_val;
<> 150:02e0a0aed4ec 67 pin_func = (pin_function_t *)pinmap_find_function(scl, PinMap_I2C_SCL);
<> 150:02e0a0aed4ec 68 io_req.value |= pin_func->req_val;
<> 150:02e0a0aed4ec 69
<> 150:02e0a0aed4ec 70 sys_cfg_i2cm_t sys_cfg;
<> 150:02e0a0aed4ec 71 sys_cfg.io_cfg.req_reg = pin_func->reg_req;
<> 150:02e0a0aed4ec 72 sys_cfg.io_cfg.ack_reg = pin_func->reg_ack;
<> 150:02e0a0aed4ec 73 sys_cfg.io_cfg.req_val = io_req;
<> 150:02e0a0aed4ec 74 sys_cfg.clk_scale = CLKMAN_SCALE_DIV_1;
<> 150:02e0a0aed4ec 75
<> 150:02e0a0aed4ec 76 I2CM_Init(obj->i2c, &sys_cfg, I2CM_SPEED_400KHZ);
<> 150:02e0a0aed4ec 77 }
<> 150:02e0a0aed4ec 78
<> 150:02e0a0aed4ec 79 //******************************************************************************
<> 150:02e0a0aed4ec 80 void i2c_frequency(i2c_t *obj, int hz)
<> 150:02e0a0aed4ec 81 {
<> 150:02e0a0aed4ec 82 I2CM_SetFrequency(obj->i2c, hz);
<> 150:02e0a0aed4ec 83 }
<> 150:02e0a0aed4ec 84
<> 150:02e0a0aed4ec 85 //******************************************************************************
<> 150:02e0a0aed4ec 86 int i2c_start(i2c_t *obj)
<> 150:02e0a0aed4ec 87 {
<> 150:02e0a0aed4ec 88 obj->start_pending = 1;
<> 150:02e0a0aed4ec 89 return 0;
<> 150:02e0a0aed4ec 90 }
<> 150:02e0a0aed4ec 91
<> 150:02e0a0aed4ec 92 //******************************************************************************
<> 150:02e0a0aed4ec 93 int i2c_stop(i2c_t *obj)
<> 150:02e0a0aed4ec 94 {
<> 150:02e0a0aed4ec 95 obj->start_pending = 0;
<> 150:02e0a0aed4ec 96 I2CM_WriteTxFifo(obj->i2c, obj->fifo, MXC_S_I2CM_TRANS_TAG_STOP);
<> 150:02e0a0aed4ec 97 I2CM_TxInProgress(obj->i2c);
<> 150:02e0a0aed4ec 98 return 0;
<> 150:02e0a0aed4ec 99 }
<> 150:02e0a0aed4ec 100
<> 150:02e0a0aed4ec 101 //******************************************************************************
<> 150:02e0a0aed4ec 102 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
<> 150:02e0a0aed4ec 103 {
<> 150:02e0a0aed4ec 104 MBED_ASSERT(stop != 0);
<> 150:02e0a0aed4ec 105 return I2CM_Read(obj->i2c, address >> 1, NULL, 0, (uint8_t *)data, length);
<> 150:02e0a0aed4ec 106 }
<> 150:02e0a0aed4ec 107
<> 150:02e0a0aed4ec 108 //******************************************************************************
<> 150:02e0a0aed4ec 109 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
<> 150:02e0a0aed4ec 110 {
<> 150:02e0a0aed4ec 111 mxc_i2cm_regs_t *i2cm = obj->i2c;
<> 150:02e0a0aed4ec 112 mxc_i2cm_fifo_regs_t *fifo = obj->fifo;
<> 150:02e0a0aed4ec 113
<> 150:02e0a0aed4ec 114 if (stop) {
<> 150:02e0a0aed4ec 115 return I2CM_Write(i2cm, address >> 1, NULL, 0, (uint8_t *)data, length);
<> 150:02e0a0aed4ec 116 }
<> 150:02e0a0aed4ec 117
<> 150:02e0a0aed4ec 118 i2cm->inten = 0;
<> 150:02e0a0aed4ec 119 i2cm->intfl = i2cm->intfl;
<> 150:02e0a0aed4ec 120 if (I2CM_Tx(i2cm, fifo, address >> 1, (uint8_t *)data, length, 0) == E_NO_ERROR) {
<> 150:02e0a0aed4ec 121 return length;
<> 150:02e0a0aed4ec 122 } else {
<> 150:02e0a0aed4ec 123 return -1;
<> 150:02e0a0aed4ec 124 }
<> 150:02e0a0aed4ec 125 }
<> 150:02e0a0aed4ec 126
<> 150:02e0a0aed4ec 127 //******************************************************************************
<> 150:02e0a0aed4ec 128 void i2c_reset(i2c_t *obj)
<> 150:02e0a0aed4ec 129 {
<> 150:02e0a0aed4ec 130 I2CM_Recover(obj->i2c);
<> 150:02e0a0aed4ec 131 }
<> 150:02e0a0aed4ec 132
<> 150:02e0a0aed4ec 133 //******************************************************************************
<> 150:02e0a0aed4ec 134 int i2c_byte_read(i2c_t *obj, int last)
<> 150:02e0a0aed4ec 135 {
<> 150:02e0a0aed4ec 136 mxc_i2cm_regs_t *i2cm = obj->i2c;
<> 150:02e0a0aed4ec 137 mxc_i2cm_fifo_regs_t *fifo = obj->fifo;
<> 150:02e0a0aed4ec 138 int tmp;
<> 150:02e0a0aed4ec 139
<> 150:02e0a0aed4ec 140 // Start the transaction if it is not currently ongoing
<> 150:02e0a0aed4ec 141 if (!(i2cm->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
<> 150:02e0a0aed4ec 142 i2cm->trans |= MXC_F_I2CM_TRANS_TX_START;
<> 150:02e0a0aed4ec 143 }
<> 150:02e0a0aed4ec 144
<> 150:02e0a0aed4ec 145 if (last) {
<> 150:02e0a0aed4ec 146 // NACK the last read byte
<> 150:02e0a0aed4ec 147 if (I2CM_WriteTxFifo(i2cm, fifo, MXC_S_I2CM_TRANS_TAG_RXDATA_NACK) != E_NO_ERROR) {
<> 150:02e0a0aed4ec 148 goto byte_write_err;
<> 150:02e0a0aed4ec 149 }
<> 150:02e0a0aed4ec 150
<> 150:02e0a0aed4ec 151 // Send the stop condition
<> 150:02e0a0aed4ec 152 if (I2CM_WriteTxFifo(i2cm, fifo, MXC_S_I2CM_TRANS_TAG_STOP) != E_NO_ERROR) {
<> 150:02e0a0aed4ec 153 goto byte_write_err;
<> 150:02e0a0aed4ec 154 }
<> 150:02e0a0aed4ec 155 } else {
<> 150:02e0a0aed4ec 156 if (I2CM_WriteTxFifo(i2cm, fifo, MXC_S_I2CM_TRANS_TAG_RXDATA_COUNT) != E_NO_ERROR) {
<> 150:02e0a0aed4ec 157 goto byte_write_err;
<> 150:02e0a0aed4ec 158 }
<> 150:02e0a0aed4ec 159 }
<> 150:02e0a0aed4ec 160
<> 150:02e0a0aed4ec 161 do {
<> 150:02e0a0aed4ec 162 // Wait for data in RX FIFO
<> 150:02e0a0aed4ec 163 int timeout = MXC_I2CM_RX_TIMEOUT;
<> 150:02e0a0aed4ec 164 while (!(i2cm->intfl & MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY) &&
<> 150:02e0a0aed4ec 165 ((i2cm->bb & MXC_F_I2CM_BB_RX_FIFO_CNT) == 0)) {
<> 150:02e0a0aed4ec 166
<> 150:02e0a0aed4ec 167 if((timeout-- < 0) || (i2cm->trans & MXC_F_I2CM_TRANS_TX_TIMEOUT)) {
<> 150:02e0a0aed4ec 168 goto byte_write_err;
<> 150:02e0a0aed4ec 169 }
<> 150:02e0a0aed4ec 170
<> 150:02e0a0aed4ec 171 if (i2cm->trans & (MXC_F_I2CM_TRANS_TX_LOST_ARBITR | MXC_F_I2CM_TRANS_TX_NACKED)) {
<> 150:02e0a0aed4ec 172 goto byte_write_err;
<> 150:02e0a0aed4ec 173 }
<> 150:02e0a0aed4ec 174 }
<> 150:02e0a0aed4ec 175 i2cm->intfl = MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY;
<> 150:02e0a0aed4ec 176
<> 150:02e0a0aed4ec 177 } while ((tmp = fifo->rx) & MXC_S_I2CM_RSTLS_TAG_EMPTY);
<> 150:02e0a0aed4ec 178
<> 150:02e0a0aed4ec 179 return (uint8_t)tmp;
<> 150:02e0a0aed4ec 180
<> 150:02e0a0aed4ec 181 byte_write_err:
<> 150:02e0a0aed4ec 182 i2c_reset(obj);
<> 150:02e0a0aed4ec 183 return -1;
<> 150:02e0a0aed4ec 184 }
<> 150:02e0a0aed4ec 185
<> 150:02e0a0aed4ec 186 //******************************************************************************
<> 150:02e0a0aed4ec 187 int i2c_byte_write(i2c_t *obj, int data)
<> 150:02e0a0aed4ec 188 {
<> 150:02e0a0aed4ec 189 mxc_i2cm_regs_t *i2cm = obj->i2c;
<> 150:02e0a0aed4ec 190 mxc_i2cm_fifo_regs_t *fifo = obj->fifo;
<> 150:02e0a0aed4ec 191 int result;
<> 150:02e0a0aed4ec 192
<> 150:02e0a0aed4ec 193 if (obj->start_pending) {
<> 150:02e0a0aed4ec 194 obj->start_pending = 0;
<> 150:02e0a0aed4ec 195 data |= MXC_S_I2CM_TRANS_TAG_START;
<> 150:02e0a0aed4ec 196 } else {
<> 150:02e0a0aed4ec 197 data |= MXC_S_I2CM_TRANS_TAG_TXDATA_ACK;
<> 150:02e0a0aed4ec 198 }
<> 150:02e0a0aed4ec 199
<> 150:02e0a0aed4ec 200 if ((result = I2CM_WriteTxFifo(i2cm, fifo, data)) != E_NO_ERROR) {
<> 150:02e0a0aed4ec 201 i2c_reset(obj);
<> 150:02e0a0aed4ec 202 if (result == E_COMM_ERR) {
<> 150:02e0a0aed4ec 203 return MBED_NAK;
<> 150:02e0a0aed4ec 204 }
<> 150:02e0a0aed4ec 205 return MBED_TIMEOUT;
<> 150:02e0a0aed4ec 206 }
<> 150:02e0a0aed4ec 207
<> 150:02e0a0aed4ec 208 // Start the transaction if it is not currently ongoing
<> 150:02e0a0aed4ec 209 if (!(i2cm->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
<> 150:02e0a0aed4ec 210 i2cm->trans |= MXC_F_I2CM_TRANS_TX_START;
<> 150:02e0a0aed4ec 211 }
<> 150:02e0a0aed4ec 212
<> 150:02e0a0aed4ec 213 return MBED_ACK;
<> 150:02e0a0aed4ec 214 }