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