You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2020/01/07 20:22:37 UTC

[GitHub] [mynewt-core] utzig commented on a change in pull request #2144: OTP Tool: Support BLE Addr and HWID

utzig commented on a change in pull request #2144: OTP Tool: Support BLE Addr and HWID
URL: https://github.com/apache/mynewt-core/pull/2144#discussion_r363934099
 
 

 ##########
 File path: hw/bsp/dialog_da1469x-dk-pro/otp_tool.py
 ##########
 @@ -605,6 +615,141 @@ def test_alive_target(uart):
         SystemExit("Successfully communicated with target")
 
 
+@click.option('-u', '--uart', required=True, help='uart port')
+@click.command(help='Read the BLE Address of the device')
+def otp_read_ble_addr(uart):
+    try:
+        ser = serial.Serial(port=uart, baudrate=1000000, timeout=1,
+                            bytesize=8, stopbits=serial.STOPBITS_ONE)
+    except serial.SerialException:
+        raise SystemExit("Failed to open serial port")
+
+    cmd = cmd_no_payload(0xaa55aa55, Cmd.READ_BLEADDR)
+    msg = struct.pack('II', *cmd)
+
+    try:
+        ser.write(msg)
+    except serial.SerialException:
+        raise SystemExit("Failed to write to %s" % uart)
+
+    data = read_exact(ser, 16)
+    response = cmd_response._make(struct.unpack_from('IIII', data))
+    if response.status == 0:
+        # BLE Addr is 48 bits long, read as 2 uint32_t values
+        ble_words = struct.unpack_from('II', ser.read(8))
+        print("BLE Addr - Low Word:" + hex(ble_words[0])
+            + " High Word:" + hex(ble_words[1]))
+    else:
+        raise SystemExit("Error reading BLE Addr with status %s" %
+                         hex(response.status))
+
+
+@click.option('-u', '--uart', required=True, help='uart port')
+@click.command(help='Generate and program the BLE Address of the device')
+def otp_write_ble_addr(uart):
+    try:
+        ser = serial.Serial(port=uart, baudrate=1000000, timeout=1,
+                            bytesize=8, stopbits=serial.STOPBITS_ONE)
+    except serial.SerialException:
+        raise SystemExit("Failed to open serial port")
+
+    cmd = cmd_no_payload(0xaa55aa55, Cmd.GEN_BLEADDR)
+    msg = struct.pack('II', *cmd)
+
+    try:
+        ser.write(msg)
+    except serial.SerialException:
+        raise SystemExit("Failed to write %s" % uart)
+
+    data = read_exact(ser, 16)
+    response = cmd_response._make(struct.unpack_from('IIII', data))
+    if (response.status == 0 or response.status == 1):
+        ble_words = struct.unpack_from('II', ser.read(8))
+        if response.status == 0:
+            print("Newly programmed BLE Address")
+        elif response.status == 1:
+            print("Previously programmed")
+        print("BLE Addr - Low Word:" + hex(ble_words[0])
+            + " High Word:" + hex(ble_words[1]))
+    else:
+        raise SystemExit("Error generating/programming BLE Address")
+
+
+@click.option('-u', '--uart', required=True, help='uart port')
+@click.command(help='Read the HWID of the device')
+def otp_read_hwid(uart):
+    try:
+        ser = serial.Serial(port=uart, baudrate=1000000, timeout=1,
+                            bytesize=8, stopbits=serial.STOPBITS_ONE)
+    except serial.SerialException:
+        raise SystemExit("Failed to open serial port")
+
+    cmd = cmd_no_payload(0xaa55aa55, Cmd.READ_HWID)
+    msg = struct.pack('II', *cmd)
+
+    try:
+        ser.write(msg)
+    except serial.SerialException:
+        raise SystemExit("Failed to write to %s" % uart)
+
+    data = read_exact(ser, 16)
+    response = cmd_response._make(struct.unpack_from('IIII', data))
+
+    if response.status == 0:
+        ble_addr = ser.read(16)
+        print("HWID:" + ble_addr.hex())
+    else:
+        raise SystemExit("Error reading HWID with status %s" %
+                         hex(response.status))
+
+
+@click.argument('infile')
+@click.option('-u', '--uart', required=True, help='uart port')
+@click.command(help='Write hardware ID into OTP')
+def otp_write_hwid(infile, uart):
+    hwid = bytearray()
+    hwid_len = 10
+    try:
+        with open(infile, "rb") as f:
+                # read hwid file
+                buf = f.read()
+
+                # Expect 10 byte HWID
+                if len(buf) != 10:
+                    raise SystemExit("HWID has incorrect length")
 
 Review comment:
   You don't need to wrap the context manager inside a `try` block, just use "Failed to read key from file" here and remove the `except` below.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services