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