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 mbed SDK
bryantaylor 0:eafc3fd41f75 3 Copyright (c) 2011-2013 ARM Limited
bryantaylor 0:eafc3fd41f75 4
bryantaylor 0:eafc3fd41f75 5 Licensed under the Apache License, Version 2.0 (the "License");
bryantaylor 0:eafc3fd41f75 6 you may not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 7 You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 8
bryantaylor 0:eafc3fd41f75 9 http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 10
bryantaylor 0:eafc3fd41f75 11 Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 12 distributed under the License is distributed on an "AS IS" BASIS,
bryantaylor 0:eafc3fd41f75 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 14 See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 15 limitations under the License.
bryantaylor 0:eafc3fd41f75 16
bryantaylor 0:eafc3fd41f75 17
bryantaylor 0:eafc3fd41f75 18 http://www.nxp.com/documents/user_manual/UM10360.pdf
bryantaylor 0:eafc3fd41f75 19
bryantaylor 0:eafc3fd41f75 20 32.3.1.1 Criterion for Valid User Code
bryantaylor 0:eafc3fd41f75 21 The reserved Cortex-M3 exception vector location 7 (offset 0x1C in the vector table)
bryantaylor 0:eafc3fd41f75 22 should contain the 2's complement of the check-sum of table entries 0 through 6. This
bryantaylor 0:eafc3fd41f75 23 causes the checksum of the first 8 table entries to be 0. The boot loader code checksums
bryantaylor 0:eafc3fd41f75 24 the first 8 locations in sector 0 of the flash. If the result is 0, then execution control is
bryantaylor 0:eafc3fd41f75 25 transferred to the user code.
bryantaylor 0:eafc3fd41f75 26 """
bryantaylor 0:eafc3fd41f75 27 from struct import unpack, pack
bryantaylor 0:eafc3fd41f75 28
bryantaylor 0:eafc3fd41f75 29
bryantaylor 0:eafc3fd41f75 30 def patch(bin_path):
bryantaylor 0:eafc3fd41f75 31 with open(bin_path, 'r+b') as bin:
bryantaylor 0:eafc3fd41f75 32 # Read entries 0 through 6 (Little Endian 32bits words)
bryantaylor 0:eafc3fd41f75 33 vector = [unpack('<I', bin.read(4))[0] for _ in range(7)]
bryantaylor 0:eafc3fd41f75 34
bryantaylor 0:eafc3fd41f75 35 # location 7 (offset 0x1C in the vector table) should contain the 2's
bryantaylor 0:eafc3fd41f75 36 # complement of the check-sum of table entries 0 through 6
bryantaylor 0:eafc3fd41f75 37 bin.seek(0x1C)
bryantaylor 0:eafc3fd41f75 38 bin.write(pack('<I', (~sum(vector) + 1) & 0xFFFFFFFF))
bryantaylor 0:eafc3fd41f75 39
bryantaylor 0:eafc3fd41f75 40
bryantaylor 0:eafc3fd41f75 41 def is_patched(bin_path):
bryantaylor 0:eafc3fd41f75 42 with open(bin_path, 'rb') as bin:
bryantaylor 0:eafc3fd41f75 43 # The checksum of the first 8 table entries should be 0
bryantaylor 0:eafc3fd41f75 44 return (sum([unpack('<I', bin.read(4))[0] for _ in range(8)]) & 0xFFFFFFFF) == 0
bryantaylor 0:eafc3fd41f75 45
bryantaylor 0:eafc3fd41f75 46
bryantaylor 0:eafc3fd41f75 47 if __name__ == '__main__':
bryantaylor 0:eafc3fd41f75 48 bin_path = "C:/Users/emimon01/releases/emilmont/build/test/LPC1768/ARM/MBED_A1/basic.bin"
bryantaylor 0:eafc3fd41f75 49 patch(bin_path)
bryantaylor 0:eafc3fd41f75 50 assert is_patched(bin_path), "The file is not patched"