1

I am trying to read the MCU_ID (device electronic signature) from STM32L476 chip using a JTAG ST-Link/V2 on Windows 7. No code has to be uploaded inside the chip, the program shall just be launched on my cumputer and read this information from the flash memory.

I have managed to find and extract the following screenshot from the Reference Manuel given on ST website :

Device Electronic Signature Registre

So I have to read the value stored in the flash memory at the adess 0x1FFF7590 by using a C program. I am using the Atollic TrueStudio IDE which is recommended by ST itself, but it seems to me that it includes the "stm32l476xx.h"library which does not even contain any function which could help me.

What I have done so far

After spending days and days looking for some functions or examples to do something as simple as read flash memory, I have asked on this very site How to interact with a STM32 chip memory, which helped me understand a couple of things about what I had to do; nevertheless, I haven't been able to find what I was looking for even after days reading all the links and docs advised in the comments.

I have asked a couple of professionals who told me that I should search for a JTAG driver to interact with the flash memory, but it seems a bit complicated and I haven't been able to found any. Someone on this site told me that simply using pointer should be enough; the lack of C example and internet tutorials couldn't help me figure out how to do so.

Finally, I started recently digging around STM32Cube and HAL, even since I wanted to avoid using those because I thought that a simple read could be done without having to include those layers. Asking this question is my final hope before trying to use them.

In Conclusion :

I can't show any code since the only thing I have so far is a #include "stm32l476xx.h"and an empty main.
A hint or solution on How to read a STM32L476's flash memory in C would be just perfect. Every example of C (or any programming language which would be as low or higher level) program or instructions interacting with a STM32 chip's memory could help me a lot since it is very hard to find on internet.

Community
  • 1
  • 1
Badda
  • 1,329
  • 2
  • 15
  • 40
  • Connect with JTAG/SWD to STM32 and go to debug. If you have ST-Link, use ST-Link utility and connect to device. You will be able to choose which memory address you want to read. – unalignedmemoryaccess May 10 '17 at 08:15
  • 1
    Why do you want to write your own program rather than using an existing tool such as [OpenOCD](http://openocd.org/) – Brian Sidebotham May 10 '17 at 08:31
  • @tilz0R I need a program which could do this automatically. I know how to read with the JTAG application, but it is not very helpful in my case. – Badda May 10 '17 at 08:44
  • @BrianSidebotham I did not know it existed, and that is why I asked the question. After quickly reading the documentation, it looks very interesting. However, even if I manage to make C calls to OpenOCD, wouldn't it require a lot of ressouces to use this application instead of a simplier code ? Considering the fact that my program might be used on a lot of chips in a industrial context. – Badda May 10 '17 at 08:44
  • 1
    I don't understand the actual point. If you have know address where device signature is then you can do this: `uint16_t sign = *(uint16_t *)0x1FFF7590;` and you have signature of device. – unalignedmemoryaccess May 10 '17 at 08:45
  • To read the value via JTAG implies code running on the development host, not the target, so the target compiler you are using is irrelevant. You need an API for the JTAG interface and a compiler for your PC, not the target - OpenOCD perhaps for the API, the compiler is up to you and the PC OS you are using perhaps. – Clifford May 10 '17 at 09:04
  • 1
    The production-friendly way would probably be to 1) either have the MCU program spit out the data on your favourite flavour of serial bus, or 2) download a dummy program which does nothing but spitting out the data, then overwrite it with the actual firmware later. – Lundin May 10 '17 at 09:06
  • @Clifford If I have understood correctly your comment, AtollicStudio helps debugging firmware, but since I am not trying to develop a firmware it isn't revelant ? I would just need an API to communicate with the chip via the JTAG, since it cannot be done just by using C ? AtollicStudio seems to use correctly the JTAG interface, so wouldn't it be helpful anyway ? – Badda May 10 '17 at 09:22
  • @Lundin The problem is I am not looking for any firmware; MCU should be exactly the same before and after reading the device signature, and it does not seem very relevant to write then erase a firmware just to "spit out" data, does it ? – Badda May 10 '17 at 09:26
  • 1
    It is common practice to load a test firmware out of factory (pre-flashed MCU) and then overwrite it with the actual application later. But since you are not looking for firmware or programming advise, it means that the question is nothing but a tool recommendation, which is explicitly off-topic for this site. – Lundin May 10 '17 at 10:00
  • One could also link and upload a test firmware into the controller RAM, run it from there by setting the boot pins appropriately, then forget it. – followed Monica to Codidact May 10 '17 at 10:53
  • 2
    The debugger/programmer in Atollic Studio is a program running on your PC that communicates with the JTAG interface; so of course you can write your own program to do that; however you have to know how to talk to the JTAG, and I suggested OpenOCD as a generic means to do that with a wide variety of JTAG devices. Your JTAG vendor may provide their own API; there may even be a command line interface to the Atollic software that you could use (I am not familiar with it so I cannot advise). – Clifford May 10 '17 at 11:00
  • 1
    If you want a useful answer you need to specify the JTAG hardware you are using and the OS platform you want this to work on. Not all JTAG devices work in the same way or use the same interfaces (USB, parallel port, Ethernet etc.) All the redundant information about your target toolchain and code loaded and executed on the target should be removed - that is not what you are asking for and serves only to confuse. – Clifford May 10 '17 at 11:09
  • @Clifford: The JTAG hardware is ST-Link/V2, that's stated in the first sentence. – followed Monica to Codidact May 10 '17 at 11:35
  • @Lundin Thanks for the information test firmwares, I am starting to consider this option then. I am not looking for a tool recommendation btw, it's just that I need to do what you advised me to do with a firmware but without a firmware. Advising me APIs helps but it is likely that I shall found out how to read the flash memory in the source code of those APIs – Badda May 10 '17 at 11:53

3 Answers3

2

Reading MCU ID using ST-Link (graphical interface)

You can use ST-Link Utility (can be downloaded from ST.com here: http://www.st.com/en/embedded-software/stsw-link004.html). After you do Target->Connect you can specify the address and number of bytes you want to read on top of the Window. This also works for the memory area where MCU ID is defined.

For STM32L476 MCU that you use it's going to be memory address 0x1FFF7590, size 0xC (96 bits). Pressing enter should allow you to see the unique ID read from the MCU you're connected to, in form of 3x32 bit values.

Reading MCU ID using ST-Link (command line interface)

ST-Link Utility provides CLI (command line interface) to do the most common operations. This is done using ST-LINK_CLI.exe located in your ST-Link Utility installation directory.

Reading Unique ID as 32-bit values (STM32L476 MCU from the example):

ST-LINK_CLI.exe -r32 0x1FFF7590 0xC

Reading as 8-bit values:

ST-LINK_CLI.exe -r8 0x1FFF7590 0xC

You can also read it to file using -Dump parameter:

ST-LINK_CLI.exe -Dump 0x1FFF7590 0xC D:\temp\out.bin

Keep in mind that you must have the priviledges to write to the destination directory. If you don't run the command prompt with administrative priviledges, in most common cases this means that you won't be able to create the file in locations such as root drive directory (C:\out.bin) or inside "Program Files", where most likely your program is installed (for example by specifying a relative path, such as giving an output file name only out.bin). The program sadly doesn't inform about failed attempts to write the file, however it does say when it succeeds to create the file. The program execution should end with a green line saying Dumping memory to D:\temp\out.bin succeded. In addition, keep in mind that only the following file extensions are supported: *.bin *.hex *.srec *.s19. It cannot be anything because the extension determines the format in which the data will be written to the file.

You can find more information about CLI parameters in User Manual UM0892.

Reading MCU ID using C code

The same can be done using a program loaded into the MCU. You read it by simply accessing the memory directly. Sample code:

#define STM32_UNIQUEID_ADDR 0x1FFF7590
uint32_t id[3];
id[0] = *(STM32_UNIQUEID_ADDR + 0);
id[1] = *(STM32_UNIQUEID_ADDR + 1);
id[2] = *(STM32_UNIQUEID_ADDR + 2);

After this operation id array should contain the same 3x32bit values you've previously read using ST-Link Utility. You may of course choose to read it as uint8_t byte array of size 12, you may even choose to read it into a struct, in case you're interested in the details (lot number, wafer number etc.). This example should however give you a general idea of how to access this value.

J_S
  • 2,985
  • 3
  • 15
  • 38
  • Let's say I do load this code into MCU. How then can I use id[] ? I mean, even if I could print it in a console, I couldn't use that variable in any way, could I ? Is it possible to kind of return the valor of this variable from the program and then use it in a C non-embedded program ? – Badda May 11 '17 at 09:13
  • 1
    If your final goal is reading it back to your PC, such as automating some producion process, you can use the ST-Link CLI (command line interface) to acheive the same thing that was done with ST-Link Utility's graphical interface. I've edited my post above with an example. – J_S May 11 '17 at 09:45
  • For that just use the command parameter of the ST Link CLI that does exactly that: -Dump Description: Reads target memory and save it in a file Syntax: -Dump
    . Again, please refer to the UM0892 for more details about the options you have.
    – J_S May 11 '17 at 14:54
  • That's what I have been using but I can't seem to find the created file. Something in the command line must be wrong.. What should look like ? Also, I thank you a lot for providing these solutions; I suggest you add, if you want, the -Dump command line solution into your answer since it is exactly the perfect answer to my question, and people might come here looking for a quick fix or hint. – Badda May 11 '17 at 15:21
  • 1
    Probably lacking write priviledges - either run command prompt with administrative priviledges or write it to a location you know you can create files in. More information in the (edited) answer. – J_S May 11 '17 at 15:52
  • You're the best. – Badda May 12 '17 at 06:46
0

There is Texane stlink, that does what you want. It's written in C, interacts with STM32 chips through an ST-Link adapter, and it can read from chip memory.

  • It seems to be an API like OpenOCD ? Not the ideal solution I am looking for, but could be useful. Thanks. – Badda May 10 '17 at 09:18
  • 1
    You can examine the C source code to see how it's accessing controller memory. Is it not what you want? What are you looking for then, if it's not ideal? – followed Monica to Codidact May 10 '17 at 09:36
  • Well yeah, the API itself is not what I am looking for, however examining the source code could literally give me the perfect answer. More thanks for reminding me that. – Badda May 10 '17 at 09:42
  • @Badda : Your C code needs to know how to talk to teh JTAG device, so what is your concern about an API to allow it to do that - any device needs a device driver; if you want to access a JTAG device directly you will first have to know exactly how it works and write a device driver - Neither Windows nor Linux will just let you access the device directly with "user level" code. – Clifford May 10 '17 at 11:03
  • @Clifford: The code I've linked is a nice counterexample, as it works with the system-supplied generic USB drivers, at least on Linux. – followed Monica to Codidact May 10 '17 at 11:21
  • Well I am curious to know who is right. OpenOCD seems to be an easier solution, but using the Texane source code would even be better. – Badda May 10 '17 at 12:16
  • @berendi I have just realised that it was for Linux and I am on Windows. I should have said it before, my bad. – Badda May 10 '17 at 13:27
  • @berendi : My comment was aimed at Badda regarding is aversion to any kind of "API" or third-party driver or debug server; not to your suggestion. – Clifford May 10 '17 at 13:54
  • 1
    @Badda : Take a look at the source first before you decide that - they are both open source, OpenOCD is a gdbserver implementation for a wide number of JTAG devices, the Texane software is specific to ST-Link. – Clifford May 10 '17 at 13:58
  • @Badda: both of them run on Windows too – followed Monica to Codidact May 11 '17 at 10:04
0

What you are looking for is not a feature of ST but a feature of ARM.
Remember, ST simply uses an ARM core. I know most programmers load some code in RAM and use that to access flash. You can find these simple programs in the install directory or Keil for example.

I think this is the manual you will need. But I don't know if there is more information behind the login-wall.

Jeroen3
  • 919
  • 5
  • 20