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:
Kojto
Date:
Wed Jul 19 17:31:21 2017 +0100
Revision:
169:e3b6fe271b81
Parent:
160:d5399cc887bb
This updates the lib to the mbed lib v 147

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 160:d5399cc887bb 1 /* mbed Microcontroller Library
<> 160:d5399cc887bb 2 * Copyright (c) 2017 ARM Limited
<> 160:d5399cc887bb 3 *
<> 160:d5399cc887bb 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
<> 160:d5399cc887bb 5 * of this software and associated documentation files (the "Software"), to deal
<> 160:d5399cc887bb 6 * in the Software without restriction, including without limitation the rights
<> 160:d5399cc887bb 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
<> 160:d5399cc887bb 8 * copies of the Software, and to permit persons to whom the Software is
<> 160:d5399cc887bb 9 * furnished to do so, subject to the following conditions:
<> 160:d5399cc887bb 10 *
<> 160:d5399cc887bb 11 * The above copyright notice and this permission notice shall be included in
<> 160:d5399cc887bb 12 * all copies or substantial portions of the Software.
<> 160:d5399cc887bb 13 *
<> 160:d5399cc887bb 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
<> 160:d5399cc887bb 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
<> 160:d5399cc887bb 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
<> 160:d5399cc887bb 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
<> 160:d5399cc887bb 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
<> 160:d5399cc887bb 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
<> 160:d5399cc887bb 20 * SOFTWARE.
<> 160:d5399cc887bb 21 */
<> 160:d5399cc887bb 22
<> 160:d5399cc887bb 23 #include <string.h>
<> 160:d5399cc887bb 24 #include "FlashIAP.h"
<> 160:d5399cc887bb 25 #include "mbed_assert.h"
<> 160:d5399cc887bb 26
<> 160:d5399cc887bb 27
<> 160:d5399cc887bb 28 #ifdef DEVICE_FLASH
<> 160:d5399cc887bb 29
<> 160:d5399cc887bb 30 namespace mbed {
<> 160:d5399cc887bb 31
<> 160:d5399cc887bb 32 SingletonPtr<PlatformMutex> FlashIAP::_mutex;
<> 160:d5399cc887bb 33
<> 160:d5399cc887bb 34 static inline bool is_aligned(uint32_t number, uint32_t alignment)
<> 160:d5399cc887bb 35 {
<> 160:d5399cc887bb 36 if ((number % alignment) != 0) {
<> 160:d5399cc887bb 37 return false;
<> 160:d5399cc887bb 38 } else {
<> 160:d5399cc887bb 39 return true;
<> 160:d5399cc887bb 40 }
<> 160:d5399cc887bb 41 }
<> 160:d5399cc887bb 42
<> 160:d5399cc887bb 43 FlashIAP::FlashIAP()
<> 160:d5399cc887bb 44 {
<> 160:d5399cc887bb 45
<> 160:d5399cc887bb 46 }
<> 160:d5399cc887bb 47
<> 160:d5399cc887bb 48 FlashIAP::~FlashIAP()
<> 160:d5399cc887bb 49 {
<> 160:d5399cc887bb 50
<> 160:d5399cc887bb 51 }
<> 160:d5399cc887bb 52
<> 160:d5399cc887bb 53 int FlashIAP::init()
<> 160:d5399cc887bb 54 {
<> 160:d5399cc887bb 55 int ret = 0;
<> 160:d5399cc887bb 56 _mutex->lock();
<> 160:d5399cc887bb 57 if (flash_init(&_flash)) {
Kojto 169:e3b6fe271b81 58 ret = -1;
<> 160:d5399cc887bb 59 }
<> 160:d5399cc887bb 60 _mutex->unlock();
<> 160:d5399cc887bb 61 return ret;
<> 160:d5399cc887bb 62 }
<> 160:d5399cc887bb 63
<> 160:d5399cc887bb 64 int FlashIAP::deinit()
<> 160:d5399cc887bb 65 {
<> 160:d5399cc887bb 66 int ret = 0;
<> 160:d5399cc887bb 67 _mutex->lock();
<> 160:d5399cc887bb 68 if (flash_free(&_flash)) {
Kojto 169:e3b6fe271b81 69 ret = -1;
<> 160:d5399cc887bb 70 }
<> 160:d5399cc887bb 71 _mutex->unlock();
<> 160:d5399cc887bb 72 return ret;
<> 160:d5399cc887bb 73 }
<> 160:d5399cc887bb 74
<> 160:d5399cc887bb 75
<> 160:d5399cc887bb 76 int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size)
<> 160:d5399cc887bb 77 {
Kojto 169:e3b6fe271b81 78 int32_t ret = -1;
<> 160:d5399cc887bb 79 _mutex->lock();
Kojto 169:e3b6fe271b81 80 ret = flash_read(&_flash, addr, (uint8_t *) buffer, size);
<> 160:d5399cc887bb 81 _mutex->unlock();
Kojto 169:e3b6fe271b81 82 return ret;
<> 160:d5399cc887bb 83 }
<> 160:d5399cc887bb 84
<> 160:d5399cc887bb 85 int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size)
<> 160:d5399cc887bb 86 {
<> 160:d5399cc887bb 87 uint32_t page_size = get_page_size();
<> 160:d5399cc887bb 88 uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
<> 160:d5399cc887bb 89 // addr and size should be aligned to page size, and multiple of page size
<> 160:d5399cc887bb 90 // page program should not cross sector boundaries
<> 160:d5399cc887bb 91 if (!is_aligned(addr, page_size) ||
<> 160:d5399cc887bb 92 !is_aligned(size, page_size) ||
<> 160:d5399cc887bb 93 (size < page_size) ||
<> 160:d5399cc887bb 94 (((addr % current_sector_size) + size) > current_sector_size)) {
<> 160:d5399cc887bb 95 return -1;
<> 160:d5399cc887bb 96 }
<> 160:d5399cc887bb 97
<> 160:d5399cc887bb 98 int ret = 0;
<> 160:d5399cc887bb 99 _mutex->lock();
<> 160:d5399cc887bb 100 if (flash_program_page(&_flash, addr, (const uint8_t *)buffer, size)) {
<> 160:d5399cc887bb 101 ret = -1;
<> 160:d5399cc887bb 102 }
<> 160:d5399cc887bb 103 _mutex->unlock();
<> 160:d5399cc887bb 104 return ret;
<> 160:d5399cc887bb 105 }
<> 160:d5399cc887bb 106
<> 160:d5399cc887bb 107 bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size)
<> 160:d5399cc887bb 108 {
<> 160:d5399cc887bb 109 uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
<> 160:d5399cc887bb 110 if (!is_aligned(size, current_sector_size) ||
<> 160:d5399cc887bb 111 !is_aligned(addr, current_sector_size)) {
<> 160:d5399cc887bb 112 return false;
<> 160:d5399cc887bb 113 } else {
<> 160:d5399cc887bb 114 return true;
<> 160:d5399cc887bb 115 }
<> 160:d5399cc887bb 116 }
<> 160:d5399cc887bb 117
<> 160:d5399cc887bb 118 int FlashIAP::erase(uint32_t addr, uint32_t size)
<> 160:d5399cc887bb 119 {
<> 160:d5399cc887bb 120 uint32_t current_sector_size = 0UL;
<> 160:d5399cc887bb 121
<> 160:d5399cc887bb 122 if (!is_aligned_to_sector(addr, size)) {
<> 160:d5399cc887bb 123 return -1;
<> 160:d5399cc887bb 124 }
<> 160:d5399cc887bb 125
<> 160:d5399cc887bb 126 int32_t ret = 0;
<> 160:d5399cc887bb 127 _mutex->lock();
<> 160:d5399cc887bb 128 while (size) {
<> 160:d5399cc887bb 129 ret = flash_erase_sector(&_flash, addr);
<> 160:d5399cc887bb 130 if (ret != 0) {
<> 160:d5399cc887bb 131 ret = -1;
<> 160:d5399cc887bb 132 break;
<> 160:d5399cc887bb 133 }
<> 160:d5399cc887bb 134 current_sector_size = flash_get_sector_size(&_flash, addr);
<> 160:d5399cc887bb 135 if (!is_aligned_to_sector(addr, size)) {
<> 160:d5399cc887bb 136 ret = -1;
<> 160:d5399cc887bb 137 break;
<> 160:d5399cc887bb 138 }
<> 160:d5399cc887bb 139 size -= current_sector_size;
<> 160:d5399cc887bb 140 addr += current_sector_size;
<> 160:d5399cc887bb 141 }
<> 160:d5399cc887bb 142 _mutex->unlock();
<> 160:d5399cc887bb 143 return ret;
<> 160:d5399cc887bb 144 }
<> 160:d5399cc887bb 145
<> 160:d5399cc887bb 146 uint32_t FlashIAP::get_page_size() const
<> 160:d5399cc887bb 147 {
<> 160:d5399cc887bb 148 return flash_get_page_size(&_flash);
<> 160:d5399cc887bb 149 }
<> 160:d5399cc887bb 150
<> 160:d5399cc887bb 151 uint32_t FlashIAP::get_sector_size(uint32_t addr) const
<> 160:d5399cc887bb 152 {
<> 160:d5399cc887bb 153 return flash_get_sector_size(&_flash, addr);
<> 160:d5399cc887bb 154 }
<> 160:d5399cc887bb 155
<> 160:d5399cc887bb 156 uint32_t FlashIAP::get_flash_start() const
<> 160:d5399cc887bb 157 {
<> 160:d5399cc887bb 158 return flash_get_start_address(&_flash);
<> 160:d5399cc887bb 159 }
<> 160:d5399cc887bb 160
<> 160:d5399cc887bb 161 uint32_t FlashIAP::get_flash_size() const
<> 160:d5399cc887bb 162 {
<> 160:d5399cc887bb 163 return flash_get_size(&_flash);
<> 160:d5399cc887bb 164 }
<> 160:d5399cc887bb 165
<> 160:d5399cc887bb 166 }
<> 160:d5399cc887bb 167
<> 160:d5399cc887bb 168 #endif