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