Skip to content

Commit 8a654ff

Browse files
committed
feat(psram): ecc feature on c5 c61
1 parent 6505bcd commit 8a654ff

File tree

8 files changed

+560
-13
lines changed

8 files changed

+560
-13
lines changed

components/esp_psram/device/esp_psram_impl_ap_quad.c

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ uint8_t esp_psram_impl_get_cs_io(void)
3131
return s_psram_cs_io;
3232
}
3333

34-
void psram_exec_cmd(int spi_num, psram_hal_cmd_mode_t mode,
34+
void psram_exec_cmd(int spi_num, psram_cmd_mode_t mode,
3535
uint32_t cmd, int cmd_bit_len,
3636
uint32_t addr, int addr_bit_len,
3737
int dummy_bits,
@@ -72,7 +72,7 @@ static void psram_disable_qio_mode(int spi_num)
7272
//TODO IDF-4307
7373
//switch psram burst length(32 bytes or 1024 bytes)
7474
//datasheet says it should be 1024 bytes by default
75-
static void psram_set_wrap_burst_length(int spi_num, psram_hal_cmd_mode_t mode)
75+
static void psram_set_wrap_burst_length(int spi_num, psram_cmd_mode_t mode)
7676
{
7777
psram_exec_cmd(spi_num, mode,
7878
PSRAM_QUAD_SET_BURST_LEN, 8, /* command and command bit len*/
@@ -170,8 +170,50 @@ static void psram_set_cs_timing(void)
170170
{
171171
psram_ctrlr_ll_set_cs_hold(PSRAM_CTRLR_LL_MSPI_ID_0, PSRAM_QUAD_CS_HOLD_VAL);
172172
psram_ctrlr_ll_set_cs_setup(PSRAM_CTRLR_LL_MSPI_ID_0, PSRAM_QUAD_CS_SETUP_VAL);
173+
#if CONFIG_SPIRAM_ECC_ENABLE
174+
psram_ctrlr_ll_set_ecc_cs_hold(PSRAM_CTRLR_LL_MSPI_ID_0, PSRAM_QUAD_CS_ECC_HOLD_TIME_VAL);
175+
#endif
173176
}
174177

178+
#if CONFIG_SPIRAM_ECC_ENABLE
179+
static void s_mspi_ecc_show_info(void)
180+
{
181+
for (int i = 0; i < PSRAM_CTRLR_LL_PMS_REGION_NUMS; i++) {
182+
ESP_EARLY_LOGV(TAG, "region[%d] addr: 0x%08x", i, psram_ctrlr_ll_get_pms_region_start_addr(PSRAM_CTRLR_LL_MSPI_ID_0, i));
183+
ESP_EARLY_LOGV(TAG, "region[%d] size: 0x%08x", i, psram_ctrlr_ll_get_pms_region_size(PSRAM_CTRLR_LL_MSPI_ID_0, i));
184+
}
185+
186+
uint32_t page_size = psram_ctrlr_ll_get_page_size(PSRAM_CTRLR_LL_MSPI_ID_0);
187+
ESP_EARLY_LOGV(TAG, "ECC page size: %d", page_size);
188+
}
189+
190+
/**
191+
* Enable error correcting code feature
192+
*
193+
* Can add an input parameter for selecting ECC mode if needed
194+
*/
195+
static void s_configure_psram_ecc(void)
196+
{
197+
psram_ctrlr_ll_set_ecc_mode(PSRAM_CTRLR_LL_MSPI_ID_0, PSRAM_LL_ECC_MODE_16TO18);
198+
psram_ctrlr_ll_enable_skip_page_corner(PSRAM_CTRLR_LL_MSPI_ID_0, true);
199+
psram_ctrlr_ll_enable_split_trans(PSRAM_CTRLR_LL_MSPI_ID_0, true);
200+
psram_ctrlr_ll_set_page_size(PSRAM_CTRLR_LL_MSPI_ID_0, PSRAM_QUAD_PAGE_SIZE);
201+
psram_ctrlr_ll_enable_ecc_addr_conversion(PSRAM_CTRLR_LL_MSPI_ID_0, true);
202+
203+
/**
204+
* Enable ECC region 0 (ACE0)
205+
* Default: ACE0 range: 0 ~ 256MB
206+
* For current Quad PSRAM, ACE0 is enough
207+
*/
208+
psram_ctrlr_ll_set_pms_region_start_addr(PSRAM_CTRLR_LL_MSPI_ID_0, 0, 0);
209+
psram_ctrlr_ll_set_pms_region_size(PSRAM_CTRLR_LL_MSPI_ID_0, 0, 4096);
210+
psram_ctrlr_ll_set_pms_region_attr(PSRAM_CTRLR_LL_MSPI_ID_0, 0, PSRAM_CTRLR_LL_PMS_ATTR_WRITABLE | PSRAM_CTRLR_LL_PMS_ATTR_READABLE);
211+
psram_ctrlr_ll_enable_pms_region_ecc(PSRAM_CTRLR_LL_MSPI_ID_0, 0, true);
212+
ESP_EARLY_LOGI(TAG, "ECC is enabled");
213+
s_mspi_ecc_show_info();
214+
}
215+
#endif
216+
175217
static void psram_gpio_config(void)
176218
{
177219
//CS1
@@ -240,6 +282,9 @@ esp_err_t esp_psram_impl_enable(void)
240282
{
241283
psram_gpio_config();
242284
psram_set_cs_timing();
285+
#if CONFIG_SPIRAM_ECC_ENABLE
286+
s_configure_psram_ecc();
287+
#endif
243288

244289
#if SOC_SPI_MEM_SUPPORT_TIMING_TUNING
245290
//enter MSPI slow mode to init PSRAM device registers
@@ -330,16 +375,18 @@ esp_err_t esp_psram_impl_get_physical_size(uint32_t *out_size_bytes)
330375

331376
/**
332377
* This function is to get the available physical psram size in bytes.
333-
*
334-
* When ECC is enabled, the available size will be reduced.
335-
* On S3 Quad PSRAM, ECC is not enabled for now.
378+
* If ECC is enabled, available PSRAM size will be 7/8 times its physical size.
336379
*/
337380
esp_err_t esp_psram_impl_get_available_size(uint32_t *out_size_bytes)
338381
{
339382
if (!out_size_bytes) {
340383
return ESP_ERR_INVALID_ARG;
341384
}
342385

386+
#if CONFIG_SPIRAM_ECC_ENABLE
387+
*out_size_bytes = s_psram_size * 7 / 8;
388+
#else
343389
*out_size_bytes = s_psram_size;
390+
#endif
344391
return (s_psram_size ? ESP_OK : ESP_ERR_INVALID_STATE);
345392
}

components/esp_psram/device/esp_quad_psram_defs_ap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ extern "C" {
7373

7474
#define PSRAM_QUAD_CS_HOLD_VAL 1
7575
#define PSRAM_QUAD_CS_SETUP_VAL 1
76+
#define PSRAM_QUAD_CS_ECC_HOLD_TIME_VAL 3
77+
78+
#define PSRAM_QUAD_PAGE_SIZE 512
79+
#define PSRAM_QUAD_ECC_ENABLE_MASK BIT(8)
7680

7781
// QEMU has a simulated 16MB and 32MB Quad SPI PSRAM. Use a fake ID for these.
7882
#define PSRAM_QUAD_QEMU_16MB_ID 0x6a

components/esp_psram/esp32c5/Kconfig.spiram

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,13 @@ menu "SPI RAM config"
6868
This is a helper indicating this condition:
6969
`CONFIG_SPIRAM_XIP_FROM_PSRAM && CONFIG_IDF_TARGET_ESP32C5`
7070

71+
config SPIRAM_ECC_ENABLE
72+
bool "Enable PSRAM ECC"
73+
default n
74+
help
75+
Enable MSPI Error-Correcting Code function when accessing PSRAM.
76+
77+
If enabled, 1/8 of the PSRAM total size will be reserved for error-correcting code.
78+
7179
source "$IDF_PATH/components/esp_psram/Kconfig.spiram.common" # insert non-chip-specific items here
7280
endmenu

components/esp_psram/esp32c61/Kconfig.spiram

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,13 @@ menu "SPI RAM config"
6868
This is a helper indicating this condition:
6969
`CONFIG_SPIRAM_XIP_FROM_PSRAM && CONFIG_IDF_TARGET_ESP32C61`
7070

71+
config SPIRAM_ECC_ENABLE
72+
bool "Enable PSRAM ECC"
73+
default n
74+
help
75+
Enable MSPI Error-Correcting Code function when accessing PSRAM.
76+
77+
If enabled, 1/8 of the PSRAM total size will be reserved for error-correcting code.
78+
7179
source "$IDF_PATH/components/esp_psram/Kconfig.spiram.common" # insert non-chip-specific items here
7280
endmenu

0 commit comments

Comments
 (0)