mbed-os for GR-LYCHEE
Dependents: mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more
tools/add_fib.py@0:f782d9c66c49, 2018-02-02 (annotated)
- Committer:
- dkato
- Date:
- Fri Feb 02 05:42:23 2018 +0000
- Revision:
- 0:f782d9c66c49
mbed-os for GR-LYCHEE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:f782d9c66c49 | 1 | """ |
dkato | 0:f782d9c66c49 | 2 | @copyright (c) 2012 ON Semiconductor. All rights reserved. |
dkato | 0:f782d9c66c49 | 3 | ON Semiconductor is supplying this software for use with ON Semiconductor |
dkato | 0:f782d9c66c49 | 4 | processor based microcontrollers only. |
dkato | 0:f782d9c66c49 | 5 | THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED |
dkato | 0:f782d9c66c49 | 6 | OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF |
dkato | 0:f782d9c66c49 | 7 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. |
dkato | 0:f782d9c66c49 | 8 | ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, |
dkato | 0:f782d9c66c49 | 9 | INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
dkato | 0:f782d9c66c49 | 10 | """ |
dkato | 0:f782d9c66c49 | 11 | |
dkato | 0:f782d9c66c49 | 12 | from __future__ import absolute_import |
dkato | 0:f782d9c66c49 | 13 | from __future__ import print_function |
dkato | 0:f782d9c66c49 | 14 | |
dkato | 0:f782d9c66c49 | 15 | import itertools |
dkato | 0:f782d9c66c49 | 16 | import binascii |
dkato | 0:f782d9c66c49 | 17 | import intelhex |
dkato | 0:f782d9c66c49 | 18 | from tools.config import Config |
dkato | 0:f782d9c66c49 | 19 | |
dkato | 0:f782d9c66c49 | 20 | FIB_BASE = 0x2000 |
dkato | 0:f782d9c66c49 | 21 | FLASH_BASE = 0x3000 |
dkato | 0:f782d9c66c49 | 22 | FW_REV = 0x01000100 |
dkato | 0:f782d9c66c49 | 23 | TRIM_BASE = 0x2800 |
dkato | 0:f782d9c66c49 | 24 | |
dkato | 0:f782d9c66c49 | 25 | def ranges(i): |
dkato | 0:f782d9c66c49 | 26 | for _, b in itertools.groupby(enumerate(i), lambda x_y: x_y[1] - x_y[0]): |
dkato | 0:f782d9c66c49 | 27 | b = list(b) |
dkato | 0:f782d9c66c49 | 28 | yield b[0][1], b[-1][1] |
dkato | 0:f782d9c66c49 | 29 | |
dkato | 0:f782d9c66c49 | 30 | |
dkato | 0:f782d9c66c49 | 31 | def add_fib_at_start(arginput): |
dkato | 0:f782d9c66c49 | 32 | input_file = arginput + ".bin" |
dkato | 0:f782d9c66c49 | 33 | file_name_hex = arginput + "_fib.hex" |
dkato | 0:f782d9c66c49 | 34 | file_name_bin = arginput + ".bin" |
dkato | 0:f782d9c66c49 | 35 | |
dkato | 0:f782d9c66c49 | 36 | # Read in hex file |
dkato | 0:f782d9c66c49 | 37 | input_hex_file = intelhex.IntelHex() |
dkato | 0:f782d9c66c49 | 38 | input_hex_file.padding = 0x00 |
dkato | 0:f782d9c66c49 | 39 | input_hex_file.loadbin(input_file, offset=FLASH_BASE) |
dkato | 0:f782d9c66c49 | 40 | |
dkato | 0:f782d9c66c49 | 41 | output_hex_file = intelhex.IntelHex() |
dkato | 0:f782d9c66c49 | 42 | output_hex_file.padding = 0x00 |
dkato | 0:f782d9c66c49 | 43 | |
dkato | 0:f782d9c66c49 | 44 | # Get the starting and ending address |
dkato | 0:f782d9c66c49 | 45 | addresses = input_hex_file.addresses() |
dkato | 0:f782d9c66c49 | 46 | addresses.sort() |
dkato | 0:f782d9c66c49 | 47 | start_end_pairs = list(ranges(addresses)) |
dkato | 0:f782d9c66c49 | 48 | regions = len(start_end_pairs) |
dkato | 0:f782d9c66c49 | 49 | |
dkato | 0:f782d9c66c49 | 50 | if regions == 1: |
dkato | 0:f782d9c66c49 | 51 | start, end = start_end_pairs[0] |
dkato | 0:f782d9c66c49 | 52 | else: |
dkato | 0:f782d9c66c49 | 53 | start = min(min(start_end_pairs)) |
dkato | 0:f782d9c66c49 | 54 | end = max(max(start_end_pairs)) |
dkato | 0:f782d9c66c49 | 55 | |
dkato | 0:f782d9c66c49 | 56 | assert start >= FLASH_BASE, ("Error - start 0x%x less than begining of user\ |
dkato | 0:f782d9c66c49 | 57 | flash area" %start) |
dkato | 0:f782d9c66c49 | 58 | # Compute checksum over the range (don't include data at location of crc) |
dkato | 0:f782d9c66c49 | 59 | size = end - start + 1 |
dkato | 0:f782d9c66c49 | 60 | data = input_hex_file.tobinarray(start=start, size=size) |
dkato | 0:f782d9c66c49 | 61 | crc32 = binascii.crc32(data) & 0xFFFFFFFF |
dkato | 0:f782d9c66c49 | 62 | |
dkato | 0:f782d9c66c49 | 63 | fw_rev = FW_REV |
dkato | 0:f782d9c66c49 | 64 | |
dkato | 0:f782d9c66c49 | 65 | checksum = (start + size + crc32 + fw_rev) & 0xFFFFFFFF |
dkato | 0:f782d9c66c49 | 66 | |
dkato | 0:f782d9c66c49 | 67 | print("Writing FIB: base 0x%08X, size 0x%08X, crc32 0x%08X, fw rev 0x%08X,\ |
dkato | 0:f782d9c66c49 | 68 | checksum 0x%08X" % (start, size, crc32, fw_rev, checksum)) |
dkato | 0:f782d9c66c49 | 69 | |
dkato | 0:f782d9c66c49 | 70 | #expected initial values used by daplink to validate that it is a valid bin |
dkato | 0:f782d9c66c49 | 71 | #file added as dummy values in this file because the fib area preceeds the |
dkato | 0:f782d9c66c49 | 72 | #application area the bootloader will ignore these dummy values |
dkato | 0:f782d9c66c49 | 73 | # 00 is stack pointer (RAM address) |
dkato | 0:f782d9c66c49 | 74 | # 04 is Reset vector (FLASH address) |
dkato | 0:f782d9c66c49 | 75 | # 08 NMI_Handler (FLASH address) |
dkato | 0:f782d9c66c49 | 76 | # 0C HardFault_Handler(FLASH address) |
dkato | 0:f782d9c66c49 | 77 | # 10 dummy |
dkato | 0:f782d9c66c49 | 78 | dummy_sp = 0x3FFFFC00 |
dkato | 0:f782d9c66c49 | 79 | dummy_reset_vector = 0x00003625 |
dkato | 0:f782d9c66c49 | 80 | dummy_nmi_handler = 0x00003761 |
dkato | 0:f782d9c66c49 | 81 | dummy_hardfault_handler = 0x00003691 |
dkato | 0:f782d9c66c49 | 82 | dummy_blank = 0x00000000 |
dkato | 0:f782d9c66c49 | 83 | |
dkato | 0:f782d9c66c49 | 84 | #expected fib structure |
dkato | 0:f782d9c66c49 | 85 | #typedef struct fib{ |
dkato | 0:f782d9c66c49 | 86 | #uint32_t base; /**< Base offset of firmware, indicating what flash the |
dkato | 0:f782d9c66c49 | 87 | # firmware is in. (will never be 0x11111111) */ |
dkato | 0:f782d9c66c49 | 88 | #uint32_t size; /**< Size of the firmware */ |
dkato | 0:f782d9c66c49 | 89 | #uint32_t crc; /**< CRC32 for firmware correctness check */ |
dkato | 0:f782d9c66c49 | 90 | #uint32_t rev; /**< Revision number */ |
dkato | 0:f782d9c66c49 | 91 | #uint32_t checksum; /**< Check-sum of information block */ |
dkato | 0:f782d9c66c49 | 92 | #}fib_t, *fib_pt; |
dkato | 0:f782d9c66c49 | 93 | |
dkato | 0:f782d9c66c49 | 94 | fib_start = FIB_BASE |
dkato | 0:f782d9c66c49 | 95 | dummy_fib_size = 20 |
dkato | 0:f782d9c66c49 | 96 | fib_size = 20 |
dkato | 0:f782d9c66c49 | 97 | trim_size = 24 |
dkato | 0:f782d9c66c49 | 98 | user_code_start = FLASH_BASE |
dkato | 0:f782d9c66c49 | 99 | trim_area_start = TRIM_BASE |
dkato | 0:f782d9c66c49 | 100 | |
dkato | 0:f782d9c66c49 | 101 | # Write FIB to the file in little endian |
dkato | 0:f782d9c66c49 | 102 | output_hex_file[fib_start + 0] = (dummy_sp >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 103 | output_hex_file[fib_start + 1] = (dummy_sp >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 104 | output_hex_file[fib_start + 2] = (dummy_sp >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 105 | output_hex_file[fib_start + 3] = (dummy_sp >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 106 | |
dkato | 0:f782d9c66c49 | 107 | output_hex_file[fib_start + 4] = (dummy_reset_vector >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 108 | output_hex_file[fib_start + 5] = (dummy_reset_vector >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 109 | output_hex_file[fib_start + 6] = (dummy_reset_vector >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 110 | output_hex_file[fib_start + 7] = (dummy_reset_vector >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 111 | |
dkato | 0:f782d9c66c49 | 112 | output_hex_file[fib_start + 8] = (dummy_nmi_handler >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 113 | output_hex_file[fib_start + 9] = (dummy_nmi_handler >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 114 | output_hex_file[fib_start + 10] = (dummy_nmi_handler >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 115 | output_hex_file[fib_start + 11] = (dummy_nmi_handler >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 116 | |
dkato | 0:f782d9c66c49 | 117 | output_hex_file[fib_start + 12] = (dummy_hardfault_handler >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 118 | output_hex_file[fib_start + 13] = (dummy_hardfault_handler >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 119 | output_hex_file[fib_start + 14] = (dummy_hardfault_handler >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 120 | output_hex_file[fib_start + 15] = (dummy_hardfault_handler >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 121 | |
dkato | 0:f782d9c66c49 | 122 | output_hex_file[fib_start + 16] = (dummy_blank >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 123 | output_hex_file[fib_start + 17] = (dummy_blank >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 124 | output_hex_file[fib_start + 18] = (dummy_blank >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 125 | output_hex_file[fib_start + 19] = (dummy_blank >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 126 | |
dkato | 0:f782d9c66c49 | 127 | # Write FIB to the file in little endian |
dkato | 0:f782d9c66c49 | 128 | output_hex_file[fib_start + 20] = (start >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 129 | output_hex_file[fib_start + 21] = (start >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 130 | output_hex_file[fib_start + 22] = (start >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 131 | output_hex_file[fib_start + 23] = (start >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 132 | |
dkato | 0:f782d9c66c49 | 133 | output_hex_file[fib_start + 24] = (size >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 134 | output_hex_file[fib_start + 25] = (size >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 135 | output_hex_file[fib_start + 26] = (size >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 136 | output_hex_file[fib_start + 27] = (size >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 137 | |
dkato | 0:f782d9c66c49 | 138 | output_hex_file[fib_start + 28] = (crc32 >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 139 | output_hex_file[fib_start + 29] = (crc32 >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 140 | output_hex_file[fib_start + 30] = (crc32 >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 141 | output_hex_file[fib_start + 31] = (crc32 >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 142 | |
dkato | 0:f782d9c66c49 | 143 | output_hex_file[fib_start + 32] = (fw_rev >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 144 | output_hex_file[fib_start + 33] = (fw_rev >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 145 | output_hex_file[fib_start + 34] = (fw_rev >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 146 | output_hex_file[fib_start + 35] = (fw_rev >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 147 | |
dkato | 0:f782d9c66c49 | 148 | output_hex_file[fib_start + 36] = (checksum >> 0) & 0xFF |
dkato | 0:f782d9c66c49 | 149 | output_hex_file[fib_start + 37] = (checksum >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 150 | output_hex_file[fib_start + 38] = (checksum >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 151 | output_hex_file[fib_start + 39] = (checksum >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 152 | |
dkato | 0:f782d9c66c49 | 153 | #pad the rest of the file |
dkato | 0:f782d9c66c49 | 154 | for i in range(fib_start + dummy_fib_size + fib_size, trim_area_start): |
dkato | 0:f782d9c66c49 | 155 | output_hex_file[i] = 0xFF |
dkato | 0:f782d9c66c49 | 156 | |
dkato | 0:f782d9c66c49 | 157 | # Read in configuration data from the config parameter in targets.json |
dkato | 0:f782d9c66c49 | 158 | configData = Config('NCS36510') |
dkato | 0:f782d9c66c49 | 159 | paramData = configData.get_target_config_data() |
dkato | 0:f782d9c66c49 | 160 | for v in paramData.values(): |
dkato | 0:f782d9c66c49 | 161 | if (v.name == "target.mac-addr-high"): |
dkato | 0:f782d9c66c49 | 162 | mac_addr_high = int(v.value, 16) |
dkato | 0:f782d9c66c49 | 163 | elif (v.name == "target.mac-addr-low"): |
dkato | 0:f782d9c66c49 | 164 | mac_addr_low = int(v.value,16) |
dkato | 0:f782d9c66c49 | 165 | elif (v.name == "target.32KHz-clk-trim"): |
dkato | 0:f782d9c66c49 | 166 | clk_32k_trim = int(v.value,16) |
dkato | 0:f782d9c66c49 | 167 | elif (v.name == "target.32MHz-clk-trim"): |
dkato | 0:f782d9c66c49 | 168 | clk_32m_trim = int(v.value,16) |
dkato | 0:f782d9c66c49 | 169 | elif (v.name == "target.rssi-trim"): |
dkato | 0:f782d9c66c49 | 170 | rssi = int(v.value,16) |
dkato | 0:f782d9c66c49 | 171 | elif (v.name == "target.txtune-trim"): |
dkato | 0:f782d9c66c49 | 172 | txtune = int(v.value,16) |
dkato | 0:f782d9c66c49 | 173 | else: |
dkato | 0:f782d9c66c49 | 174 | print("Not a valid param") |
dkato | 0:f782d9c66c49 | 175 | |
dkato | 0:f782d9c66c49 | 176 | output_hex_file[trim_area_start + 0] = mac_addr_low & 0xFF |
dkato | 0:f782d9c66c49 | 177 | output_hex_file[trim_area_start + 1] = (mac_addr_low >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 178 | output_hex_file[trim_area_start + 2] = (mac_addr_low >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 179 | output_hex_file[trim_area_start + 3] = (mac_addr_low >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 180 | |
dkato | 0:f782d9c66c49 | 181 | output_hex_file[trim_area_start + 4] = mac_addr_high & 0xFF |
dkato | 0:f782d9c66c49 | 182 | output_hex_file[trim_area_start + 5] = (mac_addr_high >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 183 | output_hex_file[trim_area_start + 6] = (mac_addr_high >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 184 | output_hex_file[trim_area_start + 7] = (mac_addr_high >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 185 | |
dkato | 0:f782d9c66c49 | 186 | output_hex_file[trim_area_start + 8] = clk_32k_trim & 0xFF |
dkato | 0:f782d9c66c49 | 187 | output_hex_file[trim_area_start + 9] = (clk_32k_trim >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 188 | output_hex_file[trim_area_start + 10] = (clk_32k_trim >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 189 | output_hex_file[trim_area_start + 11] = (clk_32k_trim >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 190 | |
dkato | 0:f782d9c66c49 | 191 | output_hex_file[trim_area_start + 12] = clk_32m_trim & 0xFF |
dkato | 0:f782d9c66c49 | 192 | output_hex_file[trim_area_start + 13] = (clk_32m_trim >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 193 | output_hex_file[trim_area_start + 14] = (clk_32m_trim >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 194 | output_hex_file[trim_area_start + 15] = (clk_32m_trim >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 195 | |
dkato | 0:f782d9c66c49 | 196 | output_hex_file[trim_area_start + 16] = rssi & 0xFF |
dkato | 0:f782d9c66c49 | 197 | output_hex_file[trim_area_start + 17] = (rssi >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 198 | output_hex_file[trim_area_start + 18] = (rssi >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 199 | output_hex_file[trim_area_start + 19] = (rssi >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 200 | |
dkato | 0:f782d9c66c49 | 201 | output_hex_file[trim_area_start + 20] = txtune & 0xFF |
dkato | 0:f782d9c66c49 | 202 | output_hex_file[trim_area_start + 21] = (txtune >> 8) & 0xFF |
dkato | 0:f782d9c66c49 | 203 | output_hex_file[trim_area_start + 22] = (txtune >> 16) & 0xFF |
dkato | 0:f782d9c66c49 | 204 | output_hex_file[trim_area_start + 23] = (txtune >> 24) & 0xFF |
dkato | 0:f782d9c66c49 | 205 | |
dkato | 0:f782d9c66c49 | 206 | # pad the rest of the area with 0xFF |
dkato | 0:f782d9c66c49 | 207 | for i in range(trim_area_start + trim_size, user_code_start): |
dkato | 0:f782d9c66c49 | 208 | output_hex_file[i] = 0xFF |
dkato | 0:f782d9c66c49 | 209 | |
dkato | 0:f782d9c66c49 | 210 | #merge two hex files |
dkato | 0:f782d9c66c49 | 211 | output_hex_file.merge(input_hex_file, overlap='error') |
dkato | 0:f782d9c66c49 | 212 | |
dkato | 0:f782d9c66c49 | 213 | # Write out file(s) |
dkato | 0:f782d9c66c49 | 214 | output_hex_file.tofile(file_name_hex, 'hex') |
dkato | 0:f782d9c66c49 | 215 | output_hex_file.tofile(file_name_bin, 'bin') |