| 1 |
# This file is part of the MicroPython project, http://micropython.org/ |
| 2 |
# |
| 3 |
# The MIT License (MIT) |
| 4 |
# |
| 5 |
# Copyright (c) 2020 Damien P. George |
| 6 |
# Copyright (c) 2020 Jim Mussared |
| 7 |
# |
| 8 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
# of this software and associated documentation files (the "Software"), to deal |
| 10 |
# in the Software without restriction, including without limitation the rights |
| 11 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
# copies of the Software, and to permit persons to whom the Software is |
| 13 |
# furnished to do so, subject to the following conditions: |
| 14 |
# |
| 15 |
# The above copyright notice and this permission notice shall be included in |
| 16 |
# all copies or substantial portions of the Software. |
| 17 |
# |
| 18 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
# THE SOFTWARE. |
| 25 |
|
| 26 |
# This script provides some helpers to allow Python code to access the IPCC |
| 27 |
# mechanism in the WB55, and works with the memory layout configured in |
| 28 |
# ports/stm32/rfcore.c -- i.e. it expects that rfcore_init() has been run. |
| 29 |
|
| 30 |
# e.g. |
| 31 |
# ../../tools/pyboard.py --device /dev/ttyACM0 boards/NUCLEO_WB55/rfcore.py |
| 32 |
# to print out SRAM2A, register state and FUS/WS info. |
| 33 |
# |
| 34 |
# The `stm` module provides some helper functions to access rfcore functionality. |
| 35 |
# See rfcore_firmware.py for more information. |
| 36 |
|
| 37 |
from machine import mem8, mem16, mem32 |
| 38 |
from micropython import const |
| 39 |
import stm |
| 40 |
|
| 41 |
SRAM2A_BASE = const(0x2003_0000) |
| 42 |
|
| 43 |
# for vendor OGF |
| 44 |
OGF_VENDOR = const(0x3F) |
| 45 |
OCF_FUS_GET_STATE = const(0x52) |
| 46 |
OCF_FUS_FW_UPGRADE = const(0x54) |
| 47 |
OCF_FUS_FW_DELETE = const(0x55) |
| 48 |
OCF_FUS_START_WS = const(0x5A) |
| 49 |
OCF_BLE_INIT = const(0x66) |
| 50 |
|
| 51 |
TABLE_DEVICE_INFO = const(0) |
| 52 |
TABLE_BLE = const(1) |
| 53 |
TABLE_SYS = const(3) |
| 54 |
TABLE_MEM_MANAGER = const(4) |
| 55 |
|
| 56 |
CHANNEL_BLE = const(1) |
| 57 |
CHANNEL_SYS = const(2) |
| 58 |
CHANNEL_TRACES = const(4) |
| 59 |
CHANNEL_ACL = const(6) |
| 60 |
|
| 61 |
INDICATOR_HCI_COMMAND = const(0x01) |
| 62 |
INDICATOR_HCI_EVENT = const(0x04) |
| 63 |
INDICATOR_FUS_COMMAND = const(0x10) |
| 64 |
INDICATOR_FUS_RESPONSE = const(0x11) |
| 65 |
INDICATOR_FUS_EVENT = const(0x12) |
| 66 |
|
| 67 |
MAGIC_FUS_ACTIVE = const(0xA94656B9) |
| 68 |
|
| 69 |
|
| 70 |
def get_ipccdba(): |
| 71 |
return mem32[stm.FLASH + stm.FLASH_IPCCBR] & 0x3FFF |
| 72 |
|
| 73 |
|
| 74 |
def get_ipcc_table(table): |
| 75 |
return mem32[SRAM2A_BASE + get_ipccdba() + table * 4] |
| 76 |
|
| 77 |
|
| 78 |
def get_ipcc_table_word(table, offset): |
| 79 |
return mem32[get_ipcc_table(table) + offset * 4] & 0xFFFFFFFF |
| 80 |
|
| 81 |
|
| 82 |
def get_ipcc_table_byte(table, offset): |
| 83 |
return mem8[get_ipcc_table(table) + offset] & 0xFF |
| 84 |
|
| 85 |
|
| 86 |
def sram2a_dump(num_words=64, width=8): |
| 87 |
print("SRAM2A @%08x" % SRAM2A_BASE) |
| 88 |
for i in range((num_words + width - 1) // width): |
| 89 |
print(" %04x " % (i * 4 * width), end="") |
| 90 |
for j in range(width): |
| 91 |
print(" %08x" % (mem32[SRAM2A_BASE + (i * width + j) * 4] & 0xFFFFFFFF), end="") |
| 92 |
print() |
| 93 |
|
| 94 |
|
| 95 |
SYS_CMD_BUF = 0 # next*,prev*,type8,...; 272 bytes |
| 96 |
SYS_SYS_QUEUE = 0 # next*,prev* |
| 97 |
|
| 98 |
MM_BLE_SPARE_EVT_BUF = 0 # next*,prev*; 272 bytes |
| 99 |
MM_SYS_SPARE_EVT_BUF = 0 # next*,prev*; 272 bytes |
| 100 |
MM_BLE_POOL = 0 # ? |
| 101 |
MM_BLE_POOL_SIZE = 0 # ? |
| 102 |
MM_FREE_BUF_QUEUE = 0 # next*,prev* |
| 103 |
MM_EV_POOL = 0 # ? |
| 104 |
MM_EV_POOL_SIZE = 0 # ? |
| 105 |
|
| 106 |
BLE_CMD_BUF = 0 |
| 107 |
BLE_CS_BUF = 0 |
| 108 |
BLE_EVT_QUEUE = 0 |
| 109 |
BLE_HCI_ACL_DATA_BUF = 0 |
| 110 |
|
| 111 |
|
| 112 |
def ipcc_init(): |
| 113 |
global SYS_CMD_BUF, SYS_SYS_QUEUE |
| 114 |
SYS_CMD_BUF = get_ipcc_table_word(TABLE_SYS, 0) |
| 115 |
SYS_SYS_QUEUE = get_ipcc_table_word(TABLE_SYS, 1) |
| 116 |
|
| 117 |
global \ |
| 118 |
MM_BLE_SPARE_EVT_BUF, \ |
| 119 |
MM_SYS_SPARE_EVT_BUF, \ |
| 120 |
MM_BLE_POOL, \ |
| 121 |
MM_BLE_POOL_SIZE, \ |
| 122 |
MM_FREE_BUF_QUEUE, \ |
| 123 |
MM_EV_POOL, \ |
| 124 |
MM_EV_POOL_SIZE |
| 125 |
MM_BLE_SPARE_EVT_BUF = get_ipcc_table_word(TABLE_MEM_MANAGER, 0) |
| 126 |
MM_SYS_SPARE_EVT_BUF = get_ipcc_table_word(TABLE_MEM_MANAGER, 1) |
| 127 |
MM_BLE_POOL = get_ipcc_table_word(TABLE_MEM_MANAGER, 2) |
| 128 |
MM_BLE_POOL_SIZE = get_ipcc_table_word(TABLE_MEM_MANAGER, 3) |
| 129 |
MM_FREE_BUF_QUEUE = get_ipcc_table_word(TABLE_MEM_MANAGER, 4) |
| 130 |
MM_EV_POOL = get_ipcc_table_word(TABLE_MEM_MANAGER, 5) |
| 131 |
MM_EV_POOL_SIZE = get_ipcc_table_word(TABLE_MEM_MANAGER, 6) |
| 132 |
|
| 133 |
global BLE_CMD_BUF, BLE_CS_BUF, BLE_EVT_QUEUE, BLE_HCI_ACL_DATA_BUF |
| 134 |
BLE_CMD_BUF = get_ipcc_table_word(TABLE_BLE, 0) |
| 135 |
BLE_CS_BUF = get_ipcc_table_word(TABLE_BLE, 1) |
| 136 |
BLE_EVT_QUEUE = get_ipcc_table_word(TABLE_BLE, 2) |
| 137 |
BLE_HCI_ACL_DATA_BUF = get_ipcc_table_word(TABLE_BLE, 3) |
| 138 |
|
| 139 |
# Disable interrupts, the code here uses polling |
| 140 |
mem32[stm.IPCC + stm.IPCC_C1CR] = 0 |
| 141 |
|
| 142 |
print("IPCC initialised") |
| 143 |
print("SYS: 0x%08x 0x%08x" % (SYS_CMD_BUF, SYS_SYS_QUEUE)) |
| 144 |
print("BLE: 0x%08x 0x%08x 0x%08x" % (BLE_CMD_BUF, BLE_CS_BUF, BLE_EVT_QUEUE)) |
| 145 |
|
| 146 |
|
| 147 |
def fus_active(): |
| 148 |
return get_ipcc_table_word(TABLE_DEVICE_INFO, 0) == MAGIC_FUS_ACTIVE |
| 149 |
|
| 150 |
|
| 151 |
def info(): |
| 152 |
sfr = mem32[stm.FLASH + stm.FLASH_SFR] |
| 153 |
srrvr = mem32[stm.FLASH + stm.FLASH_SRRVR] |
| 154 |
|
| 155 |
print("IPCCDBA : 0x%08x" % (get_ipccdba() & 0x3FFF)) |
| 156 |
print("DDS : %r" % bool(sfr & (1 << 12))) |
| 157 |
print("FSD : %r" % bool(sfr & (1 << 8))) |
| 158 |
print("SFSA : 0x%08x" % (sfr & 0xFF)) |
| 159 |
print("C2OPT : %r" % bool(srrvr & (1 << 31))) |
| 160 |
print("NBRSD : %r" % bool(srrvr & (1 << 30))) |
| 161 |
print("SNBRSA : 0x%08x" % ((srrvr >> 25) & 0x1F)) |
| 162 |
print("BRSD : %r" % bool(srrvr & (1 << 23))) |
| 163 |
print("SBRSA : 0x%08x" % ((srrvr >> 18) & 0x1F)) |
| 164 |
print("SBRV : 0x%08x" % (srrvr & 0x3FFFF)) |
| 165 |
|
| 166 |
|
| 167 |
def dev_info(): |
| 168 |
def dump_version(offset): |
| 169 |
x = get_ipcc_table_word(TABLE_DEVICE_INFO, offset) |
| 170 |
print( |
| 171 |
"0x%08x (%u.%u.%u.%u.%u)" |
| 172 |
% (x, x >> 24, x >> 16 & 0xFF, x >> 8 & 0xFF, x >> 4 & 0xF, x & 0xF) |
| 173 |
) |
| 174 |
|
| 175 |
def dump_memory_size(offset): |
| 176 |
x = get_ipcc_table_word(TABLE_DEVICE_INFO, offset) |
| 177 |
print( |
| 178 |
"0x%08x (SRAM2b=%uk SRAM2a=%uk flash=%uk)" |
| 179 |
% (x, x >> 24, x >> 16 & 0xFF, (x & 0xFF) * 4) |
| 180 |
) |
| 181 |
|
| 182 |
print("Device information table @%08x:" % get_ipcc_table(TABLE_DEVICE_INFO)) |
| 183 |
if fus_active(): |
| 184 |
# layout when running FUS |
| 185 |
print("FUS is active") |
| 186 |
print("state : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 0)) |
| 187 |
print("last FUS active state : 0x%02x" % get_ipcc_table_byte(TABLE_DEVICE_INFO, 5)) |
| 188 |
print("last wireless stack state: 0x%02x" % get_ipcc_table_byte(TABLE_DEVICE_INFO, 6)) |
| 189 |
print("cur wireless stack type : 0x%02x" % get_ipcc_table_byte(TABLE_DEVICE_INFO, 7)) |
| 190 |
print("safe boot version : ", end="") |
| 191 |
dump_version(2) |
| 192 |
print("FUS version : ", end="") |
| 193 |
dump_version(3) |
| 194 |
print("FUS memory size : ", end="") |
| 195 |
dump_memory_size(4) |
| 196 |
print("wireless stack version : ", end="") |
| 197 |
dump_version(5) |
| 198 |
print("wireless stack mem size : ", end="") |
| 199 |
dump_memory_size(6) |
| 200 |
print("wireless FW-BLE info : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 7)) |
| 201 |
print("wireless FW-thread info : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 8)) |
| 202 |
print( |
| 203 |
"UID64 : 0x%08x 0x%08x" |
| 204 |
% ( |
| 205 |
get_ipcc_table_word(TABLE_DEVICE_INFO, 9), |
| 206 |
get_ipcc_table_word(TABLE_DEVICE_INFO, 10), |
| 207 |
) |
| 208 |
) |
| 209 |
print("device ID : 0x%04x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 11)) |
| 210 |
else: |
| 211 |
# layout when running WS |
| 212 |
print("WS is active") |
| 213 |
print("safe boot version : ", end="") |
| 214 |
dump_version(0) |
| 215 |
print("FUS version : ", end="") |
| 216 |
dump_version(1) |
| 217 |
print("FUS memory size : ", end="") |
| 218 |
dump_memory_size(2) |
| 219 |
print("FUS info : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 3)) |
| 220 |
print("wireless stack version : ", end="") |
| 221 |
dump_version(4) |
| 222 |
print("wireless stack mem size : ", end="") |
| 223 |
dump_memory_size(5) |
| 224 |
print("wireless stack info : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 7)) |
| 225 |
print("wireless reserved : 0x%08x" % get_ipcc_table_word(TABLE_DEVICE_INFO, 7)) |
| 226 |
|
| 227 |
|
| 228 |
def ipcc_state(): |
| 229 |
print("IPCC:") |
| 230 |
print(" C1CR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C1CR] & 0xFFFFFFFF), end="") |
| 231 |
print(" C2CR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C2CR] & 0xFFFFFFFF)) |
| 232 |
print(" C1MR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C1MR] & 0xFFFFFFFF), end="") |
| 233 |
print(" C2MR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C2MR] & 0xFFFFFFFF)) |
| 234 |
# these always read 0 |
| 235 |
# print(' C1SCR: 0x%08x' % (mem32[stm.IPCC + stm.IPCC_C1SCR] & 0xffffffff), end='') |
| 236 |
# print(' C2SCR: 0x%08x' % (mem32[stm.IPCC + stm.IPCC_C2SCR] & 0xffffffff)) |
| 237 |
print(" C1TOC2SR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C1TOC2SR] & 0xFFFFFFFF), end="") |
| 238 |
print(" C2TOC1SR: 0x%08x" % (mem32[stm.IPCC + stm.IPCC_C2TOC1SR] & 0xFFFFFFFF)) |
| 239 |
|
| 240 |
|
| 241 |
sram2a_dump(264) |
| 242 |
ipcc_init() |
| 243 |
info() |
| 244 |
dev_info() |
| 245 |
ipcc_state() |