mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
186:707f6e361f3e
mbed library release version 165

Who changed what in which revision?

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