2 * Lua RTOS, SPIFFS low access
4 * Copyright (C) 2015 - 2017
5 * IBEROXARXA SERVICIOS INTEGRALES, S.L. & CSS IBÉRICA, S.L.
7 * Author: Jaume Olivé (jolive@iberoxarxa.com / jolive@whitecatboard.org)
11 * Permission to use, copy, modify, and distribute this software
12 * and its documentation for any purpose and without fee is hereby
13 * granted, provided that the above copyright notice appear in all
14 * copies and that both that the copyright notice and this
15 * permission notice and warranty disclaimer appear in supporting
16 * documentation, and that the name of the author not be used in
17 * advertising or publicity pertaining to distribution of the
18 * software without specific, written prior permission.
20 * The author disclaim all warranties with regard to this
21 * software, including all implied warranties of merchantability
22 * and fitness. In no event shall the author be liable for any
23 * special, indirect or consequential damages or any damages
24 * whatsoever resulting from loss of use, data or profits, whether
25 * in an action of contract, negligence or other tortious action,
26 * arising out of or in connection with the use or performance of
32 #include "esp_spiffs.h"
37 #include <esp_spi_flash.h>
39 s32_t IRAM_ATTR esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) {
47 // Align address to 4 byte
48 aaddr = (addr + (4 - 1)) & (u32_t)-4;
51 asize += (addr - aaddr);
54 // Align size to 4 byte
55 asize = (asize + (4 - 1)) & (u32_t)-4;
57 if ((aaddr != addr) || (asize != size)) {
59 buff = malloc(asize + 4);
61 return SPIFFS_ERR_INTERNAL;
64 abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & (u32_t)-4);
66 if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
68 return SPIFFS_ERR_INTERNAL;
71 memcpy(dst, abuff + (addr - aaddr), size);
75 if (spi_flash_read(addr, (void *)dst, size) != 0) {
76 return SPIFFS_ERR_INTERNAL;
83 s32_t IRAM_ATTR esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) {
91 // Align address to 4 byte
92 aaddr = (addr + (4 - 1)) & -4;
95 asize += (addr - aaddr);
98 // Align size to 4 byte
99 asize = (asize + (4 - 1)) & -4;
101 if ((aaddr != addr) || (asize != size)) {
103 buff = malloc(asize + 4);
105 return SPIFFS_ERR_INTERNAL;
108 abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & -4);
110 if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
112 return SPIFFS_ERR_INTERNAL;
115 memcpy(abuff + (addr - aaddr), src, size);
117 if (spi_flash_write(aaddr, (uint32_t *)abuff, asize) != 0) {
119 return SPIFFS_ERR_INTERNAL;
124 if (spi_flash_write(addr, (uint32_t *)src, size) != 0) {
125 return SPIFFS_ERR_INTERNAL;
132 s32_t IRAM_ATTR esp32_spi_flash_erase(u32_t addr, u32_t size) {
133 if (spi_flash_erase_sector(addr >> 12) != 0) {
134 return SPIFFS_ERR_INTERNAL;