Hello VaneB.
The problem was not in the firmware .
To solve the issue first I program the target with FSEC bits set in mode "unsecure" in "Startup... .S" file.
.long 0xFFFF7BBE // FDPROT:FEPRO:FOTP:FSEC
Then I change the bits above in mode "Secure"
.long 0xFFFF7BBF // FDPROT:FEPRO:FOTP:FSEC
Rebuild all.
Then I start a new programming/debugging session preserving two memory ranges:
0x00000 -> 0x0040B and 0x00410 -> 0x7FFFF
so that I program only the four bytes FDPROT:FEPRO:FOTP:FSEC
After programming, the debugger tries to connect to target but now the target is secured.
So it asks me if I want to erase the target.
I answer "No" for two times , then the debugger stops and disconnects from target.
Now the target is locked.
At this time, if I unplug the usb cable of the debugger from the PC, the target becomes active and executes the application firmware correctly.
Now the target is working well and is secured.
Thank you.
Best regards
Nicola