1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #ifndef _PSRAM_H
9 #define _PSRAM_H
10 #include "soc/spi_mem_reg.h"
11 #include "esp_err.h"
12 #include "sdkconfig.h"
13 
14 typedef enum {
15     PSRAM_CACHE_S80M = 1,
16     PSRAM_CACHE_S40M,
17     PSRAM_CACHE_S26M,
18     PSRAM_CACHE_S20M,
19     PSRAM_CACHE_MAX,
20 } psram_cache_mode_t;
21 
22 typedef enum {
23     PSRAM_SIZE_16MBITS = 0,
24     PSRAM_SIZE_32MBITS = 1,
25     PSRAM_SIZE_64MBITS = 2,
26     PSRAM_SIZE_MAX,
27 } psram_size_t;
28 
29 /*
30 See the TRM, chapter PID/MPU/MMU, header 'External RAM' for the definitions of these modes.
31 
32 Important is that NORMAL works with the app CPU cache disabled, but gives huge cache coherency
33 issues when both app and pro CPU are enabled. LOWHIGH and EVENODD do not have these coherency
34 issues but cannot be used when the app CPU cache is disabled.
35 */
36 typedef enum {
37     PSRAM_VADDR_MODE_NORMAL=0, ///< App and pro CPU use their own flash cache for external RAM access
38     PSRAM_VADDR_MODE_LOWHIGH,  ///< App and pro CPU share external RAM caches: pro CPU has low 2M, app CPU has high 2M
39     PSRAM_VADDR_MODE_EVENODD,  ///< App and pro CPU share external RAM caches: pro CPU does even 32yte ranges, app does odd ones.
40 } psram_vaddr_mode_t;
41 
42 /**
43  * @brief get psram size
44  * @return
45  *     - PSRAM_SIZE_MAX if psram not enabled or not valid
46  *     - PSRAM size
47  */
48 psram_size_t psram_get_size(void);
49 
50 /**
51  * @brief psram cache enable function
52  *
53  * Esp-idf uses this to initialize cache for psram, mapping it into the main memory
54  * address space.
55  *
56  * @param mode       SPI mode to access psram in
57  * @param vaddrmode  Mode the psram cache works in.
58  * @return ESP_OK on success, ESP_ERR_INVALID_STATE when VSPI peripheral is needed but cannot be claimed.
59  */
60 esp_err_t psram_enable(psram_cache_mode_t mode, psram_vaddr_mode_t vaddrmode);
61 
62 typedef enum {
63     SPIRAM_WRAP_MODE_16B,
64     SPIRAM_WRAP_MODE_32B,
65     SPIRAM_WRAP_MODE_64B,
66     SPIRAM_WRAP_MODE_DISABLE
67 } spiram_wrap_mode_t;
68 
69 esp_err_t esp_spiram_wrap_set(spiram_wrap_mode_t mode);
70 
71 /**
72  * @brief get psram CS IO
73  *
74  * @return psram CS IO
75  */
76 uint8_t psram_get_cs_io(void);
77 
78 #endif
79