on 2026-03-19 12:30 PM
STM32C5 devices integrate a RAM configuration controller (RAMCFG) that provides error code correction (ECC) on internal SRAM. On STM32C5, ECC is available only on SRAM2. ECC allows the following:
This document explains how to enable and use RAM ECC on STM32C5, based on RM0522 (RAMCFG chapter), and provides:
Install the following tools:
The hardware used in this tutorial is the NUCLEO-C562RE board.
ECC is supported only by SRAM2 when the SRAM2_ECC user option bits are enabled.
When ECC is enabled for SRAM2, the entire SRAM2 is protected by ECC.
|
Product |
SRAM2 S bus address range |
SRAM2 size |
ECC |
|
STM32C53x/54 |
0x20008000 – 0x2000FFFF |
32 KB |
Yes |
|
STM32C55x/56 |
0x20010000 – 0x2001FFFF |
64 KB |
Yes |
|
STM32C59x/5A |
0x20020000 – 0x2003FFFF |
128 KB |
Yes |
CMSIS defines this as SRAM2_BASE.
Single error:
Double error:
Key registers for ECC (SRAM2 = memory 2):
Use case: Robust state memory in an industrial controller
Consider an STM32C5 controlling an industrial process:
Typical pattern:
Follow these steps to create an application project for the NUCLEO-C562RE board. This exercise creates a simple SRAM2 ECC application.
Open STM32CubeMX2. On the "Home" page, click the MCU square to create a new project.
In the search field under MCU name, enter STM32C562RE and select the MCU. Click [Continue].
Enter the project name and location. Click [Automatically Download, Install & Create Project] to finish project creation.
Click [Launch Project] to start.
In STM32CubeMX2, go to the [Peripherals] section, then click [Memory]. Enable the RAMCFG-SRAM2 with the ECC.
To generate the code:
Open Visual Studio Code and open the project folder.
If prompted, select the configuration. If not prompted, press Ctrl+Shift+P, type CMake: Select Configure Preset, and choose the debug configuration.
Build the project to ensure everything is set, then proceed to code implementation.
The code aims to create two user functions, the first function to trigger the single bit error and the second function to trigger the double bit error. Both use the first position of SRAM2 0x2001 0000 as base.
This handler is called after checking for single or double errors, reads and logs the failing address, and clears the corresponding flags.
__WEAK hal_status_t HAL_RAMCFG_ECC_ErrorCallback(hal_ramcfg_t instance)
RAMCFG can erase the entire SRAM2 by writing zero to all locations.
hal_status_t HAL_RAMCFG_MassErase(hal_ramcfg_t instance, uint32_t timeout)
RM0522 proposes a software sequence to test the ECC mechanism:
main.c
/**
******************************************************************************
* file : main.c
* brief : Main program body
* Calls target system initialization then loop in main.
******************************************************************************
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define TEST_WORD_ADDR ((volatile uint32_t *)SRAM2_BASE)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private functions prototype -----------------------------------------------*/
hal_status_t HAL_RAMCFG_ECC_ErrorCallback(hal_ramcfg_t instance)
{
/* Prevent unused argument(s) compilation warning */
STM32_UNUSED(instance);
return HAL_ERROR;
}
void RAM_ECC_Test_SingleError(void)
{
uint32_t pattern = 0x12345678;
uint32_t tmp;
HAL_RAMCFG_MassErase(HAL_RAMCFG_SRAM2,1000);
HAL_RAMCFG_ECC_Enable_IT(HAL_RAMCFG_SRAM2, HAL_RAMCFG_IT_ECC_SINGLE | HAL_RAMCFG_IT_ECC_DOUBLE);
*TEST_WORD_ADDR = pattern;
HAL_RAMCFG_ECC_Disable(HAL_RAMCFG_SRAM2);
tmp = *TEST_WORD_ADDR;
tmp ^= (1UL << 3);
*TEST_WORD_ADDR = tmp;
HAL_RAMCFG_ECC_Enable_IT(HAL_RAMCFG_SRAM2, HAL_RAMCFG_IT_ECC_SINGLE | HAL_RAMCFG_IT_ECC_DOUBLE);
volatile uint32_t read_back = *TEST_WORD_ADDR;
(void)read_back;
}
void RAM_ECC_Test_DoubleError(void)
{
uint32_t pattern = 0xA5A5A5A5;
uint32_t tmp;
HAL_RAMCFG_MassErase(HAL_RAMCFG_SRAM2,1000);
HAL_RAMCFG_ECC_Enable_IT(HAL_RAMCFG_SRAM2, HAL_RAMCFG_IT_ECC_SINGLE | HAL_RAMCFG_IT_ECC_DOUBLE);
*TEST_WORD_ADDR = pattern;
HAL_RAMCFG_ECC_Disable(HAL_RAMCFG_SRAM2);
tmp = *TEST_WORD_ADDR;
tmp ^= (1UL << 1);
tmp ^= (1UL << 5);
*TEST_WORD_ADDR = tmp;
HAL_RAMCFG_ECC_Enable_IT(HAL_RAMCFG_SRAM2, HAL_RAMCFG_IT_ECC_SINGLE | HAL_RAMCFG_IT_ECC_DOUBLE);
volatile uint32_t read_back = *TEST_WORD_ADDR;
(void)read_back;
}
/**
* brief: The application entry point.
* retval: none but we specify int to comply with C99 standard
*/
int main(void)
{
/** System Init: this code placed in targets folder initializes your system.
* It calls the initialization (and sets the initial configuration) of the peripherals.
* You can use STM32CubeMX to generate and call this code or not in this project.
* It also contains the HAL initialization and the initial clock configuration.
*/
if (mx_system_init() != SYSTEM_OK)
{
return (-1);
}
else
{
/*
* You can start your application code here
*/
RAM_ECC_Test_SingleError();
RAM_ECC_Test_DoubleError();
while (1) {}
}
} /* end main */
After building the application, click the [Run and Debug] icon. Create a debug session by selecting the STLINK GDB Server option.
Add a breakpoint in the callback and monitor the CALL STACK.
To monitor the address caused by the ECC, access the register tree. Given the used define TEST_WORD_ADDR is the first address of SRAM2 (0x2001 0000) we can observe it in the register.
When tracking the CALL STACK, it is possible to identify the error, either single or double error. The image below shows the check for double error.