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