Saturday, December 29, 2012

STM32F3 Discovery on Windows with Eclipse and OpenOCD

Hi!

Because of a lot of requests, I have decided to make the Windows version of the tutorial for setting up STM32F3-Discovery board, with free tools.

First, download ST-LINK V2 driver from here. Open the archive and install what is in it.
Then plug the device and let the driver install.
Now, get the latest OpenOCD installer from here and extract it somewhere. Copy <openocd_path>\scripts\board\stm32f3discovery.cfg to <openocd_path>\bin folder
Now you should be able to connect to the board in Command Prompt like this:
D:\embedded\openocd-0.7.0-dev-121112115725\bin>openocd-0.7.0-dev-121112115725.ex
e -f stm32f3discovery.cfg
Open On-Chip Debugger 0.7.0-dev-00079-g08ddb19 (2012-11-12-17:14)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.sourceforge.net/doc/doxygen/bugs.html
adapter speed: 1000 kHz
srst_only separate srst_nogate srst_open_drain
Info : clock speed 1000 kHz
Info : stm32f3x.cpu: hardware has 6 breakpoints, 4 watchpoints
Now close the Command Prompt.

Download latest Eclipse.
Go to Eclipse download page and download Eclipse IDE for C/C++ developers. Extract it and start eclipse.exe. Go to Workbench.

Set up Eclipse.
Go to Help->Install New Software, select All Available Sites in Work With dropdown list. Enter "GDB Hardware Debugging" in the search box and install the package. Download Arm Eclipse Plugin from here and then go to Help->Install New Software in Eclipse. Click Add and then click archive and select the previous downloaded file. Then select the packet and install it.

Install GCC Arm Embedded toolchain from here.

Now we need some Linux tools like make and rm for Windows. Download Cygwin from here. Select whichever mirror you want. Search for "make" and select the binary from Devel dropdown. Click next and wait for it to install.
Now, download this project and import it in Eclipse Workspace. Right click project and select  Properties->C/C++ Build->Environment. Here, check if PATH variable has link to toolchain and to Cygwin binaries folders. Here, you can add <gcc-arm-none-eabi-4_6-2012q4/arm-none-eabi/include> path for the Eclipse IDE to find some headers like stdint.h.
If everything  was installed correctly you should be able to clean the project and to build it.

With the project selected, click Run->External Tools->External Tools Configurations and select from the left the configuration named OpenOCD(win). Check if the path for OpenOCD at top is correct.
Now, click Run.

After this, click Run->Debug Configurations and select from the left Navigation-debug. In the Debugger tab check if the arm-none-eabi-gdb.exe path is correct. Now click Debug and you should see the Debug Window from Eclipse and the Program counter pointing at the first instruction from main().

42 comments:

  1. Andrei,
    Thank you much for this, best tutorial out there, hands down.

    ReplyDelete
  2. Hi,

    Thanks very much for the tutorial, is there a way to use it without Eclipse and OpenOCD, I mean directly like here: http://hertaville.com/the-stm32f0discovery-board/

    but this is for F0 boards....

    Am new to MakeFiles and Linkers, hence asking... ?

    Thanks for the tutorial though.

    ReplyDelete
    Replies
    1. Hi!

      I don't understand what do you mean. In that tutorial, it is used OpenOCD and also Eclipse. You need something to interract with the STLINK hardware debugger(OpenOCD or ST-Util). I have used OpenOCD because, at the time of writing the tutorial, st-util(from texane) didn't have support for STM32F3 chips.
      If you want to replace Eclipse, you can use arm-none-eabi-gdb or arm-none-eabi-gdbtui from the toolchain, in the command line, for debugging, flashing. You can take the commands from the Debug window(Debug Configuration) in the Eclipse project.

      Regards

      Delete
  3. Many thanks for this Andrei (and for dropping a note on my blog so that I found these instructions for Windows).

    It seems like you stopped just as it was getting interesting (at that main() breakpoint). I was hoping to see the printf output in an Eclipse debug console, but nothing comes up. Do I need to be running another tool to monitor for output?

    --
    Chris

    ReplyDelete
    Replies
    1. Hi Chris!
      in order to see the output of the printf you have to use the USART2 pins(PA2 tx and PA3 rx). You have to connect them to a ttl to uart convertor and use Putty or any serial port monitor to see the messages.

      Regards,
      Andrei

      Delete
  4. Hi, done your tutorial, but i have error:

    symbol-file C:\\Users\\HP\\workspace\\Navigation\\Navigation.elf
    load C:\\Users\\HP\\workspace\\Navigation\\Navigation.elf
    You can't do that when your target is `None'
    tbreak main
    Cannot access memory at address 0x8002730
    continue
    The program is not being run.
    kill
    The program is not being run.


    What it could be ?

    ReplyDelete
    Replies
    1. Hi!
      Can you please send me(andrei.istodorescu@gmail.com) an archive with your project?
      It is very difficult to debug like this.

      Andrei

      Delete
    2. Thanks for reply, Andrei
      But i'm trying to run your project that i downloaded from this page. It builds just fine, but i cant run\debug it. Seems i've made a mistake somewhere. Tryed to reconfigure eclipse twice, but result is the same.

      Delete
    3. windwail:
      exactly the same crap goin' on here. I suppose it's an openocd bug (maybe expects older STLink driver or does not like my XP with HUN lang), but I have no time and patience to puzzle out this usual 'opensource suxx' so a possible solution that helped me.
      Start manually the openocd from cmd.exe:
      openocd-0.8.0-dev-130717143429.exe -f stm32f3discovery.cfg -c init -c"reset init" -c"poll"
      then start debug from eclipse as Andrei wrote and gdb will work (works for me anyway with terrible low speed).

      My error message in eclipes is:
      -------------
      symbol-file D:\\STM32_Discovery\\eclipse\\workspace\\Navigation\\Navigation.elf
      load D:\\STM32_Discovery\\eclipse\\workspace\\Navigation\\Navigation.elf
      You can't do that when your target is `None'
      tbreak main
      Cannot access memory at address 0x8002368
      continue
      The program is not being run.
      ---------

      One more observation in my case the openocd prints this strange info:
      ---------------
      Info : This adapter doesn't support configurable speed <<< WTF??!?!?!
      Info : STLINK v2 JTAG v17 API v2 SWIM v0 VID 0x0483 PID 0x3748
      Info : Target voltage: 2.893034
      Info : stm32f3x.cpu: hardware has 6 breakpoints, 4 watchpoints
      etc...
      ----------
      good luck:
      Balázs

      Delete
    4. Hi Balazs,

      I got exactly the same problem as yours and did the same thing you suggested and it works! Thanks.
      However, have you figure out the real cause among OpenOCD, ST-link and Eclipse?

      Ted

      Delete
    5. Hi there,

      I think I found the problem. You should assign full path in argument for -f
      such as -f D:\STM32\openocd-0.9.0-dev-swd-140623233947\bin-x64\stm32f3discovery.cfg

      Delete
  5. Open On-Chip Debugger 0.7.0-dev-00079-g08ddb19 (2012-11-12-17:18)
    Licensed under GNU GPL v2
    For bug reports, read
    http://openocd.sourceforge.net/doc/doxygen/bugs.html
    adapter speed: 1000 kHz
    srst_only separate srst_nogate srst_open_drain
    Info : clock speed 1000 kHz
    libusbx: info [cache_config_descriptors] could not access configuration descriptor (dummy) for '\\.\USB#VID_138A&PID_0018#5903E97C682E': [31] Присоединенное к системе устройство не работает.
    (Device that connetced to system doesnt work)
    libusbx: error [winusbx_open] could not open device \\.\USB#VID_0483&PID_3748#5&1F0AE993&0&1#{A5DCBF10-6530-11D2-901F-00C04FB951ED} (interface 0): [5] Отказано в доступе. (Access denied)
    Error: open failed
    in procedure 'transport'
    in procedure 'init'

    ReplyDelete
    Replies
    1. im on windows 7, tryed to disable UAC, nothing changed... may be some problem with driver (IAR Embedded Workbench works fine)

      Delete
    2. It seems that there is a problem of communication between openocd and stlink. Maybe you should try to popst this issue on openocd forums (https://forum.sparkfun.com/viewforum.php?f=18).

      Regards,
      Andrei

      Delete
  6. I've a problem with building the elf or hex in eclipse.
    When I try it (Click Build Project in Eclipse) I get an error

    "makefile:48: *** unterminated variable reference. Stop."

    Here is an explanation of the problem

    http://sunsite.ualberta.ca/Documentation/Gnu/make-3.79/html_chapter/make_16.html

    but I don't know what I can do next to fix it.

    I've environment setup for STM32F1 and everything works.
    Is there any difference in the Toolchain for STM32F3?

    I need to build a elf or hex without modified makefile (I am using the CodeSourcery makefile automatically) and upload file by OpenOCD (.bat)

    More than that, OpenOCD version 0.6.1 supports built STLik V2. Try to type in CMD

    openocd-x64-0.6.1-f interface/jtagkey.cfg
    -f target/stm32f1x.cfg

    and that will connect you to the STM32F3. If it works, it should not be a problem with the upload elf or hex. I didn't upload hex like this yet with STM32F3, but with F1 works without a problem (of course you need to change the interface and the target).

    But well before that needs to build elf or hex without error in makefile.

    ReplyDelete
    Replies
    1. Please replace you Makefile with the one from my archives. Maybe you have changed something in the original Makefile.
      Or, you can send me your Makefile to take a look and see what is the problem.

      Regards,
      Andrei

      Delete
    2. I fix the problem already. I written wrong the path to the linker. Must be:

      ${workspace_loc:/${ProjName}/xxxxx/stm32f30x_flash.ld}

      xxxxx - name folder where is a linker file in workspace.

      In the path was missing parentheses.

      Now, when I click on Build Project, I get files .elf and .hex of project in folder Debug.

      BTW I've a question. After click Build Project I get folder Debug and there is the many files (i.a. .hex, .elf, makefile etc.) and I get Binaries. And I all time upload like that configured hex to STM32F1 and my program works like I want. So the question is - that configured hex file is correct for the STM32F1, STM32F3 or other WITHOUT click Debug function (Run->Debug as) in eclipse?

      Delete
    3. I don't understand what do you mean.
      The elf file generated by this project is for STM32F3 MCU. You should be able to flash this elf or hex to the board with other tools, like the utility tool from ST.

      Andrei

      Delete
    4. Yes, I know it. To load program I using by OpenOCD (OpenOCD + telnet command in file .bat) but many people use extra debugging configurations (GDB Hardware Debugging) like You in your tutorial and before to load program (Run as) click Debug as (choose defined configuration) and then open Debug Window. I don't make it like that. I don't use method with modification makefile. I use Built Project and default settings of debugging from Codesoucery and I wonder that .hex file is debug correct? Maybe I don't good understand what is going on when I click Build Project. Files are debugging correct or just I generated file hex what somehow works when I load it by OpenOCD to my STM32.

      I would to know if I'm doing it correctly ;)

      Delete
  7. Hi Andrei,

    Bought one of these board for a hobby project (still can't believe how cheap!) as a step up from PIC uc's. Had never delved into the world of ARM before so needed my hand held through the initial steps.

    Your tutorials were unbelievable and far and away the best I have found on the web for this board. Many thanks for all your hard work!

    Now if only we can work out a way to email beer...

    ReplyDelete
    Replies
    1. Hi Tim!

      Thank you for your appreciation!

      Good luck with your projects!

      Andrei

      Delete
  8. Link to USB driver for ST-LINK V2 has changed. See http://www.st.com/web/en/catalog/tools/PF258167

    ReplyDelete
  9. Many thanks for this tutorial - F3 Discovery worked "out of the box", and it was straightforward to make the small modifications to get a working F4 template.

    ReplyDelete
  10. hi andrei
    i hope u can help with this problem, i don't understand why it's not working.
    \openocd-0.7.0-rc2\bin>openocd-0.7.0-rc2.exe -f stm32f3discovery.cfg
    Open On-Chip Debugger 0.7.0-rc2 (2013-04-28-12:55)
    Licensed under GNU GPL v2
    For bug reports, read
    http://openocd.sourceforge.net/doc/doxygen/bugs.html
    srst_only separate srst_nogate srst_open_drain connect_deassert_srst
    Info : This adapter doesn't support configurable speed
    Error: open failed
    in procedure 'transport'
    in procedure 'init'

    ReplyDelete
    Replies
    1. I am having the same issue right now. If you have found a solution, I'd love to hear it.

      Delete
    2. Hi!

      I would suggest you to use 0.7.0-dev version of OpenOCD, because this one worked for me. Maybe it is a bug in the rc2 version.
      You can also try to ask a question on the OpenOCD mailing list: http://openocd.sourceforge.net/discussion/mailing-lists/
      I am sorry but I don't know what to say more regarding this issue.

      Best Regards,
      Andrei

      Delete
    3. I had this same exact problem just today.

      Turns out it was the USB port on my computer.

      At first I used the USB hub in my monitor. My PC could recognise the F3 board, it could retrieve the VID and PID, but it would not proceed any further. I tried the ST-Link application and I could read the flash, but I couldn't not erase the flash or upgrade the firmware. It was half working. I tried a different computer and had no problems.

      So I changed to a USB port that was directly on the motherboard, and the problems went away.

      Delete
  11. Hi Andrei,

    Thanks so much for making this tutorial! It's helped me get really close to a working eclipse with the f3 discovery board.

    I'm currently getting an error stating " "arm-none-eabi-gcc" not found in PATH "
    I've tried including this specific file in the environment PATH as well as in my windows PATH, as well as trying the bin and include folders from the parent folder. Do you know what I could be doing wrong? Thanks for the help.

    ReplyDelete
    Replies
    1. I figured out what was the issue. I had a previous copy of GCC installed and, somehow, the PATH followed both of them, so I had an incorrect link to the file arm-none-eabi-gcc. Thanks for the tutorial!

      Delete
    2. Great!

      I am glad you have figured it out.
      :-)

      Delete
  12. So i followed your guide and have run into 2 problems.
    When i try the cmd prompt i get this
    Open On-Chip Debugger 0.7.0 (2013-05-05-10:41)
    Licensed under GNU GPL v2
    For bug reports, read
    http://openocd.sourceforge.net/doc/doxygen/bugs.html
    Runtime Error: embedded:startup.tcl:47: Can't find stm32f3discovery.cfg
    in procedure 'script'
    at file "embedded:startup.tcl", line 47

    The file is in the folder like it should be but it just wont work.

    And second in Eclipse i have the error "arm-none-eabi-gcc" not found in PATH "

    I am very new to using arm and st devices, for most my life it was arduino 1 simple program.
    So im not sure what i did wrong.
    Here is my path
    C:\cygwin\bin;C:\Program Files (x86)\GNU Tools ARM Embedded\4.7 2013q2\arm-none-eabi\bin
    Im thinking its missing something let me know.
    As i would love to be able to use my F3 board

    ReplyDelete
    Replies
    1. Hi, i had also problems to connect the board to my computer including the message "Can't find stm32f3discovery.cfg". Using the version openocd-0.7.0-dev-130127142934 i got some error with libusb.
      Then i followed the instructions in
      openocd-0.7.0\drivers\libusb-1.0 drivers.txt
      Now everything works fine with openocd-0.7.0 and with openocd-0.7.0-dev-130127142934.

      Delete
  13. I managed to do all the build and debug part but when i try to run the project i get error
    'cmd' is not recognized as an internal or external command,
    operable program or batch file.
    can someone help me out how can i fix this?
    thx

    ReplyDelete
    Replies
    1. Hi!

      You said: debug is working; do you mean that you can step over instructions?
      By running the project you mean Eclipse Run configuration?

      Delete
    2. yes, when i start the run configuration then run it wont start

      Delete
    3. I haven't set up the run configuration.
      You have to set it up if you really need it.

      Delete
    4. could u help me with it, im not very into this so i probably wont be able to do it alone
      thx

      Delete
  14. Works perfectly, thank you very much!

    ReplyDelete
  15. Hi Andru,

    Very glad that you also have a tutorial for Windows users. Thanks a lot.
    Following the instructions without major problems. Only struggling a while with the PATH settings at Windows environment.

    A problem occurred after setting Run->External Tools->External Tool Configurations and press "Run" button.
    The Location was set to the bin-x64 directory of Openocd
    and working directory was set to where your project "Navigation" was unzipped.

    Below are messages in console window:

    Open On-Chip Debugger 0.9.0-dev-00067-g2502d8a (2014-06-26-08:30)
    Licensed under GNU GPL v2
    For bug reports, read
    http://openocd.sourceforge.net/doc/doxygen/bugs.html
    srst_only separate srst_nogate srst_open_drain connect_deassert_srst
    Info : This adapter doesn't support configurable speed
    Info : STLINK v2 JTAG v16 API v2 SWIM v0 VID 0x0483 PID 0x3748
    Info : using stlink api v2
    Info : Target voltage: 2.920310
    Info : stm32f3x.cpu: hardware has 6 breakpoints, 4 watchpoints
    target state: halted
    target halted due to debug-request, current mode: Thread
    xPSR: 0x01000000 pc: 0x080040b4 msp: 0x20000648
    background polling: on
    TAP: stm32f3x.cpu (enabled)
    target state: halted
    target halted due to debug-request, current mode: Thread
    xPSR: 0x01000000 pc: 0x080040b4 msp: 0x20000648

    Can you tell where I did wrong?

    ReplyDelete
  16. I am having the exact same problem on 64bit Linux. Did you ever get to fix that problem?

    target state: halted
    target halted due to debug-request, current mode: Thread
    xPSR: 0x01000000 pc: 0x080007fc msp: 0x2000a000
    background polling: on
    TAP: stm32f3x.cpu (enabled)
    target state: halted
    target halted due to debug-request, current mode: Thread
    xPSR: 0x01000000 pc: 0x080007fc msp: 0x2000a000

    Thanks

    ReplyDelete
  17. Could you update the link to the sample project please. The link to sample project states that "The project andrei-development was not found". Thanks.

    ReplyDelete