If your code is based on STM32CubeMX generation, try to use the feature allowing to import a project from one MCU to another. Some more details are in the article MCU1 to MCU2 migration option within STM32CubeIDE.
-Amel
To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.