]> granicus.if.org Git - esp-idf/blob - components/esp32/spiram.c
Merge branch 'doc/heap_tracing' into 'master'
[esp-idf] / components / esp32 / spiram.c
1 /*
2 Abstraction layer for spi-ram. For now, it's no more than a stub for the spiram_psram functions, but if 
3 we add more types of external RAM memory, this can be made into a more intelligent dispatcher.
4 */
5
6 // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 //     http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19
20 #include <stdint.h>
21 #include <string.h>
22
23 #include "sdkconfig.h"
24 #include "esp_attr.h"
25 #include "esp_err.h"
26 #include "esp_spiram.h"
27 #include "spiram_psram.h"
28 #include "esp_log.h"
29 #include "freertos/FreeRTOS.h"
30 #include "freertos/xtensa_api.h"
31 #include "soc/soc.h"
32 #include "esp_heap_caps_init.h"
33 #include "soc/soc_memory_layout.h"
34 #include "soc/dport_reg.h"
35 #include "rom/cache.h"
36
37 #if CONFIG_FREERTOS_UNICORE
38 #define PSRAM_MODE PSRAM_VADDR_MODE_NORMAL
39 #else
40 #if CONFIG_MEMMAP_SPIRAM_CACHE_EVENODD
41 #define PSRAM_MODE PSRAM_VADDR_MODE_EVENODD
42 #else
43 #define PSRAM_MODE PSRAM_VADDR_MODE_LOWHIGH
44 #endif
45 #endif
46
47 #if CONFIG_SPIRAM_SUPPORT
48
49 static const char* TAG = "spiram";
50
51 #if CONFIG_SPIRAM_SPEED_40M && CONFIG_ESPTOOLPY_FLASHFREQ_40M
52 #define PSRAM_SPEED PSRAM_CACHE_F40M_S40M
53 #elif CONFIG_SPIRAM_SPEED_40M && CONFIG_ESPTOOLPY_FLASHFREQ_80M
54 #define PSRAM_SPEED PSRAM_CACHE_F80M_S40M
55 #elif CONFIG_SPIRAM_SPEED_80M && CONFIG_ESPTOOLPY_FLASHFREQ_80M
56 #define PSRAM_SPEED PSRAM_CACHE_F80M_S80M
57 #else
58 #error "FLASH speed can only be equal to or higher than SRAM speed while SRAM is enabled!"
59 #endif
60
61
62 static bool spiram_inited=false;
63
64
65 /*
66  Simple RAM test. Writes a word every 32 bytes. Takes about a second to complete for 4MiB. Returns
67  true when RAM seems OK, false when test fails. WARNING: Do not run this before the 2nd cpu has been
68  initialized (in a two-core system) or after the heap allocator has taken ownership of the memory.
69 */
70 bool esp_spiram_test()
71 {
72     volatile int *spiram=(volatile int*)SOC_EXTRAM_DATA_LOW;
73     size_t p;
74     size_t s=CONFIG_SPIRAM_SIZE;
75     int errct=0;
76     int initial_err=-1;
77     for (p=0; p<(s/sizeof(int)); p+=8) {
78         spiram[p]=p^0xAAAAAAAA;
79     }
80     for (p=0; p<(s/sizeof(int)); p+=8) {
81         if (spiram[p]!=(p^0xAAAAAAAA)) {
82             errct++;
83             if (errct==1) initial_err=p*4;
84         }
85     }
86     if (errct) {
87         ESP_EARLY_LOGE(TAG, "SPI SRAM memory test fail. %d/%d writes failed, first @ %X\n", errct, s/32, initial_err+SOC_EXTRAM_DATA_LOW);
88         return false;
89     } else {
90         ESP_EARLY_LOGI(TAG, "SPI SRAM memory test OK");
91         return true;
92     }
93 }
94
95 void IRAM_ATTR esp_spiram_init_cache()
96 {
97     //Enable external RAM in MMU
98     cache_sram_mmu_set( 0, 0, SOC_EXTRAM_DATA_LOW, 0, 32, 128 );
99     //Flush and enable icache for APP CPU
100 #if !CONFIG_FREERTOS_UNICORE
101     DPORT_CLEAR_PERI_REG_MASK(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MASK_DRAM1);
102     cache_sram_mmu_set( 1, 0, SOC_EXTRAM_DATA_LOW, 0, 32, 128 );
103 #endif
104 }
105
106 esp_spiram_volt_t esp_spiram_get_chip_volt()
107 {
108     if (!spiram_inited) {
109         ESP_LOGE(TAG, "SPI RAM not initialized");
110         return ESP_SPIRAM_VOLT_INVALID;
111     }
112     psram_volt_t volt = psram_get_volt();
113     switch (volt) {
114         case PSRAM_VOLT_1V8:
115             return ESP_SPIRAM_VOLT_1V8;
116         case PSRAM_VOLT_3V3:
117             return ESP_SPIRAM_VOLT_3V3;
118         default:
119             return ESP_SPIRAM_VOLT_INVALID;
120     }
121 }
122
123 esp_spiram_size_t esp_spiram_get_chip_size()
124 {
125     if (!spiram_inited) {
126         ESP_LOGE(TAG, "SPI RAM not initialized");
127         return ESP_SPIRAM_SIZE_INVALID;
128     }
129     psram_size_t psram_size = psram_get_size();
130     switch (psram_size) {
131         case PSRAM_SIZE_32MBITS:
132             return ESP_SPIRAM_SIZE_32MBITS;
133         case PSRAM_SIZE_64MBITS:
134             return ESP_SPIRAM_SIZE_64MBITS;
135         default:
136             return ESP_SPIRAM_SIZE_INVALID;
137     }
138 }
139
140 esp_err_t esp_spiram_init()
141 {
142     esp_err_t r;
143     r = psram_enable(PSRAM_SPEED, PSRAM_MODE);
144     if (r != ESP_OK) {
145 #if CONFIG_SPIRAM_IGNORE_NOTFOUND
146         ESP_EARLY_LOGE(TAG, "SPI RAM enabled but initialization failed. Bailing out.");
147 #endif
148         return r;
149     }
150
151     ESP_EARLY_LOGI(TAG, "SPI RAM mode: %s", PSRAM_SPEED == PSRAM_CACHE_F40M_S40M ? "flash 40m sram 40m" : \
152                                           PSRAM_SPEED == PSRAM_CACHE_F80M_S40M ? "flash 80m sram 40m" : \
153                                           PSRAM_SPEED == PSRAM_CACHE_F80M_S80M ? "flash 80m sram 80m" : "ERROR");
154     ESP_EARLY_LOGI(TAG, "PSRAM initialized, cache is in %s mode.", \
155                                           (PSRAM_MODE==PSRAM_VADDR_MODE_EVENODD)?"even/odd (2-core)": \
156                                           (PSRAM_MODE==PSRAM_VADDR_MODE_LOWHIGH)?"low/high (2-core)": \
157                                           (PSRAM_MODE==PSRAM_VADDR_MODE_NORMAL)?"normal (1-core)":"ERROR");
158     spiram_inited=true;
159     return ESP_OK;
160 }
161
162
163 esp_err_t esp_spiram_add_to_heapalloc()
164 {
165     ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", CONFIG_SPIRAM_SIZE/1024);
166     //Add entire external RAM region to heap allocator. Heap allocator knows the capabilities of this type of memory, so there's
167     //no need to explicitly specify them.
168     return heap_caps_add_region((intptr_t)SOC_EXTRAM_DATA_LOW, (intptr_t)SOC_EXTRAM_DATA_LOW + CONFIG_SPIRAM_SIZE-1);
169 }
170
171
172 static uint8_t *dma_heap;
173
174 esp_err_t esp_spiram_reserve_dma_pool(size_t size) {
175     if (size==0) return ESP_OK; //no-op
176     ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size/1024);
177     dma_heap=heap_caps_malloc(size, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
178     if (!dma_heap) return ESP_ERR_NO_MEM;
179     uint32_t caps[]={MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL, 0, MALLOC_CAP_8BIT|MALLOC_CAP_32BIT};
180     return heap_caps_add_region_with_caps(caps, (intptr_t) dma_heap, (intptr_t) dma_heap+size-1);
181 }
182
183 size_t esp_spiram_get_size()
184 {
185     return CONFIG_SPIRAM_SIZE;
186 }
187
188 /*
189  Before flushing the cache, if psram is enabled as a memory-mapped thing, we need to write back the data in the cache to the psram first,
190  otherwise it will get lost. For now, we just read 64/128K of random PSRAM memory to do this.
191 */
192 void IRAM_ATTR esp_spiram_writeback_cache() 
193 {
194     int x;
195     volatile int i=0;
196     volatile uint8_t *psram=(volatile uint8_t*)SOC_EXTRAM_DATA_LOW;
197     int cache_was_disabled=0;
198
199     if (!spiram_inited) return;
200
201     //We need cache enabled for this to work. Re-enable it if needed; make sure we 
202     //disable it again on exit as well.
203     if (DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE)==0) {
204         cache_was_disabled|=(1<<0);
205         DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
206     }
207 #ifndef CONFIG_FREERTOS_UNICORE
208     if (DPORT_REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE)==0) {
209         cache_was_disabled|=(1<<1);
210         DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
211     }
212 #endif
213
214 #if CONFIG_FREERTOS_UNICORE
215     for (x=0; x<1024*64; x+=32) {
216         i+=psram[x];
217     }
218 #else
219     /*
220     Note: this assumes the amount of external RAM is >2M. If it is 2M or less, what this code does is undefined. If 
221     we ever support external RAM chips of 2M or smaller, this may need adjusting.
222     */
223     for (x=0; x<1024*64; x+=32) {
224         i+=psram[x];
225         i+=psram[x+(1024*1024*2)+(1024*64)]; //address picked to also clear cache of app cpu in low/high mode
226     }
227 #endif
228
229     if (cache_was_disabled&(1<<0)) {
230         while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) ;
231         DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
232     }
233 #ifndef CONFIG_FREERTOS_UNICORE
234     if (cache_was_disabled&(1<<1)) {
235         while (DPORT_GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1);
236         DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
237     }
238 #endif
239 }
240
241
242
243 #endif