Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FlashSimBlockDevice.cpp Source File

FlashSimBlockDevice.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 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 "FlashSimBlockDevice.h"
00018 #include "mbed_assert.h"
00019 #include <algorithm>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 
00023 static const bd_size_t min_blank_buf_size = 32;
00024 
00025 static inline uint32_t align_up(bd_size_t val, bd_size_t size)
00026 {
00027     return (((val - 1) / size) + 1) * size;
00028 }
00029 
00030 FlashSimBlockDevice::FlashSimBlockDevice(BlockDevice *bd, uint8_t erase_value) :
00031     _erase_value(erase_value), _blank_buf_size(0),
00032     _blank_buf(0), _bd(bd)
00033 {
00034 }
00035 
00036 FlashSimBlockDevice::~FlashSimBlockDevice()
00037 {
00038     deinit();
00039     delete[] _blank_buf;
00040 }
00041 
00042 int FlashSimBlockDevice::init()
00043 {
00044     int ret = _bd->init();
00045     if (ret) {
00046         return ret;
00047     }
00048     _blank_buf_size = align_up(min_blank_buf_size, _bd->get_program_size());
00049     if (!_blank_buf) {
00050         _blank_buf = new uint8_t[_blank_buf_size];
00051         MBED_ASSERT(_blank_buf);
00052     }
00053     return BD_ERROR_OK;
00054 }
00055 
00056 int FlashSimBlockDevice::deinit()
00057 {
00058     return _bd->deinit();
00059 }
00060 
00061 int FlashSimBlockDevice::sync()
00062 {
00063     return _bd->sync();
00064 }
00065 
00066 bd_size_t FlashSimBlockDevice::get_read_size() const
00067 {
00068     return _bd->get_read_size();
00069 }
00070 
00071 bd_size_t FlashSimBlockDevice::get_program_size() const
00072 {
00073     return _bd->get_program_size();
00074 }
00075 
00076 bd_size_t FlashSimBlockDevice::get_erase_size() const
00077 {
00078     return _bd->get_erase_size();
00079 }
00080 
00081 bd_size_t FlashSimBlockDevice::get_erase_size(bd_addr_t addr) const
00082 {
00083     return _bd->get_erase_size(addr);
00084 }
00085 
00086 bd_size_t FlashSimBlockDevice::size() const
00087 {
00088     return _bd->size();
00089 }
00090 
00091 int FlashSimBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
00092 {
00093     return _bd->read(b, addr, size);
00094 }
00095 
00096 int FlashSimBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
00097 {
00098     MBED_ASSERT(is_valid_program(addr, size));
00099     bd_addr_t curr_addr = addr;
00100     bd_size_t curr_size = size;
00101 
00102     const uint8_t *buf = (const uint8_t *) b;
00103     while (curr_size) {
00104         bd_size_t read_size = std::min(_blank_buf_size, curr_size);
00105         int ret = _bd->read(_blank_buf, curr_addr, read_size);
00106         if (ret) {
00107             return ret;
00108         }
00109         for (bd_size_t i = 0; i < read_size; i++) {
00110             // Allow either programming on blanks or programming the same value
00111             // (as real flash devices do)
00112             if ((_blank_buf[i] != _erase_value) && (_blank_buf[i] != *buf)) {
00113                 return BD_ERROR_NOT_ERASED;
00114             }
00115             buf++;
00116         }
00117         curr_addr += read_size;
00118         curr_size -= read_size;
00119     }
00120 
00121     return _bd->program(b, addr, size);
00122 }
00123 
00124 int FlashSimBlockDevice::erase(bd_addr_t addr, bd_size_t size)
00125 {
00126     MBED_ASSERT(is_valid_erase(addr, size));
00127 
00128     bd_addr_t curr_addr = addr;
00129     bd_size_t curr_size = size;
00130 
00131     memset(_blank_buf, _erase_value, (unsigned int) _blank_buf_size);
00132 
00133     while (curr_size) {
00134         bd_size_t prog_size = std::min(_blank_buf_size, curr_size);
00135         int ret = _bd->program(_blank_buf, curr_addr, prog_size);
00136         if (ret) {
00137             return ret;
00138         }
00139         curr_addr += prog_size;
00140         curr_size -= prog_size;
00141     }
00142 
00143     return BD_ERROR_OK;
00144 }
00145 
00146 int FlashSimBlockDevice::get_erase_value() const
00147 {
00148     return _erase_value;
00149 }