4545

Dependents:   LSS_Rev_1

Fork of mbed-dev by Umar Naeem

Committer:
<>
Date:
Tue Mar 14 16:40:56 2017 +0000
Revision:
160:d5399cc887bb
This updates the lib to the mbed lib v138

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)) {
<> 160:d5399cc887bb 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)) {
<> 160:d5399cc887bb 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 {
<> 160:d5399cc887bb 78 _mutex->lock();
<> 160:d5399cc887bb 79 memcpy(buffer, (const void *)addr, size);
<> 160:d5399cc887bb 80 _mutex->unlock();
<> 160:d5399cc887bb 81 return 0;
<> 160:d5399cc887bb 82 }
<> 160:d5399cc887bb 83
<> 160:d5399cc887bb 84 int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size)
<> 160:d5399cc887bb 85 {
<> 160:d5399cc887bb 86 uint32_t page_size = get_page_size();
<> 160:d5399cc887bb 87 uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
<> 160:d5399cc887bb 88 // addr and size should be aligned to page size, and multiple of page size
<> 160:d5399cc887bb 89 // page program should not cross sector boundaries
<> 160:d5399cc887bb 90 if (!is_aligned(addr, page_size) ||
<> 160:d5399cc887bb 91 !is_aligned(size, page_size) ||
<> 160:d5399cc887bb 92 (size < page_size) ||
<> 160:d5399cc887bb 93 (((addr % current_sector_size) + size) > current_sector_size)) {
<> 160:d5399cc887bb 94 return -1;
<> 160:d5399cc887bb 95 }
<> 160:d5399cc887bb 96
<> 160:d5399cc887bb 97 int ret = 0;
<> 160:d5399cc887bb 98 _mutex->lock();
<> 160:d5399cc887bb 99 if (flash_program_page(&_flash, addr, (const uint8_t *)buffer, size)) {
<> 160:d5399cc887bb 100 ret = -1;
<> 160:d5399cc887bb 101 }
<> 160:d5399cc887bb 102 _mutex->unlock();
<> 160:d5399cc887bb 103 return ret;
<> 160:d5399cc887bb 104 }
<> 160:d5399cc887bb 105
<> 160:d5399cc887bb 106 bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size)
<> 160:d5399cc887bb 107 {
<> 160:d5399cc887bb 108 uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
<> 160:d5399cc887bb 109 if (!is_aligned(size, current_sector_size) ||
<> 160:d5399cc887bb 110 !is_aligned(addr, current_sector_size)) {
<> 160:d5399cc887bb 111 return false;
<> 160:d5399cc887bb 112 } else {
<> 160:d5399cc887bb 113 return true;
<> 160:d5399cc887bb 114 }
<> 160:d5399cc887bb 115 }
<> 160:d5399cc887bb 116
<> 160:d5399cc887bb 117 int FlashIAP::erase(uint32_t addr, uint32_t size)
<> 160:d5399cc887bb 118 {
<> 160:d5399cc887bb 119 uint32_t current_sector_size = 0UL;
<> 160:d5399cc887bb 120
<> 160:d5399cc887bb 121 if (!is_aligned_to_sector(addr, size)) {
<> 160:d5399cc887bb 122 return -1;
<> 160:d5399cc887bb 123 }
<> 160:d5399cc887bb 124
<> 160:d5399cc887bb 125 int32_t ret = 0;
<> 160:d5399cc887bb 126 _mutex->lock();
<> 160:d5399cc887bb 127 while (size) {
<> 160:d5399cc887bb 128 ret = flash_erase_sector(&_flash, addr);
<> 160:d5399cc887bb 129 if (ret != 0) {
<> 160:d5399cc887bb 130 ret = -1;
<> 160:d5399cc887bb 131 break;
<> 160:d5399cc887bb 132 }
<> 160:d5399cc887bb 133 current_sector_size = flash_get_sector_size(&_flash, addr);
<> 160:d5399cc887bb 134 if (!is_aligned_to_sector(addr, size)) {
<> 160:d5399cc887bb 135 ret = -1;
<> 160:d5399cc887bb 136 break;
<> 160:d5399cc887bb 137 }
<> 160:d5399cc887bb 138 size -= current_sector_size;
<> 160:d5399cc887bb 139 addr += current_sector_size;
<> 160:d5399cc887bb 140 }
<> 160:d5399cc887bb 141 _mutex->unlock();
<> 160:d5399cc887bb 142 return ret;
<> 160:d5399cc887bb 143 }
<> 160:d5399cc887bb 144
<> 160:d5399cc887bb 145 uint32_t FlashIAP::get_page_size() const
<> 160:d5399cc887bb 146 {
<> 160:d5399cc887bb 147 return flash_get_page_size(&_flash);
<> 160:d5399cc887bb 148 }
<> 160:d5399cc887bb 149
<> 160:d5399cc887bb 150 uint32_t FlashIAP::get_sector_size(uint32_t addr) const
<> 160:d5399cc887bb 151 {
<> 160:d5399cc887bb 152 return flash_get_sector_size(&_flash, addr);
<> 160:d5399cc887bb 153 }
<> 160:d5399cc887bb 154
<> 160:d5399cc887bb 155 uint32_t FlashIAP::get_flash_start() const
<> 160:d5399cc887bb 156 {
<> 160:d5399cc887bb 157 return flash_get_start_address(&_flash);
<> 160:d5399cc887bb 158 }
<> 160:d5399cc887bb 159
<> 160:d5399cc887bb 160 uint32_t FlashIAP::get_flash_size() const
<> 160:d5399cc887bb 161 {
<> 160:d5399cc887bb 162 return flash_get_size(&_flash);
<> 160:d5399cc887bb 163 }
<> 160:d5399cc887bb 164
<> 160:d5399cc887bb 165 }
<> 160:d5399cc887bb 166
<> 160:d5399cc887bb 167 #endif