mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c

Dependents:   Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay

Fork of mbed-dev by mbed official

Committer:
maxxir
Date:
Tue Nov 07 16:46:29 2017 +0000
Revision:
177:619788de047e
Parent:
174:b96e65c34a4d
To fix broken RTC on Nucleo_F103RB / STM32F103 BluePill etc..;  Used direct RTC register manipulation for STM32F1xx;  rtc_read() && rtc_write()  (native rtc_init() - works good);  also added stub for non-working on STM32F1xx rtc_read_subseconds().

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 * Copyright (c) 2006-2015 ARM Limited
<> 149:156823d33999 3 *
<> 149:156823d33999 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 5 * you may not use this file except in compliance with the License.
<> 149:156823d33999 6 * You may obtain a copy of the License at
<> 149:156823d33999 7 *
<> 149:156823d33999 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 9 *
<> 149:156823d33999 10 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 13 * See the License for the specific language governing permissions and
<> 149:156823d33999 14 * limitations under the License.
<> 149:156823d33999 15 */
<> 149:156823d33999 16 #include "drivers/I2C.h"
<> 149:156823d33999 17
<> 149:156823d33999 18 #if DEVICE_I2C
<> 149:156823d33999 19
AnnaBridge 174:b96e65c34a4d 20 #if DEVICE_I2C_ASYNCH
AnnaBridge 174:b96e65c34a4d 21 #include "platform/mbed_sleep.h"
AnnaBridge 174:b96e65c34a4d 22 #endif
AnnaBridge 174:b96e65c34a4d 23
<> 149:156823d33999 24 namespace mbed {
<> 149:156823d33999 25
<> 149:156823d33999 26 I2C *I2C::_owner = NULL;
<> 149:156823d33999 27 SingletonPtr<PlatformMutex> I2C::_mutex;
<> 149:156823d33999 28
<> 149:156823d33999 29 I2C::I2C(PinName sda, PinName scl) :
<> 149:156823d33999 30 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 31 _irq(this), _usage(DMA_USAGE_NEVER),
<> 149:156823d33999 32 #endif
<> 149:156823d33999 33 _i2c(), _hz(100000) {
<> 149:156823d33999 34 // No lock needed in the constructor
<> 149:156823d33999 35
<> 149:156823d33999 36 // The init function also set the frequency to 100000
<> 149:156823d33999 37 i2c_init(&_i2c, sda, scl);
<> 149:156823d33999 38
<> 149:156823d33999 39 // Used to avoid unnecessary frequency updates
<> 149:156823d33999 40 _owner = this;
<> 149:156823d33999 41 }
<> 149:156823d33999 42
<> 149:156823d33999 43 void I2C::frequency(int hz) {
<> 149:156823d33999 44 lock();
<> 149:156823d33999 45 _hz = hz;
<> 149:156823d33999 46
<> 149:156823d33999 47 // We want to update the frequency even if we are already the bus owners
<> 149:156823d33999 48 i2c_frequency(&_i2c, _hz);
<> 149:156823d33999 49
<> 149:156823d33999 50 // Updating the frequency of the bus we become the owners of it
<> 149:156823d33999 51 _owner = this;
<> 149:156823d33999 52 unlock();
<> 149:156823d33999 53 }
<> 149:156823d33999 54
<> 149:156823d33999 55 void I2C::aquire() {
<> 149:156823d33999 56 lock();
<> 149:156823d33999 57 if (_owner != this) {
<> 149:156823d33999 58 i2c_frequency(&_i2c, _hz);
<> 149:156823d33999 59 _owner = this;
<> 149:156823d33999 60 }
<> 149:156823d33999 61 unlock();
<> 149:156823d33999 62 }
<> 149:156823d33999 63
<> 149:156823d33999 64 // write - Master Transmitter Mode
<> 149:156823d33999 65 int I2C::write(int address, const char* data, int length, bool repeated) {
<> 149:156823d33999 66 lock();
<> 149:156823d33999 67 aquire();
<> 149:156823d33999 68
<> 149:156823d33999 69 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 70 int written = i2c_write(&_i2c, address, data, length, stop);
<> 149:156823d33999 71
<> 149:156823d33999 72 unlock();
<> 149:156823d33999 73 return length != written;
<> 149:156823d33999 74 }
<> 149:156823d33999 75
<> 149:156823d33999 76 int I2C::write(int data) {
<> 149:156823d33999 77 lock();
<> 149:156823d33999 78 int ret = i2c_byte_write(&_i2c, data);
<> 149:156823d33999 79 unlock();
<> 149:156823d33999 80 return ret;
<> 149:156823d33999 81 }
<> 149:156823d33999 82
<> 149:156823d33999 83 // read - Master Reciever Mode
<> 149:156823d33999 84 int I2C::read(int address, char* data, int length, bool repeated) {
<> 149:156823d33999 85 lock();
<> 149:156823d33999 86 aquire();
<> 149:156823d33999 87
<> 149:156823d33999 88 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 89 int read = i2c_read(&_i2c, address, data, length, stop);
<> 149:156823d33999 90
<> 149:156823d33999 91 unlock();
<> 149:156823d33999 92 return length != read;
<> 149:156823d33999 93 }
<> 149:156823d33999 94
<> 149:156823d33999 95 int I2C::read(int ack) {
<> 149:156823d33999 96 lock();
<> 149:156823d33999 97 int ret;
<> 149:156823d33999 98 if (ack) {
<> 149:156823d33999 99 ret = i2c_byte_read(&_i2c, 0);
<> 149:156823d33999 100 } else {
<> 149:156823d33999 101 ret = i2c_byte_read(&_i2c, 1);
<> 149:156823d33999 102 }
<> 149:156823d33999 103 unlock();
<> 149:156823d33999 104 return ret;
<> 149:156823d33999 105 }
<> 149:156823d33999 106
<> 149:156823d33999 107 void I2C::start(void) {
<> 149:156823d33999 108 lock();
<> 149:156823d33999 109 i2c_start(&_i2c);
<> 149:156823d33999 110 unlock();
<> 149:156823d33999 111 }
<> 149:156823d33999 112
<> 149:156823d33999 113 void I2C::stop(void) {
<> 149:156823d33999 114 lock();
<> 149:156823d33999 115 i2c_stop(&_i2c);
<> 149:156823d33999 116 unlock();
<> 149:156823d33999 117 }
<> 149:156823d33999 118
<> 149:156823d33999 119 void I2C::lock() {
<> 149:156823d33999 120 _mutex->lock();
<> 149:156823d33999 121 }
<> 149:156823d33999 122
<> 149:156823d33999 123 void I2C::unlock() {
<> 149:156823d33999 124 _mutex->unlock();
<> 149:156823d33999 125 }
<> 149:156823d33999 126
<> 149:156823d33999 127 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 128
<> 149:156823d33999 129 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)
<> 149:156823d33999 130 {
<> 149:156823d33999 131 lock();
<> 149:156823d33999 132 if (i2c_active(&_i2c)) {
<> 149:156823d33999 133 unlock();
<> 149:156823d33999 134 return -1; // transaction ongoing
<> 149:156823d33999 135 }
AnnaBridge 174:b96e65c34a4d 136 sleep_manager_lock_deep_sleep();
<> 149:156823d33999 137 aquire();
<> 149:156823d33999 138
<> 149:156823d33999 139 _callback = callback;
<> 149:156823d33999 140 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 141 _irq.callback(&I2C::irq_handler_asynch);
<> 149:156823d33999 142 i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
<> 149:156823d33999 143 unlock();
<> 149:156823d33999 144 return 0;
<> 149:156823d33999 145 }
<> 149:156823d33999 146
<> 149:156823d33999 147 void I2C::abort_transfer(void)
<> 149:156823d33999 148 {
<> 149:156823d33999 149 lock();
<> 149:156823d33999 150 i2c_abort_asynch(&_i2c);
AnnaBridge 174:b96e65c34a4d 151 sleep_manager_unlock_deep_sleep();
<> 149:156823d33999 152 unlock();
<> 149:156823d33999 153 }
<> 149:156823d33999 154
<> 149:156823d33999 155 void I2C::irq_handler_asynch(void)
<> 149:156823d33999 156 {
<> 149:156823d33999 157 int event = i2c_irq_handler_asynch(&_i2c);
<> 149:156823d33999 158 if (_callback && event) {
<> 149:156823d33999 159 _callback.call(event);
<> 149:156823d33999 160 }
AnnaBridge 174:b96e65c34a4d 161 if (event) {
AnnaBridge 174:b96e65c34a4d 162 sleep_manager_unlock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 163 }
<> 149:156823d33999 164
<> 149:156823d33999 165 }
<> 149:156823d33999 166
<> 149:156823d33999 167
<> 149:156823d33999 168 #endif
<> 149:156823d33999 169
<> 149:156823d33999 170 } // namespace mbed
<> 149:156823d33999 171
<> 149:156823d33999 172 #endif