Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /** |
| 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. |
||
| 10 | * All rights reserved.</center></h2> |
||
| 11 | * |
||
| 12 | * This software component is licensed by ST under BSD 3-Clause license, |
||
| 13 | * the "License"; You may not use this file except in compliance with the |
||
| 14 | * License. You may obtain a copy of the License at: |
||
| 15 | * opensource.org/licenses/BSD-3-Clause |
||
| 16 | * |
||
| 17 | ****************************************************************************** |
||
| 18 | */ |
||
| 19 | #if defined(USE_FULL_LL_DRIVER) |
||
| 20 | |||
| 21 | /* Includes ------------------------------------------------------------------*/ |
||
| 22 | #include "stm32f0xx_ll_i2c.h" |
||
| 23 | #include "stm32f0xx_ll_bus.h" |
||
| 24 | #ifdef USE_FULL_ASSERT |
||
| 25 | #include "stm32_assert.h" |
||
| 26 | #else |
||
| 27 | #define assert_param(expr) ((void)0U) |
||
| 28 | #endif |
||
| 29 | |||
| 30 | /** @addtogroup STM32F0xx_LL_Driver |
||
| 31 | * @{ |
||
| 32 | */ |
||
| 33 | |||
| 34 | #if defined (I2C1) || defined (I2C2) |
||
| 35 | |||
| 36 | /** @defgroup I2C_LL I2C |
||
| 37 | * @{ |
||
| 38 | */ |
||
| 39 | |||
| 40 | /* Private types -------------------------------------------------------------*/ |
||
| 41 | /* Private variables ---------------------------------------------------------*/ |
||
| 42 | /* Private constants ---------------------------------------------------------*/ |
||
| 43 | /* Private macros ------------------------------------------------------------*/ |
||
| 44 | /** @addtogroup I2C_LL_Private_Macros |
||
| 45 | * @{ |
||
| 46 | */ |
||
| 47 | |||
| 48 | #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_DEVICE) || \ |
||
| 51 | ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP)) |
||
| 52 | |||
| 53 | #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \ |
||
| 54 | ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE)) |
||
| 55 | |||
| 56 | #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU) |
||
| 57 | |||
| 58 | #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU) |
||
| 59 | |||
| 60 | #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \ |
||
| 61 | ((__VALUE__) == LL_I2C_NACK)) |
||
| 62 | |||
| 63 | #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \ |
||
| 64 | ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT)) |
||
| 65 | /** |
||
| 66 | * @} |
||
| 67 | */ |
||
| 68 | |||
| 69 | /* Private function prototypes -----------------------------------------------*/ |
||
| 70 | |||
| 71 | /* Exported functions --------------------------------------------------------*/ |
||
| 72 | /** @addtogroup I2C_LL_Exported_Functions |
||
| 73 | * @{ |
||
| 74 | */ |
||
| 75 | |||
| 76 | /** @addtogroup I2C_LL_EF_Init |
||
| 77 | * @{ |
||
| 78 | */ |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @brief De-initialize the I2C registers to their default reset values. |
||
| 82 | * @param I2Cx I2C Instance. |
||
| 83 | * @retval An ErrorStatus enumeration value: |
||
| 84 | * - SUCCESS: I2C registers are de-initialized |
||
| 85 | * - ERROR: I2C registers are not de-initialized |
||
| 86 | */ |
||
| 87 | ErrorStatus LL_I2C_DeInit(I2C_TypeDef *I2Cx) |
||
| 88 | { |
||
| 89 | ErrorStatus status = SUCCESS; |
||
| 90 | |||
| 91 | /* Check the I2C Instance I2Cx */ |
||
| 92 | assert_param(IS_I2C_ALL_INSTANCE(I2Cx)); |
||
| 93 | |||
| 94 | if (I2Cx == I2C1) |
||
| 95 | { |
||
| 96 | /* Force reset of I2C clock */ |
||
| 97 | LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1); |
||
| 98 | |||
| 99 | /* Release reset of I2C clock */ |
||
| 100 | LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1); |
||
| 101 | } |
||
| 102 | #if defined(I2C2) |
||
| 103 | else if (I2Cx == I2C2) |
||
| 104 | { |
||
| 105 | /* Force reset of I2C clock */ |
||
| 106 | LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2); |
||
| 107 | |||
| 108 | /* Release reset of I2C clock */ |
||
| 109 | LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2); |
||
| 110 | |||
| 111 | } |
||
| 112 | #endif |
||
| 113 | else |
||
| 114 | { |
||
| 115 | status = ERROR; |
||
| 116 | } |
||
| 117 | |||
| 118 | return status; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct. |
||
| 123 | * @param I2Cx I2C Instance. |
||
| 124 | * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure. |
||
| 125 | * @retval An ErrorStatus enumeration value: |
||
| 126 | * - SUCCESS: I2C registers are initialized |
||
| 127 | * - ERROR: Not applicable |
||
| 128 | */ |
||
| 129 | ErrorStatus LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct) |
||
| 130 | { |
||
| 131 | /* Check the I2C Instance I2Cx */ |
||
| 132 | assert_param(IS_I2C_ALL_INSTANCE(I2Cx)); |
||
| 133 | |||
| 134 | /* Check the I2C parameters from I2C_InitStruct */ |
||
| 135 | assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode)); |
||
| 136 | assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter)); |
||
| 137 | assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter)); |
||
| 138 | assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1)); |
||
| 139 | assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge)); |
||
| 140 | assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize)); |
||
| 141 | |||
| 142 | /* Disable the selected I2Cx Peripheral */ |
||
| 143 | LL_I2C_Disable(I2Cx); |
||
| 144 | |||
| 145 | /*---------------------------- I2Cx CR1 Configuration ------------------------ |
||
| 146 | * Configure the analog and digital noise filters with parameters : |
||
| 147 | * - AnalogFilter: I2C_CR1_ANFOFF bit |
||
| 148 | * - DigitalFilter: I2C_CR1_DNF[3:0] bits |
||
| 149 | */ |
||
| 150 | LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter); |
||
| 151 | |||
| 152 | /*---------------------------- I2Cx TIMINGR Configuration -------------------- |
||
| 153 | * Configure the SDA setup, hold time and the SCL high, low period with parameter : |
||
| 154 | * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0], |
||
| 155 | * I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits |
||
| 156 | */ |
||
| 157 | LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing); |
||
| 158 | |||
| 159 | /* Enable the selected I2Cx Peripheral */ |
||
| 160 | LL_I2C_Enable(I2Cx); |
||
| 161 | |||
| 162 | /*---------------------------- I2Cx OAR1 Configuration ----------------------- |
||
| 163 | * Disable, Configure and Enable I2Cx device own address 1 with parameters : |
||
| 164 | * - OwnAddress1: I2C_OAR1_OA1[9:0] bits |
||
| 165 | * - OwnAddrSize: I2C_OAR1_OA1MODE bit |
||
| 166 | */ |
||
| 167 | LL_I2C_DisableOwnAddress1(I2Cx); |
||
| 168 | LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize); |
||
| 169 | |||
| 170 | /* OwnAdress1 == 0 is reserved for General Call address */ |
||
| 171 | if (I2C_InitStruct->OwnAddress1 != 0U) |
||
| 172 | { |
||
| 173 | LL_I2C_EnableOwnAddress1(I2Cx); |
||
| 174 | } |
||
| 175 | |||
| 176 | /*---------------------------- I2Cx MODE Configuration ----------------------- |
||
| 177 | * Configure I2Cx peripheral mode with parameter : |
||
| 178 | * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits |
||
| 179 | */ |
||
| 180 | LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode); |
||
| 181 | |||
| 182 | /*---------------------------- I2Cx CR2 Configuration ------------------------ |
||
| 183 | * Configure the ACKnowledge or Non ACKnowledge condition |
||
| 184 | * after the address receive match code or next received byte with parameter : |
||
| 185 | * - TypeAcknowledge: I2C_CR2_NACK bit |
||
| 186 | */ |
||
| 187 | LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge); |
||
| 188 | |||
| 189 | return SUCCESS; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @brief Set each @ref LL_I2C_InitTypeDef field to default value. |
||
| 194 | * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure. |
||
| 195 | * @retval None |
||
| 196 | */ |
||
| 197 | void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct) |
||
| 198 | { |
||
| 199 | /* Set I2C_InitStruct fields to default values */ |
||
| 200 | I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C; |
||
| 201 | I2C_InitStruct->Timing = 0U; |
||
| 202 | I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE; |
||
| 203 | I2C_InitStruct->DigitalFilter = 0U; |
||
| 204 | I2C_InitStruct->OwnAddress1 = 0U; |
||
| 205 | I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK; |
||
| 206 | I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @} |
||
| 211 | */ |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @} |
||
| 215 | */ |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @} |
||
| 219 | */ |
||
| 220 | |||
| 221 | #endif /* I2C1 || I2C2 */ |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @} |
||
| 225 | */ |
||
| 226 | |||
| 227 | #endif /* USE_FULL_LL_DRIVER */ |
||
| 228 | |||
| 229 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |