mbed-os for GR-LYCHEE
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
features/filesystem/bd/HeapBlockDevice.cpp@0:f782d9c66c49, 2018-02-02 (annotated)
- Committer:
- dkato
- Date:
- Fri Feb 02 05:42:23 2018 +0000
- Revision:
- 0:f782d9c66c49
mbed-os for GR-LYCHEE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:f782d9c66c49 | 1 | /* mbed Microcontroller Library |
dkato | 0:f782d9c66c49 | 2 | * Copyright (c) 2017 ARM Limited |
dkato | 0:f782d9c66c49 | 3 | * |
dkato | 0:f782d9c66c49 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dkato | 0:f782d9c66c49 | 5 | * you may not use this file except in compliance with the License. |
dkato | 0:f782d9c66c49 | 6 | * You may obtain a copy of the License at |
dkato | 0:f782d9c66c49 | 7 | * |
dkato | 0:f782d9c66c49 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dkato | 0:f782d9c66c49 | 9 | * |
dkato | 0:f782d9c66c49 | 10 | * Unless required by applicable law or agreed to in writing, software |
dkato | 0:f782d9c66c49 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dkato | 0:f782d9c66c49 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dkato | 0:f782d9c66c49 | 13 | * See the License for the specific language governing permissions and |
dkato | 0:f782d9c66c49 | 14 | * limitations under the License. |
dkato | 0:f782d9c66c49 | 15 | */ |
dkato | 0:f782d9c66c49 | 16 | |
dkato | 0:f782d9c66c49 | 17 | #include "HeapBlockDevice.h" |
dkato | 0:f782d9c66c49 | 18 | |
dkato | 0:f782d9c66c49 | 19 | |
dkato | 0:f782d9c66c49 | 20 | HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t block) |
dkato | 0:f782d9c66c49 | 21 | : _read_size(block), _program_size(block), _erase_size(block) |
dkato | 0:f782d9c66c49 | 22 | , _count(size / block), _blocks(0) |
dkato | 0:f782d9c66c49 | 23 | { |
dkato | 0:f782d9c66c49 | 24 | MBED_ASSERT(_count * _erase_size == size); |
dkato | 0:f782d9c66c49 | 25 | } |
dkato | 0:f782d9c66c49 | 26 | |
dkato | 0:f782d9c66c49 | 27 | HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase) |
dkato | 0:f782d9c66c49 | 28 | : _read_size(read), _program_size(program), _erase_size(erase) |
dkato | 0:f782d9c66c49 | 29 | , _count(size / erase), _blocks(0) |
dkato | 0:f782d9c66c49 | 30 | { |
dkato | 0:f782d9c66c49 | 31 | MBED_ASSERT(_count * _erase_size == size); |
dkato | 0:f782d9c66c49 | 32 | } |
dkato | 0:f782d9c66c49 | 33 | |
dkato | 0:f782d9c66c49 | 34 | HeapBlockDevice::~HeapBlockDevice() |
dkato | 0:f782d9c66c49 | 35 | { |
dkato | 0:f782d9c66c49 | 36 | if (_blocks) { |
dkato | 0:f782d9c66c49 | 37 | for (size_t i = 0; i < _count; i++) { |
dkato | 0:f782d9c66c49 | 38 | free(_blocks[i]); |
dkato | 0:f782d9c66c49 | 39 | } |
dkato | 0:f782d9c66c49 | 40 | |
dkato | 0:f782d9c66c49 | 41 | delete[] _blocks; |
dkato | 0:f782d9c66c49 | 42 | _blocks = 0; |
dkato | 0:f782d9c66c49 | 43 | } |
dkato | 0:f782d9c66c49 | 44 | } |
dkato | 0:f782d9c66c49 | 45 | |
dkato | 0:f782d9c66c49 | 46 | int HeapBlockDevice::init() |
dkato | 0:f782d9c66c49 | 47 | { |
dkato | 0:f782d9c66c49 | 48 | if (!_blocks) { |
dkato | 0:f782d9c66c49 | 49 | _blocks = new uint8_t*[_count]; |
dkato | 0:f782d9c66c49 | 50 | for (size_t i = 0; i < _count; i++) { |
dkato | 0:f782d9c66c49 | 51 | _blocks[i] = 0; |
dkato | 0:f782d9c66c49 | 52 | } |
dkato | 0:f782d9c66c49 | 53 | } |
dkato | 0:f782d9c66c49 | 54 | |
dkato | 0:f782d9c66c49 | 55 | return BD_ERROR_OK; |
dkato | 0:f782d9c66c49 | 56 | } |
dkato | 0:f782d9c66c49 | 57 | |
dkato | 0:f782d9c66c49 | 58 | int HeapBlockDevice::deinit() |
dkato | 0:f782d9c66c49 | 59 | { |
dkato | 0:f782d9c66c49 | 60 | // Heapory is lazily cleaned up in destructor to allow |
dkato | 0:f782d9c66c49 | 61 | // data to live across de/reinitialization |
dkato | 0:f782d9c66c49 | 62 | return BD_ERROR_OK; |
dkato | 0:f782d9c66c49 | 63 | } |
dkato | 0:f782d9c66c49 | 64 | |
dkato | 0:f782d9c66c49 | 65 | bd_size_t HeapBlockDevice::get_read_size() const |
dkato | 0:f782d9c66c49 | 66 | { |
dkato | 0:f782d9c66c49 | 67 | return _read_size; |
dkato | 0:f782d9c66c49 | 68 | } |
dkato | 0:f782d9c66c49 | 69 | |
dkato | 0:f782d9c66c49 | 70 | bd_size_t HeapBlockDevice::get_program_size() const |
dkato | 0:f782d9c66c49 | 71 | { |
dkato | 0:f782d9c66c49 | 72 | return _program_size; |
dkato | 0:f782d9c66c49 | 73 | } |
dkato | 0:f782d9c66c49 | 74 | |
dkato | 0:f782d9c66c49 | 75 | bd_size_t HeapBlockDevice::get_erase_size() const |
dkato | 0:f782d9c66c49 | 76 | { |
dkato | 0:f782d9c66c49 | 77 | return _erase_size; |
dkato | 0:f782d9c66c49 | 78 | } |
dkato | 0:f782d9c66c49 | 79 | |
dkato | 0:f782d9c66c49 | 80 | bd_size_t HeapBlockDevice::size() const |
dkato | 0:f782d9c66c49 | 81 | { |
dkato | 0:f782d9c66c49 | 82 | return _count * _erase_size; |
dkato | 0:f782d9c66c49 | 83 | } |
dkato | 0:f782d9c66c49 | 84 | |
dkato | 0:f782d9c66c49 | 85 | int HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) |
dkato | 0:f782d9c66c49 | 86 | { |
dkato | 0:f782d9c66c49 | 87 | MBED_ASSERT(is_valid_read(addr, size)); |
dkato | 0:f782d9c66c49 | 88 | uint8_t *buffer = static_cast<uint8_t*>(b); |
dkato | 0:f782d9c66c49 | 89 | |
dkato | 0:f782d9c66c49 | 90 | while (size > 0) { |
dkato | 0:f782d9c66c49 | 91 | bd_addr_t hi = addr / _erase_size; |
dkato | 0:f782d9c66c49 | 92 | bd_addr_t lo = addr % _erase_size; |
dkato | 0:f782d9c66c49 | 93 | |
dkato | 0:f782d9c66c49 | 94 | if (_blocks[hi]) { |
dkato | 0:f782d9c66c49 | 95 | memcpy(buffer, &_blocks[hi][lo], _read_size); |
dkato | 0:f782d9c66c49 | 96 | } else { |
dkato | 0:f782d9c66c49 | 97 | memset(buffer, 0, _read_size); |
dkato | 0:f782d9c66c49 | 98 | } |
dkato | 0:f782d9c66c49 | 99 | |
dkato | 0:f782d9c66c49 | 100 | buffer += _read_size; |
dkato | 0:f782d9c66c49 | 101 | addr += _read_size; |
dkato | 0:f782d9c66c49 | 102 | size -= _read_size; |
dkato | 0:f782d9c66c49 | 103 | } |
dkato | 0:f782d9c66c49 | 104 | |
dkato | 0:f782d9c66c49 | 105 | return 0; |
dkato | 0:f782d9c66c49 | 106 | } |
dkato | 0:f782d9c66c49 | 107 | |
dkato | 0:f782d9c66c49 | 108 | int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) |
dkato | 0:f782d9c66c49 | 109 | { |
dkato | 0:f782d9c66c49 | 110 | MBED_ASSERT(is_valid_program(addr, size)); |
dkato | 0:f782d9c66c49 | 111 | const uint8_t *buffer = static_cast<const uint8_t*>(b); |
dkato | 0:f782d9c66c49 | 112 | |
dkato | 0:f782d9c66c49 | 113 | while (size > 0) { |
dkato | 0:f782d9c66c49 | 114 | bd_addr_t hi = addr / _erase_size; |
dkato | 0:f782d9c66c49 | 115 | bd_addr_t lo = addr % _erase_size; |
dkato | 0:f782d9c66c49 | 116 | |
dkato | 0:f782d9c66c49 | 117 | if (!_blocks[hi]) { |
dkato | 0:f782d9c66c49 | 118 | _blocks[hi] = (uint8_t*)malloc(_erase_size); |
dkato | 0:f782d9c66c49 | 119 | if (!_blocks[hi]) { |
dkato | 0:f782d9c66c49 | 120 | return BD_ERROR_DEVICE_ERROR; |
dkato | 0:f782d9c66c49 | 121 | } |
dkato | 0:f782d9c66c49 | 122 | } |
dkato | 0:f782d9c66c49 | 123 | |
dkato | 0:f782d9c66c49 | 124 | memcpy(&_blocks[hi][lo], buffer, _program_size); |
dkato | 0:f782d9c66c49 | 125 | |
dkato | 0:f782d9c66c49 | 126 | buffer += _program_size; |
dkato | 0:f782d9c66c49 | 127 | addr += _program_size; |
dkato | 0:f782d9c66c49 | 128 | size -= _program_size; |
dkato | 0:f782d9c66c49 | 129 | } |
dkato | 0:f782d9c66c49 | 130 | |
dkato | 0:f782d9c66c49 | 131 | return 0; |
dkato | 0:f782d9c66c49 | 132 | } |
dkato | 0:f782d9c66c49 | 133 | |
dkato | 0:f782d9c66c49 | 134 | int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size) |
dkato | 0:f782d9c66c49 | 135 | { |
dkato | 0:f782d9c66c49 | 136 | MBED_ASSERT(is_valid_erase(addr, size)); |
dkato | 0:f782d9c66c49 | 137 | // TODO assert on programming unerased blocks |
dkato | 0:f782d9c66c49 | 138 | |
dkato | 0:f782d9c66c49 | 139 | return 0; |
dkato | 0:f782d9c66c49 | 140 | } |
dkato | 0:f782d9c66c49 | 141 |