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