123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143 |
- /*
- ******************************************************************************
- * @file HAL_I2C.c
- * @version V1.0.0
- * @date 2020
- * @brief I2C HAL module driver.
- * This file provides firmware functions to manage the following
- * functionalities of the Inter Integrated Circuit (I2C) peripheral:
- * @ Initialization and de-initialization functions
- * @ IO operation functions
- * @ Peripheral Control functions
- ******************************************************************************
- */
- #include "ACM32Fxx_HAL.h"
- /* Private functions for I2C */
- static HAL_StatusTypeDef I2C_Set_Clock_Speed(I2C_HandleTypeDef *hi2c, uint32_t ClockSpeed);
- static HAL_StatusTypeDef I2C_Master_Request_Write(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout);
- static HAL_StatusTypeDef I2C_Master_Request_Read(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout);
- static HAL_StatusTypeDef I2C_Check_Device_Ready(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout);
- static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
- /************************************************************************
- * function : HAL_I2C_IRQHandler
- * Description: This function handles I2C interrupt request.
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- ************************************************************************/
- __weak void HAL_I2C_IRQHandler(I2C_HandleTypeDef *hi2c)
- {
- uint32_t i;
-
- /* Slave ADDR1 Interrupt */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1))
- {
- /* Clear ADDR1 Interrupt Flag */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
- /* Slave Transmit */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_SRW))
- {
- i = 1;
- /* Wait for transmission End*/
- while(!READ_BIT(hi2c->Instance->SR, I2C_SR_MTF));
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- /* BUS BUSY */
- while(READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
- {
- if (i >= hi2c->Tx_Size && hi2c->Tx_Size != 0)
- {
- break;
- }
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_MTF))
- {
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- }
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_TXE))
- {
- hi2c->Instance->DR = hi2c->Tx_Buffer[i++];
- hi2c->Tx_Count++;
- }
- }
-
- /* Set Slave machine is DILE */
- hi2c->Slave_TxState = SLAVE_TX_STATE_IDLE;
- }
- /* Slave Receive */
- else
- {
- i = 0;
- /* Wait for transmission End*/
- while(!READ_BIT(hi2c->Instance->SR, I2C_SR_MTF));
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- /* BUS BUSY */
- while(READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
- {
- /* Receive Data */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RXNE))
- {
- hi2c->Rx_Buffer[i++] = hi2c->Instance->DR;
- /* Wait for transmission End*/
- while(!READ_BIT(hi2c->Instance->SR, I2C_SR_MTF));
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- hi2c->Rx_Count++;
- if (hi2c->Rx_Size != 0)
- {
- if (i >= hi2c->Rx_Size)
- {
- break;
- }
- }
- }
- }
-
- /* Set Slave machine is DILE */
- hi2c->Slave_RxState = SLAVE_RX_STATE_IDLE;
- }
- if (hi2c->Slave_RxState == SLAVE_RX_STATE_IDLE && hi2c->Slave_TxState == SLAVE_TX_STATE_IDLE)
- {
- /* Disable RX_ADDR1_INT_EN */
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_RX_ADDR1_INT_EN);
- }
- }
-
- /* STOP Flag Interrupt */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_STOPF))
- {
- /* Clear STOPF Interrupt Flag */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_STOPF);
-
- /* Clear STOPF */
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_STOPF_INTEN);
-
- if (hi2c->I2C_STOPF_Callback != NULL)
- {
- hi2c->I2C_STOPF_Callback();
- }
- }
- }
- /************************************************************************
- * function : HAL_I2C_MspInit
- * Description:
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- ************************************************************************/
- __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
- {
- /*
- NOTE : This function should be modified by the user.
- */
-
- /* For Example */
- GPIO_InitTypeDef GPIO_Handle;
-
- /* I2C1 */
- if (hi2c->Instance == I2C1)
- {
- /* Enable Clock */
- System_Module_Enable(EN_I2C1);
- System_Module_Enable(EN_GPIOAB);
- /* I2C1 SDA PortB Pin7 */
- /* I2C1 SCL PortB Pin6 */
- GPIO_Handle.Pin = GPIO_PIN_6 | GPIO_PIN_7;
- GPIO_Handle.Mode = GPIO_MODE_AF_PP;
- GPIO_Handle.Pull = GPIO_PULLUP;
- GPIO_Handle.Alternate = GPIO_FUNCTION_6;
- HAL_GPIO_Init(GPIOB, &GPIO_Handle);
-
- /* Clear Pending Interrupt */
- NVIC_ClearPendingIRQ(I2C1_IRQn);
-
- /* Enable External Interrupt */
- NVIC_EnableIRQ(I2C1_IRQn);
- }
- /* I2C2 */
- else if (hi2c->Instance == I2C2)
- {
- }
- }
- /************************************************************************
- * function : HAL_I2C_MspDeInit
- * Description:
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- ************************************************************************/
- __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
- {
- /*
- NOTE : This function should be modified by the user.
- */
-
- /* For Example */
- GPIO_InitTypeDef GPIO_Handle;
-
- /* I2C1 */
- if (hi2c->Instance == I2C1)
- {
- /* Disable Clock */
- System_Module_Disable(EN_I2C1);
- /* I2C1 SDA PortB Pin7 */
- /* I2C1 SCL PortB Pin6 */
- HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6 | GPIO_PIN_7);
- /* Clear Pending Interrupt */
- NVIC_ClearPendingIRQ(I2C1_IRQn);
-
- /* Disable External Interrupt */
- NVIC_DisableIRQ(I2C1_IRQn);
- }
- /* I2C2 */
- else if (hi2c->Instance == I2C2)
- {
- }
- }
- /************************************************************************
- * function : HAL_I2C_Init
- * Description: I2c initial with parameters.
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
- {
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
- if (!IS_I2C_ALL_MODE(hi2c->Init.I2C_Mode)) return HAL_ERROR;
- if (!IS_I2C_CLOCK_SPEED(hi2c->Init.Clock_Speed)) return HAL_ERROR;
- if (!IS_I2C_TX_AUTO_EN(hi2c->Init.Tx_Auto_En)) return HAL_ERROR;
- if (!IS_I2C_STRETCH_EN(hi2c->Init.No_Stretch_Mode)) return HAL_ERROR;
- /* Disable the selected I2C peripheral */
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_MEN);
- /* Init the low level hardware : GPIO, CLOCK, NVIC */
- HAL_I2C_MspInit(hi2c);
- switch (hi2c->Init.I2C_Mode)
- {
- /* Master Mode */
- case I2C_MODE_MASTER:
- {
- /* Set Master Mode */
- SET_BIT(hi2c->Instance->CR, I2C_CR_MASTER);
-
- /* Set Clock Speed */
- I2C_Set_Clock_Speed(hi2c, hi2c->Init.Clock_Speed);
-
- /* Set SDA auto change the direction */
- if (hi2c->Init.Tx_Auto_En == TX_AUTO_EN_ENABLE)
- SET_BIT(hi2c->Instance->CR, I2C_CR_TX_AUTO_EN);
- else
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TX_AUTO_EN);
-
- /* Enable the selected I2C peripheral */
- SET_BIT(hi2c->Instance->CR, I2C_CR_MEN);
- }break;
-
- /* Slave Mode */
- case I2C_MODE_SLAVE:
- {
- SET_BIT(hi2c->Instance->CR, I2C_CR_TXE_SEL);
- /* Set SDA auto change the direction */
- if (hi2c->Init.Tx_Auto_En == TX_AUTO_EN_ENABLE)
- SET_BIT(hi2c->Instance->CR, I2C_CR_TX_AUTO_EN);
- else
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TX_AUTO_EN);
-
- /* Set Clock Stretch Mode */
- if (hi2c->Init.No_Stretch_Mode == NO_STRETCH_MODE_NOSTRETCH)
- SET_BIT(hi2c->Instance->CR, I2C_CR_NOSTRETCH);
- else
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_NOSTRETCH);
- /* Set Address 1 */
- hi2c->Instance->SLAVE_ADDR1 = hi2c->Init.Own_Address;
-
- /* Enable the selected I2C peripheral */
- SET_BIT(hi2c->Instance->CR, I2C_CR_MEN);
- }break;
-
- default: break;
- }
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_DeInit
- * Description: I2c De-initial with parameters.
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
- {
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
-
- hi2c->Slave_RxState = SLAVE_RX_STATE_IDLE;
- hi2c->Slave_TxState = SLAVE_TX_STATE_IDLE;
-
- HAL_I2C_MspDeInit(hi2c);
-
- hi2c->Tx_Size = 0;
- hi2c->Rx_Size = 0;
- hi2c->Tx_Count = 0;
- hi2c->Rx_Count = 0;
-
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Master_Transmit
- * Description: Transmits in master mode an amount of data in blocking mode.
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * DevAddress : Target device address
- * pData : Pointer to data buffer
- * Size : Amount of data to be sent
- * Timeout : Timeout value
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
- {
- uint32_t i;
-
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
-
- hi2c->Tx_Buffer = pData;
- hi2c->Tx_Size = Size;
- hi2c->Tx_Count = 0;
-
- /* Send Write Access Request */
- if (I2C_Master_Request_Write(hi2c, DevAddress, 0) == HAL_OK)
- {
- for (i = 0; i < hi2c->Tx_Size; i++)
- {
- /* Wait TXE Flag */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_TXE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
-
- /* Send Data */
- hi2c->Instance->DR = hi2c->Tx_Buffer[hi2c->Tx_Count++];
-
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
-
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
-
- /* Get NACK */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
- {
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
-
- return HAL_ERROR;
- }
- }
-
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
- }
- else
- {
- return HAL_ERROR;
- }
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Master_Receive
- * Description: Transmits in master mode an amount of data in blocking mode.
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * DevAddress : Target device address
- * pData : Pointer to data buffer
- * Size : Amount of data to be Receive
- * Timeout : Timeout value
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
- {
- uint32_t i;
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
-
- hi2c->Rx_Buffer = pData;
- hi2c->Rx_Size = Size;
- hi2c->Rx_Count = 0;
-
- /* Send Read Access Request */
- if (I2C_Master_Request_Read(hi2c, DevAddress, Timeout) == HAL_OK)
- {
- /* Wait Master Transition receiving state */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_TX_RX_FLAG, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear TX_RX_FLAG */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_TX_RX_FLAG);
- /* Generate ACK */
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
- for (i = 0; i < hi2c->Rx_Size - 1; i++)
- {
- /* Wait RXNE Flag */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RXNE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Read Data */
- hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- }
- /* Prepare for Generate NACK */
- SET_BIT(hi2c->Instance->CR, I2C_CR_TACK);
- /* Prepare for Generate STOP */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
- /* Wait RXNE Flag */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RXNE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Read Data */
- hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Generate ACK */
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
- }
- else
- {
- return HAL_ERROR;
- }
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Slave_Transmit
- * Description: Transmits in Slave mode an amount of data in blocking mode.
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * pData : Pointer to data buffer
- * Size : Amount of data to be sent
- * Timeout : Timeout value
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size, uint32_t Timeout)
- {
- uint32_t i = 0;
-
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
-
- hi2c->Tx_Buffer = pData;
- hi2c->Tx_Size = Size;
- hi2c->Tx_Count = 0;
- /* Clear RX_ADDR1 Flag */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
- /* Match the Address 1 */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RX_ADDR1, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear RX_ADDR1 Flag */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
- /* Slave Transmit */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_SRW))
- {
- /* BUS BUSY */
- while(READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
- {
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_MTF))
- {
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- hi2c->Tx_Count++;
- }
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_TXE))
- {
- if (i < hi2c->Tx_Size || hi2c->Tx_Size == 0)
- {
- hi2c->Instance->DR = hi2c->Tx_Buffer[i++];
- }
- }
- }
- hi2c->Instance->SR = READ_REG(hi2c->Instance->SR);
- }
- else
- {
- return HAL_ERROR;
- }
- hi2c->Tx_Count--;
-
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Slave_Transmit_IT
- * Description: Transmit in slave mode an amount of data in non-blocking mode with Interrupt
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * pData : Pointer to data buffer
- * Size : Amount of data to be sent
- * return : HAL_StatusTypeDef
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size)
- {
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
- /* Rx machine is running */
- if (hi2c->Slave_TxState != SLAVE_TX_STATE_IDLE)
- return HAL_ERROR;
- /* Set Slave machine is sending */
- hi2c->Slave_TxState = SLAVE_TX_STATE_SENDING;
- hi2c->Tx_Buffer = pData;
- hi2c->Tx_Size = Size;
- hi2c->Tx_Count = 0;
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TXE_SEL);
- hi2c->Instance->DR = hi2c->Tx_Buffer[0];
- hi2c->Tx_Count++;
- /* Clear RX ADDR1 Flag */
- SET_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
- /* RX ADDR1 Interrupt Enable */
- SET_BIT(hi2c->Instance->CR, I2C_CR_RX_ADDR1_INT_EN);
-
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Slave_Receive
- * Description: Receive in Slave mode an amount of data in blocking mode.
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * pData : Pointer to data buffer
- * Size : Amount of data to be sent
- * Timeout : Timeout value
- * return : HAL_StatusTypeDef
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size, uint32_t Timeout)
- {
- uint32_t i = 0;
- HAL_StatusTypeDef Status;
-
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;;
-
- hi2c->Rx_Buffer = pData;
- hi2c->Rx_Size = Size;
- hi2c->Rx_Count = 0;
-
- /* Match the Address 1 */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RX_ADDR1, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear RX_ADDR1 Flag */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
-
- /* Slave Receive */
- if (!READ_BIT(hi2c->Instance->SR, I2C_SR_SRW))
- {
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- /* BUS BUSY */
- while(READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
- {
- /* Receive Data */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RXNE))
- {
- hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
-
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- if (hi2c->Rx_Size != 0)
- {
- if (hi2c->Rx_Count >= hi2c->Rx_Size)
- {
- break;
- }
- }
- }
- }
-
- /* Generate ACK */
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
-
- hi2c->Instance->SR = READ_REG(hi2c->Instance->SR);
- }
- /* Slave Transmit */
- else
- {
- return HAL_ERROR;
- }
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Slave_Receive_IT
- * Description: Receive in slave mode an amount of data in non-blocking mode with Interrupt
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * pData : Pointer to data buffer
- * Size : Amount of data to be sent
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size)
- {
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
- /* Rx machine is running */
- if (hi2c->Slave_RxState != SLAVE_RX_STATE_IDLE)
- return HAL_ERROR;
- /* Set Slave machine is receiving */
- hi2c->Slave_RxState = SLAVE_RX_STATE_RECEIVING;
-
- hi2c->Rx_Buffer = pData;
- hi2c->Rx_Size = Size;
- hi2c->Rx_Count = 0;
-
- /* Clear RX ADDR1 Flag */
- SET_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
- /* RX ADDR1 Interrupt Enable */
- SET_BIT(hi2c->Instance->CR, I2C_CR_RX_ADDR1_INT_EN);
-
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Slave_Receive_DMA
- * Description: Receive in slave mode an amount of data in non-blocking mode with DMA
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * pData : Pointer to data buffer
- * Size : Amount of data to be sent
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size)
- {
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
- hi2c->Rx_Buffer = pData;
- hi2c->Rx_Size = Size;
- hi2c->Rx_Count = Size;
- /* DMA Enable */
- SET_BIT(hi2c->Instance->CR, I2C_CR_DMA_EN);
- /* Clear STOPF Interrupt Flag */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_STOPF);
- /* STOPF Interrupt Enable */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOPF_INTEN);
- HAL_DMA_Start(hi2c->HDMA_Rx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->Rx_Buffer, hi2c->Rx_Size);
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Slave_Transmit_DMA
- * Description: Transmit in slave mode an amount of data in non-blocking mode with DMA
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * pData : Pointer to data buffer
- * Size : Amount of data to be sent
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size)
- {
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
- hi2c->Tx_Buffer = pData;
- hi2c->Tx_Size = Size;
- hi2c->Tx_Count = Size;
-
- /* Must Set TXE_SEL In DMA Mode !!! */
- SET_BIT(hi2c->Instance->CR, I2C_CR_TXE_SEL);
- /* DMA Enable */
- SET_BIT(hi2c->Instance->CR, I2C_CR_DMA_EN);
- HAL_DMA_Start_IT(hi2c->HDMA_Tx, (uint32_t)hi2c->Tx_Buffer, (uint32_t)&hi2c->Instance->DR, hi2c->Tx_Size);
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Mem_Write
- * Description: Write an amount of data in blocking mode to a specific memory address
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * DevAddress : Target device address
- * MemAddress : MemAddress Internal memory address
- * MemAddSize : MemAddSize Size of internal memory address
- * pData : Pointer to data buffer
- * Size : Amount of data to be sent
- * Timeout : Timeout value
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
- {
- uint32_t i;
-
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
- hi2c->Tx_Buffer = pData;
- hi2c->Tx_Size = Size;
- hi2c->Tx_Count = 0;
-
- /* Bus Busy */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
- return HAL_ERROR;
-
- /* Send Write Access Request */
- if (I2C_Master_Request_Write(hi2c, DevAddress,0) == HAL_OK)
- {
- /* If Memory address size is 8Bit */
- if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
- {
- /* Send Memory Address */
- hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
- }
- /* If Memory address size is 16Bit */
- else
- {
- /* Send Memory Address MSB*/
- hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- /* Send Memory Address LSB*/
- hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
- }
-
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
-
- /* Get NACK */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
- {
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
-
- return HAL_ERROR;
- }
- /* Get ACK */
- else
- {
- for (i = 0; i < hi2c->Tx_Size; i++)
- {
- /* Wait TXE Flag */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_TXE, RESET, Timeout)!= HAL_OK) return HAL_ERROR;
-
- /* Send Data */
- hi2c->Instance->DR = hi2c->Tx_Buffer[hi2c->Tx_Count++];
-
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
-
- /* Get NACK */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
- {
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
- return HAL_ERROR;
- }
- }
-
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Check Device Ready */
- while(I2C_Check_Device_Ready(hi2c, DevAddress, Timeout) != HAL_OK);
- }
- }
- else
- {
- return HAL_ERROR;
- }
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_Mem_Read
- * Description: Read an amount of data in blocking mode to a specific memory address
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * DevAddress : Target device address
- * MemAddress : MemAddress Internal memory address
- * MemAddSize : MemAddSize Size of internal memory address
- * pData : Pointer to data buffer
- * Size : Amount of data to be sent
- * Timeout : Timeout value
- ************************************************************************/
- HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
- {
- uint32_t i;
-
- /* Check I2C Parameter */
- if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
- hi2c->Rx_Buffer = pData;
- hi2c->Rx_Size = Size;
- hi2c->Rx_Count = 0;
-
- /* Bus Busy */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
- return HAL_ERROR;
-
- /* Send Write Access Request */
- if (I2C_Master_Request_Write(hi2c, DevAddress,0) == HAL_OK)
- {
- /* If Memory address size is 8Bit */
- if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
- {
- /* Send Memory Address */
- hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
- }
- /* If Memory address size is 16Bit */
- else
- {
- /* Send Memory Address MSB*/
- hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- /* Send Memory Address LSB*/
- hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
- }
-
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
-
- /* Get NACK */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
- {
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
-
- return HAL_ERROR;
- }
- /* Get ACK */
- else
- {
- /* Send Write Read Request */
- if (I2C_Master_Request_Read(hi2c, DevAddress, Timeout) == HAL_OK)
- {
- /* Wait Master Transition receiving state */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_TX_RX_FLAG, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear TX_RX_FLAG */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_TX_RX_FLAG);
- /* Generate ACK */
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
- for (i = 0; i < hi2c->Rx_Size - 1; i++)
- {
- /* Wait RXNE Flag */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RXNE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Read Data */
- hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- }
- /* Prepare for Generate NACK */
- SET_BIT(hi2c->Instance->CR, I2C_CR_TACK);
- /* Prepare for Generate STOP */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
- /* Wait RXNE Flag */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RXNE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Read Data */
- hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Generate ACK */
- CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
- }
- else
- {
- /* Get NACK */
- return HAL_ERROR;
- }
- }
- }
- else
- {
- return HAL_ERROR;
- }
- return HAL_OK;
- }
- /************************************************************************
- * function : HAL_I2C_GetSlaveRxState
- * Description: Get Slave Rx State
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * return : Slave State
- ************************************************************************/
- uint8_t HAL_I2C_GetSlaveRxState(I2C_HandleTypeDef *hi2c)
- {
- return hi2c->Slave_RxState;
- }
- /************************************************************************
- * function : HAL_I2C_GetSlaveTxState
- * Description: Get Slave Tx State
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * return : Slave State
- ************************************************************************/
- uint8_t HAL_I2C_GetSlaveTxState(I2C_HandleTypeDef *hi2c)
- {
- return hi2c->Slave_TxState;
- }
- /************************************************************************
- * function : I2C_Set_Clock_Speed
- * Description: Set I2C Clock Speed
- * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * ClockSpeed: I2C Clock Speed
- ************************************************************************/
- static HAL_StatusTypeDef I2C_Set_Clock_Speed(I2C_HandleTypeDef *hi2c, uint32_t ClockSpeed)
- {
- uint32_t APB_Clock;
- APB_Clock = System_Get_APBClock();
- hi2c->Instance->CLK_DIV = APB_Clock / (4 * ClockSpeed) - 1;
- return HAL_OK;
- }
- /**
- * @brief This function handles I2C Communication Timeout.
- * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
- * the configuration information for I2C module
- * @param Flag specifies the I2C flag to check.
- * @param Status The new Flag status (SET or RESET).
- * @param Timeout Timeout duration
- * @param Tickstart Tick start value
- * @retval HAL status
- */
- static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
- {
- __IO uint32_t lu32_Timeout;
- /* have no timeout */
- if (Timeout == 0)
- {
- while (__HAL_I2C_GET_FLAG(hi2c, Flag)==Status);
- }
- else
- {
- lu32_Timeout = Timeout * 0xFF;
-
- while (__HAL_I2C_GET_FLAG(hi2c, Flag)==Status)
- {
- if (lu32_Timeout-- == 0)
- {
- return HAL_ERROR;
- }
- }
- }
- return HAL_OK;
- }
- /************************************************************************
- * function : I2C_Master_Request_Write
- * Description: I2C Write Access Request
- * input : hi2c : pointer to a I2C_HandleTypeDef structure
- * DevAddress: Device address
- * Timeout: Timeout value
- ************************************************************************/
- static HAL_StatusTypeDef I2C_Master_Request_Write(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout)
- {
- /* Generate Start */
- SET_BIT(hi2c->Instance->CR, I2C_CR_START);
- /* Clear MTF, To Prevent Errors */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- /* Send Device Address */
- hi2c->Instance->DR = DevAddress & 0xFE;
- /* Wait for transmission End*/
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
- /* Get NACK */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
- {
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
-
- return HAL_ERROR;
- }
- /* Get ACK */
- else
- {
- return HAL_OK;
- }
- }
- /************************************************************************
- * function : I2C_Master_Request_Read
- * Description: I2C Read Access Request
- * input : hi2c : pointer to a I2C_HandleTypeDef structure
- * DevAddress: Device address
- * Timeout: Timeout value
- ************************************************************************/
- static HAL_StatusTypeDef I2C_Master_Request_Read(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout)
- {
- /* Generate Start */
- SET_BIT(hi2c->Instance->CR, I2C_CR_START);
- /* Clear MTF, To Prevent Errors */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
-
- /* Send Device Address */
- hi2c->Instance->DR = DevAddress | 0x01;
-
- /* Wait for transmission End */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
-
- /* Get NACK */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
- {
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
-
- return HAL_ERROR;
- }
- /* Get ACK */
- else
- {
- return HAL_OK;
- }
- }
- /************************************************************************
- * function : I2C_Check_Device_Ready
- * Description: Check Device Ready
- * input : hi2c : pointer to a I2C_HandleTypeDef structure
- * DevAddress: Device address
- * Timeout: Timeout value
- ************************************************************************/
- static HAL_StatusTypeDef I2C_Check_Device_Ready(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout)
- {
- /* Bus Busy */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
- return HAL_ERROR;
- /* Generate Start */
- SET_BIT(hi2c->Instance->CR, I2C_CR_START);
-
- /* Send Device Address */
- hi2c->Instance->DR = DevAddress;
-
- /* Wait for transmission End */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
- /* Clear MTF */
- hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
-
- /* Get NACK */
- if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
- {
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
-
- return HAL_ERROR;
- }
- /* Get ACK */
- else
- {
- /* Generate Stop */
- SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
-
- /* Wait for the bus to idle */
- if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
-
- return HAL_OK;
- }
- }
|