mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jan 07 11:00:05 2014 +0000
Revision:
70:c1fbde68b492
Parent:
52:a51c77007319
Child:
84:f54042cbc282
Synchronized with git revision 3f438a307904431f2782db3c8fa49946b9fc1d85

Full URL: https://github.com/mbedmicro/mbed/commit/3f438a307904431f2782db3c8fa49946b9fc1d85/

[NUCLEO_F103RB] license text changed + sleep hal updated

Who changed what in which revision?

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