mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
126:549ba18ddd81
test with CLOCK_SETUP = 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 126:549ba18ddd81 1 /**
mbed_official 126:549ba18ddd81 2 ******************************************************************************
mbed_official 126:549ba18ddd81 3 * @file stm32f10x_i2c.c
mbed_official 126:549ba18ddd81 4 * @author MCD Application Team
mbed_official 126:549ba18ddd81 5 * @version V3.6.1
mbed_official 126:549ba18ddd81 6 * @date 05-March-2012
mbed_official 126:549ba18ddd81 7 * @brief This file provides all the I2C firmware functions.
mbed_official 126:549ba18ddd81 8 *******************************************************************************
mbed_official 126:549ba18ddd81 9 * Copyright (c) 2014, STMicroelectronics
mbed_official 126:549ba18ddd81 10 * All rights reserved.
mbed_official 126:549ba18ddd81 11 *
mbed_official 126:549ba18ddd81 12 * Redistribution and use in source and binary forms, with or without
mbed_official 126:549ba18ddd81 13 * modification, are permitted provided that the following conditions are met:
mbed_official 126:549ba18ddd81 14 *
mbed_official 126:549ba18ddd81 15 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 126:549ba18ddd81 16 * this list of conditions and the following disclaimer.
mbed_official 126:549ba18ddd81 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 126:549ba18ddd81 18 * this list of conditions and the following disclaimer in the documentation
mbed_official 126:549ba18ddd81 19 * and/or other materials provided with the distribution.
mbed_official 126:549ba18ddd81 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 126:549ba18ddd81 21 * may be used to endorse or promote products derived from this software
mbed_official 126:549ba18ddd81 22 * without specific prior written permission.
mbed_official 126:549ba18ddd81 23 *
mbed_official 126:549ba18ddd81 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 126:549ba18ddd81 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 126:549ba18ddd81 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 126:549ba18ddd81 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 126:549ba18ddd81 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 126:549ba18ddd81 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 126:549ba18ddd81 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 126:549ba18ddd81 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 126:549ba18ddd81 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 126:549ba18ddd81 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 126:549ba18ddd81 34 *******************************************************************************
mbed_official 126:549ba18ddd81 35 */
mbed_official 126:549ba18ddd81 36
mbed_official 126:549ba18ddd81 37 /* Includes ------------------------------------------------------------------*/
mbed_official 126:549ba18ddd81 38 #include "stm32f10x_i2c.h"
mbed_official 126:549ba18ddd81 39 #include "stm32f10x_rcc.h"
mbed_official 126:549ba18ddd81 40
mbed_official 126:549ba18ddd81 41
mbed_official 126:549ba18ddd81 42 /** @addtogroup STM32F10x_StdPeriph_Driver
mbed_official 126:549ba18ddd81 43 * @{
mbed_official 126:549ba18ddd81 44 */
mbed_official 126:549ba18ddd81 45
mbed_official 126:549ba18ddd81 46 /** @defgroup I2C
mbed_official 126:549ba18ddd81 47 * @brief I2C driver modules
mbed_official 126:549ba18ddd81 48 * @{
mbed_official 126:549ba18ddd81 49 */
mbed_official 126:549ba18ddd81 50
mbed_official 126:549ba18ddd81 51 /** @defgroup I2C_Private_TypesDefinitions
mbed_official 126:549ba18ddd81 52 * @{
mbed_official 126:549ba18ddd81 53 */
mbed_official 126:549ba18ddd81 54
mbed_official 126:549ba18ddd81 55 /**
mbed_official 126:549ba18ddd81 56 * @}
mbed_official 126:549ba18ddd81 57 */
mbed_official 126:549ba18ddd81 58
mbed_official 126:549ba18ddd81 59 /** @defgroup I2C_Private_Defines
mbed_official 126:549ba18ddd81 60 * @{
mbed_official 126:549ba18ddd81 61 */
mbed_official 126:549ba18ddd81 62
mbed_official 126:549ba18ddd81 63 /* I2C SPE mask */
mbed_official 126:549ba18ddd81 64 #define CR1_PE_Set ((uint16_t)0x0001)
mbed_official 126:549ba18ddd81 65 #define CR1_PE_Reset ((uint16_t)0xFFFE)
mbed_official 126:549ba18ddd81 66
mbed_official 126:549ba18ddd81 67 /* I2C START mask */
mbed_official 126:549ba18ddd81 68 #define CR1_START_Set ((uint16_t)0x0100)
mbed_official 126:549ba18ddd81 69 #define CR1_START_Reset ((uint16_t)0xFEFF)
mbed_official 126:549ba18ddd81 70
mbed_official 126:549ba18ddd81 71 /* I2C STOP mask */
mbed_official 126:549ba18ddd81 72 #define CR1_STOP_Set ((uint16_t)0x0200)
mbed_official 126:549ba18ddd81 73 #define CR1_STOP_Reset ((uint16_t)0xFDFF)
mbed_official 126:549ba18ddd81 74
mbed_official 126:549ba18ddd81 75 /* I2C ACK mask */
mbed_official 126:549ba18ddd81 76 #define CR1_ACK_Set ((uint16_t)0x0400)
mbed_official 126:549ba18ddd81 77 #define CR1_ACK_Reset ((uint16_t)0xFBFF)
mbed_official 126:549ba18ddd81 78
mbed_official 126:549ba18ddd81 79 /* I2C ENGC mask */
mbed_official 126:549ba18ddd81 80 #define CR1_ENGC_Set ((uint16_t)0x0040)
mbed_official 126:549ba18ddd81 81 #define CR1_ENGC_Reset ((uint16_t)0xFFBF)
mbed_official 126:549ba18ddd81 82
mbed_official 126:549ba18ddd81 83 /* I2C SWRST mask */
mbed_official 126:549ba18ddd81 84 #define CR1_SWRST_Set ((uint16_t)0x8000)
mbed_official 126:549ba18ddd81 85 #define CR1_SWRST_Reset ((uint16_t)0x7FFF)
mbed_official 126:549ba18ddd81 86
mbed_official 126:549ba18ddd81 87 /* I2C PEC mask */
mbed_official 126:549ba18ddd81 88 #define CR1_PEC_Set ((uint16_t)0x1000)
mbed_official 126:549ba18ddd81 89 #define CR1_PEC_Reset ((uint16_t)0xEFFF)
mbed_official 126:549ba18ddd81 90
mbed_official 126:549ba18ddd81 91 /* I2C ENPEC mask */
mbed_official 126:549ba18ddd81 92 #define CR1_ENPEC_Set ((uint16_t)0x0020)
mbed_official 126:549ba18ddd81 93 #define CR1_ENPEC_Reset ((uint16_t)0xFFDF)
mbed_official 126:549ba18ddd81 94
mbed_official 126:549ba18ddd81 95 /* I2C ENARP mask */
mbed_official 126:549ba18ddd81 96 #define CR1_ENARP_Set ((uint16_t)0x0010)
mbed_official 126:549ba18ddd81 97 #define CR1_ENARP_Reset ((uint16_t)0xFFEF)
mbed_official 126:549ba18ddd81 98
mbed_official 126:549ba18ddd81 99 /* I2C NOSTRETCH mask */
mbed_official 126:549ba18ddd81 100 #define CR1_NOSTRETCH_Set ((uint16_t)0x0080)
mbed_official 126:549ba18ddd81 101 #define CR1_NOSTRETCH_Reset ((uint16_t)0xFF7F)
mbed_official 126:549ba18ddd81 102
mbed_official 126:549ba18ddd81 103 /* I2C registers Masks */
mbed_official 126:549ba18ddd81 104 #define CR1_CLEAR_Mask ((uint16_t)0xFBF5)
mbed_official 126:549ba18ddd81 105
mbed_official 126:549ba18ddd81 106 /* I2C DMAEN mask */
mbed_official 126:549ba18ddd81 107 #define CR2_DMAEN_Set ((uint16_t)0x0800)
mbed_official 126:549ba18ddd81 108 #define CR2_DMAEN_Reset ((uint16_t)0xF7FF)
mbed_official 126:549ba18ddd81 109
mbed_official 126:549ba18ddd81 110 /* I2C LAST mask */
mbed_official 126:549ba18ddd81 111 #define CR2_LAST_Set ((uint16_t)0x1000)
mbed_official 126:549ba18ddd81 112 #define CR2_LAST_Reset ((uint16_t)0xEFFF)
mbed_official 126:549ba18ddd81 113
mbed_official 126:549ba18ddd81 114 /* I2C FREQ mask */
mbed_official 126:549ba18ddd81 115 #define CR2_FREQ_Reset ((uint16_t)0xFFC0)
mbed_official 126:549ba18ddd81 116
mbed_official 126:549ba18ddd81 117 /* I2C ADD0 mask */
mbed_official 126:549ba18ddd81 118 #define OAR1_ADD0_Set ((uint16_t)0x0001)
mbed_official 126:549ba18ddd81 119 #define OAR1_ADD0_Reset ((uint16_t)0xFFFE)
mbed_official 126:549ba18ddd81 120
mbed_official 126:549ba18ddd81 121 /* I2C ENDUAL mask */
mbed_official 126:549ba18ddd81 122 #define OAR2_ENDUAL_Set ((uint16_t)0x0001)
mbed_official 126:549ba18ddd81 123 #define OAR2_ENDUAL_Reset ((uint16_t)0xFFFE)
mbed_official 126:549ba18ddd81 124
mbed_official 126:549ba18ddd81 125 /* I2C ADD2 mask */
mbed_official 126:549ba18ddd81 126 #define OAR2_ADD2_Reset ((uint16_t)0xFF01)
mbed_official 126:549ba18ddd81 127
mbed_official 126:549ba18ddd81 128 /* I2C F/S mask */
mbed_official 126:549ba18ddd81 129 #define CCR_FS_Set ((uint16_t)0x8000)
mbed_official 126:549ba18ddd81 130
mbed_official 126:549ba18ddd81 131 /* I2C CCR mask */
mbed_official 126:549ba18ddd81 132 #define CCR_CCR_Set ((uint16_t)0x0FFF)
mbed_official 126:549ba18ddd81 133
mbed_official 126:549ba18ddd81 134 /* I2C FLAG mask */
mbed_official 126:549ba18ddd81 135 #define FLAG_Mask ((uint32_t)0x00FFFFFF)
mbed_official 126:549ba18ddd81 136
mbed_official 126:549ba18ddd81 137 /* I2C Interrupt Enable mask */
mbed_official 126:549ba18ddd81 138 #define ITEN_Mask ((uint32_t)0x07000000)
mbed_official 126:549ba18ddd81 139
mbed_official 126:549ba18ddd81 140 /**
mbed_official 126:549ba18ddd81 141 * @}
mbed_official 126:549ba18ddd81 142 */
mbed_official 126:549ba18ddd81 143
mbed_official 126:549ba18ddd81 144 /** @defgroup I2C_Private_Macros
mbed_official 126:549ba18ddd81 145 * @{
mbed_official 126:549ba18ddd81 146 */
mbed_official 126:549ba18ddd81 147
mbed_official 126:549ba18ddd81 148 /**
mbed_official 126:549ba18ddd81 149 * @}
mbed_official 126:549ba18ddd81 150 */
mbed_official 126:549ba18ddd81 151
mbed_official 126:549ba18ddd81 152 /** @defgroup I2C_Private_Variables
mbed_official 126:549ba18ddd81 153 * @{
mbed_official 126:549ba18ddd81 154 */
mbed_official 126:549ba18ddd81 155
mbed_official 126:549ba18ddd81 156 /**
mbed_official 126:549ba18ddd81 157 * @}
mbed_official 126:549ba18ddd81 158 */
mbed_official 126:549ba18ddd81 159
mbed_official 126:549ba18ddd81 160 /** @defgroup I2C_Private_FunctionPrototypes
mbed_official 126:549ba18ddd81 161 * @{
mbed_official 126:549ba18ddd81 162 */
mbed_official 126:549ba18ddd81 163
mbed_official 126:549ba18ddd81 164 /**
mbed_official 126:549ba18ddd81 165 * @}
mbed_official 126:549ba18ddd81 166 */
mbed_official 126:549ba18ddd81 167
mbed_official 126:549ba18ddd81 168 /** @defgroup I2C_Private_Functions
mbed_official 126:549ba18ddd81 169 * @{
mbed_official 126:549ba18ddd81 170 */
mbed_official 126:549ba18ddd81 171
mbed_official 126:549ba18ddd81 172 /**
mbed_official 126:549ba18ddd81 173 * @brief Deinitializes the I2Cx peripheral registers to their default reset values.
mbed_official 126:549ba18ddd81 174 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 175 * @retval None
mbed_official 126:549ba18ddd81 176 */
mbed_official 126:549ba18ddd81 177 void I2C_DeInit(I2C_TypeDef* I2Cx)
mbed_official 126:549ba18ddd81 178 {
mbed_official 126:549ba18ddd81 179 /* Check the parameters */
mbed_official 126:549ba18ddd81 180 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 181
mbed_official 126:549ba18ddd81 182 if (I2Cx == I2C1)
mbed_official 126:549ba18ddd81 183 {
mbed_official 126:549ba18ddd81 184 /* Enable I2C1 reset state */
mbed_official 126:549ba18ddd81 185 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
mbed_official 126:549ba18ddd81 186 /* Release I2C1 from reset state */
mbed_official 126:549ba18ddd81 187 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
mbed_official 126:549ba18ddd81 188 }
mbed_official 126:549ba18ddd81 189 else
mbed_official 126:549ba18ddd81 190 {
mbed_official 126:549ba18ddd81 191 /* Enable I2C2 reset state */
mbed_official 126:549ba18ddd81 192 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
mbed_official 126:549ba18ddd81 193 /* Release I2C2 from reset state */
mbed_official 126:549ba18ddd81 194 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
mbed_official 126:549ba18ddd81 195 }
mbed_official 126:549ba18ddd81 196 }
mbed_official 126:549ba18ddd81 197
mbed_official 126:549ba18ddd81 198 /**
mbed_official 126:549ba18ddd81 199 * @brief Initializes the I2Cx peripheral according to the specified
mbed_official 126:549ba18ddd81 200 * parameters in the I2C_InitStruct.
mbed_official 126:549ba18ddd81 201 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 202 * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
mbed_official 126:549ba18ddd81 203 * contains the configuration information for the specified I2C peripheral.
mbed_official 126:549ba18ddd81 204 * @retval None
mbed_official 126:549ba18ddd81 205 */
mbed_official 126:549ba18ddd81 206 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
mbed_official 126:549ba18ddd81 207 {
mbed_official 126:549ba18ddd81 208 uint16_t tmpreg = 0, freqrange = 0;
mbed_official 126:549ba18ddd81 209 uint16_t result = 0x04;
mbed_official 126:549ba18ddd81 210 uint32_t pclk1 = 8000000;
mbed_official 126:549ba18ddd81 211 RCC_ClocksTypeDef rcc_clocks;
mbed_official 126:549ba18ddd81 212 /* Check the parameters */
mbed_official 126:549ba18ddd81 213 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 214 assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
mbed_official 126:549ba18ddd81 215 assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
mbed_official 126:549ba18ddd81 216 assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->I2C_DutyCycle));
mbed_official 126:549ba18ddd81 217 assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
mbed_official 126:549ba18ddd81 218 assert_param(IS_I2C_ACK_STATE(I2C_InitStruct->I2C_Ack));
mbed_official 126:549ba18ddd81 219 assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
mbed_official 126:549ba18ddd81 220
mbed_official 126:549ba18ddd81 221 /*---------------------------- I2Cx CR2 Configuration ------------------------*/
mbed_official 126:549ba18ddd81 222 /* Get the I2Cx CR2 value */
mbed_official 126:549ba18ddd81 223 tmpreg = I2Cx->CR2;
mbed_official 126:549ba18ddd81 224 /* Clear frequency FREQ[5:0] bits */
mbed_official 126:549ba18ddd81 225 tmpreg &= CR2_FREQ_Reset;
mbed_official 126:549ba18ddd81 226 /* Get pclk1 frequency value */
mbed_official 126:549ba18ddd81 227 RCC_GetClocksFreq(&rcc_clocks);
mbed_official 126:549ba18ddd81 228 pclk1 = rcc_clocks.PCLK1_Frequency;
mbed_official 126:549ba18ddd81 229 /* Set frequency bits depending on pclk1 value */
mbed_official 126:549ba18ddd81 230 freqrange = (uint16_t)(pclk1 / 1000000);
mbed_official 126:549ba18ddd81 231 tmpreg |= freqrange;
mbed_official 126:549ba18ddd81 232 /* Write to I2Cx CR2 */
mbed_official 126:549ba18ddd81 233 I2Cx->CR2 = tmpreg;
mbed_official 126:549ba18ddd81 234
mbed_official 126:549ba18ddd81 235 /*---------------------------- I2Cx CCR Configuration ------------------------*/
mbed_official 126:549ba18ddd81 236 /* Disable the selected I2C peripheral to configure TRISE */
mbed_official 126:549ba18ddd81 237 I2Cx->CR1 &= CR1_PE_Reset;
mbed_official 126:549ba18ddd81 238 /* Reset tmpreg value */
mbed_official 126:549ba18ddd81 239 /* Clear F/S, DUTY and CCR[11:0] bits */
mbed_official 126:549ba18ddd81 240 tmpreg = 0;
mbed_official 126:549ba18ddd81 241
mbed_official 126:549ba18ddd81 242 /* Configure speed in standard mode */
mbed_official 126:549ba18ddd81 243 if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
mbed_official 126:549ba18ddd81 244 {
mbed_official 126:549ba18ddd81 245 /* Standard mode speed calculate */
mbed_official 126:549ba18ddd81 246 result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));
mbed_official 126:549ba18ddd81 247 /* Test if CCR value is under 0x4*/
mbed_official 126:549ba18ddd81 248 if (result < 0x04)
mbed_official 126:549ba18ddd81 249 {
mbed_official 126:549ba18ddd81 250 /* Set minimum allowed value */
mbed_official 126:549ba18ddd81 251 result = 0x04;
mbed_official 126:549ba18ddd81 252 }
mbed_official 126:549ba18ddd81 253 /* Set speed value for standard mode */
mbed_official 126:549ba18ddd81 254 tmpreg |= result;
mbed_official 126:549ba18ddd81 255 /* Set Maximum Rise Time for standard mode */
mbed_official 126:549ba18ddd81 256 I2Cx->TRISE = freqrange + 1;
mbed_official 126:549ba18ddd81 257 }
mbed_official 126:549ba18ddd81 258 /* Configure speed in fast mode */
mbed_official 126:549ba18ddd81 259 else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
mbed_official 126:549ba18ddd81 260 {
mbed_official 126:549ba18ddd81 261 if (I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_2)
mbed_official 126:549ba18ddd81 262 {
mbed_official 126:549ba18ddd81 263 /* Fast mode speed calculate: Tlow/Thigh = 2 */
mbed_official 126:549ba18ddd81 264 result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 3));
mbed_official 126:549ba18ddd81 265 }
mbed_official 126:549ba18ddd81 266 else /*I2C_InitStruct->I2C_DutyCycle == I2C_DutyCycle_16_9*/
mbed_official 126:549ba18ddd81 267 {
mbed_official 126:549ba18ddd81 268 /* Fast mode speed calculate: Tlow/Thigh = 16/9 */
mbed_official 126:549ba18ddd81 269 result = (uint16_t)(pclk1 / (I2C_InitStruct->I2C_ClockSpeed * 25));
mbed_official 126:549ba18ddd81 270 /* Set DUTY bit */
mbed_official 126:549ba18ddd81 271 result |= I2C_DutyCycle_16_9;
mbed_official 126:549ba18ddd81 272 }
mbed_official 126:549ba18ddd81 273
mbed_official 126:549ba18ddd81 274 /* Test if CCR value is under 0x1*/
mbed_official 126:549ba18ddd81 275 if ((result & CCR_CCR_Set) == 0)
mbed_official 126:549ba18ddd81 276 {
mbed_official 126:549ba18ddd81 277 /* Set minimum allowed value */
mbed_official 126:549ba18ddd81 278 result |= (uint16_t)0x0001;
mbed_official 126:549ba18ddd81 279 }
mbed_official 126:549ba18ddd81 280 /* Set speed value and set F/S bit for fast mode */
mbed_official 126:549ba18ddd81 281 tmpreg |= (uint16_t)(result | CCR_FS_Set);
mbed_official 126:549ba18ddd81 282 /* Set Maximum Rise Time for fast mode */
mbed_official 126:549ba18ddd81 283 I2Cx->TRISE = (uint16_t)(((freqrange * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);
mbed_official 126:549ba18ddd81 284 }
mbed_official 126:549ba18ddd81 285
mbed_official 126:549ba18ddd81 286 /* Write to I2Cx CCR */
mbed_official 126:549ba18ddd81 287 I2Cx->CCR = tmpreg;
mbed_official 126:549ba18ddd81 288 /* Enable the selected I2C peripheral */
mbed_official 126:549ba18ddd81 289 I2Cx->CR1 |= CR1_PE_Set;
mbed_official 126:549ba18ddd81 290
mbed_official 126:549ba18ddd81 291 /*---------------------------- I2Cx CR1 Configuration ------------------------*/
mbed_official 126:549ba18ddd81 292 /* Get the I2Cx CR1 value */
mbed_official 126:549ba18ddd81 293 tmpreg = I2Cx->CR1;
mbed_official 126:549ba18ddd81 294 /* Clear ACK, SMBTYPE and SMBUS bits */
mbed_official 126:549ba18ddd81 295 tmpreg &= CR1_CLEAR_Mask;
mbed_official 126:549ba18ddd81 296 /* Configure I2Cx: mode and acknowledgement */
mbed_official 126:549ba18ddd81 297 /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
mbed_official 126:549ba18ddd81 298 /* Set ACK bit according to I2C_Ack value */
mbed_official 126:549ba18ddd81 299 tmpreg |= (uint16_t)((uint32_t)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
mbed_official 126:549ba18ddd81 300 /* Write to I2Cx CR1 */
mbed_official 126:549ba18ddd81 301 I2Cx->CR1 = tmpreg;
mbed_official 126:549ba18ddd81 302
mbed_official 126:549ba18ddd81 303 /*---------------------------- I2Cx OAR1 Configuration -----------------------*/
mbed_official 126:549ba18ddd81 304 /* Set I2Cx Own Address1 and acknowledged address */
mbed_official 126:549ba18ddd81 305 I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
mbed_official 126:549ba18ddd81 306 }
mbed_official 126:549ba18ddd81 307
mbed_official 126:549ba18ddd81 308 /**
mbed_official 126:549ba18ddd81 309 * @brief Fills each I2C_InitStruct member with its default value.
mbed_official 126:549ba18ddd81 310 * @param I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.
mbed_official 126:549ba18ddd81 311 * @retval None
mbed_official 126:549ba18ddd81 312 */
mbed_official 126:549ba18ddd81 313 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
mbed_official 126:549ba18ddd81 314 {
mbed_official 126:549ba18ddd81 315 /*---------------- Reset I2C init structure parameters values ----------------*/
mbed_official 126:549ba18ddd81 316 /* initialize the I2C_ClockSpeed member */
mbed_official 126:549ba18ddd81 317 I2C_InitStruct->I2C_ClockSpeed = 5000;
mbed_official 126:549ba18ddd81 318 /* Initialize the I2C_Mode member */
mbed_official 126:549ba18ddd81 319 I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
mbed_official 126:549ba18ddd81 320 /* Initialize the I2C_DutyCycle member */
mbed_official 126:549ba18ddd81 321 I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
mbed_official 126:549ba18ddd81 322 /* Initialize the I2C_OwnAddress1 member */
mbed_official 126:549ba18ddd81 323 I2C_InitStruct->I2C_OwnAddress1 = 0;
mbed_official 126:549ba18ddd81 324 /* Initialize the I2C_Ack member */
mbed_official 126:549ba18ddd81 325 I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
mbed_official 126:549ba18ddd81 326 /* Initialize the I2C_AcknowledgedAddress member */
mbed_official 126:549ba18ddd81 327 I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
mbed_official 126:549ba18ddd81 328 }
mbed_official 126:549ba18ddd81 329
mbed_official 126:549ba18ddd81 330 /**
mbed_official 126:549ba18ddd81 331 * @brief Enables or disables the specified I2C peripheral.
mbed_official 126:549ba18ddd81 332 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 333 * @param NewState: new state of the I2Cx peripheral.
mbed_official 126:549ba18ddd81 334 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 335 * @retval None
mbed_official 126:549ba18ddd81 336 */
mbed_official 126:549ba18ddd81 337 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 338 {
mbed_official 126:549ba18ddd81 339 /* Check the parameters */
mbed_official 126:549ba18ddd81 340 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 341 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 342 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 343 {
mbed_official 126:549ba18ddd81 344 /* Enable the selected I2C peripheral */
mbed_official 126:549ba18ddd81 345 I2Cx->CR1 |= CR1_PE_Set;
mbed_official 126:549ba18ddd81 346 }
mbed_official 126:549ba18ddd81 347 else
mbed_official 126:549ba18ddd81 348 {
mbed_official 126:549ba18ddd81 349 /* Disable the selected I2C peripheral */
mbed_official 126:549ba18ddd81 350 I2Cx->CR1 &= CR1_PE_Reset;
mbed_official 126:549ba18ddd81 351 }
mbed_official 126:549ba18ddd81 352 }
mbed_official 126:549ba18ddd81 353
mbed_official 126:549ba18ddd81 354 /**
mbed_official 126:549ba18ddd81 355 * @brief Enables or disables the specified I2C DMA requests.
mbed_official 126:549ba18ddd81 356 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 357 * @param NewState: new state of the I2C DMA transfer.
mbed_official 126:549ba18ddd81 358 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 359 * @retval None
mbed_official 126:549ba18ddd81 360 */
mbed_official 126:549ba18ddd81 361 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 362 {
mbed_official 126:549ba18ddd81 363 /* Check the parameters */
mbed_official 126:549ba18ddd81 364 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 365 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 366 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 367 {
mbed_official 126:549ba18ddd81 368 /* Enable the selected I2C DMA requests */
mbed_official 126:549ba18ddd81 369 I2Cx->CR2 |= CR2_DMAEN_Set;
mbed_official 126:549ba18ddd81 370 }
mbed_official 126:549ba18ddd81 371 else
mbed_official 126:549ba18ddd81 372 {
mbed_official 126:549ba18ddd81 373 /* Disable the selected I2C DMA requests */
mbed_official 126:549ba18ddd81 374 I2Cx->CR2 &= CR2_DMAEN_Reset;
mbed_official 126:549ba18ddd81 375 }
mbed_official 126:549ba18ddd81 376 }
mbed_official 126:549ba18ddd81 377
mbed_official 126:549ba18ddd81 378 /**
mbed_official 126:549ba18ddd81 379 * @brief Specifies if the next DMA transfer will be the last one.
mbed_official 126:549ba18ddd81 380 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 381 * @param NewState: new state of the I2C DMA last transfer.
mbed_official 126:549ba18ddd81 382 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 383 * @retval None
mbed_official 126:549ba18ddd81 384 */
mbed_official 126:549ba18ddd81 385 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 386 {
mbed_official 126:549ba18ddd81 387 /* Check the parameters */
mbed_official 126:549ba18ddd81 388 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 389 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 390 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 391 {
mbed_official 126:549ba18ddd81 392 /* Next DMA transfer is the last transfer */
mbed_official 126:549ba18ddd81 393 I2Cx->CR2 |= CR2_LAST_Set;
mbed_official 126:549ba18ddd81 394 }
mbed_official 126:549ba18ddd81 395 else
mbed_official 126:549ba18ddd81 396 {
mbed_official 126:549ba18ddd81 397 /* Next DMA transfer is not the last transfer */
mbed_official 126:549ba18ddd81 398 I2Cx->CR2 &= CR2_LAST_Reset;
mbed_official 126:549ba18ddd81 399 }
mbed_official 126:549ba18ddd81 400 }
mbed_official 126:549ba18ddd81 401
mbed_official 126:549ba18ddd81 402 /**
mbed_official 126:549ba18ddd81 403 * @brief Generates I2Cx communication START condition.
mbed_official 126:549ba18ddd81 404 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 405 * @param NewState: new state of the I2C START condition generation.
mbed_official 126:549ba18ddd81 406 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 407 * @retval None.
mbed_official 126:549ba18ddd81 408 */
mbed_official 126:549ba18ddd81 409 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 410 {
mbed_official 126:549ba18ddd81 411 /* Check the parameters */
mbed_official 126:549ba18ddd81 412 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 413 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 414 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 415 {
mbed_official 126:549ba18ddd81 416 /* Generate a START condition */
mbed_official 126:549ba18ddd81 417 I2Cx->CR1 |= CR1_START_Set;
mbed_official 126:549ba18ddd81 418 }
mbed_official 126:549ba18ddd81 419 else
mbed_official 126:549ba18ddd81 420 {
mbed_official 126:549ba18ddd81 421 /* Disable the START condition generation */
mbed_official 126:549ba18ddd81 422 I2Cx->CR1 &= CR1_START_Reset;
mbed_official 126:549ba18ddd81 423 }
mbed_official 126:549ba18ddd81 424 }
mbed_official 126:549ba18ddd81 425
mbed_official 126:549ba18ddd81 426 /**
mbed_official 126:549ba18ddd81 427 * @brief Generates I2Cx communication STOP condition.
mbed_official 126:549ba18ddd81 428 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 429 * @param NewState: new state of the I2C STOP condition generation.
mbed_official 126:549ba18ddd81 430 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 431 * @retval None.
mbed_official 126:549ba18ddd81 432 */
mbed_official 126:549ba18ddd81 433 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 434 {
mbed_official 126:549ba18ddd81 435 /* Check the parameters */
mbed_official 126:549ba18ddd81 436 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 437 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 438 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 439 {
mbed_official 126:549ba18ddd81 440 /* Generate a STOP condition */
mbed_official 126:549ba18ddd81 441 I2Cx->CR1 |= CR1_STOP_Set;
mbed_official 126:549ba18ddd81 442 }
mbed_official 126:549ba18ddd81 443 else
mbed_official 126:549ba18ddd81 444 {
mbed_official 126:549ba18ddd81 445 /* Disable the STOP condition generation */
mbed_official 126:549ba18ddd81 446 I2Cx->CR1 &= CR1_STOP_Reset;
mbed_official 126:549ba18ddd81 447 }
mbed_official 126:549ba18ddd81 448 }
mbed_official 126:549ba18ddd81 449
mbed_official 126:549ba18ddd81 450 /**
mbed_official 126:549ba18ddd81 451 * @brief Enables or disables the specified I2C acknowledge feature.
mbed_official 126:549ba18ddd81 452 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 453 * @param NewState: new state of the I2C Acknowledgement.
mbed_official 126:549ba18ddd81 454 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 455 * @retval None.
mbed_official 126:549ba18ddd81 456 */
mbed_official 126:549ba18ddd81 457 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 458 {
mbed_official 126:549ba18ddd81 459 /* Check the parameters */
mbed_official 126:549ba18ddd81 460 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 461 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 462 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 463 {
mbed_official 126:549ba18ddd81 464 /* Enable the acknowledgement */
mbed_official 126:549ba18ddd81 465 I2Cx->CR1 |= CR1_ACK_Set;
mbed_official 126:549ba18ddd81 466 }
mbed_official 126:549ba18ddd81 467 else
mbed_official 126:549ba18ddd81 468 {
mbed_official 126:549ba18ddd81 469 /* Disable the acknowledgement */
mbed_official 126:549ba18ddd81 470 I2Cx->CR1 &= CR1_ACK_Reset;
mbed_official 126:549ba18ddd81 471 }
mbed_official 126:549ba18ddd81 472 }
mbed_official 126:549ba18ddd81 473
mbed_official 126:549ba18ddd81 474 /**
mbed_official 126:549ba18ddd81 475 * @brief Configures the specified I2C own address2.
mbed_official 126:549ba18ddd81 476 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 477 * @param Address: specifies the 7bit I2C own address2.
mbed_official 126:549ba18ddd81 478 * @retval None.
mbed_official 126:549ba18ddd81 479 */
mbed_official 126:549ba18ddd81 480 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
mbed_official 126:549ba18ddd81 481 {
mbed_official 126:549ba18ddd81 482 uint16_t tmpreg = 0;
mbed_official 126:549ba18ddd81 483
mbed_official 126:549ba18ddd81 484 /* Check the parameters */
mbed_official 126:549ba18ddd81 485 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 486
mbed_official 126:549ba18ddd81 487 /* Get the old register value */
mbed_official 126:549ba18ddd81 488 tmpreg = I2Cx->OAR2;
mbed_official 126:549ba18ddd81 489
mbed_official 126:549ba18ddd81 490 /* Reset I2Cx Own address2 bit [7:1] */
mbed_official 126:549ba18ddd81 491 tmpreg &= OAR2_ADD2_Reset;
mbed_official 126:549ba18ddd81 492
mbed_official 126:549ba18ddd81 493 /* Set I2Cx Own address2 */
mbed_official 126:549ba18ddd81 494 tmpreg |= (uint16_t)((uint16_t)Address & (uint16_t)0x00FE);
mbed_official 126:549ba18ddd81 495
mbed_official 126:549ba18ddd81 496 /* Store the new register value */
mbed_official 126:549ba18ddd81 497 I2Cx->OAR2 = tmpreg;
mbed_official 126:549ba18ddd81 498 }
mbed_official 126:549ba18ddd81 499
mbed_official 126:549ba18ddd81 500 /**
mbed_official 126:549ba18ddd81 501 * @brief Enables or disables the specified I2C dual addressing mode.
mbed_official 126:549ba18ddd81 502 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 503 * @param NewState: new state of the I2C dual addressing mode.
mbed_official 126:549ba18ddd81 504 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 505 * @retval None
mbed_official 126:549ba18ddd81 506 */
mbed_official 126:549ba18ddd81 507 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 508 {
mbed_official 126:549ba18ddd81 509 /* Check the parameters */
mbed_official 126:549ba18ddd81 510 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 511 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 512 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 513 {
mbed_official 126:549ba18ddd81 514 /* Enable dual addressing mode */
mbed_official 126:549ba18ddd81 515 I2Cx->OAR2 |= OAR2_ENDUAL_Set;
mbed_official 126:549ba18ddd81 516 }
mbed_official 126:549ba18ddd81 517 else
mbed_official 126:549ba18ddd81 518 {
mbed_official 126:549ba18ddd81 519 /* Disable dual addressing mode */
mbed_official 126:549ba18ddd81 520 I2Cx->OAR2 &= OAR2_ENDUAL_Reset;
mbed_official 126:549ba18ddd81 521 }
mbed_official 126:549ba18ddd81 522 }
mbed_official 126:549ba18ddd81 523
mbed_official 126:549ba18ddd81 524 /**
mbed_official 126:549ba18ddd81 525 * @brief Enables or disables the specified I2C general call feature.
mbed_official 126:549ba18ddd81 526 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 527 * @param NewState: new state of the I2C General call.
mbed_official 126:549ba18ddd81 528 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 529 * @retval None
mbed_official 126:549ba18ddd81 530 */
mbed_official 126:549ba18ddd81 531 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 532 {
mbed_official 126:549ba18ddd81 533 /* Check the parameters */
mbed_official 126:549ba18ddd81 534 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 535 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 536 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 537 {
mbed_official 126:549ba18ddd81 538 /* Enable generall call */
mbed_official 126:549ba18ddd81 539 I2Cx->CR1 |= CR1_ENGC_Set;
mbed_official 126:549ba18ddd81 540 }
mbed_official 126:549ba18ddd81 541 else
mbed_official 126:549ba18ddd81 542 {
mbed_official 126:549ba18ddd81 543 /* Disable generall call */
mbed_official 126:549ba18ddd81 544 I2Cx->CR1 &= CR1_ENGC_Reset;
mbed_official 126:549ba18ddd81 545 }
mbed_official 126:549ba18ddd81 546 }
mbed_official 126:549ba18ddd81 547
mbed_official 126:549ba18ddd81 548 /**
mbed_official 126:549ba18ddd81 549 * @brief Enables or disables the specified I2C interrupts.
mbed_official 126:549ba18ddd81 550 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 551 * @param I2C_IT: specifies the I2C interrupts sources to be enabled or disabled.
mbed_official 126:549ba18ddd81 552 * This parameter can be any combination of the following values:
mbed_official 126:549ba18ddd81 553 * @arg I2C_IT_BUF: Buffer interrupt mask
mbed_official 126:549ba18ddd81 554 * @arg I2C_IT_EVT: Event interrupt mask
mbed_official 126:549ba18ddd81 555 * @arg I2C_IT_ERR: Error interrupt mask
mbed_official 126:549ba18ddd81 556 * @param NewState: new state of the specified I2C interrupts.
mbed_official 126:549ba18ddd81 557 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 558 * @retval None
mbed_official 126:549ba18ddd81 559 */
mbed_official 126:549ba18ddd81 560 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
mbed_official 126:549ba18ddd81 561 {
mbed_official 126:549ba18ddd81 562 /* Check the parameters */
mbed_official 126:549ba18ddd81 563 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 564 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 565 assert_param(IS_I2C_CONFIG_IT(I2C_IT));
mbed_official 126:549ba18ddd81 566
mbed_official 126:549ba18ddd81 567 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 568 {
mbed_official 126:549ba18ddd81 569 /* Enable the selected I2C interrupts */
mbed_official 126:549ba18ddd81 570 I2Cx->CR2 |= I2C_IT;
mbed_official 126:549ba18ddd81 571 }
mbed_official 126:549ba18ddd81 572 else
mbed_official 126:549ba18ddd81 573 {
mbed_official 126:549ba18ddd81 574 /* Disable the selected I2C interrupts */
mbed_official 126:549ba18ddd81 575 I2Cx->CR2 &= (uint16_t)~I2C_IT;
mbed_official 126:549ba18ddd81 576 }
mbed_official 126:549ba18ddd81 577 }
mbed_official 126:549ba18ddd81 578
mbed_official 126:549ba18ddd81 579 /**
mbed_official 126:549ba18ddd81 580 * @brief Sends a data byte through the I2Cx peripheral.
mbed_official 126:549ba18ddd81 581 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 582 * @param Data: Byte to be transmitted..
mbed_official 126:549ba18ddd81 583 * @retval None
mbed_official 126:549ba18ddd81 584 */
mbed_official 126:549ba18ddd81 585 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
mbed_official 126:549ba18ddd81 586 {
mbed_official 126:549ba18ddd81 587 /* Check the parameters */
mbed_official 126:549ba18ddd81 588 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 589 /* Write in the DR register the data to be sent */
mbed_official 126:549ba18ddd81 590 I2Cx->DR = Data;
mbed_official 126:549ba18ddd81 591 }
mbed_official 126:549ba18ddd81 592
mbed_official 126:549ba18ddd81 593 /**
mbed_official 126:549ba18ddd81 594 * @brief Returns the most recent received data by the I2Cx peripheral.
mbed_official 126:549ba18ddd81 595 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 596 * @retval The value of the received data.
mbed_official 126:549ba18ddd81 597 */
mbed_official 126:549ba18ddd81 598 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
mbed_official 126:549ba18ddd81 599 {
mbed_official 126:549ba18ddd81 600 /* Check the parameters */
mbed_official 126:549ba18ddd81 601 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 602 /* Return the data in the DR register */
mbed_official 126:549ba18ddd81 603 return (uint8_t)I2Cx->DR;
mbed_official 126:549ba18ddd81 604 }
mbed_official 126:549ba18ddd81 605
mbed_official 126:549ba18ddd81 606 /**
mbed_official 126:549ba18ddd81 607 * @brief Transmits the address byte to select the slave device.
mbed_official 126:549ba18ddd81 608 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 609 * @param Address: specifies the slave address which will be transmitted
mbed_official 126:549ba18ddd81 610 * @param I2C_Direction: specifies whether the I2C device will be a
mbed_official 126:549ba18ddd81 611 * Transmitter or a Receiver. This parameter can be one of the following values
mbed_official 126:549ba18ddd81 612 * @arg I2C_Direction_Transmitter: Transmitter mode
mbed_official 126:549ba18ddd81 613 * @arg I2C_Direction_Receiver: Receiver mode
mbed_official 126:549ba18ddd81 614 * @retval None.
mbed_official 126:549ba18ddd81 615 */
mbed_official 126:549ba18ddd81 616 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
mbed_official 126:549ba18ddd81 617 {
mbed_official 126:549ba18ddd81 618 /* Check the parameters */
mbed_official 126:549ba18ddd81 619 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 620 assert_param(IS_I2C_DIRECTION(I2C_Direction));
mbed_official 126:549ba18ddd81 621 /* Test on the direction to set/reset the read/write bit */
mbed_official 126:549ba18ddd81 622 if (I2C_Direction != I2C_Direction_Transmitter)
mbed_official 126:549ba18ddd81 623 {
mbed_official 126:549ba18ddd81 624 /* Set the address bit0 for read */
mbed_official 126:549ba18ddd81 625 Address |= OAR1_ADD0_Set;
mbed_official 126:549ba18ddd81 626 }
mbed_official 126:549ba18ddd81 627 else
mbed_official 126:549ba18ddd81 628 {
mbed_official 126:549ba18ddd81 629 /* Reset the address bit0 for write */
mbed_official 126:549ba18ddd81 630 Address &= OAR1_ADD0_Reset;
mbed_official 126:549ba18ddd81 631 }
mbed_official 126:549ba18ddd81 632 /* Send the address */
mbed_official 126:549ba18ddd81 633 I2Cx->DR = Address;
mbed_official 126:549ba18ddd81 634 }
mbed_official 126:549ba18ddd81 635
mbed_official 126:549ba18ddd81 636 /**
mbed_official 126:549ba18ddd81 637 * @brief Reads the specified I2C register and returns its value.
mbed_official 126:549ba18ddd81 638 * @param I2C_Register: specifies the register to read.
mbed_official 126:549ba18ddd81 639 * This parameter can be one of the following values:
mbed_official 126:549ba18ddd81 640 * @arg I2C_Register_CR1: CR1 register.
mbed_official 126:549ba18ddd81 641 * @arg I2C_Register_CR2: CR2 register.
mbed_official 126:549ba18ddd81 642 * @arg I2C_Register_OAR1: OAR1 register.
mbed_official 126:549ba18ddd81 643 * @arg I2C_Register_OAR2: OAR2 register.
mbed_official 126:549ba18ddd81 644 * @arg I2C_Register_DR: DR register.
mbed_official 126:549ba18ddd81 645 * @arg I2C_Register_SR1: SR1 register.
mbed_official 126:549ba18ddd81 646 * @arg I2C_Register_SR2: SR2 register.
mbed_official 126:549ba18ddd81 647 * @arg I2C_Register_CCR: CCR register.
mbed_official 126:549ba18ddd81 648 * @arg I2C_Register_TRISE: TRISE register.
mbed_official 126:549ba18ddd81 649 * @retval The value of the read register.
mbed_official 126:549ba18ddd81 650 */
mbed_official 126:549ba18ddd81 651 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
mbed_official 126:549ba18ddd81 652 {
mbed_official 126:549ba18ddd81 653 __IO uint32_t tmp = 0;
mbed_official 126:549ba18ddd81 654
mbed_official 126:549ba18ddd81 655 /* Check the parameters */
mbed_official 126:549ba18ddd81 656 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 657 assert_param(IS_I2C_REGISTER(I2C_Register));
mbed_official 126:549ba18ddd81 658
mbed_official 126:549ba18ddd81 659 tmp = (uint32_t) I2Cx;
mbed_official 126:549ba18ddd81 660 tmp += I2C_Register;
mbed_official 126:549ba18ddd81 661
mbed_official 126:549ba18ddd81 662 /* Return the selected register value */
mbed_official 126:549ba18ddd81 663 return (*(__IO uint16_t *) tmp);
mbed_official 126:549ba18ddd81 664 }
mbed_official 126:549ba18ddd81 665
mbed_official 126:549ba18ddd81 666 /**
mbed_official 126:549ba18ddd81 667 * @brief Enables or disables the specified I2C software reset.
mbed_official 126:549ba18ddd81 668 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 669 * @param NewState: new state of the I2C software reset.
mbed_official 126:549ba18ddd81 670 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 671 * @retval None
mbed_official 126:549ba18ddd81 672 */
mbed_official 126:549ba18ddd81 673 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 674 {
mbed_official 126:549ba18ddd81 675 /* Check the parameters */
mbed_official 126:549ba18ddd81 676 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 677 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 678 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 679 {
mbed_official 126:549ba18ddd81 680 /* Peripheral under reset */
mbed_official 126:549ba18ddd81 681 I2Cx->CR1 |= CR1_SWRST_Set;
mbed_official 126:549ba18ddd81 682 }
mbed_official 126:549ba18ddd81 683 else
mbed_official 126:549ba18ddd81 684 {
mbed_official 126:549ba18ddd81 685 /* Peripheral not under reset */
mbed_official 126:549ba18ddd81 686 I2Cx->CR1 &= CR1_SWRST_Reset;
mbed_official 126:549ba18ddd81 687 }
mbed_official 126:549ba18ddd81 688 }
mbed_official 126:549ba18ddd81 689
mbed_official 126:549ba18ddd81 690 /**
mbed_official 126:549ba18ddd81 691 * @brief Selects the specified I2C NACK position in master receiver mode.
mbed_official 126:549ba18ddd81 692 * This function is useful in I2C Master Receiver mode when the number
mbed_official 126:549ba18ddd81 693 * of data to be received is equal to 2. In this case, this function
mbed_official 126:549ba18ddd81 694 * should be called (with parameter I2C_NACKPosition_Next) before data
mbed_official 126:549ba18ddd81 695 * reception starts,as described in the 2-byte reception procedure
mbed_official 126:549ba18ddd81 696 * recommended in Reference Manual in Section: Master receiver.
mbed_official 126:549ba18ddd81 697 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 698 * @param I2C_NACKPosition: specifies the NACK position.
mbed_official 126:549ba18ddd81 699 * This parameter can be one of the following values:
mbed_official 126:549ba18ddd81 700 * @arg I2C_NACKPosition_Next: indicates that the next byte will be the last
mbed_official 126:549ba18ddd81 701 * received byte.
mbed_official 126:549ba18ddd81 702 * @arg I2C_NACKPosition_Current: indicates that current byte is the last
mbed_official 126:549ba18ddd81 703 * received byte.
mbed_official 126:549ba18ddd81 704 *
mbed_official 126:549ba18ddd81 705 * @note This function configures the same bit (POS) as I2C_PECPositionConfig()
mbed_official 126:549ba18ddd81 706 * but is intended to be used in I2C mode while I2C_PECPositionConfig()
mbed_official 126:549ba18ddd81 707 * is intended to used in SMBUS mode.
mbed_official 126:549ba18ddd81 708 *
mbed_official 126:549ba18ddd81 709 * @retval None
mbed_official 126:549ba18ddd81 710 */
mbed_official 126:549ba18ddd81 711 void I2C_NACKPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_NACKPosition)
mbed_official 126:549ba18ddd81 712 {
mbed_official 126:549ba18ddd81 713 /* Check the parameters */
mbed_official 126:549ba18ddd81 714 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 715 assert_param(IS_I2C_NACK_POSITION(I2C_NACKPosition));
mbed_official 126:549ba18ddd81 716
mbed_official 126:549ba18ddd81 717 /* Check the input parameter */
mbed_official 126:549ba18ddd81 718 if (I2C_NACKPosition == I2C_NACKPosition_Next)
mbed_official 126:549ba18ddd81 719 {
mbed_official 126:549ba18ddd81 720 /* Next byte in shift register is the last received byte */
mbed_official 126:549ba18ddd81 721 I2Cx->CR1 |= I2C_NACKPosition_Next;
mbed_official 126:549ba18ddd81 722 }
mbed_official 126:549ba18ddd81 723 else
mbed_official 126:549ba18ddd81 724 {
mbed_official 126:549ba18ddd81 725 /* Current byte in shift register is the last received byte */
mbed_official 126:549ba18ddd81 726 I2Cx->CR1 &= I2C_NACKPosition_Current;
mbed_official 126:549ba18ddd81 727 }
mbed_official 126:549ba18ddd81 728 }
mbed_official 126:549ba18ddd81 729
mbed_official 126:549ba18ddd81 730 /**
mbed_official 126:549ba18ddd81 731 * @brief Drives the SMBusAlert pin high or low for the specified I2C.
mbed_official 126:549ba18ddd81 732 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 733 * @param I2C_SMBusAlert: specifies SMBAlert pin level.
mbed_official 126:549ba18ddd81 734 * This parameter can be one of the following values:
mbed_official 126:549ba18ddd81 735 * @arg I2C_SMBusAlert_Low: SMBAlert pin driven low
mbed_official 126:549ba18ddd81 736 * @arg I2C_SMBusAlert_High: SMBAlert pin driven high
mbed_official 126:549ba18ddd81 737 * @retval None
mbed_official 126:549ba18ddd81 738 */
mbed_official 126:549ba18ddd81 739 void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, uint16_t I2C_SMBusAlert)
mbed_official 126:549ba18ddd81 740 {
mbed_official 126:549ba18ddd81 741 /* Check the parameters */
mbed_official 126:549ba18ddd81 742 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 743 assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
mbed_official 126:549ba18ddd81 744 if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
mbed_official 126:549ba18ddd81 745 {
mbed_official 126:549ba18ddd81 746 /* Drive the SMBusAlert pin Low */
mbed_official 126:549ba18ddd81 747 I2Cx->CR1 |= I2C_SMBusAlert_Low;
mbed_official 126:549ba18ddd81 748 }
mbed_official 126:549ba18ddd81 749 else
mbed_official 126:549ba18ddd81 750 {
mbed_official 126:549ba18ddd81 751 /* Drive the SMBusAlert pin High */
mbed_official 126:549ba18ddd81 752 I2Cx->CR1 &= I2C_SMBusAlert_High;
mbed_official 126:549ba18ddd81 753 }
mbed_official 126:549ba18ddd81 754 }
mbed_official 126:549ba18ddd81 755
mbed_official 126:549ba18ddd81 756 /**
mbed_official 126:549ba18ddd81 757 * @brief Enables or disables the specified I2C PEC transfer.
mbed_official 126:549ba18ddd81 758 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 759 * @param NewState: new state of the I2C PEC transmission.
mbed_official 126:549ba18ddd81 760 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 761 * @retval None
mbed_official 126:549ba18ddd81 762 */
mbed_official 126:549ba18ddd81 763 void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 764 {
mbed_official 126:549ba18ddd81 765 /* Check the parameters */
mbed_official 126:549ba18ddd81 766 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 767 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 768 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 769 {
mbed_official 126:549ba18ddd81 770 /* Enable the selected I2C PEC transmission */
mbed_official 126:549ba18ddd81 771 I2Cx->CR1 |= CR1_PEC_Set;
mbed_official 126:549ba18ddd81 772 }
mbed_official 126:549ba18ddd81 773 else
mbed_official 126:549ba18ddd81 774 {
mbed_official 126:549ba18ddd81 775 /* Disable the selected I2C PEC transmission */
mbed_official 126:549ba18ddd81 776 I2Cx->CR1 &= CR1_PEC_Reset;
mbed_official 126:549ba18ddd81 777 }
mbed_official 126:549ba18ddd81 778 }
mbed_official 126:549ba18ddd81 779
mbed_official 126:549ba18ddd81 780 /**
mbed_official 126:549ba18ddd81 781 * @brief Selects the specified I2C PEC position.
mbed_official 126:549ba18ddd81 782 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 783 * @param I2C_PECPosition: specifies the PEC position.
mbed_official 126:549ba18ddd81 784 * This parameter can be one of the following values:
mbed_official 126:549ba18ddd81 785 * @arg I2C_PECPosition_Next: indicates that the next byte is PEC
mbed_official 126:549ba18ddd81 786 * @arg I2C_PECPosition_Current: indicates that current byte is PEC
mbed_official 126:549ba18ddd81 787 *
mbed_official 126:549ba18ddd81 788 * @note This function configures the same bit (POS) as I2C_NACKPositionConfig()
mbed_official 126:549ba18ddd81 789 * but is intended to be used in SMBUS mode while I2C_NACKPositionConfig()
mbed_official 126:549ba18ddd81 790 * is intended to used in I2C mode.
mbed_official 126:549ba18ddd81 791 *
mbed_official 126:549ba18ddd81 792 * @retval None
mbed_official 126:549ba18ddd81 793 */
mbed_official 126:549ba18ddd81 794 void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, uint16_t I2C_PECPosition)
mbed_official 126:549ba18ddd81 795 {
mbed_official 126:549ba18ddd81 796 /* Check the parameters */
mbed_official 126:549ba18ddd81 797 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 798 assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));
mbed_official 126:549ba18ddd81 799 if (I2C_PECPosition == I2C_PECPosition_Next)
mbed_official 126:549ba18ddd81 800 {
mbed_official 126:549ba18ddd81 801 /* Next byte in shift register is PEC */
mbed_official 126:549ba18ddd81 802 I2Cx->CR1 |= I2C_PECPosition_Next;
mbed_official 126:549ba18ddd81 803 }
mbed_official 126:549ba18ddd81 804 else
mbed_official 126:549ba18ddd81 805 {
mbed_official 126:549ba18ddd81 806 /* Current byte in shift register is PEC */
mbed_official 126:549ba18ddd81 807 I2Cx->CR1 &= I2C_PECPosition_Current;
mbed_official 126:549ba18ddd81 808 }
mbed_official 126:549ba18ddd81 809 }
mbed_official 126:549ba18ddd81 810
mbed_official 126:549ba18ddd81 811 /**
mbed_official 126:549ba18ddd81 812 * @brief Enables or disables the PEC value calculation of the transferred bytes.
mbed_official 126:549ba18ddd81 813 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 814 * @param NewState: new state of the I2Cx PEC value calculation.
mbed_official 126:549ba18ddd81 815 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 816 * @retval None
mbed_official 126:549ba18ddd81 817 */
mbed_official 126:549ba18ddd81 818 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 819 {
mbed_official 126:549ba18ddd81 820 /* Check the parameters */
mbed_official 126:549ba18ddd81 821 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 822 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 823 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 824 {
mbed_official 126:549ba18ddd81 825 /* Enable the selected I2C PEC calculation */
mbed_official 126:549ba18ddd81 826 I2Cx->CR1 |= CR1_ENPEC_Set;
mbed_official 126:549ba18ddd81 827 }
mbed_official 126:549ba18ddd81 828 else
mbed_official 126:549ba18ddd81 829 {
mbed_official 126:549ba18ddd81 830 /* Disable the selected I2C PEC calculation */
mbed_official 126:549ba18ddd81 831 I2Cx->CR1 &= CR1_ENPEC_Reset;
mbed_official 126:549ba18ddd81 832 }
mbed_official 126:549ba18ddd81 833 }
mbed_official 126:549ba18ddd81 834
mbed_official 126:549ba18ddd81 835 /**
mbed_official 126:549ba18ddd81 836 * @brief Returns the PEC value for the specified I2C.
mbed_official 126:549ba18ddd81 837 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 838 * @retval The PEC value.
mbed_official 126:549ba18ddd81 839 */
mbed_official 126:549ba18ddd81 840 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)
mbed_official 126:549ba18ddd81 841 {
mbed_official 126:549ba18ddd81 842 /* Check the parameters */
mbed_official 126:549ba18ddd81 843 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 844 /* Return the selected I2C PEC value */
mbed_official 126:549ba18ddd81 845 return ((I2Cx->SR2) >> 8);
mbed_official 126:549ba18ddd81 846 }
mbed_official 126:549ba18ddd81 847
mbed_official 126:549ba18ddd81 848 /**
mbed_official 126:549ba18ddd81 849 * @brief Enables or disables the specified I2C ARP.
mbed_official 126:549ba18ddd81 850 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 851 * @param NewState: new state of the I2Cx ARP.
mbed_official 126:549ba18ddd81 852 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 853 * @retval None
mbed_official 126:549ba18ddd81 854 */
mbed_official 126:549ba18ddd81 855 void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 856 {
mbed_official 126:549ba18ddd81 857 /* Check the parameters */
mbed_official 126:549ba18ddd81 858 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 859 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 860 if (NewState != DISABLE)
mbed_official 126:549ba18ddd81 861 {
mbed_official 126:549ba18ddd81 862 /* Enable the selected I2C ARP */
mbed_official 126:549ba18ddd81 863 I2Cx->CR1 |= CR1_ENARP_Set;
mbed_official 126:549ba18ddd81 864 }
mbed_official 126:549ba18ddd81 865 else
mbed_official 126:549ba18ddd81 866 {
mbed_official 126:549ba18ddd81 867 /* Disable the selected I2C ARP */
mbed_official 126:549ba18ddd81 868 I2Cx->CR1 &= CR1_ENARP_Reset;
mbed_official 126:549ba18ddd81 869 }
mbed_official 126:549ba18ddd81 870 }
mbed_official 126:549ba18ddd81 871
mbed_official 126:549ba18ddd81 872 /**
mbed_official 126:549ba18ddd81 873 * @brief Enables or disables the specified I2C Clock stretching.
mbed_official 126:549ba18ddd81 874 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 875 * @param NewState: new state of the I2Cx Clock stretching.
mbed_official 126:549ba18ddd81 876 * This parameter can be: ENABLE or DISABLE.
mbed_official 126:549ba18ddd81 877 * @retval None
mbed_official 126:549ba18ddd81 878 */
mbed_official 126:549ba18ddd81 879 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 126:549ba18ddd81 880 {
mbed_official 126:549ba18ddd81 881 /* Check the parameters */
mbed_official 126:549ba18ddd81 882 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 883 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 126:549ba18ddd81 884 if (NewState == DISABLE)
mbed_official 126:549ba18ddd81 885 {
mbed_official 126:549ba18ddd81 886 /* Enable the selected I2C Clock stretching */
mbed_official 126:549ba18ddd81 887 I2Cx->CR1 |= CR1_NOSTRETCH_Set;
mbed_official 126:549ba18ddd81 888 }
mbed_official 126:549ba18ddd81 889 else
mbed_official 126:549ba18ddd81 890 {
mbed_official 126:549ba18ddd81 891 /* Disable the selected I2C Clock stretching */
mbed_official 126:549ba18ddd81 892 I2Cx->CR1 &= CR1_NOSTRETCH_Reset;
mbed_official 126:549ba18ddd81 893 }
mbed_official 126:549ba18ddd81 894 }
mbed_official 126:549ba18ddd81 895
mbed_official 126:549ba18ddd81 896 /**
mbed_official 126:549ba18ddd81 897 * @brief Selects the specified I2C fast mode duty cycle.
mbed_official 126:549ba18ddd81 898 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 899 * @param I2C_DutyCycle: specifies the fast mode duty cycle.
mbed_official 126:549ba18ddd81 900 * This parameter can be one of the following values:
mbed_official 126:549ba18ddd81 901 * @arg I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2
mbed_official 126:549ba18ddd81 902 * @arg I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9
mbed_official 126:549ba18ddd81 903 * @retval None
mbed_official 126:549ba18ddd81 904 */
mbed_official 126:549ba18ddd81 905 void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, uint16_t I2C_DutyCycle)
mbed_official 126:549ba18ddd81 906 {
mbed_official 126:549ba18ddd81 907 /* Check the parameters */
mbed_official 126:549ba18ddd81 908 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 909 assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
mbed_official 126:549ba18ddd81 910 if (I2C_DutyCycle != I2C_DutyCycle_16_9)
mbed_official 126:549ba18ddd81 911 {
mbed_official 126:549ba18ddd81 912 /* I2C fast mode Tlow/Thigh=2 */
mbed_official 126:549ba18ddd81 913 I2Cx->CCR &= I2C_DutyCycle_2;
mbed_official 126:549ba18ddd81 914 }
mbed_official 126:549ba18ddd81 915 else
mbed_official 126:549ba18ddd81 916 {
mbed_official 126:549ba18ddd81 917 /* I2C fast mode Tlow/Thigh=16/9 */
mbed_official 126:549ba18ddd81 918 I2Cx->CCR |= I2C_DutyCycle_16_9;
mbed_official 126:549ba18ddd81 919 }
mbed_official 126:549ba18ddd81 920 }
mbed_official 126:549ba18ddd81 921
mbed_official 126:549ba18ddd81 922
mbed_official 126:549ba18ddd81 923
mbed_official 126:549ba18ddd81 924 /**
mbed_official 126:549ba18ddd81 925 * @brief
mbed_official 126:549ba18ddd81 926 ****************************************************************************************
mbed_official 126:549ba18ddd81 927 *
mbed_official 126:549ba18ddd81 928 * I2C State Monitoring Functions
mbed_official 126:549ba18ddd81 929 *
mbed_official 126:549ba18ddd81 930 ****************************************************************************************
mbed_official 126:549ba18ddd81 931 * This I2C driver provides three different ways for I2C state monitoring
mbed_official 126:549ba18ddd81 932 * depending on the application requirements and constraints:
mbed_official 126:549ba18ddd81 933 *
mbed_official 126:549ba18ddd81 934 *
mbed_official 126:549ba18ddd81 935 * 1) Basic state monitoring:
mbed_official 126:549ba18ddd81 936 * Using I2C_CheckEvent() function:
mbed_official 126:549ba18ddd81 937 * It compares the status registers (SR1 and SR2) content to a given event
mbed_official 126:549ba18ddd81 938 * (can be the combination of one or more flags).
mbed_official 126:549ba18ddd81 939 * It returns SUCCESS if the current status includes the given flags
mbed_official 126:549ba18ddd81 940 * and returns ERROR if one or more flags are missing in the current status.
mbed_official 126:549ba18ddd81 941 * - When to use:
mbed_official 126:549ba18ddd81 942 * - This function is suitable for most applications as well as for startup
mbed_official 126:549ba18ddd81 943 * activity since the events are fully described in the product reference manual
mbed_official 126:549ba18ddd81 944 * (RM0008).
mbed_official 126:549ba18ddd81 945 * - It is also suitable for users who need to define their own events.
mbed_official 126:549ba18ddd81 946 * - Limitations:
mbed_official 126:549ba18ddd81 947 * - If an error occurs (ie. error flags are set besides to the monitored flags),
mbed_official 126:549ba18ddd81 948 * the I2C_CheckEvent() function may return SUCCESS despite the communication
mbed_official 126:549ba18ddd81 949 * hold or corrupted real state.
mbed_official 126:549ba18ddd81 950 * In this case, it is advised to use error interrupts to monitor the error
mbed_official 126:549ba18ddd81 951 * events and handle them in the interrupt IRQ handler.
mbed_official 126:549ba18ddd81 952 *
mbed_official 126:549ba18ddd81 953 * @note
mbed_official 126:549ba18ddd81 954 * For error management, it is advised to use the following functions:
mbed_official 126:549ba18ddd81 955 * - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).
mbed_official 126:549ba18ddd81 956 * - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.
mbed_official 126:549ba18ddd81 957 * Where x is the peripheral instance (I2C1, I2C2 ...)
mbed_official 126:549ba18ddd81 958 * - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into I2Cx_ER_IRQHandler()
mbed_official 126:549ba18ddd81 959 * in order to determine which error occured.
mbed_official 126:549ba18ddd81 960 * - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()
mbed_official 126:549ba18ddd81 961 * and/or I2C_GenerateStop() in order to clear the error flag and source,
mbed_official 126:549ba18ddd81 962 * and return to correct communication status.
mbed_official 126:549ba18ddd81 963 *
mbed_official 126:549ba18ddd81 964 *
mbed_official 126:549ba18ddd81 965 * 2) Advanced state monitoring:
mbed_official 126:549ba18ddd81 966 * Using the function I2C_GetLastEvent() which returns the image of both status
mbed_official 126:549ba18ddd81 967 * registers in a single word (uint32_t) (Status Register 2 value is shifted left
mbed_official 126:549ba18ddd81 968 * by 16 bits and concatenated to Status Register 1).
mbed_official 126:549ba18ddd81 969 * - When to use:
mbed_official 126:549ba18ddd81 970 * - This function is suitable for the same applications above but it allows to
mbed_official 126:549ba18ddd81 971 * overcome the mentioned limitation of I2C_GetFlagStatus() function.
mbed_official 126:549ba18ddd81 972 * The returned value could be compared to events already defined in the
mbed_official 126:549ba18ddd81 973 * library (stm32f10x_i2c.h) or to custom values defined by user.
mbed_official 126:549ba18ddd81 974 * - This function is suitable when multiple flags are monitored at the same time.
mbed_official 126:549ba18ddd81 975 * - At the opposite of I2C_CheckEvent() function, this function allows user to
mbed_official 126:549ba18ddd81 976 * choose when an event is accepted (when all events flags are set and no
mbed_official 126:549ba18ddd81 977 * other flags are set or just when the needed flags are set like
mbed_official 126:549ba18ddd81 978 * I2C_CheckEvent() function).
mbed_official 126:549ba18ddd81 979 * - Limitations:
mbed_official 126:549ba18ddd81 980 * - User may need to define his own events.
mbed_official 126:549ba18ddd81 981 * - Same remark concerning the error management is applicable for this
mbed_official 126:549ba18ddd81 982 * function if user decides to check only regular communication flags (and
mbed_official 126:549ba18ddd81 983 * ignores error flags).
mbed_official 126:549ba18ddd81 984 *
mbed_official 126:549ba18ddd81 985 *
mbed_official 126:549ba18ddd81 986 * 3) Flag-based state monitoring:
mbed_official 126:549ba18ddd81 987 * Using the function I2C_GetFlagStatus() which simply returns the status of
mbed_official 126:549ba18ddd81 988 * one single flag (ie. I2C_FLAG_RXNE ...).
mbed_official 126:549ba18ddd81 989 * - When to use:
mbed_official 126:549ba18ddd81 990 * - This function could be used for specific applications or in debug phase.
mbed_official 126:549ba18ddd81 991 * - It is suitable when only one flag checking is needed (most I2C events
mbed_official 126:549ba18ddd81 992 * are monitored through multiple flags).
mbed_official 126:549ba18ddd81 993 * - Limitations:
mbed_official 126:549ba18ddd81 994 * - When calling this function, the Status register is accessed. Some flags are
mbed_official 126:549ba18ddd81 995 * cleared when the status register is accessed. So checking the status
mbed_official 126:549ba18ddd81 996 * of one Flag, may clear other ones.
mbed_official 126:549ba18ddd81 997 * - Function may need to be called twice or more in order to monitor one
mbed_official 126:549ba18ddd81 998 * single event.
mbed_official 126:549ba18ddd81 999 *
mbed_official 126:549ba18ddd81 1000 * For detailed description of Events, please refer to section I2C_Events in
mbed_official 126:549ba18ddd81 1001 * stm32f10x_i2c.h file.
mbed_official 126:549ba18ddd81 1002 *
mbed_official 126:549ba18ddd81 1003 */
mbed_official 126:549ba18ddd81 1004
mbed_official 126:549ba18ddd81 1005 /**
mbed_official 126:549ba18ddd81 1006 *
mbed_official 126:549ba18ddd81 1007 * 1) Basic state monitoring
mbed_official 126:549ba18ddd81 1008 *******************************************************************************
mbed_official 126:549ba18ddd81 1009 */
mbed_official 126:549ba18ddd81 1010
mbed_official 126:549ba18ddd81 1011 /**
mbed_official 126:549ba18ddd81 1012 * @brief Checks whether the last I2Cx Event is equal to the one passed
mbed_official 126:549ba18ddd81 1013 * as parameter.
mbed_official 126:549ba18ddd81 1014 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 1015 * @param I2C_EVENT: specifies the event to be checked.
mbed_official 126:549ba18ddd81 1016 * This parameter can be one of the following values:
mbed_official 126:549ba18ddd81 1017 * @arg I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED : EV1
mbed_official 126:549ba18ddd81 1018 * @arg I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED : EV1
mbed_official 126:549ba18ddd81 1019 * @arg I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED : EV1
mbed_official 126:549ba18ddd81 1020 * @arg I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED : EV1
mbed_official 126:549ba18ddd81 1021 * @arg I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED : EV1
mbed_official 126:549ba18ddd81 1022 * @arg I2C_EVENT_SLAVE_BYTE_RECEIVED : EV2
mbed_official 126:549ba18ddd81 1023 * @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF) : EV2
mbed_official 126:549ba18ddd81 1024 * @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL) : EV2
mbed_official 126:549ba18ddd81 1025 * @arg I2C_EVENT_SLAVE_BYTE_TRANSMITTED : EV3
mbed_official 126:549ba18ddd81 1026 * @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF) : EV3
mbed_official 126:549ba18ddd81 1027 * @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL) : EV3
mbed_official 126:549ba18ddd81 1028 * @arg I2C_EVENT_SLAVE_ACK_FAILURE : EV3_2
mbed_official 126:549ba18ddd81 1029 * @arg I2C_EVENT_SLAVE_STOP_DETECTED : EV4
mbed_official 126:549ba18ddd81 1030 * @arg I2C_EVENT_MASTER_MODE_SELECT : EV5
mbed_official 126:549ba18ddd81 1031 * @arg I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED : EV6
mbed_official 126:549ba18ddd81 1032 * @arg I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED : EV6
mbed_official 126:549ba18ddd81 1033 * @arg I2C_EVENT_MASTER_BYTE_RECEIVED : EV7
mbed_official 126:549ba18ddd81 1034 * @arg I2C_EVENT_MASTER_BYTE_TRANSMITTING : EV8
mbed_official 126:549ba18ddd81 1035 * @arg I2C_EVENT_MASTER_BYTE_TRANSMITTED : EV8_2
mbed_official 126:549ba18ddd81 1036 * @arg I2C_EVENT_MASTER_MODE_ADDRESS10 : EV9
mbed_official 126:549ba18ddd81 1037 *
mbed_official 126:549ba18ddd81 1038 * @note: For detailed description of Events, please refer to section
mbed_official 126:549ba18ddd81 1039 * I2C_Events in stm32f10x_i2c.h file.
mbed_official 126:549ba18ddd81 1040 *
mbed_official 126:549ba18ddd81 1041 * @retval An ErrorStatus enumeration value:
mbed_official 126:549ba18ddd81 1042 * - SUCCESS: Last event is equal to the I2C_EVENT
mbed_official 126:549ba18ddd81 1043 * - ERROR: Last event is different from the I2C_EVENT
mbed_official 126:549ba18ddd81 1044 */
mbed_official 126:549ba18ddd81 1045 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
mbed_official 126:549ba18ddd81 1046 {
mbed_official 126:549ba18ddd81 1047 uint32_t lastevent = 0;
mbed_official 126:549ba18ddd81 1048 uint32_t flag1 = 0, flag2 = 0;
mbed_official 126:549ba18ddd81 1049 ErrorStatus status = ERROR;
mbed_official 126:549ba18ddd81 1050
mbed_official 126:549ba18ddd81 1051 /* Check the parameters */
mbed_official 126:549ba18ddd81 1052 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 1053 assert_param(IS_I2C_EVENT(I2C_EVENT));
mbed_official 126:549ba18ddd81 1054
mbed_official 126:549ba18ddd81 1055 /* Read the I2Cx status register */
mbed_official 126:549ba18ddd81 1056 flag1 = I2Cx->SR1;
mbed_official 126:549ba18ddd81 1057 flag2 = I2Cx->SR2;
mbed_official 126:549ba18ddd81 1058 flag2 = flag2 << 16;
mbed_official 126:549ba18ddd81 1059
mbed_official 126:549ba18ddd81 1060 /* Get the last event value from I2C status register */
mbed_official 126:549ba18ddd81 1061 lastevent = (flag1 | flag2) & FLAG_Mask;
mbed_official 126:549ba18ddd81 1062
mbed_official 126:549ba18ddd81 1063 /* Check whether the last event contains the I2C_EVENT */
mbed_official 126:549ba18ddd81 1064 if ((lastevent & I2C_EVENT) == I2C_EVENT)
mbed_official 126:549ba18ddd81 1065 {
mbed_official 126:549ba18ddd81 1066 /* SUCCESS: last event is equal to I2C_EVENT */
mbed_official 126:549ba18ddd81 1067 status = SUCCESS;
mbed_official 126:549ba18ddd81 1068 }
mbed_official 126:549ba18ddd81 1069 else
mbed_official 126:549ba18ddd81 1070 {
mbed_official 126:549ba18ddd81 1071 /* ERROR: last event is different from I2C_EVENT */
mbed_official 126:549ba18ddd81 1072 status = ERROR;
mbed_official 126:549ba18ddd81 1073 }
mbed_official 126:549ba18ddd81 1074 /* Return status */
mbed_official 126:549ba18ddd81 1075 return status;
mbed_official 126:549ba18ddd81 1076 }
mbed_official 126:549ba18ddd81 1077
mbed_official 126:549ba18ddd81 1078 /**
mbed_official 126:549ba18ddd81 1079 *
mbed_official 126:549ba18ddd81 1080 * 2) Advanced state monitoring
mbed_official 126:549ba18ddd81 1081 *******************************************************************************
mbed_official 126:549ba18ddd81 1082 */
mbed_official 126:549ba18ddd81 1083
mbed_official 126:549ba18ddd81 1084 /**
mbed_official 126:549ba18ddd81 1085 * @brief Returns the last I2Cx Event.
mbed_official 126:549ba18ddd81 1086 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 1087 *
mbed_official 126:549ba18ddd81 1088 * @note: For detailed description of Events, please refer to section
mbed_official 126:549ba18ddd81 1089 * I2C_Events in stm32f10x_i2c.h file.
mbed_official 126:549ba18ddd81 1090 *
mbed_official 126:549ba18ddd81 1091 * @retval The last event
mbed_official 126:549ba18ddd81 1092 */
mbed_official 126:549ba18ddd81 1093 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)
mbed_official 126:549ba18ddd81 1094 {
mbed_official 126:549ba18ddd81 1095 uint32_t lastevent = 0;
mbed_official 126:549ba18ddd81 1096 uint32_t flag1 = 0, flag2 = 0;
mbed_official 126:549ba18ddd81 1097
mbed_official 126:549ba18ddd81 1098 /* Check the parameters */
mbed_official 126:549ba18ddd81 1099 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 1100
mbed_official 126:549ba18ddd81 1101 /* Read the I2Cx status register */
mbed_official 126:549ba18ddd81 1102 flag1 = I2Cx->SR1;
mbed_official 126:549ba18ddd81 1103 flag2 = I2Cx->SR2;
mbed_official 126:549ba18ddd81 1104 flag2 = flag2 << 16;
mbed_official 126:549ba18ddd81 1105
mbed_official 126:549ba18ddd81 1106 /* Get the last event value from I2C status register */
mbed_official 126:549ba18ddd81 1107 lastevent = (flag1 | flag2) & FLAG_Mask;
mbed_official 126:549ba18ddd81 1108
mbed_official 126:549ba18ddd81 1109 /* Return status */
mbed_official 126:549ba18ddd81 1110 return lastevent;
mbed_official 126:549ba18ddd81 1111 }
mbed_official 126:549ba18ddd81 1112
mbed_official 126:549ba18ddd81 1113 /**
mbed_official 126:549ba18ddd81 1114 *
mbed_official 126:549ba18ddd81 1115 * 3) Flag-based state monitoring
mbed_official 126:549ba18ddd81 1116 *******************************************************************************
mbed_official 126:549ba18ddd81 1117 */
mbed_official 126:549ba18ddd81 1118
mbed_official 126:549ba18ddd81 1119 /**
mbed_official 126:549ba18ddd81 1120 * @brief Checks whether the specified I2C flag is set or not.
mbed_official 126:549ba18ddd81 1121 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 1122 * @param I2C_FLAG: specifies the flag to check.
mbed_official 126:549ba18ddd81 1123 * This parameter can be one of the following values:
mbed_official 126:549ba18ddd81 1124 * @arg I2C_FLAG_DUALF: Dual flag (Slave mode)
mbed_official 126:549ba18ddd81 1125 * @arg I2C_FLAG_SMBHOST: SMBus host header (Slave mode)
mbed_official 126:549ba18ddd81 1126 * @arg I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)
mbed_official 126:549ba18ddd81 1127 * @arg I2C_FLAG_GENCALL: General call header flag (Slave mode)
mbed_official 126:549ba18ddd81 1128 * @arg I2C_FLAG_TRA: Transmitter/Receiver flag
mbed_official 126:549ba18ddd81 1129 * @arg I2C_FLAG_BUSY: Bus busy flag
mbed_official 126:549ba18ddd81 1130 * @arg I2C_FLAG_MSL: Master/Slave flag
mbed_official 126:549ba18ddd81 1131 * @arg I2C_FLAG_SMBALERT: SMBus Alert flag
mbed_official 126:549ba18ddd81 1132 * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
mbed_official 126:549ba18ddd81 1133 * @arg I2C_FLAG_PECERR: PEC error in reception flag
mbed_official 126:549ba18ddd81 1134 * @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
mbed_official 126:549ba18ddd81 1135 * @arg I2C_FLAG_AF: Acknowledge failure flag
mbed_official 126:549ba18ddd81 1136 * @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
mbed_official 126:549ba18ddd81 1137 * @arg I2C_FLAG_BERR: Bus error flag
mbed_official 126:549ba18ddd81 1138 * @arg I2C_FLAG_TXE: Data register empty flag (Transmitter)
mbed_official 126:549ba18ddd81 1139 * @arg I2C_FLAG_RXNE: Data register not empty (Receiver) flag
mbed_official 126:549ba18ddd81 1140 * @arg I2C_FLAG_STOPF: Stop detection flag (Slave mode)
mbed_official 126:549ba18ddd81 1141 * @arg I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)
mbed_official 126:549ba18ddd81 1142 * @arg I2C_FLAG_BTF: Byte transfer finished flag
mbed_official 126:549ba18ddd81 1143 * @arg I2C_FLAG_ADDR: Address sent flag (Master mode) "ADSL"
mbed_official 126:549ba18ddd81 1144 * Address matched flag (Slave mode)"ENDA"
mbed_official 126:549ba18ddd81 1145 * @arg I2C_FLAG_SB: Start bit flag (Master mode)
mbed_official 126:549ba18ddd81 1146 * @retval The new state of I2C_FLAG (SET or RESET).
mbed_official 126:549ba18ddd81 1147 */
mbed_official 126:549ba18ddd81 1148 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
mbed_official 126:549ba18ddd81 1149 {
mbed_official 126:549ba18ddd81 1150 FlagStatus bitstatus = RESET;
mbed_official 126:549ba18ddd81 1151 __IO uint32_t i2creg = 0, i2cxbase = 0;
mbed_official 126:549ba18ddd81 1152
mbed_official 126:549ba18ddd81 1153 /* Check the parameters */
mbed_official 126:549ba18ddd81 1154 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 1155 assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
mbed_official 126:549ba18ddd81 1156
mbed_official 126:549ba18ddd81 1157 /* Get the I2Cx peripheral base address */
mbed_official 126:549ba18ddd81 1158 i2cxbase = (uint32_t)I2Cx;
mbed_official 126:549ba18ddd81 1159
mbed_official 126:549ba18ddd81 1160 /* Read flag register index */
mbed_official 126:549ba18ddd81 1161 i2creg = I2C_FLAG >> 28;
mbed_official 126:549ba18ddd81 1162
mbed_official 126:549ba18ddd81 1163 /* Get bit[23:0] of the flag */
mbed_official 126:549ba18ddd81 1164 I2C_FLAG &= FLAG_Mask;
mbed_official 126:549ba18ddd81 1165
mbed_official 126:549ba18ddd81 1166 if(i2creg != 0)
mbed_official 126:549ba18ddd81 1167 {
mbed_official 126:549ba18ddd81 1168 /* Get the I2Cx SR1 register address */
mbed_official 126:549ba18ddd81 1169 i2cxbase += 0x14;
mbed_official 126:549ba18ddd81 1170 }
mbed_official 126:549ba18ddd81 1171 else
mbed_official 126:549ba18ddd81 1172 {
mbed_official 126:549ba18ddd81 1173 /* Flag in I2Cx SR2 Register */
mbed_official 126:549ba18ddd81 1174 I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
mbed_official 126:549ba18ddd81 1175 /* Get the I2Cx SR2 register address */
mbed_official 126:549ba18ddd81 1176 i2cxbase += 0x18;
mbed_official 126:549ba18ddd81 1177 }
mbed_official 126:549ba18ddd81 1178
mbed_official 126:549ba18ddd81 1179 if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
mbed_official 126:549ba18ddd81 1180 {
mbed_official 126:549ba18ddd81 1181 /* I2C_FLAG is set */
mbed_official 126:549ba18ddd81 1182 bitstatus = SET;
mbed_official 126:549ba18ddd81 1183 }
mbed_official 126:549ba18ddd81 1184 else
mbed_official 126:549ba18ddd81 1185 {
mbed_official 126:549ba18ddd81 1186 /* I2C_FLAG is reset */
mbed_official 126:549ba18ddd81 1187 bitstatus = RESET;
mbed_official 126:549ba18ddd81 1188 }
mbed_official 126:549ba18ddd81 1189
mbed_official 126:549ba18ddd81 1190 /* Return the I2C_FLAG status */
mbed_official 126:549ba18ddd81 1191 return bitstatus;
mbed_official 126:549ba18ddd81 1192 }
mbed_official 126:549ba18ddd81 1193
mbed_official 126:549ba18ddd81 1194
mbed_official 126:549ba18ddd81 1195
mbed_official 126:549ba18ddd81 1196 /**
mbed_official 126:549ba18ddd81 1197 * @brief Clears the I2Cx's pending flags.
mbed_official 126:549ba18ddd81 1198 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 1199 * @param I2C_FLAG: specifies the flag to clear.
mbed_official 126:549ba18ddd81 1200 * This parameter can be any combination of the following values:
mbed_official 126:549ba18ddd81 1201 * @arg I2C_FLAG_SMBALERT: SMBus Alert flag
mbed_official 126:549ba18ddd81 1202 * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
mbed_official 126:549ba18ddd81 1203 * @arg I2C_FLAG_PECERR: PEC error in reception flag
mbed_official 126:549ba18ddd81 1204 * @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
mbed_official 126:549ba18ddd81 1205 * @arg I2C_FLAG_AF: Acknowledge failure flag
mbed_official 126:549ba18ddd81 1206 * @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
mbed_official 126:549ba18ddd81 1207 * @arg I2C_FLAG_BERR: Bus error flag
mbed_official 126:549ba18ddd81 1208 *
mbed_official 126:549ba18ddd81 1209 * @note
mbed_official 126:549ba18ddd81 1210 * - STOPF (STOP detection) is cleared by software sequence: a read operation
mbed_official 126:549ba18ddd81 1211 * to I2C_SR1 register (I2C_GetFlagStatus()) followed by a write operation
mbed_official 126:549ba18ddd81 1212 * to I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).
mbed_official 126:549ba18ddd81 1213 * - ADD10 (10-bit header sent) is cleared by software sequence: a read
mbed_official 126:549ba18ddd81 1214 * operation to I2C_SR1 (I2C_GetFlagStatus()) followed by writing the
mbed_official 126:549ba18ddd81 1215 * second byte of the address in DR register.
mbed_official 126:549ba18ddd81 1216 * - BTF (Byte Transfer Finished) is cleared by software sequence: a read
mbed_official 126:549ba18ddd81 1217 * operation to I2C_SR1 register (I2C_GetFlagStatus()) followed by a
mbed_official 126:549ba18ddd81 1218 * read/write to I2C_DR register (I2C_SendData()).
mbed_official 126:549ba18ddd81 1219 * - ADDR (Address sent) is cleared by software sequence: a read operation to
mbed_official 126:549ba18ddd81 1220 * I2C_SR1 register (I2C_GetFlagStatus()) followed by a read operation to
mbed_official 126:549ba18ddd81 1221 * I2C_SR2 register ((void)(I2Cx->SR2)).
mbed_official 126:549ba18ddd81 1222 * - SB (Start Bit) is cleared software sequence: a read operation to I2C_SR1
mbed_official 126:549ba18ddd81 1223 * register (I2C_GetFlagStatus()) followed by a write operation to I2C_DR
mbed_official 126:549ba18ddd81 1224 * register (I2C_SendData()).
mbed_official 126:549ba18ddd81 1225 * @retval None
mbed_official 126:549ba18ddd81 1226 */
mbed_official 126:549ba18ddd81 1227 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
mbed_official 126:549ba18ddd81 1228 {
mbed_official 126:549ba18ddd81 1229 uint32_t flagpos = 0;
mbed_official 126:549ba18ddd81 1230 /* Check the parameters */
mbed_official 126:549ba18ddd81 1231 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 1232 assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
mbed_official 126:549ba18ddd81 1233 /* Get the I2C flag position */
mbed_official 126:549ba18ddd81 1234 flagpos = I2C_FLAG & FLAG_Mask;
mbed_official 126:549ba18ddd81 1235 /* Clear the selected I2C flag */
mbed_official 126:549ba18ddd81 1236 I2Cx->SR1 = (uint16_t)~flagpos;
mbed_official 126:549ba18ddd81 1237 }
mbed_official 126:549ba18ddd81 1238
mbed_official 126:549ba18ddd81 1239 /**
mbed_official 126:549ba18ddd81 1240 * @brief Checks whether the specified I2C interrupt has occurred or not.
mbed_official 126:549ba18ddd81 1241 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 1242 * @param I2C_IT: specifies the interrupt source to check.
mbed_official 126:549ba18ddd81 1243 * This parameter can be one of the following values:
mbed_official 126:549ba18ddd81 1244 * @arg I2C_IT_SMBALERT: SMBus Alert flag
mbed_official 126:549ba18ddd81 1245 * @arg I2C_IT_TIMEOUT: Timeout or Tlow error flag
mbed_official 126:549ba18ddd81 1246 * @arg I2C_IT_PECERR: PEC error in reception flag
mbed_official 126:549ba18ddd81 1247 * @arg I2C_IT_OVR: Overrun/Underrun flag (Slave mode)
mbed_official 126:549ba18ddd81 1248 * @arg I2C_IT_AF: Acknowledge failure flag
mbed_official 126:549ba18ddd81 1249 * @arg I2C_IT_ARLO: Arbitration lost flag (Master mode)
mbed_official 126:549ba18ddd81 1250 * @arg I2C_IT_BERR: Bus error flag
mbed_official 126:549ba18ddd81 1251 * @arg I2C_IT_TXE: Data register empty flag (Transmitter)
mbed_official 126:549ba18ddd81 1252 * @arg I2C_IT_RXNE: Data register not empty (Receiver) flag
mbed_official 126:549ba18ddd81 1253 * @arg I2C_IT_STOPF: Stop detection flag (Slave mode)
mbed_official 126:549ba18ddd81 1254 * @arg I2C_IT_ADD10: 10-bit header sent flag (Master mode)
mbed_official 126:549ba18ddd81 1255 * @arg I2C_IT_BTF: Byte transfer finished flag
mbed_official 126:549ba18ddd81 1256 * @arg I2C_IT_ADDR: Address sent flag (Master mode) "ADSL"
mbed_official 126:549ba18ddd81 1257 * Address matched flag (Slave mode)"ENDAD"
mbed_official 126:549ba18ddd81 1258 * @arg I2C_IT_SB: Start bit flag (Master mode)
mbed_official 126:549ba18ddd81 1259 * @retval The new state of I2C_IT (SET or RESET).
mbed_official 126:549ba18ddd81 1260 */
mbed_official 126:549ba18ddd81 1261 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
mbed_official 126:549ba18ddd81 1262 {
mbed_official 126:549ba18ddd81 1263 ITStatus bitstatus = RESET;
mbed_official 126:549ba18ddd81 1264 uint32_t enablestatus = 0;
mbed_official 126:549ba18ddd81 1265
mbed_official 126:549ba18ddd81 1266 /* Check the parameters */
mbed_official 126:549ba18ddd81 1267 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 1268 assert_param(IS_I2C_GET_IT(I2C_IT));
mbed_official 126:549ba18ddd81 1269
mbed_official 126:549ba18ddd81 1270 /* Check if the interrupt source is enabled or not */
mbed_official 126:549ba18ddd81 1271 enablestatus = (uint32_t)(((I2C_IT & ITEN_Mask) >> 16) & (I2Cx->CR2)) ;
mbed_official 126:549ba18ddd81 1272
mbed_official 126:549ba18ddd81 1273 /* Get bit[23:0] of the flag */
mbed_official 126:549ba18ddd81 1274 I2C_IT &= FLAG_Mask;
mbed_official 126:549ba18ddd81 1275
mbed_official 126:549ba18ddd81 1276 /* Check the status of the specified I2C flag */
mbed_official 126:549ba18ddd81 1277 if (((I2Cx->SR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)
mbed_official 126:549ba18ddd81 1278 {
mbed_official 126:549ba18ddd81 1279 /* I2C_IT is set */
mbed_official 126:549ba18ddd81 1280 bitstatus = SET;
mbed_official 126:549ba18ddd81 1281 }
mbed_official 126:549ba18ddd81 1282 else
mbed_official 126:549ba18ddd81 1283 {
mbed_official 126:549ba18ddd81 1284 /* I2C_IT is reset */
mbed_official 126:549ba18ddd81 1285 bitstatus = RESET;
mbed_official 126:549ba18ddd81 1286 }
mbed_official 126:549ba18ddd81 1287 /* Return the I2C_IT status */
mbed_official 126:549ba18ddd81 1288 return bitstatus;
mbed_official 126:549ba18ddd81 1289 }
mbed_official 126:549ba18ddd81 1290
mbed_official 126:549ba18ddd81 1291 /**
mbed_official 126:549ba18ddd81 1292 * @brief Clears the I2Cx’s interrupt pending bits.
mbed_official 126:549ba18ddd81 1293 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 126:549ba18ddd81 1294 * @param I2C_IT: specifies the interrupt pending bit to clear.
mbed_official 126:549ba18ddd81 1295 * This parameter can be any combination of the following values:
mbed_official 126:549ba18ddd81 1296 * @arg I2C_IT_SMBALERT: SMBus Alert interrupt
mbed_official 126:549ba18ddd81 1297 * @arg I2C_IT_TIMEOUT: Timeout or Tlow error interrupt
mbed_official 126:549ba18ddd81 1298 * @arg I2C_IT_PECERR: PEC error in reception interrupt
mbed_official 126:549ba18ddd81 1299 * @arg I2C_IT_OVR: Overrun/Underrun interrupt (Slave mode)
mbed_official 126:549ba18ddd81 1300 * @arg I2C_IT_AF: Acknowledge failure interrupt
mbed_official 126:549ba18ddd81 1301 * @arg I2C_IT_ARLO: Arbitration lost interrupt (Master mode)
mbed_official 126:549ba18ddd81 1302 * @arg I2C_IT_BERR: Bus error interrupt
mbed_official 126:549ba18ddd81 1303 *
mbed_official 126:549ba18ddd81 1304 * @note
mbed_official 126:549ba18ddd81 1305 * - STOPF (STOP detection) is cleared by software sequence: a read operation
mbed_official 126:549ba18ddd81 1306 * to I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to
mbed_official 126:549ba18ddd81 1307 * I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).
mbed_official 126:549ba18ddd81 1308 * - ADD10 (10-bit header sent) is cleared by software sequence: a read
mbed_official 126:549ba18ddd81 1309 * operation to I2C_SR1 (I2C_GetITStatus()) followed by writing the second
mbed_official 126:549ba18ddd81 1310 * byte of the address in I2C_DR register.
mbed_official 126:549ba18ddd81 1311 * - BTF (Byte Transfer Finished) is cleared by software sequence: a read
mbed_official 126:549ba18ddd81 1312 * operation to I2C_SR1 register (I2C_GetITStatus()) followed by a
mbed_official 126:549ba18ddd81 1313 * read/write to I2C_DR register (I2C_SendData()).
mbed_official 126:549ba18ddd81 1314 * - ADDR (Address sent) is cleared by software sequence: a read operation to
mbed_official 126:549ba18ddd81 1315 * I2C_SR1 register (I2C_GetITStatus()) followed by a read operation to
mbed_official 126:549ba18ddd81 1316 * I2C_SR2 register ((void)(I2Cx->SR2)).
mbed_official 126:549ba18ddd81 1317 * - SB (Start Bit) is cleared by software sequence: a read operation to
mbed_official 126:549ba18ddd81 1318 * I2C_SR1 register (I2C_GetITStatus()) followed by a write operation to
mbed_official 126:549ba18ddd81 1319 * I2C_DR register (I2C_SendData()).
mbed_official 126:549ba18ddd81 1320 * @retval None
mbed_official 126:549ba18ddd81 1321 */
mbed_official 126:549ba18ddd81 1322 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
mbed_official 126:549ba18ddd81 1323 {
mbed_official 126:549ba18ddd81 1324 uint32_t flagpos = 0;
mbed_official 126:549ba18ddd81 1325 /* Check the parameters */
mbed_official 126:549ba18ddd81 1326 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 126:549ba18ddd81 1327 assert_param(IS_I2C_CLEAR_IT(I2C_IT));
mbed_official 126:549ba18ddd81 1328 /* Get the I2C flag position */
mbed_official 126:549ba18ddd81 1329 flagpos = I2C_IT & FLAG_Mask;
mbed_official 126:549ba18ddd81 1330 /* Clear the selected I2C flag */
mbed_official 126:549ba18ddd81 1331 I2Cx->SR1 = (uint16_t)~flagpos;
mbed_official 126:549ba18ddd81 1332 }
mbed_official 126:549ba18ddd81 1333
mbed_official 126:549ba18ddd81 1334 /**
mbed_official 126:549ba18ddd81 1335 * @}
mbed_official 126:549ba18ddd81 1336 */
mbed_official 126:549ba18ddd81 1337
mbed_official 126:549ba18ddd81 1338 /**
mbed_official 126:549ba18ddd81 1339 * @}
mbed_official 126:549ba18ddd81 1340 */
mbed_official 126:549ba18ddd81 1341
mbed_official 126:549ba18ddd81 1342 /**
mbed_official 126:549ba18ddd81 1343 * @}
mbed_official 126:549ba18ddd81 1344 */
mbed_official 126:549ba18ddd81 1345
mbed_official 126:549ba18ddd81 1346 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/