MCU / 软件积累 · 2023年7月31日

Nordic生产数据设置

Nordic生产数据设置

MAC地址写入及关闭调试功能

""" 
    Program to flash firmware and key to NRF52 based tracker board
"""

from __future__ import print_function
import argparse
import os
import csv
import sys
import time
print(os.urandom(16))
# Import pynrfjprog API module
try:
    from .. import LowLevel, Hex
except Exception:
    from pynrfjprog import LowLevel, Hex


def run(file, snr=None):
    """
    Run example script.

    @param (optional) int snr: Specify serial number of DK to run example on.
    """

    # Initialize an API object with the target family. This will load nrfjprog.dll with the proper target family.
    api = LowLevel.API(LowLevel.DeviceFamily.UNKNOWN)
    
    # Open the loaded DLL and connect to an emulator probe. If several are connected a pop up will appear.
    api.open()
    try:

        if snr is not None:
            api.connect_to_emu_with_snr(snr)
        else:
            api.connect_to_emu_without_snr()
		
        api.select_family(api.read_device_family())
        
        device_version = api.read_device_version()
        hex_file_path = os.path.abspath(file)

        if hex_file_path is None:
            api.close()
            raise Exception("Could not find example binary for device " + device_version.lower() + ".\n" +
                            "This example may not support your device yet.")
							
        # Erase all the flash of the device.
        api.erase_all()
        api.erase_uicr()
		
        # Parse the hex file with the help of the HEX module
        test_program = Hex.Hex(hex_file_path)

        # Program the parsed hex into the device's memory.
        print('Writing %s to device.' % hex_file_path)
        for segment in test_program:
            api.write(segment.address, segment.data, True)


        # Parts
        int1 = int.from_bytes(os.urandom(4), "little")
        int2 = int.from_bytes(os.urandom(4), "little")
        int3 = int.from_bytes(os.urandom(4), "little")
        int4 = int.from_bytes(os.urandom(4), "little")
        #print('INT rep : ', int1, int2, int3, int4)
        #print('HEX rep : ', hex(int1), hex(int2), hex(int3), hex(int4))

        # Write to addresses 0x10001080 -0x1000108F
        print('Writing key to device.')
        api.write_u32(0x10001080, int1, True)
        api.write_u32(0x10001084, int2, True)
        api.write_u32(0x10001088, int3, True)
        api.write_u32(0x1000108C, int4, True)
        #api.write(0x10001080, int1, True)
        #api.write(0x10001084, int2, True)
        #api.write(0x10001088, int3, True)
        #api.write(0x1000108C, int4, True)

        # Read from addresses 0x10001080 to 0x1000108F and print the result.
        # print('Reading memory address 0x10001080 to 0x1000108F..')
        a1 = hex(api.read_u32(0x10001080))
        a2 = hex(api.read_u32(0x10001084))
        a3 = hex(api.read_u32(0x10001088))
        a4 = hex(api.read_u32(0x1000108C))
        #print('Read key Int: ',  api.read_u32(0x10001080), api.read_u32(0x10001084), api.read_u32(0x10001088), api.read_u32(0x1000108C))
        print('Read key Hex2: ', (a1[2:]).rjust(8, '0').upper(), a2[2:].rjust(8, '0').upper(), a3[2:].rjust(8, '0').upper(), a4[2:].rjust(8, '0').upper(),sep='')
        a_list = (api.read(0x100000A4, 6))
        m1 = hex(a_list[5]|0xC0)
        m2 = hex(a_list[4])
        m3 = hex(a_list[3])
        m4 = hex(a_list[2])
        m5 = hex(a_list[1])
        m6 = hex(a_list[0])
        print("Read MAC Address - " + m1[2:]+":"+ m2[2:]+":"+ m3[2:]+":"+m4[2:]+":"+m5[2:]+":"+m6[2:])
        f = open('datafile.csv', 'a')
        original_stdout = sys.stdout
		
		# Change standard output to file
        sys.stdout = f 
        print('', m1[2:]+":"+ m2[2:]+":"+ m3[2:]+":"+m4[2:]+":"+m5[2:]+":"+m6[2:],
            ',', (a1[2:]).rjust(8, '0').upper(), a2[2:].rjust(8, '0').upper(), a3[2:].rjust(8, '0').upper(), a4[2:].rjust(8, '0').upper(),sep='')
        f.close()
		
		# Change standard output to stdout
        sys.stdout = original_stdout
        
        #print('Wait device restart!')
        #time.sleep(2)
		
		#Protect
        api.write_u32(0x10001208, 0x00000000, True)
        api.write_u32(0x10001210, 0xFFFF0000, True)

        #Reset
        api.sys_reset()
        #api.go()
		
        # Close the loaded DLL to free resources.
        api.close()

        print('Firmware flash done. Restart device to test!')

    except LowLevel.APIError:
        api.close()
        raise
    
 
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--file',  help="F/W file path", required=True)
    args = parser.parse_args()
	
    run(args.file)

打赏作者