cancel
Showing results for 
Search instead for 
Did you mean: 

HEX File to Array in C on STM32F7

SGasp.1
Senior

Hi guys.. I am working with a huge hex file that I need to parse from hex file to an array of byte.. do you know an efficient way to do that without having an hw fault on the micro... Thanks a lot ...

1 ACCEPTED SOLUTION

Accepted Solutions

Typically you'd want to stage each line to a buffer, before checking the address and alignment, and you could eliminate 99.99% of hard faults.

Check also the address you're writing at is blank.

Hard Fault Handlers can return so one could do a try/catch type probing if really adventurous.

.HEX have the ability to be sparse, out-of-order, and describe partial flash-lines.

To process the 0.01%, you could probably just take all of the complete records/lines, and cache the odds, and then come back and try and combine/match up the odd ones, or sort them into a more manageable order/form.

One could also output an error saying the .HEX is invalid or needs to be normalized on a PC, if you really don't like the form as it is presented by the user.

Binary packaging tends to be preferable, the files are 2.5x smaller from the outset, and can be signed/authenticated, or compressed/encrypted.

Yes, built linkers/loaders in the past, tools to generate/consume hex files (Motorola and Intel), DFU files, last FOTA thing I built pulled .HEX from a server via HTTP and wrote content to flash.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

3 REPLIES 3

What is the source of this hex file, and where is the resulting binary ought to go?

JW

Typically you'd want to stage each line to a buffer, before checking the address and alignment, and you could eliminate 99.99% of hard faults.

Check also the address you're writing at is blank.

Hard Fault Handlers can return so one could do a try/catch type probing if really adventurous.

.HEX have the ability to be sparse, out-of-order, and describe partial flash-lines.

To process the 0.01%, you could probably just take all of the complete records/lines, and cache the odds, and then come back and try and combine/match up the odd ones, or sort them into a more manageable order/form.

One could also output an error saying the .HEX is invalid or needs to be normalized on a PC, if you really don't like the form as it is presented by the user.

Binary packaging tends to be preferable, the files are 2.5x smaller from the outset, and can be signed/authenticated, or compressed/encrypted.

Yes, built linkers/loaders in the past, tools to generate/consume hex files (Motorola and Intel), DFU files, last FOTA thing I built pulled .HEX from a server via HTTP and wrote content to flash.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

At compile time?

Convert it to binary first:

https://www.keil.com/download/docs/7.asp

Then convert binary to a C array:

https://github.com/megastep/bin2c

At runtime is harder since you need to decode the HEX file, which can be complex, but it is a doable task.

If you feel a post has answered your question, please click "Accept as Solution".