12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 |
- /*
- ******************************************************************************
- * @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 */
- /* 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)
- {
- /* 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;
- }
- }
|