1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_ll_fmpi2c.c
4   * @author  MCD Application Team
5   * @brief   FMPI2C 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 #if defined(FMPI2C_CR1_PE)
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32f4xx_ll_fmpi2c.h"
23 #include "stm32f4xx_ll_bus.h"
24 #ifdef  USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif /* USE_FULL_ASSERT */
29 
30 /** @addtogroup STM32F4xx_LL_Driver
31   * @{
32   */
33 
34 #if defined (FMPI2C1)
35 
36 /** @defgroup FMPI2C_LL FMPI2C
37   * @{
38   */
39 
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
44 /** @addtogroup FMPI2C_LL_Private_Macros
45   * @{
46   */
47 
48 #define IS_LL_FMPI2C_PERIPHERAL_MODE(__VALUE__)    (((__VALUE__) == LL_FMPI2C_MODE_I2C)          || \
49                                                  ((__VALUE__) == LL_FMPI2C_MODE_SMBUS_HOST)   || \
50                                                  ((__VALUE__) == LL_FMPI2C_MODE_SMBUS_DEVICE) || \
51                                                  ((__VALUE__) == LL_FMPI2C_MODE_SMBUS_DEVICE_ARP))
52 
53 #define IS_LL_FMPI2C_ANALOG_FILTER(__VALUE__)      (((__VALUE__) == LL_FMPI2C_ANALOGFILTER_ENABLE) || \
54                                                  ((__VALUE__) == LL_FMPI2C_ANALOGFILTER_DISABLE))
55 
56 #define IS_LL_FMPI2C_DIGITAL_FILTER(__VALUE__)     ((__VALUE__) <= 0x0000000FU)
57 
58 #define IS_LL_FMPI2C_OWN_ADDRESS1(__VALUE__)       ((__VALUE__) <= 0x000003FFU)
59 
60 #define IS_LL_FMPI2C_TYPE_ACKNOWLEDGE(__VALUE__)   (((__VALUE__) == LL_FMPI2C_ACK) || \
61                                                  ((__VALUE__) == LL_FMPI2C_NACK))
62 
63 #define IS_LL_FMPI2C_OWN_ADDRSIZE(__VALUE__)       (((__VALUE__) == LL_FMPI2C_OWNADDRESS1_7BIT) || \
64                                                  ((__VALUE__) == LL_FMPI2C_OWNADDRESS1_10BIT))
65 /**
66   * @}
67   */
68 
69 /* Private function prototypes -----------------------------------------------*/
70 
71 /* Exported functions --------------------------------------------------------*/
72 /** @addtogroup FMPI2C_LL_Exported_Functions
73   * @{
74   */
75 
76 /** @addtogroup FMPI2C_LL_EF_Init
77   * @{
78   */
79 
80 /**
81   * @brief  De-initialize the FMPI2C registers to their default reset values.
82   * @param  FMPI2Cx FMPI2C Instance.
83   * @retval An ErrorStatus enumeration value:
84   *          - SUCCESS: FMPI2C registers are de-initialized
85   *          - ERROR: FMPI2C registers are not de-initialized
86   */
LL_FMPI2C_DeInit(FMPI2C_TypeDef * FMPI2Cx)87 ErrorStatus LL_FMPI2C_DeInit(FMPI2C_TypeDef *FMPI2Cx)
88 {
89   ErrorStatus status = SUCCESS;
90 
91   /* Check the FMPI2C Instance FMPI2Cx */
92   assert_param(IS_FMPI2C_ALL_INSTANCE(FMPI2Cx));
93 
94   if (FMPI2Cx == FMPI2C1)
95   {
96     /* Force reset of FMPI2C clock */
97     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_FMPI2C1);
98 
99     /* Release reset of FMPI2C clock */
100     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_FMPI2C1);
101   }
102   else
103   {
104     status = ERROR;
105   }
106 
107   return status;
108 }
109 
110 /**
111   * @brief  Initialize the FMPI2C registers according to the specified parameters in FMPI2C_InitStruct.
112   * @param  FMPI2Cx FMPI2C Instance.
113   * @param  FMPI2C_InitStruct pointer to a @ref LL_FMPI2C_InitTypeDef structure.
114   * @retval An ErrorStatus enumeration value:
115   *          - SUCCESS: FMPI2C registers are initialized
116   *          - ERROR: Not applicable
117   */
LL_FMPI2C_Init(FMPI2C_TypeDef * FMPI2Cx,LL_FMPI2C_InitTypeDef * FMPI2C_InitStruct)118 ErrorStatus LL_FMPI2C_Init(FMPI2C_TypeDef *FMPI2Cx, LL_FMPI2C_InitTypeDef *FMPI2C_InitStruct)
119 {
120   /* Check the FMPI2C Instance FMPI2Cx */
121   assert_param(IS_FMPI2C_ALL_INSTANCE(FMPI2Cx));
122 
123   /* Check the FMPI2C parameters from FMPI2C_InitStruct */
124   assert_param(IS_LL_FMPI2C_PERIPHERAL_MODE(FMPI2C_InitStruct->PeripheralMode));
125   assert_param(IS_LL_FMPI2C_ANALOG_FILTER(FMPI2C_InitStruct->AnalogFilter));
126   assert_param(IS_LL_FMPI2C_DIGITAL_FILTER(FMPI2C_InitStruct->DigitalFilter));
127   assert_param(IS_LL_FMPI2C_OWN_ADDRESS1(FMPI2C_InitStruct->OwnAddress1));
128   assert_param(IS_LL_FMPI2C_TYPE_ACKNOWLEDGE(FMPI2C_InitStruct->TypeAcknowledge));
129   assert_param(IS_LL_FMPI2C_OWN_ADDRSIZE(FMPI2C_InitStruct->OwnAddrSize));
130 
131   /* Disable the selected FMPI2Cx Peripheral */
132   LL_FMPI2C_Disable(FMPI2Cx);
133 
134   /*---------------------------- FMPI2Cx CR1 Configuration ------------------------
135    * Configure the analog and digital noise filters with parameters :
136    * - AnalogFilter: FMPI2C_CR1_ANFOFF bit
137    * - DigitalFilter: FMPI2C_CR1_DNF[3:0] bits
138    */
139   LL_FMPI2C_ConfigFilters(FMPI2Cx, FMPI2C_InitStruct->AnalogFilter, FMPI2C_InitStruct->DigitalFilter);
140 
141   /*---------------------------- FMPI2Cx TIMINGR Configuration --------------------
142    * Configure the SDA setup, hold time and the SCL high, low period with parameter :
143    * - Timing: FMPI2C_TIMINGR_PRESC[3:0], FMPI2C_TIMINGR_SCLDEL[3:0], FMPI2C_TIMINGR_SDADEL[3:0],
144    *           FMPI2C_TIMINGR_SCLH[7:0] and FMPI2C_TIMINGR_SCLL[7:0] bits
145    */
146   LL_FMPI2C_SetTiming(FMPI2Cx, FMPI2C_InitStruct->Timing);
147 
148   /* Enable the selected FMPI2Cx Peripheral */
149   LL_FMPI2C_Enable(FMPI2Cx);
150 
151   /*---------------------------- FMPI2Cx OAR1 Configuration -----------------------
152    * Disable, Configure and Enable FMPI2Cx device own address 1 with parameters :
153    * - OwnAddress1:  FMPI2C_OAR1_OA1[9:0] bits
154    * - OwnAddrSize:  FMPI2C_OAR1_OA1MODE bit
155    */
156   LL_FMPI2C_DisableOwnAddress1(FMPI2Cx);
157   LL_FMPI2C_SetOwnAddress1(FMPI2Cx, FMPI2C_InitStruct->OwnAddress1, FMPI2C_InitStruct->OwnAddrSize);
158 
159   /* OwnAdress1 == 0 is reserved for General Call address */
160   if (FMPI2C_InitStruct->OwnAddress1 != 0U)
161   {
162     LL_FMPI2C_EnableOwnAddress1(FMPI2Cx);
163   }
164 
165   /*---------------------------- FMPI2Cx MODE Configuration -----------------------
166   * Configure FMPI2Cx peripheral mode with parameter :
167    * - PeripheralMode: FMPI2C_CR1_SMBDEN and FMPI2C_CR1_SMBHEN bits
168    */
169   LL_FMPI2C_SetMode(FMPI2Cx, FMPI2C_InitStruct->PeripheralMode);
170 
171   /*---------------------------- FMPI2Cx CR2 Configuration ------------------------
172    * Configure the ACKnowledge or Non ACKnowledge condition
173    * after the address receive match code or next received byte with parameter :
174    * - TypeAcknowledge: FMPI2C_CR2_NACK bit
175    */
176   LL_FMPI2C_AcknowledgeNextData(FMPI2Cx, FMPI2C_InitStruct->TypeAcknowledge);
177 
178   return SUCCESS;
179 }
180 
181 /**
182   * @brief  Set each @ref LL_FMPI2C_InitTypeDef field to default value.
183   * @param  FMPI2C_InitStruct Pointer to a @ref LL_FMPI2C_InitTypeDef structure.
184   * @retval None
185   */
LL_FMPI2C_StructInit(LL_FMPI2C_InitTypeDef * FMPI2C_InitStruct)186 void LL_FMPI2C_StructInit(LL_FMPI2C_InitTypeDef *FMPI2C_InitStruct)
187 {
188   /* Set FMPI2C_InitStruct fields to default values */
189   FMPI2C_InitStruct->PeripheralMode  = LL_FMPI2C_MODE_I2C;
190   FMPI2C_InitStruct->Timing          = 0U;
191   FMPI2C_InitStruct->AnalogFilter    = LL_FMPI2C_ANALOGFILTER_ENABLE;
192   FMPI2C_InitStruct->DigitalFilter   = 0U;
193   FMPI2C_InitStruct->OwnAddress1     = 0U;
194   FMPI2C_InitStruct->TypeAcknowledge = LL_FMPI2C_NACK;
195   FMPI2C_InitStruct->OwnAddrSize     = LL_FMPI2C_OWNADDRESS1_7BIT;
196 }
197 
198 /**
199   * @}
200   */
201 
202 /**
203   * @}
204   */
205 
206 /**
207   * @}
208   */
209 
210 #endif /* FMPI2C1 */
211 
212 /**
213   * @}
214   */
215 
216 #endif /* FMPI2C_CR1_PE */
217 #endif /* USE_FULL_LL_DRIVER */
218