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