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