Ihex 32 Bit¶
Forth Project Cloning¶
When projects are built using the non-interactive C Programming Language, the Source Code is compiled to Machine Code on a PC. This leaves a single binary file on the PC ready to flash to more MCU’s if required.
When projects are built using interactive Mecrisp-Stellaris Forth, the Source Code is compiled to Machine Code on the MCU itself. The only compiled Machine Code resides in the MCU “Dictionary”.
To create multiple identical MCU’s with Mecrisp-Stellaris Forth there are two choices.
Upload all the Forth source files again to additional MCU(s). This means there is no binary file record of the project.
Use iHex.fs below to produce a single, bootable Machine Code binary file on a PC ready to flash to more MCU’s if required. This also allows easy redistribution of the Forth project and is the method used for all binaries distributed via this site.
iHex.fs¶
iHex.fs dumps the complete Dictionary binary content when the target is cloned.
This method uses “arm-none-eabi-objcopy” to then convert the ASCII data back into a bootable binary image file on your PC so it may be flashed to another MCU thereby “cloning” the target chip.
Intel Hex¶
The “Intel Hex” (iHex) format is used to transfer binary data over ASCII mediums, such as via a serial terminal. The data is also checksummed for later verification.
Ihex 04 Type Extended Addresses¶
Intel Hex is a old format from the 8 bit MCU days, when 16 addressing was common. Nowadays MCU’s have much larger address with 32 bit addressing used in ARM Cortex-M.
Extended Addressing is used to extend the capability of Intel Hex from a maximum of 64kB to 4GB. It does this by inserting ‘extended addresses’ into the iHex file every 64kB as shown below.
Ihex 04 Type Extended Addresses Example¶
Showing type 04 Type Ihex Extended Addresses. These are calculated in ihex.fs up to 4GB.
Hex |
Extended Address |
Bytes |
---|---|---|
$10000 |
:020000040001F9 |
64 kB |
$20000 |
:020000040002F8 |
128 kB |
$30000 |
:020000040003F7 |
192 kB |
$40000 |
:020000040004F6 |
256 kB |
$50000 |
:020000040005F5 |
320 kB |
$60000 |
:020000040006F4 |
384 kB |
$70000 |
:020000040007F3 |
448 kB |
$80000 |
:020000040008F2 |
512 kB |
$90000 |
:020000040009F1 |
576 kB |
$A0000 |
:02000004000AF0 |
640 kB |
$B0000 |
:02000004000BEF |
720 kB |
$C0000 |
:02000004000CEE |
786 kB |
$D0000 |
:02000004000DED |
851 kB |
$E0000 |
:02000004000EEC |
917 kB |
$F0000 |
:02000004000FEB |
983 kB |
$100000 |
:020000040010EA |
1 MB |
Ihex Dump Example¶
Showing first extended address insertion “:020000040001F9” at $10000 - 64kB
Note
This page replaces older versions cloning and cloning-integrated
Description¶
Ihex.fs dumps the entire Dictionary contents to the terminal in Intel Hex Extended Ascii format, and can handle sizes up to the maximum 4GB available with 32 bit addressing as used by Cortex-M.
ihex.fs reads the STM32 MCU “Flash Size Register” to determine the maximum size and won’t attempt to read beyond that. This is to prevent a ‘exception’ being thrown by the MCU.
This works fine except where STM have actually included more Flash than stated in the “Flash Size Register”, such as in the STM32F103C8 which declares 64kB but usually has 128kB of Flash. This situation is handled with a parameter option to “clone”, see below.
Note
The “Flash Size Register” address is DIFFERENT BETWEEN DIFFERENT MODELS and must be changed to suit YOUR MCU. The address is given in the STM Technical Reference Manual and a few examples below.
Operation¶
Load ihex.fs
Enable your terminal log to capture the output, and then run the “clone” program as shown below. “Clone” requires TWO parameters, the first is a “flash-size-register-address” modifier, the second is the “flash-size-register-address” itself. This may be in Hex or Decimal as desired.
There is NO PARAMETER CHECKING. If you make a mistake the program will probably generate a Exception and/or produce incorrect results.
Usage: “1|2 flash-size-register-address clone”
‘1’ = normal use
‘2’ = only for a STM32F103C8 which reports 64kB of Flash but actually has double (128kB) OR if you suspect your chip MAY have double the flash advertized. Note: will crash this program with a Exception if it doesn’t!
‘flash-size-register-address’ = “Flash size data register address”. Check your STM reference manual “Device electronic signature” section.
‘clone’ name of the Forth program as listed below in the source
Example usage for:
STM32F051: “1 $1FFFF7CC clone”
STM32F103CB: “1 $1FFFF7E0 clone”
STM32F103C8: “2 $1FFFF7E0 clone”
The iHex dump starts with the word “clone” and ends with the word “clone_end” for easy parsing to remove any extraneous text before using ‘arm-none-eabi-objcopy’.
Once you have the clean iHex File process it as below. Note that any junk,extra text or checksum errors in this file will cause arm-none-eabi-objcopy to abort with a error message.
“ arm-none-eabi-objcopy -I ihex -O binary <your-iHex-logfile> <binaryfile-name.bin> “
After the dump is complete, save/disable your terminal log and edit-out the pre-amble (everything before and including “clone”) and post-amble (remove “clone_end” at the very end of the file) text.
Note
Arm-none-eabi is required but you should have it installed if you have compiled Mecrisp-Stellaris previously.
Sourcecode¶
1 \ Program Name: ihex.fs 2 \ Date: Sun 5 Jan 2020 21:19:51 AEDT 3 \ Copyright 2020 by t.j.porter <terry@tjporter.com.au>, licensed under the GPLV2 4 \ For Mecrsp-Stellars by Matthas Koch. 5 \ https://sourceforge.net/projects/mecrisp/ 6 \ MCU: STM32Fxxx 7 \ This Forth Program : Dumps a iHex image of the target MCU dictionary to the 8 \ terminal where it can be captured in a terminal log and turned into a 9 \ bootable binary file clone of the target. 10 \ ---------------------------------------------------------------------------------------\ 11 \ 12 \ Usage: "1|2 flash-size-register-address clone" 13 \ 14 \ '1' = normal use 15 \ 16 \ '2' = only for a STM32F103C8 which reports 64kB of Flash but actually has double (128kB) 17 \ OR if you suspect your chip MAY have double the flash advertized. Note: will crash 18 \ this program with a Exception if it doesn't! 19 \ 20 \ 'flash-size-register-address' = "Flash size data register address". Check your STM 21 \ reference manual "Device electronic signature" section. 22 \ 23 \ 'clone' name of the program as listed below in the source 24 \ 25 \ Example usage for: 26 \ STM32F051: "1 $1FFFF7CC clone" 27 \ STM32F103CB: "1 $1FFFF7E0 clone" 28 \ STM32F103C8: "2 $1FFFF7E0 clone" 29 \ 30 \ The iHex dump starts with the word "clone" and ends with the word "clone_end" for 31 \ easy parsing to remove any extraneous text before using 'arm-none-eabi-objcopy'. 32 \ 33 \ Once you have the iHex File process it as below. 34 \ 35 \ " arm-none-eabi-objcopy -I ihex -O binary <your-iHex-logfile> <binaryfile-name.bin> " 36 \ ---------------------------------------------------------------------------------------\ 37 \ Notes: 38 \ Can produce extended iHex files up to 32 bits, i.e. 4 GB. 39 \ iHex Extended Addressing type 04 is used 40 \ ihex.fs reads the STM32 MCU "Flash Size Register" to determine the maximum size and won't 41 \ attempt to read beyond that because a Exception may be thrown if it is the Flash end. 42 \ Check your STM reference manual for your MCU's "Flash Size Register" address. 43 \ 44 \ flash-size-register-address MCU 45 \ --------------------------- ---------- 46 \ $1FFFF7CC STM32F0XX 47 \ $1FFFF7E0 STM32F1XX 48 \ $1FFF7A22 STM32F405/415, STM32F407/417, STM32F427/437 and STM32F429/439 49 \ -------------------- Shouldn't need to change anything below ------------------------- \ 50 51 : erased? ( -- ) \ Check Flash bytes NOT erased. 52 0 \ Flag of 0 = erased byte, ignore data afterwards 53 i 16 + i do \ Scan data 54 i c@ $FF <> or \ Set flag if there is a non-$FF byte. Some chips have Flash = $00 as erased. 55 loop 56 ; 57 58 : ea04generate ( -- print 04 Extended Address Type record ) 59 ." :02000004" \ Write 04 Extended Address Type record preamble 60 dup h.4 \ quotient = LBA 61 $06 + not $FF and 1 + \ calculate checksum. $06 is record preamble value. 62 h.2 cr \ print checksum 63 ; 64 65 : insert.04ea? ( 32 bit hex address -- 04 Extended Address iHex Type records ) 66 i $10000 >= IF \ eliminate addresses under 64kB ($10000) 67 i $10000 u/mod \ quotient is TOS, then remainder 68 swap 69 0= IF \ only want values where there is no remainder 70 ea04generate \ quotient passed to ea04generate as LBA 71 ELSE drop \ drop unwanted non zero remainder 72 THEN 73 THEN 74 ; 75 76 : clone ( 1|2 flash-size-register-address -- ) cr \ Dumps a bootable Flash Image (core + all words) 77 @ $FFFF and 1024 * * \ calculate flash size 78 0 do 79 erased? 80 if 81 insert.04ea? 82 ." :10" i h.4 ." 00" \ Write record-intro with 4 digits 83 $10 \ Begin checksum 84 i $FF and + \ Sum the address bytes 85 i 8 rshift $FF and + \ separately into the checksum 86 i 16 + i do \ EOL 87 i c@ h.2 \ Print data with 2 digits 88 i c@ + \ Sum it up for Checksum 89 loop 90 negate h.2 \ Write Checksum 91 cr 92 then \ End of Flash or area of erased bytes ( $FFFF ) 93 16 +loop 94 ." :00000001FF " \ iHex terminator 95 ." clone_end " cr \ clone.sh search log terminator 96 ;