Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
HeapBlockDevice.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "HeapBlockDevice.h" 00018 00019 00020 HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t block) 00021 : _read_size(block), _program_size(block), _erase_size(block) 00022 , _count(size / block), _blocks(0) 00023 { 00024 MBED_ASSERT(_count * _erase_size == size); 00025 } 00026 00027 HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase) 00028 : _read_size(read), _program_size(program), _erase_size(erase) 00029 , _count(size / erase), _blocks(0) 00030 { 00031 MBED_ASSERT(_count * _erase_size == size); 00032 } 00033 00034 HeapBlockDevice::~HeapBlockDevice() 00035 { 00036 if (_blocks) { 00037 for (size_t i = 0; i < _count; i++) { 00038 free(_blocks[i]); 00039 } 00040 00041 delete[] _blocks; 00042 _blocks = 0; 00043 } 00044 } 00045 00046 int HeapBlockDevice::init() 00047 { 00048 if (!_blocks) { 00049 _blocks = new uint8_t*[_count]; 00050 for (size_t i = 0; i < _count; i++) { 00051 _blocks[i] = 0; 00052 } 00053 } 00054 00055 return BD_ERROR_OK; 00056 } 00057 00058 int HeapBlockDevice::deinit() 00059 { 00060 // Heapory is lazily cleaned up in destructor to allow 00061 // data to live across de/reinitialization 00062 return BD_ERROR_OK; 00063 } 00064 00065 bd_size_t HeapBlockDevice::get_read_size() const 00066 { 00067 return _read_size; 00068 } 00069 00070 bd_size_t HeapBlockDevice::get_program_size() const 00071 { 00072 return _program_size; 00073 } 00074 00075 bd_size_t HeapBlockDevice::get_erase_size() const 00076 { 00077 return _erase_size; 00078 } 00079 00080 bd_size_t HeapBlockDevice::size() const 00081 { 00082 return _count * _erase_size; 00083 } 00084 00085 int HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) 00086 { 00087 MBED_ASSERT(is_valid_read(addr, size)); 00088 uint8_t *buffer = static_cast<uint8_t*>(b); 00089 00090 while (size > 0) { 00091 bd_addr_t hi = addr / _erase_size; 00092 bd_addr_t lo = addr % _erase_size; 00093 00094 if (_blocks[hi]) { 00095 memcpy(buffer, &_blocks[hi][lo], _read_size); 00096 } else { 00097 memset(buffer, 0, _read_size); 00098 } 00099 00100 buffer += _read_size; 00101 addr += _read_size; 00102 size -= _read_size; 00103 } 00104 00105 return 0; 00106 } 00107 00108 int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) 00109 { 00110 MBED_ASSERT(is_valid_program(addr, size)); 00111 const uint8_t *buffer = static_cast<const uint8_t*>(b); 00112 00113 while (size > 0) { 00114 bd_addr_t hi = addr / _erase_size; 00115 bd_addr_t lo = addr % _erase_size; 00116 00117 if (!_blocks[hi]) { 00118 _blocks[hi] = (uint8_t*)malloc(_erase_size); 00119 if (!_blocks[hi]) { 00120 return BD_ERROR_DEVICE_ERROR; 00121 } 00122 } 00123 00124 memcpy(&_blocks[hi][lo], buffer, _program_size); 00125 00126 buffer += _program_size; 00127 addr += _program_size; 00128 size -= _program_size; 00129 } 00130 00131 return 0; 00132 } 00133 00134 int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size) 00135 { 00136 MBED_ASSERT(is_valid_erase(addr, size)); 00137 // TODO assert on programming unerased blocks 00138 00139 return 0; 00140 } 00141
Generated on Tue Jul 12 2022 11:02:25 by
1.7.2