mbed library sources, include can_api for nucleo-f091rc

Dependents:   CanNucleoF0_example

Fork of mbed-src by mbed official

Committer:
ptpaterson
Date:
Thu Jan 07 05:49:05 2016 +0000
Revision:
645:13c87cbecd54
Parent:
507:d4fc7603a669
corrected freeze on CAN_RECEIVE_IT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 507:d4fc7603a669 1 /*******************************************************************************
mbed_official 507:d4fc7603a669 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
mbed_official 507:d4fc7603a669 3 *
mbed_official 507:d4fc7603a669 4 * Permission is hereby granted, free of charge, to any person obtaining a
mbed_official 507:d4fc7603a669 5 * copy of this software and associated documentation files (the "Software"),
mbed_official 507:d4fc7603a669 6 * to deal in the Software without restriction, including without limitation
mbed_official 507:d4fc7603a669 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
mbed_official 507:d4fc7603a669 8 * and/or sell copies of the Software, and to permit persons to whom the
mbed_official 507:d4fc7603a669 9 * Software is furnished to do so, subject to the following conditions:
mbed_official 507:d4fc7603a669 10 *
mbed_official 507:d4fc7603a669 11 * The above copyright notice and this permission notice shall be included
mbed_official 507:d4fc7603a669 12 * in all copies or substantial portions of the Software.
mbed_official 507:d4fc7603a669 13 *
mbed_official 507:d4fc7603a669 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
mbed_official 507:d4fc7603a669 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
mbed_official 507:d4fc7603a669 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
mbed_official 507:d4fc7603a669 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
mbed_official 507:d4fc7603a669 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
mbed_official 507:d4fc7603a669 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
mbed_official 507:d4fc7603a669 20 * OTHER DEALINGS IN THE SOFTWARE.
mbed_official 507:d4fc7603a669 21 *
mbed_official 507:d4fc7603a669 22 * Except as contained in this notice, the name of Maxim Integrated
mbed_official 507:d4fc7603a669 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
mbed_official 507:d4fc7603a669 24 * Products, Inc. Branding Policy.
mbed_official 507:d4fc7603a669 25 *
mbed_official 507:d4fc7603a669 26 * The mere transfer of this software does not imply any licenses
mbed_official 507:d4fc7603a669 27 * of trade secrets, proprietary technology, copyrights, patents,
mbed_official 507:d4fc7603a669 28 * trademarks, maskwork rights, or any other form of intellectual
mbed_official 507:d4fc7603a669 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
mbed_official 507:d4fc7603a669 30 * ownership rights.
mbed_official 507:d4fc7603a669 31 *******************************************************************************
mbed_official 507:d4fc7603a669 32 */
mbed_official 507:d4fc7603a669 33
mbed_official 507:d4fc7603a669 34 #include "mbed_assert.h"
mbed_official 507:d4fc7603a669 35 #include "i2c_api.h"
mbed_official 507:d4fc7603a669 36 #include "cmsis.h"
mbed_official 507:d4fc7603a669 37 #include "i2cm_regs.h"
mbed_official 507:d4fc7603a669 38 #include "clkman_regs.h"
mbed_official 507:d4fc7603a669 39 #include "ioman_regs.h"
mbed_official 507:d4fc7603a669 40 #include "PeripheralPins.h"
mbed_official 507:d4fc7603a669 41
mbed_official 507:d4fc7603a669 42 #define I2C_SLAVE_ADDR_READ_BIT 0x0001
mbed_official 507:d4fc7603a669 43
mbed_official 507:d4fc7603a669 44 #ifndef MXC_I2CM_TX_TIMEOUT
mbed_official 507:d4fc7603a669 45 #define MXC_I2CM_TX_TIMEOUT 0x5000
mbed_official 507:d4fc7603a669 46 #endif
mbed_official 507:d4fc7603a669 47
mbed_official 507:d4fc7603a669 48 #ifndef MXC_I2CM_RX_TIMEOUT
mbed_official 507:d4fc7603a669 49 #define MXC_I2CM_RX_TIMEOUT 0x5000
mbed_official 507:d4fc7603a669 50 #endif
mbed_official 507:d4fc7603a669 51
mbed_official 507:d4fc7603a669 52 typedef enum {
mbed_official 507:d4fc7603a669 53 /** 100KHz */
mbed_official 507:d4fc7603a669 54 MXC_E_I2CM_SPEED_100KHZ = 0,
mbed_official 507:d4fc7603a669 55 /** 400KHz */
mbed_official 507:d4fc7603a669 56 MXC_E_I2CM_SPEED_400KHZ,
mbed_official 507:d4fc7603a669 57 /** 1MHz */
mbed_official 507:d4fc7603a669 58 MXC_E_I2CM_SPEED_1MHZ
mbed_official 507:d4fc7603a669 59 } i2cm_speed_t;
mbed_official 507:d4fc7603a669 60
mbed_official 507:d4fc7603a669 61 /* Clock divider lookup table */
mbed_official 507:d4fc7603a669 62 static const uint32_t clk_div_table[3][8] = {
mbed_official 507:d4fc7603a669 63 /* MXC_E_I2CM_SPEED_100KHZ */
mbed_official 507:d4fc7603a669 64 {
mbed_official 507:d4fc7603a669 65 /* 0: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 66 /* 1: 6MHz */ (( 3 << MXC_F_I2CM_CLK_DIV_FILTER_CLK_DIV_POS) | ( 7 << MXC_F_I2CM_CLK_DIV_SCL_HI_CNT_POS) | ( 36 << MXC_F_I2CM_CLK_DIV_SCL_LO_CNT_POS)),
mbed_official 507:d4fc7603a669 67 /* 2: 8MHz */ (( 4 << MXC_F_I2CM_CLK_DIV_FILTER_CLK_DIV_POS) | (10 << MXC_F_I2CM_CLK_DIV_SCL_HI_CNT_POS) | ( 48 << MXC_F_I2CM_CLK_DIV_SCL_LO_CNT_POS)),
mbed_official 507:d4fc7603a669 68 /* 3: 12MHz */ (( 6 << MXC_F_I2CM_CLK_DIV_FILTER_CLK_DIV_POS) | (17 << MXC_F_I2CM_CLK_DIV_SCL_HI_CNT_POS) | ( 72 << MXC_F_I2CM_CLK_DIV_SCL_LO_CNT_POS)),
mbed_official 507:d4fc7603a669 69 /* 4: 16MHz */ (( 8 << MXC_F_I2CM_CLK_DIV_FILTER_CLK_DIV_POS) | (24 << MXC_F_I2CM_CLK_DIV_SCL_HI_CNT_POS) | ( 96 << MXC_F_I2CM_CLK_DIV_SCL_LO_CNT_POS)),
mbed_official 507:d4fc7603a669 70 /* 5: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 71 /* 6: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 72 /* 7: 24MHz */ ((12 << MXC_F_I2CM_CLK_DIV_FILTER_CLK_DIV_POS) | (38 << MXC_F_I2CM_CLK_DIV_SCL_HI_CNT_POS) | (144 << MXC_F_I2CM_CLK_DIV_SCL_LO_CNT_POS)),
mbed_official 507:d4fc7603a669 73 },
mbed_official 507:d4fc7603a669 74 /* MXC_E_I2CM_SPEED_400KHZ */
mbed_official 507:d4fc7603a669 75 {
mbed_official 507:d4fc7603a669 76 /* 0: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 77 /* 1: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 78 /* 2: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 79 /* 3: 12MHz */ ((2 << MXC_F_I2CM_CLK_DIV_FILTER_CLK_DIV_POS) | (1 << MXC_F_I2CM_CLK_DIV_SCL_HI_CNT_POS) | (18 << MXC_F_I2CM_CLK_DIV_SCL_LO_CNT_POS)),
mbed_official 507:d4fc7603a669 80 /* 4: 16MHz */ ((2 << MXC_F_I2CM_CLK_DIV_FILTER_CLK_DIV_POS) | (2 << MXC_F_I2CM_CLK_DIV_SCL_HI_CNT_POS) | (24 << MXC_F_I2CM_CLK_DIV_SCL_LO_CNT_POS)),
mbed_official 507:d4fc7603a669 81 /* 5: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 82 /* 6: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 83 /* 7: 24MHz */ ((3 << MXC_F_I2CM_CLK_DIV_FILTER_CLK_DIV_POS) | (5 << MXC_F_I2CM_CLK_DIV_SCL_HI_CNT_POS) | (36 << MXC_F_I2CM_CLK_DIV_SCL_LO_CNT_POS)),
mbed_official 507:d4fc7603a669 84 },
mbed_official 507:d4fc7603a669 85 /* MXC_E_I2CM_SPEED_1MHZ */
mbed_official 507:d4fc7603a669 86 {
mbed_official 507:d4fc7603a669 87 /* 0: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 88 /* 1: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 89 /* 2: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 90 /* 3: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 91 /* 4: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 92 /* 5: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 93 /* 6: */ 0, /* not supported */
mbed_official 507:d4fc7603a669 94 /* 7: 24MHz */ ((1 << MXC_F_I2CM_CLK_DIV_FILTER_CLK_DIV_POS) | (0 << MXC_F_I2CM_CLK_DIV_SCL_HI_CNT_POS) | (14 << MXC_F_I2CM_CLK_DIV_SCL_LO_CNT_POS)),
mbed_official 507:d4fc7603a669 95 },
mbed_official 507:d4fc7603a669 96 };
mbed_official 507:d4fc7603a669 97
mbed_official 507:d4fc7603a669 98 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
mbed_official 507:d4fc7603a669 99 {
mbed_official 507:d4fc7603a669 100 // determine the I2C to use
mbed_official 507:d4fc7603a669 101 I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
mbed_official 507:d4fc7603a669 102 I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
mbed_official 507:d4fc7603a669 103 mxc_i2cm_regs_t *i2c = (mxc_i2cm_regs_t*)pinmap_merge(i2c_sda, i2c_scl);
mbed_official 507:d4fc7603a669 104 MBED_ASSERT((int)i2c != NC);
mbed_official 507:d4fc7603a669 105
mbed_official 507:d4fc7603a669 106 obj->i2c = i2c;
mbed_official 507:d4fc7603a669 107 obj->txfifo = (uint16_t*)MXC_I2CM_GET_BASE_TX_FIFO(MXC_I2CM_BASE_TO_INSTANCE(i2c));
mbed_official 507:d4fc7603a669 108 obj->rxfifo = (uint16_t*)MXC_I2CM_GET_BASE_RX_FIFO(MXC_I2CM_BASE_TO_INSTANCE(i2c));
mbed_official 507:d4fc7603a669 109 obj->start_pending = 0;
mbed_official 507:d4fc7603a669 110 obj->stop_pending = 0;
mbed_official 507:d4fc7603a669 111
mbed_official 507:d4fc7603a669 112 // configure the pins
mbed_official 507:d4fc7603a669 113 pinmap_pinout(sda, PinMap_I2C_SDA);
mbed_official 507:d4fc7603a669 114 pinmap_pinout(scl, PinMap_I2C_SCL);
mbed_official 507:d4fc7603a669 115
mbed_official 507:d4fc7603a669 116 // enable the clock
mbed_official 507:d4fc7603a669 117 MXC_CLKMAN->clk_ctrl_6_i2cm = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
mbed_official 507:d4fc7603a669 118
mbed_official 507:d4fc7603a669 119 // reset module
mbed_official 507:d4fc7603a669 120 i2c->ctrl = MXC_F_I2CM_CTRL_MSTR_RESET_EN;
mbed_official 507:d4fc7603a669 121 i2c->ctrl = 0;
mbed_official 507:d4fc7603a669 122
mbed_official 507:d4fc7603a669 123 // set default frequency at 100k
mbed_official 507:d4fc7603a669 124 i2c_frequency(obj, 100000);
mbed_official 507:d4fc7603a669 125
mbed_official 507:d4fc7603a669 126 // set timeout to 255 ms and turn on the auto-stop option
mbed_official 507:d4fc7603a669 127 i2c->timeout = (0xFF << MXC_F_I2CM_TIMEOUT_TX_TIMEOUT_POS) | MXC_F_I2CM_TIMEOUT_AUTO_STOP_EN;
mbed_official 507:d4fc7603a669 128
mbed_official 507:d4fc7603a669 129 // enable tx_fifo and rx_fifo
mbed_official 507:d4fc7603a669 130 i2c->ctrl |= (MXC_F_I2CM_CTRL_TX_FIFO_EN | MXC_F_I2CM_CTRL_RX_FIFO_EN);
mbed_official 507:d4fc7603a669 131 }
mbed_official 507:d4fc7603a669 132
mbed_official 507:d4fc7603a669 133 void i2c_frequency(i2c_t *obj, int hz)
mbed_official 507:d4fc7603a669 134 {
mbed_official 507:d4fc7603a669 135 // compute clock array index
mbed_official 507:d4fc7603a669 136 int clki = ((SystemCoreClock + 1500000) / 3000000) - 1;
mbed_official 507:d4fc7603a669 137
mbed_official 507:d4fc7603a669 138 // get clock divider settings from lookup table
mbed_official 507:d4fc7603a669 139 if ((hz < 400000) && (clk_div_table[MXC_E_I2CM_SPEED_100KHZ][clki] > 0)) {
mbed_official 507:d4fc7603a669 140 obj->i2c->fs_clk_div = clk_div_table[MXC_E_I2CM_SPEED_100KHZ][clki];
mbed_official 507:d4fc7603a669 141 } else if ((hz < 1000000) && (clk_div_table[MXC_E_I2CM_SPEED_400KHZ][clki] > 0)) {
mbed_official 507:d4fc7603a669 142 obj->i2c->fs_clk_div = clk_div_table[MXC_E_I2CM_SPEED_400KHZ][clki];
mbed_official 507:d4fc7603a669 143 } else if ((hz >= 1000000) && (clk_div_table[MXC_E_I2CM_SPEED_1MHZ][clki] > 0)) {
mbed_official 507:d4fc7603a669 144 obj->i2c->hs_clk_div = clk_div_table[MXC_E_I2CM_SPEED_1MHZ][clki];
mbed_official 507:d4fc7603a669 145 }
mbed_official 507:d4fc7603a669 146 }
mbed_official 507:d4fc7603a669 147
mbed_official 507:d4fc7603a669 148 static int write_tx_fifo(i2c_t *obj, const uint16_t data)
mbed_official 507:d4fc7603a669 149 {
mbed_official 507:d4fc7603a669 150 int timeout = MXC_I2CM_TX_TIMEOUT;
mbed_official 507:d4fc7603a669 151
mbed_official 507:d4fc7603a669 152 while (*obj->txfifo) {
mbed_official 507:d4fc7603a669 153 uint32_t intfl = obj->i2c->intfl;
mbed_official 507:d4fc7603a669 154 if (intfl & MXC_F_I2CM_INTFL_TX_NACKED) {
mbed_official 507:d4fc7603a669 155 return I2C_ERROR_NO_SLAVE;
mbed_official 507:d4fc7603a669 156 }
mbed_official 507:d4fc7603a669 157 if (!timeout || (intfl & (MXC_F_I2CM_INTFL_TX_TIMEOUT | MXC_F_I2CM_INTFL_TX_LOST_ARBITR))) {
mbed_official 507:d4fc7603a669 158 return I2C_ERROR_BUS_BUSY;
mbed_official 507:d4fc7603a669 159 }
mbed_official 507:d4fc7603a669 160 timeout--;
mbed_official 507:d4fc7603a669 161 }
mbed_official 507:d4fc7603a669 162 *obj->txfifo = data;
mbed_official 507:d4fc7603a669 163
mbed_official 507:d4fc7603a669 164 return 0;
mbed_official 507:d4fc7603a669 165 }
mbed_official 507:d4fc7603a669 166
mbed_official 507:d4fc7603a669 167 static int wait_tx_in_progress(i2c_t *obj)
mbed_official 507:d4fc7603a669 168 {
mbed_official 507:d4fc7603a669 169 int timeout = MXC_I2CM_TX_TIMEOUT;
mbed_official 507:d4fc7603a669 170
mbed_official 507:d4fc7603a669 171 while ((obj->i2c->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS) && --timeout);
mbed_official 507:d4fc7603a669 172
mbed_official 507:d4fc7603a669 173 uint32_t intfl = obj->i2c->intfl;
mbed_official 507:d4fc7603a669 174
mbed_official 507:d4fc7603a669 175 if (intfl & MXC_F_I2CM_INTFL_TX_NACKED) {
mbed_official 507:d4fc7603a669 176 i2c_reset(obj);
mbed_official 507:d4fc7603a669 177 return I2C_ERROR_NO_SLAVE;
mbed_official 507:d4fc7603a669 178 }
mbed_official 507:d4fc7603a669 179
mbed_official 507:d4fc7603a669 180 if (!timeout || (intfl & (MXC_F_I2CM_INTFL_TX_TIMEOUT | MXC_F_I2CM_INTFL_TX_LOST_ARBITR))) {
mbed_official 507:d4fc7603a669 181 i2c_reset(obj);
mbed_official 507:d4fc7603a669 182 return I2C_ERROR_BUS_BUSY;
mbed_official 507:d4fc7603a669 183 }
mbed_official 507:d4fc7603a669 184
mbed_official 507:d4fc7603a669 185 return 0;
mbed_official 507:d4fc7603a669 186 }
mbed_official 507:d4fc7603a669 187
mbed_official 507:d4fc7603a669 188 int i2c_start(i2c_t *obj)
mbed_official 507:d4fc7603a669 189 {
mbed_official 507:d4fc7603a669 190 obj->start_pending = 1;
mbed_official 507:d4fc7603a669 191 return 0;
mbed_official 507:d4fc7603a669 192 }
mbed_official 507:d4fc7603a669 193
mbed_official 507:d4fc7603a669 194 int i2c_stop(i2c_t *obj)
mbed_official 507:d4fc7603a669 195 {
mbed_official 507:d4fc7603a669 196 obj->start_pending = 0;
mbed_official 507:d4fc7603a669 197 write_tx_fifo(obj, MXC_S_I2CM_TRANS_TAG_STOP);
mbed_official 507:d4fc7603a669 198
mbed_official 507:d4fc7603a669 199 return wait_tx_in_progress(obj);
mbed_official 507:d4fc7603a669 200 }
mbed_official 507:d4fc7603a669 201
mbed_official 507:d4fc7603a669 202 void i2c_reset(i2c_t *obj)
mbed_official 507:d4fc7603a669 203 {
mbed_official 507:d4fc7603a669 204 obj->i2c->ctrl = MXC_F_I2CM_CTRL_MSTR_RESET_EN;
mbed_official 507:d4fc7603a669 205 obj->i2c->intfl = 0x3FF; // clear all interrupts
mbed_official 507:d4fc7603a669 206 obj->i2c->ctrl = MXC_F_I2CM_CTRL_TX_FIFO_EN | MXC_F_I2CM_CTRL_RX_FIFO_EN;
mbed_official 507:d4fc7603a669 207 obj->start_pending = 0;
mbed_official 507:d4fc7603a669 208 }
mbed_official 507:d4fc7603a669 209
mbed_official 507:d4fc7603a669 210 int i2c_byte_write(i2c_t *obj, int data)
mbed_official 507:d4fc7603a669 211 {
mbed_official 507:d4fc7603a669 212 int err;
mbed_official 507:d4fc7603a669 213
mbed_official 507:d4fc7603a669 214 // clear all interrupts
mbed_official 507:d4fc7603a669 215 obj->i2c->intfl = 0x3FF;
mbed_official 507:d4fc7603a669 216
mbed_official 507:d4fc7603a669 217 if (obj->start_pending) {
mbed_official 507:d4fc7603a669 218 obj->start_pending = 0;
mbed_official 507:d4fc7603a669 219 data = (data & 0xFF) | MXC_S_I2CM_TRANS_TAG_START;
mbed_official 507:d4fc7603a669 220 } else {
mbed_official 507:d4fc7603a669 221 data = (data & 0xFF) | MXC_S_I2CM_TRANS_TAG_TXDATA_ACK;
mbed_official 507:d4fc7603a669 222 }
mbed_official 507:d4fc7603a669 223
mbed_official 507:d4fc7603a669 224 if ((err = write_tx_fifo(obj, data)) != 0) {
mbed_official 507:d4fc7603a669 225 return err;
mbed_official 507:d4fc7603a669 226 }
mbed_official 507:d4fc7603a669 227
mbed_official 507:d4fc7603a669 228 obj->i2c->trans |= MXC_F_I2CM_TRANS_TX_START;
mbed_official 507:d4fc7603a669 229
mbed_official 507:d4fc7603a669 230 return 0;
mbed_official 507:d4fc7603a669 231 }
mbed_official 507:d4fc7603a669 232
mbed_official 507:d4fc7603a669 233 int i2c_byte_read(i2c_t *obj, int last)
mbed_official 507:d4fc7603a669 234 {
mbed_official 507:d4fc7603a669 235 uint16_t fifo_value;
mbed_official 507:d4fc7603a669 236 int err;
mbed_official 507:d4fc7603a669 237
mbed_official 507:d4fc7603a669 238 // clear all interrupts
mbed_official 507:d4fc7603a669 239 obj->i2c->intfl = 0x3FF;
mbed_official 507:d4fc7603a669 240
mbed_official 507:d4fc7603a669 241 if (last) {
mbed_official 507:d4fc7603a669 242 fifo_value = MXC_S_I2CM_TRANS_TAG_RXDATA_NACK;
mbed_official 507:d4fc7603a669 243 } else {
mbed_official 507:d4fc7603a669 244 fifo_value = MXC_S_I2CM_TRANS_TAG_RXDATA_COUNT;
mbed_official 507:d4fc7603a669 245 }
mbed_official 507:d4fc7603a669 246
mbed_official 507:d4fc7603a669 247 if ((err = write_tx_fifo(obj, fifo_value)) != 0) {
mbed_official 507:d4fc7603a669 248 return err;
mbed_official 507:d4fc7603a669 249 }
mbed_official 507:d4fc7603a669 250
mbed_official 507:d4fc7603a669 251 obj->i2c->trans |= MXC_F_I2CM_TRANS_TX_START;
mbed_official 507:d4fc7603a669 252
mbed_official 507:d4fc7603a669 253 int timeout = MXC_I2CM_RX_TIMEOUT;
mbed_official 507:d4fc7603a669 254 while (!(obj->i2c->intfl & MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY) &&
mbed_official 507:d4fc7603a669 255 (!(obj->i2c->bb & MXC_F_I2CM_BB_RX_FIFO_CNT))) {
mbed_official 507:d4fc7603a669 256 if ((--timeout < 0) || !(obj->i2c->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
mbed_official 507:d4fc7603a669 257 break;
mbed_official 507:d4fc7603a669 258 }
mbed_official 507:d4fc7603a669 259 }
mbed_official 507:d4fc7603a669 260
mbed_official 507:d4fc7603a669 261 if (obj->i2c->intfl & MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY) {
mbed_official 507:d4fc7603a669 262 obj->i2c->intfl = MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY;
mbed_official 507:d4fc7603a669 263 return *obj->rxfifo;
mbed_official 507:d4fc7603a669 264 }
mbed_official 507:d4fc7603a669 265
mbed_official 507:d4fc7603a669 266 return -1;
mbed_official 507:d4fc7603a669 267 }
mbed_official 507:d4fc7603a669 268
mbed_official 507:d4fc7603a669 269 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
mbed_official 507:d4fc7603a669 270 {
mbed_official 507:d4fc7603a669 271 int err, retval = 0;
mbed_official 507:d4fc7603a669 272 int i;
mbed_official 507:d4fc7603a669 273
mbed_official 507:d4fc7603a669 274 if (!(obj->stop_pending) && (obj->i2c->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
mbed_official 507:d4fc7603a669 275 return 0;
mbed_official 507:d4fc7603a669 276 }
mbed_official 507:d4fc7603a669 277
mbed_official 507:d4fc7603a669 278 // clear all interrupts
mbed_official 507:d4fc7603a669 279 obj->i2c->intfl = 0x3FF;
mbed_official 507:d4fc7603a669 280
mbed_official 507:d4fc7603a669 281 // write the address to the fifo
mbed_official 507:d4fc7603a669 282 if ((err = write_tx_fifo(obj, (MXC_S_I2CM_TRANS_TAG_START | address))) != 0) { // start + addr (write)
mbed_official 507:d4fc7603a669 283 return err;
mbed_official 507:d4fc7603a669 284 }
mbed_official 507:d4fc7603a669 285 obj->start_pending = 0;
mbed_official 507:d4fc7603a669 286
mbed_official 507:d4fc7603a669 287 // start the transaction
mbed_official 507:d4fc7603a669 288 obj->i2c->trans |= MXC_F_I2CM_TRANS_TX_START;
mbed_official 507:d4fc7603a669 289
mbed_official 507:d4fc7603a669 290 // load as much of the cmd into the FIFO as possible
mbed_official 507:d4fc7603a669 291 for (i = 0; i < length; i++) {
mbed_official 507:d4fc7603a669 292 if ((err = write_tx_fifo(obj, (MXC_S_I2CM_TRANS_TAG_TXDATA_ACK | data[i]))) != 0) { // cmd (expect ACK)
mbed_official 507:d4fc7603a669 293 retval = (retval ? retval : err);
mbed_official 507:d4fc7603a669 294 break;
mbed_official 507:d4fc7603a669 295 }
mbed_official 507:d4fc7603a669 296 }
mbed_official 507:d4fc7603a669 297
mbed_official 507:d4fc7603a669 298 if (stop) {
mbed_official 507:d4fc7603a669 299 obj->stop_pending = 0;
mbed_official 507:d4fc7603a669 300 if ((err = write_tx_fifo(obj, MXC_S_I2CM_TRANS_TAG_STOP)) != 0) { // stop condition
mbed_official 507:d4fc7603a669 301 retval = (retval ? retval : err);
mbed_official 507:d4fc7603a669 302 }
mbed_official 507:d4fc7603a669 303
mbed_official 507:d4fc7603a669 304 if ((err = wait_tx_in_progress(obj)) != 0) {
mbed_official 507:d4fc7603a669 305 retval = (retval ? retval : err);
mbed_official 507:d4fc7603a669 306 }
mbed_official 507:d4fc7603a669 307 } else {
mbed_official 507:d4fc7603a669 308 obj->stop_pending = 1;
mbed_official 507:d4fc7603a669 309 int timeout = MXC_I2CM_TX_TIMEOUT;
mbed_official 507:d4fc7603a669 310 // Wait for TX fifo to be empty
mbed_official 507:d4fc7603a669 311 while(!(obj->i2c->intfl & MXC_F_I2CM_INTFL_TX_FIFO_EMPTY) && timeout--) {}
mbed_official 507:d4fc7603a669 312 }
mbed_official 507:d4fc7603a669 313
mbed_official 507:d4fc7603a669 314 if (retval == 0) {
mbed_official 507:d4fc7603a669 315 return length;
mbed_official 507:d4fc7603a669 316 }
mbed_official 507:d4fc7603a669 317
mbed_official 507:d4fc7603a669 318 return retval;
mbed_official 507:d4fc7603a669 319 }
mbed_official 507:d4fc7603a669 320
mbed_official 507:d4fc7603a669 321 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
mbed_official 507:d4fc7603a669 322 {
mbed_official 507:d4fc7603a669 323 int err, retval = 0;
mbed_official 507:d4fc7603a669 324 int i = length;
mbed_official 507:d4fc7603a669 325 int timeout;
mbed_official 507:d4fc7603a669 326
mbed_official 507:d4fc7603a669 327 if (!(obj->stop_pending) && (obj->i2c->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
mbed_official 507:d4fc7603a669 328 return 0;
mbed_official 507:d4fc7603a669 329 }
mbed_official 507:d4fc7603a669 330
mbed_official 507:d4fc7603a669 331 // clear all interrupts
mbed_official 507:d4fc7603a669 332 obj->i2c->intfl = 0x3FF;
mbed_official 507:d4fc7603a669 333
mbed_official 507:d4fc7603a669 334 // start + addr (read)
mbed_official 507:d4fc7603a669 335 if ((retval = write_tx_fifo(obj, (MXC_S_I2CM_TRANS_TAG_START | address | I2C_SLAVE_ADDR_READ_BIT))) != 0) {
mbed_official 507:d4fc7603a669 336 goto read_done;
mbed_official 507:d4fc7603a669 337 }
mbed_official 507:d4fc7603a669 338 obj->start_pending = 0;
mbed_official 507:d4fc7603a669 339
mbed_official 507:d4fc7603a669 340 while (i > 256) {
mbed_official 507:d4fc7603a669 341 if ((retval = write_tx_fifo(obj, (MXC_S_I2CM_TRANS_TAG_RXDATA_COUNT | 255))) != 0) {
mbed_official 507:d4fc7603a669 342 goto read_done;
mbed_official 507:d4fc7603a669 343 }
mbed_official 507:d4fc7603a669 344 i -= 256;
mbed_official 507:d4fc7603a669 345 }
mbed_official 507:d4fc7603a669 346
mbed_official 507:d4fc7603a669 347 if (i > 1) {
mbed_official 507:d4fc7603a669 348 if ((retval = write_tx_fifo(obj, (MXC_S_I2CM_TRANS_TAG_RXDATA_COUNT | (i - 2)))) != 0) {
mbed_official 507:d4fc7603a669 349 goto read_done;
mbed_official 507:d4fc7603a669 350 }
mbed_official 507:d4fc7603a669 351 }
mbed_official 507:d4fc7603a669 352
mbed_official 507:d4fc7603a669 353 // start the transaction
mbed_official 507:d4fc7603a669 354 obj->i2c->trans |= MXC_F_I2CM_TRANS_TX_START;
mbed_official 507:d4fc7603a669 355
mbed_official 507:d4fc7603a669 356 if ((retval = write_tx_fifo(obj, MXC_S_I2CM_TRANS_TAG_RXDATA_NACK)) != 0) { // NACK last data byte
mbed_official 507:d4fc7603a669 357 goto read_done;
mbed_official 507:d4fc7603a669 358 }
mbed_official 507:d4fc7603a669 359
mbed_official 507:d4fc7603a669 360 if (stop) {
mbed_official 507:d4fc7603a669 361 if ((retval = write_tx_fifo(obj, MXC_S_I2CM_TRANS_TAG_STOP)) != 0) { // stop condition
mbed_official 507:d4fc7603a669 362 goto read_done;
mbed_official 507:d4fc7603a669 363 }
mbed_official 507:d4fc7603a669 364 }
mbed_official 507:d4fc7603a669 365
mbed_official 507:d4fc7603a669 366 timeout = MXC_I2CM_RX_TIMEOUT;
mbed_official 507:d4fc7603a669 367 i = 0;
mbed_official 507:d4fc7603a669 368 while (i < length) {
mbed_official 507:d4fc7603a669 369 while (!(obj->i2c->intfl & MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY) &&
mbed_official 507:d4fc7603a669 370 (!(obj->i2c->bb & MXC_F_I2CM_BB_RX_FIFO_CNT))) {
mbed_official 507:d4fc7603a669 371 if ((--timeout < 0) || !(obj->i2c->trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) {
mbed_official 507:d4fc7603a669 372 retval = -3;
mbed_official 507:d4fc7603a669 373 goto read_done;
mbed_official 507:d4fc7603a669 374 }
mbed_official 507:d4fc7603a669 375 }
mbed_official 507:d4fc7603a669 376
mbed_official 507:d4fc7603a669 377 timeout = MXC_I2CM_RX_TIMEOUT;
mbed_official 507:d4fc7603a669 378
mbed_official 507:d4fc7603a669 379 obj->i2c->intfl = MXC_F_I2CM_INTFL_RX_FIFO_NOT_EMPTY;
mbed_official 507:d4fc7603a669 380
mbed_official 507:d4fc7603a669 381 uint16_t temp = *obj->rxfifo;
mbed_official 507:d4fc7603a669 382
mbed_official 507:d4fc7603a669 383 if (temp & MXC_S_I2CM_RSTLS_TAG_EMPTY) {
mbed_official 507:d4fc7603a669 384 continue;
mbed_official 507:d4fc7603a669 385 }
mbed_official 507:d4fc7603a669 386 data[i++] = (uint8_t) temp;
mbed_official 507:d4fc7603a669 387 }
mbed_official 507:d4fc7603a669 388
mbed_official 507:d4fc7603a669 389 read_done:
mbed_official 507:d4fc7603a669 390
mbed_official 507:d4fc7603a669 391 if (stop) {
mbed_official 507:d4fc7603a669 392 obj->stop_pending = 0;
mbed_official 507:d4fc7603a669 393 if ((err = wait_tx_in_progress(obj)) != 0) {
mbed_official 507:d4fc7603a669 394 retval = (retval ? retval : err);
mbed_official 507:d4fc7603a669 395 }
mbed_official 507:d4fc7603a669 396 } else {
mbed_official 507:d4fc7603a669 397 obj->stop_pending = 1;
mbed_official 507:d4fc7603a669 398 }
mbed_official 507:d4fc7603a669 399
mbed_official 507:d4fc7603a669 400 if (retval == 0) {
mbed_official 507:d4fc7603a669 401 return length;
mbed_official 507:d4fc7603a669 402 }
mbed_official 507:d4fc7603a669 403
mbed_official 507:d4fc7603a669 404 return retval;
mbed_official 507:d4fc7603a669 405 }