joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

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 
00019 FIB_BASE = 0x2000
00020 FLASH_BASE = 0x3000
00021 FW_REV = 0x01000100
00022 def ranges(i):
00023     for _, b in itertools.groupby(enumerate(i), lambda x_y: x_y[1] - x_y[0]):
00024         b = list(b)
00025         yield b[0][1], b[-1][1]
00026 
00027 
00028 def add_fib_at_start(arginput):
00029     input_file = arginput + ".bin"
00030     file_name_hex = arginput + "_fib.hex"
00031     file_name_bin = arginput + ".bin"
00032 
00033     # Read in hex file
00034     input_hex_file = intelhex.IntelHex()
00035     input_hex_file.padding = 0x00
00036     input_hex_file.loadbin(input_file, offset=FLASH_BASE)
00037 
00038     output_hex_file = intelhex.IntelHex()
00039     output_hex_file.padding = 0x00
00040 
00041     # Get the starting and ending address
00042     addresses = input_hex_file.addresses()
00043     addresses.sort()
00044     start_end_pairs = list(ranges(addresses))
00045     regions = len(start_end_pairs)
00046 
00047     if regions == 1:
00048         start, end = start_end_pairs[0]
00049     else:
00050         start = min(min(start_end_pairs))
00051         end = max(max(start_end_pairs))
00052 
00053     assert start >= FLASH_BASE, ("Error - start 0x%x less than begining of user\
00054     flash area" %start)
00055     # Compute checksum over the range (don't include data at location of crc)
00056     size = end - start + 1
00057     data = input_hex_file.tobinarray(start=start, size=size)
00058     crc32 = binascii.crc32(data) & 0xFFFFFFFF
00059 
00060     fw_rev = FW_REV
00061 
00062     checksum = (start + size + crc32 + fw_rev) & 0xFFFFFFFF
00063 
00064     print("Writing FIB: base 0x%08X, size 0x%08X, crc32 0x%08X, fw rev 0x%08X,\
00065     checksum 0x%08X" % (start, size, crc32, fw_rev, checksum))
00066 
00067 #expected initial values used by daplink to validate that it is a valid bin
00068 #file added as dummy values in this file because the fib area preceeds the
00069 #application area the bootloader will ignore these dummy values
00070 #  00 is stack pointer (RAM address)
00071 #  04 is Reset vector  (FLASH address)
00072 #  08 NMI_Handler      (FLASH address)
00073 #  0C HardFault_Handler(FLASH address)
00074 #  10 dummy
00075     dummy_sp = 0x3FFFFC00
00076     dummy_reset_vector = 0x00003625
00077     dummy_nmi_handler = 0x00003761
00078     dummy_hardfault_handler = 0x00003691
00079     dummy_blank = 0x00000000
00080 
00081 #expected fib structure
00082 #typedef struct fib{
00083     #uint32_t base;     /**< Base offset of firmware, indicating what flash the
00084     #                        firmware is in. (will never be 0x11111111) */
00085     #uint32_t size;     /**< Size of the firmware */
00086     #uint32_t crc;      /**< CRC32 for firmware correctness check */
00087     #uint32_t rev;      /**< Revision number */
00088     #uint32_t checksum; /**< Check-sum of information block */
00089 #}fib_t, *fib_pt;
00090 
00091     fib_start = FIB_BASE
00092     dummy_fib_size = 20
00093     fib_size = 20
00094     user_code_start = FLASH_BASE
00095 
00096     # Write FIB to the file in little endian
00097     output_hex_file[fib_start + 0] = (dummy_sp >> 0) & 0xFF
00098     output_hex_file[fib_start + 1] = (dummy_sp >> 8) & 0xFF
00099     output_hex_file[fib_start + 2] = (dummy_sp >> 16) & 0xFF
00100     output_hex_file[fib_start + 3] = (dummy_sp >> 24) & 0xFF
00101 
00102     output_hex_file[fib_start + 4] = (dummy_reset_vector >> 0) & 0xFF
00103     output_hex_file[fib_start + 5] = (dummy_reset_vector >> 8) & 0xFF
00104     output_hex_file[fib_start + 6] = (dummy_reset_vector >> 16) & 0xFF
00105     output_hex_file[fib_start + 7] = (dummy_reset_vector >> 24) & 0xFF
00106 
00107     output_hex_file[fib_start + 8] = (dummy_nmi_handler >> 0) & 0xFF
00108     output_hex_file[fib_start + 9] = (dummy_nmi_handler >> 8) & 0xFF
00109     output_hex_file[fib_start + 10] = (dummy_nmi_handler >> 16) & 0xFF
00110     output_hex_file[fib_start + 11] = (dummy_nmi_handler >> 24) & 0xFF
00111 
00112     output_hex_file[fib_start + 12] = (dummy_hardfault_handler >> 0) & 0xFF
00113     output_hex_file[fib_start + 13] = (dummy_hardfault_handler >> 8) & 0xFF
00114     output_hex_file[fib_start + 14] = (dummy_hardfault_handler >> 16) & 0xFF
00115     output_hex_file[fib_start + 15] = (dummy_hardfault_handler >> 24) & 0xFF
00116 
00117     output_hex_file[fib_start + 16] = (dummy_blank >> 0) & 0xFF
00118     output_hex_file[fib_start + 17] = (dummy_blank >> 8) & 0xFF
00119     output_hex_file[fib_start + 18] = (dummy_blank >> 16) & 0xFF
00120     output_hex_file[fib_start + 19] = (dummy_blank >> 24) & 0xFF
00121 
00122     # Write FIB to the file in little endian
00123     output_hex_file[fib_start + 20] = (start >> 0) & 0xFF
00124     output_hex_file[fib_start + 21] = (start >> 8) & 0xFF
00125     output_hex_file[fib_start + 22] = (start >> 16) & 0xFF
00126     output_hex_file[fib_start + 23] = (start >> 24) & 0xFF
00127 
00128     output_hex_file[fib_start + 24] = (size >> 0) & 0xFF
00129     output_hex_file[fib_start + 25] = (size >> 8) & 0xFF
00130     output_hex_file[fib_start + 26] = (size >> 16) & 0xFF
00131     output_hex_file[fib_start + 27] = (size >> 24) & 0xFF
00132 
00133     output_hex_file[fib_start + 28] = (crc32 >> 0) & 0xFF
00134     output_hex_file[fib_start + 29] = (crc32 >> 8) & 0xFF
00135     output_hex_file[fib_start + 30] = (crc32 >> 16) & 0xFF
00136     output_hex_file[fib_start + 31] = (crc32 >> 24) & 0xFF
00137 
00138     output_hex_file[fib_start + 32] = (fw_rev >> 0) & 0xFF
00139     output_hex_file[fib_start + 33] = (fw_rev >> 8) & 0xFF
00140     output_hex_file[fib_start + 34] = (fw_rev >> 16) & 0xFF
00141     output_hex_file[fib_start + 35] = (fw_rev >> 24) & 0xFF
00142 
00143     output_hex_file[fib_start + 36] = (checksum >> 0) & 0xFF
00144     output_hex_file[fib_start + 37] = (checksum >> 8) & 0xFF
00145     output_hex_file[fib_start + 38] = (checksum >> 16) & 0xFF
00146     output_hex_file[fib_start + 39] = (checksum >> 24) & 0xFF
00147 
00148     #pad the rest of the file
00149     for i in range(fib_start + dummy_fib_size + fib_size, user_code_start):
00150         output_hex_file[i] = 0xFF
00151 
00152     #merge two hex files
00153     output_hex_file.merge(input_hex_file, overlap='error')
00154 
00155     # Write out file(s)
00156     output_hex_file.tofile(file_name_hex, 'hex')
00157     output_hex_file.tofile(file_name_bin, 'bin')
00158