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