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