Extended MaximInterface
Dependents: mbed_DS28EC20_GPIO
Platforms/mxc/I2CMaster.cpp@0:f77ad7f72d04, 2017-11-06 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Mon Nov 06 14:39:18 2017 -0600
- Revision:
- 0:f77ad7f72d04
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 0:f77ad7f72d04 | 1 | /******************************************************************************* |
IanBenzMaxim | 0:f77ad7f72d04 | 2 | * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved. |
IanBenzMaxim | 0:f77ad7f72d04 | 3 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
IanBenzMaxim | 0:f77ad7f72d04 | 5 | * copy of this software and associated documentation files (the "Software"), |
IanBenzMaxim | 0:f77ad7f72d04 | 6 | * to deal in the Software without restriction, including without limitation |
IanBenzMaxim | 0:f77ad7f72d04 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
IanBenzMaxim | 0:f77ad7f72d04 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
IanBenzMaxim | 0:f77ad7f72d04 | 9 | * Software is furnished to do so, subject to the following conditions: |
IanBenzMaxim | 0:f77ad7f72d04 | 10 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 11 | * The above copyright notice and this permission notice shall be included |
IanBenzMaxim | 0:f77ad7f72d04 | 12 | * in all copies or substantial portions of the Software. |
IanBenzMaxim | 0:f77ad7f72d04 | 13 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
IanBenzMaxim | 0:f77ad7f72d04 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
IanBenzMaxim | 0:f77ad7f72d04 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
IanBenzMaxim | 0:f77ad7f72d04 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
IanBenzMaxim | 0:f77ad7f72d04 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
IanBenzMaxim | 0:f77ad7f72d04 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
IanBenzMaxim | 0:f77ad7f72d04 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
IanBenzMaxim | 0:f77ad7f72d04 | 21 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
IanBenzMaxim | 0:f77ad7f72d04 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
IanBenzMaxim | 0:f77ad7f72d04 | 24 | * Products, Inc. Branding Policy. |
IanBenzMaxim | 0:f77ad7f72d04 | 25 | * |
IanBenzMaxim | 0:f77ad7f72d04 | 26 | * The mere transfer of this software does not imply any licenses |
IanBenzMaxim | 0:f77ad7f72d04 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
IanBenzMaxim | 0:f77ad7f72d04 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
IanBenzMaxim | 0:f77ad7f72d04 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
IanBenzMaxim | 0:f77ad7f72d04 | 30 | * ownership rights. |
IanBenzMaxim | 0:f77ad7f72d04 | 31 | *******************************************************************************/ |
IanBenzMaxim | 0:f77ad7f72d04 | 32 | |
IanBenzMaxim | 0:f77ad7f72d04 | 33 | #include <mxc_device.h> |
IanBenzMaxim | 0:f77ad7f72d04 | 34 | #include "Error.hpp" |
IanBenzMaxim | 0:f77ad7f72d04 | 35 | #include "I2CMaster.hpp" |
IanBenzMaxim | 0:f77ad7f72d04 | 36 | |
IanBenzMaxim | 0:f77ad7f72d04 | 37 | namespace MaximInterface { |
IanBenzMaxim | 0:f77ad7f72d04 | 38 | namespace mxc { |
IanBenzMaxim | 0:f77ad7f72d04 | 39 | |
IanBenzMaxim | 0:f77ad7f72d04 | 40 | static void verifyTransactionStarted(mxc_i2cm_regs_t & i2cm) { |
IanBenzMaxim | 0:f77ad7f72d04 | 41 | if (!(i2cm.trans & MXC_F_I2CM_TRANS_TX_IN_PROGRESS)) { |
IanBenzMaxim | 0:f77ad7f72d04 | 42 | i2cm.trans |= MXC_F_I2CM_TRANS_TX_START; |
IanBenzMaxim | 0:f77ad7f72d04 | 43 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 44 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 45 | |
IanBenzMaxim | 0:f77ad7f72d04 | 46 | error_code I2CMaster::initialize(const sys_cfg_i2cm_t & sysCfg, |
IanBenzMaxim | 0:f77ad7f72d04 | 47 | i2cm_speed_t speed) { |
IanBenzMaxim | 0:f77ad7f72d04 | 48 | return makeErrorCode(I2CM_Init(i2cm, &sysCfg, speed)); |
IanBenzMaxim | 0:f77ad7f72d04 | 49 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 50 | |
IanBenzMaxim | 0:f77ad7f72d04 | 51 | error_code I2CMaster::shutdown() { return makeErrorCode(I2CM_Shutdown(i2cm)); } |
IanBenzMaxim | 0:f77ad7f72d04 | 52 | |
IanBenzMaxim | 0:f77ad7f72d04 | 53 | error_code I2CMaster::writeByte(uint_least8_t data, bool start) { |
IanBenzMaxim | 0:f77ad7f72d04 | 54 | const uint16_t fifoData = |
IanBenzMaxim | 0:f77ad7f72d04 | 55 | static_cast<uint16_t>(data & 0xFF) | |
IanBenzMaxim | 0:f77ad7f72d04 | 56 | (start ? MXC_S_I2CM_TRANS_TAG_START : MXC_S_I2CM_TRANS_TAG_TXDATA_ACK); |
IanBenzMaxim | 0:f77ad7f72d04 | 57 | const error_code result = |
IanBenzMaxim | 0:f77ad7f72d04 | 58 | makeErrorCode(I2CM_WriteTxFifo(i2cm, i2cmFifo, fifoData)); |
IanBenzMaxim | 0:f77ad7f72d04 | 59 | if (result) { |
IanBenzMaxim | 0:f77ad7f72d04 | 60 | I2CM_Recover(i2cm); |
IanBenzMaxim | 0:f77ad7f72d04 | 61 | } else { |
IanBenzMaxim | 0:f77ad7f72d04 | 62 | verifyTransactionStarted(*i2cm); |
IanBenzMaxim | 0:f77ad7f72d04 | 63 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 64 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 65 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 66 | |
IanBenzMaxim | 0:f77ad7f72d04 | 67 | error_code I2CMaster::start(uint_least8_t address) { |
IanBenzMaxim | 0:f77ad7f72d04 | 68 | return writeByte(address, true); |
IanBenzMaxim | 0:f77ad7f72d04 | 69 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 70 | |
IanBenzMaxim | 0:f77ad7f72d04 | 71 | error_code I2CMaster::stop() { |
IanBenzMaxim | 0:f77ad7f72d04 | 72 | error_code result = makeErrorCode( |
IanBenzMaxim | 0:f77ad7f72d04 | 73 | I2CM_WriteTxFifo(i2cm, i2cmFifo, MXC_S_I2CM_TRANS_TAG_STOP)); |
IanBenzMaxim | 0:f77ad7f72d04 | 74 | if (!result) { |
IanBenzMaxim | 0:f77ad7f72d04 | 75 | result = makeErrorCode(I2CM_TxInProgress(i2cm)); |
IanBenzMaxim | 0:f77ad7f72d04 | 76 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 77 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 78 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 79 | |
IanBenzMaxim | 0:f77ad7f72d04 | 80 | error_code I2CMaster::writeByte(uint_least8_t data) { |
IanBenzMaxim | 0:f77ad7f72d04 | 81 | return writeByte(data, false); |
IanBenzMaxim | 0:f77ad7f72d04 | 82 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 83 | |
IanBenzMaxim | 0:f77ad7f72d04 | 84 | error_code I2CMaster::readByte(AckStatus status, uint_least8_t & data) { |
IanBenzMaxim | 0:f77ad7f72d04 | 85 | uint16_t fifoData = (status == Nack) ? MXC_S_I2CM_TRANS_TAG_RXDATA_NACK |
IanBenzMaxim | 0:f77ad7f72d04 | 86 | : MXC_S_I2CM_TRANS_TAG_RXDATA_COUNT; |
IanBenzMaxim | 0:f77ad7f72d04 | 87 | error_code result = makeErrorCode(I2CM_WriteTxFifo(i2cm, i2cmFifo, fifoData)); |
IanBenzMaxim | 0:f77ad7f72d04 | 88 | if (!result) { |
IanBenzMaxim | 0:f77ad7f72d04 | 89 | verifyTransactionStarted(*i2cm); |
IanBenzMaxim | 0:f77ad7f72d04 | 90 | do { |
IanBenzMaxim | 0:f77ad7f72d04 | 91 | int timeout = 0x5000; |
IanBenzMaxim | 0:f77ad7f72d04 | 92 | while ((i2cm->bb & MXC_F_I2CM_BB_RX_FIFO_CNT) == 0) { |
IanBenzMaxim | 0:f77ad7f72d04 | 93 | if ((--timeout <= 0) || (i2cm->trans & MXC_F_I2CM_TRANS_TX_TIMEOUT)) { |
IanBenzMaxim | 0:f77ad7f72d04 | 94 | result = makeErrorCode(E_TIME_OUT); |
IanBenzMaxim | 0:f77ad7f72d04 | 95 | goto exit; |
IanBenzMaxim | 0:f77ad7f72d04 | 96 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 97 | if (i2cm->trans & |
IanBenzMaxim | 0:f77ad7f72d04 | 98 | (MXC_F_I2CM_TRANS_TX_LOST_ARBITR | MXC_F_I2CM_TRANS_TX_NACKED)) { |
IanBenzMaxim | 0:f77ad7f72d04 | 99 | result = make_error_code(NackError); |
IanBenzMaxim | 0:f77ad7f72d04 | 100 | goto exit; |
IanBenzMaxim | 0:f77ad7f72d04 | 101 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 102 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 103 | fifoData = i2cmFifo->rx; |
IanBenzMaxim | 0:f77ad7f72d04 | 104 | } while (fifoData & MXC_S_I2CM_RSTLS_TAG_EMPTY); |
IanBenzMaxim | 0:f77ad7f72d04 | 105 | data = fifoData & 0xFF; |
IanBenzMaxim | 0:f77ad7f72d04 | 106 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 107 | |
IanBenzMaxim | 0:f77ad7f72d04 | 108 | exit: |
IanBenzMaxim | 0:f77ad7f72d04 | 109 | if (result) { |
IanBenzMaxim | 0:f77ad7f72d04 | 110 | I2CM_Recover(i2cm); |
IanBenzMaxim | 0:f77ad7f72d04 | 111 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 112 | return result; |
IanBenzMaxim | 0:f77ad7f72d04 | 113 | } |
IanBenzMaxim | 0:f77ad7f72d04 | 114 | |
IanBenzMaxim | 0:f77ad7f72d04 | 115 | } // namespace mxc |
IanBenzMaxim | 0:f77ad7f72d04 | 116 | } // namespace MaximInterface |