Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HeapBlockDevice.cpp Source File

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 #include "platform/mbed_atomic.h"
00019 #include <stdlib.h>
00020 #include <string.h>
00021 
00022 namespace mbed {
00023 
00024 HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t block)
00025     : _read_size(block), _program_size(block), _erase_size(block)
00026     , _count(size / block), _blocks(0), _init_ref_count(0), _is_initialized(false)
00027 {
00028     MBED_ASSERT(_count * _erase_size == size);
00029 }
00030 
00031 HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase)
00032     : _read_size(read), _program_size(program), _erase_size(erase)
00033     , _count(size / erase), _blocks(0), _init_ref_count(0), _is_initialized(false)
00034 {
00035     MBED_ASSERT(_count * _erase_size == size);
00036 }
00037 
00038 HeapBlockDevice::~HeapBlockDevice()
00039 {
00040     if (_blocks) {
00041         for (size_t i = 0; i < _count; i++) {
00042             free(_blocks[i]);
00043         }
00044 
00045         delete[] _blocks;
00046         _blocks = 0;
00047     }
00048 }
00049 
00050 int HeapBlockDevice::init()
00051 {
00052     uint32_t val = core_util_atomic_incr_u32 (&_init_ref_count, 1);
00053 
00054     if (val != 1) {
00055         return BD_ERROR_OK;
00056     }
00057 
00058     if (!_blocks) {
00059         _blocks = new uint8_t *[_count];
00060         for (size_t i = 0; i < _count; i++) {
00061             _blocks[i] = 0;
00062         }
00063     }
00064 
00065     _is_initialized = true;
00066     return BD_ERROR_OK;
00067 }
00068 
00069 int HeapBlockDevice::deinit()
00070 {
00071     if (!_is_initialized) {
00072         return BD_ERROR_OK;
00073     }
00074 
00075     uint32_t val = core_util_atomic_decr_u32 (&_init_ref_count, 1);
00076 
00077     if (val) {
00078         return BD_ERROR_OK;
00079     }
00080 
00081     MBED_ASSERT(_blocks != NULL);
00082     // Memory is lazily cleaned up in destructor to allow
00083     // data to live across de/reinitialization
00084     _is_initialized = false;
00085     return BD_ERROR_OK;
00086 }
00087 
00088 bd_size_t HeapBlockDevice::get_read_size() const
00089 {
00090     return _read_size;
00091 }
00092 
00093 bd_size_t HeapBlockDevice::get_program_size() const
00094 {
00095     return _program_size;
00096 }
00097 
00098 bd_size_t HeapBlockDevice::get_erase_size() const
00099 {
00100     return _erase_size;
00101 }
00102 
00103 bd_size_t HeapBlockDevice::get_erase_size(bd_addr_t addr) const
00104 {
00105     return _erase_size;
00106 }
00107 
00108 bd_size_t HeapBlockDevice::size() const
00109 {
00110     return _count * _erase_size;
00111 }
00112 
00113 int HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
00114 {
00115     if (!_is_initialized) {
00116         return BD_ERROR_DEVICE_ERROR;
00117     }
00118     if (!is_valid_read(addr, size)) {
00119         return BD_ERROR_DEVICE_ERROR;
00120     }
00121 
00122     uint8_t *buffer = static_cast<uint8_t *>(b);
00123 
00124     while (size > 0) {
00125         bd_addr_t hi = addr / _erase_size;
00126         bd_addr_t lo = addr % _erase_size;
00127 
00128         if (_blocks[hi]) {
00129             memcpy(buffer, &_blocks[hi][lo], _read_size);
00130         } else {
00131             memset(buffer, 0, _read_size);
00132         }
00133 
00134         buffer += _read_size;
00135         addr += _read_size;
00136         size -= _read_size;
00137     }
00138 
00139     return 0;
00140 }
00141 
00142 int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
00143 {
00144     if (!_is_initialized) {
00145         return BD_ERROR_DEVICE_ERROR;
00146     }
00147     if (!is_valid_program(addr, size)) {
00148         return BD_ERROR_DEVICE_ERROR;
00149     }
00150 
00151     const uint8_t *buffer = static_cast<const uint8_t *>(b);
00152 
00153     while (size > 0) {
00154         bd_addr_t hi = addr / _erase_size;
00155         bd_addr_t lo = addr % _erase_size;
00156 
00157         if (!_blocks[hi]) {
00158             _blocks[hi] = (uint8_t *)malloc(_erase_size);
00159             if (!_blocks[hi]) {
00160                 return BD_ERROR_DEVICE_ERROR;
00161             }
00162         }
00163 
00164         memcpy(&_blocks[hi][lo], buffer, _program_size);
00165 
00166         buffer += _program_size;
00167         addr += _program_size;
00168         size -= _program_size;
00169     }
00170 
00171     return 0;
00172 }
00173 
00174 int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size)
00175 {
00176     if (!_is_initialized) {
00177         return BD_ERROR_DEVICE_ERROR;
00178     }
00179     if (!is_valid_erase(addr, size)) {
00180         return BD_ERROR_DEVICE_ERROR;
00181     }
00182     return 0;
00183 }
00184 
00185 const char *HeapBlockDevice::get_type() const
00186 {
00187     return "HEAP";
00188 }
00189 
00190 } // namespace mbed