mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Mar 21 11:45:09 2014 +0000
Revision:
130:1dec54e4aec3
Synchronized with git revision e5c9ff6781a4e277a5a454e5a0b037f76e31739d

Full URL: https://github.com/mbedmicro/mbed/commit/e5c9ff6781a4e277a5a454e5a0b037f76e31739d/

STM32F0-Discovery (STM32F051R8) initial port

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 130:1dec54e4aec3 1 /**
mbed_official 130:1dec54e4aec3 2 ******************************************************************************
mbed_official 130:1dec54e4aec3 3 * @file stm32f0xx_i2c.c
mbed_official 130:1dec54e4aec3 4 * @author MCD Application Team
mbed_official 130:1dec54e4aec3 5 * @version V1.3.0
mbed_official 130:1dec54e4aec3 6 * @date 16-January-2014
mbed_official 130:1dec54e4aec3 7 * @brief This file provides firmware functions to manage the following
mbed_official 130:1dec54e4aec3 8 * functionalities of the Inter-Integrated circuit (I2C):
mbed_official 130:1dec54e4aec3 9 * + Initialization and Configuration
mbed_official 130:1dec54e4aec3 10 * + Communications handling
mbed_official 130:1dec54e4aec3 11 * + SMBUS management
mbed_official 130:1dec54e4aec3 12 * + I2C registers management
mbed_official 130:1dec54e4aec3 13 * + Data transfers management
mbed_official 130:1dec54e4aec3 14 * + DMA transfers management
mbed_official 130:1dec54e4aec3 15 * + Interrupts and flags management
mbed_official 130:1dec54e4aec3 16 *
mbed_official 130:1dec54e4aec3 17 * @verbatim
mbed_official 130:1dec54e4aec3 18 ============================================================================
mbed_official 130:1dec54e4aec3 19 ##### How to use this driver #####
mbed_official 130:1dec54e4aec3 20 ============================================================================
mbed_official 130:1dec54e4aec3 21 [..]
mbed_official 130:1dec54e4aec3 22 (#) Enable peripheral clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE)
mbed_official 130:1dec54e4aec3 23 function for I2C1 or I2C2.
mbed_official 130:1dec54e4aec3 24 (#) Enable SDA, SCL and SMBA (when used) GPIO clocks using
mbed_official 130:1dec54e4aec3 25 RCC_AHBPeriphClockCmd() function.
mbed_official 130:1dec54e4aec3 26 (#) Peripherals alternate function:
mbed_official 130:1dec54e4aec3 27 (++) Connect the pin to the desired peripherals' Alternate
mbed_official 130:1dec54e4aec3 28 Function (AF) using GPIO_PinAFConfig() function.
mbed_official 130:1dec54e4aec3 29 (++) Configure the desired pin in alternate function by:
mbed_official 130:1dec54e4aec3 30 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
mbed_official 130:1dec54e4aec3 31 (++) Select the type, OpenDrain and speed via
mbed_official 130:1dec54e4aec3 32 GPIO_PuPd, GPIO_OType and GPIO_Speed members
mbed_official 130:1dec54e4aec3 33 (++) Call GPIO_Init() function.
mbed_official 130:1dec54e4aec3 34 (#) Program the Mode, Timing , Own address, Ack and Acknowledged Address
mbed_official 130:1dec54e4aec3 35 using the I2C_Init() function.
mbed_official 130:1dec54e4aec3 36 (#) Optionally you can enable/configure the following parameters without
mbed_official 130:1dec54e4aec3 37 re-initialization (i.e there is no need to call again I2C_Init() function):
mbed_official 130:1dec54e4aec3 38 (++) Enable the acknowledge feature using I2C_AcknowledgeConfig() function.
mbed_official 130:1dec54e4aec3 39 (++) Enable the dual addressing mode using I2C_DualAddressCmd() function.
mbed_official 130:1dec54e4aec3 40 (++) Enable the general call using the I2C_GeneralCallCmd() function.
mbed_official 130:1dec54e4aec3 41 (++) Enable the clock stretching using I2C_StretchClockCmd() function.
mbed_official 130:1dec54e4aec3 42 (++) Enable the PEC Calculation using I2C_CalculatePEC() function.
mbed_official 130:1dec54e4aec3 43 (++) For SMBus Mode:
mbed_official 130:1dec54e4aec3 44 (+++) Enable the SMBusAlert pin using I2C_SMBusAlertCmd() function.
mbed_official 130:1dec54e4aec3 45 (#) Enable the NVIC and the corresponding interrupt using the function
mbed_official 130:1dec54e4aec3 46 I2C_ITConfig() if you need to use interrupt mode.
mbed_official 130:1dec54e4aec3 47 (#) When using the DMA mode
mbed_official 130:1dec54e4aec3 48 (++) Configure the DMA using DMA_Init() function.
mbed_official 130:1dec54e4aec3 49 (++) Active the needed channel Request using I2C_DMACmd() function.
mbed_official 130:1dec54e4aec3 50 (#) Enable the I2C using the I2C_Cmd() function.
mbed_official 130:1dec54e4aec3 51 (#) Enable the DMA using the DMA_Cmd() function when using DMA mode in the
mbed_official 130:1dec54e4aec3 52 transfers.
mbed_official 130:1dec54e4aec3 53 [..]
mbed_official 130:1dec54e4aec3 54 (@) When using I2C in Fast Mode Plus, SCL and SDA pin 20mA current drive capability
mbed_official 130:1dec54e4aec3 55 must be enabled by setting the driving capability control bit in SYSCFG.
mbed_official 130:1dec54e4aec3 56
mbed_official 130:1dec54e4aec3 57 @endverbatim
mbed_official 130:1dec54e4aec3 58 ******************************************************************************
mbed_official 130:1dec54e4aec3 59 * @attention
mbed_official 130:1dec54e4aec3 60 *
mbed_official 130:1dec54e4aec3 61 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 130:1dec54e4aec3 62 *
mbed_official 130:1dec54e4aec3 63 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 130:1dec54e4aec3 64 * are permitted provided that the following conditions are met:
mbed_official 130:1dec54e4aec3 65 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 130:1dec54e4aec3 66 * this list of conditions and the following disclaimer.
mbed_official 130:1dec54e4aec3 67 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 130:1dec54e4aec3 68 * this list of conditions and the following disclaimer in the documentation
mbed_official 130:1dec54e4aec3 69 * and/or other materials provided with the distribution.
mbed_official 130:1dec54e4aec3 70 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 130:1dec54e4aec3 71 * may be used to endorse or promote products derived from this software
mbed_official 130:1dec54e4aec3 72 * without specific prior written permission.
mbed_official 130:1dec54e4aec3 73 *
mbed_official 130:1dec54e4aec3 74 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 130:1dec54e4aec3 75 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 130:1dec54e4aec3 76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 130:1dec54e4aec3 77 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 130:1dec54e4aec3 78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 130:1dec54e4aec3 79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 130:1dec54e4aec3 80 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 130:1dec54e4aec3 81 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 130:1dec54e4aec3 82 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 130:1dec54e4aec3 83 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 130:1dec54e4aec3 84 *
mbed_official 130:1dec54e4aec3 85 ******************************************************************************
mbed_official 130:1dec54e4aec3 86 */
mbed_official 130:1dec54e4aec3 87
mbed_official 130:1dec54e4aec3 88 /* Includes ------------------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 89 #include "stm32f0xx_i2c.h"
mbed_official 130:1dec54e4aec3 90 #include "stm32f0xx_rcc.h"
mbed_official 130:1dec54e4aec3 91
mbed_official 130:1dec54e4aec3 92 /** @addtogroup STM32F0xx_StdPeriph_Driver
mbed_official 130:1dec54e4aec3 93 * @{
mbed_official 130:1dec54e4aec3 94 */
mbed_official 130:1dec54e4aec3 95
mbed_official 130:1dec54e4aec3 96 /** @defgroup I2C
mbed_official 130:1dec54e4aec3 97 * @brief I2C driver modules
mbed_official 130:1dec54e4aec3 98 * @{
mbed_official 130:1dec54e4aec3 99 */
mbed_official 130:1dec54e4aec3 100
mbed_official 130:1dec54e4aec3 101 /* Private typedef -----------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 102 /* Private define ------------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 103
mbed_official 130:1dec54e4aec3 104 #define CR1_CLEAR_MASK ((uint32_t)0x00CFE0FF) /*<! I2C CR1 clear register Mask */
mbed_official 130:1dec54e4aec3 105 #define CR2_CLEAR_MASK ((uint32_t)0x07FF7FFF) /*<! I2C CR2 clear register Mask */
mbed_official 130:1dec54e4aec3 106 #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! I2C TIMING clear register Mask */
mbed_official 130:1dec54e4aec3 107 #define ERROR_IT_MASK ((uint32_t)0x00003F00) /*<! I2C Error interrupt register Mask */
mbed_official 130:1dec54e4aec3 108 #define TC_IT_MASK ((uint32_t)0x000000C0) /*<! I2C TC interrupt register Mask */
mbed_official 130:1dec54e4aec3 109
mbed_official 130:1dec54e4aec3 110 /* Private macro -------------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 111 /* Private variables ---------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 112 /* Private function prototypes -----------------------------------------------*/
mbed_official 130:1dec54e4aec3 113 /* Private functions ---------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 114
mbed_official 130:1dec54e4aec3 115 /** @defgroup I2C_Private_Functions
mbed_official 130:1dec54e4aec3 116 * @{
mbed_official 130:1dec54e4aec3 117 */
mbed_official 130:1dec54e4aec3 118
mbed_official 130:1dec54e4aec3 119
mbed_official 130:1dec54e4aec3 120 /** @defgroup I2C_Group1 Initialization and Configuration functions
mbed_official 130:1dec54e4aec3 121 * @brief Initialization and Configuration functions
mbed_official 130:1dec54e4aec3 122 *
mbed_official 130:1dec54e4aec3 123 @verbatim
mbed_official 130:1dec54e4aec3 124 ===============================================================================
mbed_official 130:1dec54e4aec3 125 ##### Initialization and Configuration functions #####
mbed_official 130:1dec54e4aec3 126 ===============================================================================
mbed_official 130:1dec54e4aec3 127 [..] This section provides a set of functions allowing to initialize the I2C Mode,
mbed_official 130:1dec54e4aec3 128 I2C Timing, I2C filters, I2C Addressing mode, I2C OwnAddress1.
mbed_official 130:1dec54e4aec3 129
mbed_official 130:1dec54e4aec3 130 [..] The I2C_Init() function follows the I2C configuration procedures (these procedures
mbed_official 130:1dec54e4aec3 131 are available in reference manual).
mbed_official 130:1dec54e4aec3 132
mbed_official 130:1dec54e4aec3 133 [..] When the Software Reset is performed using I2C_SoftwareResetCmd() function, the internal
mbed_official 130:1dec54e4aec3 134 states machines are reset and communication control bits, as well as status bits come
mbed_official 130:1dec54e4aec3 135 back to their reset value.
mbed_official 130:1dec54e4aec3 136
mbed_official 130:1dec54e4aec3 137 [..] Before enabling Stop mode using I2C_StopModeCmd() I2C Clock source must be set to
mbed_official 130:1dec54e4aec3 138 HSI and Digital filters must be disabled.
mbed_official 130:1dec54e4aec3 139
mbed_official 130:1dec54e4aec3 140 [..] Before enabling Own Address 2 via I2C_DualAddressCmd() function, OA2 and mask should be
mbed_official 130:1dec54e4aec3 141 configured using I2C_OwnAddress2Config() function.
mbed_official 130:1dec54e4aec3 142
mbed_official 130:1dec54e4aec3 143 [..] I2C_SlaveByteControlCmd() enable Slave byte control that allow user to get control of
mbed_official 130:1dec54e4aec3 144 each byte in slave mode when NBYTES is set to 0x01.
mbed_official 130:1dec54e4aec3 145
mbed_official 130:1dec54e4aec3 146 @endverbatim
mbed_official 130:1dec54e4aec3 147 * @{
mbed_official 130:1dec54e4aec3 148 */
mbed_official 130:1dec54e4aec3 149
mbed_official 130:1dec54e4aec3 150 /**
mbed_official 130:1dec54e4aec3 151 * @brief Deinitializes the I2Cx peripheral registers to their default reset values.
mbed_official 130:1dec54e4aec3 152 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 153 * @retval None
mbed_official 130:1dec54e4aec3 154 */
mbed_official 130:1dec54e4aec3 155 void I2C_DeInit(I2C_TypeDef* I2Cx)
mbed_official 130:1dec54e4aec3 156 {
mbed_official 130:1dec54e4aec3 157 /* Check the parameters */
mbed_official 130:1dec54e4aec3 158 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 159
mbed_official 130:1dec54e4aec3 160 if (I2Cx == I2C1)
mbed_official 130:1dec54e4aec3 161 {
mbed_official 130:1dec54e4aec3 162 /* Enable I2C1 reset state */
mbed_official 130:1dec54e4aec3 163 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
mbed_official 130:1dec54e4aec3 164 /* Release I2C1 from reset state */
mbed_official 130:1dec54e4aec3 165 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
mbed_official 130:1dec54e4aec3 166 }
mbed_official 130:1dec54e4aec3 167 else
mbed_official 130:1dec54e4aec3 168 {
mbed_official 130:1dec54e4aec3 169 /* Enable I2C2 reset state */
mbed_official 130:1dec54e4aec3 170 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
mbed_official 130:1dec54e4aec3 171 /* Release I2C2 from reset state */
mbed_official 130:1dec54e4aec3 172 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
mbed_official 130:1dec54e4aec3 173 }
mbed_official 130:1dec54e4aec3 174 }
mbed_official 130:1dec54e4aec3 175
mbed_official 130:1dec54e4aec3 176 /**
mbed_official 130:1dec54e4aec3 177 * @brief Initializes the I2Cx peripheral according to the specified
mbed_official 130:1dec54e4aec3 178 * parameters in the I2C_InitStruct.
mbed_official 130:1dec54e4aec3 179 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 180 * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
mbed_official 130:1dec54e4aec3 181 * contains the configuration information for the specified I2C peripheral.
mbed_official 130:1dec54e4aec3 182 * @retval None
mbed_official 130:1dec54e4aec3 183 */
mbed_official 130:1dec54e4aec3 184 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
mbed_official 130:1dec54e4aec3 185 {
mbed_official 130:1dec54e4aec3 186 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 187
mbed_official 130:1dec54e4aec3 188 /* Check the parameters */
mbed_official 130:1dec54e4aec3 189 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 190 assert_param(IS_I2C_ANALOG_FILTER(I2C_InitStruct->I2C_AnalogFilter));
mbed_official 130:1dec54e4aec3 191 assert_param(IS_I2C_DIGITAL_FILTER(I2C_InitStruct->I2C_DigitalFilter));
mbed_official 130:1dec54e4aec3 192 assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
mbed_official 130:1dec54e4aec3 193 assert_param(IS_I2C_OWN_ADDRESS1(I2C_InitStruct->I2C_OwnAddress1));
mbed_official 130:1dec54e4aec3 194 assert_param(IS_I2C_ACK(I2C_InitStruct->I2C_Ack));
mbed_official 130:1dec54e4aec3 195 assert_param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AcknowledgedAddress));
mbed_official 130:1dec54e4aec3 196
mbed_official 130:1dec54e4aec3 197 /* Disable I2Cx Peripheral */
mbed_official 130:1dec54e4aec3 198 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
mbed_official 130:1dec54e4aec3 199
mbed_official 130:1dec54e4aec3 200 /*---------------------------- I2Cx FILTERS Configuration ------------------*/
mbed_official 130:1dec54e4aec3 201 /* Get the I2Cx CR1 value */
mbed_official 130:1dec54e4aec3 202 tmpreg = I2Cx->CR1;
mbed_official 130:1dec54e4aec3 203 /* Clear I2Cx CR1 register */
mbed_official 130:1dec54e4aec3 204 tmpreg &= CR1_CLEAR_MASK;
mbed_official 130:1dec54e4aec3 205 /* Configure I2Cx: analog and digital filter */
mbed_official 130:1dec54e4aec3 206 /* Set ANFOFF bit according to I2C_AnalogFilter value */
mbed_official 130:1dec54e4aec3 207 /* Set DFN bits according to I2C_DigitalFilter value */
mbed_official 130:1dec54e4aec3 208 tmpreg |= (uint32_t)I2C_InitStruct->I2C_AnalogFilter |(I2C_InitStruct->I2C_DigitalFilter << 8);
mbed_official 130:1dec54e4aec3 209
mbed_official 130:1dec54e4aec3 210 /* Write to I2Cx CR1 */
mbed_official 130:1dec54e4aec3 211 I2Cx->CR1 = tmpreg;
mbed_official 130:1dec54e4aec3 212
mbed_official 130:1dec54e4aec3 213 /*---------------------------- I2Cx TIMING Configuration -------------------*/
mbed_official 130:1dec54e4aec3 214 /* Configure I2Cx: Timing */
mbed_official 130:1dec54e4aec3 215 /* Set TIMINGR bits according to I2C_Timing */
mbed_official 130:1dec54e4aec3 216 /* Write to I2Cx TIMING */
mbed_official 130:1dec54e4aec3 217 I2Cx->TIMINGR = I2C_InitStruct->I2C_Timing & TIMING_CLEAR_MASK;
mbed_official 130:1dec54e4aec3 218
mbed_official 130:1dec54e4aec3 219 /* Enable I2Cx Peripheral */
mbed_official 130:1dec54e4aec3 220 I2Cx->CR1 |= I2C_CR1_PE;
mbed_official 130:1dec54e4aec3 221
mbed_official 130:1dec54e4aec3 222 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
mbed_official 130:1dec54e4aec3 223 /* Clear tmpreg local variable */
mbed_official 130:1dec54e4aec3 224 tmpreg = 0;
mbed_official 130:1dec54e4aec3 225 /* Clear OAR1 register */
mbed_official 130:1dec54e4aec3 226 I2Cx->OAR1 = (uint32_t)tmpreg;
mbed_official 130:1dec54e4aec3 227 /* Clear OAR2 register */
mbed_official 130:1dec54e4aec3 228 I2Cx->OAR2 = (uint32_t)tmpreg;
mbed_official 130:1dec54e4aec3 229 /* Configure I2Cx: Own Address1 and acknowledged address */
mbed_official 130:1dec54e4aec3 230 /* Set OA1MODE bit according to I2C_AcknowledgedAddress value */
mbed_official 130:1dec54e4aec3 231 /* Set OA1 bits according to I2C_OwnAddress1 value */
mbed_official 130:1dec54e4aec3 232 tmpreg = (uint32_t)((uint32_t)I2C_InitStruct->I2C_AcknowledgedAddress | \
mbed_official 130:1dec54e4aec3 233 (uint32_t)I2C_InitStruct->I2C_OwnAddress1);
mbed_official 130:1dec54e4aec3 234 /* Write to I2Cx OAR1 */
mbed_official 130:1dec54e4aec3 235 I2Cx->OAR1 = tmpreg;
mbed_official 130:1dec54e4aec3 236 /* Enable Own Address1 acknowledgement */
mbed_official 130:1dec54e4aec3 237 I2Cx->OAR1 |= I2C_OAR1_OA1EN;
mbed_official 130:1dec54e4aec3 238
mbed_official 130:1dec54e4aec3 239 /*---------------------------- I2Cx MODE Configuration ---------------------*/
mbed_official 130:1dec54e4aec3 240 /* Configure I2Cx: mode */
mbed_official 130:1dec54e4aec3 241 /* Set SMBDEN and SMBHEN bits according to I2C_Mode value */
mbed_official 130:1dec54e4aec3 242 tmpreg = I2C_InitStruct->I2C_Mode;
mbed_official 130:1dec54e4aec3 243 /* Write to I2Cx CR1 */
mbed_official 130:1dec54e4aec3 244 I2Cx->CR1 |= tmpreg;
mbed_official 130:1dec54e4aec3 245
mbed_official 130:1dec54e4aec3 246 /*---------------------------- I2Cx ACK Configuration ----------------------*/
mbed_official 130:1dec54e4aec3 247 /* Get the I2Cx CR2 value */
mbed_official 130:1dec54e4aec3 248 tmpreg = I2Cx->CR2;
mbed_official 130:1dec54e4aec3 249 /* Clear I2Cx CR2 register */
mbed_official 130:1dec54e4aec3 250 tmpreg &= CR2_CLEAR_MASK;
mbed_official 130:1dec54e4aec3 251 /* Configure I2Cx: acknowledgement */
mbed_official 130:1dec54e4aec3 252 /* Set NACK bit according to I2C_Ack value */
mbed_official 130:1dec54e4aec3 253 tmpreg |= I2C_InitStruct->I2C_Ack;
mbed_official 130:1dec54e4aec3 254 /* Write to I2Cx CR2 */
mbed_official 130:1dec54e4aec3 255 I2Cx->CR2 = tmpreg;
mbed_official 130:1dec54e4aec3 256 }
mbed_official 130:1dec54e4aec3 257
mbed_official 130:1dec54e4aec3 258 /**
mbed_official 130:1dec54e4aec3 259 * @brief Fills each I2C_InitStruct member with its default value.
mbed_official 130:1dec54e4aec3 260 * @param I2C_InitStruct: pointer to an I2C_InitTypeDef structure which will be initialized.
mbed_official 130:1dec54e4aec3 261 * @retval None
mbed_official 130:1dec54e4aec3 262 */
mbed_official 130:1dec54e4aec3 263 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
mbed_official 130:1dec54e4aec3 264 {
mbed_official 130:1dec54e4aec3 265 /*---------------- Reset I2C init structure parameters values --------------*/
mbed_official 130:1dec54e4aec3 266 /* Initialize the I2C_Timing member */
mbed_official 130:1dec54e4aec3 267 I2C_InitStruct->I2C_Timing = 0;
mbed_official 130:1dec54e4aec3 268 /* Initialize the I2C_AnalogFilter member */
mbed_official 130:1dec54e4aec3 269 I2C_InitStruct->I2C_AnalogFilter = I2C_AnalogFilter_Enable;
mbed_official 130:1dec54e4aec3 270 /* Initialize the I2C_DigitalFilter member */
mbed_official 130:1dec54e4aec3 271 I2C_InitStruct->I2C_DigitalFilter = 0;
mbed_official 130:1dec54e4aec3 272 /* Initialize the I2C_Mode member */
mbed_official 130:1dec54e4aec3 273 I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
mbed_official 130:1dec54e4aec3 274 /* Initialize the I2C_OwnAddress1 member */
mbed_official 130:1dec54e4aec3 275 I2C_InitStruct->I2C_OwnAddress1 = 0;
mbed_official 130:1dec54e4aec3 276 /* Initialize the I2C_Ack member */
mbed_official 130:1dec54e4aec3 277 I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
mbed_official 130:1dec54e4aec3 278 /* Initialize the I2C_AcknowledgedAddress member */
mbed_official 130:1dec54e4aec3 279 I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
mbed_official 130:1dec54e4aec3 280 }
mbed_official 130:1dec54e4aec3 281
mbed_official 130:1dec54e4aec3 282 /**
mbed_official 130:1dec54e4aec3 283 * @brief Enables or disables the specified I2C peripheral.
mbed_official 130:1dec54e4aec3 284 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 285 * @param NewState: new state of the I2Cx peripheral.
mbed_official 130:1dec54e4aec3 286 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 287 * @retval None
mbed_official 130:1dec54e4aec3 288 */
mbed_official 130:1dec54e4aec3 289 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 290 {
mbed_official 130:1dec54e4aec3 291 /* Check the parameters */
mbed_official 130:1dec54e4aec3 292 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 293 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 294 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 295 {
mbed_official 130:1dec54e4aec3 296 /* Enable the selected I2C peripheral */
mbed_official 130:1dec54e4aec3 297 I2Cx->CR1 |= I2C_CR1_PE;
mbed_official 130:1dec54e4aec3 298 }
mbed_official 130:1dec54e4aec3 299 else
mbed_official 130:1dec54e4aec3 300 {
mbed_official 130:1dec54e4aec3 301 /* Disable the selected I2C peripheral */
mbed_official 130:1dec54e4aec3 302 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
mbed_official 130:1dec54e4aec3 303 }
mbed_official 130:1dec54e4aec3 304 }
mbed_official 130:1dec54e4aec3 305
mbed_official 130:1dec54e4aec3 306 /**
mbed_official 130:1dec54e4aec3 307 * @brief Enables or disables the specified I2C software reset.
mbed_official 130:1dec54e4aec3 308 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 309 * @retval None
mbed_official 130:1dec54e4aec3 310 */
mbed_official 130:1dec54e4aec3 311 void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx)
mbed_official 130:1dec54e4aec3 312 {
mbed_official 130:1dec54e4aec3 313 /* Check the parameters */
mbed_official 130:1dec54e4aec3 314 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 315
mbed_official 130:1dec54e4aec3 316 /* Disable peripheral */
mbed_official 130:1dec54e4aec3 317 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PE);
mbed_official 130:1dec54e4aec3 318
mbed_official 130:1dec54e4aec3 319 /* Perform a dummy read to delay the disable of peripheral for minimum
mbed_official 130:1dec54e4aec3 320 3 APB clock cycles to perform the software reset functionality */
mbed_official 130:1dec54e4aec3 321 *(__IO uint32_t *)(uint32_t)I2Cx;
mbed_official 130:1dec54e4aec3 322
mbed_official 130:1dec54e4aec3 323 /* Enable peripheral */
mbed_official 130:1dec54e4aec3 324 I2Cx->CR1 |= I2C_CR1_PE;
mbed_official 130:1dec54e4aec3 325 }
mbed_official 130:1dec54e4aec3 326
mbed_official 130:1dec54e4aec3 327 /**
mbed_official 130:1dec54e4aec3 328 * @brief Enables or disables the specified I2C interrupts.
mbed_official 130:1dec54e4aec3 329 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 330 * @param I2C_IT: specifies the I2C interrupts sources to be enabled or disabled.
mbed_official 130:1dec54e4aec3 331 * This parameter can be any combination of the following values:
mbed_official 130:1dec54e4aec3 332 * @arg I2C_IT_ERRI: Error interrupt mask
mbed_official 130:1dec54e4aec3 333 * @arg I2C_IT_TCI: Transfer Complete interrupt mask
mbed_official 130:1dec54e4aec3 334 * @arg I2C_IT_STOPI: Stop Detection interrupt mask
mbed_official 130:1dec54e4aec3 335 * @arg I2C_IT_NACKI: Not Acknowledge received interrupt mask
mbed_official 130:1dec54e4aec3 336 * @arg I2C_IT_ADDRI: Address Match interrupt mask
mbed_official 130:1dec54e4aec3 337 * @arg I2C_IT_RXI: RX interrupt mask
mbed_official 130:1dec54e4aec3 338 * @arg I2C_IT_TXI: TX interrupt mask
mbed_official 130:1dec54e4aec3 339 * @param NewState: new state of the specified I2C interrupts.
mbed_official 130:1dec54e4aec3 340 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 341 * @retval None
mbed_official 130:1dec54e4aec3 342 */
mbed_official 130:1dec54e4aec3 343 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint32_t I2C_IT, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 344 {
mbed_official 130:1dec54e4aec3 345 /* Check the parameters */
mbed_official 130:1dec54e4aec3 346 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 347 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 348 assert_param(IS_I2C_CONFIG_IT(I2C_IT));
mbed_official 130:1dec54e4aec3 349
mbed_official 130:1dec54e4aec3 350 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 351 {
mbed_official 130:1dec54e4aec3 352 /* Enable the selected I2C interrupts */
mbed_official 130:1dec54e4aec3 353 I2Cx->CR1 |= I2C_IT;
mbed_official 130:1dec54e4aec3 354 }
mbed_official 130:1dec54e4aec3 355 else
mbed_official 130:1dec54e4aec3 356 {
mbed_official 130:1dec54e4aec3 357 /* Disable the selected I2C interrupts */
mbed_official 130:1dec54e4aec3 358 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_IT);
mbed_official 130:1dec54e4aec3 359 }
mbed_official 130:1dec54e4aec3 360 }
mbed_official 130:1dec54e4aec3 361
mbed_official 130:1dec54e4aec3 362 /**
mbed_official 130:1dec54e4aec3 363 * @brief Enables or disables the I2C Clock stretching.
mbed_official 130:1dec54e4aec3 364 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 365 * @param NewState: new state of the I2Cx Clock stretching.
mbed_official 130:1dec54e4aec3 366 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 367 * @retval None
mbed_official 130:1dec54e4aec3 368 */
mbed_official 130:1dec54e4aec3 369 void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 370 {
mbed_official 130:1dec54e4aec3 371 /* Check the parameters */
mbed_official 130:1dec54e4aec3 372 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 373 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 374
mbed_official 130:1dec54e4aec3 375 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 376 {
mbed_official 130:1dec54e4aec3 377 /* Enable clock stretching */
mbed_official 130:1dec54e4aec3 378 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_NOSTRETCH);
mbed_official 130:1dec54e4aec3 379 }
mbed_official 130:1dec54e4aec3 380 else
mbed_official 130:1dec54e4aec3 381 {
mbed_official 130:1dec54e4aec3 382 /* Disable clock stretching */
mbed_official 130:1dec54e4aec3 383 I2Cx->CR1 |= I2C_CR1_NOSTRETCH;
mbed_official 130:1dec54e4aec3 384 }
mbed_official 130:1dec54e4aec3 385 }
mbed_official 130:1dec54e4aec3 386
mbed_official 130:1dec54e4aec3 387 /**
mbed_official 130:1dec54e4aec3 388 * @brief Enables or disables I2C wakeup from stop mode.
mbed_official 130:1dec54e4aec3 389 * This function is not applicable for STM32F030 devices.
mbed_official 130:1dec54e4aec3 390 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 391 * @param NewState: new state of the I2Cx stop mode.
mbed_official 130:1dec54e4aec3 392 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 393 * @retval None
mbed_official 130:1dec54e4aec3 394 */
mbed_official 130:1dec54e4aec3 395 void I2C_StopModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 396 {
mbed_official 130:1dec54e4aec3 397 /* Check the parameters */
mbed_official 130:1dec54e4aec3 398 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 399 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 400
mbed_official 130:1dec54e4aec3 401 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 402 {
mbed_official 130:1dec54e4aec3 403 /* Enable wakeup from stop mode */
mbed_official 130:1dec54e4aec3 404 I2Cx->CR1 |= I2C_CR1_WUPEN;
mbed_official 130:1dec54e4aec3 405 }
mbed_official 130:1dec54e4aec3 406 else
mbed_official 130:1dec54e4aec3 407 {
mbed_official 130:1dec54e4aec3 408 /* Disable wakeup from stop mode */
mbed_official 130:1dec54e4aec3 409 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_WUPEN);
mbed_official 130:1dec54e4aec3 410 }
mbed_official 130:1dec54e4aec3 411 }
mbed_official 130:1dec54e4aec3 412
mbed_official 130:1dec54e4aec3 413 /**
mbed_official 130:1dec54e4aec3 414 * @brief Enables or disables the I2C own address 2.
mbed_official 130:1dec54e4aec3 415 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 416 * @param NewState: new state of the I2C own address 2.
mbed_official 130:1dec54e4aec3 417 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 418 * @retval None
mbed_official 130:1dec54e4aec3 419 */
mbed_official 130:1dec54e4aec3 420 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 421 {
mbed_official 130:1dec54e4aec3 422 /* Check the parameters */
mbed_official 130:1dec54e4aec3 423 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 424 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 425
mbed_official 130:1dec54e4aec3 426 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 427 {
mbed_official 130:1dec54e4aec3 428 /* Enable own address 2 */
mbed_official 130:1dec54e4aec3 429 I2Cx->OAR2 |= I2C_OAR2_OA2EN;
mbed_official 130:1dec54e4aec3 430 }
mbed_official 130:1dec54e4aec3 431 else
mbed_official 130:1dec54e4aec3 432 {
mbed_official 130:1dec54e4aec3 433 /* Disable own address 2 */
mbed_official 130:1dec54e4aec3 434 I2Cx->OAR2 &= (uint32_t)~((uint32_t)I2C_OAR2_OA2EN);
mbed_official 130:1dec54e4aec3 435 }
mbed_official 130:1dec54e4aec3 436 }
mbed_official 130:1dec54e4aec3 437
mbed_official 130:1dec54e4aec3 438 /**
mbed_official 130:1dec54e4aec3 439 * @brief Configures the I2C slave own address 2 and mask.
mbed_official 130:1dec54e4aec3 440 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 441 * @param Address: specifies the slave address to be programmed.
mbed_official 130:1dec54e4aec3 442 * @param Mask: specifies own address 2 mask to be programmed.
mbed_official 130:1dec54e4aec3 443 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 444 * @arg I2C_OA2_NoMask: no mask.
mbed_official 130:1dec54e4aec3 445 * @arg I2C_OA2_Mask01: OA2[1] is masked and don't care.
mbed_official 130:1dec54e4aec3 446 * @arg I2C_OA2_Mask02: OA2[2:1] are masked and don't care.
mbed_official 130:1dec54e4aec3 447 * @arg I2C_OA2_Mask03: OA2[3:1] are masked and don't care.
mbed_official 130:1dec54e4aec3 448 * @arg I2C_OA2_Mask04: OA2[4:1] are masked and don't care.
mbed_official 130:1dec54e4aec3 449 * @arg I2C_OA2_Mask05: OA2[5:1] are masked and don't care.
mbed_official 130:1dec54e4aec3 450 * @arg I2C_OA2_Mask06: OA2[6:1] are masked and don't care.
mbed_official 130:1dec54e4aec3 451 * @arg I2C_OA2_Mask07: OA2[7:1] are masked and don't care.
mbed_official 130:1dec54e4aec3 452 * @retval None
mbed_official 130:1dec54e4aec3 453 */
mbed_official 130:1dec54e4aec3 454 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Mask)
mbed_official 130:1dec54e4aec3 455 {
mbed_official 130:1dec54e4aec3 456 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 457
mbed_official 130:1dec54e4aec3 458 /* Check the parameters */
mbed_official 130:1dec54e4aec3 459 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 460 assert_param(IS_I2C_OWN_ADDRESS2(Address));
mbed_official 130:1dec54e4aec3 461 assert_param(IS_I2C_OWN_ADDRESS2_MASK(Mask));
mbed_official 130:1dec54e4aec3 462
mbed_official 130:1dec54e4aec3 463 /* Get the old register value */
mbed_official 130:1dec54e4aec3 464 tmpreg = I2Cx->OAR2;
mbed_official 130:1dec54e4aec3 465
mbed_official 130:1dec54e4aec3 466 /* Reset I2Cx OA2 bit [7:1] and OA2MSK bit [1:0] */
mbed_official 130:1dec54e4aec3 467 tmpreg &= (uint32_t)~((uint32_t)(I2C_OAR2_OA2 | I2C_OAR2_OA2MSK));
mbed_official 130:1dec54e4aec3 468
mbed_official 130:1dec54e4aec3 469 /* Set I2Cx SADD */
mbed_official 130:1dec54e4aec3 470 tmpreg |= (uint32_t)(((uint32_t)Address & I2C_OAR2_OA2) | \
mbed_official 130:1dec54e4aec3 471 (((uint32_t)Mask << 8) & I2C_OAR2_OA2MSK)) ;
mbed_official 130:1dec54e4aec3 472
mbed_official 130:1dec54e4aec3 473 /* Store the new register value */
mbed_official 130:1dec54e4aec3 474 I2Cx->OAR2 = tmpreg;
mbed_official 130:1dec54e4aec3 475 }
mbed_official 130:1dec54e4aec3 476
mbed_official 130:1dec54e4aec3 477 /**
mbed_official 130:1dec54e4aec3 478 * @brief Enables or disables the I2C general call mode.
mbed_official 130:1dec54e4aec3 479 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 480 * @param NewState: new state of the I2C general call mode.
mbed_official 130:1dec54e4aec3 481 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 482 * @retval None
mbed_official 130:1dec54e4aec3 483 */
mbed_official 130:1dec54e4aec3 484 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 485 {
mbed_official 130:1dec54e4aec3 486 /* Check the parameters */
mbed_official 130:1dec54e4aec3 487 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 488 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 489
mbed_official 130:1dec54e4aec3 490 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 491 {
mbed_official 130:1dec54e4aec3 492 /* Enable general call mode */
mbed_official 130:1dec54e4aec3 493 I2Cx->CR1 |= I2C_CR1_GCEN;
mbed_official 130:1dec54e4aec3 494 }
mbed_official 130:1dec54e4aec3 495 else
mbed_official 130:1dec54e4aec3 496 {
mbed_official 130:1dec54e4aec3 497 /* Disable general call mode */
mbed_official 130:1dec54e4aec3 498 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_GCEN);
mbed_official 130:1dec54e4aec3 499 }
mbed_official 130:1dec54e4aec3 500 }
mbed_official 130:1dec54e4aec3 501
mbed_official 130:1dec54e4aec3 502 /**
mbed_official 130:1dec54e4aec3 503 * @brief Enables or disables the I2C slave byte control.
mbed_official 130:1dec54e4aec3 504 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 505 * @param NewState: new state of the I2C slave byte control.
mbed_official 130:1dec54e4aec3 506 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 507 * @retval None
mbed_official 130:1dec54e4aec3 508 */
mbed_official 130:1dec54e4aec3 509 void I2C_SlaveByteControlCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 510 {
mbed_official 130:1dec54e4aec3 511 /* Check the parameters */
mbed_official 130:1dec54e4aec3 512 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 513 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 514
mbed_official 130:1dec54e4aec3 515 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 516 {
mbed_official 130:1dec54e4aec3 517 /* Enable slave byte control */
mbed_official 130:1dec54e4aec3 518 I2Cx->CR1 |= I2C_CR1_SBC;
mbed_official 130:1dec54e4aec3 519 }
mbed_official 130:1dec54e4aec3 520 else
mbed_official 130:1dec54e4aec3 521 {
mbed_official 130:1dec54e4aec3 522 /* Disable slave byte control */
mbed_official 130:1dec54e4aec3 523 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_SBC);
mbed_official 130:1dec54e4aec3 524 }
mbed_official 130:1dec54e4aec3 525 }
mbed_official 130:1dec54e4aec3 526
mbed_official 130:1dec54e4aec3 527 /**
mbed_official 130:1dec54e4aec3 528 * @brief Configures the slave address to be transmitted after start generation.
mbed_official 130:1dec54e4aec3 529 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 530 * @param Address: specifies the slave address to be programmed.
mbed_official 130:1dec54e4aec3 531 * @note This function should be called before generating start condition.
mbed_official 130:1dec54e4aec3 532 * @retval None
mbed_official 130:1dec54e4aec3 533 */
mbed_official 130:1dec54e4aec3 534 void I2C_SlaveAddressConfig(I2C_TypeDef* I2Cx, uint16_t Address)
mbed_official 130:1dec54e4aec3 535 {
mbed_official 130:1dec54e4aec3 536 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 537
mbed_official 130:1dec54e4aec3 538 /* Check the parameters */
mbed_official 130:1dec54e4aec3 539 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 540 assert_param(IS_I2C_SLAVE_ADDRESS(Address));
mbed_official 130:1dec54e4aec3 541
mbed_official 130:1dec54e4aec3 542 /* Get the old register value */
mbed_official 130:1dec54e4aec3 543 tmpreg = I2Cx->CR2;
mbed_official 130:1dec54e4aec3 544
mbed_official 130:1dec54e4aec3 545 /* Reset I2Cx SADD bit [9:0] */
mbed_official 130:1dec54e4aec3 546 tmpreg &= (uint32_t)~((uint32_t)I2C_CR2_SADD);
mbed_official 130:1dec54e4aec3 547
mbed_official 130:1dec54e4aec3 548 /* Set I2Cx SADD */
mbed_official 130:1dec54e4aec3 549 tmpreg |= (uint32_t)((uint32_t)Address & I2C_CR2_SADD);
mbed_official 130:1dec54e4aec3 550
mbed_official 130:1dec54e4aec3 551 /* Store the new register value */
mbed_official 130:1dec54e4aec3 552 I2Cx->CR2 = tmpreg;
mbed_official 130:1dec54e4aec3 553 }
mbed_official 130:1dec54e4aec3 554
mbed_official 130:1dec54e4aec3 555 /**
mbed_official 130:1dec54e4aec3 556 * @brief Enables or disables the I2C 10-bit addressing mode for the master.
mbed_official 130:1dec54e4aec3 557 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 558 * @param NewState: new state of the I2C 10-bit addressing mode.
mbed_official 130:1dec54e4aec3 559 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 560 * @note This function should be called before generating start condition.
mbed_official 130:1dec54e4aec3 561 * @retval None
mbed_official 130:1dec54e4aec3 562 */
mbed_official 130:1dec54e4aec3 563 void I2C_10BitAddressingModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 564 {
mbed_official 130:1dec54e4aec3 565 /* Check the parameters */
mbed_official 130:1dec54e4aec3 566 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 567 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 568
mbed_official 130:1dec54e4aec3 569 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 570 {
mbed_official 130:1dec54e4aec3 571 /* Enable 10-bit addressing mode */
mbed_official 130:1dec54e4aec3 572 I2Cx->CR2 |= I2C_CR2_ADD10;
mbed_official 130:1dec54e4aec3 573 }
mbed_official 130:1dec54e4aec3 574 else
mbed_official 130:1dec54e4aec3 575 {
mbed_official 130:1dec54e4aec3 576 /* Disable 10-bit addressing mode */
mbed_official 130:1dec54e4aec3 577 I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_ADD10);
mbed_official 130:1dec54e4aec3 578 }
mbed_official 130:1dec54e4aec3 579 }
mbed_official 130:1dec54e4aec3 580
mbed_official 130:1dec54e4aec3 581 /**
mbed_official 130:1dec54e4aec3 582 * @}
mbed_official 130:1dec54e4aec3 583 */
mbed_official 130:1dec54e4aec3 584
mbed_official 130:1dec54e4aec3 585
mbed_official 130:1dec54e4aec3 586 /** @defgroup I2C_Group2 Communications handling functions
mbed_official 130:1dec54e4aec3 587 * @brief Communications handling functions
mbed_official 130:1dec54e4aec3 588 *
mbed_official 130:1dec54e4aec3 589 @verbatim
mbed_official 130:1dec54e4aec3 590 ===============================================================================
mbed_official 130:1dec54e4aec3 591 ##### Communications handling functions #####
mbed_official 130:1dec54e4aec3 592 ===============================================================================
mbed_official 130:1dec54e4aec3 593 [..] This section provides a set of functions that handles I2C communication.
mbed_official 130:1dec54e4aec3 594
mbed_official 130:1dec54e4aec3 595 [..] Automatic End mode is enabled using I2C_AutoEndCmd() function. When Reload
mbed_official 130:1dec54e4aec3 596 mode is enabled via I2C_ReloadCmd() AutoEnd bit has no effect.
mbed_official 130:1dec54e4aec3 597
mbed_official 130:1dec54e4aec3 598 [..] I2C_NumberOfBytesConfig() function set the number of bytes to be transferred,
mbed_official 130:1dec54e4aec3 599 this configuration should be done before generating start condition in master
mbed_official 130:1dec54e4aec3 600 mode.
mbed_official 130:1dec54e4aec3 601
mbed_official 130:1dec54e4aec3 602 [..] When switching from master write operation to read operation in 10Bit addressing
mbed_official 130:1dec54e4aec3 603 mode, master can only sends the 1st 7 bits of the 10 bit address, followed by
mbed_official 130:1dec54e4aec3 604 Read direction by enabling HEADR bit using I2C_10BitAddressHeader() function.
mbed_official 130:1dec54e4aec3 605
mbed_official 130:1dec54e4aec3 606 [..] In master mode, when transferring more than 255 bytes Reload mode should be used
mbed_official 130:1dec54e4aec3 607 to handle communication. In the first phase of transfer, Nbytes should be set to
mbed_official 130:1dec54e4aec3 608 255. After transferring these bytes TCR flag is set and I2C_TransferHandling()
mbed_official 130:1dec54e4aec3 609 function should be called to handle remaining communication.
mbed_official 130:1dec54e4aec3 610
mbed_official 130:1dec54e4aec3 611 [..] In master mode, when software end mode is selected when all data is transferred
mbed_official 130:1dec54e4aec3 612 TC flag is set I2C_TransferHandling() function should be called to generate STOP
mbed_official 130:1dec54e4aec3 613 or generate ReStart.
mbed_official 130:1dec54e4aec3 614
mbed_official 130:1dec54e4aec3 615 @endverbatim
mbed_official 130:1dec54e4aec3 616 * @{
mbed_official 130:1dec54e4aec3 617 */
mbed_official 130:1dec54e4aec3 618
mbed_official 130:1dec54e4aec3 619 /**
mbed_official 130:1dec54e4aec3 620 * @brief Enables or disables the I2C automatic end mode (stop condition is
mbed_official 130:1dec54e4aec3 621 * automatically sent when nbytes data are transferred).
mbed_official 130:1dec54e4aec3 622 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 623 * @param NewState: new state of the I2C automatic end mode.
mbed_official 130:1dec54e4aec3 624 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 625 * @note This function has effect if Reload mode is disabled.
mbed_official 130:1dec54e4aec3 626 * @retval None
mbed_official 130:1dec54e4aec3 627 */
mbed_official 130:1dec54e4aec3 628 void I2C_AutoEndCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 629 {
mbed_official 130:1dec54e4aec3 630 /* Check the parameters */
mbed_official 130:1dec54e4aec3 631 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 632 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 633
mbed_official 130:1dec54e4aec3 634 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 635 {
mbed_official 130:1dec54e4aec3 636 /* Enable Auto end mode */
mbed_official 130:1dec54e4aec3 637 I2Cx->CR2 |= I2C_CR2_AUTOEND;
mbed_official 130:1dec54e4aec3 638 }
mbed_official 130:1dec54e4aec3 639 else
mbed_official 130:1dec54e4aec3 640 {
mbed_official 130:1dec54e4aec3 641 /* Disable Auto end mode */
mbed_official 130:1dec54e4aec3 642 I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_AUTOEND);
mbed_official 130:1dec54e4aec3 643 }
mbed_official 130:1dec54e4aec3 644 }
mbed_official 130:1dec54e4aec3 645
mbed_official 130:1dec54e4aec3 646 /**
mbed_official 130:1dec54e4aec3 647 * @brief Enables or disables the I2C nbytes reload mode.
mbed_official 130:1dec54e4aec3 648 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 649 * @param NewState: new state of the nbytes reload mode.
mbed_official 130:1dec54e4aec3 650 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 651 * @retval None
mbed_official 130:1dec54e4aec3 652 */
mbed_official 130:1dec54e4aec3 653 void I2C_ReloadCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 654 {
mbed_official 130:1dec54e4aec3 655 /* Check the parameters */
mbed_official 130:1dec54e4aec3 656 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 657 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 658
mbed_official 130:1dec54e4aec3 659 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 660 {
mbed_official 130:1dec54e4aec3 661 /* Enable Auto Reload mode */
mbed_official 130:1dec54e4aec3 662 I2Cx->CR2 |= I2C_CR2_RELOAD;
mbed_official 130:1dec54e4aec3 663 }
mbed_official 130:1dec54e4aec3 664 else
mbed_official 130:1dec54e4aec3 665 {
mbed_official 130:1dec54e4aec3 666 /* Disable Auto Reload mode */
mbed_official 130:1dec54e4aec3 667 I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_RELOAD);
mbed_official 130:1dec54e4aec3 668 }
mbed_official 130:1dec54e4aec3 669 }
mbed_official 130:1dec54e4aec3 670
mbed_official 130:1dec54e4aec3 671 /**
mbed_official 130:1dec54e4aec3 672 * @brief Configures the number of bytes to be transmitted/received.
mbed_official 130:1dec54e4aec3 673 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 674 * @param Number_Bytes: specifies the number of bytes to be programmed.
mbed_official 130:1dec54e4aec3 675 * @retval None
mbed_official 130:1dec54e4aec3 676 */
mbed_official 130:1dec54e4aec3 677 void I2C_NumberOfBytesConfig(I2C_TypeDef* I2Cx, uint8_t Number_Bytes)
mbed_official 130:1dec54e4aec3 678 {
mbed_official 130:1dec54e4aec3 679 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 680
mbed_official 130:1dec54e4aec3 681 /* Check the parameters */
mbed_official 130:1dec54e4aec3 682 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 683
mbed_official 130:1dec54e4aec3 684 /* Get the old register value */
mbed_official 130:1dec54e4aec3 685 tmpreg = I2Cx->CR2;
mbed_official 130:1dec54e4aec3 686
mbed_official 130:1dec54e4aec3 687 /* Reset I2Cx Nbytes bit [7:0] */
mbed_official 130:1dec54e4aec3 688 tmpreg &= (uint32_t)~((uint32_t)I2C_CR2_NBYTES);
mbed_official 130:1dec54e4aec3 689
mbed_official 130:1dec54e4aec3 690 /* Set I2Cx Nbytes */
mbed_official 130:1dec54e4aec3 691 tmpreg |= (uint32_t)(((uint32_t)Number_Bytes << 16 ) & I2C_CR2_NBYTES);
mbed_official 130:1dec54e4aec3 692
mbed_official 130:1dec54e4aec3 693 /* Store the new register value */
mbed_official 130:1dec54e4aec3 694 I2Cx->CR2 = tmpreg;
mbed_official 130:1dec54e4aec3 695 }
mbed_official 130:1dec54e4aec3 696
mbed_official 130:1dec54e4aec3 697 /**
mbed_official 130:1dec54e4aec3 698 * @brief Configures the type of transfer request for the master.
mbed_official 130:1dec54e4aec3 699 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 700 * @param I2C_Direction: specifies the transfer request direction to be programmed.
mbed_official 130:1dec54e4aec3 701 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 702 * @arg I2C_Direction_Transmitter: Master request a write transfer
mbed_official 130:1dec54e4aec3 703 * @arg I2C_Direction_Receiver: Master request a read transfer
mbed_official 130:1dec54e4aec3 704 * @retval None
mbed_official 130:1dec54e4aec3 705 */
mbed_official 130:1dec54e4aec3 706 void I2C_MasterRequestConfig(I2C_TypeDef* I2Cx, uint16_t I2C_Direction)
mbed_official 130:1dec54e4aec3 707 {
mbed_official 130:1dec54e4aec3 708 /* Check the parameters */
mbed_official 130:1dec54e4aec3 709 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 710 assert_param(IS_I2C_DIRECTION(I2C_Direction));
mbed_official 130:1dec54e4aec3 711
mbed_official 130:1dec54e4aec3 712 /* Test on the direction to set/reset the read/write bit */
mbed_official 130:1dec54e4aec3 713 if (I2C_Direction == I2C_Direction_Transmitter)
mbed_official 130:1dec54e4aec3 714 {
mbed_official 130:1dec54e4aec3 715 /* Request a write Transfer */
mbed_official 130:1dec54e4aec3 716 I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_RD_WRN);
mbed_official 130:1dec54e4aec3 717 }
mbed_official 130:1dec54e4aec3 718 else
mbed_official 130:1dec54e4aec3 719 {
mbed_official 130:1dec54e4aec3 720 /* Request a read Transfer */
mbed_official 130:1dec54e4aec3 721 I2Cx->CR2 |= I2C_CR2_RD_WRN;
mbed_official 130:1dec54e4aec3 722 }
mbed_official 130:1dec54e4aec3 723 }
mbed_official 130:1dec54e4aec3 724
mbed_official 130:1dec54e4aec3 725 /**
mbed_official 130:1dec54e4aec3 726 * @brief Generates I2Cx communication START condition.
mbed_official 130:1dec54e4aec3 727 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 728 * @param NewState: new state of the I2C START condition generation.
mbed_official 130:1dec54e4aec3 729 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 730 * @retval None
mbed_official 130:1dec54e4aec3 731 */
mbed_official 130:1dec54e4aec3 732 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 733 {
mbed_official 130:1dec54e4aec3 734 /* Check the parameters */
mbed_official 130:1dec54e4aec3 735 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 736 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 737
mbed_official 130:1dec54e4aec3 738 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 739 {
mbed_official 130:1dec54e4aec3 740 /* Generate a START condition */
mbed_official 130:1dec54e4aec3 741 I2Cx->CR2 |= I2C_CR2_START;
mbed_official 130:1dec54e4aec3 742 }
mbed_official 130:1dec54e4aec3 743 else
mbed_official 130:1dec54e4aec3 744 {
mbed_official 130:1dec54e4aec3 745 /* Disable the START condition generation */
mbed_official 130:1dec54e4aec3 746 I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_START);
mbed_official 130:1dec54e4aec3 747 }
mbed_official 130:1dec54e4aec3 748 }
mbed_official 130:1dec54e4aec3 749
mbed_official 130:1dec54e4aec3 750 /**
mbed_official 130:1dec54e4aec3 751 * @brief Generates I2Cx communication STOP condition.
mbed_official 130:1dec54e4aec3 752 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 753 * @param NewState: new state of the I2C STOP condition generation.
mbed_official 130:1dec54e4aec3 754 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 755 * @retval None
mbed_official 130:1dec54e4aec3 756 */
mbed_official 130:1dec54e4aec3 757 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 758 {
mbed_official 130:1dec54e4aec3 759 /* Check the parameters */
mbed_official 130:1dec54e4aec3 760 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 761 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 762
mbed_official 130:1dec54e4aec3 763 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 764 {
mbed_official 130:1dec54e4aec3 765 /* Generate a STOP condition */
mbed_official 130:1dec54e4aec3 766 I2Cx->CR2 |= I2C_CR2_STOP;
mbed_official 130:1dec54e4aec3 767 }
mbed_official 130:1dec54e4aec3 768 else
mbed_official 130:1dec54e4aec3 769 {
mbed_official 130:1dec54e4aec3 770 /* Disable the STOP condition generation */
mbed_official 130:1dec54e4aec3 771 I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_STOP);
mbed_official 130:1dec54e4aec3 772 }
mbed_official 130:1dec54e4aec3 773 }
mbed_official 130:1dec54e4aec3 774
mbed_official 130:1dec54e4aec3 775 /**
mbed_official 130:1dec54e4aec3 776 * @brief Enables or disables the I2C 10-bit header only mode with read direction.
mbed_official 130:1dec54e4aec3 777 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 778 * @param NewState: new state of the I2C 10-bit header only mode.
mbed_official 130:1dec54e4aec3 779 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 780 * @note This mode can be used only when switching from master transmitter mode
mbed_official 130:1dec54e4aec3 781 * to master receiver mode.
mbed_official 130:1dec54e4aec3 782 * @retval None
mbed_official 130:1dec54e4aec3 783 */
mbed_official 130:1dec54e4aec3 784 void I2C_10BitAddressHeaderCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 785 {
mbed_official 130:1dec54e4aec3 786 /* Check the parameters */
mbed_official 130:1dec54e4aec3 787 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 788 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 789
mbed_official 130:1dec54e4aec3 790 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 791 {
mbed_official 130:1dec54e4aec3 792 /* Enable 10-bit header only mode */
mbed_official 130:1dec54e4aec3 793 I2Cx->CR2 |= I2C_CR2_HEAD10R;
mbed_official 130:1dec54e4aec3 794 }
mbed_official 130:1dec54e4aec3 795 else
mbed_official 130:1dec54e4aec3 796 {
mbed_official 130:1dec54e4aec3 797 /* Disable 10-bit header only mode */
mbed_official 130:1dec54e4aec3 798 I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_HEAD10R);
mbed_official 130:1dec54e4aec3 799 }
mbed_official 130:1dec54e4aec3 800 }
mbed_official 130:1dec54e4aec3 801
mbed_official 130:1dec54e4aec3 802 /**
mbed_official 130:1dec54e4aec3 803 * @brief Generates I2C communication Acknowledge.
mbed_official 130:1dec54e4aec3 804 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 805 * @param NewState: new state of the Acknowledge.
mbed_official 130:1dec54e4aec3 806 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 807 * @retval None
mbed_official 130:1dec54e4aec3 808 */
mbed_official 130:1dec54e4aec3 809 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 810 {
mbed_official 130:1dec54e4aec3 811 /* Check the parameters */
mbed_official 130:1dec54e4aec3 812 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 813 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 814
mbed_official 130:1dec54e4aec3 815 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 816 {
mbed_official 130:1dec54e4aec3 817 /* Enable ACK generation */
mbed_official 130:1dec54e4aec3 818 I2Cx->CR2 &= (uint32_t)~((uint32_t)I2C_CR2_NACK);
mbed_official 130:1dec54e4aec3 819 }
mbed_official 130:1dec54e4aec3 820 else
mbed_official 130:1dec54e4aec3 821 {
mbed_official 130:1dec54e4aec3 822 /* Enable NACK generation */
mbed_official 130:1dec54e4aec3 823 I2Cx->CR2 |= I2C_CR2_NACK;
mbed_official 130:1dec54e4aec3 824 }
mbed_official 130:1dec54e4aec3 825 }
mbed_official 130:1dec54e4aec3 826
mbed_official 130:1dec54e4aec3 827 /**
mbed_official 130:1dec54e4aec3 828 * @brief Returns the I2C slave matched address .
mbed_official 130:1dec54e4aec3 829 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 830 * @retval The value of the slave matched address .
mbed_official 130:1dec54e4aec3 831 */
mbed_official 130:1dec54e4aec3 832 uint8_t I2C_GetAddressMatched(I2C_TypeDef* I2Cx)
mbed_official 130:1dec54e4aec3 833 {
mbed_official 130:1dec54e4aec3 834 /* Check the parameters */
mbed_official 130:1dec54e4aec3 835 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 836
mbed_official 130:1dec54e4aec3 837 /* Return the slave matched address in the SR1 register */
mbed_official 130:1dec54e4aec3 838 return (uint8_t)(((uint32_t)I2Cx->ISR & I2C_ISR_ADDCODE) >> 16) ;
mbed_official 130:1dec54e4aec3 839 }
mbed_official 130:1dec54e4aec3 840
mbed_official 130:1dec54e4aec3 841 /**
mbed_official 130:1dec54e4aec3 842 * @brief Returns the I2C slave received request.
mbed_official 130:1dec54e4aec3 843 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 844 * @retval The value of the received request.
mbed_official 130:1dec54e4aec3 845 */
mbed_official 130:1dec54e4aec3 846 uint16_t I2C_GetTransferDirection(I2C_TypeDef* I2Cx)
mbed_official 130:1dec54e4aec3 847 {
mbed_official 130:1dec54e4aec3 848 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 849 uint16_t direction = 0;
mbed_official 130:1dec54e4aec3 850
mbed_official 130:1dec54e4aec3 851 /* Check the parameters */
mbed_official 130:1dec54e4aec3 852 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 853
mbed_official 130:1dec54e4aec3 854 /* Return the slave matched address in the SR1 register */
mbed_official 130:1dec54e4aec3 855 tmpreg = (uint32_t)(I2Cx->ISR & I2C_ISR_DIR);
mbed_official 130:1dec54e4aec3 856
mbed_official 130:1dec54e4aec3 857 /* If write transfer is requested */
mbed_official 130:1dec54e4aec3 858 if (tmpreg == 0)
mbed_official 130:1dec54e4aec3 859 {
mbed_official 130:1dec54e4aec3 860 /* write transfer is requested */
mbed_official 130:1dec54e4aec3 861 direction = I2C_Direction_Transmitter;
mbed_official 130:1dec54e4aec3 862 }
mbed_official 130:1dec54e4aec3 863 else
mbed_official 130:1dec54e4aec3 864 {
mbed_official 130:1dec54e4aec3 865 /* Read transfer is requested */
mbed_official 130:1dec54e4aec3 866 direction = I2C_Direction_Receiver;
mbed_official 130:1dec54e4aec3 867 }
mbed_official 130:1dec54e4aec3 868 return direction;
mbed_official 130:1dec54e4aec3 869 }
mbed_official 130:1dec54e4aec3 870
mbed_official 130:1dec54e4aec3 871 /**
mbed_official 130:1dec54e4aec3 872 * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
mbed_official 130:1dec54e4aec3 873 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 874 * @param Address: specifies the slave address to be programmed.
mbed_official 130:1dec54e4aec3 875 * @param Number_Bytes: specifies the number of bytes to be programmed.
mbed_official 130:1dec54e4aec3 876 * This parameter must be a value between 0 and 255.
mbed_official 130:1dec54e4aec3 877 * @param ReloadEndMode: new state of the I2C START condition generation.
mbed_official 130:1dec54e4aec3 878 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 879 * @arg I2C_Reload_Mode: Enable Reload mode .
mbed_official 130:1dec54e4aec3 880 * @arg I2C_AutoEnd_Mode: Enable Automatic end mode.
mbed_official 130:1dec54e4aec3 881 * @arg I2C_SoftEnd_Mode: Enable Software end mode.
mbed_official 130:1dec54e4aec3 882 * @param StartStopMode: new state of the I2C START condition generation.
mbed_official 130:1dec54e4aec3 883 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 884 * @arg I2C_No_StartStop: Don't Generate stop and start condition.
mbed_official 130:1dec54e4aec3 885 * @arg I2C_Generate_Stop: Generate stop condition (Number_Bytes should be set to 0).
mbed_official 130:1dec54e4aec3 886 * @arg I2C_Generate_Start_Read: Generate Restart for read request.
mbed_official 130:1dec54e4aec3 887 * @arg I2C_Generate_Start_Write: Generate Restart for write request.
mbed_official 130:1dec54e4aec3 888 * @retval None
mbed_official 130:1dec54e4aec3 889 */
mbed_official 130:1dec54e4aec3 890 void I2C_TransferHandling(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Number_Bytes, uint32_t ReloadEndMode, uint32_t StartStopMode)
mbed_official 130:1dec54e4aec3 891 {
mbed_official 130:1dec54e4aec3 892 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 893
mbed_official 130:1dec54e4aec3 894 /* Check the parameters */
mbed_official 130:1dec54e4aec3 895 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 896 assert_param(IS_I2C_SLAVE_ADDRESS(Address));
mbed_official 130:1dec54e4aec3 897 assert_param(IS_RELOAD_END_MODE(ReloadEndMode));
mbed_official 130:1dec54e4aec3 898 assert_param(IS_START_STOP_MODE(StartStopMode));
mbed_official 130:1dec54e4aec3 899
mbed_official 130:1dec54e4aec3 900 /* Get the CR2 register value */
mbed_official 130:1dec54e4aec3 901 tmpreg = I2Cx->CR2;
mbed_official 130:1dec54e4aec3 902
mbed_official 130:1dec54e4aec3 903 /* clear tmpreg specific bits */
mbed_official 130:1dec54e4aec3 904 tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
mbed_official 130:1dec54e4aec3 905
mbed_official 130:1dec54e4aec3 906 /* update tmpreg */
mbed_official 130:1dec54e4aec3 907 tmpreg |= (uint32_t)(((uint32_t)Address & I2C_CR2_SADD) | (((uint32_t)Number_Bytes << 16 ) & I2C_CR2_NBYTES) | \
mbed_official 130:1dec54e4aec3 908 (uint32_t)ReloadEndMode | (uint32_t)StartStopMode);
mbed_official 130:1dec54e4aec3 909
mbed_official 130:1dec54e4aec3 910 /* update CR2 register */
mbed_official 130:1dec54e4aec3 911 I2Cx->CR2 = tmpreg;
mbed_official 130:1dec54e4aec3 912 }
mbed_official 130:1dec54e4aec3 913
mbed_official 130:1dec54e4aec3 914 /**
mbed_official 130:1dec54e4aec3 915 * @}
mbed_official 130:1dec54e4aec3 916 */
mbed_official 130:1dec54e4aec3 917
mbed_official 130:1dec54e4aec3 918
mbed_official 130:1dec54e4aec3 919 /** @defgroup I2C_Group3 SMBUS management functions
mbed_official 130:1dec54e4aec3 920 * @brief SMBUS management functions
mbed_official 130:1dec54e4aec3 921 *
mbed_official 130:1dec54e4aec3 922 @verbatim
mbed_official 130:1dec54e4aec3 923 ===============================================================================
mbed_official 130:1dec54e4aec3 924 ##### SMBUS management functions #####
mbed_official 130:1dec54e4aec3 925 ===============================================================================
mbed_official 130:1dec54e4aec3 926 [..] This section provides a set of functions that handles SMBus communication
mbed_official 130:1dec54e4aec3 927 and timeouts detection.
mbed_official 130:1dec54e4aec3 928
mbed_official 130:1dec54e4aec3 929 [..] The SMBus Device default address (0b1100 001) is enabled by calling I2C_Init()
mbed_official 130:1dec54e4aec3 930 function and setting I2C_Mode member of I2C_InitTypeDef() structure to
mbed_official 130:1dec54e4aec3 931 I2C_Mode_SMBusDevice.
mbed_official 130:1dec54e4aec3 932
mbed_official 130:1dec54e4aec3 933 [..] The SMBus Host address (0b0001 000) is enabled by calling I2C_Init()
mbed_official 130:1dec54e4aec3 934 function and setting I2C_Mode member of I2C_InitTypeDef() structure to
mbed_official 130:1dec54e4aec3 935 I2C_Mode_SMBusHost.
mbed_official 130:1dec54e4aec3 936
mbed_official 130:1dec54e4aec3 937 [..] The Alert Response Address (0b0001 100) is enabled using I2C_SMBusAlertCmd()
mbed_official 130:1dec54e4aec3 938 function.
mbed_official 130:1dec54e4aec3 939
mbed_official 130:1dec54e4aec3 940 [..] To detect cumulative SCL stretch in master and slave mode, TIMEOUTB should be
mbed_official 130:1dec54e4aec3 941 configured (in accordance to SMBus specification) using I2C_TimeoutBConfig()
mbed_official 130:1dec54e4aec3 942 function then I2C_ExtendedClockTimeoutCmd() function should be called to enable
mbed_official 130:1dec54e4aec3 943 the detection.
mbed_official 130:1dec54e4aec3 944
mbed_official 130:1dec54e4aec3 945 [..] SCL low timeout is detected by configuring TIMEOUTB using I2C_TimeoutBConfig()
mbed_official 130:1dec54e4aec3 946 function followed by the call of I2C_ClockTimeoutCmd(). When adding to this
mbed_official 130:1dec54e4aec3 947 procedure the call of I2C_IdleClockTimeoutCmd() function, Bus Idle condition
mbed_official 130:1dec54e4aec3 948 (both SCL and SDA high) is detected also.
mbed_official 130:1dec54e4aec3 949
mbed_official 130:1dec54e4aec3 950 @endverbatim
mbed_official 130:1dec54e4aec3 951 * @{
mbed_official 130:1dec54e4aec3 952 */
mbed_official 130:1dec54e4aec3 953
mbed_official 130:1dec54e4aec3 954 /**
mbed_official 130:1dec54e4aec3 955 * @brief Enables or disables I2C SMBus alert.
mbed_official 130:1dec54e4aec3 956 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 957 * @param NewState: new state of the I2Cx SMBus alert.
mbed_official 130:1dec54e4aec3 958 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 959 * @retval None
mbed_official 130:1dec54e4aec3 960 */
mbed_official 130:1dec54e4aec3 961 void I2C_SMBusAlertCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 962 {
mbed_official 130:1dec54e4aec3 963 /* Check the parameters */
mbed_official 130:1dec54e4aec3 964 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 965 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 966
mbed_official 130:1dec54e4aec3 967 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 968 {
mbed_official 130:1dec54e4aec3 969 /* Enable SMBus alert */
mbed_official 130:1dec54e4aec3 970 I2Cx->CR1 |= I2C_CR1_ALERTEN;
mbed_official 130:1dec54e4aec3 971 }
mbed_official 130:1dec54e4aec3 972 else
mbed_official 130:1dec54e4aec3 973 {
mbed_official 130:1dec54e4aec3 974 /* Disable SMBus alert */
mbed_official 130:1dec54e4aec3 975 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_ALERTEN);
mbed_official 130:1dec54e4aec3 976 }
mbed_official 130:1dec54e4aec3 977 }
mbed_official 130:1dec54e4aec3 978
mbed_official 130:1dec54e4aec3 979 /**
mbed_official 130:1dec54e4aec3 980 * @brief Enables or disables I2C Clock Timeout (SCL Timeout detection).
mbed_official 130:1dec54e4aec3 981 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 982 * @param NewState: new state of the I2Cx clock Timeout.
mbed_official 130:1dec54e4aec3 983 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 984 * @retval None
mbed_official 130:1dec54e4aec3 985 */
mbed_official 130:1dec54e4aec3 986 void I2C_ClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 987 {
mbed_official 130:1dec54e4aec3 988 /* Check the parameters */
mbed_official 130:1dec54e4aec3 989 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 990 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 991
mbed_official 130:1dec54e4aec3 992 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 993 {
mbed_official 130:1dec54e4aec3 994 /* Enable Clock Timeout */
mbed_official 130:1dec54e4aec3 995 I2Cx->TIMEOUTR |= I2C_TIMEOUTR_TIMOUTEN;
mbed_official 130:1dec54e4aec3 996 }
mbed_official 130:1dec54e4aec3 997 else
mbed_official 130:1dec54e4aec3 998 {
mbed_official 130:1dec54e4aec3 999 /* Disable Clock Timeout */
mbed_official 130:1dec54e4aec3 1000 I2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIMOUTEN);
mbed_official 130:1dec54e4aec3 1001 }
mbed_official 130:1dec54e4aec3 1002 }
mbed_official 130:1dec54e4aec3 1003
mbed_official 130:1dec54e4aec3 1004 /**
mbed_official 130:1dec54e4aec3 1005 * @brief Enables or disables I2C Extended Clock Timeout (SCL cumulative Timeout detection).
mbed_official 130:1dec54e4aec3 1006 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1007 * @param NewState: new state of the I2Cx Extended clock Timeout.
mbed_official 130:1dec54e4aec3 1008 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 1009 * @retval None
mbed_official 130:1dec54e4aec3 1010 */
mbed_official 130:1dec54e4aec3 1011 void I2C_ExtendedClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 1012 {
mbed_official 130:1dec54e4aec3 1013 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1014 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1015 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 1016
mbed_official 130:1dec54e4aec3 1017 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 1018 {
mbed_official 130:1dec54e4aec3 1019 /* Enable Clock Timeout */
mbed_official 130:1dec54e4aec3 1020 I2Cx->TIMEOUTR |= I2C_TIMEOUTR_TEXTEN;
mbed_official 130:1dec54e4aec3 1021 }
mbed_official 130:1dec54e4aec3 1022 else
mbed_official 130:1dec54e4aec3 1023 {
mbed_official 130:1dec54e4aec3 1024 /* Disable Clock Timeout */
mbed_official 130:1dec54e4aec3 1025 I2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TEXTEN);
mbed_official 130:1dec54e4aec3 1026 }
mbed_official 130:1dec54e4aec3 1027 }
mbed_official 130:1dec54e4aec3 1028
mbed_official 130:1dec54e4aec3 1029 /**
mbed_official 130:1dec54e4aec3 1030 * @brief Enables or disables I2C Idle Clock Timeout (Bus idle SCL and SDA
mbed_official 130:1dec54e4aec3 1031 * high detection).
mbed_official 130:1dec54e4aec3 1032 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1033 * @param NewState: new state of the I2Cx Idle clock Timeout.
mbed_official 130:1dec54e4aec3 1034 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 1035 * @retval None
mbed_official 130:1dec54e4aec3 1036 */
mbed_official 130:1dec54e4aec3 1037 void I2C_IdleClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 1038 {
mbed_official 130:1dec54e4aec3 1039 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1040 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1041 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 1042
mbed_official 130:1dec54e4aec3 1043 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 1044 {
mbed_official 130:1dec54e4aec3 1045 /* Enable Clock Timeout */
mbed_official 130:1dec54e4aec3 1046 I2Cx->TIMEOUTR |= I2C_TIMEOUTR_TIDLE;
mbed_official 130:1dec54e4aec3 1047 }
mbed_official 130:1dec54e4aec3 1048 else
mbed_official 130:1dec54e4aec3 1049 {
mbed_official 130:1dec54e4aec3 1050 /* Disable Clock Timeout */
mbed_official 130:1dec54e4aec3 1051 I2Cx->TIMEOUTR &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIDLE);
mbed_official 130:1dec54e4aec3 1052 }
mbed_official 130:1dec54e4aec3 1053 }
mbed_official 130:1dec54e4aec3 1054
mbed_official 130:1dec54e4aec3 1055 /**
mbed_official 130:1dec54e4aec3 1056 * @brief Configures the I2C Bus Timeout A (SCL Timeout when TIDLE = 0 or Bus
mbed_official 130:1dec54e4aec3 1057 * idle SCL and SDA high when TIDLE = 1).
mbed_official 130:1dec54e4aec3 1058 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1059 * @param Timeout: specifies the TimeoutA to be programmed.
mbed_official 130:1dec54e4aec3 1060 * @retval None
mbed_official 130:1dec54e4aec3 1061 */
mbed_official 130:1dec54e4aec3 1062 void I2C_TimeoutAConfig(I2C_TypeDef* I2Cx, uint16_t Timeout)
mbed_official 130:1dec54e4aec3 1063 {
mbed_official 130:1dec54e4aec3 1064 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 1065
mbed_official 130:1dec54e4aec3 1066 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1067 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1068 assert_param(IS_I2C_TIMEOUT(Timeout));
mbed_official 130:1dec54e4aec3 1069
mbed_official 130:1dec54e4aec3 1070 /* Get the old register value */
mbed_official 130:1dec54e4aec3 1071 tmpreg = I2Cx->TIMEOUTR;
mbed_official 130:1dec54e4aec3 1072
mbed_official 130:1dec54e4aec3 1073 /* Reset I2Cx TIMEOUTA bit [11:0] */
mbed_official 130:1dec54e4aec3 1074 tmpreg &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIMEOUTA);
mbed_official 130:1dec54e4aec3 1075
mbed_official 130:1dec54e4aec3 1076 /* Set I2Cx TIMEOUTA */
mbed_official 130:1dec54e4aec3 1077 tmpreg |= (uint32_t)((uint32_t)Timeout & I2C_TIMEOUTR_TIMEOUTA) ;
mbed_official 130:1dec54e4aec3 1078
mbed_official 130:1dec54e4aec3 1079 /* Store the new register value */
mbed_official 130:1dec54e4aec3 1080 I2Cx->TIMEOUTR = tmpreg;
mbed_official 130:1dec54e4aec3 1081 }
mbed_official 130:1dec54e4aec3 1082
mbed_official 130:1dec54e4aec3 1083 /**
mbed_official 130:1dec54e4aec3 1084 * @brief Configures the I2C Bus Timeout B (SCL cumulative Timeout).
mbed_official 130:1dec54e4aec3 1085 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1086 * @param Timeout: specifies the TimeoutB to be programmed.
mbed_official 130:1dec54e4aec3 1087 * @retval None
mbed_official 130:1dec54e4aec3 1088 */
mbed_official 130:1dec54e4aec3 1089 void I2C_TimeoutBConfig(I2C_TypeDef* I2Cx, uint16_t Timeout)
mbed_official 130:1dec54e4aec3 1090 {
mbed_official 130:1dec54e4aec3 1091 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 1092
mbed_official 130:1dec54e4aec3 1093 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1094 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1095 assert_param(IS_I2C_TIMEOUT(Timeout));
mbed_official 130:1dec54e4aec3 1096
mbed_official 130:1dec54e4aec3 1097 /* Get the old register value */
mbed_official 130:1dec54e4aec3 1098 tmpreg = I2Cx->TIMEOUTR;
mbed_official 130:1dec54e4aec3 1099
mbed_official 130:1dec54e4aec3 1100 /* Reset I2Cx TIMEOUTB bit [11:0] */
mbed_official 130:1dec54e4aec3 1101 tmpreg &= (uint32_t)~((uint32_t)I2C_TIMEOUTR_TIMEOUTB);
mbed_official 130:1dec54e4aec3 1102
mbed_official 130:1dec54e4aec3 1103 /* Set I2Cx TIMEOUTB */
mbed_official 130:1dec54e4aec3 1104 tmpreg |= (uint32_t)(((uint32_t)Timeout << 16) & I2C_TIMEOUTR_TIMEOUTB) ;
mbed_official 130:1dec54e4aec3 1105
mbed_official 130:1dec54e4aec3 1106 /* Store the new register value */
mbed_official 130:1dec54e4aec3 1107 I2Cx->TIMEOUTR = tmpreg;
mbed_official 130:1dec54e4aec3 1108 }
mbed_official 130:1dec54e4aec3 1109
mbed_official 130:1dec54e4aec3 1110 /**
mbed_official 130:1dec54e4aec3 1111 * @brief Enables or disables I2C PEC calculation.
mbed_official 130:1dec54e4aec3 1112 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1113 * @param NewState: new state of the I2Cx PEC calculation.
mbed_official 130:1dec54e4aec3 1114 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 1115 * @retval None
mbed_official 130:1dec54e4aec3 1116 */
mbed_official 130:1dec54e4aec3 1117 void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 1118 {
mbed_official 130:1dec54e4aec3 1119 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1120 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1121 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 1122
mbed_official 130:1dec54e4aec3 1123 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 1124 {
mbed_official 130:1dec54e4aec3 1125 /* Enable PEC calculation */
mbed_official 130:1dec54e4aec3 1126 I2Cx->CR1 |= I2C_CR1_PECEN;
mbed_official 130:1dec54e4aec3 1127 }
mbed_official 130:1dec54e4aec3 1128 else
mbed_official 130:1dec54e4aec3 1129 {
mbed_official 130:1dec54e4aec3 1130 /* Disable PEC calculation */
mbed_official 130:1dec54e4aec3 1131 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR1_PECEN);
mbed_official 130:1dec54e4aec3 1132 }
mbed_official 130:1dec54e4aec3 1133 }
mbed_official 130:1dec54e4aec3 1134
mbed_official 130:1dec54e4aec3 1135 /**
mbed_official 130:1dec54e4aec3 1136 * @brief Enables or disables I2C PEC transmission/reception request.
mbed_official 130:1dec54e4aec3 1137 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1138 * @param NewState: new state of the I2Cx PEC request.
mbed_official 130:1dec54e4aec3 1139 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 1140 * @retval None
mbed_official 130:1dec54e4aec3 1141 */
mbed_official 130:1dec54e4aec3 1142 void I2C_PECRequestCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 1143 {
mbed_official 130:1dec54e4aec3 1144 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1145 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1146 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 1147
mbed_official 130:1dec54e4aec3 1148 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 1149 {
mbed_official 130:1dec54e4aec3 1150 /* Enable PEC transmission/reception request */
mbed_official 130:1dec54e4aec3 1151 I2Cx->CR1 |= I2C_CR2_PECBYTE;
mbed_official 130:1dec54e4aec3 1152 }
mbed_official 130:1dec54e4aec3 1153 else
mbed_official 130:1dec54e4aec3 1154 {
mbed_official 130:1dec54e4aec3 1155 /* Disable PEC transmission/reception request */
mbed_official 130:1dec54e4aec3 1156 I2Cx->CR1 &= (uint32_t)~((uint32_t)I2C_CR2_PECBYTE);
mbed_official 130:1dec54e4aec3 1157 }
mbed_official 130:1dec54e4aec3 1158 }
mbed_official 130:1dec54e4aec3 1159
mbed_official 130:1dec54e4aec3 1160 /**
mbed_official 130:1dec54e4aec3 1161 * @brief Returns the I2C PEC.
mbed_official 130:1dec54e4aec3 1162 * @param I2Cx: where x can be 1 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1163 * @retval The value of the PEC .
mbed_official 130:1dec54e4aec3 1164 */
mbed_official 130:1dec54e4aec3 1165 uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx)
mbed_official 130:1dec54e4aec3 1166 {
mbed_official 130:1dec54e4aec3 1167 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1168 assert_param(IS_I2C_1_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1169
mbed_official 130:1dec54e4aec3 1170 /* Return the slave matched address in the SR1 register */
mbed_official 130:1dec54e4aec3 1171 return (uint8_t)((uint32_t)I2Cx->PECR & I2C_PECR_PEC);
mbed_official 130:1dec54e4aec3 1172 }
mbed_official 130:1dec54e4aec3 1173
mbed_official 130:1dec54e4aec3 1174 /**
mbed_official 130:1dec54e4aec3 1175 * @}
mbed_official 130:1dec54e4aec3 1176 */
mbed_official 130:1dec54e4aec3 1177
mbed_official 130:1dec54e4aec3 1178
mbed_official 130:1dec54e4aec3 1179 /** @defgroup I2C_Group4 I2C registers management functions
mbed_official 130:1dec54e4aec3 1180 * @brief I2C registers management functions
mbed_official 130:1dec54e4aec3 1181 *
mbed_official 130:1dec54e4aec3 1182 @verbatim
mbed_official 130:1dec54e4aec3 1183 ===============================================================================
mbed_official 130:1dec54e4aec3 1184 ##### I2C registers management functions #####
mbed_official 130:1dec54e4aec3 1185 ===============================================================================
mbed_official 130:1dec54e4aec3 1186 [..] This section provides a functions that allow user the management of
mbed_official 130:1dec54e4aec3 1187 I2C registers.
mbed_official 130:1dec54e4aec3 1188
mbed_official 130:1dec54e4aec3 1189 @endverbatim
mbed_official 130:1dec54e4aec3 1190 * @{
mbed_official 130:1dec54e4aec3 1191 */
mbed_official 130:1dec54e4aec3 1192
mbed_official 130:1dec54e4aec3 1193 /**
mbed_official 130:1dec54e4aec3 1194 * @brief Reads the specified I2C register and returns its value.
mbed_official 130:1dec54e4aec3 1195 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1196 * @param I2C_Register: specifies the register to read.
mbed_official 130:1dec54e4aec3 1197 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 1198 * @arg I2C_Register_CR1: CR1 register.
mbed_official 130:1dec54e4aec3 1199 * @arg I2C_Register_CR2: CR2 register.
mbed_official 130:1dec54e4aec3 1200 * @arg I2C_Register_OAR1: OAR1 register.
mbed_official 130:1dec54e4aec3 1201 * @arg I2C_Register_OAR2: OAR2 register.
mbed_official 130:1dec54e4aec3 1202 * @arg I2C_Register_TIMINGR: TIMING register.
mbed_official 130:1dec54e4aec3 1203 * @arg I2C_Register_TIMEOUTR: TIMEOUTR register.
mbed_official 130:1dec54e4aec3 1204 * @arg I2C_Register_ISR: ISR register.
mbed_official 130:1dec54e4aec3 1205 * @arg I2C_Register_ICR: ICR register.
mbed_official 130:1dec54e4aec3 1206 * @arg I2C_Register_PECR: PECR register.
mbed_official 130:1dec54e4aec3 1207 * @arg I2C_Register_RXDR: RXDR register.
mbed_official 130:1dec54e4aec3 1208 * @arg I2C_Register_TXDR: TXDR register.
mbed_official 130:1dec54e4aec3 1209 * @retval The value of the read register.
mbed_official 130:1dec54e4aec3 1210 */
mbed_official 130:1dec54e4aec3 1211 uint32_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
mbed_official 130:1dec54e4aec3 1212 {
mbed_official 130:1dec54e4aec3 1213 __IO uint32_t tmp = 0;
mbed_official 130:1dec54e4aec3 1214
mbed_official 130:1dec54e4aec3 1215 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1216 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1217 assert_param(IS_I2C_REGISTER(I2C_Register));
mbed_official 130:1dec54e4aec3 1218
mbed_official 130:1dec54e4aec3 1219 tmp = (uint32_t)I2Cx;
mbed_official 130:1dec54e4aec3 1220 tmp += I2C_Register;
mbed_official 130:1dec54e4aec3 1221
mbed_official 130:1dec54e4aec3 1222 /* Return the selected register value */
mbed_official 130:1dec54e4aec3 1223 return (*(__IO uint32_t *) tmp);
mbed_official 130:1dec54e4aec3 1224 }
mbed_official 130:1dec54e4aec3 1225
mbed_official 130:1dec54e4aec3 1226 /**
mbed_official 130:1dec54e4aec3 1227 * @}
mbed_official 130:1dec54e4aec3 1228 */
mbed_official 130:1dec54e4aec3 1229
mbed_official 130:1dec54e4aec3 1230 /** @defgroup I2C_Group5 Data transfers management functions
mbed_official 130:1dec54e4aec3 1231 * @brief Data transfers management functions
mbed_official 130:1dec54e4aec3 1232 *
mbed_official 130:1dec54e4aec3 1233 @verbatim
mbed_official 130:1dec54e4aec3 1234 ===============================================================================
mbed_official 130:1dec54e4aec3 1235 ##### Data transfers management functions #####
mbed_official 130:1dec54e4aec3 1236 ===============================================================================
mbed_official 130:1dec54e4aec3 1237 [..] This subsection provides a set of functions allowing to manage
mbed_official 130:1dec54e4aec3 1238 the I2C data transfers.
mbed_official 130:1dec54e4aec3 1239
mbed_official 130:1dec54e4aec3 1240 [..] The read access of the I2C_RXDR register can be done using
mbed_official 130:1dec54e4aec3 1241 the I2C_ReceiveData() function and returns the received value.
mbed_official 130:1dec54e4aec3 1242 Whereas a write access to the I2C_TXDR can be done using I2C_SendData()
mbed_official 130:1dec54e4aec3 1243 function and stores the written data into TXDR.
mbed_official 130:1dec54e4aec3 1244 @endverbatim
mbed_official 130:1dec54e4aec3 1245 * @{
mbed_official 130:1dec54e4aec3 1246 */
mbed_official 130:1dec54e4aec3 1247
mbed_official 130:1dec54e4aec3 1248 /**
mbed_official 130:1dec54e4aec3 1249 * @brief Sends a data byte through the I2Cx peripheral.
mbed_official 130:1dec54e4aec3 1250 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1251 * @param Data: Byte to be transmitted..
mbed_official 130:1dec54e4aec3 1252 * @retval None
mbed_official 130:1dec54e4aec3 1253 */
mbed_official 130:1dec54e4aec3 1254 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
mbed_official 130:1dec54e4aec3 1255 {
mbed_official 130:1dec54e4aec3 1256 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1257 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1258
mbed_official 130:1dec54e4aec3 1259 /* Write in the DR register the data to be sent */
mbed_official 130:1dec54e4aec3 1260 I2Cx->TXDR = (uint8_t)Data;
mbed_official 130:1dec54e4aec3 1261 }
mbed_official 130:1dec54e4aec3 1262
mbed_official 130:1dec54e4aec3 1263 /**
mbed_official 130:1dec54e4aec3 1264 * @brief Returns the most recent received data by the I2Cx peripheral.
mbed_official 130:1dec54e4aec3 1265 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1266 * @retval The value of the received data.
mbed_official 130:1dec54e4aec3 1267 */
mbed_official 130:1dec54e4aec3 1268 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
mbed_official 130:1dec54e4aec3 1269 {
mbed_official 130:1dec54e4aec3 1270 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1271 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1272
mbed_official 130:1dec54e4aec3 1273 /* Return the data in the DR register */
mbed_official 130:1dec54e4aec3 1274 return (uint8_t)I2Cx->RXDR;
mbed_official 130:1dec54e4aec3 1275 }
mbed_official 130:1dec54e4aec3 1276
mbed_official 130:1dec54e4aec3 1277 /**
mbed_official 130:1dec54e4aec3 1278 * @}
mbed_official 130:1dec54e4aec3 1279 */
mbed_official 130:1dec54e4aec3 1280
mbed_official 130:1dec54e4aec3 1281
mbed_official 130:1dec54e4aec3 1282 /** @defgroup I2C_Group6 DMA transfers management functions
mbed_official 130:1dec54e4aec3 1283 * @brief DMA transfers management functions
mbed_official 130:1dec54e4aec3 1284 *
mbed_official 130:1dec54e4aec3 1285 @verbatim
mbed_official 130:1dec54e4aec3 1286 ===============================================================================
mbed_official 130:1dec54e4aec3 1287 ##### DMA transfers management functions #####
mbed_official 130:1dec54e4aec3 1288 ===============================================================================
mbed_official 130:1dec54e4aec3 1289 [..] This section provides two functions that can be used only in DMA mode.
mbed_official 130:1dec54e4aec3 1290 [..] In DMA Mode, the I2C communication can be managed by 2 DMA Channel
mbed_official 130:1dec54e4aec3 1291 requests:
mbed_official 130:1dec54e4aec3 1292 (#) I2C_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
mbed_official 130:1dec54e4aec3 1293 (#) I2C_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
mbed_official 130:1dec54e4aec3 1294 [..] In this Mode it is advised to use the following function:
mbed_official 130:1dec54e4aec3 1295 (+) I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState);
mbed_official 130:1dec54e4aec3 1296 @endverbatim
mbed_official 130:1dec54e4aec3 1297 * @{
mbed_official 130:1dec54e4aec3 1298 */
mbed_official 130:1dec54e4aec3 1299
mbed_official 130:1dec54e4aec3 1300 /**
mbed_official 130:1dec54e4aec3 1301 * @brief Enables or disables the I2C DMA interface.
mbed_official 130:1dec54e4aec3 1302 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1303 * @param I2C_DMAReq: specifies the I2C DMA transfer request to be enabled or disabled.
mbed_official 130:1dec54e4aec3 1304 * This parameter can be any combination of the following values:
mbed_official 130:1dec54e4aec3 1305 * @arg I2C_DMAReq_Tx: Tx DMA transfer request
mbed_official 130:1dec54e4aec3 1306 * @arg I2C_DMAReq_Rx: Rx DMA transfer request
mbed_official 130:1dec54e4aec3 1307 * @param NewState: new state of the selected I2C DMA transfer request.
mbed_official 130:1dec54e4aec3 1308 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 1309 * @retval None
mbed_official 130:1dec54e4aec3 1310 */
mbed_official 130:1dec54e4aec3 1311 void I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 1312 {
mbed_official 130:1dec54e4aec3 1313 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1314 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1315 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 1316 assert_param(IS_I2C_DMA_REQ(I2C_DMAReq));
mbed_official 130:1dec54e4aec3 1317
mbed_official 130:1dec54e4aec3 1318 if (NewState != DISABLE)
mbed_official 130:1dec54e4aec3 1319 {
mbed_official 130:1dec54e4aec3 1320 /* Enable the selected I2C DMA requests */
mbed_official 130:1dec54e4aec3 1321 I2Cx->CR1 |= I2C_DMAReq;
mbed_official 130:1dec54e4aec3 1322 }
mbed_official 130:1dec54e4aec3 1323 else
mbed_official 130:1dec54e4aec3 1324 {
mbed_official 130:1dec54e4aec3 1325 /* Disable the selected I2C DMA requests */
mbed_official 130:1dec54e4aec3 1326 I2Cx->CR1 &= (uint32_t)~I2C_DMAReq;
mbed_official 130:1dec54e4aec3 1327 }
mbed_official 130:1dec54e4aec3 1328 }
mbed_official 130:1dec54e4aec3 1329 /**
mbed_official 130:1dec54e4aec3 1330 * @}
mbed_official 130:1dec54e4aec3 1331 */
mbed_official 130:1dec54e4aec3 1332
mbed_official 130:1dec54e4aec3 1333
mbed_official 130:1dec54e4aec3 1334 /** @defgroup I2C_Group7 Interrupts and flags management functions
mbed_official 130:1dec54e4aec3 1335 * @brief Interrupts and flags management functions
mbed_official 130:1dec54e4aec3 1336 *
mbed_official 130:1dec54e4aec3 1337 @verbatim
mbed_official 130:1dec54e4aec3 1338 ===============================================================================
mbed_official 130:1dec54e4aec3 1339 ##### Interrupts and flags management functions #####
mbed_official 130:1dec54e4aec3 1340 ===============================================================================
mbed_official 130:1dec54e4aec3 1341 [..] This section provides functions allowing to configure the I2C Interrupts
mbed_official 130:1dec54e4aec3 1342 sources and check or clear the flags or pending bits status.
mbed_official 130:1dec54e4aec3 1343 The user should identify which mode will be used in his application to manage
mbed_official 130:1dec54e4aec3 1344 the communication: Polling mode, Interrupt mode or DMA mode(refer I2C_Group6).
mbed_official 130:1dec54e4aec3 1345
mbed_official 130:1dec54e4aec3 1346 *** Polling Mode ***
mbed_official 130:1dec54e4aec3 1347 ====================
mbed_official 130:1dec54e4aec3 1348 [..] In Polling Mode, the I2C communication can be managed by 15 flags:
mbed_official 130:1dec54e4aec3 1349 (#) I2C_FLAG_TXE: to indicate the status of Transmit data register empty flag.
mbed_official 130:1dec54e4aec3 1350 (#) I2C_FLAG_TXIS: to indicate the status of Transmit interrupt status flag .
mbed_official 130:1dec54e4aec3 1351 (#) I2C_FLAG_RXNE: to indicate the status of Receive data register not empty flag.
mbed_official 130:1dec54e4aec3 1352 (#) I2C_FLAG_ADDR: to indicate the status of Address matched flag (slave mode).
mbed_official 130:1dec54e4aec3 1353 (#) I2C_FLAG_NACKF: to indicate the status of NACK received flag.
mbed_official 130:1dec54e4aec3 1354 (#) I2C_FLAG_STOPF: to indicate the status of STOP detection flag.
mbed_official 130:1dec54e4aec3 1355 (#) I2C_FLAG_TC: to indicate the status of Transfer complete flag(master mode).
mbed_official 130:1dec54e4aec3 1356 (#) I2C_FLAG_TCR: to indicate the status of Transfer complete reload flag.
mbed_official 130:1dec54e4aec3 1357 (#) I2C_FLAG_BERR: to indicate the status of Bus error flag.
mbed_official 130:1dec54e4aec3 1358 (#) I2C_FLAG_ARLO: to indicate the status of Arbitration lost flag.
mbed_official 130:1dec54e4aec3 1359 (#) I2C_FLAG_OVR: to indicate the status of Overrun/Underrun flag.
mbed_official 130:1dec54e4aec3 1360 (#) I2C_FLAG_PECERR: to indicate the status of PEC error in reception flag.
mbed_official 130:1dec54e4aec3 1361 (#) I2C_FLAG_TIMEOUT: to indicate the status of Timeout or Tlow detection flag.
mbed_official 130:1dec54e4aec3 1362 (#) I2C_FLAG_ALERT: to indicate the status of SMBus Alert flag.
mbed_official 130:1dec54e4aec3 1363 (#) I2C_FLAG_BUSY: to indicate the status of Bus busy flag.
mbed_official 130:1dec54e4aec3 1364
mbed_official 130:1dec54e4aec3 1365 [..] In this Mode it is advised to use the following functions:
mbed_official 130:1dec54e4aec3 1366 (+) FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
mbed_official 130:1dec54e4aec3 1367 (+) void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
mbed_official 130:1dec54e4aec3 1368
mbed_official 130:1dec54e4aec3 1369 [..]
mbed_official 130:1dec54e4aec3 1370 (@)Do not use the BUSY flag to handle each data transmission or reception.It is
mbed_official 130:1dec54e4aec3 1371 better to use the TXIS and RXNE flags instead.
mbed_official 130:1dec54e4aec3 1372
mbed_official 130:1dec54e4aec3 1373 *** Interrupt Mode ***
mbed_official 130:1dec54e4aec3 1374 ======================
mbed_official 130:1dec54e4aec3 1375 [..] In Interrupt Mode, the I2C communication can be managed by 7 interrupt sources
mbed_official 130:1dec54e4aec3 1376 and 15 pending bits:
mbed_official 130:1dec54e4aec3 1377 [..] Interrupt Source:
mbed_official 130:1dec54e4aec3 1378 (#) I2C_IT_ERRI: specifies the interrupt source for the Error interrupt.
mbed_official 130:1dec54e4aec3 1379 (#) I2C_IT_TCI: specifies the interrupt source for the Transfer Complete interrupt.
mbed_official 130:1dec54e4aec3 1380 (#) I2C_IT_STOPI: specifies the interrupt source for the Stop Detection interrupt.
mbed_official 130:1dec54e4aec3 1381 (#) I2C_IT_NACKI: specifies the interrupt source for the Not Acknowledge received interrupt.
mbed_official 130:1dec54e4aec3 1382 (#) I2C_IT_ADDRI: specifies the interrupt source for the Address Match interrupt.
mbed_official 130:1dec54e4aec3 1383 (#) I2C_IT_RXI: specifies the interrupt source for the RX interrupt.
mbed_official 130:1dec54e4aec3 1384 (#) I2C_IT_TXI: specifies the interrupt source for the TX interrupt.
mbed_official 130:1dec54e4aec3 1385
mbed_official 130:1dec54e4aec3 1386 [..] Pending Bits:
mbed_official 130:1dec54e4aec3 1387 (#) I2C_IT_TXIS: to indicate the status of Transmit interrupt status flag.
mbed_official 130:1dec54e4aec3 1388 (#) I2C_IT_RXNE: to indicate the status of Receive data register not empty flag.
mbed_official 130:1dec54e4aec3 1389 (#) I2C_IT_ADDR: to indicate the status of Address matched flag (slave mode).
mbed_official 130:1dec54e4aec3 1390 (#) I2C_IT_NACKF: to indicate the status of NACK received flag.
mbed_official 130:1dec54e4aec3 1391 (#) I2C_IT_STOPF: to indicate the status of STOP detection flag.
mbed_official 130:1dec54e4aec3 1392 (#) I2C_IT_TC: to indicate the status of Transfer complete flag (master mode).
mbed_official 130:1dec54e4aec3 1393 (#) I2C_IT_TCR: to indicate the status of Transfer complete reload flag.
mbed_official 130:1dec54e4aec3 1394 (#) I2C_IT_BERR: to indicate the status of Bus error flag.
mbed_official 130:1dec54e4aec3 1395 (#) I2C_IT_ARLO: to indicate the status of Arbitration lost flag.
mbed_official 130:1dec54e4aec3 1396 (#) I2C_IT_OVR: to indicate the status of Overrun/Underrun flag.
mbed_official 130:1dec54e4aec3 1397 (#) I2C_IT_PECERR: to indicate the status of PEC error in reception flag.
mbed_official 130:1dec54e4aec3 1398 (#) I2C_IT_TIMEOUT: to indicate the status of Timeout or Tlow detection flag.
mbed_official 130:1dec54e4aec3 1399 (#) I2C_IT_ALERT: to indicate the status of SMBus Alert flag.
mbed_official 130:1dec54e4aec3 1400
mbed_official 130:1dec54e4aec3 1401 [..] In this Mode it is advised to use the following functions:
mbed_official 130:1dec54e4aec3 1402 (+) void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
mbed_official 130:1dec54e4aec3 1403 (+) ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
mbed_official 130:1dec54e4aec3 1404
mbed_official 130:1dec54e4aec3 1405 @endverbatim
mbed_official 130:1dec54e4aec3 1406 * @{
mbed_official 130:1dec54e4aec3 1407 */
mbed_official 130:1dec54e4aec3 1408
mbed_official 130:1dec54e4aec3 1409 /**
mbed_official 130:1dec54e4aec3 1410 * @brief Checks whether the specified I2C flag is set or not.
mbed_official 130:1dec54e4aec3 1411 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1412 * @param I2C_FLAG: specifies the flag to check.
mbed_official 130:1dec54e4aec3 1413 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 1414 * @arg I2C_FLAG_TXE: Transmit data register empty
mbed_official 130:1dec54e4aec3 1415 * @arg I2C_FLAG_TXIS: Transmit interrupt status
mbed_official 130:1dec54e4aec3 1416 * @arg I2C_FLAG_RXNE: Receive data register not empty
mbed_official 130:1dec54e4aec3 1417 * @arg I2C_FLAG_ADDR: Address matched (slave mode)
mbed_official 130:1dec54e4aec3 1418 * @arg I2C_FLAG_NACKF: NACK received flag
mbed_official 130:1dec54e4aec3 1419 * @arg I2C_FLAG_STOPF: STOP detection flag
mbed_official 130:1dec54e4aec3 1420 * @arg I2C_FLAG_TC: Transfer complete (master mode)
mbed_official 130:1dec54e4aec3 1421 * @arg I2C_FLAG_TCR: Transfer complete reload
mbed_official 130:1dec54e4aec3 1422 * @arg I2C_FLAG_BERR: Bus error
mbed_official 130:1dec54e4aec3 1423 * @arg I2C_FLAG_ARLO: Arbitration lost
mbed_official 130:1dec54e4aec3 1424 * @arg I2C_FLAG_OVR: Overrun/Underrun
mbed_official 130:1dec54e4aec3 1425 * @arg I2C_FLAG_PECERR: PEC error in reception
mbed_official 130:1dec54e4aec3 1426 * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
mbed_official 130:1dec54e4aec3 1427 * @arg I2C_FLAG_ALERT: SMBus Alert
mbed_official 130:1dec54e4aec3 1428 * @arg I2C_FLAG_BUSY: Bus busy
mbed_official 130:1dec54e4aec3 1429 * @retval The new state of I2C_FLAG (SET or RESET).
mbed_official 130:1dec54e4aec3 1430 */
mbed_official 130:1dec54e4aec3 1431 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
mbed_official 130:1dec54e4aec3 1432 {
mbed_official 130:1dec54e4aec3 1433 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 1434 FlagStatus bitstatus = RESET;
mbed_official 130:1dec54e4aec3 1435
mbed_official 130:1dec54e4aec3 1436 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1437 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1438 assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
mbed_official 130:1dec54e4aec3 1439
mbed_official 130:1dec54e4aec3 1440 /* Get the ISR register value */
mbed_official 130:1dec54e4aec3 1441 tmpreg = I2Cx->ISR;
mbed_official 130:1dec54e4aec3 1442
mbed_official 130:1dec54e4aec3 1443 /* Get flag status */
mbed_official 130:1dec54e4aec3 1444 tmpreg &= I2C_FLAG;
mbed_official 130:1dec54e4aec3 1445
mbed_official 130:1dec54e4aec3 1446 if(tmpreg != 0)
mbed_official 130:1dec54e4aec3 1447 {
mbed_official 130:1dec54e4aec3 1448 /* I2C_FLAG is set */
mbed_official 130:1dec54e4aec3 1449 bitstatus = SET;
mbed_official 130:1dec54e4aec3 1450 }
mbed_official 130:1dec54e4aec3 1451 else
mbed_official 130:1dec54e4aec3 1452 {
mbed_official 130:1dec54e4aec3 1453 /* I2C_FLAG is reset */
mbed_official 130:1dec54e4aec3 1454 bitstatus = RESET;
mbed_official 130:1dec54e4aec3 1455 }
mbed_official 130:1dec54e4aec3 1456 return bitstatus;
mbed_official 130:1dec54e4aec3 1457 }
mbed_official 130:1dec54e4aec3 1458
mbed_official 130:1dec54e4aec3 1459 /**
mbed_official 130:1dec54e4aec3 1460 * @brief Clears the I2Cx's pending flags.
mbed_official 130:1dec54e4aec3 1461 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1462 * @param I2C_FLAG: specifies the flag to clear.
mbed_official 130:1dec54e4aec3 1463 * This parameter can be any combination of the following values:
mbed_official 130:1dec54e4aec3 1464 * @arg I2C_FLAG_ADDR: Address matched (slave mode)
mbed_official 130:1dec54e4aec3 1465 * @arg I2C_FLAG_NACKF: NACK received flag
mbed_official 130:1dec54e4aec3 1466 * @arg I2C_FLAG_STOPF: STOP detection flag
mbed_official 130:1dec54e4aec3 1467 * @arg I2C_FLAG_BERR: Bus error
mbed_official 130:1dec54e4aec3 1468 * @arg I2C_FLAG_ARLO: Arbitration lost
mbed_official 130:1dec54e4aec3 1469 * @arg I2C_FLAG_OVR: Overrun/Underrun
mbed_official 130:1dec54e4aec3 1470 * @arg I2C_FLAG_PECERR: PEC error in reception
mbed_official 130:1dec54e4aec3 1471 * @arg I2C_FLAG_TIMEOUT: Timeout or Tlow detection flag
mbed_official 130:1dec54e4aec3 1472 * @arg I2C_FLAG_ALERT: SMBus Alert
mbed_official 130:1dec54e4aec3 1473 * @retval The new state of I2C_FLAG (SET or RESET).
mbed_official 130:1dec54e4aec3 1474 */
mbed_official 130:1dec54e4aec3 1475 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
mbed_official 130:1dec54e4aec3 1476 {
mbed_official 130:1dec54e4aec3 1477 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1478 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1479 assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
mbed_official 130:1dec54e4aec3 1480
mbed_official 130:1dec54e4aec3 1481 /* Clear the selected flag */
mbed_official 130:1dec54e4aec3 1482 I2Cx->ICR = I2C_FLAG;
mbed_official 130:1dec54e4aec3 1483 }
mbed_official 130:1dec54e4aec3 1484
mbed_official 130:1dec54e4aec3 1485 /**
mbed_official 130:1dec54e4aec3 1486 * @brief Checks whether the specified I2C interrupt has occurred or not.
mbed_official 130:1dec54e4aec3 1487 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1488 * @param I2C_IT: specifies the interrupt source to check.
mbed_official 130:1dec54e4aec3 1489 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 1490 * @arg I2C_IT_TXIS: Transmit interrupt status
mbed_official 130:1dec54e4aec3 1491 * @arg I2C_IT_RXNE: Receive data register not empty
mbed_official 130:1dec54e4aec3 1492 * @arg I2C_IT_ADDR: Address matched (slave mode)
mbed_official 130:1dec54e4aec3 1493 * @arg I2C_IT_NACKF: NACK received flag
mbed_official 130:1dec54e4aec3 1494 * @arg I2C_IT_STOPF: STOP detection flag
mbed_official 130:1dec54e4aec3 1495 * @arg I2C_IT_TC: Transfer complete (master mode)
mbed_official 130:1dec54e4aec3 1496 * @arg I2C_IT_TCR: Transfer complete reload
mbed_official 130:1dec54e4aec3 1497 * @arg I2C_IT_BERR: Bus error
mbed_official 130:1dec54e4aec3 1498 * @arg I2C_IT_ARLO: Arbitration lost
mbed_official 130:1dec54e4aec3 1499 * @arg I2C_IT_OVR: Overrun/Underrun
mbed_official 130:1dec54e4aec3 1500 * @arg I2C_IT_PECERR: PEC error in reception
mbed_official 130:1dec54e4aec3 1501 * @arg I2C_IT_TIMEOUT: Timeout or Tlow detection flag
mbed_official 130:1dec54e4aec3 1502 * @arg I2C_IT_ALERT: SMBus Alert
mbed_official 130:1dec54e4aec3 1503 * @retval The new state of I2C_IT (SET or RESET).
mbed_official 130:1dec54e4aec3 1504 */
mbed_official 130:1dec54e4aec3 1505 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
mbed_official 130:1dec54e4aec3 1506 {
mbed_official 130:1dec54e4aec3 1507 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 1508 ITStatus bitstatus = RESET;
mbed_official 130:1dec54e4aec3 1509 uint32_t enablestatus = 0;
mbed_official 130:1dec54e4aec3 1510
mbed_official 130:1dec54e4aec3 1511 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1512 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1513 assert_param(IS_I2C_GET_IT(I2C_IT));
mbed_official 130:1dec54e4aec3 1514
mbed_official 130:1dec54e4aec3 1515 /* Check if the interrupt source is enabled or not */
mbed_official 130:1dec54e4aec3 1516 /* If Error interrupt */
mbed_official 130:1dec54e4aec3 1517 if ((uint32_t)(I2C_IT & ERROR_IT_MASK))
mbed_official 130:1dec54e4aec3 1518 {
mbed_official 130:1dec54e4aec3 1519 enablestatus = (uint32_t)((I2C_CR1_ERRIE) & (I2Cx->CR1));
mbed_official 130:1dec54e4aec3 1520 }
mbed_official 130:1dec54e4aec3 1521 /* If TC interrupt */
mbed_official 130:1dec54e4aec3 1522 else if ((uint32_t)(I2C_IT & TC_IT_MASK))
mbed_official 130:1dec54e4aec3 1523 {
mbed_official 130:1dec54e4aec3 1524 enablestatus = (uint32_t)((I2C_CR1_TCIE) & (I2Cx->CR1));
mbed_official 130:1dec54e4aec3 1525 }
mbed_official 130:1dec54e4aec3 1526 else
mbed_official 130:1dec54e4aec3 1527 {
mbed_official 130:1dec54e4aec3 1528 enablestatus = (uint32_t)((I2C_IT) & (I2Cx->CR1));
mbed_official 130:1dec54e4aec3 1529 }
mbed_official 130:1dec54e4aec3 1530
mbed_official 130:1dec54e4aec3 1531 /* Get the ISR register value */
mbed_official 130:1dec54e4aec3 1532 tmpreg = I2Cx->ISR;
mbed_official 130:1dec54e4aec3 1533
mbed_official 130:1dec54e4aec3 1534 /* Get flag status */
mbed_official 130:1dec54e4aec3 1535 tmpreg &= I2C_IT;
mbed_official 130:1dec54e4aec3 1536
mbed_official 130:1dec54e4aec3 1537 /* Check the status of the specified I2C flag */
mbed_official 130:1dec54e4aec3 1538 if((tmpreg != RESET) && enablestatus)
mbed_official 130:1dec54e4aec3 1539 {
mbed_official 130:1dec54e4aec3 1540 /* I2C_IT is set */
mbed_official 130:1dec54e4aec3 1541 bitstatus = SET;
mbed_official 130:1dec54e4aec3 1542 }
mbed_official 130:1dec54e4aec3 1543 else
mbed_official 130:1dec54e4aec3 1544 {
mbed_official 130:1dec54e4aec3 1545 /* I2C_IT is reset */
mbed_official 130:1dec54e4aec3 1546 bitstatus = RESET;
mbed_official 130:1dec54e4aec3 1547 }
mbed_official 130:1dec54e4aec3 1548
mbed_official 130:1dec54e4aec3 1549 /* Return the I2C_IT status */
mbed_official 130:1dec54e4aec3 1550 return bitstatus;
mbed_official 130:1dec54e4aec3 1551 }
mbed_official 130:1dec54e4aec3 1552
mbed_official 130:1dec54e4aec3 1553 /**
mbed_official 130:1dec54e4aec3 1554 * @brief Clears the I2Cx's interrupt pending bits.
mbed_official 130:1dec54e4aec3 1555 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 130:1dec54e4aec3 1556 * @param I2C_IT: specifies the interrupt pending bit to clear.
mbed_official 130:1dec54e4aec3 1557 * This parameter can be any combination of the following values:
mbed_official 130:1dec54e4aec3 1558 * @arg I2C_IT_ADDR: Address matched (slave mode)
mbed_official 130:1dec54e4aec3 1559 * @arg I2C_IT_NACKF: NACK received flag
mbed_official 130:1dec54e4aec3 1560 * @arg I2C_IT_STOPF: STOP detection flag
mbed_official 130:1dec54e4aec3 1561 * @arg I2C_IT_BERR: Bus error
mbed_official 130:1dec54e4aec3 1562 * @arg I2C_IT_ARLO: Arbitration lost
mbed_official 130:1dec54e4aec3 1563 * @arg I2C_IT_OVR: Overrun/Underrun
mbed_official 130:1dec54e4aec3 1564 * @arg I2C_IT_PECERR: PEC error in reception
mbed_official 130:1dec54e4aec3 1565 * @arg I2C_IT_TIMEOUT: Timeout or Tlow detection flag
mbed_official 130:1dec54e4aec3 1566 * @arg I2C_IT_ALERT: SMBus Alert
mbed_official 130:1dec54e4aec3 1567 * @retval The new state of I2C_IT (SET or RESET).
mbed_official 130:1dec54e4aec3 1568 */
mbed_official 130:1dec54e4aec3 1569 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
mbed_official 130:1dec54e4aec3 1570 {
mbed_official 130:1dec54e4aec3 1571 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1572 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
mbed_official 130:1dec54e4aec3 1573 assert_param(IS_I2C_CLEAR_IT(I2C_IT));
mbed_official 130:1dec54e4aec3 1574
mbed_official 130:1dec54e4aec3 1575 /* Clear the selected flag */
mbed_official 130:1dec54e4aec3 1576 I2Cx->ICR = I2C_IT;
mbed_official 130:1dec54e4aec3 1577 }
mbed_official 130:1dec54e4aec3 1578
mbed_official 130:1dec54e4aec3 1579 /**
mbed_official 130:1dec54e4aec3 1580 * @}
mbed_official 130:1dec54e4aec3 1581 */
mbed_official 130:1dec54e4aec3 1582
mbed_official 130:1dec54e4aec3 1583 /**
mbed_official 130:1dec54e4aec3 1584 * @}
mbed_official 130:1dec54e4aec3 1585 */
mbed_official 130:1dec54e4aec3 1586
mbed_official 130:1dec54e4aec3 1587 /**
mbed_official 130:1dec54e4aec3 1588 * @}
mbed_official 130:1dec54e4aec3 1589 */
mbed_official 130:1dec54e4aec3 1590
mbed_official 130:1dec54e4aec3 1591 /**
mbed_official 130:1dec54e4aec3 1592 * @}
mbed_official 130:1dec54e4aec3 1593 */
mbed_official 130:1dec54e4aec3 1594
mbed_official 130:1dec54e4aec3 1595 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/