mbed library with additional peripherals for ST F401 board

Fork of mbed-src by mbed official

This mbed LIB has additional peripherals for ST F401 board

  • UART2 : PA_3 rx, PA_2 tx
  • UART3 : PC_7 rx, PC_6 tx
  • I2C2 : PB_3 SDA, PB_10 SCL
  • I2C3 : PB_4 SDA, PA_8 SCL
Committer:
mbed_official
Date:
Mon Jan 27 14:30:07 2014 +0000
Revision:
76:aeb1df146756
Child:
80:66393a7b209d
Synchronized with git revision a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd

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

Add NUCLEO_L152RE

Who changed what in which revision?

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