2 ******************************************************************************
3 * @file stm32h7xx_ll_i2c.c
4 * @author MCD Application Team
5 * @brief I2C LL module driver.
6 ******************************************************************************
9 * Copyright (c) 2017 STMicroelectronics.
10 * All rights reserved.
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
16 ******************************************************************************
18 #if defined(USE_FULL_LL_DRIVER)
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32h7xx_ll_i2c.h"
22 #include "stm32h7xx_ll_bus.h"
23 #ifdef USE_FULL_ASSERT
24 #include "stm32_assert.h"
27 #define assert_param(expr) ((void)0U)
29 #endif /* USE_FULL_ASSERT */
31 /** @addtogroup STM32H7xx_LL_Driver
35 #if defined (I2C1) || defined (I2C2) || defined (I2C3) || defined (I2C4) || defined (I2C5)
37 /** @defgroup I2C_LL I2C
41 /* Private types -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 /* Private constants ---------------------------------------------------------*/
44 /* Private macros ------------------------------------------------------------*/
45 /** @addtogroup I2C_LL_Private_Macros
49 #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
50 ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
51 ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
52 ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
54 #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
55 ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
57 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
59 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
61 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
62 ((__VALUE__) == LL_I2C_NACK))
64 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
65 ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
70 /* Private function prototypes -----------------------------------------------*/
72 /* Exported functions --------------------------------------------------------*/
73 /** @addtogroup I2C_LL_Exported_Functions
77 /** @addtogroup I2C_LL_EF_Init
82 * @brief De-initialize the I2C registers to their default reset values.
83 * @param I2Cx I2C Instance.
84 * @retval An ErrorStatus enumeration value:
85 * - SUCCESS: I2C registers are de-initialized
86 * - ERROR: I2C registers are not de-initialized
88 ErrorStatus
LL_I2C_DeInit(const I2C_TypeDef
*I2Cx
)
90 ErrorStatus status
= SUCCESS
;
92 /* Check the I2C Instance I2Cx */
93 assert_param(IS_I2C_ALL_INSTANCE(I2Cx
));
97 /* Force reset of I2C clock */
98 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1
);
100 /* Release reset of I2C clock */
101 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1
);
103 else if (I2Cx
== I2C2
)
105 /* Force reset of I2C clock */
106 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2
);
108 /* Release reset of I2C clock */
109 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2
);
112 else if (I2Cx
== I2C3
)
114 /* Force reset of I2C clock */
115 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3
);
117 /* Release reset of I2C clock */
118 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3
);
120 else if (I2Cx
== I2C4
)
122 /* Force reset of I2C clock */
123 LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_I2C4
);
125 /* Release reset of I2C clock */
126 LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_I2C4
);
129 else if (I2Cx
== I2C5
)
131 /* Force reset of I2C clock */
132 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C5
);
134 /* Release reset of I2C clock */
135 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C5
);
147 * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
148 * @param I2Cx I2C Instance.
149 * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
150 * @retval An ErrorStatus enumeration value:
151 * - SUCCESS: I2C registers are initialized
152 * - ERROR: Not applicable
154 ErrorStatus
LL_I2C_Init(I2C_TypeDef
*I2Cx
, const LL_I2C_InitTypeDef
*I2C_InitStruct
)
156 /* Check the I2C Instance I2Cx */
157 assert_param(IS_I2C_ALL_INSTANCE(I2Cx
));
159 /* Check the I2C parameters from I2C_InitStruct */
160 assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct
->PeripheralMode
));
161 assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct
->AnalogFilter
));
162 assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct
->DigitalFilter
));
163 assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct
->OwnAddress1
));
164 assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct
->TypeAcknowledge
));
165 assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct
->OwnAddrSize
));
167 /* Disable the selected I2Cx Peripheral */
168 LL_I2C_Disable(I2Cx
);
170 /*---------------------------- I2Cx CR1 Configuration ------------------------
171 * Configure the analog and digital noise filters with parameters :
172 * - AnalogFilter: I2C_CR1_ANFOFF bit
173 * - DigitalFilter: I2C_CR1_DNF[3:0] bits
175 LL_I2C_ConfigFilters(I2Cx
, I2C_InitStruct
->AnalogFilter
, I2C_InitStruct
->DigitalFilter
);
177 /*---------------------------- I2Cx TIMINGR Configuration --------------------
178 * Configure the SDA setup, hold time and the SCL high, low period with parameter :
179 * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0],
180 * I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits
182 LL_I2C_SetTiming(I2Cx
, I2C_InitStruct
->Timing
);
184 /* Enable the selected I2Cx Peripheral */
187 /*---------------------------- I2Cx OAR1 Configuration -----------------------
188 * Disable, Configure and Enable I2Cx device own address 1 with parameters :
189 * - OwnAddress1: I2C_OAR1_OA1[9:0] bits
190 * - OwnAddrSize: I2C_OAR1_OA1MODE bit
192 LL_I2C_DisableOwnAddress1(I2Cx
);
193 LL_I2C_SetOwnAddress1(I2Cx
, I2C_InitStruct
->OwnAddress1
, I2C_InitStruct
->OwnAddrSize
);
195 /* OwnAdress1 == 0 is reserved for General Call address */
196 if (I2C_InitStruct
->OwnAddress1
!= 0U)
198 LL_I2C_EnableOwnAddress1(I2Cx
);
201 /*---------------------------- I2Cx MODE Configuration -----------------------
202 * Configure I2Cx peripheral mode with parameter :
203 * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits
205 LL_I2C_SetMode(I2Cx
, I2C_InitStruct
->PeripheralMode
);
207 /*---------------------------- I2Cx CR2 Configuration ------------------------
208 * Configure the ACKnowledge or Non ACKnowledge condition
209 * after the address receive match code or next received byte with parameter :
210 * - TypeAcknowledge: I2C_CR2_NACK bit
212 LL_I2C_AcknowledgeNextData(I2Cx
, I2C_InitStruct
->TypeAcknowledge
);
218 * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
219 * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
222 void LL_I2C_StructInit(LL_I2C_InitTypeDef
*I2C_InitStruct
)
224 /* Set I2C_InitStruct fields to default values */
225 I2C_InitStruct
->PeripheralMode
= LL_I2C_MODE_I2C
;
226 I2C_InitStruct
->Timing
= 0U;
227 I2C_InitStruct
->AnalogFilter
= LL_I2C_ANALOGFILTER_ENABLE
;
228 I2C_InitStruct
->DigitalFilter
= 0U;
229 I2C_InitStruct
->OwnAddress1
= 0U;
230 I2C_InitStruct
->TypeAcknowledge
= LL_I2C_NACK
;
231 I2C_InitStruct
->OwnAddrSize
= LL_I2C_OWNADDRESS1_7BIT
;
246 #endif /* I2C1 || I2C2 || I2C3 || I2C4 || I2C5 */
252 #endif /* USE_FULL_LL_DRIVER */