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.
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 MBED_ASSERT(_blocks != NULL); 00061 // Memory is lazily cleaned up in destructor to allow 00062 // data to live across de/reinitialization 00063 return BD_ERROR_OK; 00064 } 00065 00066 bd_size_t HeapBlockDevice::get_read_size() const 00067 { 00068 MBED_ASSERT(_blocks != NULL); 00069 return _read_size; 00070 } 00071 00072 bd_size_t HeapBlockDevice::get_program_size() const 00073 { 00074 MBED_ASSERT(_blocks != NULL); 00075 return _program_size; 00076 } 00077 00078 bd_size_t HeapBlockDevice::get_erase_size() const 00079 { 00080 MBED_ASSERT(_blocks != NULL); 00081 return _erase_size; 00082 } 00083 00084 bd_size_t HeapBlockDevice::get_erase_size(bd_addr_t addr) const 00085 { 00086 MBED_ASSERT(_blocks != NULL); 00087 return _erase_size; 00088 } 00089 00090 bd_size_t HeapBlockDevice::size() const 00091 { 00092 MBED_ASSERT(_blocks != NULL); 00093 return _count * _erase_size; 00094 } 00095 00096 int HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) 00097 { 00098 MBED_ASSERT(_blocks != NULL); 00099 MBED_ASSERT(is_valid_read(addr, size)); 00100 uint8_t *buffer = static_cast<uint8_t*>(b); 00101 00102 while (size > 0) { 00103 bd_addr_t hi = addr / _erase_size; 00104 bd_addr_t lo = addr % _erase_size; 00105 00106 if (_blocks[hi]) { 00107 memcpy(buffer, &_blocks[hi][lo], _read_size); 00108 } else { 00109 memset(buffer, 0, _read_size); 00110 } 00111 00112 buffer += _read_size; 00113 addr += _read_size; 00114 size -= _read_size; 00115 } 00116 00117 return 0; 00118 } 00119 00120 int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) 00121 { 00122 MBED_ASSERT(_blocks != NULL); 00123 MBED_ASSERT(is_valid_program(addr, size)); 00124 const uint8_t *buffer = static_cast<const uint8_t*>(b); 00125 00126 while (size > 0) { 00127 bd_addr_t hi = addr / _erase_size; 00128 bd_addr_t lo = addr % _erase_size; 00129 00130 if (!_blocks[hi]) { 00131 _blocks[hi] = (uint8_t*)malloc(_erase_size); 00132 if (!_blocks[hi]) { 00133 return BD_ERROR_DEVICE_ERROR; 00134 } 00135 } 00136 00137 memcpy(&_blocks[hi][lo], buffer, _program_size); 00138 00139 buffer += _program_size; 00140 addr += _program_size; 00141 size -= _program_size; 00142 } 00143 00144 return 0; 00145 } 00146 00147 int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size) 00148 { 00149 MBED_ASSERT(_blocks != NULL); 00150 MBED_ASSERT(is_valid_erase(addr, size)); 00151 // TODO assert on programming unerased blocks 00152 00153 return 0; 00154 } 00155
Generated on Tue Jul 12 2022 12:44:20 by
