mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 1:9db0e321a9f4 1 /* mbed Microcontroller Library
kenjiArai 1:9db0e321a9f4 2 * Copyright (c) 2006-2019 ARM Limited
kenjiArai 1:9db0e321a9f4 3 * SPDX-License-Identifier: Apache-2.0
kenjiArai 1:9db0e321a9f4 4 *
kenjiArai 1:9db0e321a9f4 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:9db0e321a9f4 6 * you may not use this file except in compliance with the License.
kenjiArai 1:9db0e321a9f4 7 * You may obtain a copy of the License at
kenjiArai 1:9db0e321a9f4 8 *
kenjiArai 1:9db0e321a9f4 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:9db0e321a9f4 10 *
kenjiArai 1:9db0e321a9f4 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:9db0e321a9f4 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:9db0e321a9f4 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:9db0e321a9f4 14 * See the License for the specific language governing permissions and
kenjiArai 1:9db0e321a9f4 15 * limitations under the License.
kenjiArai 1:9db0e321a9f4 16 */
kenjiArai 1:9db0e321a9f4 17
kenjiArai 1:9db0e321a9f4 18 #include "drivers/I2C.h"
kenjiArai 1:9db0e321a9f4 19 #include "drivers/DigitalInOut.h"
kenjiArai 1:9db0e321a9f4 20 #include "platform/mbed_wait_api.h"
kenjiArai 1:9db0e321a9f4 21
kenjiArai 1:9db0e321a9f4 22 #if DEVICE_I2C
kenjiArai 1:9db0e321a9f4 23
kenjiArai 1:9db0e321a9f4 24 #if DEVICE_I2C_ASYNCH
kenjiArai 1:9db0e321a9f4 25 #include "platform/mbed_power_mgmt.h"
kenjiArai 1:9db0e321a9f4 26 #endif
kenjiArai 1:9db0e321a9f4 27
kenjiArai 1:9db0e321a9f4 28 namespace mbed {
kenjiArai 1:9db0e321a9f4 29
kenjiArai 1:9db0e321a9f4 30 I2C *I2C::_owner = NULL;
kenjiArai 1:9db0e321a9f4 31 SingletonPtr<PlatformMutex> I2C::_mutex;
kenjiArai 1:9db0e321a9f4 32
kenjiArai 1:9db0e321a9f4 33 I2C::I2C(PinName sda, PinName scl) :
kenjiArai 1:9db0e321a9f4 34 #if DEVICE_I2C_ASYNCH
kenjiArai 1:9db0e321a9f4 35 _irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false),
kenjiArai 1:9db0e321a9f4 36 #endif
kenjiArai 1:9db0e321a9f4 37 _i2c(), _hz(100000)
kenjiArai 1:9db0e321a9f4 38 {
kenjiArai 1:9db0e321a9f4 39 lock();
kenjiArai 1:9db0e321a9f4 40 // The init function also set the frequency to 100000
kenjiArai 1:9db0e321a9f4 41 _sda = sda;
kenjiArai 1:9db0e321a9f4 42 _scl = scl;
kenjiArai 1:9db0e321a9f4 43 recover(sda, scl);
kenjiArai 1:9db0e321a9f4 44 i2c_init(&_i2c, _sda, _scl);
kenjiArai 1:9db0e321a9f4 45 // Used to avoid unnecessary frequency updates
kenjiArai 1:9db0e321a9f4 46 _owner = this;
kenjiArai 1:9db0e321a9f4 47 unlock();
kenjiArai 1:9db0e321a9f4 48 }
kenjiArai 1:9db0e321a9f4 49
kenjiArai 1:9db0e321a9f4 50 I2C::I2C(const i2c_pinmap_t &static_pinmap) :
kenjiArai 1:9db0e321a9f4 51 #if DEVICE_I2C_ASYNCH
kenjiArai 1:9db0e321a9f4 52 _irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false),
kenjiArai 1:9db0e321a9f4 53 #endif
kenjiArai 1:9db0e321a9f4 54 _i2c(), _hz(100000)
kenjiArai 1:9db0e321a9f4 55 {
kenjiArai 1:9db0e321a9f4 56 lock();
kenjiArai 1:9db0e321a9f4 57 // The init function also set the frequency to 100000
kenjiArai 1:9db0e321a9f4 58 _sda = static_pinmap.sda_pin;
kenjiArai 1:9db0e321a9f4 59 _scl = static_pinmap.scl_pin;
kenjiArai 1:9db0e321a9f4 60 recover(static_pinmap.sda_pin, static_pinmap.scl_pin);
kenjiArai 1:9db0e321a9f4 61 i2c_init_direct(&_i2c, &static_pinmap);
kenjiArai 1:9db0e321a9f4 62 // Used to avoid unnecessary frequency updates
kenjiArai 1:9db0e321a9f4 63 _owner = this;
kenjiArai 1:9db0e321a9f4 64 unlock();
kenjiArai 1:9db0e321a9f4 65 }
kenjiArai 1:9db0e321a9f4 66
kenjiArai 1:9db0e321a9f4 67 void I2C::frequency(int hz)
kenjiArai 1:9db0e321a9f4 68 {
kenjiArai 1:9db0e321a9f4 69 lock();
kenjiArai 1:9db0e321a9f4 70 _hz = hz;
kenjiArai 1:9db0e321a9f4 71
kenjiArai 1:9db0e321a9f4 72 // We want to update the frequency even if we are already the bus owners
kenjiArai 1:9db0e321a9f4 73 i2c_frequency(&_i2c, _hz);
kenjiArai 1:9db0e321a9f4 74
kenjiArai 1:9db0e321a9f4 75 // Updating the frequency of the bus we become the owners of it
kenjiArai 1:9db0e321a9f4 76 _owner = this;
kenjiArai 1:9db0e321a9f4 77 unlock();
kenjiArai 1:9db0e321a9f4 78 }
kenjiArai 1:9db0e321a9f4 79
kenjiArai 1:9db0e321a9f4 80 void I2C::aquire()
kenjiArai 1:9db0e321a9f4 81 {
kenjiArai 1:9db0e321a9f4 82 lock();
kenjiArai 1:9db0e321a9f4 83 if (_owner != this) {
kenjiArai 1:9db0e321a9f4 84 i2c_frequency(&_i2c, _hz);
kenjiArai 1:9db0e321a9f4 85 _owner = this;
kenjiArai 1:9db0e321a9f4 86 }
kenjiArai 1:9db0e321a9f4 87 unlock();
kenjiArai 1:9db0e321a9f4 88 }
kenjiArai 1:9db0e321a9f4 89
kenjiArai 1:9db0e321a9f4 90 // write - Master Transmitter Mode
kenjiArai 1:9db0e321a9f4 91 int I2C::write(int address, const char *data, int length, bool repeated)
kenjiArai 1:9db0e321a9f4 92 {
kenjiArai 1:9db0e321a9f4 93 lock();
kenjiArai 1:9db0e321a9f4 94 aquire();
kenjiArai 1:9db0e321a9f4 95
kenjiArai 1:9db0e321a9f4 96 int stop = (repeated) ? 0 : 1;
kenjiArai 1:9db0e321a9f4 97 int written = i2c_write(&_i2c, address, data, length, stop);
kenjiArai 1:9db0e321a9f4 98
kenjiArai 1:9db0e321a9f4 99 unlock();
kenjiArai 1:9db0e321a9f4 100 return length != written;
kenjiArai 1:9db0e321a9f4 101 }
kenjiArai 1:9db0e321a9f4 102
kenjiArai 1:9db0e321a9f4 103 int I2C::write(int data)
kenjiArai 1:9db0e321a9f4 104 {
kenjiArai 1:9db0e321a9f4 105 lock();
kenjiArai 1:9db0e321a9f4 106 int ret = i2c_byte_write(&_i2c, data);
kenjiArai 1:9db0e321a9f4 107 unlock();
kenjiArai 1:9db0e321a9f4 108 return ret;
kenjiArai 1:9db0e321a9f4 109 }
kenjiArai 1:9db0e321a9f4 110
kenjiArai 1:9db0e321a9f4 111 // read - Master Receiver Mode
kenjiArai 1:9db0e321a9f4 112 int I2C::read(int address, char *data, int length, bool repeated)
kenjiArai 1:9db0e321a9f4 113 {
kenjiArai 1:9db0e321a9f4 114 lock();
kenjiArai 1:9db0e321a9f4 115 aquire();
kenjiArai 1:9db0e321a9f4 116
kenjiArai 1:9db0e321a9f4 117 int stop = (repeated) ? 0 : 1;
kenjiArai 1:9db0e321a9f4 118 int read = i2c_read(&_i2c, address, data, length, stop);
kenjiArai 1:9db0e321a9f4 119
kenjiArai 1:9db0e321a9f4 120 unlock();
kenjiArai 1:9db0e321a9f4 121 return length != read;
kenjiArai 1:9db0e321a9f4 122 }
kenjiArai 1:9db0e321a9f4 123
kenjiArai 1:9db0e321a9f4 124 int I2C::read(int ack)
kenjiArai 1:9db0e321a9f4 125 {
kenjiArai 1:9db0e321a9f4 126 lock();
kenjiArai 1:9db0e321a9f4 127 int ret;
kenjiArai 1:9db0e321a9f4 128 if (ack) {
kenjiArai 1:9db0e321a9f4 129 ret = i2c_byte_read(&_i2c, 0);
kenjiArai 1:9db0e321a9f4 130 } else {
kenjiArai 1:9db0e321a9f4 131 ret = i2c_byte_read(&_i2c, 1);
kenjiArai 1:9db0e321a9f4 132 }
kenjiArai 1:9db0e321a9f4 133 unlock();
kenjiArai 1:9db0e321a9f4 134 return ret;
kenjiArai 1:9db0e321a9f4 135 }
kenjiArai 1:9db0e321a9f4 136
kenjiArai 1:9db0e321a9f4 137 void I2C::start(void)
kenjiArai 1:9db0e321a9f4 138 {
kenjiArai 1:9db0e321a9f4 139 lock();
kenjiArai 1:9db0e321a9f4 140 i2c_start(&_i2c);
kenjiArai 1:9db0e321a9f4 141 unlock();
kenjiArai 1:9db0e321a9f4 142 }
kenjiArai 1:9db0e321a9f4 143
kenjiArai 1:9db0e321a9f4 144 void I2C::stop(void)
kenjiArai 1:9db0e321a9f4 145 {
kenjiArai 1:9db0e321a9f4 146 lock();
kenjiArai 1:9db0e321a9f4 147 i2c_stop(&_i2c);
kenjiArai 1:9db0e321a9f4 148 unlock();
kenjiArai 1:9db0e321a9f4 149 }
kenjiArai 1:9db0e321a9f4 150
kenjiArai 1:9db0e321a9f4 151 void I2C::lock()
kenjiArai 1:9db0e321a9f4 152 {
kenjiArai 1:9db0e321a9f4 153 _mutex->lock();
kenjiArai 1:9db0e321a9f4 154 }
kenjiArai 1:9db0e321a9f4 155
kenjiArai 1:9db0e321a9f4 156 void I2C::unlock()
kenjiArai 1:9db0e321a9f4 157 {
kenjiArai 1:9db0e321a9f4 158 _mutex->unlock();
kenjiArai 1:9db0e321a9f4 159 }
kenjiArai 1:9db0e321a9f4 160
kenjiArai 1:9db0e321a9f4 161 int I2C::recover(PinName sda, PinName scl)
kenjiArai 1:9db0e321a9f4 162 {
kenjiArai 1:9db0e321a9f4 163 DigitalInOut pin_sda(sda, PIN_INPUT, PullNone, 1);
kenjiArai 1:9db0e321a9f4 164 DigitalInOut pin_scl(scl, PIN_INPUT, PullNone, 1);
kenjiArai 1:9db0e321a9f4 165
kenjiArai 1:9db0e321a9f4 166 // Return as SCL is low and no access to become master.
kenjiArai 1:9db0e321a9f4 167 if (pin_scl == 0) {
kenjiArai 1:9db0e321a9f4 168 return I2C_ERROR_BUS_BUSY;
kenjiArai 1:9db0e321a9f4 169 }
kenjiArai 1:9db0e321a9f4 170
kenjiArai 1:9db0e321a9f4 171 // Return successfully as SDA and SCL is high
kenjiArai 1:9db0e321a9f4 172 if (pin_sda == 1) {
kenjiArai 1:9db0e321a9f4 173 return 0;
kenjiArai 1:9db0e321a9f4 174 }
kenjiArai 1:9db0e321a9f4 175
kenjiArai 1:9db0e321a9f4 176 // Send clock pulses, for device to recover 9
kenjiArai 1:9db0e321a9f4 177 pin_scl.mode(PullNone);
kenjiArai 1:9db0e321a9f4 178 pin_scl.output();
kenjiArai 1:9db0e321a9f4 179 for (int count = 0; count < 10; count++) {
kenjiArai 1:9db0e321a9f4 180 pin_scl.mode(PullNone);
kenjiArai 1:9db0e321a9f4 181 pin_scl = 0;
kenjiArai 1:9db0e321a9f4 182 wait_us(5);
kenjiArai 1:9db0e321a9f4 183 pin_scl.mode(PullUp);
kenjiArai 1:9db0e321a9f4 184 pin_scl = 1;
kenjiArai 1:9db0e321a9f4 185 wait_us(5);
kenjiArai 1:9db0e321a9f4 186 }
kenjiArai 1:9db0e321a9f4 187
kenjiArai 1:9db0e321a9f4 188 // Send Stop
kenjiArai 1:9db0e321a9f4 189 pin_sda.output();
kenjiArai 1:9db0e321a9f4 190 pin_sda = 0;
kenjiArai 1:9db0e321a9f4 191 wait_us(5);
kenjiArai 1:9db0e321a9f4 192 pin_scl = 1;
kenjiArai 1:9db0e321a9f4 193 wait_us(5);
kenjiArai 1:9db0e321a9f4 194 pin_sda = 1;
kenjiArai 1:9db0e321a9f4 195 wait_us(5);
kenjiArai 1:9db0e321a9f4 196
kenjiArai 1:9db0e321a9f4 197 pin_sda.input();
kenjiArai 1:9db0e321a9f4 198 pin_scl.input();
kenjiArai 1:9db0e321a9f4 199 if ((pin_scl == 0) || (pin_sda == 0)) {
kenjiArai 1:9db0e321a9f4 200 // Return as SCL is low and no access to become master.
kenjiArai 1:9db0e321a9f4 201 return I2C_ERROR_BUS_BUSY;
kenjiArai 1:9db0e321a9f4 202 }
kenjiArai 1:9db0e321a9f4 203
kenjiArai 1:9db0e321a9f4 204 return 0;
kenjiArai 1:9db0e321a9f4 205 }
kenjiArai 1:9db0e321a9f4 206
kenjiArai 1:9db0e321a9f4 207 #if DEVICE_I2C_ASYNCH
kenjiArai 1:9db0e321a9f4 208
kenjiArai 1:9db0e321a9f4 209 int I2C::transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event, bool repeated)
kenjiArai 1:9db0e321a9f4 210 {
kenjiArai 1:9db0e321a9f4 211 lock();
kenjiArai 1:9db0e321a9f4 212 if (i2c_active(&_i2c)) {
kenjiArai 1:9db0e321a9f4 213 unlock();
kenjiArai 1:9db0e321a9f4 214 return -1; // transaction ongoing
kenjiArai 1:9db0e321a9f4 215 }
kenjiArai 1:9db0e321a9f4 216 lock_deep_sleep();
kenjiArai 1:9db0e321a9f4 217 aquire();
kenjiArai 1:9db0e321a9f4 218
kenjiArai 1:9db0e321a9f4 219 _callback = callback;
kenjiArai 1:9db0e321a9f4 220 int stop = (repeated) ? 0 : 1;
kenjiArai 1:9db0e321a9f4 221 _irq.callback(&I2C::irq_handler_asynch);
kenjiArai 1:9db0e321a9f4 222 i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
kenjiArai 1:9db0e321a9f4 223 unlock();
kenjiArai 1:9db0e321a9f4 224 return 0;
kenjiArai 1:9db0e321a9f4 225 }
kenjiArai 1:9db0e321a9f4 226
kenjiArai 1:9db0e321a9f4 227 void I2C::abort_transfer(void)
kenjiArai 1:9db0e321a9f4 228 {
kenjiArai 1:9db0e321a9f4 229 lock();
kenjiArai 1:9db0e321a9f4 230 i2c_abort_asynch(&_i2c);
kenjiArai 1:9db0e321a9f4 231 unlock_deep_sleep();
kenjiArai 1:9db0e321a9f4 232 unlock();
kenjiArai 1:9db0e321a9f4 233 }
kenjiArai 1:9db0e321a9f4 234
kenjiArai 1:9db0e321a9f4 235 void I2C::irq_handler_asynch(void)
kenjiArai 1:9db0e321a9f4 236 {
kenjiArai 1:9db0e321a9f4 237 int event = i2c_irq_handler_asynch(&_i2c);
kenjiArai 1:9db0e321a9f4 238 if (_callback && event) {
kenjiArai 1:9db0e321a9f4 239 _callback.call(event);
kenjiArai 1:9db0e321a9f4 240 }
kenjiArai 1:9db0e321a9f4 241
kenjiArai 1:9db0e321a9f4 242 if (event) {
kenjiArai 1:9db0e321a9f4 243 unlock_deep_sleep();
kenjiArai 1:9db0e321a9f4 244 }
kenjiArai 1:9db0e321a9f4 245 }
kenjiArai 1:9db0e321a9f4 246
kenjiArai 1:9db0e321a9f4 247 void I2C::lock_deep_sleep()
kenjiArai 1:9db0e321a9f4 248 {
kenjiArai 1:9db0e321a9f4 249 if (_deep_sleep_locked == false) {
kenjiArai 1:9db0e321a9f4 250 sleep_manager_lock_deep_sleep();
kenjiArai 1:9db0e321a9f4 251 _deep_sleep_locked = true;
kenjiArai 1:9db0e321a9f4 252 }
kenjiArai 1:9db0e321a9f4 253 }
kenjiArai 1:9db0e321a9f4 254
kenjiArai 1:9db0e321a9f4 255 void I2C::unlock_deep_sleep()
kenjiArai 1:9db0e321a9f4 256 {
kenjiArai 1:9db0e321a9f4 257 if (_deep_sleep_locked == true) {
kenjiArai 1:9db0e321a9f4 258 sleep_manager_unlock_deep_sleep();
kenjiArai 1:9db0e321a9f4 259 _deep_sleep_locked = false;
kenjiArai 1:9db0e321a9f4 260 }
kenjiArai 1:9db0e321a9f4 261 }
kenjiArai 1:9db0e321a9f4 262
kenjiArai 1:9db0e321a9f4 263 #endif
kenjiArai 1:9db0e321a9f4 264
kenjiArai 1:9db0e321a9f4 265 } // namespace mbed
kenjiArai 1:9db0e321a9f4 266
kenjiArai 1:9db0e321a9f4 267 #endif