bd: Adopted the block device api in the HeapBlockDevice

Port of MemBlockDevice from mbed 2
pull/3762/head
Christopher Haster 2017-01-20 12:34:26 -06:00 committed by Simon Hughes
parent 99698379ef
commit 4633d3129d
3 changed files with 367 additions and 0 deletions

View File

@ -0,0 +1,75 @@
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "HeapBlockDevice.h"
#include <stdlib.h>
using namespace utest::v1;
#define BLOCK_SIZE 512
uint8_t write_block[BLOCK_SIZE];
uint8_t read_block[BLOCK_SIZE];
// Simple test which reads and writes a block
void test_read_write() {
HeapBlockDevice bd(16*BLOCK_SIZE, BLOCK_SIZE);
int err = bd.init();
TEST_ASSERT_EQUAL(0, err);
// Fill with random sequence
srand(1);
for (int i = 0; i < BLOCK_SIZE; i++) {
write_block[i] = 0xff & rand();
}
// Write, sync, and read the block
err = bd.write(write_block, 0, BLOCK_SIZE);
TEST_ASSERT_EQUAL(0, err);
err = bd.read(read_block, 0, BLOCK_SIZE);
TEST_ASSERT_EQUAL(0, err);
// Check that the data was unmodified
srand(1);
for (int i = 0; i < BLOCK_SIZE; i++) {
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
}
err = bd.deinit();
TEST_ASSERT_EQUAL(0, err);
}
// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(10, "default_auto");
return verbose_test_setup_handler(number_of_cases);
}
Case cases[] = {
Case("Testing read write of a block", test_read_write),
};
Specification specification(test_setup, cases);
int main() {
return !Harness::run(specification);
}

View File

@ -0,0 +1,160 @@
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "HeapBlockDevice.h"
HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t block)
: _read_size(block), _program_size(block), _erase_size(block)
, _count(size / block), _blocks(0)
{
MBED_ASSERT(_count * _erase_size == size);
}
HeapBlockDevice::HeapBlockDevice(bd_size_t size,
bd_size_t read, bd_size_t program, bd_size_t erase)
: _read_size(read), _program_size(program), _erase_size(erase)
, _count(size / erase), _blocks(0)
{
MBED_ASSERT(_count * _erase_size == size);
}
HeapBlockDevice::~HeapBlockDevice()
{
if (_blocks) {
for (size_t i = 0; i < _count; i++) {
free(_blocks[i]);
}
delete[] _blocks;
_blocks = 0;
}
}
bd_error_t HeapBlockDevice::init()
{
if (!_blocks) {
_blocks = new uint8_t*[_count];
for (size_t i = 0; i < _count; i++) {
_blocks[i] = 0;
}
}
return BD_ERROR_OK;
}
bd_error_t HeapBlockDevice::deinit()
{
// Heapory is lazily cleaned up in destructor to allow
// data to live across de/reinitialization
return BD_ERROR_OK;
}
bd_size_t HeapBlockDevice::get_read_size()
{
return _read_size;
}
bd_size_t HeapBlockDevice::get_program_size()
{
return _program_size;
}
bd_size_t HeapBlockDevice::get_erase_size()
{
return _erase_size;
}
bd_size_t HeapBlockDevice::size()
{
return _count * _erase_size;
}
bd_error_t HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
{
if (!is_valid_read(addr, size)) {
return BD_ERROR_PARAMETER;
}
uint8_t *buffer = static_cast<uint8_t*>(b);
while (size > 0) {
bd_addr_t hi = addr / _erase_size;
bd_addr_t lo = addr % _erase_size;
if (_blocks[hi]) {
memcpy(buffer, &_blocks[hi][lo], _read_size);
} else {
memset(buffer, 0, _read_size);
}
buffer += _read_size;
addr += _read_size;
size -= _read_size;
}
return 0;
}
bd_error_t HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
{
if (!is_valid_program(addr, size)) {
return BD_ERROR_PARAMETER;
}
const uint8_t *buffer = static_cast<const uint8_t*>(b);
while (size > 0) {
bd_addr_t hi = addr / _erase_size;
bd_addr_t lo = addr % _erase_size;
if (!_blocks[hi]) {
_blocks[hi] = (uint8_t*)malloc(_erase_size);
if (!_blocks[hi]) {
return BD_ERROR_DEVICE_ERROR;
}
}
memcpy(&_blocks[hi][lo], buffer, _program_size);
buffer += _program_size;
addr += _program_size;
size -= _program_size;
}
return 0;
}
bd_error_t HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size)
{
if (!is_valid_erase(addr, size)) {
return BD_ERROR_PARAMETER;
}
#ifndef NDEBUG
while (size > 0) {
bd_addr_t hi = addr / _erase_size;
if (_blocks[hi]) {
memset(_blocks[hi], 0xcc, _erase_size);
}
addr += _erase_size;
size -= _erase_size;
}
#endif
return 0;
}

View File

@ -0,0 +1,132 @@
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MBED_MEM_BLOCK_DEVICE_H
#define MBED_MEM_BLOCK_DEVICE_H
#include "BlockDevice.h"
#include "mbed.h"
/** Lazily allocated heap-backed block device
*
* Useful for simulating a block device and tests
*
* @code
* #include "mbed.h"
* #include "HeapBlockDevice.h"
*
* HeapBlockDevice bd(2048, 512); // 2048 bytes with a block size of 512 bytes
* uint8_t block[512] = "Hello World!\n";
*
* int main() {
* bd.init();
* bd.write(block, 0);
* bd.read(block, 0);
* printf("%s", block);
* bd.deinit();
* }
*/
class HeapBlockDevice : public BlockDevice {
public:
/** Lifetime of the memory block device
*/
HeapBlockDevice(bd_size_t size, bd_size_t block=512);
HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase);
virtual ~HeapBlockDevice();
/** Initialize a block device
*
* @return 0 on success or a negative error code on failure
*/
virtual bd_error_t init() = 0;
/** Deinitialize a block device
*
* @return 0 on success or a negative error code on failure
*/
virtual bd_error_t deinit() = 0;
/** Read blocks from a block device
*
* @param buffer Buffer to write blocks to
* @param addr Address of block to begin reading from
* @param size Size to read in bytes, must be a multiple of read block size
* @return 0 on success, negative error code on failure
*/
virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size) = 0;
/** Program blocks to a block device
*
* The blocks must have been erased prior to being programmed
*
* @param buffer Buffer of data to write to blocks
* @param addr Address of block to begin writing to
* @param size Size to write in bytes, must be a multiple of program block size
* @return 0 on success, negative error code on failure
*/
virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size);
/** Erase blocks on a block device
*
* The state of an erased block is undefined until it has been programmed
*
* @param addr Address of block to begin erasing
* @param size Size to erase in bytes, must be a multiple of erase block size
* @return 0 on success, negative error code on failure
*/
virtual bd_error_t erase(bd_addr_t addr, bd_size_t size);
/** Get the size of a readable block
*
* @return Size of a readable block in bytes
*/
virtual bd_size_t get_read_size();
/** Get the size of a programable block
*
* @return Size of a programable block in bytes
*/
virtual bd_size_t get_program_size();
/** Get the size of a eraseable block
*
* @return Size of a eraseable block in bytes
*/
virtual bd_size_t get_erase_size();
/** Get the total size of the underlying device
*
* @return Size of the underlying device in bytes
*/
virtual bd_size_t size();
private:
bd_size_t _read_size;
bd_size_t _program_size;
bd_size_t _erase_size;
bd_size_t _count;
uint8_t **_blocks;
};
#endif