Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /* mbed Microcontroller Library
marcozecchini 0:9fca2b23d0ba 2 * Copyright (c) 2017 ARM Limited
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
marcozecchini 0:9fca2b23d0ba 5 * of this software and associated documentation files (the "Software"), to deal
marcozecchini 0:9fca2b23d0ba 6 * in the Software without restriction, including without limitation the rights
marcozecchini 0:9fca2b23d0ba 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
marcozecchini 0:9fca2b23d0ba 8 * copies of the Software, and to permit persons to whom the Software is
marcozecchini 0:9fca2b23d0ba 9 * furnished to do so, subject to the following conditions:
marcozecchini 0:9fca2b23d0ba 10 *
marcozecchini 0:9fca2b23d0ba 11 * The above copyright notice and this permission notice shall be included in
marcozecchini 0:9fca2b23d0ba 12 * all copies or substantial portions of the Software.
marcozecchini 0:9fca2b23d0ba 13 *
marcozecchini 0:9fca2b23d0ba 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
marcozecchini 0:9fca2b23d0ba 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
marcozecchini 0:9fca2b23d0ba 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
marcozecchini 0:9fca2b23d0ba 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
marcozecchini 0:9fca2b23d0ba 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
marcozecchini 0:9fca2b23d0ba 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
marcozecchini 0:9fca2b23d0ba 20 * SOFTWARE.
marcozecchini 0:9fca2b23d0ba 21 */
marcozecchini 0:9fca2b23d0ba 22 #ifndef MBED_CHAINING_BLOCK_DEVICE_H
marcozecchini 0:9fca2b23d0ba 23 #define MBED_CHAINING_BLOCK_DEVICE_H
marcozecchini 0:9fca2b23d0ba 24
marcozecchini 0:9fca2b23d0ba 25 #include "BlockDevice.h"
marcozecchini 0:9fca2b23d0ba 26 #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 27
marcozecchini 0:9fca2b23d0ba 28
marcozecchini 0:9fca2b23d0ba 29 /** Block device for chaining multiple block devices
marcozecchini 0:9fca2b23d0ba 30 * with the similar block sizes at sequential addresses
marcozecchini 0:9fca2b23d0ba 31 *
marcozecchini 0:9fca2b23d0ba 32 * @code
marcozecchini 0:9fca2b23d0ba 33 * #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 34 * #include "HeapBlockDevice.h"
marcozecchini 0:9fca2b23d0ba 35 * #include "ChainingBlockDevice.h"
marcozecchini 0:9fca2b23d0ba 36 *
marcozecchini 0:9fca2b23d0ba 37 * // Create two smaller block devices with
marcozecchini 0:9fca2b23d0ba 38 * // 64 and 32 blocks of size 512 bytes
marcozecchini 0:9fca2b23d0ba 39 * HeapBlockDevice mem1(64*512, 512);
marcozecchini 0:9fca2b23d0ba 40 * HeapBlockDevice mem2(32*512, 512);
marcozecchini 0:9fca2b23d0ba 41 *
marcozecchini 0:9fca2b23d0ba 42 * // Create a block device backed by mem1 and mem2
marcozecchini 0:9fca2b23d0ba 43 * // contains 96 blocks of size 512 bytes
marcozecchini 0:9fca2b23d0ba 44 * BlockDevice *bds[] = {&mem1, &mem2};
marcozecchini 0:9fca2b23d0ba 45 * ChainingBlockDevice chainmem(bds);
marcozecchini 0:9fca2b23d0ba 46 * @endcode
marcozecchini 0:9fca2b23d0ba 47 */
marcozecchini 0:9fca2b23d0ba 48 class ChainingBlockDevice : public BlockDevice
marcozecchini 0:9fca2b23d0ba 49 {
marcozecchini 0:9fca2b23d0ba 50 public:
marcozecchini 0:9fca2b23d0ba 51 /** Lifetime of the memory block device
marcozecchini 0:9fca2b23d0ba 52 *
marcozecchini 0:9fca2b23d0ba 53 * @param bds Array of block devices to chain with sequential block addresses
marcozecchini 0:9fca2b23d0ba 54 * @param bd_count Number of block devices to chain
marcozecchini 0:9fca2b23d0ba 55 * @note All block devices must have the same block size
marcozecchini 0:9fca2b23d0ba 56 */
marcozecchini 0:9fca2b23d0ba 57 ChainingBlockDevice(BlockDevice **bds, size_t bd_count);
marcozecchini 0:9fca2b23d0ba 58
marcozecchini 0:9fca2b23d0ba 59 /** Lifetime of the memory block device
marcozecchini 0:9fca2b23d0ba 60 *
marcozecchini 0:9fca2b23d0ba 61 * @param bds Array of block devices to chain with sequential block addresses
marcozecchini 0:9fca2b23d0ba 62 * @note All block devices must have the same block size
marcozecchini 0:9fca2b23d0ba 63 */
marcozecchini 0:9fca2b23d0ba 64 template <size_t Size>
marcozecchini 0:9fca2b23d0ba 65 ChainingBlockDevice(BlockDevice *(&bds)[Size])
marcozecchini 0:9fca2b23d0ba 66 : _bds(bds), _bd_count(sizeof(bds) / sizeof(bds[0]))
marcozecchini 0:9fca2b23d0ba 67 , _read_size(0), _program_size(0), _erase_size(0), _size(0)
marcozecchini 0:9fca2b23d0ba 68 {
marcozecchini 0:9fca2b23d0ba 69 }
marcozecchini 0:9fca2b23d0ba 70
marcozecchini 0:9fca2b23d0ba 71 /** Lifetime of the memory block device
marcozecchini 0:9fca2b23d0ba 72 *
marcozecchini 0:9fca2b23d0ba 73 */
marcozecchini 0:9fca2b23d0ba 74 virtual ~ChainingBlockDevice() {}
marcozecchini 0:9fca2b23d0ba 75
marcozecchini 0:9fca2b23d0ba 76 /** Initialize a block device
marcozecchini 0:9fca2b23d0ba 77 *
marcozecchini 0:9fca2b23d0ba 78 * @return 0 on success or a negative error code on failure
marcozecchini 0:9fca2b23d0ba 79 */
marcozecchini 0:9fca2b23d0ba 80 virtual int init();
marcozecchini 0:9fca2b23d0ba 81
marcozecchini 0:9fca2b23d0ba 82 /** Deinitialize a block device
marcozecchini 0:9fca2b23d0ba 83 *
marcozecchini 0:9fca2b23d0ba 84 * @return 0 on success or a negative error code on failure
marcozecchini 0:9fca2b23d0ba 85 */
marcozecchini 0:9fca2b23d0ba 86 virtual int deinit();
marcozecchini 0:9fca2b23d0ba 87
marcozecchini 0:9fca2b23d0ba 88 /** Read blocks from a block device
marcozecchini 0:9fca2b23d0ba 89 *
marcozecchini 0:9fca2b23d0ba 90 * @param buffer Buffer to write blocks to
marcozecchini 0:9fca2b23d0ba 91 * @param addr Address of block to begin reading from
marcozecchini 0:9fca2b23d0ba 92 * @param size Size to read in bytes, must be a multiple of read block size
marcozecchini 0:9fca2b23d0ba 93 * @return 0 on success, negative error code on failure
marcozecchini 0:9fca2b23d0ba 94 */
marcozecchini 0:9fca2b23d0ba 95 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
marcozecchini 0:9fca2b23d0ba 96
marcozecchini 0:9fca2b23d0ba 97 /** Program blocks to a block device
marcozecchini 0:9fca2b23d0ba 98 *
marcozecchini 0:9fca2b23d0ba 99 * The blocks must have been erased prior to being programmed
marcozecchini 0:9fca2b23d0ba 100 *
marcozecchini 0:9fca2b23d0ba 101 * @param buffer Buffer of data to write to blocks
marcozecchini 0:9fca2b23d0ba 102 * @param addr Address of block to begin writing to
marcozecchini 0:9fca2b23d0ba 103 * @param size Size to write in bytes, must be a multiple of program block size
marcozecchini 0:9fca2b23d0ba 104 * @return 0 on success, negative error code on failure
marcozecchini 0:9fca2b23d0ba 105 */
marcozecchini 0:9fca2b23d0ba 106 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
marcozecchini 0:9fca2b23d0ba 107
marcozecchini 0:9fca2b23d0ba 108 /** Erase blocks on a block device
marcozecchini 0:9fca2b23d0ba 109 *
marcozecchini 0:9fca2b23d0ba 110 * The state of an erased block is undefined until it has been programmed
marcozecchini 0:9fca2b23d0ba 111 *
marcozecchini 0:9fca2b23d0ba 112 * @param addr Address of block to begin erasing
marcozecchini 0:9fca2b23d0ba 113 * @param size Size to erase in bytes, must be a multiple of erase block size
marcozecchini 0:9fca2b23d0ba 114 * @return 0 on success, negative error code on failure
marcozecchini 0:9fca2b23d0ba 115 */
marcozecchini 0:9fca2b23d0ba 116 virtual int erase(bd_addr_t addr, bd_size_t size);
marcozecchini 0:9fca2b23d0ba 117
marcozecchini 0:9fca2b23d0ba 118 /** Get the size of a readable block
marcozecchini 0:9fca2b23d0ba 119 *
marcozecchini 0:9fca2b23d0ba 120 * @return Size of a readable block in bytes
marcozecchini 0:9fca2b23d0ba 121 */
marcozecchini 0:9fca2b23d0ba 122 virtual bd_size_t get_read_size() const;
marcozecchini 0:9fca2b23d0ba 123
marcozecchini 0:9fca2b23d0ba 124 /** Get the size of a programable block
marcozecchini 0:9fca2b23d0ba 125 *
marcozecchini 0:9fca2b23d0ba 126 * @return Size of a programable block in bytes
marcozecchini 0:9fca2b23d0ba 127 * @note Must be a multiple of the read size
marcozecchini 0:9fca2b23d0ba 128 */
marcozecchini 0:9fca2b23d0ba 129 virtual bd_size_t get_program_size() const;
marcozecchini 0:9fca2b23d0ba 130
marcozecchini 0:9fca2b23d0ba 131 /** Get the size of a eraseable block
marcozecchini 0:9fca2b23d0ba 132 *
marcozecchini 0:9fca2b23d0ba 133 * @return Size of a eraseable block in bytes
marcozecchini 0:9fca2b23d0ba 134 * @note Must be a multiple of the program size
marcozecchini 0:9fca2b23d0ba 135 */
marcozecchini 0:9fca2b23d0ba 136 virtual bd_size_t get_erase_size() const;
marcozecchini 0:9fca2b23d0ba 137
marcozecchini 0:9fca2b23d0ba 138 /** Get the total size of the underlying device
marcozecchini 0:9fca2b23d0ba 139 *
marcozecchini 0:9fca2b23d0ba 140 * @return Size of the underlying device in bytes
marcozecchini 0:9fca2b23d0ba 141 */
marcozecchini 0:9fca2b23d0ba 142 virtual bd_size_t size() const;
marcozecchini 0:9fca2b23d0ba 143
marcozecchini 0:9fca2b23d0ba 144 protected:
marcozecchini 0:9fca2b23d0ba 145 BlockDevice **_bds;
marcozecchini 0:9fca2b23d0ba 146 size_t _bd_count;
marcozecchini 0:9fca2b23d0ba 147 bd_size_t _read_size;
marcozecchini 0:9fca2b23d0ba 148 bd_size_t _program_size;
marcozecchini 0:9fca2b23d0ba 149 bd_size_t _erase_size;
marcozecchini 0:9fca2b23d0ba 150 bd_size_t _size;
marcozecchini 0:9fca2b23d0ba 151 };
marcozecchini 0:9fca2b23d0ba 152
marcozecchini 0:9fca2b23d0ba 153
marcozecchini 0:9fca2b23d0ba 154 #endif