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