Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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