Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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