Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / m5stack-tft / repo / components / spiffs / esp_spiffs.c
1 /*
2  * Lua RTOS, SPIFFS low access
3  *
4  * Copyright (C) 2015 - 2017
5  * IBEROXARXA SERVICIOS INTEGRALES, S.L. & CSS IBÉRICA, S.L.
6  * 
7  * Author: Jaume Olivé (jolive@iberoxarxa.com / jolive@whitecatboard.org)
8  * 
9  * All rights reserved.  
10  *
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.
19  *
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
27  * this software.
28  */
29
30 #include <stdlib.h>
31
32 #include "esp_spiffs.h"
33 #include "esp_attr.h"
34
35 #include "spiffs.h"
36
37 #include <esp_spi_flash.h>
38
39 s32_t IRAM_ATTR esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) {
40         u32_t aaddr;
41         u8_t *buff = NULL;
42         u8_t *abuff = NULL;
43         u32_t asize;
44
45         asize = size;
46         
47         // Align address to 4 byte
48         aaddr = (addr + (4 - 1)) & (u32_t)-4;
49         if (aaddr != addr) {
50                 aaddr -= 4;
51                 asize += (addr - aaddr);
52         }
53
54         // Align size to 4 byte
55         asize = (asize + (4 - 1)) & (u32_t)-4;
56
57         if ((aaddr != addr) || (asize != size)) {
58                 // Align buffer
59                 buff = malloc(asize + 4);
60                 if (!buff) {
61                         return SPIFFS_ERR_INTERNAL;
62                 }
63
64                 abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & (u32_t)-4);
65
66                 if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
67                         free(buff);
68                         return SPIFFS_ERR_INTERNAL;
69                 }
70
71                 memcpy(dst, abuff + (addr - aaddr), size);
72
73                 free(buff);
74         } else {
75                 if (spi_flash_read(addr, (void *)dst, size) != 0) {
76                         return SPIFFS_ERR_INTERNAL;
77                 }
78         }
79         
80     return SPIFFS_OK;
81 }
82
83 s32_t IRAM_ATTR esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) {
84         u32_t aaddr;
85         u8_t *buff = NULL;
86         u8_t *abuff = NULL;
87         u32_t asize;
88
89         asize = size;
90         
91         // Align address to 4 byte
92         aaddr = (addr + (4 - 1)) & -4;
93         if (aaddr != addr) {
94                 aaddr -= 4;
95                 asize += (addr - aaddr);
96         }
97
98         // Align size to 4 byte
99         asize = (asize + (4 - 1)) & -4; 
100
101         if ((aaddr != addr) || (asize != size)) {
102                 // Align buffer
103                 buff = malloc(asize + 4);
104                 if (!buff) {
105                         return SPIFFS_ERR_INTERNAL;
106                 }
107
108                 abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & -4);
109
110                 if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
111                         free(buff);
112                         return SPIFFS_ERR_INTERNAL;
113                 }
114
115                 memcpy(abuff + (addr - aaddr), src, size);
116
117                 if (spi_flash_write(aaddr, (uint32_t *)abuff, asize) != 0) {
118                         free(buff);
119                         return SPIFFS_ERR_INTERNAL;
120                 }
121
122                 free(buff);
123         } else {
124                 if (spi_flash_write(addr, (uint32_t *)src, size) != 0) {
125                         return SPIFFS_ERR_INTERNAL;
126                 }
127         }
128         
129     return SPIFFS_OK;
130 }
131
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;
135         }
136         
137     return SPIFFS_OK;
138 }