Amit Gandhi / sensen_copy_2

Fork of mbed-dev by mbed official

Committer:
Kojto
Date:
Tue Feb 14 14:44:10 2017 +0000
Revision:
158:b23ee177fd68
This updates the lib to the mbed lib v136

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