nkjnm
Dependencies: MAX44000 nexpaq_mdk
Fork of LED_Demo by
mbd_os/tools/add_fib.py@7:3a65ef12ba31, 2016-11-04 (annotated)
- Committer:
- nitsshukla
- Date:
- Fri Nov 04 12:06:04 2016 +0000
- Revision:
- 7:3a65ef12ba31
- Parent:
- 1:55a6170b404f
kghj;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | """ |
nexpaq | 1:55a6170b404f | 2 | @copyright (c) 2012 ON Semiconductor. All rights reserved. |
nexpaq | 1:55a6170b404f | 3 | ON Semiconductor is supplying this software for use with ON Semiconductor |
nexpaq | 1:55a6170b404f | 4 | processor based microcontrollers only. |
nexpaq | 1:55a6170b404f | 5 | THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED |
nexpaq | 1:55a6170b404f | 6 | OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF |
nexpaq | 1:55a6170b404f | 7 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. |
nexpaq | 1:55a6170b404f | 8 | ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, |
nexpaq | 1:55a6170b404f | 9 | INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
nexpaq | 1:55a6170b404f | 10 | """ |
nexpaq | 1:55a6170b404f | 11 | |
nexpaq | 1:55a6170b404f | 12 | from __future__ import absolute_import |
nexpaq | 1:55a6170b404f | 13 | from __future__ import print_function |
nexpaq | 1:55a6170b404f | 14 | |
nexpaq | 1:55a6170b404f | 15 | import itertools |
nexpaq | 1:55a6170b404f | 16 | import binascii |
nexpaq | 1:55a6170b404f | 17 | import intelhex |
nexpaq | 1:55a6170b404f | 18 | |
nexpaq | 1:55a6170b404f | 19 | FIB_BASE = 0x2000 |
nexpaq | 1:55a6170b404f | 20 | FLASH_BASE = 0x3000 |
nexpaq | 1:55a6170b404f | 21 | FW_REV = 0x01000100 |
nexpaq | 1:55a6170b404f | 22 | def ranges(i): |
nexpaq | 1:55a6170b404f | 23 | for _, b in itertools.groupby(enumerate(i), lambda x_y: x_y[1] - x_y[0]): |
nexpaq | 1:55a6170b404f | 24 | b = list(b) |
nexpaq | 1:55a6170b404f | 25 | yield b[0][1], b[-1][1] |
nexpaq | 1:55a6170b404f | 26 | |
nexpaq | 1:55a6170b404f | 27 | |
nexpaq | 1:55a6170b404f | 28 | def add_fib_at_start(arginput): |
nexpaq | 1:55a6170b404f | 29 | input_file = arginput + ".bin" |
nexpaq | 1:55a6170b404f | 30 | file_name_hex = arginput + "_fib.hex" |
nexpaq | 1:55a6170b404f | 31 | file_name_bin = arginput + ".bin" |
nexpaq | 1:55a6170b404f | 32 | |
nexpaq | 1:55a6170b404f | 33 | # Read in hex file |
nexpaq | 1:55a6170b404f | 34 | input_hex_file = intelhex.IntelHex() |
nexpaq | 1:55a6170b404f | 35 | input_hex_file.padding = 0x00 |
nexpaq | 1:55a6170b404f | 36 | input_hex_file.loadbin(input_file, offset=FLASH_BASE) |
nexpaq | 1:55a6170b404f | 37 | |
nexpaq | 1:55a6170b404f | 38 | output_hex_file = intelhex.IntelHex() |
nexpaq | 1:55a6170b404f | 39 | output_hex_file.padding = 0x00 |
nexpaq | 1:55a6170b404f | 40 | |
nexpaq | 1:55a6170b404f | 41 | # Get the starting and ending address |
nexpaq | 1:55a6170b404f | 42 | addresses = input_hex_file.addresses() |
nexpaq | 1:55a6170b404f | 43 | addresses.sort() |
nexpaq | 1:55a6170b404f | 44 | start_end_pairs = list(ranges(addresses)) |
nexpaq | 1:55a6170b404f | 45 | regions = len(start_end_pairs) |
nexpaq | 1:55a6170b404f | 46 | |
nexpaq | 1:55a6170b404f | 47 | if regions == 1: |
nexpaq | 1:55a6170b404f | 48 | start, end = start_end_pairs[0] |
nexpaq | 1:55a6170b404f | 49 | else: |
nexpaq | 1:55a6170b404f | 50 | start = min(min(start_end_pairs)) |
nexpaq | 1:55a6170b404f | 51 | end = max(max(start_end_pairs)) |
nexpaq | 1:55a6170b404f | 52 | |
nexpaq | 1:55a6170b404f | 53 | assert start >= FLASH_BASE, ("Error - start 0x%x less than begining of user\ |
nexpaq | 1:55a6170b404f | 54 | flash area" %start) |
nexpaq | 1:55a6170b404f | 55 | # Compute checksum over the range (don't include data at location of crc) |
nexpaq | 1:55a6170b404f | 56 | size = end - start + 1 |
nexpaq | 1:55a6170b404f | 57 | data = input_hex_file.tobinarray(start=start, size=size) |
nexpaq | 1:55a6170b404f | 58 | crc32 = binascii.crc32(data) & 0xFFFFFFFF |
nexpaq | 1:55a6170b404f | 59 | |
nexpaq | 1:55a6170b404f | 60 | fw_rev = FW_REV |
nexpaq | 1:55a6170b404f | 61 | |
nexpaq | 1:55a6170b404f | 62 | checksum = (start + size + crc32 + fw_rev) & 0xFFFFFFFF |
nexpaq | 1:55a6170b404f | 63 | |
nexpaq | 1:55a6170b404f | 64 | print("Writing FIB: base 0x%08X, size 0x%08X, crc32 0x%08X, fw rev 0x%08X,\ |
nexpaq | 1:55a6170b404f | 65 | checksum 0x%08X" % (start, size, crc32, fw_rev, checksum)) |
nexpaq | 1:55a6170b404f | 66 | |
nexpaq | 1:55a6170b404f | 67 | #expected initial values used by daplink to validate that it is a valid bin |
nexpaq | 1:55a6170b404f | 68 | #file added as dummy values in this file because the fib area preceeds the |
nexpaq | 1:55a6170b404f | 69 | #application area the bootloader will ignore these dummy values |
nexpaq | 1:55a6170b404f | 70 | # 00 is stack pointer (RAM address) |
nexpaq | 1:55a6170b404f | 71 | # 04 is Reset vector (FLASH address) |
nexpaq | 1:55a6170b404f | 72 | # 08 NMI_Handler (FLASH address) |
nexpaq | 1:55a6170b404f | 73 | # 0C HardFault_Handler(FLASH address) |
nexpaq | 1:55a6170b404f | 74 | # 10 dummy |
nexpaq | 1:55a6170b404f | 75 | dummy_sp = 0x3FFFFC00 |
nexpaq | 1:55a6170b404f | 76 | dummy_reset_vector = 0x00003625 |
nexpaq | 1:55a6170b404f | 77 | dummy_nmi_handler = 0x00003761 |
nexpaq | 1:55a6170b404f | 78 | dummy_hardfault_handler = 0x00003691 |
nexpaq | 1:55a6170b404f | 79 | dummy_blank = 0x00000000 |
nexpaq | 1:55a6170b404f | 80 | |
nexpaq | 1:55a6170b404f | 81 | #expected fib structure |
nexpaq | 1:55a6170b404f | 82 | #typedef struct fib{ |
nexpaq | 1:55a6170b404f | 83 | #uint32_t base; /**< Base offset of firmware, indicating what flash the |
nexpaq | 1:55a6170b404f | 84 | # firmware is in. (will never be 0x11111111) */ |
nexpaq | 1:55a6170b404f | 85 | #uint32_t size; /**< Size of the firmware */ |
nexpaq | 1:55a6170b404f | 86 | #uint32_t crc; /**< CRC32 for firmware correctness check */ |
nexpaq | 1:55a6170b404f | 87 | #uint32_t rev; /**< Revision number */ |
nexpaq | 1:55a6170b404f | 88 | #uint32_t checksum; /**< Check-sum of information block */ |
nexpaq | 1:55a6170b404f | 89 | #}fib_t, *fib_pt; |
nexpaq | 1:55a6170b404f | 90 | |
nexpaq | 1:55a6170b404f | 91 | fib_start = FIB_BASE |
nexpaq | 1:55a6170b404f | 92 | dummy_fib_size = 20 |
nexpaq | 1:55a6170b404f | 93 | fib_size = 20 |
nexpaq | 1:55a6170b404f | 94 | user_code_start = FLASH_BASE |
nexpaq | 1:55a6170b404f | 95 | |
nexpaq | 1:55a6170b404f | 96 | # Write FIB to the file in little endian |
nexpaq | 1:55a6170b404f | 97 | output_hex_file[fib_start + 0] = (dummy_sp >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 98 | output_hex_file[fib_start + 1] = (dummy_sp >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 99 | output_hex_file[fib_start + 2] = (dummy_sp >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 100 | output_hex_file[fib_start + 3] = (dummy_sp >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 101 | |
nexpaq | 1:55a6170b404f | 102 | output_hex_file[fib_start + 4] = (dummy_reset_vector >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 103 | output_hex_file[fib_start + 5] = (dummy_reset_vector >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 104 | output_hex_file[fib_start + 6] = (dummy_reset_vector >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 105 | output_hex_file[fib_start + 7] = (dummy_reset_vector >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 106 | |
nexpaq | 1:55a6170b404f | 107 | output_hex_file[fib_start + 8] = (dummy_nmi_handler >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 108 | output_hex_file[fib_start + 9] = (dummy_nmi_handler >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 109 | output_hex_file[fib_start + 10] = (dummy_nmi_handler >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 110 | output_hex_file[fib_start + 11] = (dummy_nmi_handler >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 111 | |
nexpaq | 1:55a6170b404f | 112 | output_hex_file[fib_start + 12] = (dummy_hardfault_handler >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 113 | output_hex_file[fib_start + 13] = (dummy_hardfault_handler >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 114 | output_hex_file[fib_start + 14] = (dummy_hardfault_handler >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 115 | output_hex_file[fib_start + 15] = (dummy_hardfault_handler >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 116 | |
nexpaq | 1:55a6170b404f | 117 | output_hex_file[fib_start + 16] = (dummy_blank >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 118 | output_hex_file[fib_start + 17] = (dummy_blank >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 119 | output_hex_file[fib_start + 18] = (dummy_blank >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 120 | output_hex_file[fib_start + 19] = (dummy_blank >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 121 | |
nexpaq | 1:55a6170b404f | 122 | # Write FIB to the file in little endian |
nexpaq | 1:55a6170b404f | 123 | output_hex_file[fib_start + 20] = (start >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 124 | output_hex_file[fib_start + 21] = (start >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 125 | output_hex_file[fib_start + 22] = (start >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 126 | output_hex_file[fib_start + 23] = (start >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 127 | |
nexpaq | 1:55a6170b404f | 128 | output_hex_file[fib_start + 24] = (size >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 129 | output_hex_file[fib_start + 25] = (size >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 130 | output_hex_file[fib_start + 26] = (size >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 131 | output_hex_file[fib_start + 27] = (size >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 132 | |
nexpaq | 1:55a6170b404f | 133 | output_hex_file[fib_start + 28] = (crc32 >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 134 | output_hex_file[fib_start + 29] = (crc32 >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 135 | output_hex_file[fib_start + 30] = (crc32 >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 136 | output_hex_file[fib_start + 31] = (crc32 >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 137 | |
nexpaq | 1:55a6170b404f | 138 | output_hex_file[fib_start + 32] = (fw_rev >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 139 | output_hex_file[fib_start + 33] = (fw_rev >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 140 | output_hex_file[fib_start + 34] = (fw_rev >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 141 | output_hex_file[fib_start + 35] = (fw_rev >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 142 | |
nexpaq | 1:55a6170b404f | 143 | output_hex_file[fib_start + 36] = (checksum >> 0) & 0xFF |
nexpaq | 1:55a6170b404f | 144 | output_hex_file[fib_start + 37] = (checksum >> 8) & 0xFF |
nexpaq | 1:55a6170b404f | 145 | output_hex_file[fib_start + 38] = (checksum >> 16) & 0xFF |
nexpaq | 1:55a6170b404f | 146 | output_hex_file[fib_start + 39] = (checksum >> 24) & 0xFF |
nexpaq | 1:55a6170b404f | 147 | |
nexpaq | 1:55a6170b404f | 148 | #pad the rest of the file |
nexpaq | 1:55a6170b404f | 149 | for i in range(fib_start + dummy_fib_size + fib_size, user_code_start): |
nexpaq | 1:55a6170b404f | 150 | output_hex_file[i] = 0xFF |
nexpaq | 1:55a6170b404f | 151 | |
nexpaq | 1:55a6170b404f | 152 | #merge two hex files |
nexpaq | 1:55a6170b404f | 153 | output_hex_file.merge(input_hex_file, overlap='error') |
nexpaq | 1:55a6170b404f | 154 | |
nexpaq | 1:55a6170b404f | 155 | # Write out file(s) |
nexpaq | 1:55a6170b404f | 156 | output_hex_file.tofile(file_name_hex, 'hex') |
nexpaq | 1:55a6170b404f | 157 | output_hex_file.tofile(file_name_bin, 'bin') |
nexpaq | 1:55a6170b404f | 158 |