Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Child:
4:75df35ef4fb6
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 /* mbed Microcontroller Library
borlanic 0:380207fcb5c1 2 * Copyright (c) 2017 ARM Limited
borlanic 0:380207fcb5c1 3 *
borlanic 0:380207fcb5c1 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:380207fcb5c1 5 * you may not use this file except in compliance with the License.
borlanic 0:380207fcb5c1 6 * You may obtain a copy of the License at
borlanic 0:380207fcb5c1 7 *
borlanic 0:380207fcb5c1 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:380207fcb5c1 9 *
borlanic 0:380207fcb5c1 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:380207fcb5c1 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:380207fcb5c1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:380207fcb5c1 13 * See the License for the specific language governing permissions and
borlanic 0:380207fcb5c1 14 * limitations under the License.
borlanic 0:380207fcb5c1 15 */
borlanic 0:380207fcb5c1 16
borlanic 0:380207fcb5c1 17 #include "HeapBlockDevice.h"
borlanic 0:380207fcb5c1 18
borlanic 0:380207fcb5c1 19
borlanic 0:380207fcb5c1 20 HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t block)
borlanic 0:380207fcb5c1 21 : _read_size(block), _program_size(block), _erase_size(block)
borlanic 0:380207fcb5c1 22 , _count(size / block), _blocks(0)
borlanic 0:380207fcb5c1 23 {
borlanic 0:380207fcb5c1 24 MBED_ASSERT(_count * _erase_size == size);
borlanic 0:380207fcb5c1 25 }
borlanic 0:380207fcb5c1 26
borlanic 0:380207fcb5c1 27 HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase)
borlanic 0:380207fcb5c1 28 : _read_size(read), _program_size(program), _erase_size(erase)
borlanic 0:380207fcb5c1 29 , _count(size / erase), _blocks(0)
borlanic 0:380207fcb5c1 30 {
borlanic 0:380207fcb5c1 31 MBED_ASSERT(_count * _erase_size == size);
borlanic 0:380207fcb5c1 32 }
borlanic 0:380207fcb5c1 33
borlanic 0:380207fcb5c1 34 HeapBlockDevice::~HeapBlockDevice()
borlanic 0:380207fcb5c1 35 {
borlanic 0:380207fcb5c1 36 if (_blocks) {
borlanic 0:380207fcb5c1 37 for (size_t i = 0; i < _count; i++) {
borlanic 0:380207fcb5c1 38 free(_blocks[i]);
borlanic 0:380207fcb5c1 39 }
borlanic 0:380207fcb5c1 40
borlanic 0:380207fcb5c1 41 delete[] _blocks;
borlanic 0:380207fcb5c1 42 _blocks = 0;
borlanic 0:380207fcb5c1 43 }
borlanic 0:380207fcb5c1 44 }
borlanic 0:380207fcb5c1 45
borlanic 0:380207fcb5c1 46 int HeapBlockDevice::init()
borlanic 0:380207fcb5c1 47 {
borlanic 0:380207fcb5c1 48 if (!_blocks) {
borlanic 0:380207fcb5c1 49 _blocks = new uint8_t*[_count];
borlanic 0:380207fcb5c1 50 for (size_t i = 0; i < _count; i++) {
borlanic 0:380207fcb5c1 51 _blocks[i] = 0;
borlanic 0:380207fcb5c1 52 }
borlanic 0:380207fcb5c1 53 }
borlanic 0:380207fcb5c1 54
borlanic 0:380207fcb5c1 55 return BD_ERROR_OK;
borlanic 0:380207fcb5c1 56 }
borlanic 0:380207fcb5c1 57
borlanic 0:380207fcb5c1 58 int HeapBlockDevice::deinit()
borlanic 0:380207fcb5c1 59 {
borlanic 0:380207fcb5c1 60 MBED_ASSERT(_blocks != NULL);
borlanic 0:380207fcb5c1 61 // Memory is lazily cleaned up in destructor to allow
borlanic 0:380207fcb5c1 62 // data to live across de/reinitialization
borlanic 0:380207fcb5c1 63 return BD_ERROR_OK;
borlanic 0:380207fcb5c1 64 }
borlanic 0:380207fcb5c1 65
borlanic 0:380207fcb5c1 66 bd_size_t HeapBlockDevice::get_read_size() const
borlanic 0:380207fcb5c1 67 {
borlanic 0:380207fcb5c1 68 MBED_ASSERT(_blocks != NULL);
borlanic 0:380207fcb5c1 69 return _read_size;
borlanic 0:380207fcb5c1 70 }
borlanic 0:380207fcb5c1 71
borlanic 0:380207fcb5c1 72 bd_size_t HeapBlockDevice::get_program_size() const
borlanic 0:380207fcb5c1 73 {
borlanic 0:380207fcb5c1 74 MBED_ASSERT(_blocks != NULL);
borlanic 0:380207fcb5c1 75 return _program_size;
borlanic 0:380207fcb5c1 76 }
borlanic 0:380207fcb5c1 77
borlanic 0:380207fcb5c1 78 bd_size_t HeapBlockDevice::get_erase_size() const
borlanic 0:380207fcb5c1 79 {
borlanic 0:380207fcb5c1 80 MBED_ASSERT(_blocks != NULL);
borlanic 0:380207fcb5c1 81 return _erase_size;
borlanic 0:380207fcb5c1 82 }
borlanic 0:380207fcb5c1 83
borlanic 0:380207fcb5c1 84 bd_size_t HeapBlockDevice::size() const
borlanic 0:380207fcb5c1 85 {
borlanic 0:380207fcb5c1 86 MBED_ASSERT(_blocks != NULL);
borlanic 0:380207fcb5c1 87 return _count * _erase_size;
borlanic 0:380207fcb5c1 88 }
borlanic 0:380207fcb5c1 89
borlanic 0:380207fcb5c1 90 int HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
borlanic 0:380207fcb5c1 91 {
borlanic 0:380207fcb5c1 92 MBED_ASSERT(_blocks != NULL);
borlanic 0:380207fcb5c1 93 MBED_ASSERT(is_valid_read(addr, size));
borlanic 0:380207fcb5c1 94 uint8_t *buffer = static_cast<uint8_t*>(b);
borlanic 0:380207fcb5c1 95
borlanic 0:380207fcb5c1 96 while (size > 0) {
borlanic 0:380207fcb5c1 97 bd_addr_t hi = addr / _erase_size;
borlanic 0:380207fcb5c1 98 bd_addr_t lo = addr % _erase_size;
borlanic 0:380207fcb5c1 99
borlanic 0:380207fcb5c1 100 if (_blocks[hi]) {
borlanic 0:380207fcb5c1 101 memcpy(buffer, &_blocks[hi][lo], _read_size);
borlanic 0:380207fcb5c1 102 } else {
borlanic 0:380207fcb5c1 103 memset(buffer, 0, _read_size);
borlanic 0:380207fcb5c1 104 }
borlanic 0:380207fcb5c1 105
borlanic 0:380207fcb5c1 106 buffer += _read_size;
borlanic 0:380207fcb5c1 107 addr += _read_size;
borlanic 0:380207fcb5c1 108 size -= _read_size;
borlanic 0:380207fcb5c1 109 }
borlanic 0:380207fcb5c1 110
borlanic 0:380207fcb5c1 111 return 0;
borlanic 0:380207fcb5c1 112 }
borlanic 0:380207fcb5c1 113
borlanic 0:380207fcb5c1 114 int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
borlanic 0:380207fcb5c1 115 {
borlanic 0:380207fcb5c1 116 MBED_ASSERT(_blocks != NULL);
borlanic 0:380207fcb5c1 117 MBED_ASSERT(is_valid_program(addr, size));
borlanic 0:380207fcb5c1 118 const uint8_t *buffer = static_cast<const uint8_t*>(b);
borlanic 0:380207fcb5c1 119
borlanic 0:380207fcb5c1 120 while (size > 0) {
borlanic 0:380207fcb5c1 121 bd_addr_t hi = addr / _erase_size;
borlanic 0:380207fcb5c1 122 bd_addr_t lo = addr % _erase_size;
borlanic 0:380207fcb5c1 123
borlanic 0:380207fcb5c1 124 if (!_blocks[hi]) {
borlanic 0:380207fcb5c1 125 _blocks[hi] = (uint8_t*)malloc(_erase_size);
borlanic 0:380207fcb5c1 126 if (!_blocks[hi]) {
borlanic 0:380207fcb5c1 127 return BD_ERROR_DEVICE_ERROR;
borlanic 0:380207fcb5c1 128 }
borlanic 0:380207fcb5c1 129 }
borlanic 0:380207fcb5c1 130
borlanic 0:380207fcb5c1 131 memcpy(&_blocks[hi][lo], buffer, _program_size);
borlanic 0:380207fcb5c1 132
borlanic 0:380207fcb5c1 133 buffer += _program_size;
borlanic 0:380207fcb5c1 134 addr += _program_size;
borlanic 0:380207fcb5c1 135 size -= _program_size;
borlanic 0:380207fcb5c1 136 }
borlanic 0:380207fcb5c1 137
borlanic 0:380207fcb5c1 138 return 0;
borlanic 0:380207fcb5c1 139 }
borlanic 0:380207fcb5c1 140
borlanic 0:380207fcb5c1 141 int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size)
borlanic 0:380207fcb5c1 142 {
borlanic 0:380207fcb5c1 143 MBED_ASSERT(_blocks != NULL);
borlanic 0:380207fcb5c1 144 MBED_ASSERT(is_valid_erase(addr, size));
borlanic 0:380207fcb5c1 145 // TODO assert on programming unerased blocks
borlanic 0:380207fcb5c1 146
borlanic 0:380207fcb5c1 147 return 0;
borlanic 0:380207fcb5c1 148 }
borlanic 0:380207fcb5c1 149