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