spiram.c 8.7 KB

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