It’s a lot of fun writing code on your Discovery board with its myriad of peripherals to explore and exposed headers for further experimentation. Nothing, however, beats the satisfaction to be had from creating and coding your own board and there comes a time when every red blooded geek must strike out on their own!
I’ve recently been presented with an opportunity to create a new board that required a lot more grunt than the PIC based boards I normally deal with. We needed a much faster processor, an FPU and some DSP capability. As a result I opted to use the STM32F405RG as the MCU. The next thing I wanted to do was to mash up a prototype but I needed a quick and easy way to program it. I remembered that the Discovery board has an ST-LINK section with an SWD interface broken out onto headers so I thought I’d give it a go.
Setting up your Discovery to use it as a programmer is a very straightforward affair. The ST-LINK section is the top third of the board and can be easily isolated by removing the two CN3 jumpers shown above.
You’re also clearly going to need to expose the SWD interface on your board so you have something to connect the ST-LINK to. The schematic below shows how I did it.
A couple of things to note about the circuit above. I’ve pulled BOOT0 low which means that I’m programming flash memory. If you want to program other areas of memory then you’ll want to be able to change the state of this and the BOOT1 pin which is multiplexed with PB2 on the STM32F40x parts. Details can be found in section 2.4 of the reference manual.
The other thing I did was to expose the additional JTAG interface pins also in order to give me several programming and debugging options. Finally, I connected the NRST pin on the ST-LINK to the NJRST pin on my STM32. That may be wrong, you might be better off connecting it to the NRST pin on the STM32. Perhaps someone out there can let me know in the comments below.
The SWD interface is connected as follows:
I chose to expose the interface as a row of 0.1” pitch header and connected the two boards together with female to female patch leads.
The next step is to try and get some code running on the device. I’m using Atollic TrueSTUDIO Lite for this but I’m sure the process is pretty much the same for other IDEs too.
Project Creation Steps
To get up a program up and running on the new board you need to create a new project that targets the STM32F405 or whichever STM32 part you’re using. This will generate a new, compilable and runnable project. If you run the program up now in the debugger you should be able to step through the code, look at variable values and memory content. This shows that everything is working fine.
The final step is to sort out your clocks. TrueSTUDIO will create a default system_stm32f4xx.c file containing a clock configuration for you but unless you happen to be using a 25MHz crystal you’ll want to change it for one targeting your own board. Mine, for example, uses a 4MHz external clock.
The easiest way to do this (as far as I’m aware at least) is to use the clock configuration tool provided by STMicroelectronics for the purpose. You can download the tool and the accompanying notes from their website. I used the simple wizard mode, followed the instructions very carefully and where I wasn’t sure of a value, I looked it up in the original file created by TrueSTUDIO. Once the new system_stm32f4xx.c is generated, it needs to replace the one in your project. Simply re-build the project, make sure you can still launch and debug it and you’re up and running.
So I’ve been running with this setup for a couple of weeks now with very few problems. I did get a bit of a scare when I saw smoke pouring out of the Discovery’s USB port once but I think that was a pretty freak incident as it hasn’t happened since. I’ve had the odd unexplained debugger disconnection but given the amount of myopic prodding I tend to do with scope probes that could be more about what I’m doing than the what the debugger is doing. All in all I think it provides quite a good option to anyone wishing to experiment with creating their own STM32 boards without forking out for new kit.