123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386 |
- /*
- * Copyright (c) 2016, Freescale Semiconductor, Inc.
- * Copyright 2016-2017 NXP
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright notice, this list
- * of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
- *
- * o Neither the name of the copyright holder nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "fsl_i2c.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- /*! @brief i2c transfer state. */
- enum _i2c_transfer_states
- {
- kIdleState = 0x0U, /*!< I2C bus idle. */
- kCheckAddressState = 0x1U, /*!< 7-bit address check state. */
- kSendCommandState = 0x2U, /*!< Send command byte phase. */
- kSendDataState = 0x3U, /*!< Send data transfer phase. */
- kReceiveDataBeginState = 0x4U, /*!< Receive data transfer phase begin. */
- kReceiveDataState = 0x5U, /*!< Receive data transfer phase. */
- };
- /*! @brief Common sets of flags used by the driver. */
- enum _i2c_flag_constants
- {
- kClearFlags = kI2C_ArbitrationLostFlag | kI2C_IntPendingFlag,
- kIrqFlags = kI2C_GlobalInterruptEnable,
- };
- /*! @brief Typedef for interrupt handler. */
- typedef void (*i2c_isr_t)(I2C_Type *base, void *i2cHandle);
- /*******************************************************************************
- * Prototypes
- ******************************************************************************/
- /*!
- * @brief Get instance number for I2C module.
- *
- * @param base I2C peripheral base address.
- */
- uint32_t I2C_GetInstance(I2C_Type *base);
- /*!
- * @brief Set up master transfer, send slave address and decide the initial
- * transfer state.
- *
- * @param base I2C peripheral base address.
- * @param handle pointer to i2c_master_handle_t structure which stores the transfer state.
- * @param xfer pointer to i2c_master_transfer_t structure.
- */
- static status_t I2C_InitTransferStateMachine(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer);
- /*!
- * @brief Check and clear status operation.
- *
- * @param base I2C peripheral base address.
- * @param status current i2c hardware status.
- * @retval kStatus_Success No error found.
- * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
- * @retval kStatus_I2C_Nak Received Nak error.
- */
- static status_t I2C_CheckAndClearError(I2C_Type *base, uint32_t status);
- /*!
- * @brief Master run transfer state machine to perform a byte of transfer.
- *
- * @param base I2C peripheral base address.
- * @param handle pointer to i2c_master_handle_t structure which stores the transfer state
- * @param isDone input param to get whether the thing is done, true is done
- * @retval kStatus_Success No error found.
- * @retval kStatus_I2C_ArbitrationLost Transfer error, arbitration lost.
- * @retval kStatus_I2C_Nak Received Nak error.
- * @retval kStatus_I2C_Timeout Transfer error, wait signal timeout.
- */
- static status_t I2C_MasterTransferRunStateMachine(I2C_Type *base, i2c_master_handle_t *handle, bool *isDone);
- /*!
- * @brief I2C common interrupt handler.
- *
- * @param base I2C peripheral base address.
- * @param handle pointer to i2c_master_handle_t structure which stores the transfer state
- */
- static void I2C_TransferCommonIRQHandler(I2C_Type *base, void *handle);
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- /*! @brief SCL clock divider used to calculate baudrate. */
- static const uint16_t s_i2cDividerTable[] = {
- 30, 32, 36, 42, 48, 52, 60, 72, 80, 88, 104, 128, 144, 160, 192, 240,
- 288, 320, 384, 480, 576, 640, 768, 960, 1152, 1280, 1536, 1920, 2304, 2560, 3072, 3840,
- 22, 24, 26, 28, 32, 36, 40, 44, 48, 56, 64, 72, 80, 96, 112, 128,
- 160, 192, 224, 256, 320, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792, 2048};
- /*! @brief Pointers to i2c bases for each instance. */
- static I2C_Type *const s_i2cBases[] = I2C_BASE_PTRS;
- /*! @brief Pointers to i2c IRQ number for each instance. */
- static const IRQn_Type s_i2cIrqs[] = I2C_IRQS;
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /*! @brief Pointers to i2c clocks for each instance. */
- static const clock_ip_name_t s_i2cClocks[] = I2C_CLOCKS;
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- /*! @brief Pointers to i2c handles for each instance. */
- static void *s_i2cHandle[ARRAY_SIZE(s_i2cBases)] = {NULL};
- /*! @brief Pointer to master IRQ handler for each instance. */
- static i2c_isr_t s_i2cMasterIsr;
- /*! @brief Pointer to slave IRQ handler for each instance. */
- static i2c_isr_t s_i2cSlaveIsr;
- /*******************************************************************************
- * Codes
- ******************************************************************************/
- uint32_t I2C_GetInstance(I2C_Type *base)
- {
- uint32_t instance;
- /* Find the instance index from base address mappings. */
- for (instance = 0; instance < ARRAY_SIZE(s_i2cBases); instance++)
- {
- if (s_i2cBases[instance] == base)
- {
- break;
- }
- }
- assert(instance < ARRAY_SIZE(s_i2cBases));
- return instance;
- }
- static status_t I2C_InitTransferStateMachine(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer)
- {
- status_t result = kStatus_Success;
- i2c_direction_t direction = xfer->direction;
- /* Initialize the handle transfer information. */
- handle->transfer = *xfer;
- /* Save total transfer size. */
- handle->transferSize = xfer->dataSize;
- /* Initial transfer state. */
- if (handle->transfer.subaddressSize > 0)
- {
- if (xfer->direction == kI2C_Read)
- {
- direction = kI2C_Write;
- }
- }
- handle->state = kCheckAddressState;
- /* Clear all status before transfer. */
- I2C_MasterClearStatusFlags(base, kClearFlags);
- /* If repeated start is requested, send repeated start. */
- if (handle->transfer.flags & kI2C_TransferRepeatedStartFlag)
- {
- result = I2C_MasterRepeatedStart(base, handle->transfer.slaveAddress, direction);
- }
- else /* For normal transfer, send start. */
- {
- result = I2C_MasterStart(base, handle->transfer.slaveAddress, direction);
- }
- return result;
- }
- static status_t I2C_CheckAndClearError(I2C_Type *base, uint32_t status)
- {
- status_t result = kStatus_Success;
- /* Check arbitration lost. */
- if (status & kI2C_ArbitrationLostFlag)
- {
- /* Clear arbitration lost flag. */
- base->I2SR &= (uint8_t)(~kI2C_ArbitrationLostFlag);
- /* Reset I2C controller*/
- base->I2CR &= ~I2C_I2CR_IEN_MASK;
- base->I2CR |= I2C_I2CR_IEN_MASK;
- result = kStatus_I2C_ArbitrationLost;
- }
- /* Check NAK */
- else if (status & kI2C_ReceiveNakFlag)
- {
- result = kStatus_I2C_Nak;
- }
- else
- {
- }
- return result;
- }
- static status_t I2C_MasterTransferRunStateMachine(I2C_Type *base, i2c_master_handle_t *handle, bool *isDone)
- {
- status_t result = kStatus_Success;
- uint32_t statusFlags = base->I2SR;
- *isDone = false;
- volatile uint8_t dummy = 0;
- bool ignoreNak = ((handle->state == kSendDataState) && (handle->transfer.dataSize == 0U)) ||
- ((handle->state == kReceiveDataState) && (handle->transfer.dataSize == 1U));
- /* Add this to avoid build warning. */
- dummy++;
- /* Check & clear error flags. */
- result = I2C_CheckAndClearError(base, statusFlags);
- /* Ignore Nak when it's appeared for last byte. */
- if ((result == kStatus_I2C_Nak) && ignoreNak)
- {
- result = kStatus_Success;
- }
- /* Handle Check address state to check the slave address is Acked in slave
- probe application. */
- if (handle->state == kCheckAddressState)
- {
- if (statusFlags & kI2C_ReceiveNakFlag)
- {
- result = kStatus_I2C_Addr_Nak;
- }
- else
- {
- if (handle->transfer.subaddressSize > 0)
- {
- handle->state = kSendCommandState;
- }
- else
- {
- if (handle->transfer.direction == kI2C_Write)
- {
- /* Next state, send data. */
- handle->state = kSendDataState;
- }
- else
- {
- /* Next state, receive data begin. */
- handle->state = kReceiveDataBeginState;
- }
- }
- }
- }
- if (result)
- {
- return result;
- }
- /* Run state machine. */
- switch (handle->state)
- {
- /* Send I2C command. */
- case kSendCommandState:
- if (handle->transfer.subaddressSize)
- {
- handle->transfer.subaddressSize--;
- base->I2DR = ((handle->transfer.subaddress) >> (8 * handle->transfer.subaddressSize));
- }
- else
- {
- if (handle->transfer.direction == kI2C_Write)
- {
- /* Next state, send data. */
- handle->state = kSendDataState;
- /* Send first byte of data. */
- if (handle->transfer.dataSize > 0)
- {
- base->I2DR = *handle->transfer.data;
- handle->transfer.data++;
- handle->transfer.dataSize--;
- }
- }
- else
- {
- /* Send repeated start and slave address. */
- result = I2C_MasterRepeatedStart(base, handle->transfer.slaveAddress, kI2C_Read);
- /* Next state, receive data begin. */
- handle->state = kReceiveDataBeginState;
- }
- }
- break;
- /* Send I2C data. */
- case kSendDataState:
- /* Send one byte of data. */
- if (handle->transfer.dataSize > 0)
- {
- base->I2DR = *handle->transfer.data;
- handle->transfer.data++;
- handle->transfer.dataSize--;
- }
- else
- {
- *isDone = true;
- }
- break;
- /* Start I2C data receive. */
- case kReceiveDataBeginState:
- base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
- /* Send nak at the last receive byte. */
- if (handle->transfer.dataSize == 1)
- {
- base->I2CR |= I2C_I2CR_TXAK_MASK;
- }
- /* Read dummy to release the bus. */
- dummy = base->I2DR;
- /* Next state, receive data. */
- handle->state = kReceiveDataState;
- break;
- /* Receive I2C data. */
- case kReceiveDataState:
- /* Receive one byte of data. */
- if (handle->transfer.dataSize--)
- {
- if (handle->transfer.dataSize == 0)
- {
- *isDone = true;
- /* Send stop if kI2C_TransferNoStop is not asserted. */
- if (!(handle->transfer.flags & kI2C_TransferNoStopFlag))
- {
- result = I2C_MasterStop(base);
- }
- else
- {
- base->I2CR |= I2C_I2CR_MTX_MASK;
- }
- }
- /* Send NAK at the last receive byte. */
- if (handle->transfer.dataSize == 1)
- {
- base->I2CR |= I2C_I2CR_TXAK_MASK;
- }
- /* Read the data byte into the transfer buffer. */
- *handle->transfer.data = base->I2DR;
- handle->transfer.data++;
- }
- break;
- default:
- break;
- }
- return result;
- }
- static void I2C_TransferCommonIRQHandler(I2C_Type *base, void *handle)
- {
- /* Check if master interrupt. */
- if ((base->I2SR & kI2C_ArbitrationLostFlag) || (base->I2CR & I2C_I2CR_MSTA_MASK))
- {
- s_i2cMasterIsr(base, handle);
- }
- else
- {
- s_i2cSlaveIsr(base, handle);
- }
- __DSB();
- }
- void I2C_MasterInit(I2C_Type *base, const i2c_master_config_t *masterConfig, uint32_t srcClock_Hz)
- {
- assert(masterConfig && srcClock_Hz);
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /* Enable I2C clock. */
- CLOCK_EnableClock(s_i2cClocks[I2C_GetInstance(base)]);
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- /* Disable I2C prior to configuring it. */
- base->I2CR &= ~(I2C_I2CR_IEN_MASK);
- /* Clear all flags. */
- I2C_MasterClearStatusFlags(base, kClearFlags);
- /* Configure baud rate. */
- I2C_MasterSetBaudRate(base, masterConfig->baudRate_Bps, srcClock_Hz);
- /* Enable the I2C peripheral based on the configuration. */
- base->I2CR = I2C_I2CR_IEN(masterConfig->enableMaster);
- }
- void I2C_MasterDeinit(I2C_Type *base)
- {
- /* Disable I2C module. */
- I2C_Enable(base, false);
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /* Disable I2C clock. */
- CLOCK_DisableClock(s_i2cClocks[I2C_GetInstance(base)]);
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- }
- void I2C_MasterGetDefaultConfig(i2c_master_config_t *masterConfig)
- {
- assert(masterConfig);
- /* Default baud rate at 100kbps. */
- masterConfig->baudRate_Bps = 100000U;
- /* Enable the I2C peripheral. */
- masterConfig->enableMaster = true;
- }
- void I2C_EnableInterrupts(I2C_Type *base, uint32_t mask)
- {
- if (mask & kI2C_GlobalInterruptEnable)
- {
- base->I2CR |= I2C_I2CR_IIEN_MASK;
- }
- }
- void I2C_DisableInterrupts(I2C_Type *base, uint32_t mask)
- {
- if (mask & kI2C_GlobalInterruptEnable)
- {
- base->I2CR &= ~I2C_I2CR_IIEN_MASK;
- }
- }
- void I2C_MasterSetBaudRate(I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz)
- {
- uint32_t computedRate;
- uint32_t absError;
- uint32_t bestError = UINT32_MAX;
- uint32_t bestIcr = 0u;
- uint8_t i;
- /* Scan table to find best match. */
- for (i = 0u; i < sizeof(s_i2cDividerTable) / sizeof(s_i2cDividerTable[0]); ++i)
- {
- computedRate = srcClock_Hz / s_i2cDividerTable[i];
- absError = baudRate_Bps > computedRate ? (baudRate_Bps - computedRate) : (computedRate - baudRate_Bps);
- if (absError < bestError)
- {
- bestIcr = i;
- bestError = absError;
- /* If the error is 0, then we can stop searching because we won't find a better match. */
- if (absError == 0)
- {
- break;
- }
- }
- }
- /* Set frequency register based on best settings. */
- base->IFDR = I2C_IFDR_IC(bestIcr);
- }
- status_t I2C_MasterStart(I2C_Type *base, uint8_t address, i2c_direction_t direction)
- {
- status_t result = kStatus_Success;
- uint32_t statusFlags = I2C_MasterGetStatusFlags(base);
- /* Return an error if the bus is already in use. */
- if (statusFlags & kI2C_BusBusyFlag)
- {
- result = kStatus_I2C_Busy;
- }
- else
- {
- /* Send the START signal. */
- base->I2CR |= I2C_I2CR_MSTA_MASK | I2C_I2CR_MTX_MASK;
- base->I2DR = (((uint32_t)address) << 1U | ((direction == kI2C_Read) ? 1U : 0U));
- }
- return result;
- }
- status_t I2C_MasterRepeatedStart(I2C_Type *base, uint8_t address, i2c_direction_t direction)
- {
- status_t result = kStatus_Success;
- uint32_t statusFlags = I2C_MasterGetStatusFlags(base);
- /* Return an error if the bus is already in use, but not by us. */
- if ((statusFlags & kI2C_BusBusyFlag) && ((base->I2CR & I2C_I2CR_MSTA_MASK) == 0))
- {
- result = kStatus_I2C_Busy;
- }
- else
- {
- /* We are already in a transfer, so send a repeated start. */
- base->I2CR |= I2C_I2CR_RSTA_MASK | I2C_I2CR_MTX_MASK;
- base->I2DR = (((uint32_t)address) << 1U | ((direction == kI2C_Read) ? 1U : 0U));
- }
- return result;
- }
- status_t I2C_MasterStop(I2C_Type *base)
- {
- status_t result = kStatus_Success;
- uint16_t timeout = UINT16_MAX;
- /* Issue the STOP command on the bus. */
- base->I2CR &= ~(I2C_I2CR_MSTA_MASK | I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
- /* Wait until data transfer complete. */
- while ((base->I2SR & kI2C_BusBusyFlag) && (--timeout))
- {
- }
- if (timeout == 0)
- {
- result = kStatus_I2C_Timeout;
- }
- return result;
- }
- status_t I2C_MasterWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize, uint32_t flags)
- {
- status_t result = kStatus_Success;
- uint8_t statusFlags = 0;
- /* Wait until the data register is ready for transmit. */
- while (!(base->I2SR & kI2C_TransferCompleteFlag))
- {
- }
- /* Clear the IICIF flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Setup the I2C peripheral to transmit data. */
- base->I2CR |= I2C_I2CR_MTX_MASK;
- while (txSize--)
- {
- /* Send a byte of data. */
- base->I2DR = *txBuff++;
- /* Wait until data transfer complete. */
- while (!(base->I2SR & kI2C_IntPendingFlag))
- {
- }
- statusFlags = base->I2SR;
- /* Clear the IICIF flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Check if arbitration lost or no acknowledgement (NAK), return failure status. */
- if (statusFlags & kI2C_ArbitrationLostFlag)
- {
- base->I2SR = kI2C_ArbitrationLostFlag;
- result = kStatus_I2C_ArbitrationLost;
- }
- if ((statusFlags & kI2C_ReceiveNakFlag) && txSize)
- {
- base->I2SR = kI2C_ReceiveNakFlag;
- result = kStatus_I2C_Nak;
- }
- if (result != kStatus_Success)
- {
- /* Breaking out of the send loop. */
- break;
- }
- }
- if (((result == kStatus_Success) && (!(flags & kI2C_TransferNoStopFlag))) || (result == kStatus_I2C_Nak))
- {
- /* Clear the IICIF flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Send stop. */
- result = I2C_MasterStop(base);
- }
- return result;
- }
- status_t I2C_MasterReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize, uint32_t flags)
- {
- status_t result = kStatus_Success;
- volatile uint8_t dummy = 0;
- /* Add this to avoid build warning. */
- dummy++;
- /* Wait until the data register is ready for transmit. */
- while (!(base->I2SR & kI2C_TransferCompleteFlag))
- {
- }
- /* Clear the IICIF flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Setup the I2C peripheral to receive data. */
- base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
- /* If rxSize equals 1, configure to send NAK. */
- if (rxSize == 1)
- {
- /* Issue NACK on read. */
- base->I2CR |= I2C_I2CR_TXAK_MASK;
- }
- /* Do dummy read. */
- dummy = base->I2DR;
- while ((rxSize--))
- {
- /* Wait until data transfer complete. */
- while (!(base->I2SR & kI2C_IntPendingFlag))
- {
- }
- /* Clear the IICIF flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Single byte use case. */
- if (rxSize == 0)
- {
- if (!(flags & kI2C_TransferNoStopFlag))
- {
- /* Issue STOP command before reading last byte. */
- result = I2C_MasterStop(base);
- }
- else
- {
- /* Change direction to Tx to avoid extra clocks. */
- base->I2CR |= I2C_I2CR_MTX_MASK;
- }
- }
- if (rxSize == 1)
- {
- /* Issue NACK on read. */
- base->I2CR |= I2C_I2CR_TXAK_MASK;
- }
- /* Read from the data register. */
- *rxBuff++ = base->I2DR;
- }
- return result;
- }
- status_t I2C_MasterTransferBlocking(I2C_Type *base, i2c_master_transfer_t *xfer)
- {
- assert(xfer);
- i2c_direction_t direction = xfer->direction;
- status_t result = kStatus_Success;
- /* Clear all status before transfer. */
- I2C_MasterClearStatusFlags(base, kClearFlags);
- /* Wait until ready to complete. */
- while (!(base->I2SR & kI2C_TransferCompleteFlag))
- {
- }
- /* Change to send write address when it's a read operation with command. */
- if ((xfer->subaddressSize > 0) && (xfer->direction == kI2C_Read))
- {
- direction = kI2C_Write;
- }
- /* If repeated start is requested, send repeated start. */
- if (xfer->flags & kI2C_TransferRepeatedStartFlag)
- {
- result = I2C_MasterRepeatedStart(base, xfer->slaveAddress, direction);
- }
- else /* For normal transfer, send start. */
- {
- result = I2C_MasterStart(base, xfer->slaveAddress, direction);
- }
- /* Return if error. */
- if (result)
- {
- return result;
- }
- while (!(base->I2SR & kI2C_IntPendingFlag))
- {
- }
- /* Check if there's transfer error. */
- result = I2C_CheckAndClearError(base, base->I2SR);
- /* Return if error. */
- if (result)
- {
- if (result == kStatus_I2C_Nak)
- {
- result = kStatus_I2C_Addr_Nak;
- I2C_MasterStop(base);
- }
- return result;
- }
- /* Send subaddress. */
- if (xfer->subaddressSize)
- {
- do
- {
- /* Clear interrupt pending flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- xfer->subaddressSize--;
- base->I2DR = ((xfer->subaddress) >> (8 * xfer->subaddressSize));
- /* Wait until data transfer complete. */
- while (!(base->I2SR & kI2C_IntPendingFlag))
- {
- }
- /* Check if there's transfer error. */
- result = I2C_CheckAndClearError(base, base->I2SR);
- if (result)
- {
- if (result == kStatus_I2C_Nak)
- {
- I2C_MasterStop(base);
- }
- return result;
- }
- } while ((xfer->subaddressSize > 0) && (result == kStatus_Success));
- if (xfer->direction == kI2C_Read)
- {
- /* Clear pending flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Send repeated start and slave address. */
- result = I2C_MasterRepeatedStart(base, xfer->slaveAddress, kI2C_Read);
- /* Return if error. */
- if (result)
- {
- return result;
- }
- /* Wait until data transfer complete. */
- while (!(base->I2SR & kI2C_IntPendingFlag))
- {
- }
- /* Check if there's transfer error. */
- result = I2C_CheckAndClearError(base, base->I2SR);
- if (result)
- {
- if (result == kStatus_I2C_Nak)
- {
- result = kStatus_I2C_Addr_Nak;
- I2C_MasterStop(base);
- }
- return result;
- }
- }
- }
- /* Transmit data. */
- if ((xfer->direction == kI2C_Write) && (xfer->dataSize > 0))
- {
- /* Send Data. */
- result = I2C_MasterWriteBlocking(base, xfer->data, xfer->dataSize, xfer->flags);
- }
- /* Receive Data. */
- if ((xfer->direction == kI2C_Read) && (xfer->dataSize > 0))
- {
- result = I2C_MasterReadBlocking(base, xfer->data, xfer->dataSize, xfer->flags);
- }
- return result;
- }
- void I2C_MasterTransferCreateHandle(I2C_Type *base,
- i2c_master_handle_t *handle,
- i2c_master_transfer_callback_t callback,
- void *userData)
- {
- assert(handle);
- uint32_t instance = I2C_GetInstance(base);
- /* Zero handle. */
- memset(handle, 0, sizeof(*handle));
- /* Set callback and userData. */
- handle->completionCallback = callback;
- handle->userData = userData;
- /* Save the context in global variables to support the double weak mechanism. */
- s_i2cHandle[instance] = handle;
- /* Save master interrupt handler. */
- s_i2cMasterIsr = I2C_MasterTransferHandleIRQ;
- /* Enable NVIC interrupt. */
- EnableIRQ(s_i2cIrqs[instance]);
- }
- status_t I2C_MasterTransferNonBlocking(I2C_Type *base, i2c_master_handle_t *handle, i2c_master_transfer_t *xfer)
- {
- assert(handle);
- assert(xfer);
- status_t result = kStatus_Success;
- /* Check if the I2C bus is idle - if not return busy status. */
- if (handle->state != kIdleState)
- {
- result = kStatus_I2C_Busy;
- }
- else
- {
- /* Start up the master transfer state machine. */
- result = I2C_InitTransferStateMachine(base, handle, xfer);
- if (result == kStatus_Success)
- {
- /* Enable the I2C interrupts. */
- I2C_EnableInterrupts(base, kI2C_GlobalInterruptEnable);
- }
- }
- return result;
- }
- void I2C_MasterTransferAbort(I2C_Type *base, i2c_master_handle_t *handle)
- {
- assert(handle);
- volatile uint8_t dummy = 0;
- /* Add this to avoid build warning. */
- dummy++;
- /* Disable interrupt. */
- I2C_DisableInterrupts(base, kI2C_GlobalInterruptEnable);
- /* Reset the state to idle. */
- handle->state = kIdleState;
- /* Send STOP signal. */
- if (handle->transfer.direction == kI2C_Read)
- {
- base->I2CR |= I2C_I2CR_TXAK_MASK;
- while (!(base->I2SR & kI2C_IntPendingFlag))
- {
- }
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- base->I2CR &= ~(I2C_I2CR_MSTA_MASK | I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
- dummy = base->I2DR;
- }
- else
- {
- while (!(base->I2SR & kI2C_IntPendingFlag))
- {
- }
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- base->I2CR &= ~(I2C_I2CR_MSTA_MASK | I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
- }
- }
- status_t I2C_MasterTransferGetCount(I2C_Type *base, i2c_master_handle_t *handle, size_t *count)
- {
- assert(handle);
- if (!count)
- {
- return kStatus_InvalidArgument;
- }
- *count = handle->transferSize - handle->transfer.dataSize;
- return kStatus_Success;
- }
- void I2C_MasterTransferHandleIRQ(I2C_Type *base, void *i2cHandle)
- {
- assert(i2cHandle);
- i2c_master_handle_t *handle = (i2c_master_handle_t *)i2cHandle;
- status_t result = kStatus_Success;
- bool isDone;
- /* Clear the interrupt flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Check transfer complete flag. */
- result = I2C_MasterTransferRunStateMachine(base, handle, &isDone);
- if (isDone || result)
- {
- /* Send stop command if transfer done or received Nak. */
- if ((!(handle->transfer.flags & kI2C_TransferNoStopFlag)) || (result == kStatus_I2C_Nak) ||
- (result == kStatus_I2C_Addr_Nak))
- {
- /* Ensure stop command is a need. */
- if ((base->I2CR & I2C_I2CR_MSTA_MASK))
- {
- if (I2C_MasterStop(base) != kStatus_Success)
- {
- result = kStatus_I2C_Timeout;
- }
- }
- }
- /* Restore handle to idle state. */
- handle->state = kIdleState;
- /* Disable interrupt. */
- I2C_DisableInterrupts(base, kI2C_GlobalInterruptEnable);
- /* Call the callback function after the function has completed. */
- if (handle->completionCallback)
- {
- handle->completionCallback(base, handle, result, handle->userData);
- }
- }
- }
- void I2C_SlaveInit(I2C_Type *base, const i2c_slave_config_t *slaveConfig)
- {
- assert(slaveConfig);
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /* Enable I2C clock. */
- CLOCK_EnableClock(s_i2cClocks[I2C_GetInstance(base)]);
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- base->IADR = ((uint32_t)(slaveConfig->slaveAddress)) << 1U;
- base->I2CR = I2C_I2CR_IEN(slaveConfig->enableSlave);
- }
- void I2C_SlaveDeinit(I2C_Type *base)
- {
- /* Disable I2C module. */
- I2C_Enable(base, false);
- #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
- /* Disable I2C clock. */
- CLOCK_DisableClock(s_i2cClocks[I2C_GetInstance(base)]);
- #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
- }
- void I2C_SlaveGetDefaultConfig(i2c_slave_config_t *slaveConfig)
- {
- assert(slaveConfig);
- /* Enable the I2C peripheral. */
- slaveConfig->enableSlave = true;
- }
- status_t I2C_SlaveWriteBlocking(I2C_Type *base, const uint8_t *txBuff, size_t txSize)
- {
- status_t result = kStatus_Success;
- volatile uint8_t dummy = 0;
- /* Add this to avoid build warning. */
- dummy++;
- /* Wait for address match flag. */
- while (!(base->I2SR & kI2C_AddressMatchFlag))
- {
- }
- /* Read dummy to release bus. */
- dummy = base->I2DR;
- result = I2C_MasterWriteBlocking(base, txBuff, txSize, kI2C_TransferDefaultFlag);
- /* Switch to receive mode. */
- base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
- /* Read dummy to release bus. */
- dummy = base->I2DR;
- return result;
- }
- void I2C_SlaveReadBlocking(I2C_Type *base, uint8_t *rxBuff, size_t rxSize)
- {
- volatile uint8_t dummy = 0;
- /* Add this to avoid build warning. */
- dummy++;
- /* Wait for address match and int pending flag. */
- while (!(base->I2SR & kI2C_AddressMatchFlag))
- {
- }
- /* Read dummy to release bus. */
- dummy = base->I2DR;
- /* Clear the IICIF flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Setup the I2C peripheral to receive data. */
- base->I2CR &= ~(I2C_I2CR_MTX_MASK);
- while (rxSize--)
- {
- /* Wait until data transfer complete. */
- while (!(base->I2SR & kI2C_IntPendingFlag))
- {
- }
- /* Clear the IICIF flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Read from the data register. */
- *rxBuff++ = base->I2DR;
- }
- }
- void I2C_SlaveTransferCreateHandle(I2C_Type *base,
- i2c_slave_handle_t *handle,
- i2c_slave_transfer_callback_t callback,
- void *userData)
- {
- assert(handle);
- uint32_t instance = I2C_GetInstance(base);
- /* Zero handle. */
- memset(handle, 0, sizeof(*handle));
- /* Set callback and userData. */
- handle->callback = callback;
- handle->userData = userData;
- /* Save the context in global variables to support the double weak mechanism. */
- s_i2cHandle[instance] = handle;
- /* Save slave interrupt handler. */
- s_i2cSlaveIsr = I2C_SlaveTransferHandleIRQ;
- /* Enable NVIC interrupt. */
- EnableIRQ(s_i2cIrqs[instance]);
- }
- status_t I2C_SlaveTransferNonBlocking(I2C_Type *base, i2c_slave_handle_t *handle, uint32_t eventMask)
- {
- assert(handle);
- /* Check if the I2C bus is idle - if not return busy status. */
- if (handle->state != kIdleState)
- {
- return kStatus_I2C_Busy;
- }
- else
- {
- /* Disable LPI2C IRQ sources while we configure stuff. */
- I2C_DisableInterrupts(base, kIrqFlags);
- /* Clear transfer in handle. */
- memset(&handle->transfer, 0, sizeof(handle->transfer));
- /* Record that we're busy. */
- handle->state = kCheckAddressState;
- /* Set up event mask. tx and rx are always enabled. */
- handle->eventMask = eventMask | kI2C_SlaveTransmitEvent | kI2C_SlaveReceiveEvent;
- /* Clear all flags. */
- I2C_SlaveClearStatusFlags(base, kClearFlags);
- /* Enable I2C internal IRQ sources. NVIC IRQ was enabled in CreateHandle() */
- I2C_EnableInterrupts(base, kIrqFlags);
- }
- return kStatus_Success;
- }
- void I2C_SlaveTransferAbort(I2C_Type *base, i2c_slave_handle_t *handle)
- {
- assert(handle);
- if (handle->state != kIdleState)
- {
- /* Disable interrupts. */
- I2C_DisableInterrupts(base, kIrqFlags);
- /* Reset transfer info. */
- memset(&handle->transfer, 0, sizeof(handle->transfer));
- /* Reset the state to idle. */
- handle->state = kIdleState;
- }
- }
- status_t I2C_SlaveTransferGetCount(I2C_Type *base, i2c_slave_handle_t *handle, size_t *count)
- {
- assert(handle);
- if (!count)
- {
- return kStatus_InvalidArgument;
- }
- /* Catch when there is not an active transfer. */
- if (handle->state == kIdleState)
- {
- *count = 0;
- return kStatus_NoTransferInProgress;
- }
- /* For an active transfer, just return the count from the handle. */
- *count = handle->transfer.transferredCount;
- return kStatus_Success;
- }
- void I2C_SlaveTransferHandleIRQ(I2C_Type *base, void *i2cHandle)
- {
- assert(i2cHandle);
- uint16_t status;
- bool doTransmit = false;
- i2c_slave_handle_t *handle = (i2c_slave_handle_t *)i2cHandle;
- i2c_slave_transfer_t *xfer;
- volatile uint8_t dummy = 0;
- /* Add this to avoid build warning. */
- dummy++;
- status = I2C_SlaveGetStatusFlags(base);
- xfer = &(handle->transfer);
- /* Clear the interrupt flag. */
- base->I2SR &= (uint8_t)~kI2C_IntPendingFlag;
- /* Check NAK */
- if (status & kI2C_ReceiveNakFlag)
- {
- /* Set receive mode. */
- base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
- /* Read dummy. */
- dummy = base->I2DR;
- if (handle->transfer.dataSize != 0)
- {
- xfer->event = kI2C_SlaveCompletionEvent;
- xfer->completionStatus = kStatus_I2C_Nak;
- handle->state = kIdleState;
- if ((handle->eventMask & xfer->event) && (handle->callback))
- {
- handle->callback(base, xfer, handle->userData);
- }
- }
- else
- {
- xfer->event = kI2C_SlaveCompletionEvent;
- xfer->completionStatus = kStatus_Success;
- handle->state = kIdleState;
- if ((handle->eventMask & xfer->event) && (handle->callback))
- {
- handle->callback(base, xfer, handle->userData);
- }
- }
- }
- /* Check address match. */
- else if (status & kI2C_AddressMatchFlag)
- {
- xfer->event = kI2C_SlaveAddressMatchEvent;
- /* Slave transmit, master reading from slave. */
- if (status & kI2C_TransferDirectionFlag)
- {
- handle->state = kSendDataState;
- /* Change direction to send data. */
- base->I2CR |= I2C_I2CR_MTX_MASK;
- doTransmit = true;
- }
- else
- {
- handle->state = kReceiveDataState;
- /* Slave receive, master writing to slave. */
- base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
- /* Read dummy to release the bus. */
- dummy = base->I2DR;
- }
- if ((handle->eventMask & xfer->event) && (handle->callback))
- {
- handle->callback(base, xfer, handle->userData);
- }
- }
- /* Check transfer complete flag. */
- else if (status & kI2C_TransferCompleteFlag)
- {
- /* Slave transmit, master reading from slave. */
- if (handle->state == kSendDataState)
- {
- doTransmit = true;
- }
- else
- {
- /* If we're out of data, invoke callback to get more. */
- if ((!xfer->data) || (!xfer->dataSize))
- {
- xfer->event = kI2C_SlaveReceiveEvent;
- if (handle->callback)
- {
- handle->callback(base, xfer, handle->userData);
- }
- /* Clear the transferred count now that we have a new buffer. */
- xfer->transferredCount = 0;
- }
- /* Slave receive, master writing to slave. */
- uint8_t data = base->I2DR;
- if (handle->transfer.dataSize)
- {
- /* Receive data. */
- *handle->transfer.data++ = data;
- handle->transfer.dataSize--;
- xfer->transferredCount++;
- if (!handle->transfer.dataSize)
- {
- xfer->event = kI2C_SlaveCompletionEvent;
- xfer->completionStatus = kStatus_Success;
- handle->state = kIdleState;
- /* Proceed receive complete event. */
- if ((handle->eventMask & xfer->event) && (handle->callback))
- {
- handle->callback(base, xfer, handle->userData);
- }
- }
- }
- }
- }
- else
- {
- /* Read dummy to release bus. */
- dummy = base->I2DR;
- }
- /* Send data if there is the need. */
- if (doTransmit)
- {
- /* If we're out of data, invoke callback to get more. */
- if ((!xfer->data) || (!xfer->dataSize))
- {
- xfer->event = kI2C_SlaveTransmitEvent;
- if (handle->callback)
- {
- handle->callback(base, xfer, handle->userData);
- }
- /* Clear the transferred count now that we have a new buffer. */
- xfer->transferredCount = 0;
- }
- if (handle->transfer.dataSize)
- {
- /* Send data. */
- base->I2DR = *handle->transfer.data++;
- handle->transfer.dataSize--;
- xfer->transferredCount++;
- }
- else
- {
- /* Switch to receive mode. */
- base->I2CR &= ~(I2C_I2CR_MTX_MASK | I2C_I2CR_TXAK_MASK);
- /* Read dummy to release bus. */
- dummy = base->I2DR;
- xfer->event = kI2C_SlaveCompletionEvent;
- xfer->completionStatus = kStatus_Success;
- handle->state = kIdleState;
- /* Proceed txdone event. */
- if ((handle->eventMask & xfer->event) && (handle->callback))
- {
- handle->callback(base, xfer, handle->userData);
- }
- }
- }
- }
- #if defined(I2C1)
- void I2C1_DriverIRQHandler(void)
- {
- I2C_TransferCommonIRQHandler(I2C1, s_i2cHandle[1]);
- }
- #endif
- #if defined(I2C2)
- void I2C2_DriverIRQHandler(void)
- {
- I2C_TransferCommonIRQHandler(I2C2, s_i2cHandle[2]);
- }
- #endif
- #if defined(I2C3)
- void I2C3_DriverIRQHandler(void)
- {
- I2C_TransferCommonIRQHandler(I2C3, s_i2cHandle[3]);
- }
- #endif
- #if defined(I2C4)
- void I2C4_DriverIRQHandler(void)
- {
- I2C_TransferCommonIRQHandler(I2C4, s_i2cHandle[4]);
- }
- #endif
- #include <drv_i2c.h>
- void I2C_DriverIRQHandler(int irq, void *base)
- {
- struct imx6ull_i2c_config *config;
- uint32_t i2c_instance = 0;
- config = (struct imx6ull_i2c_config*)base;
- i2c_instance = I2C_GetInstance(config->hw_base);
- I2C_TransferCommonIRQHandler(config->I2C, s_i2cHandle[i2c_instance]);
- }
|