BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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