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_SLICING_BLOCK_DEVICE_H
marcozecchini 0:9fca2b23d0ba 23 #define MBED_SLICING_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 mapping to a slice of another block device
marcozecchini 0:9fca2b23d0ba 30 *
marcozecchini 0:9fca2b23d0ba 31 * @code
marcozecchini 0:9fca2b23d0ba 32 * #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 33 * #include "HeapBlockDevice.h"
marcozecchini 0:9fca2b23d0ba 34 * #include "SlicingBlockDevice.h"
marcozecchini 0:9fca2b23d0ba 35 *
marcozecchini 0:9fca2b23d0ba 36 * // Create a block device with 64 blocks of size 512
marcozecchini 0:9fca2b23d0ba 37 * HeapBlockDevice mem(64*512, 512);
marcozecchini 0:9fca2b23d0ba 38 *
marcozecchini 0:9fca2b23d0ba 39 * // Create a block device that maps to the first 32 blocks
marcozecchini 0:9fca2b23d0ba 40 * SlicingBlockDevice slice1(&mem, 0*512, 32*512);
marcozecchini 0:9fca2b23d0ba 41 *
marcozecchini 0:9fca2b23d0ba 42 * // Create a block device that maps to the last 32 blocks
marcozecchini 0:9fca2b23d0ba 43 * SlicingBlockDevice slice2(&mem, 32*512);
marcozecchini 0:9fca2b23d0ba 44 *
marcozecchini 0:9fca2b23d0ba 45 * // Create a block device that maps to the middle 32 blocks
marcozecchini 0:9fca2b23d0ba 46 * SlicingBlockDevice slice3(&mem, 16*512, -16*512);
marcozecchini 0:9fca2b23d0ba 47 * @endcode
marcozecchini 0:9fca2b23d0ba 48 */
marcozecchini 0:9fca2b23d0ba 49 class SlicingBlockDevice : public BlockDevice
marcozecchini 0:9fca2b23d0ba 50 {
marcozecchini 0:9fca2b23d0ba 51 public:
marcozecchini 0:9fca2b23d0ba 52 /** Lifetime of the memory block device
marcozecchini 0:9fca2b23d0ba 53 *
marcozecchini 0:9fca2b23d0ba 54 * @param bd Block device to back the SlicingBlockDevice
marcozecchini 0:9fca2b23d0ba 55 * @param start Start block address to map to block 0, negative addresses
marcozecchini 0:9fca2b23d0ba 56 * are calculated from the end of the underlying block device
marcozecchini 0:9fca2b23d0ba 57 * @param end End block address to mark the end of the block device,
marcozecchini 0:9fca2b23d0ba 58 * this block is not mapped, negative addresses are
marcozecchini 0:9fca2b23d0ba 59 * calculated from the end of the underlying block device.
marcozecchini 0:9fca2b23d0ba 60 * The default stops at end of the underlying block device.
marcozecchini 0:9fca2b23d0ba 61 */
marcozecchini 0:9fca2b23d0ba 62 SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t end = 0);
marcozecchini 0:9fca2b23d0ba 63
marcozecchini 0:9fca2b23d0ba 64 /** Lifetime of a block device
marcozecchini 0:9fca2b23d0ba 65 */
marcozecchini 0:9fca2b23d0ba 66 virtual ~SlicingBlockDevice() {};
marcozecchini 0:9fca2b23d0ba 67
marcozecchini 0:9fca2b23d0ba 68 /** Initialize a block device
marcozecchini 0:9fca2b23d0ba 69 *
marcozecchini 0:9fca2b23d0ba 70 * @return 0 on success or a negative error code on failure
marcozecchini 0:9fca2b23d0ba 71 */
marcozecchini 0:9fca2b23d0ba 72 virtual int init();
marcozecchini 0:9fca2b23d0ba 73
marcozecchini 0:9fca2b23d0ba 74 /** Deinitialize a block device
marcozecchini 0:9fca2b23d0ba 75 *
marcozecchini 0:9fca2b23d0ba 76 * @return 0 on success or a negative error code on failure
marcozecchini 0:9fca2b23d0ba 77 */
marcozecchini 0:9fca2b23d0ba 78 virtual int deinit();
marcozecchini 0:9fca2b23d0ba 79
marcozecchini 0:9fca2b23d0ba 80 /** Read blocks from a block device
marcozecchini 0:9fca2b23d0ba 81 *
marcozecchini 0:9fca2b23d0ba 82 * @param buffer Buffer to read blocks into
marcozecchini 0:9fca2b23d0ba 83 * @param addr Address of block to begin reading from
marcozecchini 0:9fca2b23d0ba 84 * @param size Size to read in bytes, must be a multiple of read block size
marcozecchini 0:9fca2b23d0ba 85 * @return 0 on success, negative error code on failure
marcozecchini 0:9fca2b23d0ba 86 */
marcozecchini 0:9fca2b23d0ba 87 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
marcozecchini 0:9fca2b23d0ba 88
marcozecchini 0:9fca2b23d0ba 89 /** Program blocks to a block device
marcozecchini 0:9fca2b23d0ba 90 *
marcozecchini 0:9fca2b23d0ba 91 * The blocks must have been erased prior to being programmed
marcozecchini 0:9fca2b23d0ba 92 *
marcozecchini 0:9fca2b23d0ba 93 * @param buffer Buffer of data to write to blocks
marcozecchini 0:9fca2b23d0ba 94 * @param addr Address of block to begin writing to
marcozecchini 0:9fca2b23d0ba 95 * @param size Size to write in bytes, must be a multiple of program block size
marcozecchini 0:9fca2b23d0ba 96 * @return 0 on success, negative error code on failure
marcozecchini 0:9fca2b23d0ba 97 */
marcozecchini 0:9fca2b23d0ba 98 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
marcozecchini 0:9fca2b23d0ba 99
marcozecchini 0:9fca2b23d0ba 100 /** Erase blocks on a block device
marcozecchini 0:9fca2b23d0ba 101 *
marcozecchini 0:9fca2b23d0ba 102 * The state of an erased block is undefined until it has been programmed
marcozecchini 0:9fca2b23d0ba 103 *
marcozecchini 0:9fca2b23d0ba 104 * @param addr Address of block to begin erasing
marcozecchini 0:9fca2b23d0ba 105 * @param size Size to erase in bytes, must be a multiple of erase block size
marcozecchini 0:9fca2b23d0ba 106 * @return 0 on success, negative error code on failure
marcozecchini 0:9fca2b23d0ba 107 */
marcozecchini 0:9fca2b23d0ba 108 virtual int erase(bd_addr_t addr, bd_size_t size);
marcozecchini 0:9fca2b23d0ba 109
marcozecchini 0:9fca2b23d0ba 110 /** Get the size of a readable block
marcozecchini 0:9fca2b23d0ba 111 *
marcozecchini 0:9fca2b23d0ba 112 * @return Size of a readable block in bytes
marcozecchini 0:9fca2b23d0ba 113 */
marcozecchini 0:9fca2b23d0ba 114 virtual bd_size_t get_read_size() const;
marcozecchini 0:9fca2b23d0ba 115
marcozecchini 0:9fca2b23d0ba 116 /** Get the size of a programable block
marcozecchini 0:9fca2b23d0ba 117 *
marcozecchini 0:9fca2b23d0ba 118 * @return Size of a programable block in bytes
marcozecchini 0:9fca2b23d0ba 119 * @note Must be a multiple of the read size
marcozecchini 0:9fca2b23d0ba 120 */
marcozecchini 0:9fca2b23d0ba 121 virtual bd_size_t get_program_size() const;
marcozecchini 0:9fca2b23d0ba 122
marcozecchini 0:9fca2b23d0ba 123 /** Get the size of a eraseable block
marcozecchini 0:9fca2b23d0ba 124 *
marcozecchini 0:9fca2b23d0ba 125 * @return Size of a eraseable block in bytes
marcozecchini 0:9fca2b23d0ba 126 * @note Must be a multiple of the program size
marcozecchini 0:9fca2b23d0ba 127 */
marcozecchini 0:9fca2b23d0ba 128 virtual bd_size_t get_erase_size() const;
marcozecchini 0:9fca2b23d0ba 129
marcozecchini 0:9fca2b23d0ba 130 /** Get the total size of the underlying device
marcozecchini 0:9fca2b23d0ba 131 *
marcozecchini 0:9fca2b23d0ba 132 * @return Size of the underlying device in bytes
marcozecchini 0:9fca2b23d0ba 133 */
marcozecchini 0:9fca2b23d0ba 134 virtual bd_size_t size() const;
marcozecchini 0:9fca2b23d0ba 135
marcozecchini 0:9fca2b23d0ba 136 protected:
marcozecchini 0:9fca2b23d0ba 137 BlockDevice *_bd;
marcozecchini 0:9fca2b23d0ba 138 bool _start_from_end;
marcozecchini 0:9fca2b23d0ba 139 bd_size_t _start;
marcozecchini 0:9fca2b23d0ba 140 bool _stop_from_end;
marcozecchini 0:9fca2b23d0ba 141 bd_size_t _stop;
marcozecchini 0:9fca2b23d0ba 142 };
marcozecchini 0:9fca2b23d0ba 143
marcozecchini 0:9fca2b23d0ba 144
marcozecchini 0:9fca2b23d0ba 145 #endif