[Stm32F401] Target not detected after setting MODER register! - Page 1
Forum section: electronics / general

author: kgavionics
author_rank: Regular Contributor
timestamp: August 04, 2022, 11:29:46 pm
content: Hi guysI have a weird problem that is my blackpill ( based on STM32F401 chip) is not detected after initializing the MODER register. Here's my code:Code:RCC->AHB1ENR |=(1<<0); //Enable GPIOA clockGPIOA->MODER =0; //clear MODER registerGPIOA->MODER=0X555; //PA0-PA5 AS OUTPUTRight after, I flash the board, I get the error that my blackpill is not detected ( see picture 1).The solution, I found to get my board working again is by keep pressing on BOOT0 switch and erasing the chip using stm link utility.The weird thing, if I comment the two last lines out in my code above and flash again, my board is still working fine ( see picture 2).I'm really lost, because the problem happens even with another blackpill I have.Does someone have an idea what's going on?Thanks in advance

author: ataradov
author_rank: Super Contributor
timestamp: August 04, 2022, 11:50:48 pm
content: This code disables SWD alternate function on the SWD pins. You can't clear or overwrite the whole register, you need to keep the values for the PA13/PA14 intact.

author: langwadt
author_rank: Super Contributor
timestamp: August 05, 2022, 12:00:02 am
content: the debug pins are also in PORTAtry changing only the bits controlling the pins you want to use, so you don't change the settings of the debug pins

author: kgavionics
author_rank: Regular Contributor
timestamp: August 05, 2022, 12:05:27 am
content: Thank you guys for your help, I really didn't think about the debug pins!

author: kgavionics
author_rank: Regular Contributor
timestamp: August 06, 2022, 12:28:59 pm
content: Just out of curiosity, are the alternate functions set by the PORTA reset value ? In my case, the reset value is 0x0C000000, and it's not appearing to set PA13 nor PA14 as alternate functions. Can someone shed some light on that please?

author: pcprogrammer
author_rank: Super Contributor
timestamp: August 06, 2022, 12:57:40 pm
content: Check the manuals here:https://www.st.com/en/microcontrollers-microprocessors/stm32f401.html#documentationThe reference manual on page 158 will shed some light on things, but it is a bit weird. Pin A13 is set to analog modeThe datasheet on page 44 does show that AF00 for PA13 and PA14 is for the SWD interfaceNeeds more investigationEdit1: Try to read the value of the MODER register with a debugger. Maybe the manual is wrong.Edit2: The reference manual for the F411 shows the reset value to be 0xA8000000 for port A, which makes more sense because that also enables the JTAG pin JTDI on PA15.

author: wek
author_rank: Frequent Contributor
timestamp: August 06, 2022, 01:48:56 pm
content: https://community.st.com/s/question/0D53W00001iJBVFSA4/stm32f401rm0368bad-gpioamoder-reset-value

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 06, 2022, 05:26:54 pm
content: Just connect nRST instead only SWD/SWC, that way the programmer will use hardware reset, will always work.Software reset can only work if the debugging pins are enabled, that's exactly the issue you're having by disabling those pins, breaks the debugging interface.

author: kgavionics
author_rank: Regular Contributor
timestamp: August 06, 2022, 09:16:37 pm
content: Quote from: DavidAlfa on August 06, 2022, 05:26:54 pmwe are not talking about the nRST here, we talk about the MODER register reset value!

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 06, 2022, 09:22:51 pm
content: What? In the original message he's talking about the MCU no longer being detected after modifying MODER, requiring to set boot0 to program it again.Connecting nRST will solve this, no matter the MODER value.

author: ataradov
author_rank: Super Contributor
timestamp: August 06, 2022, 09:23:32 pm
content: Quote from: kgavionics on August 06, 2022, 09:16:37 pmHaving nRST connected would eliminate your issue. You can write whatever you want into MODER and the debugger would be able to connect. Debugger will connect while nRST is asserted and the register value is default (whatever it is), making SWD pins be assigned their SWD function..

author: kgavionics
author_rank: Regular Contributor
timestamp: August 06, 2022, 11:34:55 pm
content: I tried to use the nRST on the stm link dongles , but for an unknown reason, they are not seem to work,that’s why I’m using the software reset.

author: ataradov
author_rank: Super Contributor
timestamp: August 07, 2022, 12:14:56 am
content: In Keil you may need to experiment with "Connect and Reset Options" in the dialog you show.I'm not sue how Keil behaves exactly here, but generally it is always possible to connect to the target if nRST is connected.

author: gamalot
author_rank: Super Contributor
timestamp: August 07, 2022, 02:06:54 am
content: A debug port with nRST connected is best practice, you never know when will you need it.I used to get some STM32 MCUs from Chinese sellers that looked brand new but were actually pre-programmed and locked, you couldn't erase and rewrite them without the nRST connected.