the other jimmy / mbed-sdk-tools

Fork of mbed-sdk-tools by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers add_fib.py Source File

add_fib.py

00001 """
00002 @copyright (c) 2012 ON Semiconductor. All rights reserved.
00003 ON Semiconductor is supplying this software for use with ON Semiconductor
00004 processor based microcontrollers only.
00005 THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
00006 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
00007 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
00008 ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
00009 INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
00010 """
00011 
00012 from __future__ import absolute_import
00013 from __future__ import print_function
00014 
00015 import itertools
00016 import binascii
00017 import intelhex
00018 from tools.config import Config
00019 
00020 FIB_BASE = 0x2000
00021 FLASH_BASE = 0x3000
00022 FW_REV = 0x01000100
00023 TRIM_BASE = 0x2800
00024 
00025 def ranges(i):
00026     for _, b in itertools.groupby(enumerate(i), lambda x_y: x_y[1] - x_y[0]):
00027         b = list(b)
00028         yield b[0][1], b[-1][1]
00029 
00030 
00031 def add_fib_at_start(arginput):
00032     input_file = arginput + ".bin"
00033     file_name_hex = arginput + "_fib.hex"
00034     file_name_bin = arginput + ".bin"
00035 
00036     # Read in hex file
00037     input_hex_file = intelhex.IntelHex()
00038     input_hex_file.padding = 0x00
00039     input_hex_file.loadbin(input_file, offset=FLASH_BASE)
00040 
00041     output_hex_file = intelhex.IntelHex()
00042     output_hex_file.padding = 0x00
00043 
00044     # Get the starting and ending address
00045     addresses = input_hex_file.addresses()
00046     addresses.sort()
00047     start_end_pairs = list(ranges(addresses))
00048     regions = len(start_end_pairs)
00049 
00050     if regions == 1:
00051         start, end = start_end_pairs[0]
00052     else:
00053         start = min(min(start_end_pairs))
00054         end = max(max(start_end_pairs))
00055 
00056     assert start >= FLASH_BASE, ("Error - start 0x%x less than begining of user\
00057     flash area" %start)
00058     # Compute checksum over the range (don't include data at location of crc)
00059     size = end - start + 1
00060     data = input_hex_file.tobinarray(start=start, size=size)
00061     crc32 = binascii.crc32(data) & 0xFFFFFFFF
00062 
00063     fw_rev = FW_REV
00064 
00065     checksum = (start + size + crc32 + fw_rev) & 0xFFFFFFFF
00066 
00067     print("Writing FIB: base 0x%08X, size 0x%08X, crc32 0x%08X, fw rev 0x%08X,\
00068     checksum 0x%08X" % (start, size, crc32, fw_rev, checksum))
00069 
00070 #expected initial values used by daplink to validate that it is a valid bin
00071 #file added as dummy values in this file because the fib area preceeds the
00072 #application area the bootloader will ignore these dummy values
00073 #  00 is stack pointer (RAM address)
00074 #  04 is Reset vector  (FLASH address)
00075 #  08 NMI_Handler      (FLASH address)
00076 #  0C HardFault_Handler(FLASH address)
00077 #  10 dummy
00078     dummy_sp = 0x3FFFFC00
00079     dummy_reset_vector = 0x00003625
00080     dummy_nmi_handler = 0x00003761
00081     dummy_hardfault_handler = 0x00003691
00082     dummy_blank = 0x00000000
00083 
00084 #expected fib structure
00085 #typedef struct fib{
00086     #uint32_t base;     /**< Base offset of firmware, indicating what flash the
00087     #                        firmware is in. (will never be 0x11111111) */
00088     #uint32_t size;     /**< Size of the firmware */
00089     #uint32_t crc;      /**< CRC32 for firmware correctness check */
00090     #uint32_t rev;      /**< Revision number */
00091     #uint32_t checksum; /**< Check-sum of information block */
00092 #}fib_t, *fib_pt;
00093 
00094     fib_start = FIB_BASE
00095     dummy_fib_size = 20
00096     fib_size = 20
00097     trim_size = 24
00098     user_code_start = FLASH_BASE
00099     trim_area_start = TRIM_BASE
00100 
00101     # Write FIB to the file in little endian
00102     output_hex_file[fib_start + 0] = (dummy_sp >> 0) & 0xFF
00103     output_hex_file[fib_start + 1] = (dummy_sp >> 8) & 0xFF
00104     output_hex_file[fib_start + 2] = (dummy_sp >> 16) & 0xFF
00105     output_hex_file[fib_start + 3] = (dummy_sp >> 24) & 0xFF
00106 
00107     output_hex_file[fib_start + 4] = (dummy_reset_vector >> 0) & 0xFF
00108     output_hex_file[fib_start + 5] = (dummy_reset_vector >> 8) & 0xFF
00109     output_hex_file[fib_start + 6] = (dummy_reset_vector >> 16) & 0xFF
00110     output_hex_file[fib_start + 7] = (dummy_reset_vector >> 24) & 0xFF
00111 
00112     output_hex_file[fib_start + 8] = (dummy_nmi_handler >> 0) & 0xFF
00113     output_hex_file[fib_start + 9] = (dummy_nmi_handler >> 8) & 0xFF
00114     output_hex_file[fib_start + 10] = (dummy_nmi_handler >> 16) & 0xFF
00115     output_hex_file[fib_start + 11] = (dummy_nmi_handler >> 24) & 0xFF
00116 
00117     output_hex_file[fib_start + 12] = (dummy_hardfault_handler >> 0) & 0xFF
00118     output_hex_file[fib_start + 13] = (dummy_hardfault_handler >> 8) & 0xFF
00119     output_hex_file[fib_start + 14] = (dummy_hardfault_handler >> 16) & 0xFF
00120     output_hex_file[fib_start + 15] = (dummy_hardfault_handler >> 24) & 0xFF
00121 
00122     output_hex_file[fib_start + 16] = (dummy_blank >> 0) & 0xFF
00123     output_hex_file[fib_start + 17] = (dummy_blank >> 8) & 0xFF
00124     output_hex_file[fib_start + 18] = (dummy_blank >> 16) & 0xFF
00125     output_hex_file[fib_start + 19] = (dummy_blank >> 24) & 0xFF
00126 
00127     # Write FIB to the file in little endian
00128     output_hex_file[fib_start + 20] = (start >> 0) & 0xFF
00129     output_hex_file[fib_start + 21] = (start >> 8) & 0xFF
00130     output_hex_file[fib_start + 22] = (start >> 16) & 0xFF
00131     output_hex_file[fib_start + 23] = (start >> 24) & 0xFF
00132 
00133     output_hex_file[fib_start + 24] = (size >> 0) & 0xFF
00134     output_hex_file[fib_start + 25] = (size >> 8) & 0xFF
00135     output_hex_file[fib_start + 26] = (size >> 16) & 0xFF
00136     output_hex_file[fib_start + 27] = (size >> 24) & 0xFF
00137 
00138     output_hex_file[fib_start + 28] = (crc32 >> 0) & 0xFF
00139     output_hex_file[fib_start + 29] = (crc32 >> 8) & 0xFF
00140     output_hex_file[fib_start + 30] = (crc32 >> 16) & 0xFF
00141     output_hex_file[fib_start + 31] = (crc32 >> 24) & 0xFF
00142 
00143     output_hex_file[fib_start + 32] = (fw_rev >> 0) & 0xFF
00144     output_hex_file[fib_start + 33] = (fw_rev >> 8) & 0xFF
00145     output_hex_file[fib_start + 34] = (fw_rev >> 16) & 0xFF
00146     output_hex_file[fib_start + 35] = (fw_rev >> 24) & 0xFF
00147 
00148     output_hex_file[fib_start + 36] = (checksum >> 0) & 0xFF
00149     output_hex_file[fib_start + 37] = (checksum >> 8) & 0xFF
00150     output_hex_file[fib_start + 38] = (checksum >> 16) & 0xFF
00151     output_hex_file[fib_start + 39] = (checksum >> 24) & 0xFF
00152 
00153     #pad the rest of the file
00154     for i in range(fib_start + dummy_fib_size + fib_size, trim_area_start):
00155         output_hex_file[i] = 0xFF
00156 
00157     # Read in configuration data from the config parameter in targets.json
00158     configData = Config('NCS36510')
00159     paramData = configData.get_target_config_data()
00160     for v in paramData.values():
00161         if (v.name == "target.mac-addr-high"):
00162             mac_addr_high = int(v.value, 16)
00163         elif (v.name == "target.mac-addr-low"):
00164             mac_addr_low = int(v.value,16)
00165         elif (v.name == "target.32KHz-clk-trim"):
00166             clk_32k_trim = int(v.value,16)
00167         elif (v.name == "target.32MHz-clk-trim"):
00168             clk_32m_trim = int(v.value,16)
00169         elif (v.name == "target.rssi-trim"):
00170             rssi = int(v.value,16)
00171         elif (v.name == "target.txtune-trim"):
00172             txtune = int(v.value,16)
00173         else:
00174             print("Not a valid param")
00175 
00176     output_hex_file[trim_area_start + 0] = mac_addr_low & 0xFF
00177     output_hex_file[trim_area_start + 1] = (mac_addr_low >> 8)  & 0xFF
00178     output_hex_file[trim_area_start + 2] = (mac_addr_low >> 16) & 0xFF
00179     output_hex_file[trim_area_start + 3] = (mac_addr_low >> 24) & 0xFF
00180     
00181     output_hex_file[trim_area_start + 4] = mac_addr_high & 0xFF
00182     output_hex_file[trim_area_start + 5] = (mac_addr_high >> 8)  & 0xFF
00183     output_hex_file[trim_area_start + 6] = (mac_addr_high >> 16) & 0xFF
00184     output_hex_file[trim_area_start + 7] = (mac_addr_high >> 24) & 0xFF
00185 
00186     output_hex_file[trim_area_start + 8] = clk_32k_trim & 0xFF
00187     output_hex_file[trim_area_start + 9] = (clk_32k_trim >> 8)  & 0xFF
00188     output_hex_file[trim_area_start + 10] = (clk_32k_trim >> 16) & 0xFF
00189     output_hex_file[trim_area_start + 11] = (clk_32k_trim >> 24) & 0xFF
00190 
00191     output_hex_file[trim_area_start + 12] = clk_32m_trim & 0xFF
00192     output_hex_file[trim_area_start + 13] = (clk_32m_trim >> 8)  & 0xFF
00193     output_hex_file[trim_area_start + 14] = (clk_32m_trim >> 16) & 0xFF
00194     output_hex_file[trim_area_start + 15] = (clk_32m_trim >> 24) & 0xFF
00195 
00196     output_hex_file[trim_area_start + 16] = rssi & 0xFF
00197     output_hex_file[trim_area_start + 17] = (rssi >> 8)  & 0xFF
00198     output_hex_file[trim_area_start + 18] = (rssi >> 16) & 0xFF
00199     output_hex_file[trim_area_start + 19] = (rssi >> 24) & 0xFF
00200 
00201     output_hex_file[trim_area_start + 20] = txtune & 0xFF
00202     output_hex_file[trim_area_start + 21] = (txtune >> 8)  & 0xFF
00203     output_hex_file[trim_area_start + 22] = (txtune >> 16) & 0xFF
00204     output_hex_file[trim_area_start + 23] = (txtune >> 24) & 0xFF
00205     
00206     # pad the rest of the area with 0xFF
00207     for i in range(trim_area_start + trim_size, user_code_start):
00208         output_hex_file[i] = 0xFF
00209 
00210     #merge two hex files
00211     output_hex_file.merge(input_hex_file, overlap='error')
00212 
00213     # Write out file(s)
00214     output_hex_file.tofile(file_name_hex, 'hex')
00215     output_hex_file.tofile(file_name_bin, 'bin')