Stores data on the flash memory of stm32f4xx
Dependents: DISCO-F429ZI_LCDTS_demo_richard
Fork of storage_on_flash by
SOFBlock.cpp@1:801ddc4634a5, 2018-03-16 (annotated)
- Committer:
- pierrebizouard
- Date:
- Fri Mar 16 15:50:04 2018 +0000
- Revision:
- 1:801ddc4634a5
- Parent:
- 0:4cb438b58dc2
added support for 429ZI up to bank 11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hendreh | 0:4cb438b58dc2 | 1 | /* |
hendreh | 0:4cb438b58dc2 | 2 | * @file SOFBlock.cpp |
hendreh | 0:4cb438b58dc2 | 3 | * @author hillkim7@gmail.com |
hendreh | 0:4cb438b58dc2 | 4 | * |
hendreh | 0:4cb438b58dc2 | 5 | * @brief Simple storage implementation on internal MCU flash memory. |
hendreh | 0:4cb438b58dc2 | 6 | * |
hendreh | 0:4cb438b58dc2 | 7 | */ |
hendreh | 0:4cb438b58dc2 | 8 | |
hendreh | 0:4cb438b58dc2 | 9 | #include "SOFBlock.h" |
hendreh | 0:4cb438b58dc2 | 10 | #include <string.h> |
hendreh | 0:4cb438b58dc2 | 11 | #include <stdlib.h> |
hendreh | 0:4cb438b58dc2 | 12 | |
hendreh | 0:4cb438b58dc2 | 13 | |
hendreh | 0:4cb438b58dc2 | 14 | SOFBlock::SOFBlock() |
hendreh | 0:4cb438b58dc2 | 15 | : hblock_(NULL) |
hendreh | 0:4cb438b58dc2 | 16 | { |
hendreh | 0:4cb438b58dc2 | 17 | } |
hendreh | 0:4cb438b58dc2 | 18 | |
hendreh | 0:4cb438b58dc2 | 19 | SOFBlock::~SOFBlock() |
hendreh | 0:4cb438b58dc2 | 20 | { |
hendreh | 0:4cb438b58dc2 | 21 | close(); |
hendreh | 0:4cb438b58dc2 | 22 | } |
hendreh | 0:4cb438b58dc2 | 23 | |
hendreh | 0:4cb438b58dc2 | 24 | void SOFBlock::close() |
hendreh | 0:4cb438b58dc2 | 25 | { |
hendreh | 0:4cb438b58dc2 | 26 | if (hblock_ != NULL) { |
hendreh | 0:4cb438b58dc2 | 27 | SOF_block_close(hblock_); |
hendreh | 0:4cb438b58dc2 | 28 | hblock_ = NULL; |
hendreh | 0:4cb438b58dc2 | 29 | } |
hendreh | 0:4cb438b58dc2 | 30 | } |
hendreh | 0:4cb438b58dc2 | 31 | |
hendreh | 0:4cb438b58dc2 | 32 | bool SOFBlock::format( uint8_t sector_index ) |
hendreh | 0:4cb438b58dc2 | 33 | { |
hendreh | 0:4cb438b58dc2 | 34 | return SOF_block_format(sector_index); |
hendreh | 0:4cb438b58dc2 | 35 | } |
hendreh | 0:4cb438b58dc2 | 36 | |
hendreh | 0:4cb438b58dc2 | 37 | bool SOFBlock::get_stat( uint8_t sector_index, SOF_Statics_t &statics ) |
hendreh | 0:4cb438b58dc2 | 38 | { |
hendreh | 0:4cb438b58dc2 | 39 | return SOF_block_get_statics(sector_index, &statics) == kSOF_ErrNone; |
hendreh | 0:4cb438b58dc2 | 40 | } |
hendreh | 0:4cb438b58dc2 | 41 | |
hendreh | 0:4cb438b58dc2 | 42 | SOFWriter::SOFWriter() |
hendreh | 0:4cb438b58dc2 | 43 | { |
hendreh | 0:4cb438b58dc2 | 44 | |
hendreh | 0:4cb438b58dc2 | 45 | } |
hendreh | 0:4cb438b58dc2 | 46 | |
hendreh | 0:4cb438b58dc2 | 47 | SOFWriter::~SOFWriter() |
hendreh | 0:4cb438b58dc2 | 48 | { |
hendreh | 0:4cb438b58dc2 | 49 | |
hendreh | 0:4cb438b58dc2 | 50 | } |
hendreh | 0:4cb438b58dc2 | 51 | |
hendreh | 0:4cb438b58dc2 | 52 | SOF_Error_t SOFWriter::open( uint8_t sector_index ) |
hendreh | 0:4cb438b58dc2 | 53 | { |
hendreh | 0:4cb438b58dc2 | 54 | if (is_open()) { |
hendreh | 0:4cb438b58dc2 | 55 | return kSOF_ErrBusyBlock; |
hendreh | 0:4cb438b58dc2 | 56 | } |
hendreh | 0:4cb438b58dc2 | 57 | |
hendreh | 0:4cb438b58dc2 | 58 | SOF_Error_t err; |
hendreh | 0:4cb438b58dc2 | 59 | |
hendreh | 0:4cb438b58dc2 | 60 | hblock_ = SOF_block_create_storage(sector_index, &err); |
hendreh | 0:4cb438b58dc2 | 61 | |
hendreh | 0:4cb438b58dc2 | 62 | return err; |
hendreh | 0:4cb438b58dc2 | 63 | } |
hendreh | 0:4cb438b58dc2 | 64 | |
hendreh | 0:4cb438b58dc2 | 65 | bool SOFWriter::write_byte_data( uint8_t c ) |
hendreh | 0:4cb438b58dc2 | 66 | { |
hendreh | 0:4cb438b58dc2 | 67 | if (!is_open()) { |
hendreh | 0:4cb438b58dc2 | 68 | return false; |
hendreh | 0:4cb438b58dc2 | 69 | } |
hendreh | 0:4cb438b58dc2 | 70 | |
hendreh | 0:4cb438b58dc2 | 71 | return SOF_block_putc(hblock_, c); |
hendreh | 0:4cb438b58dc2 | 72 | } |
hendreh | 0:4cb438b58dc2 | 73 | |
hendreh | 0:4cb438b58dc2 | 74 | size_t SOFWriter::write_data( const uint8_t *p, size_t p_size ) |
hendreh | 0:4cb438b58dc2 | 75 | { |
hendreh | 0:4cb438b58dc2 | 76 | if (!is_open()) { |
hendreh | 0:4cb438b58dc2 | 77 | return false; |
hendreh | 0:4cb438b58dc2 | 78 | } |
hendreh | 0:4cb438b58dc2 | 79 | |
hendreh | 0:4cb438b58dc2 | 80 | return SOF_block_write(hblock_, p, p_size); |
hendreh | 0:4cb438b58dc2 | 81 | } |
hendreh | 0:4cb438b58dc2 | 82 | |
hendreh | 0:4cb438b58dc2 | 83 | size_t SOFWriter::get_free_size() |
hendreh | 0:4cb438b58dc2 | 84 | { |
hendreh | 0:4cb438b58dc2 | 85 | if (!is_open()) { |
hendreh | 0:4cb438b58dc2 | 86 | return 0; |
hendreh | 0:4cb438b58dc2 | 87 | } |
hendreh | 0:4cb438b58dc2 | 88 | |
hendreh | 0:4cb438b58dc2 | 89 | return SOF_block_get_free_size(hblock_); |
hendreh | 0:4cb438b58dc2 | 90 | } |
hendreh | 0:4cb438b58dc2 | 91 | |
hendreh | 0:4cb438b58dc2 | 92 | |
hendreh | 0:4cb438b58dc2 | 93 | SOFReader::SOFReader() |
hendreh | 0:4cb438b58dc2 | 94 | { |
hendreh | 0:4cb438b58dc2 | 95 | |
hendreh | 0:4cb438b58dc2 | 96 | } |
hendreh | 0:4cb438b58dc2 | 97 | |
hendreh | 0:4cb438b58dc2 | 98 | SOFReader::~SOFReader() |
hendreh | 0:4cb438b58dc2 | 99 | { |
hendreh | 0:4cb438b58dc2 | 100 | |
hendreh | 0:4cb438b58dc2 | 101 | } |
hendreh | 0:4cb438b58dc2 | 102 | |
hendreh | 0:4cb438b58dc2 | 103 | SOF_Error_t SOFReader::open( uint8_t sector_index ) |
hendreh | 0:4cb438b58dc2 | 104 | { |
hendreh | 0:4cb438b58dc2 | 105 | if (is_open()) { |
hendreh | 0:4cb438b58dc2 | 106 | return kSOF_ErrBusyBlock; |
hendreh | 0:4cb438b58dc2 | 107 | } |
hendreh | 0:4cb438b58dc2 | 108 | |
hendreh | 0:4cb438b58dc2 | 109 | SOF_Error_t err; |
hendreh | 0:4cb438b58dc2 | 110 | |
hendreh | 0:4cb438b58dc2 | 111 | hblock_ = SOF_block_open_storage(sector_index, &err); |
hendreh | 0:4cb438b58dc2 | 112 | |
hendreh | 0:4cb438b58dc2 | 113 | return err; |
hendreh | 0:4cb438b58dc2 | 114 | } |
hendreh | 0:4cb438b58dc2 | 115 | |
hendreh | 0:4cb438b58dc2 | 116 | uint8_t * SOFReader::get_physical_data_addr() |
hendreh | 0:4cb438b58dc2 | 117 | { |
hendreh | 0:4cb438b58dc2 | 118 | if (!is_open()) { |
hendreh | 0:4cb438b58dc2 | 119 | return NULL; |
hendreh | 0:4cb438b58dc2 | 120 | } |
hendreh | 0:4cb438b58dc2 | 121 | |
hendreh | 0:4cb438b58dc2 | 122 | return SOF_block_base_addr(hblock_); |
hendreh | 0:4cb438b58dc2 | 123 | } |
hendreh | 0:4cb438b58dc2 | 124 | |
hendreh | 0:4cb438b58dc2 | 125 | size_t SOFReader::get_data_size() |
hendreh | 0:4cb438b58dc2 | 126 | { |
hendreh | 0:4cb438b58dc2 | 127 | if (!is_open()) { |
hendreh | 0:4cb438b58dc2 | 128 | return 0; |
hendreh | 0:4cb438b58dc2 | 129 | } |
hendreh | 0:4cb438b58dc2 | 130 | |
hendreh | 0:4cb438b58dc2 | 131 | return SOF_block_storage_size(hblock_); |
hendreh | 0:4cb438b58dc2 | 132 | } |
hendreh | 0:4cb438b58dc2 | 133 | |
hendreh | 0:4cb438b58dc2 | 134 | bool SOFReader::read_byte_data( uint8_t *c ) |
hendreh | 0:4cb438b58dc2 | 135 | { |
hendreh | 0:4cb438b58dc2 | 136 | if (!is_open()) { |
hendreh | 0:4cb438b58dc2 | 137 | return false; |
hendreh | 0:4cb438b58dc2 | 138 | } |
hendreh | 0:4cb438b58dc2 | 139 | |
hendreh | 0:4cb438b58dc2 | 140 | return SOF_block_getc(hblock_, c); |
hendreh | 0:4cb438b58dc2 | 141 | } |
hendreh | 0:4cb438b58dc2 | 142 | |
hendreh | 0:4cb438b58dc2 | 143 | size_t SOFReader::read_data( uint8_t *p, size_t p_size ) |
hendreh | 0:4cb438b58dc2 | 144 | { |
hendreh | 0:4cb438b58dc2 | 145 | if (!is_open()) { |
hendreh | 0:4cb438b58dc2 | 146 | return 0; |
hendreh | 0:4cb438b58dc2 | 147 | } |
hendreh | 0:4cb438b58dc2 | 148 | |
hendreh | 0:4cb438b58dc2 | 149 | return SOF_block_read(hblock_, p, p_size); |
hendreh | 0:4cb438b58dc2 | 150 | } |
hendreh | 0:4cb438b58dc2 | 151 | |
hendreh | 0:4cb438b58dc2 | 152 | |
hendreh | 0:4cb438b58dc2 | 153 |