This issue is discussed also in AN5342, although, it covers only the 'H7, 'U5 and 'H5 families.
Software to proparely detect and handle ECC errors has to be tested somehow, too. This means that ECC single-bit and double-bit errors have to be generated somehow.
ECC errors in SRAM are relatively easy: uninitialized SRAM after power removal/restore is quite likely to contain errorneous rows. While most of these will probably be double-bit errors, it's quick to scan through the SRAM to try to find rows with single-bit errors, too. AN5342 hints that there may be STM32 families out there which can produce ECC errors at request, but then it does not tell, which ones are such and how they do that...
To produce FLASH ECC errors, AN5342 suggest writing two different values consecutively at the same position. Of course, there may be couples of values which have the same ECC "checksum", and also it may be tricky to find couples of values which result in single-bit error. User 20jmorrison provides the following couples for 'H7:
uint64_t SingleErrorA[4] = {0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCC0};
uint64_t SingleErrorB[4] = {0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCC1};
uint64_t DoubleErrorA[4] = {0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCB};
uint64_t DoubleErrorB[4] = {0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC};
However, the FLASH controller of some STM32 - e.g. in the 'L4 family - simply doesn't allow writing to a FLASH row, which is not in the erased state 1. Attempt to do so results in programming error signalled by FLASH status bits, but not in faulty FLASH content. Here, the trick is to abort the erasing/programmig process by power removal. It appears, that even reset will abort the erasing/programming, resulting in FLASH corruption and ECC error (usually double-bit one). This fact is used by xarantolus in a method, where ECC error is generated "automatically" by binary-searching the proper timing of watchdog reset so that it occurs during FLASH programming (unfortunately the published code is in Rust).
Some STM32 provide a "known faulty" position in the System memory (which is a manufacturer-written-then-locked portion of FLASH); for example in 'U5, 0x0BFA1F00 contains a one-bit error and 0x0BFA1F80 a two-bit error. An interesting gotcha (acknowledged by the RM) resulting from this is, that NMI is triggered even if the latter address is read by the debugger.