TUKS MCU Introductory course / TUKS-COURSE-THERMOMETER

Fork of TUKS-COURSE-TIMER by TUKS MCU Introductory course

Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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