Filesystem library designed for flash devices
Dependents: flash-fs-example Dragonfly_Filesystem_Example STM32F407VET6_SPIFlash Dragonfly_Filesystem_Example_mbed_5
spiffs_cache.c@3:9adcf49bb77d, 2020-06-26 (annotated)
- Committer:
- Leon Lindenfelser
- Date:
- Fri Jun 26 14:31:42 2020 -0500
- Revision:
- 3:9adcf49bb77d
- Parent:
- 0:bb4e812f7c97
Update to latest from Multitech git repo
1. Add timeout when waiting for WIP
2. Check 'write in progress bit' after writing the status register
3. Add ability to write the status register
4. Read identification function actually reads identification
5. Hardcode memory size as parameter in constructor
6. Move wakeup() call after CS, WP, and HOLD setup so it wakes the flash if it was sleeping
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:bb4e812f7c97 | 1 | /* |
mfiore | 0:bb4e812f7c97 | 2 | * spiffs_cache.c |
mfiore | 0:bb4e812f7c97 | 3 | * |
mfiore | 0:bb4e812f7c97 | 4 | * Created on: Jun 23, 2013 |
mfiore | 0:bb4e812f7c97 | 5 | * Author: petera |
mfiore | 0:bb4e812f7c97 | 6 | */ |
mfiore | 0:bb4e812f7c97 | 7 | |
mfiore | 0:bb4e812f7c97 | 8 | #include "spiffs.h" |
mfiore | 0:bb4e812f7c97 | 9 | #include "spiffs_nucleus.h" |
mfiore | 0:bb4e812f7c97 | 10 | |
mfiore | 0:bb4e812f7c97 | 11 | #if SPIFFS_CACHE |
mfiore | 0:bb4e812f7c97 | 12 | |
mfiore | 0:bb4e812f7c97 | 13 | // returns cached page for give page index, or null if no such cached page |
mfiore | 0:bb4e812f7c97 | 14 | static spiffs_cache_page *spiffs_cache_page_get(spiffs *fs, spiffs_page_ix pix) { |
mfiore | 0:bb4e812f7c97 | 15 | spiffs_cache *cache = spiffs_get_cache(fs); |
mfiore | 0:bb4e812f7c97 | 16 | if ((cache->cpage_use_map & cache->cpage_use_mask) == 0) return 0; |
mfiore | 0:bb4e812f7c97 | 17 | int i; |
mfiore | 0:bb4e812f7c97 | 18 | for (i = 0; i < cache->cpage_count; i++) { |
mfiore | 0:bb4e812f7c97 | 19 | spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i); |
mfiore | 0:bb4e812f7c97 | 20 | if ((cache->cpage_use_map & (1<<i)) && |
mfiore | 0:bb4e812f7c97 | 21 | (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 && |
mfiore | 0:bb4e812f7c97 | 22 | cp->pix == pix ) { |
mfiore | 0:bb4e812f7c97 | 23 | SPIFFS_CACHE_DBG("CACHE_GET: have cache page %i for %04x\n", i, pix); |
mfiore | 0:bb4e812f7c97 | 24 | cp->last_access = cache->last_access; |
mfiore | 0:bb4e812f7c97 | 25 | return cp; |
mfiore | 0:bb4e812f7c97 | 26 | } |
mfiore | 0:bb4e812f7c97 | 27 | } |
mfiore | 0:bb4e812f7c97 | 28 | //SPIFFS_CACHE_DBG("CACHE_GET: no cache for %04x\n", pix); |
mfiore | 0:bb4e812f7c97 | 29 | return 0; |
mfiore | 0:bb4e812f7c97 | 30 | } |
mfiore | 0:bb4e812f7c97 | 31 | |
mfiore | 0:bb4e812f7c97 | 32 | // frees cached page |
mfiore | 0:bb4e812f7c97 | 33 | static s32_t spiffs_cache_page_free(spiffs *fs, int ix, u8_t write_back) { |
mfiore | 0:bb4e812f7c97 | 34 | s32_t res = SPIFFS_OK; |
mfiore | 0:bb4e812f7c97 | 35 | spiffs_cache *cache = spiffs_get_cache(fs); |
mfiore | 0:bb4e812f7c97 | 36 | spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, ix); |
mfiore | 0:bb4e812f7c97 | 37 | if (cache->cpage_use_map & (1<<ix)) { |
mfiore | 0:bb4e812f7c97 | 38 | if (write_back && |
mfiore | 0:bb4e812f7c97 | 39 | (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 && |
mfiore | 0:bb4e812f7c97 | 40 | (cp->flags & SPIFFS_CACHE_FLAG_DIRTY)) { |
mfiore | 0:bb4e812f7c97 | 41 | u8_t *mem = spiffs_get_cache_page(fs, cache, ix); |
mfiore | 0:bb4e812f7c97 | 42 | res = fs->cfg.hal_write_f(SPIFFS_PAGE_TO_PADDR(fs, cp->pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), mem); |
mfiore | 0:bb4e812f7c97 | 43 | } |
mfiore | 0:bb4e812f7c97 | 44 | |
mfiore | 0:bb4e812f7c97 | 45 | cp->flags = 0; |
mfiore | 0:bb4e812f7c97 | 46 | cache->cpage_use_map &= ~(1 << ix); |
mfiore | 0:bb4e812f7c97 | 47 | |
mfiore | 0:bb4e812f7c97 | 48 | if (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) { |
mfiore | 0:bb4e812f7c97 | 49 | SPIFFS_CACHE_DBG("CACHE_FREE: free cache page %i objid %04x\n", ix, cp->obj_id); |
mfiore | 0:bb4e812f7c97 | 50 | } else { |
mfiore | 0:bb4e812f7c97 | 51 | SPIFFS_CACHE_DBG("CACHE_FREE: free cache page %i pix %04x\n", ix, cp->pix); |
mfiore | 0:bb4e812f7c97 | 52 | } |
mfiore | 0:bb4e812f7c97 | 53 | } |
mfiore | 0:bb4e812f7c97 | 54 | |
mfiore | 0:bb4e812f7c97 | 55 | return res; |
mfiore | 0:bb4e812f7c97 | 56 | } |
mfiore | 0:bb4e812f7c97 | 57 | |
mfiore | 0:bb4e812f7c97 | 58 | // removes the oldest accessed cached page |
mfiore | 0:bb4e812f7c97 | 59 | static s32_t spiffs_cache_page_remove_oldest(spiffs *fs, u8_t flag_mask, u8_t flags) { |
mfiore | 0:bb4e812f7c97 | 60 | s32_t res = SPIFFS_OK; |
mfiore | 0:bb4e812f7c97 | 61 | spiffs_cache *cache = spiffs_get_cache(fs); |
mfiore | 0:bb4e812f7c97 | 62 | |
mfiore | 0:bb4e812f7c97 | 63 | if ((cache->cpage_use_map & cache->cpage_use_mask) != cache->cpage_use_mask) { |
mfiore | 0:bb4e812f7c97 | 64 | // at least one free cpage |
mfiore | 0:bb4e812f7c97 | 65 | return SPIFFS_OK; |
mfiore | 0:bb4e812f7c97 | 66 | } |
mfiore | 0:bb4e812f7c97 | 67 | |
mfiore | 0:bb4e812f7c97 | 68 | // all busy, scan thru all to find the cpage which has oldest access |
mfiore | 0:bb4e812f7c97 | 69 | int i; |
mfiore | 0:bb4e812f7c97 | 70 | int cand_ix = -1; |
mfiore | 0:bb4e812f7c97 | 71 | u32_t oldest_val = 0; |
mfiore | 0:bb4e812f7c97 | 72 | for (i = 0; i < cache->cpage_count; i++) { |
mfiore | 0:bb4e812f7c97 | 73 | spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i); |
mfiore | 0:bb4e812f7c97 | 74 | if ((cache->last_access - cp->last_access) > oldest_val && |
mfiore | 0:bb4e812f7c97 | 75 | (cp->flags & flag_mask) == flags) { |
mfiore | 0:bb4e812f7c97 | 76 | oldest_val = cache->last_access - cp->last_access; |
mfiore | 0:bb4e812f7c97 | 77 | cand_ix = i; |
mfiore | 0:bb4e812f7c97 | 78 | } |
mfiore | 0:bb4e812f7c97 | 79 | } |
mfiore | 0:bb4e812f7c97 | 80 | |
mfiore | 0:bb4e812f7c97 | 81 | if (cand_ix >= 0) { |
mfiore | 0:bb4e812f7c97 | 82 | res = spiffs_cache_page_free(fs, cand_ix, 1); |
mfiore | 0:bb4e812f7c97 | 83 | } |
mfiore | 0:bb4e812f7c97 | 84 | |
mfiore | 0:bb4e812f7c97 | 85 | return res; |
mfiore | 0:bb4e812f7c97 | 86 | } |
mfiore | 0:bb4e812f7c97 | 87 | |
mfiore | 0:bb4e812f7c97 | 88 | // allocates a new cached page and returns it, or null if all cache pages are busy |
mfiore | 0:bb4e812f7c97 | 89 | static spiffs_cache_page *spiffs_cache_page_allocate(spiffs *fs) { |
mfiore | 0:bb4e812f7c97 | 90 | spiffs_cache *cache = spiffs_get_cache(fs); |
mfiore | 0:bb4e812f7c97 | 91 | if (cache->cpage_use_map == 0xffffffff) { |
mfiore | 0:bb4e812f7c97 | 92 | // out of cache memory |
mfiore | 0:bb4e812f7c97 | 93 | return 0; |
mfiore | 0:bb4e812f7c97 | 94 | } |
mfiore | 0:bb4e812f7c97 | 95 | int i; |
mfiore | 0:bb4e812f7c97 | 96 | for (i = 0; i < cache->cpage_count; i++) { |
mfiore | 0:bb4e812f7c97 | 97 | if ((cache->cpage_use_map & (1<<i)) == 0) { |
mfiore | 0:bb4e812f7c97 | 98 | spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i); |
mfiore | 0:bb4e812f7c97 | 99 | cache->cpage_use_map |= (1<<i); |
mfiore | 0:bb4e812f7c97 | 100 | cp->last_access = cache->last_access; |
mfiore | 0:bb4e812f7c97 | 101 | SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page %i\n", i); |
mfiore | 0:bb4e812f7c97 | 102 | return cp; |
mfiore | 0:bb4e812f7c97 | 103 | } |
mfiore | 0:bb4e812f7c97 | 104 | } |
mfiore | 0:bb4e812f7c97 | 105 | // out of cache entries |
mfiore | 0:bb4e812f7c97 | 106 | return 0; |
mfiore | 0:bb4e812f7c97 | 107 | } |
mfiore | 0:bb4e812f7c97 | 108 | |
mfiore | 0:bb4e812f7c97 | 109 | // drops the cache page for give page index |
mfiore | 0:bb4e812f7c97 | 110 | void spiffs_cache_drop_page(spiffs *fs, spiffs_page_ix pix) { |
mfiore | 0:bb4e812f7c97 | 111 | spiffs_cache_page *cp = spiffs_cache_page_get(fs, pix); |
mfiore | 0:bb4e812f7c97 | 112 | if (cp) { |
mfiore | 0:bb4e812f7c97 | 113 | spiffs_cache_page_free(fs, cp->ix, 0); |
mfiore | 0:bb4e812f7c97 | 114 | } |
mfiore | 0:bb4e812f7c97 | 115 | } |
mfiore | 0:bb4e812f7c97 | 116 | |
mfiore | 0:bb4e812f7c97 | 117 | // ------------------------------ |
mfiore | 0:bb4e812f7c97 | 118 | |
mfiore | 0:bb4e812f7c97 | 119 | // reads from spi flash or the cache |
mfiore | 0:bb4e812f7c97 | 120 | s32_t spiffs_phys_rd( |
mfiore | 0:bb4e812f7c97 | 121 | spiffs *fs, |
mfiore | 0:bb4e812f7c97 | 122 | u8_t op, |
mfiore | 0:bb4e812f7c97 | 123 | spiffs_file fh, |
mfiore | 0:bb4e812f7c97 | 124 | u32_t addr, |
mfiore | 0:bb4e812f7c97 | 125 | u32_t len, |
mfiore | 0:bb4e812f7c97 | 126 | u8_t *dst) { |
mfiore | 0:bb4e812f7c97 | 127 | s32_t res = SPIFFS_OK; |
mfiore | 0:bb4e812f7c97 | 128 | spiffs_cache *cache = spiffs_get_cache(fs); |
mfiore | 0:bb4e812f7c97 | 129 | spiffs_cache_page *cp = spiffs_cache_page_get(fs, SPIFFS_PADDR_TO_PAGE(fs, addr)); |
mfiore | 0:bb4e812f7c97 | 130 | cache->last_access++; |
mfiore | 0:bb4e812f7c97 | 131 | if (cp) { |
mfiore | 0:bb4e812f7c97 | 132 | #if SPIFFS_CACHE_STATS |
mfiore | 0:bb4e812f7c97 | 133 | fs->cache_hits++; |
mfiore | 0:bb4e812f7c97 | 134 | #endif |
mfiore | 0:bb4e812f7c97 | 135 | cp->last_access = cache->last_access; |
mfiore | 0:bb4e812f7c97 | 136 | } else { |
mfiore | 0:bb4e812f7c97 | 137 | if ((op & SPIFFS_OP_TYPE_MASK) == SPIFFS_OP_T_OBJ_LU2) { |
mfiore | 0:bb4e812f7c97 | 138 | // for second layer lookup functions, we do not cache in order to prevent shredding |
mfiore | 0:bb4e812f7c97 | 139 | return fs->cfg.hal_read_f( |
mfiore | 0:bb4e812f7c97 | 140 | addr , |
mfiore | 0:bb4e812f7c97 | 141 | len, |
mfiore | 0:bb4e812f7c97 | 142 | dst); |
mfiore | 0:bb4e812f7c97 | 143 | } |
mfiore | 0:bb4e812f7c97 | 144 | #if SPIFFS_CACHE_STATS |
mfiore | 0:bb4e812f7c97 | 145 | fs->cache_misses++; |
mfiore | 0:bb4e812f7c97 | 146 | #endif |
mfiore | 0:bb4e812f7c97 | 147 | res = spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0); |
mfiore | 0:bb4e812f7c97 | 148 | cp = spiffs_cache_page_allocate(fs); |
mfiore | 0:bb4e812f7c97 | 149 | if (cp) { |
mfiore | 0:bb4e812f7c97 | 150 | cp->flags = SPIFFS_CACHE_FLAG_WRTHRU; |
mfiore | 0:bb4e812f7c97 | 151 | cp->pix = SPIFFS_PADDR_TO_PAGE(fs, addr); |
mfiore | 0:bb4e812f7c97 | 152 | } |
mfiore | 0:bb4e812f7c97 | 153 | |
mfiore | 0:bb4e812f7c97 | 154 | s32_t res2 = fs->cfg.hal_read_f( |
mfiore | 0:bb4e812f7c97 | 155 | addr - SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr), |
mfiore | 0:bb4e812f7c97 | 156 | SPIFFS_CFG_LOG_PAGE_SZ(fs), |
mfiore | 0:bb4e812f7c97 | 157 | spiffs_get_cache_page(fs, cache, cp->ix)); |
mfiore | 0:bb4e812f7c97 | 158 | if (res2 != SPIFFS_OK) { |
mfiore | 0:bb4e812f7c97 | 159 | res = res2; |
mfiore | 0:bb4e812f7c97 | 160 | } |
mfiore | 0:bb4e812f7c97 | 161 | } |
mfiore | 0:bb4e812f7c97 | 162 | u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix); |
mfiore | 0:bb4e812f7c97 | 163 | memcpy(dst, &mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], len); |
mfiore | 0:bb4e812f7c97 | 164 | return res; |
mfiore | 0:bb4e812f7c97 | 165 | } |
mfiore | 0:bb4e812f7c97 | 166 | |
mfiore | 0:bb4e812f7c97 | 167 | // writes to spi flash and/or the cache |
mfiore | 0:bb4e812f7c97 | 168 | s32_t spiffs_phys_wr( |
mfiore | 0:bb4e812f7c97 | 169 | spiffs *fs, |
mfiore | 0:bb4e812f7c97 | 170 | u8_t op, |
mfiore | 0:bb4e812f7c97 | 171 | spiffs_file fh, |
mfiore | 0:bb4e812f7c97 | 172 | u32_t addr, |
mfiore | 0:bb4e812f7c97 | 173 | u32_t len, |
mfiore | 0:bb4e812f7c97 | 174 | u8_t *src) { |
mfiore | 0:bb4e812f7c97 | 175 | spiffs_page_ix pix = SPIFFS_PADDR_TO_PAGE(fs, addr); |
mfiore | 0:bb4e812f7c97 | 176 | spiffs_cache *cache = spiffs_get_cache(fs); |
mfiore | 0:bb4e812f7c97 | 177 | spiffs_cache_page *cp = spiffs_cache_page_get(fs, pix); |
mfiore | 0:bb4e812f7c97 | 178 | |
mfiore | 0:bb4e812f7c97 | 179 | if (cp && (op & SPIFFS_OP_COM_MASK) != SPIFFS_OP_C_WRTHRU) { |
mfiore | 0:bb4e812f7c97 | 180 | // have a cache page |
mfiore | 0:bb4e812f7c97 | 181 | // copy in data to cache page |
mfiore | 0:bb4e812f7c97 | 182 | |
mfiore | 0:bb4e812f7c97 | 183 | if ((op & SPIFFS_OP_COM_MASK) == SPIFFS_OP_C_DELE && |
mfiore | 0:bb4e812f7c97 | 184 | (op & SPIFFS_OP_TYPE_MASK) != SPIFFS_OP_T_OBJ_LU) { |
mfiore | 0:bb4e812f7c97 | 185 | // page is being deleted, wipe from cache - unless it is a lookup page |
mfiore | 0:bb4e812f7c97 | 186 | spiffs_cache_page_free(fs, cp->ix, 0); |
mfiore | 0:bb4e812f7c97 | 187 | return fs->cfg.hal_write_f(addr, len, src); |
mfiore | 0:bb4e812f7c97 | 188 | } |
mfiore | 0:bb4e812f7c97 | 189 | |
mfiore | 0:bb4e812f7c97 | 190 | u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix); |
mfiore | 0:bb4e812f7c97 | 191 | memcpy(&mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], src, len); |
mfiore | 0:bb4e812f7c97 | 192 | |
mfiore | 0:bb4e812f7c97 | 193 | cache->last_access++; |
mfiore | 0:bb4e812f7c97 | 194 | cp->last_access = cache->last_access; |
mfiore | 0:bb4e812f7c97 | 195 | |
mfiore | 0:bb4e812f7c97 | 196 | if (cp->flags && SPIFFS_CACHE_FLAG_WRTHRU) { |
mfiore | 0:bb4e812f7c97 | 197 | // page is being updated, no write-cache, just pass thru |
mfiore | 0:bb4e812f7c97 | 198 | return fs->cfg.hal_write_f(addr, len, src); |
mfiore | 0:bb4e812f7c97 | 199 | } else { |
mfiore | 0:bb4e812f7c97 | 200 | return SPIFFS_OK; |
mfiore | 0:bb4e812f7c97 | 201 | } |
mfiore | 0:bb4e812f7c97 | 202 | } else { |
mfiore | 0:bb4e812f7c97 | 203 | // no cache page, no write cache - just write thru |
mfiore | 0:bb4e812f7c97 | 204 | return fs->cfg.hal_write_f(addr, len, src); |
mfiore | 0:bb4e812f7c97 | 205 | } |
mfiore | 0:bb4e812f7c97 | 206 | } |
mfiore | 0:bb4e812f7c97 | 207 | |
mfiore | 0:bb4e812f7c97 | 208 | #if SPIFFS_CACHE_WR |
mfiore | 0:bb4e812f7c97 | 209 | // returns the cache page that this fd refers, or null if no cache page |
mfiore | 0:bb4e812f7c97 | 210 | spiffs_cache_page *spiffs_cache_page_get_by_fd(spiffs *fs, spiffs_fd *fd) { |
mfiore | 0:bb4e812f7c97 | 211 | spiffs_cache *cache = spiffs_get_cache(fs); |
mfiore | 0:bb4e812f7c97 | 212 | |
mfiore | 0:bb4e812f7c97 | 213 | if ((cache->cpage_use_map & cache->cpage_use_mask) == 0) { |
mfiore | 0:bb4e812f7c97 | 214 | // all cpages free, no cpage cannot be assigned to obj_id |
mfiore | 0:bb4e812f7c97 | 215 | return 0; |
mfiore | 0:bb4e812f7c97 | 216 | } |
mfiore | 0:bb4e812f7c97 | 217 | |
mfiore | 0:bb4e812f7c97 | 218 | int i; |
mfiore | 0:bb4e812f7c97 | 219 | for (i = 0; i < cache->cpage_count; i++) { |
mfiore | 0:bb4e812f7c97 | 220 | spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i); |
mfiore | 0:bb4e812f7c97 | 221 | if ((cache->cpage_use_map & (1<<i)) && |
mfiore | 0:bb4e812f7c97 | 222 | (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) && |
mfiore | 0:bb4e812f7c97 | 223 | cp->obj_id == fd->obj_id) { |
mfiore | 0:bb4e812f7c97 | 224 | return cp; |
mfiore | 0:bb4e812f7c97 | 225 | } |
mfiore | 0:bb4e812f7c97 | 226 | } |
mfiore | 0:bb4e812f7c97 | 227 | |
mfiore | 0:bb4e812f7c97 | 228 | return 0; |
mfiore | 0:bb4e812f7c97 | 229 | } |
mfiore | 0:bb4e812f7c97 | 230 | |
mfiore | 0:bb4e812f7c97 | 231 | // allocates a new cache page and refers this to given fd - flushes an old cache |
mfiore | 0:bb4e812f7c97 | 232 | // page if all cache is busy |
mfiore | 0:bb4e812f7c97 | 233 | spiffs_cache_page *spiffs_cache_page_allocate_by_fd(spiffs *fs, spiffs_fd *fd) { |
mfiore | 0:bb4e812f7c97 | 234 | // before this function is called, it is ensured that there is no already existing |
mfiore | 0:bb4e812f7c97 | 235 | // cache page with same object id |
mfiore | 0:bb4e812f7c97 | 236 | spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0); |
mfiore | 0:bb4e812f7c97 | 237 | spiffs_cache_page *cp = spiffs_cache_page_allocate(fs); |
mfiore | 0:bb4e812f7c97 | 238 | if (cp == 0) { |
mfiore | 0:bb4e812f7c97 | 239 | // could not get cache page |
mfiore | 0:bb4e812f7c97 | 240 | return 0; |
mfiore | 0:bb4e812f7c97 | 241 | } |
mfiore | 0:bb4e812f7c97 | 242 | |
mfiore | 0:bb4e812f7c97 | 243 | cp->flags = SPIFFS_CACHE_FLAG_TYPE_WR; |
mfiore | 0:bb4e812f7c97 | 244 | cp->obj_id = fd->obj_id; |
mfiore | 0:bb4e812f7c97 | 245 | fd->cache_page = cp; |
mfiore | 0:bb4e812f7c97 | 246 | return cp; |
mfiore | 0:bb4e812f7c97 | 247 | } |
mfiore | 0:bb4e812f7c97 | 248 | |
mfiore | 0:bb4e812f7c97 | 249 | // unrefers all fds that this cache page refers to and releases the cache page |
mfiore | 0:bb4e812f7c97 | 250 | void spiffs_cache_fd_release(spiffs *fs, spiffs_cache_page *cp) { |
mfiore | 0:bb4e812f7c97 | 251 | if (cp == 0) return; |
mfiore | 0:bb4e812f7c97 | 252 | int i; |
mfiore | 0:bb4e812f7c97 | 253 | spiffs_fd *fds = (spiffs_fd *)fs->fd_space; |
mfiore | 0:bb4e812f7c97 | 254 | for (i = 0; i < fs->fd_count; i++) { |
mfiore | 0:bb4e812f7c97 | 255 | spiffs_fd *cur_fd = &fds[i]; |
mfiore | 0:bb4e812f7c97 | 256 | if (cur_fd->file_nbr != 0 && cur_fd->cache_page == cp) { |
mfiore | 0:bb4e812f7c97 | 257 | cur_fd->cache_page = 0; |
mfiore | 0:bb4e812f7c97 | 258 | } |
mfiore | 0:bb4e812f7c97 | 259 | } |
mfiore | 0:bb4e812f7c97 | 260 | spiffs_cache_page_free(fs, cp->ix, 0); |
mfiore | 0:bb4e812f7c97 | 261 | |
mfiore | 0:bb4e812f7c97 | 262 | cp->obj_id = 0; |
mfiore | 0:bb4e812f7c97 | 263 | } |
mfiore | 0:bb4e812f7c97 | 264 | |
mfiore | 0:bb4e812f7c97 | 265 | #endif |
mfiore | 0:bb4e812f7c97 | 266 | |
mfiore | 0:bb4e812f7c97 | 267 | // initializes the cache |
mfiore | 0:bb4e812f7c97 | 268 | void spiffs_cache_init(spiffs *fs) { |
mfiore | 0:bb4e812f7c97 | 269 | if (fs->cache == 0) return; |
mfiore | 0:bb4e812f7c97 | 270 | u32_t sz = fs->cache_size; |
mfiore | 0:bb4e812f7c97 | 271 | u32_t cache_mask = 0; |
mfiore | 0:bb4e812f7c97 | 272 | int i; |
mfiore | 0:bb4e812f7c97 | 273 | int cache_entries = |
mfiore | 0:bb4e812f7c97 | 274 | (sz - sizeof(spiffs_cache)) / (SPIFFS_CACHE_PAGE_SIZE(fs)); |
mfiore | 0:bb4e812f7c97 | 275 | if (cache_entries <= 0) return; |
mfiore | 0:bb4e812f7c97 | 276 | |
mfiore | 0:bb4e812f7c97 | 277 | for (i = 0; i < cache_entries; i++) { |
mfiore | 0:bb4e812f7c97 | 278 | cache_mask <<= 1; |
mfiore | 0:bb4e812f7c97 | 279 | cache_mask |= 1; |
mfiore | 0:bb4e812f7c97 | 280 | } |
mfiore | 0:bb4e812f7c97 | 281 | |
mfiore | 0:bb4e812f7c97 | 282 | spiffs_cache cache; |
mfiore | 0:bb4e812f7c97 | 283 | memset(&cache, 0, sizeof(spiffs_cache)); |
mfiore | 0:bb4e812f7c97 | 284 | cache.cpage_count = cache_entries; |
mfiore | 0:bb4e812f7c97 | 285 | cache.cpages = (u8_t *)(fs->cache + sizeof(spiffs_cache)); |
mfiore | 0:bb4e812f7c97 | 286 | |
mfiore | 0:bb4e812f7c97 | 287 | cache.cpage_use_map = 0xffffffff; |
mfiore | 0:bb4e812f7c97 | 288 | cache.cpage_use_mask = cache_mask; |
mfiore | 0:bb4e812f7c97 | 289 | memcpy(fs->cache, &cache, sizeof(spiffs_cache)); |
mfiore | 0:bb4e812f7c97 | 290 | |
mfiore | 0:bb4e812f7c97 | 291 | spiffs_cache *c = spiffs_get_cache(fs); |
mfiore | 0:bb4e812f7c97 | 292 | |
mfiore | 0:bb4e812f7c97 | 293 | memset(c->cpages, 0, c->cpage_count * SPIFFS_CACHE_PAGE_SIZE(fs)); |
mfiore | 0:bb4e812f7c97 | 294 | |
mfiore | 0:bb4e812f7c97 | 295 | c->cpage_use_map &= ~(c->cpage_use_mask); |
mfiore | 0:bb4e812f7c97 | 296 | for (i = 0; i < cache.cpage_count; i++) { |
mfiore | 0:bb4e812f7c97 | 297 | spiffs_get_cache_page_hdr(fs, c, i)->ix = i; |
mfiore | 0:bb4e812f7c97 | 298 | } |
mfiore | 0:bb4e812f7c97 | 299 | } |
mfiore | 0:bb4e812f7c97 | 300 | |
Leon Lindenfelser |
3:9adcf49bb77d | 301 | #endif // SPIFFS_CACHE |