提交 | 用户 | age
|
483170
|
1 |
/** |
Q |
2 |
****************************************************************************** |
|
3 |
* @file stm32f0xx_ll_i2c.c |
|
4 |
* @author MCD Application Team |
|
5 |
* @brief I2C LL module driver. |
|
6 |
****************************************************************************** |
|
7 |
* @attention |
|
8 |
* |
|
9 |
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
|
10 |
* |
|
11 |
* Redistribution and use in source and binary forms, with or without modification, |
|
12 |
* are permitted provided that the following conditions are met: |
|
13 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
14 |
* this list of conditions and the following disclaimer. |
|
15 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
16 |
* this list of conditions and the following disclaimer in the documentation |
|
17 |
* and/or other materials provided with the distribution. |
|
18 |
* 3. Neither the name of STMicroelectronics nor the names of its contributors |
|
19 |
* may be used to endorse or promote products derived from this software |
|
20 |
* without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
23 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
24 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
25 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
26 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
27 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
28 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
29 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
30 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
31 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
* |
|
33 |
****************************************************************************** |
|
34 |
*/ |
|
35 |
#if defined(USE_FULL_LL_DRIVER) |
|
36 |
|
|
37 |
/* Includes ------------------------------------------------------------------*/ |
|
38 |
#include "stm32f0xx_ll_i2c.h" |
|
39 |
#include "stm32f0xx_ll_bus.h" |
|
40 |
#ifdef USE_FULL_ASSERT |
|
41 |
#include "stm32_assert.h" |
|
42 |
#else |
|
43 |
#define assert_param(expr) ((void)0U) |
|
44 |
#endif |
|
45 |
|
|
46 |
/** @addtogroup STM32F0xx_LL_Driver |
|
47 |
* @{ |
|
48 |
*/ |
|
49 |
|
|
50 |
#if defined (I2C1) || defined (I2C2) |
|
51 |
|
|
52 |
/** @defgroup I2C_LL I2C |
|
53 |
* @{ |
|
54 |
*/ |
|
55 |
|
|
56 |
/* Private types -------------------------------------------------------------*/ |
|
57 |
/* Private variables ---------------------------------------------------------*/ |
|
58 |
/* Private constants ---------------------------------------------------------*/ |
|
59 |
/* Private macros ------------------------------------------------------------*/ |
|
60 |
/** @addtogroup I2C_LL_Private_Macros |
|
61 |
* @{ |
|
62 |
*/ |
|
63 |
|
|
64 |
#define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \ |
|
65 |
((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \ |
|
66 |
((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \ |
|
67 |
((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP)) |
|
68 |
|
|
69 |
#define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \ |
|
70 |
((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE)) |
|
71 |
|
|
72 |
#define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU) |
|
73 |
|
|
74 |
#define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU) |
|
75 |
|
|
76 |
#define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \ |
|
77 |
((__VALUE__) == LL_I2C_NACK)) |
|
78 |
|
|
79 |
#define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \ |
|
80 |
((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT)) |
|
81 |
/** |
|
82 |
* @} |
|
83 |
*/ |
|
84 |
|
|
85 |
/* Private function prototypes -----------------------------------------------*/ |
|
86 |
|
|
87 |
/* Exported functions --------------------------------------------------------*/ |
|
88 |
/** @addtogroup I2C_LL_Exported_Functions |
|
89 |
* @{ |
|
90 |
*/ |
|
91 |
|
|
92 |
/** @addtogroup I2C_LL_EF_Init |
|
93 |
* @{ |
|
94 |
*/ |
|
95 |
|
|
96 |
/** |
|
97 |
* @brief De-initialize the I2C registers to their default reset values. |
|
98 |
* @param I2Cx I2C Instance. |
|
99 |
* @retval An ErrorStatus enumeration value: |
|
100 |
* - SUCCESS: I2C registers are de-initialized |
|
101 |
* - ERROR: I2C registers are not de-initialized |
|
102 |
*/ |
|
103 |
uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx) |
|
104 |
{ |
|
105 |
ErrorStatus status = SUCCESS; |
|
106 |
|
|
107 |
/* Check the I2C Instance I2Cx */ |
|
108 |
assert_param(IS_I2C_ALL_INSTANCE(I2Cx)); |
|
109 |
|
|
110 |
if (I2Cx == I2C1) |
|
111 |
{ |
|
112 |
/* Force reset of I2C clock */ |
|
113 |
LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1); |
|
114 |
|
|
115 |
/* Release reset of I2C clock */ |
|
116 |
LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1); |
|
117 |
} |
|
118 |
#if defined(I2C2) |
|
119 |
else if (I2Cx == I2C2) |
|
120 |
{ |
|
121 |
/* Force reset of I2C clock */ |
|
122 |
LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2); |
|
123 |
|
|
124 |
/* Release reset of I2C clock */ |
|
125 |
LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2); |
|
126 |
|
|
127 |
} |
|
128 |
#endif |
|
129 |
else |
|
130 |
{ |
|
131 |
status = ERROR; |
|
132 |
} |
|
133 |
|
|
134 |
return status; |
|
135 |
} |
|
136 |
|
|
137 |
/** |
|
138 |
* @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct. |
|
139 |
* @param I2Cx I2C Instance. |
|
140 |
* @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure. |
|
141 |
* @retval An ErrorStatus enumeration value: |
|
142 |
* - SUCCESS: I2C registers are initialized |
|
143 |
* - ERROR: Not applicable |
|
144 |
*/ |
|
145 |
uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct) |
|
146 |
{ |
|
147 |
/* Check the I2C Instance I2Cx */ |
|
148 |
assert_param(IS_I2C_ALL_INSTANCE(I2Cx)); |
|
149 |
|
|
150 |
/* Check the I2C parameters from I2C_InitStruct */ |
|
151 |
assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode)); |
|
152 |
assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter)); |
|
153 |
assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter)); |
|
154 |
assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1)); |
|
155 |
assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge)); |
|
156 |
assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize)); |
|
157 |
|
|
158 |
/* Disable the selected I2Cx Peripheral */ |
|
159 |
LL_I2C_Disable(I2Cx); |
|
160 |
|
|
161 |
/*---------------------------- I2Cx CR1 Configuration ------------------------ |
|
162 |
* Configure the analog and digital noise filters with parameters : |
|
163 |
* - AnalogFilter: I2C_CR1_ANFOFF bit |
|
164 |
* - DigitalFilter: I2C_CR1_DNF[3:0] bits |
|
165 |
*/ |
|
166 |
LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter); |
|
167 |
|
|
168 |
/*---------------------------- I2Cx TIMINGR Configuration -------------------- |
|
169 |
* Configure the SDA setup, hold time and the SCL high, low period with parameter : |
|
170 |
* - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0], |
|
171 |
* I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits |
|
172 |
*/ |
|
173 |
LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing); |
|
174 |
|
|
175 |
/* Enable the selected I2Cx Peripheral */ |
|
176 |
LL_I2C_Enable(I2Cx); |
|
177 |
|
|
178 |
/*---------------------------- I2Cx OAR1 Configuration ----------------------- |
|
179 |
* Disable, Configure and Enable I2Cx device own address 1 with parameters : |
|
180 |
* - OwnAddress1: I2C_OAR1_OA1[9:0] bits |
|
181 |
* - OwnAddrSize: I2C_OAR1_OA1MODE bit |
|
182 |
*/ |
|
183 |
LL_I2C_DisableOwnAddress1(I2Cx); |
|
184 |
LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize); |
|
185 |
|
|
186 |
/* OwnAdress1 == 0 is reserved for General Call address */ |
|
187 |
if (I2C_InitStruct->OwnAddress1 != 0U) |
|
188 |
{ |
|
189 |
LL_I2C_EnableOwnAddress1(I2Cx); |
|
190 |
} |
|
191 |
|
|
192 |
/*---------------------------- I2Cx MODE Configuration ----------------------- |
|
193 |
* Configure I2Cx peripheral mode with parameter : |
|
194 |
* - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits |
|
195 |
*/ |
|
196 |
LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode); |
|
197 |
|
|
198 |
/*---------------------------- I2Cx CR2 Configuration ------------------------ |
|
199 |
* Configure the ACKnowledge or Non ACKnowledge condition |
|
200 |
* after the address receive match code or next received byte with parameter : |
|
201 |
* - TypeAcknowledge: I2C_CR2_NACK bit |
|
202 |
*/ |
|
203 |
LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge); |
|
204 |
|
|
205 |
return SUCCESS; |
|
206 |
} |
|
207 |
|
|
208 |
/** |
|
209 |
* @brief Set each @ref LL_I2C_InitTypeDef field to default value. |
|
210 |
* @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure. |
|
211 |
* @retval None |
|
212 |
*/ |
|
213 |
void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct) |
|
214 |
{ |
|
215 |
/* Set I2C_InitStruct fields to default values */ |
|
216 |
I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C; |
|
217 |
I2C_InitStruct->Timing = 0U; |
|
218 |
I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE; |
|
219 |
I2C_InitStruct->DigitalFilter = 0U; |
|
220 |
I2C_InitStruct->OwnAddress1 = 0U; |
|
221 |
I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK; |
|
222 |
I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; |
|
223 |
} |
|
224 |
|
|
225 |
/** |
|
226 |
* @} |
|
227 |
*/ |
|
228 |
|
|
229 |
/** |
|
230 |
* @} |
|
231 |
*/ |
|
232 |
|
|
233 |
/** |
|
234 |
* @} |
|
235 |
*/ |
|
236 |
|
|
237 |
#endif /* I2C1 || I2C2 */ |
|
238 |
|
|
239 |
/** |
|
240 |
* @} |
|
241 |
*/ |
|
242 |
|
|
243 |
#endif /* USE_FULL_LL_DRIVER */ |
|
244 |
|
|
245 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |