1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_ll_i2c.c
4   * @author  MCD Application Team
5   * @brief   I2C LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2016 STMicroelectronics.
10   * All rights reserved.
11   *
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.
15   *
16   ******************************************************************************
17   */
18 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32f2xx_ll_i2c.h"
22 #include "stm32f2xx_ll_bus.h"
23 #include "stm32f2xx_ll_rcc.h"
24 #ifdef  USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif
29 
30 /** @addtogroup STM32F2xx_LL_Driver
31   * @{
32   */
33 
34 #if defined (I2C1) || defined (I2C2) || defined (I2C3)
35 
36 /** @defgroup I2C_LL I2C
37   * @{
38   */
39 
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
44 /** @addtogroup I2C_LL_Private_Macros
45   * @{
46   */
47 
48 #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__)    (((__VALUE__) == LL_I2C_MODE_I2C)          || \
49                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST)   || \
50                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
51                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
52 
53 #define IS_LL_I2C_CLOCK_SPEED(__VALUE__)           (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
54 
55 #define IS_LL_I2C_DUTY_CYCLE(__VALUE__)            (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
56                                                  ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
57 
58 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__)       ((__VALUE__) <= 0x000003FFU)
59 
60 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__)   (((__VALUE__) == LL_I2C_ACK) || \
61                                                  ((__VALUE__) == LL_I2C_NACK))
62 
63 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__)       (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
64                                                  ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
65 /**
66   * @}
67   */
68 
69 /* Private function prototypes -----------------------------------------------*/
70 
71 /* Exported functions --------------------------------------------------------*/
72 /** @addtogroup I2C_LL_Exported_Functions
73   * @{
74   */
75 
76 /** @addtogroup I2C_LL_EF_Init
77   * @{
78   */
79 
80 /**
81   * @brief  De-initialize the I2C registers to their default reset values.
82   * @param  I2Cx I2C Instance.
83   * @retval An ErrorStatus enumeration value:
84   *          - SUCCESS  I2C registers are de-initialized
85   *          - ERROR  I2C registers are not de-initialized
86   */
LL_I2C_DeInit(I2C_TypeDef * I2Cx)87 uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
88 {
89   ErrorStatus status = SUCCESS;
90 
91   /* Check the I2C Instance I2Cx */
92   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
93 
94   if (I2Cx == I2C1)
95   {
96     /* Force reset of I2C clock */
97     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
98 
99     /* Release reset of I2C clock */
100     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
101   }
102   else if (I2Cx == I2C2)
103   {
104     /* Force reset of I2C clock */
105     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
106 
107     /* Release reset of I2C clock */
108     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
109 
110   }
111   else if (I2Cx == I2C3)
112   {
113     /* Force reset of I2C clock */
114     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
115 
116     /* Release reset of I2C clock */
117     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
118   }
119   else
120   {
121     status = ERROR;
122   }
123 
124   return status;
125 }
126 
127 /**
128   * @brief  Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
129   * @param  I2Cx I2C Instance.
130   * @param  I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
131   * @retval An ErrorStatus enumeration value:
132   *          - SUCCESS  I2C registers are initialized
133   *          - ERROR  Not applicable
134   */
LL_I2C_Init(I2C_TypeDef * I2Cx,LL_I2C_InitTypeDef * I2C_InitStruct)135 uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
136 {
137   LL_RCC_ClocksTypeDef rcc_clocks;
138 
139   /* Check the I2C Instance I2Cx */
140   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
141 
142   /* Check the I2C parameters from I2C_InitStruct */
143   assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
144   assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
145   assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
146   assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
147   assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
148   assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
149 
150   /* Disable the selected I2Cx Peripheral */
151   LL_I2C_Disable(I2Cx);
152 
153   /* Retrieve Clock frequencies */
154   LL_RCC_GetSystemClocksFreq(&rcc_clocks);
155 
156   /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
157    * Configure the SCL speed :
158    * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
159    *           and I2C_CCR_CCR[11:0] bits
160    * - DutyCycle: I2C_CCR_DUTY[7:0] bits
161    */
162   LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
163 
164   /*---------------------------- I2Cx OAR1 Configuration -----------------------
165    * Disable, Configure and Enable I2Cx device own address 1 with parameters :
166    * - OwnAddress1:  I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
167    * - OwnAddrSize:  I2C_OAR1_ADDMODE bit
168    */
169   LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
170 
171   /*---------------------------- I2Cx MODE Configuration -----------------------
172   * Configure I2Cx peripheral mode with parameter :
173    * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
174    */
175   LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
176 
177   /* Enable the selected I2Cx Peripheral */
178   LL_I2C_Enable(I2Cx);
179 
180   /*---------------------------- I2Cx CR2 Configuration ------------------------
181    * Configure the ACKnowledge or Non ACKnowledge condition
182    * after the address receive match code or next received byte with parameter :
183    * - TypeAcknowledge: I2C_CR2_NACK bit
184    */
185   LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
186 
187   return SUCCESS;
188 }
189 
190 /**
191   * @brief  Set each @ref LL_I2C_InitTypeDef field to default value.
192   * @param  I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
193   * @retval None
194   */
LL_I2C_StructInit(LL_I2C_InitTypeDef * I2C_InitStruct)195 void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
196 {
197   /* Set I2C_InitStruct fields to default values */
198   I2C_InitStruct->PeripheralMode  = LL_I2C_MODE_I2C;
199   I2C_InitStruct->ClockSpeed      = 5000U;
200   I2C_InitStruct->DutyCycle       = LL_I2C_DUTYCYCLE_2;
201   I2C_InitStruct->OwnAddress1     = 0U;
202   I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
203   I2C_InitStruct->OwnAddrSize     = LL_I2C_OWNADDRESS1_7BIT;
204 }
205 
206 /**
207   * @}
208   */
209 
210 /**
211   * @}
212   */
213 
214 /**
215   * @}
216   */
217 
218 #endif /* I2C1 || I2C2 || I2C3 */
219 
220 /**
221   * @}
222   */
223 
224 #endif /* USE_FULL_LL_DRIVER */
225 
226