Hi@lim
You don't need to write any code, just modify these areas.
for example:Enable system security and Enable mass erase.
__FlashConfig
DCD 0xFFFFFFFF ; 8 bytes backdoor comparison key
DCD 0xFFFFFFFF ;
DCD 0xFFFFFFFF ; 4 bytes program flash protection bytes
DCD 0xFFFF7FFF ; FDPROT:FEPROT:FOPT:FSEC(0xFE = unsecured)
__FlashConfig_End

please refers to this article.
https://www.nxp.com.cn/docs/en/application-note/AN12130.pdf
know more:
It is very similar to the security features of the Kinetis family MCUs. You can refer to AN4507 Using the Kinetis Security and Flash Protection Features
https://www.nxp.com/docs/en/application-note/AN4507.pdf
BR!
Jim,