Click here to Skip to main content
13,763,747 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

77.1K views
3.2K downloads
24 bookmarked
Posted 19 Feb 2013
Licenced CPOL

ST Micro-electronics Device Firmware Upgrade (DFU/DfuSe) from C#

, 12 Mar 2013
Rate this:
Please Sign up or sign in to vote.
ST provide a DFU module for their STM32 micro-controllers for firmware upgrade over USB. This is an example of how to use it from C#.

Introduction

ST Micro-electronics STM32F series micro-controllers have the possibility to be programmed via USB using ST's DFU protocol. Some of their micros have the DFU module in ROM and all can have it in FLASH. DFU is a very handy way to program a micro-controller, particularly when it is in the micro's ROM, but all the example code from ST is in C++ and is somewhat complicated and buggy. ST's version of DFU is called DfuSe (Device firmware upgrade STMicroelectronics Extension).

Using this code, I have been able to implement a scheme whereby the C# application which is communicating with the ST micro-controller based hardware, is able to automatically update the hardware's firmware. So when the C# application is updated, the firmware gets updated too.

Background

ST provides a C++ demo application, which includes the Windows drivers for the DFU device and two DLLs which this application requires (STDFU.dll, STTubeDevice30.dll). I'm not sure of the redistribution terms of the DLLs so you should download them directly from ST by downloading and installing ST's demo package (see below). Then copy the two DLLs into the main solution directory, from where they will be copied into the appropriate bin directory when the solution is built.

ST provides a STDFU library specification (UM0384) and file format document (UM0391) as part of the demo package.

Some of the USB communications has been borrowed from an excellent article on USB HID programming that I found here.

ST's application uses another two DLLs, STDFUFiles.dll and STDFUPRT.dll. These perform higher level functions which are either not performed by my example, or done by the C#. I just wanted to write as much of the code in C# as possible, and minimise time spent trying to decode DLL function calls. I also haven't bothered to implement any more functionality than the bare minimum I needed, but I hope this will provide a starting point for people wanting to do more.

DFU files can be generated from HEX or S19 files by an application which is included in ST's demo package. So you can take the HEX file output from your compiler (e.g., Keil's MDK-ARM) and convert it into a DFU file which can then be programmed into a micro-controller using this application. Normally, this is something you would do with released code, rather than during software development. Using the DFU avoids the need for the production department or service engineer to have a JTAG programmer, and the need to directly access the target hardware as long as the USB port is accessible.

Using the Code

The supplied code is a demo application plus my FirmwareUpdate class which does the hard work of communicating with the hardware. My intention is more that it provides an example of how to implement DFU programming in your own test harness than that it be used standalone. I don't claim to be an expert in USB or P/Invoke, what I am presenting has been patched together by running the C++ demo under a debugger and experimentation.

IFirmwareUpdate is of course an interface class to the FirmwareUpdate class. To do a firmware update, first hook the OnFirmwareUpdateProgress event to an event handler, then call UpdateFirmware with the path of the DFU file. You make like to use ParseDFU_File beforehand to obtain the intended VID and PID of the DFU file so you can check that they are correct for the target hardware.

This code has been tested against a STM32F207's ROM bootloader, a STM32F103 running a FLASH DFU module (as copied from ST's example) in the bottom $3000 bytes to load an application into the FLASH from $08003000 onwards, and another STM32F103 running a slightly different version of ST's DFU example. I have built the project both "x86" and "x64". You will need to install the correct version of the DFU driver, as supplied in the form of an installer EXE file in ST's demo package.

You may notice a function called FindAndDetachHID. This is for use with a HID device (Human Interface Device, such as keyboard or joystick, but also one of the easiest USB classes to use for any kind of low data rate I/O device) which supports the "HID detach" feature. This is basically a special message telling the device to stop being whatever it is, disconnect from the computer, and then re-connect as a DFU device. ST's demo app supports this feature.

History

  • 19/2/2013: First posted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mark McLean (ExpElec)
Engineer Expedition Electrics
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionDFU Read File Failed Pin
AndyW9996-Nov-18 4:41
memberAndyW9996-Nov-18 4:41 
QuestionNot possible to read DLL-filen STDFU.dll Pin
Member 119034472-Sep-18 20:52
memberMember 119034472-Sep-18 20:52 
QuestionAbout STDFU Error Code 0 Pin
Member 1390698831-Jul-18 4:37
memberMember 1390698831-Jul-18 4:37 
QuestionAbout Com Port Selection Pin
Member 1390698831-Jul-18 3:41
memberMember 1390698831-Jul-18 3:41 
QuestionWrong Input String exception Pin
Member 138698986-Jul-18 2:59
memberMember 138698986-Jul-18 2:59 
QuestionFYI _parentHandle is not defined and causes crash if device is HID Pin
Roger Clark Melbourne23-May-18 21:53
memberRoger Clark Melbourne23-May-18 21:53 
QuestionTweaked and published some NuGet packages Pin
Member 1383414222-May-18 8:36
memberMember 1383414222-May-18 8:36 
GeneralMy vote of 5 Pin
Member 1342871625-Oct-17 5:35
memberMember 1342871625-Oct-17 5:35 
QuestionDownload Failing (STM32F072) Pin
Chris Jameson-Groves3-Oct-17 7:23
memberChris Jameson-Groves3-Oct-17 7:23 
AnswerRe: Download Failing (STM32F072) Pin
Chris Jameson-Groves4-Oct-17 2:57
memberChris Jameson-Groves4-Oct-17 2:57 
QuestionHas anyone tested this on the STM32F4 series chip? Pin
Jeremy Vance12-Aug-16 6:18
memberJeremy Vance12-Aug-16 6:18 
AnswerRe: Has anyone tested this on the STM32F4 series chip? Pin
Member 127129061-Sep-16 6:12
memberMember 127129061-Sep-16 6:12 
AnswerRe: Has anyone tested this on the STM32F4 series chip? Pin
rbelivan7-Sep-16 4:19
memberrbelivan7-Sep-16 4:19 
AnswerRe: Has anyone tested this on the STM32F4 series chip? Pin
NikolaB29-Jan-17 22:41
memberNikolaB29-Jan-17 22:41 
QuestionCode Verification Pin
rbelivan15-Jun-16 15:18
memberrbelivan15-Jun-16 15:18 
AnswerRe: Code Verification Pin
rbelivan7-Sep-16 7:06
memberrbelivan7-Sep-16 7:06 
GeneralThank you! Pin
kotofalk26-Mar-16 3:24
memberkotofalk26-Mar-16 3:24 
QuestionThank You Pin
Denziljr5-Nov-15 23:02
memberDenziljr5-Nov-15 23:02 
SuggestionRead Protection Bit Pin
stukdev2-Nov-15 22:50
memberstukdev2-Nov-15 22:50 
QuestionNeeded C# application for .hex file conversion into DFU(.dfu) file. Pin
Vinod KUmar S N5-Dec-14 0:20
memberVinod KUmar S N5-Dec-14 0:20 
AnswerRe: Needed C# application for .hex file conversion into DFU(.dfu) file. Pin
vivchun8-Apr-15 14:49
membervivchun8-Apr-15 14:49 
Questionwhat IDE to use Pin
Member 112871774-Dec-14 16:21
memberMember 112871774-Dec-14 16:21 
AnswerRe: what IDE to use Pin
Garth J Lancaster4-Dec-14 17:36
professionalGarth J Lancaster4-Dec-14 17:36 
GeneralRe: what IDE to use Pin
Member 112871774-Dec-14 18:56
memberMember 112871774-Dec-14 18:56 
News*.hex to *.dfu Pin
ZsirosD22-Jun-13 9:38
memberZsirosD22-Jun-13 9:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05-2016 | 2.8.181113.2 | Last Updated 12 Mar 2013
Article Copyright 2013 by Mark McLean (ExpElec)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid