the other jimmy / mbed-sdk-tools

Fork of mbed-sdk-tools by mbed official

Committer:
The Other Jimmy
Date:
Wed Jan 04 11:58:24 2017 -0600
Revision:
32:8ea194f6145b
Update tools to follow mbed-os tools release 5.3.1

Who changed what in which revision?

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