test

Committer:
elijahsj
Date:
Mon Nov 09 00:02:47 2020 -0500
Revision:
1:8a094db1347f
test

Who changed what in which revision?

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