Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more
targets/hal/TARGET_STM/TARGET_NUCLEO_F091RC/i2c_api.c@340:28d1f895c6fe, 2014-10-09 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Oct 09 08:15:07 2014 +0100
- Revision:
- 340:28d1f895c6fe
- Child:
- 414:4ec4c5b614b0
Synchronized with git revision b5a4c8e80393336b2656fb29ab46d405d3068602
Full URL: https://github.com/mbedmicro/mbed/commit/b5a4c8e80393336b2656fb29ab46d405d3068602/
HAL: nrf51822 - Few fixes for PWM and Serial
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 340:28d1f895c6fe | 1 | /* mbed Microcontroller Library |
| mbed_official | 340:28d1f895c6fe | 2 | ******************************************************************************* |
| mbed_official | 340:28d1f895c6fe | 3 | * Copyright (c) 2014, STMicroelectronics |
| mbed_official | 340:28d1f895c6fe | 4 | * All rights reserved. |
| mbed_official | 340:28d1f895c6fe | 5 | * |
| mbed_official | 340:28d1f895c6fe | 6 | * Redistribution and use in source and binary forms, with or without |
| mbed_official | 340:28d1f895c6fe | 7 | * modification, are permitted provided that the following conditions are met: |
| mbed_official | 340:28d1f895c6fe | 8 | * |
| mbed_official | 340:28d1f895c6fe | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
| mbed_official | 340:28d1f895c6fe | 10 | * this list of conditions and the following disclaimer. |
| mbed_official | 340:28d1f895c6fe | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| mbed_official | 340:28d1f895c6fe | 12 | * this list of conditions and the following disclaimer in the documentation |
| mbed_official | 340:28d1f895c6fe | 13 | * and/or other materials provided with the distribution. |
| mbed_official | 340:28d1f895c6fe | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| mbed_official | 340:28d1f895c6fe | 15 | * may be used to endorse or promote products derived from this software |
| mbed_official | 340:28d1f895c6fe | 16 | * without specific prior written permission. |
| mbed_official | 340:28d1f895c6fe | 17 | * |
| mbed_official | 340:28d1f895c6fe | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| mbed_official | 340:28d1f895c6fe | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| mbed_official | 340:28d1f895c6fe | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| mbed_official | 340:28d1f895c6fe | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| mbed_official | 340:28d1f895c6fe | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| mbed_official | 340:28d1f895c6fe | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| mbed_official | 340:28d1f895c6fe | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| mbed_official | 340:28d1f895c6fe | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| mbed_official | 340:28d1f895c6fe | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| mbed_official | 340:28d1f895c6fe | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| mbed_official | 340:28d1f895c6fe | 28 | ******************************************************************************* |
| mbed_official | 340:28d1f895c6fe | 29 | */ |
| mbed_official | 340:28d1f895c6fe | 30 | #include "mbed_assert.h" |
| mbed_official | 340:28d1f895c6fe | 31 | #include "i2c_api.h" |
| mbed_official | 340:28d1f895c6fe | 32 | |
| mbed_official | 340:28d1f895c6fe | 33 | #if DEVICE_I2C |
| mbed_official | 340:28d1f895c6fe | 34 | |
| mbed_official | 340:28d1f895c6fe | 35 | #include "cmsis.h" |
| mbed_official | 340:28d1f895c6fe | 36 | #include "pinmap.h" |
| mbed_official | 340:28d1f895c6fe | 37 | |
| mbed_official | 340:28d1f895c6fe | 38 | /* Timeout values for flags and events waiting loops. These timeouts are |
| mbed_official | 340:28d1f895c6fe | 39 | not based on accurate values, they just guarantee that the application will |
| mbed_official | 340:28d1f895c6fe | 40 | not remain stuck if the I2C communication is corrupted. */ |
| mbed_official | 340:28d1f895c6fe | 41 | #define FLAG_TIMEOUT ((int)0x1000) |
| mbed_official | 340:28d1f895c6fe | 42 | #define LONG_TIMEOUT ((int)0x8000) |
| mbed_official | 340:28d1f895c6fe | 43 | |
| mbed_official | 340:28d1f895c6fe | 44 | static const PinMap PinMap_I2C_SDA[] = { |
| mbed_official | 340:28d1f895c6fe | 45 | {PA_10, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)}, |
| mbed_official | 340:28d1f895c6fe | 46 | {PA_12, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF5_I2C2)}, |
| mbed_official | 340:28d1f895c6fe | 47 | {PB_7, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF1_I2C1)}, |
| mbed_official | 340:28d1f895c6fe | 48 | {PB_9, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF1_I2C1)}, |
| mbed_official | 340:28d1f895c6fe | 49 | {PB_11, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF1_I2C2)}, |
| mbed_official | 340:28d1f895c6fe | 50 | {PB_14, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF5_I2C2)}, |
| mbed_official | 340:28d1f895c6fe | 51 | // {PF_0, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF1_I2C1)}, // OSC_IN |
| mbed_official | 340:28d1f895c6fe | 52 | {NC, NC, 0} |
| mbed_official | 340:28d1f895c6fe | 53 | }; |
| mbed_official | 340:28d1f895c6fe | 54 | |
| mbed_official | 340:28d1f895c6fe | 55 | static const PinMap PinMap_I2C_SCL[] = { |
| mbed_official | 340:28d1f895c6fe | 56 | {PA_9, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF4_I2C1)}, |
| mbed_official | 340:28d1f895c6fe | 57 | {PA_11, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF5_I2C2)}, |
| mbed_official | 340:28d1f895c6fe | 58 | {PB_6, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF1_I2C1)}, |
| mbed_official | 340:28d1f895c6fe | 59 | {PB_8, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF1_I2C1)}, |
| mbed_official | 340:28d1f895c6fe | 60 | {PB_10, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF1_I2C2)}, |
| mbed_official | 340:28d1f895c6fe | 61 | {PB_13, I2C_2, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF5_I2C2)}, |
| mbed_official | 340:28d1f895c6fe | 62 | // {PF_1, I2C_1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, GPIO_AF1_I2C1)}, // OSC_OUT |
| mbed_official | 340:28d1f895c6fe | 63 | {NC, NC, 0} |
| mbed_official | 340:28d1f895c6fe | 64 | }; |
| mbed_official | 340:28d1f895c6fe | 65 | |
| mbed_official | 340:28d1f895c6fe | 66 | I2C_HandleTypeDef I2cHandle; |
| mbed_official | 340:28d1f895c6fe | 67 | |
| mbed_official | 340:28d1f895c6fe | 68 | int i2c1_inited = 0; |
| mbed_official | 340:28d1f895c6fe | 69 | int i2c2_inited = 0; |
| mbed_official | 340:28d1f895c6fe | 70 | |
| mbed_official | 340:28d1f895c6fe | 71 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) |
| mbed_official | 340:28d1f895c6fe | 72 | { |
| mbed_official | 340:28d1f895c6fe | 73 | // Determine the I2C to use |
| mbed_official | 340:28d1f895c6fe | 74 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
| mbed_official | 340:28d1f895c6fe | 75 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
| mbed_official | 340:28d1f895c6fe | 76 | |
| mbed_official | 340:28d1f895c6fe | 77 | obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
| mbed_official | 340:28d1f895c6fe | 78 | MBED_ASSERT(obj->i2c != (I2CName)NC); |
| mbed_official | 340:28d1f895c6fe | 79 | |
| mbed_official | 340:28d1f895c6fe | 80 | // Enable I2C1 clock and pinout if not done |
| mbed_official | 340:28d1f895c6fe | 81 | if ((obj->i2c == I2C_1) && !i2c1_inited) { |
| mbed_official | 340:28d1f895c6fe | 82 | i2c1_inited = 1; |
| mbed_official | 340:28d1f895c6fe | 83 | __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK); |
| mbed_official | 340:28d1f895c6fe | 84 | __I2C1_CLK_ENABLE(); |
| mbed_official | 340:28d1f895c6fe | 85 | // Configure I2C pins |
| mbed_official | 340:28d1f895c6fe | 86 | pinmap_pinout(sda, PinMap_I2C_SDA); |
| mbed_official | 340:28d1f895c6fe | 87 | pinmap_pinout(scl, PinMap_I2C_SCL); |
| mbed_official | 340:28d1f895c6fe | 88 | pin_mode(sda, OpenDrain); |
| mbed_official | 340:28d1f895c6fe | 89 | pin_mode(scl, OpenDrain); |
| mbed_official | 340:28d1f895c6fe | 90 | } |
| mbed_official | 340:28d1f895c6fe | 91 | |
| mbed_official | 340:28d1f895c6fe | 92 | // Enable I2C2 clock and pinout if not done |
| mbed_official | 340:28d1f895c6fe | 93 | if ((obj->i2c == I2C_2) && !i2c2_inited) { |
| mbed_official | 340:28d1f895c6fe | 94 | i2c2_inited = 1; |
| mbed_official | 340:28d1f895c6fe | 95 | __I2C2_CLK_ENABLE(); |
| mbed_official | 340:28d1f895c6fe | 96 | // Configure I2C pins |
| mbed_official | 340:28d1f895c6fe | 97 | pinmap_pinout(sda, PinMap_I2C_SDA); |
| mbed_official | 340:28d1f895c6fe | 98 | pinmap_pinout(scl, PinMap_I2C_SCL); |
| mbed_official | 340:28d1f895c6fe | 99 | pin_mode(sda, OpenDrain); |
| mbed_official | 340:28d1f895c6fe | 100 | pin_mode(scl, OpenDrain); |
| mbed_official | 340:28d1f895c6fe | 101 | } |
| mbed_official | 340:28d1f895c6fe | 102 | |
| mbed_official | 340:28d1f895c6fe | 103 | // Reset to clear pending flags if any |
| mbed_official | 340:28d1f895c6fe | 104 | i2c_reset(obj); |
| mbed_official | 340:28d1f895c6fe | 105 | |
| mbed_official | 340:28d1f895c6fe | 106 | // I2C configuration |
| mbed_official | 340:28d1f895c6fe | 107 | i2c_frequency(obj, 100000); // 100 kHz per default |
| mbed_official | 340:28d1f895c6fe | 108 | } |
| mbed_official | 340:28d1f895c6fe | 109 | |
| mbed_official | 340:28d1f895c6fe | 110 | void i2c_frequency(i2c_t *obj, int hz) |
| mbed_official | 340:28d1f895c6fe | 111 | { |
| mbed_official | 340:28d1f895c6fe | 112 | MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000)); |
| mbed_official | 340:28d1f895c6fe | 113 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 114 | int timeout; |
| mbed_official | 340:28d1f895c6fe | 115 | |
| mbed_official | 340:28d1f895c6fe | 116 | // wait before init |
| mbed_official | 340:28d1f895c6fe | 117 | timeout = LONG_TIMEOUT; |
| mbed_official | 340:28d1f895c6fe | 118 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); |
| mbed_official | 340:28d1f895c6fe | 119 | |
| mbed_official | 340:28d1f895c6fe | 120 | // Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0 |
| mbed_official | 340:28d1f895c6fe | 121 | switch (hz) { |
| mbed_official | 340:28d1f895c6fe | 122 | case 100000: |
| mbed_official | 340:28d1f895c6fe | 123 | I2cHandle.Init.Timing = 0x10805E89; // Standard mode with Rise Time = 400ns and Fall Time = 100ns |
| mbed_official | 340:28d1f895c6fe | 124 | break; |
| mbed_official | 340:28d1f895c6fe | 125 | case 400000: |
| mbed_official | 340:28d1f895c6fe | 126 | I2cHandle.Init.Timing = 0x00901850; // Fast mode with Rise Time = 250ns and Fall Time = 100ns |
| mbed_official | 340:28d1f895c6fe | 127 | break; |
| mbed_official | 340:28d1f895c6fe | 128 | case 1000000: |
| mbed_official | 340:28d1f895c6fe | 129 | I2cHandle.Init.Timing = 0x00700818; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns |
| mbed_official | 340:28d1f895c6fe | 130 | break; |
| mbed_official | 340:28d1f895c6fe | 131 | default: |
| mbed_official | 340:28d1f895c6fe | 132 | break; |
| mbed_official | 340:28d1f895c6fe | 133 | } |
| mbed_official | 340:28d1f895c6fe | 134 | |
| mbed_official | 340:28d1f895c6fe | 135 | // I2C configuration |
| mbed_official | 340:28d1f895c6fe | 136 | I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
| mbed_official | 340:28d1f895c6fe | 137 | I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
| mbed_official | 340:28d1f895c6fe | 138 | I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
| mbed_official | 340:28d1f895c6fe | 139 | I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
| mbed_official | 340:28d1f895c6fe | 140 | I2cHandle.Init.OwnAddress1 = 0; |
| mbed_official | 340:28d1f895c6fe | 141 | I2cHandle.Init.OwnAddress2 = 0; |
| mbed_official | 340:28d1f895c6fe | 142 | I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK; |
| mbed_official | 340:28d1f895c6fe | 143 | HAL_I2C_Init(&I2cHandle); |
| mbed_official | 340:28d1f895c6fe | 144 | } |
| mbed_official | 340:28d1f895c6fe | 145 | |
| mbed_official | 340:28d1f895c6fe | 146 | inline int i2c_start(i2c_t *obj) |
| mbed_official | 340:28d1f895c6fe | 147 | { |
| mbed_official | 340:28d1f895c6fe | 148 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 149 | int timeout; |
| mbed_official | 340:28d1f895c6fe | 150 | |
| mbed_official | 340:28d1f895c6fe | 151 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 152 | |
| mbed_official | 340:28d1f895c6fe | 153 | // Clear Acknowledge failure flag |
| mbed_official | 340:28d1f895c6fe | 154 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF); |
| mbed_official | 340:28d1f895c6fe | 155 | |
| mbed_official | 340:28d1f895c6fe | 156 | // Generate the START condition |
| mbed_official | 340:28d1f895c6fe | 157 | i2c->CR2 |= I2C_CR2_START; |
| mbed_official | 340:28d1f895c6fe | 158 | |
| mbed_official | 340:28d1f895c6fe | 159 | // Wait the START condition has been correctly sent |
| mbed_official | 340:28d1f895c6fe | 160 | timeout = FLAG_TIMEOUT; |
| mbed_official | 340:28d1f895c6fe | 161 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == RESET) { |
| mbed_official | 340:28d1f895c6fe | 162 | if ((timeout--) == 0) { |
| mbed_official | 340:28d1f895c6fe | 163 | return 1; |
| mbed_official | 340:28d1f895c6fe | 164 | } |
| mbed_official | 340:28d1f895c6fe | 165 | } |
| mbed_official | 340:28d1f895c6fe | 166 | |
| mbed_official | 340:28d1f895c6fe | 167 | return 0; |
| mbed_official | 340:28d1f895c6fe | 168 | } |
| mbed_official | 340:28d1f895c6fe | 169 | |
| mbed_official | 340:28d1f895c6fe | 170 | inline int i2c_stop(i2c_t *obj) |
| mbed_official | 340:28d1f895c6fe | 171 | { |
| mbed_official | 340:28d1f895c6fe | 172 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 173 | |
| mbed_official | 340:28d1f895c6fe | 174 | // Generate the STOP condition |
| mbed_official | 340:28d1f895c6fe | 175 | i2c->CR2 |= I2C_CR2_STOP; |
| mbed_official | 340:28d1f895c6fe | 176 | |
| mbed_official | 340:28d1f895c6fe | 177 | return 0; |
| mbed_official | 340:28d1f895c6fe | 178 | } |
| mbed_official | 340:28d1f895c6fe | 179 | |
| mbed_official | 340:28d1f895c6fe | 180 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) |
| mbed_official | 340:28d1f895c6fe | 181 | { |
| mbed_official | 340:28d1f895c6fe | 182 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 183 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 184 | int timeout; |
| mbed_official | 340:28d1f895c6fe | 185 | int count; |
| mbed_official | 340:28d1f895c6fe | 186 | int value; |
| mbed_official | 340:28d1f895c6fe | 187 | |
| mbed_official | 340:28d1f895c6fe | 188 | // Update CR2 register |
| mbed_official | 340:28d1f895c6fe | 189 | i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP))) |
| mbed_official | 340:28d1f895c6fe | 190 | | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ); |
| mbed_official | 340:28d1f895c6fe | 191 | |
| mbed_official | 340:28d1f895c6fe | 192 | // Read all bytes |
| mbed_official | 340:28d1f895c6fe | 193 | for (count = 0; count < length; count++) { |
| mbed_official | 340:28d1f895c6fe | 194 | value = i2c_byte_read(obj, 0); |
| mbed_official | 340:28d1f895c6fe | 195 | data[count] = (char)value; |
| mbed_official | 340:28d1f895c6fe | 196 | } |
| mbed_official | 340:28d1f895c6fe | 197 | |
| mbed_official | 340:28d1f895c6fe | 198 | // Wait transfer complete |
| mbed_official | 340:28d1f895c6fe | 199 | timeout = FLAG_TIMEOUT; |
| mbed_official | 340:28d1f895c6fe | 200 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { |
| mbed_official | 340:28d1f895c6fe | 201 | timeout--; |
| mbed_official | 340:28d1f895c6fe | 202 | if (timeout == 0) { |
| mbed_official | 340:28d1f895c6fe | 203 | return -1; |
| mbed_official | 340:28d1f895c6fe | 204 | } |
| mbed_official | 340:28d1f895c6fe | 205 | } |
| mbed_official | 340:28d1f895c6fe | 206 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); |
| mbed_official | 340:28d1f895c6fe | 207 | |
| mbed_official | 340:28d1f895c6fe | 208 | // If not repeated start, send stop. |
| mbed_official | 340:28d1f895c6fe | 209 | if (stop) { |
| mbed_official | 340:28d1f895c6fe | 210 | i2c_stop(obj); |
| mbed_official | 340:28d1f895c6fe | 211 | // Wait until STOPF flag is set |
| mbed_official | 340:28d1f895c6fe | 212 | timeout = FLAG_TIMEOUT; |
| mbed_official | 340:28d1f895c6fe | 213 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
| mbed_official | 340:28d1f895c6fe | 214 | timeout--; |
| mbed_official | 340:28d1f895c6fe | 215 | if (timeout == 0) { |
| mbed_official | 340:28d1f895c6fe | 216 | return -1; |
| mbed_official | 340:28d1f895c6fe | 217 | } |
| mbed_official | 340:28d1f895c6fe | 218 | } |
| mbed_official | 340:28d1f895c6fe | 219 | // Clear STOP Flag |
| mbed_official | 340:28d1f895c6fe | 220 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); |
| mbed_official | 340:28d1f895c6fe | 221 | } |
| mbed_official | 340:28d1f895c6fe | 222 | |
| mbed_official | 340:28d1f895c6fe | 223 | return length; |
| mbed_official | 340:28d1f895c6fe | 224 | } |
| mbed_official | 340:28d1f895c6fe | 225 | |
| mbed_official | 340:28d1f895c6fe | 226 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) |
| mbed_official | 340:28d1f895c6fe | 227 | { |
| mbed_official | 340:28d1f895c6fe | 228 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 229 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 230 | int timeout; |
| mbed_official | 340:28d1f895c6fe | 231 | int count; |
| mbed_official | 340:28d1f895c6fe | 232 | |
| mbed_official | 340:28d1f895c6fe | 233 | // Update CR2 register |
| mbed_official | 340:28d1f895c6fe | 234 | i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP))) |
| mbed_official | 340:28d1f895c6fe | 235 | | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE); |
| mbed_official | 340:28d1f895c6fe | 236 | |
| mbed_official | 340:28d1f895c6fe | 237 | for (count = 0; count < length; count++) { |
| mbed_official | 340:28d1f895c6fe | 238 | i2c_byte_write(obj, data[count]); |
| mbed_official | 340:28d1f895c6fe | 239 | } |
| mbed_official | 340:28d1f895c6fe | 240 | |
| mbed_official | 340:28d1f895c6fe | 241 | // Wait transfer complete |
| mbed_official | 340:28d1f895c6fe | 242 | timeout = FLAG_TIMEOUT; |
| mbed_official | 340:28d1f895c6fe | 243 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { |
| mbed_official | 340:28d1f895c6fe | 244 | timeout--; |
| mbed_official | 340:28d1f895c6fe | 245 | if (timeout == 0) { |
| mbed_official | 340:28d1f895c6fe | 246 | return -1; |
| mbed_official | 340:28d1f895c6fe | 247 | } |
| mbed_official | 340:28d1f895c6fe | 248 | } |
| mbed_official | 340:28d1f895c6fe | 249 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); |
| mbed_official | 340:28d1f895c6fe | 250 | |
| mbed_official | 340:28d1f895c6fe | 251 | // If not repeated start, send stop |
| mbed_official | 340:28d1f895c6fe | 252 | if (stop) { |
| mbed_official | 340:28d1f895c6fe | 253 | i2c_stop(obj); |
| mbed_official | 340:28d1f895c6fe | 254 | // Wait until STOPF flag is set |
| mbed_official | 340:28d1f895c6fe | 255 | timeout = FLAG_TIMEOUT; |
| mbed_official | 340:28d1f895c6fe | 256 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { |
| mbed_official | 340:28d1f895c6fe | 257 | timeout--; |
| mbed_official | 340:28d1f895c6fe | 258 | if (timeout == 0) { |
| mbed_official | 340:28d1f895c6fe | 259 | return -1; |
| mbed_official | 340:28d1f895c6fe | 260 | } |
| mbed_official | 340:28d1f895c6fe | 261 | } |
| mbed_official | 340:28d1f895c6fe | 262 | // Clear STOP Flag |
| mbed_official | 340:28d1f895c6fe | 263 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); |
| mbed_official | 340:28d1f895c6fe | 264 | } |
| mbed_official | 340:28d1f895c6fe | 265 | |
| mbed_official | 340:28d1f895c6fe | 266 | return count; |
| mbed_official | 340:28d1f895c6fe | 267 | } |
| mbed_official | 340:28d1f895c6fe | 268 | |
| mbed_official | 340:28d1f895c6fe | 269 | int i2c_byte_read(i2c_t *obj, int last) |
| mbed_official | 340:28d1f895c6fe | 270 | { |
| mbed_official | 340:28d1f895c6fe | 271 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 272 | int timeout; |
| mbed_official | 340:28d1f895c6fe | 273 | |
| mbed_official | 340:28d1f895c6fe | 274 | // Wait until the byte is received |
| mbed_official | 340:28d1f895c6fe | 275 | timeout = FLAG_TIMEOUT; |
| mbed_official | 340:28d1f895c6fe | 276 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE) == RESET) { |
| mbed_official | 340:28d1f895c6fe | 277 | if ((timeout--) == 0) { |
| mbed_official | 340:28d1f895c6fe | 278 | return -1; |
| mbed_official | 340:28d1f895c6fe | 279 | } |
| mbed_official | 340:28d1f895c6fe | 280 | } |
| mbed_official | 340:28d1f895c6fe | 281 | |
| mbed_official | 340:28d1f895c6fe | 282 | return (int)i2c->RXDR; |
| mbed_official | 340:28d1f895c6fe | 283 | } |
| mbed_official | 340:28d1f895c6fe | 284 | |
| mbed_official | 340:28d1f895c6fe | 285 | int i2c_byte_write(i2c_t *obj, int data) |
| mbed_official | 340:28d1f895c6fe | 286 | { |
| mbed_official | 340:28d1f895c6fe | 287 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 288 | int timeout; |
| mbed_official | 340:28d1f895c6fe | 289 | |
| mbed_official | 340:28d1f895c6fe | 290 | // Wait until the previous byte is transmitted |
| mbed_official | 340:28d1f895c6fe | 291 | timeout = FLAG_TIMEOUT; |
| mbed_official | 340:28d1f895c6fe | 292 | while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXIS) == RESET) { |
| mbed_official | 340:28d1f895c6fe | 293 | if ((timeout--) == 0) { |
| mbed_official | 340:28d1f895c6fe | 294 | return 0; |
| mbed_official | 340:28d1f895c6fe | 295 | } |
| mbed_official | 340:28d1f895c6fe | 296 | } |
| mbed_official | 340:28d1f895c6fe | 297 | |
| mbed_official | 340:28d1f895c6fe | 298 | i2c->TXDR = (uint8_t)data; |
| mbed_official | 340:28d1f895c6fe | 299 | |
| mbed_official | 340:28d1f895c6fe | 300 | return 1; |
| mbed_official | 340:28d1f895c6fe | 301 | } |
| mbed_official | 340:28d1f895c6fe | 302 | |
| mbed_official | 340:28d1f895c6fe | 303 | void i2c_reset(i2c_t *obj) |
| mbed_official | 340:28d1f895c6fe | 304 | { |
| mbed_official | 340:28d1f895c6fe | 305 | int timeout; |
| mbed_official | 340:28d1f895c6fe | 306 | |
| mbed_official | 340:28d1f895c6fe | 307 | // Wait before reset |
| mbed_official | 340:28d1f895c6fe | 308 | timeout = LONG_TIMEOUT; |
| mbed_official | 340:28d1f895c6fe | 309 | while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); |
| mbed_official | 340:28d1f895c6fe | 310 | |
| mbed_official | 340:28d1f895c6fe | 311 | if (obj->i2c == I2C_1) { |
| mbed_official | 340:28d1f895c6fe | 312 | __I2C1_FORCE_RESET(); |
| mbed_official | 340:28d1f895c6fe | 313 | __I2C1_RELEASE_RESET(); |
| mbed_official | 340:28d1f895c6fe | 314 | } |
| mbed_official | 340:28d1f895c6fe | 315 | if (obj->i2c == I2C_2) { |
| mbed_official | 340:28d1f895c6fe | 316 | __I2C2_FORCE_RESET(); |
| mbed_official | 340:28d1f895c6fe | 317 | __I2C2_RELEASE_RESET(); |
| mbed_official | 340:28d1f895c6fe | 318 | } |
| mbed_official | 340:28d1f895c6fe | 319 | } |
| mbed_official | 340:28d1f895c6fe | 320 | |
| mbed_official | 340:28d1f895c6fe | 321 | #if DEVICE_I2CSLAVE |
| mbed_official | 340:28d1f895c6fe | 322 | |
| mbed_official | 340:28d1f895c6fe | 323 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) |
| mbed_official | 340:28d1f895c6fe | 324 | { |
| mbed_official | 340:28d1f895c6fe | 325 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 326 | uint16_t tmpreg = 0; |
| mbed_official | 340:28d1f895c6fe | 327 | |
| mbed_official | 340:28d1f895c6fe | 328 | // disable |
| mbed_official | 340:28d1f895c6fe | 329 | i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN); |
| mbed_official | 340:28d1f895c6fe | 330 | // Get the old register value |
| mbed_official | 340:28d1f895c6fe | 331 | tmpreg = i2c->OAR1; |
| mbed_official | 340:28d1f895c6fe | 332 | // Reset address bits |
| mbed_official | 340:28d1f895c6fe | 333 | tmpreg &= 0xFC00; |
| mbed_official | 340:28d1f895c6fe | 334 | // Set new address |
| mbed_official | 340:28d1f895c6fe | 335 | tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits |
| mbed_official | 340:28d1f895c6fe | 336 | // Store the new register value |
| mbed_official | 340:28d1f895c6fe | 337 | i2c->OAR1 = tmpreg; |
| mbed_official | 340:28d1f895c6fe | 338 | // enable |
| mbed_official | 340:28d1f895c6fe | 339 | i2c->OAR1 |= I2C_OAR1_OA1EN; |
| mbed_official | 340:28d1f895c6fe | 340 | } |
| mbed_official | 340:28d1f895c6fe | 341 | |
| mbed_official | 340:28d1f895c6fe | 342 | void i2c_slave_mode(i2c_t *obj, int enable_slave) |
| mbed_official | 340:28d1f895c6fe | 343 | { |
| mbed_official | 340:28d1f895c6fe | 344 | I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 345 | uint16_t tmpreg; |
| mbed_official | 340:28d1f895c6fe | 346 | |
| mbed_official | 340:28d1f895c6fe | 347 | // Get the old register value |
| mbed_official | 340:28d1f895c6fe | 348 | tmpreg = i2c->OAR1; |
| mbed_official | 340:28d1f895c6fe | 349 | |
| mbed_official | 340:28d1f895c6fe | 350 | // Enable / disable slave |
| mbed_official | 340:28d1f895c6fe | 351 | if (enable_slave == 1) { |
| mbed_official | 340:28d1f895c6fe | 352 | tmpreg |= I2C_OAR1_OA1EN; |
| mbed_official | 340:28d1f895c6fe | 353 | } else { |
| mbed_official | 340:28d1f895c6fe | 354 | tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN); |
| mbed_official | 340:28d1f895c6fe | 355 | } |
| mbed_official | 340:28d1f895c6fe | 356 | |
| mbed_official | 340:28d1f895c6fe | 357 | // Set new mode |
| mbed_official | 340:28d1f895c6fe | 358 | i2c->OAR1 = tmpreg; |
| mbed_official | 340:28d1f895c6fe | 359 | } |
| mbed_official | 340:28d1f895c6fe | 360 | |
| mbed_official | 340:28d1f895c6fe | 361 | // See I2CSlave.h |
| mbed_official | 340:28d1f895c6fe | 362 | #define NoData 0 // the slave has not been addressed |
| mbed_official | 340:28d1f895c6fe | 363 | #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter) |
| mbed_official | 340:28d1f895c6fe | 364 | #define WriteGeneral 2 // the master is writing to all slave |
| mbed_official | 340:28d1f895c6fe | 365 | #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) |
| mbed_official | 340:28d1f895c6fe | 366 | |
| mbed_official | 340:28d1f895c6fe | 367 | int i2c_slave_receive(i2c_t *obj) |
| mbed_official | 340:28d1f895c6fe | 368 | { |
| mbed_official | 340:28d1f895c6fe | 369 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 370 | int retValue = NoData; |
| mbed_official | 340:28d1f895c6fe | 371 | |
| mbed_official | 340:28d1f895c6fe | 372 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) { |
| mbed_official | 340:28d1f895c6fe | 373 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) { |
| mbed_official | 340:28d1f895c6fe | 374 | if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1) |
| mbed_official | 340:28d1f895c6fe | 375 | retValue = ReadAddressed; |
| mbed_official | 340:28d1f895c6fe | 376 | else |
| mbed_official | 340:28d1f895c6fe | 377 | retValue = WriteAddressed; |
| mbed_official | 340:28d1f895c6fe | 378 | |
| mbed_official | 340:28d1f895c6fe | 379 | __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR); |
| mbed_official | 340:28d1f895c6fe | 380 | } |
| mbed_official | 340:28d1f895c6fe | 381 | } |
| mbed_official | 340:28d1f895c6fe | 382 | |
| mbed_official | 340:28d1f895c6fe | 383 | return (retValue); |
| mbed_official | 340:28d1f895c6fe | 384 | } |
| mbed_official | 340:28d1f895c6fe | 385 | |
| mbed_official | 340:28d1f895c6fe | 386 | int i2c_slave_read(i2c_t *obj, char *data, int length) |
| mbed_official | 340:28d1f895c6fe | 387 | { |
| mbed_official | 340:28d1f895c6fe | 388 | char size = 0; |
| mbed_official | 340:28d1f895c6fe | 389 | |
| mbed_official | 340:28d1f895c6fe | 390 | while (size < length) data[size++] = (char)i2c_byte_read(obj, 0); |
| mbed_official | 340:28d1f895c6fe | 391 | |
| mbed_official | 340:28d1f895c6fe | 392 | return size; |
| mbed_official | 340:28d1f895c6fe | 393 | } |
| mbed_official | 340:28d1f895c6fe | 394 | |
| mbed_official | 340:28d1f895c6fe | 395 | int i2c_slave_write(i2c_t *obj, const char *data, int length) |
| mbed_official | 340:28d1f895c6fe | 396 | { |
| mbed_official | 340:28d1f895c6fe | 397 | char size = 0; |
| mbed_official | 340:28d1f895c6fe | 398 | I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); |
| mbed_official | 340:28d1f895c6fe | 399 | |
| mbed_official | 340:28d1f895c6fe | 400 | do { |
| mbed_official | 340:28d1f895c6fe | 401 | i2c_byte_write(obj, data[size]); |
| mbed_official | 340:28d1f895c6fe | 402 | size++; |
| mbed_official | 340:28d1f895c6fe | 403 | } while (size < length); |
| mbed_official | 340:28d1f895c6fe | 404 | |
| mbed_official | 340:28d1f895c6fe | 405 | return size; |
| mbed_official | 340:28d1f895c6fe | 406 | } |
| mbed_official | 340:28d1f895c6fe | 407 | |
| mbed_official | 340:28d1f895c6fe | 408 | |
| mbed_official | 340:28d1f895c6fe | 409 | #endif // DEVICE_I2CSLAVE |
| mbed_official | 340:28d1f895c6fe | 410 | |
| mbed_official | 340:28d1f895c6fe | 411 | #endif // DEVICE_I2C |

